X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/11e1c70d8e6aa75e2ecb660fffd61b6cfcb8ffc7..82ea63e6e046652bc0799badd9d62f91c9918dbe:/samples/text/text.cpp diff --git a/samples/text/text.cpp b/samples/text/text.cpp index 4d26e23e96..09e8e6013d 100644 --- a/samples/text/text.cpp +++ b/samples/text/text.cpp @@ -28,6 +28,10 @@ #include "wx/clipbrd.h" #endif +#if wxUSE_FILE + #include "wx/file.h" +#endif + #if wxUSE_TOOLTIPS #include "wx/tooltip.h" #endif @@ -57,12 +61,14 @@ class MyTextCtrl : public wxTextCtrl public: MyTextCtrl(wxWindow *parent, wxWindowID id, const wxString &value, const wxPoint &pos, const wxSize &size, int style = 0) - : wxTextCtrl(parent, id, value, pos, size, style) { } + : wxTextCtrl(parent, id, value, pos, size, style) { m_hasCapture = FALSE; } void OnKeyDown(wxKeyEvent& event); void OnKeyUp(wxKeyEvent& event); void OnChar(wxKeyEvent& event); + bool m_hasCapture; + private: static inline wxChar GetChar(bool on, wxChar c) { return on ? c : _T('-'); } void LogEvent(const wxChar *name, wxKeyEvent& event) const; @@ -94,6 +100,8 @@ public: MyTextCtrl *m_multitext; MyTextCtrl *m_horizontal; + MyTextCtrl *m_textrich; + wxTextCtrl *m_log; private: @@ -124,6 +132,8 @@ public: void OnMoveToEndOfEntry( wxCommandEvent &event ) { m_panel->DoMoveToEndOfEntry(); } + void OnLogClear(wxCommandEvent& event); + void OnFileSave(wxCommandEvent& event); void OnFileLoad(wxCommandEvent& event); void OnIdle( wxIdleEvent& event ); @@ -149,6 +159,8 @@ enum TEXT_QUIT = 100, TEXT_ABOUT, TEXT_LOAD, + TEXT_SAVE, + TEXT_CLEAR, // clipboard menu TEXT_CLIPBOARD_COPY = 200, @@ -167,10 +179,14 @@ bool MyApp::OnInit() { // Create the main frame window MyFrame *frame = new MyFrame((wxFrame *) NULL, - "Text wxWindows sample", 50, 50, 640, 420); + "Text wxWindows sample", 50, 50, 660, 420); frame->SetSizeHints( 500, 400 ); wxMenu *file_menu = new wxMenu; + file_menu->Append(TEXT_CLEAR, "&Clear the log\tCtrl-C", + "Clear the log window contents"); + file_menu->Append(TEXT_SAVE, "&Save file\tCtrl-S", + "Save the text control contents to file"); file_menu->Append(TEXT_LOAD, "&Load file\tCtrl-O", "Load the sample file into text control"); file_menu->AppendSeparator(); @@ -334,13 +350,13 @@ void MyTextCtrl::LogEvent(const wxChar *name, wxKeyEvent& event) const case WXK_NUMPAD_SUBTRACT: key = "NUMPAD_SUBTRACT"; break; case WXK_NUMPAD_DECIMAL: key = "NUMPAD_DECIMAL"; break; - default: + default: { if ( wxIsprint((int)keycode) ) key.Printf( _T("'%c'") , (char)keycode); else - key.Printf( _T("unknown (%ld)"), keycode); - } + key.Printf( _T("unknown (%ld)"), keycode); + } } } @@ -411,9 +427,30 @@ void MyTextCtrl::OnKeyDown(wxKeyEvent& event) // go to position 10 SetInsertionPoint(10); break; - } - LogEvent( _("Key down"), event); + case WXK_F4: + if (!m_hasCapture) + { + wxLogDebug( wxT("Now capturing mouse and events.") ); + m_hasCapture = TRUE; + CaptureMouse(); + } + else + { + wxLogDebug( wxT("Stopped capturing mouse and events.") ); + m_hasCapture = TRUE; + ReleaseMouse(); + } + break; + + case WXK_F5: + // insert a blank line + WriteText("\n"); + break; + + default: + LogEvent( _("Key down"), event); + } event.Skip(); } @@ -463,7 +500,7 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) m_multitext->WriteText( "Prepended. " ); #if wxUSE_TOOLTIPS - m_multitext->SetToolTip("Press F1 here."); + m_multitext->SetToolTip("Press F1 here for statitics, F4 for capture and uncapture mouse."); #endif m_tab = new MyTextCtrl( this, -1, "Multiline, allow processing.", @@ -471,6 +508,11 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) m_enter = new MyTextCtrl( this, -1, "Multiline, allow processing.", wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE); + + m_textrich = new MyTextCtrl(this, -1, "Allows more than 30Kb of text\n" + "(even under broken Win9x)", + wxPoint(450, 10), wxSize(200, 230), + wxTE_RICH | wxTE_MULTILINE); } void MyPanel::OnSize( wxSizeEvent &event ) @@ -526,6 +568,11 @@ void MyPanel::DoPasteFromClipboard() void MyPanel::DoCopyToClipboard() { + // On X11, we want to get the data from the primary selection instead + // of the normal clipboard (which isn't normal under X11 at all). This + // call has no effect under MSW. + wxTheClipboard->UsePrimarySelection(); + wxString text( m_multitext->GetLineText(0) ); if (text.IsEmpty()) @@ -583,7 +630,9 @@ void MyPanel::DoMoveToEndOfEntry() BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(TEXT_QUIT, MyFrame::OnQuit) EVT_MENU(TEXT_ABOUT, MyFrame::OnAbout) + EVT_MENU(TEXT_SAVE, MyFrame::OnFileSave) EVT_MENU(TEXT_LOAD, MyFrame::OnFileLoad) + EVT_MENU(TEXT_CLEAR, MyFrame::OnLogClear) #if wxUSE_TOOLTIPS EVT_MENU(TEXT_TOOLTIPS_SETDELAY, MyFrame::OnSetTooltipDelay) @@ -666,9 +715,31 @@ void MyFrame::OnToggleTooltips(wxCommandEvent& event) } #endif // tooltips +void MyFrame::OnLogClear(wxCommandEvent& WXUNUSED(event)) +{ + m_panel->m_log->Clear(); +} + +void MyFrame::OnFileSave(wxCommandEvent& event) +{ + if ( m_panel->m_textrich->SaveFile("dummy.txt") ) + { +#if wxUSE_FILE + // verify that the fil length is correct (it wasn't under Win95) + wxFile file("dummy.txt"); + wxLogStatus(this, _T("Successfully saved file " + "(text len = %ld, file size = %ld)"), + m_panel->m_textrich->GetValue().length(), + file.Length()); +#endif + } + else + wxLogStatus(this, _T("Couldn't save the file")); +} + void MyFrame::OnFileLoad(wxCommandEvent& event) { - if ( m_panel->m_multitext->LoadFile("text.rc") ) + if ( m_panel->m_textrich->LoadFile("dummy.txt") ) wxLogStatus(this, _T("Successfully loaded file")); else wxLogStatus(this, _T("Couldn't load the file"));