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"
17 #include "wx/settings.h"
18 #include "wx/arrstr.h"
21 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
23 #include "wx/gtk1/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 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
),
140 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
141 combo
->SetValue( combo
->GetStringSelection() );
142 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
), "changed",
143 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
145 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
146 // because when combobox popup is shown, gtk_combo_select_child_callback is
147 // called each times the mouse is over an item with a pressed button so a lot
148 // of SELECTED event could be generated if the user keep the mouse button down
149 // and select other items ...
150 if (g_SelectionBeforePopup
== wxID_NONE
)
152 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
153 event
.SetInt( curSelection
);
154 event
.SetString( combo
->GetStringSelection() );
155 event
.SetEventObject( combo
);
156 combo
->GetEventHandler()->ProcessEvent( event
);
158 // for consistency with the other ports, don't generate text update
159 // events while the user is browsing the combobox neither
160 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
161 event2
.SetString( combo
->GetValue() );
162 event2
.SetEventObject( combo
);
163 combo
->GetEventHandler()->ProcessEvent( event2
);
168 //-----------------------------------------------------------------------------
170 //-----------------------------------------------------------------------------
172 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
174 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
175 EVT_SIZE(wxComboBox::OnSize
)
176 EVT_CHAR(wxComboBox::OnChar
)
178 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
179 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
180 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
181 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
182 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
183 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
184 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
186 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
187 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
188 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
189 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
190 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
191 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
192 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
195 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
196 const wxString
& value
,
197 const wxPoint
& pos
, const wxSize
& size
,
198 const wxArrayString
& choices
,
199 long style
, const wxValidator
& validator
,
200 const wxString
& name
)
202 wxCArrayString
chs(choices
);
204 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
205 chs
.GetStrings(), style
, validator
, name
);
208 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
209 const wxPoint
& pos
, const wxSize
& size
,
210 int n
, const wxString choices
[],
211 long style
, const wxValidator
& validator
,
212 const wxString
& name
)
214 m_ignoreNextUpdate
= false;
216 m_acceptsFocus
= true;
219 if (!PreCreation( parent
, pos
, size
) ||
220 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
222 wxFAIL_MSG( wxT("wxComboBox creation failed") );
226 m_widget
= gtk_combo_new();
227 GtkCombo
*combo
= GTK_COMBO(m_widget
);
229 // Disable GTK's broken events ...
230 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
231 // ... and add surrogate handler.
232 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
233 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
235 // make it more useable
236 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
238 // and case-sensitive
239 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
241 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
243 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
245 for (int i
= 0; i
< n
; i
++)
247 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
249 m_clientDataList
.Append( (wxObject
*)NULL
);
250 m_clientObjectList
.Append( (wxObject
*)NULL
);
252 gtk_container_add( GTK_CONTAINER(list
), list_item
);
254 gtk_widget_show( list_item
);
257 m_parent
->DoAddChild( this );
259 m_focusWidget
= combo
->entry
;
263 ConnectWidget( combo
->button
);
265 // MSW's combo box shows the value and the selection is -1
266 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
267 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
269 if (style
& wxCB_READONLY
)
270 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
272 // "show" and "hide" events are generated when user click on the combobox button which popups a list
273 // this list is the "popwin" gtk widget
274 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "hide",
275 GTK_SIGNAL_FUNC(gtk_popup_hide_callback
), (gpointer
)this );
276 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "show",
277 GTK_SIGNAL_FUNC(gtk_popup_show_callback
), (gpointer
)this );
279 gtk_signal_connect_after( GTK_OBJECT(combo
->entry
), "changed",
280 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
282 gtk_signal_connect_after( GTK_OBJECT(combo
->list
), "select-child",
283 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
285 SetBestSize(size
); // need this too because this is a wxControlWithItems
287 // This is required for tool bar support
288 // wxSize setsize = GetSize();
289 // gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
294 wxComboBox::~wxComboBox()
296 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
299 wxClientData
*cd
= (wxClientData
*)node
->GetData();
301 node
= node
->GetNext();
303 m_clientObjectList
.Clear();
305 m_clientDataList
.Clear();
308 void wxComboBox::SetFocus()
312 // don't do anything if we already have focus
316 gtk_widget_grab_focus( m_focusWidget
);
319 int wxComboBox::DoAppend( const wxString
&item
)
321 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
325 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
327 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
329 gtk_container_add( GTK_CONTAINER(list
), list_item
);
331 if (GTK_WIDGET_REALIZED(m_widget
))
333 gtk_widget_realize( list_item
);
334 gtk_widget_realize( GTK_BIN(list_item
)->child
);
337 // Apply current widget style to the new list_item
338 GtkRcStyle
*style
= CreateWidgetStyle();
341 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
342 GtkBin
*bin
= GTK_BIN( list_item
);
343 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
344 gtk_widget_modify_style( label
, style
);
345 gtk_rc_style_unref( style
);
348 gtk_widget_show( list_item
);
350 const unsigned int count
= GetCount();
352 if ( m_clientDataList
.GetCount() < count
)
353 m_clientDataList
.Append( (wxObject
*) NULL
);
354 if ( m_clientObjectList
.GetCount() < count
)
355 m_clientObjectList
.Append( (wxObject
*) NULL
);
359 InvalidateBestSize();
364 int wxComboBox::DoInsert( const wxString
&item
, unsigned int pos
)
366 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
367 wxT("can't insert into sorted list"));
369 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
371 wxCHECK_MSG( IsValidInsert(pos
), -1, wxT("invalid index") );
373 if (pos
== GetCount())
378 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
380 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
382 GList
*gitem_list
= g_list_alloc ();
383 gitem_list
->data
= list_item
;
384 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
386 if (GTK_WIDGET_REALIZED(m_widget
))
388 gtk_widget_realize( list_item
);
389 gtk_widget_realize( GTK_BIN(list_item
)->child
);
394 gtk_widget_show( list_item
);
396 const unsigned int count
= GetCount();
398 if ( m_clientDataList
.GetCount() < count
)
399 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
400 if ( m_clientObjectList
.GetCount() < count
)
401 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
405 InvalidateBestSize();
410 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
412 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
414 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
417 node
->SetData( (wxObject
*) clientData
);
420 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
422 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
424 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
426 return node
? node
->GetData() : NULL
;
429 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
431 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
433 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
436 // wxItemContainer already deletes data for us
438 node
->SetData( (wxObject
*) clientData
);
441 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
443 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
445 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
447 return node
? (wxClientData
*) node
->GetData() : NULL
;
450 void wxComboBox::Clear()
452 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
456 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
457 gtk_list_clear_items( GTK_LIST(list
), 0, (int)GetCount() );
459 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
462 wxClientData
*cd
= (wxClientData
*)node
->GetData();
464 node
= node
->GetNext();
466 m_clientObjectList
.Clear();
468 m_clientDataList
.Clear();
472 InvalidateBestSize();
475 void wxComboBox::Delete(unsigned int n
)
477 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
479 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
481 GList
*child
= g_list_nth( listbox
->children
, n
);
485 wxFAIL_MSG(wxT("wrong index"));
491 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
492 gtk_list_remove_items( listbox
, list
);
495 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
498 wxClientData
*cd
= (wxClientData
*)node
->GetData();
500 m_clientObjectList
.Erase( node
);
503 node
= m_clientDataList
.Item( n
);
505 m_clientDataList
.Erase( node
);
509 InvalidateBestSize();
512 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
514 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
516 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
518 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
521 GtkBin
*bin
= GTK_BIN( child
->data
);
522 GtkLabel
*label
= GTK_LABEL( bin
->child
);
523 gtk_label_set_text(label
, wxGTK_CONV(text
));
527 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
530 InvalidateBestSize();
533 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
535 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
537 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
539 GList
*child
= GTK_LIST(list
)->children
;
543 GtkBin
*bin
= GTK_BIN( child
->data
);
544 GtkLabel
*label
= GTK_LABEL( bin
->child
);
545 wxString
str( label
->label
);
546 if (item
.IsSameAs( str
, bCase
) )
556 int wxComboBox::GetSelection() const
558 // if the popup is currently opened, use the selection as it had been
559 // before it dropped down
560 return g_SelectionBeforePopup
== wxID_NONE
? GetCurrentSelection()
561 : g_SelectionBeforePopup
;
564 int wxComboBox::GetCurrentSelection() const
566 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
568 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
570 GList
*selection
= GTK_LIST(list
)->selection
;
573 GList
*child
= GTK_LIST(list
)->children
;
577 if (child
->data
== selection
->data
) return count
;
586 wxString
wxComboBox::GetString(unsigned int n
) const
588 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
590 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
593 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
596 GtkBin
*bin
= GTK_BIN( child
->data
);
597 GtkLabel
*label
= GTK_LABEL( bin
->child
);
598 str
= wxString( label
->label
);
602 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
608 wxString
wxComboBox::GetStringSelection() const
610 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
612 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
614 GList
*selection
= GTK_LIST(list
)->selection
;
617 GtkBin
*bin
= GTK_BIN( selection
->data
);
618 GtkLabel
*label
= GTK_LABEL( bin
->child
);
619 wxString
tmp( label
->label
);
623 wxFAIL_MSG( wxT("wxComboBox: no selection") );
625 return wxEmptyString
;
628 unsigned int wxComboBox::GetCount() const
630 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
632 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
634 GList
*child
= GTK_LIST(list
)->children
;
635 unsigned int count
= 0;
636 while (child
) { count
++; child
= child
->next
; }
640 void wxComboBox::SetSelection( int n
)
642 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
646 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
647 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
648 gtk_list_select_item( GTK_LIST(list
), n
);
654 wxString
wxComboBox::GetValue() const
656 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
657 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
660 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
663 printf( "%d ", (int) (c
) );
671 void wxComboBox::SetValue( const wxString
& value
)
673 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
675 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
677 if (!value
.IsNull()) tmp
= value
;
678 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
680 InvalidateBestSize();
683 void wxComboBox::Copy()
685 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
687 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
688 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
691 void wxComboBox::Cut()
693 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
695 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
696 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
699 void wxComboBox::Paste()
701 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
703 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
704 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
707 void wxComboBox::Undo()
712 void wxComboBox::Redo()
717 void wxComboBox::SelectAll()
719 SetSelection(0, GetLastPosition());
722 bool wxComboBox::CanUndo() const
728 bool wxComboBox::CanRedo() const
734 bool wxComboBox::HasSelection() const
737 GetSelection(&from
, &to
);
741 bool wxComboBox::CanCopy() const
743 // Can copy if there's a selection
744 return HasSelection();
747 bool wxComboBox::CanCut() const
749 return CanCopy() && IsEditable();
752 bool wxComboBox::CanPaste() const
754 // TODO: check for text on the clipboard
755 return IsEditable() ;
758 bool wxComboBox::IsEditable() const
760 return !HasFlag(wxCB_READONLY
);
764 void wxComboBox::SetInsertionPoint( long pos
)
766 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
768 if ( pos
== GetLastPosition() )
771 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
772 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
775 long wxComboBox::GetInsertionPoint() const
777 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
780 wxTextPos
wxComboBox::GetLastPosition() const
782 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
783 int pos
= GTK_ENTRY(entry
)->text_length
;
787 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
789 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
791 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
792 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
793 if (value
.IsNull()) return;
797 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
798 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
800 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.length(), &pos
);
804 void wxComboBox::SetSelection( long from
, long to
)
806 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
807 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
810 void wxComboBox::GetSelection( long* from
, long* to
) const
814 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
815 *from
= (long) editable
->selection_start_pos
;
816 *to
= (long) editable
->selection_end_pos
;
820 void wxComboBox::SetEditable( bool editable
)
822 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
823 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
826 void wxComboBox::OnChar( wxKeyEvent
&event
)
828 if ( event
.GetKeyCode() == WXK_RETURN
)
830 // GTK automatically selects an item if its in the list
831 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
832 eventEnter
.SetString( GetValue() );
833 eventEnter
.SetInt( GetSelection() );
834 eventEnter
.SetEventObject( this );
836 if (!GetEventHandler()->ProcessEvent( eventEnter
))
838 // This will invoke the dialog default action, such
839 // as the clicking the default button.
841 wxWindow
*top_frame
= m_parent
;
842 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
843 top_frame
= top_frame
->GetParent();
845 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
847 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
849 if (window
->default_widget
)
850 gtk_widget_activate (window
->default_widget
);
854 // Catch GTK event so that GTK doesn't open the drop
855 // down list upon RETURN.
862 void wxComboBox::DisableEvents()
864 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
865 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
866 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
867 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
870 void wxComboBox::EnableEvents()
872 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
873 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
874 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
875 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
878 void wxComboBox::OnSize( wxSizeEvent
&event
)
880 // NB: In some situations (e.g. on non-first page of a wizard, if the
881 // size used is default size), GtkCombo widget is resized correctly,
882 // but it's look is not updated, it's rendered as if it was much wider.
883 // No other widgets are affected, so it looks like a bug in GTK+.
884 // Manually requesting resize calculation (as gtk_pizza_set_size does)
886 if (GTK_WIDGET_VISIBLE(m_widget
))
887 gtk_widget_queue_resize(m_widget
);
892 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
894 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
896 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
897 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
899 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
900 GList
*child
= list
->children
;
903 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
905 GtkBin
*bin
= GTK_BIN(child
->data
);
906 gtk_widget_modify_style( bin
->child
, style
);
912 GtkWidget
* wxComboBox::GetConnectWidget()
914 return GTK_COMBO(m_widget
)->entry
;
917 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
919 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
920 (window
== GTK_COMBO(m_widget
)->button
->window
) );
923 wxSize
wxComboBox::DoGetBestSize() const
925 wxSize
ret( wxControl::DoGetBestSize() );
927 // we know better our horizontal extent: it depends on the longest string
932 unsigned int count
= GetCount();
933 for ( unsigned int n
= 0; n
< count
; n
++ )
935 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
941 // empty combobox should have some reasonable default size too
951 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
953 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
956 // ----------------------------------------------------------------------------
957 // standard event handling
958 // ----------------------------------------------------------------------------
960 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
965 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
970 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
975 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
980 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
985 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
988 GetSelection(& from
, & to
);
989 if (from
!= -1 && to
!= -1)
993 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
995 SetSelection(-1, -1);
998 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1000 event
.Enable( CanCut() );
1003 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1005 event
.Enable( CanCopy() );
1008 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1010 event
.Enable( CanPaste() );
1013 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1015 event
.Enable( CanUndo() );
1018 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1020 event
.Enable( CanRedo() );
1023 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1025 event
.Enable(HasSelection() && IsEditable()) ;
1028 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1030 event
.Enable(GetLastPosition() > 0);