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"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
42 gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
44 if (g_isIdle
) wxapp_install_idle_handler();
46 if (!combo
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 if (combo
->m_alreadySent
)
52 combo
->m_alreadySent
= FALSE
;
56 combo
->m_alreadySent
= TRUE
;
58 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
59 event
.SetInt( combo
->GetSelection() );
60 event
.SetString( combo
->GetStringSelection() );
61 event
.SetEventObject( combo
);
63 combo
->GetEventHandler()->ProcessEvent( event
);
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
71 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
73 if (g_isIdle
) wxapp_install_idle_handler();
75 if (!combo
->m_hasVMT
) return;
77 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
78 event
.SetString( combo
->GetValue() );
79 event
.SetEventObject( combo
);
80 combo
->GetEventHandler()->ProcessEvent( event
);
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
87 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
89 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
90 EVT_SIZE(wxComboBox::OnSize
)
91 EVT_CHAR(wxComboBox::OnChar
)
94 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
95 const wxPoint
& pos
, const wxSize
& size
,
96 int n
, const wxString choices
[],
97 long style
, const wxValidator
& validator
,
98 const wxString
& name
)
100 m_alreadySent
= FALSE
;
102 m_acceptsFocus
= TRUE
;
104 if (!PreCreation( parent
, pos
, size
) ||
105 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
107 wxFAIL_MSG( wxT("wxComboBox creation failed") );
111 m_widget
= gtk_combo_new();
113 // make it more useable
114 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
116 // and case-sensitive
117 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
120 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
122 for (int i
= 0; i
< n
; i
++)
124 /* don't send first event, which GTK sends aways when
125 inserting the first item */
126 m_alreadySent
= TRUE
;
128 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
].mbc_str() );
130 m_clientDataList
.Append( (wxObject
*)NULL
);
131 m_clientObjectList
.Append( (wxObject
*)NULL
);
133 gtk_container_add( GTK_CONTAINER(list
), list_item
);
135 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
136 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
138 gtk_widget_show( list_item
);
141 m_parent
->DoAddChild( this );
145 ConnectWidget( GTK_COMBO(m_widget
)->button
);
147 if (!value
.IsNull()) SetValue( value
);
149 if (style
& wxCB_READONLY
)
150 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget
)->entry
), FALSE
);
152 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
153 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
155 wxSize
size_best( DoGetBestSize() );
156 wxSize
new_size( size
);
157 if (new_size
.x
== -1)
158 new_size
.x
= size_best
.x
;
159 if (new_size
.y
== -1)
160 new_size
.y
= size_best
.y
;
161 if (new_size
.y
> size_best
.y
)
162 new_size
.y
= size_best
.y
;
163 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
164 SetSize( new_size
.x
, new_size
.y
);
166 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
167 SetForegroundColour( parent
->GetForegroundColour() );
174 wxComboBox::~wxComboBox()
176 wxNode
*node
= m_clientObjectList
.First();
179 wxClientData
*cd
= (wxClientData
*)node
->Data();
183 m_clientObjectList
.Clear();
185 m_clientDataList
.Clear();
188 void wxComboBox::AppendCommon( const wxString
&item
)
190 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
192 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
194 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
196 gtk_container_add( GTK_CONTAINER(list
), list_item
);
198 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
199 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
201 if (GTK_WIDGET_REALIZED(m_widget
))
203 gtk_widget_realize( list_item
);
204 gtk_widget_realize( GTK_BIN(list_item
)->child
);
206 if (m_widgetStyle
) ApplyWidgetStyle();
209 gtk_widget_show( list_item
);
212 void wxComboBox::Append( const wxString
&item
)
214 m_clientDataList
.Append( (wxObject
*) NULL
);
215 m_clientObjectList
.Append( (wxObject
*) NULL
);
217 AppendCommon( item
);
220 void wxComboBox::Append( const wxString
&item
, void *clientData
)
222 m_clientDataList
.Append( (wxObject
*) clientData
);
223 m_clientObjectList
.Append( (wxObject
*)NULL
);
225 AppendCommon( item
);
228 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
230 m_clientDataList
.Append( (wxObject
*) NULL
);
231 m_clientObjectList
.Append( (wxObject
*) clientData
);
233 AppendCommon( item
);
236 void wxComboBox::SetClientData( int n
, void* clientData
)
238 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
240 wxNode
*node
= m_clientDataList
.Nth( n
);
243 node
->SetData( (wxObject
*) clientData
);
246 void* wxComboBox::GetClientData( int n
)
248 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
250 wxNode
*node
= m_clientDataList
.Nth( n
);
251 if (!node
) return NULL
;
256 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
258 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
260 wxNode
*node
= m_clientObjectList
.Nth( n
);
263 wxClientData
*cd
= (wxClientData
*) node
->Data();
266 node
->SetData( (wxObject
*) clientData
);
269 wxClientData
* wxComboBox::GetClientObject( int n
)
271 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
273 wxNode
*node
= m_clientDataList
.Nth( n
);
274 if (!node
) return (wxClientData
*) NULL
;
276 return (wxClientData
*) node
->Data();
279 void wxComboBox::Clear()
281 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
283 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
284 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
286 wxNode
*node
= m_clientObjectList
.First();
289 wxClientData
*cd
= (wxClientData
*)node
->Data();
293 m_clientObjectList
.Clear();
295 m_clientDataList
.Clear();
298 void wxComboBox::Delete( int n
)
300 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
302 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
304 GList
*child
= g_list_nth( listbox
->children
, n
);
308 wxFAIL_MSG(wxT("wrong index"));
312 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
313 gtk_list_remove_items( listbox
, list
);
316 wxNode
*node
= m_clientObjectList
.Nth( n
);
319 wxClientData
*cd
= (wxClientData
*)node
->Data();
321 m_clientObjectList
.DeleteNode( node
);
324 node
= m_clientDataList
.Nth( n
);
327 m_clientDataList
.DeleteNode( node
);
331 int wxComboBox::FindString( const wxString
&item
)
333 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
335 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
337 GList
*child
= GTK_LIST(list
)->children
;
341 GtkBin
*bin
= GTK_BIN( child
->data
);
342 GtkLabel
*label
= GTK_LABEL( bin
->child
);
343 if (item
== wxString(label
->label
,*wxConvCurrent
))
352 int wxComboBox::GetSelection() const
354 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
356 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
358 GList
*selection
= GTK_LIST(list
)->selection
;
361 GList
*child
= GTK_LIST(list
)->children
;
365 if (child
->data
== selection
->data
) return count
;
374 wxString
wxComboBox::GetString( int n
) const
376 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
378 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
381 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
384 GtkBin
*bin
= GTK_BIN( child
->data
);
385 GtkLabel
*label
= GTK_LABEL( bin
->child
);
386 str
= wxString(label
->label
,*wxConvCurrent
);
390 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
396 wxString
wxComboBox::GetStringSelection() const
398 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
400 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
402 GList
*selection
= GTK_LIST(list
)->selection
;
405 GtkBin
*bin
= GTK_BIN( selection
->data
);
406 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
410 wxFAIL_MSG( wxT("wxComboBox: no selection") );
415 int wxComboBox::Number() const
417 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
419 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
421 GList
*child
= GTK_LIST(list
)->children
;
423 while (child
) { count
++; child
= child
->next
; }
427 void wxComboBox::SetSelection( int n
)
429 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
433 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
434 gtk_list_select_item( GTK_LIST(list
), n
);
439 void wxComboBox::SetStringSelection( const wxString
&string
)
441 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
443 int res
= FindString( string
);
444 if (res
== -1) return;
448 wxString
wxComboBox::GetValue() const
450 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
451 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
455 void wxComboBox::SetValue( const wxString
& value
)
457 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
459 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
460 wxString tmp
= wxT("");
461 if (!value
.IsNull()) tmp
= value
;
462 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
465 void wxComboBox::Copy()
467 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
469 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
470 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
471 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
473 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
477 void wxComboBox::Cut()
479 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
481 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
482 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
483 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
485 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
489 void wxComboBox::Paste()
491 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
493 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
494 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
495 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
497 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
501 void wxComboBox::SetInsertionPoint( long pos
)
503 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
505 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
506 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
509 void wxComboBox::SetInsertionPointEnd()
511 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
513 SetInsertionPoint( -1 );
516 long wxComboBox::GetInsertionPoint() const
518 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
519 return (long) GTK_EDITABLE(entry
)->current_pos
;
522 long wxComboBox::GetLastPosition() const
524 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
525 int pos
= GTK_ENTRY(entry
)->text_length
;
529 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
531 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
532 // FIXME: not quite sure how to do this method right in multibyte mode
534 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
535 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
536 if (value
.IsNull()) return;
538 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
541 void wxComboBox::Remove(long from
, long to
)
543 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
545 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
546 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
549 void wxComboBox::SetSelection( long from
, long to
)
551 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
552 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
555 void wxComboBox::SetEditable( bool editable
)
557 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
558 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
561 void wxComboBox::OnChar( wxKeyEvent
&event
)
563 if ( event
.KeyCode() == WXK_RETURN
)
565 wxString value
= GetValue();
569 // make Enter generate "selected" event if there is only one item
570 // in the combobox - without it, it's impossible to select it at
572 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
574 event
.SetString( value
);
575 event
.SetEventObject( this );
576 GetEventHandler()->ProcessEvent( event
);
580 // add the item to the list if it's not there yet
581 if ( FindString(value
) == wxNOT_FOUND
)
584 SetStringSelection(value
);
586 // and generate the selected event for it
587 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
588 event
.SetInt( Number() - 1 );
589 event
.SetString( value
);
590 event
.SetEventObject( this );
591 GetEventHandler()->ProcessEvent( event
);
593 //else: do nothing, this will open the listbox
600 void wxComboBox::DisableEvents()
602 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
603 GList
*child
= list
->children
;
606 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
607 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
613 void wxComboBox::EnableEvents()
615 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
616 GList
*child
= list
->children
;
619 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
620 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
626 void wxComboBox::OnSize( wxSizeEvent
&event
)
632 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
634 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
635 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
639 void wxComboBox::ApplyWidgetStyle()
643 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
644 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
645 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
647 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
648 GList
*child
= list
->children
;
651 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
653 GtkBin
*bin
= GTK_BIN(child
->data
);
654 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
660 GtkWidget
* wxComboBox::GetConnectWidget()
662 return GTK_COMBO(m_widget
)->entry
;
665 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
667 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
668 (window
== GTK_COMBO(m_widget
)->button
->window
) );
671 wxSize
wxComboBox::DoGetBestSize() const
673 wxSize
ret( wxControl::DoGetBestSize() );
675 // we know better our horizontal extent: it depends on the longest string
680 GdkFont
*font
= m_font
.GetInternalFont();
683 size_t count
= Number();
684 for ( size_t n
= 0; n
< count
; n
++ )
686 width
= (wxCoord
)gdk_string_width(font
, GetString(n
).mbc_str());
692 // empty combobox should have some reasonable default size too