]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: choice.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "choice.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/choice.h" | |
16 | ||
17 | #if wxUSE_CHOICE | |
18 | ||
19 | #include <gdk/gdk.h> | |
20 | #include <gtk/gtk.h> | |
21 | ||
22 | //----------------------------------------------------------------------------- | |
23 | // idle system | |
24 | //----------------------------------------------------------------------------- | |
25 | ||
26 | extern void wxapp_install_idle_handler(); | |
27 | extern bool g_isIdle; | |
28 | ||
29 | //----------------------------------------------------------------------------- | |
30 | // data | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
33 | extern bool g_blockEventsOnDrag; | |
34 | ||
35 | //----------------------------------------------------------------------------- | |
36 | // "activate" | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
39 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) | |
40 | { | |
41 | if (g_isIdle) | |
42 | wxapp_install_idle_handler(); | |
43 | ||
44 | if (!choice->m_hasVMT) return; | |
45 | ||
46 | if (g_blockEventsOnDrag) return; | |
47 | ||
48 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); | |
49 | int n = choice->GetSelection(); | |
50 | ||
51 | event.SetInt( n ); | |
52 | event.SetString( choice->GetStringSelection() ); | |
53 | event.SetEventObject(choice); | |
54 | ||
55 | if ( choice->HasClientObjectData() ) | |
56 | event.SetClientObject( choice->GetClientObject(n) ); | |
57 | else if ( choice->HasClientUntypedData() ) | |
58 | event.SetClientData( choice->GetClientData(n) ); | |
59 | ||
60 | choice->GetEventHandler()->ProcessEvent(event); | |
61 | } | |
62 | ||
63 | //----------------------------------------------------------------------------- | |
64 | // wxChoice | |
65 | //----------------------------------------------------------------------------- | |
66 | ||
67 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) | |
68 | ||
69 | wxChoice::wxChoice() | |
70 | { | |
71 | m_strings = (wxSortedArrayString *)NULL; | |
72 | } | |
73 | ||
74 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, | |
75 | const wxPoint &pos, const wxSize &size, | |
76 | int n, const wxString choices[], | |
77 | long style, const wxValidator& validator, const wxString &name ) | |
78 | { | |
79 | m_needParent = TRUE; | |
80 | #if (GTK_MINOR_VERSION > 0) | |
81 | m_acceptsFocus = TRUE; | |
82 | #endif | |
83 | ||
84 | if (!PreCreation( parent, pos, size ) || | |
85 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
86 | { | |
87 | wxFAIL_MSG( wxT("wxChoice creation failed") ); | |
88 | return FALSE; | |
89 | } | |
90 | ||
91 | m_widget = gtk_option_menu_new(); | |
92 | ||
93 | SetSizeOrDefault( size ); | |
94 | ||
95 | if ( style & wxCB_SORT ) | |
96 | { | |
97 | // if our m_strings != NULL, DoAppend() will check for it and insert | |
98 | // items in the correct order | |
99 | m_strings = new wxSortedArrayString; | |
100 | } | |
101 | ||
102 | GtkWidget *menu = gtk_menu_new(); | |
103 | ||
104 | for (int i = 0; i < n; i++) | |
105 | { | |
106 | GtkAppendHelper(menu, choices[i]); | |
107 | } | |
108 | ||
109 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
110 | ||
111 | m_parent->DoAddChild( this ); | |
112 | ||
113 | PostCreation(); | |
114 | ||
115 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
116 | SetForegroundColour( parent->GetForegroundColour() ); | |
117 | SetFont( parent->GetFont() ); | |
118 | ||
119 | Show( TRUE ); | |
120 | ||
121 | return TRUE; | |
122 | } | |
123 | ||
124 | wxChoice::~wxChoice() | |
125 | { | |
126 | Clear(); | |
127 | ||
128 | delete m_strings; | |
129 | } | |
130 | ||
131 | int wxChoice::DoAppend( const wxString &item ) | |
132 | { | |
133 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); | |
134 | ||
135 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); | |
136 | ||
137 | return GtkAppendHelper(menu, item); | |
138 | } | |
139 | ||
140 | void wxChoice::DoSetItemClientData( int n, void* clientData ) | |
141 | { | |
142 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); | |
143 | ||
144 | wxNode *node = m_clientList.Nth( n ); | |
145 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") ); | |
146 | ||
147 | node->SetData( (wxObject*) clientData ); | |
148 | } | |
149 | ||
150 | void* wxChoice::DoGetItemClientData( int n ) const | |
151 | { | |
152 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") ); | |
153 | ||
154 | wxNode *node = m_clientList.Nth( n ); | |
155 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") ); | |
156 | ||
157 | return node->Data(); | |
158 | } | |
159 | ||
160 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) | |
161 | { | |
162 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); | |
163 | ||
164 | wxNode *node = m_clientList.Nth( n ); | |
165 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") ); | |
166 | ||
167 | wxClientData *cd = (wxClientData*) node->Data(); | |
168 | delete cd; | |
169 | ||
170 | node->SetData( (wxObject*) clientData ); | |
171 | } | |
172 | ||
173 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const | |
174 | { | |
175 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") ); | |
176 | ||
177 | wxNode *node = m_clientList.Nth( n ); | |
178 | wxCHECK_MSG( node, (wxClientData *)NULL, | |
179 | wxT("invalid index in wxChoice::DoGetItemClientObject") ); | |
180 | ||
181 | return (wxClientData*) node->Data(); | |
182 | } | |
183 | ||
184 | void wxChoice::Clear() | |
185 | { | |
186 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
187 | ||
188 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); | |
189 | GtkWidget *menu = gtk_menu_new(); | |
190 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
191 | ||
192 | if ( HasClientObjectData() ) | |
193 | { | |
194 | // destroy the data (due to Robert's idea of using wxList<wxObject> | |
195 | // and not wxList<wxClientData> we can't just say | |
196 | // m_clientList.DeleteContents(TRUE) - this would crash! | |
197 | wxNode *node = m_clientList.First(); | |
198 | while ( node ) | |
199 | { | |
200 | delete (wxClientData *)node->Data(); | |
201 | node = node->Next(); | |
202 | } | |
203 | } | |
204 | m_clientList.Clear(); | |
205 | ||
206 | if ( m_strings ) | |
207 | m_strings->Clear(); | |
208 | } | |
209 | ||
210 | void wxChoice::Delete( int WXUNUSED(n) ) | |
211 | { | |
212 | wxFAIL_MSG( wxT("wxChoice:Delete not implemented") ); | |
213 | } | |
214 | ||
215 | int wxChoice::FindString( const wxString &string ) const | |
216 | { | |
217 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); | |
218 | ||
219 | // If you read this code once and you think you understand | |
220 | // it, then you are very wrong. Robert Roebling. | |
221 | ||
222 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
223 | int count = 0; | |
224 | GList *child = menu_shell->children; | |
225 | while (child) | |
226 | { | |
227 | GtkBin *bin = GTK_BIN( child->data ); | |
228 | GtkLabel *label = (GtkLabel *) NULL; | |
229 | if (bin->child) label = GTK_LABEL(bin->child); | |
230 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); | |
231 | ||
232 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); | |
233 | ||
234 | if (string == wxString(label->label,*wxConvCurrent)) | |
235 | return count; | |
236 | ||
237 | child = child->next; | |
238 | count++; | |
239 | } | |
240 | ||
241 | return -1; | |
242 | } | |
243 | ||
244 | int wxChoice::GetSelection() const | |
245 | { | |
246 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); | |
247 | ||
248 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
249 | int count = 0; | |
250 | GList *child = menu_shell->children; | |
251 | while (child) | |
252 | { | |
253 | GtkBin *bin = GTK_BIN( child->data ); | |
254 | if (!bin->child) return count; | |
255 | child = child->next; | |
256 | count++; | |
257 | } | |
258 | ||
259 | return -1; | |
260 | } | |
261 | ||
262 | void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) ) | |
263 | { | |
264 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
265 | ||
266 | wxFAIL_MSG(wxT("not implemented")); | |
267 | } | |
268 | ||
269 | wxString wxChoice::GetString( int n ) const | |
270 | { | |
271 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") ); | |
272 | ||
273 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
274 | int count = 0; | |
275 | GList *child = menu_shell->children; | |
276 | while (child) | |
277 | { | |
278 | GtkBin *bin = GTK_BIN( child->data ); | |
279 | if (count == n) | |
280 | { | |
281 | GtkLabel *label = (GtkLabel *) NULL; | |
282 | if (bin->child) label = GTK_LABEL(bin->child); | |
283 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); | |
284 | ||
285 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); | |
286 | ||
287 | return wxString(label->label,*wxConvCurrent); | |
288 | } | |
289 | child = child->next; | |
290 | count++; | |
291 | } | |
292 | ||
293 | wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") ); | |
294 | ||
295 | return wxT(""); | |
296 | } | |
297 | ||
298 | int wxChoice::GetCount() const | |
299 | { | |
300 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") ); | |
301 | ||
302 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
303 | int count = 0; | |
304 | GList *child = menu_shell->children; | |
305 | while (child) | |
306 | { | |
307 | count++; | |
308 | child = child->next; | |
309 | } | |
310 | return count; | |
311 | } | |
312 | ||
313 | void wxChoice::SetSelection( int n ) | |
314 | { | |
315 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
316 | ||
317 | int tmp = n; | |
318 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
319 | } | |
320 | ||
321 | void wxChoice::ApplyWidgetStyle() | |
322 | { | |
323 | SetWidgetStyle(); | |
324 | ||
325 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
326 | ||
327 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
328 | gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle ); | |
329 | ||
330 | GList *child = menu_shell->children; | |
331 | while (child) | |
332 | { | |
333 | gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle ); | |
334 | ||
335 | GtkBin *bin = GTK_BIN( child->data ); | |
336 | GtkWidget *label = (GtkWidget *) NULL; | |
337 | if (bin->child) label = bin->child; | |
338 | if (!label) label = GTK_BUTTON(m_widget)->child; | |
339 | ||
340 | gtk_widget_set_style( label, m_widgetStyle ); | |
341 | ||
342 | child = child->next; | |
343 | } | |
344 | } | |
345 | ||
346 | size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item) | |
347 | { | |
348 | GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() ); | |
349 | ||
350 | size_t index; | |
351 | if ( m_strings ) | |
352 | { | |
353 | // sorted control, need to insert at the correct index | |
354 | index = m_strings->Add(item); | |
355 | ||
356 | gtk_menu_insert( GTK_MENU(menu), menu_item, index ); | |
357 | ||
358 | if ( index ) | |
359 | { | |
360 | m_clientList.Insert( m_clientList.Item(index - 1), | |
361 | (wxObject*) NULL ); | |
362 | } | |
363 | else | |
364 | { | |
365 | m_clientList.Insert( (wxObject*) NULL ); | |
366 | } | |
367 | } | |
368 | else | |
369 | { | |
370 | // normal control, just append | |
371 | gtk_menu_append( GTK_MENU(menu), menu_item ); | |
372 | ||
373 | m_clientList.Append( (wxObject*) NULL ); | |
374 | ||
375 | // don't call wxChoice::GetCount() from here because it doesn't work | |
376 | // if we're called from ctor (and GtkMenuShell is still NULL) | |
377 | index = m_clientList.GetCount() - 1; | |
378 | } | |
379 | ||
380 | if (GTK_WIDGET_REALIZED(m_widget)) | |
381 | { | |
382 | gtk_widget_realize( menu_item ); | |
383 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
384 | ||
385 | if (m_widgetStyle) ApplyWidgetStyle(); | |
386 | } | |
387 | ||
388 | gtk_signal_connect( GTK_OBJECT( menu_item ), "activate", | |
389 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
390 | ||
391 | gtk_widget_show( menu_item ); | |
392 | ||
393 | // return the index of the item in the control | |
394 | return index; | |
395 | } | |
396 | ||
397 | wxSize wxChoice::DoGetBestSize() const | |
398 | { | |
399 | return wxSize(80, 26); | |
400 | } | |
401 | ||
402 | #endif |