1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/combobox.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/combobox.h"
18 #include "wx/settings.h"
19 #include "wx/textctrl.h" // for wxEVT_TEXT
20 #include "wx/arrstr.h"
23 #include "wx/gtk1/private.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
37 static int g_SelectionBeforePopup
= wxID_NONE
; // this means the popup is hidden
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 //-----------------------------------------------------------------------------
46 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
48 if (g_isIdle
) wxapp_install_idle_handler();
50 if (combo
->m_ignoreNextUpdate
)
52 combo
->m_ignoreNextUpdate
= false;
56 if (!combo
->m_hasVMT
) return;
58 wxCommandEvent
event( wxEVT_TEXT
, combo
->GetId() );
59 event
.SetString( combo
->GetValue() );
60 event
.SetEventObject( combo
);
61 combo
->HandleWindowEvent( event
);
67 gtk_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
74 gtk_popup_hide_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
76 // when the popup is hidden, throw a SELECTED event only if the combobox
78 const int curSelection
= combo
->GetCurrentSelection();
80 const bool hasChanged
= curSelection
!= g_SelectionBeforePopup
;
82 // reset the selection flag to value meaning that it is hidden and do it
83 // now, before generating the events, so that GetSelection() returns the
84 // new value from the event handler
85 g_SelectionBeforePopup
= wxID_NONE
;
89 wxCommandEvent
event( wxEVT_COMBOBOX
, combo
->GetId() );
90 event
.SetInt( curSelection
);
91 event
.SetString( combo
->GetStringSelection() );
92 event
.SetEventObject( combo
);
93 combo
->HandleWindowEvent( event
);
95 // for consistency with the other ports, send TEXT event
96 wxCommandEvent
event2( wxEVT_TEXT
, combo
->GetId() );
97 event2
.SetString( combo
->GetStringSelection() );
98 event2
.SetEventObject( combo
);
99 combo
->HandleWindowEvent( event2
);
106 gtk_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
108 // store the combobox selection value before the popup is shown
109 g_SelectionBeforePopup
= combo
->GetCurrentSelection();
113 //-----------------------------------------------------------------------------
114 // "select-child" - click/cursor get select-child, changed, select-child
115 //-----------------------------------------------------------------------------
119 gtk_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
121 if (g_isIdle
) wxapp_install_idle_handler();
123 if (!combo
->m_hasVMT
) return;
125 if (g_blockEventsOnDrag
) return;
127 int curSelection
= combo
->GetCurrentSelection();
129 if (combo
->m_prevSelection
== curSelection
) return;
131 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
132 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
134 combo
->m_prevSelection
= curSelection
;
136 // Quickly set the value of the combo box
137 // as GTK+ does that only AFTER the event
139 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
),
140 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
141 combo
->SetValue( combo
->GetStringSelection() );
142 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
), "changed",
143 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
145 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
146 // because when combobox popup is shown, gtk_combo_select_child_callback is
147 // called each times the mouse is over an item with a pressed button so a lot
148 // of SELECTED event could be generated if the user keep the mouse button down
149 // and select other items ...
150 if (g_SelectionBeforePopup
== wxID_NONE
)
152 wxCommandEvent
event( wxEVT_COMBOBOX
, combo
->GetId() );
153 event
.SetInt( curSelection
);
154 event
.SetString( combo
->GetStringSelection() );
155 event
.SetEventObject( combo
);
156 combo
->HandleWindowEvent( event
);
158 // for consistency with the other ports, don't generate text update
159 // events while the user is browsing the combobox neither
160 wxCommandEvent
event2( wxEVT_TEXT
, combo
->GetId() );
161 event2
.SetString( combo
->GetValue() );
162 event2
.SetEventObject( combo
);
163 combo
->HandleWindowEvent( event2
);
168 //-----------------------------------------------------------------------------
170 //-----------------------------------------------------------------------------
172 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
173 EVT_SIZE(wxComboBox::OnSize
)
174 EVT_CHAR(wxComboBox::OnChar
)
176 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
177 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
178 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
179 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
180 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
181 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
182 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
184 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
185 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
186 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
187 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
188 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
189 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
190 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
193 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
194 const wxString
& value
,
195 const wxPoint
& pos
, const wxSize
& size
,
196 const wxArrayString
& choices
,
197 long style
, const wxValidator
& validator
,
198 const wxString
& name
)
200 wxCArrayString
chs(choices
);
202 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
203 chs
.GetStrings(), style
, validator
, name
);
206 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
207 const wxPoint
& pos
, const wxSize
& size
,
208 int n
, const wxString choices
[],
209 long style
, const wxValidator
& validator
,
210 const wxString
& name
)
212 m_ignoreNextUpdate
= false;
214 m_acceptsFocus
= true;
217 if (!PreCreation( parent
, pos
, size
) ||
218 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
220 wxFAIL_MSG( wxT("wxComboBox creation failed") );
224 m_widget
= gtk_combo_new();
225 GtkCombo
*combo
= GTK_COMBO(m_widget
);
227 // Disable GTK's broken events ...
228 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
229 // ... and add surrogate handler.
230 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
231 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
233 // make it more useable
234 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
236 // and case-sensitive
237 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
239 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
241 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
243 for (int i
= 0; i
< n
; i
++)
245 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
247 m_clientDataList
.Append( NULL
);
248 m_clientObjectList
.Append( NULL
);
250 gtk_container_add( GTK_CONTAINER(list
), list_item
);
252 gtk_widget_show( list_item
);
255 m_parent
->DoAddChild( this );
257 m_focusWidget
= combo
->entry
;
261 ConnectWidget( combo
->button
);
263 // MSW's combo box shows the value and the selection is -1
264 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
265 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
267 if (style
& wxCB_READONLY
)
268 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
270 // "show" and "hide" events are generated when user click on the combobox button which popups a list
271 // this list is the "popwin" gtk widget
272 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "hide",
273 GTK_SIGNAL_FUNC(gtk_popup_hide_callback
), (gpointer
)this );
274 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "show",
275 GTK_SIGNAL_FUNC(gtk_popup_show_callback
), (gpointer
)this );
277 gtk_signal_connect_after( GTK_OBJECT(combo
->entry
), "changed",
278 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
280 gtk_signal_connect_after( GTK_OBJECT(combo
->list
), "select-child",
281 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
283 SetInitialSize(size
); // need this too because this is a wxControlWithItems
285 // This is required for tool bar support
286 // wxSize setsize = GetSize();
287 // gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
292 wxComboBox::~wxComboBox()
294 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
297 wxClientData
*cd
= (wxClientData
*)node
->GetData();
299 node
= node
->GetNext();
301 m_clientObjectList
.Clear();
303 m_clientDataList
.Clear();
306 void wxComboBox::SetFocus()
310 // don't do anything if we already have focus
314 gtk_widget_grab_focus( m_focusWidget
);
317 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
320 wxClientDataType type
)
322 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
326 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
328 GtkRcStyle
*style
= CreateWidgetStyle();
330 const unsigned int count
= items
.GetCount();
331 for( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
334 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( items
[i
] ) );
336 if ( pos
== GetCount() )
338 gtk_container_add( GTK_CONTAINER(list
), list_item
);
340 else // insert, not append
342 GList
*gitem_list
= g_list_alloc ();
343 gitem_list
->data
= list_item
;
344 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
347 if (GTK_WIDGET_REALIZED(m_widget
))
349 gtk_widget_realize( list_item
);
350 gtk_widget_realize( GTK_BIN(list_item
)->child
);
354 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
355 GtkBin
*bin
= GTK_BIN( list_item
);
356 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
357 gtk_widget_modify_style( label
, style
);
362 gtk_widget_show( list_item
);
364 if ( m_clientDataList
.GetCount() < GetCount() )
365 m_clientDataList
.Insert( pos
, NULL
);
366 if ( m_clientObjectList
.GetCount() < GetCount() )
367 m_clientObjectList
.Insert( pos
, NULL
);
369 AssignNewItemClientData(pos
, clientData
, i
, type
);
373 gtk_rc_style_unref( style
);
377 InvalidateBestSize();
382 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
384 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
386 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
389 node
->SetData( (wxObject
*) clientData
);
392 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
394 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
396 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
398 return node
? node
->GetData() : NULL
;
401 void wxComboBox::DoClear()
403 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
407 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
408 gtk_list_clear_items( GTK_LIST(list
), 0, (int)GetCount() );
410 m_clientObjectList
.Clear();
412 m_clientDataList
.Clear();
416 InvalidateBestSize();
419 void wxComboBox::DoDeleteOneItem(unsigned int n
)
421 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
423 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
425 GList
*child
= g_list_nth( listbox
->children
, n
);
429 wxFAIL_MSG(wxT("wrong index"));
435 GList
*list
= g_list_append( NULL
, child
->data
);
436 gtk_list_remove_items( listbox
, list
);
439 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
442 m_clientObjectList
.Erase( node
);
445 node
= m_clientDataList
.Item( n
);
447 m_clientDataList
.Erase( node
);
451 InvalidateBestSize();
454 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
456 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
458 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
460 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
463 GtkBin
*bin
= GTK_BIN( child
->data
);
464 GtkLabel
*label
= GTK_LABEL( bin
->child
);
465 gtk_label_set_text(label
, wxGTK_CONV(text
));
469 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
472 InvalidateBestSize();
475 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
477 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
479 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
481 GList
*child
= GTK_LIST(list
)->children
;
485 GtkBin
*bin
= GTK_BIN( child
->data
);
486 GtkLabel
*label
= GTK_LABEL( bin
->child
);
487 wxString
str( label
->label
);
488 if (item
.IsSameAs( str
, bCase
) )
498 int wxComboBox::GetSelection() const
500 // if the popup is currently opened, use the selection as it had been
501 // before it dropped down
502 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
503 : g_SelectionBeforePopup
;
506 int wxComboBox::GetCurrentSelection() const
508 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
510 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
512 GList
*selection
= GTK_LIST(list
)->selection
;
515 GList
*child
= GTK_LIST(list
)->children
;
519 if (child
->data
== selection
->data
) return count
;
528 wxString
wxComboBox::GetString(unsigned int n
) const
530 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
532 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
535 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
538 GtkBin
*bin
= GTK_BIN( child
->data
);
539 GtkLabel
*label
= GTK_LABEL( bin
->child
);
540 str
= wxString( label
->label
);
544 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
550 wxString
wxComboBox::GetStringSelection() const
552 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
554 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
556 GList
*selection
= GTK_LIST(list
)->selection
;
559 GtkBin
*bin
= GTK_BIN( selection
->data
);
560 GtkLabel
*label
= GTK_LABEL( bin
->child
);
561 wxString
tmp( label
->label
);
565 wxFAIL_MSG( wxT("wxComboBox: no selection") );
567 return wxEmptyString
;
570 unsigned int wxComboBox::GetCount() const
572 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
574 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
576 GList
*child
= GTK_LIST(list
)->children
;
577 unsigned int count
= 0;
578 while (child
) { count
++; child
= child
->next
; }
582 void wxComboBox::SetSelection( int n
)
584 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
588 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
589 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
590 gtk_list_select_item( GTK_LIST(list
), n
);
596 wxString
wxComboBox::DoGetValue() const
598 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
599 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
602 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
605 printf( "%d ", (int) (c
) );
613 void wxComboBox::SetValue( const wxString
& value
)
615 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
617 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
618 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( value
) );
620 InvalidateBestSize();
623 void wxComboBox::WriteText(const wxString
& value
)
625 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
627 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
628 GtkEditable
* const edit
= GTK_EDITABLE(entry
);
630 gtk_editable_delete_selection(edit
);
631 gint len
= gtk_editable_get_position(edit
);
632 gtk_editable_insert_text(edit
, wxGTK_CONV(value
), -1, &len
);
633 gtk_editable_set_position(edit
, len
);
636 void wxComboBox::Copy()
638 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
640 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
641 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
644 void wxComboBox::Cut()
646 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
648 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
649 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
652 void wxComboBox::Paste()
654 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
656 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
657 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
660 void wxComboBox::Undo()
665 void wxComboBox::Redo()
670 void wxComboBox::SelectAll()
672 SetSelection(0, GetLastPosition());
675 bool wxComboBox::CanUndo() const
681 bool wxComboBox::CanRedo() const
687 bool wxComboBox::HasSelection() const
690 GetSelection(&from
, &to
);
694 bool wxComboBox::CanCopy() const
696 // Can copy if there's a selection
697 return HasSelection();
700 bool wxComboBox::CanCut() const
702 return CanCopy() && IsEditable();
705 bool wxComboBox::CanPaste() const
707 // TODO: check for text on the clipboard
708 return IsEditable() ;
711 bool wxComboBox::IsEditable() const
713 return !HasFlag(wxCB_READONLY
);
717 void wxComboBox::SetInsertionPoint( long pos
)
719 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
721 if ( pos
== GetLastPosition() )
724 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
725 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
728 long wxComboBox::GetInsertionPoint() const
730 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
733 wxTextPos
wxComboBox::GetLastPosition() const
735 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
736 int pos
= GTK_ENTRY(entry
)->text_length
;
740 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
742 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
744 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
745 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
746 if ( value
.empty() ) return;
750 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
751 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
753 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.length(), &pos
);
757 void wxComboBox::SetSelection( long from
, long to
)
759 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
760 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
763 void wxComboBox::GetSelection( long* from
, long* to
) const
767 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
768 *from
= (long) editable
->selection_start_pos
;
769 *to
= (long) editable
->selection_end_pos
;
773 void wxComboBox::SetEditable( bool editable
)
775 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
776 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
779 void wxComboBox::OnChar( wxKeyEvent
&event
)
781 if ( event
.GetKeyCode() == WXK_RETURN
)
783 // GTK automatically selects an item if its in the list
784 wxCommandEvent
eventEnter(wxEVT_TEXT_ENTER
, GetId());
785 eventEnter
.SetString( GetValue() );
786 eventEnter
.SetInt( GetSelection() );
787 eventEnter
.SetEventObject( this );
789 if (!HandleWindowEvent( eventEnter
))
791 // This will invoke the dialog default action, such
792 // as the clicking the default button.
794 wxWindow
*top_frame
= m_parent
;
795 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
796 top_frame
= top_frame
->GetParent();
798 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
800 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
802 if (window
->default_widget
)
803 gtk_widget_activate (window
->default_widget
);
807 // Catch GTK event so that GTK doesn't open the drop
808 // down list upon RETURN.
815 void wxComboBox::DisableEvents()
817 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
818 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
819 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
820 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
823 void wxComboBox::EnableEvents()
825 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
826 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
827 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
828 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
831 void wxComboBox::OnSize( wxSizeEvent
&event
)
833 // NB: In some situations (e.g. on non-first page of a wizard, if the
834 // size used is default size), GtkCombo widget is resized correctly,
835 // but it's look is not updated, it's rendered as if it was much wider.
836 // No other widgets are affected, so it looks like a bug in GTK+.
837 // Manually requesting resize calculation (as gtk_pizza_set_size does)
839 if (GTK_WIDGET_VISIBLE(m_widget
))
840 gtk_widget_queue_resize(m_widget
);
845 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
847 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
849 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
850 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
852 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
853 GList
*child
= list
->children
;
856 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
858 GtkBin
*bin
= GTK_BIN(child
->data
);
859 gtk_widget_modify_style( bin
->child
, style
);
865 GtkWidget
* wxComboBox::GetConnectWidget()
867 return GTK_COMBO(m_widget
)->entry
;
870 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
872 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
873 (window
== GTK_COMBO(m_widget
)->button
->window
) );
876 wxSize
wxComboBox::DoGetBestSize() const
878 wxSize
ret( wxControl::DoGetBestSize() );
880 // we know better our horizontal extent: it depends on the longest string
885 unsigned int count
= GetCount();
886 for ( unsigned int n
= 0; n
< count
; n
++ )
888 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
894 // empty combobox should have some reasonable default size too
904 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
906 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
909 // ----------------------------------------------------------------------------
910 // standard event handling
911 // ----------------------------------------------------------------------------
913 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
918 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
923 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
928 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
933 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
938 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
941 GetSelection(& from
, & to
);
942 if (from
!= -1 && to
!= -1)
946 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
948 SetSelection(-1, -1);
951 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
953 event
.Enable( CanCut() );
956 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
958 event
.Enable( CanCopy() );
961 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
963 event
.Enable( CanPaste() );
966 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
968 event
.Enable( CanUndo() );
971 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
973 event
.Enable( CanRedo() );
976 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
978 event
.Enable(HasSelection() && IsEditable()) ;
981 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
983 event
.Enable(GetLastPosition() > 0);