]> git.saurik.com Git - wxWidgets.git/blob - samples/keyboard/keyboard.cpp
Fix problems with reference counting in wxActiveXContainer.
[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 __WXMSW__
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 OnTestAccelA(wxCommandEvent& WXUNUSED(event))
37 { m_logText->AppendText("Test accelerator \"A\" used.\n"); }
38 void OnTestAccelCtrlA(wxCommandEvent& WXUNUSED(event))
39 { m_logText->AppendText("Test accelerator \"Ctrl-A\" used.\n"); }
40 void OnTestAccelEsc(wxCommandEvent& WXUNUSED(event))
41 { m_logText->AppendText("Test accelerator \"Esc\" used.\n"); }
42
43 void OnClear(wxCommandEvent& WXUNUSED(event)) { m_logText->Clear(); }
44 void OnSkip(wxCommandEvent& event) { m_skip = event.IsChecked(); }
45
46 void OnKeyDown(wxKeyEvent& event) { LogEvent("KeyDown", event); }
47 void OnKeyUp(wxKeyEvent& event) { LogEvent("KeyUp", event); }
48 void OnChar(wxKeyEvent& event) { LogEvent("Char", event); }
49 void OnPaintInputWin(wxPaintEvent& event);
50
51 void LogEvent(const wxString& name, wxKeyEvent& event);
52
53 wxTextCtrl *m_logText;
54 wxWindow *m_inputWin;
55 bool m_skip;
56 };
57
58
59 // Define a new application type, each program should derive a class from wxApp
60 class MyApp : public wxApp
61 {
62 public:
63 // 'Main program' equivalent: the program execution "starts" here
64 virtual bool OnInit()
65 {
66 // create the main application window
67 new MyFrame("Keyboard wxWidgets App");
68
69 // If we returned false here, the application would exit immediately.
70 return true;
71 }
72 };
73
74 // Create a new application object: this macro will allow wxWidgets to create
75 // the application object during program execution (it's better than using a
76 // static object for many reasons) and also declares the accessor function
77 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
78 // not wxApp)
79 IMPLEMENT_APP(MyApp)
80
81
82 // ============================================================================
83 // implementation
84 // ============================================================================
85
86 // frame constructor
87 MyFrame::MyFrame(const wxString& title)
88 : wxFrame(NULL, wxID_ANY, title),
89 m_inputWin(NULL),
90 m_skip(true)
91 {
92 SetIcon(wxICON(sample));
93
94 // IDs for menu items
95 enum
96 {
97 QuitID = wxID_EXIT,
98 ClearID = wxID_CLEAR,
99 SkipID = 100,
100 TestAccelA,
101 TestAccelCtrlA,
102 TestAccelEsc
103 };
104
105 // create a menu bar
106 wxMenu *menuFile = new wxMenu;
107
108 menuFile->Append(ClearID, "&Clear log\tCtrl-L");
109 menuFile->AppendSeparator();
110
111 menuFile->Append(TestAccelA, "Test accelerator &1\tA");
112 menuFile->Append(TestAccelCtrlA, "Test accelerator &2\tCtrl-A");
113 menuFile->Append(TestAccelEsc, "Test accelerator &3\tEsc");
114 menuFile->AppendSeparator();
115
116 menuFile->AppendCheckItem(SkipID, "Call event.&Skip()\tCtrl-S");
117 menuFile->Check(SkipID, true);
118 menuFile->AppendSeparator();
119
120 menuFile->Append(QuitID, "E&xit\tAlt-X", "Quit this program");
121
122 // the "About" item should be in the help menu
123 wxMenu *menuHelp = new wxMenu;
124 menuHelp->Append(wxID_ABOUT, "&About...\tF1", "Show about dialog");
125
126 // now append the freshly created menu to the menu bar...
127 wxMenuBar *menuBar = new wxMenuBar();
128 menuBar->Append(menuFile, "&File");
129 menuBar->Append(menuHelp, "&Help");
130
131 // ... and attach this menu bar to the frame
132 SetMenuBar(menuBar);
133
134 m_inputWin = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxSize(-1, 50),
135 wxRAISED_BORDER),
136 m_inputWin->SetBackgroundColour(*wxBLUE);
137
138 wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "",
139 wxDefaultPosition, wxDefaultSize,
140 wxTE_READONLY);
141 headerText->SetValue(
142 " event key KeyCode mod UnicodeKey "
143 " RawKeyCode RawKeyFlags");
144
145
146 m_logText = new wxTextCtrl(this, wxID_ANY, "",
147 wxDefaultPosition, wxDefaultSize,
148 wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL);
149
150 // set monospace font to have output in nice columns
151 wxFont font(10, wxFONTFAMILY_TELETYPE,
152 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
153 headerText->SetFont(font);
154 m_logText->SetFont(font);
155
156 // layout
157 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
158 sizer->Add(m_inputWin, wxSizerFlags().Expand());
159 sizer->Add(headerText, wxSizerFlags().Expand());
160 sizer->Add(m_logText, wxSizerFlags(1).Expand());
161 SetSizerAndFit(sizer);
162
163 // set size and position on screen
164 SetSize(700, 340);
165 CentreOnScreen();
166
167 // connect menu event handlers
168
169 Connect(QuitID, wxEVT_COMMAND_MENU_SELECTED,
170 wxCommandEventHandler(MyFrame::OnQuit));
171
172 Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED,
173 wxCommandEventHandler(MyFrame::OnAbout));
174
175 Connect(ClearID, wxEVT_COMMAND_MENU_SELECTED,
176 wxCommandEventHandler(MyFrame::OnClear));
177
178 Connect(SkipID, wxEVT_COMMAND_MENU_SELECTED,
179 wxCommandEventHandler(MyFrame::OnSkip));
180
181 Connect(TestAccelA, wxEVT_COMMAND_MENU_SELECTED,
182 wxCommandEventHandler(MyFrame::OnTestAccelA));
183
184 Connect(TestAccelCtrlA, wxEVT_COMMAND_MENU_SELECTED,
185 wxCommandEventHandler(MyFrame::OnTestAccelCtrlA));
186
187 Connect(TestAccelEsc, wxEVT_COMMAND_MENU_SELECTED,
188 wxCommandEventHandler(MyFrame::OnTestAccelEsc));
189
190 // connect event handlers for the blue input window
191 m_inputWin->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MyFrame::OnKeyDown),
192 NULL, this);
193 m_inputWin->Connect(wxEVT_KEY_UP, wxKeyEventHandler(MyFrame::OnKeyUp),
194 NULL, this);
195 m_inputWin->Connect(wxEVT_CHAR, wxKeyEventHandler(MyFrame::OnChar),
196 NULL, this);
197 m_inputWin->Connect(wxEVT_PAINT,
198 wxPaintEventHandler(MyFrame::OnPaintInputWin),
199 NULL, this);
200
201 // and show itself (the frames, unlike simple controls, are not shown when
202 // created initially)
203 Show(true);
204 }
205
206 // event handlers
207
208 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
209 {
210 wxMessageBox("Demonstrates keyboard event processing in wxWidgets\n"
211 "(c) 2002 Vadim Zeitlin\n"
212 "(c) 2008 Marcin Wojdyr",
213 "About wxWidgets Keyboard Sample",
214 wxOK | wxICON_INFORMATION, this);
215 }
216
217 void MyFrame::OnPaintInputWin(wxPaintEvent& WXUNUSED(event))
218 {
219 wxPaintDC dc(m_inputWin);
220 dc.SetTextForeground(*wxWHITE);
221 wxFont font(*wxSWISS_FONT);
222 font.SetWeight(wxFONTWEIGHT_BOLD);
223 font.SetPointSize(font.GetPointSize() + 2);
224 dc.SetFont(font);
225
226 dc.DrawLabel("Press keys here",
227 m_inputWin->GetClientRect(), wxALIGN_CENTER);
228 }
229
230
231 // helper function that returns textual description of wx virtual keycode
232 const char* GetVirtualKeyCodeName(int keycode)
233 {
234 switch ( keycode )
235 {
236 #define WXK_(x) \
237 case WXK_##x: return #x;
238
239 WXK_(BACK)
240 WXK_(TAB)
241 WXK_(RETURN)
242 WXK_(ESCAPE)
243 WXK_(SPACE)
244 WXK_(DELETE)
245 WXK_(START)
246 WXK_(LBUTTON)
247 WXK_(RBUTTON)
248 WXK_(CANCEL)
249 WXK_(MBUTTON)
250 WXK_(CLEAR)
251 WXK_(SHIFT)
252 WXK_(ALT)
253 WXK_(CONTROL)
254 WXK_(MENU)
255 WXK_(PAUSE)
256 WXK_(CAPITAL)
257 WXK_(END)
258 WXK_(HOME)
259 WXK_(LEFT)
260 WXK_(UP)
261 WXK_(RIGHT)
262 WXK_(DOWN)
263 WXK_(SELECT)
264 WXK_(PRINT)
265 WXK_(EXECUTE)
266 WXK_(SNAPSHOT)
267 WXK_(INSERT)
268 WXK_(HELP)
269 WXK_(NUMPAD0)
270 WXK_(NUMPAD1)
271 WXK_(NUMPAD2)
272 WXK_(NUMPAD3)
273 WXK_(NUMPAD4)
274 WXK_(NUMPAD5)
275 WXK_(NUMPAD6)
276 WXK_(NUMPAD7)
277 WXK_(NUMPAD8)
278 WXK_(NUMPAD9)
279 WXK_(MULTIPLY)
280 WXK_(ADD)
281 WXK_(SEPARATOR)
282 WXK_(SUBTRACT)
283 WXK_(DECIMAL)
284 WXK_(DIVIDE)
285 WXK_(F1)
286 WXK_(F2)
287 WXK_(F3)
288 WXK_(F4)
289 WXK_(F5)
290 WXK_(F6)
291 WXK_(F7)
292 WXK_(F8)
293 WXK_(F9)
294 WXK_(F10)
295 WXK_(F11)
296 WXK_(F12)
297 WXK_(F13)
298 WXK_(F14)
299 WXK_(F15)
300 WXK_(F16)
301 WXK_(F17)
302 WXK_(F18)
303 WXK_(F19)
304 WXK_(F20)
305 WXK_(F21)
306 WXK_(F22)
307 WXK_(F23)
308 WXK_(F24)
309 WXK_(NUMLOCK)
310 WXK_(SCROLL)
311 WXK_(PAGEUP)
312 WXK_(PAGEDOWN)
313 WXK_(NUMPAD_SPACE)
314 WXK_(NUMPAD_TAB)
315 WXK_(NUMPAD_ENTER)
316 WXK_(NUMPAD_F1)
317 WXK_(NUMPAD_F2)
318 WXK_(NUMPAD_F3)
319 WXK_(NUMPAD_F4)
320 WXK_(NUMPAD_HOME)
321 WXK_(NUMPAD_LEFT)
322 WXK_(NUMPAD_UP)
323 WXK_(NUMPAD_RIGHT)
324 WXK_(NUMPAD_DOWN)
325 WXK_(NUMPAD_PAGEUP)
326 WXK_(NUMPAD_PAGEDOWN)
327 WXK_(NUMPAD_END)
328 WXK_(NUMPAD_BEGIN)
329 WXK_(NUMPAD_INSERT)
330 WXK_(NUMPAD_DELETE)
331 WXK_(NUMPAD_EQUAL)
332 WXK_(NUMPAD_MULTIPLY)
333 WXK_(NUMPAD_ADD)
334 WXK_(NUMPAD_SEPARATOR)
335 WXK_(NUMPAD_SUBTRACT)
336 WXK_(NUMPAD_DECIMAL)
337 WXK_(NUMPAD_DIVIDE)
338
339 WXK_(WINDOWS_LEFT)
340 WXK_(WINDOWS_RIGHT)
341 WXK_(COMMAND)
342 #undef WXK_
343
344 default:
345 return NULL;
346 }
347 }
348
349 // helper function that returns textual description of key in the event
350 wxString GetKeyName(const wxKeyEvent &event)
351 {
352 int keycode = event.GetKeyCode();
353 const char* virt = GetVirtualKeyCodeName(keycode);
354 if ( virt )
355 return virt;
356 if ( keycode > 0 && keycode < 32 )
357 return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode - 1));
358 if ( keycode >= 32 && keycode < 128 )
359 return wxString::Format("'%c'", (unsigned char)keycode);
360
361 #if wxUSE_UNICODE
362 int uc = event.GetUnicodeKey();
363 if ( uc != WXK_NONE )
364 return wxString::Format("'%c'", uc);
365 #endif
366
367 return "unknown";
368 }
369
370
371 void MyFrame::LogEvent(const wxString& name, wxKeyEvent& event)
372 {
373 wxString msg;
374 // event key_name KeyCode modifiers Unicode raw_code raw_flags
375 msg.Printf("%7s %15s %5d %c%c%c%c"
376 #if wxUSE_UNICODE
377 "%5d (U+%04x)"
378 #else
379 " none "
380 #endif
381 #ifdef wxHAS_RAW_KEY_CODES
382 " %7lu 0x%08lx"
383 #else
384 " not-set not-set"
385 #endif
386 "\n",
387 name,
388 GetKeyName(event),
389 event.GetKeyCode(),
390 event.ControlDown() ? 'C' : '-',
391 event.AltDown() ? 'A' : '-',
392 event.ShiftDown() ? 'S' : '-',
393 event.MetaDown() ? 'M' : '-'
394 #if wxUSE_UNICODE
395 , event.GetUnicodeKey()
396 , event.GetUnicodeKey()
397 #endif
398 #ifdef wxHAS_RAW_KEY_CODES
399 , (unsigned long) event.GetRawKeyCode()
400 , (unsigned long) event.GetRawKeyFlags()
401 #endif
402 );
403
404 m_logText->AppendText(msg);
405
406 if ( m_skip )
407 event.Skip();
408 }
409
410