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
;
951 gtk_entry_set_text( entry
, wxGTK_CONV( tmp
) );
954 InvalidateBestSize();
957 void wxComboBox::Copy()
959 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
961 GtkEntry
*entry
= NULL
;
963 if (!gtk_check_version(2,4,0))
964 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
967 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
969 gtk_editable_copy_clipboard(GTK_EDITABLE(entry
));
972 void wxComboBox::Cut()
974 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
976 GtkEntry
*entry
= NULL
;
978 if (!gtk_check_version(2,4,0))
979 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
982 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
984 gtk_editable_cut_clipboard(GTK_EDITABLE(entry
));
987 void wxComboBox::Paste()
989 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
991 GtkEntry
*entry
= NULL
;
993 if (!gtk_check_version(2,4,0))
994 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
997 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
999 gtk_editable_paste_clipboard(GTK_EDITABLE(entry
));
1002 void wxComboBox::Undo()
1007 void wxComboBox::Redo()
1012 void wxComboBox::SelectAll()
1014 SetSelection(0, GetLastPosition());
1017 bool wxComboBox::CanUndo() const
1023 bool wxComboBox::CanRedo() const
1029 bool wxComboBox::HasSelection() const
1032 GetSelection(&from
, &to
);
1036 bool wxComboBox::CanCopy() const
1038 // Can copy if there's a selection
1039 return HasSelection();
1042 bool wxComboBox::CanCut() const
1044 return CanCopy() && IsEditable();
1047 bool wxComboBox::CanPaste() const
1049 // TODO: check for text on the clipboard
1050 return IsEditable() ;
1053 bool wxComboBox::IsEditable() const
1055 return !HasFlag(wxCB_READONLY
);
1059 void wxComboBox::SetInsertionPoint( long pos
)
1061 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1063 if ( pos
== GetLastPosition() )
1066 GtkEntry
*entry
= NULL
;
1068 if (!gtk_check_version(2,4,0))
1069 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1072 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1074 gtk_entry_set_position( entry
, (int)pos
);
1077 long wxComboBox::GetInsertionPoint() const
1079 GtkEntry
*entry
= NULL
;
1081 if (!gtk_check_version(2,4,0))
1082 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1085 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1087 return (long) gtk_editable_get_position(GTK_EDITABLE(entry
));
1090 wxTextPos
wxComboBox::GetLastPosition() const
1092 GtkEntry
*entry
= NULL
;
1094 if (!gtk_check_version(2,4,0))
1095 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1098 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1100 int pos
= entry
->text_length
;
1101 return (long) pos
-1;
1104 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
1106 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1108 GtkEntry
*entry
= NULL
;
1110 if (!gtk_check_version(2,4,0))
1111 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1114 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1116 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1117 if (value
.IsNull()) return;
1118 gint pos
= (gint
)to
;
1121 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
1122 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
1124 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.length(), &pos
);
1128 void wxComboBox::SetSelection( long from
, long to
)
1130 GtkEntry
*entry
= NULL
;
1132 if (!gtk_check_version(2,4,0))
1133 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1136 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1138 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1141 void wxComboBox::GetSelection( long* from
, long* to
) const
1143 GtkEntry
*entry
= NULL
;
1145 if (!gtk_check_version(2,4,0))
1146 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1149 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1153 GtkEditable
*editable
= GTK_EDITABLE(entry
);
1155 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
1161 void wxComboBox::SetEditable( bool editable
)
1163 GtkEntry
*entry
= NULL
;
1165 if (!gtk_check_version(2,4,0))
1166 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1169 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1171 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
1174 void wxComboBox::OnChar( wxKeyEvent
&event
)
1176 if ( event
.GetKeyCode() == WXK_RETURN
)
1178 // GTK automatically selects an item if its in the list
1179 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1180 eventEnter
.SetString( GetValue() );
1181 eventEnter
.SetInt( GetSelection() );
1182 eventEnter
.SetEventObject( this );
1184 if (!GetEventHandler()->ProcessEvent( eventEnter
))
1186 // This will invoke the dialog default action, such
1187 // as the clicking the default button.
1189 wxWindow
*top_frame
= m_parent
;
1190 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1191 top_frame
= top_frame
->GetParent();
1193 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1195 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1197 if (window
->default_widget
)
1198 gtk_widget_activate (window
->default_widget
);
1202 // Catch GTK event so that GTK doesn't open the drop
1203 // down list upon RETURN.
1210 void wxComboBox::DisableEvents()
1213 if (!gtk_check_version(2,4,0))
1215 g_signal_handlers_disconnect_by_func (GTK_BIN(m_widget
)->child
,
1216 (gpointer
)gtkcombobox_text_changed_callback
, this);
1218 g_signal_handlers_disconnect_by_func (m_widget
,
1219 (gpointer
)gtkcombobox_changed_callback
, this);
1224 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->list
,
1225 (gpointer
) gtkcombo_combo_select_child_callback
, this);
1227 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->entry
,
1228 (gpointer
) gtkcombo_text_changed_callback
, this);
1232 void wxComboBox::EnableEvents()
1235 if (!gtk_check_version(2,4,0))
1237 g_signal_connect_after (GTK_BIN(m_widget
)->child
, "changed",
1238 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
1240 g_signal_connect_after (m_widget
, "changed",
1241 G_CALLBACK (gtkcombobox_changed_callback
), this);
1246 g_signal_connect_after (GTK_COMBO(m_widget
)->list
, "select-child",
1247 G_CALLBACK (gtkcombo_combo_select_child_callback
),
1249 g_signal_connect_after (GTK_COMBO(m_widget
)->entry
, "changed",
1250 G_CALLBACK (gtkcombo_text_changed_callback
),
1255 void wxComboBox::OnSize( wxSizeEvent
&event
)
1258 if (!gtk_check_version(2,4,0))
1265 // NB: In some situations (e.g. on non-first page of a wizard, if the
1266 // size used is default size), GtkCombo widget is resized correctly,
1267 // but it's look is not updated, it's rendered as if it was much wider.
1268 // No other widgets are affected, so it looks like a bug in GTK+.
1269 // Manually requesting resize calculation (as gtk_pizza_set_size does)
1271 if (GTK_WIDGET_VISIBLE(m_widget
))
1272 gtk_widget_queue_resize(m_widget
);
1278 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1281 if (!gtk_check_version(2,4,0))
1288 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
1290 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
1291 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
1293 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
1294 GList
*child
= list
->children
;
1297 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1299 GtkBin
*bin
= GTK_BIN(child
->data
);
1300 gtk_widget_modify_style( bin
->child
, style
);
1302 child
= child
->next
;
1307 GtkWidget
* wxComboBox::GetConnectWidget()
1309 GtkEntry
*entry
= NULL
;
1311 if (!gtk_check_version(2,4,0))
1312 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1315 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1317 return GTK_WIDGET( entry
);
1320 GdkWindow
*wxComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
1323 if (!gtk_check_version(2,4,0))
1325 wxUnusedVar(windows
);
1327 return GTK_ENTRY(GTK_BIN(m_widget
)->child
)->text_area
;
1332 windows
.push_back(GTK_ENTRY(GTK_COMBO(m_widget
)->entry
)->text_area
);
1333 windows
.push_back(GTK_COMBO(m_widget
)->button
->window
);
1335 // indicate that we return multiple windows in the windows array
1340 wxSize
wxComboBox::DoGetBestSize() const
1342 wxSize
ret( wxControl::DoGetBestSize() );
1344 // we know better our horizontal extent: it depends on the longest string
1349 unsigned int count
= GetCount();
1350 for ( unsigned int n
= 0; n
< count
; n
++ )
1352 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
1353 if ( width
> ret
.x
)
1358 // empty combobox should have some reasonable default size too
1368 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1371 if (!gtk_check_version(2,4,0))
1372 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
1375 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
1378 // ----------------------------------------------------------------------------
1379 // standard event handling
1380 // ----------------------------------------------------------------------------
1382 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
1387 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1392 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1397 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1402 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1407 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1410 GetSelection(& from
, & to
);
1411 if (from
!= -1 && to
!= -1)
1415 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1417 SetSelection(-1, -1);
1420 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1422 event
.Enable( CanCut() );
1425 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1427 event
.Enable( CanCopy() );
1430 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1432 event
.Enable( CanPaste() );
1435 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1437 event
.Enable( CanUndo() );
1440 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1442 event
.Enable( CanRedo() );
1445 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1447 event
.Enable(HasSelection() && IsEditable()) ;
1450 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1452 event
.Enable(GetLastPosition() > 0);