1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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"
13 #include "wx/combobox.h"
17 #include "wx/settings.h"
18 #include "wx/arrstr.h"
21 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
23 #include "wx/gtk/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_COMMAND_TEXT_UPDATED
, combo
->GetId() );
59 event
.SetString( combo
->GetValue() );
60 event
.SetEventObject( combo
);
61 combo
->GetEventHandler()->ProcessEvent( 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 int curSelection
= combo
->GetCurrentSelection();
79 if (g_SelectionBeforePopup
!= curSelection
)
81 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
82 event
.SetInt( curSelection
);
83 event
.SetString( combo
->GetStringSelection() );
84 event
.SetEventObject( combo
);
85 combo
->GetEventHandler()->ProcessEvent( event
);
88 // reset the selection flag to value meaning that it is hidden
89 g_SelectionBeforePopup
= wxID_NONE
;
95 gtk_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
97 // store the combobox selection value before the popup is shown
98 g_SelectionBeforePopup
= combo
->GetCurrentSelection();
102 //-----------------------------------------------------------------------------
103 // "select-child" - click/cursor get select-child, changed, select-child
104 //-----------------------------------------------------------------------------
108 gtk_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
110 if (g_isIdle
) wxapp_install_idle_handler();
112 if (!combo
->m_hasVMT
) return;
114 if (g_blockEventsOnDrag
) return;
116 int curSelection
= combo
->GetCurrentSelection();
118 if (combo
->m_prevSelection
== curSelection
) return;
120 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
121 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
123 combo
->m_prevSelection
= curSelection
;
125 // Quickly set the value of the combo box
126 // as GTK+ does that only AFTER the event
128 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
),
129 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
130 combo
->SetValue( combo
->GetStringSelection() );
131 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
), "changed",
132 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
134 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
135 // because when combobox popup is shown, gtk_combo_select_child_callback is
136 // called each times the mouse is over an item with a pressed button so a lot
137 // of SELECTED event could be generated if the user keep the mouse button down
138 // and select other items ...
139 if (g_SelectionBeforePopup
== wxID_NONE
)
141 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
142 event
.SetInt( curSelection
);
143 event
.SetString( combo
->GetStringSelection() );
144 event
.SetEventObject( combo
);
145 combo
->GetEventHandler()->ProcessEvent( event
);
147 // for consistencu with the other ports, don't generate text update
148 // events while the user is browsing the combobox neither
149 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
150 event2
.SetString( combo
->GetValue() );
151 event2
.SetEventObject( combo
);
152 combo
->GetEventHandler()->ProcessEvent( event2
);
157 //-----------------------------------------------------------------------------
159 //-----------------------------------------------------------------------------
161 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
163 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
164 EVT_SIZE(wxComboBox::OnSize
)
165 EVT_CHAR(wxComboBox::OnChar
)
167 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
168 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
169 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
170 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
171 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
172 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
173 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
175 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
176 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
177 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
178 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
179 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
180 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
181 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
184 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
185 const wxString
& value
,
186 const wxPoint
& pos
, const wxSize
& size
,
187 const wxArrayString
& choices
,
188 long style
, const wxValidator
& validator
,
189 const wxString
& name
)
191 wxCArrayString
chs(choices
);
193 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
194 chs
.GetStrings(), style
, validator
, name
);
197 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
198 const wxPoint
& pos
, const wxSize
& size
,
199 int n
, const wxString choices
[],
200 long style
, const wxValidator
& validator
,
201 const wxString
& name
)
203 m_ignoreNextUpdate
= false;
205 m_acceptsFocus
= true;
208 if (!PreCreation( parent
, pos
, size
) ||
209 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
211 wxFAIL_MSG( wxT("wxComboBox creation failed") );
215 m_widget
= gtk_combo_new();
216 GtkCombo
*combo
= GTK_COMBO(m_widget
);
218 // Disable GTK's broken events ...
219 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
220 // ... and add surogate handler.
221 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
222 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
224 // make it more useable
225 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
227 // and case-sensitive
228 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
231 if (style
& wxNO_BORDER
)
232 g_object_set( GTK_ENTRY( combo
->entry
), "has-frame", FALSE
, NULL
);
235 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
238 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
241 for (int i
= 0; i
< n
; i
++)
243 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
245 m_clientDataList
.Append( (wxObject
*)NULL
);
246 m_clientObjectList
.Append( (wxObject
*)NULL
);
248 gtk_container_add( GTK_CONTAINER(list
), list_item
);
250 gtk_widget_show( list_item
);
253 m_parent
->DoAddChild( this );
255 m_focusWidget
= combo
->entry
;
259 ConnectWidget( combo
->button
);
261 // MSW's combo box shows the value and the selection is -1
262 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
263 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
265 if (style
& wxCB_READONLY
)
266 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
268 // "show" and "hide" events are generated when user click on the combobox button which popups a list
269 // this list is the "popwin" gtk widget
270 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "hide",
271 GTK_SIGNAL_FUNC(gtk_popup_hide_callback
), (gpointer
)this );
272 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "show",
273 GTK_SIGNAL_FUNC(gtk_popup_show_callback
), (gpointer
)this );
275 gtk_signal_connect_after( GTK_OBJECT(combo
->entry
), "changed",
276 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
278 gtk_signal_connect_after( GTK_OBJECT(combo
->list
), "select-child",
279 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
281 SetBestSize(size
); // need this too because this is a wxControlWithItems
283 // This is required for tool bar support
284 // wxSize setsize = GetSize();
285 // gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
290 wxComboBox::~wxComboBox()
292 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
295 wxClientData
*cd
= (wxClientData
*)node
->GetData();
297 node
= node
->GetNext();
299 m_clientObjectList
.Clear();
301 m_clientDataList
.Clear();
304 void wxComboBox::SetFocus()
308 // don't do anything if we already have focus
312 gtk_widget_grab_focus( m_focusWidget
);
315 int wxComboBox::DoAppend( const wxString
&item
)
317 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
321 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
323 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
325 gtk_container_add( GTK_CONTAINER(list
), list_item
);
327 if (GTK_WIDGET_REALIZED(m_widget
))
329 gtk_widget_realize( list_item
);
330 gtk_widget_realize( GTK_BIN(list_item
)->child
);
333 // Apply current widget style to the new list_item
334 GtkRcStyle
*style
= CreateWidgetStyle();
337 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
338 GtkBin
*bin
= GTK_BIN( list_item
);
339 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
340 gtk_widget_modify_style( label
, style
);
341 gtk_rc_style_unref( style
);
344 gtk_widget_show( list_item
);
346 const int count
= GetCount();
348 if ( (int)m_clientDataList
.GetCount() < count
)
349 m_clientDataList
.Append( (wxObject
*) NULL
);
350 if ( (int)m_clientObjectList
.GetCount() < count
)
351 m_clientObjectList
.Append( (wxObject
*) NULL
);
355 InvalidateBestSize();
360 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
362 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
363 wxT("can't insert into sorted list"));
365 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
367 int count
= GetCount();
368 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
375 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
377 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
379 GList
*gitem_list
= g_list_alloc ();
380 gitem_list
->data
= list_item
;
381 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
383 if (GTK_WIDGET_REALIZED(m_widget
))
385 gtk_widget_realize( list_item
);
386 gtk_widget_realize( GTK_BIN(list_item
)->child
);
391 gtk_widget_show( list_item
);
395 if ( (int)m_clientDataList
.GetCount() < count
)
396 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
397 if ( (int)m_clientObjectList
.GetCount() < count
)
398 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
402 InvalidateBestSize();
407 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
409 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
411 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
414 node
->SetData( (wxObject
*) clientData
);
417 void* wxComboBox::DoGetItemClientData( int n
) const
419 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
421 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
423 return node
? node
->GetData() : NULL
;
426 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
428 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
430 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
433 // wxItemContainer already deletes data for us
435 node
->SetData( (wxObject
*) clientData
);
438 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
440 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
442 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
444 return node
? (wxClientData
*) node
->GetData() : NULL
;
447 void wxComboBox::Clear()
449 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
453 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
454 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
456 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
459 wxClientData
*cd
= (wxClientData
*)node
->GetData();
461 node
= node
->GetNext();
463 m_clientObjectList
.Clear();
465 m_clientDataList
.Clear();
469 InvalidateBestSize();
472 void wxComboBox::Delete( int n
)
474 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
476 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
478 GList
*child
= g_list_nth( listbox
->children
, n
);
482 wxFAIL_MSG(wxT("wrong index"));
488 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
489 gtk_list_remove_items( listbox
, list
);
492 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
495 wxClientData
*cd
= (wxClientData
*)node
->GetData();
497 m_clientObjectList
.Erase( node
);
500 node
= m_clientDataList
.Item( n
);
502 m_clientDataList
.Erase( node
);
506 InvalidateBestSize();
509 void wxComboBox::SetString(int n
, const wxString
&text
)
511 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
513 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
515 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
518 GtkBin
*bin
= GTK_BIN( child
->data
);
519 GtkLabel
*label
= GTK_LABEL( bin
->child
);
520 gtk_label_set_text(label
, wxGTK_CONV(text
));
524 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
527 InvalidateBestSize();
530 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
532 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
534 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
536 GList
*child
= GTK_LIST(list
)->children
;
540 GtkBin
*bin
= GTK_BIN( child
->data
);
541 GtkLabel
*label
= GTK_LABEL( bin
->child
);
543 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
545 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( 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
);
600 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
602 str
= wxString( label
->label
);
607 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
613 wxString
wxComboBox::GetStringSelection() const
615 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
617 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
619 GList
*selection
= GTK_LIST(list
)->selection
;
622 GtkBin
*bin
= GTK_BIN( selection
->data
);
623 GtkLabel
*label
= GTK_LABEL( bin
->child
);
625 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
627 wxString
tmp( label
->label
);
632 wxFAIL_MSG( wxT("wxComboBox: no selection") );
634 return wxEmptyString
;
637 int wxComboBox::GetCount() const
639 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
641 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
643 GList
*child
= GTK_LIST(list
)->children
;
645 while (child
) { count
++; child
= child
->next
; }
649 void wxComboBox::SetSelection( int n
)
651 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
655 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
656 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
657 gtk_list_select_item( GTK_LIST(list
), n
);
663 wxString
wxComboBox::GetValue() const
665 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
666 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
669 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
672 printf( "%d ", (int) (c
) );
680 void wxComboBox::SetValue( const wxString
& value
)
682 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
684 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
686 if (!value
.IsNull()) tmp
= value
;
687 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
689 InvalidateBestSize();
692 void wxComboBox::Copy()
694 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
696 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
697 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
700 void wxComboBox::Cut()
702 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
704 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
705 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
708 void wxComboBox::Paste()
710 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
712 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
713 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
716 void wxComboBox::Undo()
721 void wxComboBox::Redo()
726 void wxComboBox::SelectAll()
728 SetSelection(0, GetLastPosition());
731 bool wxComboBox::CanUndo() const
737 bool wxComboBox::CanRedo() const
743 bool wxComboBox::HasSelection() const
746 GetSelection(&from
, &to
);
750 bool wxComboBox::CanCopy() const
752 // Can copy if there's a selection
753 return HasSelection();
756 bool wxComboBox::CanCut() const
758 return CanCopy() && IsEditable();
761 bool wxComboBox::CanPaste() const
763 // TODO: check for text on the clipboard
764 return IsEditable() ;
767 bool wxComboBox::IsEditable() const
769 return !HasFlag(wxCB_READONLY
);
773 void wxComboBox::SetInsertionPoint( long pos
)
775 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
777 if ( pos
== GetLastPosition() )
780 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
781 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
784 long wxComboBox::GetInsertionPoint() const
786 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
789 wxTextPos
wxComboBox::GetLastPosition() const
791 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
792 int pos
= GTK_ENTRY(entry
)->text_length
;
796 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
798 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
800 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
801 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
802 if (value
.IsNull()) return;
806 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
807 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
809 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
813 void wxComboBox::SetSelection( long from
, long to
)
815 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
816 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
819 void wxComboBox::GetSelection( long* from
, long* to
) const
823 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
826 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
830 *from
= (long) editable
->selection_start_pos
;
831 *to
= (long) editable
->selection_end_pos
;
836 void wxComboBox::SetEditable( bool editable
)
838 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
839 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
842 void wxComboBox::OnChar( wxKeyEvent
&event
)
844 if ( event
.GetKeyCode() == WXK_RETURN
)
846 // GTK automatically selects an item if its in the list
847 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
848 eventEnter
.SetString( GetValue() );
849 eventEnter
.SetInt( GetSelection() );
850 eventEnter
.SetEventObject( this );
852 if (!GetEventHandler()->ProcessEvent( eventEnter
))
854 // This will invoke the dialog default action, such
855 // as the clicking the default button.
857 wxWindow
*top_frame
= m_parent
;
858 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
859 top_frame
= top_frame
->GetParent();
861 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
863 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
865 if (window
->default_widget
)
866 gtk_widget_activate (window
->default_widget
);
870 // Catch GTK event so that GTK doesn't open the drop
871 // down list upon RETURN.
878 void wxComboBox::DisableEvents()
880 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
881 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
882 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
883 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
886 void wxComboBox::EnableEvents()
888 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
889 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
890 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
891 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
894 void wxComboBox::OnSize( wxSizeEvent
&event
)
896 // NB: In some situations (e.g. on non-first page of a wizard, if the
897 // size used is default size), GtkCombo widget is resized correctly,
898 // but it's look is not updated, it's rendered as if it was much wider.
899 // No other widgets are affected, so it looks like a bug in GTK+.
900 // Manually requesting resize calculation (as gtk_pizza_set_size does)
902 if (GTK_WIDGET_VISIBLE(m_widget
))
903 gtk_widget_queue_resize(m_widget
);
908 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
910 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
912 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
913 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
915 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
916 GList
*child
= list
->children
;
919 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
921 GtkBin
*bin
= GTK_BIN(child
->data
);
922 gtk_widget_modify_style( bin
->child
, style
);
928 GtkWidget
* wxComboBox::GetConnectWidget()
930 return GTK_COMBO(m_widget
)->entry
;
933 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
935 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
936 (window
== GTK_COMBO(m_widget
)->button
->window
) );
939 wxSize
wxComboBox::DoGetBestSize() const
941 wxSize
ret( wxControl::DoGetBestSize() );
943 // we know better our horizontal extent: it depends on the longest string
948 size_t count
= GetCount();
949 for ( size_t n
= 0; n
< count
; n
++ )
951 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
957 // empty combobox should have some reasonable default size too
967 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
969 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
972 // ----------------------------------------------------------------------------
973 // standard event handling
974 // ----------------------------------------------------------------------------
976 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
981 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
986 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
991 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
996 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1001 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1004 GetSelection(& from
, & to
);
1005 if (from
!= -1 && to
!= -1)
1009 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1011 SetSelection(-1, -1);
1014 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1016 event
.Enable( CanCut() );
1019 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1021 event
.Enable( CanCopy() );
1024 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1026 event
.Enable( CanPaste() );
1029 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1031 event
.Enable( CanUndo() );
1034 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1036 event
.Enable( CanRedo() );
1039 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1041 event
.Enable(HasSelection() && IsEditable()) ;
1044 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1046 event
.Enable(GetLastPosition() > 0);