]> git.saurik.com Git - wxWidgets.git/blame - samples/caret/caret.cpp
Replaced WXGTK13 with WXGTK20
[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);
f6bcfd97 64 void OnSetBlinkTime(wxCommandEvent& event);
0290598f
VZ
65
66private:
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
72class MyCanvas: public wxScrolledWindow
73{
74public:
75 MyCanvas() { }
76 MyCanvas( wxWindow *parent );
77 ~MyCanvas();
78
92607616 79 wxChar& CharAt(int x, int y) { return *(m_text + x + m_xChars * y); }
0290598f
VZ
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
96private:
0290598f
VZ
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
92607616 112 wxChar *m_text;
0290598f
VZ
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
123enum
124{
125 // menu items
7e12d1bf
VZ
126 Caret_Quit = 1,
127 Caret_About,
f6bcfd97 128 Caret_SetBlinkTime,
0290598f
VZ
129
130 // controls start here (the numbers are, of course, arbitrary)
7e12d1bf 131 Caret_Text = 1000
0290598f
VZ
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)
7e12d1bf
VZ
142 EVT_MENU(Caret_Quit, MyFrame::OnQuit)
143 EVT_MENU(Caret_About, MyFrame::OnAbout)
f6bcfd97 144 EVT_MENU(Caret_SetBlinkTime, MyFrame::OnSetBlinkTime)
0290598f
VZ
145END_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)
152IMPLEMENT_APP(MyApp)
153
154// ============================================================================
155// implementation
156// ============================================================================
157
158// ----------------------------------------------------------------------------
159// the application class
160// ----------------------------------------------------------------------------
161
162// `Main program' equivalent: the program execution "starts" here
163bool MyApp::OnInit()
164{
165 // Create the main application window
7e12d1bf 166 MyFrame *frame = new MyFrame("Caret wxWindows sample",
0290598f
VZ
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
185MyFrame::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
f6bcfd97
BP
194 menuFile->Append(Caret_SetBlinkTime, "&Blink time...\tCtrl-B");
195 menuFile->AppendSeparator();
7e12d1bf 196 menuFile->Append(Caret_About, "&About...\tCtrl-A", "Show about dialog");
0290598f 197 menuFile->AppendSeparator();
7e12d1bf 198 menuFile->Append(Caret_Quit, "E&xit\tAlt-X", "Quit this program");
0290598f
VZ
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
217void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
218{
219 // TRUE is to force the frame to close
220 Close(TRUE);
221}
222
223void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
224{
f6bcfd97
BP
225