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
);
222 if (style
& wxNO_BORDER
)
223 g_object_set( GTK_ENTRY( combo
->entry
), "has-frame", FALSE
, NULL
);
225 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
228 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
231 for (int i
= 0; i
< n
; i
++)
233 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
235 m_clientDataList
.Append( (wxObject
*)NULL
);
236 m_clientObjectList
.Append( (wxObject
*)NULL
);
238 gtk_container_add( GTK_CONTAINER(list
), list_item
);
240 gtk_widget_show( list_item
);
243 m_parent
->DoAddChild( this );
245 m_focusWidget
= combo
->entry
;
249 ConnectWidget( combo
->button
);
251 // MSW's combo box shows the value and the selection is -1
252 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
253 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
255 if (style
& wxCB_READONLY
)
256 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
258 // "show" and "hide" events are generated when user click on the combobox button which popups a list
259 // this list is the "popwin" gtk widget
260 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "hide",
261 GTK_SIGNAL_FUNC(gtk_popup_hide_callback
), (gpointer
)this );
262 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "show",
263 GTK_SIGNAL_FUNC(gtk_popup_show_callback
), (gpointer
)this );
265 gtk_signal_connect( GTK_OBJECT(combo
->entry
), "changed",
266 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
268 gtk_signal_connect( GTK_OBJECT(combo
->list
), "select-child",
269 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
271 SetBestSize(size
); // need this too because this is a wxControlWithItems
273 // This is required for tool bar support
274 wxSize setsize
= GetSize();
275 gtk_widget_set_usize( m_widget
, setsize
.x
, setsize
.y
);
280 wxComboBox::~wxComboBox()
282 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
285 wxClientData
*cd
= (wxClientData
*)node
->GetData();
287 node
= node
->GetNext();
289 m_clientObjectList
.Clear();
291 m_clientDataList
.Clear();
294 void wxComboBox::SetFocus()
298 // don't do anything if we already have focus
302 gtk_widget_grab_focus( m_focusWidget
);
305 int wxComboBox::DoAppend( const wxString
&item
)
307 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
311 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
313 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
315 gtk_container_add( GTK_CONTAINER(list
), list_item
);
317 if (GTK_WIDGET_REALIZED(m_widget
))
319 gtk_widget_realize( list_item
);
320 gtk_widget_realize( GTK_BIN(list_item
)->child
);
323 // Apply current widget style to the new list_item
324 GtkRcStyle
*style
= CreateWidgetStyle();
327 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
328 GtkBin
*bin
= GTK_BIN( list_item
);
329 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
330 gtk_widget_modify_style( label
, style
);
331 gtk_rc_style_unref( style
);
334 gtk_widget_show( list_item
);
336 const int count
= GetCount();
338 if ( (int)m_clientDataList
.GetCount() < count
)
339 m_clientDataList
.Append( (wxObject
*) NULL
);
340 if ( (int)m_clientObjectList
.GetCount() < count
)
341 m_clientObjectList
.Append( (wxObject
*) NULL
);
345 InvalidateBestSize();
350 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
352 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
353 wxT("can't insert into sorted list"));
355 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
357 int count
= GetCount();
358 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
365 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
367 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
369 GList
*gitem_list
= g_list_alloc ();
370 gitem_list
->data
= list_item
;
371 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
373 if (GTK_WIDGET_REALIZED(m_widget
))
375 gtk_widget_realize( list_item
);
376 gtk_widget_realize( GTK_BIN(list_item
)->child
);
381 gtk_widget_show( list_item
);
385 if ( (int)m_clientDataList
.GetCount() < count
)
386 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
387 if ( (int)m_clientObjectList
.GetCount() < count
)
388 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
392 InvalidateBestSize();
397 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
399 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
401 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
404 node
->SetData( (wxObject
*) clientData
);
407 void* wxComboBox::DoGetItemClientData( int n
) const
409 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
411 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
413 return node
? node
->GetData() : NULL
;
416 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
418 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
420 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
423 // wxItemContainer already deletes data for us
425 node
->SetData( (wxObject
*) clientData
);
428 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
430 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
432 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
434 return node
? (wxClientData
*) node
->GetData() : NULL
;
437 void wxComboBox::Clear()
439 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
443 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
444 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
446 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
449 wxClientData
*cd
= (wxClientData
*)node
->GetData();
451 node
= node
->GetNext();
453 m_clientObjectList
.Clear();
455 m_clientDataList
.Clear();
459 InvalidateBestSize();
462 void wxComboBox::Delete( int n
)
464 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
466 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
468 GList
*child
= g_list_nth( listbox
->children
, n
);
472 wxFAIL_MSG(wxT("wrong index"));
478 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
479 gtk_list_remove_items( listbox
, list
);
482 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
485 wxClientData
*cd
= (wxClientData
*)node
->GetData();
487 m_clientObjectList
.Erase( node
);
490 node
= m_clientDataList
.Item( n
);
492 m_clientDataList
.Erase( node
);
496 InvalidateBestSize();
499 void wxComboBox::SetString(int n
, const wxString
&text
)
501 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
503 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
505 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
508 GtkBin
*bin
= GTK_BIN( child
->data
);
509 GtkLabel
*label
= GTK_LABEL( bin
->child
);
510 gtk_label_set_text(label
, wxGTK_CONV(text
));
514 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
517 InvalidateBestSize();
520 int wxComboBox::FindString( const wxString
&item
) const
522 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
524 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
526 GList
*child
= GTK_LIST(list
)->children
;
530 GtkBin
*bin
= GTK_BIN( child
->data
);
531 GtkLabel
*label
= GTK_LABEL( bin
->child
);
533 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
535 wxString
str( label
->label
);
547 int wxComboBox::GetSelection() const
549 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
551 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
553 GList
*selection
= GTK_LIST(list
)->selection
;
556 GList
*child
= GTK_LIST(list
)->children
;
560 if (child
->data
== selection
->data
) return count
;
569 wxString
wxComboBox::GetString( int n
) const
571 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
573 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
576 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
579 GtkBin
*bin
= GTK_BIN( child
->data
);
580 GtkLabel
*label
= GTK_LABEL( bin
->child
);
582 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
584 str
= wxString( label
->label
);
589 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
595 wxString
wxComboBox::GetStringSelection() const
597 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
599 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
601 GList
*selection
= GTK_LIST(list
)->selection
;
604 GtkBin
*bin
= GTK_BIN( selection
->data
);
605 GtkLabel
*label
= GTK_LABEL( bin
->child
);
607 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
609 wxString
tmp( label
->label
);
614 wxFAIL_MSG( wxT("wxComboBox: no selection") );
619 int wxComboBox::GetCount() const
621 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
623 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
625 GList
*child
= GTK_LIST(list
)->children
;
627 while (child
) { count
++; child
= child
->next
; }
631 void wxComboBox::SetSelection( int n
)
633 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
637 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
638 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
639 gtk_list_select_item( GTK_LIST(list
), n
);
645 bool wxComboBox::SetStringSelection( const wxString
&string
)
647 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid combobox") );
649 int res
= FindString( string
);
650 if (res
== -1) return false;
655 wxString
wxComboBox::GetValue() const
657 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
658 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
661 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
664 printf( "%d ", (int) (c
) );
672 void wxComboBox::SetValue( const wxString
& value
)
674 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
676 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
677 wxString tmp
= wxT("");
678 if (!value
.IsNull()) tmp
= value
;
679 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
681 InvalidateBestSize();
684 void wxComboBox::Copy()
686 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
688 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
689 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
692 void wxComboBox::Cut()
694 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
696 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
697 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
700 void wxComboBox::Paste()
702 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
704 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
705 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
708 void wxComboBox::Undo()
713 void wxComboBox::Redo()
718 void wxComboBox::SelectAll()
720 SetSelection(0, GetLastPosition());
723 bool wxComboBox::CanUndo() const
729 bool wxComboBox::CanRedo() const
735 bool wxComboBox::HasSelection() const
738 GetSelection(&from
, &to
);
742 bool wxComboBox::CanCopy() const
744 // Can copy if there's a selection
745 return HasSelection();
748 bool wxComboBox::CanCut() const
750 return CanCopy() && IsEditable();
753 bool wxComboBox::CanPaste() const
755 // TODO: check for text on the clipboard
756 return IsEditable() ;
759 bool wxComboBox::IsEditable() const
761 return !HasFlag(wxCB_READONLY
);
765 void wxComboBox::SetInsertionPoint( long pos
)
767 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
769 if ( pos
== GetLastPosition() )
772 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
773 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
776 long wxComboBox::GetInsertionPoint() const
778 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
781 long wxComboBox::GetLastPosition() const
783 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
784 int pos
= GTK_ENTRY(entry
)->text_length
;
788 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
790 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
792 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
793 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
794 if (value
.IsNull()) return;
798 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
799 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
801 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
805 void wxComboBox::SetSelection( long from
, long to
)
807 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
808 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
811 void wxComboBox::GetSelection( long* from
, long* to
) const
815 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
818 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
822 *from
= (long) editable
->selection_start_pos
;
823 *to
= (long) editable
->selection_end_pos
;
828 void wxComboBox::SetEditable( bool editable
)
830 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
831 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
834 void wxComboBox::OnChar( wxKeyEvent
&event
)
836 if ( event
.GetKeyCode() == WXK_RETURN
)
838 // GTK automatically selects an item if its in the list
839 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, GetId());
840 event
.SetString( GetValue() );
841 event
.SetInt( GetSelection() );
842 event
.SetEventObject( this );
844 if (!GetEventHandler()->ProcessEvent( event
))
846 // This will invoke the dialog default action, such
847 // as the clicking the default button.
849 wxWindow
*top_frame
= m_parent
;
850 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
851 top_frame
= top_frame
->GetParent();
853 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
855 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
857 if (window
->default_widget
)
858 gtk_widget_activate (window
->default_widget
);
862 // Catch GTK event so that GTK doesn't open the drop
863 // down list upon RETURN.
870 void wxComboBox::DisableEvents()
872 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
873 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
874 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
875 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
878 void wxComboBox::EnableEvents()
880 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
881 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
882 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
883 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
886 void wxComboBox::OnSize( wxSizeEvent
&event
)
888 // NB: In some situations (e.g. on non-first page of a wizard, if the
889 // size used is default size), GtkCombo widget is resized correctly,
890 // but it's look is not updated, it's rendered as if it was much wider.
891 // No other widgets are affected, so it looks like a bug in GTK+.
892 // Manually requesting resize calculation (as gtk_pizza_set_size does)
894 if (GTK_WIDGET_VISIBLE(m_widget
))
895 gtk_widget_queue_resize(m_widget
);
900 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
902 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
904 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
905 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
907 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
908 GList
*child
= list
->children
;
911 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
913 GtkBin
*bin
= GTK_BIN(child
->data
);
914 gtk_widget_modify_style( bin
->child
, style
);
920 GtkWidget
* wxComboBox::GetConnectWidget()
922 return GTK_COMBO(m_widget
)->entry
;
925 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
927 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
928 (window
== GTK_COMBO(m_widget
)->button
->window
) );
931 wxSize
wxComboBox::DoGetBestSize() const
933 wxSize
ret( wxControl::DoGetBestSize() );
935 // we know better our horizontal extent: it depends on the longest string
940 size_t count
= GetCount();
941 for ( size_t n
= 0; n
< count
; n
++ )
943 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
949 // empty combobox should have some reasonable default size too
959 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
961 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
964 // ----------------------------------------------------------------------------
965 // standard event handling
966 // ----------------------------------------------------------------------------
968 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
973 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
978 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
983 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
988 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
993 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
996 GetSelection(& from
, & to
);
997 if (from
!= -1 && to
!= -1)
1001 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1003 SetSelection(-1, -1);
1006 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1008 event
.Enable( CanCut() );
1011 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1013 event
.Enable( CanCopy() );
1016 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1018 event
.Enable( CanPaste() );
1021 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1023 event
.Enable( CanUndo() );
1026 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1028 event
.Enable( CanRedo() );
1031 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1033 event
.Enable(HasSelection() && IsEditable()) ;
1036 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1038 event
.Enable(GetLastPosition() > 0);