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
);
89 wxCheckBox
* m_cbUseAnim
;
95 // Common list of items for all dialogs.
96 wxArrayString m_arrItems
;
99 // any class wishing to process wxWidgets events must use this macro
100 DECLARE_EVENT_TABLE()
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 // IDs for the controls and the menu commands
110 ComboControl_Compare
= wxID_HIGHEST
,
113 ComboControl_Quit
= wxID_EXIT
,
115 // it is important for the id corresponding to the "About" command to have
116 // this standard value as otherwise it won't be handled properly under Mac
117 // (where it is special and put into the "Apple" menu)
118 ComboControl_About
= wxID_ABOUT
121 // ----------------------------------------------------------------------------
122 // event tables and other macros for wxWidgets
123 // ----------------------------------------------------------------------------
125 // the event tables connect the wxWidgets events with the functions (event
126 // handlers) which process them. It can be also done at run-time, but for the
127 // simple menu events like this the static method is much simpler.
128 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
129 EVT_TEXT(wxID_ANY
,MyFrame::OnComboBoxUpdate
)
130 EVT_COMBOBOX(wxID_ANY
,MyFrame::OnComboBoxUpdate
)
132 EVT_MENU(ComboControl_Compare
, MyFrame::OnShowComparison
)
133 EVT_MENU(ComboControl_Quit
, MyFrame::OnQuit
)
134 EVT_MENU(ComboControl_About
, MyFrame::OnAbout
)
136 EVT_IDLE(MyFrame::OnIdle
)
139 // Create a new application object: this macro will allow wxWidgets to create
140 // the application object during program execution (it's better than using a
141 // static object for many reasons) and also implements the accessor function
142 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
146 // ============================================================================
148 // ============================================================================
150 // ----------------------------------------------------------------------------
151 // the application class
152 // ----------------------------------------------------------------------------
154 // 'Main program' equivalent: the program execution "starts" here
157 if ( !wxApp::OnInit() )
160 // create the main application window
161 MyFrame
*frame
= new MyFrame(wxT("wxComboCtrl and wxOwnerDrawnComboBox Sample"));
163 // and show it (the frames, unlike simple controls, are not shown when
164 // created initially)
167 // success: wxApp::OnRun() will be called which will enter the main message
168 // loop and the application will run. If we returned false here, the
169 // application would exit immediately.
174 // ----------------------------------------------------------------------------
175 // wxOwnerDrawnComboBox with custom paint list items
176 // ----------------------------------------------------------------------------
178 class wxPenStyleComboBox
: public wxOwnerDrawnComboBox
181 virtual void OnDrawItem( wxDC
& dc
,
186 if ( item
== wxNOT_FOUND
)
193 int penStyle
= wxSOLID
;
195 penStyle
= wxTRANSPARENT
;
196 else if ( item
== 2 )
198 else if ( item
== 3 )
199 penStyle
= wxLONG_DASH
;
200 else if ( item
== 4 )
201 penStyle
= wxSHORT_DASH
;
202 else if ( item
== 5 )
203 penStyle
= wxDOT_DASH
;
204 else if ( item
== 6 )
205 penStyle
= wxBDIAGONAL_HATCH
;
206 else if ( item
== 7 )
207 penStyle
= wxCROSSDIAG_HATCH
;
208 else if ( item
== 8 )
209 penStyle
= wxFDIAGONAL_HATCH
;
210 else if ( item
== 9 )
211 penStyle
= wxCROSS_HATCH
;
212 else if ( item
== 10 )
213 penStyle
= wxHORIZONTAL_HATCH
;
214 else if ( item
== 11 )
215 penStyle
= wxVERTICAL_HATCH
;
217 wxPen
pen( dc
.GetTextForeground(), 3, penStyle
);
219 // Get text colour as pen colour
222 if ( !(flags
& wxODCB_PAINTING_CONTROL
) )
224 dc
.DrawText(GetString( item
),
226 (r
.y
+ 0) + ( (r
.height
/2) - dc
.GetCharHeight() )/2
229 dc
.DrawLine( r
.x
+5, r
.y
+((r
.height
/4)*3), r
.x
+r
.width
- 5, r
.y
+((r
.height
/4)*3) );
233 dc
.DrawLine( r
.x
+5, r
.y
+r
.height
/2, r
.x
+r
.width
- 5, r
.y
+r
.height
/2 );
237 virtual void OnDrawBackground( wxDC
& dc
, const wxRect
& rect
,
238 int item
, int flags
) const
241 // If item is selected or even, or we are painting the
242 // combo control itself, use the default rendering.
243 if ( (flags
& (wxODCB_PAINTING_CONTROL
|wxODCB_PAINTING_SELECTED
)) ||
246 wxOwnerDrawnComboBox::OnDrawBackground(dc
,rect
,item
,flags
);
250 // Otherwise, draw every other background with different colour.
251 wxColour
bgCol(240,240,250);
252 dc
.SetBrush(wxBrush(bgCol
));
253 dc
.SetPen(wxPen(bgCol
));
254 dc
.DrawRectangle(rect
);
257 virtual wxCoord
OnMeasureItem( size_t item
) const
259 // Simply demonstrate the ability to have variable-height items
266 virtual wxCoord
OnMeasureItemWidth( size_t WXUNUSED(item
) ) const
268 return -1; // default - will be measured from text width
274 // ----------------------------------------------------------------------------
275 // wxListView Custom popup interface
276 // ----------------------------------------------------------------------------
278 #include <wx/listctrl.h>
280 class ListViewComboPopup
: public wxListView
, public wxComboPopup
287 m_itemHere
= -1; // hot item in list
290 virtual bool Create( wxWindow
* parent
)
292 return wxListView::Create(parent
,1,
293 wxPoint(0,0),wxDefaultSize
,
294 wxLC_LIST
|wxLC_SINGLE_SEL
|
295 wxLC_SORT_ASCENDING
|wxSIMPLE_BORDER
);
298 virtual wxWindow
*GetControl() { return this; }
300 virtual void SetStringValue( const wxString
& s
)
302 int n
= wxListView::FindItem(-1,s
);
303 if ( n
>= 0 && n
< GetItemCount() )
304 wxListView::Select(n
);
307 virtual wxString
GetStringValue() const
310 return wxListView::GetItemText(m_value
);
311 return wxEmptyString
;
315 // Popup event handlers
318 // Mouse hot-tracking
319 void OnMouseMove(wxMouseEvent
& event
)
321 // Move selection to cursor if it is inside the popup
324 int itemHere
= HitTest(event
.GetPosition(),resFlags
);
327 wxListView::Select(itemHere
,true);
328 m_itemHere
= itemHere
;
333 // On mouse left, set the value and close the popup
334 void OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
336 m_value
= m_itemHere
;
342 // Utilies for item manipulation
345 void AddSelection( const wxString
& selstr
)
347 wxListView::InsertItem(GetItemCount(),selstr
);
352 int m_value
; // current item index
353 int m_itemHere
; // hot item in popup
356 DECLARE_EVENT_TABLE()
359 BEGIN_EVENT_TABLE(ListViewComboPopup
, wxListView
)
360 EVT_MOTION(ListViewComboPopup::OnMouseMove
)
361 // NOTE: Left down event is used instead of left up right now
362 // since MSW wxListCtrl doesn't seem to emit left ups
364 EVT_LEFT_DOWN(ListViewComboPopup::OnMouseClick
)
368 // ----------------------------------------------------------------------------
369 // wxTreeCtrl Custom popup interface
370 // ----------------------------------------------------------------------------
372 #include <wx/treectrl.h>
374 class TreeCtrlComboPopup
: public wxTreeCtrl
, public wxComboPopup
382 virtual bool Create( wxWindow
* parent
)
384 return wxTreeCtrl::Create(parent
,1,
385 wxPoint(0,0),wxDefaultSize
,
386 wxTR_DEFAULT_STYLE
| wxTR_HIDE_ROOT
| wxSIMPLE_BORDER
);
389 virtual void OnShow()
391 // make sure selected item is visible
392 if ( m_value
.IsOk() )
393 EnsureVisible(m_value
);
396 virtual wxSize
GetAdjustedSize( int minWidth
,
397 int WXUNUSED(prefHeight
),
400 return wxSize(wxMax(300,minWidth
),wxMin(250,maxHeight
));
403 virtual wxWindow
*GetControl() { return this; }
405 // Needed by SetStringValue
406 wxTreeItemId
FindItemByText( wxTreeItemId parent
, const wxString
& text
)
408 wxTreeItemIdValue cookie
;
409 wxTreeItemId child
= GetFirstChild(parent
,cookie
);
410 while ( child
.IsOk() )
412 if ( GetItemText(child
) == text
)
416 if ( ItemHasChildren(child
) )
418 wxTreeItemId found
= FindItemByText(child
,text
);
422 child
= GetNextChild(parent
,cookie
);
424 return wxTreeItemId();
427 virtual void SetStringValue( const wxString
& s
)
429 wxTreeItemId root
= GetRootItem();
433 wxTreeItemId found
= FindItemByText(root
,s
);
436 m_value
= m_itemHere
= found
;
437 wxTreeCtrl::SelectItem(found
);
441 virtual wxString
GetStringValue() const
443 if ( m_value
.IsOk() )
444 return wxTreeCtrl::GetItemText(m_value
);
445 return wxEmptyString
;
449 // Popup event handlers
452 // Mouse hot-tracking
453 void OnMouseMove(wxMouseEvent
& event
)
456 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
457 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
459 wxTreeCtrl::SelectItem(itemHere
,true);
460 m_itemHere
= itemHere
;
465 // On mouse left, set the value and close the popup
466 void OnMouseClick(wxMouseEvent
& event
)
469 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
470 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
472 m_itemHere
= itemHere
;
482 wxTreeItemId m_value
; // current item index
483 wxTreeItemId m_itemHere
; // hot item in popup
486 DECLARE_EVENT_TABLE()
489 BEGIN_EVENT_TABLE(TreeCtrlComboPopup
, wxTreeCtrl
)
490 EVT_MOTION(TreeCtrlComboPopup::OnMouseMove
)
491 // NOTE: Left down event is used instead of left up right now
492 // since MSW wxTreeCtrl doesn't seem to emit left ups
494 EVT_LEFT_DOWN(TreeCtrlComboPopup::OnMouseClick
)
497 // ----------------------------------------------------------------------------
498 // wxComboCtrl with custom popup animation. We use EVT_TIMER, which is quite
499 // safe, but requires much more can than doing it in a single function (ie.
500 // AnimateShow) and using combination of wxSleep and wxSafeYield.
501 // ----------------------------------------------------------------------------
505 #define CUSTOM_COMBOBOX_ANIMATION_DURATION 200 // In milliseconds
507 #include "wx/timer.h"
509 class wxComboCtrlWithCustomPopupAnim
: public wxComboCtrl
513 virtual bool AnimateShow( const wxRect
& rect
, int flags
)
515 MyFrame
* myFrame
= (MyFrame
*) ::wxGetTopLevelParent(this);
517 if ( !myFrame
->m_cbUseAnim
->GetValue() )
520 m_animStart
= ::wxGetLocalTimeMillis();
526 wxBitmap
bitmap( rect
.width
, rect
.height
, -1 );
527 wxMemoryDC
memdc( bitmap
);
528 memdc
.Blit( 0, 0, rect
.width
, rect
.height
, &dc
, rect
.x
, rect
.y
);
529 memdc
.SelectObject(wxNullBitmap
);
530 m_animBackBitmap
= bitmap
;
532 m_animTimer
.SetOwner( this, wxID_ANY
);
533 m_animTimer
.Start( 10, wxTIMER_CONTINUOUS
);
540 void OnTimerEvent( wxTimerEvent
& WXUNUSED(event
) )
547 bool stopTimer
= false;
549 wxWindow
* popup
= GetPopupControl()->GetControl();
551 const wxRect
& rect
= m_animRect
;
553 // Popup was hidden before it was fully shown?
554 if ( IsPopupWindowState(Hidden
) )
560 wxLongLong t
= ::wxGetLocalTimeMillis();
562 int pos
= (int) (t
-m_animStart
).GetLo();
563 if ( pos
< CUSTOM_COMBOBOX_ANIMATION_DURATION
)
566 // Actual animation happens here
568 int width
= rect
.width
;
569 int height
= rect
.height
;
571 int center_x
= rect
.x
+ (width
/2);
572 int center_y
= rect
.y
+ (height
/2);
574 double d_height
= (double) height
;
576 dc
.SetPen( *wxBLACK_PEN
);
577 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
579 int w
= (((pos
*256)/CUSTOM_COMBOBOX_ANIMATION_DURATION
)*width
)/256;
581 double ratio
= ((double)w
/ (double)width
);
582 int h
= (int)(d_height
* ratio
);
583 dc
.DrawBitmap( m_animBackBitmap
, rect
.x
, rect
.y
);
584 dc
.DrawRectangle( center_x
- w
/2, center_y
- h
/2, w
, h
);
594 dc
.DrawBitmap( m_animBackBitmap
, rect
.x
, rect
.y
);
597 DoShowPopup( m_animRect
, m_animFlags
);
601 // Popup animation related
602 wxLongLong m_animStart
;
605 wxBitmap m_animBackBitmap
;
608 DECLARE_EVENT_TABLE()
611 BEGIN_EVENT_TABLE(wxComboCtrlWithCustomPopupAnim
, wxComboCtrl
)
612 EVT_TIMER(wxID_ANY
, wxComboCtrlWithCustomPopupAnim::OnTimerEvent
)
617 #define wxComboCtrlWithCustomPopupAnim wxComboCtrl
621 // ----------------------------------------------------------------------------
622 // wxComboCtrl with entirely custom button action (opens file dialog)
623 // ----------------------------------------------------------------------------
625 class wxFileSelectorCombo
: public wxComboCtrl
628 wxFileSelectorCombo() : wxComboCtrl() { Init(); }
630 wxFileSelectorCombo(wxWindow
*parent
,
631 wxWindowID id
= wxID_ANY
,
632 const wxString
& value
= wxEmptyString
,
633 const wxPoint
& pos
= wxDefaultPosition
,
634 const wxSize
& size
= wxDefaultSize
,
636 const wxValidator
& validator
= wxDefaultValidator
,
637 const wxString
& name
= wxComboBoxNameStr
)
641 Create(parent
,id
,value
,
643 // Style flag wxCC_STD_BUTTON makes the button
644 // behave more like a standard push button.
645 style
| wxCC_STD_BUTTON
,
649 // Prepare custom button bitmap (just '...' text)
652 dc
.SelectObject(bmp
);
654 // Draw transparent background
655 wxColour
magic(255,0,255);
656 wxBrush
magicBrush(magic
);
657 dc
.SetBrush( magicBrush
);
658 dc
.SetPen( *wxTRANSPARENT_PEN
);
659 dc
.DrawRectangle(0,0,bmp
.GetWidth(),bmp
.GetHeight());
662 wxString str
= wxT("...");
664 dc
.GetTextExtent(str
, &w
, &h
, 0, 0);
665 dc
.DrawText(str
, (bmp
.GetWidth()-w
)/2, (bmp
.GetHeight()-h
)/2);
667 dc
.SelectObject( wxNullBitmap
);
669 // Finalize transparency with a mask
670 wxMask
*mask
= new wxMask( bmp
, magic
);
673 SetButtonBitmaps(bmp
,true);
676 virtual void OnButtonClick()
678 // Show standard wxFileDialog on button click
680 wxFileDialog
dlg(this,
684 wxT("All files (*.*)|*.*"),
687 if ( dlg
.ShowModal() == wxID_OK
)
689 SetValue(dlg
.GetPath());
693 // Implement empty DoSetPopupControl to prevent assertion failure.
694 virtual void DoSetPopupControl(wxComboPopup
* WXUNUSED(popup
))
701 // Initialize member variables here
705 // ----------------------------------------------------------------------------
707 // ----------------------------------------------------------------------------
710 MyFrame::MyFrame(const wxString
& title
)
711 : wxFrame(NULL
, wxID_ANY
, title
)
713 wxBoxSizer
* topSizer
;
714 wxBoxSizer
* topRowSizer
;
715 wxBoxSizer
* colSizer
;
716 wxBoxSizer
* rowSizer
;
718 // set the frame icon
719 SetIcon(wxICON(sample
));
723 wxMenu
*fileMenu
= new wxMenu
;
725 // the "About" item should be in the help menu
726 wxMenu
*helpMenu
= new wxMenu
;
727 helpMenu
->Append(ComboControl_About
, wxT("&About...\tF1"), wxT("Show about dialog"));
729 fileMenu
->Append(ComboControl_Compare
, wxT("&Compare against wxComboBox..."),
730 wxT("Show some wxOwnerDrawnComboBoxes side-by-side with native wxComboBoxes."));
731 fileMenu
->AppendSeparator();
732 fileMenu
->Append(ComboControl_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
734 // now append the freshly created menu to the menu bar...
735 wxMenuBar
*menuBar
= new wxMenuBar();
736 menuBar
->Append(fileMenu
, wxT("&File"));
737 menuBar
->Append(helpMenu
, wxT("&Help"));
739 // ... and attach this menu bar to the frame
741 #endif // wxUSE_MENUS
743 wxPanel
* panel
= new wxPanel(this);
745 // Prepare log window right away since it shows EVT_TEXTs
746 m_logWin
= new wxTextCtrl(panel
, 105, wxEmptyString
,
750 wxLogTextCtrl
* logger
= new wxLogTextCtrl(m_logWin
);
751 m_logOld
= logger
->SetActiveTarget(logger
);
752 logger
->DisableTimestamp();
755 topSizer
= new wxBoxSizer( wxVERTICAL
);
757 topRowSizer
= new wxBoxSizer( wxHORIZONTAL
);
759 colSizer
= new wxBoxSizer( wxVERTICAL
);
763 wxGenericComboCtrl
* gcc
;
764 wxOwnerDrawnComboBox
* odc
;
766 // Create common strings array
767 m_arrItems
.Add( wxT("Solid") );
768 m_arrItems
.Add( wxT("Transparent") );
769 m_arrItems
.Add( wxT("Dot") );
770 m_arrItems
.Add( wxT("Long Dash") );
771 m_arrItems
.Add( wxT("Short Dash") );
772 m_arrItems
.Add( wxT("Dot Dash") );
773 m_arrItems
.Add( wxT("Backward Diagonal Hatch") );
774 m_arrItems
.Add( wxT("Cross-diagonal Hatch") );
775 m_arrItems
.Add( wxT("Forward Diagonal Hatch") );
776 m_arrItems
.Add( wxT("Cross Hatch") );
777 m_arrItems
.Add( wxT("Horizontal Hatch") );
778 m_arrItems
.Add( wxT("Vertical Hatch") );
782 // Create pen selector ODComboBox with owner-drawn items
784 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
785 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
786 wxT("OwnerDrawnComboBox with owner-drawn items:")), 1,
787 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
788 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
790 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
793 // When defining derivative class for callbacks, we need
794 // to use two-stage creation (or redefine the common wx
796 odc
= new wxPenStyleComboBox();
797 odc
->Create(panel
,wxID_ANY
,wxEmptyString
,
798 wxDefaultPosition
, wxDefaultSize
,
800 wxCB_READONLY
//wxNO_BORDER | wxCB_READONLY
804 odc
->SetSelection(0);
806 rowSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
807 rowSizer
->AddStretchSpacer(1);
808 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
813 // Same but with changed button position
815 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
816 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
817 wxT("OwnerDrawnComboBox with owner-drawn items and button on the left:")), 1,
818 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
819 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
821 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
824 // When defining derivative class for callbacks, we need
825 // to use two-stage creation (or redefine the common wx
827 odc
= new wxPenStyleComboBox();
828 odc
->Create(panel
,wxID_ANY
,wxEmptyString
,
829 wxDefaultPosition
, wxDefaultSize
,
831 wxCB_READONLY
//wxNO_BORDER | wxCB_READONLY
835 odc
->SetSelection(0);
837 // Use button size that is slightly smaller than the default.
838 wxSize butSize
= odc
->GetButtonSize();
839 odc
->SetButtonPosition(butSize
.x
- 2, // button width
840 butSize
.y
- 6, // button height
842 2 // horizontal spacing
845 rowSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
846 rowSizer
->AddStretchSpacer(1);
847 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
851 // List View wxComboCtrl
854 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
855 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,wxT("List View wxComboCtrl:")), 1,
856 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
857 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,wxT("Tree Ctrl wxComboControl:")), 1,
858 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
859 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
861 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
862 cc
= new wxComboCtrlWithCustomPopupAnim();
863 cc
->Create(panel
, wxID_ANY
, wxEmptyString
);
865 // Make sure we use popup that allows focusing the listview.
866 cc
->UseAltPopupWindow();
868 cc
->SetPopupMinWidth(300);
870 ListViewComboPopup
* iface
= new ListViewComboPopup();
871 cc
->SetPopupControl(iface
);
874 for ( i
=0; i
<100; i
++ )
875 iface
->AddSelection( wxString::Format(wxT("Item %02i"),i
));
877 rowSizer
->Add( cc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
881 // Tree Ctrl wxComboCtrl
884 // Note that we test that wxGenericComboCtrl works
885 gcc
= new wxGenericComboCtrl(panel
,wxID_ANY
,wxEmptyString
,
886 wxDefaultPosition
, wxDefaultSize
);
888 // Make sure we use popup that allows focusing the treectrl.
889 gcc
->UseAltPopupWindow();
891 // Set popup interface right away, otherwise some of the calls
893 TreeCtrlComboPopup
* tcPopup
= new TreeCtrlComboPopup();
894 gcc
->SetPopupControl(tcPopup
);
896 // Add items using wxTreeCtrl methods directly
897 wxTreeItemId rootId
= tcPopup
->AddRoot(wxT("<hidden_root>"));
899 wxTreeItemId groupId
;
901 for ( i
=0; i
<4; i
++ )
903 groupId
= tcPopup
->AppendItem(rootId
,
904 wxString::Format(wxT("Branch %02i"),i
));
907 for ( n
=0; n
<25; n
++ )
908 tcPopup
->AppendItem(groupId
,
909 wxString::Format(wxT("Subitem %02i"),(i
*25)+n
));
912 gcc
->SetValue(wxT("Subitem 05"));
914 // Move button to left - it makes more sense for a tree ctrl
915 gcc
->SetButtonPosition(-1, // button width
918 0 // horizontal spacing
921 rowSizer
->Add( gcc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
923 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
926 wxInitAllImageHandlers();
929 // Custom Dropbutton Bitmaps
930 // (second one uses blank button background)
932 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
933 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
934 wxT("OwnerDrawnComboBox with simple dropbutton graphics:")), 1,
935 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
937 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
939 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
941 odc
= new wxOwnerDrawnComboBox(panel
,wxID_ANY
,wxEmptyString
,
942 wxDefaultPosition
, wxDefaultSize
,
944 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
947 wxOwnerDrawnComboBox
* odc2
;
948 odc2
= new wxOwnerDrawnComboBox(panel
,wxID_ANY
,wxEmptyString
,
949 wxDefaultPosition
, wxDefaultSize
,
951 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
954 // Load images from disk
955 wxImage
imgNormal(wxT("dropbutn.png"));
956 wxImage
imgPressed(wxT("dropbutp.png"));
957 wxImage
imgHover(wxT("dropbuth.png"));
959 if ( imgNormal
.IsOk() && imgPressed
.IsOk() && imgHover
.IsOk() )
961 wxBitmap
bmpNormal(imgNormal
);
962 wxBitmap
bmpPressed(imgPressed
);
963 wxBitmap
bmpHover(imgHover
);
964 odc
->SetButtonBitmaps(bmpNormal
,false,bmpPressed
,bmpHover
);
965 odc2
->SetButtonBitmaps(bmpNormal
,true,bmpPressed
,bmpHover
);
968 wxLogError(wxT("Dropbutton images not found"));
970 //odc2->SetButtonPosition(0, // width adjustment
971 // 0, // height adjustment
973 // 0 // horizontal spacing
976 rowSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
977 rowSizer
->Add( odc2
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
978 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
983 // wxComboCtrl with totally custom button action (open file dialog)
985 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
986 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
987 wxT("wxComboCtrl with custom button action:")), 1,
988 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
991 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
993 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
994 wxFileSelectorCombo
* fsc
;
996 fsc
= new wxFileSelectorCombo(panel
,wxID_ANY
,wxEmptyString
,
997 wxDefaultPosition
, wxDefaultSize
,
1001 rowSizer
->Add( fsc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
1002 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
1005 // Make sure GetFeatures is implemented
1006 wxComboCtrl::GetFeatures();
1009 topRowSizer
->Add( colSizer
, 1, wxALL
, 2 );
1011 colSizer
= new wxBoxSizer( wxVERTICAL
);
1013 wxStaticBoxSizer
* sbSizer
= new wxStaticBoxSizer( new wxStaticBox(panel
,
1018 m_cbUseAnim
= new wxCheckBox(panel
, wxID_ANY
, wxT("Custom popup animation for ListView wxComboCtrl"));
1019 m_cbUseAnim
->SetValue(true);
1020 sbSizer
->Add( m_cbUseAnim
, 0, wxALL
, 3 );
1022 colSizer
->Add( sbSizer
, 0, wxEXPAND
|wxALL
, 3 );
1023 colSizer
->AddSpacer(8);
1024 colSizer
->Add( new wxStaticText(panel
, wxID_ANY
, wxT("Log Messages:")), 0, wxTOP
|wxLEFT
, 3 );
1025 colSizer
->Add( m_logWin
, 1, wxEXPAND
|wxALL
, 3 );
1027 topRowSizer
->Add( colSizer
, 1, wxEXPAND
|wxALL
, 2 );
1028 topSizer
->Add( topRowSizer
, 1, wxEXPAND
);
1030 panel
->SetSizer( topSizer
);
1031 topSizer
->SetSizeHints( panel
);
1039 void MyFrame::OnComboBoxUpdate( wxCommandEvent
& event
)
1041 // Don't show messages for the log output window (it'll crash)
1042 if ( !event
.GetEventObject()->IsKindOf(CLASSINFO(wxComboCtrl
)) )
1045 if ( event
.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED
)
1047 wxLogDebug(wxT("EVT_COMBOBOX(id=%i,selection=%i)"),event
.GetId(),event
.GetSelection());
1049 else if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1051 wxLogDebug(wxT("EVT_TEXT(id=%i,string=\"%s\")"),event
.GetId(),event
.GetString().c_str());
1055 void MyFrame::OnShowComparison( wxCommandEvent
& WXUNUSED(event
) )
1058 // Show some wxOwnerDrawComboBoxes for comparison
1061 wxBoxSizer
* colSizer
;
1062 wxBoxSizer
* rowSizer
;
1063 wxStaticBoxSizer
* groupSizer
;
1066 wxOwnerDrawnComboBox
* odc
;
1068 const int border
= 4;
1070 wxDialog
* dlg
= new wxDialog(this,wxID_ANY
,
1071 wxT("Compare against wxComboBox"),
1072 wxDefaultPosition
,wxDefaultSize
,
1073 wxDEFAULT_DIALOG_STYLE
|wxRESIZE_BORDER
);
1075 colSizer
= new wxBoxSizer( wxVERTICAL
);
1077 rowSizer
= new wxBoxSizer(wxHORIZONTAL
);
1079 groupSizer
= new wxStaticBoxSizer(new wxStaticBox(dlg
,wxID_ANY
,wxT(" wxOwnerDrawnComboBox ")),
1082 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Writable, sorted:")), 0,
1083 wxALIGN_CENTER_VERTICAL
|wxRIGHT
|wxEXPAND
, border
);
1085 odc
= new wxOwnerDrawnComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1086 wxDefaultPosition
, wxDefaultSize
,
1088 wxCB_SORT
// wxNO_BORDER|wxCB_READONLY
1091 odc
->Append(wxT("H - Appended Item")); // test sorting in append
1093 odc
->SetValue(wxT("Dot Dash"));
1095 groupSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1098 // Readonly ODComboBox
1099 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Read-only:")), 0,
1100 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1102 odc
= new wxOwnerDrawnComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1103 wxDefaultPosition
, wxDefaultSize
,
1105 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1108 odc
->SetValue(wxT("Dot Dash"));
1109 odc
->SetText(wxT("Dot Dash (Testing SetText)"));
1111 groupSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1114 // Disabled ODComboBox
1115 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Disabled:")), 0,
1116 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1118 odc
= new wxOwnerDrawnComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1119 wxDefaultPosition
, wxDefaultSize
,
1121 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1124 odc
->SetValue(wxT("Dot Dash"));
1127 groupSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1129 rowSizer
->Add( groupSizer
, 1, wxEXPAND
|wxALL
, border
);
1132 groupSizer
= new wxStaticBoxSizer(new wxStaticBox(dlg
,wxID_ANY
,wxT(" wxComboBox ")),
1138 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Writable, sorted:")), 0,
1139 wxALIGN_CENTER_VERTICAL
|wxRIGHT
|wxEXPAND
, border
);
1141 cb
= new wxComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1142 wxDefaultPosition
, wxDefaultSize
,
1144 wxCB_SORT
// wxNO_BORDER|wxCB_READONLY
1147 cb
->Append(wxT("H - Appended Item")); // test sorting in append
1149 cb
->SetValue(wxT("Dot Dash"));
1151 groupSizer
->Add( cb
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1154 // Readonly wxComboBox
1155 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Read-only:")), 0,
1156 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1158 cb
= new wxComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1159 wxDefaultPosition
, wxDefaultSize
,
1161 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1164 cb
->SetValue(wxT("Dot Dash"));
1166 groupSizer
->Add( cb
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1169 // Disabled wxComboBox
1170 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Disabled:")), 0,
1171 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1173 cb
= new wxComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1174 wxDefaultPosition
, wxDefaultSize
,
1176 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1179 cb
->SetValue(wxT("Dot Dash"));
1182 groupSizer
->Add( cb
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1184 rowSizer
->Add( groupSizer
, 1, wxEXPAND
|wxALL
, border
);
1186 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, border
);
1188 dlg
->SetSizer( colSizer
);
1189 colSizer
->SetSizeHints( dlg
);
1191 dlg
->SetSize(60,240);
1198 delete wxLog::SetActiveTarget(m_logOld
);
1201 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
1203 // true is to force the frame to close
1207 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
1209 wxMessageBox(wxString::Format(
1210 wxT("Welcome to %s!\n")
1212 wxT("This is the wxWidgets wxComboCtrl and wxOwnerDrawnComboBox sample\n")
1213 wxT("running under %s."),
1215 wxGetOsDescription().c_str()
1217 wxT("About wxComboCtrl sample"),
1218 wxOK
| wxICON_INFORMATION
,
1222 void MyFrame::OnIdle(wxIdleEvent
& event
)
1224 // This code is useful for debugging focus problems
1225 // (which are plentiful when dealing with popup windows).
1227 static wxWindow
* lastFocus
= (wxWindow
*) NULL
;
1229 wxWindow
* curFocus
= ::wxWindow::FindFocus();
1231 if ( curFocus
!= lastFocus
)
1233 const wxChar
* className
= wxT("<none>");
1235 className
= curFocus
->GetClassInfo()->GetClassName();
1236 lastFocus
= curFocus
;
1237 wxLogDebug( wxT("FOCUSED: %s %X"),
1239 (unsigned int)curFocus
);