]> git.saurik.com Git - wxWidgets.git/blame - samples/keyboard/keyboard.cpp
latex include not properly working for links and titlepage
[wxWidgets.git] / samples / keyboard / keyboard.cpp
CommitLineData
7de7aed4
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: keyboard.cpp
be5a51fb 3// Purpose: Keyboard wxWidgets sample
7de7aed4
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 07.04.02
7// RCS-ID: $Id$
8// Copyright: (c) 2002 Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27// for all others, include the necessary headers (this file is usually all you
be5a51fb 28// need because it includes almost all "standard" wxWidgets headers)
7de7aed4
VZ
29#ifndef WX_PRECOMP
30 #include "wx/wx.h"
31#endif
32
b29903d4
WS
33#if !wxUSE_LOG
34# error You must set wxUSE_LOG to 1 in setup.h
35#endif
36
7de7aed4
VZ
37// ----------------------------------------------------------------------------
38// private classes
39// ----------------------------------------------------------------------------
40
41// Define a new application type, each program should derive a class from wxApp
42class MyApp : public wxApp
43{
44public:
45 // override base class virtuals
46 // ----------------------------
47
48 // this one is called on application startup and is a good place for the app
49 // initialization (doing it here and not in the ctor allows to have an error
50 // return: if OnInit() returns false, the application terminates)
51 virtual bool OnInit();
52};
53
54// Define a new frame type: this is going to be our main frame
55class MyFrame : public wxFrame
56{
57public:
58 // ctor(s)
59 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
60 long style = wxDEFAULT_FRAME_STYLE);
61
62 ~MyFrame() { delete m_logTarget; }
63
64 // event handlers (these functions should _not_ be virtual)
65 void OnQuit(wxCommandEvent& event);
66 void OnAbout(wxCommandEvent& event);
67 void OnClear(wxCommandEvent& event);
68 void OnSkip(wxCommandEvent& event);
62fa9712 69 void OnShowRaw(wxCommandEvent& event);
7de7aed4
VZ
70
71 void OnSize(wxSizeEvent& event);
72
73private:
74 wxLog *m_logTarget;
75
76 class TextWindow *m_winText;
77 wxListBox *m_lboxLog;
78
be5a51fb 79 // any class wishing to process wxWidgets events must use this macro
7de7aed4
VZ
80 DECLARE_EVENT_TABLE()
81};
82
83// A log target which just redirects the messages to a listbox
84class LboxLogger : public wxLog
85{
86public:
87 LboxLogger(wxListBox *lbox, wxLog *logOld)
88 {
89 m_lbox = lbox;
90 //m_lbox->Disable(); -- looks ugly under MSW
91 m_logOld = logOld;
92 }
93
94 virtual ~LboxLogger()
95 {
96 wxLog::SetActiveTarget(m_logOld);
97 }
98
99private:
100 // implement sink functions
6e69dd00 101 virtual void DoLog(wxLogLevel level, const wxString& szString, time_t t)
7de7aed4
VZ
102 {
103 // don't put trace messages into listbox or we can get into infinite
104 // recursion
105 if ( level == wxLOG_Trace )
106 {
107 if ( m_logOld )
108 {
109 // cast is needed to call protected method
110 ((LboxLogger *)m_logOld)->DoLog(level, szString, t);
111 }
112 }
113 else
114 {
115 wxLog::DoLog(level, szString, t);
116 }
117 }
118
d1f47235 119 virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t))
7de7aed4
VZ
120 {
121 wxString msg;
122 TimeStamp(&msg);
123 msg += szString;
124
125 #ifdef __WXUNIVERSAL__
126 m_lbox->AppendAndEnsureVisible(msg);
127 #else // other ports don't have this method yet
128 m_lbox->Append(msg);
129 m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
130 #endif
131 }
132
133 // the control we use
134 wxListBox *m_lbox;
135
136 // the old log target
137 wxLog *m_logOld;
138};
139
140class TextWindow : public wxWindow
141{
142public:
143 TextWindow(wxWindow *parent)
ee1a622d 144 : wxWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
7de7aed4
VZ
145 wxRAISED_BORDER)
146 {
ee1a622d
WS
147 m_skip = true;
148 m_showRaw = false;
7de7aed4
VZ
149
150 SetBackgroundColour(*wxBLUE);
151 }
152
153 void SetSkip(bool skip) { m_skip = skip; }
62fa9712 154 void SetShowRaw(bool show) { m_showRaw = show; }
7de7aed4
VZ
155
156protected:
157 void OnKeyDown(wxKeyEvent& event) { LogEvent(_T("Key down"), event); }
158 void OnKeyUp(wxKeyEvent& event) { LogEvent(_T("Key up"), event); }
159 void OnChar(wxKeyEvent& event) { LogEvent(_T("Char"), event); }
160
d1f47235 161 void OnPaint(wxPaintEvent& WXUNUSED(event))
7de7aed4
VZ
162 {
163 wxPaintDC dc(this);
164 dc.SetTextForeground(*wxWHITE);
4849eced
VZ
165 wxFont font(*wxSWISS_FONT);
166 font.SetWeight(wxFONTWEIGHT_BOLD);
167 font.SetPointSize(font.GetPointSize() + 2);
168 dc.SetFont(font);
169
7de7aed4
VZ
170 dc.DrawLabel(_T("Press keys here"), GetClientRect(), wxALIGN_CENTER);
171 }
172
173private:
174 static inline wxChar GetChar(bool on, wxChar c) { return on ? c : _T('-'); }
175
176 void LogEvent(const wxChar *name, wxKeyEvent& event);
177
178 bool m_skip;
62fa9712 179 bool m_showRaw;
7de7aed4
VZ
180
181 DECLARE_EVENT_TABLE()
182};
183
184BEGIN_EVENT_TABLE(TextWindow, wxWindow)
185 EVT_KEY_DOWN(TextWindow::OnKeyDown)
186 EVT_KEY_UP(TextWindow::OnKeyUp)
187 EVT_CHAR(TextWindow::OnChar)
188
189 EVT_PAINT(TextWindow::OnPaint)
190END_EVENT_TABLE()
191
192// ----------------------------------------------------------------------------
193// constants
194// ----------------------------------------------------------------------------
195
196// IDs for the controls and the menu commands
197enum
198{
199 // menu items
200 Keyboard_Quit = 1,
201
202 Keyboard_Clear,
203 Keyboard_Skip,
62fa9712 204 Keyboard_ShowRaw,
7de7aed4
VZ
205
206 // it is important for the id corresponding to the "About" command to have
207 // this standard value as otherwise it won't be handled properly under Mac
208 // (where it is special and put into the "Apple" menu)
209 Keyboard_About = wxID_ABOUT
210};
211
212// ----------------------------------------------------------------------------
be5a51fb 213// event tables and other macros for wxWidgets
7de7aed4
VZ
214// ----------------------------------------------------------------------------
215
be5a51fb 216// the event tables connect the wxWidgets events with the functions (event
7de7aed4
VZ
217// handlers) which process them. It can be also done at run-time, but for the
218// simple menu events like this the static method is much simpler.
219BEGIN_EVENT_TABLE(MyFrame, wxFrame)
220 EVT_MENU(Keyboard_Quit, MyFrame::OnQuit)
221 EVT_MENU(Keyboard_About, MyFrame::OnAbout)
222
223 EVT_MENU(Keyboard_Clear, MyFrame::OnClear)
224 EVT_MENU(Keyboard_Skip, MyFrame::OnSkip)
62fa9712 225 EVT_MENU(Keyboard_ShowRaw, MyFrame::OnShowRaw)
7de7aed4
VZ
226
227 EVT_SIZE(MyFrame::OnSize)
228END_EVENT_TABLE()
229
be5a51fb 230// Create a new application object: this macro will allow wxWidgets to create
7de7aed4
VZ
231// the application object during program execution (it's better than using a
232// static object for many reasons) and also declares the accessor function
233// wxGetApp() which will return the reference of the right type (i.e. MyApp and
234// not wxApp)
235IMPLEMENT_APP(MyApp)
236
237// ============================================================================
238// implementation
239// ============================================================================
240
241// ----------------------------------------------------------------------------
242// the application class
243// ----------------------------------------------------------------------------
244
245// 'Main program' equivalent: the program execution "starts" here
246bool MyApp::OnInit()
247{
45e6e6f8
VZ
248 if ( !wxApp::OnInit() )
249 return false;
250
7de7aed4 251 // create the main application window
be5a51fb 252 MyFrame *frame = new MyFrame(_T("Keyboard wxWidgets App"),
7de7aed4
VZ
253 wxPoint(50, 50), wxSize(450, 340));
254
255 // and show it (the frames, unlike simple controls, are not shown when
256 // created initially)
ee1a622d 257 frame->Show(true);
7de7aed4
VZ
258
259 // success: wxApp::OnRun() will be called which will enter the main message
ee1a622d 260 // loop and the application will run. If we returned false here, the
7de7aed4 261 // application would exit immediately.
ee1a622d 262 return true;
7de7aed4
VZ
263}
264
265// ----------------------------------------------------------------------------
266// main frame
267// ----------------------------------------------------------------------------
268
269// frame constructor
270MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
ee1a622d 271 : wxFrame(NULL, wxID_ANY, title, pos, size, style),
7de7aed4
VZ
272 m_winText(NULL)
273{
274#if wxUSE_MENUS
275 // create a menu bar
276 wxMenu *menuFile = new wxMenu;
277 menuFile->Append(Keyboard_Clear, _T("&Clear log\tCtrl-L"));
278 menuFile->AppendSeparator();
279 menuFile->Append(Keyboard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
280
281 wxMenu *menuKeys = new wxMenu;
62fa9712
VZ
282 menuKeys->AppendCheckItem(Keyboard_ShowRaw, _T("Show &raw keys\tCtrl-R"));
283 menuKeys->AppendSeparator();
7de7aed4
VZ
284 menuKeys->AppendCheckItem(Keyboard_Skip, _T("&Skip key down\tCtrl-S"));
285
286 // the "About" item should be in the help menu
287 wxMenu *menuHelp = new wxMenu;
288 menuHelp->Append(Keyboard_About, _T("&About...\tF1"), _T("Show about dialog"));
289
290 // now append the freshly created menu to the menu bar...
291 wxMenuBar *menuBar = new wxMenuBar();
292 menuBar->Append(menuFile, _T("&File"));
293 menuBar->Append(menuKeys, _T("&Keys"));
294 menuBar->Append(menuHelp, _T("&Help"));
295
296 // ... and attach this menu bar to the frame
297 SetMenuBar(menuBar);
298
ee1a622d 299 menuBar->Check(Keyboard_Skip, true);
62fa9712
VZ
300
301#ifndef wxHAS_RAW_KEY_CODES
ee1a622d 302 menuBar->Enable(Keyboard_ShowRaw, false);
62fa9712 303#endif // !wxHAS_RAW_KEY_CODES
7de7aed4
VZ
304#endif // wxUSE_MENUS
305
306 m_winText = new TextWindow(this);
ee1a622d 307 m_lboxLog = new wxListBox(this, wxID_ANY);
7de7aed4
VZ
308
309 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
310 wxLog::SetActiveTarget(m_logTarget);
311
312#if wxUSE_STATUSBAR
313 // create a status bar just for fun (by default with 1 pane only)
314 CreateStatusBar(2);
be5a51fb 315 SetStatusText(_T("Welcome to wxWidgets!"));
7de7aed4
VZ
316#endif // wxUSE_STATUSBAR
317}
318
319// event handlers
320
321void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
322{
ee1a622d
WS
323 // true is to force the frame to close
324 Close(true);
7de7aed4
VZ
325}
326
327void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
328{
be5a51fb 329 wxString msg = _T("Demonstrates keyboard event processing in wxWidgets\n")
749bfe9a 330 _T("(c) 2002 Vadim Zeitlin");
7de7aed4 331
62fa9712 332 wxMessageBox(msg, _T("About wxKeyboard"), wxOK | wxICON_INFORMATION, this);
7de7aed4
VZ
333}
334
87728739 335void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
7de7aed4
VZ
336{
337 m_lboxLog->Clear();
338}
339
340void MyFrame::OnSkip(wxCommandEvent& event)
341{
342 m_winText->SetSkip(event.IsChecked());
343}
344
62fa9712
VZ
345void MyFrame::OnShowRaw(wxCommandEvent& event)
346{
347 m_winText->SetShowRaw(event.IsChecked());
348}
349
7de7aed4
VZ
350void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
351{
352 if ( m_winText )
353 {
354 wxSize size = GetClientSize();
355 m_winText->SetSize(0, 0, size.x, 50);
356 m_lboxLog->SetSize(0, 51, size.x, size.y - 50);
357 }
358}
359
360// ----------------------------------------------------------------------------
361// TextWindow
362// ----------------------------------------------------------------------------
363
364void TextWindow::LogEvent(const wxChar *name, wxKeyEvent& event)
365{
366 wxString key;
dacaa6f1 367 long keycode = event.GetKeyCode();
59db5113 368 switch ( keycode )
7de7aed4 369 {
59db5113
VZ
370 case WXK_BACK: key = _T("BACK"); break;
371 case WXK_TAB: key = _T("TAB"); break;
372 case WXK_RETURN: key = _T("RETURN"); break;
373 case WXK_ESCAPE: key = _T("ESCAPE"); break;
374 case WXK_SPACE: key = _T("SPACE"); break;
375 case WXK_DELETE: key = _T("DELETE"); break;
376 case WXK_START: key = _T("START"); break;
377 case WXK_LBUTTON: key = _T("LBUTTON"); break;
378 case WXK_RBUTTON: key = _T("RBUTTON"); break;
379 case WXK_CANCEL: key = _T("CANCEL"); break;
380 case WXK_MBUTTON: key = _T("MBUTTON"); break;
381 case WXK_CLEAR: key = _T("CLEAR"); break;
382 case WXK_SHIFT: key = _T("SHIFT"); break;
383 case WXK_ALT: key = _T("ALT"); break;
384 case WXK_CONTROL: key = _T("CONTROL"); break;
385 case WXK_MENU: key = _T("MENU"); break;
386 case WXK_PAUSE: key = _T("PAUSE"); break;
387 case WXK_CAPITAL: key = _T("CAPITAL"); break;
59db5113
VZ
388 case WXK_END: key = _T("END"); break;
389 case WXK_HOME: key = _T("HOME"); break;
390 case WXK_LEFT: key = _T("LEFT"); break;
391 case WXK_UP: key = _T("UP"); break;
392 case WXK_RIGHT: key = _T("RIGHT"); break;
393 case WXK_DOWN: key = _T("DOWN"); break;
394 case WXK_SELECT: key = _T("SELECT"); break;
395 case WXK_PRINT: key = _T("PRINT"); break;
396 case WXK_EXECUTE: key = _T("EXECUTE"); break;
397 case WXK_SNAPSHOT: key = _T("SNAPSHOT"); break;
398 case WXK_INSERT: key = _T("INSERT"); break;
399 case WXK_HELP: key = _T("HELP"); break;
400 case WXK_NUMPAD0: key = _T("NUMPAD0"); break;
401 case WXK_NUMPAD1: key = _T("NUMPAD1"); break;
402 case WXK_NUMPAD2: key = _T("NUMPAD2"); break;
403 case WXK_NUMPAD3: key = _T("NUMPAD3"); break;
404 case WXK_NUMPAD4: key = _T("NUMPAD4"); break;
405 case WXK_NUMPAD5: key = _T("NUMPAD5"); break;
406 case WXK_NUMPAD6: key = _T("NUMPAD6"); break;
407 case WXK_NUMPAD7: key = _T("NUMPAD7"); break;
408 case WXK_NUMPAD8: key = _T("NUMPAD8"); break;
409 case WXK_NUMPAD9: key = _T("NUMPAD9"); break;
410 case WXK_MULTIPLY: key = _T("MULTIPLY"); break;
411 case WXK_ADD: key = _T("ADD"); break;
412 case WXK_SEPARATOR: key = _T("SEPARATOR"); break;
413 case WXK_SUBTRACT: key = _T("SUBTRACT"); break;
414 case WXK_DECIMAL: key = _T("DECIMAL"); break;
415 case WXK_DIVIDE: key = _T("DIVIDE"); break;
416 case WXK_F1: key = _T("F1"); break;
417 case WXK_F2: key = _T("F2"); break;
418 case WXK_F3: key = _T("F3"); break;
419 case WXK_F4: key = _T("F4"); break;
420 case WXK_F5: key = _T("F5"); break;
421 case WXK_F6: key = _T("F6"); break;
422 case WXK_F7: key = _T("F7"); break;
423 case WXK_F8: key = _T("F8"); break;
424 case WXK_F9: key = _T("F9"); break;
425 case WXK_F10: key = _T("F10"); break;
426 case WXK_F11: key = _T("F11"); break;
427 case WXK_F12: key = _T("F12"); break;
428 case WXK_F13: key = _T("F13"); break;
429 case WXK_F14: key = _T("F14"); break;
430 case WXK_F15: key = _T("F15"); break;
431 case WXK_F16: key = _T("F16"); break;
432 case WXK_F17: key = _T("F17"); break;
433 case WXK_F18: key = _T("F18"); break;
434 case WXK_F19: key = _T("F19"); break;
435 case WXK_F20: key = _T("F20"); break;
436 case WXK_F21: key = _T("F21"); break;
437 case WXK_F22: key = _T("F22"); break;
438 case WXK_F23: key = _T("F23"); break;
439 case WXK_F24: key = _T("F24"); break;
440 case WXK_NUMLOCK: key = _T("NUMLOCK"); break;
441 case WXK_SCROLL: key = _T("SCROLL"); break;
442 case WXK_PAGEUP: key = _T("PAGEUP"); break;
443 case WXK_PAGEDOWN: key = _T("PAGEDOWN"); break;
444 case WXK_NUMPAD_SPACE: key = _T("NUMPAD_SPACE"); break;
445 case WXK_NUMPAD_TAB: key = _T("NUMPAD_TAB"); break;
446 case WXK_NUMPAD_ENTER: key = _T("NUMPAD_ENTER"); break;
447 case WXK_NUMPAD_F1: key = _T("NUMPAD_F1"); break;
448 case WXK_NUMPAD_F2: key = _T("NUMPAD_F2"); break;
449 case WXK_NUMPAD_F3: key = _T("NUMPAD_F3"); break;
450 case WXK_NUMPAD_F4: key = _T("NUMPAD_F4"); break;
451 case WXK_NUMPAD_HOME: key = _T("NUMPAD_HOME"); break;
452 case WXK_NUMPAD_LEFT: key = _T("NUMPAD_LEFT"); break;
453 case WXK_NUMPAD_UP: key = _T("NUMPAD_UP"); break;
454 case WXK_NUMPAD_RIGHT: key = _T("NUMPAD_RIGHT"); break;
455 case WXK_NUMPAD_DOWN: key = _T("NUMPAD_DOWN"); break;
59db5113
VZ
456 case WXK_NUMPAD_PAGEUP: key = _T("NUMPAD_PAGEUP"); break;
457 case WXK_NUMPAD_PAGEDOWN: key = _T("NUMPAD_PAGEDOWN"); break;
458 case WXK_NUMPAD_END: key = _T("NUMPAD_END"); break;
459 case WXK_NUMPAD_BEGIN: key = _T("NUMPAD_BEGIN"); break;
460 case WXK_NUMPAD_INSERT: key = _T("NUMPAD_INSERT"); break;
461 case WXK_NUMPAD_DELETE: key = _T("NUMPAD_DELETE"); break;
462 case WXK_NUMPAD_EQUAL: key = _T("NUMPAD_EQUAL"); break;
463 case WXK_NUMPAD_MULTIPLY: key = _T("NUMPAD_MULTIPLY"); break;
464 case WXK_NUMPAD_ADD: key = _T("NUMPAD_ADD"); break;
465 case WXK_NUMPAD_SEPARATOR: key = _T("NUMPAD_SEPARATOR"); break;
466 case WXK_NUMPAD_SUBTRACT: key = _T("NUMPAD_SUBTRACT"); break;
467 case WXK_NUMPAD_DECIMAL: key = _T("NUMPAD_DECIMAL"); break;
468 case WXK_NUMPAD_DIVIDE: key = _T("NUMPAD_DIVIDE"); break;
469
470 default:
7de7aed4 471 {
59db5113 472 if ( keycode < 256 )
7de7aed4 473 {
59db5113
VZ
474 if ( keycode == 0 )
475 key.Printf(_T("NUL"));
476 else if ( keycode < 27 )
2b5f62a0
VZ
477 key.Printf(_T("Ctrl-%c"),
478 (unsigned char)(_T('A') + keycode - 1));
59db5113
VZ
479 else
480 key.Printf(_T("'%c'"), (unsigned char)keycode);
481 }
482 else
483 {
484 key.Printf(_T("unknown (%ld)"), keycode);
7de7aed4
VZ
485 }
486 }
487 }
488
62fa9712
VZ
489 wxString msg;
490 msg.Printf(_T("%s event: %s (flags = %c%c%c%c)"),
491 name,
492 key.c_str(),
493 GetChar(event.ControlDown(), _T('C')),
494 GetChar(event.AltDown(), _T('A')),
495 GetChar(event.ShiftDown(), _T('S')),
496 GetChar(event.MetaDown(), _T('M')));
497
498 if ( m_showRaw )
499 {
500 msg += wxString::Format(_T(" (raw key code/flags: %lu and 0x%lx)"),
2b5f62a0
VZ
501 (unsigned long)event.GetRawKeyCode(),
502 (unsigned long)event.GetRawKeyFlags());
62fa9712
VZ
503 }
504
505 wxLogMessage(msg);
7de7aed4
VZ
506
507 if ( m_skip )
508 {
509 event.Skip();
510 }
511}