1 /////////////////////////////////////////////////////////////////////////////
2 // Name: custom_combo.h
3 // Purpose: Define some custom wxComboCtrls
4 // Author: Utensil Candel (UtensilCandel@@gmail.com)
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
9 #ifndef WX_CUSTOM_COMBO_H
10 #define WX_CUSTOM_COMBO_H
12 // ----------------------------------------------------------------------------
13 // class PenStyleComboBox
14 // This class is a modified version of the one from samples/combo.cpp
15 // ----------------------------------------------------------------------------
17 #include <wx/odcombo.h>
19 class PenStyleComboBox
: public wxOwnerDrawnComboBox
22 virtual void OnDrawItem( wxDC
& dc
,
28 virtual void OnDrawBackground( wxDC
& dc
, const wxRect
& rect
,
29 int item
, int flags
) const;
31 virtual wxCoord
OnMeasureItem( size_t item
) const;
33 virtual wxCoord
OnMeasureItemWidth( size_t WXUNUSED(item
) ) const;
35 static PenStyleComboBox
* CreateSample(wxWindow
* parent
);
39 // ----------------------------------------------------------------------------
40 // wxListView Custom popup interface
41 // This class is a modified version of the one from samples/combo.cpp
42 // ----------------------------------------------------------------------------
44 #include <wx/listctrl.h>
46 class ListViewComboPopup
: public wxListView
, public wxComboPopup
53 m_itemHere
= -1; // hot item in list
56 virtual bool Create( wxWindow
* parent
)
58 return wxListView::Create(parent
,1,
59 wxPoint(0,0),wxDefaultSize
,
60 wxLC_LIST
|wxLC_VRULES
|wxBORDER_THEME
);
63 virtual wxWindow
*GetControl()
66 virtual void SetStringValue( const wxString
& s
)
68 int n
= wxListView::FindItem(-1,s
);
69 if ( n
>= 0 && n
< GetItemCount() )
70 wxListView::Select(n
);
73 virtual wxString
GetStringValue() const
76 return wxListView::GetItemText(m_value
);
81 // Popup event handlers
85 void OnMouseMove(wxMouseEvent
& event
)
87 // Move selection to cursor if it is inside the popup
90 int itemHere
= HitTest(event
.GetPosition(),resFlags
);
93 wxListView::Select(itemHere
,true);
94 m_itemHere
= itemHere
;
99 // On mouse left, set the value and close the popup
100 void OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
102 m_value
= m_itemHere
;
108 // Utilies for item manipulation
111 void AddSelection( const wxString
& selstr
)
113 wxListView::InsertItem(GetItemCount(),selstr
);
117 int m_value
; // current item index
118 int m_itemHere
; // hot item in popup
121 DECLARE_EVENT_TABLE()
125 // ----------------------------------------------------------------------------
126 // wxTreeCtrl Custom popup interface
127 // This class is a modified version of the one from samples/combo.cpp
128 // ----------------------------------------------------------------------------
130 #include <wx/treectrl.h>
132 class TreeCtrlComboPopup
: public wxTreeCtrl
, public wxComboPopup
140 virtual bool Create( wxWindow
* parent
)
142 return wxTreeCtrl::Create(parent
,1,
143 wxPoint(0,0),wxDefaultSize
,
144 wxTR_HAS_BUTTONS
|wxTR_SINGLE
|
145 wxTR_LINES_AT_ROOT
|wxBORDER_THEME
);
148 virtual void OnShow()
150 // make sure selected item is visible
151 if ( m_value
.IsOk() )
152 EnsureVisible(m_value
);
155 virtual wxSize
GetAdjustedSize( int minWidth
,
156 int WXUNUSED(prefHeight
),
159 return wxSize(minWidth
, wxMin(80, maxHeight
));
162 virtual wxWindow
*GetControl()
165 // Needed by SetStringValue
166 wxTreeItemId
FindItemByText( wxTreeItemId parent
, const wxString
& text
)
168 wxTreeItemIdValue cookie
;
169 wxTreeItemId child
= GetFirstChild(parent
,cookie
);
170 while ( child
.IsOk() )
172 if ( GetItemText(child
) == text
)
176 if ( ItemHasChildren(child
) )
178 wxTreeItemId found
= FindItemByText(child
,text
);
182 child
= GetNextChild(parent
,cookie
);
184 return wxTreeItemId();
187 virtual void SetStringValue( const wxString
& s
)
189 wxTreeItemId root
= GetRootItem();
193 wxTreeItemId found
= FindItemByText(root
,s
);
196 m_value
= m_itemHere
= found
;
197 wxTreeCtrl::SelectItem(found
);
201 virtual wxString
GetStringValue() const
203 if ( m_value
.IsOk() )
204 return wxTreeCtrl::GetItemText(m_value
);
205 return wxEmptyString
;
209 // Popup event handlers
212 // Mouse hot-tracking
213 void OnMouseMove(wxMouseEvent
& event
)
216 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
217 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
219 wxTreeCtrl::SelectItem(itemHere
,true);
220 m_itemHere
= itemHere
;
225 // On mouse left, set the value and close the popup
226 void OnMouseClick(wxMouseEvent
& event
)
229 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
230 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
232 m_itemHere
= itemHere
;
241 wxTreeItemId m_value
; // current item index
242 wxTreeItemId m_itemHere
; // hot item in popup
245 DECLARE_EVENT_TABLE()
249 #endif // WX_CUSTOM_COMBO_H