1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/combobox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/combobox.h"
19 #include "wx/settings.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
21 #include "wx/arrstr.h"
24 // We use GtkCombo which has been deprecated since GTK+ 2.3.0
25 // in favour of GtkComboBox for <GTK2.4 runtime
26 // We also use GtkList
27 #ifdef GTK_DISABLE_DEPRECATED
28 #undef GTK_DISABLE_DEPRECATED
30 #include "wx/gtk/private.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
37 static int g_SelectionBeforePopup
= wxID_NONE
; // this means the popup is hidden
39 //-----------------------------------------------------------------------------
40 // "changed" - typing and list item matches get changed, select-child
41 // if it doesn't match an item then just get a single changed
42 //-----------------------------------------------------------------------------
46 gtkcombo_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
48 if (combo
->m_ignoreNextUpdate
)
50 combo
->m_ignoreNextUpdate
= false;
54 if (!combo
->m_hasVMT
) return;
56 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
57 event
.SetString( combo
->GetValue() );
58 event
.SetEventObject( combo
);
59 combo
->GetEventHandler()->ProcessEvent( event
);
65 gtkcombo_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
72 gtkcombo_popup_hide_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
74 // when the popup is hidden, throw a SELECTED event only if the combobox
76 const int curSelection
= combo
->GetCurrentSelection();
78 const bool hasChanged
= curSelection
!= g_SelectionBeforePopup
;
80 // reset the selection flag to value meaning that it is hidden and do it
81 // now, before generating the events, so that GetSelection() returns the
82 // new value from the event handler
83 g_SelectionBeforePopup
= wxID_NONE
;
87 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
88 event
.SetInt( curSelection
);
89 event
.SetString( combo
->GetStringSelection() );
90 event
.SetEventObject( combo
);
91 combo
->GetEventHandler()->ProcessEvent( event
);
93 // for consistency with the other ports, send TEXT event
94 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
95 event2
.SetString( combo
->GetStringSelection() );
96 event2
.SetEventObject( combo
);
97 combo
->GetEventHandler()->ProcessEvent( event2
);
104 gtkcombo_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
106 // store the combobox selection value before the popup is shown
107 g_SelectionBeforePopup
= combo
->GetCurrentSelection();
111 //-----------------------------------------------------------------------------
112 // "select-child" - click/cursor get select-child, changed, select-child
113 //-----------------------------------------------------------------------------
117 gtkcombo_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
119 if (!combo
->m_hasVMT
) return;
121 if (g_blockEventsOnDrag
) return;
123 int curSelection
= combo
->GetCurrentSelection();
125 if (combo
->m_prevSelection
== curSelection
) return;
127 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
128 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
130 combo
->m_prevSelection
= curSelection
;
132 // Quickly set the value of the combo box
133 // as GTK+ does that only AFTER the event
135 g_signal_handlers_disconnect_by_func (GTK_COMBO (combo
->GetHandle())->entry
,
136 (gpointer
) gtkcombo_text_changed_callback
,
138 combo
->SetValue( combo
->GetStringSelection() );
139 g_signal_connect_after (GTK_COMBO (combo
->GetHandle())->entry
, "changed",
140 G_CALLBACK (gtkcombo_text_changed_callback
), combo
);
142 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
143 // because when combobox popup is shown, gtkcombo_combo_select_child_callback is
144 // called each times the mouse is over an item with a pressed button so a lot
145 // of SELECTED event could be generated if the user keep the mouse button down
146 // and select other items ...
147 if (g_SelectionBeforePopup
== wxID_NONE
)
149 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
150 event
.SetInt( curSelection
);
151 event
.SetString( combo
->GetStringSelection() );
152 event
.SetEventObject( combo
);
153 combo
->GetEventHandler()->ProcessEvent( event
);
155 // for consistency with the other ports, don't generate text update
156 // events while the user is browsing the combobox neither
157 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
158 event2
.SetString( combo
->GetValue() );
159 event2
.SetEventObject( combo
);
160 combo
->GetEventHandler()->ProcessEvent( event2
);
168 gtkcombobox_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
170 if (!combo
->m_hasVMT
) return;
172 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
173 event
.SetString( combo
->GetValue() );
174 event
.SetEventObject( combo
);
175 combo
->GetEventHandler()->ProcessEvent( event
);
181 gtkcombobox_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
183 if (!combo
->m_hasVMT
) return;
185 if (combo
->GetSelection() == -1)
188 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
189 event
.SetInt( combo
->GetSelection() );
190 event
.SetString( combo
->GetStringSelection() );
191 event
.SetEventObject( combo
);
192 combo
->GetEventHandler()->ProcessEvent( event
);
198 //-----------------------------------------------------------------------------
200 //-----------------------------------------------------------------------------
202 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
204 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
205 EVT_SIZE(wxComboBox::OnSize
)
206 EVT_CHAR(wxComboBox::OnChar
)
208 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
209 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
210 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
211 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
212 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
213 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
214 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
216 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
217 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
218 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
219 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
220 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
221 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
222 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
225 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
226 const wxString
& value
,
227 const wxPoint
& pos
, const wxSize
& size
,
228 const wxArrayString
& choices
,
229 long style
, const wxValidator
& validator
,
230 const wxString
& name
)
232 wxCArrayString
chs(choices
);
234 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
235 chs
.GetStrings(), style
, validator
, name
);
238 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
239 const wxPoint
& pos
, const wxSize
& size
,
240 int n
, const wxString choices
[],
241 long style
, const wxValidator
& validator
,
242 const wxString
& name
)
244 m_ignoreNextUpdate
= false;
248 if (!PreCreation( parent
, pos
, size
) ||
249 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
251 wxFAIL_MSG( wxT("wxComboBox creation failed") );
256 if (!gtk_check_version(2,4,0))
258 m_widget
= gtk_combo_box_entry_new_text();
259 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
261 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget
)->child
), TRUE
);
263 for (int i
= 0; i
< n
; i
++)
265 gtk_combo_box_append_text( combobox
, wxGTK_CONV( choices
[i
] ) );
267 m_clientDataList
.Append( (wxObject
*)NULL
);
268 m_clientObjectList
.Append( (wxObject
*)NULL
);
274 m_widget
= gtk_combo_new();
275 GtkCombo
* combo
= GTK_COMBO(m_widget
);
277 // Disable GTK's broken events ...
278 g_signal_handler_disconnect (combo
->entry
, combo
->entry_change_id
);
279 // ... and add surrogate handler.
280 combo
->entry_change_id
= g_signal_connect (combo
->entry
, "changed",
281 G_CALLBACK (gtkcombo_dummy_callback
),
284 // make it more useable
285 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
287 // and case-sensitive
288 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
290 if (style
& wxNO_BORDER
)
291 g_object_set (combo
->entry
, "has-frame", FALSE
, NULL
);
293 GtkWidget
*list
= combo
->list
;
295 for (int i
= 0; i
< n
; i
++)
297 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
299 m_clientDataList
.Append( (wxObject
*)NULL
);
300 m_clientObjectList
.Append( (wxObject
*)NULL
);
302 gtk_container_add( GTK_CONTAINER(list
), list_item
);
304 gtk_widget_show( list_item
);
309 m_parent
->DoAddChild( this );
311 GtkEntry
*entry
= NULL
;
313 if (!gtk_check_version(2,4,0))
314 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
317 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
319 m_focusWidget
= GTK_WIDGET( entry
);
324 if (!gtk_check_version(2,4,0))
325 ConnectWidget( m_widget
);
328 ConnectWidget( GTK_COMBO(m_widget
)->button
);
331 if (!gtk_check_version(2,4,0))
333 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
335 if (style
& wxCB_READONLY
)
336 gtk_entry_set_editable( entry
, FALSE
);
338 g_signal_connect_after (entry
, "changed",
339 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
341 g_signal_connect_after (m_widget
, "changed",
342 G_CALLBACK (gtkcombobox_changed_callback
), this);
348 GtkCombo
*combo
= GTK_COMBO(m_widget
);
349 // MSW's combo box shows the value and the selection is -1
350 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
351 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
353 if (style
& wxCB_READONLY
)
354 gtk_entry_set_editable( entry
, FALSE
);
356 // "show" and "hide" events are generated when user click on the combobox button which popups a list
357 // this list is the "popwin" gtk widget
358 g_signal_connect (GTK_COMBO(combo
)->popwin
, "hide",
359 G_CALLBACK (gtkcombo_popup_hide_callback
), this);
360 g_signal_connect (GTK_COMBO(combo
)->popwin
, "show",
361 G_CALLBACK (gtkcombo_popup_show_callback
), this);
362 g_signal_connect_after (combo
->list
, "select-child",
363 G_CALLBACK (gtkcombo_combo_select_child_callback
),
365 g_signal_connect_after (entry
, "changed",
366 G_CALLBACK (gtkcombo_text_changed_callback
), this);
369 SetInitialSize(size
); // need this too because this is a wxControlWithItems
374 wxComboBox::~wxComboBox()
376 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
379 wxClientData
*cd
= (wxClientData
*)node
->GetData();
381 node
= node
->GetNext();
383 m_clientObjectList
.Clear();
385 m_clientDataList
.Clear();
388 void wxComboBox::SetFocus()
392 // don't do anything if we already have focus
396 gtk_widget_grab_focus( m_focusWidget
);
399 int wxComboBox::DoAppend( const wxString
&item
)
401 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
404 if (!gtk_check_version(2,4,0))
406 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
407 gtk_combo_box_append_text( combobox
, wxGTK_CONV( item
) );
414 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
415 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
417 gtk_container_add( GTK_CONTAINER(list
), list_item
);
419 if (GTK_WIDGET_REALIZED(m_widget
))
421 gtk_widget_realize( list_item
);
422 gtk_widget_realize( GTK_BIN(list_item
)->child
);
425 // Apply current widget style to the new list_item
426 GtkRcStyle
*style
= CreateWidgetStyle();
429 gtk_widget_modify_style(list_item
, style
);
430 GtkBin
*bin
= GTK_BIN( list_item
);
431 GtkWidget
*label
= bin
->child
;
432 gtk_widget_modify_style( label
, style
);
433 gtk_rc_style_unref( style
);
436 gtk_widget_show( list_item
);
441 const unsigned int count
= GetCount();
443 if ( m_clientDataList
.GetCount() < count
)
444 m_clientDataList
.Append( (wxObject
*) NULL
);
445 if ( m_clientObjectList
.GetCount() < count
)
446 m_clientObjectList
.Append( (wxObject
*) NULL
);
448 InvalidateBestSize();
453 int wxComboBox::DoInsert(const wxString
&item
, unsigned int pos
)
455 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
456 wxT("can't insert into sorted list"));
458 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
459 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("invalid index") );
461 unsigned int count
= GetCount();
467 if (!gtk_check_version(2,4,0))
469 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
470 gtk_combo_box_insert_text( combobox
, pos
, wxGTK_CONV( item
) );
477 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
478 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
480 GList
*gitem_list
= g_list_alloc ();
481 gitem_list
->data
= list_item
;
482 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
484 if (GTK_WIDGET_REALIZED(m_widget
))
486 gtk_widget_realize( list_item
);
487 gtk_widget_realize( GTK_BIN(list_item
)->child
);
492 gtk_widget_show( list_item
);
499 if ( m_clientDataList
.GetCount() < count
)
500 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
501 if ( m_clientObjectList
.GetCount() < count
)
502 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
504 InvalidateBestSize();
509 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
511 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
513 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
516 node
->SetData( (wxObject
*) clientData
);
519 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
521 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
523 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
525 return node
? node
->GetData() : NULL
;
528 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
530 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
532 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
535 // wxItemContainer already deletes data for us
537 node
->SetData( (wxObject
*) clientData
);
540 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
542 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
544 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
546 return node
? (wxClientData
*) node
->GetData() : NULL
;
549 void wxComboBox::Clear()
551 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
556 if (!gtk_check_version(2,4,0))
558 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
559 const unsigned int count
= GetCount();
560 for (unsigned int i
= 0; i
< count
; i
++)
561 gtk_combo_box_remove_text( combobox
, 0 );
564 #endif // __WXGTK24__
566 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
567 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
570 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
573 wxClientData
*cd
= (wxClientData
*)node
->GetData();
575 node
= node
->GetNext();
577 m_clientObjectList
.Clear();
579 m_clientDataList
.Clear();
583 InvalidateBestSize();
586 void wxComboBox::Delete(unsigned int n
)
588 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
591 if (!gtk_check_version(2,4,0))
593 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
595 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
596 gtk_combo_box_remove_text( combobox
, n
);
601 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
603 GList
*child
= g_list_nth( listbox
->children
, n
);
607 wxFAIL_MSG(wxT("wrong index"));
613 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
614 gtk_list_remove_items( listbox
, list
);
620 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
623 wxClientData
*cd
= (wxClientData
*)node
->GetData();
625 m_clientObjectList
.Erase( node
);
628 node
= m_clientDataList
.Item( n
);
630 m_clientDataList
.Erase( node
);
632 InvalidateBestSize();
635 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
637 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
640 if (!gtk_check_version(2,4,0))
642 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
643 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
645 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
647 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
649 GValue value
= { 0, };
650 g_value_init( &value
, G_TYPE_STRING
);
651 g_value_set_string( &value
, wxGTK_CONV( text
) );
652 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, 0, &value
);
653 g_value_unset( &value
);
659 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
661 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
664 GtkBin
*bin
= GTK_BIN( child
->data
);
665 GtkLabel
*label
= GTK_LABEL( bin
->child
);
666 gtk_label_set_text(label
, wxGTK_CONV(text
));
670 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
674 InvalidateBestSize();
677 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
679 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
682 if (!gtk_check_version(2,4,0))
684 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
685 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
687 gtk_tree_model_get_iter_first( model
, &iter
);
688 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
693 GValue value
= { 0, };
694 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
695 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
696 g_value_unset( &value
);
698 if (item
.IsSameAs( str
, bCase
) )
703 } while (gtk_tree_model_iter_next( model
, &iter
));
708 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
710 GList
*child
= GTK_LIST(list
)->children
;
714 GtkBin
*bin
= GTK_BIN( child
->data
);
715 GtkLabel
*label
= GTK_LABEL( bin
->child
);
716 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
718 if (item
.IsSameAs( str
, bCase
) )
729 int wxComboBox::GetSelection() const
732 if (!gtk_check_version(2,4,0))
734 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
735 return gtk_combo_box_get_active( combobox
);
739 // if the popup is currently opened, use the selection as it had been
740 // before it dropped down
741 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
742 : g_SelectionBeforePopup
;
745 int wxComboBox::GetCurrentSelection() const
747 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
750 if (!gtk_check_version(2,4,0))
752 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
753 return gtk_combo_box_get_active( combobox
);
758 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
760 GList
*selection
= GTK_LIST(list
)->selection
;
763 GList
*child
= GTK_LIST(list
)->children
;
767 if (child
->data
== selection
->data
) return count
;
777 wxString
wxComboBox::GetString(unsigned int n
) const
779 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
784 if (!gtk_check_version(2,4,0))
786 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
787 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
789 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
791 GValue value
= { 0, };
792 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
793 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
794 g_value_unset( &value
);
801 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
803 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
806 GtkBin
*bin
= GTK_BIN( child
->data
);
807 GtkLabel
*label
= GTK_LABEL( bin
->child
);
808 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
812 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
819 wxString
wxComboBox::GetStringSelection() const
821 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
824 if (!gtk_check_version(2,4,0))
826 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
827 int sel
= gtk_combo_box_get_active( combobox
);
829 return wxEmptyString
;
830 return GetString(sel
);
835 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
837 GList
*selection
= GTK_LIST(list
)->selection
;
840 GtkBin
*bin
= GTK_BIN( selection
->data
);
841 GtkLabel
*label
= GTK_LABEL( bin
->child
);
842 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
846 wxFAIL_MSG( wxT("wxComboBox: no selection") );
849 return wxEmptyString
;
852 unsigned int wxComboBox::GetCount() const
854 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
857 if (!gtk_check_version(2,4,0))
859 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
860 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
862 gtk_tree_model_get_iter_first( model
, &iter
);
863 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
865 unsigned int ret
= 1;
866 while (gtk_tree_model_iter_next( model
, &iter
))
873 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
875 GList
*child
= GTK_LIST(list
)->children
;
876 unsigned int count
= 0;
888 void wxComboBox::SetSelection( int n
)
890 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
895 if (!gtk_check_version(2,4,0))
897 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
898 gtk_combo_box_set_active( combobox
, n
);
903 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
904 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
905 gtk_list_select_item( GTK_LIST(list
), n
);
912 wxString
wxComboBox::GetValue() const
914 GtkEntry
*entry
= NULL
;
916 if (!gtk_check_version(2,4,0))
917 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
920 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
922 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
925 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
928 printf( "%d ", (int) (c
) );
936 void wxComboBox::SetValue( const wxString
& value
)
938 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
940 GtkEntry
*entry
= NULL
;
942 if (!gtk_check_version(2,4,0))
943 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
946 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
949 if (!value
.IsNull()) tmp
= value
;
950 gtk_entry_set_text( entry
, wxGTK_CONV( tmp
) );
952 InvalidateBestSize();
955 void wxComboBox::Copy()
957 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
959 GtkEntry
*entry
= NULL
;
961 if (!gtk_check_version(2,4,0))
962 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
965 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
967 gtk_editable_copy_clipboard(GTK_EDITABLE(entry
));
970 void wxComboBox::Cut()
972 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
974 GtkEntry
*entry
= NULL
;
976 if (!gtk_check_version(2,4,0))
977 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
980 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
982 gtk_editable_cut_clipboard(GTK_EDITABLE(entry
));
985 void wxComboBox::Paste()
987 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
989 GtkEntry
*entry
= NULL
;
991 if (!gtk_check_version(2,4,0))
992 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
995 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
997 gtk_editable_paste_clipboard(GTK_EDITABLE(entry
));
1000 void wxComboBox::Undo()
1005 void wxComboBox::Redo()
1010 void wxComboBox::SelectAll()
1012 SetSelection(0, GetLastPosition());
1015 bool wxComboBox::CanUndo() const
1021 bool wxComboBox::CanRedo() const
1027 bool wxComboBox::HasSelection() const
1030 GetSelection(&from
, &to
);
1034 bool wxComboBox::CanCopy() const
1036 // Can copy if there's a selection
1037 return HasSelection();
1040 bool wxComboBox::CanCut() const
1042 return CanCopy() && IsEditable();
1045 bool wxComboBox::CanPaste() const
1047 // TODO: check for text on the clipboard
1048 return IsEditable() ;
1051 bool wxComboBox::IsEditable() const
1053 return !HasFlag(wxCB_READONLY
);
1057 void wxComboBox::SetInsertionPoint( long pos
)
1059 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1061 if ( pos
== GetLastPosition() )
1064 GtkEntry
*entry
= NULL
;
1066 if (!gtk_check_version(2,4,0))
1067 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1070 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1072 gtk_entry_set_position( entry
, (int)pos
);
1075 long wxComboBox::GetInsertionPoint() const
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 return (long) gtk_editable_get_position(GTK_EDITABLE(entry
));
1088 wxTextPos
wxComboBox::GetLastPosition() 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 int pos
= entry
->text_length
;
1099 return (long) pos
-1;
1102 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
1104 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1106 GtkEntry
*entry
= NULL
;
1108 if (!gtk_check_version(2,4,0))
1109 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1112 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1114 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1115 if (value
.IsNull()) return;
1116 gint pos
= (gint
)to
;
1119 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
1120 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
1122 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.length(), &pos
);
1126 void wxComboBox::SetSelection( long from
, long to
)
1128 GtkEntry
*entry
= NULL
;
1130 if (!gtk_check_version(2,4,0))
1131 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1134 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1136 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1139 void wxComboBox::GetSelection( long* from
, long* to
) const
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
);
1151 GtkEditable
*editable
= GTK_EDITABLE(entry
);
1153 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
1159 void wxComboBox::SetEditable( bool editable
)
1161 GtkEntry
*entry
= NULL
;
1163 if (!gtk_check_version(2,4,0))
1164 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1167 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1169 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
1172 void wxComboBox::OnChar( wxKeyEvent
&event
)
1174 if ( event
.GetKeyCode() == WXK_RETURN
)
1176 // GTK automatically selects an item if its in the list
1177 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1178 eventEnter
.SetString( GetValue() );
1179 eventEnter
.SetInt( GetSelection() );
1180 eventEnter
.SetEventObject( this );
1182 if (!GetEventHandler()->ProcessEvent( eventEnter
))
1184 // This will invoke the dialog default action, such
1185 // as the clicking the default button.
1187 wxWindow
*top_frame
= m_parent
;
1188 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1189 top_frame
= top_frame
->GetParent();
1191 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1193 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1195 if (window
->default_widget
)
1196 gtk_widget_activate (window
->default_widget
);
1200 // Catch GTK event so that GTK doesn't open the drop
1201 // down list upon RETURN.
1208 void wxComboBox::DisableEvents()
1211 if (!gtk_check_version(2,4,0))
1213 g_signal_handlers_disconnect_by_func (GTK_BIN(m_widget
)->child
,
1214 (gpointer
)gtkcombobox_text_changed_callback
, this);
1216 g_signal_handlers_disconnect_by_func (m_widget
,
1217 (gpointer
)gtkcombobox_changed_callback
, this);
1222 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->list
,
1223 (gpointer
) gtkcombo_combo_select_child_callback
, this);
1225 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->entry
,
1226 (gpointer
) gtkcombo_text_changed_callback
, this);
1230 void wxComboBox::EnableEvents()
1233 if (!gtk_check_version(2,4,0))
1235 g_signal_connect_after (GTK_BIN(m_widget
)->child
, "changed",
1236 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
1238 g_signal_connect_after (m_widget
, "changed",
1239 G_CALLBACK (gtkcombobox_changed_callback
), this);
1244 g_signal_connect_after (GTK_COMBO(m_widget
)->list
, "select-child",
1245 G_CALLBACK (gtkcombo_combo_select_child_callback
),
1247 g_signal_connect_after (GTK_COMBO(m_widget
)->entry
, "changed",
1248 G_CALLBACK (gtkcombo_text_changed_callback
),
1253 void wxComboBox::OnSize( wxSizeEvent
&event
)
1256 if (!gtk_check_version(2,4,0))
1263 // NB: In some situations (e.g. on non-first page of a wizard, if the
1264 // size used is default size), GtkCombo widget is resized correctly,
1265 // but it's look is not updated, it's rendered as if it was much wider.
1266 // No other widgets are affected, so it looks like a bug in GTK+.
1267 // Manually requesting resize calculation (as gtk_pizza_set_size does)
1269 if (GTK_WIDGET_VISIBLE(m_widget
))
1270 gtk_widget_queue_resize(m_widget
);
1276 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1279 if (!gtk_check_version(2,4,0))
1286 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
1288 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
1289 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
1291 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
1292 GList
*child
= list
->children
;
1295 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1297 GtkBin
*bin
= GTK_BIN(child
->data
);
1298 gtk_widget_modify_style( bin
->child
, style
);
1300 child
= child
->next
;
1305 GtkWidget
* wxComboBox::GetConnectWidget()
1307 GtkEntry
*entry
= NULL
;
1309 if (!gtk_check_version(2,4,0))
1310 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1313 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1315 return GTK_WIDGET( entry
);
1318 GdkWindow
*wxComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
1321 if (!gtk_check_version(2,4,0))
1323 wxUnusedVar(windows
);
1325 return GTK_ENTRY(GTK_BIN(m_widget
)->child
)->text_area
;
1330 windows
.push_back(GTK_ENTRY(GTK_COMBO(m_widget
)->entry
)->text_area
);
1331 windows
.push_back(GTK_COMBO(m_widget
)->button
->window
);
1333 // indicate that we return multiple windows in the windows array
1338 wxSize
wxComboBox::DoGetBestSize() const
1340 wxSize
ret( wxControl::DoGetBestSize() );
1342 // we know better our horizontal extent: it depends on the longest string
1347 unsigned int count
= GetCount();
1348 for ( unsigned int n
= 0; n
< count
; n
++ )
1350 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
1351 if ( width
> ret
.x
)
1356 // empty combobox should have some reasonable default size too
1366 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1369 if (!gtk_check_version(2,4,0))
1370 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
1373 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
1376 // ----------------------------------------------------------------------------
1377 // standard event handling
1378 // ----------------------------------------------------------------------------
1380 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
1385 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1390 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1395 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1400 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1405 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1408 GetSelection(& from
, & to
);
1409 if (from
!= -1 && to
!= -1)
1413 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1415 SetSelection(-1, -1);
1418 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1420 event
.Enable( CanCut() );
1423 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1425 event
.Enable( CanCopy() );
1428 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1430 event
.Enable( CanPaste() );
1433 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1435 event
.Enable( CanUndo() );
1438 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1440 event
.Enable( CanRedo() );
1443 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1445 event
.Enable(HasSelection() && IsEditable()) ;
1448 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1450 event
.Enable(GetLastPosition() > 0);