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 // create the main application window
158 MyFrame
*frame
= new MyFrame(_T("wxComboCtrl and wxOwnerDrawnComboBox Sample"));
160 // and show it (the frames, unlike simple controls, are not shown when
161 // created initially)
164 // success: wxApp::OnRun() will be called which will enter the main message
165 // loop and the application will run. If we returned false here, the
166 // application would exit immediately.
171 // ----------------------------------------------------------------------------
172 // wxOwnerDrawnComboBox with custom paint list items
173 // ----------------------------------------------------------------------------
175 class wxPenStyleComboBox
: public wxOwnerDrawnComboBox
178 virtual void OnDrawItem( wxDC
& dc
,
183 if ( item
== wxNOT_FOUND
)
190 int penStyle
= wxSOLID
;
192 penStyle
= wxTRANSPARENT
;
193 else if ( item
== 2 )
195 else if ( item
== 3 )
196 penStyle
= wxLONG_DASH
;
197 else if ( item
== 4 )
198 penStyle
= wxSHORT_DASH
;
199 else if ( item
== 5 )
200 penStyle
= wxDOT_DASH
;
201 else if ( item
== 6 )
202 penStyle
= wxBDIAGONAL_HATCH
;
203 else if ( item
== 7 )
204 penStyle
= wxCROSSDIAG_HATCH
;
205 else if ( item
== 8 )
206 penStyle
= wxFDIAGONAL_HATCH
;
207 else if ( item
== 9 )
208 penStyle
= wxCROSS_HATCH
;
209 else if ( item
== 10 )
210 penStyle
= wxHORIZONTAL_HATCH
;
211 else if ( item
== 11 )
212 penStyle
= wxVERTICAL_HATCH
;
214 wxPen
pen( dc
.GetTextForeground(), 3, penStyle
);
216 // Get text colour as pen colour
219 if ( !(flags
& wxODCB_PAINTING_CONTROL
) )
221 dc
.DrawText(GetString( item
),
223 (r
.y
+ 0) + ( (r
.height
/2) - dc
.GetCharHeight() )/2
226 dc
.DrawLine( r
.x
+5, r
.y
+((r
.height
/4)*3), r
.x
+r
.width
- 5, r
.y
+((r
.height
/4)*3) );
230 dc
.DrawLine( r
.x
+5, r
.y
+r
.height
/2, r
.x
+r
.width
- 5, r
.y
+r
.height
/2 );
234 virtual void OnDrawBackground( wxDC
& dc
, const wxRect
& rect
,
235 int item
, int flags
) const
238 // If item is selected or even, or we are painting the
239 // combo control itself, use the default rendering.
240 if ( (flags
& (wxODCB_PAINTING_CONTROL
|wxODCB_PAINTING_SELECTED
)) ||
243 wxOwnerDrawnComboBox::OnDrawBackground(dc
,rect
,item
,flags
);
247 // Otherwise, draw every other background with different colour.
248 wxColour
bgCol(240,240,250);
249 dc
.SetBrush(wxBrush(bgCol
));
250 dc
.SetPen(wxPen(bgCol
));
251 dc
.DrawRectangle(rect
);
254 virtual wxCoord
OnMeasureItem( size_t item
) const
256 // Simply demonstrate the ability to have variable-height items
263 virtual wxCoord
OnMeasureItemWidth( size_t WXUNUSED(item
) ) const
265 return -1; // default - will be measured from text width
271 // ----------------------------------------------------------------------------
272 // wxListView Custom popup interface
273 // ----------------------------------------------------------------------------
275 #include <wx/listctrl.h>
277 class ListViewComboPopup
: public wxListView
, public wxComboPopup
284 m_itemHere
= -1; // hot item in list
287 virtual bool Create( wxWindow
* parent
)
289 return wxListView::Create(parent
,1,
290 wxPoint(0,0),wxDefaultSize
,
291 wxLC_LIST
|wxLC_SINGLE_SEL
|
292 wxLC_SORT_ASCENDING
|wxSIMPLE_BORDER
);
295 virtual wxWindow
*GetControl() { return this; }
297 virtual void SetStringValue( const wxString
& s
)
299 int n
= wxListView::FindItem(-1,s
);
300 if ( n
>= 0 && n
< GetItemCount() )
301 wxListView::Select(n
);
304 virtual wxString
GetStringValue() const
307 return wxListView::GetItemText(m_value
);
308 return wxEmptyString
;
312 // Popup event handlers
315 // Mouse hot-tracking
316 void OnMouseMove(wxMouseEvent
& event
)
318 // Move selection to cursor if it is inside the popup
321 int itemHere
= HitTest(event
.GetPosition(),resFlags
);
324 wxListView::Select(itemHere
,true);
325 m_itemHere
= itemHere
;
330 // On mouse left, set the value and close the popup
331 void OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
333 m_value
= m_itemHere
;
339 // Utilies for item manipulation
342 void AddSelection( const wxString
& selstr
)
344 wxListView::InsertItem(GetItemCount(),selstr
);
349 int m_value
; // current item index
350 int m_itemHere
; // hot item in popup
353 DECLARE_EVENT_TABLE()
356 BEGIN_EVENT_TABLE(ListViewComboPopup
, wxListView
)
357 EVT_MOTION(ListViewComboPopup::OnMouseMove
)
358 // NOTE: Left down event is used instead of left up right now
359 // since MSW wxListCtrl doesn't seem to emit left ups
361 EVT_LEFT_DOWN(ListViewComboPopup::OnMouseClick
)
365 // ----------------------------------------------------------------------------
366 // wxTreeCtrl Custom popup interface
367 // ----------------------------------------------------------------------------
369 #include <wx/treectrl.h>
371 class TreeCtrlComboPopup
: public wxTreeCtrl
, public wxComboPopup
379 virtual bool Create( wxWindow
* parent
)
381 return wxTreeCtrl::Create(parent
,1,
382 wxPoint(0,0),wxDefaultSize
,
383 wxTR_HIDE_ROOT
|wxTR_HAS_BUTTONS
|
384 wxTR_SINGLE
|wxTR_LINES_AT_ROOT
|
388 virtual void OnShow()
390 // make sure selected item is visible
391 if ( m_value
.IsOk() )
392 EnsureVisible(m_value
);
395 virtual wxSize
GetAdjustedSize( int minWidth
,
396 int WXUNUSED(prefHeight
),
399 return wxSize(wxMax(300,minWidth
),wxMin(250,maxHeight
));
402 virtual wxWindow
*GetControl() { return this; }
404 // Needed by SetStringValue
405 wxTreeItemId
FindItemByText( wxTreeItemId parent
, const wxString
& text
)
407 wxTreeItemIdValue cookie
;
408 wxTreeItemId child
= GetFirstChild(parent
,cookie
);
409 while ( child
.IsOk() )
411 if ( GetItemText(child
) == text
)
415 if ( ItemHasChildren(child
) )
417 wxTreeItemId found
= FindItemByText(child
,text
);
421 child
= GetNextChild(parent
,cookie
);
423 return wxTreeItemId();
426 virtual void SetStringValue( const wxString
& s
)
428 wxTreeItemId root
= GetRootItem();
432 wxTreeItemId found
= FindItemByText(root
,s
);
435 m_value
= m_itemHere
= found
;
436 wxTreeCtrl::SelectItem(found
);
440 virtual wxString
GetStringValue() const
442 if ( m_value
.IsOk() )
443 return wxTreeCtrl::GetItemText(m_value
);
444 return wxEmptyString
;
448 // Popup event handlers
451 // Mouse hot-tracking
452 void OnMouseMove(wxMouseEvent
& event
)
455 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
456 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
458 wxTreeCtrl::SelectItem(itemHere
,true);
459 m_itemHere
= itemHere
;
464 // On mouse left, set the value and close the popup
465 void OnMouseClick(wxMouseEvent
& event
)
468 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
469 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
471 m_itemHere
= itemHere
;
481 wxTreeItemId m_value
; // current item index
482 wxTreeItemId m_itemHere
; // hot item in popup
485 DECLARE_EVENT_TABLE()
488 BEGIN_EVENT_TABLE(TreeCtrlComboPopup
, wxTreeCtrl
)
489 EVT_MOTION(TreeCtrlComboPopup::OnMouseMove
)
490 // NOTE: Left down event is used instead of left up right now
491 // since MSW wxTreeCtrl doesn't seem to emit left ups
493 EVT_LEFT_DOWN(TreeCtrlComboPopup::OnMouseClick
)
496 // ----------------------------------------------------------------------------
497 // wxComboCtrl with custom popup animation. We use EVT_TIMER, which is quite
498 // safe, but requires much more can than doing it in a single function (ie.
499 // AnimateShow) and using combination of wxSleep and wxSafeYield.
500 // ----------------------------------------------------------------------------
504 #define CUSTOM_COMBOBOX_ANIMATION_DURATION 200 // In milliseconds
506 #include "wx/timer.h"
508 class wxComboCtrlWithCustomPopupAnim
: public wxComboCtrl
512 virtual bool AnimateShow( const wxRect
& rect
, int flags
)
514 MyFrame
* myFrame
= (MyFrame
*) ::wxGetTopLevelParent(this);
516 if ( !myFrame
->m_cbUseAnim
->GetValue() )
519 m_animStart
= ::wxGetLocalTimeMillis();
525 wxBitmap
bitmap( rect
.width
, rect
.height
, -1 );
526 wxMemoryDC
memdc( bitmap
);
527 memdc
.Blit( 0, 0, rect
.width
, rect
.height
, &dc
, rect
.x
, rect
.y
);
528 memdc
.SelectObject(wxNullBitmap
);
529 m_animBackBitmap
= bitmap
;
531 m_animTimer
.SetOwner( this, wxID_ANY
);
532 m_animTimer
.Start( 10, wxTIMER_CONTINUOUS
);
534 OnTimerEvent(*((wxTimerEvent
*)NULL
)); // Event is never used, so we can give NULL
538 void OnTimerEvent( wxTimerEvent
& WXUNUSED(event
) )
540 bool stopTimer
= false;
542 wxWindow
* popup
= GetPopupControl()->GetControl();
544 const wxRect
& rect
= m_animRect
;
546 // Popup was hidden before it was fully shown?
547 if ( IsPopupWindowState(Hidden
) )
553 wxLongLong t
= ::wxGetLocalTimeMillis();
555 int pos
= (int) (t
-m_animStart
).GetLo();
556 if ( pos
< CUSTOM_COMBOBOX_ANIMATION_DURATION
)
559 // Actual animation happens here
561 int width
= rect
.width
;
562 int height
= rect
.height
;
564 int center_x
= rect
.x
+ (width
/2);
565 int center_y
= rect
.y
+ (height
/2);
567 double d_height
= (double) height
;
569 dc
.SetPen( *wxBLACK_PEN
);
570 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
572 int w
= (((pos
*256)/CUSTOM_COMBOBOX_ANIMATION_DURATION
)*width
)/256;
574 double ratio
= ((double)w
/ (double)width
);
575 int h
= (int)(d_height
* ratio
);
576 dc
.DrawBitmap( m_animBackBitmap
, rect
.x
, rect
.y
);
577 dc
.DrawRectangle( center_x
- w
/2, center_y
- h
/2, w
, h
);
587 dc
.DrawBitmap( m_animBackBitmap
, rect
.x
, rect
.y
);
590 DoShowPopup( m_animRect
, m_animFlags
);
596 // Popup animation related
597 wxLongLong m_animStart
;
600 wxBitmap m_animBackBitmap
;
604 DECLARE_EVENT_TABLE()
607 BEGIN_EVENT_TABLE(wxComboCtrlWithCustomPopupAnim
, wxComboCtrl
)
608 EVT_TIMER(wxID_ANY
, wxComboCtrlWithCustomPopupAnim::OnTimerEvent
)
613 #define wxComboCtrlWithCustomPopupAnim wxComboCtrl
617 // ----------------------------------------------------------------------------
618 // wxComboCtrl with entirely custom button action (opens file dialog)
619 // ----------------------------------------------------------------------------
621 class wxFileSelectorCombo
: public wxComboCtrl
624 wxFileSelectorCombo() : wxComboCtrl() { Init(); }
626 wxFileSelectorCombo(wxWindow
*parent
,
627 wxWindowID id
= wxID_ANY
,
628 const wxString
& value
= wxEmptyString
,
629 const wxPoint
& pos
= wxDefaultPosition
,
630 const wxSize
& size
= wxDefaultSize
,
632 const wxValidator
& validator
= wxDefaultValidator
,
633 const wxString
& name
= wxComboBoxNameStr
)
637 Create(parent
,id
,value
,
639 // Style flag wxCC_STD_BUTTON makes the button
640 // behave more like a standard push button.
641 style
| wxCC_STD_BUTTON
,
645 // Prepare custom button bitmap (just '...' text)
648 dc
.SelectObject(bmp
);
650 // Draw transparent background
651 wxColour
magic(255,0,255);
652 wxBrush
magicBrush(magic
);
653 dc
.SetBrush( magicBrush
);
654 dc
.SetPen( *wxTRANSPARENT_PEN
);
655 dc
.DrawRectangle(0,0,bmp
.GetWidth(),bmp
.GetHeight());
658 wxString str
= wxT("...");
660 dc
.GetTextExtent(str
, &w
, &h
, 0, 0);
661 dc
.DrawText(str
, (bmp
.GetWidth()-w
)/2, (bmp
.GetHeight()-h
)/2);
663 dc
.SelectObject( wxNullBitmap
);
665 // Finalize transparency with a mask
666 wxMask
*mask
= new wxMask( bmp
, magic
);
669 SetButtonBitmaps(bmp
,true);
672 virtual void OnButtonClick()
674 // Show standard wxFileDialog on button click
676 wxFileDialog
dlg(this,
680 wxT("All files (*.*)|*.*"),
683 if ( dlg
.ShowModal() == wxID_OK
)
685 SetValue(dlg
.GetPath());
689 // Implement empty DoSetPopupControl to prevent assertion failure.
690 virtual void DoSetPopupControl(wxComboPopup
* WXUNUSED(popup
))
697 // Initialize member variables here
701 // ----------------------------------------------------------------------------
703 // ----------------------------------------------------------------------------
706 MyFrame::MyFrame(const wxString
& title
)
707 : wxFrame(NULL
, wxID_ANY
, title
)
709 wxBoxSizer
* topSizer
;
710 wxBoxSizer
* topRowSizer
;
711 wxBoxSizer
* colSizer
;
712 wxBoxSizer
* rowSizer
;
714 // set the frame icon
715 SetIcon(wxICON(sample
));
719 wxMenu
*fileMenu
= new wxMenu
;
721 // the "About" item should be in the help menu
722 wxMenu
*helpMenu
= new wxMenu
;
723 helpMenu
->Append(ComboControl_About
, _T("&About...\tF1"), _T("Show about dialog"));
725 fileMenu
->Append(ComboControl_Compare
, _T("&Compare against wxComboBox..."),
726 _T("Show some wxOwnerDrawnComboBoxes side-by-side with native wxComboBoxes."));
727 fileMenu
->AppendSeparator();
728 fileMenu
->Append(ComboControl_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
730 // now append the freshly created menu to the menu bar...
731 wxMenuBar
*menuBar
= new wxMenuBar();
732 menuBar
->Append(fileMenu
, _T("&File"));
733 menuBar
->Append(helpMenu
, _T("&Help"));
735 // ... and attach this menu bar to the frame
737 #endif // wxUSE_MENUS
739 wxPanel
* panel
= new wxPanel(this);
741 // Prepare log window right away since it shows EVT_TEXTs
742 m_logWin
= new wxTextCtrl( panel
, 105, wxEmptyString
, wxDefaultPosition
,
743 wxSize(-1,125), wxTE_MULTILINE
|wxFULL_REPAINT_ON_RESIZE
);
744 m_logWin
->SetEditable(false);
745 wxLogTextCtrl
* logger
= new wxLogTextCtrl( m_logWin
);
746 m_logOld
= logger
->SetActiveTarget( logger
);
747 logger
->SetTimestamp( NULL
);
750 topSizer
= new wxBoxSizer( wxVERTICAL
);
752 topRowSizer
= new wxBoxSizer( wxHORIZONTAL
);
754 colSizer
= new wxBoxSizer( wxVERTICAL
);
758 wxGenericComboCtrl
* gcc
;
759 wxOwnerDrawnComboBox
* odc
;
761 // Create common strings array
762 m_arrItems
.Add( wxT("Solid") );
763 m_arrItems
.Add( wxT("Transparent") );
764 m_arrItems
.Add( wxT("Dot") );
765 m_arrItems
.Add( wxT("Long Dash") );
766 m_arrItems
.Add( wxT("Short Dash") );
767 m_arrItems
.Add( wxT("Dot Dash") );
768 m_arrItems
.Add( wxT("Backward Diagonal Hatch") );
769 m_arrItems
.Add( wxT("Cross-diagonal Hatch") );
770 m_arrItems
.Add( wxT("Forward Diagonal Hatch") );
771 m_arrItems
.Add( wxT("Cross Hatch") );
772 m_arrItems
.Add( wxT("Horizontal Hatch") );
773 m_arrItems
.Add( wxT("Vertical Hatch") );
777 // Create pen selector ODComboBox with owner-drawn items
779 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
780 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
781 wxT("OwnerDrawnComboBox with owner-drawn items:")), 1,
782 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
783 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
785 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
788 // When defining derivative class for callbacks, we need
789 // to use two-stage creation (or redefine the common wx
791 odc
= new wxPenStyleComboBox();
792 odc
->Create(panel
,wxID_ANY
,wxEmptyString
,
793 wxDefaultPosition
, wxDefaultSize
,
795 wxCB_READONLY
//wxNO_BORDER | wxCB_READONLY
799 odc
->SetSelection(0);
801 rowSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
802 rowSizer
->AddStretchSpacer(1);
803 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
808 // Same but with changed button position
810 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
811 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
812 wxT("OwnerDrawnComboBox with owner-drawn items and button on the left:")), 1,
813 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
814 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
816 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
819 // When defining derivative class for callbacks, we need
820 // to use two-stage creation (or redefine the common wx
822 odc
= new wxPenStyleComboBox();
823 odc
->Create(panel
,wxID_ANY
,wxEmptyString
,
824 wxDefaultPosition
, wxDefaultSize
,
826 wxCB_READONLY
//wxNO_BORDER | wxCB_READONLY
830 odc
->SetSelection(0);
832 // Use button size that is slightly smaller than the default.
833 wxSize butSize
= odc
->GetButtonSize();
834 odc
->SetButtonPosition(butSize
.x
- 2, // button width
835 butSize
.y
- 6, // button height
837 2 // horizontal spacing
840 rowSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
841 rowSizer
->AddStretchSpacer(1);
842 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
846 // List View wxComboCtrl
849 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
850 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,wxT("List View wxComboCtrl:")), 1,
851 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
852 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,wxT("Tree Ctrl wxComboControl:")), 1,
853 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
854 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
856 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
857 cc
= new wxComboCtrlWithCustomPopupAnim();
858 cc
->Create(panel
, wxID_ANY
, wxEmptyString
);
860 // Make sure we use popup that allows focusing the listview.
861 cc
->UseAltPopupWindow();
863 cc
->SetPopupMinWidth(300);
865 ListViewComboPopup
* iface
= new ListViewComboPopup();
866 cc
->SetPopupControl(iface
);
869 for ( i
=0; i
<100; i
++ )
870 iface
->AddSelection( wxString::Format(wxT("Item %02i"),i
));
872 rowSizer
->Add( cc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
876 // Tree Ctrl wxComboCtrl
879 // Note that we test that wxGenericComboCtrl works
880 gcc
= new wxGenericComboCtrl(panel
,wxID_ANY
,wxEmptyString
,
881 wxDefaultPosition
, wxDefaultSize
);
883 // Make sure we use popup that allows focusing the treectrl.
884 gcc
->UseAltPopupWindow();
886 // Set popup interface right away, otherwise some of the calls
888 TreeCtrlComboPopup
* tcPopup
= new TreeCtrlComboPopup();
889 gcc
->SetPopupControl(tcPopup
);
891 // Add items using wxTreeCtrl methods directly
892 wxTreeItemId rootId
= tcPopup
->AddRoot(wxT("<hidden_root>"));
894 wxTreeItemId groupId
;
896 for ( i
=0; i
<4; i
++ )
898 groupId
= tcPopup
->AppendItem(rootId
,
899 wxString::Format(wxT("Branch %02i"),i
));
902 for ( n
=0; n
<25; n
++ )
903 tcPopup
->AppendItem(groupId
,
904 wxString::Format(wxT("Subitem %02i"),(i
*25)+n
));
907 gcc
->SetValue(wxT("Subitem 05"));
909 // Move button to left - it makes more sense for a tree ctrl
910 gcc
->SetButtonPosition(-1, // button width
913 0 // horizontal spacing
916 rowSizer
->Add( gcc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
918 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
921 wxInitAllImageHandlers();
924 // Custom Dropbutton Bitmaps
925 // (second one uses blank button background)
927 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
928 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
929 wxT("OwnerDrawnComboBox with simple dropbutton graphics:")), 1,
930 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
932 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
934 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
936 odc
= new wxOwnerDrawnComboBox(panel
,wxID_ANY
,wxEmptyString
,
937 wxDefaultPosition
, wxDefaultSize
,
939 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
942 wxOwnerDrawnComboBox
* odc2
;
943 odc2
= new wxOwnerDrawnComboBox(panel
,wxID_ANY
,wxEmptyString
,
944 wxDefaultPosition
, wxDefaultSize
,
946 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
949 // Load images from disk
950 wxImage
imgNormal(wxT("dropbutn.png"));
951 wxImage
imgPressed(wxT("dropbutp.png"));
952 wxImage
imgHover(wxT("dropbuth.png"));
954 if ( imgNormal
.IsOk() && imgPressed
.IsOk() && imgHover
.IsOk() )
956 wxBitmap
bmpNormal(imgNormal
);
957 wxBitmap
bmpPressed(imgPressed
);
958 wxBitmap
bmpHover(imgHover
);
959 odc
->SetButtonBitmaps(bmpNormal
,false,bmpPressed
,bmpHover
);
960 odc2
->SetButtonBitmaps(bmpNormal
,true,bmpPressed
,bmpHover
);
963 wxLogError(wxT("Dropbutton images not found"));
965 //odc2->SetButtonPosition(0, // width adjustment
966 // 0, // height adjustment
968 // 0 // horizontal spacing
971 rowSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
972 rowSizer
->Add( odc2
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
973 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
978 // wxComboCtrl with totally custom button action (open file dialog)
980 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
981 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
982 wxT("wxComboCtrl with custom button action:")), 1,
983 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
986 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
988 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
989 wxFileSelectorCombo
* fsc
;
991 fsc
= new wxFileSelectorCombo(panel
,wxID_ANY
,wxEmptyString
,
992 wxDefaultPosition
, wxDefaultSize
,
996 rowSizer
->Add( fsc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
997 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
1000 // Make sure GetFeatures is implemented
1001 wxComboCtrl::GetFeatures();
1004 topRowSizer
->Add( colSizer
, 1, wxALL
, 2 );
1006 colSizer
= new wxBoxSizer( wxVERTICAL
);
1008 wxStaticBoxSizer
* sbSizer
= new wxStaticBoxSizer( new wxStaticBox(panel
,
1013 m_cbUseAnim
= new wxCheckBox(panel
, wxID_ANY
, wxT("Custom popup animation for ListView wxComboCtrl"));
1014 m_cbUseAnim
->SetValue(true);
1015 sbSizer
->Add( m_cbUseAnim
, 0, wxALL
, 3 );
1017 colSizer
->Add( sbSizer
, 0, wxEXPAND
|wxALL
, 3 );
1018 colSizer
->AddSpacer(8);
1019 colSizer
->Add( new wxStaticText(panel
, wxID_ANY
, wxT("Log Messages:")), 0, wxTOP
|wxLEFT
, 3 );
1020 colSizer
->Add( m_logWin
, 1, wxEXPAND
|wxALL
, 3 );
1022 topRowSizer
->Add( colSizer
, 1, wxEXPAND
|wxALL
, 2 );
1023 topSizer
->Add( topRowSizer
, 1, wxEXPAND
);
1025 panel
->SetSizer( topSizer
);
1026 topSizer
->SetSizeHints( panel
);
1034 void MyFrame::OnComboBoxUpdate( wxCommandEvent
& event
)
1036 // Don't show messages for the log output window (it'll crash)
1037 if ( !event
.GetEventObject()->IsKindOf(CLASSINFO(wxComboCtrl
)) )
1040 if ( event
.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED
)
1041 wxLogDebug(wxT("EVT_COMBOBOX(id=%i,selection=%i)"),event
.GetId(),event
.GetSelection());
1042 else if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
1043 wxLogDebug(wxT("EVT_TEXT(id=%i,string=\"%s\")"),event
.GetId(),event
.GetString().c_str());
1046 void MyFrame::OnShowComparison( wxCommandEvent
& WXUNUSED(event
) )
1049 // Show some wxOwnerDrawComboBoxes for comparison
1052 wxBoxSizer
* colSizer
;
1053 wxBoxSizer
* rowSizer
;
1054 wxStaticBoxSizer
* groupSizer
;
1057 wxOwnerDrawnComboBox
* odc
;
1059 const int border
= 4;
1061 wxDialog
* dlg
= new wxDialog(this,wxID_ANY
,
1062 wxT("Compare against wxComboBox"),
1063 wxDefaultPosition
,wxDefaultSize
,
1064 wxDEFAULT_DIALOG_STYLE
|wxRESIZE_BORDER
);
1066 colSizer
= new wxBoxSizer( wxVERTICAL
);
1068 rowSizer
= new wxBoxSizer(wxHORIZONTAL
);
1070 groupSizer
= new wxStaticBoxSizer(new wxStaticBox(dlg
,wxID_ANY
,wxT(" wxOwnerDrawnComboBox ")),
1073 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Writable, sorted:")), 0,
1074 wxALIGN_CENTER_VERTICAL
|wxRIGHT
|wxEXPAND
, border
);
1076 odc
= new wxOwnerDrawnComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1077 wxDefaultPosition
, wxDefaultSize
,
1079 wxCB_SORT
// wxNO_BORDER|wxCB_READONLY
1082 odc
->Append(wxT("H - Appended Item")); // test sorting in append
1084 odc
->SetValue(wxT("Dot Dash"));
1086 groupSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1089 // Readonly ODComboBox
1090 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Read-only:")), 0,
1091 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1093 odc
= new wxOwnerDrawnComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1094 wxDefaultPosition
, wxDefaultSize
,
1096 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1099 odc
->SetValue(wxT("Dot Dash"));
1100 odc
->SetText(wxT("Dot Dash (Testing SetText)"));
1102 groupSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1105 // Disabled ODComboBox
1106 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Disabled:")), 0,
1107 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1109 odc
= new wxOwnerDrawnComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1110 wxDefaultPosition
, wxDefaultSize
,
1112 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1115 odc
->SetValue(wxT("Dot Dash"));
1118 groupSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1120 rowSizer
->Add( groupSizer
, 1, wxEXPAND
|wxALL
, border
);
1123 groupSizer
= new wxStaticBoxSizer(new wxStaticBox(dlg
,wxID_ANY
,wxT(" wxComboBox ")),
1129 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Writable, sorted:")), 0,
1130 wxALIGN_CENTER_VERTICAL
|wxRIGHT
|wxEXPAND
, border
);
1132 cb
= new wxComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1133 wxDefaultPosition
, wxDefaultSize
,
1135 wxCB_SORT
// wxNO_BORDER|wxCB_READONLY
1138 cb
->Append(wxT("H - Appended Item")); // test sorting in append
1140 cb
->SetValue(wxT("Dot Dash"));
1142 groupSizer
->Add( cb
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1145 // Readonly wxComboBox
1146 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Read-only:")), 0,
1147 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1149 cb
= new wxComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1150 wxDefaultPosition
, wxDefaultSize
,
1152 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1155 cb
->SetValue(wxT("Dot Dash"));
1157 groupSizer
->Add( cb
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1160 // Disabled wxComboBox
1161 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Disabled:")), 0,
1162 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1164 cb
= new wxComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1165 wxDefaultPosition
, wxDefaultSize
,
1167 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1170 cb
->SetValue(wxT("Dot Dash"));
1173 groupSizer
->Add( cb
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1175 rowSizer
->Add( groupSizer
, 1, wxEXPAND
|wxALL
, border
);
1177 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, border
);
1179 dlg
->SetSizer( colSizer
);
1180 colSizer
->SetSizeHints( dlg
);
1182 dlg
->SetSize(60,240);
1189 delete wxLog::SetActiveTarget(m_logOld
);
1192 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
1194 // true is to force the frame to close
1198 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
1200 wxMessageBox(wxString::Format(
1201 _T("Welcome to %s!\n")
1203 _T("This is the wxWidgets wxComboCtrl and wxOwnerDrawnComboBox sample\n")
1204 _T("running under %s."),
1206 wxGetOsDescription().c_str()
1208 _T("About wxComboCtrl sample"),
1209 wxOK
| wxICON_INFORMATION
,
1213 void MyFrame::OnIdle(wxIdleEvent
& event
)
1215 // This code is useful for debugging focus problems
1216 // (which are plentiful when dealing with popup windows).
1218 static wxWindow
* lastFocus
= (wxWindow
*) NULL
;
1220 wxWindow
* curFocus
= ::wxWindow::FindFocus();
1222 if ( curFocus
!= lastFocus
)
1224 const wxChar
* className
= wxT("<none>");
1226 className
= curFocus
->GetClassInfo()->GetClassName();
1227 lastFocus
= curFocus
;
1228 wxLogDebug( wxT("FOCUSED: %s %X"),
1230 (unsigned int)curFocus
);