]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/bmpcombobox.cpp
Fix various documentation warnings throughout core and base.
[wxWidgets.git] / samples / widgets / bmpcombobox.cpp
CommitLineData
95a46303
RR
1/////////////////////////////////////////////////////////////////////////////
2// Program: wxWidgets Widgets Sample
3// Name: bmpcombobox.cpp
4// Purpose: Part of the widgets sample showing wxBitmapComboBox
5// Author: Jaakko Salli
6// Created: Sep-01-2006
95a46303 7// Copyright: (c) 2006 Jaakko Salli
526954c5 8// Licence: wxWindows licence
95a46303
RR
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx/wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#if wxUSE_BITMAPCOMBOBOX
27
28// for all others, include the necessary headers
29#ifndef WX_PRECOMP
30 #include "wx/log.h"
31
32 #include "wx/bitmap.h"
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/combobox.h"
36 #include "wx/radiobox.h"
37 #include "wx/statbox.h"
38 #include "wx/textctrl.h"
39 #include "wx/filedlg.h"
40#endif
41
42#include "wx/stattext.h"
43#include "wx/dc.h"
44#include "wx/dcmemory.h"
45#include "wx/sizer.h"
46#include "wx/icon.h"
47#include "wx/dir.h"
dc5a8485 48#include "wx/msgdlg.h"
95a46303
RR
49#include "wx/filename.h"
50#include "wx/image.h"
51#include "wx/imaglist.h"
52#include "wx/bmpcbox.h"
53
a236aa20 54#include "itemcontainer.h"
95a46303
RR
55#include "widgets.h"
56
b2b98dfd 57#include "icons/bmpcombobox.xpm"
95a46303
RR
58
59// Images loaded from file are reduced this width and height, if larger
dc5a8485 60#define IMG_SIZE_TRUNC 256
95a46303
RR
61
62
63// ----------------------------------------------------------------------------
64// constants
65// ----------------------------------------------------------------------------
66
67// control ids
68enum
69{
70 BitmapComboBoxPage_Reset = wxID_HIGHEST,
71 BitmapComboBoxPage_Insert,
72 BitmapComboBoxPage_InsertText,
73 BitmapComboBoxPage_ChangeHeight,
74 BitmapComboBoxPage_LoadFromFile,
75 BitmapComboBoxPage_SetFromFile,
76 BitmapComboBoxPage_AddWidgetIcons,
77 BitmapComboBoxPage_AddSeveralWithImages,
78 BitmapComboBoxPage_AddSeveral,
79 BitmapComboBoxPage_AddMany,
80 BitmapComboBoxPage_Clear,
81 BitmapComboBoxPage_Change,
82 BitmapComboBoxPage_Delete,
83 BitmapComboBoxPage_DeleteText,
84 BitmapComboBoxPage_DeleteSel,
a236aa20
VZ
85 BitmapComboBoxPage_Combo,
86 BitmapComboBoxPage_ContainerTests
95a46303
RR
87};
88
67abc783
VZ
89// kinds of comboboxes
90enum
91{
92 ComboKind_Default,
93 ComboKind_Simple,
94 ComboKind_DropDown
95};
95a46303
RR
96
97// ----------------------------------------------------------------------------
98// BitmapComboBoxWidgetsPage
99// ----------------------------------------------------------------------------
100
a236aa20 101class BitmapComboBoxWidgetsPage : public ItemContainerWidgetsPage
95a46303
RR
102{
103public:
104 BitmapComboBoxWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
105
106 virtual wxControl *GetWidget() const { return m_combobox; }
a236aa20 107 virtual wxItemContainer* GetContainer() const { return m_combobox; }
95a46303
RR
108 virtual void RecreateWidget() { CreateCombo(); }
109
110 // lazy creation of the content
111 virtual void CreateContent();
112
113protected:
114 // event handlers
115 void OnButtonReset(wxCommandEvent& event);
116 void OnButtonChange(wxCommandEvent& event);
117 void OnButtonDelete(wxCommandEvent& event);
118 void OnButtonDeleteSel(wxCommandEvent& event);
119 void OnButtonClear(wxCommandEvent& event);
120 void OnButtonInsert(wxCommandEvent &event);
121 void OnTextChangeHeight(wxCommandEvent& event);
122 void OnButtonLoadFromFile(wxCommandEvent& event);
123 void OnButtonSetFromFile(wxCommandEvent& event);
124 void OnButtonAddSeveral(wxCommandEvent& event);
125 void OnButtonAddSeveralWithImages(wxCommandEvent& event);
126 void OnButtonAddWidgetIcons(wxCommandEvent& event);
127 void OnButtonAddMany(wxCommandEvent& event);
128
129 void OnComboBox(wxCommandEvent& event);
3bb3875e
VZ
130 void OnDropDown(wxCommandEvent& event);
131 void OnCloseUp(wxCommandEvent& event);
95a46303
RR
132 void OnComboText(wxCommandEvent& event);
133
134 void OnCheckOrRadioBox(wxCommandEvent& event);
135
136 void OnTextPopupWidth(wxCommandEvent& event);
137 void OnTextPopupHeight(wxCommandEvent& event);
138 void OnTextButtonAll(wxCommandEvent& event);
139
140 void OnUpdateUIInsert(wxUpdateUIEvent& event);
95a46303
RR
141 void OnUpdateUIClearButton(wxUpdateUIEvent& event);
142 void OnUpdateUIDeleteButton(wxUpdateUIEvent& event);
9654efd3 143 void OnUpdateUIItemManipulator(wxUpdateUIEvent& event);
95a46303
RR
144 void OnUpdateUIResetButton(wxUpdateUIEvent& event);
145
146 // reset the bmpcombobox parameters
147 void Reset();
148
149 // (re)create the bmpcombobox
150 void CreateCombo();
151
152 // helpers for creating bitmaps
153 wxBitmap CreateBitmap(const wxColour& colour);
95a46303
RR
154 wxBitmap LoadBitmap(const wxString& filepath);
155 wxBitmap QueryBitmap(wxString* pStr);
156
157 void LoadWidgetImages( wxArrayString* strings, wxImageList* images );
158
159 wxSizer *CreateSizerWithSmallTextAndLabel(const wxString& label,
160 wxWindowID id,
161 wxTextCtrl **ppText);
162
dc5a8485
WS
163#if wxUSE_IMAGE
164 void RescaleImage(wxImage& image, int w, int h);
165#endif
166
95a46303
RR
167 // the controls
168 // ------------
169
67abc783
VZ
170 // the sel mode radiobox
171 wxRadioBox *m_radioKind;
172
95a46303
RR
173 // the checkboxes for styles
174 wxCheckBox *m_chkSort,
dc5a8485 175 *m_chkReadonly;
95a46303
RR
176
177 // the combobox itself and the sizer it is in
178 wxBitmapComboBox *m_combobox;
179 wxSizer *m_sizerCombo;
180
181 // the text entries for "Add/change string" and "Delete" buttons
182 wxTextCtrl *m_textInsert,
183 *m_textChangeHeight,
184 *m_textChange,
185 *m_textDelete;
186
187private:
188 DECLARE_EVENT_TABLE()
189 DECLARE_WIDGETS_PAGE(BitmapComboBoxWidgetsPage)
190};
191
192// ----------------------------------------------------------------------------
193// event tables
194// ----------------------------------------------------------------------------
195
196BEGIN_EVENT_TABLE(BitmapComboBoxWidgetsPage, WidgetsPage)
197 EVT_BUTTON(BitmapComboBoxPage_Reset, BitmapComboBoxWidgetsPage::OnButtonReset)
198 EVT_BUTTON(BitmapComboBoxPage_Change, BitmapComboBoxWidgetsPage::OnButtonChange)
199 EVT_BUTTON(BitmapComboBoxPage_Delete, BitmapComboBoxWidgetsPage::OnButtonDelete)
200 EVT_BUTTON(BitmapComboBoxPage_DeleteSel, BitmapComboBoxWidgetsPage::OnButtonDeleteSel)
201 EVT_BUTTON(BitmapComboBoxPage_Clear, BitmapComboBoxWidgetsPage::OnButtonClear)
202 EVT_BUTTON(BitmapComboBoxPage_Insert, BitmapComboBoxWidgetsPage::OnButtonInsert)
203 EVT_BUTTON(BitmapComboBoxPage_AddSeveral, BitmapComboBoxWidgetsPage::OnButtonAddSeveral)
204 EVT_BUTTON(BitmapComboBoxPage_AddSeveralWithImages, BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages)
205 EVT_BUTTON(BitmapComboBoxPage_AddWidgetIcons, BitmapComboBoxWidgetsPage::OnButtonAddWidgetIcons)
206 EVT_BUTTON(BitmapComboBoxPage_AddMany, BitmapComboBoxWidgetsPage::OnButtonAddMany)
207 EVT_BUTTON(BitmapComboBoxPage_LoadFromFile, BitmapComboBoxWidgetsPage::OnButtonLoadFromFile)
208 EVT_BUTTON(BitmapComboBoxPage_SetFromFile, BitmapComboBoxWidgetsPage::OnButtonSetFromFile)
a236aa20 209 EVT_BUTTON(BitmapComboBoxPage_ContainerTests, ItemContainerWidgetsPage::OnButtonTestItemContainer)
95a46303
RR
210
211 EVT_TEXT_ENTER(BitmapComboBoxPage_InsertText, BitmapComboBoxWidgetsPage::OnButtonInsert)
212 EVT_TEXT(BitmapComboBoxPage_ChangeHeight, BitmapComboBoxWidgetsPage::OnTextChangeHeight)
213 EVT_TEXT_ENTER(BitmapComboBoxPage_DeleteText, BitmapComboBoxWidgetsPage::OnButtonDelete)
214
215 EVT_UPDATE_UI(BitmapComboBoxPage_Reset, BitmapComboBoxWidgetsPage::OnUpdateUIResetButton)
216 EVT_UPDATE_UI(BitmapComboBoxPage_Insert, BitmapComboBoxWidgetsPage::OnUpdateUIInsert)
217 EVT_UPDATE_UI(BitmapComboBoxPage_LoadFromFile, BitmapComboBoxWidgetsPage::OnUpdateUIInsert)
95a46303
RR
218 EVT_UPDATE_UI(BitmapComboBoxPage_Clear, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton)
219 EVT_UPDATE_UI(BitmapComboBoxPage_DeleteText, BitmapComboBoxWidgetsPage::OnUpdateUIClearButton)
220 EVT_UPDATE_UI(BitmapComboBoxPage_Delete, BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton)
9654efd3
WS
221 EVT_UPDATE_UI(BitmapComboBoxPage_Change, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator)
222 EVT_UPDATE_UI(BitmapComboBoxPage_SetFromFile, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator)
223 EVT_UPDATE_UI(BitmapComboBoxPage_DeleteSel, BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator)
95a46303 224
3bb3875e
VZ
225 EVT_COMBOBOX_DROPDOWN(BitmapComboBoxPage_Combo, BitmapComboBoxWidgetsPage::OnDropDown)
226 EVT_COMBOBOX_CLOSEUP(BitmapComboBoxPage_Combo, BitmapComboBoxWidgetsPage::OnCloseUp)
95a46303
RR
227 EVT_COMBOBOX(BitmapComboBoxPage_Combo, BitmapComboBoxWidgetsPage::OnComboBox)
228 EVT_TEXT(BitmapComboBoxPage_Combo, BitmapComboBoxWidgetsPage::OnComboText)
229 EVT_TEXT_ENTER(BitmapComboBoxPage_Combo, BitmapComboBoxWidgetsPage::OnComboText)
230
231 EVT_CHECKBOX(wxID_ANY, BitmapComboBoxWidgetsPage::OnCheckOrRadioBox)
232 EVT_RADIOBOX(wxID_ANY, BitmapComboBoxWidgetsPage::OnCheckOrRadioBox)
233END_EVENT_TABLE()
234
235// ============================================================================
236// implementation
237// ============================================================================
238
67abc783
VZ
239#if defined(__WXMSW__) || defined(__WXGTK__)
240 #define NATIVE_OR_GENERIC_CTRLS NATIVE_CTRLS
241#else
242 #define NATIVE_OR_GENERIC_CTRLS GENERIC_CTRLS
243#endif
95a46303 244
9a83f860 245IMPLEMENT_WIDGETS_PAGE(BitmapComboBoxWidgetsPage, wxT("BitmapCombobox"),
67abc783 246 NATIVE_OR_GENERIC_CTRLS | WITH_ITEMS_CTRLS | COMBO_CTRLS
95a46303
RR
247 );
248
249
250BitmapComboBoxWidgetsPage::BitmapComboBoxWidgetsPage(WidgetsBookCtrl *book,
251 wxImageList *imaglist)
a236aa20 252 : ItemContainerWidgetsPage(book, imaglist, bmpcombobox_xpm)
95a46303
RR
253{
254 // init everything
255 m_chkSort =
73545b12 256 m_chkReadonly = NULL;
95a46303 257
73545b12
VZ
258 m_combobox = NULL;
259 m_sizerCombo = NULL;
260
261 m_textInsert =
262 m_textChangeHeight =
263 m_textChange =
264 m_textDelete = NULL;
95a46303
RR
265}
266
267// create a sizer containing a label and a small text ctrl
268wxSizer *BitmapComboBoxWidgetsPage::CreateSizerWithSmallTextAndLabel(const wxString& label,
269 wxWindowID id,
270 wxTextCtrl **ppText)
271{
272 wxControl* control = new wxStaticText(this, wxID_ANY, label);
273 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
274 wxTextCtrl *text = new wxTextCtrl(this, id, wxEmptyString,
008a2655 275 wxDefaultPosition, wxSize(50,wxDefaultCoord), wxTE_PROCESS_ENTER);
95a46303
RR
276
277 sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
278 sizerRow->Add(text, 1, wxFIXED_MINSIZE | wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
279
280 if ( ppText )
281 *ppText = text;
282
283 return sizerRow;
284}
285
286void BitmapComboBoxWidgetsPage::CreateContent()
287{
288 /*
289 What we create here is a frame having 3 panes: style pane is the
290 leftmost one, in the middle the pane with buttons allowing to perform
291 miscellaneous combobox operations and the pane containing the combobox
292 itself to the right
293 */
95a46303
RR
294 wxSizer *sizerRow;
295
296 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
297
298 wxSizer *sizerLeft = new wxBoxSizer(wxVERTICAL);
299
300 // left pane - style box
9a83f860 301 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style"));
95a46303 302
67abc783
VZ
303
304 // should be in sync with ComboKind_XXX values
305 static const wxString kinds[] =
306 {
9a83f860
VZ
307 wxT("default"),
308 wxT("simple"),
309 wxT("drop down"),
67abc783
VZ
310 };
311
9a83f860 312 m_radioKind = new wxRadioBox(this, wxID_ANY, wxT("Combobox &kind:"),
67abc783
VZ
313 wxDefaultPosition, wxDefaultSize,
314 WXSIZEOF(kinds), kinds,
315 1, wxRA_SPECIFY_COLS);
316
95a46303
RR
317 wxSizer *sizerStyle = new wxStaticBoxSizer(box, wxVERTICAL);
318
9a83f860
VZ
319 m_chkSort = CreateCheckBoxAndAddToSizer(sizerStyle, wxT("&Sort items"));
320 m_chkReadonly = CreateCheckBoxAndAddToSizer(sizerStyle, wxT("&Read only"));
95a46303 321
9a83f860 322 wxButton *btn = new wxButton(this, BitmapComboBoxPage_Reset, wxT("&Reset"));
95a46303
RR
323 sizerStyle->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 3);
324
325 sizerLeft->Add(sizerStyle, 0, wxGROW | wxALIGN_CENTRE_HORIZONTAL);
67abc783 326 sizerLeft->Add(m_radioKind, 0, wxGROW | wxALL, 5);
95a46303
RR
327
328 // left pane - other options box
9a83f860 329 box = new wxStaticBox(this, wxID_ANY, wxT("Demo options"));
95a46303
RR
330
331 wxSizer *sizerOptions = new wxStaticBoxSizer(box, wxVERTICAL);
332
9a83f860 333 sizerRow = CreateSizerWithSmallTextAndLabel(wxT("Control &height:"),
95a46303
RR
334 BitmapComboBoxPage_ChangeHeight,
335 &m_textChangeHeight);
008a2655 336 m_textChangeHeight->SetSize(20, wxDefaultCoord);
95a46303
RR
337 sizerOptions->Add(sizerRow, 0, wxALL | wxFIXED_MINSIZE /*| wxGROW*/, 5);
338
339 sizerLeft->Add(sizerOptions, 0, wxGROW | wxALIGN_CENTRE_HORIZONTAL | wxTOP, 2);
340
341 // middle pane
342 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
9a83f860 343 wxT("&Change wxBitmapComboBox contents"));
95a46303
RR
344 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
345
9a83f860 346 btn = new wxButton(this, BitmapComboBoxPage_ContainerTests, wxT("Run &tests"));
a236aa20
VZ
347 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
348
95a46303 349#if wxUSE_IMAGE
9a83f860 350 btn = new wxButton(this, BitmapComboBoxPage_AddWidgetIcons, wxT("Add &widget icons"));
95a46303
RR
351 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
352
9a83f860 353 btn = new wxButton(this, BitmapComboBoxPage_LoadFromFile, wxT("Insert image from &file"));
95a46303
RR
354 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
355
9a83f860 356 btn = new wxButton(this, BitmapComboBoxPage_SetFromFile, wxT("&Set image from file"));
95a46303
RR
357 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
358#endif
359
9a83f860 360 btn = new wxButton(this, BitmapComboBoxPage_AddSeveralWithImages, wxT("A&ppend a few strings with images"));
95a46303
RR
361 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
362
9a83f860 363 btn = new wxButton(this, BitmapComboBoxPage_AddSeveral, wxT("Append a &few strings"));
95a46303
RR
364 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
365
9a83f860 366 btn = new wxButton(this, BitmapComboBoxPage_AddMany, wxT("Append &many strings"));
95a46303
RR
367 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
368
369 sizerRow = CreateSizerWithTextAndButton(BitmapComboBoxPage_Delete,
9a83f860 370 wxT("&Delete this item"),
95a46303
RR
371 BitmapComboBoxPage_DeleteText,
372 &m_textDelete);
373 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
374
9a83f860 375 btn = new wxButton(this, BitmapComboBoxPage_DeleteSel, wxT("Delete &selection"));
95a46303
RR
376 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
377
9a83f860 378 btn = new wxButton(this, BitmapComboBoxPage_Clear, wxT("&Clear"));
95a46303
RR
379 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
380
381#if wxUSE_IMAGE
382 wxInitAllImageHandlers();
383#endif
384
385 // right pane
386 wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
387 m_combobox = new wxBitmapComboBox();
388 m_combobox->Create(this, BitmapComboBoxPage_Combo, wxEmptyString,
389 wxDefaultPosition, wxDefaultSize,
390 0, NULL,
391 wxCB_READONLY);
392
393#if defined(wxGENERIC_BITMAPCOMBOBOX)
394 // This will sure make the list look nicer when larger images are used.
395 m_combobox->SetPopupMaxHeight(600);
396#endif
397
398 sizerRight->Add(m_combobox, 0, wxGROW | wxALL, 5);
399 sizerRight->SetMinSize(150, 0);
400 m_sizerCombo = sizerRight; // save it to modify it later
401
402 // the 3 panes panes compose the window
403 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
404 sizerTop->Add(sizerMiddle, 5, wxGROW | wxALL, 10);
405 sizerTop->Add(sizerRight, 4, wxGROW | (wxALL & ~wxRIGHT), 10);
406
407 // final initializations
408 Reset();
409
410 SetSizer(sizerTop);
95a46303
RR
411}
412
413// ----------------------------------------------------------------------------
414// operations
415// ----------------------------------------------------------------------------
416
417void BitmapComboBoxWidgetsPage::Reset()
418{
419 m_chkSort->SetValue(false);
420 m_chkReadonly->SetValue(true);
95a46303
RR
421}
422
423void BitmapComboBoxWidgetsPage::CreateCombo()
424{
425 int flags = ms_defaultFlags;
426
427 if ( m_chkSort->GetValue() )
428 flags |= wxCB_SORT;
429 if ( m_chkReadonly->GetValue() )
430 flags |= wxCB_READONLY;
431
67abc783
VZ
432 switch ( m_radioKind->GetSelection() )
433 {
434 default:
9a83f860 435 wxFAIL_MSG( wxT("unknown combo kind") );
67abc783
VZ
436 // fall through
437
438 case ComboKind_Default:
439 break;
440
441 case ComboKind_Simple:
442 flags |= wxCB_SIMPLE;
443 break;
444
445 case ComboKind_DropDown:
446 flags = wxCB_DROPDOWN;
447 break;
448 }
449
95a46303
RR
450 wxArrayString items;
451 wxArrayPtrVoid bitmaps;
452 if ( m_combobox )
453 {
454 unsigned int count = m_combobox->GetCount();
455 for ( unsigned int n = 0; n < count; n++ )
456 {
457 items.Add(m_combobox->GetString(n));
832c54d4
JS
458 wxBitmap bmp = m_combobox->GetItemBitmap(n);
459 bitmaps.Add(new wxBitmap(bmp));
95a46303
RR
460 }
461
462 m_sizerCombo->Detach( m_combobox );
463 delete m_combobox;
464 }
465
466 m_combobox = new wxBitmapComboBox();
467 m_combobox->Create(this, BitmapComboBoxPage_Combo, wxEmptyString,
468 wxDefaultPosition, wxDefaultSize,
469 0, NULL,
470 flags);
471
472#if defined(wxGENERIC_BITMAPCOMBOBOX)
473 // This will sure make the list look nicer when larger images are used.
474 m_combobox->SetPopupMaxHeight(600);
475#endif
476
477 unsigned int count = items.GetCount();
478 for ( unsigned int n = 0; n < count; n++ )
479 {
480 wxBitmap* bmp = (wxBitmap*) bitmaps[n];
481 m_combobox->Append(items[n], *bmp);
482 delete bmp;
483 }
484
485 m_sizerCombo->Add(m_combobox, 0, wxGROW | wxALL, 5);
486 m_sizerCombo->Layout();
487
4c51a665 488 // Allow changing height in order to demonstrate flexible
95a46303
RR
489 // size of image "thumbnail" painted in the control itself.
490 long h = 0;
491 m_textChangeHeight->GetValue().ToLong(&h);
492 if ( h >= 5 )
008a2655 493 m_combobox->SetSize(wxDefaultCoord, h);
95a46303
RR
494}
495
496// ----------------------------------------------------------------------------
497// event handlers
498// ----------------------------------------------------------------------------
499
500void BitmapComboBoxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
501{
502 Reset();
503
504 CreateCombo();
505}
506
507void BitmapComboBoxWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event))
508{
509 int sel = m_combobox->GetSelection();
510 if ( sel != wxNOT_FOUND )
511 {
512#ifndef __WXGTK__
513 m_combobox->SetString(sel, m_textChange->GetValue());
514#else
9a83f860 515 wxLogMessage(wxT("Not implemented in wxGTK"));
95a46303
RR
516#endif
517 }
518}
519
520void BitmapComboBoxWidgetsPage::OnButtonDelete(wxCommandEvent& WXUNUSED(event))
521{
522 unsigned long n;
523 if ( !m_textDelete->GetValue().ToULong(&n) ||
524 (n >= m_combobox->GetCount()) )
525 {
526 return;
527 }
528
529 m_combobox->Delete(n);
530}
531
532void BitmapComboBoxWidgetsPage::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event))
533{
534 int sel = m_combobox->GetSelection();
535 if ( sel != wxNOT_FOUND )
536 {
537 m_combobox->Delete(sel);
538 }
539}
540
541void BitmapComboBoxWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
542{
543 m_combobox->Clear();
544}
545
546void BitmapComboBoxWidgetsPage::OnButtonInsert(wxCommandEvent& WXUNUSED(event))
547{
548 static unsigned int s_item = 0;
549
550 wxString s = m_textInsert->GetValue();
551 if ( !m_textInsert->IsModified() )
552 {
553 // update the default string
9a83f860 554 m_textInsert->SetValue(wxString::Format(wxT("test item %u"), ++s_item));
95a46303
RR
555 }
556
008a2655
WS
557 int sel = m_combobox->GetSelection();
558 if ( sel == wxNOT_FOUND )
559 sel = m_combobox->GetCount();
560
561 m_combobox->Insert(s, wxNullBitmap, m_combobox->GetSelection());
95a46303
RR
562}
563
564void BitmapComboBoxWidgetsPage::OnTextChangeHeight(wxCommandEvent& WXUNUSED(event))
565{
566 long h = 0;
73545b12
VZ
567 if ( m_textChangeHeight )
568 m_textChangeHeight->GetValue().ToLong(&h);
95a46303
RR
569 if ( h < 5 )
570 return;
008a2655 571 m_combobox->SetSize(wxDefaultCoord, h);
95a46303
RR
572}
573
574void BitmapComboBoxWidgetsPage::OnButtonLoadFromFile(wxCommandEvent& WXUNUSED(event))
575{
576 wxString s;
008a2655
WS
577 int sel = m_combobox->GetSelection();
578 if ( sel == wxNOT_FOUND )
579 sel = m_combobox->GetCount();
580
1bf04545
FM
581 wxBitmap bmp = QueryBitmap(&s);
582 if (bmp.IsOk())
583 m_combobox->Insert(s, bmp, sel);
95a46303
RR
584}
585
586void BitmapComboBoxWidgetsPage::OnButtonSetFromFile(wxCommandEvent& WXUNUSED(event))
587{
1bf04545
FM
588 wxBitmap bmp = QueryBitmap(NULL);
589 if (bmp.IsOk())
590 m_combobox->SetItemBitmap(m_combobox->GetSelection(), bmp);
95a46303
RR
591}
592
593void BitmapComboBoxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
594{
595 // "many" means 1000 here
596 for ( unsigned int n = 0; n < 1000; n++ )
597 {
9a83f860 598 m_combobox->Append(wxString::Format(wxT("item #%u"), n));
95a46303
RR
599 }
600}
601
602void BitmapComboBoxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event))
603{
9a83f860
VZ
604 m_combobox->Append(wxT("First"));
605 m_combobox->Append(wxT("another one"));
606 m_combobox->Append(wxT("and the last (very very very very very very very very very very long) one"));
95a46303
RR
607}
608
609void BitmapComboBoxWidgetsPage::OnButtonAddSeveralWithImages(wxCommandEvent& WXUNUSED(event))
610{
c8d380be
VZ
611 static const struct TestEntry
612 {
613 const char *text;
614 unsigned long rgb;
615 } s_entries[] =
616 {
617 { "Red circle", 0x0000ff },
618 { "Blue circle", 0xff0000 },
619 { "Green circle", 0x00ff00 },
620 { "Black circle", 0x000000 },
621 };
95a46303 622
6214812c 623 for ( unsigned i = 0; i < WXSIZEOF(s_entries); i++ )
95a46303 624 {
c8d380be
VZ
625 const TestEntry& e = s_entries[i];
626 m_combobox->Append(e.text, CreateBitmap(wxColour(e.rgb)));
95a46303
RR
627 }
628}
629
dc5a8485
WS
630#if wxUSE_IMAGE
631void BitmapComboBoxWidgetsPage::RescaleImage(wxImage& image, int w, int h)
632{
633 if ( image.GetWidth() == w && image.GetHeight() == h )
634 return;
635
636 if ( w <= 0 || h <= 0 )
637 return;
638
639 static bool isFirstScale = true;
640
641 if ( isFirstScale && m_combobox->GetCount() > 0 )
642 {
643 wxMessageBox( wxT("wxBitmapComboBox normally only supports images of one size. ")
644 wxT("However, for demonstration purposes, loaded bitmaps are scaled to fit ")
645 wxT("using wxImage::Rescale."),
646 wxT("Notice"),
647 wxOK,
648 this );
649
650 isFirstScale = false;
651 }
652
653 image.Rescale(w, h);
654}
655#endif
656
95a46303
RR
657void BitmapComboBoxWidgetsPage::LoadWidgetImages( wxArrayString* strings, wxImageList* images )
658{
659 wxFileName fn;
660 fn.AssignCwd();
661 fn.AppendDir(wxT("icons"));
008a2655 662
95a46303
RR
663 wxSetCursor(*wxHOURGLASS_CURSOR);
664
665 if ( !wxDir::Exists(fn.GetFullPath()) ||
666 !wxDir::GetAllFiles(fn.GetFullPath(),strings,wxT("*.xpm")) )
667 {
1bf04545 668 // Try ../../samples/widgets/icons
95a46303
RR
669 fn.RemoveLastDir();
670 fn.RemoveLastDir();
671 fn.AppendDir(wxT("icons"));
672 if ( !wxDir::Exists(fn.GetFullPath()) ||
673 !wxDir::GetAllFiles(fn.GetFullPath(),strings,wxT("*.xpm")) )
674 {
675 // Try ../../../samples/widgets/icons
676 fn.AssignCwd();
677 fn.RemoveLastDir();
678 fn.RemoveLastDir();
679 fn.RemoveLastDir();
680 fn.AppendDir(wxT("samples"));
681 fn.AppendDir(wxT("widgets"));
682 fn.AppendDir(wxT("icons"));
683 if ( !wxDir::Exists(fn.GetFullPath()) ||
684 !wxDir::GetAllFiles(fn.GetFullPath(),strings,wxT("*.xpm")) )
685 {
686 wxLogWarning(wxT("Could not load widget icons."));
687 wxSetCursor(*wxSTANDARD_CURSOR);
688 return;
689 }
690 }
691 }
692
693 unsigned int i;
694
695 // Get size of existing images in list
696 wxSize foundSize = m_combobox->GetBitmapSize();
697
698 for ( i=0; i<strings->size(); i++ )
699 {
700 fn.SetFullName((*strings)[i]);
1bf04545 701 wxString name = fn.GetName();
95a46303
RR
702
703 // Handle few exceptions
704 if ( name == wxT("bmpbtn") )
705 {
706 strings->RemoveAt(i);
707 i--;
708 }
709 else
710 {
711#if wxUSE_IMAGE
712 wxASSERT(fn.FileExists());
713 wxImage image(fn.GetFullPath());
a1b806b9 714 wxASSERT(image.IsOk());
dc5a8485 715 RescaleImage(image, foundSize.x, foundSize.y);
95a46303 716 wxBitmap bmp(image);
a1b806b9 717 wxASSERT( bmp.IsOk() );
95a46303
RR
718#else
719 wxBitmap bmp(wxNullBitmap);
720#endif
721 images->Add(bmp);
722 (*strings)[i] = name;
1bf04545
FM
723
724 // if the combobox is empty, use as bitmap size of the image list
725 // the size of the first valid image loaded
726 if (foundSize == wxDefaultSize)
727 foundSize = bmp.GetSize();
95a46303
RR
728 }
729 }
730
731 wxSetCursor(*wxSTANDARD_CURSOR);
732}
733
734void BitmapComboBoxWidgetsPage::OnButtonAddWidgetIcons(wxCommandEvent& WXUNUSED(event))
735{
736 wxArrayString strings;
737
008a2655
WS
738 wxSize sz = m_combobox->GetBitmapSize();
739 if ( sz.x <= 0 )
740 {
741 sz.x = 32;
742 sz.y = 32;
743 }
95a46303 744
008a2655 745 wxImageList images(sz.x, sz.y);
95a46303
RR
746
747 LoadWidgetImages(&strings, &images);
748
749 unsigned int i;
750
751 for ( i=0; i<strings.size(); i++ )
752 {
753 m_combobox->Append(strings[i], images.GetBitmap(i));
754 }
755}
756
757void BitmapComboBoxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
758{
759 if (m_combobox)
760 event.Enable( m_chkSort->GetValue() || m_chkReadonly->GetValue() );
761}
762
763void BitmapComboBoxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent& event)
764{
765 if (m_combobox)
766 {
767 bool enable = !(m_combobox->GetWindowStyle() & wxCB_SORT);
768
769 event.Enable(enable);
770 }
771}
772
773void BitmapComboBoxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event)
774{
775 if (m_combobox)
776 {
777 unsigned long n;
778 event.Enable(m_textDelete->GetValue().ToULong(&n) &&
779 (n < (unsigned)m_combobox->GetCount()));
780 }
781}
782
9654efd3 783void BitmapComboBoxWidgetsPage::OnUpdateUIItemManipulator(wxUpdateUIEvent& event)
95a46303
RR
784{
785 if (m_combobox)
786 event.Enable(m_combobox->GetSelection() != wxNOT_FOUND);
787}
788
789void BitmapComboBoxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
790{
791 if (m_combobox)
792 event.Enable(m_combobox->GetCount() != 0);
793}
794
95a46303
RR
795void BitmapComboBoxWidgetsPage::OnComboText(wxCommandEvent& event)
796{
797 if (!m_combobox)
798 return;
799
800 wxString s = event.GetString();
801
802 wxASSERT_MSG( s == m_combobox->GetValue(),
9a83f860 803 wxT("event and combobox values should be the same") );
95a46303 804
ce7fe42e 805 if (event.GetEventType() == wxEVT_TEXT_ENTER)
43b2d5e7 806 {
9a83f860 807 wxLogMessage(wxT("BitmapCombobox enter pressed (now '%s')"), s.c_str());
43b2d5e7 808 }
95a46303 809 else
43b2d5e7 810 {
9a83f860 811 wxLogMessage(wxT("BitmapCombobox text changed (now '%s')"), s.c_str());
43b2d5e7 812 }
95a46303
RR
813}
814
815void BitmapComboBoxWidgetsPage::OnComboBox(wxCommandEvent& event)
816{
817 long sel = event.GetInt();
9a83f860 818 m_textDelete->SetValue(wxString::Format(wxT("%ld"), sel));
95a46303 819
9a83f860 820 wxLogMessage(wxT("BitmapCombobox item %ld selected"), sel);
95a46303 821
9a83f860 822 wxLogMessage(wxT("BitmapCombobox GetValue(): %s"), m_combobox->GetValue().c_str() );
95a46303
RR
823}
824
825void BitmapComboBoxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
826{
827 CreateCombo();
828}
829
830#if wxUSE_IMAGE
831wxBitmap BitmapComboBoxWidgetsPage::LoadBitmap(const wxString& filepath)
832{
833 // Get size of existing images in list
834 wxSize foundSize = m_combobox->GetBitmapSize();
835
008a2655
WS
836 // Have some reasonable maximum size
837 if ( foundSize.x <= 0 )
838 {
dc5a8485
WS
839 foundSize.x = IMG_SIZE_TRUNC;
840 foundSize.y = IMG_SIZE_TRUNC;
008a2655
WS
841 }
842
95a46303 843 wxImage image(filepath);
a1b806b9 844 if ( image.IsOk() )
95a46303
RR
845 {
846 // Rescale very large images
847 int ow = image.GetWidth();
848 int oh = image.GetHeight();
849
850 if ( foundSize.x > 0 &&
851 (ow != foundSize.x || oh != foundSize.y) )
852 {
853 int w = ow;
854 if ( w > foundSize.x )
855 w = foundSize.x;
856 int h = oh;
857 if ( h > foundSize.y )
858 h = foundSize.y;
859
dc5a8485 860 RescaleImage(image, w, h);
95a46303
RR
861 }
862
863 return wxBitmap(image);
864 }
865
866 return wxNullBitmap;
867}
868#else
869wxBitmap BitmapComboBoxWidgetsPage::LoadBitmap(const wxString& WXUNUSED(filepath))
870{
871 return wxNullBitmap;
872}
873#endif
874
875wxBitmap BitmapComboBoxWidgetsPage::QueryBitmap(wxString* pStr)
876{
f2a18fbe 877 wxString filepath = wxLoadFileSelector(wxT("image"),
008a2655
WS
878 wxEmptyString,
879 wxEmptyString,
95a46303
RR
880 this);
881
882 wxBitmap bitmap;
883
884 ::wxSetCursor( *wxHOURGLASS_CURSOR );
885
6636ef8d 886 if ( !filepath.empty() )
95a46303
RR
887 {
888 if ( pStr )
889 {
890 *pStr = wxFileName(filepath).GetName();
891 }
892
893 bitmap = LoadBitmap(filepath);
894 }
895
1bf04545 896 if (bitmap.IsOk())
9fceb168 897 {
1bf04545 898 wxLogDebug(wxT("%i, %i"),bitmap.GetWidth(), bitmap.GetHeight());
9fceb168 899 }
dc5a8485 900
95a46303
RR
901 ::wxSetCursor( *wxSTANDARD_CURSOR );
902
903 return bitmap;
904}
905
906wxBitmap BitmapComboBoxWidgetsPage::CreateBitmap(const wxColour& colour)
907{
b133e7e2
VZ
908 const int w = 10,
909 h = 10;
95a46303
RR
910
911 wxMemoryDC dc;
b133e7e2 912 wxBitmap bmp(w, h);
95a46303
RR
913 dc.SelectObject(bmp);
914
915 // Draw transparent background
b133e7e2 916 wxColour magic(255, 0, 255);
95a46303
RR
917 wxBrush magicBrush(magic);
918 dc.SetBrush(magicBrush);
919 dc.SetPen(*wxTRANSPARENT_PEN);
b133e7e2 920 dc.DrawRectangle(0, 0, w, h);
95a46303
RR
921
922 // Draw image content
923 dc.SetBrush(wxBrush(colour));
b133e7e2 924 dc.DrawCircle(h/2, h/2+1, h/2);
95a46303
RR
925
926 dc.SelectObject(wxNullBitmap);
927
928 // Finalize transparency with a mask
929 wxMask *mask = new wxMask(bmp, magic);
930 bmp.SetMask(mask);
931
932 return bmp;
933}
934
8a28bf76 935void BitmapComboBoxWidgetsPage::OnDropDown(wxCommandEvent& WXUNUSED(event))
3bb3875e 936{
9a83f860 937 wxLogMessage(wxT("Combobox dropped down"));
3bb3875e
VZ
938}
939
8a28bf76 940void BitmapComboBoxWidgetsPage::OnCloseUp(wxCommandEvent& WXUNUSED(event))
3bb3875e 941{
9a83f860 942 wxLogMessage(wxT("Combobox closed up"));
3bb3875e
VZ
943}
944
c8d380be 945#endif // wxUSE_BITMAPCOMBOBOX