]> git.saurik.com Git - wxWidgets.git/blob - samples/keyboard/keyboard.cpp
use <bitmapsize> to load bitmaps at the specified size in wxToolBar's XRC handler
[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 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);
41
42 void LogEvent(const wxString& name, wxKeyEvent& event);
43
44 wxTextCtrl *m_logText;
45 wxWindow *m_inputWin;
46 bool m_skip;
47 };
48
49
50 // Define a new application type, each program should derive a class from wxApp
51 class MyApp : public wxApp
52 {
53 public:
54 // 'Main program' equivalent: the program execution "starts" here
55 virtual bool OnInit()
56 {
57 // create the main application window
58 new MyFrame("Keyboard wxWidgets App");
59
60 // If we returned false here, the application would exit immediately.
61 return true;
62 }
63 };
64
65 // Create a new application object: this macro will allow wxWidgets to create
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)
70 IMPLEMENT_APP(MyApp)
71
72
73 // ============================================================================
74 // implementation
75 // ============================================================================
76
77 // frame constructor
78 MyFrame::MyFrame(const wxString& title)
79 : wxFrame(NULL, wxID_ANY, title),
80 m_inputWin(NULL),
81 m_skip(true)
82 {
83 SetIcon(wxICON(sample));
84
85 // IDs for menu items
86 enum
87 {
88 QuitID = wxID_EXIT,
89 ClearID = wxID_CLEAR,
90 SkipID = 100,
91 };
92
93 // create a menu bar
94 wxMenu *menuFile = new wxMenu;
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);
101 menuFile->AppendSeparator();
102
103 menuFile->Append(QuitID, "E&xit\tAlt-X", "Quit this program");
104
105 // the "About" item should be in the help menu
106 wxMenu *menuHelp = new wxMenu;
107 menuHelp->Append(wxID_ABOUT, "&About...\tF1", "Show about dialog");
108
109 // now append the freshly created menu to the menu bar...
110 wxMenuBar *menuBar = new wxMenuBar();
111 menuBar->Append(menuFile, "&File");
112 menuBar->Append(menuHelp, "&Help");
113
114 // ... and attach this menu bar to the frame
115 SetMenuBar(menuBar);
116
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
129 m_logText = new wxTextCtrl(this, wxID_ANY, "",
130 wxDefaultPosition, wxDefaultSize,
131 wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL);
132
133 // set monospace font to have output in nice columns
134 wxFont font(10, wxFONTFAMILY_TELETYPE,
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
164 // connect event handlers for the blue input window
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);
171 m_inputWin->Connect(wxEVT_PAINT,
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);
178 }
179
180 // event handlers
181
182 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
183 {
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);
189 }
190
191 void MyFrame::OnPaintInputWin(wxPaintEvent& WXUNUSED(event))
192 {
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
200 dc.DrawLabel("Press keys here",
201 m_inputWin->GetClientRect(), wxALIGN_CENTER);
202 }
203
204
205 // helper function that returns textual description of wx virtual keycode
206 const char* GetVirtualKeyCodeName(int keycode)
207 {
208 switch ( keycode )
209 {
210 #define WXK_(x) \
211 case WXK_##x: return #x;
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_
313 default:
314 return NULL;
315 }
316 }
317
318 // helper function that returns textual description of key in the event
319 wxString GetKeyName(const wxKeyEvent &event)
320 {
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);
329 #if wxUSE_UNICODE
330 return wxString::Format("'%c'", event.GetUnicodeKey());
331 #else
332 return "unknown";
333 #endif
334 }
335
336
337 void MyFrame::LogEvent(const wxString& name, wxKeyEvent& event)
338 {
339 wxString msg;
340 // event key_name KeyCode modifiers Unicode raw_code raw_flags
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",
353 name,
354 GetKeyName(event),
355 event.GetKeyCode(),
356 event.ControlDown() ? 'C' : '-',
357 event.AltDown() ? 'A' : '-',
358 event.ShiftDown() ? 'S' : '-',
359 event.MetaDown() ? 'M' : '-'
360 #if wxUSE_UNICODE
361 , event.GetUnicodeKey()
362 , event.GetUnicodeKey()
363 #endif
364 #ifdef wxHAS_RAW_KEY_CODES
365 , (unsigned long) event.GetRawKeyCode()
366 , (unsigned long) event.GetRawKeyFlags()
367 #endif
368 );
369
370 m_logText->AppendText(msg);
371
372 if ( m_skip )
373 event.Skip();
374 }
375
376