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
23 #include "wx/gtk/private.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
43 gtk_combo_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
45 if (g_isIdle
) wxapp_install_idle_handler();
47 if (!combo
->m_hasVMT
) return;
49 if (g_blockEventsOnDrag
) return;
51 if (combo
->m_alreadySent
)
53 combo
->m_alreadySent
= FALSE
;
57 combo
->m_alreadySent
= TRUE
;
59 int curSelection
= combo
->GetSelection();
61 if (combo
->m_prevSelection
!= curSelection
)
63 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
64 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
67 combo
->m_prevSelection
= curSelection
;
69 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
70 event
.SetInt( curSelection
);
71 event
.SetString( combo
->GetStringSelection() );
72 event
.SetEventObject( combo
);
74 combo
->GetEventHandler()->ProcessEvent( event
);
77 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
82 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
84 if (g_isIdle
) wxapp_install_idle_handler();
86 if (!combo
->m_hasVMT
) return;
88 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
89 event
.SetString( combo
->GetValue() );
90 event
.SetEventObject( combo
);
91 combo
->GetEventHandler()->ProcessEvent( event
);
94 //-----------------------------------------------------------------------------
96 //-----------------------------------------------------------------------------
98 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
100 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
101 EVT_SIZE(wxComboBox::OnSize
)
102 EVT_CHAR(wxComboBox::OnChar
)
105 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
106 const wxPoint
& pos
, const wxSize
& size
,
107 int n
, const wxString choices
[],
108 long style
, const wxValidator
& validator
,
109 const wxString
& name
)
111 m_alreadySent
= FALSE
;
113 m_acceptsFocus
= TRUE
;
116 if (!PreCreation( parent
, pos
, size
) ||
117 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
119 wxFAIL_MSG( wxT("wxComboBox creation failed") );
123 m_widget
= gtk_combo_new();
125 // make it more useable
126 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
128 // and case-sensitive
129 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
132 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
135 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
138 for (int i
= 0; i
< n
; i
++)
140 /* don't send first event, which GTK sends aways when
141 inserting the first item */
142 m_alreadySent
= TRUE
;
144 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
146 m_clientDataList
.Append( (wxObject
*)NULL
);
147 m_clientObjectList
.Append( (wxObject
*)NULL
);
149 gtk_container_add( GTK_CONTAINER(list
), list_item
);
151 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
152 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
154 gtk_widget_show( list_item
);
157 m_parent
->DoAddChild( this );
159 m_focusWidget
= GTK_COMBO(m_widget
)->entry
;
163 ConnectWidget( GTK_COMBO(m_widget
)->button
);
165 if (!value
.IsNull()) SetValue( value
);
167 if (style
& wxCB_READONLY
)
168 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget
)->entry
), FALSE
);
170 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
171 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
173 wxSize
size_best( DoGetBestSize() );
174 wxSize
new_size( size
);
175 if (new_size
.x
== -1)
176 new_size
.x
= size_best
.x
;
177 if (new_size
.y
== -1)
178 new_size
.y
= size_best
.y
;
179 if (new_size
.y
> size_best
.y
)
180 new_size
.y
= size_best
.y
;
181 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
183 SetSize( new_size
.x
, new_size
.y
);
185 // This is required for tool bar support
186 gtk_widget_set_usize( m_widget
, new_size
.x
, new_size
.y
);
190 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
) );
191 SetForegroundColour( parent
->GetForegroundColour() );
198 wxComboBox::~wxComboBox()
200 wxNode
*node
= m_clientObjectList
.First();
203 wxClientData
*cd
= (wxClientData
*)node
->Data();
207 m_clientObjectList
.Clear();
209 m_clientDataList
.Clear();
212 void wxComboBox::AppendCommon( const wxString
&item
)
214 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
216 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
218 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
220 gtk_container_add( GTK_CONTAINER(list
), list_item
);
222 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
223 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
225 if (GTK_WIDGET_REALIZED(m_widget
))
227 gtk_widget_realize( list_item
);
228 gtk_widget_realize( GTK_BIN(list_item
)->child
);
230 if (m_widgetStyle
) ApplyWidgetStyle();
233 gtk_widget_show( list_item
);
236 void wxComboBox::Append( const wxString
&item
)
238 m_clientDataList
.Append( (wxObject
*) NULL
);
239 m_clientObjectList
.Append( (wxObject
*) NULL
);
241 AppendCommon( item
);
244 void wxComboBox::Append( const wxString
&item
, void *clientData
)
246 m_clientDataList
.Append( (wxObject
*) clientData
);
247 m_clientObjectList
.Append( (wxObject
*)NULL
);
249 AppendCommon( item
);
252 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
254 m_clientDataList
.Append( (wxObject
*) NULL
);
255 m_clientObjectList
.Append( (wxObject
*) clientData
);
257 AppendCommon( item
);
260 void wxComboBox::SetClientData( int n
, void* clientData
)
262 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
264 wxNode
*node
= m_clientDataList
.Nth( n
);
267 node
->SetData( (wxObject
*) clientData
);
270 void* wxComboBox::GetClientData( int n
)
272 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
274 wxNode
*node
= m_clientDataList
.Nth( n
);
275 if (!node
) return NULL
;
280 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
282 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
284 wxNode
*node
= m_clientObjectList
.Nth( n
);
287 wxClientData
*cd
= (wxClientData
*) node
->Data();
290 node
->SetData( (wxObject
*) clientData
);
293 wxClientData
* wxComboBox::GetClientObject( int n
)
295 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
297 wxNode
*node
= m_clientObjectList
.Nth( n
);
298 if (!node
) return (wxClientData
*) NULL
;
300 return (wxClientData
*) node
->Data();
303 void wxComboBox::Clear()
305 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
307 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
308 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
310 wxNode
*node
= m_clientObjectList
.First();
313 wxClientData
*cd
= (wxClientData
*)node
->Data();
317 m_clientObjectList
.Clear();
319 m_clientDataList
.Clear();
322 void wxComboBox::Delete( int n
)
324 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
326 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
328 GList
*child
= g_list_nth( listbox
->children
, n
);
332 wxFAIL_MSG(wxT("wrong index"));
336 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
337 gtk_list_remove_items( listbox
, list
);
340 wxNode
*node
= m_clientObjectList
.Nth( n
);
343 wxClientData
*cd
= (wxClientData
*)node
->Data();
345 m_clientObjectList
.DeleteNode( node
);
348 node
= m_clientDataList
.Nth( n
);
351 m_clientDataList
.DeleteNode( node
);
355 int wxComboBox::FindString( const wxString
&item
)
357 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
359 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
361 GList
*child
= GTK_LIST(list
)->children
;
365 GtkBin
*bin
= GTK_BIN( child
->data
);
366 GtkLabel
*label
= GTK_LABEL( bin
->child
);
367 if (item
== wxString(label
->label
,*wxConvCurrent
))
376 int wxComboBox::GetSelection() const
378 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
380 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
382 GList
*selection
= GTK_LIST(list
)->selection
;
385 GList
*child
= GTK_LIST(list
)->children
;
389 if (child
->data
== selection
->data
) return count
;
398 wxString
wxComboBox::GetString( int n
) const
400 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
402 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
405 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
408 GtkBin
*bin
= GTK_BIN( child
->data
);
409 GtkLabel
*label
= GTK_LABEL( bin
->child
);
411 str
= wxGTK_CONV_BACK( gtk_label_get_text( label
) );
413 str
= wxString( label
->label
);
418 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
424 wxString
wxComboBox::GetStringSelection() const
426 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
428 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
430 GList
*selection
= GTK_LIST(list
)->selection
;
433 GtkBin
*bin
= GTK_BIN( selection
->data
);
434 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
438 wxFAIL_MSG( wxT("wxComboBox: no selection") );
443 int wxComboBox::Number() const
445 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
447 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
449 GList
*child
= GTK_LIST(list
)->children
;
451 while (child
) { count
++; child
= child
->next
; }
455 void wxComboBox::SetSelection( int n
)
457 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
461 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
462 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
463 gtk_list_select_item( GTK_LIST(list
), n
);
469 void wxComboBox::SetStringSelection( const wxString
&string
)
471 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
473 int res
= FindString( string
);
474 if (res
== -1) return;
478 wxString
wxComboBox::GetValue() const
480 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
481 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
484 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
487 printf( "%d ", (int) (c
) );
495 void wxComboBox::SetValue( const wxString
& value
)
497 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
499 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
500 wxString tmp
= wxT("");
501 if (!value
.IsNull()) tmp
= value
;
502 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
505 void wxComboBox::Copy()
507 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
509 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
510 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
513 void wxComboBox::Cut()
515 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
517 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
518 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
521 void wxComboBox::Paste()
523 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
525 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
526 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
529 void wxComboBox::SetInsertionPoint( long pos
)
531 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
533 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
534 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
537 void wxComboBox::SetInsertionPointEnd()
539 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
541 SetInsertionPoint( -1 );
544 long wxComboBox::GetInsertionPoint() const
546 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
549 long wxComboBox::GetLastPosition() const
551 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
552 int pos
= GTK_ENTRY(entry
)->text_length
;
556 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
558 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
560 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
561 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
562 if (value
.IsNull()) return;
566 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
567 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
569 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
573 void wxComboBox::Remove(long from
, long to
)
575 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
577 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
578 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
581 void wxComboBox::SetSelection( long from
, long to
)
583 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
584 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
587 void wxComboBox::SetEditable( bool editable
)
589 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
590 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
593 void wxComboBox::OnChar( wxKeyEvent
&event
)
595 if ( event
.KeyCode() == WXK_RETURN
)
597 wxString value
= GetValue();
601 // make Enter generate "selected" event if there is only one item
602 // in the combobox - without it, it's impossible to select it at
604 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
606 event
.SetString( value
);
607 event
.SetEventObject( this );
608 GetEventHandler()->ProcessEvent( event
);
612 // add the item to the list if it's not there yet
613 if ( FindString(value
) == wxNOT_FOUND
)
616 SetStringSelection(value
);
618 // and generate the selected event for it
619 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
620 event
.SetInt( Number() - 1 );
621 event
.SetString( value
);
622 event
.SetEventObject( this );
623 GetEventHandler()->ProcessEvent( event
);
626 // This will invoke the dialog default action, such
627 // as the clicking the default button.
629 wxWindow
*top_frame
= m_parent
;
630 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
631 top_frame
= top_frame
->GetParent();
633 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
635 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
637 if (window
->default_widget
)
639 gtk_widget_activate (window
->default_widget
);
651 void wxComboBox::DisableEvents()
653 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
654 GList
*child
= list
->children
;
657 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
658 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
664 void wxComboBox::EnableEvents()
666 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
667 GList
*child
= list
->children
;
670 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
671 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
677 void wxComboBox::OnSize( wxSizeEvent
&event
)
683 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
685 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
686 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
690 void wxComboBox::ApplyWidgetStyle()
694 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
695 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
696 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
698 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
699 GList
*child
= list
->children
;
702 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
704 GtkBin
*bin
= GTK_BIN(child
->data
);
705 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
711 GtkWidget
* wxComboBox::GetConnectWidget()
713 return GTK_COMBO(m_widget
)->entry
;
716 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
718 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
719 (window
== GTK_COMBO(m_widget
)->button
->window
) );
722 wxSize
wxComboBox::DoGetBestSize() const
724 wxSize
ret( wxControl::DoGetBestSize() );
726 // we know better our horizontal extent: it depends on the longest string
731 GdkFont
*font
= m_font
.GetInternalFont();
734 size_t count
= Number();
735 for ( size_t n
= 0; n
< count
; n
++ )
737 width
= (wxCoord
)gdk_string_width(font
, wxGTK_CONV( GetString(n
) ) );
743 // empty combobox should have some reasonable default size too