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