]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/keyboard/keyboard.cpp
Fix wrong in wxListCtrl::SetItemColumnImage() in r74716.
[wxWidgets.git] / samples / keyboard / keyboard.cpp
index f17ffb4b9835d43520b44c3081c2bc843dc862b4..7a11cd84fff89ab069a54c1e6add3f081d45a9d7 100644 (file)
@@ -4,7 +4,6 @@
 // Author:      Vadim Zeitlin
 // Modified by: Marcin Wojdyr
 // Created:     07.04.02
-// RCS-ID:      $Id$
 // Copyright:   (c) 2002 Vadim Zeitlin
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
     #include "../sample.xpm"
 #endif
 
+// IDs for menu items
+enum
+{
+    QuitID = wxID_EXIT,
+    ClearID = wxID_CLEAR,
+    SkipHook = 100,
+    SkipDown,
+
+    // These IDs must be in the same order as MyFrame::InputKind enum elements.
+    IDInputCustom,
+    IDInputEntry,
+    IDInputText,
+
+    TestAccelA,
+    TestAccelCtrlA,
+    TestAccelEsc
+};
+
 // Define a new frame type: this is going to be our main frame
 class MyFrame : public wxFrame
 {
@@ -33,6 +50,8 @@ private:
     void OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); }
     void OnAbout(wxCommandEvent& event);
 
+    void OnInputWindowKind(wxCommandEvent& event);
+
     void OnTestAccelA(wxCommandEvent& WXUNUSED(event))
         { m_logText->AppendText("Test accelerator \"A\" used.\n"); }
     void OnTestAccelCtrlA(wxCommandEvent& WXUNUSED(event))
@@ -51,7 +70,7 @@ private:
             event.Skip();
     }
     void OnKeyUp(wxKeyEvent& event) { LogEvent("KeyUp", event); }
-    void OnChar(wxKeyEvent& event) { LogEvent("Char", event); }
+    void OnChar(wxKeyEvent& event) { LogEvent("Char", event); event.Skip(); }
     void OnCharHook(wxKeyEvent& event)
     {
         // The logged messages can be confusing if the input window doesn't
@@ -73,6 +92,16 @@ private:
 
     void LogEvent(const wxString& name, wxKeyEvent& event);
 
+    // Set m_inputWin to either a new window of the given kind:
+    enum InputKind
+    {
+        Input_Custom,   // Just a plain wxWindow
+        Input_Entry,    // Single-line wxTextCtrl
+        Input_Text      // Multi-line wxTextCtrl
+    };
+
+    void DoCreateInputWindow(InputKind inputKind);
+
     wxTextCtrl *m_logText;
     wxWindow *m_inputWin;
     bool m_skipHook,
@@ -116,18 +145,6 @@ MyFrame::MyFrame(const wxString& title)
 {
     SetIcon(wxICON(sample));
 
-    // IDs for menu items
-    enum
-    {
-        QuitID = wxID_EXIT,
-        ClearID = wxID_CLEAR,
-        SkipHook = 100,
-        SkipDown,
-        TestAccelA,
-        TestAccelCtrlA,
-        TestAccelEsc
-    };
-
     // create a menu bar
     wxMenu *menuFile = new wxMenu;
 
@@ -149,6 +166,17 @@ MyFrame::MyFrame(const wxString& title)
     menuFile->Check(SkipDown, true);
     menuFile->AppendSeparator();
 
+    menuFile->AppendRadioItem(IDInputCustom, "Use &custom control\tCtrl-C",
+        "Use custom wxWindow for input window"
+    );
+    menuFile->AppendRadioItem(IDInputEntry, "Use text &entry\tCtrl-E",
+        "Use single-line wxTextCtrl for input window"
+    );
+    menuFile->AppendRadioItem(IDInputText, "Use &text control\tCtrl-T",
+        "Use multi-line wxTextCtrl for input window"
+    );
+    menuFile->AppendSeparator();
+
     menuFile->Append(QuitID, "E&xit\tAlt-X", "Quit this program");
 
     // the "About" item should be in the help menu
@@ -163,9 +191,7 @@ MyFrame::MyFrame(const wxString& title)
     // ... and attach this menu bar to the frame
     SetMenuBar(menuBar);
 
-    m_inputWin = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxSize(-1, 50),
-                              wxRAISED_BORDER);
-    m_inputWin->SetBackgroundColour(*wxBLUE);
+    DoCreateInputWindow(Input_Custom);
 
     wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "",
                                             wxDefaultPosition, wxDefaultSize,
@@ -198,40 +224,32 @@ MyFrame::MyFrame(const wxString& title)
 
     // connect menu event handlers
 
-    Connect(QuitID, wxEVT_COMMAND_MENU_SELECTED,
+    Connect(QuitID, wxEVT_MENU,
             wxCommandEventHandler(MyFrame::OnQuit));
 
-    Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED,
+    Connect(wxID_ABOUT, wxEVT_MENU,
             wxCommandEventHandler(MyFrame::OnAbout));
 
-    Connect(ClearID, wxEVT_COMMAND_MENU_SELECTED,
+    Connect(ClearID, wxEVT_MENU,
             wxCommandEventHandler(MyFrame::OnClear));
 
-    Connect(SkipHook, wxEVT_COMMAND_MENU_SELECTED,
+    Connect(SkipHook, wxEVT_MENU,
             wxCommandEventHandler(MyFrame::OnSkipHook));
-    Connect(SkipDown, wxEVT_COMMAND_MENU_SELECTED,
+    Connect(SkipDown, wxEVT_MENU,
             wxCommandEventHandler(MyFrame::OnSkipDown));
 
-    Connect(TestAccelA, wxEVT_COMMAND_MENU_SELECTED,
+    Connect(IDInputCustom, IDInputText, wxEVT_MENU,
+            wxCommandEventHandler(MyFrame::OnInputWindowKind));
+
+    Connect(TestAccelA, wxEVT_MENU,
             wxCommandEventHandler(MyFrame::OnTestAccelA));
 
-    Connect(TestAccelCtrlA, wxEVT_COMMAND_MENU_SELECTED,
+    Connect(TestAccelCtrlA, wxEVT_MENU,
             wxCommandEventHandler(MyFrame::OnTestAccelCtrlA));
 
-    Connect(TestAccelEsc, wxEVT_COMMAND_MENU_SELECTED,
+    Connect(TestAccelEsc, wxEVT_MENU,
             wxCommandEventHandler(MyFrame::OnTestAccelEsc));
 
-    // connect event handlers for the blue input window
-    m_inputWin->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MyFrame::OnKeyDown),
-                        NULL, this);
-    m_inputWin->Connect(wxEVT_KEY_UP, wxKeyEventHandler(MyFrame::OnKeyUp),
-                        NULL, this);
-    m_inputWin->Connect(wxEVT_CHAR, wxKeyEventHandler(MyFrame::OnChar),
-                        NULL, this);
-    m_inputWin->Connect(wxEVT_PAINT,
-                        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));
@@ -255,6 +273,62 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
                  wxOK | wxICON_INFORMATION, this);
 }
 
+void MyFrame::DoCreateInputWindow(InputKind inputKind)
+{
+    wxWindow* const oldWin = m_inputWin;
+
+    switch ( inputKind )
+    {
+        case Input_Custom:
+            m_inputWin = new wxWindow(this, wxID_ANY,
+                                      wxDefaultPosition, wxSize(-1, 50),
+                                      wxRAISED_BORDER);
+            break;
+
+        case Input_Entry:
+            m_inputWin = new wxTextCtrl(this, wxID_ANY, "Press keys here");
+            break;
+
+        case Input_Text:
+            m_inputWin = new wxTextCtrl(this, wxID_ANY, "Press keys here",
+                                        wxDefaultPosition, wxSize(-1, 50),
+                                        wxTE_MULTILINE);
+            break;
+    }
+
+    m_inputWin->SetBackgroundColour(*wxBLUE);
+    m_inputWin->SetForegroundColour(*wxWHITE);
+
+    // connect event handlers for the blue input window
+    m_inputWin->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MyFrame::OnKeyDown),
+                        NULL, this);
+    m_inputWin->Connect(wxEVT_KEY_UP, wxKeyEventHandler(MyFrame::OnKeyUp),
+                        NULL, this);
+    m_inputWin->Connect(wxEVT_CHAR, wxKeyEventHandler(MyFrame::OnChar),
+                        NULL, this);
+
+    if ( inputKind == Input_Custom )
+    {
+        m_inputWin->Connect(wxEVT_PAINT,
+                            wxPaintEventHandler(MyFrame::OnPaintInputWin),
+                            NULL, this);
+    }
+
+    if ( oldWin )
+    {
+        GetSizer()->Replace(oldWin, m_inputWin);
+        Layout();
+        delete oldWin;
+    }
+}
+
+void MyFrame::OnInputWindowKind(wxCommandEvent& event)
+{
+    DoCreateInputWindow(
+        static_cast<InputKind>(event.GetId() - IDInputCustom)
+    );
+}
+
 void MyFrame::OnPaintInputWin(wxPaintEvent& WXUNUSED(event))
 {
     wxPaintDC dc(m_inputWin);