]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/widgets/bmpcombobox.cpp
add support for reading alpha channel from BMP images (#9741)
[wxWidgets.git] / samples / widgets / bmpcombobox.cpp
index ca8ffd6cd4963db9ad5cafb912163c63e641eac2..fc8a64fa3cc39a4cf6b4d2909ca1788f8acfafcf 100644 (file)
@@ -52,6 +52,7 @@
 #include "wx/imaglist.h"
 #include "wx/bmpcbox.h"
 
+#include "itemcontainer.h"
 #include "widgets.h"
 
 #include "icons/bmpcombobox.xpm"
@@ -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
@@ -142,7 +145,6 @@ protected:
 
     // helpers for creating bitmaps
     wxBitmap CreateBitmap(const wxColour& colour);
-    wxBitmap CreateRandomBitmap(wxString* pStr);
     wxBitmap LoadBitmap(const wxString& filepath);
     wxBitmap QueryBitmap(wxString* pStr);
 
@@ -195,6 +197,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)
@@ -233,14 +236,19 @@ 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 = (wxCheckBox *)NULL;
+    m_chkReadonly = NULL;
 
-    m_combobox = (wxBitmapComboBox *)NULL;
-    m_sizerCombo = (wxSizer *)NULL;
+    m_combobox = NULL;
+    m_sizerCombo = NULL;
+
+    m_textInsert =
+    m_textChangeHeight =
+    m_textChange =
+    m_textDelete = NULL;
 }
 
 // create a sizer containing a label and a small text ctrl
@@ -270,7 +278,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);
@@ -308,6 +315,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 +380,6 @@ void BitmapComboBoxWidgetsPage::CreateContent()
     Reset();
 
     SetSizer(sizerTop);
-
-    sizerTop->Fit(this);
 }
 
 // ----------------------------------------------------------------------------
@@ -509,7 +517,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);
@@ -548,13 +557,22 @@ void BitmapComboBoxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(even
 
 void BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages(wxCommandEvent& WXUNUSED(event))
 {
-    int i;
+    static const struct TestEntry
+    {
+        const char *text;
+        unsigned long rgb;
+    } s_entries[] =
+    {
+        { "Red circle",     0x0000ff },
+        { "Blue circle",    0xff0000 },
+        { "Green circle",   0x00ff00 },
+        { "Black circle",   0x000000 },
+    };
 
-    for ( i=0; i<4; i++ )
+    for ( unsigned i = 0; i < WXSIZEOF(s_entries); i++ )
     {
-        wxString s;
-        wxBitmap bmp = CreateRandomBitmap(&s);
-        m_combobox->Append(s, bmp);
+        const TestEntry& e = s_entries[i];
+        m_combobox->Append(e.text, CreateBitmap(wxColour(e.rgb)));
     }
 }
 
@@ -838,33 +856,23 @@ wxBitmap BitmapComboBoxWidgetsPage::QueryBitmap(wxString* pStr)
 
 wxBitmap BitmapComboBoxWidgetsPage::CreateBitmap(const wxColour& colour)
 {
-    int ch = m_combobox->GetBitmapSize().y;
-    int h0 = ch - 5;
-
-    long w = ch;
-    long h = ch;
-
-    if ( w <= 0 )
-        w = h0 - 1;
-    if ( h <= 0 )
-        h = h0;
-    if ( h > ch )
-        h = ch;
+    const int w = 10,
+              h = 10;
 
     wxMemoryDC dc;
-    wxBitmap bmp(w,h);
+    wxBitmap bmp(w, h);
     dc.SelectObject(bmp);
 
     // Draw transparent background
-    wxColour magic(255,0,255);
+    wxColour magic(255, 0, 255);
     wxBrush magicBrush(magic);
     dc.SetBrush(magicBrush);
     dc.SetPen(*wxTRANSPARENT_PEN);
-    dc.DrawRectangle(0,0,bmp.GetWidth(),bmp.GetHeight());
+    dc.DrawRectangle(0, 0, w, h);
 
     // Draw image content
     dc.SetBrush(wxBrush(colour));
-    dc.DrawCircle(h/2,h/2+1,(h/2));
+    dc.DrawCircle(h/2, h/2+1, h/2);
 
     dc.SelectObject(wxNullBitmap);
 
@@ -875,47 +883,4 @@ wxBitmap BitmapComboBoxWidgetsPage::CreateBitmap(const wxColour& colour)
     return bmp;
 }
 
-wxBitmap BitmapComboBoxWidgetsPage::CreateRandomBitmap( wxString* pStr )
-{
-    int i = rand() % 6;
-    const wxChar* str = wxT("");
-    wxBitmap bmp;
-
-    if ( i == 0 )
-    {
-        str = wxT("Red Circle");
-        bmp = CreateBitmap( *wxRED );
-    }
-    else if ( i == 1 )
-    {
-        str = wxT("Green Circle");
-        bmp = CreateBitmap( *wxGREEN );
-    }
-    else if ( i == 2 )
-    {
-        str = wxT("Blue Circle");
-        bmp = CreateBitmap( *wxBLUE );
-    }
-    else if ( i == 3 )
-    {
-        str = wxT("Black Circle");
-        bmp = CreateBitmap( *wxBLACK );
-    }
-    else if ( i == 4 )
-    {
-        str = wxT("Cyan Circle");
-        bmp = CreateBitmap( *wxCYAN );
-    }
-    else if ( i == 5 )
-    {
-        str = wxT("Light Grey Circle");
-        bmp = CreateBitmap( *wxLIGHT_GREY );
-    }
-
-    if ( pStr )
-        *pStr = str;
-
-    return bmp;
-}
-
-#endif //wxUSE_BITMAPCOMBOBOX
+#endif // wxUSE_BITMAPCOMBOBOX