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