]> git.saurik.com Git - wxWidgets.git/blame - samples/keyboard/keyboard.cpp
Unselect all wxDataViewCtrl items when clicking outside of the item area.
[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
VZ
24
25// Define a new frame type: this is going to be our main frame
26class MyFrame : public wxFrame
27{
28public:
a96bab36 29 MyFrame(const wxString& title);
7de7aed4
VZ
30
31private:
a96bab36
VZ
32 // event handlers
33 void OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); }
34 void OnAbout(wxCommandEvent& event);
a30305f8 35
7c8bfa87
VZ
36 void OnUseTextCtrl(wxCommandEvent& event);
37
a30305f8
VZ
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
a96bab36 45 void OnClear(wxCommandEvent& WXUNUSED(event)) { m_logText->Clear(); }
edf5822a
VZ
46 void OnSkipDown(wxCommandEvent& event) { m_skipDown = event.IsChecked(); }
47 void OnSkipHook(wxCommandEvent& event) { m_skipHook = event.IsChecked(); }
a30305f8 48
edf5822a
VZ
49 void OnKeyDown(wxKeyEvent& event)
50 {
51 LogEvent("KeyDown", event);
52 if ( m_skipDown )
53 event.Skip();
54 }
a96bab36
VZ
55 void OnKeyUp(wxKeyEvent& event) { LogEvent("KeyUp", event); }
56 void OnChar(wxKeyEvent& event) { LogEvent("Char", event); }
edf5822a
VZ
57 void OnCharHook(wxKeyEvent& event)
58 {
c4f49d69
VZ
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
edf5822a
VZ
69 LogEvent("Hook", event);
70 if ( m_skipHook )
71 event.Skip();
72 }
73
a96bab36 74 void OnPaintInputWin(wxPaintEvent& event);
7de7aed4 75
a96bab36 76 void LogEvent(const wxString& name, wxKeyEvent& event);
7de7aed4 77
7c8bfa87
VZ
78 // Set m_inputWin to either a new wxWindow or new wxTextCtrl.
79 void DoCreateInputWindow(bool text);
80
a96bab36
VZ
81 wxTextCtrl *m_logText;
82 wxWindow *m_inputWin;
edf5822a
VZ
83 bool m_skipHook,
84 m_skipDown;
7de7aed4
VZ
85};
86
7de7aed4 87
a96bab36
VZ
88// Define a new application type, each program should derive a class from wxApp
89class MyApp : public wxApp
7de7aed4
VZ
90{
91public:
a96bab36
VZ
92 // 'Main program' equivalent: the program execution "starts" here
93 virtual bool OnInit()
7de7aed4 94 {
a96bab36
VZ
95 // create the main application window
96 new MyFrame("Keyboard wxWidgets App");
7de7aed4 97
a96bab36
VZ
98 // If we returned false here, the application would exit immediately.
99 return true;
7de7aed4 100 }
7de7aed4
VZ
101};
102
be5a51fb 103// Create a new application object: this macro will allow wxWidgets to create
7de7aed4
VZ
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)
108IMPLEMENT_APP(MyApp)
109
a96bab36 110
7de7aed4
VZ
111// ============================================================================
112// implementation
113// ============================================================================
114
7de7aed4 115// frame constructor
a96bab36
VZ
116MyFrame::MyFrame(const wxString& title)
117 : wxFrame(NULL, wxID_ANY, title),
118 m_inputWin(NULL),
edf5822a
VZ
119 m_skipHook(true),
120 m_skipDown(true)
7de7aed4 121{
41f02b9a
FM
122 SetIcon(wxICON(sample));
123
a96bab36
VZ
124 // IDs for menu items
125 enum
126 {
127 QuitID = wxID_EXIT,
128 ClearID = wxID_CLEAR,
edf5822a
VZ
129 SkipHook = 100,
130 SkipDown,
7c8bfa87 131 UseTextCtrl,
a30305f8
VZ
132 TestAccelA,
133 TestAccelCtrlA,
134 TestAccelEsc
a96bab36
VZ
135 };
136
7de7aed4
VZ
137 // create a menu bar
138 wxMenu *menuFile = new wxMenu;
a96bab36
VZ
139
140 menuFile->Append(ClearID, "&Clear log\tCtrl-L");
141 menuFile->AppendSeparator();
142
a30305f8
VZ
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
edf5822a
VZ
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);
7de7aed4 156 menuFile->AppendSeparator();
7de7aed4 157
7c8bfa87
VZ
158 menuFile->AppendCheckItem(UseTextCtrl, "Use &text control\tCtrl-T",
159 "Use wxTextCtrl or a simple wxWindow for input window"
160 );
161 menuFile->AppendSeparator();
162
a96bab36 163 menuFile->Append(QuitID, "E&xit\tAlt-X", "Quit this program");
7de7aed4
VZ
164
165 // the "About" item should be in the help menu
166 wxMenu *menuHelp = new wxMenu;
2d143b66 167 menuHelp->Append(wxID_ABOUT, "&About\tF1", "Show about dialog");
7de7aed4
VZ
168
169 // now append the freshly created menu to the menu bar...
170 wxMenuBar *menuBar = new wxMenuBar();
a96bab36
VZ
171 menuBar->Append(menuFile, "&File");
172 menuBar->Append(menuHelp, "&Help");
7de7aed4
VZ
173
174 // ... and attach this menu bar to the frame
175 SetMenuBar(menuBar);
176
7c8bfa87 177 DoCreateInputWindow(false /* simple window initially */);
a96bab36
VZ
178
179 wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "",
180 wxDefaultPosition, wxDefaultSize,
181 wxTE_READONLY);
182 headerText->SetValue(
183 " event key KeyCode mod UnicodeKey "
2f7baaec 184 " RawKeyCode RawKeyFlags Position");
a96bab36
VZ
185
186
41f02b9a 187 m_logText = new wxTextCtrl(this, wxID_ANY, "",
a96bab36 188 wxDefaultPosition, wxDefaultSize,
c4f49d69 189 wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH|wxHSCROLL);
a96bab36
VZ
190
191 // set monospace font to have output in nice columns
41f02b9a 192 wxFont font(10, wxFONTFAMILY_TELETYPE,
a96bab36
VZ
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
edf5822a
VZ
219 Connect(SkipHook, wxEVT_COMMAND_MENU_SELECTED,
220 wxCommandEventHandler(MyFrame::OnSkipHook));
221 Connect(SkipDown, wxEVT_COMMAND_MENU_SELECTED,
222 wxCommandEventHandler(MyFrame::OnSkipDown));
a96bab36 223
7c8bfa87
VZ
224 Connect(UseTextCtrl, wxEVT_COMMAND_MENU_SELECTED,
225 wxCommandEventHandler(MyFrame::OnUseTextCtrl));
226
a30305f8
VZ
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
edf5822a
VZ
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
a96bab36
VZ
243 // and show itself (the frames, unlike simple controls, are not shown when
244 // created initially)
245 Show(true);
7de7aed4
VZ
246}
247
248// event handlers
249
7de7aed4
VZ
250void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
251{
a96bab36
VZ
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);
7de7aed4
VZ
257}
258
7c8bfa87
VZ
259void 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
295void MyFrame::OnUseTextCtrl(wxCommandEvent& event)
296{
297 DoCreateInputWindow(event.IsChecked());
298}
299
a96bab36 300void MyFrame::OnPaintInputWin(wxPaintEvent& WXUNUSED(event))
7de7aed4 301{
a96bab36
VZ
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
41f02b9a 309 dc.DrawLabel("Press keys here",
a96bab36 310 m_inputWin->GetClientRect(), wxALIGN_CENTER);
7de7aed4
VZ
311}
312
62fa9712 313
a96bab36
VZ
314// helper function that returns textual description of wx virtual keycode
315const char* GetVirtualKeyCodeName(int keycode)
7de7aed4 316{
a96bab36 317 switch ( keycode )
7de7aed4 318 {
a96bab36 319#define WXK_(x) \
41f02b9a 320 case WXK_##x: return #x;
a96bab36
VZ
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)
e7b12eec
VZ
421
422 WXK_(WINDOWS_LEFT)
423 WXK_(WINDOWS_RIGHT)
d3f81b85
SC
424#ifdef __WXOSX__
425 WXK_(RAW_CONTROL)
426#endif
a96bab36 427#undef WXK_
e7b12eec 428
41f02b9a
FM
429 default:
430 return NULL;
7de7aed4
VZ
431 }
432}
433
a96bab36
VZ
434// helper function that returns textual description of key in the event
435wxString GetKeyName(const wxKeyEvent &event)
7de7aed4 436{
a96bab36
VZ
437 int keycode = event.GetKeyCode();
438 const char* virt = GetVirtualKeyCodeName(keycode);
439 if ( virt )
440 return virt;
b6885972 441 if ( keycode > 0 && keycode < 32 )
a96bab36 442 return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode - 1));
b6885972 443 if ( keycode >= 32 && keycode < 128 )
a96bab36 444 return wxString::Format("'%c'", (unsigned char)keycode);
e7b12eec 445
52bbab36 446#if wxUSE_UNICODE
e7b12eec
VZ
447 int uc = event.GetUnicodeKey();
448 if ( uc != WXK_NONE )
449 return wxString::Format("'%c'", uc);
52bbab36 450#endif
e7b12eec
VZ
451
452 return "unknown";
a96bab36
VZ
453}
454
7de7aed4 455
a96bab36
VZ
456void MyFrame::LogEvent(const wxString& name, wxKeyEvent& event)
457{
62fa9712 458 wxString msg;
2f7baaec 459 // event key_name KeyCode modifiers Unicode raw_code raw_flags pos
a96bab36
VZ
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
b6885972 467 " %7lu 0x%08lx"
a96bab36
VZ
468#else
469 " not-set not-set"
470#endif
2f7baaec 471 " (%5d,%5d)"
a96bab36 472 "\n",
62fa9712 473 name,
a96bab36
VZ
474 GetKeyName(event),
475 event.GetKeyCode(),
476 event.ControlDown() ? 'C' : '-',
477 event.AltDown() ? 'A' : '-',
478 event.ShiftDown() ? 'S' : '-',
66ad7d6a 479 event.MetaDown() ? 'M' : '-'
a96bab36 480#if wxUSE_UNICODE
66ad7d6a
VZ
481 , event.GetUnicodeKey()
482 , event.GetUnicodeKey()
a96bab36
VZ
483#endif
484#ifdef wxHAS_RAW_KEY_CODES
66ad7d6a
VZ
485 , (unsigned long) event.GetRawKeyCode()
486 , (unsigned long) event.GetRawKeyFlags()
a96bab36 487#endif
2f7baaec
VZ
488 , event.GetX()
489 , event.GetY()
a96bab36 490 );
62fa9712 491
a96bab36 492 m_logText->AppendText(msg);
7de7aed4 493}
a96bab36
VZ
494
495