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 int curSelection
= combo
->GetCurrentSelection();
79 if (g_SelectionBeforePopup
!= curSelection
)
81 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
82 event
.SetInt( curSelection
);
83 event
.SetString( combo
->GetStringSelection() );
84 event
.SetEventObject( combo
);
85 combo
->GetEventHandler()->ProcessEvent( event
);
87 // for consistency with the other ports, send TEXT event
88 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
89 event2
.SetString( combo
->GetStringSelection() );
90 event2
.SetEventObject( combo
);
91 combo
->GetEventHandler()->ProcessEvent( event2
);
94 // reset the selection flag to value meaning that it is hidden
95 g_SelectionBeforePopup
= wxID_NONE
;
101 gtk_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
103 // store the combobox selection value before the popup is shown
104 g_SelectionBeforePopup
= combo
->GetCurrentSelection();
108 //-----------------------------------------------------------------------------
109 // "select-child" - click/cursor get select-child, changed, select-child
110 //-----------------------------------------------------------------------------
114 gtk_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
116 if (g_isIdle
) wxapp_install_idle_handler();
118 if (!combo
->m_hasVMT
) return;
120 if (g_blockEventsOnDrag
) return;
122 int curSelection
= combo
->GetCurrentSelection();
124 if (combo
->m_prevSelection
== curSelection
) return;
126 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
127 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
129 combo
->m_prevSelection
= curSelection
;
131 // Quickly set the value of the combo box
132 // as GTK+ does that only AFTER the event
134 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
),
135 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
136 combo
->SetValue( combo
->GetStringSelection() );
137 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
), "changed",
138 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
140 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
141 // because when combobox popup is shown, gtk_combo_select_child_callback is
142 // called each times the mouse is over an item with a pressed button so a lot
143 // of SELECTED event could be generated if the user keep the mouse button down
144 // and select other items ...
145 if (g_SelectionBeforePopup
== wxID_NONE
)
147 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
148 event
.SetInt( curSelection
);
149 event
.SetString( combo
->GetStringSelection() );
150 event
.SetEventObject( combo
);
151 combo
->GetEventHandler()->ProcessEvent( event
);
153 // for consistency with the other ports, don't generate text update
154 // events while the user is browsing the combobox neither
155 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
156 event2
.SetString( combo
->GetValue() );
157 event2
.SetEventObject( combo
);
158 combo
->GetEventHandler()->ProcessEvent( event2
);
163 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
167 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
169 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
170 EVT_SIZE(wxComboBox::OnSize
)
171 EVT_CHAR(wxComboBox::OnChar
)
173 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
174 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
175 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
176 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
177 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
178 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
179 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
181 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
182 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
183 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
184 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
185 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
186 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
187 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
190 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
191 const wxString
& value
,
192 const wxPoint
& pos
, const wxSize
& size
,
193 const wxArrayString
& choices
,
194 long style
, const wxValidator
& validator
,
195 const wxString
& name
)
197 wxCArrayString
chs(choices
);
199 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
200 chs
.GetStrings(), style
, validator
, name
);
203 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
204 const wxPoint
& pos
, const wxSize
& size
,
205 int n
, const wxString choices
[],
206 long style
, const wxValidator
& validator
,
207 const wxString
& name
)
209 m_ignoreNextUpdate
= false;
211 m_acceptsFocus
= true;
214 if (!PreCreation( parent
, pos
, size
) ||
215 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
217 wxFAIL_MSG( wxT("wxComboBox creation failed") );
221 m_widget
= gtk_combo_new();
222 GtkCombo
*combo
= GTK_COMBO(m_widget
);
224 // Disable GTK's broken events ...
225 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
226 // ... and add surrogate handler.
227 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
228 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
230 // make it more useable
231 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
233 // and case-sensitive
234 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
237 if (style
& wxNO_BORDER
)
238 g_object_set( GTK_ENTRY( combo
->entry
), "has-frame", FALSE
, NULL
);
241 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
244 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
247 for (int i
= 0; i
< n
; i
++)
249 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
251 m_clientDataList
.Append( (wxObject
*)NULL
);
252 m_clientObjectList
.Append( (wxObject
*)NULL
);
254 gtk_container_add( GTK_CONTAINER(list
), list_item
);
256 gtk_widget_show( list_item
);
259 m_parent
->DoAddChild( this );
261 m_focusWidget
= combo
->entry
;
265 ConnectWidget( combo
->button
);
267 // MSW's combo box shows the value and the selection is -1
268 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
269 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
271 if (style
& wxCB_READONLY
)
272 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
274 // "show" and "hide" events are generated when user click on the combobox button which popups a list
275 // this list is the "popwin" gtk widget
276 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "hide",
277 GTK_SIGNAL_FUNC(gtk_popup_hide_callback
), (gpointer
)this );
278 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "show",
279 GTK_SIGNAL_FUNC(gtk_popup_show_callback
), (gpointer
)this );
281 gtk_signal_connect_after( GTK_OBJECT(combo
->entry
), "changed",
282 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
284 gtk_signal_connect_after( GTK_OBJECT(combo
->list
), "select-child",
285 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
287 SetBestSize(size
); // need this too because this is a wxControlWithItems
289 // This is required for tool bar support
290 // wxSize setsize = GetSize();
291 // gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
296 wxComboBox::~wxComboBox()
298 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
301 wxClientData
*cd
= (wxClientData
*)node
->GetData();
303 node
= node
->GetNext();
305 m_clientObjectList
.Clear();
307 m_clientDataList
.Clear();
310 void wxComboBox::SetFocus()
314 // don't do anything if we already have focus
318 gtk_widget_grab_focus( m_focusWidget
);
321 int wxComboBox::DoAppend( const wxString
&item
)
323 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
327 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
329 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
331 gtk_container_add( GTK_CONTAINER(list
), list_item
);
333 if (GTK_WIDGET_REALIZED(m_widget
))
335 gtk_widget_realize( list_item
);
336 gtk_widget_realize( GTK_BIN(list_item
)->child
);
339 // Apply current widget style to the new list_item
340 GtkRcStyle
*style
= CreateWidgetStyle();
343 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
344 GtkBin
*bin
= GTK_BIN( list_item
);
345 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
346 gtk_widget_modify_style( label
, style
);
347 gtk_rc_style_unref( style
);
350 gtk_widget_show( list_item
);
352 const int count
= GetCount();
354 if ( (int)m_clientDataList
.GetCount() < count
)
355 m_clientDataList
.Append( (wxObject
*) NULL
);
356 if ( (int)m_clientObjectList
.GetCount() < count
)
357 m_clientObjectList
.Append( (wxObject
*) NULL
);
361 InvalidateBestSize();
366 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
368 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
369 wxT("can't insert into sorted list"));
371 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
373 int count
= GetCount();
374 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
381 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
383 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
385 GList
*gitem_list
= g_list_alloc ();
386 gitem_list
->data
= list_item
;
387 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
389 if (GTK_WIDGET_REALIZED(m_widget
))
391 gtk_widget_realize( list_item
);
392 gtk_widget_realize( GTK_BIN(list_item
)->child
);
397 gtk_widget_show( list_item
);
401 if ( (int)m_clientDataList
.GetCount() < count
)
402 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
403 if ( (int)m_clientObjectList
.GetCount() < count
)
404 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
408 InvalidateBestSize();
413 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
415 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
417 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
420 node
->SetData( (wxObject
*) clientData
);
423 void* wxComboBox::DoGetItemClientData( int n
) const
425 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
427 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
429 return node
? node
->GetData() : NULL
;
432 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
434 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
436 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
439 // wxItemContainer already deletes data for us
441 node
->SetData( (wxObject
*) clientData
);
444 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
446 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
448 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
450 return node
? (wxClientData
*) node
->GetData() : NULL
;
453 void wxComboBox::Clear()
455 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
459 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
460 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
462 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
465 wxClientData
*cd
= (wxClientData
*)node
->GetData();
467 node
= node
->GetNext();
469 m_clientObjectList
.Clear();
471 m_clientDataList
.Clear();
475 InvalidateBestSize();
478 void wxComboBox::Delete( int n
)
480 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
482 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
484 GList
*child
= g_list_nth( listbox
->children
, n
);
488 wxFAIL_MSG(wxT("wrong index"));
494 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
495 gtk_list_remove_items( listbox
, list
);
498 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
501 wxClientData
*cd
= (wxClientData
*)node
->GetData();
503 m_clientObjectList
.Erase( node
);
506 node
= m_clientDataList
.Item( n
);
508 m_clientDataList
.Erase( node
);
512 InvalidateBestSize();
515 void wxComboBox::SetString(int n
, const wxString
&text
)
517 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
519 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
521 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
524 GtkBin
*bin
= GTK_BIN( child
->data
);
525 GtkLabel
*label
= GTK_LABEL( bin
->child
);
526 gtk_label_set_text(label
, wxGTK_CONV(text
));
530 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
533 InvalidateBestSize();
536 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
538 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
540 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
542 GList
*child
= GTK_LIST(list
)->children
;
546 GtkBin
*bin
= GTK_BIN( child
->data
);
547 GtkLabel
*label
= GTK_LABEL( bin
->child
);
549 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
551 wxString
str( label
->label
);
553 if (item
.IsSameAs( str
, bCase
) )
563 int wxComboBox::GetSelection() const
565 // if the popup is currently opened, use the selection as it had been
566 // before it dropped down
567 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
568 : g_SelectionBeforePopup
;
571 int wxComboBox::GetCurrentSelection() const
573 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
575 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
577 GList
*selection
= GTK_LIST(list
)->selection
;
580 GList
*child
= GTK_LIST(list
)->children
;
584 if (child
->data
== selection
->data
) return count
;
593 wxString
wxComboBox::GetString( int n
) const
595 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
597 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
600 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
603 GtkBin
*bin
= GTK_BIN( child
->data
);
604 GtkLabel
*label
= GTK_LABEL( bin
->child
);
606 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
608 str
= wxString( label
->label
);
613 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
619 wxString
wxComboBox::GetStringSelection() const
621 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
623 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
625 GList
*selection
= GTK_LIST(list
)->selection
;
628 GtkBin
*bin
= GTK_BIN( selection
->data
);
629 GtkLabel
*label
= GTK_LABEL( bin
->child
);
631 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
633 wxString
tmp( label
->label
);
638 wxFAIL_MSG( wxT("wxComboBox: no selection") );
640 return wxEmptyString
;
643 int wxComboBox::GetCount() const
645 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
647 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
649 GList
*child
= GTK_LIST(list
)->children
;
651 while (child
) { count
++; child
= child
->next
; }
655 void wxComboBox::SetSelection( int n
)
657 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
661 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
662 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
663 gtk_list_select_item( GTK_LIST(list
), n
);
669 wxString
wxComboBox::GetValue() const
671 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
672 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
675 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
678 printf( "%d ", (int) (c
) );
686 void wxComboBox::SetValue( const wxString
& value
)
688 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
690 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
692 if (!value
.IsNull()) tmp
= value
;
693 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
695 InvalidateBestSize();
698 void wxComboBox::Copy()
700 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
702 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
703 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
706 void wxComboBox::Cut()
708 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
710 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
711 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
714 void wxComboBox::Paste()
716 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
718 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
719 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
722 void wxComboBox::Undo()
727 void wxComboBox::Redo()
732 void wxComboBox::SelectAll()
734 SetSelection(0, GetLastPosition());
737 bool wxComboBox::CanUndo() const
743 bool wxComboBox::CanRedo() const
749 bool wxComboBox::HasSelection() const
752 GetSelection(&from
, &to
);
756 bool wxComboBox::CanCopy() const
758 // Can copy if there's a selection
759 return HasSelection();
762 bool wxComboBox::CanCut() const
764 return CanCopy() && IsEditable();
767 bool wxComboBox::CanPaste() const
769 // TODO: check for text on the clipboard
770 return IsEditable() ;
773 bool wxComboBox::IsEditable() const
775 return !HasFlag(wxCB_READONLY
);
779 void wxComboBox::SetInsertionPoint( long pos
)
781 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
783 if ( pos
== GetLastPosition() )
786 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
787 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
790 long wxComboBox::GetInsertionPoint() const
792 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
795 wxTextPos
wxComboBox::GetLastPosition() const
797 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
798 int pos
= GTK_ENTRY(entry
)->text_length
;
802 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
804 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
806 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
807 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
808 if (value
.IsNull()) return;
812 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
813 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
815 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
819 void wxComboBox::SetSelection( long from
, long to
)
821 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
822 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
825 void wxComboBox::GetSelection( long* from
, long* to
) const
829 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
832 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
836 *from
= (long) editable
->selection_start_pos
;
837 *to
= (long) editable
->selection_end_pos
;
842 void wxComboBox::SetEditable( bool editable
)
844 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
845 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
848 void wxComboBox::OnChar( wxKeyEvent
&event
)
850 if ( event
.GetKeyCode() == WXK_RETURN
)
852 // GTK automatically selects an item if its in the list
853 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
854 eventEnter
.SetString( GetValue() );
855 eventEnter
.SetInt( GetSelection() );
856 eventEnter
.SetEventObject( this );
858 if (!GetEventHandler()->ProcessEvent( eventEnter
))
860 // This will invoke the dialog default action, such
861 // as the clicking the default button.
863 wxWindow
*top_frame
= m_parent
;
864 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
865 top_frame
= top_frame
->GetParent();
867 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
869 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
871 if (window
->default_widget
)
872 gtk_widget_activate (window
->default_widget
);
876 // Catch GTK event so that GTK doesn't open the drop
877 // down list upon RETURN.
884 void wxComboBox::DisableEvents()
886 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
887 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
888 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
889 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
892 void wxComboBox::EnableEvents()
894 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
895 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
896 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
897 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
900 void wxComboBox::OnSize( wxSizeEvent
&event
)
902 // NB: In some situations (e.g. on non-first page of a wizard, if the
903 // size used is default size), GtkCombo widget is resized correctly,
904 // but it's look is not updated, it's rendered as if it was much wider.
905 // No other widgets are affected, so it looks like a bug in GTK+.
906 // Manually requesting resize calculation (as gtk_pizza_set_size does)
908 if (GTK_WIDGET_VISIBLE(m_widget
))
909 gtk_widget_queue_resize(m_widget
);
914 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
916 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
918 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
919 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
921 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
922 GList
*child
= list
->children
;
925 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
927 GtkBin
*bin
= GTK_BIN(child
->data
);
928 gtk_widget_modify_style( bin
->child
, style
);
934 GtkWidget
* wxComboBox::GetConnectWidget()
936 return GTK_COMBO(m_widget
)->entry
;
939 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
941 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
942 (window
== GTK_COMBO(m_widget
)->button
->window
) );
945 wxSize
wxComboBox::DoGetBestSize() const
947 wxSize
ret( wxControl::DoGetBestSize() );
949 // we know better our horizontal extent: it depends on the longest string
954 size_t count
= GetCount();
955 for ( size_t n
= 0; n
< count
; n
++ )
957 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
963 // empty combobox should have some reasonable default size too
973 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
975 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
978 // ----------------------------------------------------------------------------
979 // standard event handling
980 // ----------------------------------------------------------------------------
982 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
987 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
992 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
997 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1002 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1007 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1010 GetSelection(& from
, & to
);
1011 if (from
!= -1 && to
!= -1)
1015 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1017 SetSelection(-1, -1);
1020 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1022 event
.Enable( CanCut() );
1025 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1027 event
.Enable( CanCopy() );
1030 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1032 event
.Enable( CanPaste() );
1035 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1037 event
.Enable( CanUndo() );
1040 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1042 event
.Enable( CanRedo() );
1045 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1047 event
.Enable(HasSelection() && IsEditable()) ;
1050 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1052 event
.Enable(GetLastPosition() > 0);