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"
24 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
26 #include "wx/gtk/private.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern void wxapp_install_idle_handler();
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern bool g_blockEventsOnDrag
;
41 //-----------------------------------------------------------------------------
42 // "select-child" - click/cursor get select-child, changed, select-child
43 //-----------------------------------------------------------------------------
46 gtk_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
48 if (g_isIdle
) wxapp_install_idle_handler();
50 if (!combo
->m_hasVMT
) return;
52 if (g_blockEventsOnDrag
) return;
54 int curSelection
= combo
->GetSelection();
56 if (combo
->m_prevSelection
!= curSelection
)
58 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
59 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
61 combo
->m_prevSelection
= curSelection
;
63 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
64 event
.SetInt( curSelection
);
65 event
.SetString( combo
->GetStringSelection() );
66 event
.SetEventObject( combo
);
68 combo
->GetEventHandler()->ProcessEvent( event
);
71 //-----------------------------------------------------------------------------
72 // "changed" - typing and list item matches get changed, select-child
73 // if it doesn't match an item then just get a single changed
74 //-----------------------------------------------------------------------------
77 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
79 if (g_isIdle
) wxapp_install_idle_handler();
81 if (!combo
->m_hasVMT
) return;
83 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
84 event
.SetString( combo
->GetValue() );
85 event
.SetEventObject( combo
);
86 combo
->GetEventHandler()->ProcessEvent( event
);
90 gtk_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
94 //-----------------------------------------------------------------------------
96 //-----------------------------------------------------------------------------
98 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
100 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
101 EVT_SIZE(wxComboBox::OnSize
)
102 EVT_CHAR(wxComboBox::OnChar
)
105 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
106 const wxPoint
& pos
, const wxSize
& size
,
107 int n
, const wxString choices
[],
108 long style
, const wxValidator
& validator
,
109 const wxString
& name
)
111 m_alreadySent
= FALSE
;
113 m_acceptsFocus
= TRUE
;
116 if (!PreCreation( parent
, pos
, size
) ||
117 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
119 wxFAIL_MSG( wxT("wxComboBox creation failed") );
123 m_widget
= gtk_combo_new();
124 GtkCombo
*combo
= GTK_COMBO(m_widget
);
126 // Disable GTK's broken events ...
127 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
128 // ... and add surogate handler.
129 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
130 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
132 // make it more useable
133 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
135 // and case-sensitive
136 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
138 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
141 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
144 for (int i
= 0; i
< n
; i
++)
146 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
148 m_clientDataList
.Append( (wxObject
*)NULL
);
149 m_clientObjectList
.Append( (wxObject
*)NULL
);
151 gtk_container_add( GTK_CONTAINER(list
), list_item
);
153 gtk_widget_show( list_item
);
156 m_parent
->DoAddChild( this );
158 m_focusWidget
= combo
->entry
;
162 ConnectWidget( combo
->button
);
164 // MSW's combo box shows the value and the selection is -1
165 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
166 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
168 if (style
& wxCB_READONLY
)
169 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
171 gtk_signal_connect( GTK_OBJECT(combo
->entry
), "changed",
172 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
174 gtk_signal_connect( GTK_OBJECT(combo
->list
), "select-child",
175 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
177 wxSize
size_best( DoGetBestSize() );
178 wxSize
new_size( size
);
179 if (new_size
.x
== -1)
180 new_size
.x
= size_best
.x
;
181 if (new_size
.y
== -1)
182 new_size
.y
= size_best
.y
;
183 if (new_size
.y
> size_best
.y
)
184 new_size
.y
= size_best
.y
;
185 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
187 SetSize( new_size
.x
, new_size
.y
);
189 // This is required for tool bar support
190 gtk_widget_set_usize( m_widget
, new_size
.x
, new_size
.y
);
193 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
) );
194 SetForegroundColour( parent
->GetForegroundColour() );
201 wxComboBox::~wxComboBox()
203 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
206 wxClientData
*cd
= (wxClientData
*)node
->GetData();
208 node
= node
->GetNext();
210 m_clientObjectList
.Clear();
212 m_clientDataList
.Clear();
215 void wxComboBox::SetFocus()
219 // don't do anything if we already have focus
223 gtk_widget_grab_focus( m_focusWidget
);
226 int wxComboBox::DoAppend( const wxString
&item
)
228 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
232 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
234 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
236 gtk_container_add( GTK_CONTAINER(list
), list_item
);
238 if (GTK_WIDGET_REALIZED(m_widget
))
240 gtk_widget_realize( list_item
);
241 gtk_widget_realize( GTK_BIN(list_item
)->child
);
243 if (m_widgetStyle
) ApplyWidgetStyle();
246 gtk_widget_show( list_item
);
248 const int count
= GetCount();
250 if ( (int)m_clientDataList
.GetCount() < count
)
251 m_clientDataList
.Append( (wxObject
*) NULL
);
252 if ( (int)m_clientObjectList
.GetCount() < count
)
253 m_clientObjectList
.Append( (wxObject
*) NULL
);
260 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
262 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
263 wxT("can't insert into sorted list"));
265 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
267 int count
= GetCount();
268 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
275 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
277 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
279 GList
*gitem_list
= g_list_alloc ();
280 gitem_list
->data
= list_item
;
281 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
283 if (GTK_WIDGET_REALIZED(m_widget
))
285 gtk_widget_realize( list_item
);
286 gtk_widget_realize( GTK_BIN(list_item
)->child
);
292 gtk_widget_show( list_item
);
296 if ( (int)m_clientDataList
.GetCount() < count
)
297 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
298 if ( (int)m_clientObjectList
.GetCount() < count
)
299 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
306 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
308 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
310 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
313 node
->SetData( (wxObject
*) clientData
);
316 void* wxComboBox::DoGetItemClientData( int n
) const
318 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
320 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
322 return node
? node
->GetData() : NULL
;
325 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
327 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
329 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
332 wxClientData
*cd
= (wxClientData
*) node
->GetData();
335 node
->SetData( (wxObject
*) clientData
);
338 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
340 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
342 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
344 return node
? (wxClientData
*) node
->GetData() : NULL
;
347 void wxComboBox::Clear()
349 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
353 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
354 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
356 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
359 wxClientData
*cd
= (wxClientData
*)node
->GetData();
361 node
= node
->GetNext();
363 m_clientObjectList
.Clear();
365 m_clientDataList
.Clear();
370 void wxComboBox::Delete( int n
)
372 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
374 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
376 GList
*child
= g_list_nth( listbox
->children
, n
);
380 wxFAIL_MSG(wxT("wrong index"));
386 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
387 gtk_list_remove_items( listbox
, list
);
390 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
393 wxClientData
*cd
= (wxClientData
*)node
->GetData();
395 m_clientObjectList
.Erase( node
);
398 node
= m_clientDataList
.Item( n
);
400 m_clientDataList
.Erase( node
);
405 void wxComboBox::SetString(int n
, const wxString
&text
)
407 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
409 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
411 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
414 GtkBin
*bin
= GTK_BIN( child
->data
);
415 GtkLabel
*label
= GTK_LABEL( bin
->child
);
416 gtk_label_set_text(label
, wxGTK_CONV(text
));
420 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
424 int wxComboBox::FindString( const wxString
&item
) const
426 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
428 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
430 GList
*child
= GTK_LIST(list
)->children
;
434 GtkBin
*bin
= GTK_BIN( child
->data
);
435 GtkLabel
*label
= GTK_LABEL( bin
->child
);
437 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
439 wxString
str( label
->label
);
451 int wxComboBox::GetSelection() const
453 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
455 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
457 GList
*selection
= GTK_LIST(list
)->selection
;
460 GList
*child
= GTK_LIST(list
)->children
;
464 if (child
->data
== selection
->data
) return count
;
473 wxString
wxComboBox::GetString( int n
) const
475 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
477 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
480 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
483 GtkBin
*bin
= GTK_BIN( child
->data
);
484 GtkLabel
*label
= GTK_LABEL( bin
->child
);
486 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
488 str
= wxString( label
->label
);
493 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
499 wxString
wxComboBox::GetStringSelection() const
501 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
503 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
505 GList
*selection
= GTK_LIST(list
)->selection
;
508 GtkBin
*bin
= GTK_BIN( selection
->data
);
509 GtkLabel
*label
= GTK_LABEL( bin
->child
);
511 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
513 wxString
tmp( label
->label
);
518 wxFAIL_MSG( wxT("wxComboBox: no selection") );
523 int wxComboBox::GetCount() const
525 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
527 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
529 GList
*child
= GTK_LIST(list
)->children
;
531 while (child
) { count
++; child
= child
->next
; }
535 void wxComboBox::SetSelection( int n
)
537 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
541 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
542 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
543 gtk_list_select_item( GTK_LIST(list
), n
);
549 void wxComboBox::SetStringSelection( const wxString
&string
)
551 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
553 int res
= FindString( string
);
554 if (res
== -1) return;
558 wxString
wxComboBox::GetValue() const
560 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
561 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
564 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
567 printf( "%d ", (int) (c
) );
575 void wxComboBox::SetValue( const wxString
& value
)
577 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
579 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
580 wxString tmp
= wxT("");
581 if (!value
.IsNull()) tmp
= value
;
582 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
585 void wxComboBox::Copy()
587 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
589 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
590 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
593 void wxComboBox::Cut()
595 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
597 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
598 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
601 void wxComboBox::Paste()
603 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
605 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
606 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
609 void wxComboBox::SetInsertionPoint( long pos
)
611 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
613 if ( pos
== GetLastPosition() )
616 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
617 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
620 long wxComboBox::GetInsertionPoint() const
622 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
625 long wxComboBox::GetLastPosition() const
627 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
628 int pos
= GTK_ENTRY(entry
)->text_length
;
632 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
634 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
636 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
637 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
638 if (value
.IsNull()) return;
642 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
643 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
645 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
649 void wxComboBox::SetSelection( long from
, long to
)
651 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
652 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
655 void wxComboBox::SetEditable( bool editable
)
657 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
658 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
661 void wxComboBox::OnChar( wxKeyEvent
&event
)
663 if ( event
.GetKeyCode() == WXK_RETURN
)
665 // GTK automatically selects an item if its in the list
666 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, GetId());
667 event
.SetString( GetValue() );
668 event
.SetInt( GetSelection() );
669 event
.SetEventObject( this );
671 if (!GetEventHandler()->ProcessEvent( event
))
673 // This will invoke the dialog default action, such
674 // as the clicking the default button.
676 wxWindow
*top_frame
= m_parent
;
677 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
678 top_frame
= top_frame
->GetParent();
680 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
682 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
684 if (window
->default_widget
)
685 gtk_widget_activate (window
->default_widget
);
689 // Catch GTK event so that GTK doesn't open the drop
690 // down list upon RETURN.
697 void wxComboBox::DisableEvents()
699 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
700 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
701 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
702 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
705 void wxComboBox::EnableEvents()
707 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
708 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
709 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
710 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
713 void wxComboBox::OnSize( wxSizeEvent
&event
)
719 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
721 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
722 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
726 void wxComboBox::ApplyWidgetStyle()
730 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
731 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
732 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
734 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
735 GList
*child
= list
->children
;
738 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
740 GtkBin
*bin
= GTK_BIN(child
->data
);
741 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
747 GtkWidget
* wxComboBox::GetConnectWidget()
749 return GTK_COMBO(m_widget
)->entry
;
752 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
754 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
755 (window
== GTK_COMBO(m_widget
)->button
->window
) );
758 wxSize
wxComboBox::DoGetBestSize() const
760 wxSize
ret( wxControl::DoGetBestSize() );
762 // we know better our horizontal extent: it depends on the longest string
768 size_t count
= GetCount();
769 for ( size_t n
= 0; n
< count
; n
++ )
771 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
, &m_font
);
777 // empty combobox should have some reasonable default size too