]>
git.saurik.com Git - wxWidgets.git/blob - contrib/samples/stc/stctest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: sample of using wxStyledTextCtrl
8 // Copyright: (c) 2000 by Total Control Software
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(__APPLE__)
13 #pragma implementation "stctest.cpp"
14 #pragma interface "stctest.cpp"
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
24 // for all others, include the necessary headers (this file is usually all you
25 // need because it includes almost all "standard" wxWindows headers
30 #include <wx/wfstream.h>
32 #include <wx/stc/stc.h>
34 //----------------------------------------------------------------------
36 class MyApp
: public wxApp
39 virtual bool OnInit();
42 //----------------------------------------------------------------------
43 // Make an editor class
45 class MySTC
: public wxStyledTextCtrl
48 MySTC(wxWindow
*parent
, wxWindowID id
,
49 const wxPoint
& pos
= wxDefaultPosition
,
50 const wxSize
& size
= wxDefaultSize
, long style
= 0);
52 void OnKeyPressed(wxKeyEvent
& evt
);
58 BEGIN_EVENT_TABLE(MySTC
, wxStyledTextCtrl
)
59 EVT_KEY_DOWN(MySTC::OnKeyPressed
)
62 //----------------------------------------------------------------------
63 // Define a new frame type: this is going to be our main frame
64 class MyFrame
: public wxFrame
67 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
69 void OnQuit(wxCommandEvent
& event
);
70 void OnAbout(wxCommandEvent
& event
);
79 // IDs for the controls and the menu commands
88 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
89 EVT_MENU (ID_Quit
, MyFrame::OnQuit
)
90 EVT_MENU (ID_About
, MyFrame::OnAbout
)
95 //----------------------------------------------------------------------
96 // `Main program' equivalent: the program execution "starts" here
100 MyFrame
*frame
= new MyFrame(_T("Testing wxStyledTextCtrl"),
101 wxPoint(5, 5), wxSize(600, 600));
107 //----------------------------------------------------------------------
110 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
111 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
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
;
121 wxMenu
*menuFile
= new wxMenu(wxEmptyString
, wxMENU_TEAROFF
);
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"));
127 menuFile
->Append(ID_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
134 // ... and attach this menu bar to the frame
139 SetStatusText(_T("Testing wxStyledTextCtrl"));
140 #endif // wxUSE_STATUSBAR
143 //----------------------------------------
145 ed
= new MySTC(this, ID_ED
);
151 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
153 // TRUE is to force the frame to close
157 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
160 msg
.Printf( _T("Testing wxStyledTextCtrl...\n"));
162 wxMessageBox(msg
, _T("About This Test"), wxOK
| wxICON_INFORMATION
, this);
166 //----------------------------------------------------------------------
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");
181 MySTC::MySTC(wxWindow
*parent
, wxWindowID id
,
182 const wxPoint
& pos
, const wxSize
& size
,
184 : wxStyledTextCtrl(parent
, id
, pos
, size
, style
)
187 wxFont
font(10, wxMODERN
, wxNORMAL
, wxNORMAL
);
188 StyleSetFont(wxSTC_STYLE_DEFAULT
, font
);
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
);
207 StyleSetSpec(2, _T("fore:#007f00,bold,face:Arial,size:9"));
209 StyleSetSpec(2, _T("fore:#007f00,bold,face:Helvetica,size:9"));
212 // give it some text to play with
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());
219 st
= wxString::FromAscii(buf
);
225 SetLexer(wxSTC_LEX_CPP
);
226 SetKeyWords(0, keywords
);
229 void MySTC::OnKeyPressed(wxKeyEvent
& evt
)
234 int key
= evt
.GetKeyCode();
235 if ( key
== WXK_SPACE
&& evt
.ControlDown()) {
236 int pos
= GetCurrentPos();
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)"));
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...