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