]>
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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
12 | #pragma implementation "choice.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/defs.h" | |
16 | ||
17 | #if wxUSE_CHOICE | |
18 | ||
19 | #include "wx/choice.h" | |
20 | #include "wx/arrstr.h" | |
21 | ||
22 | #include "wx/gtk/private.h" | |
23 | ||
24 | //----------------------------------------------------------------------------- | |
25 | // idle system | |
26 | //----------------------------------------------------------------------------- | |
27 | ||
28 | extern void wxapp_install_idle_handler(); | |
29 | extern bool g_isIdle; | |
30 | ||
31 | //----------------------------------------------------------------------------- | |
32 | // data | |
33 | //----------------------------------------------------------------------------- | |
34 | ||
35 | extern bool g_blockEventsOnDrag; | |
36 | ||
37 | //----------------------------------------------------------------------------- | |
38 | // "activate" | |
39 | //----------------------------------------------------------------------------- | |
40 | ||
41 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) | |
42 | { | |
43 | if (g_isIdle) | |
44 | wxapp_install_idle_handler(); | |
45 | ||
46 | if (!choice->m_hasVMT) return; | |
47 | ||
48 | if (g_blockEventsOnDrag) return; | |
49 | ||
50 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); | |
51 | int n = choice->GetSelection(); | |
52 | ||
53 | event.SetInt( n ); | |
54 | event.SetString( choice->GetStringSelection() ); | |
55 | event.SetEventObject(choice); | |
56 | ||
57 | if ( choice->HasClientObjectData() ) | |
58 | event.SetClientObject( choice->GetClientObject(n) ); | |
59 | else if ( choice->HasClientUntypedData() ) | |
60 | event.SetClientData( choice->GetClientData(n) ); | |
61 | ||
62 | choice->GetEventHandler()->ProcessEvent(event); | |
63 | } | |
64 | ||
65 | //----------------------------------------------------------------------------- | |
66 | // wxChoice | |
67 | //----------------------------------------------------------------------------- | |
68 | ||
69 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) | |
70 | ||
71 | wxChoice::wxChoice() | |
72 | { | |
73 | m_strings = (wxSortedArrayString *)NULL; | |
74 | } | |
75 | ||
76 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, | |
77 | const wxPoint &pos, const wxSize &size, | |
78 | int n, const wxString choices[], | |
79 | long style, const wxValidator& validator, const wxString &name ) | |
80 | { | |
81 | m_needParent = TRUE; | |
82 | #if (GTK_MINOR_VERSION > 0) | |
83 | m_acceptsFocus = TRUE; | |
84 | #endif | |
85 | ||
86 | if (!PreCreation( parent, pos, size ) || | |
87 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
88 | { | |
89 | wxFAIL_MSG( wxT("wxChoice creation failed") ); | |
90 | return FALSE; | |
91 | } | |
92 | ||
93 | m_widget = gtk_option_menu_new(); | |
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 | GtkAddHelper(menu, i, 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 | SetFont( parent->GetFont() ); | |
116 | ||
117 | SetBestSize(size); | |
118 | ||
119 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
120 | SetForegroundColour( parent->GetForegroundColour() ); | |
121 | ||
122 | Show( TRUE ); | |
123 | ||
124 | return TRUE; | |
125 | } | |
126 | ||
127 | wxChoice::~wxChoice() | |
128 | { | |
129 | Clear(); | |
130 | ||
131 | delete m_strings; | |
132 | } | |
133 | ||
134 | int wxChoice::DoAppend( const wxString &item ) | |
135 | { | |
136 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); | |
137 | ||
138 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); | |
139 | ||
140 | return GtkAddHelper(menu, GetCount(), item); | |
141 | } | |
142 | ||
143 | int wxChoice::DoInsert( const wxString &item, int pos ) | |
144 | { | |
145 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); | |
146 | wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); | |
147 | ||
148 | if (pos == GetCount()) | |
149 | return DoAppend(item); | |
150 | ||
151 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); | |
152 | ||
153 | return GtkAddHelper(menu, pos, item); | |
154 | } | |
155 | ||
156 | void wxChoice::DoSetItemClientData( int n, void* clientData ) | |
157 | { | |
158 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); | |
159 | ||
160 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
161 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") ); | |
162 | ||
163 | node->SetData( (wxObject*) clientData ); | |
164 | } | |
165 | ||
166 | void* wxChoice::DoGetItemClientData( int n ) const | |
167 | { | |
168 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") ); | |
169 | ||
170 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
171 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") ); | |
172 | ||
173 | return node->GetData(); | |
174 | } | |
175 | ||
176 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) | |
177 | { | |
178 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); | |
179 | ||
180 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
181 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") ); | |
182 | ||
183 | // wxItemContainer already deletes data for us | |
184 | ||
185 | node->SetData( (wxObject*) clientData ); | |
186 | } | |
187 | ||
188 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const | |
189 | { | |
190 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") ); | |
191 | ||
192 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
193 | wxCHECK_MSG( node, (wxClientData *)NULL, | |
194 | wxT("invalid index in wxChoice::DoGetItemClientObject") ); | |
195 | ||
196 | return (wxClientData*) node->GetData(); | |
197 | } | |
198 | ||
199 | void wxChoice::Clear() | |
200 | { | |
201 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
202 | ||
203 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); | |
204 | GtkWidget *menu = gtk_menu_new(); | |
205 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
206 | ||
207 | if ( HasClientObjectData() ) | |
208 | { | |
209 | // destroy the data (due to Robert's idea of using wxList<wxObject> | |
210 | // and not wxList<wxClientData> we can't just say | |
211 | // m_clientList.DeleteContents(TRUE) - this would crash! | |
212 | wxList::compatibility_iterator node = m_clientList.GetFirst(); | |
213 | while ( node ) | |
214 | { | |
215 | delete (wxClientData *)node->GetData(); | |
216 | node = node->GetNext(); | |
217 | } | |
218 | } | |
219 | m_clientList.Clear(); | |
220 | ||
221 | if ( m_strings ) | |
222 | m_strings->Clear(); | |
223 | } | |
224 | ||
225 | void wxChoice::Delete( int n ) | |
226 | { | |
227 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
228 | ||
229 | // VZ: apparently GTK+ doesn't have a built-in function to do it (not even | |
230 | // in 2.0), hence this dumb implementation -- still better than nothing | |
231 | int i, | |
232 | count = GetCount(); | |
233 | ||
234 | wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") ); | |
235 | ||
236 | const bool hasClientData = m_clientDataItemsType != wxClientData_None; | |
237 | const bool hasObjectData = m_clientDataItemsType == wxClientData_Object; | |
238 | ||
239 | wxList::compatibility_iterator node = m_clientList.GetFirst(); | |
240 | ||
241 | wxArrayString items; | |
242 | wxArrayPtrVoid itemsData; | |
243 | items.Alloc(count); | |
244 | for ( i = 0; i < count; i++ ) | |
245 | { | |
246 | if ( i != n ) | |
247 | { | |
248 | items.Add(GetString(i)); | |
249 | if ( hasClientData ) | |
250 | { | |
251 | // also save the client data | |
252 | itemsData.Add(node->GetData()); | |
253 | } | |
254 | } | |
255 | else // need to delete the client object too | |
256 | { | |
257 | if ( hasObjectData ) | |
258 | { | |
259 | delete (wxClientData *)node->GetData(); | |
260 | } | |
261 | } | |
262 | ||
263 | if ( hasClientData ) | |
264 | { | |
265 | node = node->GetNext(); | |
266 | } | |
267 | } | |
268 | ||
269 | if ( hasObjectData ) | |
270 | { | |
271 | // prevent Clear() from destroying all client data | |
272 | m_clientDataItemsType = wxClientData_None; | |
273 | } | |
274 | ||
275 | Clear(); | |
276 | ||
277 | for ( i = 0; i < count - 1; i++ ) | |
278 | { | |
279 | Append(items[i]); | |
280 | ||
281 | if ( hasObjectData ) | |
282 | SetClientObject(i, (wxClientData *)itemsData[i]); | |
283 | else if ( hasClientData ) | |
284 | SetClientData(i, itemsData[i]); | |
285 | } | |
286 | } | |
287 | ||
288 | int wxChoice::FindString( const wxString &string ) const | |
289 | { | |
290 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); | |
291 | ||
292 | // If you read this code once and you think you understand | |
293 | // it, then you are very wrong. Robert Roebling. | |
294 | ||
295 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
296 | int count = 0; | |
297 | GList *child = menu_shell->children; | |
298 | while (child) | |
299 | { | |
300 | GtkBin *bin = GTK_BIN( child->data ); | |
301 | GtkLabel *label = (GtkLabel *) NULL; | |
302 | if (bin->child) | |
303 | label = GTK_LABEL(bin->child); | |
304 | if (!label) | |
305 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
306 | ||
307 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); | |
308 | ||
309 | #ifdef __WXGTK20__ | |
310 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) ); | |
311 | #else | |
312 | wxString tmp( label->label ); | |
313 | #endif | |
314 | if (string == tmp) | |
315 | return count; | |
316 | ||
317 | child = child->next; | |
318 | count++; | |
319 | } | |
320 | ||
321 | return -1; | |
322 | } | |
323 | ||
324 | int wxChoice::GetSelection() const | |
325 | { | |
326 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); | |
327 | ||
328 | #ifdef __WXGTK20__ | |
329 | ||
330 | return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) ); | |
331 | ||
332 | #else | |
333 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
334 | int count = 0; | |
335 | ||
336 | GList *child = menu_shell->children; | |
337 | while (child) | |
338 | { | |
339 | GtkBin *bin = GTK_BIN( child->data ); | |
340 | if (!bin->child) return count; | |
341 | child = child->next; | |
342 | count++; | |
343 | } | |
344 | ||
345 | return -1; | |
346 | #endif | |
347 | } | |
348 | ||
349 | void wxChoice::SetString( int n, const wxString& str ) | |
350 | { | |
351 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
352 | ||
353 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
354 | int count = 0; | |
355 | GList *child = menu_shell->children; | |
356 | while (child) | |
357 | { | |
358 | GtkBin *bin = GTK_BIN( child->data ); | |
359 | if (count == n) | |
360 | { | |
361 | GtkLabel *label = (GtkLabel *) NULL; | |
362 | if (bin->child) | |
363 | label = GTK_LABEL(bin->child); | |
364 | if (!label) | |
365 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
366 | ||
367 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); | |
368 | ||
369 | gtk_label_set_text( label, wxGTK_CONV( str ) ); | |
370 | ||
371 | return; | |
372 | } | |
373 | child = child->next; | |
374 | count++; | |
375 | } | |
376 | } | |
377 | ||
378 | wxString wxChoice::GetString( int n ) const | |
379 | { | |
380 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") ); | |
381 | ||
382 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
383 | int count = 0; | |
384 | GList *child = menu_shell->children; | |
385 | while (child) | |
386 | { | |
387 | GtkBin *bin = GTK_BIN( child->data ); | |
388 | if (count == n) | |
389 | { | |
390 | GtkLabel *label = (GtkLabel *) NULL; | |
391 | if (bin->child) | |
392 | label = GTK_LABEL(bin->child); | |
393 | if (!label) | |
394 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
395 | ||
396 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); | |
397 | ||
398 | #ifdef __WXGTK20__ | |
399 | return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) ); | |
400 | #else | |
401 | return wxString( label->label ); | |
402 | #endif | |
403 | } | |
404 | child = child->next; | |
405 | count++; | |
406 | } | |
407 | ||
408 | wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") ); | |
409 | ||
410 | return wxT(""); | |
411 | } | |
412 | ||
413 | int wxChoice::GetCount() const | |
414 | { | |
415 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") ); | |
416 | ||
417 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
418 | int count = 0; | |
419 | GList *child = menu_shell->children; | |
420 | while (child) | |
421 | { | |
422 | count++; | |
423 | child = child->next; | |
424 | } | |
425 | return count; | |
426 | } | |
427 | ||
428 | void wxChoice::SetSelection( int n ) | |
429 | { | |
430 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
431 | ||
432 | int tmp = n; | |
433 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
434 | } | |
435 | ||
436 | void wxChoice::ApplyWidgetStyle() | |
437 | { | |
438 | SetWidgetStyle(); | |
439 | ||
440 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
441 | ||
442 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
443 | gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle ); | |
444 | ||
445 | GList *child = menu_shell->children; | |
446 | while (child) | |
447 | { | |
448 | gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle ); | |
449 | ||
450 | GtkBin *bin = GTK_BIN( child->data ); | |
451 | GtkWidget *label = (GtkWidget *) NULL; | |
452 | if (bin->child) | |
453 | label = bin->child; | |
454 | if (!label) | |
455 | label = BUTTON_CHILD(m_widget); | |
456 | ||
457 | gtk_widget_set_style( label, m_widgetStyle ); | |
458 | ||
459 | child = child->next; | |
460 | } | |
461 | } | |
462 | ||
463 | int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item) | |
464 | { | |
465 | wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index")); | |
466 | ||
467 | GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) ); | |
468 | ||
469 | size_t index; | |
470 | if ( m_strings ) | |
471 | { | |
472 | // sorted control, need to insert at the correct index | |
473 | index = m_strings->Add(item); | |
474 | ||
475 | gtk_menu_insert( GTK_MENU(menu), menu_item, index ); | |
476 | ||
477 | if ( index ) | |
478 | { | |
479 | m_clientList.Insert( m_clientList.Item(index - 1), | |
480 | (wxObject*) NULL ); | |
481 | } | |
482 | else | |
483 | { | |
484 | m_clientList.Insert( (wxObject*) NULL ); | |
485 | } | |
486 | } | |
487 | else | |
488 | { | |
489 | // don't call wxChoice::GetCount() from here because it doesn't work | |
490 | // if we're called from ctor (and GtkMenuShell is still NULL) | |
491 | ||
492 | // normal control, just append | |
493 | if (pos == (int)m_clientList.GetCount()) | |
494 | { | |
495 | gtk_menu_append( GTK_MENU(menu), menu_item ); | |
496 | m_clientList.Append( (wxObject*) NULL ); | |
497 | index = m_clientList.GetCount() - 1; | |
498 | } | |
499 | else | |
500 | { | |
501 | gtk_menu_insert( GTK_MENU(menu), menu_item, pos ); | |
502 | m_clientList.Insert( pos, (wxObject*) NULL ); | |
503 | index = pos; | |
504 | } | |
505 | } | |
506 | ||
507 | if (GTK_WIDGET_REALIZED(m_widget)) | |
508 | { | |
509 | gtk_widget_realize( menu_item ); | |
510 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
511 | ||
512 | if (m_widgetStyle) ApplyWidgetStyle(); | |
513 | } | |
514 | ||
515 | gtk_signal_connect( GTK_OBJECT( menu_item ), "activate", | |
516 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
517 | ||
518 | gtk_widget_show( menu_item ); | |
519 | ||
520 | // return the index of the item in the control | |
521 | return index; | |
522 | } | |
523 | ||
524 | wxSize wxChoice::DoGetBestSize() const | |
525 | { | |
526 | wxSize ret( wxControl::DoGetBestSize() ); | |
527 | ||
528 | // we know better our horizontal extent: it depends on the longest string | |
529 | // we have | |
530 | ret.x = 0; | |
531 | if ( m_widget ) | |
532 | { | |
533 | int width; | |
534 | size_t count = GetCount(); | |
535 | for ( size_t n = 0; n < count; n++ ) | |
536 | { | |
537 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font ); | |
538 | if ( width > ret.x ) | |
539 | ret.x = width; | |
540 | } | |
541 | ||
542 | // add extra for the choice "=" button | |
543 | ||
544 | // VZ: I don't know how to get the right value, it seems to be in | |
545 | // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get | |
546 | // to it - maybe we can use gtk_option_menu_size_request() for this | |
547 | // somehow? | |
548 | // | |
549 | // This default value works only for the default GTK+ theme (i.e. | |
550 | // no theme at all) (FIXME) | |
551 | static const int widthChoiceIndicator = 35; | |
552 | ret.x += widthChoiceIndicator; | |
553 | } | |
554 | ||
555 | // but not less than the minimal width | |
556 | if ( ret.x < 80 ) | |
557 | ret.x = 80; | |
558 | ||
559 | ret.y = 16 + GetCharHeight(); | |
560 | ||
561 | return ret; | |
562 | } | |
563 | ||
564 | bool wxChoice::IsOwnGtkWindow( GdkWindow *window ) | |
565 | { | |
566 | #ifdef __WXGTK20__ | |
567 | return GTK_BUTTON(m_widget)->event_window; | |
568 | #else | |
569 | return (window == m_widget->window); | |
570 | #endif | |
571 | } | |
572 | ||
573 | ||
574 | #endif // wxUSE_CHOICE | |
575 |