]>
Commit | Line | Data |
---|---|---|
9ce192d4 RD |
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 | ||
ab7ce33c | 12 | #if defined(__GNUG__) && !defined(__APPLE__) |
9ce192d4 RD |
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 | //---------------------------------------------------------------------- | |
9e730a78 | 43 | // Make an editor class |
9ce192d4 | 44 | |
9e730a78 RD |
45 | class MySTC : public wxStyledTextCtrl |
46 | { | |
47 | public: | |
48 | MySTC(wxWindow *parent, wxWindowID id, | |
49 | const wxPoint& pos = wxDefaultPosition, | |
50 | const wxSize& size = wxDefaultSize, long style = 0); | |
51 | ||
52 | void OnKeyPressed(wxKeyEvent& evt); | |
53 | ||
54 | private: | |
55 | DECLARE_EVENT_TABLE() | |
56 | }; | |
57 | ||
58 | BEGIN_EVENT_TABLE(MySTC, wxStyledTextCtrl) | |
59 | EVT_KEY_DOWN(MySTC::OnKeyPressed) | |
60 | END_EVENT_TABLE() | |
61 | ||
62 | //---------------------------------------------------------------------- | |
9ce192d4 RD |
63 | // Define a new frame type: this is going to be our main frame |
64 | class MyFrame : public wxFrame | |
65 | { | |
66 | public: | |
67 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
68 | ||
69 | void OnQuit(wxCommandEvent& event); | |
70 | void OnAbout(wxCommandEvent& event); | |
9ce192d4 RD |
71 | |
72 | private: | |
9e730a78 | 73 | MySTC* ed; |
9ce192d4 RD |
74 | |
75 | DECLARE_EVENT_TABLE() | |
76 | }; | |
77 | ||
78 | ||
79 | // IDs for the controls and the menu commands | |
80 | enum | |
81 | { | |
82 | // menu items | |
83 | ID_Quit = 1, | |
84 | ID_About, | |
85 | ID_ED | |
86 | }; | |
87 | ||
88 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
89 | EVT_MENU (ID_Quit, MyFrame::OnQuit) | |
90 | EVT_MENU (ID_About, MyFrame::OnAbout) | |
9ce192d4 RD |
91 | END_EVENT_TABLE() |
92 | ||
93 | IMPLEMENT_APP(MyApp) | |
94 | ||
95 | //---------------------------------------------------------------------- | |
96 | // `Main program' equivalent: the program execution "starts" here | |
97 | ||
98 | bool MyApp::OnInit() | |
99 | { | |
695c21b1 | 100 | MyFrame *frame = new MyFrame(_T("Testing wxStyledTextCtrl"), |
9e730a78 | 101 | wxPoint(5, 5), wxSize(600, 600)); |
9ce192d4 RD |
102 | |
103 | frame->Show(TRUE); | |
104 | return TRUE; | |
105 | } | |
106 | ||
107 | //---------------------------------------------------------------------- | |
108 | ||
109 | // frame constructor | |
110 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
111 | : wxFrame((wxFrame *)NULL, -1, title, pos, size) | |
112 | { | |
113 | #ifdef __WXMAC__ | |
114 | // we need this in order to allow the about menu relocation, since ABOUT is | |
115 | // not the default id of the about menu | |
116 | wxApp::s_macAboutMenuItemId = ID_About; | |
117 | #endif | |
118 | ||
119 | ||
120 | // create a menu bar | |
695c21b1 | 121 | wxMenu *menuFile = new wxMenu(wxEmptyString, wxMENU_TEAROFF); |
9ce192d4 RD |
122 | |
123 | // the "About" item should be in the help menu | |
124 | wxMenu *helpMenu = new wxMenu; | |
695c21b1 | 125 | helpMenu->Append(ID_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); |
9ce192d4 | 126 | |
695c21b1 | 127 | menuFile->Append(ID_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
9ce192d4 RD |
128 | |
129 | // now append the freshly created menu to the menu bar... | |
130 | wxMenuBar *menuBar = new wxMenuBar(); | |
695c21b1 JS |
131 | menuBar->Append(menuFile, _T("&File")); |
132 | menuBar->Append(helpMenu, _T("&Help")); | |
9ce192d4 RD |
133 | |
134 | // ... and attach this menu bar to the frame | |
135 | SetMenuBar(menuBar); | |
136 | ||
137 | #if wxUSE_STATUSBAR | |
138 | CreateStatusBar(2); | |
695c21b1 | 139 | SetStatusText(_T("Testing wxStyledTextCtrl")); |
9ce192d4 RD |
140 | #endif // wxUSE_STATUSBAR |
141 | ||
142 | ||
143 | //---------------------------------------- | |
144 | // Setup the editor | |
9e730a78 RD |
145 | ed = new MySTC(this, ID_ED); |
146 | } | |
147 | ||
148 | ||
149 | // event handlers | |
150 | ||
151 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
152 | { | |
153 | // TRUE is to force the frame to close | |
154 | Close(TRUE); | |
155 | } | |
156 | ||
157 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
158 | { | |
159 | wxString msg; | |
160 | msg.Printf( _T("Testing wxStyledTextCtrl...\n")); | |
9ce192d4 | 161 | |
9e730a78 RD |
162 | wxMessageBox(msg, _T("About This Test"), wxOK | wxICON_INFORMATION, this); |
163 | } | |
164 | ||
165 | ||
166 | //---------------------------------------------------------------------- | |
167 | ||
168 | wxChar* keywords = | |
169 | _T("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 | MySTC::MySTC(wxWindow *parent, wxWindowID id, | |
182 | const wxPoint& pos, const wxSize& size, | |
183 | long style) | |
184 | : wxStyledTextCtrl(parent, id, pos, size, style) | |
185 | { | |
9ce192d4 | 186 | // Default font |
c19d0121 | 187 | wxFont font(10, wxMODERN, wxNORMAL, wxNORMAL); |
9e730a78 RD |
188 | StyleSetFont(wxSTC_STYLE_DEFAULT, font); |
189 | StyleClearAll(); | |
190 | ||
191 | StyleSetForeground(0, wxColour(0x80, 0x80, 0x80)); | |
192 | StyleSetForeground(1, wxColour(0x00, 0x7f, 0x00)); | |
193 | //StyleSetForeground(2, wxColour(0x00, 0x7f, 0x00)); | |
194 | StyleSetForeground(3, wxColour(0x7f, 0x7f, 0x7f)); | |
195 | StyleSetForeground(4, wxColour(0x00, 0x7f, 0x7f)); | |
196 | StyleSetForeground(5, wxColour(0x00, 0x00, 0x7f)); | |
197 | StyleSetForeground(6, wxColour(0x7f, 0x00, 0x7f)); | |
198 | StyleSetForeground(7, wxColour(0x7f, 0x00, 0x7f)); | |
199 | StyleSetForeground(8, wxColour(0x00, 0x7f, 0x7f)); | |
200 | StyleSetForeground(9, wxColour(0x7f, 0x7f, 0x7f)); | |
201 | StyleSetForeground(10, wxColour(0x00, 0x00, 0x00)); | |
202 | StyleSetForeground(11, wxColour(0x00, 0x00, 0x00)); | |
203 | StyleSetBold(5, TRUE); | |
204 | StyleSetBold(10, TRUE); | |
9ce192d4 RD |
205 | |
206 | #ifdef __WXMSW__ | |
9e730a78 | 207 | StyleSetSpec(2, _T("fore:#007f00,bold,face:Arial,size:9")); |
9ce192d4 | 208 | #else |
9e730a78 | 209 | StyleSetSpec(2, _T("fore:#007f00,bold,face:Helvetica,size:9")); |
9ce192d4 RD |
210 | #endif |
211 | ||
212 | // give it some text to play with | |
9ce192d4 | 213 | wxString st; |
695c21b1 JS |
214 | wxFileInputStream stream(wxT("stctest.cpp")); |
215 | size_t sz = stream.GetSize(); | |
216 | char* buf = new char[sz + 1]; | |
217 | stream.Read((void*) buf, stream.GetSize()); | |
218 | buf[sz] = 0; | |
219 | st = wxString::FromAscii(buf); | |
220 | delete[] buf; | |
9ce192d4 | 221 | |
9e730a78 RD |
222 | InsertText(0, st); |
223 | EmptyUndoBuffer(); | |
9ce192d4 | 224 | |
9e730a78 RD |
225 | SetLexer(wxSTC_LEX_CPP); |
226 | SetKeyWords(0, keywords); | |
9ce192d4 RD |
227 | } |
228 | ||
9e730a78 | 229 | void MySTC::OnKeyPressed(wxKeyEvent& evt) |
9ce192d4 | 230 | { |
9e730a78 RD |
231 | if (CallTipActive()) |
232 | CallTipCancel(); | |
233 | ||
234 | int key = evt.GetKeyCode(); | |
235 | if ( key == WXK_SPACE && evt.ControlDown()) { | |
236 | int pos = GetCurrentPos(); | |
237 | ||
238 | if (evt.ShiftDown()) { | |
239 | // show how to do CallTips | |
240 | CallTipSetBackground(wxColour(_T("YELLOW"))); | |
72767371 JS |
241 | CallTipShow(pos, _T("lots of of text: blah, blah, blah\n\n\ |
242 | show some suff, maybe parameters..\n\n\ | |
243 | fubar(param1, param2)")); | |
9e730a78 RD |
244 | } |
245 | else { | |
246 | // show how to do AutoComplete | |
247 | AutoCompSetIgnoreCase(false); | |
248 | AutoCompShow(0, keywords); // reuse the keyword list here | |
249 | // normally you would build a string of completion texts... | |
250 | } | |
251 | } | |
252 | else | |
253 | evt.Skip(); | |
9ce192d4 | 254 | } |