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
);
66 else if ((curSelection
>= 0) && (combo
->GetString(curSelection
) == combo
->GetValue()))
69 combo
->m_prevSelection
= curSelection
;
71 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
72 event
.SetInt( curSelection
);
73 event
.SetString( combo
->GetStringSelection() );
74 event
.SetEventObject( combo
);
76 combo
->GetEventHandler()->ProcessEvent( event
);
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
84 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
86 if (g_isIdle
) wxapp_install_idle_handler();
88 if (!combo
->m_hasVMT
) return;
90 // avoids double events when the GetValue = one of the selections
91 if (combo
->m_alreadySent
)
93 combo
->m_alreadySent
= FALSE
;
97 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
98 event
.SetString( combo
->GetValue() );
99 event
.SetEventObject( combo
);
100 combo
->GetEventHandler()->ProcessEvent( event
);
103 //-----------------------------------------------------------------------------
105 //-----------------------------------------------------------------------------
107 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
109 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
110 EVT_SIZE(wxComboBox::OnSize
)
111 EVT_CHAR(wxComboBox::OnChar
)
114 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
115 const wxPoint
& pos
, const wxSize
& size
,
116 int n
, const wxString choices
[],
117 long style
, const wxValidator
& validator
,
118 const wxString
& name
)
120 m_alreadySent
= FALSE
;
122 m_acceptsFocus
= TRUE
;
125 if (!PreCreation( parent
, pos
, size
) ||
126 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
128 wxFAIL_MSG( wxT("wxComboBox creation failed") );
132 m_widget
= gtk_combo_new();
134 // make it more useable
135 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
137 // and case-sensitive
138 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
140 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
143 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
146 for (int i
= 0; i
< n
; i
++)
148 /* don't send first event, which GTK sends aways when
149 inserting the first item */
150 m_alreadySent
= TRUE
;
152 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
154 m_clientDataList
.Append( (wxObject
*)NULL
);
155 m_clientObjectList
.Append( (wxObject
*)NULL
);
157 gtk_container_add( GTK_CONTAINER(list
), list_item
);
159 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
160 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
162 gtk_widget_show( list_item
);
165 m_parent
->DoAddChild( this );
167 m_focusWidget
= GTK_COMBO(m_widget
)->entry
;
171 ConnectWidget( GTK_COMBO(m_widget
)->button
);
173 if (!value
.IsNull()) SetValue( value
);
175 if (style
& wxCB_READONLY
)
176 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget
)->entry
), FALSE
);
178 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
179 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
181 wxSize
size_best( DoGetBestSize() );
182 wxSize
new_size( size
);
183 if (new_size
.x
== -1)
184 new_size
.x
= size_best
.x
;
185 if (new_size
.y
== -1)
186 new_size
.y
= size_best
.y
;
187 if (new_size
.y
> size_best
.y
)
188 new_size
.y
= size_best
.y
;
189 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
191 SetSize( new_size
.x
, new_size
.y
);
193 // This is required for tool bar support
194 gtk_widget_set_usize( m_widget
, new_size
.x
, new_size
.y
);
198 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
) );
199 SetForegroundColour( parent
->GetForegroundColour() );
206 wxComboBox::~wxComboBox()
208 wxNode
*node
= m_clientObjectList
.GetFirst();
211 wxClientData
*cd
= (wxClientData
*)node
->GetData();
213 node
= node
->GetNext();
215 m_clientObjectList
.Clear();
217 m_clientDataList
.Clear();
220 void wxComboBox::SetFocus()
224 // don't do anything if we already have focus
228 gtk_widget_grab_focus( m_focusWidget
);
231 void wxComboBox::AppendCommon( const wxString
&item
)
233 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
235 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
237 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
239 gtk_container_add( GTK_CONTAINER(list
), list_item
);
241 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
242 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
244 if (GTK_WIDGET_REALIZED(m_widget
))
246 gtk_widget_realize( list_item
);
247 gtk_widget_realize( GTK_BIN(list_item
)->child
);
249 if (m_widgetStyle
) ApplyWidgetStyle();
252 gtk_widget_show( list_item
);
255 void wxComboBox::Append( const wxString
&item
)
257 m_clientDataList
.Append( (wxObject
*) NULL
);
258 m_clientObjectList
.Append( (wxObject
*) NULL
);
260 AppendCommon( item
);
263 void wxComboBox::Append( const wxString
&item
, void *clientData
)
265 m_clientDataList
.Append( (wxObject
*) clientData
);
266 m_clientObjectList
.Append( (wxObject
*)NULL
);
268 AppendCommon( item
);
271 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
273 m_clientDataList
.Append( (wxObject
*) NULL
);
274 m_clientObjectList
.Append( (wxObject
*) clientData
);
276 AppendCommon( item
);
279 void wxComboBox::SetClientData( int n
, void* clientData
)
281 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
283 wxNode
*node
= m_clientDataList
.Item( n
);
286 node
->SetData( (wxObject
*) clientData
);
289 void* wxComboBox::GetClientData( int n
)
291 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
293 wxNode
*node
= m_clientDataList
.Item( n
);
294 if (!node
) return NULL
;
296 return node
->GetData();
299 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
301 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
303 wxNode
*node
= m_clientObjectList
.Item( n
);
306 wxClientData
*cd
= (wxClientData
*) node
->GetData();
309 node
->SetData( (wxObject
*) clientData
);
312 wxClientData
* wxComboBox::GetClientObject( int n
)
314 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
316 wxNode
*node
= m_clientObjectList
.Item( n
);
317 if (!node
) return (wxClientData
*) NULL
;
319 return (wxClientData
*) node
->GetData();
322 void wxComboBox::Clear()
324 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
326 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
327 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
329 wxNode
*node
= m_clientObjectList
.GetFirst();
332 wxClientData
*cd
= (wxClientData
*)node
->GetData();
334 node
= node
->GetNext();
336 m_clientObjectList
.Clear();
338 m_clientDataList
.Clear();
341 void wxComboBox::Delete( int n
)
343 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
345 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
347 GList
*child
= g_list_nth( listbox
->children
, n
);
351 wxFAIL_MSG(wxT("wrong index"));
355 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
356 gtk_list_remove_items( listbox
, list
);
359 wxNode
*node
= m_clientObjectList
.Item( n
);
362 wxClientData
*cd
= (wxClientData
*)node
->GetData();
364 m_clientObjectList
.DeleteNode( node
);
367 node
= m_clientDataList
.Item( n
);
370 m_clientDataList
.DeleteNode( node
);
374 int wxComboBox::FindString( const wxString
&item
)
376 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
378 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
380 GList
*child
= GTK_LIST(list
)->children
;
384 GtkBin
*bin
= GTK_BIN( child
->data
);
385 GtkLabel
*label
= GTK_LABEL( bin
->child
);
387 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
389 wxString
str( label
->label
);
401 int wxComboBox::GetSelection() const
403 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
405 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
407 GList
*selection
= GTK_LIST(list
)->selection
;
410 GList
*child
= GTK_LIST(list
)->children
;
414 if (child
->data
== selection
->data
) return count
;
423 wxString
wxComboBox::GetString( int n
) const
425 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
427 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
430 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
433 GtkBin
*bin
= GTK_BIN( child
->data
);
434 GtkLabel
*label
= GTK_LABEL( bin
->child
);
436 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
438 str
= wxString( label
->label
);
443 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
449 wxString
wxComboBox::GetStringSelection() const
451 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
453 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
455 GList
*selection
= GTK_LIST(list
)->selection
;
458 GtkBin
*bin
= GTK_BIN( selection
->data
);
459 GtkLabel
*label
= GTK_LABEL( bin
->child
);
461 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
463 wxString
tmp( label
->label
);
468 wxFAIL_MSG( wxT("wxComboBox: no selection") );
473 int wxComboBox::Number() const
475 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
477 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
479 GList
*child
= GTK_LIST(list
)->children
;
481 while (child
) { count
++; child
= child
->next
; }
485 void wxComboBox::SetSelection( int n
)
487 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
491 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
492 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
493 gtk_list_select_item( GTK_LIST(list
), n
);
499 void wxComboBox::SetStringSelection( const wxString
&string
)
501 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
503 int res
= FindString( string
);
504 if (res
== -1) return;
508 wxString
wxComboBox::GetValue() const
510 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
511 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
514 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
517 printf( "%d ", (int) (c
) );
525 void wxComboBox::SetValue( const wxString
& value
)
527 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
529 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
530 wxString tmp
= wxT("");
531 if (!value
.IsNull()) tmp
= value
;
532 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
535 void wxComboBox::Copy()
537 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
539 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
540 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
543 void wxComboBox::Cut()
545 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
547 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
548 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
551 void wxComboBox::Paste()
553 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
555 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
556 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
559 void wxComboBox::SetInsertionPoint( long pos
)
561 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
563 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
564 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
567 void wxComboBox::SetInsertionPointEnd()
569 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
571 SetInsertionPoint( -1 );
574 long wxComboBox::GetInsertionPoint() const
576 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
579 long wxComboBox::GetLastPosition() const
581 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
582 int pos
= GTK_ENTRY(entry
)->text_length
;
586 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
588 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
590 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
591 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
592 if (value
.IsNull()) return;
596 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
597 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
599 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
603 void wxComboBox::Remove(long from
, long to
)
605 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
607 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
608 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
611 void wxComboBox::SetSelection( long from
, long to
)
613 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
614 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
617 void wxComboBox::SetEditable( bool editable
)
619 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
620 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
623 void wxComboBox::OnChar( wxKeyEvent
&event
)
625 if ( event
.GetKeyCode() == WXK_RETURN
)
627 wxString value
= GetValue();
628 int selection
= GetSelection();
630 // note that gtk automatically selects an item if its in the list
631 // so you don't have to call FindString
632 if ((selection
>= 0) && (GetString(selection
) == value
))
634 // make Enter generate "selected" event if it equals an item
635 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
636 event
.SetInt( selection
);
637 event
.SetString( value
);
638 event
.SetEventObject( this );
639 GetEventHandler()->ProcessEvent( event
);
643 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, GetId());
644 event
.SetString(value
);
645 event
.SetInt(selection
);
646 event
.SetEventObject( this );
647 GetEventHandler()->ProcessEvent( event
);
655 void wxComboBox::DisableEvents()
657 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
658 GList
*child
= list
->children
;
661 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
662 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
668 void wxComboBox::EnableEvents()
670 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
671 GList
*child
= list
->children
;
674 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
675 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
681 void wxComboBox::OnSize( wxSizeEvent
&event
)
687 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
689 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
690 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
694 void wxComboBox::ApplyWidgetStyle()
698 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
699 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
700 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
702 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
703 GList
*child
= list
->children
;
706 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
708 GtkBin
*bin
= GTK_BIN(child
->data
);
709 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
715 GtkWidget
* wxComboBox::GetConnectWidget()
717 return GTK_COMBO(m_widget
)->entry
;
720 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
722 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
723 (window
== GTK_COMBO(m_widget
)->button
->window
) );
726 wxSize
wxComboBox::DoGetBestSize() const
728 wxSize
ret( wxControl::DoGetBestSize() );
730 // we know better our horizontal extent: it depends on the longest string
736 size_t count
= GetCount();
737 for ( size_t n
= 0; n
< count
; n
++ )
739 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
, &m_font
);
745 // empty combobox should have some reasonable default size too