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