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
)
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
);
245 gtk_widget_show( list_item
);
247 const int count
= GetCount();
249 if ( (int)m_clientDataList
.GetCount() < count
)
250 m_clientDataList
.Append( (wxObject
*) NULL
);
251 if ( (int)m_clientObjectList
.GetCount() < count
)
252 m_clientObjectList
.Append( (wxObject
*) NULL
);
259 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
261 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
262 wxT("can't insert into sorted list"));
264 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
266 int count
= GetCount();
267 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
274 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
276 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
278 GList
*gitem_list
= g_list_alloc ();
279 gitem_list
->data
= list_item
;
280 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
282 if (GTK_WIDGET_REALIZED(m_widget
))
284 gtk_widget_realize( list_item
);
285 gtk_widget_realize( GTK_BIN(list_item
)->child
);
290 gtk_widget_show( list_item
);
294 if ( (int)m_clientDataList
.GetCount() < count
)
295 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
296 if ( (int)m_clientObjectList
.GetCount() < count
)
297 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
304 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
306 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
308 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
311 node
->SetData( (wxObject
*) clientData
);
314 void* wxComboBox::DoGetItemClientData( int n
) const
316 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
318 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
320 return node
? node
->GetData() : NULL
;
323 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
325 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
327 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
330 // wxItemContainer already deletes data for us
332 node
->SetData( (wxObject
*) clientData
);
335 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
337 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
339 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
341 return node
? (wxClientData
*) node
->GetData() : NULL
;
344 void wxComboBox::Clear()
346 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
350 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
351 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
353 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
356 wxClientData
*cd
= (wxClientData
*)node
->GetData();
358 node
= node
->GetNext();
360 m_clientObjectList
.Clear();
362 m_clientDataList
.Clear();
367 void wxComboBox::Delete( int n
)
369 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
371 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
373 GList
*child
= g_list_nth( listbox
->children
, n
);
377 wxFAIL_MSG(wxT("wrong index"));
383 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
384 gtk_list_remove_items( listbox
, list
);
387 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
390 wxClientData
*cd
= (wxClientData
*)node
->GetData();
392 m_clientObjectList
.Erase( node
);
395 node
= m_clientDataList
.Item( n
);
397 m_clientDataList
.Erase( node
);
402 void wxComboBox::SetString(int n
, const wxString
&text
)
404 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
406 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
408 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
411 GtkBin
*bin
= GTK_BIN( child
->data
);
412 GtkLabel
*label
= GTK_LABEL( bin
->child
);
413 gtk_label_set_text(label
, wxGTK_CONV(text
));
417 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
421 int wxComboBox::FindString( const wxString
&item
) const
423 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
425 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
427 GList
*child
= GTK_LIST(list
)->children
;
431 GtkBin
*bin
= GTK_BIN( child
->data
);
432 GtkLabel
*label
= GTK_LABEL( bin
->child
);
434 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
436 wxString
str( label
->label
);
448 int wxComboBox::GetSelection() const
450 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
452 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
454 GList
*selection
= GTK_LIST(list
)->selection
;
457 GList
*child
= GTK_LIST(list
)->children
;
461 if (child
->data
== selection
->data
) return count
;
470 wxString
wxComboBox::GetString( int n
) const
472 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
474 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
477 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
480 GtkBin
*bin
= GTK_BIN( child
->data
);
481 GtkLabel
*label
= GTK_LABEL( bin
->child
);
483 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
485 str
= wxString( label
->label
);
490 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
496 wxString
wxComboBox::GetStringSelection() const
498 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
500 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
502 GList
*selection
= GTK_LIST(list
)->selection
;
505 GtkBin
*bin
= GTK_BIN( selection
->data
);
506 GtkLabel
*label
= GTK_LABEL( bin
->child
);
508 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
510 wxString
tmp( label
->label
);
515 wxFAIL_MSG( wxT("wxComboBox: no selection") );
520 int wxComboBox::GetCount() const
522 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
524 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
526 GList
*child
= GTK_LIST(list
)->children
;
528 while (child
) { count
++; child
= child
->next
; }
532 void wxComboBox::SetSelection( int n
)
534 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
538 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
539 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
540 gtk_list_select_item( GTK_LIST(list
), n
);
546 bool wxComboBox::SetStringSelection( const wxString
&string
)
548 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid combobox") );
550 int res
= FindString( string
);
551 if (res
== -1) return false;
556 wxString
wxComboBox::GetValue() const
558 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
559 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
562 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
565 printf( "%d ", (int) (c
) );
573 void wxComboBox::SetValue( const wxString
& value
)
575 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
577 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
578 wxString tmp
= wxT("");
579 if (!value
.IsNull()) tmp
= value
;
580 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
583 void wxComboBox::Copy()
585 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
587 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
588 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
591 void wxComboBox::Cut()
593 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
595 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
596 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
599 void wxComboBox::Paste()
601 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
603 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
604 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
607 void wxComboBox::SetInsertionPoint( long pos
)
609 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
611 if ( pos
== GetLastPosition() )
614 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
615 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
618 long wxComboBox::GetInsertionPoint() const
620 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
623 long wxComboBox::GetLastPosition() const
625 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
626 int pos
= GTK_ENTRY(entry
)->text_length
;
630 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
632 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
634 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
635 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
636 if (value
.IsNull()) return;
640 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
641 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
643 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
647 void wxComboBox::SetSelection( long from
, long to
)
649 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
650 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
653 void wxComboBox::SetEditable( bool editable
)
655 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
656 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
659 void wxComboBox::OnChar( wxKeyEvent
&event
)
661 if ( event
.GetKeyCode() == WXK_RETURN
)
663 // GTK automatically selects an item if its in the list
664 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, GetId());
665 event
.SetString( GetValue() );
666 event
.SetInt( GetSelection() );
667 event
.SetEventObject( this );
669 if (!GetEventHandler()->ProcessEvent( event
))
671 // This will invoke the dialog default action, such
672 // as the clicking the default button.
674 wxWindow
*top_frame
= m_parent
;
675 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
676 top_frame
= top_frame
->GetParent();
678 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
680 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
682 if (window
->default_widget
)
683 gtk_widget_activate (window
->default_widget
);
687 // Catch GTK event so that GTK doesn't open the drop
688 // down list upon RETURN.
695 void wxComboBox::DisableEvents()
697 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
698 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
699 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
700 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
703 void wxComboBox::EnableEvents()
705 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
706 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
707 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
708 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
711 void wxComboBox::OnSize( wxSizeEvent
&event
)
717 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
719 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
720 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
724 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
726 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
727 gtk_widget_modify_style( GTK_COMBO(m_widget
)->entry
, style
);
728 gtk_widget_modify_style( GTK_COMBO(m_widget
)->list
, style
);
730 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
731 GList
*child
= list
->children
;
734 gtk_widget_modify_style( GTK_WIDGET(child
->data
), style
);
736 GtkBin
*bin
= GTK_BIN(child
->data
);
737 gtk_widget_modify_style( bin
->child
, style
);
743 GtkWidget
* wxComboBox::GetConnectWidget()
745 return GTK_COMBO(m_widget
)->entry
;
748 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
750 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
751 (window
== GTK_COMBO(m_widget
)->button
->window
) );
754 wxSize
wxComboBox::DoGetBestSize() const
756 wxSize
ret( wxControl::DoGetBestSize() );
758 // we know better our horizontal extent: it depends on the longest string
764 size_t count
= GetCount();
765 for ( size_t n
= 0; n
< count
; n
++ )
767 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
);
773 // empty combobox should have some reasonable default size too
781 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
783 return GetDefaultAttributesFromGTKWidget(gtk_combo_new
, true);