]> git.saurik.com Git - wxWidgets.git/blob - samples/keyboard/keyboard.cpp
Convert wxFSW_EVENT_{WARNING,ERROR} to string correctly.
[wxWidgets.git] / samples / keyboard / keyboard.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: keyboard.cpp
3 // Purpose: Keyboard wxWidgets sample
4 // Author: Vadim Zeitlin
5 // Modified by: Marcin Wojdyr
6 // Created: 07.04.02
7 // RCS-ID: $Id$
8 // Copyright: (c) 2002 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17 #ifndef WX_PRECOMP
18 #include "wx/wx.h"
19 #endif
20
21 #ifndef wxHAS_IMAGES_IN_RESOURCES
22 #include "../sample.xpm"
23 #endif
24
25 // Define a new frame type: this is going to be our main frame
26 class MyFrame : public wxFrame
27 {
28 public:
29 MyFrame(const wxString& title);
30
31 private:
32 // event handlers
33 void OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); }
34 void OnAbout(wxCommandEvent& event);
35
36 void OnUseTextCtrl(wxCommandEvent& event);
37
38 void OnTestAccelA(wxCommandEvent& WXUNUSED(event))
39 { m_logText->AppendText("Test accelerator \"A\" used.\n"); }
40 void OnTestAccelCtrlA(wxCommandEvent& WXUNUSED(event))
41 { m_logText->AppendText("Test accelerator \"Ctrl-A\" used.\n"); }
42 void OnTestAccelEsc(wxCommandEvent& WXUNUSED(event))
43 { m_logText->AppendText("Test accelerator \"Esc\" used.\n"); }
44
45 void OnClear(wxCommandEvent& WXUNUSED(event)) { m_logText->Clear(); }
46 void OnSkipDown(wxCommandEvent& event) { m_skipDown = event.IsChecked(); }
47 void OnSkipHook(wxCommandEvent& event) { m_skipHook = event.IsChecked(); }
48
49 void OnKeyDown(wxKeyEvent& event)
50 {
51 LogEvent("KeyDown", event);
52 if ( m_skipDown )
53 event.Skip();
54 }
55 void OnKeyUp(wxKeyEvent& event) { LogEvent("KeyUp", event); }
56 void OnChar(wxKeyEvent& event) { LogEvent("Char", event); }
57 void OnCharHook(wxKeyEvent& event)
58 {
59 // The logged messages can be confusing if the input window doesn't
60 // have focus so warn about this.
61 if ( !m_inputWin->HasFocus() )
62 {
63 m_logText->SetDefaultStyle(*wxRED);
64 m_logText->AppendText("WARNING: focus is not on input window, "
65 "non-hook events won't be logged.\n");
66 m_logText->SetDefaultStyle(wxTextAttr());
67 }
68
69 LogEvent("Hook", event);
70 if ( m_skipHook )
71 event.Skip();
72 }
73
74 void OnPaintInputWin(wxPaintEvent& event);
75
76 void LogEvent(const wxString& name, wxKeyEvent& event);
77
78 // Set m_inputWin to either a new wxWindow or new wxTextCtrl.
79 void DoCreateInputWindow(bool text);
80
81 wxTextCtrl *m_logText;
82 wxWindow *m_inputWin;
83 bool m_skipHook,
84 m_skipDown;
85 };
86
87
88 // Define a new application type, each program should derive a class from wxApp
89 class MyApp : public wxApp
90 {
91 public:
92 // 'Main program' equivalent: the program execution "starts" here
93 virtual bool OnInit()
94 {
95 // create the main application window
96 new MyFrame("Keyboard wxWidgets App");
97
98 // If we returned false here, the application would exit immediately.
99 return true;
100 }
101 };
102
103 // Create a new application object: this macro will allow wxWidgets to create
104 // the application object during program execution (it's better than using a
105 // static object for many reasons) and also declares the accessor function
106 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
107 // not wxApp)
108 IMPLEMENT_APP(MyApp)
109
110
111 // ============================================================================
112 // implementation
113 // ============================================================================
114
115 // frame constructor
116 MyFrame::MyFrame(const wxString& title)
117 : wxFrame(NULL, wxID_ANY, title),
118 m_inputWin(NULL),
119 m_skipHook(true),
120 m_skipDown(true)
121 {
122 SetIcon(wxICON(sample));
123
124 // IDs for menu items
125 enum
126 {
127 QuitID = wxID_EXIT,
128 ClearID = wxID_CLEAR,
129 SkipHook = 100,
130 SkipDown,
131 UseTextCtrl,
132 TestAccelA,
133 TestAccelCtrlA,
134 TestAccelEsc
135 };
136
137 // create a menu bar
138 wxMenu *menuFile = new wxMenu;
139
140 menuFile->Append(ClearID, "&Clear log\tCtrl-L");
141 menuFile->AppendSeparator();
142
143 menuFile->Append(TestAccelA, "Test accelerator &1\tA");
144 menuFile->Append(TestAccelCtrlA, "Test accelerator &2\tCtrl-A");
145 menuFile->Append(TestAccelEsc, "Test accelerator &3\tEsc");
146 menuFile->AppendSeparator();
147
148 menuFile->AppendCheckItem(SkipHook, "Skip CHAR_HOOK event",
149 "Not skipping this event disables both KEY_DOWN and CHAR events"
150 );
151 menuFile->Check(SkipHook, true);
152 menuFile->AppendCheckItem(SkipDown, "Skip KEY_DOWN event",
153 "Not skipping this event disables CHAR event generation"
154 );
155 menuFile->Check(SkipDown, true);
156 menuFile->AppendSeparator();
157
158 menuFile->AppendCheckItem(UseTextCtrl, "Use &text control\tCtrl-T",
159 "Use wxTextCtrl or a simple wxWindow for input window"
160 );
161 menuFile->AppendSeparator();
162
163 menuFile->Append(QuitID, "E&xit\tAlt-X", "Quit this program");
164
165 // the "About" item should be in the help menu
166 wxMenu *menuHelp = new wxMenu;
167 menuHelp->Append(wxID_ABOUT, "&About\tF1", "Show about dialog");
168
169 // now append the freshly created menu to the menu bar...
170 wxMenuBar *menuBar = new wxMenuBar();
171 menuBar->Append(menuFile, "&File");
172 menuBar->Append(menuHelp, "&Help");
173
174 // ... and attach this menu bar to the frame
175 SetMenuBar(menuBar);
176
177 DoCreateInputWindow(false /* simple window initially */);
178
179 wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "",
180 wxDefaultPosition, wxDefaultSize,
181 wxTE_READONLY);
182 headerText->SetValue(
183 " event key KeyCode mod UnicodeKey "
184 " RawKeyCode RawKeyFlags Position");
185
186
187 m_logText = new wxTextCtrl(this, wxID_ANY, "",
188 wxDefaultPosition, wxDefaultSize,
189 wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH|wxHSCROLL);
190
191 // set monospace font to have output in nice columns
192 wxFont font(10, wxFONTFAMILY_TELETYPE,
193 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
194 headerText->SetFont(font);
195 m_logText->SetFont(font);
196
197 // layout
198 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
199 sizer->Add(m_inputWin, wxSizerFlags().Expand());
200 sizer->Add(headerText, wxSizerFlags().Expand());
201 sizer->Add(m_logText, wxSizerFlags(1).Expand());
202 SetSizerAndFit(sizer);
203
204 // set size and position on screen
205 SetSize(700, 340);
206 CentreOnScreen();
207
208 // connect menu event handlers
209
210 Connect(QuitID, wxEVT_COMMAND_MENU_SELECTED,
211 wxCommandEventHandler(MyFrame::OnQuit));
212
213 Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED,
214 wxCommandEventHandler(MyFrame::OnAbout));
215
216 Connect(ClearID, wxEVT_COMMAND_MENU_SELECTED,
217 wxCommandEventHandler(MyFrame::OnClear));
218
219 Connect(SkipHook, wxEVT_COMMAND_MENU_SELECTED,
220 wxCommandEventHandler(MyFrame::OnSkipHook));
221 Connect(SkipDown, wxEVT_COMMAND_MENU_SELECTED,
222 wxCommandEventHandler(MyFrame::OnSkipDown));
223
224 Connect(UseTextCtrl, wxEVT_COMMAND_MENU_SELECTED,
225 wxCommandEventHandler(MyFrame::OnUseTextCtrl));
226
227 Connect(TestAccelA, wxEVT_COMMAND_MENU_SELECTED,
228 wxCommandEventHandler(MyFrame::OnTestAccelA));
229
230 Connect(TestAccelCtrlA, wxEVT_COMMAND_MENU_SELECTED,
231 wxCommandEventHandler(MyFrame::OnTestAccelCtrlA));
232
233 Connect(TestAccelEsc, wxEVT_COMMAND_MENU_SELECTED,
234 wxCommandEventHandler(MyFrame::OnTestAccelEsc));
235
236 // notice that we don't connect OnCharHook() to the input window, unlike
237 // the usual key events this one is propagated upwards
238 Connect(wxEVT_CHAR_HOOK, wxKeyEventHandler(MyFrame::OnCharHook));
239
240 // status bar is useful for showing the menu items help strings
241 CreateStatusBar();
242
243 // and show itself (the frames, unlike simple controls, are not shown when
244 // created initially)
245 Show(true);
246 }
247
248 // event handlers
249
250 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
251 {
252 wxMessageBox("Demonstrates keyboard event processing in wxWidgets\n"
253 "(c) 2002 Vadim Zeitlin\n"
254 "(c) 2008 Marcin Wojdyr",
255 "About wxWidgets Keyboard Sample",
256 wxOK | wxICON_INFORMATION, this);
257 }
258
259 void MyFrame::DoCreateInputWindow(bool text)
260 {
261 wxWindow* const oldWin = m_inputWin;
262
263 m_inputWin = text ? new wxTextCtrl(this, wxID_ANY, "Press keys here",
264 wxDefaultPosition, wxSize(-1, 50),
265 wxTE_MULTILINE)
266 : new wxWindow(this, wxID_ANY,
267 wxDefaultPosition, wxSize(-1, 50),
268 wxRAISED_BORDER);
269 m_inputWin->SetBackgroundColour(*wxBLUE);
270 m_inputWin->SetForegroundColour(*wxWHITE);
271
272 // connect event handlers for the blue input window
273 m_inputWin->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MyFrame::OnKeyDown),
274 NULL, this);
275 m_inputWin->Connect(wxEVT_KEY_UP, wxKeyEventHandler(MyFrame::OnKeyUp),
276 NULL, this);
277 m_inputWin->Connect(wxEVT_CHAR, wxKeyEventHandler(MyFrame::OnChar),
278 NULL, this);
279
280 if ( !text )
281 {
282 m_inputWin->Connect(wxEVT_PAINT,
283 wxPaintEventHandler(MyFrame::OnPaintInputWin),
284 NULL, this);
285 }
286
287 if ( oldWin )
288 {
289 GetSizer()->Replace(oldWin, m_inputWin);
290 Layout();
291 delete oldWin;
292 }
293 }
294
295 void MyFrame::OnUseTextCtrl(wxCommandEvent& event)
296 {
297 DoCreateInputWindow(event.IsChecked());
298 }
299
300 void MyFrame::OnPaintInputWin(wxPaintEvent& WXUNUSED(event))
301 {
302 wxPaintDC dc(m_inputWin);
303 dc.SetTextForeground(*wxWHITE);
304 wxFont font(*wxSWISS_FONT);
305 font.SetWeight(wxFONTWEIGHT_BOLD);
306 font.SetPointSize(font.GetPointSize() + 2);
307 dc.SetFont(font);
308
309 dc.DrawLabel("Press keys here",
310 m_inputWin->GetClientRect(), wxALIGN_CENTER);
311 }
312
313
314 // helper function that returns textual description of wx virtual keycode
315 const char* GetVirtualKeyCodeName(int keycode)
316 {
317 switch ( keycode )
318 {
319 #define WXK_(x) \
320 case WXK_##x: return #x;
321
322 WXK_(BACK)
323 WXK_(TAB)
324 WXK_(RETURN)
325 WXK_(ESCAPE)
326 WXK_(SPACE)
327 WXK_(DELETE)
328 WXK_(START)
329 WXK_(LBUTTON)
330 WXK_(RBUTTON)
331 WXK_(CANCEL)
332 WXK_(MBUTTON)
333 WXK_(CLEAR)
334 WXK_(SHIFT)
335 WXK_(ALT)
336 WXK_(CONTROL)
337 WXK_(MENU)
338 WXK_(PAUSE)
339 WXK_(CAPITAL)
340 WXK_(END)
341 WXK_(HOME)
342 WXK_(LEFT)
343 WXK_(UP)
344 WXK_(RIGHT)
345 WXK_(DOWN)
346 WXK_(SELECT)
347 WXK_(PRINT)
348 WXK_(EXECUTE)
349 WXK_(SNAPSHOT)
350 WXK_(INSERT)
351 WXK_(HELP)
352 WXK_(NUMPAD0)
353 WXK_(NUMPAD1)
354 WXK_(NUMPAD2)
355 WXK_(NUMPAD3)
356 WXK_(NUMPAD4)
357 WXK_(NUMPAD5)
358 WXK_(NUMPAD6)
359 WXK_(NUMPAD7)
360 WXK_(NUMPAD8)
361 WXK_(NUMPAD9)
362 WXK_(MULTIPLY)
363 WXK_(ADD)
364 WXK_(SEPARATOR)
365 WXK_(SUBTRACT)
366 WXK_(DECIMAL)
367 WXK_(DIVIDE)
368 WXK_(F1)
369 WXK_(F2)
370 WXK_(F3)
371 WXK_(F4)
372 WXK_(F5)
373 WXK_(F6)
374 WXK_(F7)
375 WXK_(F8)
376 WXK_(F9)
377 WXK_(F10)
378 WXK_(F11)
379 WXK_(F12)
380 WXK_(F13)
381 WXK_(F14)
382 WXK_(F15)
383 WXK_(F16)
384 WXK_(F17)
385 WXK_(F18)
386 WXK_(F19)
387 WXK_(F20)
388 WXK_(F21)
389 WXK_(F22)
390 WXK_(F23)
391 WXK_(F24)
392 WXK_(NUMLOCK)
393 WXK_(SCROLL)
394 WXK_(PAGEUP)
395 WXK_(PAGEDOWN)
396 WXK_(NUMPAD_SPACE)
397 WXK_(NUMPAD_TAB)
398 WXK_(NUMPAD_ENTER)
399 WXK_(NUMPAD_F1)
400 WXK_(NUMPAD_F2)
401 WXK_(NUMPAD_F3)
402 WXK_(NUMPAD_F4)
403 WXK_(NUMPAD_HOME)
404 WXK_(NUMPAD_LEFT)
405 WXK_(NUMPAD_UP)
406 WXK_(NUMPAD_RIGHT)
407 WXK_(NUMPAD_DOWN)
408 WXK_(NUMPAD_PAGEUP)
409 WXK_(NUMPAD_PAGEDOWN)
410 WXK_(NUMPAD_END)
411 WXK_(NUMPAD_BEGIN)
412 WXK_(NUMPAD_INSERT)
413 WXK_(NUMPAD_DELETE)
414 WXK_(NUMPAD_EQUAL)
415 WXK_(NUMPAD_MULTIPLY)
416 WXK_(NUMPAD_ADD)
417 WXK_(NUMPAD_SEPARATOR)
418 WXK_(NUMPAD_SUBTRACT)
419 WXK_(NUMPAD_DECIMAL)
420 WXK_(NUMPAD_DIVIDE)
421
422 WXK_(WINDOWS_LEFT)
423 WXK_(WINDOWS_RIGHT)
424 #ifdef __WXOSX__
425 WXK_(RAW_CONTROL)
426 #endif
427 #undef WXK_
428
429 default:
430 return NULL;
431 }
432 }
433
434 // helper function that returns textual description of key in the event
435 wxString GetKeyName(const wxKeyEvent &event)
436 {
437 int keycode = event.GetKeyCode();
438 const char* virt = GetVirtualKeyCodeName(keycode);
439 if ( virt )
440 return virt;
441 if ( keycode > 0 && keycode < 32 )
442 return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode - 1));
443 if ( keycode >= 32 && keycode < 128 )
444 return wxString::Format("'%c'", (unsigned char)keycode);
445
446 #if wxUSE_UNICODE
447 int uc = event.GetUnicodeKey();
448 if ( uc != WXK_NONE )
449 return wxString::Format("'%c'", uc);
450 #endif
451
452 return "unknown";
453 }
454
455
456 void MyFrame::LogEvent(const wxString& name, wxKeyEvent& event)
457 {
458 wxString msg;
459 // event key_name KeyCode modifiers Unicode raw_code raw_flags pos
460 msg.Printf("%7s %15s %5d %c%c%c%c"
461 #if wxUSE_UNICODE
462 "%5d (U+%04x)"
463 #else
464 " none "
465 #endif
466 #ifdef wxHAS_RAW_KEY_CODES
467 " %7lu 0x%08lx"
468 #else
469 " not-set not-set"
470 #endif
471 " (%5d,%5d)"
472 "\n",
473 name,
474 GetKeyName(event),
475 event.GetKeyCode(),
476 event.ControlDown() ? 'C' : '-',
477 event.AltDown() ? 'A' : '-',
478 event.ShiftDown() ? 'S' : '-',
479 event.MetaDown() ? 'M' : '-'
480 #if wxUSE_UNICODE
481 , event.GetUnicodeKey()
482 , event.GetUnicodeKey()
483 #endif
484 #ifdef wxHAS_RAW_KEY_CODES
485 , (unsigned long) event.GetRawKeyCode()
486 , (unsigned long) event.GetRawKeyFlags()
487 #endif
488 , event.GetX()
489 , event.GetY()
490 );
491
492 m_logText->AppendText(msg);
493 }
494
495