]> git.saurik.com Git - wxWidgets.git/blame - samples/caret/caret.cpp
Added test to prevent crash in LogMenuEvent
[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
0290598f
VZ
54// MyCanvas is a canvas on which you can type
55class MyCanvas: public wxScrolledWindow
56{
57public:
58 MyCanvas() { }
59 MyCanvas( wxWindow *parent );
60 ~MyCanvas();
61
92607616 62 wxChar& CharAt(int x, int y) { return *(m_text + x + m_xChars * y); }
0290598f 63
697e0cdd
VZ
64 // operations
65 void CreateCaret();
66 void MoveCaret(int x, int y);
67
0290598f
VZ
68 // caret movement
69 void Home() { m_xCaret = 0; }
70 void End() { m_xCaret = m_xChars - 1; }
71 void FirstLine() { m_yCaret = 0; }
72 void LastLine() { m_yCaret = m_yChars - 1; }
73 void PrevChar() { if ( !m_xCaret-- ) { End(); PrevLine(); } }
74 void NextChar() { if ( ++m_xCaret == m_xChars ) { Home(); NextLine(); } }
75 void PrevLine() { if ( !m_yCaret-- ) LastLine(); }
76 void NextLine() { if ( ++m_yCaret == m_yChars ) FirstLine(); }
77
78 // event handlers
79 void OnPaint( wxPaintEvent &event );
80 void OnSize( wxSizeEvent &event );
81 void OnChar( wxKeyEvent &event );
82
83private:
697e0cdd
VZ
84 // move the caret to m_xCaret, m_yCaret
85 void DoMoveCaret();
86
0290598f
VZ
87 wxFont m_font;
88
89 // the margin around the text (looks nicer)
90 int m_xMargin, m_yMargin;
91
92 // size (in pixels) of one character
93 long m_widthChar, m_heightChar;
94
95 // position (in text coords) of the caret
96 int m_xCaret, m_yCaret;
97
98 // the size (in text coords) of the window
99 int m_xChars, m_yChars;
100
101 // the text
92607616 102 wxChar *m_text;
0290598f
VZ
103
104 DECLARE_DYNAMIC_CLASS(MyCanvas)
105 DECLARE_EVENT_TABLE()
106};
107
697e0cdd
VZ
108
109// Define a new frame type: this is going to be our main frame
110class MyFrame : public wxFrame
111{
112public:
113 // ctor(s)
114 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
115
116 // event handlers (these functions should _not_ be virtual)
117 void OnQuit(wxCommandEvent& event);
118 void OnAbout(wxCommandEvent& event);
119 void OnSetBlinkTime(wxCommandEvent& event);
120 void OnCaretMove(wxCommandEvent& event);
121
122private:
123 MyCanvas *m_canvas;
124
125 // any class wishing to process wxWindows events must use this macro
126 DECLARE_EVENT_TABLE()
127};
128
0290598f
VZ
129// ----------------------------------------------------------------------------
130// constants
131// ----------------------------------------------------------------------------
132
133// IDs for the controls and the menu commands
134enum
135{
136 // menu items
7e12d1bf
VZ
137 Caret_Quit = 1,
138 Caret_About,
f6bcfd97 139 Caret_SetBlinkTime,
697e0cdd 140 Caret_Move,
0290598f
VZ
141
142 // controls start here (the numbers are, of course, arbitrary)
7e12d1bf 143 Caret_Text = 1000
0290598f
VZ
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.
153BEGIN_EVENT_TABLE(MyFrame, wxFrame)
7e12d1bf
VZ
154 EVT_MENU(Caret_Quit, MyFrame::OnQuit)
155 EVT_MENU(Caret_About, MyFrame::OnAbout)
f6bcfd97 156 EVT_MENU(Caret_SetBlinkTime, MyFrame::OnSetBlinkTime)
697e0cdd 157 EVT_MENU(Caret_Move, MyFrame::OnCaretMove)
0290598f
VZ
158END_EVENT_TABLE()
159
160// Create a new application object: this macro will allow wxWindows to create
161// the application object during program execution (it's better than using a
162// static object for many reasons) and also declares the accessor function
163// wxGetApp() which will return the reference of the right type (i.e. MyApp and
164// not wxApp)
165IMPLEMENT_APP(MyApp)
166
167// ============================================================================
168// implementation
169// ============================================================================
170
171// ----------------------------------------------------------------------------
172// the application class
173// ----------------------------------------------------------------------------
174
175// `Main program' equivalent: the program execution "starts" here
176bool MyApp::OnInit()
177{
697e0cdd 178 // create and show the main application window
7e12d1bf 179 MyFrame *frame = new MyFrame("Caret wxWindows sample",
0290598f
VZ
180 wxPoint(50, 50), wxSize(450, 340));
181
0290598f 182 frame->Show(TRUE);
0290598f
VZ
183
184 // success: wxApp::OnRun() will be called which will enter the main message
185 // loop and the application will run. If we returned FALSE here, the
186 // application would exit immediately.
187 return TRUE;
188}
189
190// ----------------------------------------------------------------------------
191// main frame
192// ----------------------------------------------------------------------------
193
194// frame constructor
195MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
196 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
197{
198 // set the frame icon
199 SetIcon(wxICON(mondrian));
200
201 // create a menu bar
202 wxMenu *menuFile = new wxMenu;
203
f6bcfd97 204 menuFile->Append(Caret_SetBlinkTime, "&Blink time...\tCtrl-B");
697e0cdd 205 menuFile->Append(Caret_Move, "&Move caret\tCtrl-C");
f6bcfd97 206 menuFile->AppendSeparator();
7e12d1bf 207 menuFile->Append(Caret_About, "&About...\tCtrl-A", "Show about dialog");
0290598f 208 menuFile->AppendSeparator();
7e12d1bf 209 menuFile->Append(Caret_Quit, "E&xit\tAlt-X", "Quit this program");
0290598f
VZ
210
211 // now append the freshly created menu to the menu bar...
212 wxMenuBar *menuBar = new wxMenuBar;
213 menuBar->Append(menuFile, "&File");
214
215 // ... and attach this menu bar to the frame
216 SetMenuBar(menuBar);
217
697e0cdd 218 m_canvas = new MyCanvas(this);
0290598f
VZ
219
220 // create a status bar just for fun (by default with 1 pane only)
221 CreateStatusBar(2);
222 SetStatusText("Welcome to wxWindows!");
223}
224
225
226// event handlers
227
228void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
229{
230 // TRUE is to force the frame to close
231 Close(TRUE);
232}
233
234void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
235{
f6bcfd97
BP
236