// Purpose: Part of the widgets sample showing wxBitmapComboBox
// Author: Jaakko Salli
// Created: Sep-01-2006
-// Id: $Id:
+// Id: $Id$
// Copyright: (c) 2006 Jaakko Salli
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
#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 "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
// ----------------------------------------------------------------------------
void OnUpdateUIAddSeveralWithImages(wxUpdateUIEvent& event);
void OnUpdateUIClearButton(wxUpdateUIEvent& event);
void OnUpdateUIDeleteButton(wxUpdateUIEvent& event);
- void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event);
+ void OnUpdateUIItemManipulator(wxUpdateUIEvent& event);
void OnUpdateUIResetButton(wxUpdateUIEvent& event);
// reset the bmpcombobox parameters
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;
EVT_UPDATE_UI(BitmapComboBoxPage_Clear, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton)
EVT_UPDATE_UI(BitmapComboBoxPage_DeleteText, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton)
EVT_UPDATE_UI(BitmapComboBoxPage_Delete, BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton)
- EVT_UPDATE_UI(BitmapComboBoxPage_Change, BitmapComboBoxWidgetsPage::OnUpdateUIDeleteSelButton)
- EVT_UPDATE_UI(BitmapComboBoxPage_DeleteSel, BitmapComboBoxWidgetsPage::OnUpdateUIDeleteSelButton)
+ EVT_UPDATE_UI(BitmapComboBoxPage_Change, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator)
+ EVT_UPDATE_UI(BitmapComboBoxPage_SetFromFile, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator)
+ EVT_UPDATE_UI(BitmapComboBoxPage_DeleteSel, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator)
EVT_COMBOBOX(BitmapComboBoxPage_Combo, BitmapComboBoxWidgetsPage::OnComboBox)
EVT_TEXT(BitmapComboBoxPage_Combo, BitmapComboBoxWidgetsPage::OnComboText)
{
// init everything
m_chkSort =
- m_chkReadonly =
- m_chkScaleimages = (wxCheckBox *)NULL;
+ m_chkReadonly = (wxCheckBox *)NULL;
m_combobox = (wxBitmapComboBox *)NULL;
m_sizerCombo = (wxSizer *)NULL;
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);
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);
{
m_chkSort->SetValue(false);
m_chkReadonly->SetValue(true);
- m_chkScaleimages->SetValue(true);
}
void BitmapComboBoxWidgetsPage::CreateCombo()
long h = 0;
m_textChangeHeight->GetValue().ToLong(&h);
if ( h >= 5 )
- m_combobox->SetSize(-1, h);
+ m_combobox->SetSize(wxDefaultCoord, h);
}
// ----------------------------------------------------------------------------
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))
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))
}
}
+#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()) ||
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
{
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);
}
}
-void BitmapComboBoxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event)
+void BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator(wxUpdateUIEvent& event)
{
if (m_combobox)
event.Enable(m_combobox->GetSelection() != wxNOT_FOUND);
// 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() )
{
if ( h > foundSize.y )
h = foundSize.y;
- image.Rescale(w, h);
+ RescaleImage(image, w, h);
}
return wxBitmap(image);
wxBitmap BitmapComboBoxWidgetsPage::QueryBitmap(wxString* pStr)
{
wxString filepath = wxFileSelector(wxT("Choose image file"),
- wxT(""),
- wxT(""),
- wxT(""),
+ wxEmptyString,
+ wxEmptyString,
+ wxEmptyString,
wxT("*.*"),
- wxOPEN | wxFILE_MUST_EXIST,
+ wxFD_OPEN | wxFD_FILE_MUST_EXIST,
this);
wxBitmap bitmap;
bitmap = LoadBitmap(filepath);
}
+ wxLogDebug(wxT("%i, %i"),bitmap.GetWidth(), bitmap.GetHeight());
+
::wxSetCursor( *wxSTANDARD_CURSOR );
return bitmap;