]> git.saurik.com Git - wxWidgets.git/blob - samples/caret/caret.cpp
Implement NSMenuValidation protocol for the wxNSMenuItemTarget
[wxWidgets.git] / samples / caret / caret.cpp
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
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(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
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 // 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
62 wxChar& CharAt(int x, int y) { return *(m_text + x + m_xChars * y); }
63
64 // operations
65 void SetFontSize(int fontSize);
66 void CreateCaret();
67 void MoveCaret(int x, int y);
68
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:
85 // move the caret to m_xCaret, m_yCaret
86 void DoMoveCaret();
87
88 // update the geometry
89 void ChangeSize();
90
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
106 wxChar *m_text;
107
108 DECLARE_DYNAMIC_CLASS(MyCanvas)
109 DECLARE_EVENT_TABLE()
110 };
111
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);
124 void OnSetFontSize(wxCommandEvent& event);
125 void OnCaretMove(wxCommandEvent& event);
126
127 private:
128 MyCanvas *m_canvas;
129
130 // any class wishing to process wxWindows events must use this macro
131 DECLARE_EVENT_TABLE()
132 };
133
134 // ----------------------------------------------------------------------------
135 // constants
136 // ----------------------------------------------------------------------------
137
138 // IDs for the controls and the menu commands
139 enum
140 {
141 // menu items
142 Caret_Quit = 1,
143 Caret_About,
144 Caret_SetBlinkTime,
145 Caret_SetFontSize,
146 Caret_Move,
147
148 // controls start here (the numbers are, of course, arbitrary)
149 Caret_Text = 1000
150 };
151
152 // ----------------------------------------------------------------------------
153 // event tables and other macros for wxWindows
154 // ----------------------------------------------------------------------------
155
156 // the event tables connect the wxWindows events with the functions (event
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)
160 EVT_MENU(Caret_Quit, MyFrame::OnQuit)
161 EVT_MENU(Caret_About, MyFrame::OnAbout)
162 EVT_MENU(Caret_SetBlinkTime, MyFrame::OnSetBlinkTime)
163 EVT_MENU(Caret_SetFontSize, MyFrame::OnSetFontSize)
164 EVT_MENU(Caret_Move, MyFrame::OnCaretMove)
165 END_EVENT_TABLE()
166
167 // Create a new application object: this macro will allow wxWindows to create
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 {
185 // create and show the main application window
186 MyFrame *frame = new MyFrame(_T("Caret wxWindows sample"),
187 wxPoint(50, 50), wxSize(450, 340));
188
189 frame->Show(TRUE);
190
191 // success: wxApp::OnRun() will be called which will enter the main message
192 // loop and the application will run. If we returned FALSE here, the
193 // application would exit immediately.
194 return TRUE;
195 }
196
197 // ----------------------------------------------------------------------------
198 // main frame
199 // ----------------------------------------------------------------------------
200
201 // frame constructor
202 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
203 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
204 {
205 // set the frame icon
206 SetIcon(wxICON(mondrian));
207
208 // create a menu bar
209 wxMenu *menuFile = new wxMenu;
210
211 menuFile->Append(Caret_SetBlinkTime, _T("&Blink time...\tCtrl-B"));
212 menuFile->Append(Caret_SetFontSize, _T("&Font size...\tCtrl-S"));
213 menuFile->Append(Caret_Move, _T("&Move caret\tCtrl-C"));
214 menuFile->AppendSeparator();
215 menuFile->Append(Caret_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
216 menuFile->AppendSeparator();
217 menuFile->Append(Caret_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
218
219 // now append the freshly created menu to the menu bar...
220 wxMenuBar *menuBar = new wxMenuBar;
221 menuBar->Append(menuFile, _T("&File"));
222
223 // ... and attach this menu bar to the frame
224 SetMenuBar(menuBar);
225
226 m_canvas = new MyCanvas(this);
227
228 // create a status bar just for fun (by default with 1 pane only)
229 CreateStatusBar(2);
230 SetStatusText(_T("Welcome to wxWindows!"));
231 }
232
233
234 // event handlers
235
236 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
237 {
238 // TRUE is to force the frame to close
239 Close(TRUE);
240 }
241
242 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
243 {
244 wxMessageBox(_T("The caret wxWindows sample.\n© 1999 Vadim Zeitlin"),
245 _T("About Caret"), wxOK | wxICON_INFORMATION, this);
246 }
247
248 void MyFrame::OnCaretMove(wxCommandEvent& WXUNUSED(event))
249 {
250 m_canvas->MoveCaret(10, 10);
251 }
252
253 void MyFrame::OnSetBlinkTime(wxCommandEvent& WXUNUSED(event))
254 {
255 long blinkTime = wxGetNumberFromUser
256 (
257 _T("The caret blink time is the time between two blinks"),
258 _T("Time in milliseconds:"),
259 _T("wxCaret sample"),
260 wxCaret::GetBlinkTime(), 0, 10000,
261 this
262 );
263 if ( blinkTime != -1 )
264 {
265 wxCaret::SetBlinkTime((int)blinkTime);
266 m_canvas->CreateCaret();
267 wxLogStatus(this, _T("Blink time set to %ld milliseconds."), blinkTime);
268 }
269 }
270
271 void MyFrame::OnSetFontSize(wxCommandEvent& WXUNUSED(event))
272 {
273 long fontSize = wxGetNumberFromUser
274 (
275 _T("The font size also determines the caret size so\nthis demonstrates resizing the caret."),
276 _T("Font size (in points):"),
277 _T("wxCaret sample"),
278 12, 1, 100,
279 this
280 );
281
282 if ( fontSize != -1 )
283 {
284 m_canvas->SetFontSize((int)fontSize);
285 }
286 }
287
288 // ----------------------------------------------------------------------------
289 // MyCanvas
290 // ----------------------------------------------------------------------------
291
292 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
293
294 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
295 EVT_PAINT(MyCanvas::OnPaint)
296 EVT_SIZE(MyCanvas::OnSize)
297 EVT_CHAR(MyCanvas::OnChar)
298 END_EVENT_TABLE()
299
300 MyCanvas::MyCanvas( wxWindow *parent )
301 : wxScrolledWindow( parent, -1,
302 wxDefaultPosition, wxDefaultSize,
303 wxSUNKEN_BORDER )
304 {
305 m_text = (wxChar *)NULL;
306
307 SetBackgroundColour(*wxWHITE);
308
309 SetFontSize(12);
310
311 m_xCaret = m_yCaret =
312 m_xChars = m_yChars = 0;
313
314 m_xMargin = m_yMargin = 5;
315
316 CreateCaret();
317 }
318
319 MyCanvas::~MyCanvas()
320 {
321 free(m_text);
322 }
323
324 void MyCanvas::CreateCaret()
325 {
326 wxCaret *caret = new wxCaret(this, m_widthChar, m_heightChar);
327 SetCaret(caret);
328
329 caret->Move(m_xMargin, m_yMargin);
330 caret->Show();
331 }
332
333 void MyCanvas::SetFontSize(int fontSize)
334 {
335 m_font = wxFont(fontSize, wxFONTFAMILY_TELETYPE,
336 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
337
338 wxClientDC dc(this);
339 dc.SetFont(m_font);
340 m_heightChar = dc.GetCharHeight();
341 m_widthChar = dc.GetCharWidth();
342
343 wxCaret *caret = GetCaret();
344 if ( caret )
345 {
346 caret->SetSize(m_widthChar, m_heightChar);
347
348 ChangeSize();
349 }
350 }
351
352 void MyCanvas::MoveCaret(int x, int y)
353 {
354 m_xCaret = x;
355 m_yCaret = y;
356
357 DoMoveCaret();
358 }
359
360 void MyCanvas::DoMoveCaret()
361 {
362 wxLogStatus(_T("Caret is at (%d, %d)"), m_xCaret, m_yCaret);
363
364 GetCaret()->Move(m_xMargin + m_xCaret * m_widthChar,
365 m_yMargin + m_yCaret * m_heightChar);
366 }
367
368 void MyCanvas::OnSize(wxSizeEvent& event)
369 {
370 ChangeSize();
371
372 event.Skip();
373 }
374
375 void MyCanvas::ChangeSize()
376 {
377 wxSize size = GetClientSize();
378 m_xChars = (size.x - 2*m_xMargin) / m_widthChar;
379 m_yChars = (size.y - 2*m_yMargin) / m_heightChar;
380 if ( !m_xChars )
381 m_xChars = 1;
382 if ( !m_yChars )
383 m_yChars = 1;
384
385 free(m_text);
386 m_text = (wxChar *)calloc(m_xChars * m_yChars, sizeof(wxChar));
387
388 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
389
390 if ( frame && frame->GetStatusBar() )
391 {
392 wxString msg;
393 msg.Printf(_T("Panel size is (%d, %d)"), m_xChars, m_yChars);
394
395 frame->SetStatusText(msg, 1);
396 }
397 }
398
399 // NB: this method is horrible inefficient especially because the caret
400 // needs to be redrawn often and in this case we only have to redraw
401 // the caret location and not the entire window - in a real program we
402 // would use GetUpdateRegion() and iterate over rectangles it contains
403 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
404 {
405 wxCaretSuspend cs(this);
406 wxPaintDC dc( this );
407 PrepareDC( dc );
408 dc.Clear();
409
410 dc.SetFont( m_font );
411
412 for ( int y = 0; y < m_yChars; y++ )
413 {
414 wxString line;
415
416 for ( int x = 0; x < m_xChars; x++ )
417 {
418 wxChar ch = CharAt(x, y);
419 if ( !ch )
420 ch = _T(' ');
421 line += ch;
422 }
423
424 dc.DrawText( line, m_xMargin, m_yMargin + y * m_heightChar );
425 }
426 }
427
428 void MyCanvas::OnChar( wxKeyEvent &event )
429 {
430 switch ( event.GetKeyCode() )
431 {
432 case WXK_LEFT:
433 PrevChar();
434 break;
435
436 case WXK_RIGHT:
437 NextChar();
438 break;
439
440 case WXK_UP:
441 PrevLine();
442 break;
443
444 case WXK_DOWN:
445 NextLine();
446 break;
447
448 case WXK_HOME:
449 Home();
450 break;
451
452 case WXK_END:
453 End();
454 break;
455
456 case WXK_RETURN:
457 Home();
458 NextLine();
459 break;
460
461 default:
462 if ( !event.AltDown() && wxIsprint(event.GetKeyCode()) )
463 {
464 wxChar ch = (wxChar)event.GetKeyCode();
465 CharAt(m_xCaret, m_yCaret) = ch;
466
467 wxCaretSuspend cs(this);
468 wxClientDC dc(this);
469 dc.SetFont(m_font);
470 dc.SetBackgroundMode(wxSOLID); // overwrite old value
471 dc.DrawText(ch, m_xMargin + m_xCaret * m_widthChar,
472 m_yMargin + m_yCaret * m_heightChar );
473
474 NextChar();
475 }
476 else
477 {
478 event.Skip();
479 }
480 }
481
482 DoMoveCaret();
483 }
484