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