X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9654efd3327fcb6b6e3d8d803ea80f08904f1197..71cf399ff0f1ddb966b48e512b0fb4e690f5c440:/samples/widgets/bmpcombobox.cpp?ds=sidebyside diff --git a/samples/widgets/bmpcombobox.cpp b/samples/widgets/bmpcombobox.cpp index e67dc103bb..7f9388545f 100644 --- a/samples/widgets/bmpcombobox.cpp +++ b/samples/widgets/bmpcombobox.cpp @@ -46,18 +46,19 @@ #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; @@ -249,7 +255,7 @@ wxSizer *BitmapComboBoxWidgetsPage::CreateSizerWithSmallTextAndLabel(const wxStr wxControl* control = new wxStaticText(this, wxID_ANY, label); wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL); wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString, - wxDefaultPosition, wxSize(50,-1), wxTE_PROCESS_ENTER); + wxDefaultPosition, wxSize(50,wxDefaultCoord), wxTE_PROCESS_ENTER); sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5); sizerRow->Add(text, 1, wxFIXED_MINSIZE | wxLEFT | wxALIGN_CENTRE_VERTICAL, 5); @@ -293,12 +299,10 @@ 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); - m_textChangeHeight->SetSize(20, -1); + m_textChangeHeight->SetSize(20, wxDefaultCoord); sizerOptions->Add(sizerRow, 0, wxALL | wxFIXED_MINSIZE /*| wxGROW*/, 5); sizerLeft->Add(sizerOptions, 0, wxGROW | wxALIGN_CENTRE_HORIZONTAL | wxTOP, 2); @@ -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() @@ -436,7 +440,7 @@ void BitmapComboBoxWidgetsPage::CreateCombo() long h = 0; m_textChangeHeight->GetValue().ToLong(&h); if ( h >= 5 ) - m_combobox->SetSize(-1, h); + m_combobox->SetSize(wxDefaultCoord, h); } // ---------------------------------------------------------------------------- @@ -500,8 +504,11 @@ void BitmapComboBoxWidgetsPage::OnButtonInsert(wxCommandEvent& WXUNUSED(event)) m_textInsert->SetValue(wxString::Format(_T("test item %u"), ++s_item)); } - if (m_combobox->GetSelection() >= 0) - m_combobox->Insert(s, wxNullBitmap, m_combobox->GetSelection()); + int sel = m_combobox->GetSelection(); + if ( sel == wxNOT_FOUND ) + sel = m_combobox->GetCount(); + + m_combobox->Insert(s, wxNullBitmap, m_combobox->GetSelection()); } void BitmapComboBoxWidgetsPage::OnTextChangeHeight(wxCommandEvent& WXUNUSED(event)) @@ -510,13 +517,17 @@ void BitmapComboBoxWidgetsPage::OnTextChangeHeight(wxCommandEvent& WXUNUSED(even m_textChangeHeight->GetValue().ToLong(&h); if ( h < 5 ) return; - m_combobox->SetSize(-1, h); + m_combobox->SetSize(wxDefaultCoord, h); } void BitmapComboBoxWidgetsPage::OnButtonLoadFromFile(wxCommandEvent& WXUNUSED(event)) { wxString s; - m_combobox->Insert(s, QueryBitmap(&s), m_combobox->GetSelection()); + int sel = m_combobox->GetSelection(); + if ( sel == wxNOT_FOUND ) + sel = m_combobox->GetCount(); + + m_combobox->Insert(s, QueryBitmap(&s), sel); } void BitmapComboBoxWidgetsPage::OnButtonSetFromFile(wxCommandEvent& WXUNUSED(event)) @@ -552,12 +563,39 @@ 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; fn.AssignCwd(); fn.AppendDir(wxT("icons")); - + wxSetCursor(*wxHOURGLASS_CURSOR); if ( !wxDir::Exists(fn.GetFullPath()) || @@ -609,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 @@ -628,11 +665,14 @@ void BitmapComboBoxWidgetsPage::OnButtonAddWidgetIcons(wxCommandEvent& WXUNUSED( { wxArrayString strings; - int sz = 32; - //if ( m_chkScaleimages->GetValue() ) - // sz = 16; + wxSize sz = m_combobox->GetBitmapSize(); + if ( sz.x <= 0 ) + { + sz.x = 32; + sz.y = 32; + } - wxImageList images(sz, sz); + wxImageList images(sz.x, sz.y); LoadWidgetImages(&strings, &images); @@ -731,6 +771,13 @@ wxBitmap BitmapComboBoxWidgetsPage::LoadBitmap(const wxString& filepath) // Get size of existing images in list wxSize foundSize = m_combobox->GetBitmapSize(); + // Have some reasonable maximum size + if ( foundSize.x <= 0 ) + { + foundSize.x = IMG_SIZE_TRUNC; + foundSize.y = IMG_SIZE_TRUNC; + } + wxImage image(filepath); if ( image.Ok() ) { @@ -748,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); @@ -766,9 +813,9 @@ wxBitmap BitmapComboBoxWidgetsPage::LoadBitmap(const wxString& WXUNUSED(filepath wxBitmap BitmapComboBoxWidgetsPage::QueryBitmap(wxString* pStr) { wxString filepath = wxFileSelector(wxT("Choose image file"), - wxT(""), - wxT(""), - wxT(""), + wxEmptyString, + wxEmptyString, + wxEmptyString, wxT("*.*"), wxFD_OPEN | wxFD_FILE_MUST_EXIST, this); @@ -787,6 +834,8 @@ wxBitmap BitmapComboBoxWidgetsPage::QueryBitmap(wxString* pStr) bitmap = LoadBitmap(filepath); } + wxLogDebug(wxT("%i, %i"),bitmap.GetWidth(), bitmap.GetHeight()); + ::wxSetCursor( *wxSTANDARD_CURSOR ); return bitmap;