]> git.saurik.com Git - wxWidgets.git/blob - samples/caret/caret.cpp
Committing in .
[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(__WXMOTIF__)
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 // Define a new frame type: this is going to be our main frame
55 class MyFrame : public wxFrame
56 {
57 public:
58 // ctor(s)
59 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
60
61 // event handlers (these functions should _not_ be virtual)
62 void OnQuit(wxCommandEvent& event);
63 void OnAbout(wxCommandEvent& event);
64 void OnSetBlinkTime(wxCommandEvent& event);
65
66 private:
67 // any class wishing to process wxWindows events must use this macro
68 DECLARE_EVENT_TABLE()
69 };
70
71 // MyCanvas is a canvas on which you can type
72 class MyCanvas: public wxScrolledWindow
73 {
74 public:
75 MyCanvas() { }
76 MyCanvas( wxWindow *parent );
77 ~MyCanvas();
78
79 wxChar& CharAt(int x, int y) { return *(m_text + x + m_xChars * y); }
80
81 // caret movement
82 void Home() { m_xCaret = 0; }
83 void End() { m_xCaret = m_xChars - 1; }
84 void FirstLine() { m_yCaret = 0; }
85 void LastLine() { m_yCaret = m_yChars - 1; }
86 void PrevChar() { if ( !m_xCaret-- ) { End(); PrevLine(); } }
87 void NextChar() { if ( ++m_xCaret == m_xChars ) { Home(); NextLine(); } }
88 void PrevLine() { if ( !m_yCaret-- ) LastLine(); }
89 void NextLine() { if ( ++m_yCaret == m_yChars ) FirstLine(); }
90
91 // event handlers
92 void OnPaint( wxPaintEvent &event );
93 void OnSize( wxSizeEvent &event );
94 void OnChar( wxKeyEvent &event );
95
96 private:
97 wxFont m_font;
98
99 // the margin around the text (looks nicer)
100 int m_xMargin, m_yMargin;
101
102 // size (in pixels) of one character
103 long m_widthChar, m_heightChar;
104
105 // position (in text coords) of the caret
106 int m_xCaret, m_yCaret;
107
108 // the size (in text coords) of the window
109 int m_xChars, m_yChars;
110
111 // the text
112 wxChar *m_text;
113
114 DECLARE_DYNAMIC_CLASS(MyCanvas)
115 DECLARE_EVENT_TABLE()
116 };
117
118 // ----------------------------------------------------------------------------
119 // constants
120 // ----------------------------------------------------------------------------
121
122 // IDs for the controls and the menu commands
123 enum
124 {
125 // menu items
126 Caret_Quit = 1,
127 Caret_About,
128 Caret_SetBlinkTime,
129
130 // controls start here (the numbers are, of course, arbitrary)
131 Caret_Text = 1000
132 };
133
134 // ----------------------------------------------------------------------------
135 // event tables and other macros for wxWindows
136 // ----------------------------------------------------------------------------
137
138 // the event tables connect the wxWindows events with the functions (event
139 // handlers) which process them. It can be also done at run-time, but for the
140 // simple menu events like this the static method is much simpler.
141 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
142 EVT_MENU(Caret_Quit, MyFrame::OnQuit)
143 EVT_MENU(Caret_About, MyFrame::OnAbout)
144 EVT_MENU(Caret_SetBlinkTime, MyFrame::OnSetBlinkTime)
145 END_EVENT_TABLE()
146
147 // Create a new application object: this macro will allow wxWindows to create
148 // the application object during program execution (it's better than using a
149 // static object for many reasons) and also declares the accessor function
150 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
151 // not wxApp)
152 IMPLEMENT_APP(MyApp)
153
154 // ============================================================================
155 // implementation
156 // ============================================================================
157
158 // ----------------------------------------------------------------------------
159 // the application class
160 // ----------------------------------------------------------------------------
161
162 // `Main program' equivalent: the program execution "starts" here
163 bool MyApp::OnInit()
164 {
165 // Create the main application window
166 MyFrame *frame = new MyFrame("Caret wxWindows sample",
167 wxPoint(50, 50), wxSize(450, 340));
168
169 // Show it and tell the application that it's our main window
170 // @@@ what does it do exactly, in fact? is it necessary here?
171 frame->Show(TRUE);
172 SetTopWindow(frame);
173
174 // success: wxApp::OnRun() will be called which will enter the main message
175 // loop and the application will run. If we returned FALSE here, the
176 // application would exit immediately.
177 return TRUE;
178 }
179
180 // ----------------------------------------------------------------------------
181 // main frame
182 // ----------------------------------------------------------------------------
183
184 // frame constructor
185 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
186 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
187 {
188 // set the frame icon
189 SetIcon(wxICON(mondrian));
190
191 // create a menu bar
192 wxMenu *menuFile = new wxMenu;
193
194 menuFile->Append(Caret_SetBlinkTime, "&Blink time...\tCtrl-B");
195 menuFile->AppendSeparator();
196 menuFile->Append(Caret_About, "&About...\tCtrl-A", "Show about dialog");
197 menuFile->AppendSeparator();
198 menuFile->Append(Caret_Quit, "E&xit\tAlt-X", "Quit this program");
199
200 // now append the freshly created menu to the menu bar...
201 wxMenuBar *menuBar = new wxMenuBar;
202 menuBar->Append(menuFile, "&File");
203
204 // ... and attach this menu bar to the frame
205 SetMenuBar(menuBar);
206
207 (void) new MyCanvas( this );
208
209 // create a status bar just for fun (by default with 1 pane only)
210 CreateStatusBar(2);
211 SetStatusText("Welcome to wxWindows!");
212 }
213
214
215 // event handlers
216
217 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
218 {
219 // TRUE is to force the frame to close
220 Close(TRUE);
221 }
222
223 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
224 {
225 wxMessageBox(_T("The caret wxWindows sample.\n© 1999 Vadim Zeitlin"),
226 _T("About Caret"), wxOK | wxICON_INFORMATION, this);
227 }
228
229 void MyFrame::OnSetBlinkTime(wxCommandEvent& WXUNUSED(event))
230 {
231 long blinkTime = wxGetNumberFromUser
232 (
233 _T("The caret blink time is the time between two blinks"),
234 _T("Time in milliseconds:"),
235 _T("wxCaret sample"),
236 wxCaret::GetBlinkTime(), 0, 10000,
237 this
238 );
239 if ( blinkTime != -1 )
240 {
241 wxCaret::SetBlinkTime((int)blinkTime);
242 wxLogStatus(this, _T("Blink time set to %ld milliseconds."), blinkTime);
243 }
244 }
245
246 // ----------------------------------------------------------------------------
247 // MyCanvas
248 // ----------------------------------------------------------------------------
249
250 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
251
252 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
253 EVT_PAINT(MyCanvas::OnPaint)
254 EVT_SIZE(MyCanvas::OnSize)
255 EVT_CHAR(MyCanvas::OnChar)
256 END_EVENT_TABLE()
257
258 MyCanvas::MyCanvas( wxWindow *parent )
259 : wxScrolledWindow( parent, -1,
260 wxDefaultPosition, wxDefaultSize,
261 wxSUNKEN_BORDER )
262 {
263 m_text = (wxChar *)NULL;
264
265 SetBackgroundColour(* wxWHITE);
266
267 m_font = *wxNORMAL_FONT;
268
269 wxClientDC dc(this);
270 dc.SetFont( m_font );
271 m_heightChar = dc.GetCharHeight();
272 m_widthChar = dc.GetCharWidth();
273
274 wxCaret *caret = new wxCaret(this, m_widthChar, m_heightChar);
275 SetCaret(caret);
276
277 m_xCaret = m_yCaret =
278 m_xChars = m_yChars = 0;
279
280 m_xMargin = m_yMargin = 5;
281 caret->Move(m_xMargin, m_yMargin);
282 caret->Show();
283 }
284
285 MyCanvas::~MyCanvas()
286 {
287 free(m_text);
288 }
289
290 void MyCanvas::OnSize( wxSizeEvent &event )
291 {
292 m_xChars = (event.GetSize().x - 2*m_xMargin) / m_widthChar;
293 m_yChars = (event.GetSize().y - 2*m_yMargin) / m_heightChar;
294 if ( !m_xChars )
295 m_xChars = 1;
296 if ( !m_yChars )
297 m_yChars = 1;
298
299 free(m_text);
300 m_text = (wxChar *)calloc(m_xChars * m_yChars, sizeof(wxChar));
301
302 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
303
304 if ( frame && frame->GetStatusBar() )
305 {
306 wxString msg;
307 msg.Printf(_T("Panel size is (%d, %d)"), m_xChars, m_yChars);
308
309 frame->SetStatusText(msg, 1);
310 }
311
312 event.Skip();
313 }
314
315 // NB: this method is horrible inefficient especially because the caret
316 // needs to be redrawn often and in this case we only have to redraw
317 // the caret location and not the entire window - in a real program we
318 // would use GetUpdateRegion() and iterate over rectangles it contains
319 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
320 {
321 wxPaintDC dc( this );
322 PrepareDC( dc );
323 dc.Clear();
324
325 dc.SetFont( m_font );
326
327 for ( int y = 0; y < m_yChars; y++ )
328 {
329 wxString line;
330
331 for ( int x = 0; x < m_xChars; x++ )
332 {
333 wxChar ch = CharAt(x, y);
334 if ( !ch )
335 ch = _T(' ');
336 line += ch;
337 }
338
339 dc.DrawText( line, m_xMargin, m_yMargin + y * m_heightChar );
340 }
341 }
342
343 void MyCanvas::OnChar( wxKeyEvent &event )
344 {
345 switch ( event.KeyCode() )
346 {
347 case WXK_LEFT:
348 PrevChar();
349 break;
350
351 case WXK_RIGHT:
352 NextChar();
353 break;
354
355 case WXK_UP:
356 PrevLine();
357 break;
358
359 case WXK_DOWN:
360 NextLine();
361 break;
362
363 case WXK_HOME:
364 Home();
365 break;
366
367 case WXK_END:
368 End();
369 break;
370
371 case WXK_RETURN:
372 Home();
373 NextLine();
374 break;
375
376 default:
377 if ( !event.AltDown() && wxIsprint(event.KeyCode()) )
378 {
379 CharAt(m_xCaret, m_yCaret) = (wxChar)event.KeyCode();
380 NextChar();
381 }
382 else
383 {
384 event.Skip();
385
386 // don't refresh
387 return;
388 }
389 }
390
391 wxLogStatus(_T("Caret is at (%d, %d)"), m_xCaret, m_yCaret);
392
393 GetCaret()->Move(m_xMargin + m_xCaret * m_widthChar,
394 m_yMargin + m_yCaret * m_heightChar);
395
396 Refresh();
397 }
398