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