X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9838df2cefc5b368bb11f98c784ecc78f45ecaf7..4fa688d8bced718a7ef35d4ebd3c4db6eb2afa7d:/samples/controls/controls.cpp diff --git a/samples/controls/controls.cpp b/samples/controls/controls.cpp index 424dad13ac..9c9a7b35d7 100644 --- a/samples/controls/controls.cpp +++ b/samples/controls/controls.cpp @@ -29,6 +29,10 @@ #include "wx/spinbutt.h" #include "wx/clipbrd.h" +#ifdef __WXGTK__ +#include "wx/tooltip.h" +#endif + #if defined(__WXGTK__) || defined(__WXMOTIF__) #define USE_XPM #endif @@ -53,6 +57,21 @@ class MyApp: public wxApp bool OnInit(void); }; +// a text ctrl which allows to call different wxTextCtrl functions +// interactively by pressing function keys in it +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) { } + + void OnChar(wxKeyEvent& event); + +private: + DECLARE_EVENT_TABLE() +}; + class MyPanel: public wxPanel { public: @@ -76,6 +95,8 @@ class MyPanel: public wxPanel void OnSpinUpdate( wxSpinEvent &event ); void OnPasteFromClipboard( wxCommandEvent &event ); void OnCopyToClipboard( wxCommandEvent &event ); + void OnMoveToEndOfText( wxCommandEvent &event ); + void OnMoveToEndOfEntry( wxCommandEvent &event ); wxListBox *m_listbox; wxChoice *m_choice; @@ -86,7 +107,8 @@ class MyPanel: public wxPanel wxButton *m_fontButton; wxSpinButton *m_spinbutton; wxTextCtrl *m_spintext; - wxTextCtrl *m_multitext; + MyTextCtrl *m_multitext; + MyTextCtrl *m_textentry; wxCheckBox *m_checkbox; wxTextCtrl *m_text; @@ -114,7 +136,7 @@ class MyFrame: public wxFrame // main() //---------------------------------------------------------------------- -IMPLEMENT_APP (MyApp) +IMPLEMENT_APP(MyApp) //---------------------------------------------------------------------- // MyApp @@ -152,6 +174,52 @@ bool MyApp::OnInit(void) return TRUE; } +//---------------------------------------------------------------------- +// MyTextCtrl +//---------------------------------------------------------------------- + +BEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl) + EVT_CHAR(MyTextCtrl::OnChar) +END_EVENT_TABLE() + +void MyTextCtrl::OnChar(wxKeyEvent& event) +{ + switch ( event.KeyCode() ) + { + case WXK_F1: + // show current position and text length + { + long line, column, pos = GetInsertionPoint(); + PositionToXY(pos, &column, &line); + + wxLogMessage("Current position: %ld\n" + "Current line, column: (%ld, %ld)\n" + "Number of lines: %ld\n" + "Current line length: %ld\n" + "Total text length: %ld", + pos, + line, column, + GetNumberOfLines(), + GetLineLength(line), + GetLastPosition()); + } + break; + + case WXK_F2: + // go to the end + SetInsertionPointEnd(); + break; + + case WXK_F3: + // go to position 10 + SetInsertionPoint(10); + break; + + default: + event.Skip(); + } +} + //---------------------------------------------------------------------- // MyPanel //---------------------------------------------------------------------- @@ -188,6 +256,8 @@ const int ID_COMBO_ENABLE = 147; const int ID_TEXT = 150; const int ID_PASTE_TEXT = 151; const int ID_COPY_TEXT = 152; +const int ID_MOVE_END_ENTRY = 153; +const int ID_MOVE_END_ZONE = 154; const int ID_RADIOBOX = 160; const int ID_RADIOBOX_SEL_NUM = 161; @@ -205,7 +275,6 @@ const int ID_SLIDER = 181; const int ID_SPIN = 182; - BEGIN_EVENT_TABLE(MyPanel, wxPanel) EVT_SIZE ( MyPanel::OnSize) EVT_NOTEBOOK_PAGE_CHANGED(ID_NOTEBOOK, MyPanel::OnPageChanged) @@ -226,7 +295,7 @@ BEGIN_EVENT_TABLE(MyPanel, wxPanel) EVT_BUTTON (ID_CHOICE_DELETE, MyPanel::OnChoiceButtons) EVT_BUTTON (ID_CHOICE_FONT, MyPanel::OnChoiceButtons) EVT_CHECKBOX (ID_CHOICE_ENABLE, MyPanel::OnChoiceButtons) - EVT_CHOICE (ID_COMBO, MyPanel::OnCombo) + EVT_COMBOBOX (ID_COMBO, MyPanel::OnCombo) EVT_BUTTON (ID_COMBO_SEL_NUM, MyPanel::OnComboButtons) EVT_BUTTON (ID_COMBO_SEL_STR, MyPanel::OnComboButtons) EVT_BUTTON (ID_COMBO_CLEAR, MyPanel::OnComboButtons) @@ -244,6 +313,8 @@ BEGIN_EVENT_TABLE(MyPanel, wxPanel) EVT_SPIN (ID_SPIN, MyPanel::OnSpinUpdate) EVT_BUTTON (ID_PASTE_TEXT, MyPanel::OnPasteFromClipboard) EVT_BUTTON (ID_COPY_TEXT, MyPanel::OnCopyToClipboard) + EVT_BUTTON (ID_MOVE_END_ZONE, MyPanel::OnMoveToEndOfText) + EVT_BUTTON (ID_MOVE_END_ENTRY, MyPanel::OnMoveToEndOfEntry) END_EVENT_TABLE() MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) : @@ -329,6 +400,9 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) : // panel->SetBackgroundColour("cadet blue"); // panel->SetForegroundColour("blue"); m_listbox = new wxListBox( panel, ID_LISTBOX, wxPoint(10,10), wxSize(120,70), 5, choices ); +#ifdef __WXGTK__ + m_listbox->SetToolTip( "This is a list box" ); +#endif // m_listbox->SetBackgroundColour("wheat"); (void)new wxButton( panel, ID_LISTBOX_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) ); (void)new wxButton( panel, ID_LISTBOX_SEL_STR, "Select 'This'", wxPoint(340,30), wxSize(140,30) ); @@ -336,9 +410,16 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) : (void)new wxButton( panel, ID_LISTBOX_APPEND, "Append 'Hi!'", wxPoint(340,80), wxSize(140,30) ); (void)new wxButton( panel, ID_LISTBOX_DELETE, "Delete selected item", wxPoint(180,130), wxSize(140,30) ); button = new wxButton( panel, ID_LISTBOX_FONT, "Set Italic font", wxPoint(340,130), wxSize(140,30) ); +#ifdef __WXGTK__ + button->SetToolTip( "Press here to set italic font" ); +#endif + // button->SetForegroundColour( "red" ); m_checkbox = new wxCheckBox( panel, ID_LISTBOX_ENABLE, "Disable", wxPoint(20,130), wxSize(140,30) ); m_checkbox->SetValue(FALSE); +#ifdef __WXGTK__ + m_checkbox->SetToolTip( "Click here to disable the listbox" ); +#endif m_notebook->AddPage(panel, "wxList", TRUE, Image_List); panel = new wxPanel(m_notebook); @@ -372,15 +453,22 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) : panel = new wxPanel(m_notebook); // panel->SetBackgroundColour("cadet blue"); // panel->SetForegroundColour("blue"); - wxTextCtrl *tc = new wxTextCtrl( panel, ID_TEXT, "Write text here.", wxPoint(10,10), wxSize(320,28)); - (*tc) << " More text."; -// tc->SetBackgroundColour("wheat"); - m_multitext = new wxTextCtrl( panel, ID_TEXT, "And here.", wxPoint(10,50), wxSize(320,160), wxTE_MULTILINE ); - (*m_multitext) << " More text."; - m_multitext->SetBackgroundColour("wheat"); - (void)new wxStaticBox( panel, -1, "wxClipboard", wxPoint(345,50), wxSize(160,145) ); - (void)new wxButton( panel, ID_COPY_TEXT, "Copy line 1", wxPoint(370,80), wxSize(110,30) ); - (void)new wxButton( panel, ID_PASTE_TEXT, "Paste text", wxPoint(370,140), wxSize(110,30) ); + m_textentry = new MyTextCtrl( panel, ID_TEXT, "Write text here.", wxPoint(10,10), wxSize(320,28)); + (*m_textentry) << " More text."; +// m_textentry->SetBackgroundColour("wheat"); + m_multitext = new MyTextCtrl( panel, ID_TEXT, "And here.", wxPoint(10,50), wxSize(320,160), wxTE_MULTILINE ); + (*m_multitext) << " More text." + << "\nPress Fn keys to test different wxTextCtrl functions"; +// m_multitext->SetBackgroundColour("wheat"); + (void)new wxStaticBox( panel, -1, "Move cursor to the end of:", + wxPoint(345, 0), wxSize(160, 100) ); + (void)new wxButton(panel, ID_MOVE_END_ENTRY, "Text entry", + wxPoint(370, 20), wxSize(110, 30)); + (void)new wxButton(panel, ID_MOVE_END_ZONE, "Text zone", + wxPoint(370, 60), wxSize(110, 30)); + (void)new wxStaticBox( panel, -1, "wxClipboard", wxPoint(345,120), wxSize(160,100) ); + (void)new wxButton( panel, ID_COPY_TEXT, "Copy line 1", wxPoint(370,140), wxSize(110,30) ); + (void)new wxButton( panel, ID_PASTE_TEXT, "Paste text", wxPoint(370,180), wxSize(110,30) ); m_notebook->AddPage(panel, "wxTextCtrl" , FALSE, Image_Text); wxString choices2[] = @@ -394,7 +482,7 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) : // panel->SetForegroundColour("blue"); m_radio = new wxRadioBox( panel, ID_RADIOBOX, "That", wxPoint(10,160), wxSize(-1,-1), 2, choices2, 1, wxRA_SPECIFY_ROWS ); // m_radio->SetBackgroundColour("wheat"); - m_radio = new wxRadioBox( panel, ID_RADIOBOX, "This", wxPoint(10,10), wxSize(-1,-1), 5, choices, 1, wxRA_SPECIFY_COLS ); + m_radio = new wxRadioBox( panel, ID_RADIOBOX, "This", wxPoint(10,10), wxSize(-1,-1), 5, choices, 2, wxRA_SPECIFY_COLS ); // m_radio->SetBackgroundColour("wheat"); (void)new wxButton( panel, ID_RADIOBOX_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) ); (void)new wxButton( panel, ID_RADIOBOX_SEL_STR, "Select 'This'", wxPoint(180,80), wxSize(140,30) ); @@ -459,24 +547,31 @@ void MyPanel::OnPasteFromClipboard( wxCommandEvent &WXUNUSED(event) ) *m_text << "Successfully opened the clipboard." << "\n"; } - wxTextDataObject *data = new wxTextDataObject(); + wxTextDataObject data; - if (wxTheClipboard->GetData( data )) + if (wxTheClipboard->IsSupported( data )) { - *m_text << "Successfully retrieved data from the clipboard." << "\n"; - *m_multitext << data->GetText() << "\n"; + *m_text << "Clipboard supports requested format." << "\n"; + + if (wxTheClipboard->GetData( data )) + { + *m_text << "Successfully retrieved data from the clipboard." << "\n"; + *m_multitext << data.GetText() << "\n"; + } + else + { + *m_text << "Error getting data from the clipboard." << "\n"; + } } else { - *m_text << "Error getting data from the clipboard." << "\n"; + *m_text << "Clipboard doesn't support requested format." << "\n"; } wxTheClipboard->Close(); *m_text << "Closed the clipboard." << "\n"; - delete data; - #endif } @@ -500,10 +595,8 @@ void MyPanel::OnCopyToClipboard( wxCommandEvent &WXUNUSED(event) ) } wxTextDataObject *data = new wxTextDataObject( text ); - wxDataBroker *broker = new wxDataBroker(); - broker->Add( data ); - if (!wxTheClipboard->SetData( broker )) + if (!wxTheClipboard->SetData( data )) { *m_text << "Error while copying to the clipboard." << "\n"; } @@ -519,6 +612,18 @@ void MyPanel::OnCopyToClipboard( wxCommandEvent &WXUNUSED(event) ) #endif } +void MyPanel::OnMoveToEndOfText( wxCommandEvent &event ) +{ + m_multitext->SetInsertionPointEnd(); + m_multitext->SetFocus(); +} + +void MyPanel::OnMoveToEndOfEntry( wxCommandEvent &event ) +{ + m_textentry->SetInsertionPointEnd(); + m_textentry->SetFocus(); +} + void MyPanel::OnSize( wxSizeEvent& WXUNUSED(event) ) { int x = 0;