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
))
181 SetSize( new_size
.x
, new_size
.y
);
183 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
184 SetForegroundColour( parent
->GetForegroundColour() );
191 wxComboBox::~wxComboBox()
193 wxNode
*node
= m_clientObjectList
.First();
196 wxClientData
*cd
= (wxClientData
*)node
->Data();
200 m_clientObjectList
.Clear();
202 m_clientDataList
.Clear();
205 void wxComboBox::AppendCommon( const wxString
&item
)
207 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
209 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
211 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
213 gtk_container_add( GTK_CONTAINER(list
), list_item
);
215 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
216 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
218 if (GTK_WIDGET_REALIZED(m_widget
))
220 gtk_widget_realize( list_item
);
221 gtk_widget_realize( GTK_BIN(list_item
)->child
);
223 if (m_widgetStyle
) ApplyWidgetStyle();
226 gtk_widget_show( list_item
);
229 void wxComboBox::Append( const wxString
&item
)
231 m_clientDataList
.Append( (wxObject
*) NULL
);
232 m_clientObjectList
.Append( (wxObject
*) NULL
);
234 AppendCommon( item
);
237 void wxComboBox::Append( const wxString
&item
, void *clientData
)
239 m_clientDataList
.Append( (wxObject
*) clientData
);
240 m_clientObjectList
.Append( (wxObject
*)NULL
);
242 AppendCommon( item
);
245 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
247 m_clientDataList
.Append( (wxObject
*) NULL
);
248 m_clientObjectList
.Append( (wxObject
*) clientData
);
250 AppendCommon( item
);
253 void wxComboBox::SetClientData( int n
, void* clientData
)
255 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
257 wxNode
*node
= m_clientDataList
.Nth( n
);
260 node
->SetData( (wxObject
*) clientData
);
263 void* wxComboBox::GetClientData( int n
)
265 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
267 wxNode
*node
= m_clientDataList
.Nth( n
);
268 if (!node
) return NULL
;
273 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
275 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
277 wxNode
*node
= m_clientObjectList
.Nth( n
);
280 wxClientData
*cd
= (wxClientData
*) node
->Data();
283 node
->SetData( (wxObject
*) clientData
);
286 wxClientData
* wxComboBox::GetClientObject( int n
)
288 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
290 wxNode
*node
= m_clientDataList
.Nth( n
);
291 if (!node
) return (wxClientData
*) NULL
;
293 return (wxClientData
*) node
->Data();
296 void wxComboBox::Clear()
298 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
300 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
301 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
303 wxNode
*node
= m_clientObjectList
.First();
306 wxClientData
*cd
= (wxClientData
*)node
->Data();
310 m_clientObjectList
.Clear();
312 m_clientDataList
.Clear();
315 void wxComboBox::Delete( int n
)
317 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
319 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
321 GList
*child
= g_list_nth( listbox
->children
, n
);
325 wxFAIL_MSG(wxT("wrong index"));
329 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
330 gtk_list_remove_items( listbox
, list
);
333 wxNode
*node
= m_clientObjectList
.Nth( n
);
336 wxClientData
*cd
= (wxClientData
*)node
->Data();
338 m_clientObjectList
.DeleteNode( node
);
341 node
= m_clientDataList
.Nth( n
);
344 m_clientDataList
.DeleteNode( node
);
348 int wxComboBox::FindString( const wxString
&item
)
350 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
352 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
354 GList
*child
= GTK_LIST(list
)->children
;
358 GtkBin
*bin
= GTK_BIN( child
->data
);
359 GtkLabel
*label
= GTK_LABEL( bin
->child
);
360 if (item
== wxString(label
->label
,*wxConvCurrent
))
369 int wxComboBox::GetSelection() const
371 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
373 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
375 GList
*selection
= GTK_LIST(list
)->selection
;
378 GList
*child
= GTK_LIST(list
)->children
;
382 if (child
->data
== selection
->data
) return count
;
391 wxString
wxComboBox::GetString( int n
) const
393 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
395 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
398 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
401 GtkBin
*bin
= GTK_BIN( child
->data
);
402 GtkLabel
*label
= GTK_LABEL( bin
->child
);
403 str
= wxString(label
->label
,*wxConvCurrent
);
407 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
413 wxString
wxComboBox::GetStringSelection() const
415 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
417 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
419 GList
*selection
= GTK_LIST(list
)->selection
;
422 GtkBin
*bin
= GTK_BIN( selection
->data
);
423 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
427 wxFAIL_MSG( wxT("wxComboBox: no selection") );
432 int wxComboBox::Number() const
434 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
436 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
438 GList
*child
= GTK_LIST(list
)->children
;
440 while (child
) { count
++; child
= child
->next
; }
444 void wxComboBox::SetSelection( int n
)
446 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
450 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
451 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
452 gtk_list_select_item( GTK_LIST(list
), n
);
458 void wxComboBox::SetStringSelection( const wxString
&string
)
460 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
462 int res
= FindString( string
);
463 if (res
== -1) return;
467 wxString
wxComboBox::GetValue() const
469 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
470 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
474 void wxComboBox::SetValue( const wxString
& value
)
476 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
478 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
479 wxString tmp
= wxT("");
480 if (!value
.IsNull()) tmp
= value
;
481 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
484 void wxComboBox::Copy()
486 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
488 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
489 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
490 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
492 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
496 void wxComboBox::Cut()
498 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
500 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
501 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
502 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
504 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
508 void wxComboBox::Paste()
510 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
512 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
513 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
514 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
516 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
520 void wxComboBox::SetInsertionPoint( long pos
)
522 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
524 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
525 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
528 void wxComboBox::SetInsertionPointEnd()
530 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
532 SetInsertionPoint( -1 );
535 long wxComboBox::GetInsertionPoint() const
537 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
538 return (long) GTK_EDITABLE(entry
)->current_pos
;
541 long wxComboBox::GetLastPosition() const
543 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
544 int pos
= GTK_ENTRY(entry
)->text_length
;
548 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
550 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
551 // FIXME: not quite sure how to do this method right in multibyte mode
553 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
554 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
555 if (value
.IsNull()) return;
557 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
560 void wxComboBox::Remove(long from
, long to
)
562 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
564 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
565 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
568 void wxComboBox::SetSelection( long from
, long to
)
570 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
571 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
574 void wxComboBox::SetEditable( bool editable
)
576 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
577 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
580 void wxComboBox::OnChar( wxKeyEvent
&event
)
582 if ( event
.KeyCode() == WXK_RETURN
)
584 wxString value
= GetValue();
588 // make Enter generate "selected" event if there is only one item
589 // in the combobox - without it, it's impossible to select it at
591 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
593 event
.SetString( value
);
594 event
.SetEventObject( this );
595 GetEventHandler()->ProcessEvent( event
);
599 // add the item to the list if it's not there yet
600 if ( FindString(value
) == wxNOT_FOUND
)
603 SetStringSelection(value
);
605 // and generate the selected event for it
606 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
607 event
.SetInt( Number() - 1 );
608 event
.SetString( value
);
609 event
.SetEventObject( this );
610 GetEventHandler()->ProcessEvent( event
);
612 //else: do nothing, this will open the listbox
619 void wxComboBox::DisableEvents()
621 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
622 GList
*child
= list
->children
;
625 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
626 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
632 void wxComboBox::EnableEvents()
634 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
635 GList
*child
= list
->children
;
638 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
639 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
645 void wxComboBox::OnSize( wxSizeEvent
&event
)
651 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
653 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
654 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
658 void wxComboBox::ApplyWidgetStyle()
662 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
663 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
664 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
666 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
667 GList
*child
= list
->children
;
670 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
672 GtkBin
*bin
= GTK_BIN(child
->data
);
673 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
679 GtkWidget
* wxComboBox::GetConnectWidget()
681 return GTK_COMBO(m_widget
)->entry
;
684 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
686 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
687 (window
== GTK_COMBO(m_widget
)->button
->window
) );
690 wxSize
wxComboBox::DoGetBestSize() const
692 wxSize
ret( wxControl::DoGetBestSize() );
694 // we know better our horizontal extent: it depends on the longest string
699 GdkFont
*font
= m_font
.GetInternalFont();
702 size_t count
= Number();
703 for ( size_t n
= 0; n
< count
; n
++ )
705 width
= (wxCoord
)gdk_string_width(font
, GetString(n
).mbc_str());
711 // empty combobox should have some reasonable default size too