1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/combobox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/combobox.h"
17 #include "wx/settings.h"
18 #include "wx/arrstr.h"
21 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
23 #include "wx/gtk/private.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
37 static int g_SelectionBeforePopup
= wxID_NONE
; // this means the popup is hidden
39 //-----------------------------------------------------------------------------
40 // "changed" - typing and list item matches get changed, select-child
41 // if it doesn't match an item then just get a single changed
42 //-----------------------------------------------------------------------------
46 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
48 if (g_isIdle
) wxapp_install_idle_handler();
50 if (combo
->m_ignoreNextUpdate
)
52 combo
->m_ignoreNextUpdate
= false;
56 if (!combo
->m_hasVMT
) return;
58 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
59 event
.SetString( combo
->GetValue() );
60 event
.SetEventObject( combo
);
61 combo
->GetEventHandler()->ProcessEvent( event
);
67 gtk_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
74 gtk_popup_hide_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
76 // when the popup is hidden, throw a SELECTED event only if the combobox
78 const int curSelection
= combo
->GetCurrentSelection();
80 const bool hasChanged
= curSelection
!= g_SelectionBeforePopup
;
82 // reset the selection flag to value meaning that it is hidden and do it
83 // now, before generating the events, so that GetSelection() returns the
84 // new value from the event handler
85 g_SelectionBeforePopup
= wxID_NONE
;
89 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
90 event
.SetInt( curSelection
);
91 event
.SetString( combo
->GetStringSelection() );
92 event
.SetEventObject( combo
);
93 combo
->GetEventHandler()->ProcessEvent( event
);
95 // for consistency with the other ports, send TEXT event
96 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
97 event2
.SetString( combo
->GetStringSelection() );
98 event2
.SetEventObject( combo
);
99 combo
->GetEventHandler()->ProcessEvent( event2
);
106 gtk_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
108 // store the combobox selection value before the popup is shown
109 g_SelectionBeforePopup
= combo
->GetCurrentSelection();
113 //-----------------------------------------------------------------------------
114 // "select-child" - click/cursor get select-child, changed, select-child
115 //-----------------------------------------------------------------------------
119 gtk_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
121 if (g_isIdle
) wxapp_install_idle_handler();
123 if (!combo
->m_hasVMT
) return;
125 if (g_blockEventsOnDrag
) return;
127 int curSelection
= combo
->GetCurrentSelection();
129 if (combo
->m_prevSelection
== curSelection
) return;
131 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
132 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
134 combo
->m_prevSelection
= curSelection
;
136 // Quickly set the value of the combo box
137 // as GTK+ does that only AFTER the event
139 g_signal_handlers_disconnect_by_func (GTK_COMBO (combo
->GetHandle())->entry
,
140 (gpointer
) gtk_text_changed_callback
,
142 combo
->SetValue( combo
->GetStringSelection() );
143 g_signal_connect_after (GTK_COMBO (combo
->GetHandle())->entry
, "changed",
144 G_CALLBACK (gtk_text_changed_callback
), 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 g_signal_handler_disconnect (combo
->entry
, combo
->entry_change_id
);
232 // ... and add surrogate handler.
233 combo
->entry_change_id
= g_signal_connect (combo
->entry
, "changed",
234 G_CALLBACK (gtk_dummy_callback
),
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 if (style
& wxNO_BORDER
)
244 g_object_set( GTK_ENTRY( combo
->entry
), "has-frame", FALSE
, NULL
);
246 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
248 for (int i
= 0; i
< n
; i
++)
250 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
252 m_clientDataList
.Append( (wxObject
*)NULL
);
253 m_clientObjectList
.Append( (wxObject
*)NULL
);
255 gtk_container_add( GTK_CONTAINER(list
), list_item
);
257 gtk_widget_show( list_item
);
260 m_parent
->DoAddChild( this );
262 m_focusWidget
= combo
->entry
;
266 ConnectWidget( combo
->button
);
268 // MSW's combo box shows the value and the selection is -1
269 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
270 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
272 if (style
& wxCB_READONLY
)
273 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
275 // "show" and "hide" events are generated when user click on the combobox button which popups a list
276 // this list is the "popwin" gtk widget
277 g_signal_connect (GTK_COMBO(combo
)->popwin
, "hide",
278 G_CALLBACK (gtk_popup_hide_callback
), this);
279 g_signal_connect (GTK_COMBO(combo
)->popwin
, "show",
280 G_CALLBACK (gtk_popup_show_callback
), this);
282 g_signal_connect_after (combo
->entry
, "changed",
283 G_CALLBACK (gtk_text_changed_callback
), this);
285 g_signal_connect_after (combo
->list
, "select-child",
286 G_CALLBACK (gtk_combo_select_child_callback
),
289 SetBestSize(size
); // need this too because this is a wxControlWithItems
291 // This is required for tool bar support
292 // wxSize setsize = GetSize();
293 // gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
298 wxComboBox::~wxComboBox()
300 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
303 wxClientData
*cd
= (wxClientData
*)node
->GetData();
305 node
= node
->GetNext();
307 m_clientObjectList
.Clear();
309 m_clientDataList
.Clear();
312 void wxComboBox::SetFocus()
316 // don't do anything if we already have focus
320 gtk_widget_grab_focus( m_focusWidget
);
323 int wxComboBox::DoAppend( const wxString
&item
)
325 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
329 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
331 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
333 gtk_container_add( GTK_CONTAINER(list
), list_item
);
335 if (GTK_WIDGET_REALIZED(m_widget
))
337 gtk_widget_realize( list_item
);
338 gtk_widget_realize( GTK_BIN(list_item
)->child
);
341 // Apply current widget style to the new list_item
342 GtkRcStyle
*style
= CreateWidgetStyle();
345 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
346 GtkBin
*bin
= GTK_BIN( list_item
);
347 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
348 gtk_widget_modify_style( label
, style
);
349 gtk_rc_style_unref( style
);
352 gtk_widget_show( list_item
);
354 const int count
= GetCount();
356 if ( (int)m_clientDataList
.GetCount() < count
)
357 m_clientDataList
.Append( (wxObject
*) NULL
);
358 if ( (int)m_clientObjectList
.GetCount() < count
)
359 m_clientObjectList
.Append( (wxObject
*) NULL
);
363 InvalidateBestSize();
368 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
370 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
371 wxT("can't insert into sorted list"));
373 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
375 int count
= GetCount();
376 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
383 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
385 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
387 GList
*gitem_list
= g_list_alloc ();
388 gitem_list
->data
= list_item
;
389 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
391 if (GTK_WIDGET_REALIZED(m_widget
))
393 gtk_widget_realize( list_item
);
394 gtk_widget_realize( GTK_BIN(list_item
)->child
);
399 gtk_widget_show( list_item
);
403 if ( (int)m_clientDataList
.GetCount() < count
)
404 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
405 if ( (int)m_clientObjectList
.GetCount() < count
)
406 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
410 InvalidateBestSize();
415 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
417 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
419 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
422 node
->SetData( (wxObject
*) clientData
);
425 void* wxComboBox::DoGetItemClientData( int n
) const
427 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
429 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
431 return node
? node
->GetData() : NULL
;
434 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
436 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
438 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
441 // wxItemContainer already deletes data for us
443 node
->SetData( (wxObject
*) clientData
);
446 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
448 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
450 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
452 return node
? (wxClientData
*) node
->GetData() : NULL
;
455 void wxComboBox::Clear()
457 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
461 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
462 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
464 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
467 wxClientData
*cd
= (wxClientData
*)node
->GetData();
469 node
= node
->GetNext();
471 m_clientObjectList
.Clear();
473 m_clientDataList
.Clear();
477 InvalidateBestSize();
480 void wxComboBox::Delete( int n
)
482 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
484 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
486 GList
*child
= g_list_nth( listbox
->children
, n
);
490 wxFAIL_MSG(wxT("wrong index"));
496 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
497 gtk_list_remove_items( listbox
, list
);
500 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
503 wxClientData
*cd
= (wxClientData
*)node
->GetData();
505 m_clientObjectList
.Erase( node
);
508 node
= m_clientDataList
.Item( n
);
510 m_clientDataList
.Erase( node
);
514 InvalidateBestSize();
517 void wxComboBox::SetString(int n
, const wxString
&text
)
519 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
521 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
523 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
526 GtkBin
*bin
= GTK_BIN( child
->data
);
527 GtkLabel
*label
= GTK_LABEL( bin
->child
);
528 gtk_label_set_text(label
, wxGTK_CONV(text
));
532 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
535 InvalidateBestSize();
538 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
540 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
542 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
544 GList
*child
= GTK_LIST(list
)->children
;
548 GtkBin
*bin
= GTK_BIN( child
->data
);
549 GtkLabel
*label
= GTK_LABEL( bin
->child
);
550 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
552 if (item
.IsSameAs( str
, bCase
) )
562 int wxComboBox::GetSelection() const
564 // if the popup is currently opened, use the selection as it had been
565 // before it dropped down
566 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
567 : g_SelectionBeforePopup
;
570 int wxComboBox::GetCurrentSelection() const
572 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
574 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
576 GList
*selection
= GTK_LIST(list
)->selection
;
579 GList
*child
= GTK_LIST(list
)->children
;
583 if (child
->data
== selection
->data
) return count
;
592 wxString
wxComboBox::GetString( int n
) const
594 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
596 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
599 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
602 GtkBin
*bin
= GTK_BIN( child
->data
);
603 GtkLabel
*label
= GTK_LABEL( bin
->child
);
604 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
608 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
614 wxString
wxComboBox::GetStringSelection() const
616 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
618 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
620 GList
*selection
= GTK_LIST(list
)->selection
;
623 GtkBin
*bin
= GTK_BIN( selection
->data
);
624 GtkLabel
*label
= GTK_LABEL( bin
->child
);
625 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
629 wxFAIL_MSG( wxT("wxComboBox: no selection") );
631 return wxEmptyString
;
634 int wxComboBox::GetCount() const
636 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
638 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
640 GList
*child
= GTK_LIST(list
)->children
;
642 while (child
) { count
++; child
= child
->next
; }
646 void wxComboBox::SetSelection( int n
)
648 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
652 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
653 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
654 gtk_list_select_item( GTK_LIST(list
), n
);
660 wxString
wxComboBox::GetValue() const
662 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
663 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
666 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
669 printf( "%d ", (int) (c
) );
677 void wxComboBox::SetValue( const wxString
& value
)
679 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
681 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
683 if (!value
.IsNull()) tmp
= value
;
684 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
686 InvalidateBestSize();
689 void wxComboBox::Copy()
691 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
693 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
694 gtk_editable_copy_clipboard(GTK_EDITABLE(entry
));
697 void wxComboBox::Cut()
699 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
701 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
702 gtk_editable_cut_clipboard(GTK_EDITABLE(entry
));
705 void wxComboBox::Paste()
707 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
709 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
710 gtk_editable_paste_clipboard(GTK_EDITABLE(entry
));
713 void wxComboBox::Undo()
718 void wxComboBox::Redo()
723 void wxComboBox::SelectAll()
725 SetSelection(0, GetLastPosition());
728 bool wxComboBox::CanUndo() const
734 bool wxComboBox::CanRedo() const
740 bool wxComboBox::HasSelection() const
743 GetSelection(&from
, &to
);
747 bool wxComboBox::CanCopy() const
749 // Can copy if there's a selection
750 return HasSelection();
753 bool wxComboBox::CanCut() const
755 return CanCopy() && IsEditable();
758 bool wxComboBox::CanPaste() const
760 // TODO: check for text on the clipboard
761 return IsEditable() ;
764 bool wxComboBox::IsEditable() const
766 return !HasFlag(wxCB_READONLY
);
770 void wxComboBox::SetInsertionPoint( long pos
)
772 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
774 if ( pos
== GetLastPosition() )
777 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
778 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
781 long wxComboBox::GetInsertionPoint() const
783 return (long) gtk_editable_get_position(GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
));
786 wxTextPos
wxComboBox::GetLastPosition() const
788 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
789 int pos
= GTK_ENTRY(entry
)->text_length
;
793 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
795 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
797 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
798 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
799 if (value
.IsNull()) return;
803 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
804 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
806 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
810 void wxComboBox::SetSelection( long from
, long to
)
812 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
813 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
816 void wxComboBox::GetSelection( long* from
, long* to
) const
820 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
822 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
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
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
840 eventEnter
.SetString( GetValue() );
841 eventEnter
.SetInt( GetSelection() );
842 eventEnter
.SetEventObject( this );
844 if (!GetEventHandler()->ProcessEvent( eventEnter
))
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 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->list
,
873 (gpointer
) gtk_combo_select_child_callback
,
875 g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget
)->entry
,
876 (gpointer
) gtk_text_changed_callback
,
880 void wxComboBox::EnableEvents()
882 g_signal_connect_after (GTK_COMBO(m_widget
)->list
, "select-child",
883 G_CALLBACK (gtk_combo_select_child_callback
),
885 g_signal_connect_after (GTK_COMBO(m_widget
)->entry
, "changed",
886 G_CALLBACK (gtk_text_changed_callback
),
890 void wxComboBox::OnSize( wxSizeEvent
&event
)
892 // NB: In some situations (e.g. on non-first page of a wizard, if the
893 // size used is default size), GtkCombo widget is resized correctly,
894 // but it's look is not updated, it's rendered as if it was much wider.
895 // No other widgets are affected, so it looks like a bug in GTK+.
896 // Manually requesting resize calculation (as gtk_pizza_set_size does)
898 if (GTK_WIDGET_VISIBLE(m_widget
))
899 gtk_widget_queue_resize(m_widget
);
904 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
906 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
908 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
909 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
911 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
912 GList
*child
= list
->children
;
915 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
917 GtkBin
*bin
= GTK_BIN(child
->data
);
918 gtk_widget_modify_style( bin
->child
, style
);
924 GtkWidget
* wxComboBox::GetConnectWidget()
926 return GTK_COMBO(m_widget
)->entry
;
929 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
931 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
932 (window
== GTK_COMBO(m_widget
)->button
->window
) );
935 wxSize
wxComboBox::DoGetBestSize() const
937 wxSize
ret( wxControl::DoGetBestSize() );
939 // we know better our horizontal extent: it depends on the longest string
944 size_t count
= GetCount();
945 for ( size_t n
= 0; n
< count
; n
++ )
947 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
953 // empty combobox should have some reasonable default size too
963 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
965 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
968 // ----------------------------------------------------------------------------
969 // standard event handling
970 // ----------------------------------------------------------------------------
972 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
977 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
982 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
987 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
992 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
997 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
1000 GetSelection(& from
, & to
);
1001 if (from
!= -1 && to
!= -1)
1005 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1007 SetSelection(-1, -1);
1010 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1012 event
.Enable( CanCut() );
1015 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1017 event
.Enable( CanCopy() );
1020 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1022 event
.Enable( CanPaste() );
1025 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1027 event
.Enable( CanUndo() );
1030 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1032 event
.Enable( CanRedo() );
1035 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1037 event
.Enable(HasSelection() && IsEditable()) ;
1040 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1042 event
.Enable(GetLastPosition() > 0);