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"
21 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
44 gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
46 if (g_isIdle
) wxapp_install_idle_handler();
48 if (!combo
->m_hasVMT
) return;
50 if (g_blockEventsOnDrag
) return;
52 if (combo
->m_alreadySent
)
54 combo
->m_alreadySent
= FALSE
;
58 combo
->m_alreadySent
= TRUE
;
60 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
61 event
.SetInt( combo
->GetSelection() );
62 event
.SetString( combo
->GetStringSelection() );
63 event
.SetEventObject( combo
);
65 combo
->GetEventHandler()->ProcessEvent( event
);
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
73 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
75 if (g_isIdle
) wxapp_install_idle_handler();
77 if (!combo
->m_hasVMT
) return;
79 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
80 event
.SetString( combo
->GetValue() );
81 event
.SetEventObject( combo
);
82 combo
->GetEventHandler()->ProcessEvent( event
);
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
89 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
91 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
92 EVT_SIZE(wxComboBox::OnSize
)
93 EVT_CHAR(wxComboBox::OnChar
)
96 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
97 const wxPoint
& pos
, const wxSize
& size
,
98 int n
, const wxString choices
[],
99 long style
, const wxValidator
& validator
,
100 const wxString
& name
)
102 m_alreadySent
= FALSE
;
104 m_acceptsFocus
= TRUE
;
106 if (!PreCreation( parent
, pos
, size
) ||
107 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
109 wxFAIL_MSG( wxT("wxComboBox creation failed") );
113 m_widget
= gtk_combo_new();
115 // make it more useable
116 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
118 // and case-sensitive
119 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
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 wxSize
size_best( DoGetBestSize() );
158 wxSize
new_size( size
);
159 if (new_size
.x
== -1)
160 new_size
.x
= size_best
.x
;
161 if (new_size
.y
== -1)
162 new_size
.y
= size_best
.y
;
163 if (new_size
.y
> size_best
.y
)
164 new_size
.y
= size_best
.y
;
165 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
166 SetSize( new_size
.x
, new_size
.y
);
168 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW
) );
169 SetForegroundColour( parent
->GetForegroundColour() );
176 wxComboBox::~wxComboBox()
178 wxNode
*node
= m_clientObjectList
.First();
181 wxClientData
*cd
= (wxClientData
*)node
->Data();
185 m_clientObjectList
.Clear();
187 m_clientDataList
.Clear();
190 void wxComboBox::AppendCommon( const wxString
&item
)
192 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
194 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
196 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
198 gtk_container_add( GTK_CONTAINER(list
), list_item
);
200 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
201 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
203 if (GTK_WIDGET_REALIZED(m_widget
))
205 gtk_widget_realize( list_item
);
206 gtk_widget_realize( GTK_BIN(list_item
)->child
);
208 if (m_widgetStyle
) ApplyWidgetStyle();
211 gtk_widget_show( list_item
);
214 void wxComboBox::Append( const wxString
&item
)
216 m_clientDataList
.Append( (wxObject
*) NULL
);
217 m_clientObjectList
.Append( (wxObject
*) NULL
);
219 AppendCommon( item
);
222 void wxComboBox::Append( const wxString
&item
, void *clientData
)
224 m_clientDataList
.Append( (wxObject
*) clientData
);
225 m_clientObjectList
.Append( (wxObject
*)NULL
);
227 AppendCommon( item
);
230 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
232 m_clientDataList
.Append( (wxObject
*) NULL
);
233 m_clientObjectList
.Append( (wxObject
*) clientData
);
235 AppendCommon( item
);
238 void wxComboBox::SetClientData( int n
, void* clientData
)
240 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
242 wxNode
*node
= m_clientDataList
.Nth( n
);
245 node
->SetData( (wxObject
*) clientData
);
248 void* wxComboBox::GetClientData( int n
)
250 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
252 wxNode
*node
= m_clientDataList
.Nth( n
);
253 if (!node
) return NULL
;
258 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
260 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
262 wxNode
*node
= m_clientObjectList
.Nth( n
);
265 wxClientData
*cd
= (wxClientData
*) node
->Data();
268 node
->SetData( (wxObject
*) clientData
);
271 wxClientData
* wxComboBox::GetClientObject( int n
)
273 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
275 wxNode
*node
= m_clientDataList
.Nth( n
);
276 if (!node
) return (wxClientData
*) NULL
;
278 return (wxClientData
*) node
->Data();
281 void wxComboBox::Clear()
283 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
285 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
286 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
288 wxNode
*node
= m_clientObjectList
.First();
291 wxClientData
*cd
= (wxClientData
*)node
->Data();
295 m_clientObjectList
.Clear();
297 m_clientDataList
.Clear();
300 void wxComboBox::Delete( int n
)
302 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
304 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
306 GList
*child
= g_list_nth( listbox
->children
, n
);
310 wxFAIL_MSG(wxT("wrong index"));
314 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
315 gtk_list_remove_items( listbox
, list
);
318 wxNode
*node
= m_clientObjectList
.Nth( n
);
321 wxClientData
*cd
= (wxClientData
*)node
->Data();
323 m_clientObjectList
.DeleteNode( node
);
326 node
= m_clientDataList
.Nth( n
);
329 m_clientDataList
.DeleteNode( node
);
333 int wxComboBox::FindString( const wxString
&item
)
335 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
337 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
339 GList
*child
= GTK_LIST(list
)->children
;
343 GtkBin
*bin
= GTK_BIN( child
->data
);
344 GtkLabel
*label
= GTK_LABEL( bin
->child
);
345 if (item
== wxString(label
->label
,*wxConvCurrent
))
354 int wxComboBox::GetSelection() const
356 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
358 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
360 GList
*selection
= GTK_LIST(list
)->selection
;
363 GList
*child
= GTK_LIST(list
)->children
;
367 if (child
->data
== selection
->data
) return count
;
376 wxString
wxComboBox::GetString( int n
) const
378 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
380 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
383 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
386 GtkBin
*bin
= GTK_BIN( child
->data
);
387 GtkLabel
*label
= GTK_LABEL( bin
->child
);
388 str
= wxString(label
->label
,*wxConvCurrent
);
392 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
398 wxString
wxComboBox::GetStringSelection() const
400 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
402 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
404 GList
*selection
= GTK_LIST(list
)->selection
;
407 GtkBin
*bin
= GTK_BIN( selection
->data
);
408 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
412 wxFAIL_MSG( wxT("wxComboBox: no selection") );
417 int wxComboBox::Number() const
419 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
421 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
423 GList
*child
= GTK_LIST(list
)->children
;
425 while (child
) { count
++; child
= child
->next
; }
429 void wxComboBox::SetSelection( int n
)
431 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
435 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
436 gtk_list_select_item( GTK_LIST(list
), n
);
441 void wxComboBox::SetStringSelection( const wxString
&string
)
443 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
445 int res
= FindString( string
);
446 if (res
== -1) return;
450 wxString
wxComboBox::GetValue() const
452 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
453 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
457 void wxComboBox::SetValue( const wxString
& value
)
459 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
461 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
462 wxString tmp
= wxT("");
463 if (!value
.IsNull()) tmp
= value
;
464 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
467 void wxComboBox::Copy()
469 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
471 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
472 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
473 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) );
475 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
), 0 );
479 void wxComboBox::Cut()
481 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
483 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
484 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
485 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) );
487 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
), 0 );
491 void wxComboBox::Paste()
493 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
495 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
496 #if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0)
497 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) );
499 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
), 0 );
503 void wxComboBox::SetInsertionPoint( long pos
)
505 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
507 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
508 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
511 void wxComboBox::SetInsertionPointEnd()
513 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
515 SetInsertionPoint( -1 );
518 long wxComboBox::GetInsertionPoint() const
520 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
521 return (long) GTK_EDITABLE(entry
)->current_pos
;
524 long wxComboBox::GetLastPosition() const
526 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
527 int pos
= GTK_ENTRY(entry
)->text_length
;
531 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
533 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
534 // FIXME: not quite sure how to do this method right in multibyte mode
536 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
537 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
538 if (value
.IsNull()) return;
540 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
543 void wxComboBox::Remove(long from
, long to
)
545 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
547 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
548 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
551 void wxComboBox::SetSelection( long from
, long to
)
553 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
554 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
557 void wxComboBox::SetEditable( bool editable
)
559 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
560 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
563 void wxComboBox::OnChar( wxKeyEvent
&event
)
565 if ( event
.KeyCode() == WXK_RETURN
)
567 wxString value
= GetValue();
571 // make Enter generate "selected" event if there is only one item
572 // in the combobox - without it, it's impossible to select it at
574 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
576 event
.SetString( value
);
577 event
.SetEventObject( this );
578 GetEventHandler()->ProcessEvent( event
);
582 // add the item to the list if it's not there yet
583 if ( FindString(value
) == wxNOT_FOUND
)
586 SetStringSelection(value
);
588 // and generate the selected event for it
589 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
590 event
.SetInt( Number() - 1 );
591 event
.SetString( value
);
592 event
.SetEventObject( this );
593 GetEventHandler()->ProcessEvent( event
);
595 //else: do nothing, this will open the listbox
602 void wxComboBox::DisableEvents()
604 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
605 GList
*child
= list
->children
;
608 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
609 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
615 void wxComboBox::EnableEvents()
617 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
618 GList
*child
= list
->children
;
621 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
622 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
628 void wxComboBox::OnSize( wxSizeEvent
&event
)
634 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
636 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
637 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
641 void wxComboBox::ApplyWidgetStyle()
645 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
646 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
647 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
649 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
650 GList
*child
= list
->children
;
653 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
655 GtkBin
*bin
= GTK_BIN(child
->data
);
656 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
662 GtkWidget
* wxComboBox::GetConnectWidget()
664 return GTK_COMBO(m_widget
)->entry
;
667 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
669 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
670 (window
== GTK_COMBO(m_widget
)->button
->window
) );
673 wxSize
wxComboBox::DoGetBestSize() const
675 wxSize
ret( wxControl::DoGetBestSize() );
677 // we know better our horizontal extent: it depends on the longest string
682 GdkFont
*font
= m_font
.GetInternalFont();
685 size_t count
= Number();
686 for ( size_t n
= 0; n
< count
; n
++ )
688 width
= (wxCoord
)gdk_string_width(font
, GetString(n
).mbc_str());
694 // empty combobox should have some reasonable default size too