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;
254 m_acceptsFocus
= true;
257 if (!PreCreation( parent
, pos
, size
) ||
258 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
260 wxFAIL_MSG( wxT("wxComboBox creation failed") );
265 if (!gtk_check_version(2,4,0))
267 m_widget
= gtk_combo_box_entry_new_text();
268 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
270 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget
)->child
), TRUE
);
272 for (int i
= 0; i
< n
; i
++)
274 gtk_combo_box_append_text( combobox
, wxGTK_CONV( choices
[i
] ) );
276 m_clientDataList
.Append( (wxObject
*)NULL
);
277 m_clientObjectList
.Append( (wxObject
*)NULL
);
283 m_widget
= gtk_combo_new();
284 GtkCombo
* combo
= GTK_COMBO(m_widget
);
286 // Disable GTK's broken events ...
287 g_signal_handler_disconnect (combo
->entry
, combo
->entry_change_id
);
288 // ... and add surrogate handler.
289 combo
->entry_change_id
= g_signal_connect (combo
->entry
, "changed",
290 G_CALLBACK (gtkcombo_dummy_callback
),
293 // make it more useable
294 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
296 // and case-sensitive
297 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
299 if (style
& wxNO_BORDER
)
300 g_object_set (combo
->entry
, "has-frame", FALSE
, NULL
);
302 GtkWidget
*list
= combo
->list
;
304 for (int i
= 0; i
< n
; i
++)
306 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
308 m_clientDataList
.Append( (wxObject
*)NULL
);
309 m_clientObjectList
.Append( (wxObject
*)NULL
);
311 gtk_container_add( GTK_CONTAINER(list
), list_item
);
313 gtk_widget_show( list_item
);
318 m_parent
->DoAddChild( this );
320 GtkEntry
*entry
= NULL
;
322 if (!gtk_check_version(2,4,0))
323 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
326 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
328 m_focusWidget
= GTK_WIDGET( entry
);
333 if (!gtk_check_version(2,4,0))
334 ConnectWidget( m_widget
);
337 ConnectWidget( GTK_COMBO(m_widget
)->button
);
340 if (!gtk_check_version(2,4,0))
342 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
344 if (style
& wxCB_READONLY
)
345 gtk_entry_set_editable( entry
, FALSE
);
347 g_signal_connect_after (entry
, "changed",
348 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
350 g_signal_connect_after (m_widget
, "changed",
351 G_CALLBACK (gtkcombobox_changed_callback
), this);
357 GtkCombo
*combo
= GTK_COMBO(m_widget
);
358 // MSW's combo box shows the value and the selection is -1
359 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
360 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
362 if (style
& wxCB_READONLY
)
363 gtk_entry_set_editable( entry
, FALSE
);
365 // "show" and "hide" events are generated when user click on the combobox button which popups a list
366 // this list is the "popwin" gtk widget
367 g_signal_connect (GTK_COMBO(combo
)->popwin
, "hide",
368 G_CALLBACK (gtkcombo_popup_hide_callback
), this);
369 g_signal_connect (GTK_COMBO(combo
)->popwin
, "show",
370 G_CALLBACK (gtkcombo_popup_show_callback
), this);
371 g_signal_connect_after (combo
->list
, "select-child",
372 G_CALLBACK (gtkcombo_combo_select_child_callback
),
374 g_signal_connect_after (entry
, "changed",
375 G_CALLBACK (gtkcombo_text_changed_callback
), this);
378 SetInitialSize(size
); // need this too because this is a wxControlWithItems
383 wxComboBox::~wxComboBox()
385 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
388 wxClientData
*cd
= (wxClientData
*)node
->GetData();
390 node
= node
->GetNext();
392 m_clientObjectList
.Clear();
394 m_clientDataList
.Clear();
397 void wxComboBox::SetFocus()
401 // don't do anything if we already have focus
405 gtk_widget_grab_focus( m_focusWidget
);
408 int wxComboBox::DoAppend( const wxString
&item
)
410 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
413 if (!gtk_check_version(2,4,0))
415 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
416 gtk_combo_box_append_text( combobox
, wxGTK_CONV( item
) );
423 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
424 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
426 gtk_container_add( GTK_CONTAINER(list
), list_item
);
428 if (GTK_WIDGET_REALIZED(m_widget
))
430 gtk_widget_realize( list_item
);
431 gtk_widget_realize( GTK_BIN(list_item
)->child
);
434 // Apply current widget style to the new list_item
435 GtkRcStyle
*style
= CreateWidgetStyle();
438 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
439 GtkBin
*bin
= GTK_BIN( list_item
);
440 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
441 gtk_widget_modify_style( label
, style
);
442 gtk_rc_style_unref( style
);
445 gtk_widget_show( list_item
);
450 const unsigned int count
= GetCount();
452 if ( m_clientDataList
.GetCount() < count
)
453 m_clientDataList
.Append( (wxObject
*) NULL
);
454 if ( m_clientObjectList
.GetCount() < count
)
455 m_clientObjectList
.Append( (wxObject
*) NULL
);
457 InvalidateBestSize();
462 int wxComboBox::DoInsert(const wxString
&item
, unsigned int pos
)
464 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
465 wxT("can't insert into sorted list"));
467 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
468 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("invalid index") );
470 unsigned int count
= GetCount();
476 if (!gtk_check_version(2,4,0))
478 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
479 gtk_combo_box_insert_text( combobox
, pos
, wxGTK_CONV( item
) );
486 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
487 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
489 GList
*gitem_list
= g_list_alloc ();
490 gitem_list
->data
= list_item
;
491 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
493 if (GTK_WIDGET_REALIZED(m_widget
))
495 gtk_widget_realize( list_item
);
496 gtk_widget_realize( GTK_BIN(list_item
)->child
);
501 gtk_widget_show( list_item
);
508 if ( m_clientDataList
.GetCount() < count
)
509 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
510 if ( m_clientObjectList
.GetCount() < count
)
511 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
513 InvalidateBestSize();
518 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
520 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
522 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
525 node
->SetData( (wxObject
*) clientData
);
528 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
530 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
532 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
534 return node
? node
->GetData() : NULL
;
537 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
539 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
541 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
544 // wxItemContainer already deletes data for us
546 node
->SetData( (wxObject
*) clientData
);
549 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
551 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
553 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
555 return node
? (wxClientData
*) node
->GetData() : NULL
;
558 void wxComboBox::Clear()
560 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
565 if (!gtk_check_version(2,4,0))
567 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
568 const unsigned int count
= GetCount();
569 for (unsigned int i
= 0; i
< count
; i
++)
570 gtk_combo_box_remove_text( combobox
, 0 );
573 #endif // __WXGTK24__
575 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
576 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
579 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
582 wxClientData
*cd
= (wxClientData
*)node
->GetData();
584 node
= node
->GetNext();
586 m_clientObjectList
.Clear();
588 m_clientDataList
.Clear();
592 InvalidateBestSize();
595 void wxComboBox::Delete(unsigned int n
)
597 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
600 if (!gtk_check_version(2,4,0))
602 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
604 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
605 gtk_combo_box_remove_text( combobox
, n
);
610 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
612 GList
*child
= g_list_nth( listbox
->children
, n
);
616 wxFAIL_MSG(wxT("wrong index"));
622 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
623 gtk_list_remove_items( listbox
, list
);
629 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
632 wxClientData
*cd
= (wxClientData
*)node
->GetData();
634 m_clientObjectList
.Erase( node
);
637 node
= m_clientDataList
.Item( n
);
639 m_clientDataList
.Erase( node
);
641 InvalidateBestSize();
644 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
646 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
649 if (!gtk_check_version(2,4,0))
651 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
652 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
654 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
656 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
658 GValue value
= { 0, };
659 g_value_init( &value
, G_TYPE_STRING
);
660 g_value_set_string( &value
, wxGTK_CONV( text
) );
661 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, 0, &value
);
662 g_value_unset( &value
);
668 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
670 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
673 GtkBin
*bin
= GTK_BIN( child
->data
);
674 GtkLabel
*label
= GTK_LABEL( bin
->child
);
675 gtk_label_set_text(label
, wxGTK_CONV(text
));
679 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
683 InvalidateBestSize();
686 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
688 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
691 if (!gtk_check_version(2,4,0))
693 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
694 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
696 gtk_tree_model_get_iter_first( model
, &iter
);
697 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
702 GValue value
= { 0, };
703 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
704 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
705 g_value_unset( &value
);
707 if (item
.IsSameAs( str
, bCase
) )
712 } while (gtk_tree_model_iter_next( model
, &iter
));
717 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
719 GList
*child
= GTK_LIST(list
)->children
;
723 GtkBin
*bin
= GTK_BIN( child
->data
);
724 GtkLabel
*label
= GTK_LABEL( bin
->child
);
725 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
727 if (item
.IsSameAs( str
, bCase
) )
738 int wxComboBox::GetSelection() const
741 if (!gtk_check_version(2,4,0))
743 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
744 return gtk_combo_box_get_active( combobox
);
748 // if the popup is currently opened, use the selection as it had been
749 // before it dropped down
750 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
751 : g_SelectionBeforePopup
;
754 int wxComboBox::GetCurrentSelection() const
756 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
759 if (!gtk_check_version(2,4,0))
761 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
762 return gtk_combo_box_get_active( combobox
);
767 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
769 GList
*selection
= GTK_LIST(list
)->selection
;
772 GList
*child
= GTK_LIST(list
)->children
;
776 if (child
->data
== selection
->data
) return count
;
786 wxString
wxComboBox::GetString(unsigned int n
) const
788 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
793 if (!gtk_check_version(2,4,0))
795 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
796 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
798 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
800 GValue value
= { 0, };
801 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
802 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
803 g_value_unset( &value
);
810 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
812 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
815 GtkBin
*bin
= GTK_BIN( child
->data
);
816 GtkLabel
*label
= GTK_LABEL( bin
->child
);
817 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
821 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
828 wxString
wxComboBox::GetStringSelection() const
830 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
833 if (!gtk_check_version(2,4,0))
835 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
836 int sel
= gtk_combo_box_get_active( combobox
);
838 return wxEmptyString
;
839 return GetString(sel
);
844 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
846 GList
*selection
= GTK_LIST(list
)->selection
;
849 GtkBin
*bin
= GTK_BIN( selection
->data
);
850 GtkLabel
*label
= GTK_LABEL( bin
->child
);
851 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
855 wxFAIL_MSG( wxT("wxComboBox: no selection") );
858 return wxEmptyString
;
861 unsigned int wxComboBox::GetCount() const
863 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
866 if (!gtk_check_version(2,4,0))
868 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
869 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
871 gtk_tree_model_get_iter_first( model
, &iter
);
872 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
874 unsigned int ret
= 1;
875 while (gtk_tree_model_iter_next( model
, &iter
))
882 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
884 GList
*child
= GTK_LIST(list
)->children
;
885 unsigned int count
= 0;
897 void wxComboBox::SetSelection( int n
)
899 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
904 if (!gtk_check_version(2,4,0))
906 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
907 gtk_combo_box_set_active( combobox
, n
);
912 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
913 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
914 gtk_list_select_item( GTK_LIST(list
), n
);
921 wxString
wxComboBox::GetValue() const
923 GtkEntry
*entry
= NULL
;
925 if (!gtk_check_version(2,4,0))
926 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
929 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
931 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
934 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
937 printf( "%d ", (int) (c
) );
945 void wxComboBox::SetValue( const wxString
& value
)
947 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
949 GtkEntry
*entry
= NULL
;
951 if (!gtk_check_version(2,4,0))
952 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
955 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
958 if (!value
.IsNull()) tmp
= value
;
959 gtk_entry_set_text( entry
, wxGTK_CONV( tmp
) );
961 InvalidateBestSize();
964 void wxComboBox::Copy()
966 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
968 GtkEntry
*entry
= NULL
;
970 if (!gtk_check_version(2,4,0))
971 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
974 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
976 gtk_editable_copy_clipboard(GTK_EDITABLE(entry
));
979 void wxComboBox::Cut()
981 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
983 GtkEntry
*entry
= NULL
;
985 if (!gtk_check_version(2,4,0))
986 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
989 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
991 gtk_editable_cut_clipboard(GTK_EDITABLE(entry
));
994 void wxComboBox::Paste()
996 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
998 GtkEntry
*entry
= NULL
;
1000 if (!gtk_check_version(2,4,0))
1001 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1004 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1006 gtk_editable_paste_clipboard(GTK_EDITABLE(entry
));
1009 void wxComboBox::Undo()
1014 void wxComboBox::Redo()
1019 void wxComboBox::SelectAll()
1021 SetSelection(0, GetLastPosition());
1024 bool wxComboBox::CanUndo() const
1030 bool wxComboBox::CanRedo() const
1036 bool wxComboBox::HasSelection() const
1039 GetSelection(&from
, &to
);
1043 bool wxComboBox::CanCopy() const
1045 // Can copy if there's a selection
1046 return HasSelection();
1049 bool wxComboBox::CanCut() const
1051 return CanCopy() && IsEditable();
1054 bool wxComboBox::CanPaste() const
1056 // TODO: check for text on the clipboard
1057 return IsEditable() ;
1060 bool wxComboBox::IsEditable() const
1062 return !HasFlag(wxCB_READONLY
);
1066 void wxComboBox::SetInsertionPoint( long pos
)
1068 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1070 if ( pos
== GetLastPosition() )
1073 GtkEntry
*entry
= NULL
;
1075 if (!gtk_check_version(2,4,0))
1076 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1079 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1081 gtk_entry_set_position( entry
, (int)pos
);
1084 long wxComboBox::GetInsertionPoint() const
1086 GtkEntry
*entry
= NULL
;
1088 if (!gtk_check_version(2,4,0))
1089 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1092 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1094 return (long) gtk_editable_get_position(GTK_EDITABLE(entry
));
1097 wxTextPos
wxComboBox::GetLastPosition() const
1099 GtkEntry
*entry
= NULL
;
1101 if (!gtk_check_version(2,4,0))
1102 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1105 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1107 int pos
= entry
->text_length
;
1108 return (long) pos
-1;
1111 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
1113 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1115 GtkEntry
*entry
= NULL
;
1117 if (!gtk_check_version(2,4,0))
1118 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1121 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1123 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1124 if (value
.IsNull()) return;
1125 gint pos
= (gint
)to
;
1128 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
1129 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
1131 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.length(), &pos
);
1135 void wxComboBox::SetSelection( long from
, long to
)
1137 GtkEntry
*entry
= NULL
;
1139 if (!gtk_check_version(2,4,0))
1140 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1143 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1145 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1148 void wxComboBox::GetSelection( long* from
, long* to
) const
1150 GtkEntry
*entry
= NULL
;
1152 if (!gtk_check_version(2,4,0))
1153 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1156 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1160 GtkEditable
*editable
= GTK_EDITABLE(entry
);
1162 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
1168 void wxComboBox::SetEditable( bool editable
)
1170 GtkEntry
*entry
= NULL
;
1172 if (!gtk_check_version(2,4,0))
1173 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1176 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1178 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
1181 void wxComboBox::OnChar( wxKeyEvent
&event
)
1183 if ( event
.GetKeyCode() == WXK_RETURN
)
1185 // GTK automatically selects an item if its in the list
1186 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1187 eventEnter
.SetString( GetValue() );
1188 eventEnter
.SetInt( GetSelection() );
1189 eventEnter
.SetEventObject( this );
1191 if (!GetEventHandler()->ProcessEvent( eventEnter
))
1193 // This will invoke the dialog default action, such
1194 // as the clicking the default button.
1196 wxWindow
*top_frame
= m_parent
;
1197 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1198 top_frame
= top_frame
->GetParent();
1200 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1202 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1204 if (window
->default_widget
)
1205 gtk_widget_activate (window
->default_widget
);
1209 // Catch GTK event so that GTK doesn't open the drop
1210 // down list upon RETURN.
1217 void wxComboBox::DisableEvents()
1220 if (!gtk_check_version(2,4,0))
1222 g_signal_handlers_disconnect_by_func (GTK_BIN(m_widget
)->child
,
1223 (gpointer
)gtkcombobox_text_changed_callback
, this);
1225 g_signal_handlers_disconnect_by_func (m_widget
,
1226 (gpointer
)gtkcombobox_changed_callback
, this);
1231 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->list
,
1232 (gpointer
) gtkcombo_combo_select_child_callback
, this);
1234 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->entry
,
1235 (gpointer
) gtkcombo_text_changed_callback
, this);
1239 void wxComboBox::EnableEvents()
1242 if (!gtk_check_version(2,4,0))
1244 g_signal_connect_after (GTK_BIN(m_widget
)->child
, "changed",
1245 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
1247 g_signal_connect_after (m_widget
, "changed",
1248 G_CALLBACK (gtkcombobox_changed_callback
), this);
1253 g_signal_connect_after (GTK_COMBO(m_widget
)->list
, "select-child",
1254 G_CALLBACK (gtkcombo_combo_select_child_callback
),
1256 g_signal_connect_after (GTK_COMBO(m_widget
)->entry
, "changed",
1257 G_CALLBACK (gtkcombo_text_changed_callback
),
1262 void wxComboBox::OnSize( wxSizeEvent
&event
)
1265 if (!gtk_check_version(2,4,0))
1272 // NB: In some situations (e.g. on non-first page of a wizard, if the
1273 // size used is default size), GtkCombo widget is resized correctly,
1274 // but it's look is not updated, it's rendered as if it was much wider.
1275 // No other widgets are affected, so it looks like a bug in GTK+.
1276 // Manually requesting resize calculation (as gtk_pizza_set_size does)
1278 if (GTK_WIDGET_VISIBLE(m_widget
))
1279 gtk_widget_queue_resize(m_widget
);
1285 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1288 if (!gtk_check_version(2,4,0))
1295 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
1297 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
1298 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
1300 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
1301 GList
*child
= list
->children
;
1304 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1306 GtkBin
*bin
= GTK_BIN(child
->data
);
1307 gtk_widget_modify_style( bin
->child
, style
);
1309 child
= child
->next
;
1314 GtkWidget
* wxComboBox::GetConnectWidget()
1316 GtkEntry
*entry
= NULL
;
1318 if (!gtk_check_version(2,4,0))
1319 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1322 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1324 return GTK_WIDGET( entry
);
1327 GdkWindow
*wxComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
1330 if (!gtk_check_version(2,4,0))
1332 wxUnusedVar(windows
);
1334 return GTK_ENTRY(GTK_BIN(m_widget
)->child
)->text_area
;
1339 windows
.push_back(GTK_ENTRY(GTK_COMBO(m_widget
)->entry
)->text_area
);
1340 windows
.push_back(GTK_COMBO(m_widget
)->button
->window
);
1342 // indicate that we return multiple windows in the windows array
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);