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
23 #include "wx/arrstr.h"
25 // We use GtkCombo which has been deprecated since GTK+ 2.3.0
26 // in favour of GtkComboBox for <GTK2.4 runtime
27 // We also use GtkList
28 #ifdef GTK_DISABLE_DEPRECATED
29 #undef GTK_DISABLE_DEPRECATED
31 #include "wx/gtk/private.h"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
38 static int g_SelectionBeforePopup
= wxID_NONE
; // this means the popup is hidden
40 //-----------------------------------------------------------------------------
41 // "changed" - typing and list item matches get changed, select-child
42 // if it doesn't match an item then just get a single changed
43 //-----------------------------------------------------------------------------
47 gtkcombo_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
49 if (g_isIdle
) wxapp_install_idle_handler();
51 if (combo
->m_ignoreNextUpdate
)
53 combo
->m_ignoreNextUpdate
= false;
57 if (!combo
->m_hasVMT
) return;
59 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
60 event
.SetString( combo
->GetValue() );
61 event
.SetEventObject( combo
);
62 combo
->GetEventHandler()->ProcessEvent( event
);
68 gtkcombo_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
75 gtkcombo_popup_hide_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
77 // when the popup is hidden, throw a SELECTED event only if the combobox
79 const int curSelection
= combo
->GetCurrentSelection();
81 const bool hasChanged
= curSelection
!= g_SelectionBeforePopup
;
83 // reset the selection flag to value meaning that it is hidden and do it
84 // now, before generating the events, so that GetSelection() returns the
85 // new value from the event handler
86 g_SelectionBeforePopup
= wxID_NONE
;
90 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
91 event
.SetInt( curSelection
);
92 event
.SetString( combo
->GetStringSelection() );
93 event
.SetEventObject( combo
);
94 combo
->GetEventHandler()->ProcessEvent( event
);
96 // for consistency with the other ports, send TEXT event
97 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
98 event2
.SetString( combo
->GetStringSelection() );
99 event2
.SetEventObject( combo
);
100 combo
->GetEventHandler()->ProcessEvent( event2
);
107 gtkcombo_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
109 // store the combobox selection value before the popup is shown
110 g_SelectionBeforePopup
= combo
->GetCurrentSelection();
114 //-----------------------------------------------------------------------------
115 // "select-child" - click/cursor get select-child, changed, select-child
116 //-----------------------------------------------------------------------------
120 gtkcombo_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
122 if (g_isIdle
) wxapp_install_idle_handler();
124 if (!combo
->m_hasVMT
) return;
126 if (g_blockEventsOnDrag
) return;
128 int curSelection
= combo
->GetCurrentSelection();
130 if (combo
->m_prevSelection
== curSelection
) return;
132 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
133 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
135 combo
->m_prevSelection
= curSelection
;
137 // Quickly set the value of the combo box
138 // as GTK+ does that only AFTER the event
140 g_signal_handlers_disconnect_by_func (GTK_COMBO (combo
->GetHandle())->entry
,
141 (gpointer
) gtkcombo_text_changed_callback
,
143 combo
->SetValue( combo
->GetStringSelection() );
144 g_signal_connect_after (GTK_COMBO (combo
->GetHandle())->entry
, "changed",
145 G_CALLBACK (gtkcombo_text_changed_callback
), combo
);
147 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
148 // because when combobox popup is shown, gtkcombo_combo_select_child_callback is
149 // called each times the mouse is over an item with a pressed button so a lot
150 // of SELECTED event could be generated if the user keep the mouse button down
151 // and select other items ...
152 if (g_SelectionBeforePopup
== wxID_NONE
)
154 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
155 event
.SetInt( curSelection
);
156 event
.SetString( combo
->GetStringSelection() );
157 event
.SetEventObject( combo
);
158 combo
->GetEventHandler()->ProcessEvent( event
);
160 // for consistency with the other ports, don't generate text update
161 // events while the user is browsing the combobox neither
162 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
163 event2
.SetString( combo
->GetValue() );
164 event2
.SetEventObject( combo
);
165 combo
->GetEventHandler()->ProcessEvent( event2
);
173 gtkcombobox_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
175 if (g_isIdle
) wxapp_install_idle_handler();
177 if (!combo
->m_hasVMT
) return;
179 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
180 event
.SetString( combo
->GetValue() );
181 event
.SetEventObject( combo
);
182 combo
->GetEventHandler()->ProcessEvent( event
);
188 gtkcombobox_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
190 if (g_isIdle
) wxapp_install_idle_handler();
192 if (!combo
->m_hasVMT
) return;
194 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
195 event
.SetInt( combo
->GetSelection() );
196 event
.SetString( combo
->GetStringSelection() );
197 event
.SetEventObject( combo
);
198 combo
->GetEventHandler()->ProcessEvent( event
);
203 //-----------------------------------------------------------------------------
205 //-----------------------------------------------------------------------------
207 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
209 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
210 EVT_SIZE(wxComboBox::OnSize
)
211 EVT_CHAR(wxComboBox::OnChar
)
213 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
214 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
215 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
216 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
217 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
218 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
219 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
221 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
222 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
223 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
224 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
225 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
226 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
227 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
230 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
231 const wxString
& value
,
232 const wxPoint
& pos
, const wxSize
& size
,
233 const wxArrayString
& choices
,
234 long style
, const wxValidator
& validator
,
235 const wxString
& name
)
237 wxCArrayString
chs(choices
);
239 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
240 chs
.GetStrings(), style
, validator
, name
);
243 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
244 const wxPoint
& pos
, const wxSize
& size
,
245 int n
, const wxString choices
[],
246 long style
, const wxValidator
& validator
,
247 const wxString
& name
)
249 m_ignoreNextUpdate
= false;
251 m_acceptsFocus
= true;
254 if (!PreCreation( parent
, pos
, size
) ||
255 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
257 wxFAIL_MSG( wxT("wxComboBox creation failed") );
262 if (!gtk_check_version(2,4,0))
264 m_widget
= gtk_combo_box_entry_new_text();
265 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
267 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget
)->child
), TRUE
);
269 for (int i
= 0; i
< n
; i
++)
271 gtk_combo_box_append_text( combobox
, wxGTK_CONV( choices
[i
] ) );
273 m_clientDataList
.Append( (wxObject
*)NULL
);
274 m_clientObjectList
.Append( (wxObject
*)NULL
);
280 m_widget
= gtk_combo_new();
281 GtkCombo
* combo
= GTK_COMBO(m_widget
);
283 // Disable GTK's broken events ...
284 g_signal_handler_disconnect (combo
->entry
, combo
->entry_change_id
);
285 // ... and add surrogate handler.
286 combo
->entry_change_id
= g_signal_connect (combo
->entry
, "changed",
287 G_CALLBACK (gtkcombo_dummy_callback
),
290 // make it more useable
291 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
293 // and case-sensitive
294 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
296 if (style
& wxNO_BORDER
)
297 g_object_set (combo
->entry
, "has-frame", FALSE
, NULL
);
299 GtkWidget
*list
= combo
->list
;
301 for (int i
= 0; i
< n
; i
++)
303 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
305 m_clientDataList
.Append( (wxObject
*)NULL
);
306 m_clientObjectList
.Append( (wxObject
*)NULL
);
308 gtk_container_add( GTK_CONTAINER(list
), list_item
);
310 gtk_widget_show( list_item
);
315 m_parent
->DoAddChild( this );
317 GtkEntry
*entry
= NULL
;
319 if (!gtk_check_version(2,4,0))
320 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
323 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
325 m_focusWidget
= GTK_WIDGET( entry
);
330 if (!gtk_check_version(2,4,0))
331 ConnectWidget( m_widget
);
334 ConnectWidget( GTK_COMBO(m_widget
)->button
);
337 if (!gtk_check_version(2,4,0))
339 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
341 if (style
& wxCB_READONLY
)
342 gtk_entry_set_editable( entry
, FALSE
);
344 g_signal_connect_after (entry
, "changed",
345 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
347 g_signal_connect_after (m_widget
, "changed",
348 G_CALLBACK (gtkcombobox_changed_callback
), this);
353 GtkCombo
*combo
= GTK_COMBO(m_widget
);
354 // MSW's combo box shows the value and the selection is -1
355 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
356 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
358 if (style
& wxCB_READONLY
)
359 gtk_entry_set_editable( entry
, FALSE
);
361 // "show" and "hide" events are generated when user click on the combobox button which popups a list
362 // this list is the "popwin" gtk widget
363 g_signal_connect (GTK_COMBO(combo
)->popwin
, "hide",
364 G_CALLBACK (gtkcombo_popup_hide_callback
), this);
365 g_signal_connect (GTK_COMBO(combo
)->popwin
, "show",
366 G_CALLBACK (gtkcombo_popup_show_callback
), this);
367 g_signal_connect_after (combo
->list
, "select-child",
368 G_CALLBACK (gtkcombo_combo_select_child_callback
),
370 g_signal_connect_after (entry
, "changed",
371 G_CALLBACK (gtkcombo_text_changed_callback
), this);
373 // This is required for tool bar support
374 // Doesn't currently work
375 // wxSize setsize = GetSize();
376 // gtk_widget_set_size_request( m_widget, setsize.x, setsize.y );
379 SetBestSize(size
); // need this too because this is a wxControlWithItems
385 wxComboBox::~wxComboBox()
387 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
390 wxClientData
*cd
= (wxClientData
*)node
->GetData();
392 node
= node
->GetNext();
394 m_clientObjectList
.Clear();
396 m_clientDataList
.Clear();
399 void wxComboBox::SetFocus()
403 // don't do anything if we already have focus
407 gtk_widget_grab_focus( m_focusWidget
);
410 int wxComboBox::DoAppend( const wxString
&item
)
412 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
415 if (!gtk_check_version(2,4,0))
417 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
418 gtk_combo_box_append_text( combobox
, wxGTK_CONV( item
) );
425 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
426 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
428 gtk_container_add( GTK_CONTAINER(list
), list_item
);
430 if (GTK_WIDGET_REALIZED(m_widget
))
432 gtk_widget_realize( list_item
);
433 gtk_widget_realize( GTK_BIN(list_item
)->child
);
436 // Apply current widget style to the new list_item
437 GtkRcStyle
*style
= CreateWidgetStyle();
440 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
441 GtkBin
*bin
= GTK_BIN( list_item
);
442 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
443 gtk_widget_modify_style( label
, style
);
444 gtk_rc_style_unref( style
);
447 gtk_widget_show( list_item
);
452 const unsigned int count
= GetCount();
454 if ( m_clientDataList
.GetCount() < count
)
455 m_clientDataList
.Append( (wxObject
*) NULL
);
456 if ( m_clientObjectList
.GetCount() < count
)
457 m_clientObjectList
.Append( (wxObject
*) NULL
);
459 InvalidateBestSize();
464 int wxComboBox::DoInsert(const wxString
&item
, unsigned int pos
)
466 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
467 wxT("can't insert into sorted list"));
469 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
470 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("invalid index") );
472 unsigned int count
= GetCount();
478 if (!gtk_check_version(2,4,0))
480 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
481 gtk_combo_box_insert_text( combobox
, pos
, wxGTK_CONV( item
) );
488 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
489 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
491 GList
*gitem_list
= g_list_alloc ();
492 gitem_list
->data
= list_item
;
493 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
495 if (GTK_WIDGET_REALIZED(m_widget
))
497 gtk_widget_realize( list_item
);
498 gtk_widget_realize( GTK_BIN(list_item
)->child
);
503 gtk_widget_show( list_item
);
510 if ( m_clientDataList
.GetCount() < count
)
511 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
512 if ( m_clientObjectList
.GetCount() < count
)
513 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
515 InvalidateBestSize();
520 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
522 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
524 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
527 node
->SetData( (wxObject
*) clientData
);
530 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
532 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
534 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
536 return node
? node
->GetData() : NULL
;
539 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
541 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
543 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
546 // wxItemContainer already deletes data for us
548 node
->SetData( (wxObject
*) clientData
);
551 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
553 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
555 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
557 return node
? (wxClientData
*) node
->GetData() : NULL
;
560 void wxComboBox::Clear()
562 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
567 if (!gtk_check_version(2,4,0))
569 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
570 const unsigned int count
= GetCount();
571 for (unsigned int i
= 0; i
< count
; i
++)
572 gtk_combo_box_remove_text( combobox
, 0 );
575 #endif // __WXGTK24__
577 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
578 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
581 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
584 wxClientData
*cd
= (wxClientData
*)node
->GetData();
586 node
= node
->GetNext();
588 m_clientObjectList
.Clear();
590 m_clientDataList
.Clear();
594 InvalidateBestSize();
597 void wxComboBox::Delete(unsigned int n
)
599 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
602 if (!gtk_check_version(2,4,0))
604 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
606 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
607 gtk_combo_box_remove_text( combobox
, n
);
612 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
614 GList
*child
= g_list_nth( listbox
->children
, n
);
618 wxFAIL_MSG(wxT("wrong index"));
624 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
625 gtk_list_remove_items( listbox
, list
);
631 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
634 wxClientData
*cd
= (wxClientData
*)node
->GetData();
636 m_clientObjectList
.Erase( node
);
639 node
= m_clientDataList
.Item( n
);
641 m_clientDataList
.Erase( node
);
643 InvalidateBestSize();
646 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
648 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
651 if (!gtk_check_version(2,4,0))
653 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
654 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
656 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
658 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
660 GValue value
= { 0, };
661 g_value_init( &value
, G_TYPE_STRING
);
662 g_value_set_string( &value
, wxGTK_CONV( text
) );
663 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, 0, &value
);
664 g_value_unset( &value
);
670 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
672 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
675 GtkBin
*bin
= GTK_BIN( child
->data
);
676 GtkLabel
*label
= GTK_LABEL( bin
->child
);
677 gtk_label_set_text(label
, wxGTK_CONV(text
));
681 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
685 InvalidateBestSize();
688 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
690 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
693 if (!gtk_check_version(2,4,0))
695 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
696 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
698 gtk_tree_model_get_iter_first( model
, &iter
);
699 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
704 GValue value
= { 0, };
705 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
706 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
707 g_value_unset( &value
);
709 if (item
.IsSameAs( str
, bCase
) )
714 } while (gtk_tree_model_iter_next( model
, &iter
));
719 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
721 GList
*child
= GTK_LIST(list
)->children
;
725 GtkBin
*bin
= GTK_BIN( child
->data
);
726 GtkLabel
*label
= GTK_LABEL( bin
->child
);
727 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
729 if (item
.IsSameAs( str
, bCase
) )
740 int wxComboBox::GetSelection() const
743 if (!gtk_check_version(2,4,0))
745 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
746 return gtk_combo_box_get_active( combobox
);
750 // if the popup is currently opened, use the selection as it had been
751 // before it dropped down
752 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
753 : g_SelectionBeforePopup
;
756 int wxComboBox::GetCurrentSelection() const
758 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
761 if (!gtk_check_version(2,4,0))
763 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
764 return gtk_combo_box_get_active( combobox
);
769 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
771 GList
*selection
= GTK_LIST(list
)->selection
;
774 GList
*child
= GTK_LIST(list
)->children
;
778 if (child
->data
== selection
->data
) return count
;
788 wxString
wxComboBox::GetString(unsigned int n
) const
790 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
795 if (!gtk_check_version(2,4,0))
797 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
798 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
800 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
802 GValue value
= { 0, };
803 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
804 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
805 g_value_unset( &value
);
812 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
814 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
817 GtkBin
*bin
= GTK_BIN( child
->data
);
818 GtkLabel
*label
= GTK_LABEL( bin
->child
);
819 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
823 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
830 wxString
wxComboBox::GetStringSelection() const
832 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
835 if (!gtk_check_version(2,4,0))
837 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
838 int sel
= gtk_combo_box_get_active( combobox
);
840 return wxEmptyString
;
841 return GetString(sel
);
846 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
848 GList
*selection
= GTK_LIST(list
)->selection
;
851 GtkBin
*bin
= GTK_BIN( selection
->data
);
852 GtkLabel
*label
= GTK_LABEL( bin
->child
);
853 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
857 wxFAIL_MSG( wxT("wxComboBox: no selection") );
860 return wxEmptyString
;
863 unsigned int wxComboBox::GetCount() const
865 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
868 if (!gtk_check_version(2,4,0))
870 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
871 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
873 gtk_tree_model_get_iter_first( model
, &iter
);
874 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
876 unsigned int ret
= 1;
877 while (gtk_tree_model_iter_next( model
, &iter
))
884 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
886 GList
*child
= GTK_LIST(list
)->children
;
887 unsigned int count
= 0;
899 void wxComboBox::SetSelection( int n
)
901 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
906 if (!gtk_check_version(2,4,0))
908 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
909 gtk_combo_box_set_active( combobox
, n
);
914 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
915 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
916 gtk_list_select_item( GTK_LIST(list
), n
);
923 wxString
wxComboBox::GetValue() const
925 GtkEntry
*entry
= NULL
;
927 if (!gtk_check_version(2,4,0))
928 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
931 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
933 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
936 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
939 printf( "%d ", (int) (c
) );
947 void wxComboBox::SetValue( const wxString
& value
)
949 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
951 GtkEntry
*entry
= NULL
;
953 if (!gtk_check_version(2,4,0))
954 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
957 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
960 if (!value
.IsNull()) tmp
= value
;
961 gtk_entry_set_text( entry
, wxGTK_CONV( tmp
) );
963 InvalidateBestSize();
966 void wxComboBox::Copy()
968 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
970 GtkEntry
*entry
= NULL
;
972 if (!gtk_check_version(2,4,0))
973 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
976 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
978 gtk_editable_copy_clipboard(GTK_EDITABLE(entry
));
981 void wxComboBox::Cut()
983 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
985 GtkEntry
*entry
= NULL
;
987 if (!gtk_check_version(2,4,0))
988 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
991 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
993 gtk_editable_cut_clipboard(GTK_EDITABLE(entry
));
996 void wxComboBox::Paste()
998 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1000 GtkEntry
*entry
= NULL
;
1002 if (!gtk_check_version(2,4,0))
1003 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1006 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1008 gtk_editable_paste_clipboard(GTK_EDITABLE(entry
));
1011 void wxComboBox::Undo()
1016 void wxComboBox::Redo()
1021 void wxComboBox::SelectAll()
1023 SetSelection(0, GetLastPosition());
1026 bool wxComboBox::CanUndo() const
1032 bool wxComboBox::CanRedo() const
1038 bool wxComboBox::HasSelection() const
1041 GetSelection(&from
, &to
);
1045 bool wxComboBox::CanCopy() const
1047 // Can copy if there's a selection
1048 return HasSelection();
1051 bool wxComboBox::CanCut() const
1053 return CanCopy() && IsEditable();
1056 bool wxComboBox::CanPaste() const
1058 // TODO: check for text on the clipboard
1059 return IsEditable() ;
1062 bool wxComboBox::IsEditable() const
1064 return !HasFlag(wxCB_READONLY
);
1068 void wxComboBox::SetInsertionPoint( long pos
)
1070 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1072 if ( pos
== GetLastPosition() )
1075 GtkEntry
*entry
= NULL
;
1077 if (!gtk_check_version(2,4,0))
1078 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1081 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1083 gtk_entry_set_position( entry
, (int)pos
);
1086 long wxComboBox::GetInsertionPoint() const
1088 GtkEntry
*entry
= NULL
;
1090 if (!gtk_check_version(2,4,0))
1091 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1094 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1096 return (long) gtk_editable_get_position(GTK_EDITABLE(entry
));
1099 wxTextPos
wxComboBox::GetLastPosition() const
1101 GtkEntry
*entry
= NULL
;
1103 if (!gtk_check_version(2,4,0))
1104 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1107 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1109 int pos
= entry
->text_length
;
1110 return (long) pos
-1;
1113 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
1115 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1117 GtkEntry
*entry
= NULL
;
1119 if (!gtk_check_version(2,4,0))
1120 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1123 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1125 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1126 if (value
.IsNull()) return;
1127 gint pos
= (gint
)to
;
1130 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
1131 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
1133 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.length(), &pos
);
1137 void wxComboBox::SetSelection( long from
, long to
)
1139 GtkEntry
*entry
= NULL
;
1141 if (!gtk_check_version(2,4,0))
1142 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1145 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1147 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1150 void wxComboBox::GetSelection( long* from
, long* to
) const
1152 GtkEntry
*entry
= NULL
;
1154 if (!gtk_check_version(2,4,0))
1155 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1158 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1162 GtkEditable
*editable
= GTK_EDITABLE(entry
);
1164 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
1170 void wxComboBox::SetEditable( bool editable
)
1172 GtkEntry
*entry
= NULL
;
1174 if (!gtk_check_version(2,4,0))
1175 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1178 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1180 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
1183 void wxComboBox::OnChar( wxKeyEvent
&event
)
1185 if ( event
.GetKeyCode() == WXK_RETURN
)
1187 // GTK automatically selects an item if its in the list
1188 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1189 eventEnter
.SetString( GetValue() );
1190 eventEnter
.SetInt( GetSelection() );
1191 eventEnter
.SetEventObject( this );
1193 if (!GetEventHandler()->ProcessEvent( eventEnter
))
1195 // This will invoke the dialog default action, such
1196 // as the clicking the default button.
1198 wxWindow
*top_frame
= m_parent
;
1199 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1200 top_frame
= top_frame
->GetParent();
1202 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1204 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1206 if (window
->default_widget
)
1207 gtk_widget_activate (window
->default_widget
);
1211 // Catch GTK event so that GTK doesn't open the drop
1212 // down list upon RETURN.
1219 void wxComboBox::DisableEvents()
1222 if (!gtk_check_version(2,4,0))
1224 g_signal_handlers_disconnect_by_func (GTK_BIN(m_widget
)->child
,
1225 (gpointer
)gtkcombobox_text_changed_callback
, this);
1227 g_signal_handlers_disconnect_by_func (m_widget
,
1228 (gpointer
)gtkcombobox_changed_callback
, this);
1233 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->list
,
1234 (gpointer
) gtkcombo_combo_select_child_callback
, this);
1236 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->entry
,
1237 (gpointer
) gtkcombo_text_changed_callback
, this);
1241 void wxComboBox::EnableEvents()
1244 if (!gtk_check_version(2,4,0))
1246 g_signal_connect_after (GTK_BIN(m_widget
)->child
, "changed",
1247 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
1249 g_signal_connect_after (m_widget
, "changed",
1250 G_CALLBACK (gtkcombobox_changed_callback
), this);
1255 g_signal_connect_after (GTK_COMBO(m_widget
)->list
, "select-child",
1256 G_CALLBACK (gtkcombo_combo_select_child_callback
),
1258 g_signal_connect_after (GTK_COMBO(m_widget
)->entry
, "changed",
1259 G_CALLBACK (gtkcombo_text_changed_callback
),
1264 void wxComboBox::OnSize( wxSizeEvent
&event
)
1267 if (!gtk_check_version(2,4,0))
1274 // NB: In some situations (e.g. on non-first page of a wizard, if the
1275 // size used is default size), GtkCombo widget is resized correctly,
1276 // but it's look is not updated, it's rendered as if it was much wider.
1277 // No other widgets are affected, so it looks like a bug in GTK+.
1278 // Manually requesting resize calculation (as gtk_pizza_set_size does)
1280 if (GTK_WIDGET_VISIBLE(m_widget
))
1281 gtk_widget_queue_resize(m_widget
);
1287 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1290 if (!gtk_check_version(2,4,0))
1297 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
1299 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
1300 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
1302 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
1303 GList
*child
= list
->children
;
1306 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1308 GtkBin
*bin
= GTK_BIN(child
->data
);
1309 gtk_widget_modify_style( bin
->child
, style
);
1311 child
= child
->next
;
1316 GtkWidget
* wxComboBox::GetConnectWidget()
1318 GtkEntry
*entry
= NULL
;
1320 if (!gtk_check_version(2,4,0))
1321 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1324 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1326 return GTK_WIDGET( entry
);
1329 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
1331 GtkEntry
*entry
= NULL
;
1333 if (!gtk_check_version(2,4,0))
1335 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1336 return (window
== entry
->text_area
);
1341 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1342 return ( (window
== entry
->text_area
) ||
1343 (window
== GTK_COMBO(m_widget
)->button
->window
) );
1347 wxSize
wxComboBox::DoGetBestSize() const
1349 wxSize
ret( wxControl::DoGetBestSize() );
1351 // we know better our horizontal extent: it depends on the longest string
1356 unsigned int count
= GetCount();
1357 for ( unsigned int n
= 0; n
< count
; n
++ )
1359 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
1360 if ( width
> ret
.x
)
1365 // empty combobox should have some reasonable default size too
1375 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1378 if (!gtk_check_version(2,4,0))
1379 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
1382 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
1385 // ----------------------------------------------------------------------------
1386 // standard event handling
1387 // ----------------------------------------------------------------------------
1389 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
1394 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1399 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1404 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1409 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1414 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1417 GetSelection(& from
, & to
);
1418 if (from
!= -1 && to
!= -1)
1422 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1424 SetSelection(-1, -1);
1427 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1429 event
.Enable( CanCut() );
1432 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1434 event
.Enable( CanCopy() );
1437 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1439 event
.Enable( CanPaste() );
1442 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1444 event
.Enable( CanUndo() );
1447 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1449 event
.Enable( CanRedo() );
1452 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1454 event
.Enable(HasSelection() && IsEditable()) ;
1457 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1459 event
.Enable(GetLastPosition() > 0);