diff --git a/samples/text/text.cpp b/samples/text/text.cpp index 5647bcb927..ce6f36985a 100644 --- a/samples/text/text.cpp +++ b/samples/text/text.cpp @@ -116,6 +116,14 @@ public: void DoMoveToEndOfText(); void DoMoveToEndOfEntry(); + // return true if currently text control has any selection + bool HasSelection() const + { + long from, to; + GetFocusedText()->GetSelection(&from, &to); + return from != to; + } + MyTextCtrl *m_text; MyTextCtrl *m_password; MyTextCtrl *m_enter; @@ -134,9 +142,10 @@ public: #endif // wxUSE_LOG private: - // get the currently focused text control or return the default one is no - // text ctrl has focus - wxTextCtrl *GetFocusedText(wxTextCtrl *textDef); + // get the currently focused text control or return the default one + // (m_multitext) is no text ctrl has focus -- in any case, returns + // something non NULL + wxTextCtrl *GetFocusedText() const; }; class MyFrame: public wxFrame @@ -153,9 +162,27 @@ public: #if wxUSE_CLIPBOARD void OnPasteFromClipboard( wxCommandEvent& WXUNUSED(event) ) - { m_panel->DoPasteFromClipboard(); } + { + wxLogMessage(_T("Pasting text from clipboard.")); + m_panel->DoPasteFromClipboard(); + } void OnCopyToClipboard( wxCommandEvent& WXUNUSED(event) ) - { m_panel->DoCopyToClipboard(); } + { + wxLogMessage(_T("Copying text to clipboard.")); + m_panel->DoCopyToClipboard(); + } + + void OnUpdatePasteFromClipboard(wxUpdateUIEvent& event) + { + wxClipboardLocker lockClip; + + event.Enable( wxTheClipboard->IsSupported(wxDF_TEXT) ); + } + + void OnUpdateCopyToClipboard(wxUpdateUIEvent& event) + { + event.Enable( m_panel->HasSelection() ); + } #endif // wxUSE_CLIPBOARD void OnAddTextFreeze( wxCommandEvent& WXUNUSED(event) ) @@ -406,10 +433,12 @@ bool MyApp::OnInit() #endif // wxUSE_TOOLTIPS #if wxUSE_CLIPBOARD + // notice that we use non default accelerators on purpose here to compare + // their behaviour with the built in handling of standard Ctrl/Cmd-C/V wxMenu *menuClipboard = new wxMenu; - menuClipboard->Append(TEXT_CLIPBOARD_COPY, _T("&Copy\tCtrl-C"), - _T("Copy the first line to the clipboard")); - menuClipboard->Append(TEXT_CLIPBOARD_PASTE, _T("&Paste\tCtrl-V"), + menuClipboard->Append(TEXT_CLIPBOARD_COPY, _T("&Copy\tCtrl-Shift-C"), + _T("Copy the selection to the clipboard")); + menuClipboard->Append(TEXT_CLIPBOARD_PASTE, _T("&Paste\tCtrl-Shift-V"), _T("Paste from clipboard to the text control")); menu_bar->Append(menuClipboard, _T("&Clipboard")); #endif // wxUSE_CLIPBOARD @@ -1026,12 +1055,12 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) SetSizer(topSizer); } -wxTextCtrl *MyPanel::GetFocusedText(wxTextCtrl *textDef) +wxTextCtrl *MyPanel::GetFocusedText() const { wxWindow *win = FindFocus(); wxTextCtrl *text = win ? wxDynamicCast(win, wxTextCtrl) : NULL; - return text ? text : textDef; + return text ? text : m_multitext; } #if wxUSE_CLIPBOARD @@ -1069,7 +1098,7 @@ void MyPanel::DoPasteFromClipboard() #if wxUSE_LOG *m_log << _T("Successfully retrieved data from the clipboard.\n"); #endif // wxUSE_LOG - *m_multitext << data.GetText() << _T("\n"); + GetFocusedText()->AppendText(data.GetText()); } else { @@ -1099,7 +1128,7 @@ void MyPanel::DoCopyToClipboard() // call has no effect under MSW. wxTheClipboard->UsePrimarySelection(); - wxString text( m_multitext->GetLineText(0) ); + wxString text( GetFocusedText()->GetStringSelection() ); if (text.IsEmpty()) { @@ -1163,17 +1192,17 @@ void MyPanel::DoMoveToEndOfEntry() void MyPanel::DoRemoveText() { - GetFocusedText(m_multitext)->Remove(0, 10); + GetFocusedText()->Remove(0, 10); } void MyPanel::DoReplaceText() { - GetFocusedText(m_multitext)->Replace(3, 8, _T("ABC")); + GetFocusedText()->Replace(3, 8, _T("ABC")); } void MyPanel::DoSelectText() { - GetFocusedText(m_multitext)->SetSelection(3, 8); + GetFocusedText()->SetSelection(3, 8); } //---------------------------------------------------------------------- @@ -1204,6 +1233,9 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) #if wxUSE_CLIPBOARD EVT_MENU(TEXT_CLIPBOARD_PASTE, MyFrame::OnPasteFromClipboard) EVT_MENU(TEXT_CLIPBOARD_COPY, MyFrame::OnCopyToClipboard) + + EVT_UPDATE_UI(TEXT_CLIPBOARD_PASTE, MyFrame::OnUpdatePasteFromClipboard) + EVT_UPDATE_UI(TEXT_CLIPBOARD_COPY, MyFrame::OnUpdateCopyToClipboard) #endif // wxUSE_CLIPBOARD EVT_MENU(TEXT_REMOVE, MyFrame::OnRemoveText)