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