]> git.saurik.com Git - wxWidgets.git/blob - utils/screenshotgen/src/customcombo.h
Don't use a destroy event handler to disconnect the native control's delegate as...
[wxWidgets.git] / utils / screenshotgen / src / customcombo.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: custom_combo.h
3 // Purpose: Define some custom wxComboCtrls
4 // Author: Utensil Candel (UtensilCandel@@gmail.com)
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 #ifndef WX_CUSTOM_COMBO_H
10 #define WX_CUSTOM_COMBO_H
11
12 // ----------------------------------------------------------------------------
13 // class PenStyleComboBox
14 // This class is a modified version of the one from samples/combo.cpp
15 // ----------------------------------------------------------------------------
16
17 #include <wx/odcombo.h>
18
19 class PenStyleComboBox : public wxOwnerDrawnComboBox
20 {
21 public:
22 virtual void OnDrawItem( wxDC& dc,
23 const wxRect& rect,
24 int item,
25 int flags ) const;
26
27
28 virtual void OnDrawBackground( wxDC& dc, const wxRect& rect,
29 int item, int flags ) const;
30
31 virtual wxCoord OnMeasureItem( size_t item ) const;
32
33 virtual wxCoord OnMeasureItemWidth( size_t WXUNUSED(item) ) const;
34
35 static PenStyleComboBox* CreateSample(wxWindow* parent);
36 };
37
38
39 // ----------------------------------------------------------------------------
40 // wxListView Custom popup interface
41 // This class is a modified version of the one from samples/combo.cpp
42 // ----------------------------------------------------------------------------
43
44 #include <wx/listctrl.h>
45
46 class ListViewComboPopup : public wxListView, public wxComboPopup
47 {
48 public:
49
50 virtual void Init()
51 {
52 m_value = -1;
53 m_itemHere = -1; // hot item in list
54 }
55
56 virtual bool Create( wxWindow* parent )
57 {
58 return wxListView::Create(parent,1,
59 wxPoint(0,0),wxDefaultSize,
60 wxLC_LIST|wxLC_VRULES|wxBORDER_THEME);
61 }
62
63 virtual wxWindow *GetControl()
64 { return this; }
65
66 virtual void SetStringValue( const wxString& s )
67 {
68 int n = wxListView::FindItem(-1,s);
69 if ( n >= 0 && n < GetItemCount() )
70 wxListView::Select(n);
71 }
72
73 virtual wxString GetStringValue() const
74 {
75 if ( m_value >= 0 )
76 return wxListView::GetItemText(m_value);
77 return wxEmptyString;
78 }
79
80 //
81 // Popup event handlers
82 //
83
84 // Mouse hot-tracking
85 void OnMouseMove(wxMouseEvent& event)
86 {
87 // Move selection to cursor if it is inside the popup
88
89 int resFlags;
90 int itemHere = HitTest(event.GetPosition(),resFlags);
91 if ( itemHere >= 0 )
92 {
93 wxListView::Select(itemHere,true);
94 m_itemHere = itemHere;
95 }
96 event.Skip();
97 }
98
99 // On mouse left, set the value and close the popup
100 void OnMouseClick(wxMouseEvent& WXUNUSED(event))
101 {
102 m_value = m_itemHere;
103 // TODO: Send event
104 Dismiss();
105 }
106
107 //
108 // Utilies for item manipulation
109 //
110
111 void AddSelection( const wxString& selstr )
112 {
113 wxListView::InsertItem(GetItemCount(),selstr);
114 }
115
116 protected:
117 int m_value; // current item index
118 int m_itemHere; // hot item in popup
119
120 private:
121 DECLARE_EVENT_TABLE()
122 };
123
124
125 // ----------------------------------------------------------------------------
126 // wxTreeCtrl Custom popup interface
127 // This class is a modified version of the one from samples/combo.cpp
128 // ----------------------------------------------------------------------------
129
130 #include <wx/treectrl.h>
131
132 class TreeCtrlComboPopup : public wxTreeCtrl, public wxComboPopup
133 {
134 public:
135
136 virtual void Init()
137 {
138 }
139
140 virtual bool Create( wxWindow* parent )
141 {
142 return wxTreeCtrl::Create(parent,1,
143 wxPoint(0,0),wxDefaultSize,
144 wxTR_HAS_BUTTONS|wxTR_SINGLE|
145 wxTR_LINES_AT_ROOT|wxBORDER_THEME);
146 }
147
148 virtual void OnShow()
149 {
150 // make sure selected item is visible
151 if ( m_value.IsOk() )
152 EnsureVisible(m_value);
153 }
154
155 virtual wxSize GetAdjustedSize( int minWidth,
156 int WXUNUSED(prefHeight),
157 int maxHeight )
158 {
159 return wxSize(minWidth, wxMin(80, maxHeight));
160 }
161
162 virtual wxWindow *GetControl()
163 { return this; }
164
165 // Needed by SetStringValue
166 wxTreeItemId FindItemByText( wxTreeItemId parent, const wxString& text )
167 {
168 wxTreeItemIdValue cookie;
169 wxTreeItemId child = GetFirstChild(parent,cookie);
170 while ( child.IsOk() )
171 {
172 if ( GetItemText(child) == text )
173 {
174 return child;
175 }
176 if ( ItemHasChildren(child) )
177 {
178 wxTreeItemId found = FindItemByText(child,text);
179 if ( found.IsOk() )
180 return found;
181 }
182 child = GetNextChild(parent,cookie);
183 }
184 return wxTreeItemId();
185 }
186
187 virtual void SetStringValue( const wxString& s )
188 {
189 wxTreeItemId root = GetRootItem();
190 if ( !root.IsOk() )
191 return;
192
193 wxTreeItemId found = FindItemByText(root,s);
194 if ( found.IsOk() )
195 {
196 m_value = m_itemHere = found;
197 wxTreeCtrl::SelectItem(found);
198 }
199 }
200
201 virtual wxString GetStringValue() const
202 {
203 if ( m_value.IsOk() )
204 return wxTreeCtrl::GetItemText(m_value);
205 return wxEmptyString;
206 }
207
208 //
209 // Popup event handlers
210 //
211
212 // Mouse hot-tracking
213 void OnMouseMove(wxMouseEvent& event)
214 {
215 int resFlags;
216 wxTreeItemId itemHere = HitTest(event.GetPosition(),resFlags);
217 if ( itemHere.IsOk() && (resFlags & wxTREE_HITTEST_ONITEMLABEL) )
218 {
219 wxTreeCtrl::SelectItem(itemHere,true);
220 m_itemHere = itemHere;
221 }
222 event.Skip();
223 }
224
225 // On mouse left, set the value and close the popup
226 void OnMouseClick(wxMouseEvent& event)
227 {
228 int resFlags;
229 wxTreeItemId itemHere = HitTest(event.GetPosition(),resFlags);
230 if ( itemHere.IsOk() && (resFlags & wxTREE_HITTEST_ONITEMLABEL) )
231 {
232 m_itemHere = itemHere;
233 m_value = itemHere;
234 Dismiss();
235 // TODO: Send event
236 }
237 event.Skip();
238 }
239
240 protected:
241 wxTreeItemId m_value; // current item index
242 wxTreeItemId m_itemHere; // hot item in popup
243
244 private:
245 DECLARE_EVENT_TABLE()
246 };
247
248
249 #endif // WX_CUSTOM_COMBO_H