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"
15 #include "wx/combobox.h"
19 #include "wx/settings.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
21 #include "wx/arrstr.h"
24 // We use GtkCombo which has been deprecated since GTK+ 2.3.0
25 // in favour of GtkComboBox for <GTK2.4 runtime
26 // We also use GtkList
27 #ifdef GTK_DISABLE_DEPRECATED
28 #undef GTK_DISABLE_DEPRECATED
30 #include "wx/gtk/private.h"
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 gtkcombo_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
48 if (combo
->m_ignoreNextUpdate
)
50 combo
->m_ignoreNextUpdate
= false;
54 if (!combo
->m_hasVMT
) return;
56 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
57 event
.SetString( combo
->GetValue() );
58 event
.SetEventObject( combo
);
59 combo
->GetEventHandler()->ProcessEvent( event
);
65 gtkcombo_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
72 gtkcombo_popup_hide_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
74 // when the popup is hidden, throw a SELECTED event only if the combobox
76 const int curSelection
= combo
->GetCurrentSelection();
78 const bool hasChanged
= curSelection
!= g_SelectionBeforePopup
;
80 // reset the selection flag to value meaning that it is hidden and do it
81 // now, before generating the events, so that GetSelection() returns the
82 // new value from the event handler
83 g_SelectionBeforePopup
= wxID_NONE
;
87 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
88 event
.SetInt( curSelection
);
89 event
.SetString( combo
->GetStringSelection() );
90 event
.SetEventObject( combo
);
91 combo
->GetEventHandler()->ProcessEvent( event
);
93 // for consistency with the other ports, send TEXT event
94 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
95 event2
.SetString( combo
->GetStringSelection() );
96 event2
.SetEventObject( combo
);
97 combo
->GetEventHandler()->ProcessEvent( event2
);
104 gtkcombo_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
106 // store the combobox selection value before the popup is shown
107 g_SelectionBeforePopup
= combo
->GetCurrentSelection();
111 //-----------------------------------------------------------------------------
112 // "select-child" - click/cursor get select-child, changed, select-child
113 //-----------------------------------------------------------------------------
117 gtkcombo_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
119 if (!combo
->m_hasVMT
) return;
121 if (g_blockEventsOnDrag
) return;
123 int curSelection
= combo
->GetCurrentSelection();
125 if (combo
->m_prevSelection
== curSelection
) return;
127 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
128 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
130 combo
->m_prevSelection
= curSelection
;
132 // Quickly set the value of the combo box
133 // as GTK+ does that only AFTER the event
135 g_signal_handlers_disconnect_by_func (GTK_COMBO (combo
->GetHandle())->entry
,
136 (gpointer
) gtkcombo_text_changed_callback
,
138 combo
->SetValue( combo
->GetStringSelection() );
139 g_signal_connect_after (GTK_COMBO (combo
->GetHandle())->entry
, "changed",
140 G_CALLBACK (gtkcombo_text_changed_callback
), combo
);
142 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
143 // because when combobox popup is shown, gtkcombo_combo_select_child_callback is
144 // called each times the mouse is over an item with a pressed button so a lot
145 // of SELECTED event could be generated if the user keep the mouse button down
146 // and select other items ...
147 if (g_SelectionBeforePopup
== wxID_NONE
)
149 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
150 event
.SetInt( curSelection
);
151 event
.SetString( combo
->GetStringSelection() );
152 event
.SetEventObject( combo
);
153 combo
->GetEventHandler()->ProcessEvent( event
);
155 // for consistency with the other ports, don't generate text update
156 // events while the user is browsing the combobox neither
157 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
158 event2
.SetString( combo
->GetValue() );
159 event2
.SetEventObject( combo
);
160 combo
->GetEventHandler()->ProcessEvent( event2
);
168 gtkcombobox_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
170 if (!combo
->m_hasVMT
) return;
172 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
173 event
.SetString( combo
->GetValue() );
174 event
.SetEventObject( combo
);
175 combo
->GetEventHandler()->ProcessEvent( event
);
181 gtkcombobox_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
183 if (!combo
->m_hasVMT
) return;
185 if (combo
->GetSelection() == -1)
188 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
189 event
.SetInt( combo
->GetSelection() );
190 event
.SetString( combo
->GetStringSelection() );
191 event
.SetEventObject( combo
);
192 combo
->GetEventHandler()->ProcessEvent( event
);
198 //-----------------------------------------------------------------------------
200 //-----------------------------------------------------------------------------
202 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
204 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
205 EVT_SIZE(wxComboBox::OnSize
)
206 EVT_CHAR(wxComboBox::OnChar
)
208 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
209 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
210 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
211 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
212 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
213 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
214 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
216 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
217 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
218 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
219 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
220 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
221 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
222 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
225 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
226 const wxString
& value
,
227 const wxPoint
& pos
, const wxSize
& size
,
228 const wxArrayString
& choices
,
229 long style
, const wxValidator
& validator
,
230 const wxString
& name
)
232 wxCArrayString
chs(choices
);
234 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
235 chs
.GetStrings(), style
, validator
, name
);
238 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
239 const wxPoint
& pos
, const wxSize
& size
,
240 int n
, const wxString choices
[],
241 long style
, const wxValidator
& validator
,
242 const wxString
& name
)
244 m_ignoreNextUpdate
= false;
247 if (!PreCreation( parent
, pos
, size
) ||
248 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
250 wxFAIL_MSG( wxT("wxComboBox creation failed") );
255 if (!gtk_check_version(2,4,0))
257 m_widget
= gtk_combo_box_entry_new_text();
258 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
260 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget
)->child
), TRUE
);
262 for (int i
= 0; i
< n
; i
++)
264 gtk_combo_box_append_text( combobox
, wxGTK_CONV( choices
[i
] ) );
266 m_clientDataList
.Append( (wxObject
*)NULL
);
267 m_clientObjectList
.Append( (wxObject
*)NULL
);
273 m_widget
= gtk_combo_new();
274 GtkCombo
* combo
= GTK_COMBO(m_widget
);
276 // Disable GTK's broken events ...
277 g_signal_handler_disconnect (combo
->entry
, combo
->entry_change_id
);
278 // ... and add surrogate handler.
279 combo
->entry_change_id
= g_signal_connect (combo
->entry
, "changed",
280 G_CALLBACK (gtkcombo_dummy_callback
),
283 // make it more useable
284 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
286 // and case-sensitive
287 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
289 if (style
& wxNO_BORDER
)
290 g_object_set (combo
->entry
, "has-frame", FALSE
, NULL
);
292 GtkWidget
*list
= combo
->list
;
294 for (int i
= 0; i
< n
; i
++)
296 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
298 m_clientDataList
.Append( (wxObject
*)NULL
);
299 m_clientObjectList
.Append( (wxObject
*)NULL
);
301 gtk_container_add( GTK_CONTAINER(list
), list_item
);
303 gtk_widget_show( list_item
);
308 m_parent
->DoAddChild( this );
310 GtkEntry
*entry
= NULL
;
312 if (!gtk_check_version(2,4,0))
313 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
316 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
318 m_focusWidget
= GTK_WIDGET( entry
);
323 if (!gtk_check_version(2,4,0))
324 ConnectWidget( m_widget
);
327 ConnectWidget( GTK_COMBO(m_widget
)->button
);
330 if (!gtk_check_version(2,4,0))
332 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
334 if (style
& wxCB_READONLY
)
335 gtk_entry_set_editable( entry
, FALSE
);
337 g_signal_connect_after (entry
, "changed",
338 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
340 g_signal_connect_after (m_widget
, "changed",
341 G_CALLBACK (gtkcombobox_changed_callback
), this);
347 GtkCombo
*combo
= GTK_COMBO(m_widget
);
348 // MSW's combo box shows the value and the selection is -1
349 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
350 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
352 if (style
& wxCB_READONLY
)
353 gtk_entry_set_editable( entry
, FALSE
);
355 // "show" and "hide" events are generated when user click on the combobox button which popups a list
356 // this list is the "popwin" gtk widget
357 g_signal_connect (GTK_COMBO(combo
)->popwin
, "hide",
358 G_CALLBACK (gtkcombo_popup_hide_callback
), this);
359 g_signal_connect (GTK_COMBO(combo
)->popwin
, "show",
360 G_CALLBACK (gtkcombo_popup_show_callback
), this);
361 g_signal_connect_after (combo
->list
, "select-child",
362 G_CALLBACK (gtkcombo_combo_select_child_callback
),
364 g_signal_connect_after (entry
, "changed",
365 G_CALLBACK (gtkcombo_text_changed_callback
), this);
368 SetInitialSize(size
); // need this too because this is a wxControlWithItems
373 wxComboBox::~wxComboBox()
375 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
378 wxClientData
*cd
= (wxClientData
*)node
->GetData();
380 node
= node
->GetNext();
382 m_clientObjectList
.Clear();
384 m_clientDataList
.Clear();
387 void wxComboBox::SetFocus()
391 // don't do anything if we already have focus
395 gtk_widget_grab_focus( m_focusWidget
);
398 int wxComboBox::DoAppend( const wxString
&item
)
400 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
403 if (!gtk_check_version(2,4,0))
405 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
406 gtk_combo_box_append_text( combobox
, wxGTK_CONV( item
) );
413 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
414 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
416 gtk_container_add( GTK_CONTAINER(list
), list_item
);
418 if (GTK_WIDGET_REALIZED(m_widget
))
420 gtk_widget_realize( list_item
);
421 gtk_widget_realize( GTK_BIN(list_item
)->child
);
424 // Apply current widget style to the new list_item
425 GtkRcStyle
*style
= CreateWidgetStyle();
428 gtk_widget_modify_style(list_item
, style
);
429 GtkBin
*bin
= GTK_BIN( list_item
);
430 GtkWidget
*label
= bin
->child
;
431 gtk_widget_modify_style( label
, style
);
432 gtk_rc_style_unref( style
);
435 gtk_widget_show( list_item
);
440 const unsigned int count
= GetCount();
442 if ( m_clientDataList
.GetCount() < count
)
443 m_clientDataList
.Append( (wxObject
*) NULL
);
444 if ( m_clientObjectList
.GetCount() < count
)
445 m_clientObjectList
.Append( (wxObject
*) NULL
);
447 InvalidateBestSize();
452 int wxComboBox::DoInsert(const wxString
&item
, unsigned int pos
)
454 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
455 wxT("can't insert into sorted list"));
457 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
458 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("invalid index") );
460 unsigned int count
= GetCount();
466 if (!gtk_check_version(2,4,0))
468 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
469 gtk_combo_box_insert_text( combobox
, pos
, wxGTK_CONV( item
) );
476 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
477 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
479 GList
*gitem_list
= g_list_alloc ();
480 gitem_list
->data
= list_item
;
481 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
483 if (GTK_WIDGET_REALIZED(m_widget
))
485 gtk_widget_realize( list_item
);
486 gtk_widget_realize( GTK_BIN(list_item
)->child
);
491 gtk_widget_show( list_item
);
498 if ( m_clientDataList
.GetCount() < count
)
499 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
500 if ( m_clientObjectList
.GetCount() < count
)
501 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
503 InvalidateBestSize();
508 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
510 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
512 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
515 node
->SetData( (wxObject
*) clientData
);
518 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
520 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
522 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
524 return node
? node
->GetData() : NULL
;
527 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
529 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
531 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
534 // wxItemContainer already deletes data for us
536 node
->SetData( (wxObject
*) clientData
);
539 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
541 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
543 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
545 return node
? (wxClientData
*) node
->GetData() : NULL
;
548 void wxComboBox::Clear()
550 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
555 if (!gtk_check_version(2,4,0))
557 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
558 const unsigned int count
= GetCount();
559 for (unsigned int i
= 0; i
< count
; i
++)
560 gtk_combo_box_remove_text( combobox
, 0 );
563 #endif // __WXGTK24__
565 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
566 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
569 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
572 wxClientData
*cd
= (wxClientData
*)node
->GetData();
574 node
= node
->GetNext();
576 m_clientObjectList
.Clear();
578 m_clientDataList
.Clear();
582 InvalidateBestSize();
585 void wxComboBox::Delete(unsigned int n
)
587 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
590 if (!gtk_check_version(2,4,0))
592 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
594 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
595 gtk_combo_box_remove_text( combobox
, n
);
600 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
602 GList
*child
= g_list_nth( listbox
->children
, n
);
606 wxFAIL_MSG(wxT("wrong index"));
612 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
613 gtk_list_remove_items( listbox
, list
);
619 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
622 wxClientData
*cd
= (wxClientData
*)node
->GetData();
624 m_clientObjectList
.Erase( node
);
627 node
= m_clientDataList
.Item( n
);
629 m_clientDataList
.Erase( node
);
631 InvalidateBestSize();
634 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
636 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
639 if (!gtk_check_version(2,4,0))
641 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
642 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
644 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
646 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
648 GValue value
= { 0, };
649 g_value_init( &value
, G_TYPE_STRING
);
650 g_value_set_string( &value
, wxGTK_CONV( text
) );
651 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, 0, &value
);
652 g_value_unset( &value
);
658 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
660 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
663 GtkBin
*bin
= GTK_BIN( child
->data
);
664 GtkLabel
*label
= GTK_LABEL( bin
->child
);
665 gtk_label_set_text(label
, wxGTK_CONV(text
));
669 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
673 InvalidateBestSize();
676 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
678 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
681 if (!gtk_check_version(2,4,0))
683 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
684 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
686 gtk_tree_model_get_iter_first( model
, &iter
);
687 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
692 GValue value
= { 0, };
693 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
694 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
695 g_value_unset( &value
);
697 if (item
.IsSameAs( str
, bCase
) )
702 } while (gtk_tree_model_iter_next( model
, &iter
));
707 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
709 GList
*child
= GTK_LIST(list
)->children
;
713 GtkBin
*bin
= GTK_BIN( child
->data
);
714 GtkLabel
*label
= GTK_LABEL( bin
->child
);
715 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
717 if (item
.IsSameAs( str
, bCase
) )
728 int wxComboBox::GetSelection() const
731 if (!gtk_check_version(2,4,0))
733 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
734 return gtk_combo_box_get_active( combobox
);
738 // if the popup is currently opened, use the selection as it had been
739 // before it dropped down
740 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
741 : g_SelectionBeforePopup
;
744 int wxComboBox::GetCurrentSelection() const
746 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
749 if (!gtk_check_version(2,4,0))
751 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
752 return gtk_combo_box_get_active( combobox
);
757 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
759 GList
*selection
= GTK_LIST(list
)->selection
;
762 GList
*child
= GTK_LIST(list
)->children
;
766 if (child
->data
== selection
->data
) return count
;
776 wxString
wxComboBox::GetString(unsigned int n
) const
778 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
783 if (!gtk_check_version(2,4,0))
785 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
786 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
788 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
790 GValue value
= { 0, };
791 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
792 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
793 g_value_unset( &value
);
800 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
802 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
805 GtkBin
*bin
= GTK_BIN( child
->data
);
806 GtkLabel
*label
= GTK_LABEL( bin
->child
);
807 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
811 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
818 wxString
wxComboBox::GetStringSelection() const
820 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
823 if (!gtk_check_version(2,4,0))
825 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
826 int sel
= gtk_combo_box_get_active( combobox
);
828 return wxEmptyString
;
829 return GetString(sel
);
834 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
836 GList
*selection
= GTK_LIST(list
)->selection
;
839 GtkBin
*bin
= GTK_BIN( selection
->data
);
840 GtkLabel
*label
= GTK_LABEL( bin
->child
);
841 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
845 wxFAIL_MSG( wxT("wxComboBox: no selection") );
848 return wxEmptyString
;
851 unsigned int wxComboBox::GetCount() const
853 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
856 if (!gtk_check_version(2,4,0))
858 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
859 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
861 gtk_tree_model_get_iter_first( model
, &iter
);
862 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
864 unsigned int ret
= 1;
865 while (gtk_tree_model_iter_next( model
, &iter
))
872 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
874 GList
*child
= GTK_LIST(list
)->children
;
875 unsigned int count
= 0;
887 void wxComboBox::SetSelection( int n
)
889 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
894 if (!gtk_check_version(2,4,0))
896 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
897 gtk_combo_box_set_active( combobox
, n
);
902 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
903 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
904 gtk_list_select_item( GTK_LIST(list
), n
);
911 wxString
wxComboBox::GetValue() const
913 GtkEntry
*entry
= NULL
;
915 if (!gtk_check_version(2,4,0))
916 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
919 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
921 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
924 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
927 printf( "%d ", (int) (c
) );
935 void wxComboBox::SetValue( const wxString
& value
)
937 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
939 GtkEntry
*entry
= NULL
;
941 if (!gtk_check_version(2,4,0))
942 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
945 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
948 if (!value
.IsNull()) tmp
= value
;
949 gtk_entry_set_text( entry
, wxGTK_CONV( tmp
) );
951 InvalidateBestSize();
954 void wxComboBox::Copy()
956 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
958 GtkEntry
*entry
= NULL
;
960 if (!gtk_check_version(2,4,0))
961 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
964 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
966 gtk_editable_copy_clipboard(GTK_EDITABLE(entry
));
969 void wxComboBox::Cut()
971 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
973 GtkEntry
*entry
= NULL
;
975 if (!gtk_check_version(2,4,0))
976 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
979 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
981 gtk_editable_cut_clipboard(GTK_EDITABLE(entry
));
984 void wxComboBox::Paste()
986 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
988 GtkEntry
*entry
= NULL
;
990 if (!gtk_check_version(2,4,0))
991 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
994 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
996 gtk_editable_paste_clipboard(GTK_EDITABLE(entry
));
999 void wxComboBox::Undo()
1004 void wxComboBox::Redo()
1009 void wxComboBox::SelectAll()
1011 SetSelection(0, GetLastPosition());
1014 bool wxComboBox::CanUndo() const
1020 bool wxComboBox::CanRedo() const
1026 bool wxComboBox::HasSelection() const
1029 GetSelection(&from
, &to
);
1033 bool wxComboBox::CanCopy() const
1035 // Can copy if there's a selection
1036 return HasSelection();
1039 bool wxComboBox::CanCut() const
1041 return CanCopy() && IsEditable();
1044 bool wxComboBox::CanPaste() const
1046 // TODO: check for text on the clipboard
1047 return IsEditable() ;
1050 bool wxComboBox::IsEditable() const
1052 return !HasFlag(wxCB_READONLY
);
1056 void wxComboBox::SetInsertionPoint( long pos
)
1058 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1060 if ( pos
== GetLastPosition() )
1063 GtkEntry
*entry
= NULL
;
1065 if (!gtk_check_version(2,4,0))
1066 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1069 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1071 gtk_entry_set_position( entry
, (int)pos
);
1074 long wxComboBox::GetInsertionPoint() const
1076 GtkEntry
*entry
= NULL
;
1078 if (!gtk_check_version(2,4,0))
1079 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1082 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1084 return (long) gtk_editable_get_position(GTK_EDITABLE(entry
));
1087 wxTextPos
wxComboBox::GetLastPosition() const
1089 GtkEntry
*entry
= NULL
;
1091 if (!gtk_check_version(2,4,0))
1092 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1095 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1097 int pos
= entry
->text_length
;
1098 return (long) pos
-1;
1101 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
1103 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1105 GtkEntry
*entry
= NULL
;
1107 if (!gtk_check_version(2,4,0))
1108 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1111 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1113 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1114 if (value
.IsNull()) return;
1115 gint pos
= (gint
)to
;
1118 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
1119 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
1121 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.length(), &pos
);
1125 void wxComboBox::SetSelection( long from
, long to
)
1127 GtkEntry
*entry
= NULL
;
1129 if (!gtk_check_version(2,4,0))
1130 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1133 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1135 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1138 void wxComboBox::GetSelection( long* from
, long* to
) const
1140 GtkEntry
*entry
= NULL
;
1142 if (!gtk_check_version(2,4,0))
1143 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1146 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1150 GtkEditable
*editable
= GTK_EDITABLE(entry
);
1152 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
1158 void wxComboBox::SetEditable( bool editable
)
1160 GtkEntry
*entry
= NULL
;
1162 if (!gtk_check_version(2,4,0))
1163 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1166 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1168 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
1171 void wxComboBox::OnChar( wxKeyEvent
&event
)
1173 if ( event
.GetKeyCode() == WXK_RETURN
)
1175 // GTK automatically selects an item if its in the list
1176 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1177 eventEnter
.SetString( GetValue() );
1178 eventEnter
.SetInt( GetSelection() );
1179 eventEnter
.SetEventObject( this );
1181 if (!GetEventHandler()->ProcessEvent( eventEnter
))
1183 // This will invoke the dialog default action, such
1184 // as the clicking the default button.
1186 wxWindow
*top_frame
= m_parent
;
1187 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1188 top_frame
= top_frame
->GetParent();
1190 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1192 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1194 if (window
->default_widget
)
1195 gtk_widget_activate (window
->default_widget
);
1199 // Catch GTK event so that GTK doesn't open the drop
1200 // down list upon RETURN.
1207 void wxComboBox::DisableEvents()
1210 if (!gtk_check_version(2,4,0))
1212 g_signal_handlers_disconnect_by_func (GTK_BIN(m_widget
)->child
,
1213 (gpointer
)gtkcombobox_text_changed_callback
, this);
1215 g_signal_handlers_disconnect_by_func (m_widget
,
1216 (gpointer
)gtkcombobox_changed_callback
, this);
1221 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->list
,
1222 (gpointer
) gtkcombo_combo_select_child_callback
, this);
1224 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->entry
,
1225 (gpointer
) gtkcombo_text_changed_callback
, this);
1229 void wxComboBox::EnableEvents()
1232 if (!gtk_check_version(2,4,0))
1234 g_signal_connect_after (GTK_BIN(m_widget
)->child
, "changed",
1235 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
1237 g_signal_connect_after (m_widget
, "changed",
1238 G_CALLBACK (gtkcombobox_changed_callback
), this);
1243 g_signal_connect_after (GTK_COMBO(m_widget
)->list
, "select-child",
1244 G_CALLBACK (gtkcombo_combo_select_child_callback
),
1246 g_signal_connect_after (GTK_COMBO(m_widget
)->entry
, "changed",
1247 G_CALLBACK (gtkcombo_text_changed_callback
),
1252 void wxComboBox::OnSize( wxSizeEvent
&event
)
1255 if (!gtk_check_version(2,4,0))
1262 // NB: In some situations (e.g. on non-first page of a wizard, if the
1263 // size used is default size), GtkCombo widget is resized correctly,
1264 // but it's look is not updated, it's rendered as if it was much wider.
1265 // No other widgets are affected, so it looks like a bug in GTK+.
1266 // Manually requesting resize calculation (as gtk_pizza_set_size does)
1268 if (GTK_WIDGET_VISIBLE(m_widget
))
1269 gtk_widget_queue_resize(m_widget
);
1275 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1278 if (!gtk_check_version(2,4,0))
1285 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
1287 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
1288 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
1290 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
1291 GList
*child
= list
->children
;
1294 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1296 GtkBin
*bin
= GTK_BIN(child
->data
);
1297 gtk_widget_modify_style( bin
->child
, style
);
1299 child
= child
->next
;
1304 GtkWidget
* wxComboBox::GetConnectWidget()
1306 GtkEntry
*entry
= NULL
;
1308 if (!gtk_check_version(2,4,0))
1309 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1312 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1314 return GTK_WIDGET( entry
);
1317 GdkWindow
*wxComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
1320 if (!gtk_check_version(2,4,0))
1322 wxUnusedVar(windows
);
1324 return GTK_ENTRY(GTK_BIN(m_widget
)->child
)->text_area
;
1329 windows
.push_back(GTK_ENTRY(GTK_COMBO(m_widget
)->entry
)->text_area
);
1330 windows
.push_back(GTK_COMBO(m_widget
)->button
->window
);
1332 // indicate that we return multiple windows in the windows array
1337 wxSize
wxComboBox::DoGetBestSize() const
1339 wxSize
ret( wxControl::DoGetBestSize() );
1341 // we know better our horizontal extent: it depends on the longest string
1346 unsigned int count
= GetCount();
1347 for ( unsigned int n
= 0; n
< count
; n
++ )
1349 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
1350 if ( width
> ret
.x
)
1355 // empty combobox should have some reasonable default size too
1365 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1368 if (!gtk_check_version(2,4,0))
1369 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
1372 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
1375 // ----------------------------------------------------------------------------
1376 // standard event handling
1377 // ----------------------------------------------------------------------------
1379 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
1384 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1389 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1394 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1399 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1404 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1407 GetSelection(& from
, & to
);
1408 if (from
!= -1 && to
!= -1)
1412 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1414 SetSelection(-1, -1);
1417 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1419 event
.Enable( CanCut() );
1422 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1424 event
.Enable( CanCopy() );
1427 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1429 event
.Enable( CanPaste() );
1432 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1434 event
.Enable( CanUndo() );
1437 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1439 event
.Enable( CanRedo() );
1442 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1444 event
.Enable(HasSelection() && IsEditable()) ;
1447 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1449 event
.Enable(GetLastPosition() > 0);