]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/combo/combo.cpp
Use template class instead of template function in wxVectorSort().
[wxWidgets.git] / samples / combo / combo.cpp
index 785a1040388fef8766ce110308a0958cfaaa80d8..288fbc27e739fc2cd21eaab4021a468d9def1d9b 100644 (file)
@@ -154,8 +154,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)
@@ -380,9 +383,7 @@ public:
     {
         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()
@@ -494,65 +495,129 @@ BEGIN_EVENT_TABLE(TreeCtrlComboPopup, wxTreeCtrl)
 END_EVENT_TABLE()
 
 // ----------------------------------------------------------------------------
-// wxComboCtrl with custom popup animation
+// 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 WXUNUSED(flags) )
+    virtual bool AnimateShow( const wxRect& rect, int flags )
     {
         MyFrame* myFrame = (MyFrame*) ::wxGetTopLevelParent(this);
 
         if ( !myFrame->m_cbUseAnim->GetValue() )
             return true;
 
-        int width = rect.width;
-        int height = rect.height;
-        wxBitmap bitmap( width, height, -1 );
+        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, width, height, &dc, rect.x, rect.y );
+        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 );
 
-        wxLongLong tStart = ::wxGetLocalTimeMillis();
-        const int delay = 300;
-        const int resolution = 10;
+        DoOnTimer();
+        return false;
+    }
+
+private:
+    void OnTimerEvent( wxTimerEvent& WXUNUSED(event) )
+    {
+        DoOnTimer();
+    }
 
-        int center_x = rect.x + (width/2);
-        int center_y = rect.y + (height/2);
+    void DoOnTimer()
+    {
+        bool stopTimer = false;
 
-        double d_height = (double) height;
+        wxWindow* popup = GetPopupControl()->GetControl();
+        wxScreenDC dc;
+        const wxRect& rect = m_animRect;
 
-        dc.SetPen( *wxBLACK_PEN );
-        dc.SetBrush( *wxTRANSPARENT_BRUSH );
-        for (;;)
+        // Popup was hidden before it was fully shown?
+        if ( IsPopupWindowState(Hidden) )
+        {
+            stopTimer = true;
+        }
+        else
         {
             wxLongLong t = ::wxGetLocalTimeMillis();
-            int pos = (int) (t-tStart).GetLo();
-            if ( pos > delay )
-                break;
 
-            int w = (((pos*256)/delay)*width)/256;
+            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;
 
-            double ratio = ((double)w / (double)width);
-            int h = (int)(d_height * ratio);
-            dc.DrawRectangle( center_x - w/2, center_y - h/2, w, h );
-            wxMilliSleep( resolution );
-            wxYield();
-            dc.DrawBitmap( bitmap, rect.x, rect.y );
+                dc.SetPen( *wxBLACK_PEN );
+                dc.SetBrush( *wxTRANSPARENT_BRUSH );
 
-            if ( IsPopupWindowState(Hidden) )
-                return true;
+                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;
+            }
         }
 
-        return true;
+        if ( stopTimer )
+        {
+            dc.DrawBitmap( m_animBackBitmap, rect.x, rect.y );
+            popup->Move( 0, 0 );
+            m_animTimer.Stop();
+            DoShowPopup( m_animRect, m_animFlags );
+        }
     }
 
-protected:
+    // 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)
 // ----------------------------------------------------------------------------
@@ -659,17 +724,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(ComboControl_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(ComboControl_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(ComboControl_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);
@@ -678,12 +743,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 );
@@ -977,9 +1043,13 @@ void MyFrame::OnComboBoxUpdate( wxCommandEvent& event )
         return;
 
     if ( event.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED )
+    {
         wxLogDebug(wxT("EVT_COMBOBOX(id=%i,selection=%i)"),event.GetId(),event.GetSelection());
+    }
     else if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED )
+    {
         wxLogDebug(wxT("EVT_TEXT(id=%i,string=\"%s\")"),event.GetId(),event.GetString().c_str());
+    }
 }
 
 void MyFrame::OnShowComparison( wxCommandEvent& WXUNUSED(event) )
@@ -1137,14 +1207,14 @@ 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);
 }