//////////////////////////////////////////////////////////////////////////////
// File:        sourceeditctrl.cpp
// Purpose:     wxPackageSourceEditCtrl
// Author:      Francesco Montorsi
// Modified by:
// Created:     2006-06-08
// RCS-ID:      $Id: sourceeditctrl.cpp,v 1.1.1.1 2006/12/12 09:38:27 frm Exp $
// Copyright:   (c) Francesco Montorsi
// Licence:     wxWidgets licence
//////////////////////////////////////////////////////////////////////////////


// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all 'standard' wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "wx/filename.h"
#include "guipkg/sourceeditctrl.h"        // edit module



//----------------------------------------------------------------------------
// wxPackageSourceEditCtrl
//----------------------------------------------------------------------------

BEGIN_EVENT_TABLE(wxPackageSourceEditCtrl, wxStyledTextCtrl)
#if ENABLE_EDIT_MODE
    // edit
    EVT_MENU(wxID_CLEAR,              wxPackageSourceEditCtrl::OnEditClear)
    EVT_MENU(wxID_CUT,                wxPackageSourceEditCtrl::OnEditCut)
    EVT_MENU(wxID_COPY,               wxPackageSourceEditCtrl::OnEditCopy)
    EVT_MENU(wxID_PASTE,              wxPackageSourceEditCtrl::OnEditPaste)
    EVT_MENU(ID_SEC_INDENTINC,        wxPackageSourceEditCtrl::OnEditIndentInc)
    EVT_MENU(ID_SEC_INDENTRED,        wxPackageSourceEditCtrl::OnEditIndentRed)
    EVT_MENU(wxID_SELECTALL,          wxPackageSourceEditCtrl::OnEditSelectAll)
    EVT_MENU(wxID_REDO,               wxPackageSourceEditCtrl::OnEditRedo)
    EVT_MENU(wxID_UNDO,               wxPackageSourceEditCtrl::OnEditUndo)

    // find
    EVT_MENU(wxID_FIND,               wxPackageSourceEditCtrl::OnFind)
    EVT_MENU(ID_SEC_FINDNEXT,         wxPackageSourceEditCtrl::OnFindNext)
    EVT_MENU(ID_SEC_REPLACE,          wxPackageSourceEditCtrl::OnReplace)
    EVT_MENU(ID_SEC_REPLACENEXT,      wxPackageSourceEditCtrl::OnReplaceNext)

    // view
    EVT_MENU(ID_SEC_DISPLAYEOL,       wxPackageSourceEditCtrl::OnDisplayEOL)
    EVT_MENU(ID_SEC_LINENUMBER,       wxPackageSourceEditCtrl::OnLineNumber)
    EVT_MENU(ID_SEC_LONGLINEON,       wxPackageSourceEditCtrl::OnLongLineOn)
    EVT_MENU(ID_SEC_WHITESPACE,       wxPackageSourceEditCtrl::OnWhiteSpace)
    EVT_MENU(ID_SEC_WRAPMODEON,       wxPackageSourceEditCtrl::OnWrapmodeOn)

    // stc
    EVT_STC_MARGINCLICK(wxID_ANY,     wxPackageSourceEditCtrl::OnMarginClick)
    EVT_STC_CHARADDED(wxID_ANY,       wxPackageSourceEditCtrl::OnCharAdded)
#endif
END_EVENT_TABLE()


bool wxPackageSourceEditCtrl::Create(wxWindow *parent, wxWindowID id,
                                     const wxPoint &pos, const wxSize &size,
                                     long style)
{
    if (!wxStyledTextCtrl::Create(parent, id, pos, size, style))
        return false;

    // the lexer is fixed to XML
    SetLexer(wxSTC_LEX_XML);

    // when wrapping is off, a line will be drawn at 80-th column
    SetEdgeColumn(80);
    EnableWrapMode(false);

    // some defaults
    SetDefaultStyle();
    UpdateScrollWidth();

    return true;
}

void wxPackageSourceEditCtrl::SetDefaultStyle()
{
    // as font use a monospaced font
    m_font = wxFont(9, wxMODERN, wxNORMAL, wxNORMAL);
    StyleSetFont(wxSTC_STYLE_DEFAULT, m_font);

    // set some nice colours for the XML lexer
    StyleSetForeground(wxSTC_H_TAG, wxColour(0,0,128));
    StyleSetForeground(wxSTC_H_ATTRIBUTE, wxColour(0,128,128));
    StyleSetForeground(wxSTC_H_COMMENT, wxColour(128,128,0));
    StyleSetForeground(wxSTC_H_TAGUNKNOWN, wxColour(255,0,0));
    StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, wxColour(255,0,0));
    StyleSetForeground(wxSTC_H_DOUBLESTRING, wxColour(127,0,127));
    StyleSetForeground(wxSTC_H_SINGLESTRING, wxColour(127,0,127));

/*
#define wxSTC_H_DEFAULT 0
#define wxSTC_H_TAG 1
#define wxSTC_H_TAGUNKNOWN 2
#define wxSTC_H_ATTRIBUTE 3
#define wxSTC_H_ATTRIBUTEUNKNOWN 4
#define wxSTC_H_NUMBER 5
#define wxSTC_H_DOUBLESTRING 6
#define wxSTC_H_SINGLESTRING 7
#define wxSTC_H_OTHER 8
#define wxSTC_H_COMMENT 9
#define wxSTC_H_ENTITY 10
*/
}

void wxPackageSourceEditCtrl::UpdateScrollWidth()
{
    if (GetWrapMode() == wxSTC_WRAP_WORD)
        return;       // we have no scrollbars to update

    // compute the width of the longest line of text we contain
    // NOTE: this is more optimized than using wxDC::GetMultiLineTextExtent
    //       which also computes the height of the given block
    size_t max = 0;
    for (int i=0; i<GetLineCount(); i++)
    {
        size_t len = LineLength(i);
        max = wxMax(len, max);
    }

    // get the lenght in pixels
    wxWindowDC dc(this);
    dc.SetFont(m_font);
    SetScrollWidth(dc.GetCharWidth() * (max+1));
}

void wxPackageSourceEditCtrl::EnableWrapMode(bool enable)
{
    SetWrapMode(enable ? wxSTC_WRAP_WORD : wxSTC_WRAP_NONE);
    SetEdgeMode(!enable ? wxSTC_EDGE_LINE : wxSTC_EDGE_NONE);
}



// ----------------------------------------------------------------------------
// wxPackageSourceEditCtrl - event handlers
// ----------------------------------------------------------------------------

