1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/combobox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/combobox.h"
17 #include "wx/settings.h"
18 #include "wx/arrstr.h"
21 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
23 // We use GtkCombo which has been deprecated since GTK+ 2.3.0
24 // in favour of GtkComboBox for <GTK2.4 runtime
25 // We also use GtkList
26 #ifdef GTK_DISABLE_DEPRECATED
27 #undef GTK_DISABLE_DEPRECATED
29 #include "wx/gtk/private.h"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern void wxapp_install_idle_handler();
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 extern bool g_blockEventsOnDrag
;
43 static int g_SelectionBeforePopup
= wxID_NONE
; // this means the popup is hidden
45 //-----------------------------------------------------------------------------
46 // "changed" - typing and list item matches get changed, select-child
47 // if it doesn't match an item then just get a single changed
48 //-----------------------------------------------------------------------------
52 gtkcombo_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
54 if (g_isIdle
) wxapp_install_idle_handler();
56 if (combo
->m_ignoreNextUpdate
)
58 combo
->m_ignoreNextUpdate
= false;
62 if (!combo
->m_hasVMT
) return;
64 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
65 event
.SetString( combo
->GetValue() );
66 event
.SetEventObject( combo
);
67 combo
->GetEventHandler()->ProcessEvent( event
);
73 gtkcombo_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
80 gtkcombo_popup_hide_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
82 // when the popup is hidden, throw a SELECTED event only if the combobox
84 const int curSelection
= combo
->GetCurrentSelection();
86 const bool hasChanged
= curSelection
!= g_SelectionBeforePopup
;
88 // reset the selection flag to value meaning that it is hidden and do it
89 // now, before generating the events, so that GetSelection() returns the
90 // new value from the event handler
91 g_SelectionBeforePopup
= wxID_NONE
;
95 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
96 event
.SetInt( curSelection
);
97 event
.SetString( combo
->GetStringSelection() );
98 event
.SetEventObject( combo
);
99 combo
->GetEventHandler()->ProcessEvent( event
);
101 // for consistency with the other ports, send TEXT event
102 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
103 event2
.SetString( combo
->GetStringSelection() );
104 event2
.SetEventObject( combo
);
105 combo
->GetEventHandler()->ProcessEvent( event2
);
112 gtkcombo_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
114 // store the combobox selection value before the popup is shown
115 g_SelectionBeforePopup
= combo
->GetCurrentSelection();
119 //-----------------------------------------------------------------------------
120 // "select-child" - click/cursor get select-child, changed, select-child
121 //-----------------------------------------------------------------------------
125 gtkcombo_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
127 if (g_isIdle
) wxapp_install_idle_handler();
129 if (!combo
->m_hasVMT
) return;
131 if (g_blockEventsOnDrag
) return;
133 int curSelection
= combo
->GetCurrentSelection();
135 if (combo
->m_prevSelection
== curSelection
) return;
137 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
138 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
140 combo
->m_prevSelection
= curSelection
;
142 // Quickly set the value of the combo box
143 // as GTK+ does that only AFTER the event
145 g_signal_handlers_disconnect_by_func (GTK_COMBO (combo
->GetHandle())->entry
,
146 (gpointer
) gtkcombo_text_changed_callback
,
148 combo
->SetValue( combo
->GetStringSelection() );
149 g_signal_connect_after (GTK_COMBO (combo
->GetHandle())->entry
, "changed",
150 G_CALLBACK (gtkcombo_text_changed_callback
), combo
);
152 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
153 // because when combobox popup is shown, gtkcombo_combo_select_child_callback is
154 // called each times the mouse is over an item with a pressed button so a lot
155 // of SELECTED event could be generated if the user keep the mouse button down
156 // and select other items ...
157 if (g_SelectionBeforePopup
== wxID_NONE
)
159 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
160 event
.SetInt( curSelection
);
161 event
.SetString( combo
->GetStringSelection() );
162 event
.SetEventObject( combo
);
163 combo
->GetEventHandler()->ProcessEvent( event
);
165 // for consistency with the other ports, don't generate text update
166 // events while the user is browsing the combobox neither
167 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
168 event2
.SetString( combo
->GetValue() );
169 event2
.SetEventObject( combo
);
170 combo
->GetEventHandler()->ProcessEvent( event2
);
178 gtkcombobox_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
180 if (g_isIdle
) wxapp_install_idle_handler();
182 if (!combo
->m_hasVMT
) return;
184 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
185 event
.SetString( combo
->GetValue() );
186 event
.SetEventObject( combo
);
187 combo
->GetEventHandler()->ProcessEvent( event
);
193 gtkcombobox_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
195 if (g_isIdle
) wxapp_install_idle_handler();
197 if (!combo
->m_hasVMT
) return;
199 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
200 event
.SetInt( combo
->GetSelection() );
201 event
.SetString( combo
->GetStringSelection() );
202 event
.SetEventObject( combo
);
203 combo
->GetEventHandler()->ProcessEvent( event
);
208 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
212 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
214 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
215 EVT_SIZE(wxComboBox::OnSize
)
216 EVT_CHAR(wxComboBox::OnChar
)
218 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
219 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
220 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
221 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
222 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
223 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
224 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
226 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
227 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
228 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
229 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
230 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
231 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
232 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
235 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
236 const wxString
& value
,
237 const wxPoint
& pos
, const wxSize
& size
,
238 const wxArrayString
& choices
,
239 long style
, const wxValidator
& validator
,
240 const wxString
& name
)
242 wxCArrayString
chs(choices
);
244 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
245 chs
.GetStrings(), style
, validator
, name
);
248 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
249 const wxPoint
& pos
, const wxSize
& size
,
250 int n
, const wxString choices
[],
251 long style
, const wxValidator
& validator
,
252 const wxString
& name
)
254 m_ignoreNextUpdate
= false;
256 m_acceptsFocus
= true;
259 if (!PreCreation( parent
, pos
, size
) ||
260 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
262 wxFAIL_MSG( wxT("wxComboBox creation failed") );
267 if (!gtk_check_version(2,4,0))
269 m_widget
= gtk_combo_box_entry_new_text();
270 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
272 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget
)->child
), TRUE
);
274 for (int i
= 0; i
< n
; i
++)
276 gtk_combo_box_append_text( combobox
, wxGTK_CONV( choices
[i
] ) );
278 m_clientDataList
.Append( (wxObject
*)NULL
);
279 m_clientObjectList
.Append( (wxObject
*)NULL
);
285 m_widget
= gtk_combo_new();
286 GtkCombo
* combo
= GTK_COMBO(m_widget
);
288 // Disable GTK's broken events ...
289 g_signal_handler_disconnect (combo
->entry
, combo
->entry_change_id
);
290 // ... and add surrogate handler.
291 combo
->entry_change_id
= g_signal_connect (combo
->entry
, "changed",
292 G_CALLBACK (gtkcombo_dummy_callback
),
295 // make it more useable
296 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
298 // and case-sensitive
299 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
301 if (style
& wxNO_BORDER
)
302 g_object_set( GTK_ENTRY( combo
->entry
), "has-frame", FALSE
, NULL
);
304 GtkWidget
*list
= combo
->list
;
306 for (int i
= 0; i
< n
; i
++)
308 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
310 m_clientDataList
.Append( (wxObject
*)NULL
);
311 m_clientObjectList
.Append( (wxObject
*)NULL
);
313 gtk_container_add( GTK_CONTAINER(list
), list_item
);
315 gtk_widget_show( list_item
);
320 m_parent
->DoAddChild( this );
322 GtkEntry
*entry
= NULL
;
324 if (!gtk_check_version(2,4,0))
325 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
328 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
330 m_focusWidget
= GTK_WIDGET( entry
);
335 if (!gtk_check_version(2,4,0))
336 ConnectWidget( m_widget
);
339 ConnectWidget( GTK_COMBO(m_widget
)->button
);
342 if (!gtk_check_version(2,4,0))
344 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
346 if (style
& wxCB_READONLY
)
347 gtk_entry_set_editable( entry
, FALSE
);
349 g_signal_connect_after (entry
, "changed",
350 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
352 g_signal_connect_after (m_widget
, "changed",
353 G_CALLBACK (gtkcombobox_changed_callback
), this);
358 GtkCombo
*combo
= GTK_COMBO(m_widget
);
359 // MSW's combo box shows the value and the selection is -1
360 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
361 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
363 if (style
& wxCB_READONLY
)
364 gtk_entry_set_editable( entry
, FALSE
);
366 // "show" and "hide" events are generated when user click on the combobox button which popups a list
367 // this list is the "popwin" gtk widget
368 g_signal_connect (GTK_COMBO(combo
)->popwin
, "hide",
369 G_CALLBACK (gtkcombo_popup_hide_callback
), this);
370 g_signal_connect (GTK_COMBO(combo
)->popwin
, "show",
371 G_CALLBACK (gtkcombo_popup_show_callback
), this);
372 g_signal_connect_after (combo
->list
, "select-child",
373 G_CALLBACK (gtkcombo_combo_select_child_callback
),
375 g_signal_connect_after (entry
, "changed",
376 G_CALLBACK (gtkcombo_text_changed_callback
), this);
378 // This is required for tool bar support
379 // Doesn't currently work
380 // wxSize setsize = GetSize();
381 // gtk_widget_set_size_request( m_widget, setsize.x, setsize.y );
384 SetBestSize(size
); // need this too because this is a wxControlWithItems
390 wxComboBox::~wxComboBox()
392 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
395 wxClientData
*cd
= (wxClientData
*)node
->GetData();
397 node
= node
->GetNext();
399 m_clientObjectList
.Clear();
401 m_clientDataList
.Clear();
404 void wxComboBox::SetFocus()
408 // don't do anything if we already have focus
412 gtk_widget_grab_focus( m_focusWidget
);
415 int wxComboBox::DoAppend( const wxString
&item
)
417 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
420 if (!gtk_check_version(2,4,0))
422 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
423 gtk_combo_box_append_text( combobox
, wxGTK_CONV( item
) );
430 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
431 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
433 gtk_container_add( GTK_CONTAINER(list
), list_item
);
435 if (GTK_WIDGET_REALIZED(m_widget
))
437 gtk_widget_realize( list_item
);
438 gtk_widget_realize( GTK_BIN(list_item
)->child
);
441 // Apply current widget style to the new list_item
442 GtkRcStyle
*style
= CreateWidgetStyle();
445 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
446 GtkBin
*bin
= GTK_BIN( list_item
);
447 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
448 gtk_widget_modify_style( label
, style
);
449 gtk_rc_style_unref( style
);
452 gtk_widget_show( list_item
);
457 const int count
= GetCount();
459 if ( (int)m_clientDataList
.GetCount() < count
)
460 m_clientDataList
.Append( (wxObject
*) NULL
);
461 if ( (int)m_clientObjectList
.GetCount() < count
)
462 m_clientObjectList
.Append( (wxObject
*) NULL
);
464 InvalidateBestSize();
469 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
471 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
472 wxT("can't insert into sorted list"));
474 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
476 int count
= GetCount();
477 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
483 if (!gtk_check_version(2,4,0))
485 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
486 gtk_combo_box_insert_text( combobox
, pos
, wxGTK_CONV( item
) );
493 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
494 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
496 GList
*gitem_list
= g_list_alloc ();
497 gitem_list
->data
= list_item
;
498 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
500 if (GTK_WIDGET_REALIZED(m_widget
))
502 gtk_widget_realize( list_item
);
503 gtk_widget_realize( GTK_BIN(list_item
)->child
);
508 gtk_widget_show( list_item
);
515 if ( (int)m_clientDataList
.GetCount() < count
)
516 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
517 if ( (int)m_clientObjectList
.GetCount() < count
)
518 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
520 InvalidateBestSize();
525 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
527 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
529 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
532 node
->SetData( (wxObject
*) clientData
);
535 void* wxComboBox::DoGetItemClientData( int n
) const
537 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
539 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
541 return node
? node
->GetData() : NULL
;
544 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
546 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
548 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
551 // wxItemContainer already deletes data for us
553 node
->SetData( (wxObject
*) clientData
);
556 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
558 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
560 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
562 return node
? (wxClientData
*) node
->GetData() : NULL
;
565 void wxComboBox::Clear()
567 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
572 if (!gtk_check_version(2,4,0))
574 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
576 for (i
= 0; i
< GetCount(); i
++)
577 gtk_combo_box_remove_text( combobox
, 0 );
582 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
583 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
586 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
589 wxClientData
*cd
= (wxClientData
*)node
->GetData();
591 node
= node
->GetNext();
593 m_clientObjectList
.Clear();
595 m_clientDataList
.Clear();
599 InvalidateBestSize();
602 void wxComboBox::Delete( int n
)
604 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
607 if (!gtk_check_version(2,4,0))
609 wxCHECK_RET( (n
>= 0) && (n
<= GetCount()), wxT("invalid index") );
611 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
612 gtk_combo_box_remove_text( combobox
, n
);
617 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
619 GList
*child
= g_list_nth( listbox
->children
, n
);
623 wxFAIL_MSG(wxT("wrong index"));
629 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
630 gtk_list_remove_items( listbox
, list
);
636 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
639 wxClientData
*cd
= (wxClientData
*)node
->GetData();
641 m_clientObjectList
.Erase( node
);
644 node
= m_clientDataList
.Item( n
);
646 m_clientDataList
.Erase( node
);
648 InvalidateBestSize();
651 void wxComboBox::SetString(int n
, const wxString
&text
)
653 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
656 if (!gtk_check_version(2,4,0))
658 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
659 wxCHECK_RET( (n
>= 0) && (n
<= GetCount()), wxT("invalid index") );
661 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
663 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
665 GValue value
= { 0, };
666 g_value_init( &value
, G_TYPE_STRING
);
667 g_value_set_string( &value
, wxGTK_CONV( text
) );
668 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, 0, &value
);
669 g_value_unset( &value
);
675 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
677 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
680 GtkBin
*bin
= GTK_BIN( child
->data
);
681 GtkLabel
*label
= GTK_LABEL( bin
->child
);
682 gtk_label_set_text(label
, wxGTK_CONV(text
));
686 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
690 InvalidateBestSize();
693 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
695 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
698 if (!gtk_check_version(2,4,0))
700 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
701 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
703 gtk_tree_model_get_iter_first( model
, &iter
);
704 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
709 GValue value
= { 0, };
710 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
711 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
712 g_value_unset( &value
);
714 if (item
.IsSameAs( str
, bCase
) )
719 } while (gtk_tree_model_iter_next( model
, &iter
));
724 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
726 GList
*child
= GTK_LIST(list
)->children
;
730 GtkBin
*bin
= GTK_BIN( child
->data
);
731 GtkLabel
*label
= GTK_LABEL( bin
->child
);
732 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
734 if (item
.IsSameAs( str
, bCase
) )
745 int wxComboBox::GetSelection() const
748 if (!gtk_check_version(2,4,0))
750 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
751 return gtk_combo_box_get_active( combobox
);
755 // if the popup is currently opened, use the selection as it had been
756 // before it dropped down
757 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
758 : g_SelectionBeforePopup
;
761 int wxComboBox::GetCurrentSelection() const
763 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
766 if (!gtk_check_version(2,4,0))
768 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
769 return gtk_combo_box_get_active( combobox
);
774 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
776 GList
*selection
= GTK_LIST(list
)->selection
;
779 GList
*child
= GTK_LIST(list
)->children
;
783 if (child
->data
== selection
->data
) return count
;
793 wxString
wxComboBox::GetString( int n
) const
795 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
800 if (!gtk_check_version(2,4,0))
802 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
803 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
805 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
807 GValue value
= { 0, };
808 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
809 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
810 g_value_unset( &value
);
817 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
819 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
822 GtkBin
*bin
= GTK_BIN( child
->data
);
823 GtkLabel
*label
= GTK_LABEL( bin
->child
);
824 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
828 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
835 wxString
wxComboBox::GetStringSelection() const
837 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
840 if (!gtk_check_version(2,4,0))
842 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
843 int sel
= gtk_combo_box_get_active( combobox
);
845 return wxEmptyString
;
846 return GetString( sel
);
851 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
853 GList
*selection
= GTK_LIST(list
)->selection
;
856 GtkBin
*bin
= GTK_BIN( selection
->data
);
857 GtkLabel
*label
= GTK_LABEL( bin
->child
);
858 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
862 wxFAIL_MSG( wxT("wxComboBox: no selection") );
865 return wxEmptyString
;
868 int wxComboBox::GetCount() const
870 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
873 if (!gtk_check_version(2,4,0))
875 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
876 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
878 gtk_tree_model_get_iter_first( model
, &iter
);
879 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
882 while (gtk_tree_model_iter_next( model
, &iter
))
889 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
891 GList
*child
= GTK_LIST(list
)->children
;
893 while (child
) { count
++; child
= child
->next
; }
900 void wxComboBox::SetSelection( int n
)
902 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
907 if (!gtk_check_version(2,4,0))
909 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
910 gtk_combo_box_set_active( combobox
, n
);
915 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
916 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
917 gtk_list_select_item( GTK_LIST(list
), n
);
924 wxString
wxComboBox::GetValue() const
926 GtkEntry
*entry
= NULL
;
928 if (!gtk_check_version(2,4,0))
929 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
932 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
934 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
937 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
940 printf( "%d ", (int) (c
) );
948 void wxComboBox::SetValue( const wxString
& value
)
950 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
952 GtkEntry
*entry
= NULL
;
954 if (!gtk_check_version(2,4,0))
955 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
958 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
961 if (!value
.IsNull()) tmp
= value
;
962 gtk_entry_set_text( entry
, wxGTK_CONV( tmp
) );
964 InvalidateBestSize();
967 void wxComboBox::Copy()
969 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
971 GtkEntry
*entry
= NULL
;
973 if (!gtk_check_version(2,4,0))
974 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
977 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
979 gtk_editable_copy_clipboard(GTK_EDITABLE(entry
));
982 void wxComboBox::Cut()
984 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
986 GtkEntry
*entry
= NULL
;
988 if (!gtk_check_version(2,4,0))
989 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
992 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
994 gtk_editable_cut_clipboard(GTK_EDITABLE(entry
));
997 void wxComboBox::Paste()
999 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1001 GtkEntry
*entry
= NULL
;
1003 if (!gtk_check_version(2,4,0))
1004 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1007 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1009 gtk_editable_paste_clipboard(GTK_EDITABLE(entry
));
1012 void wxComboBox::Undo()
1017 void wxComboBox::Redo()
1022 void wxComboBox::SelectAll()
1024 SetSelection(0, GetLastPosition());
1027 bool wxComboBox::CanUndo() const
1033 bool wxComboBox::CanRedo() const
1039 bool wxComboBox::HasSelection() const
1042 GetSelection(&from
, &to
);
1046 bool wxComboBox::CanCopy() const
1048 // Can copy if there's a selection
1049 return HasSelection();
1052 bool wxComboBox::CanCut() const
1054 return CanCopy() && IsEditable();
1057 bool wxComboBox::CanPaste() const
1059 // TODO: check for text on the clipboard
1060 return IsEditable() ;
1063 bool wxComboBox::IsEditable() const
1065 return !HasFlag(wxCB_READONLY
);
1069 void wxComboBox::SetInsertionPoint( long pos
)
1071 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1073 if ( pos
== GetLastPosition() )
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 gtk_entry_set_position( entry
, (int)pos
);
1087 long wxComboBox::GetInsertionPoint() 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 return (long) gtk_editable_get_position(GTK_EDITABLE(entry
));
1100 wxTextPos
wxComboBox::GetLastPosition() const
1102 GtkEntry
*entry
= NULL
;
1104 if (!gtk_check_version(2,4,0))
1105 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1108 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1110 int pos
= entry
->text_length
;
1111 return (long) pos
-1;
1114 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
1116 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1118 GtkEntry
*entry
= NULL
;
1120 if (!gtk_check_version(2,4,0))
1121 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1124 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1126 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1127 if (value
.IsNull()) return;
1128 gint pos
= (gint
)to
;
1131 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
1132 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
1134 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
1138 void wxComboBox::SetSelection( long from
, long to
)
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
);
1148 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1151 void wxComboBox::GetSelection( long* from
, long* to
) const
1153 GtkEntry
*entry
= NULL
;
1155 if (!gtk_check_version(2,4,0))
1156 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1159 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1163 GtkEditable
*editable
= GTK_EDITABLE(entry
);
1165 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
1171 void wxComboBox::SetEditable( bool editable
)
1173 GtkEntry
*entry
= NULL
;
1175 if (!gtk_check_version(2,4,0))
1176 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1179 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1181 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
1184 void wxComboBox::OnChar( wxKeyEvent
&event
)
1186 if ( event
.GetKeyCode() == WXK_RETURN
)
1188 // GTK automatically selects an item if its in the list
1189 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1190 eventEnter
.SetString( GetValue() );
1191 eventEnter
.SetInt( GetSelection() );
1192 eventEnter
.SetEventObject( this );
1194 if (!GetEventHandler()->ProcessEvent( eventEnter
))
1196 // This will invoke the dialog default action, such
1197 // as the clicking the default button.
1199 wxWindow
*top_frame
= m_parent
;
1200 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1201 top_frame
= top_frame
->GetParent();
1203 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1205 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1207 if (window
->default_widget
)
1208 gtk_widget_activate (window
->default_widget
);
1212 // Catch GTK event so that GTK doesn't open the drop
1213 // down list upon RETURN.
1220 void wxComboBox::DisableEvents()
1223 if (!gtk_check_version(2,4,0))
1225 g_signal_handlers_disconnect_by_func (GTK_BIN(m_widget
)->child
,
1226 (gpointer
)gtkcombobox_text_changed_callback
, this);
1228 g_signal_handlers_disconnect_by_func (m_widget
,
1229 (gpointer
)gtkcombobox_changed_callback
, this);
1234 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->list
,
1235 (gpointer
) gtkcombo_combo_select_child_callback
, this);
1237 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->entry
,
1238 (gpointer
) gtkcombo_text_changed_callback
, this);
1242 void wxComboBox::EnableEvents()
1245 if (!gtk_check_version(2,4,0))
1247 g_signal_connect_after (GTK_BIN(m_widget
)->child
, "changed",
1248 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
1250 g_signal_connect_after (m_widget
, "changed",
1251 G_CALLBACK (gtkcombobox_changed_callback
), this);
1256 g_signal_connect_after (GTK_COMBO(m_widget
)->list
, "select-child",
1257 G_CALLBACK (gtkcombo_combo_select_child_callback
),
1259 g_signal_connect_after (GTK_COMBO(m_widget
)->entry
, "changed",
1260 G_CALLBACK (gtkcombo_text_changed_callback
),
1265 void wxComboBox::OnSize( wxSizeEvent
&event
)
1268 if (!gtk_check_version(2,4,0))
1275 // NB: In some situations (e.g. on non-first page of a wizard, if the
1276 // size used is default size), GtkCombo widget is resized correctly,
1277 // but it's look is not updated, it's rendered as if it was much wider.
1278 // No other widgets are affected, so it looks like a bug in GTK+.
1279 // Manually requesting resize calculation (as gtk_pizza_set_size does)
1281 if (GTK_WIDGET_VISIBLE(m_widget
))
1282 gtk_widget_queue_resize(m_widget
);
1288 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1291 if (!gtk_check_version(2,4,0))
1298 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
1300 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
1301 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
1303 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
1304 GList
*child
= list
->children
;
1307 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1309 GtkBin
*bin
= GTK_BIN(child
->data
);
1310 gtk_widget_modify_style( bin
->child
, style
);
1312 child
= child
->next
;
1317 GtkWidget
* wxComboBox::GetConnectWidget()
1319 GtkEntry
*entry
= NULL
;
1321 if (!gtk_check_version(2,4,0))
1322 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1325 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1327 return GTK_WIDGET( entry
);
1330 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
1332 GtkEntry
*entry
= NULL
;
1334 if (!gtk_check_version(2,4,0))
1336 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1337 return (window
== entry
->text_area
);
1342 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1343 return ( (window
== entry
->text_area
) ||
1344 (window
== GTK_COMBO(m_widget
)->button
->window
) );
1348 wxSize
wxComboBox::DoGetBestSize() const
1350 wxSize
ret( wxControl::DoGetBestSize() );
1352 // we know better our horizontal extent: it depends on the longest string
1357 size_t count
= GetCount();
1358 for ( size_t n
= 0; n
< count
; n
++ )
1360 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
1361 if ( width
> ret
.x
)
1366 // empty combobox should have some reasonable default size too
1376 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1379 if (!gtk_check_version(2,4,0))
1380 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
1383 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
1386 // ----------------------------------------------------------------------------
1387 // standard event handling
1388 // ----------------------------------------------------------------------------
1390 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
1395 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1400 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1405 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1410 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1415 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1418 GetSelection(& from
, & to
);
1419 if (from
!= -1 && to
!= -1)
1423 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1425 SetSelection(-1, -1);
1428 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1430 event
.Enable( CanCut() );
1433 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1435 event
.Enable( CanCopy() );
1438 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1440 event
.Enable( CanPaste() );
1443 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1445 event
.Enable( CanUndo() );
1448 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1450 event
.Enable( CanRedo() );
1453 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1455 event
.Enable(HasSelection() && IsEditable()) ;
1458 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1460 event
.Enable(GetLastPosition() > 0);