]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/keyboard/keyboard.cpp
correcting text alignment flag
[wxWidgets.git] / samples / keyboard / keyboard.cpp
index 9a98028f6edacc28a0ff85039440351eaefe62ce..d90b035fdfdf49441d01cb2f8cf5b0de1f080166 100644 (file)
@@ -18,7 +18,7 @@
     #include "wx/wx.h"
 #endif
 
     #include "wx/wx.h"
 #endif
 
-#ifndef __WXMSW__
+#ifndef wxHAS_IMAGES_IN_RESOURCES
     #include "../sample.xpm"
 #endif
 
     #include "../sample.xpm"
 #endif
 
@@ -41,18 +41,32 @@ private:
         { m_logText->AppendText("Test accelerator \"Esc\" used.\n"); }
 
     void OnClear(wxCommandEvent& WXUNUSED(event)) { m_logText->Clear(); }
         { m_logText->AppendText("Test accelerator \"Esc\" used.\n"); }
 
     void OnClear(wxCommandEvent& WXUNUSED(event)) { m_logText->Clear(); }
-    void OnSkip(wxCommandEvent& event) { m_skip = event.IsChecked(); }
+    void OnSkipDown(wxCommandEvent& event) { m_skipDown = event.IsChecked(); }
+    void OnSkipHook(wxCommandEvent& event) { m_skipHook = event.IsChecked(); }
 
 
-    void OnKeyDown(wxKeyEvent& event) { LogEvent("KeyDown", event); }
+    void OnKeyDown(wxKeyEvent& event)
+    {
+        LogEvent("KeyDown", event);
+        if ( m_skipDown )
+            event.Skip();
+    }
     void OnKeyUp(wxKeyEvent& event) { LogEvent("KeyUp", event); }
     void OnChar(wxKeyEvent& event) { LogEvent("Char", event); }
     void OnKeyUp(wxKeyEvent& event) { LogEvent("KeyUp", event); }
     void OnChar(wxKeyEvent& event) { LogEvent("Char", event); }
+    void OnCharHook(wxKeyEvent& event)
+    {
+        LogEvent("Hook", event);
+        if ( m_skipHook )
+            event.Skip();
+    }
+
     void OnPaintInputWin(wxPaintEvent& event);
 
     void LogEvent(const wxString& name, wxKeyEvent& event);
 
     wxTextCtrl *m_logText;
     wxWindow *m_inputWin;
     void OnPaintInputWin(wxPaintEvent& event);
 
     void LogEvent(const wxString& name, wxKeyEvent& event);
 
     wxTextCtrl *m_logText;
     wxWindow *m_inputWin;
-    bool m_skip;
+    bool m_skipHook,
+         m_skipDown;
 };
 
 
 };
 
 
@@ -87,7 +101,8 @@ IMPLEMENT_APP(MyApp)
 MyFrame::MyFrame(const wxString& title)
        : wxFrame(NULL, wxID_ANY, title),
          m_inputWin(NULL),
 MyFrame::MyFrame(const wxString& title)
        : wxFrame(NULL, wxID_ANY, title),
          m_inputWin(NULL),
-         m_skip(true)
+         m_skipHook(true),
+         m_skipDown(true)
 {
     SetIcon(wxICON(sample));
 
 {
     SetIcon(wxICON(sample));
 
@@ -96,7 +111,8 @@ MyFrame::MyFrame(const wxString& title)
     {
         QuitID = wxID_EXIT,
         ClearID = wxID_CLEAR,
     {
         QuitID = wxID_EXIT,
         ClearID = wxID_CLEAR,
-        SkipID = 100,
+        SkipHook = 100,
+        SkipDown,
         TestAccelA,
         TestAccelCtrlA,
         TestAccelEsc
         TestAccelA,
         TestAccelCtrlA,
         TestAccelEsc
@@ -113,15 +129,21 @@ MyFrame::MyFrame(const wxString& title)
     menuFile->Append(TestAccelEsc, "Test accelerator &3\tEsc");
     menuFile->AppendSeparator();
 
     menuFile->Append(TestAccelEsc, "Test accelerator &3\tEsc");
     menuFile->AppendSeparator();
 
-    menuFile->AppendCheckItem(SkipID, "Call event.&Skip()\tCtrl-S");
-    menuFile->Check(SkipID, true);
+    menuFile->AppendCheckItem(SkipHook, "Skip CHAR_HOOK event",
+        "Not skipping this event disables both KEY_DOWN and CHAR events"
+    );
+    menuFile->Check(SkipHook, true);
+    menuFile->AppendCheckItem(SkipDown, "Skip KEY_DOWN event",
+        "Not skipping this event disables CHAR event generation"
+    );
+    menuFile->Check(SkipDown, true);
     menuFile->AppendSeparator();
 
     menuFile->Append(QuitID, "E&xit\tAlt-X", "Quit this program");
 
     // the "About" item should be in the help menu
     wxMenu *menuHelp = new wxMenu;
     menuFile->AppendSeparator();
 
     menuFile->Append(QuitID, "E&xit\tAlt-X", "Quit this program");
 
     // the "About" item should be in the help menu
     wxMenu *menuHelp = new wxMenu;
-    menuHelp->Append(wxID_ABOUT, "&About...\tF1", "Show about dialog");
+    menuHelp->Append(wxID_ABOUT, "&About\tF1", "Show about dialog");
 
     // now append the freshly created menu to the menu bar...
     wxMenuBar *menuBar = new wxMenuBar();
 
     // now append the freshly created menu to the menu bar...
     wxMenuBar *menuBar = new wxMenuBar();
@@ -132,7 +154,7 @@ MyFrame::MyFrame(const wxString& title)
     SetMenuBar(menuBar);
 
     m_inputWin = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxSize(-1, 50),
     SetMenuBar(menuBar);
 
     m_inputWin = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxSize(-1, 50),
-                              wxRAISED_BORDER),
+                              wxRAISED_BORDER);
     m_inputWin->SetBackgroundColour(*wxBLUE);
 
     wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "",
     m_inputWin->SetBackgroundColour(*wxBLUE);
 
     wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "",
@@ -175,8 +197,10 @@ MyFrame::MyFrame(const wxString& title)
     Connect(ClearID, wxEVT_COMMAND_MENU_SELECTED,
             wxCommandEventHandler(MyFrame::OnClear));
 
     Connect(ClearID, wxEVT_COMMAND_MENU_SELECTED,
             wxCommandEventHandler(MyFrame::OnClear));
 
-    Connect(SkipID, wxEVT_COMMAND_MENU_SELECTED,
-            wxCommandEventHandler(MyFrame::OnSkip));
+    Connect(SkipHook, wxEVT_COMMAND_MENU_SELECTED,
+            wxCommandEventHandler(MyFrame::OnSkipHook));
+    Connect(SkipDown, wxEVT_COMMAND_MENU_SELECTED,
+            wxCommandEventHandler(MyFrame::OnSkipDown));
 
     Connect(TestAccelA, wxEVT_COMMAND_MENU_SELECTED,
             wxCommandEventHandler(MyFrame::OnTestAccelA));
 
     Connect(TestAccelA, wxEVT_COMMAND_MENU_SELECTED,
             wxCommandEventHandler(MyFrame::OnTestAccelA));
@@ -198,6 +222,13 @@ MyFrame::MyFrame(const wxString& title)
                         wxPaintEventHandler(MyFrame::OnPaintInputWin),
                         NULL, this);
 
                         wxPaintEventHandler(MyFrame::OnPaintInputWin),
                         NULL, this);
 
+    // notice that we don't connect OnCharHook() to the input window, unlike
+    // the usual key events this one is propagated upwards
+    Connect(wxEVT_CHAR_HOOK, wxKeyEventHandler(MyFrame::OnCharHook));
+
+    // status bar is useful for showing the menu items help strings
+    CreateStatusBar();
+
     // and show itself (the frames, unlike simple controls, are not shown when
     // created initially)
     Show(true);
     // and show itself (the frames, unlike simple controls, are not shown when
     // created initially)
     Show(true);
@@ -335,7 +366,14 @@ const char* GetVirtualKeyCodeName(int keycode)
         WXK_(NUMPAD_SUBTRACT)
         WXK_(NUMPAD_DECIMAL)
         WXK_(NUMPAD_DIVIDE)
         WXK_(NUMPAD_SUBTRACT)
         WXK_(NUMPAD_DECIMAL)
         WXK_(NUMPAD_DIVIDE)
+
+        WXK_(WINDOWS_LEFT)
+        WXK_(WINDOWS_RIGHT)
+#ifdef __WXOSX__
+        WXK_(RAW_CONTROL)
+#endif
 #undef WXK_
 #undef WXK_
+
     default:
         return NULL;
     }
     default:
         return NULL;
     }
@@ -348,15 +386,18 @@ wxString GetKeyName(const wxKeyEvent &event)
     const char* virt = GetVirtualKeyCodeName(keycode);
     if ( virt )
         return virt;
     const char* virt = GetVirtualKeyCodeName(keycode);
     if ( virt )
         return virt;
-    if ( keycode > 0 && keycode < 27 )
+    if ( keycode > 0 && keycode < 32 )
         return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode - 1));
         return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode - 1));
-    if ( keycode >= 27 && keycode < 128 )
+    if ( keycode >= 32 && keycode < 128 )
         return wxString::Format("'%c'", (unsigned char)keycode);
         return wxString::Format("'%c'", (unsigned char)keycode);
+
 #if wxUSE_UNICODE
 #if wxUSE_UNICODE
-    return wxString::Format("'%c'", event.GetUnicodeKey());
-#else
-    return "unknown";
+    int uc = event.GetUnicodeKey();
+    if ( uc != WXK_NONE )
+        return wxString::Format("'%c'", uc);
 #endif
 #endif
+
+    return "unknown";
 }
 
 
 }
 
 
@@ -371,7 +412,7 @@ void MyFrame::LogEvent(const wxString& name, wxKeyEvent& event)
                    "    none   "
 #endif
 #ifdef wxHAS_RAW_KEY_CODES
                    "    none   "
 #endif
 #ifdef wxHAS_RAW_KEY_CODES
-                   "  %7lu    0x%lx"
+                   "  %7lu    0x%08lx"
 #else
                    "  not-set    not-set"
 #endif
 #else
                    "  not-set    not-set"
 #endif
@@ -394,9 +435,6 @@ void MyFrame::LogEvent(const wxString& name, wxKeyEvent& event)
                );
 
     m_logText->AppendText(msg);
                );
 
     m_logText->AppendText(msg);
-
-    if ( m_skip )
-        event.Skip();
 }
 
 
 }