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_TEXT
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_TEXT
, combo
->GetId() );
60 event
.SetString( combo
->GetValue() );
61 event
.SetEventObject( combo
);
62 combo
->HandleWindowEvent( 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_COMBOBOX
, combo
->GetId() );
91 event
.SetInt( curSelection
);
92 event
.SetString( combo
->GetStringSelection() );
93 event
.SetEventObject( combo
);
94 combo
->HandleWindowEvent( event
);
96 // for consistency with the other ports, send TEXT event
97 wxCommandEvent
event2( wxEVT_TEXT
, combo
->GetId() );
98 event2
.SetString( combo
->GetStringSelection() );
99 event2
.SetEventObject( combo
);
100 combo
->HandleWindowEvent( 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_COMBOBOX
, combo
->GetId() );
154 event
.SetInt( curSelection
);
155 event
.SetString( combo
->GetStringSelection() );
156 event
.SetEventObject( combo
);
157 combo
->HandleWindowEvent( 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_TEXT
, combo
->GetId() );
162 event2
.SetString( combo
->GetValue() );
163 event2
.SetEventObject( combo
);
164 combo
->HandleWindowEvent( event2
);
169 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
173 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
174 EVT_SIZE(wxComboBox::OnSize
)
175 EVT_CHAR(wxComboBox::OnChar
)
177 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
178 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
179 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
180 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
181 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
182 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
183 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
185 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
186 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
187 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
188 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
189 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
190 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
191 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
194 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
195 const wxString
& value
,
196 const wxPoint
& pos
, const wxSize
& size
,
197 const wxArrayString
& choices
,
198 long style
, const wxValidator
& validator
,
199 const wxString
& name
)
201 wxCArrayString
chs(choices
);
203 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
204 chs
.GetStrings(), style
, validator
, name
);
207 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
208 const wxPoint
& pos
, const wxSize
& size
,
209 int n
, const wxString choices
[],
210 long style
, const wxValidator
& validator
,
211 const wxString
& name
)
213 m_ignoreNextUpdate
= false;
215 m_acceptsFocus
= true;
218 if (!PreCreation( parent
, pos
, size
) ||
219 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
221 wxFAIL_MSG( wxT("wxComboBox creation failed") );
225 m_widget
= gtk_combo_new();
226 GtkCombo
*combo
= GTK_COMBO(m_widget
);
228 // Disable GTK's broken events ...
229 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
230 // ... and add surrogate handler.
231 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
232 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
234 // make it more useable
235 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
237 // and case-sensitive
238 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
240 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
242 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
244 for (int i
= 0; i
< n
; i
++)
246 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
248 m_clientDataList
.Append( NULL
);
249 m_clientObjectList
.Append( NULL
);
251 gtk_container_add( GTK_CONTAINER(list
), list_item
);
253 gtk_widget_show( list_item
);
256 m_parent
->DoAddChild( this );
258 m_focusWidget
= combo
->entry
;
262 ConnectWidget( combo
->button
);
264 // MSW's combo box shows the value and the selection is -1
265 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
266 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
268 if (style
& wxCB_READONLY
)
269 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
271 // "show" and "hide" events are generated when user click on the combobox button which popups a list
272 // this list is the "popwin" gtk widget
273 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "hide",
274 GTK_SIGNAL_FUNC(gtk_popup_hide_callback
), (gpointer
)this );
275 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "show",
276 GTK_SIGNAL_FUNC(gtk_popup_show_callback
), (gpointer
)this );
278 gtk_signal_connect_after( GTK_OBJECT(combo
->entry
), "changed",
279 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
281 gtk_signal_connect_after( GTK_OBJECT(combo
->list
), "select-child",
282 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
284 SetInitialSize(size
); // need this too because this is a wxControlWithItems
286 // This is required for tool bar support
287 // wxSize setsize = GetSize();
288 // gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
293 wxComboBox::~wxComboBox()
295 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
298 wxClientData
*cd
= (wxClientData
*)node
->GetData();
300 node
= node
->GetNext();
302 m_clientObjectList
.Clear();
304 m_clientDataList
.Clear();
307 void wxComboBox::SetFocus()
311 // don't do anything if we already have focus
315 gtk_widget_grab_focus( m_focusWidget
);
318 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
321 wxClientDataType type
)
323 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
327 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
329 GtkRcStyle
*style
= CreateWidgetStyle();
331 const unsigned int count
= items
.GetCount();
332 for( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
335 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( items
[i
] ) );
337 if ( pos
== GetCount() )
339 gtk_container_add( GTK_CONTAINER(list
), list_item
);
341 else // insert, not append
343 GList
*gitem_list
= g_list_alloc ();
344 gitem_list
->data
= list_item
;
345 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
348 if (GTK_WIDGET_REALIZED(m_widget
))
350 gtk_widget_realize( list_item
);
351 gtk_widget_realize( GTK_BIN(list_item
)->child
);
355 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
356 GtkBin
*bin
= GTK_BIN( list_item
);
357 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
358 gtk_widget_modify_style( label
, style
);
363 gtk_widget_show( list_item
);
365 if ( m_clientDataList
.GetCount() < GetCount() )
366 m_clientDataList
.Insert( pos
, NULL
);
367 if ( m_clientObjectList
.GetCount() < GetCount() )
368 m_clientObjectList
.Insert( pos
, NULL
);
370 AssignNewItemClientData(pos
, clientData
, i
, type
);
374 gtk_rc_style_unref( style
);
378 InvalidateBestSize();
383 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
385 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
387 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
390 node
->SetData( (wxObject
*) clientData
);
393 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
395 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
397 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
399 return node
? node
->GetData() : NULL
;
402 void wxComboBox::DoClear()
404 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
408 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
409 gtk_list_clear_items( GTK_LIST(list
), 0, (int)GetCount() );
411 m_clientObjectList
.Clear();
413 m_clientDataList
.Clear();
417 InvalidateBestSize();
420 void wxComboBox::DoDeleteOneItem(unsigned int n
)
422 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
424 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
426 GList
*child
= g_list_nth( listbox
->children
, n
);
430 wxFAIL_MSG(wxT("wrong index"));
436 GList
*list
= g_list_append( NULL
, child
->data
);
437 gtk_list_remove_items( listbox
, list
);
440 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
443 m_clientObjectList
.Erase( node
);
446 node
= m_clientDataList
.Item( n
);
448 m_clientDataList
.Erase( node
);
452 InvalidateBestSize();
455 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
457 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
459 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
461 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
464 GtkBin
*bin
= GTK_BIN( child
->data
);
465 GtkLabel
*label
= GTK_LABEL( bin
->child
);
466 gtk_label_set_text(label
, wxGTK_CONV(text
));
470 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
473 InvalidateBestSize();
476 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
478 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
480 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
482 GList
*child
= GTK_LIST(list
)->children
;
486 GtkBin
*bin
= GTK_BIN( child
->data
);
487 GtkLabel
*label
= GTK_LABEL( bin
->child
);
488 wxString
str( label
->label
);
489 if (item
.IsSameAs( str
, bCase
) )
499 int wxComboBox::GetSelection() const
501 // if the popup is currently opened, use the selection as it had been
502 // before it dropped down
503 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
504 : g_SelectionBeforePopup
;
507 int wxComboBox::GetCurrentSelection() const
509 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
511 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
513 GList
*selection
= GTK_LIST(list
)->selection
;
516 GList
*child
= GTK_LIST(list
)->children
;
520 if (child
->data
== selection
->data
) return count
;
529 wxString
wxComboBox::GetString(unsigned int n
) const
531 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
533 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
536 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
539 GtkBin
*bin
= GTK_BIN( child
->data
);
540 GtkLabel
*label
= GTK_LABEL( bin
->child
);
541 str
= wxString( label
->label
);
545 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
551 wxString
wxComboBox::GetStringSelection() const
553 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
555 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
557 GList
*selection
= GTK_LIST(list
)->selection
;
560 GtkBin
*bin
= GTK_BIN( selection
->data
);
561 GtkLabel
*label
= GTK_LABEL( bin
->child
);
562 wxString
tmp( label
->label
);
566 wxFAIL_MSG( wxT("wxComboBox: no selection") );
568 return wxEmptyString
;
571 unsigned int wxComboBox::GetCount() const
573 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
575 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
577 GList
*child
= GTK_LIST(list
)->children
;
578 unsigned int count
= 0;
579 while (child
) { count
++; child
= child
->next
; }
583 void wxComboBox::SetSelection( int n
)
585 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
589 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
590 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
591 gtk_list_select_item( GTK_LIST(list
), n
);
597 wxString
wxComboBox::DoGetValue() const
599 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
600 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
603 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
606 printf( "%d ", (int) (c
) );
614 void wxComboBox::SetValue( const wxString
& value
)
616 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
618 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
619 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( value
) );
621 InvalidateBestSize();
624 void wxComboBox::WriteText(const wxString
& value
)
626 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
628 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
629 GtkEditable
* const edit
= GTK_EDITABLE(entry
);
631 gtk_editable_delete_selection(edit
);
632 gint len
= gtk_editable_get_position(edit
);
633 gtk_editable_insert_text(edit
, wxGTK_CONV(value
), -1, &len
);
634 gtk_editable_set_position(edit
, len
);
637 void wxComboBox::Copy()
639 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
641 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
642 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
645 void wxComboBox::Cut()
647 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
649 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
650 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
653 void wxComboBox::Paste()
655 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
657 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
658 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
661 void wxComboBox::Undo()
666 void wxComboBox::Redo()
671 void wxComboBox::SelectAll()
673 SetSelection(0, GetLastPosition());
676 bool wxComboBox::CanUndo() const
682 bool wxComboBox::CanRedo() const
688 bool wxComboBox::HasSelection() const
691 GetSelection(&from
, &to
);
695 bool wxComboBox::CanCopy() const
697 // Can copy if there's a selection
698 return HasSelection();
701 bool wxComboBox::CanCut() const
703 return CanCopy() && IsEditable();
706 bool wxComboBox::CanPaste() const
708 // TODO: check for text on the clipboard
709 return IsEditable() ;
712 bool wxComboBox::IsEditable() const
714 return !HasFlag(wxCB_READONLY
);
718 void wxComboBox::SetInsertionPoint( long pos
)
720 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
722 if ( pos
== GetLastPosition() )
725 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
726 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
729 long wxComboBox::GetInsertionPoint() const
731 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
734 wxTextPos
wxComboBox::GetLastPosition() const
736 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
737 int pos
= GTK_ENTRY(entry
)->text_length
;
741 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
743 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
745 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
746 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
747 if ( value
.empty() ) return;
751 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
752 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
754 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.length(), &pos
);
758 void wxComboBox::SetSelection( long from
, long to
)
760 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
761 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
764 void wxComboBox::GetSelection( long* from
, long* to
) const
768 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
769 *from
= (long) editable
->selection_start_pos
;
770 *to
= (long) editable
->selection_end_pos
;
774 void wxComboBox::SetEditable( bool editable
)
776 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
777 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
780 void wxComboBox::OnChar( wxKeyEvent
&event
)
782 if ( event
.GetKeyCode() == WXK_RETURN
)
784 // GTK automatically selects an item if its in the list
785 wxCommandEvent
eventEnter(wxEVT_TEXT_ENTER
, GetId());
786 eventEnter
.SetString( GetValue() );
787 eventEnter
.SetInt( GetSelection() );
788 eventEnter
.SetEventObject( this );
790 if (!HandleWindowEvent( eventEnter
))
792 // This will invoke the dialog default action, such
793 // as the clicking the default button.
795 wxWindow
*top_frame
= m_parent
;
796 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
797 top_frame
= top_frame
->GetParent();
799 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
801 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
803 if (window
->default_widget
)
804 gtk_widget_activate (window
->default_widget
);
808 // Catch GTK event so that GTK doesn't open the drop
809 // down list upon RETURN.
816 void wxComboBox::DisableEvents()
818 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
819 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
820 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
821 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
824 void wxComboBox::EnableEvents()
826 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
827 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
828 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
829 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
832 void wxComboBox::OnSize( wxSizeEvent
&event
)
834 // NB: In some situations (e.g. on non-first page of a wizard, if the
835 // size used is default size), GtkCombo widget is resized correctly,
836 // but it's look is not updated, it's rendered as if it was much wider.
837 // No other widgets are affected, so it looks like a bug in GTK+.
838 // Manually requesting resize calculation (as gtk_pizza_set_size does)
840 if (GTK_WIDGET_VISIBLE(m_widget
))
841 gtk_widget_queue_resize(m_widget
);
846 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
848 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
850 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
851 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
853 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
854 GList
*child
= list
->children
;
857 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
859 GtkBin
*bin
= GTK_BIN(child
->data
);
860 gtk_widget_modify_style( bin
->child
, style
);
866 GtkWidget
* wxComboBox::GetConnectWidget()
868 return GTK_COMBO(m_widget
)->entry
;
871 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
873 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
874 (window
== GTK_COMBO(m_widget
)->button
->window
) );
877 wxSize
wxComboBox::DoGetBestSize() const
879 wxSize
ret( wxControl::DoGetBestSize() );
881 // we know better our horizontal extent: it depends on the longest string
886 unsigned int count
= GetCount();
887 for ( unsigned int n
= 0; n
< count
; n
++ )
889 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
895 // empty combobox should have some reasonable default size too
905 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
907 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
910 // ----------------------------------------------------------------------------
911 // standard event handling
912 // ----------------------------------------------------------------------------
914 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
919 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
924 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
929 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
934 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
939 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
942 GetSelection(& from
, & to
);
943 if (from
!= -1 && to
!= -1)
947 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
949 SetSelection(-1, -1);
952 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
954 event
.Enable( CanCut() );
957 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
959 event
.Enable( CanCopy() );
962 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
964 event
.Enable( CanPaste() );
967 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
969 event
.Enable( CanUndo() );
972 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
974 event
.Enable( CanRedo() );
977 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
979 event
.Enable(HasSelection() && IsEditable()) ;
982 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
984 event
.Enable(GetLastPosition() > 0);