X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2f7baaeccf143a4606c463bd50b1bd995fd3d18a..89b3361ec25cf1e6d255b14c7633d8375646e4fb:/samples/keyboard/keyboard.cpp diff --git a/samples/keyboard/keyboard.cpp b/samples/keyboard/keyboard.cpp index 7b30978965..44456fede1 100644 --- a/samples/keyboard/keyboard.cpp +++ b/samples/keyboard/keyboard.cpp @@ -33,6 +33,8 @@ private: void OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); } void OnAbout(wxCommandEvent& event); + void OnUseTextCtrl(wxCommandEvent& event); + void OnTestAccelA(wxCommandEvent& WXUNUSED(event)) { m_logText->AppendText("Test accelerator \"A\" used.\n"); } void OnTestAccelCtrlA(wxCommandEvent& WXUNUSED(event)) @@ -54,6 +56,16 @@ private: void OnChar(wxKeyEvent& event) { LogEvent("Char", event); } 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 +75,9 @@ private: void LogEvent(const wxString& name, wxKeyEvent& event); + // Set m_inputWin to either a new wxWindow or new wxTextCtrl. + void DoCreateInputWindow(bool text); + wxTextCtrl *m_logText; wxWindow *m_inputWin; bool m_skipHook, @@ -113,6 +128,7 @@ MyFrame::MyFrame(const wxString& title) ClearID = wxID_CLEAR, SkipHook = 100, SkipDown, + UseTextCtrl, TestAccelA, TestAccelCtrlA, TestAccelEsc @@ -139,6 +155,11 @@ MyFrame::MyFrame(const wxString& title) menuFile->Check(SkipDown, true); menuFile->AppendSeparator(); + menuFile->AppendCheckItem(UseTextCtrl, "Use &text control\tCtrl-T", + "Use wxTextCtrl or a simple wxWindow 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 +174,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(false /* simple window initially */); wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, @@ -167,7 +186,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, @@ -202,6 +221,9 @@ MyFrame::MyFrame(const wxString& title) Connect(SkipDown, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnSkipDown)); + Connect(UseTextCtrl, wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(MyFrame::OnUseTextCtrl)); + Connect(TestAccelA, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnTestAccelA)); @@ -211,17 +233,6 @@ MyFrame::MyFrame(const wxString& title) Connect(TestAccelEsc, wxEVT_COMMAND_MENU_SELECTED, 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 +256,47 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) wxOK | wxICON_INFORMATION, this); } +void MyFrame::DoCreateInputWindow(bool text) +{ + wxWindow* const oldWin = m_inputWin; + + m_inputWin = text ? new wxTextCtrl(this, wxID_ANY, "Press keys here", + wxDefaultPosition, wxSize(-1, 50), + wxTE_MULTILINE) + : new wxWindow(this, wxID_ANY, + wxDefaultPosition, wxSize(-1, 50), + wxRAISED_BORDER); + 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 ( !text ) + { + m_inputWin->Connect(wxEVT_PAINT, + wxPaintEventHandler(MyFrame::OnPaintInputWin), + NULL, this); + } + + if ( oldWin ) + { + GetSizer()->Replace(oldWin, m_inputWin); + Layout(); + delete oldWin; + } +} + +void MyFrame::OnUseTextCtrl(wxCommandEvent& event) +{ + DoCreateInputWindow(event.IsChecked()); +} + void MyFrame::OnPaintInputWin(wxPaintEvent& WXUNUSED(event)) { wxPaintDC dc(m_inputWin);