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 (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 gtkcombo_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
74 gtkcombo_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 gtkcombo_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 gtkcombo_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 g_signal_handlers_disconnect_by_func (GTK_COMBO (combo
->GetHandle())->entry
,
140 (gpointer
) gtkcombo_text_changed_callback
,
142 combo
->SetValue( combo
->GetStringSelection() );
143 g_signal_connect_after (GTK_COMBO (combo
->GetHandle())->entry
, "changed",
144 G_CALLBACK (gtkcombo_text_changed_callback
), combo
);
146 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
147 // because when combobox popup is shown, gtkcombo_combo_select_child_callback is
148 // called each times the mouse is over an item with a pressed button so a lot
149 // of SELECTED event could be generated if the user keep the mouse button down
150 // and select other items ...
151 if (g_SelectionBeforePopup
== wxID_NONE
)
153 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
154 event
.SetInt( curSelection
);
155 event
.SetString( combo
->GetStringSelection() );
156 event
.SetEventObject( combo
);
157 combo
->GetEventHandler()->ProcessEvent( event
);
159 // for consistency with the other ports, don't generate text update
160 // events while the user is browsing the combobox neither
161 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
162 event2
.SetString( combo
->GetValue() );
163 event2
.SetEventObject( combo
);
164 combo
->GetEventHandler()->ProcessEvent( event2
);
172 gtkcombobox_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
174 if (g_isIdle
) wxapp_install_idle_handler();
176 if (!combo
->m_hasVMT
) return;
178 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
179 event
.SetString( combo
->GetValue() );
180 event
.SetEventObject( combo
);
181 combo
->GetEventHandler()->ProcessEvent( event
);
187 gtkcombobox_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
189 if (g_isIdle
) wxapp_install_idle_handler();
191 if (!combo
->m_hasVMT
) return;
193 if (combo
->GetSelection() == -1)
196 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
197 event
.SetInt( combo
->GetSelection() );
198 event
.SetString( combo
->GetStringSelection() );
199 event
.SetEventObject( combo
);
200 combo
->GetEventHandler()->ProcessEvent( event
);
205 //-----------------------------------------------------------------------------
207 //-----------------------------------------------------------------------------
209 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
211 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
212 EVT_SIZE(wxComboBox::OnSize
)
213 EVT_CHAR(wxComboBox::OnChar
)
215 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
216 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
217 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
218 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
219 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
220 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
221 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
223 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
224 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
225 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
226 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
227 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
228 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
229 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
232 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
233 const wxString
& value
,
234 const wxPoint
& pos
, const wxSize
& size
,
235 const wxArrayString
& choices
,
236 long style
, const wxValidator
& validator
,
237 const wxString
& name
)
239 wxCArrayString
chs(choices
);
241 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
242 chs
.GetStrings(), style
, validator
, name
);
245 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
246 const wxPoint
& pos
, const wxSize
& size
,
247 int n
, const wxString choices
[],
248 long style
, const wxValidator
& validator
,
249 const wxString
& name
)
251 m_ignoreNextUpdate
= false;
253 m_acceptsFocus
= true;
256 if (!PreCreation( parent
, pos
, size
) ||
257 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
259 wxFAIL_MSG( wxT("wxComboBox creation failed") );
264 if (!gtk_check_version(2,4,0))
266 m_widget
= gtk_combo_box_entry_new_text();
267 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
269 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget
)->child
), TRUE
);
271 for (int i
= 0; i
< n
; i
++)
273 gtk_combo_box_append_text( combobox
, wxGTK_CONV( choices
[i
] ) );
275 m_clientDataList
.Append( (wxObject
*)NULL
);
276 m_clientObjectList
.Append( (wxObject
*)NULL
);
282 m_widget
= gtk_combo_new();
283 GtkCombo
* combo
= GTK_COMBO(m_widget
);
285 // Disable GTK's broken events ...
286 g_signal_handler_disconnect (combo
->entry
, combo
->entry_change_id
);
287 // ... and add surrogate handler.
288 combo
->entry_change_id
= g_signal_connect (combo
->entry
, "changed",
289 G_CALLBACK (gtkcombo_dummy_callback
),
292 // make it more useable
293 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
295 // and case-sensitive
296 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
298 if (style
& wxNO_BORDER
)
299 g_object_set (combo
->entry
, "has-frame", FALSE
, NULL
);
301 GtkWidget
*list
= combo
->list
;
303 for (int i
= 0; i
< n
; i
++)
305 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
307 m_clientDataList
.Append( (wxObject
*)NULL
);
308 m_clientObjectList
.Append( (wxObject
*)NULL
);
310 gtk_container_add( GTK_CONTAINER(list
), list_item
);
312 gtk_widget_show( list_item
);
317 m_parent
->DoAddChild( this );
319 GtkEntry
*entry
= NULL
;
321 if (!gtk_check_version(2,4,0))
322 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
325 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
327 m_focusWidget
= GTK_WIDGET( entry
);
332 if (!gtk_check_version(2,4,0))
333 ConnectWidget( m_widget
);
336 ConnectWidget( GTK_COMBO(m_widget
)->button
);
339 if (!gtk_check_version(2,4,0))
341 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
343 if (style
& wxCB_READONLY
)
344 gtk_entry_set_editable( entry
, FALSE
);
346 g_signal_connect_after (entry
, "changed",
347 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
349 g_signal_connect_after (m_widget
, "changed",
350 G_CALLBACK (gtkcombobox_changed_callback
), this);
355 GtkCombo
*combo
= GTK_COMBO(m_widget
);
356 // MSW's combo box shows the value and the selection is -1
357 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
358 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
360 if (style
& wxCB_READONLY
)
361 gtk_entry_set_editable( entry
, FALSE
);
363 // "show" and "hide" events are generated when user click on the combobox button which popups a list
364 // this list is the "popwin" gtk widget
365 g_signal_connect (GTK_COMBO(combo
)->popwin
, "hide",
366 G_CALLBACK (gtkcombo_popup_hide_callback
), this);
367 g_signal_connect (GTK_COMBO(combo
)->popwin
, "show",
368 G_CALLBACK (gtkcombo_popup_show_callback
), this);
369 g_signal_connect_after (combo
->list
, "select-child",
370 G_CALLBACK (gtkcombo_combo_select_child_callback
),
372 g_signal_connect_after (entry
, "changed",
373 G_CALLBACK (gtkcombo_text_changed_callback
), this);
375 // This is required for tool bar support
376 // Doesn't currently work
377 // wxSize setsize = GetSize();
378 // gtk_widget_set_size_request( m_widget, setsize.x, setsize.y );
381 SetInitialSize(size
); // need this too because this is a wxControlWithItems
387 wxComboBox::~wxComboBox()
389 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
392 wxClientData
*cd
= (wxClientData
*)node
->GetData();
394 node
= node
->GetNext();
396 m_clientObjectList
.Clear();
398 m_clientDataList
.Clear();
401 void wxComboBox::SetFocus()
405 // don't do anything if we already have focus
409 gtk_widget_grab_focus( m_focusWidget
);
412 int wxComboBox::DoAppend( const wxString
&item
)
414 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
417 if (!gtk_check_version(2,4,0))
419 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
420 gtk_combo_box_append_text( combobox
, wxGTK_CONV( item
) );
427 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
428 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
430 gtk_container_add( GTK_CONTAINER(list
), list_item
);
432 if (GTK_WIDGET_REALIZED(m_widget
))
434 gtk_widget_realize( list_item
);
435 gtk_widget_realize( GTK_BIN(list_item
)->child
);
438 // Apply current widget style to the new list_item
439 GtkRcStyle
*style
= CreateWidgetStyle();
442 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
443 GtkBin
*bin
= GTK_BIN( list_item
);
444 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
445 gtk_widget_modify_style( label
, style
);
446 gtk_rc_style_unref( style
);
449 gtk_widget_show( list_item
);
454 const unsigned int count
= GetCount();
456 if ( m_clientDataList
.GetCount() < count
)
457 m_clientDataList
.Append( (wxObject
*) NULL
);
458 if ( m_clientObjectList
.GetCount() < count
)
459 m_clientObjectList
.Append( (wxObject
*) NULL
);
461 InvalidateBestSize();
466 int wxComboBox::DoInsert(const wxString
&item
, unsigned int pos
)
468 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
469 wxT("can't insert into sorted list"));
471 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
472 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("invalid index") );
474 unsigned int count
= GetCount();
480 if (!gtk_check_version(2,4,0))
482 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
483 gtk_combo_box_insert_text( combobox
, pos
, wxGTK_CONV( item
) );
490 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
491 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
493 GList
*gitem_list
= g_list_alloc ();
494 gitem_list
->data
= list_item
;
495 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
497 if (GTK_WIDGET_REALIZED(m_widget
))
499 gtk_widget_realize( list_item
);
500 gtk_widget_realize( GTK_BIN(list_item
)->child
);
505 gtk_widget_show( list_item
);
512 if ( m_clientDataList
.GetCount() < count
)
513 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
514 if ( m_clientObjectList
.GetCount() < count
)
515 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
517 InvalidateBestSize();
522 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
524 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
526 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
529 node
->SetData( (wxObject
*) clientData
);
532 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
534 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
536 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
538 return node
? node
->GetData() : NULL
;
541 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
543 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
545 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
548 // wxItemContainer already deletes data for us
550 node
->SetData( (wxObject
*) clientData
);
553 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
555 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
557 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
559 return node
? (wxClientData
*) node
->GetData() : NULL
;
562 void wxComboBox::Clear()
564 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
569 if (!gtk_check_version(2,4,0))
571 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
572 const unsigned int count
= GetCount();
573 for (unsigned int i
= 0; i
< count
; i
++)
574 gtk_combo_box_remove_text( combobox
, 0 );
577 #endif // __WXGTK24__
579 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
580 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
583 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
586 wxClientData
*cd
= (wxClientData
*)node
->GetData();
588 node
= node
->GetNext();
590 m_clientObjectList
.Clear();
592 m_clientDataList
.Clear();
596 InvalidateBestSize();
599 void wxComboBox::Delete(unsigned int n
)
601 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
604 if (!gtk_check_version(2,4,0))
606 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
608 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
609 gtk_combo_box_remove_text( combobox
, n
);
614 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
616 GList
*child
= g_list_nth( listbox
->children
, n
);
620 wxFAIL_MSG(wxT("wrong index"));
626 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
627 gtk_list_remove_items( listbox
, list
);
633 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
636 wxClientData
*cd
= (wxClientData
*)node
->GetData();
638 m_clientObjectList
.Erase( node
);
641 node
= m_clientDataList
.Item( n
);
643 m_clientDataList
.Erase( node
);
645 InvalidateBestSize();
648 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
650 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
653 if (!gtk_check_version(2,4,0))
655 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
656 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
658 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
660 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
662 GValue value
= { 0, };
663 g_value_init( &value
, G_TYPE_STRING
);
664 g_value_set_string( &value
, wxGTK_CONV( text
) );
665 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, 0, &value
);
666 g_value_unset( &value
);
672 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
674 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
677 GtkBin
*bin
= GTK_BIN( child
->data
);
678 GtkLabel
*label
= GTK_LABEL( bin
->child
);
679 gtk_label_set_text(label
, wxGTK_CONV(text
));
683 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
687 InvalidateBestSize();
690 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
692 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
695 if (!gtk_check_version(2,4,0))
697 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
698 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
700 gtk_tree_model_get_iter_first( model
, &iter
);
701 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
706 GValue value
= { 0, };
707 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
708 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
709 g_value_unset( &value
);
711 if (item
.IsSameAs( str
, bCase
) )
716 } while (gtk_tree_model_iter_next( model
, &iter
));
721 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
723 GList
*child
= GTK_LIST(list
)->children
;
727 GtkBin
*bin
= GTK_BIN( child
->data
);
728 GtkLabel
*label
= GTK_LABEL( bin
->child
);
729 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
731 if (item
.IsSameAs( str
, bCase
) )
742 int wxComboBox::GetSelection() const
745 if (!gtk_check_version(2,4,0))
747 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
748 return gtk_combo_box_get_active( combobox
);
752 // if the popup is currently opened, use the selection as it had been
753 // before it dropped down
754 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
755 : g_SelectionBeforePopup
;
758 int wxComboBox::GetCurrentSelection() const
760 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
763 if (!gtk_check_version(2,4,0))
765 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
766 return gtk_combo_box_get_active( combobox
);
771 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
773 GList
*selection
= GTK_LIST(list
)->selection
;
776 GList
*child
= GTK_LIST(list
)->children
;
780 if (child
->data
== selection
->data
) return count
;
790 wxString
wxComboBox::GetString(unsigned int n
) const
792 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
797 if (!gtk_check_version(2,4,0))
799 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
800 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
802 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
804 GValue value
= { 0, };
805 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
806 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
807 g_value_unset( &value
);
814 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
816 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
819 GtkBin
*bin
= GTK_BIN( child
->data
);
820 GtkLabel
*label
= GTK_LABEL( bin
->child
);
821 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
825 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
832 wxString
wxComboBox::GetStringSelection() const
834 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
837 if (!gtk_check_version(2,4,0))
839 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
840 int sel
= gtk_combo_box_get_active( combobox
);
842 return wxEmptyString
;
843 return GetString(sel
);
848 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
850 GList
*selection
= GTK_LIST(list
)->selection
;
853 GtkBin
*bin
= GTK_BIN( selection
->data
);
854 GtkLabel
*label
= GTK_LABEL( bin
->child
);
855 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
859 wxFAIL_MSG( wxT("wxComboBox: no selection") );
862 return wxEmptyString
;
865 unsigned int wxComboBox::GetCount() const
867 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
870 if (!gtk_check_version(2,4,0))
872 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
873 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
875 gtk_tree_model_get_iter_first( model
, &iter
);
876 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
878 unsigned int ret
= 1;
879 while (gtk_tree_model_iter_next( model
, &iter
))
886 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
888 GList
*child
= GTK_LIST(list
)->children
;
889 unsigned int count
= 0;
901 void wxComboBox::SetSelection( int n
)
903 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
908 if (!gtk_check_version(2,4,0))
910 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
911 gtk_combo_box_set_active( combobox
, n
);
916 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
917 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
918 gtk_list_select_item( GTK_LIST(list
), n
);
925 wxString
wxComboBox::GetValue() const
927 GtkEntry
*entry
= NULL
;
929 if (!gtk_check_version(2,4,0))
930 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
933 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
935 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
938 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
941 printf( "%d ", (int) (c
) );
949 void wxComboBox::SetValue( const wxString
& value
)
951 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
953 GtkEntry
*entry
= NULL
;
955 if (!gtk_check_version(2,4,0))
956 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
959 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
962 if (!value
.IsNull()) tmp
= value
;
963 gtk_entry_set_text( entry
, wxGTK_CONV( tmp
) );
965 InvalidateBestSize();
968 void wxComboBox::Copy()
970 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
972 GtkEntry
*entry
= NULL
;
974 if (!gtk_check_version(2,4,0))
975 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
978 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
980 gtk_editable_copy_clipboard(GTK_EDITABLE(entry
));
983 void wxComboBox::Cut()
985 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
987 GtkEntry
*entry
= NULL
;
989 if (!gtk_check_version(2,4,0))
990 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
993 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
995 gtk_editable_cut_clipboard(GTK_EDITABLE(entry
));
998 void wxComboBox::Paste()
1000 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1002 GtkEntry
*entry
= NULL
;
1004 if (!gtk_check_version(2,4,0))
1005 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1008 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1010 gtk_editable_paste_clipboard(GTK_EDITABLE(entry
));
1013 void wxComboBox::Undo()
1018 void wxComboBox::Redo()
1023 void wxComboBox::SelectAll()
1025 SetSelection(0, GetLastPosition());
1028 bool wxComboBox::CanUndo() const
1034 bool wxComboBox::CanRedo() const
1040 bool wxComboBox::HasSelection() const
1043 GetSelection(&from
, &to
);
1047 bool wxComboBox::CanCopy() const
1049 // Can copy if there's a selection
1050 return HasSelection();
1053 bool wxComboBox::CanCut() const
1055 return CanCopy() && IsEditable();
1058 bool wxComboBox::CanPaste() const
1060 // TODO: check for text on the clipboard
1061 return IsEditable() ;
1064 bool wxComboBox::IsEditable() const
1066 return !HasFlag(wxCB_READONLY
);
1070 void wxComboBox::SetInsertionPoint( long pos
)
1072 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1074 if ( pos
== GetLastPosition() )
1077 GtkEntry
*entry
= NULL
;
1079 if (!gtk_check_version(2,4,0))
1080 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1083 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1085 gtk_entry_set_position( entry
, (int)pos
);
1088 long wxComboBox::GetInsertionPoint() const
1090 GtkEntry
*entry
= NULL
;
1092 if (!gtk_check_version(2,4,0))
1093 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1096 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1098 return (long) gtk_editable_get_position(GTK_EDITABLE(entry
));
1101 wxTextPos
wxComboBox::GetLastPosition() const
1103 GtkEntry
*entry
= NULL
;
1105 if (!gtk_check_version(2,4,0))
1106 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1109 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1111 int pos
= entry
->text_length
;
1112 return (long) pos
-1;
1115 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
1117 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1119 GtkEntry
*entry
= NULL
;
1121 if (!gtk_check_version(2,4,0))
1122 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1125 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1127 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1128 if (value
.IsNull()) return;
1129 gint pos
= (gint
)to
;
1132 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
1133 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
1135 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.length(), &pos
);
1139 void wxComboBox::SetSelection( long from
, long to
)
1141 GtkEntry
*entry
= NULL
;
1143 if (!gtk_check_version(2,4,0))
1144 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1147 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1149 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1152 void wxComboBox::GetSelection( long* from
, long* to
) const
1154 GtkEntry
*entry
= NULL
;
1156 if (!gtk_check_version(2,4,0))
1157 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1160 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1164 GtkEditable
*editable
= GTK_EDITABLE(entry
);
1166 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
1172 void wxComboBox::SetEditable( bool editable
)
1174 GtkEntry
*entry
= NULL
;
1176 if (!gtk_check_version(2,4,0))
1177 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1180 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1182 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
1185 void wxComboBox::OnChar( wxKeyEvent
&event
)
1187 if ( event
.GetKeyCode() == WXK_RETURN
)
1189 // GTK automatically selects an item if its in the list
1190 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1191 eventEnter
.SetString( GetValue() );
1192 eventEnter
.SetInt( GetSelection() );
1193 eventEnter
.SetEventObject( this );
1195 if (!GetEventHandler()->ProcessEvent( eventEnter
))
1197 // This will invoke the dialog default action, such
1198 // as the clicking the default button.
1200 wxWindow
*top_frame
= m_parent
;
1201 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1202 top_frame
= top_frame
->GetParent();
1204 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1206 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1208 if (window
->default_widget
)
1209 gtk_widget_activate (window
->default_widget
);
1213 // Catch GTK event so that GTK doesn't open the drop
1214 // down list upon RETURN.
1221 void wxComboBox::DisableEvents()
1224 if (!gtk_check_version(2,4,0))
1226 g_signal_handlers_disconnect_by_func (GTK_BIN(m_widget
)->child
,
1227 (gpointer
)gtkcombobox_text_changed_callback
, this);
1229 g_signal_handlers_disconnect_by_func (m_widget
,
1230 (gpointer
)gtkcombobox_changed_callback
, this);
1235 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->list
,
1236 (gpointer
) gtkcombo_combo_select_child_callback
, this);
1238 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->entry
,
1239 (gpointer
) gtkcombo_text_changed_callback
, this);
1243 void wxComboBox::EnableEvents()
1246 if (!gtk_check_version(2,4,0))
1248 g_signal_connect_after (GTK_BIN(m_widget
)->child
, "changed",
1249 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
1251 g_signal_connect_after (m_widget
, "changed",
1252 G_CALLBACK (gtkcombobox_changed_callback
), this);
1257 g_signal_connect_after (GTK_COMBO(m_widget
)->list
, "select-child",
1258 G_CALLBACK (gtkcombo_combo_select_child_callback
),
1260 g_signal_connect_after (GTK_COMBO(m_widget
)->entry
, "changed",
1261 G_CALLBACK (gtkcombo_text_changed_callback
),
1266 void wxComboBox::OnSize( wxSizeEvent
&event
)
1269 if (!gtk_check_version(2,4,0))
1276 // NB: In some situations (e.g. on non-first page of a wizard, if the
1277 // size used is default size), GtkCombo widget is resized correctly,
1278 // but it's look is not updated, it's rendered as if it was much wider.
1279 // No other widgets are affected, so it looks like a bug in GTK+.
1280 // Manually requesting resize calculation (as gtk_pizza_set_size does)
1282 if (GTK_WIDGET_VISIBLE(m_widget
))
1283 gtk_widget_queue_resize(m_widget
);
1289 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1292 if (!gtk_check_version(2,4,0))
1299 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
1301 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
1302 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
1304 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
1305 GList
*child
= list
->children
;
1308 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1310 GtkBin
*bin
= GTK_BIN(child
->data
);
1311 gtk_widget_modify_style( bin
->child
, style
);
1313 child
= child
->next
;
1318 GtkWidget
* wxComboBox::GetConnectWidget()
1320 GtkEntry
*entry
= NULL
;
1322 if (!gtk_check_version(2,4,0))
1323 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1326 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1328 return GTK_WIDGET( entry
);
1331 GdkWindow
*wxComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
1334 if (!gtk_check_version(2,4,0))
1336 wxUnusedVar(windows
);
1338 return GTK_ENTRY(GTK_BIN(m_widget
)->child
)->text_area
;
1343 windows
.push_back(GTK_ENTRY(GTK_COMBO(m_widget
)->entry
)->text_area
);
1344 windows
.push_back(GTK_COMBO(m_widget
)->button
->window
);
1346 // indicate that we return multiple windows in the windows array
1351 wxSize
wxComboBox::DoGetBestSize() const
1353 wxSize
ret( wxControl::DoGetBestSize() );
1355 // we know better our horizontal extent: it depends on the longest string
1360 unsigned int count
= GetCount();
1361 for ( unsigned int n
= 0; n
< count
; n
++ )
1363 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
1364 if ( width
> ret
.x
)
1369 // empty combobox should have some reasonable default size too
1379 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1382 if (!gtk_check_version(2,4,0))
1383 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
1386 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
1389 // ----------------------------------------------------------------------------
1390 // standard event handling
1391 // ----------------------------------------------------------------------------
1393 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
1398 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1403 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1408 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1413 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1418 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1421 GetSelection(& from
, & to
);
1422 if (from
!= -1 && to
!= -1)
1426 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1428 SetSelection(-1, -1);
1431 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1433 event
.Enable( CanCut() );
1436 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1438 event
.Enable( CanCopy() );
1441 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1443 event
.Enable( CanPaste() );
1446 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1448 event
.Enable( CanUndo() );
1451 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1453 event
.Enable( CanRedo() );
1456 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1458 event
.Enable(HasSelection() && IsEditable()) ;
1461 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1463 event
.Enable(GetLastPosition() > 0);