]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/widgets/bmpcombobox.cpp
Use wxWindowUpdateLocker to speed up updates of the static widgets sample page.
[wxWidgets.git] / samples / widgets / bmpcombobox.cpp
index 59b06e47edf1b033fd4e52dfdb07aa95eca9d67e..d65f82c2598f11a99120c44be566568d5d011b86 100644 (file)
@@ -6,7 +6,7 @@
 // Created:     Sep-01-2006
 // Id:          $Id$
 // Copyright:   (c) 2006 Jaakko Salli
-// License:     wxWindows license
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
@@ -87,6 +87,13 @@ enum
     BitmapComboBoxPage_ContainerTests
 };
 
+// kinds of comboboxes
+enum
+{
+    ComboKind_Default,
+    ComboKind_Simple,
+    ComboKind_DropDown
+};
 
 // ----------------------------------------------------------------------------
 // BitmapComboBoxWidgetsPage
@@ -121,6 +128,8 @@ protected:
     void OnButtonAddMany(wxCommandEvent& event);
 
     void OnComboBox(wxCommandEvent& event);
+    void OnDropDown(wxCommandEvent& event);
+    void OnCloseUp(wxCommandEvent& event);
     void OnComboText(wxCommandEvent& event);
 
     void OnCheckOrRadioBox(wxCommandEvent& event);
@@ -130,8 +139,6 @@ protected:
     void OnTextButtonAll(wxCommandEvent& event);
 
     void OnUpdateUIInsert(wxUpdateUIEvent& event);
-    void OnUpdateUIAddSeveral(wxUpdateUIEvent& event);
-    void OnUpdateUIAddSeveralWithImages(wxUpdateUIEvent& event);
     void OnUpdateUIClearButton(wxUpdateUIEvent& event);
     void OnUpdateUIDeleteButton(wxUpdateUIEvent& event);
     void OnUpdateUIItemManipulator(wxUpdateUIEvent& event);
@@ -161,6 +168,9 @@ protected:
     // the controls
     // ------------
 
+    // the sel mode radiobox
+    wxRadioBox *m_radioKind;
+
     // the checkboxes for styles
     wxCheckBox *m_chkSort,
                *m_chkReadonly;
@@ -206,8 +216,6 @@ BEGIN_EVENT_TABLE(BitmapComboBoxWidgetsPage, WidgetsPage)
     EVT_UPDATE_UI(BitmapComboBoxPage_Reset, BitmapComboBoxWidgetsPage::OnUpdateUIResetButton)
     EVT_UPDATE_UI(BitmapComboBoxPage_Insert, BitmapComboBoxWidgetsPage::OnUpdateUIInsert)
     EVT_UPDATE_UI(BitmapComboBoxPage_LoadFromFile, BitmapComboBoxWidgetsPage::OnUpdateUIInsert)
-    EVT_UPDATE_UI(BitmapComboBoxPage_AddSeveral, BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveral)
-    EVT_UPDATE_UI(BitmapComboBoxPage_AddSeveralWithImages, BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveralWithImages)
     EVT_UPDATE_UI(BitmapComboBoxPage_Clear, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton)
     EVT_UPDATE_UI(BitmapComboBoxPage_DeleteText, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton)
     EVT_UPDATE_UI(BitmapComboBoxPage_Delete, BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton)
@@ -215,6 +223,8 @@ BEGIN_EVENT_TABLE(BitmapComboBoxWidgetsPage, WidgetsPage)
     EVT_UPDATE_UI(BitmapComboBoxPage_SetFromFile, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator)
     EVT_UPDATE_UI(BitmapComboBoxPage_DeleteSel, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator)
 
+    EVT_COMBOBOX_DROPDOWN(BitmapComboBoxPage_Combo, BitmapComboBoxWidgetsPage::OnDropDown)
+    EVT_COMBOBOX_CLOSEUP(BitmapComboBoxPage_Combo, BitmapComboBoxWidgetsPage::OnCloseUp)
     EVT_COMBOBOX(BitmapComboBoxPage_Combo, BitmapComboBoxWidgetsPage::OnComboBox)
     EVT_TEXT(BitmapComboBoxPage_Combo, BitmapComboBoxWidgetsPage::OnComboText)
     EVT_TEXT_ENTER(BitmapComboBoxPage_Combo, BitmapComboBoxWidgetsPage::OnComboText)
@@ -227,10 +237,14 @@ END_EVENT_TABLE()
 // implementation
 // ============================================================================
 
+#if defined(__WXMSW__) || defined(__WXGTK__)
+    #define NATIVE_OR_GENERIC_CTRLS     NATIVE_CTRLS
+#else
+    #define NATIVE_OR_GENERIC_CTRLS     GENERIC_CTRLS
+#endif
 
-
-IMPLEMENT_WIDGETS_PAGE(BitmapComboBoxWidgetsPage, _T("BitmapCombobox"),
-                       GENERIC_CTRLS | WITH_ITEMS_CTRLS | COMBO_CTRLS
+IMPLEMENT_WIDGETS_PAGE(BitmapComboBoxWidgetsPage, wxT("BitmapCombobox"),
+                       NATIVE_OR_GENERIC_CTRLS | WITH_ITEMS_CTRLS | COMBO_CTRLS
                        );
 
 
@@ -240,10 +254,15 @@ BitmapComboBoxWidgetsPage::BitmapComboBoxWidgetsPage(WidgetsBookCtrl *book,
 {
     // init everything
     m_chkSort =
-    m_chkReadonly = (wxCheckBox *)NULL;
+    m_chkReadonly = NULL;
+
+    m_combobox = NULL;
+    m_sizerCombo = NULL;
 
-    m_combobox = (wxBitmapComboBox *)NULL;
-    m_sizerCombo = (wxSizer *)NULL;
+    m_textInsert =
+    m_textChangeHeight =
+    m_textChange =
+    m_textDelete = NULL;
 }
 
 // create a sizer containing a label and a small text ctrl
@@ -273,7 +292,6 @@ void BitmapComboBoxWidgetsPage::CreateContent()
        miscellaneous combobox operations and the pane containing the combobox
        itself to the right
     */
-    //wxTextCtrl *text;
     wxSizer *sizerRow;
 
     wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
@@ -281,24 +299,39 @@ void BitmapComboBoxWidgetsPage::CreateContent()
     wxSizer *sizerLeft = new wxBoxSizer(wxVERTICAL);
 
     // left pane - style box
-    wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
+    wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style"));
+
+
+    // should be in sync with ComboKind_XXX values
+    static const wxString kinds[] =
+    {
+        wxT("default"),
+        wxT("simple"),
+        wxT("drop down"),
+    };
+
+    m_radioKind = new wxRadioBox(this, wxID_ANY, wxT("Combobox &kind:"),
+                                 wxDefaultPosition, wxDefaultSize,
+                                 WXSIZEOF(kinds), kinds,
+                                 1, wxRA_SPECIFY_COLS);
 
     wxSizer *sizerStyle = new wxStaticBoxSizer(box, wxVERTICAL);
 
-    m_chkSort = CreateCheckBoxAndAddToSizer(sizerStyle, _T("&Sort items"));
-    m_chkReadonly = CreateCheckBoxAndAddToSizer(sizerStyle, _T("&Read only"));
+    m_chkSort = CreateCheckBoxAndAddToSizer(sizerStyle, wxT("&Sort items"));
+    m_chkReadonly = CreateCheckBoxAndAddToSizer(sizerStyle, wxT("&Read only"));
 
-    wxButton *btn = new wxButton(this, BitmapComboBoxPage_Reset, _T("&Reset"));
+    wxButton *btn = new wxButton(this, BitmapComboBoxPage_Reset, wxT("&Reset"));
     sizerStyle->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 3);
 
     sizerLeft->Add(sizerStyle, 0, wxGROW | wxALIGN_CENTRE_HORIZONTAL);
+    sizerLeft->Add(m_radioKind, 0, wxGROW | wxALL, 5);
 
     // left pane - other options box
-    box = new wxStaticBox(this, wxID_ANY, _T("Demo options"));
+    box = new wxStaticBox(this, wxID_ANY, wxT("Demo options"));
 
     wxSizer *sizerOptions = new wxStaticBoxSizer(box, wxVERTICAL);
 
-    sizerRow = CreateSizerWithSmallTextAndLabel(_T("Control &height:"),
+    sizerRow = CreateSizerWithSmallTextAndLabel(wxT("Control &height:"),
                                                 BitmapComboBoxPage_ChangeHeight,
                                                 &m_textChangeHeight);
     m_textChangeHeight->SetSize(20, wxDefaultCoord);
@@ -308,42 +341,42 @@ void BitmapComboBoxWidgetsPage::CreateContent()
 
     // middle pane
     wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
-        _T("&Change wxBitmapComboBox contents"));
+        wxT("&Change wxBitmapComboBox contents"));
     wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
 
-    btn = new wxButton(this, BitmapComboBoxPage_ContainerTests, _T("Run &tests"));
+    btn = new wxButton(this, BitmapComboBoxPage_ContainerTests, wxT("Run &tests"));
     sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
 
 #if wxUSE_IMAGE
-    btn = new wxButton(this, BitmapComboBoxPage_AddWidgetIcons, _T("Add &widget icons"));
+    btn = new wxButton(this, BitmapComboBoxPage_AddWidgetIcons, wxT("Add &widget icons"));
     sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
 
-    btn = new wxButton(this, BitmapComboBoxPage_LoadFromFile, _T("Insert image from &file"));
+    btn = new wxButton(this, BitmapComboBoxPage_LoadFromFile, wxT("Insert image from &file"));
     sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
 
-    btn = new wxButton(this, BitmapComboBoxPage_SetFromFile, _T("&Set image from file"));
+    btn = new wxButton(this, BitmapComboBoxPage_SetFromFile, wxT("&Set image from file"));
     sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
 #endif
 
-    btn = new wxButton(this, BitmapComboBoxPage_AddSeveralWithImages, _T("A&ppend a few strings with images"));
+    btn = new wxButton(this, BitmapComboBoxPage_AddSeveralWithImages, wxT("A&ppend a few strings with images"));
     sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
 
-    btn = new wxButton(this, BitmapComboBoxPage_AddSeveral, _T("Append a &few strings"));
+    btn = new wxButton(this, BitmapComboBoxPage_AddSeveral, wxT("Append a &few strings"));
     sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
 
-    btn = new wxButton(this, BitmapComboBoxPage_AddMany, _T("Append &many strings"));
+    btn = new wxButton(this, BitmapComboBoxPage_AddMany, wxT("Append &many strings"));
     sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
 
     sizerRow = CreateSizerWithTextAndButton(BitmapComboBoxPage_Delete,
-                                            _T("&Delete this item"),
+                                            wxT("&Delete this item"),
                                             BitmapComboBoxPage_DeleteText,
                                             &m_textDelete);
     sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
 
-    btn = new wxButton(this, BitmapComboBoxPage_DeleteSel, _T("Delete &selection"));
+    btn = new wxButton(this, BitmapComboBoxPage_DeleteSel, wxT("Delete &selection"));
     sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
 
-    btn = new wxButton(this, BitmapComboBoxPage_Clear, _T("&Clear"));
+    btn = new wxButton(this, BitmapComboBoxPage_Clear, wxT("&Clear"));
     sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
 
 #if wxUSE_IMAGE
@@ -397,6 +430,24 @@ void BitmapComboBoxWidgetsPage::CreateCombo()
     if ( m_chkReadonly->GetValue() )
         flags |= wxCB_READONLY;
 
+    switch ( m_radioKind->GetSelection() )
+    {
+        default:
+            wxFAIL_MSG( wxT("unknown combo kind") );
+            // fall through
+
+        case ComboKind_Default:
+            break;
+
+        case ComboKind_Simple:
+            flags |= wxCB_SIMPLE;
+            break;
+
+        case ComboKind_DropDown:
+            flags = wxCB_DROPDOWN;
+            break;
+    }
+
     wxArrayString items;
     wxArrayPtrVoid bitmaps;
     if ( m_combobox )
@@ -405,7 +456,8 @@ void BitmapComboBoxWidgetsPage::CreateCombo()
         for ( unsigned int n = 0; n < count; n++ )
         {
             items.Add(m_combobox->GetString(n));
-            bitmaps.Add(new wxBitmap(m_combobox->GetItemBitmap(n)));
+            wxBitmap bmp = m_combobox->GetItemBitmap(n);
+            bitmaps.Add(new wxBitmap(bmp));
         }
 
         m_sizerCombo->Detach( m_combobox );
@@ -434,7 +486,7 @@ void BitmapComboBoxWidgetsPage::CreateCombo()
     m_sizerCombo->Add(m_combobox, 0, wxGROW | wxALL, 5);
     m_sizerCombo->Layout();
 
-    // Allow changing height inorder to demonstrate flexible
+    // Allow changing height in order to demonstrate flexible
     // size of image "thumbnail" painted in the control itself.
     long h = 0;
     m_textChangeHeight->GetValue().ToLong(&h);
@@ -461,7 +513,7 @@ void BitmapComboBoxWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event))
 #ifndef __WXGTK__
         m_combobox->SetString(sel, m_textChange->GetValue());
 #else
-        wxLogMessage(_T("Not implemented in wxGTK"));
+        wxLogMessage(wxT("Not implemented in wxGTK"));
 #endif
     }
 }
@@ -500,7 +552,7 @@ void BitmapComboBoxWidgetsPage::OnButtonInsert(wxCommandEvent& WXUNUSED(event))
     if ( !m_textInsert->IsModified() )
     {
         // update the default string
-        m_textInsert->SetValue(wxString::Format(_T("test item %u"), ++s_item));
+        m_textInsert->SetValue(wxString::Format(wxT("test item %u"), ++s_item));
     }
 
     int sel = m_combobox->GetSelection();
@@ -513,7 +565,8 @@ void BitmapComboBoxWidgetsPage::OnButtonInsert(wxCommandEvent& WXUNUSED(event))
 void BitmapComboBoxWidgetsPage::OnTextChangeHeight(wxCommandEvent& WXUNUSED(event))
 {
     long h = 0;
-    m_textChangeHeight->GetValue().ToLong(&h);
+    if ( m_textChangeHeight )
+        m_textChangeHeight->GetValue().ToLong(&h);
     if ( h < 5 )
         return;
     m_combobox->SetSize(wxDefaultCoord, h);
@@ -526,12 +579,16 @@ void BitmapComboBoxWidgetsPage::OnButtonLoadFromFile(wxCommandEvent& WXUNUSED(ev
     if ( sel == wxNOT_FOUND )
         sel = m_combobox->GetCount();
 
-    m_combobox->Insert(s, QueryBitmap(&s), sel);
+    wxBitmap bmp = QueryBitmap(&s);
+    if (bmp.IsOk())
+        m_combobox->Insert(s, bmp, sel);
 }
 
 void BitmapComboBoxWidgetsPage::OnButtonSetFromFile(wxCommandEvent& WXUNUSED(event))
 {
-    m_combobox->SetItemBitmap(m_combobox->GetSelection(), QueryBitmap(NULL));
+    wxBitmap bmp = QueryBitmap(NULL);
+    if (bmp.IsOk())
+        m_combobox->SetItemBitmap(m_combobox->GetSelection(), bmp);
 }
 
 void BitmapComboBoxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
@@ -539,15 +596,15 @@ void BitmapComboBoxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
     // "many" means 1000 here
     for ( unsigned int n = 0; n < 1000; n++ )
     {
-        m_combobox->Append(wxString::Format(_T("item #%u"), n));
+        m_combobox->Append(wxString::Format(wxT("item #%u"), n));
     }
 }
 
 void BitmapComboBoxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event))
 {
-    m_combobox->Append(_T("First"));
-    m_combobox->Append(_T("another one"));
-    m_combobox->Append(_T("and the last (very very very very very very very very very very long) one"));
+    m_combobox->Append(wxT("First"));
+    m_combobox->Append(wxT("another one"));
+    m_combobox->Append(wxT("and the last (very very very very very very very very very very long) one"));
 }
 
 void BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages(wxCommandEvent& WXUNUSED(event))
@@ -609,6 +666,7 @@ void BitmapComboBoxWidgetsPage::LoadWidgetImages( wxArrayString* strings, wxImag
     if ( !wxDir::Exists(fn.GetFullPath()) ||
          !wxDir::GetAllFiles(fn.GetFullPath(),strings,wxT("*.xpm")) )
     {
+        // Try ../../samples/widgets/icons
         fn.RemoveLastDir();
         fn.RemoveLastDir();
         fn.AppendDir(wxT("icons"));
@@ -641,7 +699,7 @@ void BitmapComboBoxWidgetsPage::LoadWidgetImages( wxArrayString* strings, wxImag
     for ( i=0; i<strings->size(); i++ )
     {
         fn.SetFullName((*strings)[i]);
-        wxString name =fn.GetName();
+        wxString name = fn.GetName();
 
         // Handle few exceptions
         if ( name == wxT("bmpbtn") )
@@ -654,15 +712,20 @@ void BitmapComboBoxWidgetsPage::LoadWidgetImages( wxArrayString* strings, wxImag
 #if wxUSE_IMAGE
             wxASSERT(fn.FileExists());
             wxImage image(fn.GetFullPath());
-            wxASSERT(image.Ok());
+            wxASSERT(image.IsOk());
             RescaleImage(image, foundSize.x, foundSize.y);
             wxBitmap bmp(image);
-            wxASSERT( bmp.Ok() );
+            wxASSERT( bmp.IsOk() );
 #else
             wxBitmap bmp(wxNullBitmap);
 #endif
             images->Add(bmp);
             (*strings)[i] = name;
+
+            // if the combobox is empty, use as bitmap size of the image list
+            // the size of the first valid image loaded
+            if (foundSize == wxDefaultSize)
+                foundSize = bmp.GetSize();
         }
     }
 
@@ -730,18 +793,6 @@ void BitmapComboBoxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
         event.Enable(m_combobox->GetCount() != 0);
 }
 
-void BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
-{
-    if (m_combobox)
-        event.Enable(!(m_combobox->GetWindowStyle() & wxCB_SORT));
-}
-
-void BitmapComboBoxWidgetsPage::OnUpdateUIAddSeveralWithImages(wxUpdateUIEvent& event)
-{
-    if (m_combobox)
-        event.Enable(!(m_combobox->GetWindowStyle() & wxCB_SORT));
-}
-
 void BitmapComboBoxWidgetsPage::OnComboText(wxCommandEvent& event)
 {
     if (!m_combobox)
@@ -750,22 +801,26 @@ void BitmapComboBoxWidgetsPage::OnComboText(wxCommandEvent& event)
     wxString s = event.GetString();
 
     wxASSERT_MSG( s == m_combobox->GetValue(),
-                  _T("event and combobox values should be the same") );
+                  wxT("event and combobox values should be the same") );
 
     if (event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER)
-        wxLogMessage(_T("BitmapCombobox enter pressed (now '%s')"), s.c_str());
+    {
+        wxLogMessage(wxT("BitmapCombobox enter pressed (now '%s')"), s.c_str());
+    }
     else
-        wxLogMessage(_T("BitmapCombobox text changed (now '%s')"), s.c_str());
+    {
+        wxLogMessage(wxT("BitmapCombobox text changed (now '%s')"), s.c_str());
+    }
 }
 
 void BitmapComboBoxWidgetsPage::OnComboBox(wxCommandEvent& event)
 {
     long sel = event.GetInt();
-    m_textDelete->SetValue(wxString::Format(_T("%ld"), sel));
+    m_textDelete->SetValue(wxString::Format(wxT("%ld"), sel));
 
-    wxLogMessage(_T("BitmapCombobox item %ld selected"), sel);
+    wxLogMessage(wxT("BitmapCombobox item %ld selected"), sel);
 
-    wxLogMessage(_T("BitmapCombobox GetValue(): %s"), m_combobox->GetValue().c_str() );
+    wxLogMessage(wxT("BitmapCombobox GetValue(): %s"), m_combobox->GetValue().c_str() );
 }
 
 void BitmapComboBoxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
@@ -787,7 +842,7 @@ wxBitmap BitmapComboBoxWidgetsPage::LoadBitmap(const wxString& filepath)
     }
 
     wxImage image(filepath);
-    if ( image.Ok() )
+    if ( image.IsOk() )
     {
         // Rescale very large images
         int ow = image.GetWidth();
@@ -820,19 +875,16 @@ wxBitmap BitmapComboBoxWidgetsPage::LoadBitmap(const wxString& WXUNUSED(filepath
 
 wxBitmap BitmapComboBoxWidgetsPage::QueryBitmap(wxString* pStr)
 {
-    wxString filepath = wxFileSelector(wxT("Choose image file"),
-                                       wxEmptyString,
+    wxString filepath = wxLoadFileSelector(wxT("image"),
                                        wxEmptyString,
                                        wxEmptyString,
-                                       wxT("*.*"),
-                                       wxFD_OPEN | wxFD_FILE_MUST_EXIST,
                                        this);
 
     wxBitmap bitmap;
 
     ::wxSetCursor( *wxHOURGLASS_CURSOR );
 
-    if ( filepath.length() )
+    if ( !filepath.empty() )
     {
         if ( pStr )
         {
@@ -842,7 +894,10 @@ wxBitmap BitmapComboBoxWidgetsPage::QueryBitmap(wxString* pStr)
         bitmap = LoadBitmap(filepath);
     }
 
-    wxLogDebug(wxT("%i, %i"),bitmap.GetWidth(), bitmap.GetHeight());
+    if (bitmap.IsOk())
+    {
+        wxLogDebug(wxT("%i, %i"),bitmap.GetWidth(), bitmap.GetHeight());
+    }
 
     ::wxSetCursor( *wxSTANDARD_CURSOR );
 
@@ -878,4 +933,14 @@ wxBitmap BitmapComboBoxWidgetsPage::CreateBitmap(const wxColour& colour)
     return bmp;
 }
 
+void BitmapComboBoxWidgetsPage::OnDropDown(wxCommandEvent& WXUNUSED(event))
+{
+    wxLogMessage(wxT("Combobox dropped down"));
+}
+
+void BitmapComboBoxWidgetsPage::OnCloseUp(wxCommandEvent& WXUNUSED(event))
+{
+    wxLogMessage(wxT("Combobox closed up"));
+}
+
 #endif // wxUSE_BITMAPCOMBOBOX