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 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
118 for (int i
= 0; i
< n
; i
++)
120 /* don't send first event, which GTK sends aways when
121 inserting the first item */
122 m_alreadySent
= TRUE
;
124 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
].mbc_str() );
126 m_clientDataList
.Append( (wxObject
*)NULL
);
127 m_clientObjectList
.Append( (wxObject
*)NULL
);
129 gtk_container_add( GTK_CONTAINER(list
), list_item
);
131 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
132 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
134 gtk_widget_show( list_item
);
137 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 wxSize
size_best( DoGetBestSize() );
154 wxSize
new_size( size
);
155 if (new_size
.x
== -1)
156 new_size
.x
= size_best
.x
;
157 if (new_size
.y
== -1)
158 new_size
.y
= size_best
.y
;
159 if (new_size
.y
> size_best
.y
)
160 new_size
.y
= size_best
.y
;
161 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
162 SetSize( new_size
.x
, new_size
.y
);
164 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
165 SetForegroundColour( parent
->GetForegroundColour() );
172 wxComboBox::~wxComboBox()
174 wxNode
*node
= m_clientObjectList
.First();
177 wxClientData
*cd
= (wxClientData
*)node
->Data();
181 m_clientObjectList
.Clear();
183 m_clientDataList
.Clear();
186 void wxComboBox::AppendCommon( const wxString
&item
)
188 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
190 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
192 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
194 gtk_container_add( GTK_CONTAINER(list
), list_item
);
196 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
197 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
199 if (GTK_WIDGET_REALIZED(m_widget
))
201 gtk_widget_realize( list_item
);
202 gtk_widget_realize( GTK_BIN(list_item
)->child
);
204 if (m_widgetStyle
) ApplyWidgetStyle();
207 gtk_widget_show( list_item
);
210 void wxComboBox::Append( const wxString
&item
)
212 m_clientDataList
.Append( (wxObject
*) NULL
);
213 m_clientObjectList
.Append( (wxObject
*) NULL
);
215 AppendCommon( item
);
218 void wxComboBox::Append( const wxString
&item
, void *clientData
)
220 m_clientDataList
.Append( (wxObject
*) clientData
);
221 m_clientObjectList
.Append( (wxObject
*)NULL
);
223 AppendCommon( item
);
226 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
228 m_clientDataList
.Append( (wxObject
*) NULL
);
229 m_clientObjectList
.Append( (wxObject
*) clientData
);
231 AppendCommon( item
);
234 void wxComboBox::SetClientData( int n
, void* clientData
)
236 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
238 wxNode
*node
= m_clientDataList
.Nth( n
);
241 node
->SetData( (wxObject
*) clientData
);
244 void* wxComboBox::GetClientData( int n
)
246 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
248 wxNode
*node
= m_clientDataList
.Nth( n
);
249 if (!node
) return NULL
;
254 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
256 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
258 wxNode
*node
= m_clientObjectList
.Nth( n
);
261 wxClientData
*cd
= (wxClientData
*) node
->Data();
264 node
->SetData( (wxObject
*) clientData
);
267 wxClientData
* wxComboBox::GetClientObject( int n
)
269 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
271 wxNode
*node
= m_clientDataList
.Nth( n
);
272 if (!node
) return (wxClientData
*) NULL
;
274 return (wxClientData
*) node
->Data();
277 void wxComboBox::Clear()
279 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
281 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
282 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
284 wxNode
*node
= m_clientObjectList
.First();
287 wxClientData
*cd
= (wxClientData
*)node
->Data();
291 m_clientObjectList
.Clear();
293 m_clientDataList
.Clear();
296 void wxComboBox::Delete( int n
)
298 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
300 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
302 GList
*child
= g_list_nth( listbox
->children
, n
);
306 wxFAIL_MSG(wxT("wrong index"));
310 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
311 gtk_list_remove_items( listbox
, list
);
314 wxNode
*node
= m_clientObjectList
.Nth( n
);
317 wxClientData
*cd
= (wxClientData
*)node
->Data();
319 m_clientObjectList
.DeleteNode( node
);
322 node
= m_clientDataList
.Nth( n
);
325 m_clientDataList
.DeleteNode( node
);
329 int wxComboBox::FindString( const wxString
&item
)
331 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
333 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
335 GList
*child
= GTK_LIST(list
)->children
;
339 GtkBin
*bin
= GTK_BIN( child
->data
);
340 GtkLabel
*label
= GTK_LABEL( bin
->child
);
341 if (item
== wxString(label
->label
,*wxConvCurrent
))
350 int wxComboBox::GetSelection() const
352 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
354 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
356 GList
*selection
= GTK_LIST(list
)->selection
;
359 GList
*child
= GTK_LIST(list
)->children
;
363 if (child
->data
== selection
->data
) return count
;
372 wxString
wxComboBox::GetString( int n
) const
374 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
376 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
379 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
382 GtkBin
*bin
= GTK_BIN( child
->data
);
383 GtkLabel
*label
= GTK_LABEL( bin
->child
);
384 str
= wxString(label
->label
,*wxConvCurrent
);
388 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
394 wxString
wxComboBox::GetStringSelection() const
396 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
398 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
400 GList
*selection
= GTK_LIST(list
)->selection
;
403 GtkBin
*bin
= GTK_BIN( selection
->data
);
404 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
408 wxFAIL_MSG( wxT("wxComboBox: no selection") );
413 int wxComboBox::Number() const
415 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
417 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
419 GList
*child
= GTK_LIST(list
)->children
;
421 while (child
) { count
++; child
= child
->next
; }
425 void wxComboBox::SetSelection( int n
)
427 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
431 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
432 gtk_list_select_item( GTK_LIST(list
), n
);
437 void wxComboBox::SetStringSelection( const wxString
&string
)
439 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
441 int res
= FindString( string
);
442 if (res
== -1) return;
446 wxString
wxComboBox::GetValue() const
448 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
449 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
453 void wxComboBox::SetValue( const wxString
& value
)
455 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
457 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
458 wxString tmp
= wxT("");
459 if (!value
.IsNull()) tmp
= value
;
460 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
463 void wxComboBox::Copy()
465 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
467 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
468 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
469 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
471 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
475 void wxComboBox::Cut()
477 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
479 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
480 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
481 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
483 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
487 void wxComboBox::Paste()
489 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
491 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
492 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
493 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
495 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
499 void wxComboBox::SetInsertionPoint( long pos
)
501 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
503 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
504 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
507 void wxComboBox::SetInsertionPointEnd()
509 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
511 SetInsertionPoint( -1 );
514 long wxComboBox::GetInsertionPoint() const
516 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
517 return (long) GTK_EDITABLE(entry
)->current_pos
;
520 long wxComboBox::GetLastPosition() const
522 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
523 int pos
= GTK_ENTRY(entry
)->text_length
;
527 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
529 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
530 // FIXME: not quite sure how to do this method right in multibyte mode
532 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
533 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
534 if (value
.IsNull()) return;
536 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
539 void wxComboBox::Remove(long from
, long to
)
541 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
543 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
544 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
547 void wxComboBox::SetSelection( long from
, long to
)
549 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
550 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
553 void wxComboBox::SetEditable( bool editable
)
555 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
556 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
559 void wxComboBox::OnChar( wxKeyEvent
&event
)
561 if ( event
.KeyCode() == WXK_RETURN
)
563 wxString value
= GetValue();
567 // make Enter generate "selected" event if there is only one item
568 // in the combobox - without it, it's impossible to select it at
570 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
572 event
.SetString( value
);
573 event
.SetEventObject( this );
574 GetEventHandler()->ProcessEvent( event
);
578 // add the item to the list if it's not there yet
579 if ( FindString(value
) == wxNOT_FOUND
)
583 // and generate the selected event for it
584 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
585 event
.SetInt( Number() - 1 );
586 event
.SetString( value
);
587 event
.SetEventObject( this );
588 GetEventHandler()->ProcessEvent( event
);
590 //else: do nothing, this will open the listbox
597 void wxComboBox::DisableEvents()
599 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
600 GList
*child
= list
->children
;
603 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
604 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
610 void wxComboBox::EnableEvents()
612 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
613 GList
*child
= list
->children
;
616 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
617 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
623 void wxComboBox::OnSize( wxSizeEvent
&event
)
629 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
631 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
632 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
636 void wxComboBox::ApplyWidgetStyle()
640 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
641 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
642 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
644 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
645 GList
*child
= list
->children
;
648 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
650 GtkBin
*bin
= GTK_BIN(child
->data
);
651 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
657 GtkWidget
* wxComboBox::GetConnectWidget()
659 return GTK_COMBO(m_widget
)->entry
;
662 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
664 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
665 (window
== GTK_COMBO(m_widget
)->button
->window
) );
668 wxSize
wxComboBox::DoGetBestSize() const
670 wxSize
ret( wxControl::DoGetBestSize() );
671 if (ret
.x
< 100) ret
.x
= 100;