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
);
268 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
270 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
271 wxT("can't insert into sorted list"));
273 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
275 int count
= GetCount();
276 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
283 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
285 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
287 GList
*gitem_list
= g_list_alloc ();
288 gitem_list
->data
= list_item
;
289 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
291 if (GTK_WIDGET_REALIZED(m_widget
))
293 gtk_widget_realize( list_item
);
294 gtk_widget_realize( GTK_BIN(list_item
)->child
);
299 gtk_widget_show( list_item
);
303 if ( (int)m_clientDataList
.GetCount() < count
)
304 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
305 if ( (int)m_clientObjectList
.GetCount() < count
)
306 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
313 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
315 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
317 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
320 node
->SetData( (wxObject
*) clientData
);
323 void* wxComboBox::DoGetItemClientData( int n
) const
325 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
327 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
329 return node
? node
->GetData() : NULL
;
332 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
334 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
336 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
339 // wxItemContainer already deletes data for us
341 node
->SetData( (wxObject
*) clientData
);
344 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
346 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
348 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
350 return node
? (wxClientData
*) node
->GetData() : NULL
;
353 void wxComboBox::Clear()
355 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
359 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
360 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
362 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
365 wxClientData
*cd
= (wxClientData
*)node
->GetData();
367 node
= node
->GetNext();
369 m_clientObjectList
.Clear();
371 m_clientDataList
.Clear();
376 void wxComboBox::Delete( int n
)
378 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
380 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
382 GList
*child
= g_list_nth( listbox
->children
, n
);
386 wxFAIL_MSG(wxT("wrong index"));
392 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
393 gtk_list_remove_items( listbox
, list
);
396 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
399 wxClientData
*cd
= (wxClientData
*)node
->GetData();
401 m_clientObjectList
.Erase( node
);
404 node
= m_clientDataList
.Item( n
);
406 m_clientDataList
.Erase( node
);
411 void wxComboBox::SetString(int n
, const wxString
&text
)
413 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
415 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
417 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
420 GtkBin
*bin
= GTK_BIN( child
->data
);
421 GtkLabel
*label
= GTK_LABEL( bin
->child
);
422 gtk_label_set_text(label
, wxGTK_CONV(text
));
426 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
430 int wxComboBox::FindString( const wxString
&item
) const
432 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
434 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
436 GList
*child
= GTK_LIST(list
)->children
;
440 GtkBin
*bin
= GTK_BIN( child
->data
);
441 GtkLabel
*label
= GTK_LABEL( bin
->child
);
443 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
445 wxString
str( label
->label
);
457 int wxComboBox::GetSelection() const
459 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
461 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
463 GList
*selection
= GTK_LIST(list
)->selection
;
466 GList
*child
= GTK_LIST(list
)->children
;
470 if (child
->data
== selection
->data
) return count
;
479 wxString
wxComboBox::GetString( int n
) const
481 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
483 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
486 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
489 GtkBin
*bin
= GTK_BIN( child
->data
);
490 GtkLabel
*label
= GTK_LABEL( bin
->child
);
492 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
494 str
= wxString( label
->label
);
499 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
505 wxString
wxComboBox::GetStringSelection() const
507 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
509 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
511 GList
*selection
= GTK_LIST(list
)->selection
;
514 GtkBin
*bin
= GTK_BIN( selection
->data
);
515 GtkLabel
*label
= GTK_LABEL( bin
->child
);
517 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
519 wxString
tmp( label
->label
);
524 wxFAIL_MSG( wxT("wxComboBox: no selection") );
529 int wxComboBox::GetCount() const
531 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
533 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
535 GList
*child
= GTK_LIST(list
)->children
;
537 while (child
) { count
++; child
= child
->next
; }
541 void wxComboBox::SetSelection( int n
)
543 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
547 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
548 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
549 gtk_list_select_item( GTK_LIST(list
), n
);
555 bool wxComboBox::SetStringSelection( const wxString
&string
)
557 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid combobox") );
559 int res
= FindString( string
);
560 if (res
== -1) return false;
565 wxString
wxComboBox::GetValue() const
567 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
568 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
571 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
574 printf( "%d ", (int) (c
) );
582 void wxComboBox::SetValue( const wxString
& value
)
584 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
586 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
587 wxString tmp
= wxT("");
588 if (!value
.IsNull()) tmp
= value
;
589 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
592 void wxComboBox::Copy()
594 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
596 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
597 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
600 void wxComboBox::Cut()
602 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
604 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
605 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
608 void wxComboBox::Paste()
610 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
612 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
613 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
616 void wxComboBox::SetInsertionPoint( long pos
)
618 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
620 if ( pos
== GetLastPosition() )
623 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
624 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
627 long wxComboBox::GetInsertionPoint() const
629 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
632 long wxComboBox::GetLastPosition() const
634 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
635 int pos
= GTK_ENTRY(entry
)->text_length
;
639 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
641 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
643 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
644 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
645 if (value
.IsNull()) return;
649 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
650 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
652 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
656 void wxComboBox::SetSelection( long from
, long to
)
658 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
659 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
662 void wxComboBox::SetEditable( bool editable
)
664 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
665 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
668 void wxComboBox::OnChar( wxKeyEvent
&event
)
670 if ( event
.GetKeyCode() == WXK_RETURN
)
672 // GTK automatically selects an item if its in the list
673 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, GetId());
674 event
.SetString( GetValue() );
675 event
.SetInt( GetSelection() );
676 event
.SetEventObject( this );
678 if (!GetEventHandler()->ProcessEvent( event
))
680 // This will invoke the dialog default action, such
681 // as the clicking the default button.
683 wxWindow
*top_frame
= m_parent
;
684 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
685 top_frame
= top_frame
->GetParent();
687 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
689 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
691 if (window
->default_widget
)
692 gtk_widget_activate (window
->default_widget
);
696 // Catch GTK event so that GTK doesn't open the drop
697 // down list upon RETURN.
704 void wxComboBox::DisableEvents()
706 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
707 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
708 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
709 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
712 void wxComboBox::EnableEvents()
714 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
715 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
716 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
717 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
720 void wxComboBox::OnSize( wxSizeEvent
&event
)
726 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
728 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
729 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
733 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
735 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
736 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
737 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
739 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
740 GList
*child
= list
->children
;
743 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
745 GtkBin
*bin
= GTK_BIN(child
->data
);
746 gtk_widget_modify_style( bin
->child
, style
);
752 GtkWidget
* wxComboBox::GetConnectWidget()
754 return GTK_COMBO(m_widget
)->entry
;
757 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
759 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
760 (window
== GTK_COMBO(m_widget
)->button
->window
) );
763 wxSize
wxComboBox::DoGetBestSize() const
765 wxSize
ret( wxControl::DoGetBestSize() );
767 // we know better our horizontal extent: it depends on the longest string
773 size_t count
= GetCount();
774 for ( size_t n
= 0; n
< count
; n
++ )
776 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
782 // empty combobox should have some reasonable default size too
792 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
794 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);