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 wxPenStyleComboBox
14 // This class is a modified version of the one from samples/combo.cpp
15 // ----------------------------------------------------------------------------
17 #include <wx/odcombo.h>
19 class wxPenStyleComboBox
: 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 wxPenStyleComboBox
* 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() { return this; }
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() { return this; }
163 // Needed by SetStringValue
164 wxTreeItemId
FindItemByText( wxTreeItemId parent
, const wxString
& text
)
166 wxTreeItemIdValue cookie
;
167 wxTreeItemId child
= GetFirstChild(parent
,cookie
);
168 while ( child
.IsOk() )
170 if ( GetItemText(child
) == text
)
174 if ( ItemHasChildren(child
) )
176 wxTreeItemId found
= FindItemByText(child
,text
);
180 child
= GetNextChild(parent
,cookie
);
182 return wxTreeItemId();
185 virtual void SetStringValue( const wxString
& s
)
187 wxTreeItemId root
= GetRootItem();
191 wxTreeItemId found
= FindItemByText(root
,s
);
194 m_value
= m_itemHere
= found
;
195 wxTreeCtrl::SelectItem(found
);
199 virtual wxString
GetStringValue() const
201 if ( m_value
.IsOk() )
202 return wxTreeCtrl::GetItemText(m_value
);
203 return wxEmptyString
;
207 // Popup event handlers
210 // Mouse hot-tracking
211 void OnMouseMove(wxMouseEvent
& event
)
214 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
215 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
217 wxTreeCtrl::SelectItem(itemHere
,true);
218 m_itemHere
= itemHere
;
223 // On mouse left, set the value and close the popup
224 void OnMouseClick(wxMouseEvent
& event
)
227 wxTreeItemId itemHere
= HitTest(event
.GetPosition(),resFlags
);
228 if ( itemHere
.IsOk() && (resFlags
& wxTREE_HITTEST_ONITEMLABEL
) )
230 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