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 SetSizeOrDefault( size
);
118 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
120 for (int i
= 0; i
< n
; i
++)
122 /* don't send first event, which GTK sends aways when
123 inserting the first item */
124 m_alreadySent
= TRUE
;
126 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
].mbc_str() );
128 m_clientDataList
.Append( (wxObject
*)NULL
);
129 m_clientObjectList
.Append( (wxObject
*)NULL
);
131 gtk_container_add( GTK_CONTAINER(list
), list_item
);
133 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
134 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
136 gtk_widget_show( list_item
);
139 m_parent
->DoAddChild( this );
143 ConnectWidget( GTK_COMBO(m_widget
)->button
);
145 if (!value
.IsNull()) SetValue( value
);
147 if (style
& wxCB_READONLY
)
148 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget
)->entry
), FALSE
);
150 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
151 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
153 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
154 SetForegroundColour( parent
->GetForegroundColour() );
155 SetFont( parent
->GetFont() );
162 wxComboBox::~wxComboBox()
164 wxNode
*node
= m_clientObjectList
.First();
167 wxClientData
*cd
= (wxClientData
*)node
->Data();
171 m_clientObjectList
.Clear();
173 m_clientDataList
.Clear();
176 void wxComboBox::AppendCommon( const wxString
&item
)
178 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
180 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
182 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
184 gtk_container_add( GTK_CONTAINER(list
), list_item
);
186 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
187 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
189 if (GTK_WIDGET_REALIZED(m_widget
))
191 gtk_widget_realize( list_item
);
192 gtk_widget_realize( GTK_BIN(list_item
)->child
);
194 if (m_widgetStyle
) ApplyWidgetStyle();
197 gtk_widget_show( list_item
);
200 void wxComboBox::Append( const wxString
&item
)
202 m_clientDataList
.Append( (wxObject
*) NULL
);
203 m_clientObjectList
.Append( (wxObject
*) NULL
);
205 AppendCommon( item
);
208 void wxComboBox::Append( const wxString
&item
, void *clientData
)
210 m_clientDataList
.Append( (wxObject
*) clientData
);
211 m_clientObjectList
.Append( (wxObject
*)NULL
);
213 AppendCommon( item
);
216 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
218 m_clientDataList
.Append( (wxObject
*) NULL
);
219 m_clientObjectList
.Append( (wxObject
*) clientData
);
221 AppendCommon( item
);
224 void wxComboBox::SetClientData( int n
, void* clientData
)
226 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
228 wxNode
*node
= m_clientDataList
.Nth( n
);
231 node
->SetData( (wxObject
*) clientData
);
234 void* wxComboBox::GetClientData( int n
)
236 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
238 wxNode
*node
= m_clientDataList
.Nth( n
);
239 if (!node
) return NULL
;
244 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
246 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
248 wxNode
*node
= m_clientObjectList
.Nth( n
);
251 wxClientData
*cd
= (wxClientData
*) node
->Data();
254 node
->SetData( (wxObject
*) clientData
);
257 wxClientData
* wxComboBox::GetClientObject( int n
)
259 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
261 wxNode
*node
= m_clientDataList
.Nth( n
);
262 if (!node
) return (wxClientData
*) NULL
;
264 return (wxClientData
*) node
->Data();
267 void wxComboBox::Clear()
269 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
271 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
272 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
274 wxNode
*node
= m_clientObjectList
.First();
277 wxClientData
*cd
= (wxClientData
*)node
->Data();
281 m_clientObjectList
.Clear();
283 m_clientDataList
.Clear();
286 void wxComboBox::Delete( int n
)
288 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
290 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
292 GList
*child
= g_list_nth( listbox
->children
, n
);
296 wxFAIL_MSG(wxT("wrong index"));
300 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
301 gtk_list_remove_items( listbox
, list
);
304 wxNode
*node
= m_clientObjectList
.Nth( n
);
307 wxClientData
*cd
= (wxClientData
*)node
->Data();
309 m_clientObjectList
.DeleteNode( node
);
312 node
= m_clientDataList
.Nth( n
);
315 m_clientDataList
.DeleteNode( node
);
319 int wxComboBox::FindString( const wxString
&item
)
321 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
323 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
325 GList
*child
= GTK_LIST(list
)->children
;
329 GtkBin
*bin
= GTK_BIN( child
->data
);
330 GtkLabel
*label
= GTK_LABEL( bin
->child
);
331 if (item
== wxString(label
->label
,*wxConvCurrent
))
340 int wxComboBox::GetSelection() const
342 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
344 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
346 GList
*selection
= GTK_LIST(list
)->selection
;
349 GList
*child
= GTK_LIST(list
)->children
;
353 if (child
->data
== selection
->data
) return count
;
362 wxString
wxComboBox::GetString( int n
) const
364 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
366 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
369 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
372 GtkBin
*bin
= GTK_BIN( child
->data
);
373 GtkLabel
*label
= GTK_LABEL( bin
->child
);
374 str
= wxString(label
->label
,*wxConvCurrent
);
378 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
384 wxString
wxComboBox::GetStringSelection() const
386 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
388 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
390 GList
*selection
= GTK_LIST(list
)->selection
;
393 GtkBin
*bin
= GTK_BIN( selection
->data
);
394 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
398 wxFAIL_MSG( wxT("wxComboBox: no selection") );
403 int wxComboBox::Number() const
405 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
407 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
409 GList
*child
= GTK_LIST(list
)->children
;
411 while (child
) { count
++; child
= child
->next
; }
415 void wxComboBox::SetSelection( int n
)
417 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
421 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
422 gtk_list_select_item( GTK_LIST(list
), n
);
427 void wxComboBox::SetStringSelection( const wxString
&string
)
429 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
431 int res
= FindString( string
);
432 if (res
== -1) return;
436 wxString
wxComboBox::GetValue() const
438 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
439 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
443 void wxComboBox::SetValue( const wxString
& value
)
445 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
447 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
448 wxString tmp
= wxT("");
449 if (!value
.IsNull()) tmp
= value
;
450 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
453 void wxComboBox::Copy()
455 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
457 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
458 #if (GTK_MINOR_VERSION > 0)
459 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
461 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
465 void wxComboBox::Cut()
467 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
469 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
470 #if (GTK_MINOR_VERSION > 0)
471 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
473 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
477 void wxComboBox::Paste()
479 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
481 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
482 #if (GTK_MINOR_VERSION > 0)
483 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
485 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
489 void wxComboBox::SetInsertionPoint( long pos
)
491 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
493 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
494 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
497 void wxComboBox::SetInsertionPointEnd()
499 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
501 SetInsertionPoint( -1 );
504 long wxComboBox::GetInsertionPoint() const
506 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
507 return (long) GTK_EDITABLE(entry
)->current_pos
;
510 long wxComboBox::GetLastPosition() const
512 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
513 int pos
= GTK_ENTRY(entry
)->text_length
;
517 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
519 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
520 // FIXME: not quite sure how to do this method right in multibyte mode
522 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
523 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
524 if (value
.IsNull()) return;
526 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
529 void wxComboBox::Remove(long from
, long to
)
531 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
533 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
534 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
537 void wxComboBox::SetSelection( long from
, long to
)
539 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
540 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
543 void wxComboBox::SetEditable( bool editable
)
545 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
546 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
549 void wxComboBox::OnChar( wxKeyEvent
&event
)
551 if ( event
.KeyCode() == WXK_RETURN
)
553 wxString value
= GetValue();
557 // make Enter generate "selected" event if there is only one item
558 // in the combobox - without it, it's impossible to select it at
560 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
562 event
.SetString( value
);
563 event
.SetEventObject( this );
564 GetEventHandler()->ProcessEvent( event
);
568 // add the item to the list if it's not there yet
569 if ( FindString(value
) == wxNOT_FOUND
)
573 // and generate the selected event for it
574 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
575 event
.SetInt( Number() - 1 );
576 event
.SetString( value
);
577 event
.SetEventObject( this );
578 GetEventHandler()->ProcessEvent( event
);
580 //else: do nothing, this will open the listbox
587 void wxComboBox::DisableEvents()
589 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
590 GList
*child
= list
->children
;
593 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
594 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
600 void wxComboBox::EnableEvents()
602 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
603 GList
*child
= list
->children
;
606 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
607 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
613 void wxComboBox::OnSize( wxSizeEvent
&event
)
619 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
621 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
622 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
626 void wxComboBox::ApplyWidgetStyle()
630 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
631 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
632 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
634 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
635 GList
*child
= list
->children
;
638 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
640 GtkBin
*bin
= GTK_BIN(child
->data
);
641 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
647 GtkWidget
* wxComboBox::GetConnectWidget()
649 return GTK_COMBO(m_widget
)->entry
;
652 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
654 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
655 (window
== GTK_COMBO(m_widget
)->button
->window
) );
658 wxSize
wxComboBox::DoGetBestSize() const
660 return wxSize(100, 26);