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"
15 #include "wx/settings.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern bool g_blockEventsOnDrag
;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
40 gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
42 if (g_isIdle
) wxapp_install_idle_handler();
44 if (!combo
->m_hasVMT
) return;
46 if (g_blockEventsOnDrag
) return;
48 if (combo
->m_alreadySent
)
50 combo
->m_alreadySent
= FALSE
;
54 combo
->m_alreadySent
= TRUE
;
56 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
57 event
.SetInt( combo
->GetSelection() );
58 event
.SetString( combo
->GetStringSelection() );
59 event
.SetEventObject( combo
);
61 combo
->GetEventHandler()->ProcessEvent( event
);
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
69 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
71 if (g_isIdle
) wxapp_install_idle_handler();
73 if (!combo
->m_hasVMT
) return;
75 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
76 event
.SetString( combo
->GetValue() );
77 event
.SetEventObject( combo
);
78 combo
->GetEventHandler()->ProcessEvent( event
);
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
85 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
87 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
88 EVT_SIZE(wxComboBox::OnSize
)
89 EVT_CHAR(wxComboBox::OnChar
)
92 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
93 const wxPoint
& pos
, const wxSize
& size
,
94 int n
, const wxString choices
[],
95 long style
, const wxValidator
& validator
,
96 const wxString
& name
)
98 m_alreadySent
= FALSE
;
100 m_acceptsFocus
= TRUE
;
102 PreCreation( parent
, id
, pos
, size
, style
, name
);
104 SetValidator( validator
);
106 m_widget
= gtk_combo_new();
108 // make it more useable
109 gtk_combo_set_use_arrows_always(GTK_COMBO(m_widget
), TRUE
);
111 wxSize newSize
= size
;
116 SetSize( newSize
.x
, newSize
.y
);
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_clientDataList
.First();
167 wxClientData
*cd
= (wxClientData
*)node
->Data();
171 m_clientDataList
.Clear();
174 void wxComboBox::AppendCommon( const wxString
&item
)
176 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
178 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
180 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
182 gtk_container_add( GTK_CONTAINER(list
), list_item
);
184 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
185 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
187 if (GTK_WIDGET_REALIZED(m_widget
))
189 gtk_widget_realize( list_item
);
190 gtk_widget_realize( GTK_BIN(list_item
)->child
);
192 if (m_widgetStyle
) ApplyWidgetStyle();
195 gtk_widget_show( list_item
);
198 void wxComboBox::Append( const wxString
&item
)
200 m_clientDataList
.Append( (wxObject
*) NULL
);
201 m_clientObjectList
.Append( (wxObject
*) NULL
);
203 AppendCommon( item
);
206 void wxComboBox::Append( const wxString
&item
, void *clientData
)
208 m_clientDataList
.Append( (wxObject
*) clientData
);
209 m_clientObjectList
.Append( (wxObject
*)NULL
);
211 AppendCommon( item
);
214 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
216 m_clientDataList
.Append( (wxObject
*) NULL
);
217 m_clientObjectList
.Append( (wxObject
*) clientData
);
219 AppendCommon( item
);
222 void wxComboBox::SetClientData( int n
, void* clientData
)
224 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
226 wxNode
*node
= m_clientDataList
.Nth( n
);
229 node
->SetData( (wxObject
*) clientData
);
232 void* wxComboBox::GetClientData( int n
)
234 wxCHECK_MSG( m_widget
!= NULL
, NULL
, _T("invalid combobox") );
236 wxNode
*node
= m_clientDataList
.Nth( n
);
237 if (!node
) return NULL
;
242 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
244 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
246 wxNode
*node
= m_clientObjectList
.Nth( n
);
249 wxClientData
*cd
= (wxClientData
*) node
->Data();
252 node
->SetData( (wxObject
*) clientData
);
255 wxClientData
* wxComboBox::GetClientObject( int n
)
257 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, _T("invalid combobox") );
259 wxNode
*node
= m_clientDataList
.Nth( n
);
260 if (!node
) return (wxClientData
*) NULL
;
262 return (wxClientData
*) node
->Data();
265 void wxComboBox::Clear()
267 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
269 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
270 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
272 wxNode
*node
= m_clientObjectList
.First();
275 wxClientData
*cd
= (wxClientData
*)node
->Data();
279 m_clientObjectList
.Clear();
281 m_clientDataList
.Clear();
284 void wxComboBox::Delete( int n
)
286 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
288 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
290 GList
*child
= g_list_nth( listbox
->children
, n
);
294 wxFAIL_MSG(_T("wrong index"));
298 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
299 gtk_list_remove_items( listbox
, list
);
302 wxNode
*node
= m_clientObjectList
.Nth( n
);
305 wxClientData
*cd
= (wxClientData
*)node
->Data();
307 m_clientObjectList
.DeleteNode( node
);
310 node
= m_clientDataList
.Nth( n
);
313 m_clientDataList
.DeleteNode( node
);
317 int wxComboBox::FindString( const wxString
&item
)
319 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid combobox") );
321 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
323 GList
*child
= GTK_LIST(list
)->children
;
327 GtkBin
*bin
= GTK_BIN( child
->data
);
328 GtkLabel
*label
= GTK_LABEL( bin
->child
);
329 if (item
== wxString(label
->label
,*wxConvCurrent
))
338 int wxComboBox::GetSelection() const
340 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid combobox") );
342 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
344 GList
*selection
= GTK_LIST(list
)->selection
;
347 GList
*child
= GTK_LIST(list
)->children
;
351 if (child
->data
== selection
->data
) return count
;
357 wxFAIL_MSG( _T("wxComboBox: no selection") );
362 wxString
wxComboBox::GetString( int n
) const
364 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("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( _T("wxComboBox: wrong index") );
384 wxString
wxComboBox::GetStringSelection() const
386 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("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( _T("wxComboBox: no selection") );
403 int wxComboBox::Number() const
405 wxCHECK_MSG( m_widget
!= NULL
, 0, _T("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
, _T("invalid combobox") );
419 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
420 gtk_list_select_item( GTK_LIST(list
), n
);
423 void wxComboBox::SetStringSelection( const wxString
&string
)
425 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
427 int res
= FindString( string
);
428 if (res
== -1) return;
432 wxString
wxComboBox::GetValue() const
434 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
435 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
439 void wxComboBox::SetValue( const wxString
& value
)
441 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
443 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
444 wxString tmp
= _T("");
445 if (!value
.IsNull()) tmp
= value
;
446 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
449 void wxComboBox::Copy()
451 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
453 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
454 #if (GTK_MINOR_VERSION > 0)
455 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
457 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
461 void wxComboBox::Cut()
463 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
465 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
466 #if (GTK_MINOR_VERSION > 0)
467 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
469 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
473 void wxComboBox::Paste()
475 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
477 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
478 #if (GTK_MINOR_VERSION > 0)
479 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
481 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
485 void wxComboBox::SetInsertionPoint( long pos
)
487 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
489 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
490 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
493 void wxComboBox::SetInsertionPointEnd()
495 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
497 SetInsertionPoint( -1 );
500 long wxComboBox::GetInsertionPoint() const
502 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
503 return (long) GTK_EDITABLE(entry
)->current_pos
;
506 long wxComboBox::GetLastPosition() const
508 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
509 int pos
= GTK_ENTRY(entry
)->text_length
;
513 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
515 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
516 // FIXME: not quite sure how to do this method right in multibyte mode
518 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
519 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
520 if (value
.IsNull()) return;
522 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
525 void wxComboBox::Remove(long from
, long to
)
527 wxCHECK_RET( m_widget
!= NULL
, _T("invalid combobox") );
529 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
530 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
533 void wxComboBox::SetSelection( long from
, long to
)
535 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
536 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
539 void wxComboBox::SetEditable( bool editable
)
541 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
542 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
545 void wxComboBox::OnChar( wxKeyEvent
&event
)
547 if ( event
.KeyCode() == WXK_RETURN
)
549 wxString value
= GetValue();
553 // make Enter generate "selected" event if there is only one item
554 // in the combobox - without it, it's impossible to select it at
556 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
558 event
.SetString( value
);
559 event
.SetEventObject( this );
560 GetEventHandler()->ProcessEvent( event
);
564 // add the item to the list if it's not there yet
565 if ( FindString(value
) == wxNOT_FOUND
)
569 // and generate the selected event for it
570 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
571 event
.SetInt( Number() - 1 );
572 event
.SetString( value
);
573 event
.SetEventObject( this );
574 GetEventHandler()->ProcessEvent( event
);
576 //else: do nothing, this will open the listbox
583 void wxComboBox::OnSize( wxSizeEvent
&event
)
590 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
592 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
593 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
596 void wxComboBox::ApplyWidgetStyle()
600 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
601 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
602 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
604 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
605 GList
*child
= list
->children
;
608 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
610 GtkBin
*bin
= GTK_BIN(child
->data
);
611 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
617 GtkWidget
* wxComboBox::GetConnectWidget()
619 return GTK_COMBO(m_widget
)->entry
;
622 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
624 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
625 (window
== GTK_COMBO(m_widget
)->button
->window
) );