1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/combobox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/combobox.h"
19 #include "wx/settings.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
21 #include "wx/arrstr.h"
24 #include "wx/gtk1/private.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag;
38 static int g_SelectionBeforePopup = wxID_NONE; // this means the popup is hidden
40 //-----------------------------------------------------------------------------
41 // "changed" - typing and list item matches get changed, select-child
42 // if it doesn't match an item then just get a single changed
43 //-----------------------------------------------------------------------------
47 gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
49 if (g_isIdle) wxapp_install_idle_handler();
51 if (combo->m_ignoreNextUpdate)
53 combo->m_ignoreNextUpdate = false;
57 if (!combo->m_hasVMT) return;
59 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
60 event.SetString( combo->GetValue() );
61 event.SetEventObject( combo );
62 combo->GetEventHandler()->ProcessEvent( event );
68 gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo))
75 gtk_popup_hide_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
77 // when the popup is hidden, throw a SELECTED event only if the combobox
79 const int curSelection = combo->GetCurrentSelection();
81 const bool hasChanged = curSelection != g_SelectionBeforePopup;
83 // reset the selection flag to value meaning that it is hidden and do it
84 // now, before generating the events, so that GetSelection() returns the
85 // new value from the event handler
86 g_SelectionBeforePopup = wxID_NONE;
90 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
91 event.SetInt( curSelection );
92 event.SetString( combo->GetStringSelection() );
93 event.SetEventObject( combo );
94 combo->GetEventHandler()->ProcessEvent( event );
96 // for consistency with the other ports, send TEXT event
97 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
98 event2.SetString( combo->GetStringSelection() );
99 event2.SetEventObject( combo );
100 combo->GetEventHandler()->ProcessEvent( event2 );
107 gtk_popup_show_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
109 // store the combobox selection value before the popup is shown
110 g_SelectionBeforePopup = combo->GetCurrentSelection();
114 //-----------------------------------------------------------------------------
115 // "select-child" - click/cursor get select-child, changed, select-child
116 //-----------------------------------------------------------------------------
120 gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo )
122 if (g_isIdle) wxapp_install_idle_handler();
124 if (!combo->m_hasVMT) return;
126 if (g_blockEventsOnDrag) return;
128 int curSelection = combo->GetCurrentSelection();
130 if (combo->m_prevSelection == curSelection) return;
132 GtkWidget *list = GTK_COMBO(combo->m_widget)->list;
133 gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection );
135 combo->m_prevSelection = curSelection;
137 // Quickly set the value of the combo box
138 // as GTK+ does that only AFTER the event
140 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry),
141 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo );
142 combo->SetValue( combo->GetStringSelection() );
143 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry), "changed",
144 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo );
146 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
147 // because when combobox popup is shown, gtk_combo_select_child_callback is
148 // called each times the mouse is over an item with a pressed button so a lot
149 // of SELECTED event could be generated if the user keep the mouse button down
150 // and select other items ...
151 if (g_SelectionBeforePopup == wxID_NONE)
153 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
154 event.SetInt( curSelection );
155 event.SetString( combo->GetStringSelection() );
156 event.SetEventObject( combo );
157 combo->GetEventHandler()->ProcessEvent( event );
159 // for consistency with the other ports, don't generate text update
160 // events while the user is browsing the combobox neither
161 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
162 event2.SetString( combo->GetValue() );
163 event2.SetEventObject( combo );
164 combo->GetEventHandler()->ProcessEvent( event2 );
169 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
173 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
175 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
176 EVT_SIZE(wxComboBox::OnSize)
177 EVT_CHAR(wxComboBox::OnChar)
179 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
180 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
181 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
182 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
183 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
184 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
185 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
187 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
188 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
189 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
190 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
191 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
192 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
193 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
196 bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
197 const wxString& value,
198 const wxPoint& pos, const wxSize& size,
199 const wxArrayString& choices,
200 long style, const wxValidator& validator,
201 const wxString& name )
203 wxCArrayString chs(choices);
205 return Create( parent, id, value, pos, size, chs.GetCount(),
206 chs.GetStrings(), style, validator, name );
209 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
210 const wxPoint& pos, const wxSize& size,
211 int n, const wxString choices[],
212 long style, const wxValidator& validator,
213 const wxString& name )
215 m_ignoreNextUpdate = false;
217 m_acceptsFocus = true;
220 if (!PreCreation( parent, pos, size ) ||
221 !CreateBase( parent, id, pos, size, style, validator, name ))
223 wxFAIL_MSG( wxT("wxComboBox creation failed") );
227 m_widget = gtk_combo_new();
228 GtkCombo *combo = GTK_COMBO(m_widget);
230 // Disable GTK's broken events ...
231 gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id );
232 // ... and add surrogate handler.
233 combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed",
234 (GtkSignalFunc) gtk_dummy_callback, combo);
236 // make it more useable
237 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
239 // and case-sensitive
240 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
242 GtkWidget *list = GTK_COMBO(m_widget)->list;
244 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
246 for (int i = 0; i < n; i++)
248 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) );
250 m_clientDataList.Append( (wxObject*)NULL );
251 m_clientObjectList.Append( (wxObject*)NULL );
253 gtk_container_add( GTK_CONTAINER(list), list_item );
255 gtk_widget_show( list_item );
258 m_parent->DoAddChild( this );
260 m_focusWidget = combo->entry;
264 ConnectWidget( combo->button );
266 // MSW's combo box shows the value and the selection is -1
267 gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) );
268 gtk_list_unselect_all( GTK_LIST(combo->list) );
270 if (style & wxCB_READONLY)
271 gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE );
273 // "show" and "hide" events are generated when user click on the combobox button which popups a list
274 // this list is the "popwin" gtk widget
275 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "hide",
276 GTK_SIGNAL_FUNC(gtk_popup_hide_callback), (gpointer)this );
277 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo)->popwin), "show",
278 GTK_SIGNAL_FUNC(gtk_popup_show_callback), (gpointer)this );
280 gtk_signal_connect_after( GTK_OBJECT(combo->entry), "changed",
281 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
283 gtk_signal_connect_after( GTK_OBJECT(combo->list), "select-child",
284 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
286 SetInitialSize(size); // need this too because this is a wxControlWithItems
288 // This is required for tool bar support
289 // wxSize setsize = GetSize();
290 // gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
295 wxComboBox::~wxComboBox()
297 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
300 wxClientData *cd = (wxClientData*)node->GetData();
302 node = node->GetNext();
304 m_clientObjectList.Clear();
306 m_clientDataList.Clear();
309 void wxComboBox::SetFocus()
313 // don't do anything if we already have focus
317 gtk_widget_grab_focus( m_focusWidget );
320 int wxComboBox::DoAppend( const wxString &item )
322 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
326 GtkWidget *list = GTK_COMBO(m_widget)->list;
328 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
330 gtk_container_add( GTK_CONTAINER(list), list_item );
332 if (GTK_WIDGET_REALIZED(m_widget))
334 gtk_widget_realize( list_item );
335 gtk_widget_realize( GTK_BIN(list_item)->child );
338 // Apply current widget style to the new list_item
339 GtkRcStyle *style = CreateWidgetStyle();
342 gtk_widget_modify_style( GTK_WIDGET( list_item ), style );
343 GtkBin *bin = GTK_BIN( list_item );
344 GtkWidget *label = GTK_WIDGET( bin->child );
345 gtk_widget_modify_style( label, style );
346 gtk_rc_style_unref( style );
349 gtk_widget_show( list_item );
351 const unsigned int count = GetCount();
353 if ( m_clientDataList.GetCount() < count )
354 m_clientDataList.Append( (wxObject*) NULL );
355 if ( m_clientObjectList.GetCount() < count )
356 m_clientObjectList.Append( (wxObject*) NULL );
360 InvalidateBestSize();
365 int wxComboBox::DoInsert( const wxString &item, unsigned int pos )
367 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1,
368 wxT("can't insert into sorted list"));
370 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
372 wxCHECK_MSG( IsValidInsert(pos), -1, wxT("invalid index") );
374 if (pos == GetCount())
379 GtkWidget *list = GTK_COMBO(m_widget)->list;
381 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) );
383 GList *gitem_list = g_list_alloc ();
384 gitem_list->data = list_item;
385 gtk_list_insert_items( GTK_LIST (list), gitem_list, pos );
387 if (GTK_WIDGET_REALIZED(m_widget))
389 gtk_widget_realize( list_item );
390 gtk_widget_realize( GTK_BIN(list_item)->child );
395 gtk_widget_show( list_item );
397 const unsigned int count = GetCount();
399 if ( m_clientDataList.GetCount() < count )
400 m_clientDataList.Insert( pos, (wxObject*) NULL );
401 if ( m_clientObjectList.GetCount() < count )
402 m_clientObjectList.Insert( pos, (wxObject*) NULL );
406 InvalidateBestSize();
411 void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
413 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
415 wxList::compatibility_iterator node = m_clientDataList.Item( n );
418 node->SetData( (wxObject*) clientData );
421 void* wxComboBox::DoGetItemClientData(unsigned int n) const
423 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
425 wxList::compatibility_iterator node = m_clientDataList.Item( n );
427 return node ? node->GetData() : NULL;
430 void wxComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
432 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
434 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
437 // wxItemContainer already deletes data for us
439 node->SetData( (wxObject*) clientData );
442 wxClientData* wxComboBox::DoGetItemClientObject(unsigned int n) const
444 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") );
446 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
448 return node ? (wxClientData*) node->GetData() : NULL;
451 void wxComboBox::Clear()
453 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
457 GtkWidget *list = GTK_COMBO(m_widget)->list;
458 gtk_list_clear_items( GTK_LIST(list), 0, (int)GetCount() );
460 wxList::compatibility_iterator node = m_clientObjectList.GetFirst();
463 wxClientData *cd = (wxClientData*)node->GetData();
465 node = node->GetNext();
467 m_clientObjectList.Clear();
469 m_clientDataList.Clear();
473 InvalidateBestSize();
476 void wxComboBox::Delete(unsigned int n)
478 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
480 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
482 GList *child = g_list_nth( listbox->children, n );
486 wxFAIL_MSG(wxT("wrong index"));
492 GList *list = g_list_append( (GList*) NULL, child->data );
493 gtk_list_remove_items( listbox, list );
496 wxList::compatibility_iterator node = m_clientObjectList.Item( n );
499 wxClientData *cd = (wxClientData*)node->GetData();
501 m_clientObjectList.Erase( node );
504 node = m_clientDataList.Item( n );
506 m_clientDataList.Erase( node );
510 InvalidateBestSize();
513 void wxComboBox::SetString(unsigned int n, const wxString &text)
515 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
517 GtkWidget *list = GTK_COMBO(m_widget)->list;
519 GList *child = g_list_nth( GTK_LIST(list)->children, n );
522 GtkBin *bin = GTK_BIN( child->data );
523 GtkLabel *label = GTK_LABEL( bin->child );
524 gtk_label_set_text(label, wxGTK_CONV(text));
528 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
531 InvalidateBestSize();
534 int wxComboBox::FindString( const wxString &item, bool bCase ) const
536 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
538 GtkWidget *list = GTK_COMBO(m_widget)->list;
540 GList *child = GTK_LIST(list)->children;
544 GtkBin *bin = GTK_BIN( child->data );
545 GtkLabel *label = GTK_LABEL( bin->child );
546 wxString str( label->label );
547 if (item.IsSameAs( str , bCase ) )
557 int wxComboBox::GetSelection() const
559 // if the popup is currently opened, use the selection as it had been
560 // before it dropped down
561 return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
562 : g_SelectionBeforePopup;
565 int wxComboBox::GetCurrentSelection() const
567 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
569 GtkWidget *list = GTK_COMBO(m_widget)->list;
571 GList *selection = GTK_LIST(list)->selection;
574 GList *child = GTK_LIST(list)->children;
578 if (child->data == selection->data) return count;
587 wxString wxComboBox::GetString(unsigned int n) const
589 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
591 GtkWidget *list = GTK_COMBO(m_widget)->list;
594 GList *child = g_list_nth( GTK_LIST(list)->children, n );
597 GtkBin *bin = GTK_BIN( child->data );
598 GtkLabel *label = GTK_LABEL( bin->child );
599 str = wxString( label->label );
603 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
609 wxString wxComboBox::GetStringSelection() const
611 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
613 GtkWidget *list = GTK_COMBO(m_widget)->list;
615 GList *selection = GTK_LIST(list)->selection;
618 GtkBin *bin = GTK_BIN( selection->data );
619 GtkLabel *label = GTK_LABEL( bin->child );
620 wxString tmp( label->label );
624 wxFAIL_MSG( wxT("wxComboBox: no selection") );
626 return wxEmptyString;
629 unsigned int wxComboBox::GetCount() const
631 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
633 GtkWidget *list = GTK_COMBO(m_widget)->list;
635 GList *child = GTK_LIST(list)->children;
636 unsigned int count = 0;
637 while (child) { count++; child = child->next; }
641 void wxComboBox::SetSelection( int n )
643 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
647 GtkWidget *list = GTK_COMBO(m_widget)->list;
648 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
649 gtk_list_select_item( GTK_LIST(list), n );
655 wxString wxComboBox::GetValue() const
657 GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
658 wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) );
661 for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++)
664 printf( "%d ", (int) (c) );
672 void wxComboBox::SetValue( const wxString& value )
674 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
676 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
678 if (!value.IsNull()) tmp = value;
679 gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) );
681 InvalidateBestSize();
684 void wxComboBox::Copy()
686 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
688 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
689 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
692 void wxComboBox::Cut()
694 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
696 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
697 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG );
700 void wxComboBox::Paste()
702 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
704 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
705 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG);
708 void wxComboBox::Undo()
713 void wxComboBox::Redo()
718 void wxComboBox::SelectAll()
720 SetSelection(0, GetLastPosition());
723 bool wxComboBox::CanUndo() const
729 bool wxComboBox::CanRedo() const
735 bool wxComboBox::HasSelection() const
738 GetSelection(&from, &to);
742 bool wxComboBox::CanCopy() const
744 // Can copy if there's a selection
745 return HasSelection();
748 bool wxComboBox::CanCut() const
750 return CanCopy() && IsEditable();
753 bool wxComboBox::CanPaste() const
755 // TODO: check for text on the clipboard
756 return IsEditable() ;
759 bool wxComboBox::IsEditable() const
761 return !HasFlag(wxCB_READONLY);
765 void wxComboBox::SetInsertionPoint( long pos )
767 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
769 if ( pos == GetLastPosition() )
772 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
773 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
776 long wxComboBox::GetInsertionPoint() const
778 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry );
781 wxTextPos wxComboBox::GetLastPosition() const
783 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
784 int pos = GTK_ENTRY(entry)->text_length;
788 void wxComboBox::Replace( long from, long to, const wxString& value )
790 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
792 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
793 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
794 if (value.IsNull()) return;
798 wxCharBuffer buffer = wxConvUTF8.cWX2MB( value );
799 gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos );
801 gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.length(), &pos );
805 void wxComboBox::SetSelection( long from, long to )
807 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
808 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
811 void wxComboBox::GetSelection( long* from, long* to ) const
815 GtkEditable *editable = GTK_EDITABLE(GTK_COMBO(m_widget)->entry);
816 *from = (long) editable->selection_start_pos;
817 *to = (long) editable->selection_end_pos;
821 void wxComboBox::SetEditable( bool editable )
823 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
824 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
827 void wxComboBox::OnChar( wxKeyEvent &event )
829 if ( event.GetKeyCode() == WXK_RETURN )
831 // GTK automatically selects an item if its in the list
832 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
833 eventEnter.SetString( GetValue() );
834 eventEnter.SetInt( GetSelection() );
835 eventEnter.SetEventObject( this );
837 if (!GetEventHandler()->ProcessEvent( eventEnter ))
839 // This will invoke the dialog default action, such
840 // as the clicking the default button.
842 wxWindow *top_frame = m_parent;
843 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
844 top_frame = top_frame->GetParent();
846 if (top_frame && GTK_IS_WINDOW(top_frame->m_widget))
848 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
850 if (window->default_widget)
851 gtk_widget_activate (window->default_widget);
855 // Catch GTK event so that GTK doesn't open the drop
856 // down list upon RETURN.
863 void wxComboBox::DisableEvents()
865 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list),
866 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
867 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry),
868 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
871 void wxComboBox::EnableEvents()
873 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child",
874 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this );
875 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
876 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this );
879 void wxComboBox::OnSize( wxSizeEvent &event )
881 // NB: In some situations (e.g. on non-first page of a wizard, if the
882 // size used is default size), GtkCombo widget is resized correctly,
883 // but it's look is not updated, it's rendered as if it was much wider.
884 // No other widgets are affected, so it looks like a bug in GTK+.
885 // Manually requesting resize calculation (as gtk_pizza_set_size does)
887 if (GTK_WIDGET_VISIBLE(m_widget))
888 gtk_widget_queue_resize(m_widget);
893 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
895 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
897 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
898 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
900 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
901 GList *child = list->children;
904 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
906 GtkBin *bin = GTK_BIN(child->data);
907 gtk_widget_modify_style( bin->child, style );
913 GtkWidget* wxComboBox::GetConnectWidget()
915 return GTK_COMBO(m_widget)->entry;
918 bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
920 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
921 (window == GTK_COMBO(m_widget)->button->window ) );
924 wxSize wxComboBox::DoGetBestSize() const
926 wxSize ret( wxControl::DoGetBestSize() );
928 // we know better our horizontal extent: it depends on the longest string
933 unsigned int count = GetCount();
934 for ( unsigned int n = 0; n < count; n++ )
936 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
942 // empty combobox should have some reasonable default size too
952 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
954 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
957 // ----------------------------------------------------------------------------
958 // standard event handling
959 // ----------------------------------------------------------------------------
961 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
966 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
971 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
976 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
981 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
986 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
989 GetSelection(& from, & to);
990 if (from != -1 && to != -1)
994 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
996 SetSelection(-1, -1);
999 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1001 event.Enable( CanCut() );
1004 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1006 event.Enable( CanCopy() );
1009 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1011 event.Enable( CanPaste() );
1014 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1016 event.Enable( CanUndo() );
1019 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1021 event.Enable( CanRedo() );
1024 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1026 event.Enable(HasSelection() && IsEditable()) ;
1029 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1031 event.Enable(GetLastPosition() > 0);