]> git.saurik.com Git - wxWidgets.git/blob - samples/stc/stctest.cpp
Implement NSMenuValidation protocol for the wxNSMenuItemTarget
[wxWidgets.git] / samples / stc / stctest.cpp
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 #if defined(__GNUG__) && !defined(__APPLE__)
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 // Make an editor class
44
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 //----------------------------------------------------------------------
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);
71
72 private:
73 MySTC* ed;
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)
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 {
100 MyFrame *frame = new MyFrame(_T("Testing wxStyledTextCtrl"),
101 wxPoint(5, 5), wxSize(600, 600));
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
121 wxMenu *menuFile = new wxMenu(wxEmptyString, wxMENU_TEAROFF);
122
123 // the "About" item should be in the help menu
124 wxMenu *helpMenu = new wxMenu;
125 helpMenu->Append(ID_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
126
127 menuFile->Append(ID_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
128
129 // now append the freshly created menu to the menu bar...
130 wxMenuBar *menuBar = new wxMenuBar();
131 menuBar->Append(menuFile, _T("&File"));
132 menuBar->Append(helpMenu, _T("&Help"));
133
134 // ... and attach this menu bar to the frame
135 SetMenuBar(menuBar);
136
137 #if wxUSE_STATUSBAR
138 CreateStatusBar(2);
139 SetStatusText(_T("Testing wxStyledTextCtrl"));
140 #endif // wxUSE_STATUSBAR
141
142
143 //----------------------------------------
144 // Setup the editor
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"));
161
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 {
186 // Default font
187 wxFont font(10, wxMODERN, wxNORMAL, wxNORMAL);
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);
205
206 #ifdef __WXMSW__
207 StyleSetSpec(2, _T("fore:#007f00,bold,face:Arial,size:9"));
208 #else
209 StyleSetSpec(2, _T("fore:#007f00,bold,face:Helvetica,size:9"));
210 #endif
211
212 // give it some text to play with
213 wxString st;
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;
221
222 InsertText(0, st);
223 EmptyUndoBuffer();
224
225 SetLexer(wxSTC_LEX_CPP);
226 SetKeyWords(0, keywords);
227 }
228
229 void MySTC::OnKeyPressed(wxKeyEvent& evt)
230 {
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")));
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)"));
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();
254 }