1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "combobox.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 #include "wx/combobox.h"
21 #include "wx/settings.h"
22 #include "wx/arrstr.h"
25 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
27 #include "wx/gtk/private.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern void wxapp_install_idle_handler();
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern bool g_blockEventsOnDrag
;
42 //-----------------------------------------------------------------------------
43 // "select-child" - click/cursor get select-child, changed, select-child
44 //-----------------------------------------------------------------------------
47 gtk_combo_select_child_callback( GtkList
*WXUNUSED(list
), GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
49 if (g_isIdle
) wxapp_install_idle_handler();
51 if (!combo
->m_hasVMT
) return;
53 if (g_blockEventsOnDrag
) return;
55 int curSelection
= combo
->GetSelection();
57 if (combo
->m_prevSelection
!= curSelection
)
59 GtkWidget
*list
= GTK_COMBO(combo
->m_widget
)->list
;
60 gtk_list_unselect_item( GTK_LIST(list
), combo
->m_prevSelection
);
62 combo
->m_prevSelection
= curSelection
;
64 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
65 event
.SetInt( curSelection
);
66 event
.SetString( combo
->GetStringSelection() );
67 event
.SetEventObject( combo
);
69 combo
->GetEventHandler()->ProcessEvent( event
);
72 //-----------------------------------------------------------------------------
73 // "changed" - typing and list item matches get changed, select-child
74 // if it doesn't match an item then just get a single changed
75 //-----------------------------------------------------------------------------
78 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
80 if (g_isIdle
) wxapp_install_idle_handler();
82 if (!combo
->m_hasVMT
) return;
84 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
85 event
.SetString( combo
->GetValue() );
86 event
.SetEventObject( combo
);
87 combo
->GetEventHandler()->ProcessEvent( event
);
91 gtk_dummy_callback(GtkEntry
*WXUNUSED(entry
), GtkCombo
*WXUNUSED(combo
))
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
99 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
101 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
102 EVT_SIZE(wxComboBox::OnSize
)
103 EVT_CHAR(wxComboBox::OnChar
)
106 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
107 const wxString
& value
,
108 const wxPoint
& pos
, const wxSize
& size
,
109 const wxArrayString
& choices
,
110 long style
, const wxValidator
& validator
,
111 const wxString
& name
)
113 wxCArrayString
chs(choices
);
115 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
116 chs
.GetStrings(), style
, validator
, name
);
119 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
120 const wxPoint
& pos
, const wxSize
& size
,
121 int n
, const wxString choices
[],
122 long style
, const wxValidator
& validator
,
123 const wxString
& name
)
125 m_alreadySent
= FALSE
;
127 m_acceptsFocus
= TRUE
;
130 if (!PreCreation( parent
, pos
, size
) ||
131 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
133 wxFAIL_MSG( wxT("wxComboBox creation failed") );
137 m_widget
= gtk_combo_new();
138 GtkCombo
*combo
= GTK_COMBO(m_widget
);
140 // Disable GTK's broken events ...
141 gtk_signal_disconnect( GTK_OBJECT(combo
->entry
), combo
->entry_change_id
);
142 // ... and add surogate handler.
143 combo
->entry_change_id
= gtk_signal_connect (GTK_OBJECT (combo
->entry
), "changed",
144 (GtkSignalFunc
) gtk_dummy_callback
, combo
);
146 // make it more useable
147 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget
), TRUE
);
149 // and case-sensitive
150 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget
), TRUE
);
152 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
155 // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE );
158 for (int i
= 0; i
< n
; i
++)
160 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( choices
[i
] ) );
162 m_clientDataList
.Append( (wxObject
*)NULL
);
163 m_clientObjectList
.Append( (wxObject
*)NULL
);
165 gtk_container_add( GTK_CONTAINER(list
), list_item
);
167 gtk_widget_show( list_item
);
170 m_parent
->DoAddChild( this );
172 m_focusWidget
= combo
->entry
;
177 ConnectWidget( combo
->button
);
179 // MSW's combo box shows the value and the selection is -1
180 gtk_entry_set_text( GTK_ENTRY(combo
->entry
), wxGTK_CONV(value
) );
181 gtk_list_unselect_all( GTK_LIST(combo
->list
) );
183 if (style
& wxCB_READONLY
)
184 gtk_entry_set_editable( GTK_ENTRY( combo
->entry
), FALSE
);
186 gtk_signal_connect( GTK_OBJECT(combo
->entry
), "changed",
187 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
189 gtk_signal_connect( GTK_OBJECT(combo
->list
), "select-child",
190 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
192 wxSize
size_best( DoGetBestSize() );
193 wxSize
new_size( size
);
194 if (new_size
.x
== -1)
195 new_size
.x
= size_best
.x
;
196 if (new_size
.y
== -1)
197 new_size
.y
= size_best
.y
;
198 if (new_size
.y
> size_best
.y
)
199 new_size
.y
= size_best
.y
;
200 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
202 SetSize( new_size
.x
, new_size
.y
);
204 // This is required for tool bar support
205 gtk_widget_set_usize( m_widget
, new_size
.x
, new_size
.y
);
213 wxComboBox::~wxComboBox()
215 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
218 wxClientData
*cd
= (wxClientData
*)node
->GetData();
220 node
= node
->GetNext();
222 m_clientObjectList
.Clear();
224 m_clientDataList
.Clear();
227 void wxComboBox::SetFocus()
231 // don't do anything if we already have focus
235 gtk_widget_grab_focus( m_focusWidget
);
238 int wxComboBox::DoAppend( const wxString
&item
)
240 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
244 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
246 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
248 gtk_container_add( GTK_CONTAINER(list
), list_item
);
250 if (GTK_WIDGET_REALIZED(m_widget
))
252 gtk_widget_realize( list_item
);
253 gtk_widget_realize( GTK_BIN(list_item
)->child
);
255 if (m_widgetStyle
) ApplyWidgetStyle();
258 gtk_widget_show( list_item
);
260 const int count
= GetCount();
262 if ( (int)m_clientDataList
.GetCount() < count
)
263 m_clientDataList
.Append( (wxObject
*) NULL
);
264 if ( (int)m_clientObjectList
.GetCount() < count
)
265 m_clientObjectList
.Append( (wxObject
*) NULL
);
272 int wxComboBox::DoInsert( const wxString
&item
, int pos
)
274 wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT
), -1,
275 wxT("can't insert into sorted list"));
277 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
279 int count
= GetCount();
280 wxCHECK_MSG( (pos
>= 0) && (pos
<= count
), -1, wxT("invalid index") );
287 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
289 GtkWidget
*list_item
= gtk_list_item_new_with_label( wxGTK_CONV( item
) );
291 GList
*gitem_list
= g_list_alloc ();
292 gitem_list
->data
= list_item
;
293 gtk_list_insert_items( GTK_LIST (list
), gitem_list
, pos
);
295 if (GTK_WIDGET_REALIZED(m_widget
))
297 gtk_widget_realize( list_item
);
298 gtk_widget_realize( GTK_BIN(list_item
)->child
);
304 gtk_widget_show( list_item
);
308 if ( (int)m_clientDataList
.GetCount() < count
)
309 m_clientDataList
.Insert( pos
, (wxObject
*) NULL
);
310 if ( (int)m_clientObjectList
.GetCount() < count
)
311 m_clientObjectList
.Insert( pos
, (wxObject
*) NULL
);
318 void wxComboBox::DoSetItemClientData( int n
, void* clientData
)
320 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
322 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
325 node
->SetData( (wxObject
*) clientData
);
328 void* wxComboBox::DoGetItemClientData( int n
) const
330 wxCHECK_MSG( m_widget
!= NULL
, NULL
, wxT("invalid combobox") );
332 wxList::compatibility_iterator node
= m_clientDataList
.Item( n
);
334 return node
? node
->GetData() : NULL
;
337 void wxComboBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
339 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
341 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
344 // wxItemContainer already deletes data for us
346 node
->SetData( (wxObject
*) clientData
);
349 wxClientData
* wxComboBox::DoGetItemClientObject( int n
) const
351 wxCHECK_MSG( m_widget
!= NULL
, (wxClientData
*)NULL
, wxT("invalid combobox") );
353 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
355 return node
? (wxClientData
*) node
->GetData() : NULL
;
358 void wxComboBox::Clear()
360 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
364 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
365 gtk_list_clear_items( GTK_LIST(list
), 0, GetCount() );
367 wxList::compatibility_iterator node
= m_clientObjectList
.GetFirst();
370 wxClientData
*cd
= (wxClientData
*)node
->GetData();
372 node
= node
->GetNext();
374 m_clientObjectList
.Clear();
376 m_clientDataList
.Clear();
381 void wxComboBox::Delete( int n
)
383 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
385 GtkList
*listbox
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
387 GList
*child
= g_list_nth( listbox
->children
, n
);
391 wxFAIL_MSG(wxT("wrong index"));
397 GList
*list
= g_list_append( (GList
*) NULL
, child
->data
);
398 gtk_list_remove_items( listbox
, list
);
401 wxList::compatibility_iterator node
= m_clientObjectList
.Item( n
);
404 wxClientData
*cd
= (wxClientData
*)node
->GetData();
406 m_clientObjectList
.Erase( node
);
409 node
= m_clientDataList
.Item( n
);
411 m_clientDataList
.Erase( node
);
416 void wxComboBox::SetString(int n
, const wxString
&text
)
418 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
420 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
422 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
425 GtkBin
*bin
= GTK_BIN( child
->data
);
426 GtkLabel
*label
= GTK_LABEL( bin
->child
);
427 gtk_label_set_text(label
, wxGTK_CONV(text
));
431 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
435 int wxComboBox::FindString( const wxString
&item
) const
437 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
439 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
441 GList
*child
= GTK_LIST(list
)->children
;
445 GtkBin
*bin
= GTK_BIN( child
->data
);
446 GtkLabel
*label
= GTK_LABEL( bin
->child
);
448 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
450 wxString
str( label
->label
);
462 int wxComboBox::GetSelection() const
464 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
466 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
468 GList
*selection
= GTK_LIST(list
)->selection
;
471 GList
*child
= GTK_LIST(list
)->children
;
475 if (child
->data
== selection
->data
) return count
;
484 wxString
wxComboBox::GetString( int n
) const
486 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
488 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
491 GList
*child
= g_list_nth( GTK_LIST(list
)->children
, n
);
494 GtkBin
*bin
= GTK_BIN( child
->data
);
495 GtkLabel
*label
= GTK_LABEL( bin
->child
);
497 str
= wxGTK_CONV_BACK( gtk_label_get_text(label
) );
499 str
= wxString( label
->label
);
504 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
510 wxString
wxComboBox::GetStringSelection() const
512 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid combobox") );
514 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
516 GList
*selection
= GTK_LIST(list
)->selection
;
519 GtkBin
*bin
= GTK_BIN( selection
->data
);
520 GtkLabel
*label
= GTK_LABEL( bin
->child
);
522 wxString
tmp( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
524 wxString
tmp( label
->label
);
529 wxFAIL_MSG( wxT("wxComboBox: no selection") );
534 int wxComboBox::GetCount() const
536 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
538 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
540 GList
*child
= GTK_LIST(list
)->children
;
542 while (child
) { count
++; child
= child
->next
; }
546 void wxComboBox::SetSelection( int n
)
548 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
552 GtkWidget
*list
= GTK_COMBO(m_widget
)->list
;
553 gtk_list_unselect_item( GTK_LIST(list
), m_prevSelection
);
554 gtk_list_select_item( GTK_LIST(list
), n
);
560 void wxComboBox::SetStringSelection( const wxString
&string
)
562 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
564 int res
= FindString( string
);
565 if (res
== -1) return;
569 wxString
wxComboBox::GetValue() const
571 GtkEntry
*entry
= GTK_ENTRY( GTK_COMBO(m_widget
)->entry
);
572 wxString
tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry
) ) );
575 for (int i
= 0; i
< wxStrlen(tmp
.c_str()) +1; i
++)
578 printf( "%d ", (int) (c
) );
586 void wxComboBox::SetValue( const wxString
& value
)
588 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
590 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
591 wxString tmp
= wxT("");
592 if (!value
.IsNull()) tmp
= value
;
593 gtk_entry_set_text( GTK_ENTRY(entry
), wxGTK_CONV( tmp
) );
596 void wxComboBox::Copy()
598 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
600 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
601 gtk_editable_copy_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
604 void wxComboBox::Cut()
606 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
608 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
609 gtk_editable_cut_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
612 void wxComboBox::Paste()
614 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
616 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
617 gtk_editable_paste_clipboard( GTK_EDITABLE(entry
) DUMMY_CLIPBOARD_ARG
);
620 void wxComboBox::SetInsertionPoint( long pos
)
622 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
624 if ( pos
== GetLastPosition() )
627 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
628 gtk_entry_set_position( GTK_ENTRY(entry
), (int)pos
);
631 long wxComboBox::GetInsertionPoint() const
633 return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget
)->entry
);
636 long wxComboBox::GetLastPosition() const
638 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
639 int pos
= GTK_ENTRY(entry
)->text_length
;
643 void wxComboBox::Replace( long from
, long to
, const wxString
& value
)
645 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
647 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
648 gtk_editable_delete_text( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
649 if (value
.IsNull()) return;
653 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( value
);
654 gtk_editable_insert_text( GTK_EDITABLE(entry
), (const char*) buffer
, strlen( (const char*) buffer
), &pos
);
656 gtk_editable_insert_text( GTK_EDITABLE(entry
), value
.c_str(), value
.Length(), &pos
);
660 void wxComboBox::SetSelection( long from
, long to
)
662 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
663 gtk_editable_select_region( GTK_EDITABLE(entry
), (gint
)from
, (gint
)to
);
666 void wxComboBox::SetEditable( bool editable
)
668 GtkWidget
*entry
= GTK_COMBO(m_widget
)->entry
;
669 gtk_entry_set_editable( GTK_ENTRY(entry
), editable
);
672 void wxComboBox::OnChar( wxKeyEvent
&event
)
674 if ( event
.GetKeyCode() == WXK_RETURN
)
676 // GTK automatically selects an item if its in the list
677 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, GetId());
678 event
.SetString( GetValue() );
679 event
.SetInt( GetSelection() );
680 event
.SetEventObject( this );
682 if (!GetEventHandler()->ProcessEvent( event
))
684 // This will invoke the dialog default action, such
685 // as the clicking the default button.
687 wxWindow
*top_frame
= m_parent
;
688 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
689 top_frame
= top_frame
->GetParent();
691 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
693 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
695 if (window
->default_widget
)
696 gtk_widget_activate (window
->default_widget
);
700 // Catch GTK event so that GTK doesn't open the drop
701 // down list upon RETURN.
708 void wxComboBox::DisableEvents()
710 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->list
),
711 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
712 gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
),
713 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
716 void wxComboBox::EnableEvents()
718 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->list
), "select-child",
719 GTK_SIGNAL_FUNC(gtk_combo_select_child_callback
), (gpointer
)this );
720 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget
)->entry
), "changed",
721 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this );
724 void wxComboBox::OnSize( wxSizeEvent
&event
)
730 gtk_widget_set_usize( GTK_COMBO(m_widget
)->entry
, m_width
-w
-1, m_height
);
732 gtk_widget_set_uposition( GTK_COMBO(m_widget
)->button
, m_x
+m_width
-w
, m_y
);
733 gtk_widget_set_usize( GTK_COMBO(m_widget
)->button
, w
, m_height
);
737 void wxComboBox::ApplyWidgetStyle()
741 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
742 gtk_widget_set_style( GTK_COMBO(m_widget
)->entry
, m_widgetStyle
);
743 gtk_widget_set_style( GTK_COMBO(m_widget
)->list
, m_widgetStyle
);
745 GtkList
*list
= GTK_LIST( GTK_COMBO(m_widget
)->list
);
746 GList
*child
= list
->children
;
749 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
751 GtkBin
*bin
= GTK_BIN(child
->data
);
752 gtk_widget_set_style( bin
->child
, m_widgetStyle
);
758 GtkWidget
* wxComboBox::GetConnectWidget()
760 return GTK_COMBO(m_widget
)->entry
;
763 bool wxComboBox::IsOwnGtkWindow( GdkWindow
*window
)
765 return ( (window
== GTK_ENTRY( GTK_COMBO(m_widget
)->entry
)->text_area
) ||
766 (window
== GTK_COMBO(m_widget
)->button
->window
) );
769 wxSize
wxComboBox::DoGetBestSize() const
771 wxSize
ret( wxControl::DoGetBestSize() );
773 // we know better our horizontal extent: it depends on the longest string
779 size_t count
= GetCount();
780 for ( size_t n
= 0; n
< count
; n
++ )
782 GetTextExtent( GetString(n
), &width
, NULL
, NULL
, NULL
, &m_font
);
788 // empty combobox should have some reasonable default size too