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