X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/129c8cf3524ba45c690192cada38025a7aa544ca..ac687ddffb6f199603abc2415f7bcf0d051f1eca:/samples/combo/combo.cpp diff --git a/samples/combo/combo.cpp b/samples/combo/combo.cpp index 7d10b80345..ecf48986a6 100644 --- a/samples/combo/combo.cpp +++ b/samples/combo/combo.cpp @@ -4,7 +4,6 @@ // Author: Jaakko Salli // Modified by: // Created: Apr-30-2006 -// RCS-ID: $Id$ // Copyright: (c) Jaakko Salli // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -45,7 +44,7 @@ // the application icon (under Windows and OS/2 it is in resources and even // though we could still include the XPM here it would be unused) -#if !defined(__WXMSW__) && !defined(__WXPM__) +#ifndef wxHAS_IMAGES_IN_RESOURCES #include "../sample.xpm" #endif @@ -83,6 +82,9 @@ public: // log wxComboCtrl events void OnComboBoxUpdate( wxCommandEvent& event ); + void OnIdle( wxIdleEvent& event ); + + protected: wxTextCtrl* m_logWin; wxLog* m_logOld; @@ -102,15 +104,15 @@ private: // IDs for the controls and the menu commands enum { - ComboControl_Compare = wxID_HIGHEST, + ComboCtrl_Compare = wxID_HIGHEST, // menu items - ComboControl_Quit = wxID_EXIT, + ComboCtrl_Quit = wxID_EXIT, // it is important for the id corresponding to the "About" command to have // this standard value as otherwise it won't be handled properly under Mac // (where it is special and put into the "Apple" menu) - ComboControl_About = wxID_ABOUT + ComboCtrl_About = wxID_ABOUT }; // ---------------------------------------------------------------------------- @@ -122,11 +124,14 @@ enum // simple menu events like this the static method is much simpler. BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_TEXT(wxID_ANY,MyFrame::OnComboBoxUpdate) + EVT_TEXT_ENTER(wxID_ANY,MyFrame::OnComboBoxUpdate) EVT_COMBOBOX(wxID_ANY,MyFrame::OnComboBoxUpdate) - EVT_MENU(ComboControl_Compare, MyFrame::OnShowComparison) - EVT_MENU(ComboControl_Quit, MyFrame::OnQuit) - EVT_MENU(ComboControl_About, MyFrame::OnAbout) + EVT_MENU(ComboCtrl_Compare, MyFrame::OnShowComparison) + EVT_MENU(ComboCtrl_Quit, MyFrame::OnQuit) + EVT_MENU(ComboCtrl_About, MyFrame::OnAbout) + + EVT_IDLE(MyFrame::OnIdle) END_EVENT_TABLE() // Create a new application object: this macro will allow wxWidgets to create @@ -147,8 +152,11 @@ IMPLEMENT_APP(MyApp) // 'Main program' equivalent: the program execution "starts" here bool MyApp::OnInit() { + if ( !wxApp::OnInit() ) + return false; + // create the main application window - MyFrame *frame = new MyFrame(_T("wxComboCtrl and wxOwnerDrawnComboBox Sample")); + MyFrame *frame = new MyFrame(wxT("wxComboCtrl and wxOwnerDrawnComboBox Sample")); // and show it (the frames, unlike simple controls, are not shown when // created initially) @@ -180,29 +188,29 @@ public: r.Deflate(3); r.height -= 2; - int penStyle = wxSOLID; + wxPenStyle penStyle = wxPENSTYLE_SOLID; if ( item == 1 ) - penStyle = wxTRANSPARENT; + penStyle = wxPENSTYLE_TRANSPARENT; else if ( item == 2 ) - penStyle = wxDOT; + penStyle = wxPENSTYLE_DOT; else if ( item == 3 ) - penStyle = wxLONG_DASH; + penStyle = wxPENSTYLE_LONG_DASH; else if ( item == 4 ) - penStyle = wxSHORT_DASH; + penStyle = wxPENSTYLE_SHORT_DASH; else if ( item == 5 ) - penStyle = wxDOT_DASH; + penStyle = wxPENSTYLE_DOT_DASH; else if ( item == 6 ) - penStyle = wxBDIAGONAL_HATCH; + penStyle = wxPENSTYLE_BDIAGONAL_HATCH; else if ( item == 7 ) - penStyle = wxCROSSDIAG_HATCH; + penStyle = wxPENSTYLE_CROSSDIAG_HATCH; else if ( item == 8 ) - penStyle = wxFDIAGONAL_HATCH; + penStyle = wxPENSTYLE_FDIAGONAL_HATCH; else if ( item == 9 ) - penStyle = wxCROSS_HATCH; + penStyle = wxPENSTYLE_CROSS_HATCH; else if ( item == 10 ) - penStyle = wxHORIZONTAL_HATCH; + penStyle = wxPENSTYLE_HORIZONTAL_HATCH; else if ( item == 11 ) - penStyle = wxVERTICAL_HATCH; + penStyle = wxPENSTYLE_VERTICAL_HATCH; wxPen pen( dc.GetTextForeground(), 3, penStyle ); @@ -368,14 +376,20 @@ public: virtual void Init() { } + virtual ~TreeCtrlComboPopup() + { + if (!m_isBeingDeleted) + { + wxMessageBox("error wxTreeCtrl::Destroy() was not called"); + } + SendDestroyEvent(); + } virtual bool Create( wxWindow* parent ) { return wxTreeCtrl::Create(parent,1, wxPoint(0,0),wxDefaultSize, - wxTR_HIDE_ROOT|wxTR_HAS_BUTTONS| - wxTR_SINGLE|wxTR_LINES_AT_ROOT| - wxSIMPLE_BORDER); + wxTR_DEFAULT_STYLE | wxTR_HIDE_ROOT | wxSIMPLE_BORDER ); } virtual void OnShow() @@ -486,6 +500,23 @@ BEGIN_EVENT_TABLE(TreeCtrlComboPopup, wxTreeCtrl) EVT_LEFT_DOWN(TreeCtrlComboPopup::OnMouseClick) END_EVENT_TABLE() +// ---------------------------------------------------------------------------- +// wxComboCtrl with custom popup animation, using wxWindow::ShowWithEffect(). +// ---------------------------------------------------------------------------- + +class wxComboCtrlWithCustomPopupAnim : public wxComboCtrl +{ +protected: + virtual bool AnimateShow( const wxRect& rect, int WXUNUSED(flags) ) + { + wxWindow* win = GetPopupWindow(); + win->SetSize(rect); + win->Raise(); // This is needed + win->ShowWithEffect(wxSHOW_EFFECT_BLEND); + return true; + } +}; + // ---------------------------------------------------------------------------- // wxComboCtrl with entirely custom button action (opens file dialog) // ---------------------------------------------------------------------------- @@ -592,17 +623,17 @@ MyFrame::MyFrame(const wxString& title) // the "About" item should be in the help menu wxMenu *helpMenu = new wxMenu; - helpMenu->Append(ComboControl_About, _T("&About...\tF1"), _T("Show about dialog")); + helpMenu->Append(ComboCtrl_About, wxT("&About\tF1"), wxT("Show about dialog")); - fileMenu->Append(ComboControl_Compare, _T("&Compare against wxComboBox..."), - _T("Show some wxOwnerDrawnComboBoxes side-by-side with native wxComboBoxes.")); + fileMenu->Append(ComboCtrl_Compare, wxT("&Compare against wxComboBox..."), + wxT("Show some wxOwnerDrawnComboBoxes side-by-side with native wxComboBoxes.")); fileMenu->AppendSeparator(); - fileMenu->Append(ComboControl_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); + fileMenu->Append(ComboCtrl_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program")); // now append the freshly created menu to the menu bar... wxMenuBar *menuBar = new wxMenuBar(); - menuBar->Append(fileMenu, _T("&File")); - menuBar->Append(helpMenu, _T("&Help")); + menuBar->Append(fileMenu, wxT("&File")); + menuBar->Append(helpMenu, wxT("&Help")); // ... and attach this menu bar to the frame SetMenuBar(menuBar); @@ -611,12 +642,13 @@ MyFrame::MyFrame(const wxString& title) wxPanel* panel = new wxPanel(this); // Prepare log window right away since it shows EVT_TEXTs - m_logWin = new wxTextCtrl( panel, 105, wxEmptyString, wxDefaultPosition, - wxSize(-1,125), wxTE_MULTILINE|wxFULL_REPAINT_ON_RESIZE ); - m_logWin->SetEditable(false); - wxLogTextCtrl* logger = new wxLogTextCtrl( m_logWin ); - m_logOld = logger->SetActiveTarget( logger ); - logger->SetTimestamp( NULL ); + m_logWin = new wxTextCtrl(panel, 105, wxEmptyString, + wxDefaultPosition, + wxSize(-1, 125), + wxTE_MULTILINE); + wxLogTextCtrl* logger = new wxLogTextCtrl(m_logWin); + m_logOld = logger->SetActiveTarget(logger); + logger->DisableTimestamp(); topSizer = new wxBoxSizer( wxVERTICAL ); @@ -719,15 +751,25 @@ MyFrame::MyFrame(const wxString& title) // rowSizer = new wxBoxSizer( wxHORIZONTAL ); - rowSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("List View wxComboCtrl:")), 1, - wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 ); - rowSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("Tree Ctrl wxComboControl:")), 1, + rowSizer->Add( new wxStaticText(panel, + wxID_ANY, + "List View wxComboCtrl (custom animation):"), + 1, wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 ); + rowSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("Tree Ctrl wxComboCtrl:")), 1, wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 ); colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 ); rowSizer = new wxBoxSizer( wxHORIZONTAL ); - cc = new wxComboCtrl(panel,2,wxEmptyString, - wxDefaultPosition, wxDefaultSize); + cc = new wxComboCtrlWithCustomPopupAnim(); + + // Let's set a custom style for the contained wxTextCtrl. We need to + // use two-step creation for it to work properly. + cc->SetTextCtrlStyle(wxTE_RIGHT); + + cc->Create(panel, wxID_ANY, wxEmptyString); + + // Make sure we use popup that allows focusing the listview. + cc->UseAltPopupWindow(); cc->SetPopupMinWidth(300); @@ -749,6 +791,9 @@ MyFrame::MyFrame(const wxString& title) gcc = new wxGenericComboCtrl(panel,wxID_ANY,wxEmptyString, wxDefaultPosition, wxDefaultSize); + // Make sure we use popup that allows focusing the treectrl. + gcc->UseAltPopupWindow(); + // Set popup interface right away, otherwise some of the calls // below may fail TreeCtrlComboPopup* tcPopup = new TreeCtrlComboPopup(); @@ -817,7 +862,7 @@ MyFrame::MyFrame(const wxString& title) wxImage imgPressed(wxT("dropbutp.png")); wxImage imgHover(wxT("dropbuth.png")); - if ( imgNormal.Ok() && imgPressed.Ok() && imgHover.Ok() ) + if ( imgNormal.IsOk() && imgPressed.IsOk() && imgHover.IsOk() ) { wxBitmap bmpNormal(imgNormal); wxBitmap bmpPressed(imgPressed); @@ -869,13 +914,19 @@ MyFrame::MyFrame(const wxString& title) topRowSizer->Add( colSizer, 1, wxALL, 2 ); - topRowSizer->Add( m_logWin, 1, wxEXPAND|wxALL, 5 ); + colSizer = new wxBoxSizer( wxVERTICAL ); + + colSizer->AddSpacer(8); + colSizer->Add( new wxStaticText(panel, wxID_ANY, wxT("Log Messages:")), 0, wxTOP|wxLEFT, 3 ); + colSizer->Add( m_logWin, 1, wxEXPAND|wxALL, 3 ); + + topRowSizer->Add( colSizer, 1, wxEXPAND|wxALL, 2 ); topSizer->Add( topRowSizer, 1, wxEXPAND ); panel->SetSizer( topSizer ); topSizer->SetSizeHints( panel ); - SetSize(740,400); + Fit(); Centre(); } @@ -887,10 +938,19 @@ void MyFrame::OnComboBoxUpdate( wxCommandEvent& event ) if ( !event.GetEventObject()->IsKindOf(CLASSINFO(wxComboCtrl)) ) return; - if ( event.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED ) + if ( event.GetEventType() == wxEVT_COMBOBOX ) + { wxLogDebug(wxT("EVT_COMBOBOX(id=%i,selection=%i)"),event.GetId(),event.GetSelection()); - else if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED ) + } + else if ( event.GetEventType() == wxEVT_TEXT ) + { wxLogDebug(wxT("EVT_TEXT(id=%i,string=\"%s\")"),event.GetId(),event.GetString().c_str()); + } + else if ( event.GetEventType() == wxEVT_TEXT_ENTER ) + { + wxLogDebug("EVT_TEXT_ENTER(id=%i,string=\"%s\")", + event.GetId(), event.GetString().c_str()); + } } void MyFrame::OnShowComparison( wxCommandEvent& WXUNUSED(event) ) @@ -920,7 +980,8 @@ void MyFrame::OnShowComparison( wxCommandEvent& WXUNUSED(event) ) groupSizer = new wxStaticBoxSizer(new wxStaticBox(dlg,wxID_ANY,wxT(" wxOwnerDrawnComboBox ")), wxVERTICAL); - groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Writable, sorted:")), 0, + groupSizer->Add( new wxStaticText(dlg, wxID_ANY, + wxT("Writable, with margins, sorted:")), 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxEXPAND, border ); odc = new wxOwnerDrawnComboBox(dlg,wxID_ANY,wxEmptyString, @@ -932,12 +993,14 @@ void MyFrame::OnShowComparison( wxCommandEvent& WXUNUSED(event) ) odc->Append(wxT("H - Appended Item")); // test sorting in append odc->SetValue(wxT("Dot Dash")); - - groupSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border ); + odc->SetMargins(15, 10); + groupSizer->Add( odc, 0, wxALIGN_CENTER_VERTICAL|wxALL, border ); + groupSizer->AddStretchSpacer(); // // Readonly ODComboBox - groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Read-only:")), 0, + groupSizer->Add( new wxStaticText(dlg, wxID_ANY, + wxT("Read-only, big font:")), 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, border ); odc = new wxOwnerDrawnComboBox(dlg,wxID_ANY,wxEmptyString, @@ -946,10 +1009,12 @@ void MyFrame::OnShowComparison( wxCommandEvent& WXUNUSED(event) ) wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY ); + odc->SetFont(odc->GetFont().Scale(1.5)); odc->SetValue(wxT("Dot Dash")); odc->SetText(wxT("Dot Dash (Testing SetText)")); - groupSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border ); + groupSizer->Add( odc, 0, wxALL, border ); + groupSizer->AddStretchSpacer(); // // Disabled ODComboBox @@ -965,7 +1030,7 @@ void MyFrame::OnShowComparison( wxCommandEvent& WXUNUSED(event) ) odc->SetValue(wxT("Dot Dash")); odc->Enable(false); - groupSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border ); + groupSizer->Add( odc, 3, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border ); rowSizer->Add( groupSizer, 1, wxEXPAND|wxALL, border ); @@ -976,7 +1041,8 @@ void MyFrame::OnShowComparison( wxCommandEvent& WXUNUSED(event) ) // // wxComboBox // - groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Writable, sorted:")), 0, + groupSizer->Add( new wxStaticText(dlg,wxID_ANY, + wxT("Writable, with margins, sorted:")), 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxEXPAND, border ); cb = new wxComboBox(dlg,wxID_ANY,wxEmptyString, @@ -988,12 +1054,14 @@ void MyFrame::OnShowComparison( wxCommandEvent& WXUNUSED(event) ) cb->Append(wxT("H - Appended Item")); // test sorting in append cb->SetValue(wxT("Dot Dash")); - - groupSizer->Add( cb, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border ); + cb->SetMargins(15, 10); + groupSizer->Add( cb, 0, wxALIGN_CENTER_VERTICAL|wxALL, border ); + groupSizer->AddStretchSpacer(); // // Readonly wxComboBox - groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Read-only:")), 0, + groupSizer->Add( new wxStaticText(dlg, wxID_ANY, + wxT("Read-only, big font:")), 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, border ); cb = new wxComboBox(dlg,wxID_ANY,wxEmptyString, @@ -1002,9 +1070,11 @@ void MyFrame::OnShowComparison( wxCommandEvent& WXUNUSED(event) ) wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY ); + cb->SetFont(cb->GetFont().Scale(1.5)); cb->SetValue(wxT("Dot Dash")); - groupSizer->Add( cb, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border ); + groupSizer->Add( cb, 0, wxALL, border ); + groupSizer->AddStretchSpacer(); // // Disabled wxComboBox @@ -1020,11 +1090,11 @@ void MyFrame::OnShowComparison( wxCommandEvent& WXUNUSED(event) ) cb->SetValue(wxT("Dot Dash")); cb->Enable(false); - groupSizer->Add( cb, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border ); + groupSizer->Add( cb, 3, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border ); rowSizer->Add( groupSizer, 1, wxEXPAND|wxALL, border ); - colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, border ); + colSizer->Add( rowSizer, 1, wxEXPAND|wxALL, border ); dlg->SetSizer( colSizer ); colSizer->SetSizeHints( dlg ); @@ -1048,14 +1118,38 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { wxMessageBox(wxString::Format( - _T("Welcome to %s!\n") - _T("\n") - _T("This is the wxWidgets wxComboCtrl and wxOwnerDrawnComboBox sample\n") - _T("running under %s."), + wxT("Welcome to %s!\n") + wxT("\n") + wxT("This is the wxWidgets wxComboCtrl and wxOwnerDrawnComboBox sample\n") + wxT("running under %s."), wxVERSION_STRING, wxGetOsDescription().c_str() ), - _T("About wxComboCtrl sample"), + wxT("About wxComboCtrl sample"), wxOK | wxICON_INFORMATION, this); } + +void MyFrame::OnIdle(wxIdleEvent& event) +{ + // This code is useful for debugging focus problems + // (which are plentiful when dealing with popup windows). +#if 0 + static wxWindow* lastFocus = (wxWindow*) NULL; + + wxWindow* curFocus = ::wxWindow::FindFocus(); + + if ( curFocus != lastFocus ) + { + const wxChar* className = wxT(""); + if ( curFocus ) + className = curFocus->GetClassInfo()->GetClassName(); + lastFocus = curFocus; + wxLogDebug( wxT("FOCUSED: %s %X"), + className, + (unsigned int)curFocus); + } +#endif + + event.Skip(); +}