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_after( 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_after( GTK_OBJECT(combo
->entry
), "changed",
268 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
270 gtk_signal_connect_after( 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 wxString
wxComboBox::GetValue() const
649 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
650 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
653 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
656 printf( "%d ", (int) (c
) );
664 void wxComboBox::SetValue( const wxString
& value
)
666 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
668 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
669 wxString tmp
= wxT("");
670 if (!value
.IsNull()) tmp
= value
;
671 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
673 InvalidateBestSize();
676 void wxComboBox::Copy()
678 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
680 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
681 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
684 void wxComboBox::Cut()
686 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
688 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
689 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
692 void wxComboBox::Paste()
694 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
696 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
697 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
700 void wxComboBox::Undo()
705 void wxComboBox::Redo()
710 void wxComboBox::SelectAll()
712 SetSelection(0, GetLastPosition());
715 bool wxComboBox::CanUndo() const
721 bool wxComboBox::CanRedo() const
727 bool wxComboBox::HasSelection() const
730 GetSelection(&from
, &to
);
734 bool wxComboBox::CanCopy() const
736 // Can copy if there's a selection
737 return HasSelection();
740 bool wxComboBox::CanCut() const
742 return CanCopy() && IsEditable();
745 bool wxComboBox::CanPaste() const
747 // TODO: check for text on the clipboard
748 return IsEditable() ;
751 bool wxComboBox::IsEditable() const
753 return !HasFlag(wxCB_READONLY
);
757 void wxComboBox::SetInsertionPoint( long pos
)
759 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
761 if ( pos
== GetLastPosition() )
764 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
765 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
768 long wxComboBox::GetInsertionPoint() const
770 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
773 wxTextPos
wxComboBox::GetLastPosition() const
775 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
776 int pos
= GTK_ENTRY(entry
)->text_length
;
780 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
782 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
784 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
785 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
786 if (value
.IsNull()) return;
790 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
791 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
793 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
797 void wxComboBox::SetSelection( long from
, long to
)
799 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
800 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
803 void wxComboBox::GetSelection( long* from
, long* to
) const
807 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
810 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
814 *from
= (long) editable
->selection_start_pos
;
815 *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
event(wxEVT_COMMAND_TEXT_ENTER
, GetId());
832 event
.SetString( GetValue() );
833 event
.SetInt( GetSelection() );
834 event
.SetEventObject( this );
836 if (!GetEventHandler()->ProcessEvent( event
))
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 size_t count
= GetCount();
933 for ( size_t 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);