| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: stctest.cpp |
| 3 | // Purpose: sample of using wxStyledTextCtrl |
| 4 | // Author: Robin Dunn |
| 5 | // Modified by: |
| 6 | // Created: 3-Feb-2000 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 2000 by Total Control Software |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "stctest.cpp" |
| 14 | #pragma interface "stctest.cpp" |
| 15 | #endif |
| 16 | |
| 17 | // For compilers that support precompilation, includes "wx/wx.h". |
| 18 | #include "wx/wxprec.h" |
| 19 | |
| 20 | #ifdef __BORLANDC__ |
| 21 | #pragma hdrstop |
| 22 | #endif |
| 23 | |
| 24 | // for all others, include the necessary headers (this file is usually all you |
| 25 | // need because it includes almost all "standard" wxWindows headers |
| 26 | #ifndef WX_PRECOMP |
| 27 | #include "wx/wx.h" |
| 28 | #endif |
| 29 | |
| 30 | #include <wx/wfstream.h> |
| 31 | |
| 32 | #include <wx/stc/stc.h> |
| 33 | |
| 34 | //---------------------------------------------------------------------- |
| 35 | |
| 36 | class MyApp : public wxApp |
| 37 | { |
| 38 | public: |
| 39 | virtual bool OnInit(); |
| 40 | }; |
| 41 | |
| 42 | //---------------------------------------------------------------------- |
| 43 | |
| 44 | // Define a new frame type: this is going to be our main frame |
| 45 | class MyFrame : public wxFrame |
| 46 | { |
| 47 | public: |
| 48 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); |
| 49 | |
| 50 | void OnQuit(wxCommandEvent& event); |
| 51 | void OnAbout(wxCommandEvent& event); |
| 52 | void OnStyleNeeded(wxStyledTextEvent& event); |
| 53 | |
| 54 | private: |
| 55 | wxStyledTextCtrl* ed; |
| 56 | |
| 57 | DECLARE_EVENT_TABLE() |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | // IDs for the controls and the menu commands |
| 62 | enum |
| 63 | { |
| 64 | // menu items |
| 65 | ID_Quit = 1, |
| 66 | ID_About, |
| 67 | ID_ED |
| 68 | }; |
| 69 | |
| 70 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
| 71 | EVT_MENU (ID_Quit, MyFrame::OnQuit) |
| 72 | EVT_MENU (ID_About, MyFrame::OnAbout) |
| 73 | EVT_STC_STYLENEEDED (ID_ED, MyFrame::OnStyleNeeded) |
| 74 | END_EVENT_TABLE() |
| 75 | |
| 76 | IMPLEMENT_APP(MyApp) |
| 77 | |
| 78 | //---------------------------------------------------------------------- |
| 79 | // `Main program' equivalent: the program execution "starts" here |
| 80 | |
| 81 | bool MyApp::OnInit() |
| 82 | { |
| 83 | MyFrame *frame = new MyFrame("Testing wxStyledTextCtrl", |
| 84 | wxPoint(5, 5), wxSize(400, 600)); |
| 85 | |
| 86 | frame->Show(TRUE); |
| 87 | return TRUE; |
| 88 | } |
| 89 | |
| 90 | //---------------------------------------------------------------------- |
| 91 | |
| 92 | // frame constructor |
| 93 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) |
| 94 | : wxFrame((wxFrame *)NULL, -1, title, pos, size) |
| 95 | { |
| 96 | #ifdef __WXMAC__ |
| 97 | // we need this in order to allow the about menu relocation, since ABOUT is |
| 98 | // not the default id of the about menu |
| 99 | wxApp::s_macAboutMenuItemId = ID_About; |
| 100 | #endif |
| 101 | |
| 102 | |
| 103 | // create a menu bar |
| 104 | wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF); |
| 105 | |
| 106 | // the "About" item should be in the help menu |
| 107 | wxMenu *helpMenu = new wxMenu; |
| 108 | helpMenu->Append(ID_About, "&About...\tCtrl-A", "Show about dialog"); |
| 109 | |
| 110 | menuFile->Append(ID_Quit, "E&xit\tAlt-X", "Quit this program"); |
| 111 | |
| 112 | // now append the freshly created menu to the menu bar... |
| 113 | wxMenuBar *menuBar = new wxMenuBar(); |
| 114 | menuBar->Append(menuFile, "&File"); |
| 115 | menuBar->Append(helpMenu, "&Help"); |
| 116 | |
| 117 | // ... and attach this menu bar to the frame |
| 118 | SetMenuBar(menuBar); |
| 119 | |
| 120 | #if wxUSE_STATUSBAR |
| 121 | CreateStatusBar(2); |
| 122 | SetStatusText("Testing wxStyledTextCtrl"); |
| 123 | #endif // wxUSE_STATUSBAR |
| 124 | |
| 125 | |
| 126 | //---------------------------------------- |
| 127 | // Setup the editor |
| 128 | ed = new wxStyledTextCtrl(this, ID_ED); |
| 129 | |
| 130 | // Default font |
| 131 | wxFont font(8, wxMODERN, wxNORMAL, wxNORMAL); |
| 132 | ed->StyleSetFont(wxSTC_STYLE_DEFAULT, font); |
| 133 | ed->StyleClearAll(); |
| 134 | |
| 135 | ed->StyleSetForeground(0, wxColour(0x80, 0x80, 0x80)); |
| 136 | ed->StyleSetForeground(1, wxColour(0x00, 0x7f, 0x00)); |
| 137 | //ed->StyleSetForeground(2, wxColour(0x00, 0x7f, 0x00)); |
| 138 | ed->StyleSetForeground(3, wxColour(0x7f, 0x7f, 0x7f)); |
| 139 | ed->StyleSetForeground(4, wxColour(0x00, 0x7f, 0x7f)); |
| 140 | ed->StyleSetForeground(5, wxColour(0x00, 0x00, 0x7f)); |
| 141 | ed->StyleSetForeground(6, wxColour(0x7f, 0x00, 0x7f)); |
| 142 | ed->StyleSetForeground(7, wxColour(0x7f, 0x00, 0x7f)); |
| 143 | ed->StyleSetForeground(8, wxColour(0x00, 0x7f, 0x7f)); |
| 144 | ed->StyleSetForeground(9, wxColour(0x7f, 0x7f, 0x7f)); |
| 145 | ed->StyleSetForeground(10, wxColour(0x00, 0x00, 0x00)); |
| 146 | ed->StyleSetForeground(11, wxColour(0x00, 0x00, 0x00)); |
| 147 | ed->StyleSetBold(5, TRUE); |
| 148 | ed->StyleSetBold(10, TRUE); |
| 149 | |
| 150 | #ifdef __WXMSW__ |
| 151 | ed->StyleSetSpec(2, "fore:#007f00,bold,face:Arial,size:7"); |
| 152 | #else |
| 153 | ed->StyleSetSpec(2, "fore:#007f00,bold,face:Helvetica,size:7"); |
| 154 | #endif |
| 155 | |
| 156 | // give it some text to play with |
| 157 | wxFile file("stctest.cpp"); |
| 158 | wxString st; |
| 159 | |
| 160 | char* buff = st.GetWriteBuf(file.Length()); |
| 161 | file.Read(buff, file.Length()); |
| 162 | st.UngetWriteBuf(); |
| 163 | |
| 164 | ed->InsertText(0, st); |
| 165 | ed->EmptyUndoBuffer(); |
| 166 | |
| 167 | ed->SetLexer(wxSTC_LEX_CPP); |
| 168 | ed->SetKeyWords(0, |
| 169 | "asm auto bool break case catch char class const " |
| 170 | "const_cast continue default delete do double " |
| 171 | "dynamic_cast else enum explicit export extern " |
| 172 | "false float for friend goto if inline int long " |
| 173 | "mutable namespace new operator private protected " |
| 174 | "public register reinterpret_cast return short signed " |
| 175 | "sizeof static static_cast struct switch template this " |
| 176 | "throw true try typedef typeid typename union unsigned " |
| 177 | "using virtual void volatile wchar_t while"); |
| 178 | |
| 179 | } |
| 180 | |
| 181 | |
| 182 | // event handlers |
| 183 | |
| 184 | void MyFrame::OnStyleNeeded(wxStyledTextEvent& event) { |
| 185 | int currEndStyled = ed->GetEndStyled(); |
| 186 | ed->Colourise(currEndStyled, event.GetPosition()); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
| 193 | { |
| 194 | // TRUE is to force the frame to close |
| 195 | Close(TRUE); |
| 196 | } |
| 197 | |
| 198 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
| 199 | { |
| 200 | wxString msg; |
| 201 | msg.Printf( _T("Testing wxStyledTextCtrl...\n")); |
| 202 | |
| 203 | wxMessageBox(msg, "About This Test", wxOK | wxICON_INFORMATION, this); |
| 204 | } |