X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/87728739f23c6fcc737614e2e726f68778f9415b..084ad0c2424aa998a640c151f8fa762435364449:/samples/listbox/lboxtest.cpp?ds=sidebyside diff --git a/samples/listbox/lboxtest.cpp b/samples/listbox/lboxtest.cpp index 2e38ba5d72..d5dd01608f 100644 --- a/samples/listbox/lboxtest.cpp +++ b/samples/listbox/lboxtest.cpp @@ -20,11 +20,6 @@ // declarations // ============================================================================ -#ifdef __GNUG__ - #pragma implementation "lboxtest.cpp" - #pragma interface "lboxtest.cpp" -#endif - // ---------------------------------------------------------------------------- // headers // ---------------------------------------------------------------------------- @@ -38,6 +33,7 @@ // for all others, include the necessary headers #ifndef WX_PRECOMP + #include "wx/wx.h" #include "wx/app.h" #include "wx/frame.h" #include "wx/dcclient.h" @@ -73,12 +69,15 @@ enum LboxTest_AddSeveral, LboxTest_AddMany, LboxTest_Clear, +#if wxUSE_LOG LboxTest_ClearLog, +#endif // wxUSE_LOG LboxTest_Change, LboxTest_ChangeText, LboxTest_Delete, LboxTest_DeleteText, LboxTest_DeleteSel, + LboxTest_DeselectAll, LboxTest_Listbox, LboxTest_Quit }; @@ -115,8 +114,12 @@ protected: void OnButtonChange(wxCommandEvent& event); void OnButtonDelete(wxCommandEvent& event); void OnButtonDeleteSel(wxCommandEvent& event); + void OnButtonDeselectAll(wxCommandEvent& event); void OnButtonClear(wxCommandEvent& event); + +#if wxUSE_LOG void OnButtonClearLog(wxCommandEvent& event); +#endif // wxUSE_LOG void OnButtonAdd(wxCommandEvent& event); void OnButtonAddSeveral(wxCommandEvent& event); void OnButtonAddMany(wxCommandEvent& event); @@ -124,6 +127,7 @@ protected: void OnListbox(wxCommandEvent& event); void OnListboxDClick(wxCommandEvent& event); + void OnListboxRDown(wxMouseEvent& event); void OnCheckOrRadioBox(wxCommandEvent& event); @@ -132,6 +136,7 @@ protected: void OnUpdateUIClearButton(wxUpdateUIEvent& event); void OnUpdateUIDeleteButton(wxUpdateUIEvent& event); void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event); + void OnUpdateUIDeselectAllButton(wxUpdateUIEvent& event); // reset the listbox parameters void Reset(); @@ -175,8 +180,13 @@ protected: wxListBox *m_lbox; wxSizer *m_sizerLbox; + // panel the controls such as the listbox are in + wxPanel* m_panel; + +#if wxUSE_LOG // the listbox for logging messages wxListBox *m_lboxLog; +#endif // wxUSE_LOG // the text entries for "Add/change string" and "Delete" buttons wxTextCtrl *m_textAdd, @@ -184,13 +194,16 @@ protected: *m_textDelete; private: +#if wxUSE_LOG // the log target we use to redirect messages to the listbox wxLog *m_logTarget; +#endif // wxUSE_LOG - // any class wishing to process wxWindows events must use this macro + // any class wishing to process wxWidgets events must use this macro DECLARE_EVENT_TABLE() }; +#if wxUSE_LOG // A log target which just redirects the messages to a listbox class LboxLogger : public wxLog { @@ -227,7 +240,7 @@ private: } } - virtual void DoLogString(const wxChar *szString, time_t t) + virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t)) { wxString msg; TimeStamp(&msg); @@ -250,6 +263,7 @@ private: // the old log target wxLog *m_logOld; }; +#endif // wxUSE_LOG // ---------------------------------------------------------------------------- // misc macros @@ -257,11 +271,6 @@ private: IMPLEMENT_APP(LboxTestApp) -#ifdef __WXUNIVERSAL__ - WX_USE_THEME(win32); - WX_USE_THEME(gtk); -#endif // __WXUNIVERSAL__ - // ---------------------------------------------------------------------------- // event tables // ---------------------------------------------------------------------------- @@ -272,8 +281,11 @@ BEGIN_EVENT_TABLE(LboxTestFrame, wxFrame) EVT_BUTTON(LboxTest_Change, LboxTestFrame::OnButtonChange) EVT_BUTTON(LboxTest_Delete, LboxTestFrame::OnButtonDelete) EVT_BUTTON(LboxTest_DeleteSel, LboxTestFrame::OnButtonDeleteSel) + EVT_BUTTON(LboxTest_DeselectAll, LboxTestFrame::OnButtonDeselectAll) EVT_BUTTON(LboxTest_Clear, LboxTestFrame::OnButtonClear) +#if wxUSE_LOG EVT_BUTTON(LboxTest_ClearLog, LboxTestFrame::OnButtonClearLog) +#endif // wxUSE_LOG EVT_BUTTON(LboxTest_Add, LboxTestFrame::OnButtonAdd) EVT_BUTTON(LboxTest_AddSeveral, LboxTestFrame::OnButtonAddSeveral) EVT_BUTTON(LboxTest_AddMany, LboxTestFrame::OnButtonAddMany) @@ -292,11 +304,12 @@ BEGIN_EVENT_TABLE(LboxTestFrame, wxFrame) EVT_UPDATE_UI(LboxTest_Change, LboxTestFrame::OnUpdateUIDeleteSelButton) EVT_UPDATE_UI(LboxTest_ChangeText, LboxTestFrame::OnUpdateUIDeleteSelButton) EVT_UPDATE_UI(LboxTest_DeleteSel, LboxTestFrame::OnUpdateUIDeleteSelButton) + EVT_UPDATE_UI(LboxTest_DeselectAll, LboxTestFrame::OnUpdateUIDeselectAllButton) EVT_LISTBOX(LboxTest_Listbox, LboxTestFrame::OnListbox) - EVT_LISTBOX_DCLICK(-1, LboxTestFrame::OnListboxDClick) - EVT_CHECKBOX(-1, LboxTestFrame::OnCheckOrRadioBox) - EVT_RADIOBOX(-1, LboxTestFrame::OnCheckOrRadioBox) + EVT_LISTBOX_DCLICK(wxID_ANY, LboxTestFrame::OnListboxDClick) + EVT_CHECKBOX(wxID_ANY, LboxTestFrame::OnCheckOrRadioBox) + EVT_RADIOBOX(wxID_ANY, LboxTestFrame::OnCheckOrRadioBox) END_EVENT_TABLE() // ============================================================================ @@ -312,10 +325,12 @@ bool LboxTestApp::OnInit() wxFrame *frame = new LboxTestFrame(_T("wxListBox sample")); frame->Show(); +#if wxUSE_LOG //wxLog::AddTraceMask(_T("listbox")); wxLog::AddTraceMask(_T("scrollbar")); +#endif // wxUSE_LOG - return TRUE; + return true; } // ---------------------------------------------------------------------------- @@ -323,23 +338,27 @@ bool LboxTestApp::OnInit() // ---------------------------------------------------------------------------- LboxTestFrame::LboxTestFrame(const wxString& title) - : wxFrame(NULL, -1, title, wxPoint(100, 100)) + : wxFrame(NULL, wxID_ANY, title, wxPoint(100, 100)) { // init everything - m_dirty = FALSE; + m_dirty = false; m_radioSelMode = (wxRadioBox *)NULL; m_chkVScroll = m_chkHScroll = m_chkSort = (wxCheckBox *)NULL; - m_lbox = + m_lbox = (wxListBox *)NULL; +#if wxUSE_LOG m_lboxLog = (wxListBox *)NULL; +#endif // wxUSE_LOG m_sizerLbox = (wxSizer *)NULL; +#if wxUSE_LOG m_logTarget = (wxLog *)NULL; +#endif // wxUSE_LOG - wxPanel *panel = new wxPanel(this, -1); + m_panel = new wxPanel(this, wxID_ANY); /* What we create here is a frame having 3 panes: the explanatory pane to @@ -360,15 +379,15 @@ LboxTestFrame::LboxTestFrame(const wxString& title) _T("multiple"), }; - wxStaticBox *box = new wxStaticBox(panel, -1, _T("&Set listbox parameters")); - m_radioSelMode = new wxRadioBox(panel, -1, _T("Selection &mode:"), + wxStaticBox *box = new wxStaticBox(m_panel, wxID_ANY, _T("&Set listbox parameters")); + m_radioSelMode = new wxRadioBox(m_panel, wxID_ANY, _T("Selection &mode:"), wxDefaultPosition, wxDefaultSize, WXSIZEOF(modes), modes, 1, wxRA_SPECIFY_COLS); - m_chkVScroll = new wxCheckBox(panel, -1, _T("Always show &vertical scrollbar")); - m_chkHScroll = new wxCheckBox(panel, -1, _T("Show &horizontal scrollbar")); - m_chkSort = new wxCheckBox(panel, -1, _T("&Sort items")); + m_chkVScroll = new wxCheckBox(m_panel, wxID_ANY, _T("Always show &vertical scrollbar")); + m_chkHScroll = new wxCheckBox(m_panel, wxID_ANY, _T("Show &horizontal scrollbar")); + m_chkSort = new wxCheckBox(m_panel, wxID_ANY, _T("&Sort items")); sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); @@ -379,55 +398,53 @@ LboxTestFrame::LboxTestFrame(const wxString& title) sizerLeft->Add(m_radioSelMode, 0, wxGROW | wxALL, 5); wxSizer *sizerBtn = new wxBoxSizer(wxHORIZONTAL); - wxButton *btn = new wxButton(panel, LboxTest_Reset, _T("&Reset")); + wxButton *btn = new wxButton(m_panel, LboxTest_Reset, _T("&Reset")); sizerBtn->Add(btn, 0, wxLEFT | wxRIGHT, 5); - btn = new wxButton(panel, LboxTest_Create, _T("&Create")); + btn = new wxButton(m_panel, LboxTest_Create, _T("&Create")); sizerBtn->Add(btn, 0, wxLEFT | wxRIGHT, 5); sizerLeft->Add(sizerBtn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); // middle pane - wxStaticBox *box2 = new wxStaticBox(panel, -1, _T("&Change listbox contents")); + wxStaticBox *box2 = new wxStaticBox(m_panel, wxID_ANY, _T("&Change listbox contents")); wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL); wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL); - btn = new wxButton(panel, LboxTest_Add, _T("&Add this string")); - m_textAdd = new wxTextCtrl(panel, LboxTest_AddText, _T("test item 0")); + btn = new wxButton(m_panel, LboxTest_Add, _T("&Add this string")); + m_textAdd = new wxTextCtrl(m_panel, LboxTest_AddText, _T("test item 0")); sizerRow->Add(btn, 0, wxRIGHT, 5); sizerRow->Add(m_textAdd, 1, wxLEFT, 5); sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); - btn = new wxButton(panel, LboxTest_AddSeveral, _T("&Insert a few strings")); + btn = new wxButton(m_panel, LboxTest_AddSeveral, _T("&Insert a few strings")); sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); - btn = new wxButton(panel, LboxTest_AddMany, _T("Add &many strings")); + btn = new wxButton(m_panel, LboxTest_AddMany, _T("Add &many strings")); sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); sizerRow = new wxBoxSizer(wxHORIZONTAL); - btn = new wxButton(panel, LboxTest_Change, _T("C&hange current")); - m_textChange = new wxTextCtrl(panel, LboxTest_ChangeText, _T("")); + btn = new wxButton(m_panel, LboxTest_Change, _T("C&hange current")); + m_textChange = new wxTextCtrl(m_panel, LboxTest_ChangeText, wxEmptyString); sizerRow->Add(btn, 0, wxRIGHT, 5); sizerRow->Add(m_textChange, 1, wxLEFT, 5); sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); sizerRow = new wxBoxSizer(wxHORIZONTAL); - btn = new wxButton(panel, LboxTest_Delete, _T("&Delete this item")); - m_textDelete = new wxTextCtrl(panel, LboxTest_DeleteText, _T("")); + btn = new wxButton(m_panel, LboxTest_Delete, _T("&Delete this item")); + m_textDelete = new wxTextCtrl(m_panel, LboxTest_DeleteText, wxEmptyString); sizerRow->Add(btn, 0, wxRIGHT, 5); sizerRow->Add(m_textDelete, 1, wxLEFT, 5); sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); - btn = new wxButton(panel, LboxTest_DeleteSel, _T("Delete &selection")); + btn = new wxButton(m_panel, LboxTest_DeleteSel, _T("Delete &selection")); + sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); + + btn = new wxButton(m_panel, LboxTest_DeselectAll, _T("Deselect All")); sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); - btn = new wxButton(panel, LboxTest_Clear, _T("&Clear")); + btn = new wxButton(m_panel, LboxTest_Clear, _T("&Clear")); sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); // right pane - m_lbox = new wxListBox(panel, LboxTest_Listbox, - wxDefaultPosition, wxDefaultSize, - 0, NULL, - wxLB_HSCROLL); - sizerRight->Add(m_lbox, 1, wxGROW | wxALL, 5); sizerRight->SetMinSize(250, 0); m_sizerLbox = sizerRight; // save it to modify it later @@ -437,18 +454,24 @@ LboxTestFrame::LboxTestFrame(const wxString& title) sizerUp->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); // the lower one only has the log listbox and a button to clear it +#if wxUSE_LOG wxSizer *sizerDown = new wxStaticBoxSizer ( - new wxStaticBox(panel, -1, _T("&Log window")), + new wxStaticBox(m_panel, wxID_ANY, _T("&Log window")), wxVERTICAL ); - m_lboxLog = new wxListBox(panel, -1); + m_lboxLog = new wxListBox(m_panel, wxID_ANY); sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5); +#else + wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL); +#endif // wxUSE_LOG wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL); - btn = new wxButton(panel, LboxTest_ClearLog, _T("Clear &log")); +#if wxUSE_LOG + btn = new wxButton(m_panel, LboxTest_ClearLog, _T("Clear &log")); sizerBtns->Add(btn); sizerBtns->Add(10, 0); // spacer - btn = new wxButton(panel, LboxTest_Quit, _T("E&xit")); +#endif // wxUSE_LOG + btn = new wxButton(m_panel, LboxTest_Quit, _T("E&xit")); sizerBtns->Add(btn); sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5); @@ -457,25 +480,28 @@ LboxTestFrame::LboxTestFrame(const wxString& title) sizerTop->Add(0, 5, 0, wxGROW); // spacer in between sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10); - // final initialization + // final initialization and create the listbox Reset(); - m_dirty = FALSE; + CreateLbox(); - panel->SetAutoLayout(TRUE); - panel->SetSizer(sizerTop); + m_panel->SetSizer(sizerTop); sizerTop->Fit(this); sizerTop->SetSizeHints(this); +#if wxUSE_LOG // now that everything is created we can redirect the log messages to the // listbox m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget()); wxLog::SetActiveTarget(m_logTarget); +#endif // wxUSE_LOG } LboxTestFrame::~LboxTestFrame() { +#if wxUSE_LOG delete m_logTarget; +#endif // wxUSE_LOG } // ---------------------------------------------------------------------------- @@ -494,11 +520,11 @@ void LboxTestFrame::Reset() } m_radioSelMode->SetSelection(LboxSel_Single); - m_chkSort->SetValue(FALSE); - m_chkHScroll->SetValue(TRUE); - m_chkVScroll->SetValue(FALSE); + m_chkSort->SetValue(false); + m_chkHScroll->SetValue(true); + m_chkVScroll->SetValue(false); - m_dirty = TRUE; + m_dirty = true; } void LboxTestFrame::CreateLbox() @@ -522,27 +548,32 @@ void LboxTestFrame::CreateLbox() flags |= wxLB_SORT; wxArrayString items; - if ( m_lbox ) + + if ( m_lbox ) // cache old items to restore later if listbox existed { - int count = m_lbox->GetCount(); - for ( int n = 0; n < count; n++ ) - { - items.Add(m_lbox->GetString(n)); - } + int count = m_lbox->GetCount(); + for ( int n = 0; n < count; n++ ) + { + items.Add(m_lbox->GetString(n)); + } - m_sizerLbox->Remove(m_lbox); - delete m_lbox; + m_sizerLbox->Detach(m_lbox); + delete m_lbox; } - m_lbox = new wxListBox(this, -1, + m_lbox = new wxListBox(m_panel, LboxTest_Listbox, wxDefaultPosition, wxDefaultSize, 0, NULL, flags); + m_lbox->Set(items); m_sizerLbox->Add(m_lbox, 1, wxGROW | wxALL, 5); m_sizerLbox->Layout(); - m_dirty = FALSE; + m_dirty = false; + + m_lbox->Connect(wxEVT_RIGHT_DOWN, + wxMouseEventHandler(LboxTestFrame::OnListboxRDown), NULL, this); } // ---------------------------------------------------------------------------- @@ -597,19 +628,26 @@ void LboxTestFrame::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event)) } } +void LboxTestFrame::OnButtonDeselectAll(wxCommandEvent& WXUNUSED(event)) +{ + m_lbox->SetSelection(-1); +} + void LboxTestFrame::OnButtonClear(wxCommandEvent& WXUNUSED(event)) { m_lbox->Clear(); } +#if wxUSE_LOG void LboxTestFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event)) { m_lboxLog->Clear(); } +#endif // wxUSE_LOG void LboxTestFrame::OnButtonAdd(wxCommandEvent& WXUNUSED(event)) { - static size_t s_item = 0; + static unsigned s_item = 0; wxString s = m_textAdd->GetValue(); if ( !m_textAdd->IsModified() ) @@ -624,7 +662,7 @@ void LboxTestFrame::OnButtonAdd(wxCommandEvent& WXUNUSED(event)) void LboxTestFrame::OnButtonAddMany(wxCommandEvent& WXUNUSED(event)) { // "many" means 1000 here - for ( size_t n = 0; n < 1000; n++ ) + for ( unsigned n = 0; n < 1000; n++ ) { m_lbox->Append(wxString::Format(_T("item #%u"), n)); } @@ -657,6 +695,12 @@ void LboxTestFrame::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event) event.Enable(m_lbox->GetSelections(selections) != 0); } +void LboxTestFrame::OnUpdateUIDeselectAllButton(wxUpdateUIEvent& event) +{ + wxArrayInt selections; + event.Enable(m_lbox->GetSelections(selections) != 0); +} + void LboxTestFrame::OnUpdateUIClearButton(wxUpdateUIEvent& event) { event.Enable(m_lbox->GetCount() != 0); @@ -670,18 +714,31 @@ void LboxTestFrame::OnUpdateUIAddSeveral(wxUpdateUIEvent& event) void LboxTestFrame::OnListbox(wxCommandEvent& event) { int sel = event.GetInt(); - m_textDelete->SetValue(wxString::Format(_T("%ld"), sel)); + m_textDelete->SetValue(wxString::Format(_T("%d"), sel)); wxLogMessage(_T("Listbox item %d selected"), sel); } void LboxTestFrame::OnListboxDClick(wxCommandEvent& event) { - wxLogMessage(_T("Listbox item %d double clicked"), event.GetInt()); + int sel = event.GetInt(); + wxLogMessage(_T("Listbox item %d double clicked"), sel); +} + +void LboxTestFrame::OnListboxRDown(wxMouseEvent& event) +{ + int item = m_lbox->HitTest(event.GetPosition()); + + if ( item != wxNOT_FOUND ) + wxLogMessage(_T("Listbox item %d right clicked"), item); + else + wxLogMessage(_T("Listbox right clicked but no item clicked upon")); + + event.Skip(); } void LboxTestFrame::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event)) { - m_dirty = TRUE; + m_dirty = true; }