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