]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
92607616 | 90 | wxChar& CharAt(int x, int y) { return *(m_text + x + m_xChars * y); } |
0290598f VZ |
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 | wxCaret m_caret; | |
109 | wxFont m_font; | |
110 | ||
111 | // the margin around the text (looks nicer) | |
112 | int m_xMargin, m_yMargin; | |
113 | ||
114 | // size (in pixels) of one character | |
115 | long m_widthChar, m_heightChar; | |
116 | ||
117 | // position (in text coords) of the caret | |
118 | int m_xCaret, m_yCaret; | |
119 | ||
120 | // the size (in text coords) of the window | |
121 | int m_xChars, m_yChars; | |
122 | ||
123 | // the text | |
92607616 | 124 | wxChar *m_text; |
0290598f VZ |
125 | |
126 | DECLARE_DYNAMIC_CLASS(MyCanvas) | |
127 | DECLARE_EVENT_TABLE() | |
128 | }; | |
129 | ||
130 | // ---------------------------------------------------------------------------- | |
131 | // constants | |
132 | // ---------------------------------------------------------------------------- | |
133 | ||
134 | // IDs for the controls and the menu commands | |
135 | enum | |
136 | { | |
137 | // menu items | |
138 | Minimal_Quit = 1, | |
139 | Minimal_About, | |
140 | Minimal_Test1, | |
141 | Minimal_Test2, | |
142 | ||
143 | // controls start here (the numbers are, of course, arbitrary) | |
144 | Minimal_Text = 1000, | |
145 | }; | |
146 | ||
147 | // ---------------------------------------------------------------------------- | |
148 | // event tables and other macros for wxWindows | |
149 | // ---------------------------------------------------------------------------- | |
150 | ||
151 | // the event tables connect the wxWindows events with the functions (event | |
152 | // handlers) which process them. It can be also done at run-time, but for the | |
153 | // simple menu events like this the static method is much simpler. | |
154 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
155 | EVT_MENU(Minimal_Quit, MyFrame::OnQuit) | |
156 | EVT_MENU(Minimal_About, MyFrame::OnAbout) | |
157 | END_EVENT_TABLE() | |
158 | ||
159 | // Create a new application object: this macro will allow wxWindows to create | |
160 | // the application object during program execution (it's better than using a | |
161 | // static object for many reasons) and also declares the accessor function | |
162 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
163 | // not wxApp) | |
164 | IMPLEMENT_APP(MyApp) | |
165 | ||
166 | // ============================================================================ | |
167 | // implementation | |
168 | // ============================================================================ | |
169 | ||
170 | // ---------------------------------------------------------------------------- | |
171 | // the application class | |
172 | // ---------------------------------------------------------------------------- | |
173 | ||
174 | // `Main program' equivalent: the program execution "starts" here | |
175 | bool MyApp::OnInit() | |
176 | { | |
177 | // Create the main application window | |
178 | MyFrame *frame = new MyFrame("Minimal wxWindows App", | |
179 | wxPoint(50, 50), wxSize(450, 340)); | |
180 | ||
181 | // Show it and tell the application that it's our main window | |
182 | // @@@ what does it do exactly, in fact? is it necessary here? | |
183 | frame->Show(TRUE); | |
184 | SetTopWindow(frame); | |
185 | ||
186 | // success: wxApp::OnRun() will be called which will enter the main message | |
187 | // loop and the application will run. If we returned FALSE here, the | |
188 | // application would exit immediately. | |
189 | return TRUE; | |
190 | } | |
191 | ||
192 | // ---------------------------------------------------------------------------- | |
193 | // main frame | |
194 | // ---------------------------------------------------------------------------- | |
195 | ||
196 | // frame constructor | |
197 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
198 | : wxFrame((wxFrame *)NULL, -1, title, pos, size) | |
199 | { | |
200 | // set the frame icon | |
201 | SetIcon(wxICON(mondrian)); | |
202 | ||
203 | // create a menu bar | |
204 | wxMenu *menuFile = new wxMenu; | |
205 | ||
206 | menuFile->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog"); | |
207 | menuFile->AppendSeparator(); | |
208 | menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program"); | |
209 | ||
210 | // now append the freshly created menu to the menu bar... | |
211 | wxMenuBar *menuBar = new wxMenuBar; | |
212 | menuBar->Append(menuFile, "&File"); | |
213 | ||
214 | // ... and attach this menu bar to the frame | |
215 | SetMenuBar(menuBar); | |
216 | ||
217 | (void) new MyCanvas( this ); | |
218 | ||
219 | // create a status bar just for fun (by default with 1 pane only) | |
220 | CreateStatusBar(2); | |
221 | SetStatusText("Welcome to wxWindows!"); | |
222 | } | |
223 | ||
224 | ||
225 | // event handlers | |
226 | ||
227 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
228 | { | |
229 | // TRUE is to force the frame to close | |
230 | Close(TRUE); | |
231 | } | |
232 | ||
233 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
234 | { | |
235 | wxString msg; | |
236 | msg.Printf( _T("This is the about dialog of minimal sample.\n") | |
237 | _T("Welcome to %s") | |
238 | #ifdef wxBETA_NUMBER | |
239 | _T(" (beta %d)!") | |
240 | #endif // wxBETA_NUMBER | |
241 | , wxVERSION_STRING | |
242 | #ifdef wxBETA_NUMBER | |
243 | , wxBETA_NUMBER | |
244 | #endif // wxBETA_NUMBER | |
245 | ); | |
246 | ||
247 | wxMessageBox(msg, "About Minimal", wxOK | wxICON_INFORMATION, this); | |
248 | } | |
249 | ||
250 | ||
251 | // ---------------------------------------------------------------------------- | |
252 | // MyCanvas | |
253 | // ---------------------------------------------------------------------------- | |
254 | ||
255 | IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow) | |
256 | ||
257 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
258 | EVT_PAINT(MyCanvas::OnPaint) | |
259 | EVT_SIZE(MyCanvas::OnSize) | |
260 | EVT_CHAR(MyCanvas::OnChar) | |
261 | END_EVENT_TABLE() | |
262 | ||
263 | MyCanvas::MyCanvas( wxWindow *parent ) | |
264 | : wxScrolledWindow( parent, -1, | |
265 | wxDefaultPosition, wxDefaultSize, | |
266 | wxSUNKEN_BORDER ) | |
267 | { | |
92607616 | 268 | m_text = (wxChar *)NULL; |
a8f25787 | 269 | |
0290598f VZ |
270 | SetBackgroundColour(* wxWHITE); |
271 | ||
272 | m_font = *wxNORMAL_FONT; | |
273 | ||
274 | wxClientDC dc(this); | |
275 | dc.SetFont( m_font ); | |
276 | m_heightChar = dc.GetCharHeight(); | |
277 | m_widthChar = dc.GetCharWidth(); | |
278 | ||
279 | m_caret.Create( this, m_widthChar, m_heightChar ); | |
280 | ||
281 | m_xCaret = m_yCaret = | |
282 | m_xChars = m_yChars = 0; | |
283 | ||
284 | m_xMargin = m_yMargin = 5; | |
285 | m_caret.Move(m_xMargin, m_yMargin); | |
286 | m_caret.Show(); | |
0290598f VZ |
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); | |
92607616 | 304 | m_text = (wxChar *)calloc(m_xChars * m_yChars, sizeof(wxChar)); |
0290598f VZ |
305 | |
306 | wxString msg; | |
92607616 | 307 | msg.Printf(_T("Panel size is (%d, %d)"), m_xChars, m_yChars); |
0290598f VZ |
308 | |
309 | ((wxFrame *)GetParent())->SetStatusText(msg, 1); | |
310 | ||
311 | event.Skip(); | |
312 | } | |
313 | ||
314 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
315 | { | |
316 | wxPaintDC dc( this ); | |
317 | PrepareDC( dc ); | |
318 | ||
319 | dc.SetFont( m_font ); | |
320 | ||
321 | for ( int y = 0; y < m_yChars; y++ ) | |
322 | { | |
323 | wxString line; | |
324 | ||
325 | for ( int x = 0; x < m_xChars; x++ ) | |
326 | { | |
92607616 | 327 | wxChar ch = CharAt(x, y); |
0290598f | 328 | if ( !ch ) |
92607616 | 329 | ch = _T(' '); |
0290598f VZ |
330 | line += ch; |
331 | } | |
332 | ||
333 | dc.DrawText( line, m_xMargin, m_yMargin + y * m_heightChar ); | |
334 | } | |
335 | } | |
336 | ||
337 | void MyCanvas::OnChar( wxKeyEvent &event ) | |
338 | { | |
339 | switch ( event.KeyCode() ) | |
340 | { | |
341 | case WXK_LEFT: | |
342 | PrevChar(); | |
343 | break; | |
344 | ||
345 | case WXK_RIGHT: | |
346 | NextChar(); | |
347 | break; | |
348 | ||
349 | case WXK_UP: | |
350 | PrevLine(); | |
351 | break; | |
352 | ||
353 | case WXK_DOWN: | |
354 | NextLine(); | |
355 | break; | |
356 | ||
357 | case WXK_HOME: | |
358 | Home(); | |
359 | break; | |
360 | ||
361 | case WXK_END: | |
362 | End(); | |
363 | break; | |
364 | ||
365 | case WXK_RETURN: | |
366 | Home(); | |
367 | NextLine(); | |
368 | break; | |
369 | ||
370 | default: | |
92607616 | 371 | if ( wxIsprint(event.KeyCode()) ) |
0290598f | 372 | { |
92607616 | 373 | CharAt(m_xCaret, m_yCaret) = (wxChar)event.KeyCode(); |
0290598f VZ |
374 | NextChar(); |
375 | } | |
376 | else | |
377 | { | |
378 | // don't refresh | |
379 | return; | |
380 | } | |
381 | } | |
382 | ||
92607616 | 383 | wxLogStatus(_T("Caret is at (%d, %d)"), m_xCaret, m_yCaret); |
0290598f VZ |
384 | |
385 | m_caret.Move(m_xMargin + m_xCaret * m_widthChar, | |
386 | m_yMargin + m_yCaret * m_heightChar); | |
387 | ||
388 | Refresh(); | |
389 | } | |
390 |