X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0e974385f6ab66910ee42ca287fbf9ada2392dc1..d5cc191c9cd48a915ce0dfc6e49511b2fddb6746:/samples/stc/stctest.cpp diff --git a/samples/stc/stctest.cpp b/samples/stc/stctest.cpp index 5331ae5114..8db40419ef 100644 --- a/samples/stc/stctest.cpp +++ b/samples/stc/stctest.cpp @@ -1,5 +1,5 @@ ////////////////////////////////////////////////////////////////////////////// -// File: app.cpp +// File: contrib/samples/stc/stctest.cpp // Purpose: STC test application // Maintainer: Otto Wyss // Created: 2003-09-01 @@ -12,60 +12,59 @@ // headers //---------------------------------------------------------------------------- -// For compilers that support precompilation, includes . -#include +// 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' wxWindows headers) +// need because it includes almost all 'standard' wxWidgets headers) #ifndef WX_PRECOMP - #include + #include "wx/wx.h" #endif -//! wxWindows headers -#include // configuration support -#include // file dialog support -#include // filename support -#include // notebook support -#include // system settings -#include // strings support -#include // images support +//! wxWidgets headers +#include "wx/config.h" // configuration support +#include "wx/filedlg.h" // file dialog support +#include "wx/filename.h" // filename support +#include "wx/notebook.h" // notebook support +#include "wx/settings.h" // system settings +#include "wx/string.h" // strings support +#include "wx/image.h" // images support //! application headers #include "defsext.h" // Additional definitions #include "edit.h" // Edit module #include "prefs.h" // Prefs - //---------------------------------------------------------------------------- // resources //---------------------------------------------------------------------------- // the application icon (under Windows and OS/2 it is in resources) -#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__) - #include "mondrian.xpm" +#ifndef wxHAS_IMAGES_IN_RESOURCES + #include "../sample.xpm" #endif //============================================================================ // declarations //============================================================================ -#define APP_NAME _T("STC-Test") -#define APP_DESCR _("See http://wxguide.sourceforge.net/indexedit.html") +#define APP_NAME wxT("STC-Test") +#define APP_DESCR _("See http://wxguide.sourceforge.net/") -#define APP_MAINT _T("Otto Wyss") -#define APP_VENDOR _T("wxWindows") -#define APP_COPYRIGTH _T("(C) 2003 Otto Wyss") -#define APP_LICENCE _T("wxWindows") +#define APP_MAINT wxT("Otto Wyss") +#define APP_VENDOR wxT("wxWidgets") +#define APP_COPYRIGTH wxT("(C) 2003 Otto Wyss") +#define APP_LICENCE wxT("wxWidgets") -#define APP_VERSION _T("0.1.alpha") +#define APP_VERSION wxT("0.1.alpha") #define APP_BUILD __DATE__ -#define APP_WEBSITE _T("http://www.wxWindows.org") -#define APP_MAIL _T("mailto://???") +#define APP_WEBSITE wxT("http://www.wxWidgets.org") +#define APP_MAIL wxT("mailto://???") #define NONAME _("") @@ -76,11 +75,17 @@ class AppBook; //! global application name wxString *g_appname = NULL; +#if wxUSE_PRINTING_ARCHITECTURE + //! global print data, to remember settings during the session wxPrintData *g_printData = (wxPrintData*) NULL; -wxPageSetupData *g_pageSetupData = (wxPageSetupData*) NULL; +wxPageSetupDialogData *g_pageSetupData = (wxPageSetupDialogData*) NULL; + +#endif // wxUSE_PRINTING_ARCHITECTURE +class AppFrame; + //---------------------------------------------------------------------------- //! application APP_VENDOR-APP_NAME. class App: public wxApp { @@ -97,9 +102,13 @@ private: //! frame window AppFrame* m_frame; + wxFrame* MinimalEditor(); +protected: + void OnMinimalEditor(wxCommandEvent&); + DECLARE_EVENT_TABLE() }; -// created dynamically by wxWindows +// created dynamically by wxWidgets DECLARE_APP (App); //---------------------------------------------------------------------------- @@ -184,6 +193,11 @@ private: IMPLEMENT_APP (App) + +BEGIN_EVENT_TABLE(App, wxApp) +EVT_MENU(myID_WINDOW_MINIMAL, App::OnMinimalEditor) +END_EVENT_TABLE() + //---------------------------------------------------------------------------- // App //---------------------------------------------------------------------------- @@ -191,18 +205,20 @@ IMPLEMENT_APP (App) bool App::OnInit () { wxInitAllImageHandlers(); - + // set application and vendor name SetAppName (APP_NAME); SetVendorName (APP_VENDOR); g_appname = new wxString (); g_appname->Append (APP_VENDOR); - g_appname->Append (_T("-")); + g_appname->Append (wxT("-")); g_appname->Append (APP_NAME); +#if wxUSE_PRINTING_ARCHITECTURE // initialize print data and setup g_printData = new wxPrintData; g_pageSetupData = new wxPageSetupDialogData; +#endif // wxUSE_PRINTING_ARCHITECTURE // create application frame m_frame = new AppFrame (*g_appname); @@ -210,7 +226,6 @@ bool App::OnInit () { // open application frame m_frame->Layout (); m_frame->Show (true); - SetTopWindow (m_frame); return true; } @@ -220,9 +235,11 @@ int App::OnExit () { // delete global appname delete g_appname; +#if wxUSE_PRINTING_ARCHITECTURE // delete global print data and setup if (g_printData) delete g_printData; if (g_pageSetupData) delete g_pageSetupData; +#endif // wxUSE_PRINTING_ARCHITECTURE return 0; } @@ -289,28 +306,30 @@ BEGIN_EVENT_TABLE (AppFrame, wxFrame) END_EVENT_TABLE () AppFrame::AppFrame (const wxString &title) - : wxFrame ((wxFrame *)NULL, -1, title, wxDefaultPosition, wxSize(600,400), - wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE) { + : wxFrame ((wxFrame *)NULL, wxID_ANY, title, wxDefaultPosition, wxSize(750,550), + wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE) +{ + SetIcon(wxICON(sample)); - // intitialize important variables + // initialize important variables m_edit = NULL; // set icon and background SetTitle (*g_appname); - SetIcon (wxICON (mondrian)); - SetBackgroundColour (_T("WHITE")); + SetBackgroundColour (wxT("WHITE")); // about box shown for 1 seconds - AppAbout (this, 1000); + AppAbout dlg(this, 1000); // create menu m_menuBar = new wxMenuBar; CreateMenu (); // open first page - m_edit = new Edit (this, -1); + m_edit = new Edit (this, wxID_ANY); m_edit->SetFocus(); + FileOpen (wxT("stctest.cpp")); } AppFrame::~AppFrame () { @@ -328,7 +347,7 @@ void AppFrame::OnClose (wxCloseEvent &event) { } void AppFrame::OnAbout (wxCommandEvent &WXUNUSED(event)) { - AppAbout (this); + AppAbout dlg(this); } void AppFrame::OnExit (wxCommandEvent &WXUNUSED(event)) { @@ -338,12 +357,14 @@ void AppFrame::OnExit (wxCommandEvent &WXUNUSED(event)) { // file event handlers void AppFrame::OnFileOpen (wxCommandEvent &WXUNUSED(event)) { if (!m_edit) return; +#if wxUSE_FILEDLG wxString fname; - wxFileDialog dlg (this, _T("Open file"), _T(""), _T(""), _T("Any file (*)|*"), - wxOPEN | wxFILE_MUST_EXIST | wxCHANGE_DIR); + wxFileDialog dlg (this, wxT("Open file"), wxEmptyString, wxEmptyString, wxT("Any file (*)|*"), + wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR); if (dlg.ShowModal() != wxID_OK) return; fname = dlg.GetPath (); FileOpen (fname); +#endif // wxUSE_FILEDLG } void AppFrame::OnFileSave (wxCommandEvent &WXUNUSED(event)) { @@ -358,11 +379,13 @@ void AppFrame::OnFileSave (wxCommandEvent &WXUNUSED(event)) { void AppFrame::OnFileSaveAs (wxCommandEvent &WXUNUSED(event)) { if (!m_edit) return; +#if wxUSE_FILEDLG wxString filename = wxEmptyString; - wxFileDialog dlg (this, _T("Save file"), _T(""), _T(""), _T("Any file (*)|*"), wxSAVE|wxOVERWRITE_PROMPT); + wxFileDialog dlg (this, wxT("Save file"), wxEmptyString, wxEmptyString, wxT("Any file (*)|*"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT); if (dlg.ShowModal() != wxID_OK) return; filename = dlg.GetPath(); m_edit->SaveFile (filename); +#endif // wxUSE_FILEDLG } void AppFrame::OnFileClose (wxCommandEvent &WXUNUSED(event)) { @@ -378,30 +401,36 @@ void AppFrame::OnFileClose (wxCommandEvent &WXUNUSED(event)) { } } } + m_edit->SetFilename (wxEmptyString); + m_edit->ClearAll(); + m_edit->SetSavePoint(); } // properties event handlers void AppFrame::OnProperties (wxCommandEvent &WXUNUSED(event)) { if (!m_edit) return; - EditProperties (m_edit, 0); + EditProperties dlg(m_edit, 0); } // print event handlers void AppFrame::OnPrintSetup (wxCommandEvent &WXUNUSED(event)) { +#if wxUSE_PRINTING_ARCHITECTURE (*g_pageSetupData) = * g_printData; wxPageSetupDialog pageSetupDialog(this, g_pageSetupData); pageSetupDialog.ShowModal(); (*g_printData) = pageSetupDialog.GetPageSetupData().GetPrintData(); (*g_pageSetupData) = pageSetupDialog.GetPageSetupData(); +#endif // wxUSE_PRINTING_ARCHITECTURE } void AppFrame::OnPrintPreview (wxCommandEvent &WXUNUSED(event)) { +#if wxUSE_PRINTING_ARCHITECTURE wxPrintDialogData printDialogData( *g_printData); wxPrintPreview *preview = new wxPrintPreview (new EditPrint (m_edit), new EditPrint (m_edit), &printDialogData); - if (!preview->Ok()) { + if (!preview->IsOk()) { delete preview; wxMessageBox (_("There was a problem with previewing.\n\ Perhaps your current printer is not correctly?"), @@ -414,9 +443,11 @@ void AppFrame::OnPrintPreview (wxCommandEvent &WXUNUSED(event)) { frame->Centre(wxBOTH); frame->Initialize(); frame->Show(true); +#endif // wxUSE_PRINTING_ARCHITECTURE } void AppFrame::OnPrint (wxCommandEvent &WXUNUSED(event)) { +#if wxUSE_PRINTING_ARCHITECTURE wxPrintDialogData printDialogData( *g_printData); wxPrinter printer (&printDialogData); EditPrint printout (m_edit); @@ -429,16 +460,17 @@ void AppFrame::OnPrint (wxCommandEvent &WXUNUSED(event)) { } } (*g_printData) = printer.GetPrintDialogData().GetPrintData(); +#endif // wxUSE_PRINTING_ARCHITECTURE } // edit events void AppFrame::OnEdit (wxCommandEvent &event) { - if (m_edit) m_edit->ProcessEvent (event); + if (m_edit) m_edit->GetEventHandler()->ProcessEvent (event); } // private functions -void AppFrame::CreateMenu () { - +void AppFrame::CreateMenu () +{ // File menu wxMenu *menuFile = new wxMenu; menuFile->Append (wxID_OPEN, _("&Open ..\tCtrl+O")); @@ -478,7 +510,7 @@ void AppFrame::CreateMenu () { menuEdit->Enable (myID_GOTO, false); menuEdit->AppendSeparator(); menuEdit->Append (myID_INDENTINC, _("&Indent increase\tTab")); - menuEdit->Append (myID_INDENTRED, _("I&ndent reduce\tBksp")); + menuEdit->Append (myID_INDENTRED, _("I&ndent reduce\tShift+Tab")); menuEdit->AppendSeparator(); menuEdit->Append (wxID_SELECTALL, _("&Select all\tCtrl+A")); menuEdit->Append (myID_SELECTLINE, _("Select &line\tCtrl+L")); @@ -535,6 +567,7 @@ void AppFrame::CreateMenu () { wxMenu *menuWindow = new wxMenu; menuWindow->Append (myID_PAGEPREV, _("&Previous\tCtrl+Shift+Tab")); menuWindow->Append (myID_PAGENEXT, _("&Next\tCtrl+Tab")); + menuWindow->Append(myID_WINDOW_MINIMAL, _("&Minimal editor")); // Help menu wxMenu *menuHelp = new wxMenu; @@ -548,10 +581,10 @@ void AppFrame::CreateMenu () { m_menuBar->Append (menuWindow, _("&Window")); m_menuBar->Append (menuHelp, _("&Help")); SetMenuBar (m_menuBar); - } -void AppFrame::FileOpen (wxString fname) { +void AppFrame::FileOpen (wxString fname) +{ wxFileName w(fname); w.Normalize(); fname = w.GetFullPath(); m_edit->LoadFile (fname); } @@ -582,7 +615,7 @@ END_EVENT_TABLE () AppAbout::AppAbout (wxWindow *parent, int milliseconds, long style) - : wxDialog (parent, -1, wxEmptyString, + : wxDialog (parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, style | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) { @@ -598,27 +631,27 @@ AppAbout::AppAbout (wxWindow *parent, // about info wxGridSizer *aboutinfo = new wxGridSizer (2, 0, 2); - aboutinfo->Add (new wxStaticText(this, -1, _("Written by: ")), + aboutinfo->Add (new wxStaticText(this, wxID_ANY, _("Written by: ")), 0, wxALIGN_LEFT); - aboutinfo->Add (new wxStaticText(this, -1, APP_MAINT), + aboutinfo->Add (new wxStaticText(this, wxID_ANY, APP_MAINT), 1, wxEXPAND | wxALIGN_LEFT); - aboutinfo->Add (new wxStaticText(this, -1, _("Version: ")), + aboutinfo->Add (new wxStaticText(this, wxID_ANY, _("Version: ")), 0, wxALIGN_LEFT); - aboutinfo->Add (new wxStaticText(this, -1, APP_VERSION), + aboutinfo->Add (new wxStaticText(this, wxID_ANY, APP_VERSION), 1, wxEXPAND | wxALIGN_LEFT); - aboutinfo->Add (new wxStaticText(this, -1, _("Licence type: ")), + aboutinfo->Add (new wxStaticText(this, wxID_ANY, _("Licence type: ")), 0, wxALIGN_LEFT); - aboutinfo->Add (new wxStaticText(this, -1, APP_LICENCE), + aboutinfo->Add (new wxStaticText(this, wxID_ANY, APP_LICENCE), 1, wxEXPAND | wxALIGN_LEFT); - aboutinfo->Add (new wxStaticText(this, -1, _("Copyright: ")), + aboutinfo->Add (new wxStaticText(this, wxID_ANY, _("Copyright: ")), 0, wxALIGN_LEFT); - aboutinfo->Add (new wxStaticText(this, -1, APP_COPYRIGTH), + aboutinfo->Add (new wxStaticText(this, wxID_ANY, APP_COPYRIGTH), 1, wxEXPAND | wxALIGN_LEFT); // about icontitle//info wxBoxSizer *aboutpane = new wxBoxSizer (wxHORIZONTAL); - wxBitmap bitmap = wxBitmap(wxICON (mondrian)); - aboutpane->Add (new wxStaticBitmap (this, -1, bitmap), + wxBitmap bitmap = wxBitmap(wxICON (sample)); + aboutpane->Add (new wxStaticBitmap (this, wxID_ANY, bitmap), 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT, 20); aboutpane->Add (aboutinfo, 1, wxEXPAND); aboutpane->Add (60, 0); @@ -626,12 +659,12 @@ AppAbout::AppAbout (wxWindow *parent, // about complete wxBoxSizer *totalpane = new wxBoxSizer (wxVERTICAL); totalpane->Add (0, 20); - wxStaticText *appname = new wxStaticText(this, -1, *g_appname); + wxStaticText *appname = new wxStaticText(this, wxID_ANY, *g_appname); appname->SetFont (wxFont (24, wxDEFAULT, wxNORMAL, wxBOLD)); totalpane->Add (appname, 0, wxALIGN_CENTER | wxLEFT | wxRIGHT, 40); totalpane->Add (0, 10); totalpane->Add (aboutpane, 0, wxEXPAND | wxALL, 4); - totalpane->Add (new wxStaticText(this, -1, APP_DESCR), + totalpane->Add (new wxStaticText(this, wxID_ANY, APP_DESCR), 0, wxALIGN_CENTER | wxALL, 10); wxButton *okButton = new wxButton (this, wxID_OK, _("OK")); okButton->SetDefault(); @@ -639,22 +672,152 @@ AppAbout::AppAbout (wxWindow *parent, SetSizerAndFit (totalpane); - CenterOnScreen(); + CenterOnScreen(); ShowModal(); } AppAbout::~AppAbout () { - if (m_timer) { - delete m_timer; - m_timer = NULL; - } + wxDELETE(m_timer); } //---------------------------------------------------------------------------- // event handlers void AppAbout::OnTimerEvent (wxTimerEvent &WXUNUSED(event)) { - if (m_timer) delete m_timer; - m_timer = NULL; + wxDELETE(m_timer); EndModal (wxID_OK); } +///////////////////////////////////////////////////////////////////////////// +// Minimal editor added by Troels K 2008-04-08 +// Thanks to geralds for SetLexerXml() - http://wxforum.shadonet.com/viewtopic.php?t=7155 + +class MinimalEditor : public wxStyledTextCtrl +{ + enum + { + margin_id_lineno, + margin_id_fold, + }; + +public: + MinimalEditor(wxWindow* parent, wxWindowID id = wxID_ANY) : wxStyledTextCtrl(parent, id) + { + SetLexerXml(); + + SetProperty(wxT("fold"), wxT("1")); + SetProperty(wxT("fold.comment"), wxT("1")); + SetProperty(wxT("fold.compact"), wxT("1")); + SetProperty(wxT("fold.preprocessor"), wxT("1")); + SetProperty(wxT("fold.html"), wxT("1")); + SetProperty(wxT("fold.html.preprocessor"), wxT("1")); + + SetMarginType(margin_id_lineno, wxSTC_MARGIN_NUMBER); + SetMarginWidth(margin_id_lineno, 32); + + MarkerDefine(wxSTC_MARKNUM_FOLDER, wxSTC_MARK_BOXPLUS, wxT("WHITE"), wxT("BLACK")); + MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_BOXMINUS, wxT("WHITE"), wxT("BLACK")); + MarkerDefine(wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_VLINE, wxT("WHITE"), wxT("BLACK")); + MarkerDefine(wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_BOXPLUSCONNECTED, wxT("WHITE"), wxT("BLACK")); + MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_BOXMINUSCONNECTED, wxT("WHITE"), wxT("BLACK")); + MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_TCORNER, wxT("WHITE"), wxT("BLACK")); + MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_LCORNER, wxT("WHITE"), wxT("BLACK")); + + SetMarginMask(margin_id_fold, wxSTC_MASK_FOLDERS); + SetMarginWidth(margin_id_fold, 32); + SetMarginSensitive(margin_id_fold, true); + + SetFoldFlags(wxSTC_FOLDFLAG_LINEBEFORE_CONTRACTED | wxSTC_FOLDFLAG_LINEAFTER_CONTRACTED); + + SetTabWidth(4); + SetUseTabs(false); + SetWrapMode(wxSTC_WRAP_WORD); + SetWrapVisualFlags(wxSTC_WRAPVISUALFLAG_END); + } + virtual bool SetFont(const wxFont& font) + { + StyleSetFont(wxSTC_STYLE_DEFAULT, (wxFont&)font); + return wxStyledTextCtrl::SetFont(font); + } + void SetLexerXml() + { + SetLexer(wxSTC_LEX_XML); + StyleSetForeground(wxSTC_H_DEFAULT, *wxBLACK); + StyleSetForeground(wxSTC_H_TAG, *wxBLUE); + StyleSetForeground(wxSTC_H_TAGUNKNOWN, *wxBLUE); + StyleSetForeground(wxSTC_H_ATTRIBUTE, *wxRED); + StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, *wxRED); + StyleSetBold(wxSTC_H_ATTRIBUTEUNKNOWN, true); + StyleSetForeground(wxSTC_H_NUMBER, *wxBLACK); + StyleSetForeground(wxSTC_H_DOUBLESTRING, *wxBLACK); + StyleSetForeground(wxSTC_H_SINGLESTRING, *wxBLACK); + StyleSetForeground(wxSTC_H_OTHER, *wxBLUE); + StyleSetForeground(wxSTC_H_COMMENT, wxTheColourDatabase->Find(wxT("GREY"))); + StyleSetForeground(wxSTC_H_ENTITY, *wxRED); + StyleSetBold(wxSTC_H_ENTITY, true); + StyleSetForeground(wxSTC_H_TAGEND, *wxBLUE); + StyleSetForeground(wxSTC_H_XMLSTART, *wxBLUE); + StyleSetForeground(wxSTC_H_XMLEND, *wxBLUE); + StyleSetForeground(wxSTC_H_CDATA, *wxRED); + } +protected: + void OnMarginClick(wxStyledTextEvent&); + void OnText(wxStyledTextEvent&); + DECLARE_EVENT_TABLE() +}; + +BEGIN_EVENT_TABLE(MinimalEditor, wxStyledTextCtrl) + EVT_STC_MARGINCLICK(wxID_ANY, MinimalEditor::OnMarginClick) + EVT_STC_CHANGE(wxID_ANY, MinimalEditor::OnText) +END_EVENT_TABLE() + +void MinimalEditor::OnMarginClick(wxStyledTextEvent &event) +{ + if (event.GetMargin() == margin_id_fold) + { + int lineClick = LineFromPosition(event.GetPosition()); + int levelClick = GetFoldLevel(lineClick); + if ((levelClick & wxSTC_FOLDLEVELHEADERFLAG) > 0) + { + ToggleFold(lineClick); + } + } +} + +void MinimalEditor::OnText(wxStyledTextEvent& event) +{ + wxLogDebug(wxT("Modified")); + event.Skip(); +} + +class MinimalEditorFrame : public wxFrame +{ +public: + MinimalEditorFrame() : wxFrame(NULL, wxID_ANY, _("Minimal Editor")) + { + MinimalEditor* editor = new MinimalEditor(this); + editor->SetFont(wxSystemSettings::GetFont(wxSYS_ANSI_FIXED_FONT)); + wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL); + sizer->Add(editor, 1, wxEXPAND); + SetSizer(sizer); + editor->SetText( + "\n" + " \n" + " This is xml with syntax highlighting, line numbers, folding, word wrap and context menu\n" + " \n" + "" + ); + } +}; + +wxFrame* App::MinimalEditor() +{ + MinimalEditorFrame* frame = new MinimalEditorFrame; + frame->Show(); + return frame; +} + +void App::OnMinimalEditor(wxCommandEvent& WXUNUSED(event)) +{ + MinimalEditor(); +} +