]>
Commit | Line | Data |
---|---|---|
0290598f VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: caret.cpp | |
3 | // Purpose: wxCaret sample | |
4 | // Author: Robert Roebling | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) wxWindows team | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
0290598f VZ |
12 | // For compilers that support precompilation, includes "wx/wx.h". |
13 | #include <wx/wxprec.h> | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | // for all others, include the necessary headers (this file is usually all you | |
20 | // need because it includes almost all <standard< wxWindows headers | |
21 | #ifndef WX_PRECOMP | |
22 | #include <wx/wx.h> | |
23 | ||
24 | #include <wx/log.h> | |
25 | #endif | |
26 | ||
27 | #include "wx/caret.h" | |
28 | ||
29 | // ---------------------------------------------------------------------------- | |
30 | // ressources | |
31 | // ---------------------------------------------------------------------------- | |
32 | // the application icon | |
33 | #if defined(__WXGTK__) || defined(__WXMOTIF__) | |
34 | #include "mondrian.xpm" | |
35 | #endif | |
36 | ||
37 | // ---------------------------------------------------------------------------- | |
38 | // private classes | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | // Define a new application type, each program should derive a class from wxApp | |
42 | class MyApp : public wxApp | |
43 | { | |
44 | public: | |
45 | // override base class virtuals | |
46 | // ---------------------------- | |
47 | ||
48 | // this one is called on application startup and is a good place for the app | |
49 | // initialization (doing it here and not in the ctor allows to have an error | |
50 | // return: if OnInit() returns false, the application terminates) | |
51 | virtual bool OnInit(); | |
52 | }; | |
53 | ||
54 | // Define a new frame type: this is going to be our main frame | |
55 | class MyFrame : public wxFrame | |
56 | { | |
57 | public: | |
58 | // ctor(s) | |
59 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
60 | ||
61 | // event handlers (these functions should _not_ be virtual) | |
62 | void OnQuit(wxCommandEvent& event); | |
63 | void OnAbout(wxCommandEvent& event); | |
64 | ||
65 | private: | |
66 | // any class wishing to process wxWindows events must use this macro | |
67 | DECLARE_EVENT_TABLE() | |
68 | }; | |
69 | ||
70 | // MyCanvas is a canvas on which you can type | |
71 | class MyCanvas: public wxScrolledWindow | |
72 | { | |
73 | public: | |
74 | MyCanvas() { } | |
75 | MyCanvas( wxWindow *parent ); | |
76 | ~MyCanvas(); | |
77 | ||
92607616 | 78 | wxChar& CharAt(int x, int y) { return *(m_text + x + m_xChars * y); } |
0290598f VZ |
79 | |
80 | // caret movement | |
81 | void Home() { m_xCaret = 0; } | |
82 | void End() { m_xCaret = m_xChars - 1; } | |
83 | void FirstLine() { m_yCaret = 0; } | |
84 | void LastLine() { m_yCaret = m_yChars - 1; } | |
85 | void PrevChar() { if ( !m_xCaret-- ) { End(); PrevLine(); } } | |
86 | void NextChar() { if ( ++m_xCaret == m_xChars ) { Home(); NextLine(); } } | |
87 | void PrevLine() { if ( !m_yCaret-- ) LastLine(); } | |
88 | void NextLine() { if ( ++m_yCaret == m_yChars ) FirstLine(); } | |
89 | ||
90 | // event handlers | |
91 | void OnPaint( wxPaintEvent &event ); | |
92 | void OnSize( wxSizeEvent &event ); | |
93 | void OnChar( wxKeyEvent &event ); | |
94 | ||
95 | private: | |
0290598f VZ |
96 | wxFont m_font; |
97 | ||
98 | // the margin around the text (looks nicer) | |
99 | int m_xMargin, m_yMargin; | |
100 | ||
101 | // size (in pixels) of one character | |
102 | long m_widthChar, m_heightChar; | |
103 | ||
104 | // position (in text coords) of the caret | |
105 | int m_xCaret, m_yCaret; | |
106 | ||
107 | // the size (in text coords) of the window | |
108 | int m_xChars, m_yChars; | |
109 | ||
110 | // the text | |
92607616 | 111 | wxChar *m_text; |
0290598f VZ |
112 | |
113 | DECLARE_DYNAMIC_CLASS(MyCanvas) | |
114 | DECLARE_EVENT_TABLE() | |
115 | }; | |
116 | ||
117 | // ---------------------------------------------------------------------------- | |
118 | // constants | |
119 | // ---------------------------------------------------------------------------- | |
120 | ||
121 | // IDs for the controls and the menu commands | |
122 | enum | |
123 | { | |
124 | // menu items | |
7e12d1bf VZ |
125 | Caret_Quit = 1, |
126 | Caret_About, | |
127 | Caret_Test1, | |
128 | Caret_Test2, | |
0290598f VZ |
129 | |
130 | // controls start here (the numbers are, of course, arbitrary) | |
7e12d1bf | 131 | Caret_Text = 1000 |
0290598f VZ |
132 | }; |
133 | ||
134 | // ---------------------------------------------------------------------------- | |
135 | // event tables and other macros for wxWindows | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
138 | // the event tables connect the wxWindows events with the functions (event | |
139 | // handlers) which process them. It can be also done at run-time, but for the | |
140 | // simple menu events like this the static method is much simpler. | |
141 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
7e12d1bf VZ |
142 | EVT_MENU(Caret_Quit, MyFrame::OnQuit) |
143 | EVT_MENU(Caret_About, MyFrame::OnAbout) | |
0290598f VZ |
144 | END_EVENT_TABLE() |
145 | ||
146 | // Create a new application object: this macro will allow wxWindows to create | |
147 | // the application object during program execution (it's better than using a | |
148 | // static object for many reasons) and also declares the accessor function | |
149 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
150 | // not wxApp) | |
151 | IMPLEMENT_APP(MyApp) | |
152 | ||
153 | // ============================================================================ | |
154 | // implementation | |
155 | // ============================================================================ | |
156 | ||
157 | // ---------------------------------------------------------------------------- | |
158 | // the application class | |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
161 | // `Main program' equivalent: the program execution "starts" here | |
162 | bool MyApp::OnInit() | |
163 | { | |
164 | // Create the main application window | |
7e12d1bf | 165 | MyFrame *frame = new MyFrame("Caret wxWindows sample", |
0290598f VZ |
166 | wxPoint(50, 50), wxSize(450, 340)); |
167 | ||
168 | // Show it and tell the application that it's our main window | |
169 | // @@@ what does it do exactly, in fact? is it necessary here? | |
170 | frame->Show(TRUE); | |
171 | SetTopWindow(frame); | |
172 | ||
173 | // success: wxApp::OnRun() will be called which will enter the main message | |
174 | // loop and the application will run. If we returned FALSE here, the | |
175 | // application would exit immediately. | |
176 | return TRUE; | |
177 | } | |
178 | ||
179 | // ---------------------------------------------------------------------------- | |
180 | // main frame | |
181 | // ---------------------------------------------------------------------------- | |
182 | ||
183 | // frame constructor | |
184 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
185 | : wxFrame((wxFrame *)NULL, -1, title, pos, size) | |
186 | { | |
187 | // set the frame icon | |
188 | SetIcon(wxICON(mondrian)); | |
189 | ||
190 | // create a menu bar | |
191 | wxMenu *menuFile = new wxMenu; | |
192 | ||
7e12d1bf | 193 | menuFile->Append(Caret_About, "&About...\tCtrl-A", "Show about dialog"); |
0290598f | 194 | menuFile->AppendSeparator(); |
7e12d1bf | 195 | menuFile->Append(Caret_Quit, "E&xit\tAlt-X", "Quit this program"); |
0290598f VZ |
196 | |
197 | // now append the freshly created menu to the menu bar... | |
198 | wxMenuBar *menuBar = new wxMenuBar; | |
199 | menuBar->Append(menuFile, "&File"); | |
200 | ||
201 | // ... and attach this menu bar to the frame | |
202 | SetMenuBar(menuBar); | |
203 | ||
204 | (void) new MyCanvas( this ); | |
205 | ||
206 | // create a status bar just for fun (by default with 1 pane only) | |
207 | CreateStatusBar(2); | |
208 | SetStatusText("Welcome to wxWindows!"); | |
209 | } | |
210 | ||
211 | ||
212 | // event handlers | |
213 | ||
214 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
215 | { | |
216 | // TRUE is to force the frame to close | |
217 | Close(TRUE); | |
218 | } | |
219 | ||
220 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
221 | { | |
7e12d1bf | 222 | wxMessageBox(_T("This is the about dialog of caret sample."), "About Caret", wxOK | wxICON_INFORMATION, this); |
0290598f VZ |
223 | } |
224 | ||
225 | ||
226 | // ---------------------------------------------------------------------------- | |
227 | // MyCanvas | |
228 | // ---------------------------------------------------------------------------- | |
229 | ||
230 | IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow) | |
231 | ||
232 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
233 | EVT_PAINT(MyCanvas::OnPaint) | |
234 | EVT_SIZE(MyCanvas::OnSize) | |
235 | EVT_CHAR(MyCanvas::OnChar) | |
236 | END_EVENT_TABLE() | |
237 | ||
238 | MyCanvas::MyCanvas( wxWindow *parent ) | |
239 | : wxScrolledWindow( parent, -1, | |
240 | wxDefaultPosition, wxDefaultSize, | |
241 | wxSUNKEN_BORDER ) | |
242 | { | |
92607616 | 243 | m_text = (wxChar *)NULL; |
a8f25787 | 244 | |
0290598f VZ |
245 | SetBackgroundColour(* wxWHITE); |
246 | ||
247 | m_font = *wxNORMAL_FONT; | |
248 | ||
249 | wxClientDC dc(this); | |
250 | dc.SetFont( m_font ); | |
251 | m_heightChar = dc.GetCharHeight(); | |
252 | m_widthChar = dc.GetCharWidth(); | |
253 | ||
4ee14879 VZ |
254 | wxCaret *caret = new wxCaret(this, m_widthChar, m_heightChar); |
255 | SetCaret(caret); | |
0290598f VZ |
256 | |
257 | m_xCaret = m_yCaret = | |
258 | m_xChars = m_yChars = 0; | |
259 | ||
260 | m_xMargin = m_yMargin = 5; | |
4ee14879 VZ |
261 | caret->Move(m_xMargin, m_yMargin); |
262 | caret->Show(); | |
0290598f VZ |
263 | } |
264 | ||
265 | MyCanvas::~MyCanvas() | |
266 | { | |
267 | free(m_text); | |
268 | } | |
269 | ||
270 | void MyCanvas::OnSize( wxSizeEvent &event ) | |
271 | { | |
272 | m_xChars = (event.GetSize().x - 2*m_xMargin) / m_widthChar; | |
273 | m_yChars = (event.GetSize().y - 2*m_yMargin) / m_heightChar; | |
274 | if ( !m_xChars ) | |
275 | m_xChars = 1; | |
276 | if ( !m_yChars ) | |
277 | m_yChars = 1; | |
278 | ||
279 | free(m_text); | |
92607616 | 280 | m_text = (wxChar *)calloc(m_xChars * m_yChars, sizeof(wxChar)); |
0290598f | 281 | |
4ee14879 VZ |
282 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); |
283 | ||
284 | if ( frame && frame->GetStatusBar() ) | |
285 | { | |
286 | wxString msg; | |
287 | msg.Printf(_T("Panel size is (%d, %d)"), m_xChars, m_yChars); | |
0290598f | 288 | |
4ee14879 VZ |
289 | frame->SetStatusText(msg, 1); |
290 | } | |
0290598f VZ |
291 | |
292 | event.Skip(); | |
293 | } | |
294 | ||
295 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
296 | { | |
297 | wxPaintDC dc( this ); | |
298 | PrepareDC( dc ); | |
299 | ||
300 | dc.SetFont( m_font ); | |
301 | ||
302 | for ( int y = 0; y < m_yChars; y++ ) | |
303 | { | |
304 | wxString line; | |
305 | ||
306 | for ( int x = 0; x < m_xChars; x++ ) | |
307 | { | |
92607616 | 308 | wxChar ch = CharAt(x, y); |
0290598f | 309 | if ( !ch ) |
92607616 | 310 | ch = _T(' '); |
0290598f VZ |
311 | line += ch; |
312 | } | |
313 | ||
314 | dc.DrawText( line, m_xMargin, m_yMargin + y * m_heightChar ); | |
315 | } | |
316 | } | |
317 | ||
318 | void MyCanvas::OnChar( wxKeyEvent &event ) | |
319 | { | |
320 | switch ( event.KeyCode() ) | |
321 | { | |
322 | case WXK_LEFT: | |
323 | PrevChar(); | |
324 | break; | |
325 | ||
326 | case WXK_RIGHT: | |
327 | NextChar(); | |
328 | break; | |
329 | ||
330 | case WXK_UP: | |
331 | PrevLine(); | |
332 | break; | |
333 | ||
334 | case WXK_DOWN: | |
335 | NextLine(); | |
336 | break; | |
337 | ||
338 | case WXK_HOME: | |
339 | Home(); | |
340 | break; | |
341 | ||
342 | case WXK_END: | |
343 | End(); | |
344 | break; | |
345 | ||
346 | case WXK_RETURN: | |
347 | Home(); | |
348 | NextLine(); | |
349 | break; | |
350 | ||
351 | default: | |
7e12d1bf | 352 | if ( !event.AltDown() && wxIsprint(event.KeyCode()) ) |
0290598f | 353 | { |
92607616 | 354 | CharAt(m_xCaret, m_yCaret) = (wxChar)event.KeyCode(); |
0290598f VZ |
355 | NextChar(); |
356 | } | |
357 | else | |
358 | { | |
7e12d1bf VZ |
359 | event.Skip(); |
360 | ||
0290598f VZ |
361 | // don't refresh |
362 | return; | |
363 | } | |
364 | } | |
365 | ||
92607616 | 366 | wxLogStatus(_T("Caret is at (%d, %d)"), m_xCaret, m_yCaret); |
0290598f | 367 | |
4ee14879 VZ |
368 | GetCaret()->Move(m_xMargin + m_xCaret * m_widthChar, |
369 | m_yMargin + m_yCaret * m_heightChar); | |
0290598f VZ |
370 | |
371 | Refresh(); | |
372 | } | |
373 |