1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/combobox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/combobox.h"
19 #include "wx/settings.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
21 #include "wx/arrstr.h"
24 // We use GtkCombo which has been deprecated since GTK+ 2.3.0
25 // in favour of GtkComboBox for <GTK2.4 runtime
26 // We also use GtkList
27 #ifdef GTK_DISABLE_DEPRECATED
28 #undef GTK_DISABLE_DEPRECATED
30 #include "wx/gtk/private.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
37 static int g_SelectionBeforePopup
= wxID_NONE
; // this means the popup is hidden
39 //-----------------------------------------------------------------------------
40 // "changed" - typing and list item matches get changed, select-child
41 // if it doesn't match an item then just get a single changed
42 //-----------------------------------------------------------------------------
46 gtkcombo_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
48 if (g_isIdle
) wxapp_install_idle_handler();
50 if (combo
->m_ignoreNextUpdate
)
52 combo
->m_ignoreNextUpdate
= false;
56 if (!combo
->m_hasVMT
) return;
58 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
59 event
.SetString( combo
->GetValue() );
60 event
.SetEventObject( combo
);
61 combo
->GetEventHandler()->ProcessEvent( event
);
67 gtkcombo_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
74 gtkcombo_popup_hide_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
76 // when the popup is hidden, throw a SELECTED event only if the combobox
78 const int curSelection
= combo
->GetCurrentSelection();
80 const bool hasChanged
= curSelection
!= g_SelectionBeforePopup
;
82 // reset the selection flag to value meaning that it is hidden and do it
83 // now, before generating the events, so that GetSelection() returns the
84 // new value from the event handler
85 g_SelectionBeforePopup
= wxID_NONE
;
89 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
90 event
.SetInt( curSelection
);
91 event
.SetString( combo
->GetStringSelection() );
92 event
.SetEventObject( combo
);
93 combo
->GetEventHandler()->ProcessEvent( event
);
95 // for consistency with the other ports, send TEXT event
96 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
97 event2
.SetString( combo
->GetStringSelection() );
98 event2
.SetEventObject( combo
);
99 combo
->GetEventHandler()->ProcessEvent( event2
);
106 gtkcombo_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
108 // store the combobox selection value before the popup is shown
109 g_SelectionBeforePopup
= combo
->GetCurrentSelection();
113 //-----------------------------------------------------------------------------
114 // "select-child" - click/cursor get select-child, changed, select-child
115 //-----------------------------------------------------------------------------
119 gtkcombo_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
121 if (g_isIdle
) wxapp_install_idle_handler();
123 if (!combo
->m_hasVMT
) return;
125 if (g_blockEventsOnDrag
) return;
127 int curSelection
= combo
->GetCurrentSelection();
129 if (combo
->m_prevSelection
== curSelection
) return;
131 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
132 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
134 combo
->m_prevSelection
= curSelection
;
136 // Quickly set the value of the combo box
137 // as GTK+ does that only AFTER the event
139 g_signal_handlers_disconnect_by_func (GTK_COMBO (combo
->GetHandle())->entry
,
140 (gpointer
) gtkcombo_text_changed_callback
,
142 combo
->SetValue( combo
->GetStringSelection() );
143 g_signal_connect_after (GTK_COMBO (combo
->GetHandle())->entry
, "changed",
144 G_CALLBACK (gtkcombo_text_changed_callback
), combo
);
146 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
147 // because when combobox popup is shown, gtkcombo_combo_select_child_callback is
148 // called each times the mouse is over an item with a pressed button so a lot
149 // of SELECTED event could be generated if the user keep the mouse button down
150 // and select other items ...
151 if (g_SelectionBeforePopup
== wxID_NONE
)
153 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
154 event
.SetInt( curSelection
);
155 event
.SetString( combo
->GetStringSelection() );
156 event
.SetEventObject( combo
);
157 combo
->GetEventHandler()->ProcessEvent( event
);
159 // for consistency with the other ports, don't generate text update
160 // events while the user is browsing the combobox neither
161 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
162 event2
.SetString( combo
->GetValue() );
163 event2
.SetEventObject( combo
);
164 combo
->GetEventHandler()->ProcessEvent( event2
);
172 gtkcombobox_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
174 if (g_isIdle
) wxapp_install_idle_handler();
176 if (!combo
->m_hasVMT
) return;
178 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
179 event
.SetString( combo
->GetValue() );
180 event
.SetEventObject( combo
);
181 combo
->GetEventHandler()->ProcessEvent( event
);
187 gtkcombobox_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
189 if (g_isIdle
) wxapp_install_idle_handler();
191 if (!combo
->m_hasVMT
) return;
193 if (combo
->GetSelection() == -1)
196 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
197 event
.SetInt( combo
->GetSelection() );
198 event
.SetString( combo
->GetStringSelection() );
199 event
.SetEventObject( combo
);
200 combo
->GetEventHandler()->ProcessEvent( event
);
206 //-----------------------------------------------------------------------------
208 //-----------------------------------------------------------------------------
210 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
212 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
213 EVT_SIZE(wxComboBox::OnSize
)
214 EVT_CHAR(wxComboBox::OnChar
)
216 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
217 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
218 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
219 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
220 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
221 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
222 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
224 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
225 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
226 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
227 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
228 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
229 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
230 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
233 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
234 const wxString
& value
,
235 const wxPoint
& pos
, const wxSize
& size
,
236 const wxArrayString
& choices
,
237 long style
, const wxValidator
& validator
,
238 const wxString
& name
)
240 wxCArrayString
chs(choices
);
242 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
243 chs
.GetStrings(), style
, validator
, name
);
246 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
247 const wxPoint
& pos
, const wxSize
& size
,
248 int n
, const wxString choices
[],
249 long style
, const wxValidator
& validator
,
250 const wxString
& name
)
252 m_ignoreNextUpdate
= false;
256 if (!PreCreation( parent
, pos
, size
) ||
257 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
259 wxFAIL_MSG( wxT("wxComboBox creation failed") );
264 if (!gtk_check_version(2,4,0))
266 m_widget
= gtk_combo_box_entry_new_text();
267 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
269 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget
)->child
), TRUE
);
271 for (int i
= 0; i
< n
; i
++)
273 gtk_combo_box_append_text( combobox
, wxGTK_CONV( choices
[i
] ) );
275 m_clientDataList
.Append( (wxObject
*)NULL
);
276 m_clientObjectList
.Append( (wxObject
*)NULL
);
282 m_widget
= gtk_combo_new();
283 GtkCombo
* combo
= GTK_COMBO(m_widget
);
285 // Disable GTK's broken events ...
286 g_signal_handler_disconnect (combo
->entry
, combo
->entry_change_id
);
287 // ... and add surrogate handler.
288 combo
->entry_change_id
= g_signal_connect (combo
->entry
, "changed",
289 G_CALLBACK (gtkcombo_dummy_callback
),
292 // make it more useable
293 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
295 // and case-sensitive
296 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
298 if (style
& wxNO_BORDER
)
299 g_object_set (combo
->entry
, "has-frame", FALSE
, NULL
);
301 GtkWidget
*list
= combo
->list
;
303 for (int i
= 0; i
< n
; i
++)
305 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
307 m_clientDataList
.Append( (wxObject
*)NULL
);
308 m_clientObjectList
.Append( (wxObject
*)NULL
);
310 gtk_container_add( GTK_CONTAINER(list
), list_item
);
312 gtk_widget_show( list_item
);
317 m_parent
->DoAddChild( this );
319 GtkEntry
*entry
= NULL
;
321 if (!gtk_check_version(2,4,0))
322 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
325 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
327 m_focusWidget
= GTK_WIDGET( entry
);
332 if (!gtk_check_version(2,4,0))
333 ConnectWidget( m_widget
);
336 ConnectWidget( GTK_COMBO(m_widget
)->button
);
339 if (!gtk_check_version(2,4,0))
341 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
343 if (style
& wxCB_READONLY
)
344 gtk_entry_set_editable( entry
, FALSE
);
346 g_signal_connect_after (entry
, "changed",
347 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
349 g_signal_connect_after (m_widget
, "changed",
350 G_CALLBACK (gtkcombobox_changed_callback
), this);
356 GtkCombo
*combo
= GTK_COMBO(m_widget
);
357 // MSW's combo box shows the value and the selection is -1
358 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
359 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
361 if (style
& wxCB_READONLY
)
362 gtk_entry_set_editable( entry
, FALSE
);
364 // "show" and "hide" events are generated when user click on the combobox button which popups a list
365 // this list is the "popwin" gtk widget
366 g_signal_connect (GTK_COMBO(combo
)->popwin
, "hide",
367 G_CALLBACK (gtkcombo_popup_hide_callback
), this);
368 g_signal_connect (GTK_COMBO(combo
)->popwin
, "show",
369 G_CALLBACK (gtkcombo_popup_show_callback
), this);
370 g_signal_connect_after (combo
->list
, "select-child",
371 G_CALLBACK (gtkcombo_combo_select_child_callback
),
373 g_signal_connect_after (entry
, "changed",
374 G_CALLBACK (gtkcombo_text_changed_callback
), this);
377 SetInitialSize(size
); // need this too because this is a wxControlWithItems
382 wxComboBox::~wxComboBox()
384 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
387 wxClientData
*cd
= (wxClientData
*)node
->GetData();
389 node
= node
->GetNext();
391 m_clientObjectList
.Clear();
393 m_clientDataList
.Clear();
396 void wxComboBox::SetFocus()
400 // don't do anything if we already have focus
404 gtk_widget_grab_focus( m_focusWidget
);
407 int wxComboBox::DoAppend( const wxString
&item
)
409 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
412 if (!gtk_check_version(2,4,0))
414 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
415 gtk_combo_box_append_text( combobox
, wxGTK_CONV( item
) );
422 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
423 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
425 gtk_container_add( GTK_CONTAINER(list
), list_item
);
427 if (GTK_WIDGET_REALIZED(m_widget
))
429 gtk_widget_realize( list_item
);
430 gtk_widget_realize( GTK_BIN(list_item
)->child
);
433 // Apply current widget style to the new list_item
434 GtkRcStyle
*style
= CreateWidgetStyle();
437 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
438 GtkBin
*bin
= GTK_BIN( list_item
);
439 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
440 gtk_widget_modify_style( label
, style
);
441 gtk_rc_style_unref( style
);
444 gtk_widget_show( list_item
);
449 const unsigned int count
= GetCount();
451 if ( m_clientDataList
.GetCount() < count
)
452 m_clientDataList
.Append( (wxObject
*) NULL
);
453 if ( m_clientObjectList
.GetCount() < count
)
454 m_clientObjectList
.Append( (wxObject
*) NULL
);
456 InvalidateBestSize();
461 int wxComboBox::DoInsert(const wxString
&item
, unsigned int pos
)
463 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
464 wxT("can't insert into sorted list"));
466 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
467 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("invalid index") );
469 unsigned int count
= GetCount();
475 if (!gtk_check_version(2,4,0))
477 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
478 gtk_combo_box_insert_text( combobox
, pos
, wxGTK_CONV( item
) );
485 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
486 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
488 GList
*gitem_list
= g_list_alloc ();
489 gitem_list
->data
= list_item
;
490 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
492 if (GTK_WIDGET_REALIZED(m_widget
))
494 gtk_widget_realize( list_item
);
495 gtk_widget_realize( GTK_BIN(list_item
)->child
);
500 gtk_widget_show( list_item
);
507 if ( m_clientDataList
.GetCount() < count
)
508 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
509 if ( m_clientObjectList
.GetCount() < count
)
510 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
512 InvalidateBestSize();
517 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
519 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
521 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
524 node
->SetData( (wxObject
*) clientData
);
527 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
529 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
531 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
533 return node
? node
->GetData() : NULL
;
536 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
538 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
540 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
543 // wxItemContainer already deletes data for us
545 node
->SetData( (wxObject
*) clientData
);
548 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
550 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
552 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
554 return node
? (wxClientData
*) node
->GetData() : NULL
;
557 void wxComboBox::Clear()
559 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
564 if (!gtk_check_version(2,4,0))
566 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
567 const unsigned int count
= GetCount();
568 for (unsigned int i
= 0; i
< count
; i
++)
569 gtk_combo_box_remove_text( combobox
, 0 );
572 #endif // __WXGTK24__
574 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
575 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
578 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
581 wxClientData
*cd
= (wxClientData
*)node
->GetData();
583 node
= node
->GetNext();
585 m_clientObjectList
.Clear();
587 m_clientDataList
.Clear();
591 InvalidateBestSize();
594 void wxComboBox::Delete(unsigned int n
)
596 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
599 if (!gtk_check_version(2,4,0))
601 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
603 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
604 gtk_combo_box_remove_text( combobox
, n
);
609 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
611 GList
*child
= g_list_nth( listbox
->children
, n
);
615 wxFAIL_MSG(wxT("wrong index"));
621 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
622 gtk_list_remove_items( listbox
, list
);
628 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
631 wxClientData
*cd
= (wxClientData
*)node
->GetData();
633 m_clientObjectList
.Erase( node
);
636 node
= m_clientDataList
.Item( n
);
638 m_clientDataList
.Erase( node
);
640 InvalidateBestSize();
643 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
645 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
648 if (!gtk_check_version(2,4,0))
650 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
651 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
653 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
655 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
657 GValue value
= { 0, };
658 g_value_init( &value
, G_TYPE_STRING
);
659 g_value_set_string( &value
, wxGTK_CONV( text
) );
660 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, 0, &value
);
661 g_value_unset( &value
);
667 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
669 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
672 GtkBin
*bin
= GTK_BIN( child
->data
);
673 GtkLabel
*label
= GTK_LABEL( bin
->child
);
674 gtk_label_set_text(label
, wxGTK_CONV(text
));
678 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
682 InvalidateBestSize();
685 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
687 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
690 if (!gtk_check_version(2,4,0))
692 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
693 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
695 gtk_tree_model_get_iter_first( model
, &iter
);
696 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
701 GValue value
= { 0, };
702 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
703 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
704 g_value_unset( &value
);
706 if (item
.IsSameAs( str
, bCase
) )
711 } while (gtk_tree_model_iter_next( model
, &iter
));
716 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
718 GList
*child
= GTK_LIST(list
)->children
;
722 GtkBin
*bin
= GTK_BIN( child
->data
);
723 GtkLabel
*label
= GTK_LABEL( bin
->child
);
724 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
726 if (item
.IsSameAs( str
, bCase
) )
737 int wxComboBox::GetSelection() const
740 if (!gtk_check_version(2,4,0))
742 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
743 return gtk_combo_box_get_active( combobox
);
747 // if the popup is currently opened, use the selection as it had been
748 // before it dropped down
749 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
750 : g_SelectionBeforePopup
;
753 int wxComboBox::GetCurrentSelection() const
755 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
758 if (!gtk_check_version(2,4,0))
760 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
761 return gtk_combo_box_get_active( combobox
);
766 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
768 GList
*selection
= GTK_LIST(list
)->selection
;
771 GList
*child
= GTK_LIST(list
)->children
;
775 if (child
->data
== selection
->data
) return count
;
785 wxString
wxComboBox::GetString(unsigned int n
) const
787 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
792 if (!gtk_check_version(2,4,0))
794 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
795 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
797 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
799 GValue value
= { 0, };
800 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
801 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
802 g_value_unset( &value
);
809 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
811 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
814 GtkBin
*bin
= GTK_BIN( child
->data
);
815 GtkLabel
*label
= GTK_LABEL( bin
->child
);
816 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
820 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
827 wxString
wxComboBox::GetStringSelection() const
829 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
832 if (!gtk_check_version(2,4,0))
834 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
835 int sel
= gtk_combo_box_get_active( combobox
);
837 return wxEmptyString
;
838 return GetString(sel
);
843 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
845 GList
*selection
= GTK_LIST(list
)->selection
;
848 GtkBin
*bin
= GTK_BIN( selection
->data
);
849 GtkLabel
*label
= GTK_LABEL( bin
->child
);
850 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
854 wxFAIL_MSG( wxT("wxComboBox: no selection") );
857 return wxEmptyString
;
860 unsigned int wxComboBox::GetCount() const
862 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
865 if (!gtk_check_version(2,4,0))
867 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
868 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
870 gtk_tree_model_get_iter_first( model
, &iter
);
871 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
873 unsigned int ret
= 1;
874 while (gtk_tree_model_iter_next( model
, &iter
))
881 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
883 GList
*child
= GTK_LIST(list
)->children
;
884 unsigned int count
= 0;
896 void wxComboBox::SetSelection( int n
)
898 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
903 if (!gtk_check_version(2,4,0))
905 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
906 gtk_combo_box_set_active( combobox
, n
);
911 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
912 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
913 gtk_list_select_item( GTK_LIST(list
), n
);
920 wxString
wxComboBox::GetValue() const
922 GtkEntry
*entry
= NULL
;
924 if (!gtk_check_version(2,4,0))
925 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
928 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
930 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
933 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
936 printf( "%d ", (int) (c
) );
944 void wxComboBox::SetValue( const wxString
& value
)
946 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
948 GtkEntry
*entry
= NULL
;
950 if (!gtk_check_version(2,4,0))
951 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
954 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
957 if (!value
.IsNull()) tmp
= value
;
958 gtk_entry_set_text( entry
, wxGTK_CONV( tmp
) );
960 InvalidateBestSize();
963 void wxComboBox::Copy()
965 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
967 GtkEntry
*entry
= NULL
;
969 if (!gtk_check_version(2,4,0))
970 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
973 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
975 gtk_editable_copy_clipboard(GTK_EDITABLE(entry
));
978 void wxComboBox::Cut()
980 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
982 GtkEntry
*entry
= NULL
;
984 if (!gtk_check_version(2,4,0))
985 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
988 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
990 gtk_editable_cut_clipboard(GTK_EDITABLE(entry
));
993 void wxComboBox::Paste()
995 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
997 GtkEntry
*entry
= NULL
;
999 if (!gtk_check_version(2,4,0))
1000 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1003 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1005 gtk_editable_paste_clipboard(GTK_EDITABLE(entry
));
1008 void wxComboBox::Undo()
1013 void wxComboBox::Redo()
1018 void wxComboBox::SelectAll()
1020 SetSelection(0, GetLastPosition());
1023 bool wxComboBox::CanUndo() const
1029 bool wxComboBox::CanRedo() const
1035 bool wxComboBox::HasSelection() const
1038 GetSelection(&from
, &to
);
1042 bool wxComboBox::CanCopy() const
1044 // Can copy if there's a selection
1045 return HasSelection();
1048 bool wxComboBox::CanCut() const
1050 return CanCopy() && IsEditable();
1053 bool wxComboBox::CanPaste() const
1055 // TODO: check for text on the clipboard
1056 return IsEditable() ;
1059 bool wxComboBox::IsEditable() const
1061 return !HasFlag(wxCB_READONLY
);
1065 void wxComboBox::SetInsertionPoint( long pos
)
1067 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1069 if ( pos
== GetLastPosition() )
1072 GtkEntry
*entry
= NULL
;
1074 if (!gtk_check_version(2,4,0))
1075 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1078 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1080 gtk_entry_set_position( entry
, (int)pos
);
1083 long wxComboBox::GetInsertionPoint() const
1085 GtkEntry
*entry
= NULL
;
1087 if (!gtk_check_version(2,4,0))
1088 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1091 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1093 return (long) gtk_editable_get_position(GTK_EDITABLE(entry
));
1096 wxTextPos
wxComboBox::GetLastPosition() const
1098 GtkEntry
*entry
= NULL
;
1100 if (!gtk_check_version(2,4,0))
1101 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1104 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1106 int pos
= entry
->text_length
;
1107 return (long) pos
-1;
1110 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
1112 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1114 GtkEntry
*entry
= NULL
;
1116 if (!gtk_check_version(2,4,0))
1117 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1120 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1122 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1123 if (value
.IsNull()) return;
1124 gint pos
= (gint
)to
;
1127 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
1128 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
1130 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.length(), &pos
);
1134 void wxComboBox::SetSelection( long from
, long to
)
1136 GtkEntry
*entry
= NULL
;
1138 if (!gtk_check_version(2,4,0))
1139 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1142 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1144 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1147 void wxComboBox::GetSelection( long* from
, long* to
) const
1149 GtkEntry
*entry
= NULL
;
1151 if (!gtk_check_version(2,4,0))
1152 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1155 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1159 GtkEditable
*editable
= GTK_EDITABLE(entry
);
1161 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
1167 void wxComboBox::SetEditable( bool editable
)
1169 GtkEntry
*entry
= NULL
;
1171 if (!gtk_check_version(2,4,0))
1172 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1175 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1177 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
1180 void wxComboBox::OnChar( wxKeyEvent
&event
)
1182 if ( event
.GetKeyCode() == WXK_RETURN
)
1184 // GTK automatically selects an item if its in the list
1185 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1186 eventEnter
.SetString( GetValue() );
1187 eventEnter
.SetInt( GetSelection() );
1188 eventEnter
.SetEventObject( this );
1190 if (!GetEventHandler()->ProcessEvent( eventEnter
))
1192 // This will invoke the dialog default action, such
1193 // as the clicking the default button.
1195 wxWindow
*top_frame
= m_parent
;
1196 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1197 top_frame
= top_frame
->GetParent();
1199 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1201 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1203 if (window
->default_widget
)
1204 gtk_widget_activate (window
->default_widget
);
1208 // Catch GTK event so that GTK doesn't open the drop
1209 // down list upon RETURN.
1216 void wxComboBox::DisableEvents()
1219 if (!gtk_check_version(2,4,0))
1221 g_signal_handlers_disconnect_by_func (GTK_BIN(m_widget
)->child
,
1222 (gpointer
)gtkcombobox_text_changed_callback
, this);
1224 g_signal_handlers_disconnect_by_func (m_widget
,
1225 (gpointer
)gtkcombobox_changed_callback
, this);
1230 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->list
,
1231 (gpointer
) gtkcombo_combo_select_child_callback
, this);
1233 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->entry
,
1234 (gpointer
) gtkcombo_text_changed_callback
, this);
1238 void wxComboBox::EnableEvents()
1241 if (!gtk_check_version(2,4,0))
1243 g_signal_connect_after (GTK_BIN(m_widget
)->child
, "changed",
1244 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
1246 g_signal_connect_after (m_widget
, "changed",
1247 G_CALLBACK (gtkcombobox_changed_callback
), this);
1252 g_signal_connect_after (GTK_COMBO(m_widget
)->list
, "select-child",
1253 G_CALLBACK (gtkcombo_combo_select_child_callback
),
1255 g_signal_connect_after (GTK_COMBO(m_widget
)->entry
, "changed",
1256 G_CALLBACK (gtkcombo_text_changed_callback
),
1261 void wxComboBox::OnSize( wxSizeEvent
&event
)
1264 if (!gtk_check_version(2,4,0))
1271 // NB: In some situations (e.g. on non-first page of a wizard, if the
1272 // size used is default size), GtkCombo widget is resized correctly,
1273 // but it's look is not updated, it's rendered as if it was much wider.
1274 // No other widgets are affected, so it looks like a bug in GTK+.
1275 // Manually requesting resize calculation (as gtk_pizza_set_size does)
1277 if (GTK_WIDGET_VISIBLE(m_widget
))
1278 gtk_widget_queue_resize(m_widget
);
1284 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1287 if (!gtk_check_version(2,4,0))
1294 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
1296 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
1297 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
1299 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
1300 GList
*child
= list
->children
;
1303 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1305 GtkBin
*bin
= GTK_BIN(child
->data
);
1306 gtk_widget_modify_style( bin
->child
, style
);
1308 child
= child
->next
;
1313 GtkWidget
* wxComboBox::GetConnectWidget()
1315 GtkEntry
*entry
= NULL
;
1317 if (!gtk_check_version(2,4,0))
1318 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1321 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1323 return GTK_WIDGET( entry
);
1326 GdkWindow
*wxComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
1329 if (!gtk_check_version(2,4,0))
1331 wxUnusedVar(windows
);
1333 return GTK_ENTRY(GTK_BIN(m_widget
)->child
)->text_area
;
1338 windows
.push_back(GTK_ENTRY(GTK_COMBO(m_widget
)->entry
)->text_area
);
1339 windows
.push_back(GTK_COMBO(m_widget
)->button
->window
);
1341 // indicate that we return multiple windows in the windows array
1346 wxSize
wxComboBox::DoGetBestSize() const
1348 wxSize
ret( wxControl::DoGetBestSize() );
1350 // we know better our horizontal extent: it depends on the longest string
1355 unsigned int count
= GetCount();
1356 for ( unsigned int n
= 0; n
< count
; n
++ )
1358 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
1359 if ( width
> ret
.x
)
1364 // empty combobox should have some reasonable default size too
1374 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1377 if (!gtk_check_version(2,4,0))
1378 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
1381 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
1384 // ----------------------------------------------------------------------------
1385 // standard event handling
1386 // ----------------------------------------------------------------------------
1388 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
1393 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1398 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1403 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1408 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1413 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1416 GetSelection(& from
, & to
);
1417 if (from
!= -1 && to
!= -1)
1421 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1423 SetSelection(-1, -1);
1426 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1428 event
.Enable( CanCut() );
1431 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1433 event
.Enable( CanCopy() );
1436 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1438 event
.Enable( CanPaste() );
1441 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1443 event
.Enable( CanUndo() );
1446 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1448 event
.Enable( CanRedo() );
1451 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1453 event
.Enable(HasSelection() && IsEditable()) ;
1456 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1458 event
.Enable(GetLastPosition() > 0);