[ 1515213 ] Combo sample improvements.
[wxWidgets.git] / samples / combo / combo.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combo.cpp
3 // Purpose: wxComboCtrl sample
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: Apr-30-2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #if !wxUSE_COMBOCTRL
34 #error "Please set wxUSE_COMBOCTRL to 1 and rebuild the library."
35 #endif
36
37 #include "wx/image.h"
38
39 #include "wx/combo.h"
40 #include "wx/odcombo.h"
41
42 // ----------------------------------------------------------------------------
43 // resources
44 // ----------------------------------------------------------------------------
45
46 // the application icon (under Windows and OS/2 it is in resources and even
47 // though we could still include the XPM here it would be unused)
48 #if !defined(__WXMSW__) && !defined(__WXPM__)
49 #include "../sample.xpm"
50 #endif
51
52 // ----------------------------------------------------------------------------
53 // private classes
54 // ----------------------------------------------------------------------------
55
56 // Define a new application type, each program should derive a class from wxApp
57 class MyApp : public wxApp
58 {
59 public:
60 // override base class virtuals
61 // ----------------------------
62
63 // this one is called on application startup and is a good place for the app
64 // initialization (doing it here and not in the ctor allows to have an error
65 // return: if OnInit() returns false, the application terminates)
66 virtual bool OnInit();
67 };
68
69 // Define a new frame type: this is going to be our main frame
70 class MyFrame : public wxFrame
71 {
72 public:
73 // ctor and dtor
74 MyFrame(const wxString& title);
75 ~MyFrame();
76
77 // event handlers (these functions should _not_ be virtual)
78 void OnQuit(wxCommandEvent& event);
79 void OnAbout(wxCommandEvent& event);
80
81 void OnShowComparison( wxCommandEvent& event );
82
83 // log wxComboCtrl events
84 void OnComboBoxUpdate( wxCommandEvent& event );
85
86 protected:
87 wxTextCtrl* m_logWin;
88 wxLog* m_logOld;
89
90 // Common list of items for all dialogs.
91 wxArrayString m_arrItems;
92
93 private:
94 // any class wishing to process wxWidgets events must use this macro
95 DECLARE_EVENT_TABLE()
96 };
97
98 // ----------------------------------------------------------------------------
99 // constants
100 // ----------------------------------------------------------------------------
101
102 // IDs for the controls and the menu commands
103 enum
104 {
105 ComboControl_Compare = wxID_HIGHEST,
106
107 // menu items
108 ComboControl_Quit = wxID_EXIT,
109
110 // it is important for the id corresponding to the "About" command to have
111 // this standard value as otherwise it won't be handled properly under Mac
112 // (where it is special and put into the "Apple" menu)
113 ComboControl_About = wxID_ABOUT
114 };
115
116 // ----------------------------------------------------------------------------
117 // event tables and other macros for wxWidgets
118 // ----------------------------------------------------------------------------
119
120 // the event tables connect the wxWidgets events with the functions (event
121 // handlers) which process them. It can be also done at run-time, but for the
122 // simple menu events like this the static method is much simpler.
123 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
124 EVT_TEXT(wxID_ANY,MyFrame::OnComboBoxUpdate)
125 EVT_COMBOBOX(wxID_ANY,MyFrame::OnComboBoxUpdate)
126
127 EVT_MENU(ComboControl_Compare, MyFrame::OnShowComparison)
128 EVT_MENU(ComboControl_Quit, MyFrame::OnQuit)
129 EVT_MENU(ComboControl_About, MyFrame::OnAbout)
130 END_EVENT_TABLE()
131
132 // Create a new application object: this macro will allow wxWidgets to create
133 // the application object during program execution (it's better than using a
134 // static object for many reasons) and also implements the accessor function
135 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
136 // not wxApp)
137 IMPLEMENT_APP(MyApp)
138
139 // ============================================================================
140 // implementation
141 // ============================================================================
142
143 // ----------------------------------------------------------------------------
144 // the application class
145 // ----------------------------------------------------------------------------
146
147 // 'Main program' equivalent: the program execution "starts" here
148 bool MyApp::OnInit()
149 {
150 // create the main application window
151 MyFrame *frame = new MyFrame(_T("wxComboCtrl and wxOwnerDrawnComboBox Sample"));
152
153 // and show it (the frames, unlike simple controls, are not shown when
154 // created initially)
155 frame->Show(true);
156
157 // success: wxApp::OnRun() will be called which will enter the main message
158 // loop and the application will run. If we returned false here, the
159 // application would exit immediately.
160 return true;
161 }
162
163
164 // ----------------------------------------------------------------------------
165 // wxOwnerDrawnComboBox with custom paint list items
166 // ----------------------------------------------------------------------------
167
168 class wxPenStyleComboBox : public wxOwnerDrawnComboBox
169 {
170 public:
171 virtual void OnDrawItem( wxDC& dc,
172 const wxRect& rect,
173 int item,
174 int flags ) const
175 {
176 if ( item == wxNOT_FOUND )
177 return;
178
179 wxRect r(rect);
180 r.Deflate(3);
181 r.height -= 2;
182
183 int penStyle = wxSOLID;
184 if ( item == 1 )
185 penStyle = wxTRANSPARENT;
186 else if ( item == 2 )
187 penStyle = wxDOT;
188 else if ( item == 3 )
189 penStyle = wxLONG_DASH;
190 else if ( item == 4 )
191 penStyle = wxSHORT_DASH;
192 else if ( item == 5 )
193 penStyle = wxDOT_DASH;
194 else if ( item == 6 )
195 penStyle = wxBDIAGONAL_HATCH;
196 else if ( item == 7 )
197 penStyle = wxCROSSDIAG_HATCH;
198 else if ( item == 8 )
199 penStyle = wxFDIAGONAL_HATCH;
200 else if ( item == 9 )
201 penStyle = wxCROSS_HATCH;
202 else if ( item == 10 )
203 penStyle = wxHORIZONTAL_HATCH;
204 else if ( item == 11 )
205 penStyle = wxVERTICAL_HATCH;
206
207 wxPen pen( dc.GetTextForeground(), 3, penStyle );
208
209 // Get text colour as pen colour
210 dc.SetPen( pen );
211
212 if ( !(flags & wxODCB_PAINTING_CONTROL) )
213 {
214 dc.DrawText(GetString( item ),
215 r.x + 3,
216 (r.y + 0) + ( (r.height/2) - dc.GetCharHeight() )/2
217 );
218
219 dc.DrawLine( r.x+5, r.y+((r.height/4)*3), r.x+r.width - 5, r.y+((r.height/4)*3) );
220 }
221 else
222 {
223 dc.DrawLine( r.x+5, r.y+r.height/2, r.x+r.width - 5, r.y+r.height/2 );
224 }
225 }
226
227 virtual void OnDrawBackground( wxDC& dc, const wxRect& rect,
228 int item, int flags ) const
229 {
230
231 // If item is selected or even, use the default rendering.
232 if ( GetVListBoxComboPopup()->IsCurrent((size_t)item) ||
233 (item & 1) == 0 )
234 {
235 wxOwnerDrawnComboBox::OnDrawBackground(dc,rect,item,flags);
236 return;
237 }
238
239 // Otherwise, draw every other background with different colour.
240 wxColour bgCol(240,240,250);
241 dc.SetBrush(wxBrush(bgCol));
242 dc.SetPen(wxPen(bgCol));
243 dc.DrawRectangle(rect);
244 }
245
246 virtual wxCoord OnMeasureItem( size_t item ) const
247 {
248 // Simply demonstrate the ability to have variable-height items
249 if ( item & 1 )
250 return 36;
251 else
252 return 24;
253 }
254
255 virtual wxCoord OnMeasureItemWidth( size_t WXUNUSED(item) ) const
256 {
257 return -1; // default - will be measured from text width
258 }
259
260 };
261
262
263 // ----------------------------------------------------------------------------
264 // wxListView Custom popup interface
265 // ----------------------------------------------------------------------------
266
267 #include <wx/listctrl.h>
268
269 class ListViewComboPopup : public wxListView, public wxComboPopup
270 {
271 public:
272
273 virtual void Init()
274 {
275 m_value = -1;
276 m_itemHere = -1; // hot item in list
277 }
278
279 virtual bool Create( wxWindow* parent )
280 {
281 return wxListView::Create(parent,1,
282 wxPoint(0,0),wxDefaultSize,
283 wxLC_LIST|wxLC_SINGLE_SEL|
284 wxLC_SORT_ASCENDING|wxSIMPLE_BORDER);
285 }
286
287 virtual wxWindow *GetControl() { return this; }
288
289 virtual void SetStringValue( const wxString& s )
290 {
291 int n = wxListView::FindItem(-1,s);
292 if ( n >= 0 && n < GetItemCount() )
293 wxListView::Select(n);
294 }
295
296 virtual wxString GetStringValue() const
297 {
298 if ( m_value >= 0 )
299 return wxListView::GetItemText(m_value);
300 return wxEmptyString;
301 }
302
303 //
304 // Popup event handlers
305 //
306
307 // Mouse hot-tracking
308 void OnMouseMove(wxMouseEvent& event)
309 {
310 // Move selection to cursor if it is inside the popup
311
312 int resFlags;
313 int itemHere = HitTest(event.GetPosition(),resFlags);
314 if ( itemHere >= 0 )
315 {
316 wxListView::Select(itemHere,true);
317 m_itemHere = itemHere;
318 }
319 event.Skip();
320 }
321
322 // On mouse left, set the value and close the popup
323 void OnMouseClick(wxMouseEvent& WXUNUSED(event))
324 {
325 m_value = m_itemHere;
326 // TODO: Send event
327 Dismiss();
328 }
329
330 //
331 // Utilies for item manipulation
332 //
333
334 void AddSelection( const wxString& selstr )
335 {
336 wxListView::InsertItem(GetItemCount(),selstr);
337 }
338
339 protected:
340
341 int m_value; // current item index
342 int m_itemHere; // hot item in popup
343
344 private:
345 DECLARE_EVENT_TABLE()
346 };
347
348 BEGIN_EVENT_TABLE(ListViewComboPopup, wxListView)
349 EVT_MOTION(ListViewComboPopup::OnMouseMove)
350 // NOTE: Left down event is used instead of left up right now
351 // since MSW wxListCtrl doesn't seem to emit left ups
352 // consistently.
353 EVT_LEFT_DOWN(ListViewComboPopup::OnMouseClick)
354 END_EVENT_TABLE()
355
356
357 // ----------------------------------------------------------------------------
358 // wxTreeCtrl Custom popup interface
359 // ----------------------------------------------------------------------------
360
361 #include <wx/treectrl.h>
362
363 class TreeCtrlComboPopup : public wxTreeCtrl, public wxComboPopup
364 {
365 public:
366
367 virtual void Init()
368 {
369 }
370
371 virtual bool Create( wxWindow* parent )
372 {
373 return wxTreeCtrl::Create(parent,1,
374 wxPoint(0,0),wxDefaultSize,
375 wxTR_HIDE_ROOT|wxTR_HAS_BUTTONS|
376 wxTR_SINGLE|wxTR_LINES_AT_ROOT|
377 wxSIMPLE_BORDER);
378 }
379
380 virtual void OnShow()
381 {
382 // make sure selected item is visible
383 if ( m_value.IsOk() )
384 EnsureVisible(m_value);
385 }
386
387 virtual wxSize GetAdjustedSize( int minWidth,
388 int WXUNUSED(prefHeight),
389 int maxHeight )
390 {
391 return wxSize(wxMax(300,minWidth),wxMin(250,maxHeight));
392 }
393
394 virtual wxWindow *GetControl() { return this; }
395
396 // Needed by SetStringValue
397 wxTreeItemId FindItemByText( wxTreeItemId parent, const wxString& text )
398 {
399 wxTreeItemIdValue cookie;
400 wxTreeItemId child = GetFirstChild(parent,cookie);
401 while ( child.IsOk() )
402 {
403 if ( GetItemText(child) == text )
404 {
405 return child;
406 }
407 if ( ItemHasChildren(child) )
408 {
409 wxTreeItemId found = FindItemByText(child,text);
410 if ( found.IsOk() )
411 return found;
412 }
413 child = GetNextChild(parent,cookie);
414 }
415 return wxTreeItemId();
416 }
417
418 virtual void SetStringValue( const wxString& s )
419 {
420 wxTreeItemId root = GetRootItem();
421 if ( !root.IsOk() )
422 return;
423
424 wxTreeItemId found = FindItemByText(root,s);
425 if ( found.IsOk() )
426 {
427 m_value = m_itemHere = found;
428 wxTreeCtrl::SelectItem(found);
429 }
430 }
431
432 virtual wxString GetStringValue() const
433 {
434 if ( m_value.IsOk() )
435 return wxTreeCtrl::GetItemText(m_value);
436 return wxEmptyString;
437 }
438
439 //
440 // Popup event handlers
441 //
442
443 // Mouse hot-tracking
444 void OnMouseMove(wxMouseEvent& event)
445 {
446 int resFlags;
447 wxTreeItemId itemHere = HitTest(event.GetPosition(),resFlags);
448 if ( itemHere.IsOk() && (resFlags & wxTREE_HITTEST_ONITEMLABEL) )
449 {
450 wxTreeCtrl::SelectItem(itemHere,true);
451 m_itemHere = itemHere;
452 }
453 event.Skip();
454 }
455
456 // On mouse left, set the value and close the popup
457 void OnMouseClick(wxMouseEvent& event)
458 {
459 int resFlags;
460 wxTreeItemId itemHere = HitTest(event.GetPosition(),resFlags);
461 if ( itemHere.IsOk() && (resFlags & wxTREE_HITTEST_ONITEMLABEL) )
462 {
463 m_itemHere = itemHere;
464 m_value = itemHere;
465 Dismiss();
466 // TODO: Send event
467 }
468 event.Skip();
469 }
470
471 protected:
472
473 wxTreeItemId m_value; // current item index
474 wxTreeItemId m_itemHere; // hot item in popup
475
476 private:
477 DECLARE_EVENT_TABLE()
478 };
479
480 BEGIN_EVENT_TABLE(TreeCtrlComboPopup, wxTreeCtrl)
481 EVT_MOTION(TreeCtrlComboPopup::OnMouseMove)
482 // NOTE: Left down event is used instead of left up right now
483 // since MSW wxTreeCtrl doesn't seem to emit left ups
484 // consistently.
485 EVT_LEFT_DOWN(TreeCtrlComboPopup::OnMouseClick)
486 END_EVENT_TABLE()
487
488 // ----------------------------------------------------------------------------
489 // wxComboCtrl with entirely custom button action (opens file dialog)
490 // ----------------------------------------------------------------------------
491
492 class wxFileSelectorCombo : public wxComboCtrl
493 {
494 public:
495 wxFileSelectorCombo() : wxComboCtrl() { Init(); }
496
497 wxFileSelectorCombo(wxWindow *parent,
498 wxWindowID id = wxID_ANY,
499 const wxString& value = wxEmptyString,
500 const wxPoint& pos = wxDefaultPosition,
501 const wxSize& size = wxDefaultSize,
502 long style = 0,
503 const wxValidator& validator = wxDefaultValidator,
504 const wxString& name = wxComboBoxNameStr)
505 : wxComboCtrl()
506 {
507 Init();
508 Create(parent,id,value,
509 pos,size,
510 // Style flag wxCC_STD_BUTTON makes the button
511 // behave more like a standard push button.
512 style | wxCC_STD_BUTTON,
513 validator,name);
514
515 //
516 // Prepare custom button bitmap (just '...' text)
517 wxMemoryDC dc;
518 wxBitmap bmp(12,16);
519 dc.SelectObject(bmp);
520
521 // Draw transparent background
522 wxColour magic(255,0,255);
523 wxBrush magicBrush(magic);
524 dc.SetBrush( magicBrush );
525 dc.SetPen( *wxTRANSPARENT_PEN );
526 dc.DrawRectangle(0,0,bmp.GetWidth(),bmp.GetHeight());
527
528 // Draw text
529 wxString str = wxT("...");
530 int w,h;
531 dc.GetTextExtent(str, &w, &h, 0, 0);
532 dc.DrawText(str, (bmp.GetWidth()-w)/2, (bmp.GetHeight()-h)/2);
533
534 dc.SelectObject( wxNullBitmap );
535
536 // Finalize transparency with a mask
537 wxMask *mask = new wxMask( bmp, magic );
538 bmp.SetMask( mask );
539
540 SetButtonBitmaps(bmp,true);
541 }
542
543 virtual void OnButtonClick()
544 {
545 // Show standard wxFileDialog on button click
546
547 wxFileDialog dlg(this,
548 wxT("Choose File"),
549 wxEmptyString,
550 GetValue(),
551 wxT("All files (*.*)|*.*"),
552 wxFD_OPEN);
553
554 if ( dlg.ShowModal() == wxID_OK )
555 {
556 SetValue(dlg.GetPath());
557 }
558 }
559
560 private:
561 void Init()
562 {
563 // Initialize member variables here
564 }
565 };
566
567 // ----------------------------------------------------------------------------
568 // main frame
569 // ----------------------------------------------------------------------------
570
571 // frame constructor
572 MyFrame::MyFrame(const wxString& title)
573 : wxFrame(NULL, wxID_ANY, title)
574 {
575 wxBoxSizer* topSizer;
576 wxBoxSizer* topRowSizer;
577 wxBoxSizer* colSizer;
578 wxBoxSizer* rowSizer;
579
580 // set the frame icon
581 SetIcon(wxICON(sample));
582
583 #if wxUSE_MENUS
584 // create a menu bar
585 wxMenu *fileMenu = new wxMenu;
586
587 // the "About" item should be in the help menu
588 wxMenu *helpMenu = new wxMenu;
589 helpMenu->Append(ComboControl_About, _T("&About...\tF1"), _T("Show about dialog"));
590
591 fileMenu->Append(ComboControl_Compare, _T("&Compare against wxComboBox..."),
592 _T("Show some wxOwnerDrawnComboBoxes side-by-side with native wxComboBoxes."));
593 fileMenu->AppendSeparator();
594 fileMenu->Append(ComboControl_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
595
596 // now append the freshly created menu to the menu bar...
597 wxMenuBar *menuBar = new wxMenuBar();
598 menuBar->Append(fileMenu, _T("&File"));
599 menuBar->Append(helpMenu, _T("&Help"));
600
601 // ... and attach this menu bar to the frame
602 SetMenuBar(menuBar);
603 #endif // wxUSE_MENUS
604
605 wxPanel* panel = new wxPanel(this);
606
607 // Prepare log window right away since it shows EVT_TEXTs
608 m_logWin = new wxTextCtrl( panel, 105, wxEmptyString, wxDefaultPosition,
609 wxSize(-1,125), wxTE_MULTILINE|wxFULL_REPAINT_ON_RESIZE );
610 m_logWin->SetEditable(false);
611 wxLogTextCtrl* logger = new wxLogTextCtrl( m_logWin );
612 m_logOld = logger->SetActiveTarget( logger );
613 logger->SetTimestamp( NULL );
614
615
616 topSizer = new wxBoxSizer( wxVERTICAL );
617
618 topRowSizer = new wxBoxSizer( wxHORIZONTAL );
619
620 colSizer = new wxBoxSizer( wxVERTICAL );
621
622
623 wxComboCtrl* cc;
624 wxGenericComboControl* gcc;
625 wxOwnerDrawnComboBox* odc;
626
627 // Create common strings array
628 m_arrItems.Add( wxT("Solid") );
629 m_arrItems.Add( wxT("Transparent") );
630 m_arrItems.Add( wxT("Dot") );
631 m_arrItems.Add( wxT("Long Dash") );
632 m_arrItems.Add( wxT("Short Dash") );
633 m_arrItems.Add( wxT("Dot Dash") );
634 m_arrItems.Add( wxT("Backward Diagonal Hatch") );
635 m_arrItems.Add( wxT("Cross-diagonal Hatch") );
636 m_arrItems.Add( wxT("Forward Diagonal Hatch") );
637 m_arrItems.Add( wxT("Cross Hatch") );
638 m_arrItems.Add( wxT("Horizontal Hatch") );
639 m_arrItems.Add( wxT("Vertical Hatch") );
640
641
642 //
643 // Create pen selector ODComboBox with owner-drawn items
644 //
645 rowSizer = new wxBoxSizer( wxHORIZONTAL );
646 rowSizer->Add( new wxStaticText(panel,wxID_ANY,
647 wxT("OwnerDrawnComboBox with owner-drawn items:")), 1,
648 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
649 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
650
651 rowSizer = new wxBoxSizer( wxHORIZONTAL );
652
653
654 // When defining derivative class for callbacks, we need
655 // to use two-stage creation (or redefine the common wx
656 // constructor).
657 odc = new wxPenStyleComboBox();
658 odc->Create(panel,wxID_ANY,wxEmptyString,
659 wxDefaultPosition, wxDefaultSize,
660 m_arrItems,
661 wxCB_READONLY //wxNO_BORDER | wxCB_READONLY
662 );
663
664
665 odc->SetSelection(0);
666
667 rowSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
668 rowSizer->AddStretchSpacer(1);
669 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
670
671
672
673 //
674 // Same but with changed button position
675 //
676 rowSizer = new wxBoxSizer( wxHORIZONTAL );
677 rowSizer->Add( new wxStaticText(panel,wxID_ANY,
678 wxT("OwnerDrawnComboBox with owner-drawn items and button on the left:")), 1,
679 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
680 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
681
682 rowSizer = new wxBoxSizer( wxHORIZONTAL );
683
684
685 // When defining derivative class for callbacks, we need
686 // to use two-stage creation (or redefine the common wx
687 // constructor).
688 odc = new wxPenStyleComboBox();
689 odc->Create(panel,wxID_ANY,wxEmptyString,
690 wxDefaultPosition, wxDefaultSize,
691 m_arrItems,
692 wxCB_READONLY //wxNO_BORDER | wxCB_READONLY
693 );
694
695
696 odc->SetSelection(0);
697 odc->SetButtonPosition(-2, // width adjustment
698 -6, // height adjustment
699 wxLEFT, // side
700 2 // horizontal spacing
701 );
702
703 rowSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
704 rowSizer->AddStretchSpacer(1);
705 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
706
707
708 //
709 // List View wxComboCtrl
710 //
711
712 rowSizer = new wxBoxSizer( wxHORIZONTAL );
713 rowSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("List View wxComboCtrl:")), 1,
714 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
715 rowSizer->Add( new wxStaticText(panel,wxID_ANY,wxT("Tree Ctrl wxComboControl:")), 1,
716 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
717 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
718
719 rowSizer = new wxBoxSizer( wxHORIZONTAL );
720 cc = new wxComboCtrl(panel,2,wxEmptyString,
721 wxDefaultPosition, wxDefaultSize);
722
723 cc->SetPopupMinWidth(300);
724
725 ListViewComboPopup* iface = new ListViewComboPopup();
726 cc->SetPopupControl(iface);
727
728 int i;
729 for ( i=0; i<100; i++ )
730 iface->AddSelection( wxString::Format(wxT("Item %02i"),i));
731
732 rowSizer->Add( cc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
733
734
735 //
736 // Tree Ctrl wxComboCtrl
737 //
738
739 // Note that we test that wxGenericComboControl works
740 gcc = new wxGenericComboControl(panel,wxID_ANY,wxEmptyString,
741 wxDefaultPosition, wxDefaultSize);
742
743 // Set popup interface right away, otherwise some of the calls
744 // below may fail
745 TreeCtrlComboPopup* tcPopup = new TreeCtrlComboPopup();
746 gcc->SetPopupControl(tcPopup);
747
748 // Add items using wxTreeCtrl methods directly
749 wxTreeItemId rootId = tcPopup->AddRoot(wxT("<hidden_root>"));
750
751 wxTreeItemId groupId;
752
753 for ( i=0; i<4; i++ )
754 {
755 groupId = tcPopup->AppendItem(rootId,
756 wxString::Format(wxT("Branch %02i"),i));
757
758 int n;
759 for ( n=0; n<25; n++ )
760 tcPopup->AppendItem(groupId,
761 wxString::Format(wxT("Subitem %02i"),(i*25)+n));
762 }
763
764 gcc->SetValue(wxT("Subitem 05"));
765
766 // Move button to left - it makes more sense for a tree ctrl
767 gcc->SetButtonPosition(0, // width adjustment
768 0, // height adjustment
769 wxLEFT, // side
770 0 // horizontal spacing
771 );
772
773 rowSizer->Add( gcc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
774
775 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
776
777 #if wxUSE_IMAGE
778 wxInitAllImageHandlers();
779
780 //
781 // Custom Dropbutton Bitmaps
782 // (second one uses blank button background)
783 //
784 rowSizer = new wxBoxSizer( wxHORIZONTAL );
785 rowSizer->Add( new wxStaticText(panel,wxID_ANY,
786 wxT("OwnerDrawnComboBox with simple dropbutton graphics:")), 1,
787 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
788
789 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
790
791 rowSizer = new wxBoxSizer( wxHORIZONTAL );
792
793 odc = new wxOwnerDrawnComboBox(panel,wxID_ANY,wxEmptyString,
794 wxDefaultPosition, wxDefaultSize,
795 m_arrItems,
796 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
797 );
798
799 wxOwnerDrawnComboBox* odc2;
800 odc2 = new wxOwnerDrawnComboBox(panel,wxID_ANY,wxEmptyString,
801 wxDefaultPosition, wxDefaultSize,
802 m_arrItems,
803 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
804 );
805
806 // Load images from disk
807 wxImage imgNormal(wxT("dropbutn.png"));
808 wxImage imgPressed(wxT("dropbutp.png"));
809 wxImage imgHover(wxT("dropbuth.png"));
810
811 if ( imgNormal.Ok() && imgPressed.Ok() && imgHover.Ok() )
812 {
813 wxBitmap bmpNormal(imgNormal);
814 wxBitmap bmpPressed(imgPressed);
815 wxBitmap bmpHover(imgHover);
816 odc->SetButtonBitmaps(bmpNormal,false,bmpPressed,bmpHover);
817 odc2->SetButtonBitmaps(bmpNormal,true,bmpPressed,bmpHover);
818 }
819 else
820 wxLogError(wxT("Dropbutton images not found"));
821
822 //odc2->SetButtonPosition(0, // width adjustment
823 // 0, // height adjustment
824 // wxLEFT, // side
825 // 0 // horizontal spacing
826 // );
827
828 rowSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
829 rowSizer->Add( odc2, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
830 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
831 #endif
832
833
834 //
835 // wxComboCtrl with totally custom button action (open file dialog)
836 //
837 rowSizer = new wxBoxSizer( wxHORIZONTAL );
838 rowSizer->Add( new wxStaticText(panel,wxID_ANY,
839 wxT("wxComboCtrl with custom button action:")), 1,
840 wxALIGN_CENTER_VERTICAL|wxRIGHT, 4 );
841
842
843 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
844
845 rowSizer = new wxBoxSizer( wxHORIZONTAL );
846 wxFileSelectorCombo* fsc;
847
848 fsc = new wxFileSelectorCombo(panel,wxID_ANY,wxEmptyString,
849 wxDefaultPosition, wxDefaultSize,
850 (long)0
851 );
852
853 rowSizer->Add( fsc, 1, wxALIGN_CENTER_VERTICAL|wxALL, 4 );
854 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, 5 );
855
856
857 // Make sure GetFeatures is implemented
858 wxComboCtrl::GetFeatures();
859
860
861 topRowSizer->Add( colSizer, 1, wxALL, 2 );
862
863 topRowSizer->Add( m_logWin, 1, wxEXPAND|wxALL, 5 );
864 topSizer->Add( topRowSizer, 1, wxEXPAND );
865
866 panel->SetSizer( topSizer );
867 topSizer->SetSizeHints( panel );
868
869 SetSize(740,400);
870 Centre();
871 }
872
873 // event handlers
874
875 void MyFrame::OnComboBoxUpdate( wxCommandEvent& event )
876 {
877 // Don't show messages for the log output window (it'll crash)
878 if ( !event.GetEventObject()->IsKindOf(CLASSINFO(wxComboCtrl)) )
879 return;
880
881 if ( event.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED )
882 wxLogDebug(wxT("EVT_COMBOBOX(id=%i,selection=%i)"),event.GetId(),event.GetSelection());
883 else if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED )
884 wxLogDebug(wxT("EVT_TEXT(id=%i,string=\"%s\")"),event.GetId(),event.GetString().c_str());
885 }
886
887 void MyFrame::OnShowComparison( wxCommandEvent& WXUNUSED(event) )
888 {
889 //
890 // Show some wxOwnerDrawComboBoxes for comparison
891 //
892
893 wxBoxSizer* colSizer;
894 wxBoxSizer* rowSizer;
895 wxStaticBoxSizer* groupSizer;
896
897 wxComboBox* cb;
898 wxOwnerDrawnComboBox* odc;
899
900 const int border = 4;
901
902 wxDialog* dlg = new wxDialog(this,wxID_ANY,
903 wxT("Compare against wxComboBox"),
904 wxDefaultPosition,wxDefaultSize,
905 wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER);
906
907 colSizer = new wxBoxSizer( wxVERTICAL );
908
909 rowSizer = new wxBoxSizer(wxHORIZONTAL);
910
911 groupSizer = new wxStaticBoxSizer(new wxStaticBox(dlg,wxID_ANY,wxT(" wxOwnerDrawnComboBox ")),
912 wxVERTICAL);
913
914 groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Writable, sorted:")), 0,
915 wxALIGN_CENTER_VERTICAL|wxRIGHT|wxEXPAND, border );
916
917 odc = new wxOwnerDrawnComboBox(dlg,wxID_ANY,wxEmptyString,
918 wxDefaultPosition, wxDefaultSize,
919 m_arrItems,
920 wxCB_SORT // wxNO_BORDER|wxCB_READONLY
921 );
922
923 odc->Append(wxT("H - Appended Item")); // test sorting in append
924
925 odc->SetValue(wxT("Dot Dash"));
926
927 groupSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
928
929 //
930 // Readonly ODComboBox
931 groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Read-only:")), 0,
932 wxALIGN_CENTER_VERTICAL|wxRIGHT, border );
933
934 odc = new wxOwnerDrawnComboBox(dlg,wxID_ANY,wxEmptyString,
935 wxDefaultPosition, wxDefaultSize,
936 m_arrItems,
937 wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY
938 );
939
940 odc->SetValue(wxT("Dot Dash"));
941 odc->SetText(wxT("Dot Dash (Testing SetText)"));
942
943 groupSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
944
945 //
946 // Disabled ODComboBox
947 groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Disabled:")), 0,
948 wxALIGN_CENTER_VERTICAL|wxRIGHT, border );
949
950 odc = new wxOwnerDrawnComboBox(dlg,wxID_ANY,wxEmptyString,
951 wxDefaultPosition, wxDefaultSize,
952 m_arrItems,
953 wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY
954 );
955
956 odc->SetValue(wxT("Dot Dash"));
957 odc->Enable(false);
958
959 groupSizer->Add( odc, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
960
961 rowSizer->Add( groupSizer, 1, wxEXPAND|wxALL, border );
962
963
964 groupSizer = new wxStaticBoxSizer(new wxStaticBox(dlg,wxID_ANY,wxT(" wxComboBox ")),
965 wxVERTICAL);
966
967 //
968 // wxComboBox
969 //
970 groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Writable, sorted:")), 0,
971 wxALIGN_CENTER_VERTICAL|wxRIGHT|wxEXPAND, border );
972
973 cb = new wxComboBox(dlg,wxID_ANY,wxEmptyString,
974 wxDefaultPosition, wxDefaultSize,
975 m_arrItems,
976 wxCB_SORT // wxNO_BORDER|wxCB_READONLY
977 );
978
979 cb->Append(wxT("H - Appended Item")); // test sorting in append
980
981 cb->SetValue(wxT("Dot Dash"));
982
983 groupSizer->Add( cb, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
984
985 //
986 // Readonly wxComboBox
987 groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Read-only:")), 0,
988 wxALIGN_CENTER_VERTICAL|wxRIGHT, border );
989
990 cb = new wxComboBox(dlg,wxID_ANY,wxEmptyString,
991 wxDefaultPosition, wxDefaultSize,
992 m_arrItems,
993 wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY
994 );
995
996 cb->SetValue(wxT("Dot Dash"));
997
998 groupSizer->Add( cb, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
999
1000 //
1001 // Disabled wxComboBox
1002 groupSizer->Add( new wxStaticText(dlg,wxID_ANY,wxT("Disabled:")), 0,
1003 wxALIGN_CENTER_VERTICAL|wxRIGHT, border );
1004
1005 cb = new wxComboBox(dlg,wxID_ANY,wxEmptyString,
1006 wxDefaultPosition, wxDefaultSize,
1007 m_arrItems,
1008 wxCB_SORT|wxCB_READONLY // wxNO_BORDER|wxCB_READONLY
1009 );
1010
1011 cb->SetValue(wxT("Dot Dash"));
1012 cb->Enable(false);
1013
1014 groupSizer->Add( cb, 1, wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, border );
1015
1016 rowSizer->Add( groupSizer, 1, wxEXPAND|wxALL, border );
1017
1018 colSizer->Add( rowSizer, 0, wxEXPAND|wxALL, border );
1019
1020 dlg->SetSizer( colSizer );
1021 colSizer->SetSizeHints( dlg );
1022
1023 dlg->SetSize(60,240);
1024 dlg->Centre();
1025 dlg->ShowModal();
1026 }
1027
1028 MyFrame::~MyFrame()
1029 {
1030 delete wxLog::SetActiveTarget(m_logOld);
1031 }
1032
1033 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
1034 {
1035 // true is to force the frame to close
1036 Close(true);
1037 }
1038
1039 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
1040 {
1041 wxMessageBox(wxString::Format(
1042 _T("Welcome to %s!\n")
1043 _T("\n")
1044 _T("This is the wxWidgets wxComboCtrl and wxOwnerDrawnComboBox sample\n")
1045 _T("running under %s."),
1046 wxVERSION_STRING,
1047 wxGetOsDescription().c_str()
1048 ),
1049 _T("About wxComboCtrl sample"),
1050 wxOK | wxICON_INFORMATION,
1051 this);
1052 }