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