]> git.saurik.com Git - wxWidgets.git/blame - contrib/samples/stc/stctest.cpp
Fixed redefinition of i
[wxWidgets.git] / contrib / samples / stc / stctest.cpp
CommitLineData
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
36class MyApp : public wxApp
37{
38public:
39 virtual bool OnInit();
40};
41
42//----------------------------------------------------------------------
9e730a78 43// Make an editor class
9ce192d4 44
9e730a78
RD
45class MySTC : public wxStyledTextCtrl
46{
47public:
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
54private:
55 DECLARE_EVENT_TABLE()
56};
57
58BEGIN_EVENT_TABLE(MySTC, wxStyledTextCtrl)
59 EVT_KEY_DOWN(MySTC::OnKeyPressed)
60END_EVENT_TABLE()
61
62//----------------------------------------------------------------------
9ce192d4
RD
63// Define a new frame type: this is going to be our main frame
64class MyFrame : public wxFrame
65{
66public:
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
72private:
9e730a78 73 MySTC* ed;
9ce192d4
RD
74
75 DECLARE_EVENT_TABLE()
76};
77
78
79// IDs for the controls and the menu commands
80enum
81{
82 // menu items
83 ID_Quit = 1,
84 ID_About,
85 ID_ED
86};
87
88BEGIN_EVENT_TABLE(MyFrame, wxFrame)
89 EVT_MENU (ID_Quit, MyFrame::OnQuit)
90 EVT_MENU (ID_About, MyFrame::OnAbout)
9ce192d4
RD
91END_EVENT_TABLE()
92
93IMPLEMENT_APP(MyApp)
94
95//----------------------------------------------------------------------
96// `Main program' equivalent: the program execution "starts" here
97
98bool 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
110MyFrame::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
151void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
152{
153 // TRUE is to force the frame to close
154 Close(TRUE);
155}
156
157void 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
168wxChar* keywords =
169_T("asm auto bool break case catch char class const \
170const_cast continue default delete do double \
171dynamic_cast else enum explicit export extern \
172false float for friend goto if inline int long \
173mutable namespace new operator private protected \
174public register reinterpret_cast return short signed \
175sizeof static static_cast struct switch template this \
176throw true try typedef typeid typename union unsigned \
177using virtual void volatile wchar_t while");
178
179
180
181MySTC::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 229void 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")));
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();
9ce192d4 254}