]> git.saurik.com Git - wxWidgets.git/blame - samples/keyboard/keyboard.cpp
Remove remaining occurrences of wxUSE_XPM_IN_MSW.
[wxWidgets.git] / samples / keyboard / keyboard.cpp
CommitLineData
7de7aed4
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: keyboard.cpp
be5a51fb 3// Purpose: Keyboard wxWidgets sample
7de7aed4 4// Author: Vadim Zeitlin
a96bab36 5// Modified by: Marcin Wojdyr
7de7aed4
VZ
6// Created: 07.04.02
7// RCS-ID: $Id$
8// Copyright: (c) 2002 Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
7de7aed4
VZ
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
7de7aed4
VZ
17#ifndef WX_PRECOMP
18 #include "wx/wx.h"
19#endif
20
e7092398 21#ifndef wxHAS_IMAGES_IN_RESOURCES
41f02b9a
FM
22 #include "../sample.xpm"
23#endif
7de7aed4 24
c77ef57e
VZ
25// IDs for menu items
26enum
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
7de7aed4
VZ
43// Define a new frame type: this is going to be our main frame
44class MyFrame : public wxFrame
45{
46public:
a96bab36 47 MyFrame(const wxString& title);
7de7aed4
VZ
48
49private:
a96bab36
VZ
50 // event handlers
51 void OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); }
52 void OnAbout(wxCommandEvent& event);
a30305f8 53
c77ef57e 54 void OnInputWindowKind(wxCommandEvent& event);
7c8bfa87 55
a30305f8
VZ
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
a96bab36 63 void OnClear(wxCommandEvent& WXUNUSED(event)) { m_logText->Clear(); }
edf5822a
VZ
64 void OnSkipDown(wxCommandEvent& event) { m_skipDown = event.IsChecked(); }
65 void OnSkipHook(wxCommandEvent& event) { m_skipHook = event.IsChecked(); }
a30305f8 66
edf5822a
VZ
67 void OnKeyDown(wxKeyEvent& event)
68 {
69 LogEvent("KeyDown", event);
70 if ( m_skipDown )
71 event.Skip();
72 }
a96bab36 73 void OnKeyUp(wxKeyEvent& event) { LogEvent("KeyUp", event); }
819638a7 74 void OnChar(wxKeyEvent& event) { LogEvent("Char", event); event.Skip(); }
edf5822a
VZ
75 void OnCharHook(wxKeyEvent& event)
76 {
c4f49d69
VZ
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
edf5822a
VZ
87 LogEvent("Hook", event);
88 if ( m_skipHook )
89 event.Skip();
90 }
91
a96bab36 92 void OnPaintInputWin(wxPaintEvent& event);
7de7aed4 93
a96bab36 94 void LogEvent(const wxString& name, wxKeyEvent& event);
7de7aed4 95
c77ef57e
VZ
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);
7c8bfa87 105
a96bab36
VZ
106 wxTextCtrl *m_logText;
107 wxWindow *m_inputWin;
edf5822a
VZ
108 bool m_skipHook,
109 m_skipDown;
7de7aed4
VZ
110};
111
7de7aed4 112
a96bab36
VZ
113// Define a new application type, each program should derive a class from wxApp
114class MyApp : public wxApp
7de7aed4
VZ
115{
116public:
a96bab36
VZ
117 // 'Main program' equivalent: the program execution "starts" here
118 virtual bool OnInit()
7de7aed4 119 {
a96bab36
VZ
120 // create the main application window
121 new MyFrame("Keyboard wxWidgets App");
7de7aed4 122
a96bab36
VZ
123 // If we returned false here, the application would exit immediately.
124 return true;
7de7aed4 125 }
7de7aed4
VZ
126};
127
be5a51fb 128// Create a new application object: this macro will allow wxWidgets to create
7de7aed4
VZ
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)
133IMPLEMENT_APP(MyApp)
134
a96bab36 135
7de7aed4
VZ
136// ============================================================================
137// implementation
138// ============================================================================
139
7de7aed4 140// frame constructor
a96bab36
VZ
141MyFrame::MyFrame(const wxString& title)
142 : wxFrame(NULL, wxID_ANY, title),
143 m_inputWin(NULL),
edf5822a
VZ
144 m_skipHook(true),
145 m_skipDown(true)
7de7aed4 146{
41f02b9a
FM
147 SetIcon(wxICON(sample));
148
7de7aed4
VZ
149 // create a menu bar
150 wxMenu *menuFile = new wxMenu;
a96bab36
VZ
151
152 menuFile->Append(ClearID, "&Clear log\tCtrl-L");
153 menuFile->AppendSeparator();
154
a30305f8
VZ
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
edf5822a
VZ
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);
7de7aed4 168 menuFile->AppendSeparator();
7de7aed4 169
c77ef57e
VZ
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"
7c8bfa87
VZ
178 );
179 menuFile->AppendSeparator();
180
a96bab36 181 menuFile->Append(QuitID, "E&xit\tAlt-X", "Quit this program");
7de7aed4
VZ
182
183 // the "About" item should be in the help menu
184 wxMenu *menuHelp = new wxMenu;
2d143b66 185 menuHelp->Append(wxID_ABOUT, "&About\tF1", "Show about dialog");
7de7aed4
VZ
186
187 // now append the freshly created menu to the menu bar...
188 wxMenuBar *menuBar = new wxMenuBar();
a96bab36
VZ
189 menuBar->Append(menuFile, "&File");
190 menuBar->Append(menuHelp, "&Help");
7de7aed4
VZ
191
192 // ... and attach this menu bar to the frame
193 SetMenuBar(menuBar);
194
c77ef57e 195 DoCreateInputWindow(Input_Custom);
a96bab36
VZ
196
197 wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "",
198 wxDefaultPosition, wxDefaultSize,
199 wxTE_READONLY);
200 headerText->SetValue(
201 " event key KeyCode mod UnicodeKey "
2f7baaec 202 " RawKeyCode RawKeyFlags Position");
a96bab36
VZ
203
204
41f02b9a 205 m_logText = new wxTextCtrl(this, wxID_ANY, "",
a96bab36 206 wxDefaultPosition, wxDefaultSize,
c4f49d69 207 wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH|wxHSCROLL);
a96bab36
VZ
208
209 // set monospace font to have output in nice columns
41f02b9a 210 wxFont font(10, wxFONTFAMILY_TELETYPE,
a96bab36
VZ
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_COMMAND_MENU_SELECTED,
229 wxCommandEventHandler(MyFrame::OnQuit));
230
231 Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED,
232 wxCommandEventHandler(MyFrame::OnAbout));
233
234 Connect(ClearID, wxEVT_COMMAND_MENU_SELECTED,
235 wxCommandEventHandler(MyFrame::OnClear));
236
edf5822a
VZ
237 Connect(SkipHook, wxEVT_COMMAND_MENU_SELECTED,
238 wxCommandEventHandler(MyFrame::OnSkipHook));
239 Connect(SkipDown, wxEVT_COMMAND_MENU_SELECTED,
240 wxCommandEventHandler(MyFrame::OnSkipDown));
a96bab36 241
c77ef57e
VZ
242 Connect(IDInputCustom, IDInputText, wxEVT_COMMAND_MENU_SELECTED,
243 wxCommandEventHandler(MyFrame::OnInputWindowKind));
7c8bfa87 244
a30305f8
VZ
245 Connect(TestAccelA, wxEVT_COMMAND_MENU_SELECTED,
246 wxCommandEventHandler(MyFrame::OnTestAccelA));
247
248 Connect(TestAccelCtrlA, wxEVT_COMMAND_MENU_SELECTED,
249 wxCommandEventHandler(MyFrame::OnTestAccelCtrlA));
250
251 Connect(TestAccelEsc, wxEVT_COMMAND_MENU_SELECTED,
252 wxCommandEventHandler(MyFrame::OnTestAccelEsc));
253
edf5822a
VZ
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
a96bab36
VZ
261 // and show itself (the frames, unlike simple controls, are not shown when
262 // created initially)
263 Show(true);
7de7aed4
VZ
264}
265
266// event handlers
267
7de7aed4
VZ
268void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
269{
a96bab36
VZ
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);
7de7aed4
VZ
275}
276
c77ef57e 277void MyFrame::DoCreateInputWindow(InputKind inputKind)
7c8bfa87
VZ
278{
279 wxWindow* const oldWin = m_inputWin;
280
c77ef57e
VZ
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
7c8bfa87
VZ
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
c77ef57e 311 if ( inputKind == Input_Custom )
7c8bfa87
VZ
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
c77ef57e 326void MyFrame::OnInputWindowKind(wxCommandEvent& event)
7c8bfa87 327{
c77ef57e
VZ
328 DoCreateInputWindow(
329 static_cast<InputKind>(event.GetId() - IDInputCustom)
330 );
7c8bfa87
VZ
331}
332
a96bab36 333void MyFrame::OnPaintInputWin(wxPaintEvent& WXUNUSED(event))
7de7aed4 334{
a96bab36
VZ
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
41f02b9a 342 dc.DrawLabel("Press keys here",
a96bab36 343 m_inputWin->GetClientRect(), wxALIGN_CENTER);
7de7aed4
VZ
344}
345
62fa9712 346
a96bab36
VZ
347// helper function that returns textual description of wx virtual keycode
348const char* GetVirtualKeyCodeName(int keycode)
7de7aed4 349{
a96bab36 350 switch ( keycode )
7de7aed4 351 {
a96bab36 352#define WXK_(x) \
41f02b9a 353 case WXK_##x: return #x;
a96bab36
VZ
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)
e7b12eec
VZ
454
455 WXK_(WINDOWS_LEFT)
456 WXK_(WINDOWS_RIGHT)
d3f81b85
SC
457#ifdef __WXOSX__
458 WXK_(RAW_CONTROL)
459#endif
a96bab36 460#undef WXK_
e7b12eec 461
41f02b9a
FM
462 default:
463 return NULL;
7de7aed4
VZ
464 }
465}
466
a96bab36
VZ
467// helper function that returns textual description of key in the event
468wxString GetKeyName(const wxKeyEvent &event)
7de7aed4 469{
a96bab36
VZ
470 int keycode = event.GetKeyCode();
471 const char* virt = GetVirtualKeyCodeName(keycode);
472 if ( virt )
473 return virt;
b6885972 474 if ( keycode > 0 && keycode < 32 )
a96bab36 475 return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode - 1));
b6885972 476 if ( keycode >= 32 && keycode < 128 )
a96bab36 477 return wxString::Format("'%c'", (unsigned char)keycode);
e7b12eec 478
52bbab36 479#if wxUSE_UNICODE
e7b12eec
VZ
480 int uc = event.GetUnicodeKey();
481 if ( uc != WXK_NONE )
482 return wxString::Format("'%c'", uc);
52bbab36 483#endif
e7b12eec
VZ
484
485 return "unknown";
a96bab36
VZ
486}
487
7de7aed4 488
a96bab36
VZ
489void MyFrame::LogEvent(const wxString& name, wxKeyEvent& event)
490{
62fa9712 491 wxString msg;
2f7baaec 492 // event key_name KeyCode modifiers Unicode raw_code raw_flags pos
a96bab36
VZ
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
b6885972 500 " %7lu 0x%08lx"
a96bab36
VZ
501#else
502 " not-set not-set"
503#endif
2f7baaec 504 " (%5d,%5d)"
a96bab36 505 "\n",
62fa9712 506 name,
a96bab36
VZ
507 GetKeyName(event),
508 event.GetKeyCode(),
509 event.ControlDown() ? 'C' : '-',
510 event.AltDown() ? 'A' : '-',
511 event.ShiftDown() ? 'S' : '-',
66ad7d6a 512 event.MetaDown() ? 'M' : '-'
a96bab36 513#if wxUSE_UNICODE
66ad7d6a
VZ
514 , event.GetUnicodeKey()
515 , event.GetUnicodeKey()
a96bab36
VZ
516#endif
517#ifdef wxHAS_RAW_KEY_CODES
66ad7d6a
VZ
518 , (unsigned long) event.GetRawKeyCode()
519 , (unsigned long) event.GetRawKeyFlags()
a96bab36 520#endif
2f7baaec
VZ
521 , event.GetX()
522 , event.GetY()
a96bab36 523 );
62fa9712 524
a96bab36 525 m_logText->AppendText(msg);
7de7aed4 526}
a96bab36
VZ
527
528