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 );
160 ConnectWidget( GTK_COMBO(m_widget
)->button
);
162 if (!value
.IsNull()) SetValue( value
);
164 if (style
& wxCB_READONLY
)
165 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget
)->entry
), FALSE
);
167 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
168 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
170 wxSize
size_best( DoGetBestSize() );
171 wxSize
new_size( size
);
172 if (new_size
.x
== -1)
173 new_size
.x
= size_best
.x
;
174 if (new_size
.y
== -1)
175 new_size
.y
= size_best
.y
;
176 if (new_size
.y
> size_best
.y
)
177 new_size
.y
= size_best
.y
;
178 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
179 SetSize( new_size
.x
, new_size
.y
);
181 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
182 SetForegroundColour( parent
->GetForegroundColour() );
189 wxComboBox::~wxComboBox()
191 wxNode
*node
= m_clientObjectList
.First();
194 wxClientData
*cd
= (wxClientData
*)node
->Data();
198 m_clientObjectList
.Clear();
200 m_clientDataList
.Clear();
203 void wxComboBox::AppendCommon( const wxString
&item
)
205 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
207 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
209 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
211 gtk_container_add( GTK_CONTAINER(list
), list_item
);
213 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
214 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
216 if (GTK_WIDGET_REALIZED(m_widget
))
218 gtk_widget_realize( list_item
);
219 gtk_widget_realize( GTK_BIN(list_item
)->child
);
221 if (m_widgetStyle
) ApplyWidgetStyle();
224 gtk_widget_show( list_item
);
227 void wxComboBox::Append( const wxString
&item
)
229 m_clientDataList
.Append( (wxObject
*) NULL
);
230 m_clientObjectList
.Append( (wxObject
*) NULL
);
232 AppendCommon( item
);
235 void wxComboBox::Append( const wxString
&item
, void *clientData
)
237 m_clientDataList
.Append( (wxObject
*) clientData
);
238 m_clientObjectList
.Append( (wxObject
*)NULL
);
240 AppendCommon( item
);
243 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
245 m_clientDataList
.Append( (wxObject
*) NULL
);
246 m_clientObjectList
.Append( (wxObject
*) clientData
);
248 AppendCommon( item
);
251 void wxComboBox::SetClientData( int n
, void* clientData
)
253 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
255 wxNode
*node
= m_clientDataList
.Nth( n
);
258 node
->SetData( (wxObject
*) clientData
);
261 void* wxComboBox::GetClientData( int n
)
263 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
265 wxNode
*node
= m_clientDataList
.Nth( n
);
266 if (!node
) return NULL
;
271 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
273 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
275 wxNode
*node
= m_clientObjectList
.Nth( n
);
278 wxClientData
*cd
= (wxClientData
*) node
->Data();
281 node
->SetData( (wxObject
*) clientData
);
284 wxClientData
* wxComboBox::GetClientObject( int n
)
286 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
288 wxNode
*node
= m_clientDataList
.Nth( n
);
289 if (!node
) return (wxClientData
*) NULL
;
291 return (wxClientData
*) node
->Data();
294 void wxComboBox::Clear()
296 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
298 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
299 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
301 wxNode
*node
= m_clientObjectList
.First();
304 wxClientData
*cd
= (wxClientData
*)node
->Data();
308 m_clientObjectList
.Clear();
310 m_clientDataList
.Clear();
313 void wxComboBox::Delete( int n
)
315 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
317 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
319 GList
*child
= g_list_nth( listbox
->children
, n
);
323 wxFAIL_MSG(wxT("wrong index"));
327 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
328 gtk_list_remove_items( listbox
, list
);
331 wxNode
*node
= m_clientObjectList
.Nth( n
);
334 wxClientData
*cd
= (wxClientData
*)node
->Data();
336 m_clientObjectList
.DeleteNode( node
);
339 node
= m_clientDataList
.Nth( n
);
342 m_clientDataList
.DeleteNode( node
);
346 int wxComboBox::FindString( const wxString
&item
)
348 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
350 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
352 GList
*child
= GTK_LIST(list
)->children
;
356 GtkBin
*bin
= GTK_BIN( child
->data
);
357 GtkLabel
*label
= GTK_LABEL( bin
->child
);
358 if (item
== wxString(label
->label
,*wxConvCurrent
))
367 int wxComboBox::GetSelection() const
369 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
371 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
373 GList
*selection
= GTK_LIST(list
)->selection
;
376 GList
*child
= GTK_LIST(list
)->children
;
380 if (child
->data
== selection
->data
) return count
;
389 wxString
wxComboBox::GetString( int n
) const
391 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
393 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
396 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
399 GtkBin
*bin
= GTK_BIN( child
->data
);
400 GtkLabel
*label
= GTK_LABEL( bin
->child
);
401 str
= wxString(label
->label
,*wxConvCurrent
);
405 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
411 wxString
wxComboBox::GetStringSelection() const
413 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
415 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
417 GList
*selection
= GTK_LIST(list
)->selection
;
420 GtkBin
*bin
= GTK_BIN( selection
->data
);
421 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
425 wxFAIL_MSG( wxT("wxComboBox: no selection") );
430 int wxComboBox::Number() const
432 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
434 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
436 GList
*child
= GTK_LIST(list
)->children
;
438 while (child
) { count
++; child
= child
->next
; }
442 void wxComboBox::SetSelection( int n
)
444 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
448 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
449 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
450 gtk_list_select_item( GTK_LIST(list
), n
);
456 void wxComboBox::SetStringSelection( const wxString
&string
)
458 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
460 int res
= FindString( string
);
461 if (res
== -1) return;
465 wxString
wxComboBox::GetValue() const
467 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
468 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
472 void wxComboBox::SetValue( const wxString
& value
)
474 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
476 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
477 wxString tmp
= wxT("");
478 if (!value
.IsNull()) tmp
= value
;
479 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
482 void wxComboBox::Copy()
484 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
486 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
487 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
488 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
490 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
494 void wxComboBox::Cut()
496 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
498 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
499 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
500 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
502 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
506 void wxComboBox::Paste()
508 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
510 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
511 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
512 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
514 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
518 void wxComboBox::SetInsertionPoint( long pos
)
520 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
522 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
523 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
526 void wxComboBox::SetInsertionPointEnd()
528 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
530 SetInsertionPoint( -1 );
533 long wxComboBox::GetInsertionPoint() const
535 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
536 return (long) GTK_EDITABLE(entry
)->current_pos
;
539 long wxComboBox::GetLastPosition() const
541 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
542 int pos
= GTK_ENTRY(entry
)->text_length
;
546 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
548 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
549 // FIXME: not quite sure how to do this method right in multibyte mode
551 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
552 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
553 if (value
.IsNull()) return;
555 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
558 void wxComboBox::Remove(long from
, long to
)
560 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
562 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
563 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
566 void wxComboBox::SetSelection( long from
, long to
)
568 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
569 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
572 void wxComboBox::SetEditable( bool editable
)
574 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
575 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
578 void wxComboBox::OnChar( wxKeyEvent
&event
)
580 if ( event
.KeyCode() == WXK_RETURN
)
582 wxString value
= GetValue();
586 // make Enter generate "selected" event if there is only one item
587 // in the combobox - without it, it's impossible to select it at
589 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
591 event
.SetString( value
);
592 event
.SetEventObject( this );
593 GetEventHandler()->ProcessEvent( event
);
597 // add the item to the list if it's not there yet
598 if ( FindString(value
) == wxNOT_FOUND
)
601 SetStringSelection(value
);
603 // and generate the selected event for it
604 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
605 event
.SetInt( Number() - 1 );
606 event
.SetString( value
);
607 event
.SetEventObject( this );
608 GetEventHandler()->ProcessEvent( event
);
610 //else: do nothing, this will open the listbox
617 void wxComboBox::DisableEvents()
619 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
620 GList
*child
= list
->children
;
623 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
624 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
630 void wxComboBox::EnableEvents()
632 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
633 GList
*child
= list
->children
;
636 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
637 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
643 void wxComboBox::OnSize( wxSizeEvent
&event
)
649 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
651 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
652 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
656 void wxComboBox::ApplyWidgetStyle()
660 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
661 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
662 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
664 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
665 GList
*child
= list
->children
;
668 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
670 GtkBin
*bin
= GTK_BIN(child
->data
);
671 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
677 GtkWidget
* wxComboBox::GetConnectWidget()
679 return GTK_COMBO(m_widget
)->entry
;
682 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
684 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
685 (window
== GTK_COMBO(m_widget
)->button
->window
) );
688 wxSize
wxComboBox::DoGetBestSize() const
690 wxSize
ret( wxControl::DoGetBestSize() );
692 // we know better our horizontal extent: it depends on the longest string
697 GdkFont
*font
= m_font
.GetInternalFont();
700 size_t count
= Number();
701 for ( size_t n
= 0; n
< count
; n
++ )
703 width
= (wxCoord
)gdk_string_width(font
, GetString(n
).mbc_str());
709 // empty combobox should have some reasonable default size too