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