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
);
92 // Common list of items for all dialogs.
93 wxArrayString m_arrItems
;
96 // any class wishing to process wxWidgets events must use this macro
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 // IDs for the controls and the menu commands
107 ComboControl_Compare
= wxID_HIGHEST
,
110 ComboControl_Quit
= wxID_EXIT
,
112 // it is important for the id corresponding to the "About" command to have
113 // this standard value as otherwise it won't be handled properly under Mac
114 // (where it is special and put into the "Apple" menu)
115 ComboControl_About
= wxID_ABOUT
118 // ----------------------------------------------------------------------------
119 // event tables and other macros for wxWidgets
120 // ----------------------------------------------------------------------------
122 // the event tables connect the wxWidgets events with the functions (event
123 // handlers) which process them. It can be also done at run-time, but for the
124 // simple menu events like this the static method is much simpler.
125 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
126 EVT_TEXT(wxID_ANY
,MyFrame::OnComboBoxUpdate
)
127 EVT_COMBOBOX(wxID_ANY
,MyFrame::OnComboBoxUpdate
)
129 EVT_MENU(ComboControl_Compare
, MyFrame::OnShowComparison
)
130 EVT_MENU(ComboControl_Quit
, MyFrame::OnQuit
)
131 EVT_MENU(ComboControl_About
, MyFrame::OnAbout
)
133 EVT_IDLE(MyFrame::OnIdle
)
136 // Create a new application object: this macro will allow wxWidgets to create
137 // the application object during program execution (it's better than using a
138 // static object for many reasons) and also implements the accessor function
139 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
143 // ============================================================================
145 // ============================================================================
147 // ----------------------------------------------------------------------------
148 // the application class
149 // ----------------------------------------------------------------------------
151 // 'Main program' equivalent: the program execution "starts" here
154 // create the main application window
155 MyFrame
*frame
= new MyFrame(_T("wxComboCtrl and wxOwnerDrawnComboBox Sample"));
157 // and show it (the frames, unlike simple controls, are not shown when
158 // created initially)
161 // success: wxApp::OnRun() will be called which will enter the main message
162 // loop and the application will run. If we returned false here, the
163 // application would exit immediately.
168 // ----------------------------------------------------------------------------
169 // wxOwnerDrawnComboBox with custom paint list items
170 // ----------------------------------------------------------------------------
172 class wxPenStyleComboBox
: public wxOwnerDrawnComboBox
175 virtual void OnDrawItem( wxDC
& dc
,
180 if ( item
== wxNOT_FOUND
)
187 int penStyle
= wxSOLID
;
189 penStyle
= wxTRANSPARENT
;
190 else if ( item
== 2 )
192 else if ( item
== 3 )
193 penStyle
= wxLONG_DASH
;
194 else if ( item
== 4 )
195 penStyle
= wxSHORT_DASH
;
196 else if ( item
== 5 )
197 penStyle
= wxDOT_DASH
;
198 else if ( item
== 6 )
199 penStyle
= wxBDIAGONAL_HATCH
;
200 else if ( item
== 7 )
201 penStyle
= wxCROSSDIAG_HATCH
;
202 else if ( item
== 8 )
203 penStyle
= wxFDIAGONAL_HATCH
;
204 else if ( item
== 9 )
205 penStyle
= wxCROSS_HATCH
;
206 else if ( item
== 10 )
207 penStyle
= wxHORIZONTAL_HATCH
;
208 else if ( item
== 11 )
209 penStyle
= wxVERTICAL_HATCH
;
211 wxPen
pen( dc
.GetTextForeground(), 3, penStyle
);
213 // Get text colour as pen colour
216 if ( !(flags
& wxODCB_PAINTING_CONTROL
) )
218 dc
.DrawText(GetString( item
),
220 (r
.y
+ 0) + ( (r
.height
/2) - dc
.GetCharHeight() )/2
223 dc
.DrawLine( r
.x
+5, r
.y
+((r
.height
/4)*3), r
.x
+r
.width
- 5, r
.y
+((r
.height
/4)*3) );
227 dc
.DrawLine( r
.x
+5, r
.y
+r
.height
/2, r
.x
+r
.width
- 5, r
.y
+r
.height
/2 );
231 virtual void OnDrawBackground( wxDC
& dc
, const wxRect
& rect
,
232 int item
, int flags
) const
235 // If item is selected or even, or we are painting the
236 // combo control itself, use the default rendering.
237 if ( (flags
& (wxODCB_PAINTING_CONTROL
|wxODCB_PAINTING_SELECTED
)) ||
240 wxOwnerDrawnComboBox::OnDrawBackground(dc
,rect
,item
,flags
);
244 // Otherwise, draw every other background with different colour.
245 wxColour
bgCol(240,240,250);
246 dc
.SetBrush(wxBrush(bgCol
));
247 dc
.SetPen(wxPen(bgCol
));
248 dc
.DrawRectangle(rect
);
251 virtual wxCoord
OnMeasureItem( size_t item
) const
253 // Simply demonstrate the ability to have variable-height items
260 virtual wxCoord
OnMeasureItemWidth( size_t WXUNUSED(item
) ) const
262 return -1; // default - will be measured from text width
268 // ----------------------------------------------------------------------------
269 // wxListView Custom popup interface
270 // ----------------------------------------------------------------------------
272 #include <wx/listctrl.h>
274 class ListViewComboPopup
: public wxListView
, public wxComboPopup
281 m_itemHere
= -1; // hot item in list
284 virtual bool Create( wxWindow
* parent
)
286 return wxListView::Create(parent
,1,
287 wxPoint(0,0),wxDefaultSize
,
288 wxLC_LIST
|wxLC_SINGLE_SEL
|
289 wxLC_SORT_ASCENDING
|wxSIMPLE_BORDER
);
292 virtual wxWindow
*GetControl() { return this; }
294 virtual void SetStringValue( const wxString
& s
)
296 int n
= wxListView::FindItem(-1,s
);
297 if ( n
>= 0 && n
< GetItemCount() )
298 wxListView::Select(n
);
301 virtual wxString
GetStringValue() const
304 return wxListView::GetItemText(m_value
);
305 return wxEmptyString
;
309 // Popup event handlers
312 // Mouse hot-tracking
313 void OnMouseMove(wxMouseEvent
& event
)
315 // Move selection to cursor if it is inside the popup
318 int itemHere
= HitTest(event
.GetPosition(),resFlags
);
321 wxListView::Select(itemHere
,true);
322 m_itemHere
= itemHere
;
327 // On mouse left, set the value and close the popup
328 void OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
330 m_value
= m_itemHere
;
336 // Utilies for item manipulation
339 void AddSelection( const wxString
& selstr
)
341 wxListView::InsertItem(GetItemCount(),selstr
);
346 int m_value
; // current item index
347 int m_itemHere
; // hot item in popup
350 DECLARE_EVENT_TABLE()
353 BEGIN_EVENT_TABLE(ListViewComboPopup
, wxListView
)
354 EVT_MOTION(ListViewComboPopup::OnMouseMove
)
355 // NOTE: Left down event is used instead of left up right now
356 // since MSW wxListCtrl doesn't seem to emit left ups
358 EVT_LEFT_DOWN(ListViewComboPopup::OnMouseClick
)
362 // ----------------------------------------------------------------------------
363 // wxTreeCtrl Custom popup interface
364 // ----------------------------------------------------------------------------
366 #include <wx/treectrl.h>
368 class TreeCtrlComboPopup
: public wxTreeCtrl
, public wxComboPopup
376 virtual bool Create( wxWindow
* parent
)
378 return wxTreeCtrl::Create(parent
,1,
379 wxPoint(0,0),wxDefaultSize
,
380 wxTR_HIDE_ROOT
|wxTR_HAS_BUTTONS
|
381 wxTR_SINGLE
|wxTR_LINES_AT_ROOT
|
385 virtual void OnShow()
387 // make sure selected item is visible
388 if ( m_value
.IsOk() )
389 EnsureVisible(m_value
);
392 virtual wxSize
GetAdjustedSize( int minWidth
,
393 int WXUNUSED(prefHeight
),
396 return wxSize(wxMax(300,minWidth
),wxMin(250,maxHeight
));
399 virtual wxWindow
*GetControl() { return this; }
401 // Needed by SetStringValue
402 wxTreeItemId
FindItemByText( wxTreeItemId parent
, const wxString
& text
)
404 wxTreeItemIdValue cookie
;
405 wxTreeItemId child
= GetFirstChild(parent
,cookie
);
406 while ( child
.IsOk() )
408 if ( GetItemText(child
) == text
)
412 if ( ItemHasChildren(child
) )
414 wxTreeItemId found
= FindItemByText(child
,text
);
418 child
= GetNextChild(parent
,cookie
);
420 return wxTreeItemId();
423 virtual void SetStringValue( const wxString
& s
)
425 wxTreeItemId root
= GetRootItem();
429 wxTreeItemId found
= FindItemByText(root
,s
);
432 m_value
= m_itemHere
= found
;
433 wxTreeCtrl::SelectItem(found
);
437 virtual wxString
GetStringValue() const
439 if ( m_value
.IsOk() )
440 return wxTreeCtrl::GetItemText(m_value
);
441 return wxEmptyString
;
445 // Popup event handlers
448 // Mouse hot-tracking
449 void OnMouseMove(wxMouseEvent
& event
)
452 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
453 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
455 wxTreeCtrl::SelectItem(itemHere
,true);
456 m_itemHere
= itemHere
;
461 // On mouse left, set the value and close the popup
462 void OnMouseClick(wxMouseEvent
& event
)
465 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
466 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
468 m_itemHere
= itemHere
;
478 wxTreeItemId m_value
; // current item index
479 wxTreeItemId m_itemHere
; // hot item in popup
482 DECLARE_EVENT_TABLE()
485 BEGIN_EVENT_TABLE(TreeCtrlComboPopup
, wxTreeCtrl
)
486 EVT_MOTION(TreeCtrlComboPopup::OnMouseMove
)
487 // NOTE: Left down event is used instead of left up right now
488 // since MSW wxTreeCtrl doesn't seem to emit left ups
490 EVT_LEFT_DOWN(TreeCtrlComboPopup::OnMouseClick
)
493 // ----------------------------------------------------------------------------
494 // wxComboCtrl with entirely custom button action (opens file dialog)
495 // ----------------------------------------------------------------------------
497 class wxFileSelectorCombo
: public wxComboCtrl
500 wxFileSelectorCombo() : wxComboCtrl() { Init(); }
502 wxFileSelectorCombo(wxWindow
*parent
,
503 wxWindowID id
= wxID_ANY
,
504 const wxString
& value
= wxEmptyString
,
505 const wxPoint
& pos
= wxDefaultPosition
,
506 const wxSize
& size
= wxDefaultSize
,
508 const wxValidator
& validator
= wxDefaultValidator
,
509 const wxString
& name
= wxComboBoxNameStr
)
513 Create(parent
,id
,value
,
515 // Style flag wxCC_STD_BUTTON makes the button
516 // behave more like a standard push button.
517 style
| wxCC_STD_BUTTON
,
521 // Prepare custom button bitmap (just '...' text)
524 dc
.SelectObject(bmp
);
526 // Draw transparent background
527 wxColour
magic(255,0,255);
528 wxBrush
magicBrush(magic
);
529 dc
.SetBrush( magicBrush
);
530 dc
.SetPen( *wxTRANSPARENT_PEN
);
531 dc
.DrawRectangle(0,0,bmp
.GetWidth(),bmp
.GetHeight());
534 wxString str
= wxT("...");
536 dc
.GetTextExtent(str
, &w
, &h
, 0, 0);
537 dc
.DrawText(str
, (bmp
.GetWidth()-w
)/2, (bmp
.GetHeight()-h
)/2);
539 dc
.SelectObject( wxNullBitmap
);
541 // Finalize transparency with a mask
542 wxMask
*mask
= new wxMask( bmp
, magic
);
545 SetButtonBitmaps(bmp
,true);
548 virtual void OnButtonClick()
550 // Show standard wxFileDialog on button click
552 wxFileDialog
dlg(this,
556 wxT("All files (*.*)|*.*"),
559 if ( dlg
.ShowModal() == wxID_OK
)
561 SetValue(dlg
.GetPath());
565 // Implement empty DoSetPopupControl to prevent assertion failure.
566 virtual void DoSetPopupControl(wxComboPopup
* WXUNUSED(popup
))
573 // Initialize member variables here
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
582 MyFrame::MyFrame(const wxString
& title
)
583 : wxFrame(NULL
, wxID_ANY
, title
)
585 wxBoxSizer
* topSizer
;
586 wxBoxSizer
* topRowSizer
;
587 wxBoxSizer
* colSizer
;
588 wxBoxSizer
* rowSizer
;
590 // set the frame icon
591 SetIcon(wxICON(sample
));
595 wxMenu
*fileMenu
= new wxMenu
;
597 // the "About" item should be in the help menu
598 wxMenu
*helpMenu
= new wxMenu
;
599 helpMenu
->Append(ComboControl_About
, _T("&About...\tF1"), _T("Show about dialog"));
601 fileMenu
->Append(ComboControl_Compare
, _T("&Compare against wxComboBox..."),
602 _T("Show some wxOwnerDrawnComboBoxes side-by-side with native wxComboBoxes."));
603 fileMenu
->AppendSeparator();
604 fileMenu
->Append(ComboControl_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
606 // now append the freshly created menu to the menu bar...
607 wxMenuBar
*menuBar
= new wxMenuBar();
608 menuBar
->Append(fileMenu
, _T("&File"));
609 menuBar
->Append(helpMenu
, _T("&Help"));
611 // ... and attach this menu bar to the frame
613 #endif // wxUSE_MENUS
615 wxPanel
* panel
= new wxPanel(this);
617 // Prepare log window right away since it shows EVT_TEXTs
618 m_logWin
= new wxTextCtrl( panel
, 105, wxEmptyString
, wxDefaultPosition
,
619 wxSize(-1,125), wxTE_MULTILINE
|wxFULL_REPAINT_ON_RESIZE
);
620 m_logWin
->SetEditable(false);
621 wxLogTextCtrl
* logger
= new wxLogTextCtrl( m_logWin
);
622 m_logOld
= logger
->SetActiveTarget( logger
);
623 logger
->SetTimestamp( NULL
);
626 topSizer
= new wxBoxSizer( wxVERTICAL
);
628 topRowSizer
= new wxBoxSizer( wxHORIZONTAL
);
630 colSizer
= new wxBoxSizer( wxVERTICAL
);
634 wxGenericComboCtrl
* gcc
;
635 wxOwnerDrawnComboBox
* odc
;
637 // Create common strings array
638 m_arrItems
.Add( wxT("Solid") );
639 m_arrItems
.Add( wxT("Transparent") );
640 m_arrItems
.Add( wxT("Dot") );
641 m_arrItems
.Add( wxT("Long Dash") );
642 m_arrItems
.Add( wxT("Short Dash") );
643 m_arrItems
.Add( wxT("Dot Dash") );
644 m_arrItems
.Add( wxT("Backward Diagonal Hatch") );
645 m_arrItems
.Add( wxT("Cross-diagonal Hatch") );
646 m_arrItems
.Add( wxT("Forward Diagonal Hatch") );
647 m_arrItems
.Add( wxT("Cross Hatch") );
648 m_arrItems
.Add( wxT("Horizontal Hatch") );
649 m_arrItems
.Add( wxT("Vertical Hatch") );
653 // Create pen selector ODComboBox with owner-drawn items
655 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
656 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
657 wxT("OwnerDrawnComboBox with owner-drawn items:")), 1,
658 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
659 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
661 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
664 // When defining derivative class for callbacks, we need
665 // to use two-stage creation (or redefine the common wx
667 odc
= new wxPenStyleComboBox();
668 odc
->Create(panel
,wxID_ANY
,wxEmptyString
,
669 wxDefaultPosition
, wxDefaultSize
,
671 wxCB_READONLY
//wxNO_BORDER | wxCB_READONLY
675 odc
->SetSelection(0);
677 rowSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
678 rowSizer
->AddStretchSpacer(1);
679 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
684 // Same but with changed button position
686 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
687 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
688 wxT("OwnerDrawnComboBox with owner-drawn items and button on the left:")), 1,
689 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
690 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
692 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
695 // When defining derivative class for callbacks, we need
696 // to use two-stage creation (or redefine the common wx
698 odc
= new wxPenStyleComboBox();
699 odc
->Create(panel
,wxID_ANY
,wxEmptyString
,
700 wxDefaultPosition
, wxDefaultSize
,
702 wxCB_READONLY
//wxNO_BORDER | wxCB_READONLY
706 odc
->SetSelection(0);
708 // Use button size that is slightly smaller than the default.
709 wxSize butSize
= odc
->GetButtonSize();
710 odc
->SetButtonPosition(butSize
.x
- 2, // button width
711 butSize
.y
- 6, // button height
713 2 // horizontal spacing
716 rowSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
717 rowSizer
->AddStretchSpacer(1);
718 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
722 // List View wxComboCtrl
725 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
726 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,wxT("List View wxComboCtrl:")), 1,
727 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
728 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,wxT("Tree Ctrl wxComboControl:")), 1,
729 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
730 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
732 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
733 cc
= new wxComboCtrl(panel
,2,wxEmptyString
,
734 wxDefaultPosition
, wxDefaultSize
);
736 // Make sure we use popup that allows focusing the listview.
737 cc
->UseAltPopupWindow();
739 cc
->SetPopupMinWidth(300);
741 ListViewComboPopup
* iface
= new ListViewComboPopup();
742 cc
->SetPopupControl(iface
);
745 for ( i
=0; i
<100; i
++ )
746 iface
->AddSelection( wxString::Format(wxT("Item %02i"),i
));
748 rowSizer
->Add( cc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
752 // Tree Ctrl wxComboCtrl
755 // Note that we test that wxGenericComboCtrl works
756 gcc
= new wxGenericComboCtrl(panel
,wxID_ANY
,wxEmptyString
,
757 wxDefaultPosition
, wxDefaultSize
);
759 // Make sure we use popup that allows focusing the treectrl.
760 gcc
->UseAltPopupWindow();
762 // Set popup interface right away, otherwise some of the calls
764 TreeCtrlComboPopup
* tcPopup
= new TreeCtrlComboPopup();
765 gcc
->SetPopupControl(tcPopup
);
767 // Add items using wxTreeCtrl methods directly
768 wxTreeItemId rootId
= tcPopup
->AddRoot(wxT("<hidden_root>"));
770 wxTreeItemId groupId
;
772 for ( i
=0; i
<4; i
++ )
774 groupId
= tcPopup
->AppendItem(rootId
,
775 wxString::Format(wxT("Branch %02i"),i
));
778 for ( n
=0; n
<25; n
++ )
779 tcPopup
->AppendItem(groupId
,
780 wxString::Format(wxT("Subitem %02i"),(i
*25)+n
));
783 gcc
->SetValue(wxT("Subitem 05"));
785 // Move button to left - it makes more sense for a tree ctrl
786 gcc
->SetButtonPosition(-1, // button width
789 0 // horizontal spacing
792 rowSizer
->Add( gcc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
794 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
797 wxInitAllImageHandlers();
800 // Custom Dropbutton Bitmaps
801 // (second one uses blank button background)
803 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
804 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
805 wxT("OwnerDrawnComboBox with simple dropbutton graphics:")), 1,
806 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
808 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
810 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
812 odc
= new wxOwnerDrawnComboBox(panel
,wxID_ANY
,wxEmptyString
,
813 wxDefaultPosition
, wxDefaultSize
,
815 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
818 wxOwnerDrawnComboBox
* odc2
;
819 odc2
= new wxOwnerDrawnComboBox(panel
,wxID_ANY
,wxEmptyString
,
820 wxDefaultPosition
, wxDefaultSize
,
822 (long)0 // wxCB_SORT // wxNO_BORDER | wxCB_READONLY
825 // Load images from disk
826 wxImage
imgNormal(wxT("dropbutn.png"));
827 wxImage
imgPressed(wxT("dropbutp.png"));
828 wxImage
imgHover(wxT("dropbuth.png"));
830 if ( imgNormal
.IsOk() && imgPressed
.IsOk() && imgHover
.IsOk() )
832 wxBitmap
bmpNormal(imgNormal
);
833 wxBitmap
bmpPressed(imgPressed
);
834 wxBitmap
bmpHover(imgHover
);
835 odc
->SetButtonBitmaps(bmpNormal
,false,bmpPressed
,bmpHover
);
836 odc2
->SetButtonBitmaps(bmpNormal
,true,bmpPressed
,bmpHover
);
839 wxLogError(wxT("Dropbutton images not found"));
841 //odc2->SetButtonPosition(0, // width adjustment
842 // 0, // height adjustment
844 // 0 // horizontal spacing
847 rowSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
848 rowSizer
->Add( odc2
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
849 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
854 // wxComboCtrl with totally custom button action (open file dialog)
856 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
857 rowSizer
->Add( new wxStaticText(panel
,wxID_ANY
,
858 wxT("wxComboCtrl with custom button action:")), 1,
859 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4 );
862 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
864 rowSizer
= new wxBoxSizer( wxHORIZONTAL
);
865 wxFileSelectorCombo
* fsc
;
867 fsc
= new wxFileSelectorCombo(panel
,wxID_ANY
,wxEmptyString
,
868 wxDefaultPosition
, wxDefaultSize
,
872 rowSizer
->Add( fsc
, 1, wxALIGN_CENTER_VERTICAL
|wxALL
, 4 );
873 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, 5 );
876 // Make sure GetFeatures is implemented
877 wxComboCtrl::GetFeatures();
880 topRowSizer
->Add( colSizer
, 1, wxALL
, 2 );
882 topRowSizer
->Add( m_logWin
, 1, wxEXPAND
|wxALL
, 5 );
883 topSizer
->Add( topRowSizer
, 1, wxEXPAND
);
885 panel
->SetSizer( topSizer
);
886 topSizer
->SetSizeHints( panel
);
894 void MyFrame::OnComboBoxUpdate( wxCommandEvent
& event
)
896 // Don't show messages for the log output window (it'll crash)
897 if ( !event
.GetEventObject()->IsKindOf(CLASSINFO(wxComboCtrl
)) )
900 if ( event
.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED
)
901 wxLogDebug(wxT("EVT_COMBOBOX(id=%i,selection=%i)"),event
.GetId(),event
.GetSelection());
902 else if ( event
.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED
)
903 wxLogDebug(wxT("EVT_TEXT(id=%i,string=\"%s\")"),event
.GetId(),event
.GetString().c_str());
906 void MyFrame::OnShowComparison( wxCommandEvent
& WXUNUSED(event
) )
909 // Show some wxOwnerDrawComboBoxes for comparison
912 wxBoxSizer
* colSizer
;
913 wxBoxSizer
* rowSizer
;
914 wxStaticBoxSizer
* groupSizer
;
917 wxOwnerDrawnComboBox
* odc
;
919 const int border
= 4;
921 wxDialog
* dlg
= new wxDialog(this,wxID_ANY
,
922 wxT("Compare against wxComboBox"),
923 wxDefaultPosition
,wxDefaultSize
,
924 wxDEFAULT_DIALOG_STYLE
|wxRESIZE_BORDER
);
926 colSizer
= new wxBoxSizer( wxVERTICAL
);
928 rowSizer
= new wxBoxSizer(wxHORIZONTAL
);
930 groupSizer
= new wxStaticBoxSizer(new wxStaticBox(dlg
,wxID_ANY
,wxT(" wxOwnerDrawnComboBox ")),
933 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Writable, sorted:")), 0,
934 wxALIGN_CENTER_VERTICAL
|wxRIGHT
|wxEXPAND
, border
);
936 odc
= new wxOwnerDrawnComboBox(dlg
,wxID_ANY
,wxEmptyString
,
937 wxDefaultPosition
, wxDefaultSize
,
939 wxCB_SORT
// wxNO_BORDER|wxCB_READONLY
942 odc
->Append(wxT("H - Appended Item")); // test sorting in append
944 odc
->SetValue(wxT("Dot Dash"));
946 groupSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
949 // Readonly ODComboBox
950 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Read-only:")), 0,
951 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
953 odc
= new wxOwnerDrawnComboBox(dlg
,wxID_ANY
,wxEmptyString
,
954 wxDefaultPosition
, wxDefaultSize
,
956 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
959 odc
->SetValue(wxT("Dot Dash"));
960 odc
->SetText(wxT("Dot Dash (Testing SetText)"));
962 groupSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
965 // Disabled ODComboBox
966 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Disabled:")), 0,
967 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
969 odc
= new wxOwnerDrawnComboBox(dlg
,wxID_ANY
,wxEmptyString
,
970 wxDefaultPosition
, wxDefaultSize
,
972 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
975 odc
->SetValue(wxT("Dot Dash"));
978 groupSizer
->Add( odc
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
980 rowSizer
->Add( groupSizer
, 1, wxEXPAND
|wxALL
, border
);
983 groupSizer
= new wxStaticBoxSizer(new wxStaticBox(dlg
,wxID_ANY
,wxT(" wxComboBox ")),
989 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Writable, sorted:")), 0,
990 wxALIGN_CENTER_VERTICAL
|wxRIGHT
|wxEXPAND
, border
);
992 cb
= new wxComboBox(dlg
,wxID_ANY
,wxEmptyString
,
993 wxDefaultPosition
, wxDefaultSize
,
995 wxCB_SORT
// wxNO_BORDER|wxCB_READONLY
998 cb
->Append(wxT("H - Appended Item")); // test sorting in append
1000 cb
->SetValue(wxT("Dot Dash"));
1002 groupSizer
->Add( cb
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1005 // Readonly wxComboBox
1006 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Read-only:")), 0,
1007 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1009 cb
= new wxComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1010 wxDefaultPosition
, wxDefaultSize
,
1012 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1015 cb
->SetValue(wxT("Dot Dash"));
1017 groupSizer
->Add( cb
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1020 // Disabled wxComboBox
1021 groupSizer
->Add( new wxStaticText(dlg
,wxID_ANY
,wxT("Disabled:")), 0,
1022 wxALIGN_CENTER_VERTICAL
|wxRIGHT
, border
);
1024 cb
= new wxComboBox(dlg
,wxID_ANY
,wxEmptyString
,
1025 wxDefaultPosition
, wxDefaultSize
,
1027 wxCB_SORT
|wxCB_READONLY
// wxNO_BORDER|wxCB_READONLY
1030 cb
->SetValue(wxT("Dot Dash"));
1033 groupSizer
->Add( cb
, 1, wxALIGN_CENTER_VERTICAL
|wxEXPAND
|wxALL
, border
);
1035 rowSizer
->Add( groupSizer
, 1, wxEXPAND
|wxALL
, border
);
1037 colSizer
->Add( rowSizer
, 0, wxEXPAND
|wxALL
, border
);
1039 dlg
->SetSizer( colSizer
);
1040 colSizer
->SetSizeHints( dlg
);
1042 dlg
->SetSize(60,240);
1049 delete wxLog::SetActiveTarget(m_logOld
);
1052 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
1054 // true is to force the frame to close
1058 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
1060 wxMessageBox(wxString::Format(
1061 _T("Welcome to %s!\n")
1063 _T("This is the wxWidgets wxComboCtrl and wxOwnerDrawnComboBox sample\n")
1064 _T("running under %s."),
1066 wxGetOsDescription().c_str()
1068 _T("About wxComboCtrl sample"),
1069 wxOK
| wxICON_INFORMATION
,
1073 void MyFrame::OnIdle(wxIdleEvent
& event
)
1075 // This code is useful for debugging focus problems
1076 // (which are plentiful when dealing with popup windows).
1078 static wxWindow
* lastFocus
= (wxWindow
*) NULL
;
1080 wxWindow
* curFocus
= ::wxWindow::FindFocus();
1082 if ( curFocus
!= lastFocus
)
1084 const wxChar
* className
= wxT("<none>");
1086 className
= curFocus
->GetClassInfo()->GetClassName();
1087 lastFocus
= curFocus
;
1088 wxLogDebug( wxT("FOCUSED: %s %X"),
1090 (unsigned int)curFocus
);