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