1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "combobox.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 #include "wx/combobox.h"
21 #include "wx/settings.h"
22 #include "wx/arrstr.h"
25 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
27 #include "wx/gtk/private.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern void wxapp_install_idle_handler();
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern bool g_blockEventsOnDrag
;
41 static int g_SelectionBeforePopup
= -1;
42 //-----------------------------------------------------------------------------
43 // "changed" - typing and list item matches get changed, select-child
44 // if it doesn't match an item then just get a single changed
45 //-----------------------------------------------------------------------------
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
);
67 gtk_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
72 gtk_popup_hide_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
74 // when the popup is hidden, throw a SELECTED event only if the combobox
76 int curSelection
= combo
->GetSelection();
77 if (g_SelectionBeforePopup
!= curSelection
)
79 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
80 event
.SetInt( curSelection
);
81 event
.SetString( combo
->GetStringSelection() );
82 event
.SetEventObject( combo
);
83 combo
->GetEventHandler()->ProcessEvent( event
);
86 // reset the selection flag to an identifiable value
87 g_SelectionBeforePopup
= -1;
91 gtk_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
93 // store the combobox selection value before the popup is shown
94 g_SelectionBeforePopup
= combo
->GetSelection();
97 //-----------------------------------------------------------------------------
98 // "select-child" - click/cursor get select-child, changed, select-child
99 //-----------------------------------------------------------------------------
102 gtk_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
104 if (g_isIdle
) wxapp_install_idle_handler();
106 if (!combo
->m_hasVMT
) return;
108 if (g_blockEventsOnDrag
) return;
110 int curSelection
= combo
->GetSelection();
112 if (combo
->m_prevSelection
== curSelection
) return;
114 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
115 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
117 combo
->m_prevSelection
= curSelection
;
119 // Quickly set the value of the combo box
120 // as GTK+ does that only AFTER the event
122 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
),
123 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
124 combo
->SetValue( combo
->GetStringSelection() );
125 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
), "changed",
126 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
128 // throw a SELECTED event only if the combobox popup is hidden
129 // because when combobox popup is shown, gtk_combo_select_child_callback is
130 // called each times the mouse is over an item with a pressed button so a lot
131 // of SELECTED event could be generated if the user keep the mouse button down
132 // and select other items ...
133 if (g_SelectionBeforePopup
== -1)
135 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
136 event
.SetInt( curSelection
);
137 event
.SetString( combo
->GetStringSelection() );
138 event
.SetEventObject( combo
);
139 combo
->GetEventHandler()->ProcessEvent( event
);
142 // Now send the event ourselves
143 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
144 event2
.SetString( combo
->GetValue() );
145 event2
.SetEventObject( combo
);
146 combo
->GetEventHandler()->ProcessEvent( event2
);
149 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
153 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
155 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
156 EVT_SIZE(wxComboBox::OnSize
)
157 EVT_CHAR(wxComboBox::OnChar
)
159 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
160 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
161 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
162 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
163 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
164 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
165 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
167 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
168 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
169 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
170 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
171 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
172 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
173 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
176 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
177 const wxString
& value
,
178 const wxPoint
& pos
, const wxSize
& size
,
179 const wxArrayString
& choices
,
180 long style
, const wxValidator
& validator
,
181 const wxString
& name
)
183 wxCArrayString
chs(choices
);
185 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
186 chs
.GetStrings(), style
, validator
, name
);
189 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
190 const wxPoint
& pos
, const wxSize
& size
,
191 int n
, const wxString choices
[],
192 long style
, const wxValidator
& validator
,
193 const wxString
& name
)
195 m_ignoreNextUpdate
= FALSE
;
197 m_acceptsFocus
= TRUE
;
200 if (!PreCreation( parent
, pos
, size
) ||
201 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
203 wxFAIL_MSG( wxT("wxComboBox creation failed") );
207 m_widget
= gtk_combo_new();
208 GtkCombo
*combo
= GTK_COMBO(m_widget
);
210 // Disable GTK's broken events ...
211 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
212 // ... and add surogate handler.
213 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
214 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
216 // make it more useable
217 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
219 // and case-sensitive
220 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
223 if (style
& wxNO_BORDER
)
224 g_object_set( GTK_ENTRY( combo
->entry
), "has-frame", FALSE
, NULL
);
227 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
230 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
233 for (int i
= 0; i
< n
; i
++)
235 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
237 m_clientDataList
.Append( (wxObject
*)NULL
);
238 m_clientObjectList
.Append( (wxObject
*)NULL
);
240 gtk_container_add( GTK_CONTAINER(list
), list_item
);
242 gtk_widget_show( list_item
);
245 m_parent
->DoAddChild( this );
247 m_focusWidget
= combo
->entry
;
251 ConnectWidget( combo
->button
);
253 // MSW's combo box shows the value and the selection is -1
254 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
255 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
257 if (style
& wxCB_READONLY
)
258 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
260 // "show" and "hide" events are generated when user click on the combobox button which popups a list
261 // this list is the "popwin" gtk widget
262 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "hide",
263 GTK_SIGNAL_FUNC(gtk_popup_hide_callback
), (gpointer
)this );
264 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "show",
265 GTK_SIGNAL_FUNC(gtk_popup_show_callback
), (gpointer
)this );
267 gtk_signal_connect( GTK_OBJECT(combo
->entry
), "changed",
268 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
270 gtk_signal_connect( GTK_OBJECT(combo
->list
), "select-child",
271 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
273 SetBestSize(size
); // need this too because this is a wxControlWithItems
275 // This is required for tool bar support
276 wxSize setsize
= GetSize();
277 gtk_widget_set_usize( m_widget
, setsize
.x
, setsize
.y
);
282 wxComboBox::~wxComboBox()
284 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
287 wxClientData
*cd
= (wxClientData
*)node
->GetData();
289 node
= node
->GetNext();
291 m_clientObjectList
.Clear();
293 m_clientDataList
.Clear();
296 void wxComboBox::SetFocus()
300 // don't do anything if we already have focus
304 gtk_widget_grab_focus( m_focusWidget
);
307 int wxComboBox::DoAppend( const wxString
&item
)
309 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
313 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
315 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
317 gtk_container_add( GTK_CONTAINER(list
), list_item
);
319 if (GTK_WIDGET_REALIZED(m_widget
))
321 gtk_widget_realize( list_item
);
322 gtk_widget_realize( GTK_BIN(list_item
)->child
);
325 // Apply current widget style to the new list_item
326 GtkRcStyle
*style
= CreateWidgetStyle();
329 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
330 GtkBin
*bin
= GTK_BIN( list_item
);
331 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
332 gtk_widget_modify_style( label
, style
);
333 gtk_rc_style_unref( style
);
336 gtk_widget_show( list_item
);
338 const int count
= GetCount();
340 if ( (int)m_clientDataList
.GetCount() < count
)
341 m_clientDataList
.Append( (wxObject
*) NULL
);
342 if ( (int)m_clientObjectList
.GetCount() < count
)
343 m_clientObjectList
.Append( (wxObject
*) NULL
);
347 InvalidateBestSize();
352 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
354 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
355 wxT("can't insert into sorted list"));
357 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
359 int count
= GetCount();
360 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
367 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
369 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
371 GList
*gitem_list
= g_list_alloc ();
372 gitem_list
->data
= list_item
;
373 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
375 if (GTK_WIDGET_REALIZED(m_widget
))
377 gtk_widget_realize( list_item
);
378 gtk_widget_realize( GTK_BIN(list_item
)->child
);
383 gtk_widget_show( list_item
);
387 if ( (int)m_clientDataList
.GetCount() < count
)
388 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
389 if ( (int)m_clientObjectList
.GetCount() < count
)
390 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
394 InvalidateBestSize();
399 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
401 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
403 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
406 node
->SetData( (wxObject
*) clientData
);
409 void* wxComboBox::DoGetItemClientData( int n
) const
411 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
413 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
415 return node
? node
->GetData() : NULL
;
418 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
420 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
422 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
425 // wxItemContainer already deletes data for us
427 node
->SetData( (wxObject
*) clientData
);
430 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
432 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
434 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
436 return node
? (wxClientData
*) node
->GetData() : NULL
;
439 void wxComboBox::Clear()
441 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
445 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
446 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
448 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
451 wxClientData
*cd
= (wxClientData
*)node
->GetData();
453 node
= node
->GetNext();
455 m_clientObjectList
.Clear();
457 m_clientDataList
.Clear();
461 InvalidateBestSize();
464 void wxComboBox::Delete( int n
)
466 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
468 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
470 GList
*child
= g_list_nth( listbox
->children
, n
);
474 wxFAIL_MSG(wxT("wrong index"));
480 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
481 gtk_list_remove_items( listbox
, list
);
484 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
487 wxClientData
*cd
= (wxClientData
*)node
->GetData();
489 m_clientObjectList
.Erase( node
);
492 node
= m_clientDataList
.Item( n
);
494 m_clientDataList
.Erase( node
);
498 InvalidateBestSize();
501 void wxComboBox::SetString(int n
, const wxString
&text
)
503 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
505 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
507 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
510 GtkBin
*bin
= GTK_BIN( child
->data
);
511 GtkLabel
*label
= GTK_LABEL( bin
->child
);
512 gtk_label_set_text(label
, wxGTK_CONV(text
));
516 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
519 InvalidateBestSize();
522 int wxComboBox::FindString( const wxString
&item
) const
524 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
526 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
528 GList
*child
= GTK_LIST(list
)->children
;
532 GtkBin
*bin
= GTK_BIN( child
->data
);
533 GtkLabel
*label
= GTK_LABEL( bin
->child
);
535 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
537 wxString
str( label
->label
);
549 int wxComboBox::GetSelection() const
551 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
553 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
555 GList
*selection
= GTK_LIST(list
)->selection
;
558 GList
*child
= GTK_LIST(list
)->children
;
562 if (child
->data
== selection
->data
) return count
;
571 wxString
wxComboBox::GetString( int n
) const
573 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
575 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
578 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
581 GtkBin
*bin
= GTK_BIN( child
->data
);
582 GtkLabel
*label
= GTK_LABEL( bin
->child
);
584 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
586 str
= wxString( label
->label
);
591 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
597 wxString
wxComboBox::GetStringSelection() const
599 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
601 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
603 GList
*selection
= GTK_LIST(list
)->selection
;
606 GtkBin
*bin
= GTK_BIN( selection
->data
);
607 GtkLabel
*label
= GTK_LABEL( bin
->child
);
609 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
611 wxString
tmp( label
->label
);
616 wxFAIL_MSG( wxT("wxComboBox: no selection") );
621 int wxComboBox::GetCount() const
623 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
625 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
627 GList
*child
= GTK_LIST(list
)->children
;
629 while (child
) { count
++; child
= child
->next
; }
633 void wxComboBox::SetSelection( int n
)
635 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
639 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
640 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
641 gtk_list_select_item( GTK_LIST(list
), n
);
647 bool wxComboBox::SetStringSelection( const wxString
&string
)
649 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid combobox") );
651 int res
= FindString( string
);
652 if (res
== -1) return false;
657 wxString
wxComboBox::GetValue() const
659 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
660 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
663 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
666 printf( "%d ", (int) (c
) );
674 void wxComboBox::SetValue( const wxString
& value
)
676 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
678 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
679 wxString tmp
= wxT("");
680 if (!value
.IsNull()) tmp
= value
;
681 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
683 InvalidateBestSize();
686 void wxComboBox::Copy()
688 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
690 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
691 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
694 void wxComboBox::Cut()
696 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
698 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
699 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
702 void wxComboBox::Paste()
704 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
706 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
707 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
710 void wxComboBox::Undo()
715 void wxComboBox::Redo()
720 void wxComboBox::SelectAll()
722 SetSelection(0, GetLastPosition());
725 bool wxComboBox::CanUndo() const
731 bool wxComboBox::CanRedo() const
737 bool wxComboBox::HasSelection() const
740 GetSelection(&from
, &to
);
744 bool wxComboBox::CanCopy() const
746 // Can copy if there's a selection
747 return HasSelection();
750 bool wxComboBox::CanCut() const
752 return CanCopy() && IsEditable();
755 bool wxComboBox::CanPaste() const
757 // TODO: check for text on the clipboard
758 return IsEditable() ;
761 bool wxComboBox::IsEditable() const
763 return !HasFlag(wxCB_READONLY
);
767 void wxComboBox::SetInsertionPoint( long pos
)
769 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
771 if ( pos
== GetLastPosition() )
774 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
775 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
778 long wxComboBox::GetInsertionPoint() const
780 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
783 long wxComboBox::GetLastPosition() const
785 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
786 int pos
= GTK_ENTRY(entry
)->text_length
;
790 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
792 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
794 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
795 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
796 if (value
.IsNull()) return;
800 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
801 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
803 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
807 void wxComboBox::SetSelection( long from
, long to
)
809 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
810 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
813 void wxComboBox::GetSelection( long* from
, long* to
) const
817 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
820 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
824 *from
= (long) editable
->selection_start_pos
;
825 *to
= (long) editable
->selection_end_pos
;
830 void wxComboBox::SetEditable( bool editable
)
832 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
833 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
836 void wxComboBox::OnChar( wxKeyEvent
&event
)
838 if ( event
.GetKeyCode() == WXK_RETURN
)
840 // GTK automatically selects an item if its in the list
841 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, GetId());
842 event
.SetString( GetValue() );
843 event
.SetInt( GetSelection() );
844 event
.SetEventObject( this );
846 if (!GetEventHandler()->ProcessEvent( event
))
848 // This will invoke the dialog default action, such
849 // as the clicking the default button.
851 wxWindow
*top_frame
= m_parent
;
852 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
853 top_frame
= top_frame
->GetParent();
855 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
857 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
859 if (window
->default_widget
)
860 gtk_widget_activate (window
->default_widget
);
864 // Catch GTK event so that GTK doesn't open the drop
865 // down list upon RETURN.
872 void wxComboBox::DisableEvents()
874 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
875 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
876 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
877 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
880 void wxComboBox::EnableEvents()
882 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
883 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
884 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
885 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
888 void wxComboBox::OnSize( wxSizeEvent
&event
)
890 // NB: In some situations (e.g. on non-first page of a wizard, if the
891 // size used is default size), GtkCombo widget is resized correctly,
892 // but it's look is not updated, it's rendered as if it was much wider.
893 // No other widgets are affected, so it looks like a bug in GTK+.
894 // Manually requesting resize calculation (as gtk_pizza_set_size does)
896 if (GTK_WIDGET_VISIBLE(m_widget
))
897 gtk_widget_queue_resize(m_widget
);
902 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
904 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
906 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
907 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
909 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
910 GList
*child
= list
->children
;
913 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
915 GtkBin
*bin
= GTK_BIN(child
->data
);
916 gtk_widget_modify_style( bin
->child
, style
);
922 GtkWidget
* wxComboBox::GetConnectWidget()
924 return GTK_COMBO(m_widget
)->entry
;
927 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
929 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
930 (window
== GTK_COMBO(m_widget
)->button
->window
) );
933 wxSize
wxComboBox::DoGetBestSize() const
935 wxSize
ret( wxControl::DoGetBestSize() );
937 // we know better our horizontal extent: it depends on the longest string
942 size_t count
= GetCount();
943 for ( size_t n
= 0; n
< count
; n
++ )
945 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
951 // empty combobox should have some reasonable default size too
961 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
963 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
966 // ----------------------------------------------------------------------------
967 // standard event handling
968 // ----------------------------------------------------------------------------
970 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
975 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
980 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
985 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
990 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
995 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
998 GetSelection(& from
, & to
);
999 if (from
!= -1 && to
!= -1)
1003 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1005 SetSelection(-1, -1);
1008 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1010 event
.Enable( CanCut() );
1013 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1015 event
.Enable( CanCopy() );
1018 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1020 event
.Enable( CanPaste() );
1023 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1025 event
.Enable( CanUndo() );
1028 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1030 event
.Enable( CanRedo() );
1033 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1035 event
.Enable(HasSelection() && IsEditable()) ;
1038 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1040 event
.Enable(GetLastPosition() > 0);