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