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
= -2; // -2 <=> the popup is hidden
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 (-2 = hidden)
87 g_SelectionBeforePopup
= -2;
91 gtk_popup_show_callback(GtkCombo
*WXUNUSED(gtk_combo
), wxComboBox
*combo
)
93 // store the combobox selection value before the popup is shown
94 // if there is no selection, combo->GetSelection() returns -1
95 g_SelectionBeforePopup
= combo
->GetSelection();
98 //-----------------------------------------------------------------------------
99 // "select-child" - click/cursor get select-child, changed, select-child
100 //-----------------------------------------------------------------------------
103 gtk_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
105 if (g_isIdle
) wxapp_install_idle_handler();
107 if (!combo
->m_hasVMT
) return;
109 if (g_blockEventsOnDrag
) return;
111 int curSelection
= combo
->GetSelection();
113 if (combo
->m_prevSelection
== curSelection
) return;
115 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
116 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
118 combo
->m_prevSelection
= curSelection
;
120 // Quickly set the value of the combo box
121 // as GTK+ does that only AFTER the event
123 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
),
124 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
125 combo
->SetValue( combo
->GetStringSelection() );
126 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(combo
->GetHandle())->entry
), "changed",
127 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)combo
);
129 // throw a SELECTED event only if the combobox popup is hidden (-2)
130 // because when combobox popup is shown, gtk_combo_select_child_callback is
131 // called each times the mouse is over an item with a pressed button so a lot
132 // of SELECTED event could be generated if the user keep the mouse button down
133 // and select other items ...
134 if (g_SelectionBeforePopup
== -2)
136 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
137 event
.SetInt( curSelection
);
138 event
.SetString( combo
->GetStringSelection() );
139 event
.SetEventObject( combo
);
140 combo
->GetEventHandler()->ProcessEvent( event
);
143 // Now send the event ourselves
144 wxCommandEvent
event2( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
145 event2
.SetString( combo
->GetValue() );
146 event2
.SetEventObject( combo
);
147 combo
->GetEventHandler()->ProcessEvent( event2
);
150 //-----------------------------------------------------------------------------
152 //-----------------------------------------------------------------------------
154 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
156 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
157 EVT_SIZE(wxComboBox::OnSize
)
158 EVT_CHAR(wxComboBox::OnChar
)
160 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
161 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
162 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
163 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
164 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
165 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
166 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
168 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
169 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
170 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
171 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
172 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
173 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
174 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
177 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
178 const wxString
& value
,
179 const wxPoint
& pos
, const wxSize
& size
,
180 const wxArrayString
& choices
,
181 long style
, const wxValidator
& validator
,
182 const wxString
& name
)
184 wxCArrayString
chs(choices
);
186 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
187 chs
.GetStrings(), style
, validator
, name
);
190 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
191 const wxPoint
& pos
, const wxSize
& size
,
192 int n
, const wxString choices
[],
193 long style
, const wxValidator
& validator
,
194 const wxString
& name
)
196 m_ignoreNextUpdate
= false;
198 m_acceptsFocus
= true;
201 if (!PreCreation( parent
, pos
, size
) ||
202 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
204 wxFAIL_MSG( wxT("wxComboBox creation failed") );
208 m_widget
= gtk_combo_new();
209 GtkCombo
*combo
= GTK_COMBO(m_widget
);
211 // Disable GTK's broken events ...
212 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
213 // ... and add surogate handler.
214 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
215 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
217 // make it more useable
218 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
220 // and case-sensitive
221 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
224 if (style
& wxNO_BORDER
)
225 g_object_set( GTK_ENTRY( combo
->entry
), "has-frame", FALSE
, NULL
);
228 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
231 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
234 for (int i
= 0; i
< n
; i
++)
236 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
238 m_clientDataList
.Append( (wxObject
*)NULL
);
239 m_clientObjectList
.Append( (wxObject
*)NULL
);
241 gtk_container_add( GTK_CONTAINER(list
), list_item
);
243 gtk_widget_show( list_item
);
246 m_parent
->DoAddChild( this );
248 m_focusWidget
= combo
->entry
;
252 ConnectWidget( combo
->button
);
254 // MSW's combo box shows the value and the selection is -1
255 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
256 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
258 if (style
& wxCB_READONLY
)
259 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
261 // "show" and "hide" events are generated when user click on the combobox button which popups a list
262 // this list is the "popwin" gtk widget
263 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "hide",
264 GTK_SIGNAL_FUNC(gtk_popup_hide_callback
), (gpointer
)this );
265 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo
)->popwin
), "show",
266 GTK_SIGNAL_FUNC(gtk_popup_show_callback
), (gpointer
)this );
268 gtk_signal_connect_after( GTK_OBJECT(combo
->entry
), "changed",
269 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
271 gtk_signal_connect_after( GTK_OBJECT(combo
->list
), "select-child",
272 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
274 SetBestSize(size
); // need this too because this is a wxControlWithItems
276 // This is required for tool bar support
277 // wxSize setsize = GetSize();
278 // gtk_widget_set_usize( m_widget, setsize.x, setsize.y );
283 wxComboBox::~wxComboBox()
285 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
288 wxClientData
*cd
= (wxClientData
*)node
->GetData();
290 node
= node
->GetNext();
292 m_clientObjectList
.Clear();
294 m_clientDataList
.Clear();
297 void wxComboBox::SetFocus()
301 // don't do anything if we already have focus
305 gtk_widget_grab_focus( m_focusWidget
);
308 int wxComboBox::DoAppend( const wxString
&item
)
310 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
314 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
316 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
318 gtk_container_add( GTK_CONTAINER(list
), list_item
);
320 if (GTK_WIDGET_REALIZED(m_widget
))
322 gtk_widget_realize( list_item
);
323 gtk_widget_realize( GTK_BIN(list_item
)->child
);
326 // Apply current widget style to the new list_item
327 GtkRcStyle
*style
= CreateWidgetStyle();
330 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
331 GtkBin
*bin
= GTK_BIN( list_item
);
332 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
333 gtk_widget_modify_style( label
, style
);
334 gtk_rc_style_unref( style
);
337 gtk_widget_show( list_item
);
339 const int count
= GetCount();
341 if ( (int)m_clientDataList
.GetCount() < count
)
342 m_clientDataList
.Append( (wxObject
*) NULL
);
343 if ( (int)m_clientObjectList
.GetCount() < count
)
344 m_clientObjectList
.Append( (wxObject
*) NULL
);
348 InvalidateBestSize();
353 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
355 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
356 wxT("can't insert into sorted list"));
358 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
360 int count
= GetCount();
361 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
368 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
370 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
372 GList
*gitem_list
= g_list_alloc ();
373 gitem_list
->data
= list_item
;
374 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
376 if (GTK_WIDGET_REALIZED(m_widget
))
378 gtk_widget_realize( list_item
);
379 gtk_widget_realize( GTK_BIN(list_item
)->child
);
384 gtk_widget_show( list_item
);
388 if ( (int)m_clientDataList
.GetCount() < count
)
389 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
390 if ( (int)m_clientObjectList
.GetCount() < count
)
391 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
395 InvalidateBestSize();
400 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
402 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
404 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
407 node
->SetData( (wxObject
*) clientData
);
410 void* wxComboBox::DoGetItemClientData( int n
) const
412 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
414 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
416 return node
? node
->GetData() : NULL
;
419 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
421 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
423 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
426 // wxItemContainer already deletes data for us
428 node
->SetData( (wxObject
*) clientData
);
431 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
433 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
435 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
437 return node
? (wxClientData
*) node
->GetData() : NULL
;
440 void wxComboBox::Clear()
442 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
446 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
447 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
449 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
452 wxClientData
*cd
= (wxClientData
*)node
->GetData();
454 node
= node
->GetNext();
456 m_clientObjectList
.Clear();
458 m_clientDataList
.Clear();
462 InvalidateBestSize();
465 void wxComboBox::Delete( int n
)
467 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
469 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
471 GList
*child
= g_list_nth( listbox
->children
, n
);
475 wxFAIL_MSG(wxT("wrong index"));
481 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
482 gtk_list_remove_items( listbox
, list
);
485 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
488 wxClientData
*cd
= (wxClientData
*)node
->GetData();
490 m_clientObjectList
.Erase( node
);
493 node
= m_clientDataList
.Item( n
);
495 m_clientDataList
.Erase( node
);
499 InvalidateBestSize();
502 void wxComboBox::SetString(int n
, const wxString
&text
)
504 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
506 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
508 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
511 GtkBin
*bin
= GTK_BIN( child
->data
);
512 GtkLabel
*label
= GTK_LABEL( bin
->child
);
513 gtk_label_set_text(label
, wxGTK_CONV(text
));
517 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
520 InvalidateBestSize();
523 int wxComboBox::FindString( const wxString
&item
) const
525 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
527 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
529 GList
*child
= GTK_LIST(list
)->children
;
533 GtkBin
*bin
= GTK_BIN( child
->data
);
534 GtkLabel
*label
= GTK_LABEL( bin
->child
);
536 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
538 wxString
str( label
->label
);
550 int wxComboBox::GetSelection() const
552 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
554 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
556 GList
*selection
= GTK_LIST(list
)->selection
;
559 GList
*child
= GTK_LIST(list
)->children
;
563 if (child
->data
== selection
->data
) return count
;
572 wxString
wxComboBox::GetString( int n
) const
574 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
576 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
579 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
582 GtkBin
*bin
= GTK_BIN( child
->data
);
583 GtkLabel
*label
= GTK_LABEL( bin
->child
);
585 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
587 str
= wxString( label
->label
);
592 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
598 wxString
wxComboBox::GetStringSelection() const
600 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
602 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
604 GList
*selection
= GTK_LIST(list
)->selection
;
607 GtkBin
*bin
= GTK_BIN( selection
->data
);
608 GtkLabel
*label
= GTK_LABEL( bin
->child
);
610 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
612 wxString
tmp( label
->label
);
617 wxFAIL_MSG( wxT("wxComboBox: no selection") );
622 int wxComboBox::GetCount() const
624 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
626 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
628 GList
*child
= GTK_LIST(list
)->children
;
630 while (child
) { count
++; child
= child
->next
; }
634 void wxComboBox::SetSelection( int n
)
636 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
640 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
641 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
642 gtk_list_select_item( GTK_LIST(list
), n
);
648 wxString
wxComboBox::GetValue() const
650 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
651 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
654 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
657 printf( "%d ", (int) (c
) );
665 void wxComboBox::SetValue( const wxString
& value
)
667 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
669 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
670 wxString tmp
= wxT("");
671 if (!value
.IsNull()) tmp
= value
;
672 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
674 InvalidateBestSize();
677 void wxComboBox::Copy()
679 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
681 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
682 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
685 void wxComboBox::Cut()
687 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
689 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
690 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
693 void wxComboBox::Paste()
695 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
697 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
698 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
701 void wxComboBox::Undo()
706 void wxComboBox::Redo()
711 void wxComboBox::SelectAll()
713 SetSelection(0, GetLastPosition());
716 bool wxComboBox::CanUndo() const
722 bool wxComboBox::CanRedo() const
728 bool wxComboBox::HasSelection() const
731 GetSelection(&from
, &to
);
735 bool wxComboBox::CanCopy() const
737 // Can copy if there's a selection
738 return HasSelection();
741 bool wxComboBox::CanCut() const
743 return CanCopy() && IsEditable();
746 bool wxComboBox::CanPaste() const
748 // TODO: check for text on the clipboard
749 return IsEditable() ;
752 bool wxComboBox::IsEditable() const
754 return !HasFlag(wxCB_READONLY
);
758 void wxComboBox::SetInsertionPoint( long pos
)
760 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
762 if ( pos
== GetLastPosition() )
765 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
766 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
769 long wxComboBox::GetInsertionPoint() const
771 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
774 wxTextPos
wxComboBox::GetLastPosition() const
776 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
777 int pos
= GTK_ENTRY(entry
)->text_length
;
781 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
783 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
785 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
786 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
787 if (value
.IsNull()) return;
791 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
792 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
794 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
798 void wxComboBox::SetSelection( long from
, long to
)
800 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
801 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
804 void wxComboBox::GetSelection( long* from
, long* to
) const
808 GtkEditable
*editable
= GTK_EDITABLE(GTK_COMBO(m_widget
)->entry
);
811 gtk_editable_get_selection_bounds(editable
, & start
, & end
);
815 *from
= (long) editable
->selection_start_pos
;
816 *to
= (long) editable
->selection_end_pos
;
821 void wxComboBox::SetEditable( bool editable
)
823 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
824 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
827 void wxComboBox::OnChar( wxKeyEvent
&event
)
829 if ( event
.GetKeyCode() == WXK_RETURN
)
831 // GTK automatically selects an item if its in the list
832 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, GetId());
833 event
.SetString( GetValue() );
834 event
.SetInt( GetSelection() );
835 event
.SetEventObject( this );
837 if (!GetEventHandler()->ProcessEvent( event
))
839 // This will invoke the dialog default action, such
840 // as the clicking the default button.
842 wxWindow
*top_frame
= m_parent
;
843 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
844 top_frame
= top_frame
->GetParent();
846 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
848 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
850 if (window
->default_widget
)
851 gtk_widget_activate (window
->default_widget
);
855 // Catch GTK event so that GTK doesn't open the drop
856 // down list upon RETURN.
863 void wxComboBox::DisableEvents()
865 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
866 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
867 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
868 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
871 void wxComboBox::EnableEvents()
873 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
874 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
875 gtk_signal_connect_after( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
876 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
879 void wxComboBox::OnSize( wxSizeEvent
&event
)
881 // NB: In some situations (e.g. on non-first page of a wizard, if the
882 // size used is default size), GtkCombo widget is resized correctly,
883 // but it's look is not updated, it's rendered as if it was much wider.
884 // No other widgets are affected, so it looks like a bug in GTK+.
885 // Manually requesting resize calculation (as gtk_pizza_set_size does)
887 if (GTK_WIDGET_VISIBLE(m_widget
))
888 gtk_widget_queue_resize(m_widget
);
893 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
895 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
897 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
898 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
900 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
901 GList
*child
= list
->children
;
904 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
906 GtkBin
*bin
= GTK_BIN(child
->data
);
907 gtk_widget_modify_style( bin
->child
, style
);
913 GtkWidget
* wxComboBox::GetConnectWidget()
915 return GTK_COMBO(m_widget
)->entry
;
918 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
920 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
921 (window
== GTK_COMBO(m_widget
)->button
->window
) );
924 wxSize
wxComboBox::DoGetBestSize() const
926 wxSize
ret( wxControl::DoGetBestSize() );
928 // we know better our horizontal extent: it depends on the longest string
933 size_t count
= GetCount();
934 for ( size_t n
= 0; n
< count
; n
++ )
936 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
942 // empty combobox should have some reasonable default size too
952 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
954 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);
957 // ----------------------------------------------------------------------------
958 // standard event handling
959 // ----------------------------------------------------------------------------
961 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
966 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
971 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
976 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
981 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
986 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
989 GetSelection(& from
, & to
);
990 if (from
!= -1 && to
!= -1)
994 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
996 SetSelection(-1, -1);
999 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
1001 event
.Enable( CanCut() );
1004 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
1006 event
.Enable( CanCopy() );
1009 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
1011 event
.Enable( CanPaste() );
1014 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
1016 event
.Enable( CanUndo() );
1019 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
1021 event
.Enable( CanRedo() );
1024 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
1026 event
.Enable(HasSelection() && IsEditable()) ;
1029 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1031 event
.Enable(GetLastPosition() > 0);