1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboCtrl sample
4 // Author: Jaakko Salli
6 // Created: Apr-30-2006
8 // Copyright: (c) Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
34 #error "Please set wxUSE_COMBOCTRL to 1 and rebuild the library."
40 #include "wx/odcombo.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
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"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // Define a new application type, each program should derive a class from wxApp
57 class MyApp
: public wxApp
60 // override base class virtuals
61 // ----------------------------
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();
69 // Define a new frame type: this is going to be our main frame
70 class MyFrame
: public wxFrame
74 MyFrame(const wxString
& title
);
77 // event handlers (these functions should _not_ be virtual)
78 void OnQuit(wxCommandEvent
& event
);
79 void OnAbout(wxCommandEvent
& event
);
81 void OnShowComparison( wxCommandEvent
& event
);
83 // log wxComboCtrl events
84 void OnComboBoxUpdate( wxCommandEvent
& event
);
86 void OnIdle( wxIdleEvent
& event
);
93 // Common list of items for all dialogs.
94 wxArrayString m_arrItems
;
97 // any class wishing to process wxWidgets events must use this macro
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 // IDs for the controls and the menu commands
108 ComboCtrl_Compare
= wxID_HIGHEST
,
111 ComboCtrl_Quit
= wxID_EXIT
,
113 // it is important for the id corresponding to the "About" command to have
114 // this standard value as otherwise it won't be handled properly under Mac
115 // (where it is special and put into the "Apple" menu)
116 ComboCtrl_About
= wxID_ABOUT
119 // ----------------------------------------------------------------------------
120 // event tables and other macros for wxWidgets
121 // ----------------------------------------------------------------------------
123 // the event tables connect the wxWidgets events with the functions (event
124 // handlers) which process them. It can be also done at run-time, but for the
125 // simple menu events like this the static method is much simpler.
126 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
127 EVT_TEXT(wxID_ANY
,MyFrame::OnComboBoxUpdate
)
128 EVT_COMBOBOX(wxID_ANY
,MyFrame::OnComboBoxUpdate
)
130 EVT_MENU(ComboCtrl_Compare
, MyFrame::OnShowComparison
)
131 EVT_MENU(ComboCtrl_Quit
, MyFrame::OnQuit
)
132 EVT_MENU(ComboCtrl_About
, MyFrame::OnAbout
)
134 EVT_IDLE(MyFrame::OnIdle
)
137 // Create a new application object: this macro will allow wxWidgets to create
138 // the application object during program execution (it's better than using a
139 // static object for many reasons) and also implements the accessor function
140 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
144 // ============================================================================
146 // ============================================================================
148 // ----------------------------------------------------------------------------
149 // the application class
150 // ----------------------------------------------------------------------------
152 // 'Main program' equivalent: the program execution "starts" here
155 if ( !wxApp::OnInit() )
158 // create the main application window
159 MyFrame
*frame
= new MyFrame(wxT("wxComboCtrl and wxOwnerDrawnComboBox Sample"));
161 // and show it (the frames, unlike simple controls, are not shown when
162 // created initially)
165 // success: wxApp::OnRun() will be called which will enter the main message
166 // loop and the application will run. If we returned false here, the
167 // application would exit immediately.
172 // ----------------------------------------------------------------------------
173 // wxOwnerDrawnComboBox with custom paint list items
174 // ----------------------------------------------------------------------------
176 class wxPenStyleComboBox
: public wxOwnerDrawnComboBox
179 virtual void OnDrawItem( wxDC
& dc
,
184 if ( item
== wxNOT_FOUND
)
191 int penStyle
= wxSOLID
;
193 penStyle
= wxTRANSPARENT
;
194 else if ( item
== 2 )
196 else if ( item
== 3 )
197 penStyle
= wxLONG_DASH
;
198 else if ( item
== 4 )
199 penStyle
= wxSHORT_DASH
;
200 else if ( item
== 5 )
201 penStyle
= wxDOT_DASH
;
202 else if ( item
== 6 )
203 penStyle
= wxBDIAGONAL_HATCH
;
204 else if ( item
== 7 )
205 penStyle
= wxCROSSDIAG_HATCH
;
206 else if ( item
== 8 )
207 penStyle
= wxFDIAGONAL_HATCH
;
208 else if ( item
== 9 )
209 penStyle
= wxCROSS_HATCH
;
210 else if ( item
== 10 )
211 penStyle
= wxHORIZONTAL_HATCH
;
212 else if ( item
== 11 )
213 penStyle
= wxVERTICAL_HATCH
;
215 wxPen
pen( dc
.GetTextForeground(), 3, penStyle
);
217 // Get text colour as pen colour
220 if ( !(flags
& wxODCB_PAINTING_CONTROL
) )
222 dc
.DrawText(GetString( item
),
224 (r
.y
+ 0) + ( (r
.height
/2) - dc
.GetCharHeight() )/2
227 dc
.DrawLine( r
.x
+5, r
.y
+((r
.height
/4)*3), r
.x
+r
.width
- 5, r
.y
+((r
.height
/4)*3) );
231 dc
.DrawLine( r
.x
+5, r
.y
+r
.height
/2, r
.x
+r
.width
- 5, r
.y
+r
.height
/2 );
235 virtual void OnDrawBackground( wxDC
& dc
, const wxRect
& rect
,
236 int item
, int flags
) const
239 // If item is selected or even, or we are painting the
240 // combo control itself, use the default rendering.
241 if ( (flags
& (wxODCB_PAINTING_CONTROL
|wxODCB_PAINTING_SELECTED
)) ||
244 wxOwnerDrawnComboBox::OnDrawBackground(dc
,rect
,item
,flags
);
248 // Otherwise, draw every other background with different colour.
249 wxColour
bgCol(240,240,250);
250 dc
.SetBrush(wxBrush(bgCol
));
251 dc
.SetPen(wxPen(bgCol
));
252 dc
.DrawRectangle(rect
);
255 virtual wxCoord
OnMeasureItem( size_t item
) const
257 // Simply demonstrate the ability to have variable-height items
264 virtual wxCoord
OnMeasureItemWidth( size_t WXUNUSED(item
) ) const
266 return -1; // default - will be measured from text width
272 // ----------------------------------------------------------------------------
273 // wxListView Custom popup interface
274 // ----------------------------------------------------------------------------
276 #include <wx/listctrl.h>
278 class ListViewComboPopup
: public wxListView
, public wxComboPopup
285 m_itemHere
= -1; // hot item in list
288 virtual bool Create( wxWindow
* parent
)
290 return wxListView::Create(parent
,1,
291 wxPoint(0,0),wxDefaultSize
,
292 wxLC_LIST
|wxLC_SINGLE_SEL
|
293 wxLC_SORT_ASCENDING
|wxSIMPLE_BORDER
);
296 virtual wxWindow
*GetControl() { return this; }
298 virtual void SetStringValue( const wxString
& s
)
300 int n
= wxListView::FindItem(-1,s
);
301 if ( n
>= 0 && n
< GetItemCount() )
302 wxListView::Select(n
);
305 virtual wxString
GetStringValue() const
308 return wxListView::GetItemText(m_value
);
309 return wxEmptyString
;
313 // Popup event handlers
316 // Mouse hot-tracking
317 void OnMouseMove(wxMouseEvent
& event
)
319 // Move selection to cursor if it is inside the popup
322 int itemHere
= HitTest(event
.GetPosition(),resFlags
);
325 wxListView::Select(itemHere
,true);
326 m_itemHere
= itemHere
;
331 // On mouse left, set the value and close the popup
332 void OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
334 m_value
= m_itemHere
;
340 // Utilies for item manipulation
343 void AddSelection( const wxString
& selstr
)
345 wxListView::InsertItem(GetItemCount(),selstr
);
350 int m_value
; // current item index
351 int m_itemHere
; // hot item in popup
354 DECLARE_EVENT_TABLE()
357 BEGIN_EVENT_TABLE(ListViewComboPopup
, wxListView
)
358 EVT_MOTION(ListViewComboPopup::OnMouseMove
)
359 // NOTE: Left down event is used instead of left up right now
360 // since MSW wxListCtrl doesn't seem to emit left ups
362 EVT_LEFT_DOWN(ListViewComboPopup::OnMouseClick
)
366 // ----------------------------------------------------------------------------
367 // wxTreeCtrl Custom popup interface
368 // ----------------------------------------------------------------------------
370 #include <wx/treectrl.h>
372 class TreeCtrlComboPopup
: public wxTreeCtrl
, public wxComboPopup
380 virtual bool Create( wxWindow
* parent
)
382 return wxTreeCtrl::Create(parent
,1,
383 wxPoint(0,0),wxDefaultSize
,
384 wxTR_DEFAULT_STYLE
| wxTR_HIDE_ROOT
| wxSIMPLE_BORDER
);
387 virtual void OnShow()
389 // make sure selected item is visible
390 if ( m_value
.IsOk() )
391 EnsureVisible(m_value
);
394 virtual wxSize
GetAdjustedSize( int minWidth
,
395 int WXUNUSED(prefHeight
),
398 return wxSize(wxMax(300,minWidth
),wxMin(250,maxHeight
));
401 virtual wxWindow
*GetControl() { return this; }
403 // Needed by SetStringValue
404 wxTreeItemId
FindItemByText( wxTreeItemId parent
, const wxString
& text
)
406 wxTreeItemIdValue cookie
;
407 wxTreeItemId child
= GetFirstChild(parent
,cookie
);
408 while ( child
.IsOk() )
410 if ( GetItemText(child
) == text
)
414 if ( ItemHasChildren(child
) )
416 wxTreeItemId found
= FindItemByText(child
,text
);
420 child
= GetNextChild(parent
,cookie
);
422 return wxTreeItemId();
425 virtual void SetStringValue( const wxString
& s
)
427 wxTreeItemId root
= GetRootItem();
431 wxTreeItemId found
= FindItemByText(root
,s
);
434 m_value
= m_itemHere
= found
;
435 wxTreeCtrl::SelectItem(found
);
439 virtual wxString
GetStringValue() const
441 if ( m_value
.IsOk() )
442 return wxTreeCtrl::GetItemText(m_value
);
443 return wxEmptyString
;
447 // Popup event handlers
450 // Mouse hot-tracking
451 void OnMouseMove(wxMouseEvent
& event
)
454 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
455 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
457 wxTreeCtrl::SelectItem(itemHere
,true);
458 m_itemHere
= itemHere
;
463 // On mouse left, set the value and close the popup
464 void OnMouseClick(wxMouseEvent
& event
)
467 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
468 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
470 m_itemHere
= itemHere
;
480 wxTreeItemId m_value
; // current item index
481 wxTreeItemId m_itemHere
; // hot item in popup
484 DECLARE_EVENT_TABLE()
487 BEGIN_EVENT_TABLE(TreeCtrlComboPopup
, wxTreeCtrl
)
488 EVT_MOTION(TreeCtrlComboPopup::OnMouseMove
)
489 // NOTE: Left down event is used instead of left up right now
490 // since MSW wxTreeCtrl doesn't seem to emit left ups
492 EVT_LEFT_DOWN(TreeCtrlComboPopup::OnMouseClick
)
495 // ----------------------------------------------------------------------------
496 // wxComboCtrl with custom popup animation, using wxWindow::ShowWithEffect().
497 // ----------------------------------------------------------------------------
499 class wxComboCtrlWithCustomPopupAnim
: public wxComboCtrl
502 virtual bool AnimateShow( const wxRect
& rect
, int WXUNUSED(flags
) )
504 wxWindow
* win
= GetPopupWindow();
506 win
->Raise(); // This is needed
507 win
->ShowWithEffect(wxSHOW_EFFECT_BLEND
);
512 // ----------------------------------------------------------------------------
513 // wxComboCtrl with entirely custom button action (opens file dialog)
514 // ----------------------------------------------------------------------------
516 class wxFileSelectorCombo
: public wxComboCtrl
519 wxFileSelectorCombo() : wxComboCtrl() { Init(); }
521 wxFileSelectorCombo(wxWindow
*parent
,
522 wxWindowID id
= wxID_ANY
,
523 const wxString
& value
= wxEmptyString
,
524 const wxPoint
& pos
= wxDefaultPosition
,
525 const wxSize
& size
= wxDefaultSize
,
527 const wxValidator
& validator
= wxDefaultValidator
,
528 const wxString
& name
= wxComboBoxNameStr
)
532 Create(parent
,id
,value
,
534 // Style flag wxCC_STD_BUTTON makes the button
535 // behave more like a standard push button.
536 style
| wxCC_STD_BUTTON
,
540 // Prepare custom button bitmap (just '...' text)
543 dc
.SelectObject(bmp
);
545 // Draw transparent background
546 wxColour
magic(255,0,255);
547 wxBrush
magicBrush(magic
);
548 dc
.SetBrush( magicBrush
);
549 dc
.SetPen( *wxTRANSPARENT_PEN
);
550 dc
.DrawRectangle(0,0,bmp
.GetWidth(),bmp
.GetHeight());
553 wxString str
= wxT("...");
555 dc
.GetTextExtent(str
, &w
, &h
, 0, 0);
556 dc
.DrawText(str
, (bmp
.GetWidth()-w
)/2, (bmp
.GetHeight()-h
)/2);
558 dc
.SelectObject( wxNullBitmap
);
560 // Finalize transparency with a mask
561 wxMask
*mask
= new wxMask( bmp
, magic
);
564 SetButtonBitmaps(bmp
,true);
567 virtual void OnButtonClick()
569 // Show standard wxFileDialog on button click
571 wxFileDialog
dlg(this,
575 wxT("All files (*.*)|*.*"),
578 if ( dlg
.ShowModal() == wxID_OK
)
580 SetValue(dlg
.GetPath());
584 // Implement empty DoSetPopupControl to prevent assertion failure.
585 virtual void DoSetPopupControl(wxComboPopup
* WXUNUSED(popup
))
592 // Initialize member variables here
596 // ----------------------------------------------------------------------------
598 // ----------------------------------------------------------------------------
601 MyFrame::MyFrame(const wxString
& title
)
602 : wxFrame(NULL
, wxID_ANY
, title
)
604 wxBoxSizer
* topSizer
;
605 wxBoxSizer
* topRowSizer
;
606 wxBoxSizer
* colSizer
;
607 wxBoxSizer
* rowSizer
;
609 // set the frame icon
610 SetIcon(wxICON(sample
));
614 wxMenu
*fileMenu
= new wxMenu
;
616 // the "About" item should be in the help menu
617 wxMenu
*helpMenu
= new wxMenu
;
618 helpMenu
->Append(ComboCtrl_About
, wxT("&About...\tF1"), wxT("Show about dialog"));
620 fileMenu
->Append(ComboCtrl_Compare
, wxT("&Compare against wxComboBox..."),
621 wxT("Show some wxOwnerDrawnComboBoxes side-by-side with native wxComboBoxes."));
622 fileMenu
->AppendSeparator();
623 fileMenu
->Append(ComboCtrl_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
625 // now append the freshly created menu to the menu bar...
626 wxMenuBar
*menuBar
= new wxMenuBar();
627 menuBar
->Append(fileMenu
, wxT("&File"));
628 menuBar
->Append(helpMenu
, wxT("&Help"));
630 // ... and attach this menu bar to the frame
632 #endif // wxUSE_MENUS
634 wxPanel
* panel
= new wxPanel(this);
636 // Prepare log window right away since it shows EVT_TEXTs
637 m_logWin
= new wxTextCtrl(panel
, 105, wxEmptyString
,
641 wxLogTextCtrl
* logger
= new wxLogTextCtrl(m_logWin
);
642 m_logOld
= logger
->SetActiveTarget(logger
);
643 logger
->DisableTimestamp();
646 topSizer
= new wxBoxSizer( wxVERTICAL
);
648 topRowSizer
= new wxBoxSizer( wxHORIZONTAL
);
650 colSizer
= new wxBoxSizer( wxVERTICAL
);
654 wxGenericComboCtrl
* gcc
;
655 wxOwnerDrawnComboBox
* odc
;
657 // Create common strings array
658 m_arrItems
.Add( wxT("Solid") );
659 m_arrItems
.Add( wxT("Transparent") );
660 m_arrItems
.Add( wxT("Dot") );
661 m_arrItems
.Add( wxT("Long Dash") );
662 m_arrItems
.Add( wxT("Short Dash") );
663 m_arrItems
.Add( wxT("Dot Dash") );
664 m_arrItems
.Add( wxT("Backward Diagonal Hatch") );
665 m_arrItems
.Add( wxT("Cross-diagonal Hatch") );
666 m_arrItems
.Add( wxT("Forward Diagonal Hatch") );
667 m_arrItems
.Add( wxT("Cross Hatch") );
668 m_arrItems
.Add( wxT("Horizontal Hatch") );
669 m_arrItems
.Add( wxT("Vertical Hatch") );
673 // Create pen selector ODComboBox with owner-drawn items
675 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
676 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
677 wxT("OwnerDrawnComboBox with owner-drawn items:")), 1,
678 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
679 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
681 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
684 // When defining derivative class for callbacks, we need
685 // to use two-stage creation (or redefine the common wx
687 odc
= new wxPenStyleComboBox();
688 odc
->Create(panel
,wxID_ANY
,wxEmptyString
,
689 wxDefaultPosition
, wxDefaultSize
,
691 wxCB_READONLY
//wxNO_BORDER | wxCB_READONLY
695 odc
->SetSelection(0);
697 rowSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
698 rowSizer
->AddStretchSpacer(1);
699 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
704 // Same but with changed button position
706 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
707 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
708 wxT("OwnerDrawnComboBox with owner-drawn items and button on the left:")), 1,
709 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
710 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
712 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
715 // When defining derivative class for callbacks, we need
716 // to use two-stage creation (or redefine the common wx
718 odc
= new wxPenStyleComboBox();
719 odc
->Create(panel
,wxID_ANY
,wxEmptyString
,
720 wxDefaultPosition
, wxDefaultSize
,
722 wxCB_READONLY
//wxNO_BORDER | wxCB_READONLY
726 odc
->SetSelection(0);
728 // Use button size that is slightly smaller than the default.
729 wxSize butSize
= odc
->GetButtonSize();
730 odc
->SetButtonPosition(butSize
.x
- 2, // button width
731 butSize
.y
- 6, // button height
733 2 // horizontal spacing
736 rowSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
737 rowSizer
->AddStretchSpacer(1);
738 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
742 // List View wxComboCtrl
745 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
746 rowSizer
->Add( new wxStaticText(panel
,
748 "List View wxComboCtrl (custom animation):"),
749 1, wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
750 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,wxT("Tree Ctrl wxComboCtrl:")), 1,
751 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
752 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
754 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
755 cc
= new wxComboCtrlWithCustomPopupAnim();
757 // Let's set a custom style for the contained wxTextCtrl. We need to
758 // use two-step creation for it to work properly.
759 cc
->SetTextCtrlStyle(wxTE_RIGHT
);
761 cc
->Create(panel
, wxID_ANY
, wxEmptyString
);
763 // Make sure we use popup that allows focusing the listview.
764 cc
->UseAltPopupWindow();
766 cc
->SetPopupMinWidth(300);
768 ListViewComboPopup
* iface
= new ListViewComboPopup();
769 cc
->SetPopupControl(iface
);
772 for ( i
=0; i
<100; i
++ )
773 iface
->AddSelection( wxString::Format(wxT("Item %02i"),i
));
775 rowSizer
->Add( cc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
779 // Tree Ctrl wxComboCtrl
782 // Note that we test that wxGenericComboCtrl works
783 gcc
= new wxGenericComboCtrl(panel
,wxID_ANY
,wxEmptyString
,
784 wxDefaultPosition
, wxDefaultSize
);
786 // Make sure we use popup that allows focusing the treectrl.
787 gcc
->UseAltPopupWindow();
789 // Set popup interface right away, otherwise some of the calls
791 TreeCtrlComboPopup
* tcPopup
= new TreeCtrlComboPopup();
792 gcc
->SetPopupControl(tcPopup
);
794 // Add items using wxTreeCtrl methods directly
795 wxTreeItemId rootId
= tcPopup
->AddRoot(wxT("<hidden_root>"));
797 wxTreeItemId groupId
;
799 for ( i
=0; i
<4; i
++ )
801 groupId
= tcPopup
->AppendItem(rootId
,
802 wxString::Format(wxT("Branch %02i"),i
));
805 for ( n
=0; n
<25; n
++ )
806 tcPopup
->AppendItem(groupId
,
807 wxString::Format(wxT("Subitem %02i"),(i
*25)+n
));
810 gcc
->SetValue(wxT("Subitem 05"));
812 // Move button to left - it makes more sense for a tree ctrl
813 gcc
->SetButtonPosition(-1, // button width
816 0 // horizontal spacing
819 rowSizer
->Add( gcc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
821 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
824 wxInitAllImageHandlers();
827 // Custom Dropbutton Bitmaps
828 // (second one uses blank button background)
830 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
831 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
832 wxT("OwnerDrawnComboBox with simple dropbutton graphics:")), 1,
833 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
835 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
837 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
839 odc
= new wxOwnerDrawnComboBox(panel
,wxID_ANY
,wxEmptyString
,
840 wxDefaultPosition
, wxDefaultSize
,
842 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
845 wxOwnerDrawnComboBox
* odc2
;
846 odc2
= new wxOwnerDrawnComboBox(panel
,wxID_ANY
,wxEmptyString
,
847 wxDefaultPosition
, wxDefaultSize
,
849 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
852 // Load images from disk
853 wxImage
imgNormal(wxT("dropbutn.png"));
854 wxImage
imgPressed(wxT("dropbutp.png"));
855 wxImage
imgHover(wxT("dropbuth.png"));
857 if ( imgNormal
.IsOk() && imgPressed
.IsOk() && imgHover
.IsOk() )
859 wxBitmap
bmpNormal(imgNormal
);
860 wxBitmap
bmpPressed(imgPressed
);
861 wxBitmap
bmpHover(imgHover
);
862 odc
->SetButtonBitmaps(bmpNormal
,false,bmpPressed
,bmpHover
);
863 odc2
->SetButtonBitmaps(bmpNormal
,true,bmpPressed
,bmpHover
);
866 wxLogError(wxT("Dropbutton images not found"));
868 //odc2->SetButtonPosition(0, // width adjustment
869 // 0, // height adjustment
871 // 0 // horizontal spacing
874 rowSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
875 rowSizer
->Add( odc2
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
876 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
881 // wxComboCtrl with totally custom button action (open file dialog)
883 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
884 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
885 wxT("wxComboCtrl with custom button action:")), 1,
886 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
889 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
891 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
892 wxFileSelectorCombo
* fsc
;
894 fsc
= new wxFileSelectorCombo(panel
,wxID_ANY
,wxEmptyString
,
895 wxDefaultPosition
, wxDefaultSize
,
899 rowSizer
->Add( fsc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
900 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
903 // Make sure GetFeatures is implemented
904 wxComboCtrl::GetFeatures();
907 topRowSizer
->Add( colSizer
, 1, wxALL
, 2 );
909 colSizer
= new wxBoxSizer( wxVERTICAL
);
911 colSizer
->AddSpacer(8);
912 colSizer
->Add( new wxStaticText(panel
, wxID_ANY
, wxT("Log Messages:")), 0, wxTOP
|wxLEFT
, 3 );
913 colSizer
->Add( m_logWin
, 1, wxEXPAND
|wxALL
, 3 );
915 topRowSizer
->Add( colSizer
, 1, wxEXPAND
|wxALL
, 2 );
916 topSizer
->Add( topRowSizer
, 1, wxEXPAND
);
918 panel
->SetSizer( topSizer
);
919 topSizer
->SetSizeHints( panel
);
927 void MyFrame::OnComboBoxUpdate( wxCommandEvent
& event
)
929 // Don't show messages for the log output window (it'll crash)
930 if ( !event
.GetEventObject()->IsKindOf(CLASSINFO(wxComboCtrl
)) )
933 if ( event
.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED
)
935 wxLogDebug(wxT("EVT_COMBOBOX(id=%i,selection=%i)"),event
.GetId(),event
.GetSelection());
937 else if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
939 wxLogDebug(wxT("EVT_TEXT(id=%i,string=\"%s\")"),event
.GetId(),event
.GetString().c_str());
943 void MyFrame::OnShowComparison( wxCommandEvent
& WXUNUSED(event
) )
946 // Show some wxOwnerDrawComboBoxes for comparison
949 wxBoxSizer
* colSizer
;
950 wxBoxSizer
* rowSizer
;
951 wxStaticBoxSizer
* groupSizer
;
954 wxOwnerDrawnComboBox
* odc
;
956 const int border
= 4;
958 wxDialog
* dlg
= new wxDialog(this,wxID_ANY
,
959 wxT("Compare against wxComboBox"),
960 wxDefaultPosition
,wxDefaultSize
,
961 wxDEFAULT_DIALOG_STYLE
|wxRESIZE_BORDER
);
963 colSizer
= new wxBoxSizer( wxVERTICAL
);
965 rowSizer
= new wxBoxSizer(wxHORIZONTAL
);
967 groupSizer
= new wxStaticBoxSizer(new wxStaticBox(dlg
,wxID_ANY
,wxT(" wxOwnerDrawnComboBox ")),
970 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Writable, sorted:")), 0,
971 wxALIGN_CENTER_VERTICAL
|wxRIGHT
|wxEXPAND
, border
);
973 odc
= new wxOwnerDrawnComboBox(dlg
,wxID_ANY
,wxEmptyString
,
974 wxDefaultPosition
, wxDefaultSize
,
976 wxCB_SORT
// wxNO_BORDER|wxCB_READONLY
979 odc
->Append(wxT("H - Appended Item")); // test sorting in append
981 odc
->SetValue(wxT("Dot Dash"));
983 groupSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
986 // Readonly ODComboBox
987 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Read-only:")), 0,
988 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
990 odc
= new wxOwnerDrawnComboBox(dlg
,wxID_ANY
,wxEmptyString
,
991 wxDefaultPosition
, wxDefaultSize
,
993 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
996 odc
->SetValue(wxT("Dot Dash"));
997 odc
->SetText(wxT("Dot Dash (Testing SetText)"));
999 groupSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1002 // Disabled ODComboBox
1003 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Disabled:")), 0,
1004 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1006 odc
= new wxOwnerDrawnComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1007 wxDefaultPosition
, wxDefaultSize
,
1009 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1012 odc
->SetValue(wxT("Dot Dash"));
1015 groupSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1017 rowSizer
->Add( groupSizer
, 1, wxEXPAND
|wxALL
, border
);
1020 groupSizer
= new wxStaticBoxSizer(new wxStaticBox(dlg
,wxID_ANY
,wxT(" wxComboBox ")),
1026 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Writable, sorted:")), 0,
1027 wxALIGN_CENTER_VERTICAL
|wxRIGHT
|wxEXPAND
, border
);
1029 cb
= new wxComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1030 wxDefaultPosition
, wxDefaultSize
,
1032 wxCB_SORT
// wxNO_BORDER|wxCB_READONLY
1035 cb
->Append(wxT("H - Appended Item")); // test sorting in append
1037 cb
->SetValue(wxT("Dot Dash"));
1039 groupSizer
->Add( cb
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1042 // Readonly wxComboBox
1043 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Read-only:")), 0,
1044 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1046 cb
= new wxComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1047 wxDefaultPosition
, wxDefaultSize
,
1049 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1052 cb
->SetValue(wxT("Dot Dash"));
1054 groupSizer
->Add( cb
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1057 // Disabled wxComboBox
1058 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Disabled:")), 0,
1059 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1061 cb
= new wxComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1062 wxDefaultPosition
, wxDefaultSize
,
1064 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1067 cb
->SetValue(wxT("Dot Dash"));
1070 groupSizer
->Add( cb
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1072 rowSizer
->Add( groupSizer
, 1, wxEXPAND
|wxALL
, border
);
1074 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, border
);
1076 dlg
->SetSizer( colSizer
);
1077 colSizer
->SetSizeHints( dlg
);
1079 dlg
->SetSize(60,240);
1086 delete wxLog::SetActiveTarget(m_logOld
);
1089 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
1091 // true is to force the frame to close
1095 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
1097 wxMessageBox(wxString::Format(
1098 wxT("Welcome to %s!\n")
1100 wxT("This is the wxWidgets wxComboCtrl and wxOwnerDrawnComboBox sample\n")
1101 wxT("running under %s."),
1103 wxGetOsDescription().c_str()
1105 wxT("About wxComboCtrl sample"),
1106 wxOK
| wxICON_INFORMATION
,
1110 void MyFrame::OnIdle(wxIdleEvent
& event
)
1112 // This code is useful for debugging focus problems
1113 // (which are plentiful when dealing with popup windows).
1115 static wxWindow
* lastFocus
= (wxWindow
*) NULL
;
1117 wxWindow
* curFocus
= ::wxWindow::FindFocus();
1119 if ( curFocus
!= lastFocus
)
1121 const wxChar
* className
= wxT("<none>");
1123 className
= curFocus
->GetClassInfo()->GetClassName();
1124 lastFocus
= curFocus
;
1125 wxLogDebug( wxT("FOCUSED: %s %X"),
1127 (unsigned int)curFocus
);