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
;
42 //-----------------------------------------------------------------------------
43 // "select-child" - click/cursor get select-child, changed, select-child
44 //-----------------------------------------------------------------------------
47 gtk_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
49 if (g_isIdle
) wxapp_install_idle_handler();
51 if (!combo
->m_hasVMT
) return;
53 if (g_blockEventsOnDrag
) return;
55 int curSelection
= combo
->GetSelection();
57 if (combo
->m_prevSelection
== curSelection
) return;
59 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
60 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
62 combo
->m_prevSelection
= curSelection
;
64 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
65 event
.SetInt( curSelection
);
66 event
.SetString( combo
->GetStringSelection() );
67 event
.SetEventObject( combo
);
69 combo
->GetEventHandler()->ProcessEvent( event
);
72 //-----------------------------------------------------------------------------
73 // "changed" - typing and list item matches get changed, select-child
74 // if it doesn't match an item then just get a single changed
75 //-----------------------------------------------------------------------------
78 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
80 if (g_isIdle
) wxapp_install_idle_handler();
82 if (!combo
->m_hasVMT
) return;
84 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
85 event
.SetString( combo
->GetValue() );
86 event
.SetEventObject( combo
);
87 combo
->GetEventHandler()->ProcessEvent( event
);
91 gtk_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
99 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
101 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
102 EVT_SIZE(wxComboBox::OnSize
)
103 EVT_CHAR(wxComboBox::OnChar
)
106 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
107 const wxString
& value
,
108 const wxPoint
& pos
, const wxSize
& size
,
109 const wxArrayString
& choices
,
110 long style
, const wxValidator
& validator
,
111 const wxString
& name
)
113 wxCArrayString
chs(choices
);
115 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
116 chs
.GetStrings(), style
, validator
, name
);
119 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
120 const wxPoint
& pos
, const wxSize
& size
,
121 int n
, const wxString choices
[],
122 long style
, const wxValidator
& validator
,
123 const wxString
& name
)
125 m_alreadySent
= FALSE
;
127 m_acceptsFocus
= TRUE
;
130 if (!PreCreation( parent
, pos
, size
) ||
131 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
133 wxFAIL_MSG( wxT("wxComboBox creation failed") );
137 m_widget
= gtk_combo_new();
138 GtkCombo
*combo
= GTK_COMBO(m_widget
);
140 // Disable GTK's broken events ...
141 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
142 // ... and add surogate handler.
143 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
144 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
146 // make it more useable
147 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
149 // and case-sensitive
150 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
152 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
155 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
158 for (int i
= 0; i
< n
; i
++)
160 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
162 m_clientDataList
.Append( (wxObject
*)NULL
);
163 m_clientObjectList
.Append( (wxObject
*)NULL
);
165 gtk_container_add( GTK_CONTAINER(list
), list_item
);
167 gtk_widget_show( list_item
);
170 m_parent
->DoAddChild( this );
172 m_focusWidget
= combo
->entry
;
176 ConnectWidget( combo
->button
);
178 // MSW's combo box shows the value and the selection is -1
179 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
180 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
182 if (style
& wxCB_READONLY
)
183 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
185 gtk_signal_connect( GTK_OBJECT(combo
->entry
), "changed",
186 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
188 gtk_signal_connect( GTK_OBJECT(combo
->list
), "select-child",
189 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
191 SetBestSize(size
); // need this too because this is a wxControlWithItems
193 // This is required for tool bar support
194 wxSize setsize
= GetSize();
195 gtk_widget_set_usize( m_widget
, setsize
.x
, setsize
.y
);
200 wxComboBox::~wxComboBox()
202 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
205 wxClientData
*cd
= (wxClientData
*)node
->GetData();
207 node
= node
->GetNext();
209 m_clientObjectList
.Clear();
211 m_clientDataList
.Clear();
214 void wxComboBox::SetFocus()
218 // don't do anything if we already have focus
222 gtk_widget_grab_focus( m_focusWidget
);
225 int wxComboBox::DoAppend( const wxString
&item
)
227 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
231 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
233 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
235 gtk_container_add( GTK_CONTAINER(list
), list_item
);
237 if (GTK_WIDGET_REALIZED(m_widget
))
239 gtk_widget_realize( list_item
);
240 gtk_widget_realize( GTK_BIN(list_item
)->child
);
243 // Apply current widget style to the new list_item
244 GtkRcStyle
*style
= CreateWidgetStyle();
247 gtk_widget_modify_style( GTK_WIDGET( list_item
), style
);
248 GtkBin
*bin
= GTK_BIN( list_item
);
249 GtkWidget
*label
= GTK_WIDGET( bin
->child
);
250 gtk_widget_modify_style( label
, style
);
251 gtk_rc_style_unref( style
);
254 gtk_widget_show( list_item
);
256 const int count
= GetCount();
258 if ( (int)m_clientDataList
.GetCount() < count
)
259 m_clientDataList
.Append( (wxObject
*) NULL
);
260 if ( (int)m_clientObjectList
.GetCount() < count
)
261 m_clientObjectList
.Append( (wxObject
*) NULL
);
265 InvalidateBestSize();
270 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
272 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
273 wxT("can't insert into sorted list"));
275 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
277 int count
= GetCount();
278 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
285 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
287 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
289 GList
*gitem_list
= g_list_alloc ();
290 gitem_list
->data
= list_item
;
291 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
293 if (GTK_WIDGET_REALIZED(m_widget
))
295 gtk_widget_realize( list_item
);
296 gtk_widget_realize( GTK_BIN(list_item
)->child
);
301 gtk_widget_show( list_item
);
305 if ( (int)m_clientDataList
.GetCount() < count
)
306 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
307 if ( (int)m_clientObjectList
.GetCount() < count
)
308 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
312 InvalidateBestSize();
317 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
319 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
321 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
324 node
->SetData( (wxObject
*) clientData
);
327 void* wxComboBox::DoGetItemClientData( int n
) const
329 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
331 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
333 return node
? node
->GetData() : NULL
;
336 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
338 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
340 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
343 // wxItemContainer already deletes data for us
345 node
->SetData( (wxObject
*) clientData
);
348 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
350 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
352 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
354 return node
? (wxClientData
*) node
->GetData() : NULL
;
357 void wxComboBox::Clear()
359 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
363 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
364 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
366 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
369 wxClientData
*cd
= (wxClientData
*)node
->GetData();
371 node
= node
->GetNext();
373 m_clientObjectList
.Clear();
375 m_clientDataList
.Clear();
379 InvalidateBestSize();
382 void wxComboBox::Delete( int n
)
384 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
386 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
388 GList
*child
= g_list_nth( listbox
->children
, n
);
392 wxFAIL_MSG(wxT("wrong index"));
398 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
399 gtk_list_remove_items( listbox
, list
);
402 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
405 wxClientData
*cd
= (wxClientData
*)node
->GetData();
407 m_clientObjectList
.Erase( node
);
410 node
= m_clientDataList
.Item( n
);
412 m_clientDataList
.Erase( node
);
416 InvalidateBestSize();
419 void wxComboBox::SetString(int n
, const wxString
&text
)
421 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
423 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
425 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
428 GtkBin
*bin
= GTK_BIN( child
->data
);
429 GtkLabel
*label
= GTK_LABEL( bin
->child
);
430 gtk_label_set_text(label
, wxGTK_CONV(text
));
434 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
437 InvalidateBestSize();
440 int wxComboBox::FindString( const wxString
&item
) const
442 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
444 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
446 GList
*child
= GTK_LIST(list
)->children
;
450 GtkBin
*bin
= GTK_BIN( child
->data
);
451 GtkLabel
*label
= GTK_LABEL( bin
->child
);
453 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
455 wxString
str( label
->label
);
467 int wxComboBox::GetSelection() const
469 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
471 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
473 GList
*selection
= GTK_LIST(list
)->selection
;
476 GList
*child
= GTK_LIST(list
)->children
;
480 if (child
->data
== selection
->data
) return count
;
489 wxString
wxComboBox::GetString( int n
) const
491 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
493 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
496 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
499 GtkBin
*bin
= GTK_BIN( child
->data
);
500 GtkLabel
*label
= GTK_LABEL( bin
->child
);
502 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
504 str
= wxString( label
->label
);
509 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
515 wxString
wxComboBox::GetStringSelection() const
517 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
519 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
521 GList
*selection
= GTK_LIST(list
)->selection
;
524 GtkBin
*bin
= GTK_BIN( selection
->data
);
525 GtkLabel
*label
= GTK_LABEL( bin
->child
);
527 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
529 wxString
tmp( label
->label
);
534 wxFAIL_MSG( wxT("wxComboBox: no selection") );
539 int wxComboBox::GetCount() const
541 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
543 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
545 GList
*child
= GTK_LIST(list
)->children
;
547 while (child
) { count
++; child
= child
->next
; }
551 void wxComboBox::SetSelection( int n
)
553 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
557 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
558 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
559 gtk_list_select_item( GTK_LIST(list
), n
);
565 bool wxComboBox::SetStringSelection( const wxString
&string
)
567 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid combobox") );
569 int res
= FindString( string
);
570 if (res
== -1) return false;
575 wxString
wxComboBox::GetValue() const
577 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
578 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
581 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
584 printf( "%d ", (int) (c
) );
592 void wxComboBox::SetValue( const wxString
& value
)
594 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
596 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
597 wxString tmp
= wxT("");
598 if (!value
.IsNull()) tmp
= value
;
599 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
601 InvalidateBestSize();
604 void wxComboBox::Copy()
606 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
608 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
609 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
612 void wxComboBox::Cut()
614 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
616 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
617 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
620 void wxComboBox::Paste()
622 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
624 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
625 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
628 void wxComboBox::SetInsertionPoint( long pos
)
630 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
632 if ( pos
== GetLastPosition() )
635 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
636 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
639 long wxComboBox::GetInsertionPoint() const
641 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
644 long wxComboBox::GetLastPosition() const
646 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
647 int pos
= GTK_ENTRY(entry
)->text_length
;
651 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
653 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
655 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
656 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
657 if (value
.IsNull()) return;
661 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
662 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
664 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
668 void wxComboBox::SetSelection( long from
, long to
)
670 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
671 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
674 void wxComboBox::SetEditable( bool editable
)
676 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
677 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
680 void wxComboBox::OnChar( wxKeyEvent
&event
)
682 if ( event
.GetKeyCode() == WXK_RETURN
)
684 // GTK automatically selects an item if its in the list
685 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, GetId());
686 event
.SetString( GetValue() );
687 event
.SetInt( GetSelection() );
688 event
.SetEventObject( this );
690 if (!GetEventHandler()->ProcessEvent( event
))
692 // This will invoke the dialog default action, such
693 // as the clicking the default button.
695 wxWindow
*top_frame
= m_parent
;
696 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
697 top_frame
= top_frame
->GetParent();
699 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
701 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
703 if (window
->default_widget
)
704 gtk_widget_activate (window
->default_widget
);
708 // Catch GTK event so that GTK doesn't open the drop
709 // down list upon RETURN.
716 void wxComboBox::DisableEvents()
718 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
719 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
720 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
721 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
724 void wxComboBox::EnableEvents()
726 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
727 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
728 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
729 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
732 void wxComboBox::OnSize( wxSizeEvent
&event
)
734 // NB: In some situations (e.g. on non-first page of a wizard, if the
735 // size used is default size), GtkCombo widget is resized correctly,
736 // but it's look is not updated, it's rendered as if it was much wider.
737 // No other widgets are affected, so it looks like a bug in GTK+.
738 // Manually requesting resize calculation (as gtk_pizza_set_size does)
740 if (GTK_WIDGET_VISIBLE(m_widget
))
741 gtk_widget_queue_resize(m_widget
);
746 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
748 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
750 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
751 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
753 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
754 GList
*child
= list
->children
;
757 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
759 GtkBin
*bin
= GTK_BIN(child
->data
);
760 gtk_widget_modify_style( bin
->child
, style
);
766 GtkWidget
* wxComboBox::GetConnectWidget()
768 return GTK_COMBO(m_widget
)->entry
;
771 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
773 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
774 (window
== GTK_COMBO(m_widget
)->button
->window
) );
777 wxSize
wxComboBox::DoGetBestSize() const
779 wxSize
ret( wxControl::DoGetBestSize() );
781 // we know better our horizontal extent: it depends on the longest string
786 size_t count
= GetCount();
787 for ( size_t n
= 0; n
< count
; n
++ )
789 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
795 // empty combobox should have some reasonable default size too
805 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
807 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);