]> git.saurik.com Git - wxWidgets.git/blob - samples/keyboard/keyboard.cpp
0276694acaa05c09b32de41e34271fa0a27f218f
[wxWidgets.git] / samples / keyboard / keyboard.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: keyboard.cpp
3 // Purpose: Keyboard wxWindows sample
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
28 // need because it includes almost all "standard" wxWindows headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 // ----------------------------------------------------------------------------
34 // private classes
35 // ----------------------------------------------------------------------------
36
37 // Define a new application type, each program should derive a class from wxApp
38 class MyApp : public wxApp
39 {
40 public:
41 // override base class virtuals
42 // ----------------------------
43
44 // this one is called on application startup and is a good place for the app
45 // initialization (doing it here and not in the ctor allows to have an error
46 // return: if OnInit() returns false, the application terminates)
47 virtual bool OnInit();
48 };
49
50 // Define a new frame type: this is going to be our main frame
51 class MyFrame : public wxFrame
52 {
53 public:
54 // ctor(s)
55 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
56 long style = wxDEFAULT_FRAME_STYLE);
57
58 ~MyFrame() { delete m_logTarget; }
59
60 // event handlers (these functions should _not_ be virtual)
61 void OnQuit(wxCommandEvent& event);
62 void OnAbout(wxCommandEvent& event);
63 void OnClear(wxCommandEvent& event);
64 void OnSkip(wxCommandEvent& event);
65
66 void OnSize(wxSizeEvent& event);
67
68 private:
69 wxLog *m_logTarget;
70
71 class TextWindow *m_winText;
72 wxListBox *m_lboxLog;
73
74 // any class wishing to process wxWindows events must use this macro
75 DECLARE_EVENT_TABLE()
76 };
77
78 // A log target which just redirects the messages to a listbox
79 class LboxLogger : public wxLog
80 {
81 public:
82 LboxLogger(wxListBox *lbox, wxLog *logOld)
83 {
84 m_lbox = lbox;
85 //m_lbox->Disable(); -- looks ugly under MSW
86 m_logOld = logOld;
87 }
88
89 virtual ~LboxLogger()
90 {
91 wxLog::SetActiveTarget(m_logOld);
92 }
93
94 private:
95 // implement sink functions
96 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t)
97 {
98 // don't put trace messages into listbox or we can get into infinite
99 // recursion
100 if ( level == wxLOG_Trace )
101 {
102 if ( m_logOld )
103 {
104 // cast is needed to call protected method
105 ((LboxLogger *)m_logOld)->DoLog(level, szString, t);
106 }
107 }
108 else
109 {
110 wxLog::DoLog(level, szString, t);
111 }
112 }
113
114 virtual void DoLogString(const wxChar *szString, time_t t)
115 {
116 wxString msg;
117 TimeStamp(&msg);
118 msg += szString;
119
120 #ifdef __WXUNIVERSAL__
121 m_lbox->AppendAndEnsureVisible(msg);
122 #else // other ports don't have this method yet
123 m_lbox->Append(msg);
124 m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
125 #endif
126 }
127
128 // the control we use
129 wxListBox *m_lbox;
130
131 // the old log target
132 wxLog *m_logOld;
133 };
134
135 class TextWindow : public wxWindow
136 {
137 public:
138 TextWindow(wxWindow *parent)
139 : wxWindow(parent, -1, wxDefaultPosition, wxDefaultSize,
140 wxRAISED_BORDER)
141 {
142 m_skip = TRUE;
143
144 SetBackgroundColour(*wxBLUE);
145 }
146
147 void SetSkip(bool skip) { m_skip = skip; }
148
149 protected:
150 void OnKeyDown(wxKeyEvent& event) { LogEvent(_T("Key down"), event); }
151 void OnKeyUp(wxKeyEvent& event) { LogEvent(_T("Key up"), event); }
152 void OnChar(wxKeyEvent& event) { LogEvent(_T("Char"), event); }
153
154 void OnPaint(wxPaintEvent& event)
155 {
156 wxPaintDC dc(this);
157 dc.SetTextForeground(*wxWHITE);
158 dc.DrawLabel(_T("Press keys here"), GetClientRect(), wxALIGN_CENTER);
159 }
160
161 private:
162 static inline wxChar GetChar(bool on, wxChar c) { return on ? c : _T('-'); }
163
164 void LogEvent(const wxChar *name, wxKeyEvent& event);
165
166 bool m_skip;
167
168 DECLARE_EVENT_TABLE()
169 };
170
171 BEGIN_EVENT_TABLE(TextWindow, wxWindow)
172 EVT_KEY_DOWN(TextWindow::OnKeyDown)
173 EVT_KEY_UP(TextWindow::OnKeyUp)
174 EVT_CHAR(TextWindow::OnChar)
175
176 EVT_PAINT(TextWindow::OnPaint)
177 END_EVENT_TABLE()
178
179 // ----------------------------------------------------------------------------
180 // constants
181 // ----------------------------------------------------------------------------
182
183 // IDs for the controls and the menu commands
184 enum
185 {
186 // menu items
187 Keyboard_Quit = 1,
188
189 Keyboard_Clear,
190 Keyboard_Skip,
191
192 // it is important for the id corresponding to the "About" command to have
193 // this standard value as otherwise it won't be handled properly under Mac
194 // (where it is special and put into the "Apple" menu)
195 Keyboard_About = wxID_ABOUT
196 };
197
198 // ----------------------------------------------------------------------------
199 // event tables and other macros for wxWindows
200 // ----------------------------------------------------------------------------
201
202 // the event tables connect the wxWindows events with the functions (event
203 // handlers) which process them. It can be also done at run-time, but for the
204 // simple menu events like this the static method is much simpler.
205 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
206 EVT_MENU(Keyboard_Quit, MyFrame::OnQuit)
207 EVT_MENU(Keyboard_About, MyFrame::OnAbout)
208
209 EVT_MENU(Keyboard_Clear, MyFrame::OnClear)
210 EVT_MENU(Keyboard_Skip, MyFrame::OnSkip)
211
212 EVT_SIZE(MyFrame::OnSize)
213 END_EVENT_TABLE()
214
215 // Create a new application object: this macro will allow wxWindows to create
216 // the application object during program execution (it's better than using a
217 // static object for many reasons) and also declares the accessor function
218 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
219 // not wxApp)
220 IMPLEMENT_APP(MyApp)
221
222 // ============================================================================
223 // implementation
224 // ============================================================================
225
226 // ----------------------------------------------------------------------------
227 // the application class
228 // ----------------------------------------------------------------------------
229
230 // 'Main program' equivalent: the program execution "starts" here
231 bool MyApp::OnInit()
232 {
233 // create the main application window
234 MyFrame *frame = new MyFrame(_T("Keyboard wxWindows App"),
235 wxPoint(50, 50), wxSize(450, 340));
236
237 // and show it (the frames, unlike simple controls, are not shown when
238 // created initially)
239 frame->Show(TRUE);
240
241 // success: wxApp::OnRun() will be called which will enter the main message
242 // loop and the application will run. If we returned FALSE here, the
243 // application would exit immediately.
244 return TRUE;
245 }
246
247 // ----------------------------------------------------------------------------
248 // main frame
249 // ----------------------------------------------------------------------------
250
251 // frame constructor
252 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
253 : wxFrame(NULL, -1, title, pos, size, style),
254 m_winText(NULL)
255 {
256 #if wxUSE_MENUS
257 // create a menu bar
258 wxMenu *menuFile = new wxMenu;
259 menuFile->Append(Keyboard_Clear, _T("&Clear log\tCtrl-L"));
260 menuFile->AppendSeparator();
261 menuFile->Append(Keyboard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
262
263 wxMenu *menuKeys = new wxMenu;
264 menuKeys->AppendCheckItem(Keyboard_Skip, _T("&Skip key down\tCtrl-S"));
265
266 // the "About" item should be in the help menu
267 wxMenu *menuHelp = new wxMenu;
268 menuHelp->Append(Keyboard_About, _T("&About...\tF1"), _T("Show about dialog"));
269
270 // now append the freshly created menu to the menu bar...
271 wxMenuBar *menuBar = new wxMenuBar();
272 menuBar->Append(menuFile, _T("&File"));
273 menuBar->Append(menuKeys, _T("&Keys"));
274 menuBar->Append(menuHelp, _T("&Help"));
275
276 // ... and attach this menu bar to the frame
277 SetMenuBar(menuBar);
278
279 menuBar->Check(Keyboard_Skip, TRUE);
280 #endif // wxUSE_MENUS
281
282 m_winText = new TextWindow(this);
283 m_lboxLog = new wxListBox(this, -1);
284
285 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
286 wxLog::SetActiveTarget(m_logTarget);
287
288 #if wxUSE_STATUSBAR
289 // create a status bar just for fun (by default with 1 pane only)
290 CreateStatusBar(2);
291 SetStatusText(_T("Welcome to wxWindows!"));
292 #endif // wxUSE_STATUSBAR
293 }
294
295 // event handlers
296
297 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
298 {
299 // TRUE is to force the frame to close
300 Close(TRUE);
301 }
302
303 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
304 {
305 wxString msg;
306 msg.Printf( _T("This is the about dialog of keyboard sample.\n")
307 _T("Welcome to %s"), wxVERSION_STRING);
308
309 wxMessageBox(msg, _T("About Keyboard"), wxOK | wxICON_INFORMATION, this);
310 }
311
312 void MyFrame::OnClear(wxCommandEvent& event)
313 {
314 m_lboxLog->Clear();
315 }
316
317 void MyFrame::OnSkip(wxCommandEvent& event)
318 {
319 m_winText->SetSkip(event.IsChecked());
320 }
321
322 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
323 {
324 if ( m_winText )
325 {
326 wxSize size = GetClientSize();
327 m_winText->SetSize(0, 0, size.x, 50);
328 m_lboxLog->SetSize(0, 51, size.x, size.y - 50);
329 }
330 }
331
332 // ----------------------------------------------------------------------------
333 // TextWindow
334 // ----------------------------------------------------------------------------
335
336 void TextWindow::LogEvent(const wxChar *name, wxKeyEvent& event)
337 {
338 wxString key;
339 long keycode = event.KeyCode();
340 {
341 switch ( keycode )
342 {
343 case WXK_BACK: key = "BACK"; break;
344 case WXK_TAB: key = "TAB"; break;
345 case WXK_RETURN: key = "RETURN"; break;
346 case WXK_ESCAPE: key = "ESCAPE"; break;
347 case WXK_SPACE: key = "SPACE"; break;
348 case WXK_DELETE: key = "DELETE"; break;
349 case WXK_START: key = "START"; break;
350 case WXK_LBUTTON: key = "LBUTTON"; break;
351 case WXK_RBUTTON: key = "RBUTTON"; break;
352 case WXK_CANCEL: key = "CANCEL"; break;
353 case WXK_MBUTTON: key = "MBUTTON"; break;
354 case WXK_CLEAR: key = "CLEAR"; break;
355 case WXK_SHIFT: key = "SHIFT"; break;
356 case WXK_ALT: key = "ALT"; break;
357 case WXK_CONTROL: key = "CONTROL"; break;
358 case WXK_MENU: key = "MENU"; break;
359 case WXK_PAUSE: key = "PAUSE"; break;
360 case WXK_CAPITAL: key = "CAPITAL"; break;
361 case WXK_PRIOR: key = "PRIOR"; break;
362 case WXK_NEXT: key = "NEXT"; break;
363 case WXK_END: key = "END"; break;
364 case WXK_HOME: key = "HOME"; break;
365 case WXK_LEFT: key = "LEFT"; break;
366 case WXK_UP: key = "UP"; break;
367 case WXK_RIGHT: key = "RIGHT"; break;
368 case WXK_DOWN: key = "DOWN"; break;
369 case WXK_SELECT: key = "SELECT"; break;
370 case WXK_PRINT: key = "PRINT"; break;
371 case WXK_EXECUTE: key = "EXECUTE"; break;
372 case WXK_SNAPSHOT: key = "SNAPSHOT"; break;
373 case WXK_INSERT: key = "INSERT"; break;
374 case WXK_HELP: key = "HELP"; break;
375 case WXK_NUMPAD0: key = "NUMPAD0"; break;
376 case WXK_NUMPAD1: key = "NUMPAD1"; break;
377 case WXK_NUMPAD2: key = "NUMPAD2"; break;
378 case WXK_NUMPAD3: key = "NUMPAD3"; break;
379 case WXK_NUMPAD4: key = "NUMPAD4"; break;
380 case WXK_NUMPAD5: key = "NUMPAD5"; break;
381 case WXK_NUMPAD6: key = "NUMPAD6"; break;
382 case WXK_NUMPAD7: key = "NUMPAD7"; break;
383 case WXK_NUMPAD8: key = "NUMPAD8"; break;
384 case WXK_NUMPAD9: key = "NUMPAD9"; break;
385 case WXK_MULTIPLY: key = "MULTIPLY"; break;
386 case WXK_ADD: key = "ADD"; break;
387 case WXK_SEPARATOR: key = "SEPARATOR"; break;
388 case WXK_SUBTRACT: key = "SUBTRACT"; break;
389 case WXK_DECIMAL: key = "DECIMAL"; break;
390 case WXK_DIVIDE: key = "DIVIDE"; break;
391 case WXK_F1: key = "F1"; break;
392 case WXK_F2: key = "F2"; break;
393 case WXK_F3: key = "F3"; break;
394 case WXK_F4: key = "F4"; break;
395 case WXK_F5: key = "F5"; break;
396 case WXK_F6: key = "F6"; break;
397 case WXK_F7: key = "F7"; break;
398 case WXK_F8: key = "F8"; break;
399 case WXK_F9: key = "F9"; break;
400 case WXK_F10: key = "F10"; break;
401 case WXK_F11: key = "F11"; break;
402 case WXK_F12: key = "F12"; break;
403 case WXK_F13: key = "F13"; break;
404 case WXK_F14: key = "F14"; break;
405 case WXK_F15: key = "F15"; break;
406 case WXK_F16: key = "F16"; break;
407 case WXK_F17: key = "F17"; break;
408 case WXK_F18: key = "F18"; break;
409 case WXK_F19: key = "F19"; break;
410 case WXK_F20: key = "F20"; break;
411 case WXK_F21: key = "F21"; break;
412 case WXK_F22: key = "F22"; break;
413 case WXK_F23: key = "F23"; break;
414 case WXK_F24: key = "F24"; break;
415 case WXK_NUMLOCK: key = "NUMLOCK"; break;
416 case WXK_SCROLL: key = "SCROLL"; break;
417 case WXK_PAGEUP: key = "PAGEUP"; break;
418 case WXK_PAGEDOWN: key = "PAGEDOWN"; break;
419 case WXK_NUMPAD_SPACE: key = "NUMPAD_SPACE"; break;
420 case WXK_NUMPAD_TAB: key = "NUMPAD_TAB"; break;
421 case WXK_NUMPAD_ENTER: key = "NUMPAD_ENTER"; break;
422 case WXK_NUMPAD_F1: key = "NUMPAD_F1"; break;
423 case WXK_NUMPAD_F2: key = "NUMPAD_F2"; break;
424 case WXK_NUMPAD_F3: key = "NUMPAD_F3"; break;
425 case WXK_NUMPAD_F4: key = "NUMPAD_F4"; break;
426 case WXK_NUMPAD_HOME: key = "NUMPAD_HOME"; break;
427 case WXK_NUMPAD_LEFT: key = "NUMPAD_LEFT"; break;
428 case WXK_NUMPAD_UP: key = "NUMPAD_UP"; break;
429 case WXK_NUMPAD_RIGHT: key = "NUMPAD_RIGHT"; break;
430 case WXK_NUMPAD_DOWN: key = "NUMPAD_DOWN"; break;
431 case WXK_NUMPAD_PRIOR: key = "NUMPAD_PRIOR"; break;
432 case WXK_NUMPAD_PAGEUP: key = "NUMPAD_PAGEUP"; break;
433 case WXK_NUMPAD_PAGEDOWN: key = "NUMPAD_PAGEDOWN"; break;
434 case WXK_NUMPAD_END: key = "NUMPAD_END"; break;
435 case WXK_NUMPAD_BEGIN: key = "NUMPAD_BEGIN"; break;
436 case WXK_NUMPAD_INSERT: key = "NUMPAD_INSERT"; break;
437 case WXK_NUMPAD_DELETE: key = "NUMPAD_DELETE"; break;
438 case WXK_NUMPAD_EQUAL: key = "NUMPAD_EQUAL"; break;
439 case WXK_NUMPAD_MULTIPLY: key = "NUMPAD_MULTIPLY"; break;
440 case WXK_NUMPAD_ADD: key = "NUMPAD_ADD"; break;
441 case WXK_NUMPAD_SEPARATOR: key = "NUMPAD_SEPARATOR"; break;
442 case WXK_NUMPAD_SUBTRACT: key = "NUMPAD_SUBTRACT"; break;
443 case WXK_NUMPAD_DECIMAL: key = "NUMPAD_DECIMAL"; break;
444
445 default:
446 {
447 if ( wxIsprint((int)keycode) )
448 key.Printf(_T("'%c'"), (char)keycode);
449 else if ( keycode > 0 && keycode < 27 )
450 key.Printf(_("Ctrl-%c"), _T('A') + keycode - 1);
451 else
452 key.Printf(_T("unknown (%ld)"), keycode);
453 }
454 }
455 }
456
457 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
458 name,
459 key.c_str(),
460 GetChar( event.ControlDown(), _T('C') ),
461 GetChar( event.AltDown(), _T('A') ),
462 GetChar( event.ShiftDown(), _T('S') ),
463 GetChar( event.MetaDown(), _T('M') ) );
464
465 if ( m_skip )
466 {
467 event.Skip();
468 }
469 }