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