]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/combobox.cpp
reverted unrelated changes accidentally committed as part of r49411
[wxWidgets.git] / src / gtk / combobox.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/combobox.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#if wxUSE_COMBOBOX
14
15#include "wx/combobox.h"
16
17#ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/settings.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
21 #include "wx/arrstr.h"
22#endif
23
24// We use GtkCombo which has been deprecated since GTK+ 2.3.0
25// in favour of GtkComboBox for <GTK2.4 runtime
26// We also use GtkList
27#ifdef GTK_DISABLE_DEPRECATED
28#undef GTK_DISABLE_DEPRECATED
29#endif
30#include "wx/gtk/private.h"
31
32//-----------------------------------------------------------------------------
33// data
34//-----------------------------------------------------------------------------
35
36extern bool g_blockEventsOnDrag;
37static int g_SelectionBeforePopup = wxID_NONE; // this means the popup is hidden
38
39//-----------------------------------------------------------------------------
40// "changed" - typing and list item matches get changed, select-child
41// if it doesn't match an item then just get a single changed
42//-----------------------------------------------------------------------------
43
44extern "C" {
45static void
46gtkcombo_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
47{
48 if (combo->m_ignoreNextUpdate)
49 {
50 combo->m_ignoreNextUpdate = false;
51 return;
52 }
53
54 if (!combo->m_hasVMT) return;
55
56 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
57 event.SetString( combo->GetValue() );
58 event.SetEventObject( combo );
59 combo->GetEventHandler()->ProcessEvent( event );
60}
61}
62
63extern "C" {
64static void
65gtkcombo_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo))
66{
67}
68}
69
70extern "C" {
71static void
72gtkcombo_popup_hide_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
73{
74 // when the popup is hidden, throw a SELECTED event only if the combobox
75 // selection changed.
76 const int curSelection = combo->GetCurrentSelection();
77
78 const bool hasChanged = curSelection != g_SelectionBeforePopup;
79
80 // reset the selection flag to value meaning that it is hidden and do it
81 // now, before generating the events, so that GetSelection() returns the
82 // new value from the event handler
83 g_SelectionBeforePopup = wxID_NONE;
84
85 if ( hasChanged )
86 {
87 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
88 event.SetInt( curSelection );
89 event.SetString( combo->GetStringSelection() );
90 event.SetEventObject( combo );
91 combo->GetEventHandler()->ProcessEvent( event );
92
93 // for consistency with the other ports, send TEXT event
94 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
95 event2.SetString( combo->GetStringSelection() );
96 event2.SetEventObject( combo );
97 combo->GetEventHandler()->ProcessEvent( event2 );
98 }
99}
100}
101
102extern "C" {
103static void
104gtkcombo_popup_show_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
105{
106 // store the combobox selection value before the popup is shown
107 g_SelectionBeforePopup = combo->GetCurrentSelection();
108}
109}
110
111//-----------------------------------------------------------------------------
112// "select-child" - click/cursor get select-child, changed, select-child
113//-----------------------------------------------------------------------------
114
115extern "C" {
116static void
117gtkcombo_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo )
118{
119 if (!combo->m_hasVMT) return;
120
121 if (g_blockEventsOnDrag) return;
122
123 int curSelection = combo->GetCurrentSelection();
124
125 if (combo->m_prevSelection == curSelection) return;
126
127 GtkWidget *list = GTK_COMBO(combo->m_widget)->list;
128 gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection );
129
130 combo->m_prevSelection = curSelection;
131
132 // Quickly set the value of the combo box
133 // as GTK+ does that only AFTER the event
134 // is sent.
135 GtkWidget* entry = GTK_COMBO(combo->GetHandle())->entry;
136 g_signal_handlers_block_by_func(
137 entry, (gpointer)gtkcombo_text_changed_callback, combo);
138 combo->SetValue( combo->GetStringSelection() );
139 g_signal_handlers_unblock_by_func(
140 entry, (gpointer)gtkcombo_text_changed_callback, combo);
141
142 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
143 // because when combobox popup is shown, gtkcombo_combo_select_child_callback is
144 // called each times the mouse is over an item with a pressed button so a lot
145 // of SELECTED event could be generated if the user keep the mouse button down
146 // and select other items ...
147 if (g_SelectionBeforePopup == wxID_NONE)
148 {
149 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
150 event.SetInt( curSelection );
151 event.SetString( combo->GetStringSelection() );
152 event.SetEventObject( combo );
153 combo->GetEventHandler()->ProcessEvent( event );
154
155 // for consistency with the other ports, don't generate text update
156 // events while the user is browsing the combobox neither
157 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
158 event2.SetString( combo->GetValue() );
159 event2.SetEventObject( combo );
160 combo->GetEventHandler()->ProcessEvent( event2 );
161 }
162}
163}
164
165#ifdef __WXGTK24__
166extern "C" {
167static void
168gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
169{
170 if (!combo->m_hasVMT) return;
171
172 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
173 event.SetString( combo->GetValue() );
174 event.SetEventObject( combo );
175 combo->GetEventHandler()->ProcessEvent( event );
176}
177}
178
179extern "C" {
180static void
181gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
182{
183 if (!combo->m_hasVMT) return;
184
185 if (combo->GetSelection() == -1)
186 return;
187
188 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
189 event.SetInt( combo->GetSelection() );
190 event.SetString( combo->GetStringSelection() );
191 event.SetEventObject( combo );
192 combo->GetEventHandler()->ProcessEvent( event );
193}
194}
195
196#endif
197
198//-----------------------------------------------------------------------------
199// wxComboBox
200//-----------------------------------------------------------------------------
201
202IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
203
204BEGIN_EVENT_TABLE(wxComboBox, wxControl)
205 EVT_SIZE(wxComboBox::OnSize)
206 EVT_CHAR(wxComboBox::OnChar)
207
208 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
209 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
210 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
211 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
212 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
213 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
214 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
215
216 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
217 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
218 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
219 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
220 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
221 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
222 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
223END_EVENT_TABLE()
224
225bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
226 const wxString& value,
227 const wxPoint& pos, const wxSize& size,
228 const wxArrayString& choices,
229 long style, const wxValidator& validator,
230 const wxString& name )
231{
232 wxCArrayString chs(choices);
233
234 return Create( parent, id, value, pos, size, chs.GetCount(),
235 chs.GetStrings(), style, validator, name );
236}
237
238bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
239 const wxPoint& pos, const wxSize& size,
240 int n, const wxString choices[],
241 long style, const wxValidator& validator,
242 const wxString& name )
243{
244 m_strings = NULL;
245 m_ignoreNextUpdate = false;
246 m_prevSelection = 0;
247
248 if (!PreCreation( parent, pos, size ) ||
249 !CreateBase( parent, id, pos, size, style, validator, name ))
250 {
251 wxFAIL_MSG( wxT("wxComboBox creation failed") );
252 return false;
253 }
254
255 if(HasFlag(wxCB_SORT))
256 m_strings = new wxSortedArrayString();
257
258#ifdef __WXGTK24__
259 if (!gtk_check_version(2,4,0))
260 {
261 m_widget = gtk_combo_box_entry_new_text();
262
263 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget)->child ), TRUE );
264 }
265 else
266#endif
267 {
268 m_widget = gtk_combo_new();
269 GtkCombo* combo = GTK_COMBO(m_widget);
270
271 // Disable GTK's broken events ...
272 g_signal_handler_disconnect (combo->entry, combo->entry_change_id);
273 // ... and add surrogate handler.
274 combo->entry_change_id = g_signal_connect (combo->entry, "changed",
275 G_CALLBACK (gtkcombo_dummy_callback),
276 combo);
277
278 // make it more useable
279 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
280
281 // and case-sensitive
282 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
283
284 if (style & wxNO_BORDER)
285 g_object_set (combo->entry, "has-frame", FALSE, NULL );
286 }
287
288 Append(n, choices);
289
290 m_parent->DoAddChild( this );
291
292 GtkEntry *entry = NULL;
293#ifdef __WXGTK24__
294 if (!gtk_check_version(2,4,0))
295 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
296 else
297#endif
298 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
299
300 m_focusWidget = GTK_WIDGET( entry );
301
302 PostCreation(size);
303
304#ifdef __WXGTK24__
305 if (!gtk_check_version(2,4,0))
306 ConnectWidget( m_widget );
307 else
308#endif
309 ConnectWidget( GTK_COMBO(m_widget)->button );
310
311#ifdef __WXGTK24__
312 if (!gtk_check_version(2,4,0))
313 {
314 gtk_entry_set_text( entry, wxGTK_CONV(value) );
315
316 if (style & wxCB_READONLY)
317 gtk_entry_set_editable( entry, FALSE );
318
319 g_signal_connect_after (entry, "changed",
320 G_CALLBACK (gtkcombobox_text_changed_callback), this);
321
322 g_signal_connect_after (m_widget, "changed",
323 G_CALLBACK (gtkcombobox_changed_callback), this);
324
325 }
326 else
327#endif
328 {
329 GtkCombo *combo = GTK_COMBO(m_widget);
330 // MSW's combo box shows the value and the selection is -1
331 gtk_entry_set_text( entry, wxGTK_CONV(value) );
332 gtk_list_unselect_all( GTK_LIST(combo->list) );
333
334 if (style & wxCB_READONLY)
335 gtk_entry_set_editable( entry, FALSE );
336
337 // "show" and "hide" events are generated when user click on the combobox button which popups a list
338 // this list is the "popwin" gtk widget
339 g_signal_connect (GTK_COMBO(combo)->popwin, "hide",
340 G_CALLBACK (gtkcombo_popup_hide_callback), this);
341 g_signal_connect (GTK_COMBO(combo)->popwin, "show",
342 G_CALLBACK (gtkcombo_popup_show_callback), this);
343 g_signal_connect_after (combo->list, "select-child",
344 G_CALLBACK (gtkcombo_combo_select_child_callback),
345 this);
346 g_signal_connect_after (entry, "changed",
347 G_CALLBACK (gtkcombo_text_changed_callback), this);
348 }
349
350 SetInitialSize(size); // need this too because this is a wxControlWithItems
351
352 return true;
353}
354
355GtkEditable *wxComboBox::GetEditable() const
356{
357#ifdef __WXGTK24__
358 if ( !gtk_check_version(2,4,0) )
359 return GTK_EDITABLE( GTK_BIN(m_widget)->child );
360 else
361#endif
362 return GTK_EDITABLE( GTK_COMBO(m_widget)->entry );
363}
364
365wxComboBox::~wxComboBox()
366{
367 Clear();
368
369 delete m_strings;
370}
371
372void wxComboBox::SetFocus()
373{
374 if ( m_hasFocus )
375 {
376 // don't do anything if we already have focus
377 return;
378 }
379
380 gtk_widget_grab_focus( m_focusWidget );
381}
382
383int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
384 unsigned int pos,
385 void **clientData, wxClientDataType type)
386{
387 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
388
389 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
390 _T("In a sorted combobox data could only be appended"));
391
392 const int count = items.GetCount();
393
394 int n = wxNOT_FOUND;
395
396#ifdef __WXGTK24__
397 if (!gtk_check_version(2,4,0))
398 {
399 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
400 for( int i = 0; i < count; ++i )
401 {
402 n = pos + i;
403 // If sorted, use this wxSortedArrayStrings to determine
404 // the right insertion point
405 if(m_strings)
406 n = m_strings->Add(items[i]);
407
408 gtk_combo_box_insert_text( combobox, n, wxGTK_CONV( items[i] ) );
409
410 m_clientData.Insert( NULL, n );
411 AssignNewItemClientData(n, clientData, i, type);
412 }
413 }
414 else
415#endif
416 {
417 DisableEvents();
418
419 GtkWidget *list = GTK_COMBO(m_widget)->list;
420 for( int i = 0; i < count; ++i )
421 {
422 n = pos + i;
423 // If sorted, use this wxSortedArrayStrings to determine
424 // the right insertion point
425 if(m_strings)
426 n = m_strings->Add(items[i]);
427
428 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( items[i] ) );
429
430 // TODO construct a list with all items and call gtk_list_insert_items once?
431 GList *gitem_list = g_list_alloc ();
432 gitem_list->data = list_item;
433 gtk_list_insert_items( GTK_LIST (list), gitem_list, n );
434
435 m_clientData.Insert( NULL, n );
436 AssignNewItemClientData(n, clientData, i, type);
437
438 if (GTK_WIDGET_REALIZED(m_widget))
439 {
440 gtk_widget_realize( list_item );
441 gtk_widget_realize( GTK_BIN(list_item)->child );
442
443 ApplyWidgetStyle();
444 }
445
446 gtk_widget_show( list_item );
447 }
448
449 EnableEvents();
450 }
451
452 InvalidateBestSize();
453
454 return n;
455}
456
457void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
458{
459 m_clientData[n] = clientData;
460}
461
462void* wxComboBox::DoGetItemClientData(unsigned int n) const
463{
464 return m_clientData[n];
465}
466
467void wxComboBox::DoClear()
468{
469 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
470
471 DisableEvents();
472
473#ifdef __WXGTK24__
474 if (!gtk_check_version(2,4,0))
475 {
476 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
477 const unsigned int count = GetCount();
478 for (unsigned int i = 0; i < count; i++)
479 gtk_combo_box_remove_text( combobox, 0 );
480 }
481 else // GTK+ < 2.4.0
482#endif // __WXGTK24__
483 {
484 GtkWidget *list = GTK_COMBO(m_widget)->list;
485 gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
486 }
487
488 m_clientData.Clear();
489
490 if(m_strings)
491 m_strings->Clear();
492
493 EnableEvents();
494
495 InvalidateBestSize();
496}
497
498void wxComboBox::DoDeleteOneItem(unsigned int n)
499{
500 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
501
502#ifdef __WXGTK24__
503 if (!gtk_check_version(2,4,0))
504 {
505 wxCHECK_RET( IsValid(n), wxT("invalid index") );
506
507 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
508 gtk_combo_box_remove_text( combobox, n );
509 }
510 else
511#endif
512 {
513 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
514
515 GList *child = g_list_nth( listbox->children, n );
516
517 if (!child)
518 {
519 wxFAIL_MSG(wxT("wrong index"));
520 return;
521 }
522
523 DisableEvents();
524
525 GList *list = g_list_append( (GList*) NULL, child->data );
526 gtk_list_remove_items( listbox, list );
527 g_list_free( list );
528
529 EnableEvents();
530 }
531
532 m_clientData.RemoveAt( n );
533 if(m_strings)
534 m_strings->RemoveAt( n );
535
536 InvalidateBestSize();
537}
538
539void wxComboBox::SetString(unsigned int n, const wxString &text)
540{
541 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
542
543#ifdef __WXGTK24__
544 if (!gtk_check_version(2,4,0))
545 {
546 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
547 wxCHECK_RET( IsValid(n), wxT("invalid index") );
548
549 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
550 GtkTreeIter iter;
551 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
552 {
553 GValue value = { 0, };
554 g_value_init( &value, G_TYPE_STRING );
555 g_value_set_string( &value, wxGTK_CONV( text ) );
556 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, 0, &value );
557 g_value_unset( &value );
558 }
559 }
560 else
561#endif
562 {
563 GtkWidget *list = GTK_COMBO(m_widget)->list;
564
565 GList *child = g_list_nth( GTK_LIST(list)->children, n );
566 if (child)
567 {
568 GtkBin *bin = GTK_BIN( child->data );
569 GtkLabel *label = GTK_LABEL( bin->child );
570 gtk_label_set_text(label, wxGTK_CONV(text));
571 }
572 else
573 {
574 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
575 }
576 }
577
578 InvalidateBestSize();
579}
580
581int wxComboBox::FindString( const wxString &item, bool bCase ) const
582{
583 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
584
585#ifdef __WXGTK24__
586 if (!gtk_check_version(2,4,0))
587 {
588 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
589 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
590 GtkTreeIter iter;
591 gtk_tree_model_get_iter_first( model, &iter );
592 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
593 return -1;
594 int count = 0;
595 do
596 {
597 GValue value = { 0, };
598 gtk_tree_model_get_value( model, &iter, 0, &value );
599 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
600 g_value_unset( &value );
601
602 if (item.IsSameAs( str, bCase ) )
603 return count;
604
605 count++;
606
607 } while (gtk_tree_model_iter_next( model, &iter ));
608 }
609 else
610#endif
611 {
612 GtkWidget *list = GTK_COMBO(m_widget)->list;
613
614 GList *child = GTK_LIST(list)->children;
615 int count = 0;
616 while (child)
617 {
618 GtkBin *bin = GTK_BIN( child->data );
619 GtkLabel *label = GTK_LABEL( bin->child );
620 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
621
622 if (item.IsSameAs( str , bCase ) )
623 return count;
624
625 count++;
626 child = child->next;
627 }
628 }
629
630 return wxNOT_FOUND;
631}
632
633int wxComboBox::GetSelection() const
634{
635#ifdef __WXGTK24__
636 if (!gtk_check_version(2,4,0))
637 {
638 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
639 return gtk_combo_box_get_active( combobox );
640 }
641 else
642#endif
643 // if the popup is currently opened, use the selection as it had been
644 // before it dropped down
645 return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
646 : g_SelectionBeforePopup;
647}
648
649int wxComboBox::GetCurrentSelection() const
650{
651 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
652
653#ifdef __WXGTK24__
654 if (!gtk_check_version(2,4,0))
655 {
656 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
657 return gtk_combo_box_get_active( combobox );
658 }
659 else
660#endif
661 {
662 GtkWidget *list = GTK_COMBO(m_widget)->list;
663
664 GList *selection = GTK_LIST(list)->selection;
665 if (selection)
666 {
667 GList *child = GTK_LIST(list)->children;
668 int count = 0;
669 while (child)
670 {
671 if (child->data == selection->data) return count;
672 count++;
673 child = child->next;
674 }
675 }
676 }
677
678 return -1;
679}
680
681wxString wxComboBox::GetString(unsigned int n) const
682{
683 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
684
685 wxString str;
686
687#ifdef __WXGTK24__
688 if (!gtk_check_version(2,4,0))
689 {
690 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
691 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
692 GtkTreeIter iter;
693 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
694 {
695 GValue value = { 0, };
696 gtk_tree_model_get_value( model, &iter, 0, &value );
697 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
698 g_value_unset( &value );
699 return tmp;
700 }
701 }
702 else
703#endif
704 {
705 GtkWidget *list = GTK_COMBO(m_widget)->list;
706
707 GList *child = g_list_nth( GTK_LIST(list)->children, n );
708 if (child)
709 {
710 GtkBin *bin = GTK_BIN( child->data );
711 GtkLabel *label = GTK_LABEL( bin->child );
712 str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
713 }
714 else
715 {
716 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
717 }
718 }
719
720 return str;
721}
722
723unsigned int wxComboBox::GetCount() const
724{
725 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
726
727#ifdef __WXGTK24__
728 if (!gtk_check_version(2,4,0))
729 {
730 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
731 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
732 GtkTreeIter iter;
733 gtk_tree_model_get_iter_first( model, &iter );
734 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
735 return 0;
736 unsigned int ret = 1;
737 while (gtk_tree_model_iter_next( model, &iter ))
738 ret++;
739 return ret;
740 }
741 else
742#endif
743 {
744 GtkWidget *list = GTK_COMBO(m_widget)->list;
745
746 GList *child = GTK_LIST(list)->children;
747 unsigned int count = 0;
748 while (child)
749 {
750 count++;
751 child = child->next;
752 }
753 return count;
754 }
755}
756
757void wxComboBox::SetSelection( int n )
758{
759 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
760
761 DisableEvents();
762
763#ifdef __WXGTK24__
764 if (!gtk_check_version(2,4,0))
765 {
766 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
767 gtk_combo_box_set_active( combobox, n );
768 }
769 else
770#endif
771 {
772 GtkWidget *list = GTK_COMBO(m_widget)->list;
773 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
774 gtk_list_select_item( GTK_LIST(list), n );
775 m_prevSelection = n;
776 }
777
778 EnableEvents();
779}
780
781void wxComboBox::OnChar( wxKeyEvent &event )
782{
783 if ( event.GetKeyCode() == WXK_RETURN )
784 {
785 // GTK automatically selects an item if its in the list
786 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
787 eventEnter.SetString( GetValue() );
788 eventEnter.SetInt( GetSelection() );
789 eventEnter.SetEventObject( this );
790
791 if (!GetEventHandler()->ProcessEvent( eventEnter ))
792 {
793 // This will invoke the dialog default action, such
794 // as the clicking the default button.
795
796 wxWindow *top_frame = m_parent;
797 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
798 top_frame = top_frame->GetParent();
799
800 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
801 {
802 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
803
804 if (window->default_widget)
805 gtk_widget_activate (window->default_widget);
806 }
807 }
808
809 // Catch GTK event so that GTK doesn't open the drop
810 // down list upon RETURN.
811 return;
812 }
813
814 event.Skip();
815}
816
817void wxComboBox::DisableEvents()
818{
819#ifdef __WXGTK24__
820 if (!gtk_check_version(2,4,0))
821 {
822 g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
823 (gpointer)gtkcombobox_text_changed_callback, this);
824
825 g_signal_handlers_block_by_func(m_widget,
826 (gpointer)gtkcombobox_changed_callback, this);
827 }
828 else
829#endif
830 {
831 g_signal_handlers_block_by_func(GTK_COMBO(m_widget)->list,
832 (gpointer) gtkcombo_combo_select_child_callback, this);
833
834 g_signal_handlers_block_by_func(GTK_COMBO(m_widget)->entry,
835 (gpointer) gtkcombo_text_changed_callback, this);
836 }
837}
838
839void wxComboBox::EnableEvents()
840{
841#ifdef __WXGTK24__
842 if (!gtk_check_version(2,4,0))
843 {
844 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child,
845 (gpointer)gtkcombobox_text_changed_callback, this);
846
847 g_signal_handlers_unblock_by_func(m_widget,
848 (gpointer)gtkcombobox_changed_callback, this);
849 }
850 else
851#endif
852 {
853 g_signal_handlers_unblock_by_func(GTK_COMBO(m_widget)->list,
854 (gpointer) gtkcombo_combo_select_child_callback, this);
855
856 g_signal_handlers_unblock_by_func(GTK_COMBO(m_widget)->entry,
857 (gpointer) gtkcombo_text_changed_callback, this);
858 }
859}
860
861void wxComboBox::OnSize( wxSizeEvent &event )
862{
863#ifdef __WXGTK24__
864 if (!gtk_check_version(2,4,0))
865 {
866 // Do nothing
867 }
868 else
869#endif
870 {
871 // NB: In some situations (e.g. on non-first page of a wizard, if the
872 // size used is default size), GtkCombo widget is resized correctly,
873 // but it's look is not updated, it's rendered as if it was much wider.
874 // No other widgets are affected, so it looks like a bug in GTK+.
875 // Manually requesting resize calculation (as gtk_pizza_set_size does)
876 // fixes it.
877 if (GTK_WIDGET_VISIBLE(m_widget))
878 gtk_widget_queue_resize(m_widget);
879 }
880
881 event.Skip();
882}
883
884void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
885{
886#ifdef __WXGTK24__
887 if (!gtk_check_version(2,4,0))
888 {
889 // Do nothing
890 }
891 else
892#endif
893 {
894// gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
895
896 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
897 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
898
899 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
900 GList *child = list->children;
901 while (child)
902 {
903 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
904
905 GtkBin *bin = GTK_BIN(child->data);
906 gtk_widget_modify_style( bin->child, style );
907
908 child = child->next;
909 }
910 }
911}
912
913GtkWidget* wxComboBox::GetConnectWidget()
914{
915 GtkEntry *entry = NULL;
916#ifdef __WXGTK24__
917 if (!gtk_check_version(2,4,0))
918 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
919 else
920#endif
921 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
922
923 return GTK_WIDGET( entry );
924}
925
926GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
927{
928#ifdef __WXGTK24__
929 if (!gtk_check_version(2,4,0))
930 {
931 wxUnusedVar(windows);
932
933 return GTK_ENTRY(GTK_BIN(m_widget)->child)->text_area;
934 }
935 else
936#endif // GTK+ 2.4
937 {
938 windows.push_back(GTK_ENTRY(GTK_COMBO(m_widget)->entry)->text_area);
939 windows.push_back(GTK_COMBO(m_widget)->button->window);
940
941 // indicate that we return multiple windows in the windows array
942 return NULL;
943 }
944}
945
946wxSize wxComboBox::DoGetBestSize() const
947{
948 wxSize ret( wxControl::DoGetBestSize() );
949
950 // we know better our horizontal extent: it depends on the longest string
951 // in the combobox
952 if ( m_widget )
953 {
954 int width;
955 unsigned int count = GetCount();
956 for ( unsigned int n = 0; n < count; n++ )
957 {
958 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
959 if ( width > ret.x )
960 ret.x = width;
961 }
962 }
963
964 // empty combobox should have some reasonable default size too
965 if ( ret.x < 100 )
966 ret.x = 100;
967
968 CacheBestSize(ret);
969 return ret;
970}
971
972// static
973wxVisualAttributes
974wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
975{
976#ifdef __WXGTK24__
977 if (!gtk_check_version(2,4,0))
978 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
979 else
980#endif
981 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
982}
983
984// ----------------------------------------------------------------------------
985// standard event handling
986// ----------------------------------------------------------------------------
987
988void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
989{
990 Cut();
991}
992
993void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
994{
995 Copy();
996}
997
998void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
999{
1000 Paste();
1001}
1002
1003void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
1004{
1005 Undo();
1006}
1007
1008void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
1009{
1010 Redo();
1011}
1012
1013void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
1014{
1015 long from, to;
1016 GetSelection(& from, & to);
1017 if (from != -1 && to != -1)
1018 Remove(from, to);
1019}
1020
1021void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1022{
1023 SetSelection(-1, -1);
1024}
1025
1026void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1027{
1028 event.Enable( CanCut() );
1029}
1030
1031void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1032{
1033 event.Enable( CanCopy() );
1034}
1035
1036void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1037{
1038 event.Enable( CanPaste() );
1039}
1040
1041void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1042{
1043 event.Enable( CanUndo() );
1044}
1045
1046void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1047{
1048 event.Enable( CanRedo() );
1049}
1050
1051void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1052{
1053 event.Enable(HasSelection() && IsEditable()) ;
1054}
1055
1056void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1057{
1058 event.Enable(GetLastPosition() > 0);
1059}
1060
1061#endif // wxUSE_COMBOBOX