1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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
23 #include "wx/arrstr.h"
25 #include "wx/gtk1/private.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern void wxapp_install_idle_handler();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern bool g_blockEventsOnDrag
;
39 static int g_SelectionBeforePopup
= wxID_NONE
; // this means the popup is hidden
41 //-----------------------------------------------------------------------------
42 // "changed" - typing and list item matches get changed, select-child
43 // if it doesn't match an item then just get a single changed
44 //-----------------------------------------------------------------------------
48 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
50 if (g_isIdle
) wxapp_install_idle_handler();
52 if (combo
->m_ignoreNextUpdate
)
54 combo
->m_ignoreNextUpdate
= false;
58 if (!combo
->m_hasVMT
) return;
60 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
61 event
.SetString( combo
->GetValue() );
62 event
.SetEventObject( combo
);
63 combo
->GetEventHandler()->ProcessEvent( event
);
69 gtk_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
76 gtk_popup_hide_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
78 // when the popup is hidden, throw a SELECTED event only if the combobox
80 const int curSelection
= combo
->GetCurrentSelection();
82 const bool hasChanged
= curSelection
!= g_SelectionBeforePopup
;
84 // reset the selection flag to value meaning that it is hidden and do it
85 // now, before generating the events, so that GetSelection() returns the
86 // new value from the event handler
87 g_SelectionBeforePopup
= wxID_NONE
;
91 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
92 event
.SetInt( curSelection
);
93 event
.SetString( combo
->GetStringSelection() );
94 event
.SetEventObject( combo
);
95 combo
->GetEventHandler()->ProcessEvent( event
);
97 // for consistency with the other ports, send TEXT event
98 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
99 event2
.SetString( combo
->GetStringSelection() );
100 event2
.SetEventObject( combo
);
101 combo
->GetEventHandler()->ProcessEvent( event2
);
108 gtk_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
110 // store the combobox selection value before the popup is shown
111 g_SelectionBeforePopup
= combo
->GetCurrentSelection();
115 //-----------------------------------------------------------------------------
116 // "select-child" - click/cursor get select-child, changed, select-child
117 //-----------------------------------------------------------------------------
121 gtk_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
123 if (g_isIdle
) wxapp_install_idle_handler();
125 if (!combo
->m_hasVMT
) return;
127 if (g_blockEventsOnDrag
) return;
129 int curSelection
= combo
->GetCurrentSelection();
131 if (combo
->m_prevSelection
== curSelection
) return;
133 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
134 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
136 combo
->m_prevSelection
= curSelection
;
138 // Quickly set the value of the combo box
139 // as GTK+ does that only AFTER the event
141 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
),
142 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
143 combo
->SetValue( combo
->GetStringSelection() );
144 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
), "changed",
145 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
147 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
148 // because when combobox popup is shown, gtk_combo_select_child_callback is
149 // called each times the mouse is over an item with a pressed button so a lot
150 // of SELECTED event could be generated if the user keep the mouse button down
151 // and select other items ...
152 if (g_SelectionBeforePopup
== wxID_NONE
)
154 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
155 event
.SetInt( curSelection
);
156 event
.SetString( combo
->GetStringSelection() );
157 event
.SetEventObject( combo
);
158 combo
->GetEventHandler()->ProcessEvent( event
);
160 // for consistency with the other ports, don't generate text update
161 // events while the user is browsing the combobox neither
162 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
163 event2
.SetString( combo
->GetValue() );
164 event2
.SetEventObject( combo
);
165 combo
->GetEventHandler()->ProcessEvent( event2
);
170 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
174 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
176 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
177 EVT_SIZE(wxComboBox::OnSize
)
178 EVT_CHAR(wxComboBox::OnChar
)
180 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
181 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
182 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
183 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
184 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
185 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
186 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
188 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
189 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
190 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
191 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
192 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
193 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
194 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
197 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
198 const wxString
& value
,
199 const wxPoint
& pos
, const wxSize
& size
,
200 const wxArrayString
& choices
,
201 long style
, const wxValidator
& validator
,
202 const wxString
& name
)
204 wxCArrayString
chs(choices
);
206 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
207 chs
.GetStrings(), style
, validator
, name
);
210 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
211 const wxPoint
& pos
, const wxSize
& size
,
212 int n
, const wxString choices
[],
213 long style
, const wxValidator
& validator
,
214 const wxString
& name
)
216 m_ignoreNextUpdate
= false;
218 m_acceptsFocus
= true;
221 if (!PreCreation( parent
, pos
, size
) ||
222 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
224 wxFAIL_MSG( wxT("wxComboBox creation failed") );
228 m_widget
= gtk_combo_new();
229 GtkCombo
*combo
= GTK_COMBO(m_widget
);
231 // Disable GTK's broken events ...
232 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
233 // ... and add surrogate handler.
234 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
235 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
237 // make it more useable
238 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
240 // and case-sensitive
241 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
243 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
245 // 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 unsigned int count
= GetCount();
354 if ( m_clientDataList
.GetCount() < count
)
355 m_clientDataList
.Append( (wxObject
*) NULL
);
356 if ( m_clientObjectList
.GetCount() < count
)
357 m_clientObjectList
.Append( (wxObject
*) NULL
);
361 InvalidateBestSize();
366 int wxComboBox::DoInsert( const wxString
&item
, unsigned 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 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("invalid index") );
375 if (pos
== GetCount())
380 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
382 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
384 GList
*gitem_list
= g_list_alloc ();
385 gitem_list
->data
= list_item
;
386 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
388 if (GTK_WIDGET_REALIZED(m_widget
))
390 gtk_widget_realize( list_item
);
391 gtk_widget_realize( GTK_BIN(list_item
)->child
);
396 gtk_widget_show( list_item
);
398 const unsigned int count
= GetCount();
400 if ( m_clientDataList
.GetCount() < count
)
401 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
402 if ( m_clientObjectList
.GetCount() < count
)
403 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
407 InvalidateBestSize();
412 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
414 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
416 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
419 node
->SetData( (wxObject
*) clientData
);
422 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
424 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
426 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
428 return node
? node
->GetData() : NULL
;
431 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
433 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
435 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
438 // wxItemContainer already deletes data for us
440 node
->SetData( (wxObject
*) clientData
);
443 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
445 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
447 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
449 return node
? (wxClientData
*) node
->GetData() : NULL
;
452 void wxComboBox::Clear()
454 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
458 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
459 gtk_list_clear_items( GTK_LIST(list
), 0, (int)GetCount() );
461 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
464 wxClientData
*cd
= (wxClientData
*)node
->GetData();
466 node
= node
->GetNext();
468 m_clientObjectList
.Clear();
470 m_clientDataList
.Clear();
474 InvalidateBestSize();
477 void wxComboBox::Delete(unsigned int n
)
479 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
481 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
483 GList
*child
= g_list_nth( listbox
->children
, n
);
487 wxFAIL_MSG(wxT("wrong index"));
493 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
494 gtk_list_remove_items( listbox
, list
);
497 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
500 wxClientData
*cd
= (wxClientData
*)node
->GetData();
502 m_clientObjectList
.Erase( node
);
505 node
= m_clientDataList
.Item( n
);
507 m_clientDataList
.Erase( node
);
511 InvalidateBestSize();
514 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
516 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
518 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
520 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
523 GtkBin
*bin
= GTK_BIN( child
->data
);
524 GtkLabel
*label
= GTK_LABEL( bin
->child
);
525 gtk_label_set_text(label
, wxGTK_CONV(text
));
529 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
532 InvalidateBestSize();
535 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
537 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
539 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
541 GList
*child
= GTK_LIST(list
)->children
;
545 GtkBin
*bin
= GTK_BIN( child
->data
);
546 GtkLabel
*label
= GTK_LABEL( bin
->child
);
547 wxString
str( label
->label
);
548 if (item
.IsSameAs( str
, bCase
) )
558 int wxComboBox::GetSelection() const
560 // if the popup is currently opened, use the selection as it had been
561 // before it dropped down
562 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
563 : g_SelectionBeforePopup
;
566 int wxComboBox::GetCurrentSelection() const
568 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
570 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
572 GList
*selection
= GTK_LIST(list
)->selection
;
575 GList
*child
= GTK_LIST(list
)->children
;
579 if (child
->data
== selection
->data
) return count
;
588 wxString
wxComboBox::GetString(unsigned int n
) const
590 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
592 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
595 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
598 GtkBin
*bin
= GTK_BIN( child
->data
);
599 GtkLabel
*label
= GTK_LABEL( bin
->child
);
600 str
= wxString( label
->label
);
604 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
610 wxString
wxComboBox::GetStringSelection() const
612 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
614 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
616 GList
*selection
= GTK_LIST(list
)->selection
;
619 GtkBin
*bin
= GTK_BIN( selection
->data
);
620 GtkLabel
*label
= GTK_LABEL( bin
->child
);
621 wxString
tmp( label
->label
);
625 wxFAIL_MSG( wxT("wxComboBox: no selection") );
627 return wxEmptyString
;
630 unsigned int wxComboBox::GetCount() const
632 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
634 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
636 GList
*child
= GTK_LIST(list
)->children
;
637 unsigned int count
= 0;
638 while (child
) { count
++; child
= child
->next
; }
642 void wxComboBox::SetSelection( int n
)
644 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
648 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
649 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
650 gtk_list_select_item( GTK_LIST(list
), n
);
656 wxString
wxComboBox::GetValue() const
658 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
659 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
662 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
665 printf( "%d ", (int) (c
) );
673 void wxComboBox::SetValue( const wxString
& value
)
675 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
677 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
679 if (!value
.IsNull()) tmp
= value
;
680 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
682 InvalidateBestSize();
685 void wxComboBox::Copy()
687 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
689 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
690 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
693 void wxComboBox::Cut()
695 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
697 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
698 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
701 void wxComboBox::Paste()
703 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
705 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
706 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
709 void wxComboBox::Undo()
714 void wxComboBox::Redo()
719 void wxComboBox::SelectAll()
721 SetSelection(0, GetLastPosition());
724 bool wxComboBox::CanUndo() const
730 bool wxComboBox::CanRedo() const
736 bool wxComboBox::HasSelection() const
739 GetSelection(&from
, &to
);
743 bool wxComboBox::CanCopy() const
745 // Can copy if there's a selection
746 return HasSelection();
749 bool wxComboBox::CanCut() const
751 return CanCopy() && IsEditable();
754 bool wxComboBox::CanPaste() const
756 // TODO: check for text on the clipboard
757 return IsEditable() ;
760 bool wxComboBox::IsEditable() const
762 return !HasFlag(wxCB_READONLY
);
766 void wxComboBox::SetInsertionPoint( long pos
)
768 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
770 if ( pos
== GetLastPosition() )
773 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
774 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
777 long wxComboBox::GetInsertionPoint() const
779 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
782 wxTextPos
wxComboBox::GetLastPosition() const
784 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
785 int pos
= GTK_ENTRY(entry
)->text_length
;
789 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
791 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
793 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
794 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
795 if (value
.IsNull()) return;
799 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
800 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
802 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.length(), &pos
);
806 void wxComboBox::SetSelection( long from
, long to
)
808 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
809 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
812 void wxComboBox::GetSelection( long* from
, long* to
) const
816 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
817 *from
= (long) editable
->selection_start_pos
;
818 *to
= (long) editable
->selection_end_pos
;
822 void wxComboBox::SetEditable( bool editable
)
824 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
825 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
828 void wxComboBox::OnChar( wxKeyEvent
&event
)
830 if ( event
.GetKeyCode() == WXK_RETURN
)
832 // GTK automatically selects an item if its in the list
833 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
834 eventEnter
.SetString( GetValue() );
835 eventEnter
.SetInt( GetSelection() );
836 eventEnter
.SetEventObject( this );
838 if (!GetEventHandler()->ProcessEvent( eventEnter
))
840 // This will invoke the dialog default action, such
841 // as the clicking the default button.
843 wxWindow
*top_frame
= m_parent
;
844 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
845 top_frame
= top_frame
->GetParent();
847 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
849 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
851 if (window
->default_widget
)
852 gtk_widget_activate (window
->default_widget
);
856 // Catch GTK event so that GTK doesn't open the drop
857 // down list upon RETURN.
864 void wxComboBox::DisableEvents()
866 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
867 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
868 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
869 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
872 void wxComboBox::EnableEvents()
874 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
875 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
876 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
877 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
880 void wxComboBox::OnSize( wxSizeEvent
&event
)
882 // NB: In some situations (e.g. on non-first page of a wizard, if the
883 // size used is default size), GtkCombo widget is resized correctly,
884 // but it's look is not updated, it's rendered as if it was much wider.
885 // No other widgets are affected, so it looks like a bug in GTK+.
886 // Manually requesting resize calculation (as gtk_pizza_set_size does)
888 if (GTK_WIDGET_VISIBLE(m_widget
))
889 gtk_widget_queue_resize(m_widget
);
894 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
896 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
898 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
899 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
901 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
902 GList
*child
= list
->children
;
905 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
907 GtkBin
*bin
= GTK_BIN(child
->data
);
908 gtk_widget_modify_style( bin
->child
, style
);
914 GtkWidget
* wxComboBox::GetConnectWidget()
916 return GTK_COMBO(m_widget
)->entry
;
919 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
921 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
922 (window
== GTK_COMBO(m_widget
)->button
->window
) );
925 wxSize
wxComboBox::DoGetBestSize() const
927 wxSize
ret( wxControl::DoGetBestSize() );
929 // we know better our horizontal extent: it depends on the longest string
934 unsigned int count
= GetCount();
935 for ( unsigned int n
= 0; n
< count
; n
++ )
937 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
943 // empty combobox should have some reasonable default size too
953 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
955 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
958 // ----------------------------------------------------------------------------
959 // standard event handling
960 // ----------------------------------------------------------------------------
962 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
967 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
972 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
977 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
982 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
987 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
990 GetSelection(& from
, & to
);
991 if (from
!= -1 && to
!= -1)
995 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
997 SetSelection(-1, -1);
1000 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1002 event
.Enable( CanCut() );
1005 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1007 event
.Enable( CanCopy() );
1010 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1012 event
.Enable( CanPaste() );
1015 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1017 event
.Enable( CanUndo() );
1020 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1022 event
.Enable( CanRedo() );
1025 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1027 event
.Enable(HasSelection() && IsEditable()) ;
1030 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1032 event
.Enable(GetLastPosition() > 0);