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
;
363 wxFAIL_MSG( _T("wxComboBox: no selection") );
368 wxString
wxComboBox::GetString( int n
) const
370 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid combobox") );
372 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
375 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
378 GtkBin
*bin
= GTK_BIN( child
->data
);
379 GtkLabel
*label
= GTK_LABEL( bin
->child
);
380 str
= wxString(label
->label
,*wxConvCurrent
);
384 wxFAIL_MSG( _T("wxComboBox: wrong index") );
390 wxString
wxComboBox::GetStringSelection() const
392 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid combobox") );
394 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
396 GList
*selection
= GTK_LIST(list
)->selection
;
399 GtkBin
*bin
= GTK_BIN( selection
->data
);
400 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
404 wxFAIL_MSG( _T("wxComboBox: no selection") );
409 int wxComboBox::Number() const
411 wxCHECK_MSG( m_widget
!= NULL
, 0, _T("invalid combobox") );
413 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
415 GList
*child
= GTK_LIST(list
)->children
;
417 while (child
) { count
++; child
= child
->next
; }
421 void wxComboBox::SetSelection( int n
)
423 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
427 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
428 gtk_list_select_item( GTK_LIST(list
), n
);
433 void wxComboBox::SetStringSelection( const wxString
&string
)
435 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
437 int res
= FindString( string
);
438 if (res
== -1) return;
442 wxString
wxComboBox::GetValue() const
444 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
445 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
449 void wxComboBox::SetValue( const wxString
& value
)
451 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
453 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
454 wxString tmp
= _T("");
455 if (!value
.IsNull()) tmp
= value
;
456 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
459 void wxComboBox::Copy()
461 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
463 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
464 #if (GTK_MINOR_VERSION > 0)
465 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
467 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
471 void wxComboBox::Cut()
473 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
475 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
476 #if (GTK_MINOR_VERSION > 0)
477 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
479 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
483 void wxComboBox::Paste()
485 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
487 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
488 #if (GTK_MINOR_VERSION > 0)
489 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
491 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
495 void wxComboBox::SetInsertionPoint( long pos
)
497 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
499 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
500 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
503 void wxComboBox::SetInsertionPointEnd()
505 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
507 SetInsertionPoint( -1 );
510 long wxComboBox::GetInsertionPoint() const
512 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
513 return (long) GTK_EDITABLE(entry
)->current_pos
;
516 long wxComboBox::GetLastPosition() const
518 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
519 int pos
= GTK_ENTRY(entry
)->text_length
;
523 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
525 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
526 // FIXME: not quite sure how to do this method right in multibyte mode
528 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
529 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
530 if (value
.IsNull()) return;
532 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
535 void wxComboBox::Remove(long from
, long to
)
537 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
539 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
540 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
543 void wxComboBox::SetSelection( long from
, long to
)
545 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
546 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
549 void wxComboBox::SetEditable( bool editable
)
551 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
552 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
555 void wxComboBox::OnChar( wxKeyEvent
&event
)
557 if ( event
.KeyCode() == WXK_RETURN
)
559 wxString value
= GetValue();
563 // make Enter generate "selected" event if there is only one item
564 // in the combobox - without it, it's impossible to select it at
566 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
568 event
.SetString( value
);
569 event
.SetEventObject( this );
570 GetEventHandler()->ProcessEvent( event
);
574 // add the item to the list if it's not there yet
575 if ( FindString(value
) == wxNOT_FOUND
)
579 // and generate the selected event for it
580 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
581 event
.SetInt( Number() - 1 );
582 event
.SetString( value
);
583 event
.SetEventObject( this );
584 GetEventHandler()->ProcessEvent( event
);
586 //else: do nothing, this will open the listbox
593 void wxComboBox::DisableEvents()
595 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
596 GList
*child
= list
->children
;
599 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
600 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
606 void wxComboBox::EnableEvents()
608 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
609 GList
*child
= list
->children
;
612 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
613 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
619 void wxComboBox::OnSize( wxSizeEvent
&event
)
626 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
628 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
629 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
632 void wxComboBox::ApplyWidgetStyle()
636 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
637 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
638 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
640 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
641 GList
*child
= list
->children
;
644 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
646 GtkBin
*bin
= GTK_BIN(child
->data
);
647 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
653 GtkWidget
* wxComboBox::GetConnectWidget()
655 return GTK_COMBO(m_widget
)->entry
;
658 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
660 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
661 (window
== GTK_COMBO(m_widget
)->button
->window
) );