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"
13 #include "wx/combobox.h"
17 #include "wx/settings.h"
18 #include "wx/arrstr.h"
21 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
23 #include "wx/gtk/private.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
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 gtk_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 gtk_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
74 gtk_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 gtk_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 gtk_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 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
),
140 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
141 combo
->SetValue( combo
->GetStringSelection() );
142 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
), "changed",
143 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
145 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
146 // because when combobox popup is shown, gtk_combo_select_child_callback is
147 // called each times the mouse is over an item with a pressed button so a lot
148 // of SELECTED event could be generated if the user keep the mouse button down
149 // and select other items ...
150 if (g_SelectionBeforePopup
== wxID_NONE
)
152 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
153 event
.SetInt( curSelection
);
154 event
.SetString( combo
->GetStringSelection() );
155 event
.SetEventObject( combo
);
156 combo
->GetEventHandler()->ProcessEvent( event
);
158 // for consistency with the other ports, don't generate text update
159 // events while the user is browsing the combobox neither
160 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
161 event2
.SetString( combo
->GetValue() );
162 event2
.SetEventObject( combo
);
163 combo
->GetEventHandler()->ProcessEvent( event2
);
168 //-----------------------------------------------------------------------------
170 //-----------------------------------------------------------------------------
172 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
174 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
175 EVT_SIZE(wxComboBox::OnSize
)
176 EVT_CHAR(wxComboBox::OnChar
)
178 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
179 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
180 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
181 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
182 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
183 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
184 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
186 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
187 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
188 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
189 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
190 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
191 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
192 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
195 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
196 const wxString
& value
,
197 const wxPoint
& pos
, const wxSize
& size
,
198 const wxArrayString
& choices
,
199 long style
, const wxValidator
& validator
,
200 const wxString
& name
)
202 wxCArrayString
chs(choices
);
204 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
205 chs
.GetStrings(), style
, validator
, name
);
208 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
209 const wxPoint
& pos
, const wxSize
& size
,
210 int n
, const wxString choices
[],
211 long style
, const wxValidator
& validator
,
212 const wxString
& name
)
214 m_ignoreNextUpdate
= false;
216 m_acceptsFocus
= true;
219 if (!PreCreation( parent
, pos
, size
) ||
220 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
222 wxFAIL_MSG( wxT("wxComboBox creation failed") );
226 m_widget
= gtk_combo_new();
227 GtkCombo
*combo
= GTK_COMBO(m_widget
);
229 // Disable GTK's broken events ...
230 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
231 // ... and add surrogate handler.
232 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
233 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
235 // make it more useable
236 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
238 // and case-sensitive
239 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
242 if (style
& wxNO_BORDER
)
243 g_object_set( GTK_ENTRY( combo
->entry
), "has-frame", FALSE
, NULL
);
246 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
249 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
252 for (int i
= 0; i
< n
; i
++)
254 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
256 m_clientDataList
.Append( (wxObject
*)NULL
);
257 m_clientObjectList
.Append( (wxObject
*)NULL
);
259 gtk_container_add( GTK_CONTAINER(list
), list_item
);
261 gtk_widget_show( list_item
);
264 m_parent
->DoAddChild( this );
266 m_focusWidget
= combo
->entry
;
270 ConnectWidget( combo
->button
);
272 // MSW's combo box shows the value and the selection is -1
273 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
274 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
276 if (style
& wxCB_READONLY
)
277 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
279 // "show" and "hide" events are generated when user click on the combobox button which popups a list
280 // this list is the "popwin" gtk widget
281 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "hide",
282 GTK_SIGNAL_FUNC(gtk_popup_hide_callback
), (gpointer
)this );
283 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "show",
284 GTK_SIGNAL_FUNC(gtk_popup_show_callback
), (gpointer
)this );
286 gtk_signal_connect_after( GTK_OBJECT(combo
->entry
), "changed",
287 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
289 gtk_signal_connect_after( GTK_OBJECT(combo
->list
), "select-child",
290 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
292 SetBestSize(size
); // need this too because this is a wxControlWithItems
294 // This is required for tool bar support
295 // wxSize setsize = GetSize();
296 // gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
301 wxComboBox::~wxComboBox()
303 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
306 wxClientData
*cd
= (wxClientData
*)node
->GetData();
308 node
= node
->GetNext();
310 m_clientObjectList
.Clear();
312 m_clientDataList
.Clear();
315 void wxComboBox::SetFocus()
319 // don't do anything if we already have focus
323 gtk_widget_grab_focus( m_focusWidget
);
326 int wxComboBox::DoAppend( const wxString
&item
)
328 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
332 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
334 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
336 gtk_container_add( GTK_CONTAINER(list
), list_item
);
338 if (GTK_WIDGET_REALIZED(m_widget
))
340 gtk_widget_realize( list_item
);
341 gtk_widget_realize( GTK_BIN(list_item
)->child
);
344 // Apply current widget style to the new list_item
345 GtkRcStyle
*style
= CreateWidgetStyle();
348 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
349 GtkBin
*bin
= GTK_BIN( list_item
);
350 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
351 gtk_widget_modify_style( label
, style
);
352 gtk_rc_style_unref( style
);
355 gtk_widget_show( list_item
);
357 const int count
= GetCount();
359 if ( (int)m_clientDataList
.GetCount() < count
)
360 m_clientDataList
.Append( (wxObject
*) NULL
);
361 if ( (int)m_clientObjectList
.GetCount() < count
)
362 m_clientObjectList
.Append( (wxObject
*) NULL
);
366 InvalidateBestSize();
371 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
373 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
374 wxT("can't insert into sorted list"));
376 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
378 int count
= GetCount();
379 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
386 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
388 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
390 GList
*gitem_list
= g_list_alloc ();
391 gitem_list
->data
= list_item
;
392 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
394 if (GTK_WIDGET_REALIZED(m_widget
))
396 gtk_widget_realize( list_item
);
397 gtk_widget_realize( GTK_BIN(list_item
)->child
);
402 gtk_widget_show( list_item
);
406 if ( (int)m_clientDataList
.GetCount() < count
)
407 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
408 if ( (int)m_clientObjectList
.GetCount() < count
)
409 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
413 InvalidateBestSize();
418 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
420 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
422 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
425 node
->SetData( (wxObject
*) clientData
);
428 void* wxComboBox::DoGetItemClientData( int n
) const
430 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
432 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
434 return node
? node
->GetData() : NULL
;
437 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
439 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
441 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
444 // wxItemContainer already deletes data for us
446 node
->SetData( (wxObject
*) clientData
);
449 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
451 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
453 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
455 return node
? (wxClientData
*) node
->GetData() : NULL
;
458 void wxComboBox::Clear()
460 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
464 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
465 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
467 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
470 wxClientData
*cd
= (wxClientData
*)node
->GetData();
472 node
= node
->GetNext();
474 m_clientObjectList
.Clear();
476 m_clientDataList
.Clear();
480 InvalidateBestSize();
483 void wxComboBox::Delete( int n
)
485 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
487 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
489 GList
*child
= g_list_nth( listbox
->children
, n
);
493 wxFAIL_MSG(wxT("wrong index"));
499 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
500 gtk_list_remove_items( listbox
, list
);
503 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
506 wxClientData
*cd
= (wxClientData
*)node
->GetData();
508 m_clientObjectList
.Erase( node
);
511 node
= m_clientDataList
.Item( n
);
513 m_clientDataList
.Erase( node
);
517 InvalidateBestSize();
520 void wxComboBox::SetString(int n
, const wxString
&text
)
522 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
524 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
526 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
529 GtkBin
*bin
= GTK_BIN( child
->data
);
530 GtkLabel
*label
= GTK_LABEL( bin
->child
);
531 gtk_label_set_text(label
, wxGTK_CONV(text
));
535 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
538 InvalidateBestSize();
541 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
543 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
545 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
547 GList
*child
= GTK_LIST(list
)->children
;
551 GtkBin
*bin
= GTK_BIN( child
->data
);
552 GtkLabel
*label
= GTK_LABEL( bin
->child
);
554 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
556 wxString
str( label
->label
);
558 if (item
.IsSameAs( str
, bCase
) )
568 int wxComboBox::GetSelection() const
570 // if the popup is currently opened, use the selection as it had been
571 // before it dropped down
572 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
573 : g_SelectionBeforePopup
;
576 int wxComboBox::GetCurrentSelection() const
578 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
580 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
582 GList
*selection
= GTK_LIST(list
)->selection
;
585 GList
*child
= GTK_LIST(list
)->children
;
589 if (child
->data
== selection
->data
) return count
;
598 wxString
wxComboBox::GetString( int n
) const
600 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
602 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
605 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
608 GtkBin
*bin
= GTK_BIN( child
->data
);
609 GtkLabel
*label
= GTK_LABEL( bin
->child
);
611 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
613 str
= wxString( label
->label
);
618 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
624 wxString
wxComboBox::GetStringSelection() const
626 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
628 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
630 GList
*selection
= GTK_LIST(list
)->selection
;
633 GtkBin
*bin
= GTK_BIN( selection
->data
);
634 GtkLabel
*label
= GTK_LABEL( bin
->child
);
636 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
638 wxString
tmp( label
->label
);
643 wxFAIL_MSG( wxT("wxComboBox: no selection") );
645 return wxEmptyString
;
648 int wxComboBox::GetCount() const
650 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
652 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
654 GList
*child
= GTK_LIST(list
)->children
;
656 while (child
) { count
++; child
= child
->next
; }
660 void wxComboBox::SetSelection( int n
)
662 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
666 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
667 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
668 gtk_list_select_item( GTK_LIST(list
), n
);
674 wxString
wxComboBox::GetValue() const
676 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
677 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
680 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
683 printf( "%d ", (int) (c
) );
691 void wxComboBox::SetValue( const wxString
& value
)
693 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
695 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
697 if (!value
.IsNull()) tmp
= value
;
698 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
700 InvalidateBestSize();
703 void wxComboBox::Copy()
705 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
707 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
708 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
711 void wxComboBox::Cut()
713 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
715 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
716 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
719 void wxComboBox::Paste()
721 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
723 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
724 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
727 void wxComboBox::Undo()
732 void wxComboBox::Redo()
737 void wxComboBox::SelectAll()
739 SetSelection(0, GetLastPosition());
742 bool wxComboBox::CanUndo() const
748 bool wxComboBox::CanRedo() const
754 bool wxComboBox::HasSelection() const
757 GetSelection(&from
, &to
);
761 bool wxComboBox::CanCopy() const
763 // Can copy if there's a selection
764 return HasSelection();
767 bool wxComboBox::CanCut() const
769 return CanCopy() && IsEditable();
772 bool wxComboBox::CanPaste() const
774 // TODO: check for text on the clipboard
775 return IsEditable() ;
778 bool wxComboBox::IsEditable() const
780 return !HasFlag(wxCB_READONLY
);
784 void wxComboBox::SetInsertionPoint( long pos
)
786 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
788 if ( pos
== GetLastPosition() )
791 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
792 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
795 long wxComboBox::GetInsertionPoint() const
797 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
800 wxTextPos
wxComboBox::GetLastPosition() const
802 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
803 int pos
= GTK_ENTRY(entry
)->text_length
;
807 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
809 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
811 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
812 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
813 if (value
.IsNull()) return;
817 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
818 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
820 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
824 void wxComboBox::SetSelection( long from
, long to
)
826 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
827 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
830 void wxComboBox::GetSelection( long* from
, long* to
) const
834 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
837 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
841 *from
= (long) editable
->selection_start_pos
;
842 *to
= (long) editable
->selection_end_pos
;
847 void wxComboBox::SetEditable( bool editable
)
849 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
850 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
853 void wxComboBox::OnChar( wxKeyEvent
&event
)
855 if ( event
.GetKeyCode() == WXK_RETURN
)
857 // GTK automatically selects an item if its in the list
858 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
859 eventEnter
.SetString( GetValue() );
860 eventEnter
.SetInt( GetSelection() );
861 eventEnter
.SetEventObject( this );
863 if (!GetEventHandler()->ProcessEvent( eventEnter
))
865 // This will invoke the dialog default action, such
866 // as the clicking the default button.
868 wxWindow
*top_frame
= m_parent
;
869 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
870 top_frame
= top_frame
->GetParent();
872 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
874 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
876 if (window
->default_widget
)
877 gtk_widget_activate (window
->default_widget
);
881 // Catch GTK event so that GTK doesn't open the drop
882 // down list upon RETURN.
889 void wxComboBox::DisableEvents()
891 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
892 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
893 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
894 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
897 void wxComboBox::EnableEvents()
899 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
900 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
901 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
902 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
905 void wxComboBox::OnSize( wxSizeEvent
&event
)
907 // NB: In some situations (e.g. on non-first page of a wizard, if the
908 // size used is default size), GtkCombo widget is resized correctly,
909 // but it's look is not updated, it's rendered as if it was much wider.
910 // No other widgets are affected, so it looks like a bug in GTK+.
911 // Manually requesting resize calculation (as gtk_pizza_set_size does)
913 if (GTK_WIDGET_VISIBLE(m_widget
))
914 gtk_widget_queue_resize(m_widget
);
919 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
921 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
923 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
924 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
926 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
927 GList
*child
= list
->children
;
930 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
932 GtkBin
*bin
= GTK_BIN(child
->data
);
933 gtk_widget_modify_style( bin
->child
, style
);
939 GtkWidget
* wxComboBox::GetConnectWidget()
941 return GTK_COMBO(m_widget
)->entry
;
944 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
946 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
947 (window
== GTK_COMBO(m_widget
)->button
->window
) );
950 wxSize
wxComboBox::DoGetBestSize() const
952 wxSize
ret( wxControl::DoGetBestSize() );
954 // we know better our horizontal extent: it depends on the longest string
959 size_t count
= GetCount();
960 for ( size_t n
= 0; n
< count
; n
++ )
962 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
968 // empty combobox should have some reasonable default size too
978 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
980 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
983 // ----------------------------------------------------------------------------
984 // standard event handling
985 // ----------------------------------------------------------------------------
987 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
992 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
997 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1002 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1007 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1012 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1015 GetSelection(& from
, & to
);
1016 if (from
!= -1 && to
!= -1)
1020 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1022 SetSelection(-1, -1);
1025 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1027 event
.Enable( CanCut() );
1030 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1032 event
.Enable( CanCopy() );
1035 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1037 event
.Enable( CanPaste() );
1040 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1042 event
.Enable( CanUndo() );
1045 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1047 event
.Enable( CanRedo() );
1050 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1052 event
.Enable(HasSelection() && IsEditable()) ;
1055 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1057 event
.Enable(GetLastPosition() > 0);