fix for size 1x1 wxChoice when SetSelection() is called before realization, bug 1913657
[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
16 #ifndef WX_PRECOMP
17 #include "wx/arrstr.h"
18 #endif
19
20 // FIXME: We use GtkOptionMenu which has been deprecated since GTK+ 2.3.0 in
21 // favour of GtkComboBox.
22 // Later use GtkComboBox if GTK+ runtime version is new enough.
23 #include <gtk/gtkversion.h>
24 #if defined(GTK_DISABLE_DEPRECATED) && GTK_CHECK_VERSION(2,3,0)
25 #undef GTK_DISABLE_DEPRECATED
26 #endif
27
28 #include "wx/gtk/private.h"
29
30 //-----------------------------------------------------------------------------
31 // data
32 //-----------------------------------------------------------------------------
33
34 extern bool g_blockEventsOnDrag;
35
36 //-----------------------------------------------------------------------------
37 // "activate"
38 //-----------------------------------------------------------------------------
39
40 extern "C" {
41 static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
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->HandleWindowEvent(event);
66 }
67 }
68
69 //-----------------------------------------------------------------------------
70 // wxChoice
71 //-----------------------------------------------------------------------------
72
73 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControlWithItems)
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 if (!PreCreation( parent, pos, size ) ||
98 !CreateBase( parent, id, pos, size, style, validator, name ))
99 {
100 wxFAIL_MSG( wxT("wxChoice creation failed") );
101 return false;
102 }
103
104 m_widget = gtk_option_menu_new();
105
106 if ( IsSorted() )
107 {
108 // if our m_strings != NULL, Append() will check for it and insert
109 // items in the correct order
110 m_strings = new wxSortedArrayString;
111 }
112
113 // If we have items, GTK will choose the first item by default
114 m_selection_hack = n > 0 ? 0 : wxNOT_FOUND;
115
116 GtkWidget *menu = gtk_menu_new();
117
118 for (unsigned int i = 0; i < (unsigned int)n; i++)
119 {
120 GtkAddHelper(menu, i, choices[i]);
121 }
122
123 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
124
125 m_parent->DoAddChild( this );
126
127 PostCreation(size);
128 SetInitialSize(size); // need this too because this is a wxControlWithItems
129
130 // workaround for bug in gtk_option_menu_set_history(), it causes
131 // gtk_widget_size_allocate() to be called with the current
132 // widget->allocation values, which will be zero if a proper
133 // size_allocate has not occured yet
134 m_widget->allocation.width = m_width;
135 m_widget->allocation.height = m_height;
136
137 return true;
138 }
139
140 wxChoice::~wxChoice()
141 {
142 Clear();
143
144 delete m_strings;
145 }
146
147 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
148 unsigned int pos,
149 void **clientData, wxClientDataType type)
150 {
151 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
152
153 const unsigned int count = items.GetCount();
154
155 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
156
157 for ( unsigned int i = 0; i < count; ++i, ++pos )
158 {
159 int n = GtkAddHelper(menu, pos, items[i]);
160 if ( n == wxNOT_FOUND )
161 return n;
162
163 AssignNewItemClientData(n, clientData, i, type);
164 }
165
166 // if the item to insert is at or before the selection, and the selection is valid
167 if (((int)pos <= m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
168 {
169 // move the selection forward
170 m_selection_hack += count;
171 }
172
173 // We must set the selection so that it can be read back even if
174 // the user has not modified it since GTK+ will then select the
175 // first item so well return 0.
176 if ((count > 0) && (m_selection_hack==wxNOT_FOUND))
177 m_selection_hack = 0;
178
179 return pos - 1;
180 }
181
182 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
183 {
184 m_clientData[n] = clientData;
185 }
186
187 void* wxChoice::DoGetItemClientData(unsigned int n) const
188 {
189 return m_clientData[n];
190 }
191
192 void wxChoice::DoClear()
193 {
194 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
195
196 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
197 GtkWidget *menu = gtk_menu_new();
198 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
199
200 m_clientData.Clear();
201
202 if ( m_strings )
203 m_strings->Clear();
204
205 // begin with no selection
206 m_selection_hack = wxNOT_FOUND;
207 }
208
209 void wxChoice::DoDeleteOneItem(unsigned int n)
210 {
211 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
212 wxCHECK_RET( IsValid(n), _T("invalid index in wxChoice::Delete") );
213
214 // if the item to delete is before the selection, and the selection is valid
215 if (((int)n < m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
216 {
217 // move the selection back one
218 m_selection_hack--;
219 }
220 else if ((int)n == m_selection_hack)
221 {
222 // invalidate the selection
223 m_selection_hack = wxNOT_FOUND;
224 }
225
226 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
227 // in 2.0), hence this dumb implementation -- still better than nothing
228 const unsigned int count = GetCount();
229
230 wxArrayString items;
231 wxArrayPtrVoid itemsData;
232 items.Alloc(count - 1);
233 itemsData.Alloc(count - 1);
234 for ( unsigned i = 0; i < count; i++ )
235 {
236 if ( i != n )
237 {
238 items.Add(GetString(i));
239 itemsData.Add(m_clientData[i]);
240 }
241 }
242
243 wxChoice::DoClear();
244
245 if ( count > 1 )
246 {
247 void ** const data = &itemsData[0];
248 if ( HasClientObjectData() )
249 Append(items, wx_reinterpret_cast(wxClientData **, data));
250 else
251 Append(items, data);
252 }
253 //else: the control is now empty, nothing to append
254 }
255
256 int wxChoice::FindString( const wxString &string, bool bCase ) const
257 {
258 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") );
259
260 // If you read this code once and you think you understand
261 // it, then you are very wrong. Robert Roebling.
262
263 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
264 int count = 0;
265 GList *child = menu_shell->children;
266 while (child)
267 {
268 GtkBin *bin = GTK_BIN( child->data );
269 GtkLabel *label = (GtkLabel *) NULL;
270 if (bin->child)
271 label = GTK_LABEL(bin->child);
272 if (!label)
273 label = GTK_LABEL(GTK_BIN(m_widget)->child);
274
275 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
276
277 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
278 if (string.IsSameAs( tmp, bCase ))
279 return count;
280
281 child = child->next;
282 count++;
283 }
284
285 return wxNOT_FOUND;
286 }
287
288 int wxChoice::GetSelection() const
289 {
290 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") );
291
292 return m_selection_hack;
293
294 }
295
296 void wxChoice::SetString(unsigned int n, const wxString& str)
297 {
298 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
299
300 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
301 unsigned int count = 0;
302 GList *child = menu_shell->children;
303 while (child)
304 {
305 GtkBin *bin = GTK_BIN( child->data );
306 if (count == n)
307 {
308 GtkLabel *label = (GtkLabel *) NULL;
309 if (bin->child)
310 label = GTK_LABEL(bin->child);
311 if (!label)
312 label = GTK_LABEL(GTK_BIN(m_widget)->child);
313
314 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
315
316 gtk_label_set_text( label, wxGTK_CONV( str ) );
317
318 InvalidateBestSize();
319
320 return;
321 }
322 child = child->next;
323 count++;
324 }
325 }
326
327 wxString wxChoice::GetString(unsigned int n) const
328 {
329 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid choice") );
330
331 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
332 unsigned int count = 0;
333 GList *child = menu_shell->children;
334 while (child)
335 {
336 GtkBin *bin = GTK_BIN( child->data );
337 if (count == n)
338 {
339 GtkLabel *label = (GtkLabel *) NULL;
340 if (bin->child)
341 label = GTK_LABEL(bin->child);
342 if (!label)
343 label = GTK_LABEL(GTK_BIN(m_widget)->child);
344
345 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
346
347 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
348 }
349 child = child->next;
350 count++;
351 }
352
353 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
354
355 return wxEmptyString;
356 }
357
358 unsigned int wxChoice::GetCount() const
359 {
360 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
361
362 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
363 unsigned int count = 0;
364 GList *child = menu_shell->children;
365 while (child)
366 {
367 count++;
368 child = child->next;
369 }
370 return count;
371 }
372
373 void wxChoice::SetSelection( int n )
374 {
375 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
376
377 int tmp = n;
378 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
379
380 // set the local selection variable manually
381 if ((n >= 0) && ((unsigned int)n < GetCount()))
382 {
383 // a valid selection has been made
384 m_selection_hack = n;
385 }
386 else if ((n == wxNOT_FOUND) || (GetCount() == 0))
387 {
388 // invalidates the selection if there are no items
389 // or if it is specifically set to wxNOT_FOUND
390 m_selection_hack = wxNOT_FOUND;
391 }
392 else
393 {
394 // this selects the first item by default if the selection is out of bounds
395 m_selection_hack = 0;
396 }
397 }
398
399 void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
400 {
401 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
402
403 gtk_widget_modify_style( m_widget, style );
404 gtk_widget_modify_style( GTK_WIDGET( menu_shell ), style );
405
406 GList *child = menu_shell->children;
407 while (child)
408 {
409 gtk_widget_modify_style( GTK_WIDGET( child->data ), style );
410
411 GtkBin *bin = GTK_BIN( child->data );
412 GtkWidget *label = (GtkWidget *) NULL;
413 if (bin->child)
414 label = bin->child;
415 if (!label)
416 label = GTK_BIN(m_widget)->child;
417
418 gtk_widget_modify_style( label, style );
419
420 child = child->next;
421 }
422 }
423
424 int wxChoice::GtkAddHelper(GtkWidget *menu, unsigned int pos, const wxString& item)
425 {
426 wxCHECK_MSG(pos<=m_clientData.GetCount(), -1, wxT("invalid index"));
427
428 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
429
430 if ( m_strings )
431 {
432 // sorted control, need to insert at the correct index
433 pos = m_strings->Add(item);
434 }
435
436 // don't call wxChoice::GetCount() from here because it doesn't work
437 // if we're called from ctor (and GtkMenuShell is still NULL)
438 if (pos == m_clientData.GetCount())
439 gtk_menu_shell_append( GTK_MENU_SHELL(menu), menu_item );
440 else
441 gtk_menu_shell_insert( GTK_MENU_SHELL(menu), menu_item, pos );
442
443 m_clientData.Insert( NULL, pos );
444
445 if (GTK_WIDGET_REALIZED(m_widget))
446 {
447 gtk_widget_realize( menu_item );
448 gtk_widget_realize( GTK_BIN(menu_item)->child );
449
450 ApplyWidgetStyle();
451 }
452
453 // The best size of a wxChoice should probably
454 // be changed everytime the control has been
455 // changed, but at least after adding an item
456 // it has to change. Adapted from Matt Ownby.
457 InvalidateBestSize();
458
459 g_signal_connect_after (menu_item, "activate",
460 G_CALLBACK (gtk_choice_clicked_callback),
461 this);
462
463 gtk_widget_show( menu_item );
464
465 // return the index of the item in the control
466 return pos;
467 }
468
469 wxSize wxChoice::DoGetBestSize() const
470 {
471 wxSize ret( wxControl::DoGetBestSize() );
472
473 // we know better our horizontal extent: it depends on the longest string
474 // we have
475 ret.x = 0;
476 if ( m_widget )
477 {
478 int width;
479 unsigned int count = GetCount();
480 for ( unsigned int n = 0; n < count; n++ )
481 {
482 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
483 if ( width > ret.x )
484 ret.x = width;
485 }
486
487 // add extra for the choice "=" button
488
489 // VZ: I don't know how to get the right value, it seems to be in
490 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
491 // to it - maybe we can use gtk_option_menu_size_request() for this
492 // somehow?
493 //
494 // This default value works only for the default GTK+ theme (i.e.
495 // no theme at all) (FIXME)
496 static const int widthChoiceIndicator = 35;
497 ret.x += widthChoiceIndicator;
498 }
499
500 // but not less than the minimal width
501 if ( ret.x < 80 )
502 ret.x = 80;
503
504 // If this request_size is called with no entries then
505 // the returned height is wrong. Give it a reasonable
506 // default value.
507 if (ret.y <= 18)
508 ret.y = 8 + GetCharHeight();
509
510 CacheBestSize(ret);
511 return ret;
512 }
513
514 GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
515 {
516 return GTK_BUTTON(m_widget)->event_window;
517 }
518
519 // static
520 wxVisualAttributes
521 wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
522 {
523 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new);
524 }
525
526
527 #endif // wxUSE_CHOICE