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