1 /////////////////////////////////////////////////////////////////////////////
2 // Name: custom_combo.h
3 // Purpose: Define some custom wxComboCtrls
4 // Author: Utensil Candel (UtensilCandel@@gmail.com)
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
8 #ifndef WX_CUSTOM_COMBO_H
9 #define WX_CUSTOM_COMBO_H
11 // ----------------------------------------------------------------------------
12 // class PenStyleComboBox
13 // This class is a modified version of the one from samples/combo.cpp
14 // ----------------------------------------------------------------------------
16 #include <wx/odcombo.h>
18 class PenStyleComboBox
: public wxOwnerDrawnComboBox
21 virtual void OnDrawItem( wxDC
& dc
,
27 virtual void OnDrawBackground( wxDC
& dc
, const wxRect
& rect
,
28 int item
, int flags
) const;
30 virtual wxCoord
OnMeasureItem( size_t item
) const;
32 virtual wxCoord
OnMeasureItemWidth( size_t WXUNUSED(item
) ) const;
34 static PenStyleComboBox
* CreateSample(wxWindow
* parent
);
38 // ----------------------------------------------------------------------------
39 // wxListView Custom popup interface
40 // This class is a modified version of the one from samples/combo.cpp
41 // ----------------------------------------------------------------------------
43 #include <wx/listctrl.h>
45 class ListViewComboPopup
: public wxListView
, public wxComboPopup
52 m_itemHere
= -1; // hot item in list
55 virtual bool Create( wxWindow
* parent
)
57 return wxListView::Create(parent
,1,
58 wxPoint(0,0),wxDefaultSize
,
59 wxLC_LIST
|wxLC_VRULES
|wxBORDER_THEME
);
62 virtual wxWindow
*GetControl()
65 virtual void SetStringValue( const wxString
& s
)
67 int n
= wxListView::FindItem(-1,s
);
68 if ( n
>= 0 && n
< GetItemCount() )
69 wxListView::Select(n
);
72 virtual wxString
GetStringValue() const
75 return wxListView::GetItemText(m_value
);
80 // Popup event handlers
84 void OnMouseMove(wxMouseEvent
& event
)
86 // Move selection to cursor if it is inside the popup
89 int itemHere
= HitTest(event
.GetPosition(),resFlags
);
92 wxListView::Select(itemHere
,true);
93 m_itemHere
= itemHere
;
98 // On mouse left, set the value and close the popup
99 void OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
101 m_value
= m_itemHere
;
107 // Utilies for item manipulation
110 void AddSelection( const wxString
& selstr
)
112 wxListView::InsertItem(GetItemCount(),selstr
);
116 int m_value
; // current item index
117 int m_itemHere
; // hot item in popup
120 DECLARE_EVENT_TABLE()
124 // ----------------------------------------------------------------------------
125 // wxTreeCtrl Custom popup interface
126 // This class is a modified version of the one from samples/combo.cpp
127 // ----------------------------------------------------------------------------
129 #include <wx/treectrl.h>
131 class TreeCtrlComboPopup
: public wxTreeCtrl
, public wxComboPopup
139 virtual bool Create( wxWindow
* parent
)
141 return wxTreeCtrl::Create(parent
,1,
142 wxPoint(0,0),wxDefaultSize
,
143 wxTR_HAS_BUTTONS
|wxTR_SINGLE
|
144 wxTR_LINES_AT_ROOT
|wxBORDER_THEME
);
147 virtual void OnShow()
149 // make sure selected item is visible
150 if ( m_value
.IsOk() )
151 EnsureVisible(m_value
);
154 virtual wxSize
GetAdjustedSize( int minWidth
,
155 int WXUNUSED(prefHeight
),
158 return wxSize(minWidth
, wxMin(80, maxHeight
));
161 virtual wxWindow
*GetControl()
164 // Needed by SetStringValue
165 wxTreeItemId
FindItemByText( wxTreeItemId parent
, const wxString
& text
)
167 wxTreeItemIdValue cookie
;
168 wxTreeItemId child
= GetFirstChild(parent
,cookie
);
169 while ( child
.IsOk() )
171 if ( GetItemText(child
) == text
)
175 if ( ItemHasChildren(child
) )
177 wxTreeItemId found
= FindItemByText(child
,text
);
181 child
= GetNextChild(parent
,cookie
);
183 return wxTreeItemId();
186 virtual void SetStringValue( const wxString
& s
)
188 wxTreeItemId root
= GetRootItem();
192 wxTreeItemId found
= FindItemByText(root
,s
);
195 m_value
= m_itemHere
= found
;
196 wxTreeCtrl::SelectItem(found
);
200 virtual wxString
GetStringValue() const
202 if ( m_value
.IsOk() )
203 return wxTreeCtrl::GetItemText(m_value
);
204 return wxEmptyString
;
208 // Popup event handlers
211 // Mouse hot-tracking
212 void OnMouseMove(wxMouseEvent
& event
)
215 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
216 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
218 wxTreeCtrl::SelectItem(itemHere
,true);
219 m_itemHere
= itemHere
;
224 // On mouse left, set the value and close the popup
225 void OnMouseClick(wxMouseEvent
& event
)
228 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
229 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
231 m_itemHere
= itemHere
;
240 wxTreeItemId m_value
; // current item index
241 wxTreeItemId m_itemHere
; // hot item in popup
244 DECLARE_EVENT_TABLE()
248 #endif // WX_CUSTOM_COMBO_H