]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/widgets/bmpcombobox.cpp
moving to gcc 4.0 exclusively, 10.4 minimum system
[wxWidgets.git] / samples / widgets / bmpcombobox.cpp
index b7873132e691106e454b6edebb29fd775321cf9b..7f9388545fc37725073aa76ecdeea04298416a90 100644 (file)
 #include "wx/sizer.h"
 #include "wx/icon.h"
 #include "wx/dir.h"
+#include "wx/msgdlg.h"
 #include "wx/filename.h"
 #include "wx/image.h"
 #include "wx/imaglist.h"
 #include "wx/bmpcbox.h"
 
-
+#include "itemcontainer.h"
 #include "widgets.h"
 
 #include "icons/bmpcombobox.xpm"
 
 // Images loaded from file are reduced this width and height, if larger
-#define IMG_SIZE_TRUNC  150
+#define IMG_SIZE_TRUNC  256
 
 
 // ----------------------------------------------------------------------------
@@ -82,7 +83,8 @@ enum
     BitmapComboBoxPage_Delete,
     BitmapComboBoxPage_DeleteText,
     BitmapComboBoxPage_DeleteSel,
-    BitmapComboBoxPage_Combo
+    BitmapComboBoxPage_Combo,
+    BitmapComboBoxPage_ContainerTests
 };
 
 
@@ -90,12 +92,13 @@ enum
 // BitmapComboBoxWidgetsPage
 // ----------------------------------------------------------------------------
 
-class BitmapComboBoxWidgetsPage : public WidgetsPage
+class BitmapComboBoxWidgetsPage : public ItemContainerWidgetsPage
 {
 public:
     BitmapComboBoxWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
 
     virtual wxControl *GetWidget() const { return m_combobox; }
+    virtual wxItemContainer* GetContainer() const { return m_combobox; }
     virtual void RecreateWidget() { CreateCombo(); }
 
     // lazy creation of the content
@@ -152,13 +155,16 @@ protected:
                                               wxWindowID id,
                                               wxTextCtrl **ppText);
 
+#if wxUSE_IMAGE
+    void RescaleImage(wxImage& image, int w, int h);
+#endif
+
     // the controls
     // ------------
 
     // the checkboxes for styles
     wxCheckBox *m_chkSort,
-               *m_chkReadonly,
-               *m_chkScaleimages;
+               *m_chkReadonly;
 
     // the combobox itself and the sizer it is in
     wxBitmapComboBox *m_combobox;
@@ -192,6 +198,7 @@ BEGIN_EVENT_TABLE(BitmapComboBoxWidgetsPage, WidgetsPage)
     EVT_BUTTON(BitmapComboBoxPage_AddMany, BitmapComboBoxWidgetsPage::OnButtonAddMany)
     EVT_BUTTON(BitmapComboBoxPage_LoadFromFile, BitmapComboBoxWidgetsPage::OnButtonLoadFromFile)
     EVT_BUTTON(BitmapComboBoxPage_SetFromFile, BitmapComboBoxWidgetsPage::OnButtonSetFromFile)
+    EVT_BUTTON(BitmapComboBoxPage_ContainerTests, ItemContainerWidgetsPage::OnButtonTestItemContainer)
 
     EVT_TEXT_ENTER(BitmapComboBoxPage_InsertText, BitmapComboBoxWidgetsPage::OnButtonInsert)
     EVT_TEXT(BitmapComboBoxPage_ChangeHeight, BitmapComboBoxWidgetsPage::OnTextChangeHeight)
@@ -230,12 +237,11 @@ IMPLEMENT_WIDGETS_PAGE(BitmapComboBoxWidgetsPage, _T("BitmapCombobox"),
 
 BitmapComboBoxWidgetsPage::BitmapComboBoxWidgetsPage(WidgetsBookCtrl *book,
                                              wxImageList *imaglist)
-                  : WidgetsPage(book, imaglist, bmpcombobox_xpm)
+                  : ItemContainerWidgetsPage(book, imaglist, bmpcombobox_xpm)
 {
     // init everything
     m_chkSort =
-    m_chkReadonly =
-    m_chkScaleimages = (wxCheckBox *)NULL;
+    m_chkReadonly = (wxCheckBox *)NULL;
 
     m_combobox = (wxBitmapComboBox *)NULL;
     m_sizerCombo = (wxSizer *)NULL;
@@ -293,8 +299,6 @@ void BitmapComboBoxWidgetsPage::CreateContent()
 
     wxSizer *sizerOptions = new wxStaticBoxSizer(box, wxVERTICAL);
 
-    m_chkScaleimages = CreateCheckBoxAndAddToSizer(sizerOptions, _T("&Scale loaded images to fit"));
-
     sizerRow = CreateSizerWithSmallTextAndLabel(_T("Control &height:"),
                                                 BitmapComboBoxPage_ChangeHeight,
                                                 &m_textChangeHeight);
@@ -308,6 +312,9 @@ void BitmapComboBoxWidgetsPage::CreateContent()
         _T("&Change wxBitmapComboBox contents"));
     wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
 
+    btn = new wxButton(this, BitmapComboBoxPage_ContainerTests, _T("Run &tests"));
+    sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
+
 #if wxUSE_IMAGE
     btn = new wxButton(this, BitmapComboBoxPage_AddWidgetIcons, _T("Add &widget icons"));
     sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
@@ -370,8 +377,6 @@ void BitmapComboBoxWidgetsPage::CreateContent()
     Reset();
 
     SetSizer(sizerTop);
-
-    sizerTop->Fit(this);
 }
 
 // ----------------------------------------------------------------------------
@@ -382,7 +387,6 @@ void BitmapComboBoxWidgetsPage::Reset()
 {
     m_chkSort->SetValue(false);
     m_chkReadonly->SetValue(true);
-    m_chkScaleimages->SetValue(true);
 }
 
 void BitmapComboBoxWidgetsPage::CreateCombo()
@@ -559,6 +563,33 @@ void BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages(wxCommandEvent& WXU
     }
 }
 
+#if wxUSE_IMAGE
+void BitmapComboBoxWidgetsPage::RescaleImage(wxImage& image, int w, int h)
+{
+    if ( image.GetWidth() == w && image.GetHeight() == h )
+        return;
+
+    if ( w <= 0 || h <= 0 )
+        return;
+
+    static bool isFirstScale = true;
+
+    if ( isFirstScale && m_combobox->GetCount() > 0 )
+    {
+        wxMessageBox( wxT("wxBitmapComboBox normally only supports images of one size. ")
+                      wxT("However, for demonstration purposes, loaded bitmaps are scaled to fit ")
+                      wxT("using wxImage::Rescale."),
+                      wxT("Notice"),
+                      wxOK,
+                      this );
+
+        isFirstScale = false;
+    }
+
+    image.Rescale(w, h);
+}
+#endif
+
 void BitmapComboBoxWidgetsPage::LoadWidgetImages( wxArrayString* strings, wxImageList* images )
 {
     wxFileName fn;
@@ -616,8 +647,7 @@ void BitmapComboBoxWidgetsPage::LoadWidgetImages( wxArrayString* strings, wxImag
             wxASSERT(fn.FileExists());
             wxImage image(fn.GetFullPath());
             wxASSERT(image.Ok());
-            if ( m_chkScaleimages->GetValue() && foundSize.x > 0 )
-                image.Rescale(foundSize.x, foundSize.y);
+            RescaleImage(image, foundSize.x, foundSize.y);
             wxBitmap bmp(image);
             wxASSERT( bmp.Ok() );
 #else
@@ -744,8 +774,8 @@ wxBitmap BitmapComboBoxWidgetsPage::LoadBitmap(const wxString& filepath)
     // Have some reasonable maximum size
     if ( foundSize.x <= 0 )
     {
-        foundSize.x = 256;
-        foundSize.y = 256;
+        foundSize.x = IMG_SIZE_TRUNC;
+        foundSize.y = IMG_SIZE_TRUNC;
     }
 
     wxImage image(filepath);
@@ -765,7 +795,7 @@ wxBitmap BitmapComboBoxWidgetsPage::LoadBitmap(const wxString& filepath)
             if ( h > foundSize.y )
                 h = foundSize.y;
 
-            image.Rescale(w, h);
+            RescaleImage(image, w, h);
         }
 
         return wxBitmap(image);
@@ -804,6 +834,8 @@ wxBitmap BitmapComboBoxWidgetsPage::QueryBitmap(wxString* pStr)
         bitmap = LoadBitmap(filepath);
     }
 
+    wxLogDebug(wxT("%i, %i"),bitmap.GetWidth(), bitmap.GetHeight());
+
     ::wxSetCursor( *wxSTANDARD_CURSOR );
 
     return bitmap;