1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "combobox.h"
14 #include "wx/combobox.h"
18 #include "wx/settings.h"
21 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
44 gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
46 if (g_isIdle
) wxapp_install_idle_handler();
48 if (!combo
->m_hasVMT
) return;
50 if (g_blockEventsOnDrag
) return;
52 if (combo
->m_alreadySent
)
54 combo
->m_alreadySent
= FALSE
;
58 combo
->m_alreadySent
= TRUE
;
60 int curSelection
= combo
->GetSelection();
62 if (combo
->m_prevSelection
!= curSelection
)
64 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
65 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
68 combo
->m_prevSelection
= curSelection
;
70 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
71 event
.SetInt( curSelection
);
72 event
.SetString( combo
->GetStringSelection() );
73 event
.SetEventObject( combo
);
75 combo
->GetEventHandler()->ProcessEvent( event
);
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
83 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
85 if (g_isIdle
) wxapp_install_idle_handler();
87 if (!combo
->m_hasVMT
) return;
89 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
90 event
.SetString( combo
->GetValue() );
91 event
.SetEventObject( combo
);
92 combo
->GetEventHandler()->ProcessEvent( event
);
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
, const wxString
& value
,
107 const wxPoint
& pos
, const wxSize
& size
,
108 int n
, const wxString choices
[],
109 long style
, const wxValidator
& validator
,
110 const wxString
& name
)
112 m_alreadySent
= FALSE
;
114 m_acceptsFocus
= TRUE
;
117 if (!PreCreation( parent
, pos
, size
) ||
118 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
120 wxFAIL_MSG( wxT("wxComboBox creation failed") );
124 m_widget
= gtk_combo_new();
126 // make it more useable
127 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
129 // and case-sensitive
130 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
133 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
135 gtk_list_set_selection_mode( GTK_LIST(list
), GTK_SELECTION_MULTIPLE
);
137 for (int i
= 0; i
< n
; i
++)
139 /* don't send first event, which GTK sends aways when
140 inserting the first item */
141 m_alreadySent
= TRUE
;
143 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
].mbc_str() );
145 m_clientDataList
.Append( (wxObject
*)NULL
);
146 m_clientObjectList
.Append( (wxObject
*)NULL
);
148 gtk_container_add( GTK_CONTAINER(list
), list_item
);
150 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
151 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
153 gtk_widget_show( list_item
);
156 m_parent
->DoAddChild( this );
158 m_focusWidget
= GTK_COMBO(m_widget
)->entry
;
162 ConnectWidget( GTK_COMBO(m_widget
)->button
);
164 if (!value
.IsNull()) SetValue( value
);
166 if (style
& wxCB_READONLY
)
167 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget
)->entry
), FALSE
);
169 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
170 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
172 wxSize
size_best( DoGetBestSize() );
173 wxSize
new_size( size
);
174 if (new_size
.x
== -1)
175 new_size
.x
= size_best
.x
;
176 if (new_size
.y
== -1)
177 new_size
.y
= size_best
.y
;
178 if (new_size
.y
> size_best
.y
)
179 new_size
.y
= size_best
.y
;
180 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
182 SetSize( new_size
.x
, new_size
.y
);
184 // This is required for tool bar support
185 gtk_widget_set_usize( m_widget
, new_size
.x
, new_size
.y
);
189 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
190 SetForegroundColour( parent
->GetForegroundColour() );
197 wxComboBox::~wxComboBox()
199 wxNode
*node
= m_clientObjectList
.First();
202 wxClientData
*cd
= (wxClientData
*)node
->Data();
206 m_clientObjectList
.Clear();
208 m_clientDataList
.Clear();
211 void wxComboBox::AppendCommon( const wxString
&item
)
213 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
215 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
217 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
219 gtk_container_add( GTK_CONTAINER(list
), list_item
);
221 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
222 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
224 if (GTK_WIDGET_REALIZED(m_widget
))
226 gtk_widget_realize( list_item
);
227 gtk_widget_realize( GTK_BIN(list_item
)->child
);
229 if (m_widgetStyle
) ApplyWidgetStyle();
232 gtk_widget_show( list_item
);
235 void wxComboBox::Append( const wxString
&item
)
237 m_clientDataList
.Append( (wxObject
*) NULL
);
238 m_clientObjectList
.Append( (wxObject
*) NULL
);
240 AppendCommon( item
);
243 void wxComboBox::Append( const wxString
&item
, void *clientData
)
245 m_clientDataList
.Append( (wxObject
*) clientData
);
246 m_clientObjectList
.Append( (wxObject
*)NULL
);
248 AppendCommon( item
);
251 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
253 m_clientDataList
.Append( (wxObject
*) NULL
);
254 m_clientObjectList
.Append( (wxObject
*) clientData
);
256 AppendCommon( item
);
259 void wxComboBox::SetClientData( int n
, void* clientData
)
261 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
263 wxNode
*node
= m_clientDataList
.Nth( n
);
266 node
->SetData( (wxObject
*) clientData
);
269 void* wxComboBox::GetClientData( int n
)
271 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
273 wxNode
*node
= m_clientDataList
.Nth( n
);
274 if (!node
) return NULL
;
279 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
281 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
283 wxNode
*node
= m_clientObjectList
.Nth( n
);
286 wxClientData
*cd
= (wxClientData
*) node
->Data();
289 node
->SetData( (wxObject
*) clientData
);
292 wxClientData
* wxComboBox::GetClientObject( int n
)
294 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
296 wxNode
*node
= m_clientObjectList
.Nth( n
);
297 if (!node
) return (wxClientData
*) NULL
;
299 return (wxClientData
*) node
->Data();
302 void wxComboBox::Clear()
304 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
306 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
307 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
309 wxNode
*node
= m_clientObjectList
.First();
312 wxClientData
*cd
= (wxClientData
*)node
->Data();
316 m_clientObjectList
.Clear();
318 m_clientDataList
.Clear();
321 void wxComboBox::Delete( int n
)
323 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
325 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
327 GList
*child
= g_list_nth( listbox
->children
, n
);
331 wxFAIL_MSG(wxT("wrong index"));
335 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
336 gtk_list_remove_items( listbox
, list
);
339 wxNode
*node
= m_clientObjectList
.Nth( n
);
342 wxClientData
*cd
= (wxClientData
*)node
->Data();
344 m_clientObjectList
.DeleteNode( node
);
347 node
= m_clientDataList
.Nth( n
);
350 m_clientDataList
.DeleteNode( node
);
354 int wxComboBox::FindString( const wxString
&item
)
356 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
358 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
360 GList
*child
= GTK_LIST(list
)->children
;
364 GtkBin
*bin
= GTK_BIN( child
->data
);
365 GtkLabel
*label
= GTK_LABEL( bin
->child
);
366 if (item
== wxString(label
->label
,*wxConvCurrent
))
375 int wxComboBox::GetSelection() const
377 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
379 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
381 GList
*selection
= GTK_LIST(list
)->selection
;
384 GList
*child
= GTK_LIST(list
)->children
;
388 if (child
->data
== selection
->data
) return count
;
397 wxString
wxComboBox::GetString( int n
) const
399 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
401 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
404 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
407 GtkBin
*bin
= GTK_BIN( child
->data
);
408 GtkLabel
*label
= GTK_LABEL( bin
->child
);
409 str
= wxString(label
->label
,*wxConvCurrent
);
413 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
419 wxString
wxComboBox::GetStringSelection() const
421 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
423 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
425 GList
*selection
= GTK_LIST(list
)->selection
;
428 GtkBin
*bin
= GTK_BIN( selection
->data
);
429 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
433 wxFAIL_MSG( wxT("wxComboBox: no selection") );
438 int wxComboBox::Number() const
440 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
442 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
444 GList
*child
= GTK_LIST(list
)->children
;
446 while (child
) { count
++; child
= child
->next
; }
450 void wxComboBox::SetSelection( int n
)
452 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
456 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
457 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
458 gtk_list_select_item( GTK_LIST(list
), n
);
464 void wxComboBox::SetStringSelection( const wxString
&string
)
466 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
468 int res
= FindString( string
);
469 if (res
== -1) return;
473 wxString
wxComboBox::GetValue() const
475 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
476 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
480 void wxComboBox::SetValue( const wxString
& value
)
482 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
484 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
485 wxString tmp
= wxT("");
486 if (!value
.IsNull()) tmp
= value
;
487 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
490 void wxComboBox::Copy()
492 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
494 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
495 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
496 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
498 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
502 void wxComboBox::Cut()
504 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
506 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
507 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
508 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
510 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
514 void wxComboBox::Paste()
516 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
518 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
519 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
520 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
522 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
526 void wxComboBox::SetInsertionPoint( long pos
)
528 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
530 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
531 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
534 void wxComboBox::SetInsertionPointEnd()
536 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
538 SetInsertionPoint( -1 );
541 long wxComboBox::GetInsertionPoint() const
543 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
544 return (long) GTK_EDITABLE(entry
)->current_pos
;
547 long wxComboBox::GetLastPosition() const
549 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
550 int pos
= GTK_ENTRY(entry
)->text_length
;
554 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
556 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
557 // FIXME: not quite sure how to do this method right in multibyte mode
559 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
560 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
561 if (value
.IsNull()) return;
563 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
566 void wxComboBox::Remove(long from
, long to
)
568 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
570 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
571 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
574 void wxComboBox::SetSelection( long from
, long to
)
576 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
577 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
580 void wxComboBox::SetEditable( bool editable
)
582 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
583 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
586 void wxComboBox::OnChar( wxKeyEvent
&event
)
588 if ( event
.KeyCode() == WXK_RETURN
)
590 wxString value
= GetValue();
594 // make Enter generate "selected" event if there is only one item
595 // in the combobox - without it, it's impossible to select it at
597 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
599 event
.SetString( value
);
600 event
.SetEventObject( this );
601 GetEventHandler()->ProcessEvent( event
);
605 // add the item to the list if it's not there yet
606 if ( FindString(value
) == wxNOT_FOUND
)
609 SetStringSelection(value
);
611 // and generate the selected event for it
612 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
613 event
.SetInt( Number() - 1 );
614 event
.SetString( value
);
615 event
.SetEventObject( this );
616 GetEventHandler()->ProcessEvent( event
);
618 //else: do nothing, this will open the listbox
625 void wxComboBox::DisableEvents()
627 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
628 GList
*child
= list
->children
;
631 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
632 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
638 void wxComboBox::EnableEvents()
640 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
641 GList
*child
= list
->children
;
644 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
645 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
651 void wxComboBox::OnSize( wxSizeEvent
&event
)
657 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
659 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
660 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
664 void wxComboBox::ApplyWidgetStyle()
668 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
669 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
670 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
672 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
673 GList
*child
= list
->children
;
676 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
678 GtkBin
*bin
= GTK_BIN(child
->data
);
679 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
685 GtkWidget
* wxComboBox::GetConnectWidget()
687 return GTK_COMBO(m_widget
)->entry
;
690 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
692 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
693 (window
== GTK_COMBO(m_widget
)->button
->window
) );
696 wxSize
wxComboBox::DoGetBestSize() const
698 wxSize
ret( wxControl::DoGetBestSize() );
700 // we know better our horizontal extent: it depends on the longest string
705 GdkFont
*font
= m_font
.GetInternalFont();
708 size_t count
= Number();
709 for ( size_t n
= 0; n
< count
; n
++ )
711 width
= (wxCoord
)gdk_string_width(font
, GetString(n
).mbc_str());
717 // empty combobox should have some reasonable default size too