]>
Commit | Line | Data |
---|---|---|
7de7aed4 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: keyboard.cpp | |
be5a51fb | 3 | // Purpose: Keyboard wxWidgets sample |
7de7aed4 VZ |
4 | // Author: Vadim Zeitlin |
5 | // Modified by: | |
6 | // Created: 07.04.02 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2002 Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx/wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | // for all others, include the necessary headers (this file is usually all you | |
be5a51fb | 28 | // need because it includes almost all "standard" wxWidgets headers) |
7de7aed4 VZ |
29 | #ifndef WX_PRECOMP |
30 | #include "wx/wx.h" | |
31 | #endif | |
32 | ||
b29903d4 WS |
33 | #if !wxUSE_LOG |
34 | # error You must set wxUSE_LOG to 1 in setup.h | |
35 | #endif | |
36 | ||
7de7aed4 VZ |
37 | // ---------------------------------------------------------------------------- |
38 | // private classes | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | // Define a new application type, each program should derive a class from wxApp | |
42 | class MyApp : public wxApp | |
43 | { | |
44 | public: | |
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 | |
55 | class MyFrame : public wxFrame | |
56 | { | |
57 | public: | |
58 | // ctor(s) | |
59 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, | |
60 | long style = wxDEFAULT_FRAME_STYLE); | |
61 | ||
62 | ~MyFrame() { delete m_logTarget; } | |
63 | ||
64 | // event handlers (these functions should _not_ be virtual) | |
65 | void OnQuit(wxCommandEvent& event); | |
66 | void OnAbout(wxCommandEvent& event); | |
67 | void OnClear(wxCommandEvent& event); | |
68 | void OnSkip(wxCommandEvent& event); | |
62fa9712 | 69 | void OnShowRaw(wxCommandEvent& event); |
7de7aed4 VZ |
70 | |
71 | void OnSize(wxSizeEvent& event); | |
72 | ||
73 | private: | |
74 | wxLog *m_logTarget; | |
75 | ||
76 | class TextWindow *m_winText; | |
77 | wxListBox *m_lboxLog; | |
78 | ||
be5a51fb | 79 | // any class wishing to process wxWidgets events must use this macro |
7de7aed4 VZ |
80 | DECLARE_EVENT_TABLE() |
81 | }; | |
82 | ||
83 | // A log target which just redirects the messages to a listbox | |
84 | class LboxLogger : public wxLog | |
85 | { | |
86 | public: | |
87 | LboxLogger(wxListBox *lbox, wxLog *logOld) | |
88 | { | |
89 | m_lbox = lbox; | |
90 | //m_lbox->Disable(); -- looks ugly under MSW | |
91 | m_logOld = logOld; | |
92 | } | |
93 | ||
94 | virtual ~LboxLogger() | |
95 | { | |
96 | wxLog::SetActiveTarget(m_logOld); | |
97 | } | |
98 | ||
99 | private: | |
100 | // implement sink functions | |
101 | virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t) | |
102 | { | |
103 | // don't put trace messages into listbox or we can get into infinite | |
104 | // recursion | |
105 | if ( level == wxLOG_Trace ) | |
106 | { | |
107 | if ( m_logOld ) | |
108 | { | |
109 | // cast is needed to call protected method | |
110 | ((LboxLogger *)m_logOld)->DoLog(level, szString, t); | |
111 | } | |
112 | } | |
113 | else | |
114 | { | |
115 | wxLog::DoLog(level, szString, t); | |
116 | } | |
117 | } | |
118 | ||
d1f47235 | 119 | virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
7de7aed4 VZ |
120 | { |
121 | wxString msg; | |
122 | TimeStamp(&msg); | |
123 | msg += szString; | |
124 | ||
125 | #ifdef __WXUNIVERSAL__ | |
126 | m_lbox->AppendAndEnsureVisible(msg); | |
127 | #else // other ports don't have this method yet | |
128 | m_lbox->Append(msg); | |
129 | m_lbox->SetFirstItem(m_lbox->GetCount() - 1); | |
130 | #endif | |
131 | } | |
132 | ||
133 | // the control we use | |
134 | wxListBox *m_lbox; | |
135 | ||
136 | // the old log target | |
137 | wxLog *m_logOld; | |
138 | }; | |
139 | ||
140 | class TextWindow : public wxWindow | |
141 | { | |
142 | public: | |
143 | TextWindow(wxWindow *parent) | |
ee1a622d | 144 | : wxWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, |
7de7aed4 VZ |
145 | wxRAISED_BORDER) |
146 | { | |
ee1a622d WS |
147 | m_skip = true; |
148 | m_showRaw = false; | |
7de7aed4 VZ |
149 | |
150 | SetBackgroundColour(*wxBLUE); | |
151 | } | |
152 | ||
153 | void SetSkip(bool skip) { m_skip = skip; } | |
62fa9712 | 154 | void SetShowRaw(bool show) { m_showRaw = show; } |
7de7aed4 VZ |
155 | |
156 | protected: | |
157 | void OnKeyDown(wxKeyEvent& event) { LogEvent(_T("Key down"), event); } | |
158 | void OnKeyUp(wxKeyEvent& event) { LogEvent(_T("Key up"), event); } | |
159 | void OnChar(wxKeyEvent& event) { LogEvent(_T("Char"), event); } | |
160 | ||
d1f47235 | 161 | void OnPaint(wxPaintEvent& WXUNUSED(event)) |
7de7aed4 VZ |
162 | { |
163 | wxPaintDC dc(this); | |
164 | dc.SetTextForeground(*wxWHITE); | |
165 | dc.DrawLabel(_T("Press keys here"), GetClientRect(), wxALIGN_CENTER); | |
166 | } | |
167 | ||
168 | private: | |
169 | static inline wxChar GetChar(bool on, wxChar c) { return on ? c : _T('-'); } | |
170 | ||
171 | void LogEvent(const wxChar *name, wxKeyEvent& event); | |
172 | ||
173 | bool m_skip; | |
62fa9712 | 174 | bool m_showRaw; |
7de7aed4 VZ |
175 | |
176 | DECLARE_EVENT_TABLE() | |
177 | }; | |
178 | ||
179 | BEGIN_EVENT_TABLE(TextWindow, wxWindow) | |
180 | EVT_KEY_DOWN(TextWindow::OnKeyDown) | |
181 | EVT_KEY_UP(TextWindow::OnKeyUp) | |
182 | EVT_CHAR(TextWindow::OnChar) | |
183 | ||
184 | EVT_PAINT(TextWindow::OnPaint) | |
185 | END_EVENT_TABLE() | |
186 | ||
187 | // ---------------------------------------------------------------------------- | |
188 | // constants | |
189 | // ---------------------------------------------------------------------------- | |
190 | ||
191 | // IDs for the controls and the menu commands | |
192 | enum | |
193 | { | |
194 | // menu items | |
195 | Keyboard_Quit = 1, | |
196 | ||
197 | Keyboard_Clear, | |
198 | Keyboard_Skip, | |
62fa9712 | 199 | Keyboard_ShowRaw, |
7de7aed4 VZ |
200 | |
201 | // it is important for the id corresponding to the "About" command to have | |
202 | // this standard value as otherwise it won't be handled properly under Mac | |
203 | // (where it is special and put into the "Apple" menu) | |
204 | Keyboard_About = wxID_ABOUT | |
205 | }; | |
206 | ||
207 | // ---------------------------------------------------------------------------- | |
be5a51fb | 208 | // event tables and other macros for wxWidgets |
7de7aed4 VZ |
209 | // ---------------------------------------------------------------------------- |
210 | ||
be5a51fb | 211 | // the event tables connect the wxWidgets events with the functions (event |
7de7aed4 VZ |
212 | // handlers) which process them. It can be also done at run-time, but for the |
213 | // simple menu events like this the static method is much simpler. | |
214 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
215 | EVT_MENU(Keyboard_Quit, MyFrame::OnQuit) | |
216 | EVT_MENU(Keyboard_About, MyFrame::OnAbout) | |
217 | ||
218 | EVT_MENU(Keyboard_Clear, MyFrame::OnClear) | |
219 | EVT_MENU(Keyboard_Skip, MyFrame::OnSkip) | |
62fa9712 | 220 | EVT_MENU(Keyboard_ShowRaw, MyFrame::OnShowRaw) |
7de7aed4 VZ |
221 | |
222 | EVT_SIZE(MyFrame::OnSize) | |
223 | END_EVENT_TABLE() | |
224 | ||
be5a51fb | 225 | // Create a new application object: this macro will allow wxWidgets to create |
7de7aed4 VZ |
226 | // the application object during program execution (it's better than using a |
227 | // static object for many reasons) and also declares the accessor function | |
228 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
229 | // not wxApp) | |
230 | IMPLEMENT_APP(MyApp) | |
231 | ||
232 | // ============================================================================ | |
233 | // implementation | |
234 | // ============================================================================ | |
235 | ||
236 | // ---------------------------------------------------------------------------- | |
237 | // the application class | |
238 | // ---------------------------------------------------------------------------- | |
239 | ||
240 | // 'Main program' equivalent: the program execution "starts" here | |
241 | bool MyApp::OnInit() | |
242 | { | |
243 | // create the main application window | |
be5a51fb | 244 | MyFrame *frame = new MyFrame(_T("Keyboard wxWidgets App"), |
7de7aed4 VZ |
245 | wxPoint(50, 50), wxSize(450, 340)); |
246 | ||
247 | // and show it (the frames, unlike simple controls, are not shown when | |
248 | // created initially) | |
ee1a622d | 249 | frame->Show(true); |
7de7aed4 VZ |
250 | |
251 | // success: wxApp::OnRun() will be called which will enter the main message | |
ee1a622d | 252 | // loop and the application will run. If we returned false here, the |
7de7aed4 | 253 | // application would exit immediately. |
ee1a622d | 254 | return true; |
7de7aed4 VZ |
255 | } |
256 | ||
257 | // ---------------------------------------------------------------------------- | |
258 | // main frame | |
259 | // ---------------------------------------------------------------------------- | |
260 | ||
261 | // frame constructor | |
262 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style) | |
ee1a622d | 263 | : wxFrame(NULL, wxID_ANY, title, pos, size, style), |
7de7aed4 VZ |
264 | m_winText(NULL) |
265 | { | |
266 | #if wxUSE_MENUS | |
267 | // create a menu bar | |
268 | wxMenu *menuFile = new wxMenu; | |
269 | menuFile->Append(Keyboard_Clear, _T("&Clear log\tCtrl-L")); | |
270 | menuFile->AppendSeparator(); | |
271 | menuFile->Append(Keyboard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); | |
272 | ||
273 | wxMenu *menuKeys = new wxMenu; | |
62fa9712 VZ |
274 | menuKeys->AppendCheckItem(Keyboard_ShowRaw, _T("Show &raw keys\tCtrl-R")); |
275 | menuKeys->AppendSeparator(); | |
7de7aed4 VZ |
276 | menuKeys->AppendCheckItem(Keyboard_Skip, _T("&Skip key down\tCtrl-S")); |
277 | ||
278 | // the "About" item should be in the help menu | |
279 | wxMenu *menuHelp = new wxMenu; | |
280 | menuHelp->Append(Keyboard_About, _T("&About...\tF1"), _T("Show about dialog")); | |
281 | ||
282 | // now append the freshly created menu to the menu bar... | |
283 | wxMenuBar *menuBar = new wxMenuBar(); | |
284 | menuBar->Append(menuFile, _T("&File")); | |
285 | menuBar->Append(menuKeys, _T("&Keys")); | |
286 | menuBar->Append(menuHelp, _T("&Help")); | |
287 | ||
288 | // ... and attach this menu bar to the frame | |
289 | SetMenuBar(menuBar); | |
290 | ||
ee1a622d | 291 | menuBar->Check(Keyboard_Skip, true); |
62fa9712 VZ |
292 | |
293 | #ifndef wxHAS_RAW_KEY_CODES | |
ee1a622d | 294 | menuBar->Enable(Keyboard_ShowRaw, false); |
62fa9712 | 295 | #endif // !wxHAS_RAW_KEY_CODES |
7de7aed4 VZ |
296 | #endif // wxUSE_MENUS |
297 | ||
298 | m_winText = new TextWindow(this); | |
ee1a622d | 299 | m_lboxLog = new wxListBox(this, wxID_ANY); |
7de7aed4 VZ |
300 | |
301 | m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget()); | |
302 | wxLog::SetActiveTarget(m_logTarget); | |
303 | ||
304 | #if wxUSE_STATUSBAR | |
305 | // create a status bar just for fun (by default with 1 pane only) | |
306 | CreateStatusBar(2); | |
be5a51fb | 307 | SetStatusText(_T("Welcome to wxWidgets!")); |
7de7aed4 VZ |
308 | #endif // wxUSE_STATUSBAR |
309 | } | |
310 | ||
311 | // event handlers | |
312 | ||
313 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
314 | { | |
ee1a622d WS |
315 | // true is to force the frame to close |
316 | Close(true); | |
7de7aed4 VZ |
317 | } |
318 | ||
319 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
320 | { | |
be5a51fb | 321 | wxString msg = _T("Demonstrates keyboard event processing in wxWidgets\n") |
62fa9712 | 322 |