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 //-----------------------------------------------------------------------------
39 // "select-child" - click/cursor get select-child, changed, select-child
40 //-----------------------------------------------------------------------------
43 gtk_combo_select_child_callback( GtkList
*WXUNUSED(list
), 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 int curSelection
= combo
->GetSelection();
53 if (combo
->m_prevSelection
!= curSelection
)
55 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
56 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
58 combo
->m_prevSelection
= curSelection
;
60 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
61 event
.SetInt( curSelection
);
62 event
.SetString( combo
->GetStringSelection() );
63 event
.SetEventObject( combo
);
65 combo
->GetEventHandler()->ProcessEvent( event
);
68 //-----------------------------------------------------------------------------
69 // "changed" - typing and list item matches get changed, select-child
70 // if it doesn't match an item then just get a single changed
71 //-----------------------------------------------------------------------------
74 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
76 if (g_isIdle
) wxapp_install_idle_handler();
78 if (!combo
->m_hasVMT
) return;
80 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
81 event
.SetString( combo
->GetValue() );
82 event
.SetEventObject( combo
);
83 combo
->GetEventHandler()->ProcessEvent( event
);
87 gtk_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
91 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
95 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
97 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
98 EVT_SIZE(wxComboBox::OnSize
)
99 EVT_CHAR(wxComboBox::OnChar
)
102 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
103 const wxPoint
& pos
, const wxSize
& size
,
104 int n
, const wxString choices
[],
105 long style
, const wxValidator
& validator
,
106 const wxString
& name
)
108 m_alreadySent
= FALSE
;
110 m_acceptsFocus
= TRUE
;
113 if (!PreCreation( parent
, pos
, size
) ||
114 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
116 wxFAIL_MSG( wxT("wxComboBox creation failed") );
120 m_widget
= gtk_combo_new();
121 GtkCombo
*combo
= GTK_COMBO(m_widget
);
123 // Disable GTK's broken events ...
124 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
125 // ... and add surogate handler.
126 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
127 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
129 // make it more useable
130 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
132 // and case-sensitive
133 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
135 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
138 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
141 for (int i
= 0; i
< n
; i
++)
143 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
145 m_clientDataList
.Append( (wxObject
*)NULL
);
146 m_clientObjectList
.Append( (wxObject
*)NULL
);
148 gtk_container_add( GTK_CONTAINER(list
), list_item
);
150 gtk_widget_show( list_item
);
153 m_parent
->DoAddChild( this );
155 m_focusWidget
= combo
->entry
;
159 ConnectWidget( combo
->button
);
161 // MSW's combo box shows the value and the selection is -1
162 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
163 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
165 if (style
& wxCB_READONLY
)
166 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
168 gtk_signal_connect( GTK_OBJECT(combo
->entry
), "changed",
169 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
171 gtk_signal_connect( GTK_OBJECT(combo
->list
), "select-child",
172 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
174 wxSize
size_best( DoGetBestSize() );
175 wxSize
new_size( size
);
176 if (new_size
.x
== -1)
177 new_size
.x
= size_best
.x
;
178 if (new_size
.y
== -1)
179 new_size
.y
= size_best
.y
;
180 if (new_size
.y
> size_best
.y
)
181 new_size
.y
= size_best
.y
;
182 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
184 SetSize( new_size
.x
, new_size
.y
);
186 // This is required for tool bar support
187 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
.GetFirst();
203 wxClientData
*cd
= (wxClientData
*)node
->GetData();
205 node
= node
->GetNext();
207 m_clientObjectList
.Clear();
209 m_clientDataList
.Clear();
212 void wxComboBox::SetFocus()
216 // don't do anything if we already have focus
220 gtk_widget_grab_focus( m_focusWidget
);
223 void wxComboBox::AppendCommon( const wxString
&item
)
225 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
229 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
231 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
233 gtk_container_add( GTK_CONTAINER(list
), list_item
);
235 if (GTK_WIDGET_REALIZED(m_widget
))
237 gtk_widget_realize( list_item
);
238 gtk_widget_realize( GTK_BIN(list_item
)->child
);
240 if (m_widgetStyle
) ApplyWidgetStyle();
243 gtk_widget_show( list_item
);
248 void wxComboBox::Append( const wxString
&item
)
250 m_clientDataList
.Append( (wxObject
*) NULL
);
251 m_clientObjectList
.Append( (wxObject
*) NULL
);
253 AppendCommon( item
);
256 void wxComboBox::Append( const wxString
&item
, void *clientData
)
258 m_clientDataList
.Append( (wxObject
*) clientData
);
259 m_clientObjectList
.Append( (wxObject
*)NULL
);
261 AppendCommon( item
);
264 void wxComboBox::Append( const wxString
&item
, wxClientData
*clientData
)
266 m_clientDataList
.Append( (wxObject
*) NULL
);
267 m_clientObjectList
.Append( (wxObject
*) clientData
);
269 AppendCommon( item
);
272 void wxComboBox::SetClientData( int n
, void* clientData
)
274 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
276 wxNode
*node
= m_clientDataList
.Item( n
);
279 node
->SetData( (wxObject
*) clientData
);
282 void* wxComboBox::GetClientData( int n
)
284 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
286 wxNode
*node
= m_clientDataList
.Item( n
);
287 if (!node
) return NULL
;
289 return node
->GetData();
292 void wxComboBox::SetClientObject( int n
, wxClientData
* clientData
)
294 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
296 wxNode
*node
= m_clientObjectList
.Item( n
);
299 wxClientData
*cd
= (wxClientData
*) node
->GetData();
302 node
->SetData( (wxObject
*) clientData
);
305 wxClientData
* wxComboBox::GetClientObject( int n
)
307 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
309 wxNode
*node
= m_clientObjectList
.Item( n
);
310 if (!node
) return (wxClientData
*) NULL
;
312 return (wxClientData
*) node
->GetData();
315 void wxComboBox::Clear()
317 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
321 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
322 gtk_list_clear_items( GTK_LIST(list
), 0, Number() );
324 wxNode
*node
= m_clientObjectList
.GetFirst();
327 wxClientData
*cd
= (wxClientData
*)node
->GetData();
329 node
= node
->GetNext();
331 m_clientObjectList
.Clear();
333 m_clientDataList
.Clear();
338 void wxComboBox::Delete( int n
)
340 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
342 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
344 GList
*child
= g_list_nth( listbox
->children
, n
);
348 wxFAIL_MSG(wxT("wrong index"));
354 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
355 gtk_list_remove_items( listbox
, list
);
358 wxNode
*node
= m_clientObjectList
.Item( n
);
361 wxClientData
*cd
= (wxClientData
*)node
->GetData();
363 m_clientObjectList
.DeleteNode( node
);
366 node
= m_clientDataList
.Item( n
);
368 m_clientDataList
.DeleteNode( node
);
373 void wxComboBox::SetString(int n
, const wxString
&text
)
375 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
377 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 gtk_label_set_text(label
, wxGTK_CONV(text
));
388 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
392 int wxComboBox::FindString( const wxString
&item
)
394 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
396 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
398 GList
*child
= GTK_LIST(list
)->children
;
402 GtkBin
*bin
= GTK_BIN( child
->data
);
403 GtkLabel
*label
= GTK_LABEL( bin
->child
);
405 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
407 wxString
str( label
->label
);
419 int wxComboBox::GetSelection() const
421 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
423 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
425 GList
*selection
= GTK_LIST(list
)->selection
;
428 GList
*child
= GTK_LIST(list
)->children
;
432 if (child
->data
== selection
->data
) return count
;
441 wxString
wxComboBox::GetString( int n
) const
443 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
445 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
448 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
451 GtkBin
*bin
= GTK_BIN( child
->data
);
452 GtkLabel
*label
= GTK_LABEL( bin
->child
);
454 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
456 str
= wxString( label
->label
);
461 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
467 wxString
wxComboBox::GetStringSelection() const
469 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
471 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
473 GList
*selection
= GTK_LIST(list
)->selection
;
476 GtkBin
*bin
= GTK_BIN( selection
->data
);
477 GtkLabel
*label
= GTK_LABEL( bin
->child
);
479 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
481 wxString
tmp( label
->label
);
486 wxFAIL_MSG( wxT("wxComboBox: no selection") );
491 int wxComboBox::Number() const
493 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
495 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
497 GList
*child
= GTK_LIST(list
)->children
;
499 while (child
) { count
++; child
= child
->next
; }
503 void wxComboBox::SetSelection( int n
)
505 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
509 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
510 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
511 gtk_list_select_item( GTK_LIST(list
), n
);
517 void wxComboBox::SetStringSelection( const wxString
&string
)
519 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
521 int res
= FindString( string
);
522 if (res
== -1) return;
526 wxString
wxComboBox::GetValue() const
528 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
529 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
532 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
535 printf( "%d ", (int) (c
) );
543 void wxComboBox::SetValue( const wxString
& value
)
545 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
547 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
548 wxString tmp
= wxT("");
549 if (!value
.IsNull()) tmp
= value
;
550 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
553 void wxComboBox::Copy()
555 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
557 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
558 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
561 void wxComboBox::Cut()
563 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
565 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
566 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
569 void wxComboBox::Paste()
571 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
573 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
574 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
577 void wxComboBox::SetInsertionPoint( long pos
)
579 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
581 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
582 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
585 void wxComboBox::SetInsertionPointEnd()
587 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
589 SetInsertionPoint( -1 );
592 long wxComboBox::GetInsertionPoint() const
594 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
597 long wxComboBox::GetLastPosition() const
599 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
600 int pos
= GTK_ENTRY(entry
)->text_length
;
604 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
606 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
608 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
609 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
610 if (value
.IsNull()) return;
614 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
615 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
617 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
621 void wxComboBox::Remove(long from
, long to
)
623 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
625 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
626 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
629 void wxComboBox::SetSelection( long from
, long to
)
631 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
632 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
635 void wxComboBox::SetEditable( bool editable
)
637 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
638 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
641 void wxComboBox::OnChar( wxKeyEvent
&event
)
643 if ( event
.GetKeyCode() == WXK_RETURN
)
645 // GTK automatically selects an item if its in the list
646 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, GetId());
647 event
.SetString( GetValue() );
648 event
.SetInt( GetSelection() );
649 event
.SetEventObject( this );
651 if (!GetEventHandler()->ProcessEvent( event
))
653 // This will invoke the dialog default action, such
654 // as the clicking the default button.
656 wxWindow
*top_frame
= m_parent
;
657 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
658 top_frame
= top_frame
->GetParent();
660 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
662 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
664 if (window
->default_widget
)
665 gtk_widget_activate (window
->default_widget
);
669 // Catch GTK event so that GTK doesn't open the drop
670 // down list upon RETURN.
677 void wxComboBox::DisableEvents()
679 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
680 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
681 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
682 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
685 void wxComboBox::EnableEvents()
687 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
688 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
689 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
690 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
693 void wxComboBox::OnSize( wxSizeEvent
&event
)
699 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
701 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
702 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
706 void wxComboBox::ApplyWidgetStyle()
710 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
711 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
712 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
714 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
715 GList
*child
= list
->children
;
718 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
720 GtkBin
*bin
= GTK_BIN(child
->data
);
721 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
727 GtkWidget
* wxComboBox::GetConnectWidget()
729 return GTK_COMBO(m_widget
)->entry
;
732 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
734 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
735 (window
== GTK_COMBO(m_widget
)->button
->window
) );
738 wxSize
wxComboBox::DoGetBestSize() const
740 wxSize
ret( wxControl::DoGetBestSize() );
742 // we know better our horizontal extent: it depends on the longest string
748 size_t count
= GetCount();
749 for ( size_t n
= 0; n
< count
; n
++ )
751 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
, &m_font
);
757 // empty combobox should have some reasonable default size too