]>
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 | |
0290598f VZ |
7 | // Copyright: (c) wxWindows team |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
0290598f | 11 | // For compilers that support precompilation, includes "wx/wx.h". |
92a19c2e | 12 | #include "wx/wxprec.h" |
0290598f VZ |
13 | |
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | // for all others, include the necessary headers (this file is usually all you | |
be5a51fb | 19 | // need because it includes almost all <standard< wxWidgets headers |
0290598f | 20 | #ifndef WX_PRECOMP |
012f2cb2 | 21 | #include "wx/wx.h" |
012f2cb2 | 22 | #include "wx/log.h" |
0290598f VZ |
23 | #endif |
24 | ||
25 | #include "wx/caret.h" | |
e4f3eb42 | 26 | #include "wx/numdlg.h" |
0290598f VZ |
27 | |
28 | // ---------------------------------------------------------------------------- | |
3cb332c1 | 29 | // resources |
0290598f | 30 | // ---------------------------------------------------------------------------- |
3cb332c1 | 31 | |
0290598f | 32 | // the application icon |
e7092398 | 33 | #ifndef wxHAS_IMAGES_IN_RESOURCES |
3cb332c1 | 34 | #include "../sample.xpm" |
0290598f VZ |
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 | ||
0290598f VZ |
54 | // MyCanvas is a canvas on which you can type |
55 | class MyCanvas: public wxScrolledWindow | |
56 | { | |
57 | public: | |
58 | MyCanvas() { } | |
59 | MyCanvas( wxWindow *parent ); | |
60 | ~MyCanvas(); | |
61 | ||
92607616 | 62 | wxChar& CharAt(int x, int y) { return *(m_text + x + m_xChars * y); } |
0290598f | 63 | |
697e0cdd | 64 | // operations |
fb476982 | 65 | void SetFontSize(int fontSize); |
697e0cdd VZ |
66 | void CreateCaret(); |
67 | void MoveCaret(int x, int y); | |
68 | ||
0290598f VZ |
69 | // caret movement |
70 | void Home() { m_xCaret = 0; } | |
71 | void End() { m_xCaret = m_xChars - 1; } | |
72 | void FirstLine() { m_yCaret = 0; } | |
73 | void LastLine() { m_yCaret = m_yChars - 1; } | |
74 | void PrevChar() { if ( !m_xCaret-- ) { End(); PrevLine(); } } | |
75 | void NextChar() { if ( ++m_xCaret == m_xChars ) { Home(); NextLine(); } } | |
76 | void PrevLine() { if ( !m_yCaret-- ) LastLine(); } | |
77 | void NextLine() { if ( ++m_yCaret == m_yChars ) FirstLine(); } | |
78 | ||
79 | // event handlers | |
80 | void OnPaint( wxPaintEvent &event ); | |
81 | void OnSize( wxSizeEvent &event ); | |
82 | void OnChar( wxKeyEvent &event ); | |
83 | ||
84 | private: | |
697e0cdd VZ |
85 | // move the caret to m_xCaret, m_yCaret |
86 | void DoMoveCaret(); | |
87 | ||
fb476982 VZ |
88 | // update the geometry |
89 | void ChangeSize(); | |
90 | ||
0290598f VZ |
91 | wxFont m_font; |
92 | ||
93 | // the margin around the text (looks nicer) | |
94 | int m_xMargin, m_yMargin; | |
95 | ||
96 | // size (in pixels) of one character | |
97 | long m_widthChar, m_heightChar; | |
98 | ||
99 | // position (in text coords) of the caret | |
100 | int m_xCaret, m_yCaret; | |
101 | ||
102 | // the size (in text coords) of the window | |
103 | int m_xChars, m_yChars; | |
104 | ||
105 | // the text | |
92607616 | 106 | wxChar *m_text; |
0290598f VZ |
107 | |
108 | DECLARE_DYNAMIC_CLASS(MyCanvas) | |
109 | DECLARE_EVENT_TABLE() | |
110 | }; | |
111 | ||
697e0cdd VZ |
112 | |
113 | // Define a new frame type: this is going to be our main frame | |
114 | class MyFrame : public wxFrame | |
115 | { | |
116 | public: | |
117 | // ctor(s) | |
118 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
119 | ||
120 | // event handlers (these functions should _not_ be virtual) | |
121 | void OnQuit(wxCommandEvent& event); | |
122 | void OnAbout(wxCommandEvent& event); | |
123 | void OnSetBlinkTime(wxCommandEvent& event); | |
fb476982 | 124 | void OnSetFontSize(wxCommandEvent& event); |
697e0cdd VZ |
125 | void OnCaretMove(wxCommandEvent& event); |
126 | ||
127 | private: | |
128 | MyCanvas *m_canvas; | |
129 | ||
be5a51fb | 130 | // any class wishing to process wxWidgets events must use this macro |
697e0cdd VZ |
131 | DECLARE_EVENT_TABLE() |
132 | }; | |
133 | ||
0290598f VZ |
134 | // ---------------------------------------------------------------------------- |
135 | // constants | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
138 | // IDs for the controls and the menu commands | |
139 | enum | |
140 | { | |
141 | // menu items | |
7e12d1bf VZ |
142 | Caret_Quit = 1, |
143 | Caret_About, | |
f6bcfd97 | 144 | Caret_SetBlinkTime, |
fb476982 | 145 | Caret_SetFontSize, |
697e0cdd | 146 | Caret_Move, |
0290598f VZ |
147 | |
148 | // controls start here (the numbers are, of course, arbitrary) | |
7e12d1bf | 149 | Caret_Text = 1000 |
0290598f VZ |
150 | }; |
151 | ||
152 | // ---------------------------------------------------------------------------- | |
be5a51fb | 153 | // event tables and other macros for wxWidgets |
0290598f VZ |
154 | // ---------------------------------------------------------------------------- |
155 | ||
be5a51fb | 156 | // the event tables connect the wxWidgets events with the functions (event |
0290598f VZ |
157 | // handlers) which process them. It can be also done at run-time, but for the |
158 | // simple menu events like this the static method is much simpler. | |
159 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
7e12d1bf VZ |
160 | EVT_MENU(Caret_Quit, MyFrame::OnQuit) |
161 | EVT_MENU(Caret_About, MyFrame::OnAbout) | |
f6bcfd97 | 162 | EVT_MENU(Caret_SetBlinkTime, MyFrame::OnSetBlinkTime) |
fb476982 | 163 | EVT_MENU(Caret_SetFontSize, MyFrame::OnSetFontSize) |
697e0cdd | 164 | EVT_MENU(Caret_Move, MyFrame::OnCaretMove) |
0290598f VZ |
165 | END_EVENT_TABLE() |
166 | ||
be5a51fb | 167 | // Create a new application object: this macro will allow wxWidgets to create |
0290598f VZ |
168 | // the application object during program execution (it's better than using a |
169 | // static object for many reasons) and also declares the accessor function | |
170 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
171 | // not wxApp) | |
172 | IMPLEMENT_APP(MyApp) | |
173 | ||
174 | // ============================================================================ | |
175 | // implementation | |
176 | // ============================================================================ | |
177 | ||
178 | // ---------------------------------------------------------------------------- | |
179 | // the application class | |
180 | // ---------------------------------------------------------------------------- | |
181 | ||
182 | // `Main program' equivalent: the program execution "starts" here | |
183 | bool MyApp::OnInit() | |
184 | { | |
45e6e6f8 VZ |
185 | if ( !wxApp::OnInit() ) |
186 | return false; | |
187 | ||
697e0cdd | 188 | // create and show the main application window |
9a83f860 | 189 | MyFrame *frame = new MyFrame(wxT("Caret wxWidgets sample"), |
0290598f VZ |
190 | wxPoint(50, 50), wxSize(450, 340)); |
191 | ||
5014bb3a | 192 | frame->Show(true); |
0290598f VZ |
193 | |
194 | // success: wxApp::OnRun() will be called which will enter the main message | |
5014bb3a | 195 | // loop and the application will run. If we returned false here, the |
0290598f | 196 | // application would exit immediately. |
5014bb3a | 197 | return true; |
0290598f VZ |
198 | } |
199 | ||
200 | // ---------------------------------------------------------------------------- | |
201 | // main frame | |
202 | // ---------------------------------------------------------------------------- | |
203 | ||
204 | // frame constructor | |
205 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
5014bb3a | 206 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) |
0290598f VZ |
207 | { |
208 | // set the frame icon | |
3cb332c1 | 209 | SetIcon(wxICON(sample)); |
0290598f VZ |
210 | |
211 | // create a menu bar | |
212 | wxMenu *menuFile = new wxMenu; | |
213 | ||
9a83f860 VZ |
214 | menuFile->Append(Caret_SetBlinkTime, wxT("&Blink time...\tCtrl-B")); |
215 | menuFile->Append(Caret_SetFontSize, wxT("&Font size...\tCtrl-S")); | |
216 | menuFile->Append(Caret_Move, wxT("&Move caret\tCtrl-C")); | |
f6bcfd97 | 217 | menuFile->AppendSeparator(); |
2d143b66 | 218 | menuFile->Append(Caret_About, wxT("&About\tCtrl-A"), wxT("Show about dialog")); |
0290598f | 219 | menuFile->AppendSeparator(); |
9a83f860 | 220 | menuFile->Append(Caret_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program")); |
0290598f VZ |
221 | |
222 | // now append the freshly created menu to the menu bar... | |
223 | wxMenuBar *menuBar = new wxMenuBar; | |
9a83f860 | 224 | menuBar->Append(menuFile, wxT("&File")); |
0290598f VZ |
225 | |
226 | // ... and attach this menu bar to the frame | |
227 | SetMenuBar(menuBar); | |
ce00f59b | 228 | |
697e0cdd | 229 | m_canvas = new MyCanvas(this); |
0290598f | 230 | |
8520f137 | 231 | #if wxUSE_STATUSBAR |
0290598f VZ |
232 | // create a status bar just for fun (by default with 1 pane only) |
233 | CreateStatusBar(2); | |
9a83f860 | 234 | SetStatusText(wxT("Welcome to wxWidgets!")); |
8520f137 | 235 | #endif // wxUSE_STATUSBAR |
0290598f VZ |
236 | } |
237 | ||
238 | ||
239 | // event handlers | |
240 | ||
241 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
242 | { | |
5014bb3a WS |
243 | // true is to force the frame to close |
244 | Close(true); | |
0290598f VZ |
245 | } |
246 | ||
247 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
248 | { | |
9a83f860 VZ |
249 | wxMessageBox(wxT("The caret wxWidgets sample.\n(c) 1999 Vadim Zeitlin"), |
250 | wxT("About Caret"), wxOK | wxICON_INFORMATION, this); | |
0290598f VZ |
251 | } |
252 | ||
697e0cdd VZ |
253 | void MyFrame::OnCaretMove(wxCommandEvent& WXUNUSED(event)) |
254 | { | |
255 | m_canvas->MoveCaret(10, 10); | |
256 | } | |
257 | ||
f6bcfd97 BP |
258 | void MyFrame::OnSetBlinkTime(wxCommandEvent& WXUNUSED(event)) |
259 | { | |
260 | long blinkTime = wxGetNumberFromUser | |
261 | ( | |
9a83f860 VZ |
262 | wxT("The caret blink time is the time between two blinks"), |
263 | wxT("Time in milliseconds:"), | |
264 | wxT("wxCaret sample"), | |
f6bcfd97 BP |
265 | wxCaret::GetBlinkTime(), 0, 10000, |
266 | this | |
267 | ); | |
268 | if ( blinkTime != -1 ) | |
269 | { | |
270 | wxCaret::SetBlinkTime((int)blinkTime); | |
697e0cdd | 271 | m_canvas->CreateCaret(); |
9a83f860 | 272 | wxLogStatus(this, wxT("Blink time set to %ld milliseconds."), blinkTime); |
f6bcfd97 BP |
273 | } |
274 | } | |
0290598f | 275 | |
fb476982 VZ |
276 | void MyFrame::OnSetFontSize(wxCommandEvent& WXUNUSED(event)) |
277 | { | |
278 | long fontSize = wxGetNumberFromUser | |
279 | ( | |
9a83f860 VZ |
280 | wxT("The font size also determines the caret size so\nthis demonstrates resizing the caret."), |
281 | wxT("Font size (in points):"), | |
282 | wxT("wxCaret sample"), | |
fb476982 VZ |
283 | 12, 1, 100, |
284 | this | |
285 | ); | |
286 | ||
287 | if ( fontSize != -1 ) | |
288 | { | |
289 | m_canvas->SetFontSize((int)fontSize); | |
290 | } | |
291 | } | |
292 | ||
0290598f VZ |
293 | // ---------------------------------------------------------------------------- |
294 | // MyCanvas | |
295 | // ---------------------------------------------------------------------------- | |
296 | ||
297 | IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow) | |
298 | ||
299 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
300 | EVT_PAINT(MyCanvas::OnPaint) | |
301 | EVT_SIZE(MyCanvas::OnSize) | |
302 | EVT_CHAR(MyCanvas::OnChar) | |
303 | END_EVENT_TABLE() | |
304 | ||
305 | MyCanvas::MyCanvas( wxWindow *parent ) | |
5014bb3a | 306 | : wxScrolledWindow( parent, wxID_ANY, |
0290598f VZ |
307 | wxDefaultPosition, wxDefaultSize, |
308 | wxSUNKEN_BORDER ) | |
309 | { | |
92607616 | 310 | m_text = (wxChar *)NULL; |
a8f25787 | 311 | |
697e0cdd VZ |
312 | SetBackgroundColour(*wxWHITE); |
313 | ||
fb476982 | 314 | SetFontSize(12); |
697e0cdd VZ |
315 | |
316 | m_xCaret = m_yCaret = | |
317 | m_xChars = m_yChars = 0; | |
0290598f | 318 | |
697e0cdd VZ |
319 | m_xMargin = m_yMargin = 5; |
320 | ||
321 | CreateCaret(); | |
322 | } | |
0290598f | 323 | |
697e0cdd VZ |
324 | MyCanvas::~MyCanvas() |
325 | { | |
326 | free(m_text); | |
327 | } | |
328 | ||
329 | void MyCanvas::CreateCaret() | |
330 | { | |
fb476982 VZ |
331 | wxCaret *caret = new wxCaret(this, m_widthChar, m_heightChar); |
332 | SetCaret(caret); | |
333 | ||
334 | caret->Move(m_xMargin, m_yMargin); | |
335 | caret->Show(); | |
336 | } | |
337 | ||
338 | void MyCanvas::SetFontSize(int fontSize) | |
339 | { | |
340 | m_font = wxFont(fontSize, wxFONTFAMILY_TELETYPE, | |
341 | wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL); | |
342 | ||
0290598f | 343 | wxClientDC dc(this); |
697e0cdd | 344 | dc.SetFont(m_font); |
0290598f VZ |
345 | m_heightChar = dc.GetCharHeight(); |
346 | m_widthChar = dc.GetCharWidth(); | |
347 | ||
fb476982 VZ |
348 | wxCaret *caret = GetCaret(); |
349 | if ( caret ) | |
350 | { | |
351 | caret->SetSize(m_widthChar, m_heightChar); | |
0290598f | 352 | |
fb476982 VZ |
353 | ChangeSize(); |
354 | } | |
0290598f VZ |
355 | } |
356 | ||
697e0cdd | 357 | void MyCanvas::MoveCaret(int x, int y) |
0290598f | 358 | { |
697e0cdd VZ |
359 | m_xCaret = x; |
360 | m_yCaret = y; | |
361 | ||
362 | DoMoveCaret(); | |
363 | } | |
364 | ||
365 | void MyCanvas::DoMoveCaret() | |
366 | { | |
9a83f860 | 367 | wxLogStatus(wxT("Caret is at (%d, %d)"), m_xCaret, m_yCaret); |
697e0cdd VZ |
368 | |
369 | GetCaret()->Move(m_xMargin + m_xCaret * m_widthChar, | |
370 | m_yMargin + m_yCaret * m_heightChar); | |
0290598f VZ |
371 | } |
372 | ||
fb476982 | 373 | void MyCanvas::OnSize(wxSizeEvent& event) |
0290598f | 374 | { |
fb476982 VZ |
375 | ChangeSize(); |
376 | ||
377 | event.Skip(); | |
378 | } | |
379 | ||
380 | void MyCanvas::ChangeSize() | |
381 | { | |
382 | wxSize size = GetClientSize(); | |
383 | m_xChars = (size.x - 2*m_xMargin) / m_widthChar; | |
384 | m_yChars = (size.y - 2*m_yMargin) / m_heightChar; | |
0290598f VZ |
385 | if ( !m_xChars ) |
386 | m_xChars = 1; | |
387 | if ( !m_yChars ) | |
388 | m_yChars = 1; | |
389 | ||
390 | free(m_text); | |
92607616 | 391 | m_text = (wxChar *)calloc(m_xChars * m_yChars, sizeof(wxChar)); |
0290598f | 392 | |
8520f137 | 393 | #if wxUSE_STATUSBAR |
4ee14879 VZ |
394 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); |
395 | ||
396 | if ( frame && frame->GetStatusBar() ) | |
397 | { | |
398 | wxString msg; | |
9a83f860 | 399 | msg.Printf(wxT("Panel size is (%d, %d)"), m_xChars, m_yChars); |
4ee14879 VZ |
400 | frame->SetStatusText(msg, 1); |
401 | } | |
8520f137 | 402 | #endif // wxUSE_STATUSBAR |
0290598f VZ |
403 | } |
404 | ||
f6bcfd97 BP |
405 | // NB: this method is horrible inefficient especially because the caret |
406 | // needs to be redrawn often and in this case we only have to redraw | |
407 | // the caret location and not the entire window - in a real program we | |
408 | // would use GetUpdateRegion() and iterate over rectangles it contains | |
0290598f VZ |
409 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
410 | { | |
f4b8bf2f | 411 | wxCaretSuspend cs(this); |
0290598f VZ |
412 | wxPaintDC dc( this ); |
413 | PrepareDC( dc ); | |
f6bcfd97 | 414 | dc.Clear(); |
ce00f59b | 415 | |
0290598f VZ |
416 | dc.SetFont( m_font ); |
417 | ||
418 | for ( int y = 0; y < m_yChars; y++ ) | |
419 | { | |
420 | wxString line; | |
421 | ||
422 | for ( int x = 0; x < m_xChars; x++ ) | |
423 | { | |
92607616 | 424 | wxChar ch = CharAt(x, y); |
0290598f | 425 | if ( !ch ) |
9a83f860 | 426 | ch = wxT(' '); |
21e02d56 SC |
427 | #ifdef __WXOSX__ |
428 | dc.DrawText(ch, m_xMargin + x * m_widthChar, | |
429 | m_yMargin + y * m_heightChar ); | |
430 | #else | |
0290598f | 431 | line += ch; |
21e02d56 | 432 | #endif |
0290598f VZ |
433 | } |
434 | ||
21e02d56 | 435 | #ifndef __WXOSX__ |
0290598f | 436 | dc.DrawText( line, m_xMargin, m_yMargin + y * m_heightChar ); |
21e02d56 | 437 | #endif |
0290598f VZ |
438 | } |
439 | } | |
440 | ||
441 | void MyCanvas::OnChar( wxKeyEvent &event ) | |
442 | { | |
b1d4dd7a | 443 | switch ( event.GetKeyCode() ) |
0290598f VZ |
444 | { |
445 | case WXK_LEFT: | |
446 | PrevChar(); | |
447 | break; | |
448 | ||
449 | case WXK_RIGHT: | |
450 | NextChar(); | |
451 | break; | |
452 | ||
453 | case WXK_UP: | |
454 | PrevLine(); | |
455 | break; | |
456 | ||
457 | case WXK_DOWN: | |
458 | NextLine(); | |
459 | break; | |
460 | ||
461 | case WXK_HOME: | |
462 | Home(); | |
463 | break; | |
464 | ||
465 | case WXK_END: | |
466 | End(); | |
467 | break; | |
468 | ||
469 | case WXK_RETURN: | |
470 | Home(); | |
471 | NextLine(); | |
472 | break; | |
473 | ||
474 | default: | |
b1d4dd7a | 475 | if ( !event.AltDown() && wxIsprint(event.GetKeyCode()) ) |
0290598f | 476 | { |
b1d4dd7a | 477 | wxChar ch = (wxChar)event.GetKeyCode(); |
f4b8bf2f VZ |
478 | CharAt(m_xCaret, m_yCaret) = ch; |
479 | ||
480 | wxCaretSuspend cs(this); | |
481 | wxClientDC dc(this); | |
482 | dc.SetFont(m_font); | |
483 | dc.SetBackgroundMode(wxSOLID); // overwrite old value | |
484 | dc.DrawText(ch, m_xMargin + m_xCaret * m_widthChar, | |
485 | m_yMargin + m_yCaret * m_heightChar ); | |
697e0cdd | 486 | |
f4b8bf2f | 487 | NextChar(); |
0290598f VZ |
488 | } |
489 | else | |
490 | { | |
7e12d1bf | 491 | event.Skip(); |
0290598f VZ |
492 | } |
493 | } | |
494 | ||
697e0cdd | 495 | DoMoveCaret(); |
0290598f VZ |
496 | } |
497 |