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
;
134 gtk_list_set_selection_mode( GTK_LIST(list
), GTK_SELECTION_MULTIPLE
);
136 for (int i
= 0; i
< n
; i
++)
138 /* don't send first event, which GTK sends aways when
139 inserting the first item */
140 m_alreadySent
= TRUE
;
142 GtkWidget
*list_item
= gtk_list_item_new_with_label( choices
[i
].mbc_str() );
144 m_clientDataList
.Append( (wxObject
*)NULL
);
145 m_clientObjectList
.Append( (wxObject
*)NULL
);
147 gtk_container_add( GTK_CONTAINER(list
), list_item
);
149 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
150 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
152 gtk_widget_show( list_item
);
155 m_parent
->DoAddChild( this );
157 m_focusWidget
= GTK_COMBO(m_widget
)->entry
;
161 ConnectWidget( GTK_COMBO(m_widget
)->button
);
163 if (!value
.IsNull()) SetValue( value
);
165 if (style
& wxCB_READONLY
)
166 gtk_entry_set_editable( GTK_ENTRY( GTK_COMBO(m_widget
)->entry
), FALSE
);
168 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
169 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
171 wxSize
size_best( DoGetBestSize() );
172 wxSize
new_size( size
);
173 if (new_size
.x
== -1)
174 new_size
.x
= size_best
.x
;
175 if (new_size
.y
== -1)
176 new_size
.y
= size_best
.y
;
177 if (new_size
.y
> size_best
.y
)
178 new_size
.y
= size_best
.y
;
179 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
181 SetSize( new_size
.x
, new_size
.y
);
183 // This is required for tool bar support
184 gtk_widget_set_usize( m_widget
, new_size
.x
, new_size
.y
);
188 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
) );
189 SetForegroundColour( parent
->GetForegroundColour() );
196 wxComboBox::~wxComboBox()
198 wxNode
*node
= m_clientObjectList
.First();
201 wxClientData
*cd
= (wxClientData
*)node
->Data();
205 m_clientObjectList
.Clear();
207 m_clientDataList
.Clear();
210 void wxComboBox::AppendCommon( const wxString
&item
)
212 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
214 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
216 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
.mbc_str() );
218 gtk_container_add( GTK_CONTAINER(list
), list_item
);
220 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
221 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
223 if (GTK_WIDGET_REALIZED(m_widget
))
225 gtk_widget_realize( list_item
);
226 gtk_widget_realize( GTK_BIN(list_item
)->child
);
228 if (m_widgetStyle
) ApplyWidgetStyle();
231 gtk_widget_show( list_item
);
234 void wxComboBox::Append( const wxString
&item
)
236 m_clientDataList
.Append( (wxObject
*) NULL
);
237 m_clientObjectList
.Append( (wxObject
*) NULL
);
239 AppendCommon( item
);
242 void wxComboBox::Append( const wxString
&item
, void *clientData
)
244 m_clientDataList
.Append( (wxObject
*) clientData
);
245 m_clientObjectList
.Append( (wxObject
*)NULL
);
247 AppendCommon( item
);
250 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
252 m_clientDataList
.Append( (wxObject
*) NULL
);
253 m_clientObjectList
.Append( (wxObject
*) clientData
);
255 AppendCommon( item
);
258 void wxComboBox::SetClientData( int n
, void* clientData
)
260 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
262 wxNode
*node
= m_clientDataList
.Nth( n
);
265 node
->SetData( (wxObject
*) clientData
);
268 void* wxComboBox::GetClientData( int n
)
270 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
272 wxNode
*node
= m_clientDataList
.Nth( n
);
273 if (!node
) return NULL
;
278 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
280 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
282 wxNode
*node
= m_clientObjectList
.Nth( n
);
285 wxClientData
*cd
= (wxClientData
*) node
->Data();
288 node
->SetData( (wxObject
*) clientData
);
291 wxClientData
* wxComboBox::GetClientObject( int n
)
293 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
295 wxNode
*node
= m_clientObjectList
.Nth( n
);
296 if (!node
) return (wxClientData
*) NULL
;
298 return (wxClientData
*) node
->Data();
301 void wxComboBox::Clear()
303 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
305 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
306 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
308 wxNode
*node
= m_clientObjectList
.First();
311 wxClientData
*cd
= (wxClientData
*)node
->Data();
315 m_clientObjectList
.Clear();
317 m_clientDataList
.Clear();
320 void wxComboBox::Delete( int n
)
322 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
324 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
326 GList
*child
= g_list_nth( listbox
->children
, n
);
330 wxFAIL_MSG(wxT("wrong index"));
334 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
335 gtk_list_remove_items( listbox
, list
);
338 wxNode
*node
= m_clientObjectList
.Nth( n
);
341 wxClientData
*cd
= (wxClientData
*)node
->Data();
343 m_clientObjectList
.DeleteNode( node
);
346 node
= m_clientDataList
.Nth( n
);
349 m_clientDataList
.DeleteNode( node
);
353 int wxComboBox::FindString( const wxString
&item
)
355 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
357 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
359 GList
*child
= GTK_LIST(list
)->children
;
363 GtkBin
*bin
= GTK_BIN( child
->data
);
364 GtkLabel
*label
= GTK_LABEL( bin
->child
);
365 if (item
== wxString(label
->label
,*wxConvCurrent
))
374 int wxComboBox::GetSelection() const
376 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
378 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
380 GList
*selection
= GTK_LIST(list
)->selection
;
383 GList
*child
= GTK_LIST(list
)->children
;
387 if (child
->data
== selection
->data
) return count
;
396 wxString
wxComboBox::GetString( int n
) const
398 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
400 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
403 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
406 GtkBin
*bin
= GTK_BIN( child
->data
);
407 GtkLabel
*label
= GTK_LABEL( bin
->child
);
408 str
= wxString(label
->label
,*wxConvCurrent
);
412 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
418 wxString
wxComboBox::GetStringSelection() const
420 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
422 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
424 GList
*selection
= GTK_LIST(list
)->selection
;
427 GtkBin
*bin
= GTK_BIN( selection
->data
);
428 wxString tmp
= wxString(GTK_LABEL( bin
->child
)->label
,*wxConvCurrent
);
432 wxFAIL_MSG( wxT("wxComboBox: no selection") );
437 int wxComboBox::Number() const
439 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
441 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
443 GList
*child
= GTK_LIST(list
)->children
;
445 while (child
) { count
++; child
= child
->next
; }
449 void wxComboBox::SetSelection( int n
)
451 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
455 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
456 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
457 gtk_list_select_item( GTK_LIST(list
), n
);
463 void wxComboBox::SetStringSelection( const wxString
&string
)
465 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
467 int res
= FindString( string
);
468 if (res
== -1) return;
472 wxString
wxComboBox::GetValue() const
474 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
475 wxString tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(entry
) ),*wxConvCurrent
);
479 void wxComboBox::SetValue( const wxString
& value
)
481 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
483 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
484 wxString tmp
= wxT("");
485 if (!value
.IsNull()) tmp
= value
;
486 gtk_entry_set_text( GTK_ENTRY(entry
), tmp
.mbc_str() );
489 void wxComboBox::Copy()
491 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
493 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
494 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
497 void wxComboBox::Cut()
499 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
501 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
502 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
505 void wxComboBox::Paste()
507 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
509 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
510 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
513 void wxComboBox::SetInsertionPoint( long pos
)
515 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
517 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
518 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
521 void wxComboBox::SetInsertionPointEnd()
523 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
525 SetInsertionPoint( -1 );
528 long wxComboBox::GetInsertionPoint() const
530 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
533 long wxComboBox::GetLastPosition() const
535 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
536 int pos
= GTK_ENTRY(entry
)->text_length
;
540 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
542 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
543 // FIXME: not quite sure how to do this method right in multibyte mode
545 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
546 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
547 if (value
.IsNull()) return;
549 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.mbc_str(), value
.Length(), &pos
);
552 void wxComboBox::Remove(long from
, long to
)
554 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
556 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
557 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
560 void wxComboBox::SetSelection( long from
, long to
)
562 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
563 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
566 void wxComboBox::SetEditable( bool editable
)
568 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
569 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
572 void wxComboBox::OnChar( wxKeyEvent
&event
)
574 if ( event
.KeyCode() == WXK_RETURN
)
576 wxString value
= GetValue();
580 // make Enter generate "selected" event if there is only one item
581 // in the combobox - without it, it's impossible to select it at
583 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
585 event
.SetString( value
);
586 event
.SetEventObject( this );
587 GetEventHandler()->ProcessEvent( event
);
591 // add the item to the list if it's not there yet
592 if ( FindString(value
) == wxNOT_FOUND
)
595 SetStringSelection(value
);
597 // and generate the selected event for it
598 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, GetId() );
599 event
.SetInt( Number() - 1 );
600 event
.SetString( value
);
601 event
.SetEventObject( this );
602 GetEventHandler()->ProcessEvent( event
);
606 // This will invoke the dialog default action, such
607 // as the clicking the default button.
609 wxWindow
*top_frame
= m_parent
;
610 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
611 top_frame
= top_frame
->GetParent();
613 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
615 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
617 if (window
->default_widget
)
619 gtk_widget_activate (window
->default_widget
);
630 void wxComboBox::DisableEvents()
632 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
633 GList
*child
= list
->children
;
636 gtk_signal_disconnect_by_func( GTK_OBJECT(child
->data
),
637 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
643 void wxComboBox::EnableEvents()
645 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
646 GList
*child
= list
->children
;
649 gtk_signal_connect( GTK_OBJECT(child
->data
), "select",
650 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback
), (gpointer
)this );
656 void wxComboBox::OnSize( wxSizeEvent
&event
)
662 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
664 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
665 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
669 void wxComboBox::ApplyWidgetStyle()
673 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
674 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
675 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
677 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
678 GList
*child
= list
->children
;
681 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
683 GtkBin
*bin
= GTK_BIN(child
->data
);
684 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
690 GtkWidget
* wxComboBox::GetConnectWidget()
692 return GTK_COMBO(m_widget
)->entry
;
695 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
697 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
698 (window
== GTK_COMBO(m_widget
)->button
->window
) );
701 wxSize
wxComboBox::DoGetBestSize() const
703 wxSize
ret( wxControl::DoGetBestSize() );
705 // we know better our horizontal extent: it depends on the longest string
710 GdkFont
*font
= m_font
.GetInternalFont();
713 size_t count
= Number();
714 for ( size_t n
= 0; n
< count
; n
++ )
716 width
= (wxCoord
)gdk_string_width(font
, GetString(n
).mbc_str());
722 // empty combobox should have some reasonable default size too