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 GtkWidget
* entry
= GTK_COMBO(combo
->GetHandle())->entry
;
136 g_signal_handlers_block_by_func(
137 entry
, (gpointer
)gtkcombo_text_changed_callback
, combo
);
138 combo
->SetValue( combo
->GetStringSelection() );
139 g_signal_handlers_unblock_by_func(
140 entry
, (gpointer
)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
)
245 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") );
255 if(HasFlag(wxCB_SORT
))
256 m_strings
= new wxSortedArrayString();
259 if (!gtk_check_version(2,4,0))
261 m_widget
= gtk_combo_box_entry_new_text();
263 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget
)->child
), TRUE
);
268 m_widget
= gtk_combo_new();
269 GtkCombo
* combo
= GTK_COMBO(m_widget
);
271 // Disable GTK's broken events ...
272 g_signal_handler_disconnect (combo
->entry
, combo
->entry_change_id
);
273 // ... and add surrogate handler.
274 combo
->entry_change_id
= g_signal_connect (combo
->entry
, "changed",
275 G_CALLBACK (gtkcombo_dummy_callback
),
278 // make it more useable
279 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
281 // and case-sensitive
282 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
284 if (style
& wxNO_BORDER
)
285 g_object_set (combo
->entry
, "has-frame", FALSE
, NULL
);
290 m_parent
->DoAddChild( this );
292 GtkEntry
*entry
= NULL
;
294 if (!gtk_check_version(2,4,0))
295 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
298 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
300 m_focusWidget
= GTK_WIDGET( entry
);
305 if (!gtk_check_version(2,4,0))
306 ConnectWidget( m_widget
);
309 ConnectWidget( GTK_COMBO(m_widget
)->button
);
312 if (!gtk_check_version(2,4,0))
314 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
316 if (style
& wxCB_READONLY
)
317 gtk_entry_set_editable( entry
, FALSE
);
319 g_signal_connect_after (entry
, "changed",
320 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
322 g_signal_connect_after (m_widget
, "changed",
323 G_CALLBACK (gtkcombobox_changed_callback
), this);
329 GtkCombo
*combo
= GTK_COMBO(m_widget
);
330 // MSW's combo box shows the value and the selection is -1
331 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
332 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
334 if (style
& wxCB_READONLY
)
335 gtk_entry_set_editable( entry
, FALSE
);
337 // "show" and "hide" events are generated when user click on the combobox button which popups a list
338 // this list is the "popwin" gtk widget
339 g_signal_connect (GTK_COMBO(combo
)->popwin
, "hide",
340 G_CALLBACK (gtkcombo_popup_hide_callback
), this);
341 g_signal_connect (GTK_COMBO(combo
)->popwin
, "show",
342 G_CALLBACK (gtkcombo_popup_show_callback
), this);
343 g_signal_connect_after (combo
->list
, "select-child",
344 G_CALLBACK (gtkcombo_combo_select_child_callback
),
346 g_signal_connect_after (entry
, "changed",
347 G_CALLBACK (gtkcombo_text_changed_callback
), this);
350 SetInitialSize(size
); // need this too because this is a wxControlWithItems
355 wxComboBox::~wxComboBox()
362 void wxComboBox::SetFocus()
366 // don't do anything if we already have focus
370 gtk_widget_grab_focus( m_focusWidget
);
373 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
375 void **clientData
, wxClientDataType type
)
377 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
379 wxASSERT_MSG( !IsSorted() || (pos
== GetCount()),
380 _T("In a sorted combobox data could only be appended"));
382 const int count
= items
.GetCount();
387 if (!gtk_check_version(2,4,0))
389 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
390 for( int i
= 0; i
< count
; ++i
)
393 // If sorted, use this wxSortedArrayStrings to determine
394 // the right insertion point
396 n
= m_strings
->Add(items
[i
]);
398 gtk_combo_box_insert_text( combobox
, n
, wxGTK_CONV( items
[i
] ) );
400 m_clientData
.Insert( NULL
, n
);
401 AssignNewItemClientData(n
, clientData
, i
, type
);
409 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
410 for( int i
= 0; i
< count
; ++i
)
413 // If sorted, use this wxSortedArrayStrings to determine
414 // the right insertion point
416 n
= m_strings
->Add(items
[i
]);
418 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( items
[i
] ) );
420 // TODO construct a list with all items and call gtk_list_insert_items once?
421 GList
*gitem_list
= g_list_alloc ();
422 gitem_list
->data
= list_item
;
423 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, n
);
425 m_clientData
.Insert( NULL
, n
);
426 AssignNewItemClientData(n
, clientData
, i
, type
);
428 if (GTK_WIDGET_REALIZED(m_widget
))
430 gtk_widget_realize( list_item
);
431 gtk_widget_realize( GTK_BIN(list_item
)->child
);
436 gtk_widget_show( list_item
);
442 InvalidateBestSize();
447 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
449 m_clientData
[n
] = clientData
;
452 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
454 return m_clientData
[n
];
457 void wxComboBox::DoClear()
459 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
464 if (!gtk_check_version(2,4,0))
466 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
467 const unsigned int count
= GetCount();
468 for (unsigned int i
= 0; i
< count
; i
++)
469 gtk_combo_box_remove_text( combobox
, 0 );
472 #endif // __WXGTK24__
474 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
475 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
478 m_clientData
.Clear();
485 InvalidateBestSize();
488 void wxComboBox::DoDeleteOneItem(unsigned int n
)
490 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
493 if (!gtk_check_version(2,4,0))
495 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
497 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
498 gtk_combo_box_remove_text( combobox
, n
);
503 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
505 GList
*child
= g_list_nth( listbox
->children
, n
);
509 wxFAIL_MSG(wxT("wrong index"));
515 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
516 gtk_list_remove_items( listbox
, list
);
522 m_clientData
.RemoveAt( n
);
524 m_strings
->RemoveAt( n
);
526 InvalidateBestSize();
529 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
531 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
534 if (!gtk_check_version(2,4,0))
536 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
537 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
539 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
541 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
543 GValue value
= { 0, };
544 g_value_init( &value
, G_TYPE_STRING
);
545 g_value_set_string( &value
, wxGTK_CONV( text
) );
546 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, 0, &value
);
547 g_value_unset( &value
);
553 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
555 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
558 GtkBin
*bin
= GTK_BIN( child
->data
);
559 GtkLabel
*label
= GTK_LABEL( bin
->child
);
560 gtk_label_set_text(label
, wxGTK_CONV(text
));
564 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
568 InvalidateBestSize();
571 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
573 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
576 if (!gtk_check_version(2,4,0))
578 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
579 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
581 gtk_tree_model_get_iter_first( model
, &iter
);
582 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
587 GValue value
= { 0, };
588 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
589 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
590 g_value_unset( &value
);
592 if (item
.IsSameAs( str
, bCase
) )
597 } while (gtk_tree_model_iter_next( model
, &iter
));
602 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
604 GList
*child
= GTK_LIST(list
)->children
;
608 GtkBin
*bin
= GTK_BIN( child
->data
);
609 GtkLabel
*label
= GTK_LABEL( bin
->child
);
610 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
612 if (item
.IsSameAs( str
, bCase
) )
623 int wxComboBox::GetSelection() const
626 if (!gtk_check_version(2,4,0))
628 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
629 return gtk_combo_box_get_active( combobox
);
633 // if the popup is currently opened, use the selection as it had been
634 // before it dropped down
635 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
636 : g_SelectionBeforePopup
;
639 int wxComboBox::GetCurrentSelection() const
641 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
644 if (!gtk_check_version(2,4,0))
646 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
647 return gtk_combo_box_get_active( combobox
);
652 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
654 GList
*selection
= GTK_LIST(list
)->selection
;
657 GList
*child
= GTK_LIST(list
)->children
;
661 if (child
->data
== selection
->data
) return count
;
671 wxString
wxComboBox::GetString(unsigned int n
) const
673 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
678 if (!gtk_check_version(2,4,0))
680 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
681 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
683 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
685 GValue value
= { 0, };
686 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
687 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
688 g_value_unset( &value
);
695 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
697 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
700 GtkBin
*bin
= GTK_BIN( child
->data
);
701 GtkLabel
*label
= GTK_LABEL( bin
->child
);
702 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
706 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
713 wxString
wxComboBox::GetStringSelection() const
715 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
718 if (!gtk_check_version(2,4,0))
720 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
721 int sel
= gtk_combo_box_get_active( combobox
);
723 return wxEmptyString
;
724 return GetString(sel
);
729 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
731 GList
*selection
= GTK_LIST(list
)->selection
;
734 GtkBin
*bin
= GTK_BIN( selection
->data
);
735 GtkLabel
*label
= GTK_LABEL( bin
->child
);
736 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
740 wxFAIL_MSG( wxT("wxComboBox: no selection") );
743 return wxEmptyString
;
746 unsigned int wxComboBox::GetCount() const
748 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
751 if (!gtk_check_version(2,4,0))
753 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
754 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
756 gtk_tree_model_get_iter_first( model
, &iter
);
757 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
759 unsigned int ret
= 1;
760 while (gtk_tree_model_iter_next( model
, &iter
))
767 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
769 GList
*child
= GTK_LIST(list
)->children
;
770 unsigned int count
= 0;
782 void wxComboBox::SetSelection( int n
)
784 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
789 if (!gtk_check_version(2,4,0))
791 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
792 gtk_combo_box_set_active( combobox
, n
);
797 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
798 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
799 gtk_list_select_item( GTK_LIST(list
), n
);
806 wxString
wxComboBox::GetValue() const
808 GtkEntry
*entry
= NULL
;
810 if (!gtk_check_version(2,4,0))
811 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
814 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
816 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
819 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
822 printf( "%d ", (int) (c
) );
830 void wxComboBox::SetValue( const wxString
& value
)
832 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
834 GtkEntry
*entry
= NULL
;
836 if (!gtk_check_version(2,4,0))
837 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
840 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
843 if (!value
.IsNull()) tmp
= value
;
846 gtk_entry_set_text( entry
, wxGTK_CONV( tmp
) );
849 InvalidateBestSize();
852 void wxComboBox::Copy()
854 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
856 GtkEntry
*entry
= NULL
;
858 if (!gtk_check_version(2,4,0))
859 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
862 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
864 gtk_editable_copy_clipboard(GTK_EDITABLE(entry
));
867 void wxComboBox::Cut()
869 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
871 GtkEntry
*entry
= NULL
;
873 if (!gtk_check_version(2,4,0))
874 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
877 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
879 gtk_editable_cut_clipboard(GTK_EDITABLE(entry
));
882 void wxComboBox::Paste()
884 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
886 GtkEntry
*entry
= NULL
;
888 if (!gtk_check_version(2,4,0))
889 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
892 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
894 gtk_editable_paste_clipboard(GTK_EDITABLE(entry
));
897 void wxComboBox::Undo()
902 void wxComboBox::Redo()
907 void wxComboBox::SelectAll()
909 SetSelection(0, GetLastPosition());
912 bool wxComboBox::CanUndo() const
918 bool wxComboBox::CanRedo() const
924 bool wxComboBox::HasSelection() const
927 GetSelection(&from
, &to
);
931 bool wxComboBox::CanCopy() const
933 // Can copy if there's a selection
934 return HasSelection();
937 bool wxComboBox::CanCut() const
939 return CanCopy() && IsEditable();
942 bool wxComboBox::CanPaste() const
944 // TODO: check for text on the clipboard
945 return IsEditable() ;
948 bool wxComboBox::IsEditable() const
950 return !HasFlag(wxCB_READONLY
);
954 void wxComboBox::SetInsertionPoint( long pos
)
956 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
958 if ( pos
== GetLastPosition() )
961 GtkEntry
*entry
= NULL
;
963 if (!gtk_check_version(2,4,0))
964 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
967 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
969 gtk_entry_set_position( entry
, (int)pos
);
972 long wxComboBox::GetInsertionPoint() const
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 return (long) gtk_editable_get_position(GTK_EDITABLE(entry
));
985 wxTextPos
wxComboBox::GetLastPosition() const
987 GtkEntry
*entry
= NULL
;
989 if (!gtk_check_version(2,4,0))
990 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
993 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
995 int pos
= entry
->text_length
;
999 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
1001 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
1003 GtkEntry
*entry
= NULL
;
1005 if (!gtk_check_version(2,4,0))
1006 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1009 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1011 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1012 if (value
.IsNull()) return;
1013 gint pos
= (gint
)to
;
1015 // FIXME-UTF8: wouldn't be needed if utf8_str() always returned a buffer
1016 #if wxUSE_UNICODE_UTF8
1017 const char *utf8
= value
.utf8_str();
1019 wxCharBuffer
buffer(value
.utf8_str());
1020 const char *utf8
= buffer
;
1022 gtk_editable_insert_text(GTK_EDITABLE(entry
), utf8
, strlen(utf8
), &pos
);
1025 void wxComboBox::SetSelection( long from
, long to
)
1027 GtkEntry
*entry
= NULL
;
1029 if (!gtk_check_version(2,4,0))
1030 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1033 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1035 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
1038 void wxComboBox::GetSelection( long* from
, long* to
) const
1040 GtkEntry
*entry
= NULL
;
1042 if (!gtk_check_version(2,4,0))
1043 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1046 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1050 GtkEditable
*editable
= GTK_EDITABLE(entry
);
1052 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
1058 void wxComboBox::SetEditable( bool editable
)
1060 GtkEntry
*entry
= NULL
;
1062 if (!gtk_check_version(2,4,0))
1063 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1066 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1068 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
1071 void wxComboBox::OnChar( wxKeyEvent
&event
)
1073 if ( event
.GetKeyCode() == WXK_RETURN
)
1075 // GTK automatically selects an item if its in the list
1076 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
1077 eventEnter
.SetString( GetValue() );
1078 eventEnter
.SetInt( GetSelection() );
1079 eventEnter
.SetEventObject( this );
1081 if (!GetEventHandler()->ProcessEvent( eventEnter
))
1083 // This will invoke the dialog default action, such
1084 // as the clicking the default button.
1086 wxWindow
*top_frame
= m_parent
;
1087 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1088 top_frame
= top_frame
->GetParent();
1090 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1092 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1094 if (window
->default_widget
)
1095 gtk_widget_activate (window
->default_widget
);
1099 // Catch GTK event so that GTK doesn't open the drop
1100 // down list upon RETURN.
1107 void wxComboBox::DisableEvents()
1110 if (!gtk_check_version(2,4,0))
1112 g_signal_handlers_block_by_func(GTK_BIN(m_widget
)->child
,
1113 (gpointer
)gtkcombobox_text_changed_callback
, this);
1115 g_signal_handlers_block_by_func(m_widget
,
1116 (gpointer
)gtkcombobox_changed_callback
, this);
1121 g_signal_handlers_block_by_func(GTK_COMBO(m_widget
)->list
,
1122 (gpointer
) gtkcombo_combo_select_child_callback
, this);
1124 g_signal_handlers_block_by_func(GTK_COMBO(m_widget
)->entry
,
1125 (gpointer
) gtkcombo_text_changed_callback
, this);
1129 void wxComboBox::EnableEvents()
1132 if (!gtk_check_version(2,4,0))
1134 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget
)->child
,
1135 (gpointer
)gtkcombobox_text_changed_callback
, this);
1137 g_signal_handlers_unblock_by_func(m_widget
,
1138 (gpointer
)gtkcombobox_changed_callback
, this);
1143 g_signal_handlers_unblock_by_func(GTK_COMBO(m_widget
)->list
,
1144 (gpointer
) gtkcombo_combo_select_child_callback
, this);
1146 g_signal_handlers_unblock_by_func(GTK_COMBO(m_widget
)->entry
,
1147 (gpointer
) gtkcombo_text_changed_callback
, this);
1151 void wxComboBox::OnSize( wxSizeEvent
&event
)
1154 if (!gtk_check_version(2,4,0))
1161 // NB: In some situations (e.g. on non-first page of a wizard, if the
1162 // size used is default size), GtkCombo widget is resized correctly,
1163 // but it's look is not updated, it's rendered as if it was much wider.
1164 // No other widgets are affected, so it looks like a bug in GTK+.
1165 // Manually requesting resize calculation (as gtk_pizza_set_size does)
1167 if (GTK_WIDGET_VISIBLE(m_widget
))
1168 gtk_widget_queue_resize(m_widget
);
1174 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1177 if (!gtk_check_version(2,4,0))
1184 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
1186 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
1187 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
1189 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
1190 GList
*child
= list
->children
;
1193 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
1195 GtkBin
*bin
= GTK_BIN(child
->data
);
1196 gtk_widget_modify_style( bin
->child
, style
);
1198 child
= child
->next
;
1203 GtkWidget
* wxComboBox::GetConnectWidget()
1205 GtkEntry
*entry
= NULL
;
1207 if (!gtk_check_version(2,4,0))
1208 entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
1211 entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
1213 return GTK_WIDGET( entry
);
1216 GdkWindow
*wxComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
1219 if (!gtk_check_version(2,4,0))
1221 wxUnusedVar(windows
);
1223 return GTK_ENTRY(GTK_BIN(m_widget
)->child
)->text_area
;
1228 windows
.push_back(GTK_ENTRY(GTK_COMBO(m_widget
)->entry
)->text_area
);
1229 windows
.push_back(GTK_COMBO(m_widget
)->button
->window
);
1231 // indicate that we return multiple windows in the windows array
1236 wxSize
wxComboBox::DoGetBestSize() const
1238 wxSize
ret( wxControl::DoGetBestSize() );
1240 // we know better our horizontal extent: it depends on the longest string
1245 unsigned int count
= GetCount();
1246 for ( unsigned int n
= 0; n
< count
; n
++ )
1248 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
1249 if ( width
> ret
.x
)
1254 // empty combobox should have some reasonable default size too
1264 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1267 if (!gtk_check_version(2,4,0))
1268 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
1271 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
1274 // ----------------------------------------------------------------------------
1275 // standard event handling
1276 // ----------------------------------------------------------------------------
1278 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
1283 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1288 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1293 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1298 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1303 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1306 GetSelection(& from
, & to
);
1307 if (from
!= -1 && to
!= -1)
1311 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1313 SetSelection(-1, -1);
1316 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1318 event
.Enable( CanCut() );
1321 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1323 event
.Enable( CanCopy() );
1326 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1328 event
.Enable( CanPaste() );
1331 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1333 event
.Enable( CanUndo() );
1336 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1338 event
.Enable( CanRedo() );
1341 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1343 event
.Enable(HasSelection() && IsEditable()) ;
1346 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1348 event
.Enable(GetLastPosition() > 0);