X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2f7baaeccf143a4606c463bd50b1bd995fd3d18a..d2ee1e7c75c3f7b2e06e07c66d6d3b5a55b46372:/samples/keyboard/keyboard.cpp diff --git a/samples/keyboard/keyboard.cpp b/samples/keyboard/keyboard.cpp index 7b30978965..7a11cd84ff 100644 --- a/samples/keyboard/keyboard.cpp +++ b/samples/keyboard/keyboard.cpp @@ -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 ///////////////////////////////////////////////////////////////////////////// @@ -22,6 +21,24 @@ #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,9 +70,19 @@ 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 + // have focus so warn about this. + if ( !m_inputWin->HasFocus() ) + { + m_logText->SetDefaultStyle(*wxRED); + m_logText->AppendText("WARNING: focus is not on input window, " + "non-hook events won't be logged.\n"); + m_logText->SetDefaultStyle(wxTextAttr()); + } + LogEvent("Hook", event); if ( m_skipHook ) event.Skip(); @@ -63,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, @@ -106,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; @@ -139,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 @@ -153,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, @@ -167,7 +203,7 @@ MyFrame::MyFrame(const wxString& title) m_logText = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, - wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL); + wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH|wxHSCROLL); // set monospace font to have output in nice columns wxFont font(10, wxFONTFAMILY_TELETYPE, @@ -188,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)); @@ -245,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(event.GetId() - IDInputCustom) + ); +} + void MyFrame::OnPaintInputWin(wxPaintEvent& WXUNUSED(event)) { wxPaintDC dc(m_inputWin);