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