]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/combo/combo.cpp
extract event handler body in a separate function instead of using a hack to call...
[wxWidgets.git] / samples / combo / combo.cpp
index 6b5ced6c6ea24e81c3b7803fdf65a97ca77aa3b8..fd5abd249d81a89d16f61e9cec98e3bae833468a 100644 (file)
@@ -83,6 +83,11 @@ public:
     // log wxComboCtrl events
     void OnComboBoxUpdate( wxCommandEvent& event );
 
     // log wxComboCtrl events
     void OnComboBoxUpdate( wxCommandEvent& event );
 
+    void OnIdle( wxIdleEvent& event );
+
+
+    wxCheckBox*     m_cbUseAnim;
+
 protected:
     wxTextCtrl*     m_logWin;
     wxLog*          m_logOld;
 protected:
     wxTextCtrl*     m_logWin;
     wxLog*          m_logOld;
@@ -127,6 +132,8 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(ComboControl_Compare,  MyFrame::OnShowComparison)
     EVT_MENU(ComboControl_Quit,     MyFrame::OnQuit)
     EVT_MENU(ComboControl_About,    MyFrame::OnAbout)
     EVT_MENU(ComboControl_Compare,  MyFrame::OnShowComparison)
     EVT_MENU(ComboControl_Quit,     MyFrame::OnQuit)
     EVT_MENU(ComboControl_About,    MyFrame::OnAbout)
+
+    EVT_IDLE(MyFrame::OnIdle)
 END_EVENT_TABLE()
 
 // Create a new application object: this macro will allow wxWidgets to create
 END_EVENT_TABLE()
 
 // Create a new application object: this macro will allow wxWidgets to create
@@ -147,6 +154,9 @@ IMPLEMENT_APP(MyApp)
 // 'Main program' equivalent: the program execution "starts" here
 bool MyApp::OnInit()
 {
 // '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"));
 
     // create the main application window
     MyFrame *frame = new MyFrame(_T("wxComboCtrl and wxOwnerDrawnComboBox Sample"));
 
@@ -228,8 +238,9 @@ public:
                                    int item, int flags ) const
     {
 
                                    int item, int flags ) const
     {
 
-        // If item is selected or even, use the default rendering.
-        if ( GetVListBoxComboPopup()->IsCurrent((size_t)item) ||
+        // If item is selected or even, or we are painting the
+        // combo control itself, use the default rendering.
+        if ( (flags & (wxODCB_PAINTING_CONTROL|wxODCB_PAINTING_SELECTED)) ||
              (item & 1) == 0 )
         {
             wxOwnerDrawnComboBox::OnDrawBackground(dc,rect,item,flags);
              (item & 1) == 0 )
         {
             wxOwnerDrawnComboBox::OnDrawBackground(dc,rect,item,flags);
@@ -372,9 +383,7 @@ public:
     {
         return wxTreeCtrl::Create(parent,1,
                                   wxPoint(0,0),wxDefaultSize,
     {
         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()
     }
 
     virtual void OnShow()
@@ -485,6 +494,130 @@ BEGIN_EVENT_TABLE(TreeCtrlComboPopup, wxTreeCtrl)
     EVT_LEFT_DOWN(TreeCtrlComboPopup::OnMouseClick)
 END_EVENT_TABLE()
 
     EVT_LEFT_DOWN(TreeCtrlComboPopup::OnMouseClick)
 END_EVENT_TABLE()
 
+// ----------------------------------------------------------------------------
+// wxComboCtrl with custom popup animation. We use EVT_TIMER, which is quite
+// safe, but requires much more can than doing it in a single function (ie.
+// AnimateShow) and using combination of wxSleep and wxSafeYield.
+// ----------------------------------------------------------------------------
+
+#if wxUSE_TIMER
+
+#define CUSTOM_COMBOBOX_ANIMATION_DURATION  200  // In milliseconds
+
+#include "wx/timer.h"
+
+class wxComboCtrlWithCustomPopupAnim : public wxComboCtrl
+{
+public:
+
+    virtual bool AnimateShow( const wxRect& rect, int flags )
+    {
+        MyFrame* myFrame = (MyFrame*) ::wxGetTopLevelParent(this);
+
+        if ( !myFrame->m_cbUseAnim->GetValue() )
+            return true;
+
+        m_animStart = ::wxGetLocalTimeMillis();
+        m_animRect = rect;
+        m_animFlags = flags;
+
+        wxScreenDC dc;
+
+        wxBitmap bitmap( rect.width, rect.height, -1 );
+        wxMemoryDC memdc( bitmap );
+        memdc.Blit( 0, 0, rect.width, rect.height, &dc, rect.x, rect.y );
+        memdc.SelectObject(wxNullBitmap); 
+        m_animBackBitmap = bitmap;
+
+        m_animTimer.SetOwner( this, wxID_ANY );
+        m_animTimer.Start( 10, wxTIMER_CONTINUOUS );
+
+        DoOnTimer();
+        return false;
+    }
+
+private:
+    void OnTimerEvent( wxTimerEvent& WXUNUSED(event) )
+    {
+        DoOnTimer();
+    }
+
+    void DoOnTimer()
+    {
+        bool stopTimer = false;
+
+        wxWindow* popup = GetPopupControl()->GetControl();
+        wxScreenDC dc;
+        const wxRect& rect = m_animRect;
+
+        // Popup was hidden before it was fully shown?
+        if ( IsPopupWindowState(Hidden) )
+        {
+            stopTimer = true;
+        }
+        else
+        {
+            wxLongLong t = ::wxGetLocalTimeMillis();
+
+            int pos = (int) (t-m_animStart).GetLo();
+            if ( pos < CUSTOM_COMBOBOX_ANIMATION_DURATION )
+            {
+                //
+                // Actual animation happens here
+                //
+                int width = rect.width;
+                int height = rect.height;
+
+                int center_x = rect.x + (width/2);
+                int center_y = rect.y + (height/2);
+
+                double d_height = (double) height;
+
+                dc.SetPen( *wxBLACK_PEN );
+                dc.SetBrush( *wxTRANSPARENT_BRUSH );
+
+                int w = (((pos*256)/CUSTOM_COMBOBOX_ANIMATION_DURATION)*width)/256;
+
+                double ratio = ((double)w / (double)width);
+                int h = (int)(d_height * ratio);
+                dc.DrawBitmap( m_animBackBitmap, rect.x, rect.y );
+                dc.DrawRectangle( center_x - w/2, center_y - h/2, w, h );
+            }
+            else
+            {
+                stopTimer = true;
+            }
+        }
+
+        if ( stopTimer )
+        {
+            dc.DrawBitmap( m_animBackBitmap, rect.x, rect.y );
+            popup->Move( 0, 0 );
+            m_animTimer.Stop();
+            DoShowPopup( m_animRect, m_animFlags );
+        }
+    }
+
+    // Popup animation related
+    wxLongLong  m_animStart;
+    wxTimer     m_animTimer;
+    wxRect      m_animRect;
+    wxBitmap    m_animBackBitmap;
+    int         m_animFlags;
+
+    DECLARE_EVENT_TABLE()
+};
+
+BEGIN_EVENT_TABLE(wxComboCtrlWithCustomPopupAnim, wxComboCtrl)
+    EVT_TIMER(wxID_ANY, wxComboCtrlWithCustomPopupAnim::OnTimerEvent)
+END_EVENT_TABLE()
+
+#else
+
+#define wxComboCtrlWithCustomPopupAnim wxComboCtrl
+
+#endif
+
 // ----------------------------------------------------------------------------
 // wxComboCtrl with entirely custom button action (opens file dialog)
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // wxComboCtrl with entirely custom button action (opens file dialog)
 // ----------------------------------------------------------------------------
@@ -557,6 +690,11 @@ public:
         }
     }
 
         }
     }
 
+    // Implement empty DoSetPopupControl to prevent assertion failure.
+    virtual void DoSetPopupControl(wxComboPopup* WXUNUSED(popup))
+    {
+    }
+
 private:
     void Init()
     {
 private:
     void Init()
     {
@@ -610,7 +748,7 @@ MyFrame::MyFrame(const wxString& title)
     m_logWin->SetEditable(false);
     wxLogTextCtrl* logger = new wxLogTextCtrl( m_logWin );
     m_logOld = logger->SetActiveTarget( logger );
     m_logWin->SetEditable(false);
     wxLogTextCtrl* logger = new wxLogTextCtrl( m_logWin );
     m_logOld = logger->SetActiveTarget( logger );
-    logger->SetTimestamp( NULL );
+    logger->DisableTimestamp();
 
 
     topSizer = new wxBoxSizer( wxVERTICAL );
 
 
     topSizer = new wxBoxSizer( wxVERTICAL );
@@ -621,7 +759,7 @@ MyFrame::MyFrame(const wxString& title)
 
 
     wxComboCtrl* cc;
 
 
     wxComboCtrl* cc;
-    wxGenericComboControl* gcc;
+    wxGenericComboCtrl* gcc;
     wxOwnerDrawnComboBox* odc;
 
     // Create common strings array
     wxOwnerDrawnComboBox* odc;
 
     // Create common strings array
@@ -694,8 +832,11 @@ MyFrame::MyFrame(const wxString& title)
 
 
     odc->SetSelection(0);
 
 
     odc->SetSelection(0);
-    odc->SetButtonPosition(-2, // width adjustment
-                           -6, // height adjustment
+
+    // Use button size that is slightly smaller than the default.
+    wxSize butSize = odc->GetButtonSize();
+    odc->SetButtonPosition(butSize.x - 2, // button width
+                           butSize.y - 6, // button height
                            wxLEFT, // side
                            2 // horizontal spacing
                           );
                            wxLEFT, // side
                            2 // horizontal spacing
                           );
@@ -717,8 +858,11 @@ MyFrame::MyFrame(const wxString& title)
     colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
 
     rowSizer = new wxBoxSizer( wxHORIZONTAL );
     colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
 
     rowSizer = new wxBoxSizer( wxHORIZONTAL );
-    cc = new wxComboCtrl(panel,2,wxEmptyString,
-                         wxDefaultPosition, wxDefaultSize);
+    cc = new wxComboCtrlWithCustomPopupAnim();
+    cc->Create(panel, wxID_ANY, wxEmptyString);
+
+    // Make sure we use popup that allows focusing the listview.
+    cc->UseAltPopupWindow();
 
     cc->SetPopupMinWidth(300);
 
 
     cc->SetPopupMinWidth(300);
 
@@ -736,9 +880,12 @@ MyFrame::MyFrame(const wxString& title)
     // Tree Ctrl wxComboCtrl
     //
 
     // Tree Ctrl wxComboCtrl
     //
 
-    // Note that we test that wxGenericComboControl works
-    gcc = new wxGenericComboControl(panel,wxID_ANY,wxEmptyString,
-                                    wxDefaultPosition, wxDefaultSize);
+    // Note that we test that wxGenericComboCtrl works
+    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
 
     // Set popup interface right away, otherwise some of the calls
     // below may fail
@@ -764,8 +911,8 @@ MyFrame::MyFrame(const wxString& title)
     gcc->SetValue(wxT("Subitem 05"));
 
     // Move button to left - it makes more sense for a tree ctrl
     gcc->SetValue(wxT("Subitem 05"));
 
     // Move button to left - it makes more sense for a tree ctrl
-    gcc->SetButtonPosition(0, // width adjustment
-                           0, // height adjustment
+    gcc->SetButtonPosition(-1, // button width
+                           -1, // button height
                            wxLEFT, // side
                            0 // horizontal spacing
                           );
                            wxLEFT, // side
                            0 // horizontal spacing
                           );
@@ -808,7 +955,7 @@ MyFrame::MyFrame(const wxString& title)
     wxImage imgPressed(wxT("dropbutp.png"));
     wxImage imgHover(wxT("dropbuth.png"));
 
     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);
     {
         wxBitmap bmpNormal(imgNormal);
         wxBitmap bmpPressed(imgPressed);
@@ -860,7 +1007,23 @@ MyFrame::MyFrame(const wxString& title)
 
     topRowSizer->Add( colSizer, 1, wxALL, 2 );
 
 
     topRowSizer->Add( colSizer, 1, wxALL, 2 );
 
-    topRowSizer->Add( m_logWin, 1, wxEXPAND|wxALL, 5 );
+    colSizer = new wxBoxSizer( wxVERTICAL );
+
+    wxStaticBoxSizer* sbSizer = new wxStaticBoxSizer( new wxStaticBox(panel,
+                                                                      wxID_ANY,
+                                                                      wxT("Options")),
+                                                      wxVERTICAL );
+
+    m_cbUseAnim = new wxCheckBox(panel, wxID_ANY, wxT("Custom popup animation for ListView wxComboCtrl"));
+    m_cbUseAnim->SetValue(true);
+    sbSizer->Add( m_cbUseAnim, 0, wxALL, 3 );
+
+    colSizer->Add( sbSizer, 0, wxEXPAND|wxALL, 3 );
+    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->Add( topRowSizer, 1, wxEXPAND );
 
     panel->SetSizer( topSizer );
@@ -1050,3 +1213,27 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
                  wxOK | wxICON_INFORMATION,
                  this);
 }
                  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("<none>");
+        if ( curFocus )
+            className = curFocus->GetClassInfo()->GetClassName();
+        lastFocus = curFocus;
+        wxLogDebug( wxT("FOCUSED: %s %X"),
+            className,
+            (unsigned int)curFocus);
+    }
+#endif
+
+    event.Skip();
+}