]> git.saurik.com Git - wxWidgets.git/blame - samples/caret/caret.cpp
PCH support.
[wxWidgets.git] / samples / caret / caret.cpp
CommitLineData
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 12// For compilers that support precompilation, includes "wx/wx.h".
92a19c2e 13#include "wx/wxprec.h"
0290598f
VZ
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19// for all others, include the necessary headers (this file is usually all you
be5a51fb 20// need because it includes almost all <standard< wxWidgets headers
0290598f 21#ifndef WX_PRECOMP
012f2cb2 22 #include "wx/wx.h"
012f2cb2 23 #include "wx/log.h"
0290598f
VZ
24#endif
25
26#include "wx/caret.h"
e4f3eb42 27#include "wx/numdlg.h"
0290598f
VZ
28
29// ----------------------------------------------------------------------------
30// ressources
31// ----------------------------------------------------------------------------
32// the application icon
618f2efa 33#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
0290598f
VZ
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
42class MyApp : public wxApp
43{
44public:
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
55class MyCanvas: public wxScrolledWindow
56{
57public:
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
84private:
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
114class MyFrame : public wxFrame
115{
116public:
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
127private:
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
139enum
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.
159BEGIN_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
165END_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)
172IMPLEMENT_APP(MyApp)
173
174// ============================================================================
175// implementation
176// ============================================================================
177
178// ----------------------------------------------------------------------------
179// the application class
180// ----------------------------------------------------------------------------
181
182// `Main program' equivalent: the program execution "starts" here
183bool MyApp::OnInit()
184{
697e0cdd 185 // create and show the main application window
be5a51fb 186 MyFrame *frame = new MyFrame(_T("Caret wxWidgets sample"),
0290598f
VZ
187 wxPoint(50, 50), wxSize(450, 340));
188
5014bb3a 189 frame->Show(true);
0290598f
VZ
190
191 // success: wxApp::OnRun() will be called which will enter the main message
5014bb3a 192 // loop and the application will run. If we returned false here, the
0290598f 193 // application would exit immediately.
5014bb3a 194 return true;
0290598f
VZ
195}
196
197// ----------------------------------------------------------------------------
198// main frame
199// ----------------------------------------------------------------------------
200
201// frame constructor
202MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
5014bb3a 203 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
0290598f
VZ
204{
205 // set the frame icon
206 SetIcon(wxICON(mondrian));
207
208 // create a menu bar
209 wxMenu *menuFile = new wxMenu;
210
9f84eccd 211 menuFile->Append(Caret_SetBlinkTime, _T("&Blink time...\tCtrl-B"));
fb476982 212 menuFile->Append(Caret_SetFontSize, _T("&Font size...\tCtrl-S"));
9f84eccd 213 menuFile->Append(Caret_Move, _T("&Move caret\tCtrl-C"));
f6bcfd97 214 menuFile->AppendSeparator();
9f84eccd 215 menuFile->Append(Caret_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
0290598f 216 menuFile->AppendSeparator();
9f84eccd 217 menuFile->Append(Caret_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
0290598f
VZ
218
219 // now append the freshly created menu to the menu bar...
220 wxMenuBar *menuBar = new wxMenuBar;
9f84eccd 221 menuBar->Append(menuFile, _T("&File"));
0290598f
VZ
222
223 // ... and attach this menu bar to the frame
224 SetMenuBar(menuBar);
225
697e0cdd 226 m_canvas = new MyCanvas(this);
0290598f 227
8520f137 228#if wxUSE_STATUSBAR
0290598f
VZ
229 // create a status bar just for fun (by default with 1 pane only)
230 CreateStatusBar(2);
be5a51fb 231 SetStatusText(_T("Welcome to wxWidgets!"));
8520f137 232#endif // wxUSE_STATUSBAR
0290598f
VZ
233}
234
235
236// event handlers
237
238void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
239{
5014bb3a
WS
240 // true is to force the frame to close
241 Close(true);
0290598f
VZ
242}
243
244void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
245{
749bfe9a 246 wxMessageBox(_T("The caret wxWidgets sample.\n(c) 1999 Vadim Zeitlin"),
f6bcfd97 247 _T("About Caret"), wxOK | wxICON_INFORMATION, this);
0290598f
VZ
248}
249
697e0cdd
VZ
250void MyFrame::OnCaretMove(wxCommandEvent& WXUNUSED(event))
251{
252 m_canvas->MoveCaret(10, 10);
253}
254
f6bcfd97
BP
255void MyFrame::OnSetBlinkTime(wxCommandEvent& WXUNUSED(event))
256{
257 long blinkTime = wxGetNumberFromUser
258 (
259 _T("The caret blink time is the time between two blinks"),
260 _T("Time in milliseconds:"),
261 _T("wxCaret sample"),
262 wxCaret::GetBlinkTime(), 0, 10000,
263 this
264 );
265 if ( blinkTime != -1 )
266 {
267 wxCaret::SetBlinkTime((int)blinkTime);
697e0cdd 268 m_canvas->CreateCaret();
f6bcfd97
BP
269 wxLogStatus(this, _T("Blink time set to %ld milliseconds."), blinkTime);
270 }
271}
0290598f 272
fb476982
VZ
273void MyFrame::OnSetFontSize(wxCommandEvent& WXUNUSED(event))
274{
275 long fontSize = wxGetNumberFromUser
276 (
529b7f71 277 _T("The font size also determines the caret size so\nthis demonstrates resizing the caret."),
fb476982
VZ
278 _T("Font size (in points):"),
279 _T("wxCaret sample"),
280 12, 1, 100,
281 this
282 );
283
284 if ( fontSize != -1 )
285 {
286 m_canvas->SetFontSize((int)fontSize);
287 }
288}
289
0290598f
VZ
290// ----------------------------------------------------------------------------
291// MyCanvas
292// ----------------------------------------------------------------------------
293
294IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
295
296BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
297 EVT_PAINT(MyCanvas::OnPaint)
298 EVT_SIZE(MyCanvas::OnSize)
299 EVT_CHAR(MyCanvas::OnChar)
300END_EVENT_TABLE()
301
302MyCanvas::MyCanvas( wxWindow *parent )
5014bb3a 303 : wxScrolledWindow( parent, wxID_ANY,
0290598f
VZ
304 wxDefaultPosition, wxDefaultSize,
305 wxSUNKEN_BORDER )
306{
92607616 307 m_text = (wxChar *)NULL;
a8f25787 308
697e0cdd
VZ
309 SetBackgroundColour(*wxWHITE);
310
fb476982 311 SetFontSize(12);
697e0cdd
VZ
312
313 m_xCaret = m_yCaret =
314 m_xChars = m_yChars = 0;
0290598f 315
697e0cdd
VZ
316 m_xMargin = m_yMargin = 5;
317
318 CreateCaret();
319}
0290598f 320
697e0cdd
VZ
321MyCanvas::~MyCanvas()
322{
323 free(m_text);
324}
325
326void MyCanvas::CreateCaret()
327{
fb476982
VZ
328 wxCaret *caret = new wxCaret(this, m_widthChar, m_heightChar);
329 SetCaret(caret);
330
331 caret->Move(m_xMargin, m_yMargin);
332 caret->Show();
333}
334
335void MyCanvas::SetFontSize(int fontSize)
336{
337 m_font = wxFont(fontSize, wxFONTFAMILY_TELETYPE,
338 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
339
0290598f 340 wxClientDC dc(this);
697e0cdd 341 dc.SetFont(m_font);
0290598f
VZ
342 m_heightChar = dc.GetCharHeight();
343 m_widthChar = dc.GetCharWidth();
344
fb476982
VZ
345 wxCaret *caret = GetCaret();
346 if ( caret )
347 {
348 caret->SetSize(m_widthChar, m_heightChar);
0290598f 349
fb476982
VZ
350 ChangeSize();
351 }
0290598f
VZ
352}
353
697e0cdd 354void MyCanvas::MoveCaret(int x, int y)
0290598f 355{
697e0cdd
VZ
356 m_xCaret = x;
357 m_yCaret = y;
358
359 DoMoveCaret();
360}
361
362void MyCanvas::DoMoveCaret()
363{
364 wxLogStatus(_T("Caret is at (%d, %d)"), m_xCaret, m_yCaret);
365
366 GetCaret()->Move(m_xMargin + m_xCaret * m_widthChar,
367 m_yMargin + m_yCaret * m_heightChar);
0290598f
VZ
368}
369
fb476982 370void MyCanvas::OnSize(wxSizeEvent& event)
0290598f 371{
fb476982
VZ
372 ChangeSize();
373
374 event.Skip();
375}
376
377void MyCanvas::ChangeSize()
378{
379 wxSize size = GetClientSize();
380 m_xChars = (size.x - 2*m_xMargin) / m_widthChar;
381 m_yChars = (size.y - 2*m_yMargin) / m_heightChar;
0290598f
VZ
382 if ( !m_xChars )
383 m_xChars = 1;
384 if ( !m_yChars )
385 m_yChars = 1;
386
387 free(m_text);
92607616 388 m_text = (wxChar *)calloc(m_xChars * m_yChars, sizeof(wxChar));
0290598f 389
8520f137 390#if wxUSE_STATUSBAR
4ee14879
VZ
391 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
392
393 if ( frame && frame->GetStatusBar() )
394 {
395 wxString msg;
396 msg.Printf(_T("Panel size is (%d, %d)"), m_xChars, m_yChars);
4ee14879
VZ
397 frame->SetStatusText(msg, 1);
398 }
8520f137 399#endif // wxUSE_STATUSBAR
0290598f
VZ
400}
401
f6bcfd97
BP
402// NB: this method is horrible inefficient especially because the caret
403// needs to be redrawn often and in this case we only have to redraw
404// the caret location and not the entire window - in a real program we
405// would use GetUpdateRegion() and iterate over rectangles it contains
0290598f
VZ
406void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
407{
f4b8bf2f 408 wxCaretSuspend cs(this);
0290598f
VZ
409 wxPaintDC dc( this );
410 PrepareDC( dc );
f6bcfd97 411 dc.Clear();
0290598f
VZ
412
413 dc.SetFont( m_font );
414
415 for ( int y = 0; y < m_yChars; y++ )
416 {
417 wxString line;
418
419 for ( int x = 0; x < m_xChars; x++ )
420 {
92607616 421 wxChar ch = CharAt(x, y);
0290598f 422 if ( !ch )
92607616 423 ch = _T(' ');
0290598f
VZ
424 line += ch;
425 }
426
427 dc.DrawText( line, m_xMargin, m_yMargin + y * m_heightChar );
428 }
429}
430
431void MyCanvas::OnChar( wxKeyEvent &event )
432{
b1d4dd7a 433 switch ( event.GetKeyCode() )
0290598f
VZ
434 {
435 case WXK_LEFT:
436 PrevChar();
437 break;
438
439 case WXK_RIGHT:
440 NextChar();
441 break;
442
443 case WXK_UP:
444 PrevLine();
445 break;
446
447 case WXK_DOWN:
448 NextLine();
449 break;
450
451 case WXK_HOME:
452 Home();
453 break;
454
455 case WXK_END:
456 End();
457 break;
458
459 case WXK_RETURN:
460 Home();
461 NextLine();
462 break;
463
464 default:
b1d4dd7a 465 if ( !event.AltDown() && wxIsprint(event.GetKeyCode()) )
0290598f 466 {
b1d4dd7a 467 wxChar ch = (wxChar)event.GetKeyCode();
f4b8bf2f
VZ
468 CharAt(m_xCaret, m_yCaret) = ch;
469
470 wxCaretSuspend cs(this);
471 wxClientDC dc(this);
472 dc.SetFont(m_font);
473 dc.SetBackgroundMode(wxSOLID); // overwrite old value
474 dc.DrawText(ch, m_xMargin + m_xCaret * m_widthChar,
475 m_yMargin + m_yCaret * m_heightChar );
697e0cdd 476
f4b8bf2f 477 NextChar();
0290598f
VZ
478 }
479 else
480 {
7e12d1bf 481 event.Skip();
0290598f
VZ
482 }
483 }
484
697e0cdd 485 DoMoveCaret();
0290598f
VZ
486}
487