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 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_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
90 event
.SetInt( curSelection
);
91 event
.SetString( combo
->GetStringSelection() );
92 event
.SetEventObject( combo
);
93 combo
->GetEventHandler()->ProcessEvent( event
);
95 // for consistency with the other ports, send TEXT event
96 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
97 event2
.SetString( combo
->GetStringSelection() );
98 event2
.SetEventObject( combo
);
99 combo
->GetEventHandler()->ProcessEvent( 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_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
153 event
.SetInt( curSelection
);
154 event
.SetString( combo
->GetStringSelection() );
155 event
.SetEventObject( combo
);
156 combo
->GetEventHandler()->ProcessEvent( 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_COMMAND_TEXT_UPDATED
, combo
->GetId() );
161 event2
.SetString( combo
->GetValue() );
162 event2
.SetEventObject( combo
);
163 combo
->GetEventHandler()->ProcessEvent( event2
);
168 //-----------------------------------------------------------------------------
170 //-----------------------------------------------------------------------------
172 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
174 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
175 EVT_SIZE(wxComboBox::OnSize
)
176 EVT_CHAR(wxComboBox::OnChar
)
178 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
179 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
180 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
181 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
182 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
183 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
184 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
186 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
187 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
188 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
189 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
190 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
191 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
192 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
195 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
196 const wxString
& value
,
197 const wxPoint
& pos
, const wxSize
& size
,
198 const wxArrayString
& choices
,
199 long style
, const wxValidator
& validator
,
200 const wxString
& name
)
202 wxCArrayString
chs(choices
);
204 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
205 chs
.GetStrings(), style
, validator
, name
);
208 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
209 const wxPoint
& pos
, const wxSize
& size
,
210 int n
, const wxString choices
[],
211 long style
, const wxValidator
& validator
,
212 const wxString
& name
)
214 m_ignoreNextUpdate
= false;
216 m_acceptsFocus
= true;
219 if (!PreCreation( parent
, pos
, size
) ||
220 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
222 wxFAIL_MSG( wxT("wxComboBox creation failed") );
226 m_widget
= gtk_combo_new();
227 GtkCombo
*combo
= GTK_COMBO(m_widget
);
229 // Disable GTK's broken events ...
230 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
231 // ... and add surrogate handler.
232 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
233 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
235 // make it more useable
236 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
238 // and case-sensitive
239 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
241 if (style
& wxNO_BORDER
)
242 g_object_set( GTK_ENTRY( combo
->entry
), "has-frame", FALSE
, NULL
);
244 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
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 SetBestSize(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 int count
= GetCount();
353 if ( (int)m_clientDataList
.GetCount() < count
)
354 m_clientDataList
.Append( (wxObject
*) NULL
);
355 if ( (int)m_clientObjectList
.GetCount() < count
)
356 m_clientObjectList
.Append( (wxObject
*) NULL
);
360 InvalidateBestSize();
365 int wxComboBox::DoInsert( const wxString
&item
, 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 int count
= GetCount();
373 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
380 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
382 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
384 GList
*gitem_list
= g_list_alloc ();
385 gitem_list
->data
= list_item
;
386 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
388 if (GTK_WIDGET_REALIZED(m_widget
))
390 gtk_widget_realize( list_item
);
391 gtk_widget_realize( GTK_BIN(list_item
)->child
);
396 gtk_widget_show( list_item
);
400 if ( (int)m_clientDataList
.GetCount() < count
)
401 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
402 if ( (int)m_clientObjectList
.GetCount() < count
)
403 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
407 InvalidateBestSize();
412 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
414 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
416 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
419 node
->SetData( (wxObject
*) clientData
);
422 void* wxComboBox::DoGetItemClientData( int n
) const
424 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
426 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
428 return node
? node
->GetData() : NULL
;
431 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
433 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
435 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
438 // wxItemContainer already deletes data for us
440 node
->SetData( (wxObject
*) clientData
);
443 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
445 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
447 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
449 return node
? (wxClientData
*) node
->GetData() : NULL
;
452 void wxComboBox::Clear()
454 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
458 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
459 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
461 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
464 wxClientData
*cd
= (wxClientData
*)node
->GetData();
466 node
= node
->GetNext();
468 m_clientObjectList
.Clear();
470 m_clientDataList
.Clear();
474 InvalidateBestSize();
477 void wxComboBox::Delete( int n
)
479 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
481 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
483 GList
*child
= g_list_nth( listbox
->children
, n
);
487 wxFAIL_MSG(wxT("wrong index"));
493 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
494 gtk_list_remove_items( listbox
, list
);
497 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
500 wxClientData
*cd
= (wxClientData
*)node
->GetData();
502 m_clientObjectList
.Erase( node
);
505 node
= m_clientDataList
.Item( n
);
507 m_clientDataList
.Erase( node
);
511 InvalidateBestSize();
514 void wxComboBox::SetString(int n
, const wxString
&text
)
516 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
518 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
520 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
523 GtkBin
*bin
= GTK_BIN( child
->data
);
524 GtkLabel
*label
= GTK_LABEL( bin
->child
);
525 gtk_label_set_text(label
, wxGTK_CONV(text
));
529 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
532 InvalidateBestSize();
535 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
537 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
539 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
541 GList
*child
= GTK_LIST(list
)->children
;
545 GtkBin
*bin
= GTK_BIN( child
->data
);
546 GtkLabel
*label
= GTK_LABEL( bin
->child
);
547 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
549 if (item
.IsSameAs( str
, bCase
) )
559 int wxComboBox::GetSelection() const
561 // if the popup is currently opened, use the selection as it had been
562 // before it dropped down
563 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
564 : g_SelectionBeforePopup
;
567 int wxComboBox::GetCurrentSelection() const
569 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
571 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
573 GList
*selection
= GTK_LIST(list
)->selection
;
576 GList
*child
= GTK_LIST(list
)->children
;
580 if (child
->data
== selection
->data
) return count
;
589 wxString
wxComboBox::GetString( int n
) const
591 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
593 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
596 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
599 GtkBin
*bin
= GTK_BIN( child
->data
);
600 GtkLabel
*label
= GTK_LABEL( bin
->child
);
601 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
605 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
611 wxString
wxComboBox::GetStringSelection() const
613 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
615 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
617 GList
*selection
= GTK_LIST(list
)->selection
;
620 GtkBin
*bin
= GTK_BIN( selection
->data
);
621 GtkLabel
*label
= GTK_LABEL( bin
->child
);
622 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
626 wxFAIL_MSG( wxT("wxComboBox: no selection") );
628 return wxEmptyString
;
631 int wxComboBox::GetCount() const
633 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
635 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
637 GList
*child
= GTK_LIST(list
)->children
;
639 while (child
) { count
++; child
= child
->next
; }
643 void wxComboBox::SetSelection( int n
)
645 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
649 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
650 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
651 gtk_list_select_item( GTK_LIST(list
), n
);
657 wxString
wxComboBox::GetValue() const
659 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
660 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
663 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
666 printf( "%d ", (int) (c
) );
674 void wxComboBox::SetValue( const wxString
& value
)
676 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
678 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
680 if (!value
.IsNull()) tmp
= value
;
681 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
683 InvalidateBestSize();
686 void wxComboBox::Copy()
688 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
690 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
691 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
694 void wxComboBox::Cut()
696 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
698 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
699 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
702 void wxComboBox::Paste()
704 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
706 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
707 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
710 void wxComboBox::Undo()
715 void wxComboBox::Redo()
720 void wxComboBox::SelectAll()
722 SetSelection(0, GetLastPosition());
725 bool wxComboBox::CanUndo() const
731 bool wxComboBox::CanRedo() const
737 bool wxComboBox::HasSelection() const
740 GetSelection(&from
, &to
);
744 bool wxComboBox::CanCopy() const
746 // Can copy if there's a selection
747 return HasSelection();
750 bool wxComboBox::CanCut() const
752 return CanCopy() && IsEditable();
755 bool wxComboBox::CanPaste() const
757 // TODO: check for text on the clipboard
758 return IsEditable() ;
761 bool wxComboBox::IsEditable() const
763 return !HasFlag(wxCB_READONLY
);
767 void wxComboBox::SetInsertionPoint( long pos
)
769 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
771 if ( pos
== GetLastPosition() )
774 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
775 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
778 long wxComboBox::GetInsertionPoint() const
780 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
783 wxTextPos
wxComboBox::GetLastPosition() const
785 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
786 int pos
= GTK_ENTRY(entry
)->text_length
;
790 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
792 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
794 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
795 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
796 if (value
.IsNull()) return;
800 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
801 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
803 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
807 void wxComboBox::SetSelection( long from
, long to
)
809 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
810 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
813 void wxComboBox::GetSelection( long* from
, long* to
) const
817 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
819 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
825 void wxComboBox::SetEditable( bool editable
)
827 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
828 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
831 void wxComboBox::OnChar( wxKeyEvent
&event
)
833 if ( event
.GetKeyCode() == WXK_RETURN
)
835 // GTK automatically selects an item if its in the list
836 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
837 eventEnter
.SetString( GetValue() );
838 eventEnter
.SetInt( GetSelection() );
839 eventEnter
.SetEventObject( this );
841 if (!GetEventHandler()->ProcessEvent( eventEnter
))
843 // This will invoke the dialog default action, such
844 // as the clicking the default button.
846 wxWindow
*top_frame
= m_parent
;
847 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
848 top_frame
= top_frame
->GetParent();
850 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
852 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
854 if (window
->default_widget
)
855 gtk_widget_activate (window
->default_widget
);
859 // Catch GTK event so that GTK doesn't open the drop
860 // down list upon RETURN.
867 void wxComboBox::DisableEvents()
869 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
870 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
871 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
872 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
875 void wxComboBox::EnableEvents()
877 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
878 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
879 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
880 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
883 void wxComboBox::OnSize( wxSizeEvent
&event
)
885 // NB: In some situations (e.g. on non-first page of a wizard, if the
886 // size used is default size), GtkCombo widget is resized correctly,
887 // but it's look is not updated, it's rendered as if it was much wider.
888 // No other widgets are affected, so it looks like a bug in GTK+.
889 // Manually requesting resize calculation (as gtk_pizza_set_size does)
891 if (GTK_WIDGET_VISIBLE(m_widget
))
892 gtk_widget_queue_resize(m_widget
);
897 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
899 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
901 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
902 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
904 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
905 GList
*child
= list
->children
;
908 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
910 GtkBin
*bin
= GTK_BIN(child
->data
);
911 gtk_widget_modify_style( bin
->child
, style
);
917 GtkWidget
* wxComboBox::GetConnectWidget()
919 return GTK_COMBO(m_widget
)->entry
;
922 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
924 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
925 (window
== GTK_COMBO(m_widget
)->button
->window
) );
928 wxSize
wxComboBox::DoGetBestSize() const
930 wxSize
ret( wxControl::DoGetBestSize() );
932 // we know better our horizontal extent: it depends on the longest string
937 size_t count
= GetCount();
938 for ( size_t n
= 0; n
< count
; n
++ )
940 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
946 // empty combobox should have some reasonable default size too
956 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
958 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
961 // ----------------------------------------------------------------------------
962 // standard event handling
963 // ----------------------------------------------------------------------------
965 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
970 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
975 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
980 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
985 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
990 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
993 GetSelection(& from
, & to
);
994 if (from
!= -1 && to
!= -1)
998 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1000 SetSelection(-1, -1);
1003 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1005 event
.Enable( CanCut() );
1008 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1010 event
.Enable( CanCopy() );
1013 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1015 event
.Enable( CanPaste() );
1018 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1020 event
.Enable( CanUndo() );
1023 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1025 event
.Enable( CanRedo() );
1028 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1030 event
.Enable(HasSelection() && IsEditable()) ;
1033 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1035 event
.Enable(GetLastPosition() > 0);