]> git.saurik.com Git - wxWidgets.git/blob - samples/caret/caret.cpp
Fix testing for existence of paths with trailing separators in wxMSW.
[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< wxWidgets headers
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23 #include "wx/log.h"
24 #endif
25
26 #include "wx/caret.h"
27 #include "wx/numdlg.h"
28
29 // ----------------------------------------------------------------------------
30 // resources
31 // ----------------------------------------------------------------------------
32
33 // the application icon
34 #ifndef wxHAS_IMAGES_IN_RESOURCES
35 #include "../sample.xpm"
36 #endif
37
38 // ----------------------------------------------------------------------------
39 // private classes
40 // ----------------------------------------------------------------------------
41
42 // Define a new application type, each program should derive a class from wxApp
43 class MyApp : public wxApp
44 {
45 public:
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
55 // MyCanvas is a canvas on which you can type
56 class MyCanvas: public wxScrolledWindow
57 {
58 public:
59 MyCanvas() { }
60 MyCanvas( wxWindow *parent );
61 ~MyCanvas();
62
63 wxChar& CharAt(int x, int y) { return *(m_text + x + m_xChars * y); }
64
65 // operations
66 void SetFontSize(int fontSize);
67 void CreateCaret();
68 void MoveCaret(int x, int y);
69
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
85 private:
86 // move the caret to m_xCaret, m_yCaret
87 void DoMoveCaret();
88
89 // update the geometry
90 void ChangeSize();
91
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
107 wxChar *m_text;
108
109 DECLARE_DYNAMIC_CLASS(MyCanvas)
110 DECLARE_EVENT_TABLE()
111 };
112
113
114 // Define a new frame type: this is going to be our main frame
115 class MyFrame : public wxFrame
116 {
117 public:
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);
125 void OnSetFontSize(wxCommandEvent& event);
126 void OnCaretMove(wxCommandEvent& event);
127
128 private:
129 MyCanvas *m_canvas;
130
131 // any class wishing to process wxWidgets events must use this macro
132 DECLARE_EVENT_TABLE()
133 };
134
135 // ----------------------------------------------------------------------------
136 // constants
137 // ----------------------------------------------------------------------------
138
139 // IDs for the controls and the menu commands
140 enum
141 {
142 // menu items
143 Caret_Quit = 1,
144 Caret_About,
145 Caret_SetBlinkTime,
146 Caret_SetFontSize,
147 Caret_Move,
148
149 // controls start here (the numbers are, of course, arbitrary)
150 Caret_Text = 1000
151 };
152
153 // ----------------------------------------------------------------------------
154 // event tables and other macros for wxWidgets
155 // ----------------------------------------------------------------------------
156
157 // the event tables connect the wxWidgets events with the functions (event
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.
160 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
161 EVT_MENU(Caret_Quit, MyFrame::OnQuit)
162 EVT_MENU(Caret_About, MyFrame::OnAbout)
163 EVT_MENU(Caret_SetBlinkTime, MyFrame::OnSetBlinkTime)
164 EVT_MENU(Caret_SetFontSize, MyFrame::OnSetFontSize)
165 EVT_MENU(Caret_Move, MyFrame::OnCaretMove)
166 END_EVENT_TABLE()
167
168 // Create a new application object: this macro will allow wxWidgets to create
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)
173 IMPLEMENT_APP(MyApp)
174
175 // ============================================================================
176 // implementation
177 // ============================================================================
178
179 // ----------------------------------------------------------------------------
180 // the application class
181 // ----------------------------------------------------------------------------
182
183 // `Main program' equivalent: the program execution "starts" here
184 bool MyApp::OnInit()
185 {
186 if ( !wxApp::OnInit() )
187 return false;
188
189 // create and show the main application window
190 MyFrame *frame = new MyFrame(wxT("Caret wxWidgets sample"),
191 wxPoint(50, 50), wxSize(450, 340));
192
193 frame->Show(true);
194
195 // success: wxApp::OnRun() will be called which will enter the main message
196 // loop and the application will run. If we returned false here, the
197 // application would exit immediately.
198 return true;
199 }
200
201 // ----------------------------------------------------------------------------
202 // main frame
203 // ----------------------------------------------------------------------------
204
205 // frame constructor
206 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
207 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
208 {
209 // set the frame icon
210 SetIcon(wxICON(sample));
211
212 // create a menu bar
213 wxMenu *menuFile = new wxMenu;
214
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"));
218 menuFile->AppendSeparator();
219 menuFile->Append(Caret_About, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
220 menuFile->AppendSeparator();
221 menuFile->Append(Caret_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
222
223 // now append the freshly created menu to the menu bar...
224 wxMenuBar *menuBar = new wxMenuBar;
225 menuBar->Append(menuFile, wxT("&File"));
226
227 // ... and attach this menu bar to the frame
228 SetMenuBar(menuBar);
229
230 m_canvas = new MyCanvas(this);
231
232 #if wxUSE_STATUSBAR
233 // create a status bar just for fun (by default with 1 pane only)
234 CreateStatusBar(2);
235 SetStatusText(wxT("Welcome to wxWidgets!"));
236 #endif // wxUSE_STATUSBAR
237 }
238
239
240 // event handlers
241
242 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
243 {
244 // true is to force the frame to close
245 Close(true);
246 }
247
248 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
249 {
250 wxMessageBox(wxT("The caret wxWidgets sample.\n(c) 1999 Vadim Zeitlin"),
251 wxT("About Caret"), wxOK | wxICON_INFORMATION, this);
252 }
253
254 void MyFrame::OnCaretMove(wxCommandEvent& WXUNUSED(event))
255 {
256 m_canvas->MoveCaret(10, 10);
257 }
258
259 void MyFrame::OnSetBlinkTime(wxCommandEvent& WXUNUSED(event))
260 {
261 long blinkTime = wxGetNumberFromUser
262 (
263 wxT("The caret blink time is the time between two blinks"),
264 wxT("Time in milliseconds:"),
265 wxT("wxCaret sample"),
266 wxCaret::GetBlinkTime(), 0, 10000,
267 this
268 );
269 if ( blinkTime != -1 )
270 {
271 wxCaret::SetBlinkTime((int)blinkTime);
272 m_canvas->CreateCaret();
273 wxLogStatus(this, wxT("Blink time set to %ld milliseconds."), blinkTime);
274 }
275 }
276
277 void MyFrame::OnSetFontSize(wxCommandEvent& WXUNUSED(event))
278 {
279 long fontSize = wxGetNumberFromUser
280 (
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"),
284 12, 1, 100,
285 this
286 );
287
288 if ( fontSize != -1 )
289 {
290 m_canvas->SetFontSize((int)fontSize);
291 }
292 }
293
294 // ----------------------------------------------------------------------------
295 // MyCanvas
296 // ----------------------------------------------------------------------------
297
298 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
299
300 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
301 EVT_PAINT(MyCanvas::OnPaint)
302 EVT_SIZE(MyCanvas::OnSize)
303 EVT_CHAR(MyCanvas::OnChar)
304 END_EVENT_TABLE()
305
306 MyCanvas::MyCanvas( wxWindow *parent )
307 : wxScrolledWindow( parent, wxID_ANY,
308 wxDefaultPosition, wxDefaultSize,
309 wxSUNKEN_BORDER )
310 {
311 m_text = (wxChar *)NULL;
312
313 SetBackgroundColour(*wxWHITE);
314
315 SetFontSize(12);
316
317 m_xCaret = m_yCaret =
318 m_xChars = m_yChars = 0;
319
320 m_xMargin = m_yMargin = 5;
321
322 CreateCaret();
323 }
324
325 MyCanvas::~MyCanvas()
326 {
327 free(m_text);
328 }
329
330 void MyCanvas::CreateCaret()
331 {
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
339 void MyCanvas::SetFontSize(int fontSize)
340 {
341 m_font = wxFont(fontSize, wxFONTFAMILY_TELETYPE,
342 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
343
344 wxClientDC dc(this);
345 dc.SetFont(m_font);
346 m_heightChar = dc.GetCharHeight();
347 m_widthChar = dc.GetCharWidth();
348
349 wxCaret *caret = GetCaret();
350 if ( caret )
351 {
352 caret->SetSize(m_widthChar, m_heightChar);
353
354 ChangeSize();
355 }
356 }
357
358 void MyCanvas::MoveCaret(int x, int y)
359 {
360 m_xCaret = x;
361 m_yCaret = y;
362
363 DoMoveCaret();
364 }
365
366 void MyCanvas::DoMoveCaret()
367 {
368 wxLogStatus(wxT("Caret is at (%d, %d)"), m_xCaret, m_yCaret);
369
370 GetCaret()->Move(m_xMargin + m_xCaret * m_widthChar,
371 m_yMargin + m_yCaret * m_heightChar);
372 }
373
374 void MyCanvas::OnSize(wxSizeEvent& event)
375 {
376 ChangeSize();
377
378 event.Skip();
379 }
380
381 void 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;
386 if ( !m_xChars )
387 m_xChars = 1;
388 if ( !m_yChars )
389 m_yChars = 1;
390
391 free(m_text);
392 m_text = (wxChar *)calloc(m_xChars * m_yChars, sizeof(wxChar));
393
394 #if wxUSE_STATUSBAR
395 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
396
397 if ( frame && frame->GetStatusBar() )
398 {
399 wxString msg;
400 msg.Printf(wxT("Panel size is (%d, %d)"), m_xChars, m_yChars);
401 frame->SetStatusText(msg, 1);
402 }
403 #endif // wxUSE_STATUSBAR
404 }
405
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
410 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
411 {
412 wxCaretSuspend cs(this);
413 wxPaintDC dc( this );
414 PrepareDC( dc );
415 dc.Clear();
416
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 {
425 wxChar ch = CharAt(x, y);
426 if ( !ch )
427 ch = wxT(' ');
428 #ifdef __WXOSX__
429 dc.DrawText(ch, m_xMargin + x * m_widthChar,
430 m_yMargin + y * m_heightChar );
431 #else
432 line += ch;
433 #endif
434 }
435
436 #ifndef __WXOSX__
437 dc.DrawText( line, m_xMargin, m_yMargin + y * m_heightChar );
438 #endif
439 }
440 }
441
442 void MyCanvas::OnChar( wxKeyEvent &event )
443 {
444 switch ( event.GetKeyCode() )
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:
476 if ( !event.AltDown() && wxIsprint(event.GetKeyCode()) )
477 {
478 wxChar ch = (wxChar)event.GetKeyCode();
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 );
487
488 NextChar();
489 }
490 else
491 {
492 event.Skip();
493 }
494 }
495
496 DoMoveCaret();
497 }
498