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
21 #include "wx/arrstr.h"
24 #include "wx/gtk1/private.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
38 static int g_SelectionBeforePopup
= wxID_NONE
; // this means the popup is hidden
40 //-----------------------------------------------------------------------------
41 // "changed" - typing and list item matches get changed, select-child
42 // if it doesn't match an item then just get a single changed
43 //-----------------------------------------------------------------------------
47 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
49 if (g_isIdle
) wxapp_install_idle_handler();
51 if (combo
->m_ignoreNextUpdate
)
53 combo
->m_ignoreNextUpdate
= false;
57 if (!combo
->m_hasVMT
) return;
59 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
60 event
.SetString( combo
->GetValue() );
61 event
.SetEventObject( combo
);
62 combo
->GetEventHandler()->ProcessEvent( event
);
68 gtk_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
75 gtk_popup_hide_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
77 // when the popup is hidden, throw a SELECTED event only if the combobox
79 const int curSelection
= combo
->GetCurrentSelection();
81 const bool hasChanged
= curSelection
!= g_SelectionBeforePopup
;
83 // reset the selection flag to value meaning that it is hidden and do it
84 // now, before generating the events, so that GetSelection() returns the
85 // new value from the event handler
86 g_SelectionBeforePopup
= wxID_NONE
;
90 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
91 event
.SetInt( curSelection
);
92 event
.SetString( combo
->GetStringSelection() );
93 event
.SetEventObject( combo
);
94 combo
->GetEventHandler()->ProcessEvent( event
);
96 // for consistency with the other ports, send TEXT event
97 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
98 event2
.SetString( combo
->GetStringSelection() );
99 event2
.SetEventObject( combo
);
100 combo
->GetEventHandler()->ProcessEvent( event2
);
107 gtk_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
109 // store the combobox selection value before the popup is shown
110 g_SelectionBeforePopup
= combo
->GetCurrentSelection();
114 //-----------------------------------------------------------------------------
115 // "select-child" - click/cursor get select-child, changed, select-child
116 //-----------------------------------------------------------------------------
120 gtk_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
122 if (g_isIdle
) wxapp_install_idle_handler();
124 if (!combo
->m_hasVMT
) return;
126 if (g_blockEventsOnDrag
) return;
128 int curSelection
= combo
->GetCurrentSelection();
130 if (combo
->m_prevSelection
== curSelection
) return;
132 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
133 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
135 combo
->m_prevSelection
= curSelection
;
137 // Quickly set the value of the combo box
138 // as GTK+ does that only AFTER the event
140 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
),
141 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
142 combo
->SetValue( combo
->GetStringSelection() );
143 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
), "changed",
144 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
146 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
147 // because when combobox popup is shown, gtk_combo_select_child_callback is
148 // called each times the mouse is over an item with a pressed button so a lot
149 // of SELECTED event could be generated if the user keep the mouse button down
150 // and select other items ...
151 if (g_SelectionBeforePopup
== wxID_NONE
)
153 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
154 event
.SetInt( curSelection
);
155 event
.SetString( combo
->GetStringSelection() );
156 event
.SetEventObject( combo
);
157 combo
->GetEventHandler()->ProcessEvent( event
);
159 // for consistency with the other ports, don't generate text update
160 // events while the user is browsing the combobox neither
161 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
162 event2
.SetString( combo
->GetValue() );
163 event2
.SetEventObject( combo
);
164 combo
->GetEventHandler()->ProcessEvent( event2
);
169 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
173 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
175 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
176 EVT_SIZE(wxComboBox::OnSize
)
177 EVT_CHAR(wxComboBox::OnChar
)
179 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
180 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
181 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
182 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
183 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
184 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
185 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
187 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
188 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
189 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
190 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
191 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
192 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
193 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
196 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
197 const wxString
& value
,
198 const wxPoint
& pos
, const wxSize
& size
,
199 const wxArrayString
& choices
,
200 long style
, const wxValidator
& validator
,
201 const wxString
& name
)
203 wxCArrayString
chs(choices
);
205 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
206 chs
.GetStrings(), style
, validator
, name
);
209 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
210 const wxPoint
& pos
, const wxSize
& size
,
211 int n
, const wxString choices
[],
212 long style
, const wxValidator
& validator
,
213 const wxString
& name
)
215 m_ignoreNextUpdate
= false;
217 m_acceptsFocus
= true;
220 if (!PreCreation( parent
, pos
, size
) ||
221 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
223 wxFAIL_MSG( wxT("wxComboBox creation failed") );
227 m_widget
= gtk_combo_new();
228 GtkCombo
*combo
= GTK_COMBO(m_widget
);
230 // Disable GTK's broken events ...
231 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
232 // ... and add surrogate handler.
233 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
234 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
236 // make it more useable
237 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
239 // and case-sensitive
240 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
242 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
244 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
246 for (int i
= 0; i
< n
; i
++)
248 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
250 m_clientDataList
.Append( (wxObject
*)NULL
);
251 m_clientObjectList
.Append( (wxObject
*)NULL
);
253 gtk_container_add( GTK_CONTAINER(list
), list_item
);
255 gtk_widget_show( list_item
);
258 m_parent
->DoAddChild( this );
260 m_focusWidget
= combo
->entry
;
264 ConnectWidget( combo
->button
);
266 // MSW's combo box shows the value and the selection is -1
267 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
268 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
270 if (style
& wxCB_READONLY
)
271 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
273 // "show" and "hide" events are generated when user click on the combobox button which popups a list
274 // this list is the "popwin" gtk widget
275 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "hide",
276 GTK_SIGNAL_FUNC(gtk_popup_hide_callback
), (gpointer
)this );
277 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "show",
278 GTK_SIGNAL_FUNC(gtk_popup_show_callback
), (gpointer
)this );
280 gtk_signal_connect_after( GTK_OBJECT(combo
->entry
), "changed",
281 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
283 gtk_signal_connect_after( GTK_OBJECT(combo
->list
), "select-child",
284 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
286 SetInitialSize(size
); // need this too because this is a wxControlWithItems
288 // This is required for tool bar support
289 // wxSize setsize = GetSize();
290 // gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
295 wxComboBox::~wxComboBox()
297 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
300 wxClientData
*cd
= (wxClientData
*)node
->GetData();
302 node
= node
->GetNext();
304 m_clientObjectList
.Clear();
306 m_clientDataList
.Clear();
309 void wxComboBox::SetFocus()
313 // don't do anything if we already have focus
317 gtk_widget_grab_focus( m_focusWidget
);
320 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
323 wxClientDataType type
)
325 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
329 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
331 GtkRcStyle
*style
= CreateWidgetStyle();
333 const unsigned int count
= items
.GetCount();
334 for( unsigned int i
= 0; i
< count
; ++i
, ++pos
)
337 list_item
= gtk_list_item_new_with_label( wxGTK_CONV( items
[i
] ) );
339 if ( pos
== GetCount() )
341 gtk_container_add( GTK_CONTAINER(list
), list_item
);
343 else // insert, not append
345 GList
*gitem_list
= g_list_alloc ();
346 gitem_list
->data
= list_item
;
347 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
350 if (GTK_WIDGET_REALIZED(m_widget
))
352 gtk_widget_realize( list_item
);
353 gtk_widget_realize( GTK_BIN(list_item
)->child
);
357 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
358 GtkBin
*bin
= GTK_BIN( list_item
);
359 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
360 gtk_widget_modify_style( label
, style
);
365 gtk_widget_show( list_item
);
367 if ( m_clientDataList
.GetCount() < GetCount() )
368 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
369 if ( m_clientObjectList
.GetCount() < GetCount() )
370 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
372 AssignNewItemClientData(pos
, clientData
, i
, type
);
376 gtk_rc_style_unref( style
);
380 InvalidateBestSize();
385 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
387 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
389 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
392 node
->SetData( (wxObject
*) clientData
);
395 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
397 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
399 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
401 return node
? node
->GetData() : NULL
;
404 void wxComboBox::DoClear()
406 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
410 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
411 gtk_list_clear_items( GTK_LIST(list
), 0, (int)GetCount() );
413 m_clientObjectList
.Clear();
415 m_clientDataList
.Clear();
419 InvalidateBestSize();
422 void wxComboBox::DoDeleteOneItem(unsigned int n
)
424 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
426 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
428 GList
*child
= g_list_nth( listbox
->children
, n
);
432 wxFAIL_MSG(wxT("wrong index"));
438 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
439 gtk_list_remove_items( listbox
, list
);
442 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
445 m_clientObjectList
.Erase( node
);
448 node
= m_clientDataList
.Item( n
);
450 m_clientDataList
.Erase( node
);
454 InvalidateBestSize();
457 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
459 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
461 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
463 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
466 GtkBin
*bin
= GTK_BIN( child
->data
);
467 GtkLabel
*label
= GTK_LABEL( bin
->child
);
468 gtk_label_set_text(label
, wxGTK_CONV(text
));
472 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
475 InvalidateBestSize();
478 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
480 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
482 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
484 GList
*child
= GTK_LIST(list
)->children
;
488 GtkBin
*bin
= GTK_BIN( child
->data
);
489 GtkLabel
*label
= GTK_LABEL( bin
->child
);
490 wxString
str( label
->label
);
491 if (item
.IsSameAs( str
, bCase
) )
501 int wxComboBox::GetSelection() const
503 // if the popup is currently opened, use the selection as it had been
504 // before it dropped down
505 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
506 : g_SelectionBeforePopup
;
509 int wxComboBox::GetCurrentSelection() const
511 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
513 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
515 GList
*selection
= GTK_LIST(list
)->selection
;
518 GList
*child
= GTK_LIST(list
)->children
;
522 if (child
->data
== selection
->data
) return count
;
531 wxString
wxComboBox::GetString(unsigned int n
) const
533 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
535 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
538 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
541 GtkBin
*bin
= GTK_BIN( child
->data
);
542 GtkLabel
*label
= GTK_LABEL( bin
->child
);
543 str
= wxString( label
->label
);
547 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
553 wxString
wxComboBox::GetStringSelection() const
555 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
557 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
559 GList
*selection
= GTK_LIST(list
)->selection
;
562 GtkBin
*bin
= GTK_BIN( selection
->data
);
563 GtkLabel
*label
= GTK_LABEL( bin
->child
);
564 wxString
tmp( label
->label
);
568 wxFAIL_MSG( wxT("wxComboBox: no selection") );
570 return wxEmptyString
;
573 unsigned int wxComboBox::GetCount() const
575 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
577 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
579 GList
*child
= GTK_LIST(list
)->children
;
580 unsigned int count
= 0;
581 while (child
) { count
++; child
= child
->next
; }
585 void wxComboBox::SetSelection( int n
)
587 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
591 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
592 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
593 gtk_list_select_item( GTK_LIST(list
), n
);
599 wxString
wxComboBox::GetValue() const
601 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
602 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
605 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
608 printf( "%d ", (int) (c
) );
616 void wxComboBox::SetValue( const wxString
& value
)
618 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
620 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
621 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( value
) );
623 InvalidateBestSize();
626 void wxComboBox::WriteText(const wxString
& value
)
628 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
630 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
631 GtkEditable
* const edit
= GTK_EDITABLE(entry
);
633 gtk_editable_delete_selection(edit
);
634 gint len
= gtk_editable_get_position(edit
);
635 gtk_editable_insert_text(edit
, wxGTK_CONV(value
), -1, &len
);
636 gtk_editable_set_position(edit
, len
);
639 void wxComboBox::Copy()
641 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
643 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
644 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
647 void wxComboBox::Cut()
649 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
651 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
652 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
655 void wxComboBox::Paste()
657 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
659 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
660 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
663 void wxComboBox::Undo()
668 void wxComboBox::Redo()
673 void wxComboBox::SelectAll()
675 SetSelection(0, GetLastPosition());
678 bool wxComboBox::CanUndo() const
684 bool wxComboBox::CanRedo() const
690 bool wxComboBox::HasSelection() const
693 GetSelection(&from
, &to
);
697 bool wxComboBox::CanCopy() const
699 // Can copy if there's a selection
700 return HasSelection();
703 bool wxComboBox::CanCut() const
705 return CanCopy() && IsEditable();
708 bool wxComboBox::CanPaste() const
710 // TODO: check for text on the clipboard
711 return IsEditable() ;
714 bool wxComboBox::IsEditable() const
716 return !HasFlag(wxCB_READONLY
);
720 void wxComboBox::SetInsertionPoint( long pos
)
722 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
724 if ( pos
== GetLastPosition() )
727 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
728 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
731 long wxComboBox::GetInsertionPoint() const
733 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
736 wxTextPos
wxComboBox::GetLastPosition() const
738 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
739 int pos
= GTK_ENTRY(entry
)->text_length
;
743 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
745 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
747 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
748 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
749 if (value
.IsNull()) return;
753 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
754 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
756 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.length(), &pos
);
760 void wxComboBox::SetSelection( long from
, long to
)
762 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
763 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
766 void wxComboBox::GetSelection( long* from
, long* to
) const
770 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
771 *from
= (long) editable
->selection_start_pos
;
772 *to
= (long) editable
->selection_end_pos
;
776 void wxComboBox::SetEditable( bool editable
)
778 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
779 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
782 void wxComboBox::OnChar( wxKeyEvent
&event
)
784 if ( event
.GetKeyCode() == WXK_RETURN
)
786 // GTK automatically selects an item if its in the list
787 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
788 eventEnter
.SetString( GetValue() );
789 eventEnter
.SetInt( GetSelection() );
790 eventEnter
.SetEventObject( this );
792 if (!GetEventHandler()->ProcessEvent( eventEnter
))
794 // This will invoke the dialog default action, such
795 // as the clicking the default button.
797 wxWindow
*top_frame
= m_parent
;
798 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
799 top_frame
= top_frame
->GetParent();
801 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
803 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
805 if (window
->default_widget
)
806 gtk_widget_activate (window
->default_widget
);
810 // Catch GTK event so that GTK doesn't open the drop
811 // down list upon RETURN.
818 void wxComboBox::DisableEvents()
820 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
821 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
822 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
823 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
826 void wxComboBox::EnableEvents()
828 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
829 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
830 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
831 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
834 void wxComboBox::OnSize( wxSizeEvent
&event
)
836 // NB: In some situations (e.g. on non-first page of a wizard, if the
837 // size used is default size), GtkCombo widget is resized correctly,
838 // but it's look is not updated, it's rendered as if it was much wider.
839 // No other widgets are affected, so it looks like a bug in GTK+.
840 // Manually requesting resize calculation (as gtk_pizza_set_size does)
842 if (GTK_WIDGET_VISIBLE(m_widget
))
843 gtk_widget_queue_resize(m_widget
);
848 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
850 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
852 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
853 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
855 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
856 GList
*child
= list
->children
;
859 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
861 GtkBin
*bin
= GTK_BIN(child
->data
);
862 gtk_widget_modify_style( bin
->child
, style
);
868 GtkWidget
* wxComboBox::GetConnectWidget()
870 return GTK_COMBO(m_widget
)->entry
;
873 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
875 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
876 (window
== GTK_COMBO(m_widget
)->button
->window
) );
879 wxSize
wxComboBox::DoGetBestSize() const
881 wxSize
ret( wxControl::DoGetBestSize() );
883 // we know better our horizontal extent: it depends on the longest string
888 unsigned int count
= GetCount();
889 for ( unsigned int n
= 0; n
< count
; n
++ )
891 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
897 // empty combobox should have some reasonable default size too
907 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
909 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
912 // ----------------------------------------------------------------------------
913 // standard event handling
914 // ----------------------------------------------------------------------------
916 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
921 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
926 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
931 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
936 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
941 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
944 GetSelection(& from
, & to
);
945 if (from
!= -1 && to
!= -1)
949 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
951 SetSelection(-1, -1);
954 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
956 event
.Enable( CanCut() );
959 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
961 event
.Enable( CanCopy() );
964 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
966 event
.Enable( CanPaste() );
969 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
971 event
.Enable( CanUndo() );
974 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
976 event
.Enable( CanRedo() );
979 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
981 event
.Enable(HasSelection() && IsEditable()) ;
984 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
986 event
.Enable(GetLastPosition() > 0);