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 PreCreation( parent
, id
, pos
, size
, style
, name
);
107 SetValidator( validator
);
110 m_widget
= gtk_combo_new();
112 // make it more useable
113 gtk_combo_set_use_arrows_always(GTK_COMBO(m_widget
), TRUE
);
115 wxSize newSize
= size
;
120 SetSize( newSize
.x
, newSize
.y
);
122 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
124 for (int i
= 0; i
< n
; i
++)
126 /* don't send first event, which GTK sends aways when
127 inserting the first item */
128 m_alreadySent
= TRUE
;
130 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
].mbc_str() );
132 m_clientDataList
.Append( (wxObject
*)NULL
);
133 m_clientObjectList
.Append( (wxObject
*)NULL
);
135 gtk_container_add( GTK_CONTAINER(list
), list_item
);
137 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
138 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
140 gtk_widget_show( list_item
);
143 m_parent
->DoAddChild( this );
147 ConnectWidget( GTK_COMBO(m_widget
)->button
);
149 if (!value
.IsNull()) SetValue( value
);
151 if (style
& wxCB_READONLY
)
152 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget
)->entry
), FALSE
);
154 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
155 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
157 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
158 SetForegroundColour( parent
->GetForegroundColour() );
159 SetFont( parent
->GetFont() );
166 wxComboBox::~wxComboBox()
168 wxNode
*node
= m_clientObjectList
.First();
171 wxClientData
*cd
= (wxClientData
*)node
->Data();
175 m_clientObjectList
.Clear();
177 m_clientDataList
.Clear();
180 void wxComboBox::AppendCommon( const wxString
&item
)
182 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
184 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
186 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
188 gtk_container_add( GTK_CONTAINER(list
), list_item
);
190 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
191 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
193 if (GTK_WIDGET_REALIZED(m_widget
))
195 gtk_widget_realize( list_item
);
196 gtk_widget_realize( GTK_BIN(list_item
)->child
);
198 if (m_widgetStyle
) ApplyWidgetStyle();
201 gtk_widget_show( list_item
);
204 void wxComboBox::Append( const wxString
&item
)
206 m_clientDataList
.Append( (wxObject
*) NULL
);
207 m_clientObjectList
.Append( (wxObject
*) NULL
);
209 AppendCommon( item
);
212 void wxComboBox::Append( const wxString
&item
, void *clientData
)
214 m_clientDataList
.Append( (wxObject
*) clientData
);
215 m_clientObjectList
.Append( (wxObject
*)NULL
);
217 AppendCommon( item
);
220 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
222 m_clientDataList
.Append( (wxObject
*) NULL
);
223 m_clientObjectList
.Append( (wxObject
*) clientData
);
225 AppendCommon( item
);
228 void wxComboBox::SetClientData( int n
, void* clientData
)
230 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
232 wxNode
*node
= m_clientDataList
.Nth( n
);
235 node
->SetData( (wxObject
*) clientData
);
238 void* wxComboBox::GetClientData( int n
)
240 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
242 wxNode
*node
= m_clientDataList
.Nth( n
);
243 if (!node
) return NULL
;
248 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
250 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
252 wxNode
*node
= m_clientObjectList
.Nth( n
);
255 wxClientData
*cd
= (wxClientData
*) node
->Data();
258 node
->SetData( (wxObject
*) clientData
);
261 wxClientData
* wxComboBox::GetClientObject( int n
)
263 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, _T("invalid combobox") );
265 wxNode
*node
= m_clientDataList
.Nth( n
);
266 if (!node
) return (wxClientData
*) NULL
;
268 return (wxClientData
*) node
->Data();
271 void wxComboBox::Clear()
273 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
275 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
276 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
278 wxNode
*node
= m_clientObjectList
.First();
281 wxClientData
*cd
= (wxClientData
*)node
->Data();
285 m_clientObjectList
.Clear();
287 m_clientDataList
.Clear();
290 void wxComboBox::Delete( int n
)
292 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
294 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
296 GList
*child
= g_list_nth( listbox
->children
, n
);
300 wxFAIL_MSG(_T("wrong index"));
304 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
305 gtk_list_remove_items( listbox
, list
);
308 wxNode
*node
= m_clientObjectList
.Nth( n
);
311 wxClientData
*cd
= (wxClientData
*)node
->Data();
313 m_clientObjectList
.DeleteNode( node
);
316 node
= m_clientDataList
.Nth( n
);
319 m_clientDataList
.DeleteNode( node
);
323 int wxComboBox::FindString( const wxString
&item
)
325 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid combobox") );
327 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
329 GList
*child
= GTK_LIST(list
)->children
;
333 GtkBin
*bin
= GTK_BIN( child
->data
);
334 GtkLabel
*label
= GTK_LABEL( bin
->child
);
335 if (item
== wxString(label
->label
,*wxConvCurrent
))
344 int wxComboBox::GetSelection() const
346 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid combobox") );
348 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
350 GList
*selection
= GTK_LIST(list
)->selection
;
353 GList
*child
= GTK_LIST(list
)->children
;
357 if (child
->data
== selection
->data
) return count
;
366 wxString
wxComboBox::GetString( int n
) const
368 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid combobox") );
370 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
373 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
376 GtkBin
*bin
= GTK_BIN( child
->data
);
377 GtkLabel
*label
= GTK_LABEL( bin
->child
);
378 str
= wxString(label
->label
,*wxConvCurrent
);
382 wxFAIL_MSG( _T("wxComboBox: wrong index") );
388 wxString
wxComboBox::GetStringSelection() const
390 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid combobox") );
392 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
394 GList
*selection
= GTK_LIST(list
)->selection
;
397 GtkBin
*bin
= GTK_BIN( selection
->data
);
398 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
402 wxFAIL_MSG( _T("wxComboBox: no selection") );
407 int wxComboBox::Number() const
409 wxCHECK_MSG( m_widget
!= NULL
, 0, _T("invalid combobox") );
411 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
413 GList
*child
= GTK_LIST(list
)->children
;
415 while (child
) { count
++; child
= child
->next
; }
419 void wxComboBox::SetSelection( int n
)
421 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
425 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
426 gtk_list_select_item( GTK_LIST(list
), n
);
431 void wxComboBox::SetStringSelection( const wxString
&string
)
433 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
435 int res
= FindString( string
);
436 if (res
== -1) return;
440 wxString
wxComboBox::GetValue() const
442 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
443 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
447 void wxComboBox::SetValue( const wxString
& value
)
449 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
451 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
452 wxString tmp
= _T("");
453 if (!value
.IsNull()) tmp
= value
;
454 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
457 void wxComboBox::Copy()
459 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
461 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
462 #if (GTK_MINOR_VERSION > 0)
463 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
465 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
469 void wxComboBox::Cut()
471 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
473 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
474 #if (GTK_MINOR_VERSION > 0)
475 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
477 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
481 void wxComboBox::Paste()
483 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
485 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
486 #if (GTK_MINOR_VERSION > 0)
487 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
489 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
493 void wxComboBox::SetInsertionPoint( long pos
)
495 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
497 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
498 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
501 void wxComboBox::SetInsertionPointEnd()
503 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
505 SetInsertionPoint( -1 );
508 long wxComboBox::GetInsertionPoint() const
510 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
511 return (long) GTK_EDITABLE(entry
)->current_pos
;
514 long wxComboBox::GetLastPosition() const
516 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
517 int pos
= GTK_ENTRY(entry
)->text_length
;
521 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
523 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
524 // FIXME: not quite sure how to do this method right in multibyte mode
526 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
527 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
528 if (value
.IsNull()) return;
530 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
533 void wxComboBox::Remove(long from
, long to
)
535 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
537 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
538 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
541 void wxComboBox::SetSelection( long from
, long to
)
543 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
544 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
547 void wxComboBox::SetEditable( bool editable
)
549 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
550 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
553 void wxComboBox::OnChar( wxKeyEvent
&event
)
555 if ( event
.KeyCode() == WXK_RETURN
)
557 wxString value
= GetValue();
561 // make Enter generate "selected" event if there is only one item
562 // in the combobox - without it, it's impossible to select it at
564 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
566 event
.SetString( value
);
567 event
.SetEventObject( this );
568 GetEventHandler()->ProcessEvent( event
);
572 // add the item to the list if it's not there yet
573 if ( FindString(value
) == wxNOT_FOUND
)
577 // and generate the selected event for it
578 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
579 event
.SetInt( Number() - 1 );
580 event
.SetString( value
);
581 event
.SetEventObject( this );
582 GetEventHandler()->ProcessEvent( event
);
584 //else: do nothing, this will open the listbox
591 void wxComboBox::DisableEvents()
593 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
594 GList
*child
= list
->children
;
597 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
598 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
604 void wxComboBox::EnableEvents()
606 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
607 GList
*child
= list
->children
;
610 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
611 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
617 void wxComboBox::OnSize( wxSizeEvent
&event
)
624 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
626 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
627 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
630 void wxComboBox::ApplyWidgetStyle()
634 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
635 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
636 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
638 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
639 GList
*child
= list
->children
;
642 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
644 GtkBin
*bin
= GTK_BIN(child
->data
);
645 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
651 GtkWidget
* wxComboBox::GetConnectWidget()
653 return GTK_COMBO(m_widget
)->entry
;
656 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
658 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
659 (window
== GTK_COMBO(m_widget
)->button
->window
) );