1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/combobox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/combobox.h"
19 #include "wx/settings.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
21 #include "wx/arrstr.h"
24 #include "wx/gtk/private.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
32 gtkcombobox_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
34 if (!combo
->m_hasVMT
) return;
36 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
37 event
.SetString( combo
->GetValue() );
38 event
.SetEventObject( combo
);
39 combo
->GetEventHandler()->ProcessEvent( event
);
43 gtkcombobox_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
45 if (!combo
->m_hasVMT
) return;
47 if (combo
->GetSelection() == -1)
50 wxCommandEvent
event( wxEVT_COMMAND_COMBOBOX_SELECTED
, combo
->GetId() );
51 event
.SetInt( combo
->GetSelection() );
52 event
.SetString( combo
->GetStringSelection() );
53 event
.SetEventObject( combo
);
54 combo
->GetEventHandler()->ProcessEvent( event
);
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
,wxControl
)
64 BEGIN_EVENT_TABLE(wxComboBox
, wxControl
)
65 EVT_CHAR(wxComboBox::OnChar
)
67 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
68 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
69 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
70 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
71 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
72 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
73 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
75 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
76 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
77 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
78 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
79 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
80 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
81 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
84 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
85 const wxString
& value
,
86 const wxPoint
& pos
, const wxSize
& size
,
87 const wxArrayString
& choices
,
88 long style
, const wxValidator
& validator
,
89 const wxString
& name
)
91 wxCArrayString
chs(choices
);
93 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
94 chs
.GetStrings(), style
, validator
, name
);
97 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
98 const wxPoint
& pos
, const wxSize
& size
,
99 int n
, const wxString choices
[],
100 long style
, const wxValidator
& validator
,
101 const wxString
& name
)
104 m_ignoreNextUpdate
= false;
107 if (!PreCreation( parent
, pos
, size
) ||
108 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
110 wxFAIL_MSG( wxT("wxComboBox creation failed") );
114 if(HasFlag(wxCB_SORT
))
115 m_strings
= new wxSortedArrayString();
117 m_widget
= gtk_combo_box_entry_new_text();
119 GtkEntry
* const entry
= GetEntry();
121 gtk_entry_set_editable( entry
, TRUE
);
125 m_parent
->DoAddChild( this );
127 m_focusWidget
= GTK_WIDGET( entry
);
131 ConnectWidget( m_widget
);
133 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
135 if (style
& wxCB_READONLY
)
136 gtk_entry_set_editable( entry
, FALSE
);
138 g_signal_connect_after (entry
, "changed",
139 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
141 g_signal_connect_after (m_widget
, "changed",
142 G_CALLBACK (gtkcombobox_changed_callback
), this);
145 SetInitialSize(size
); // need this too because this is a wxControlWithItems
150 GtkEntry
*wxComboBox::GetEntry() const
152 return GTK_ENTRY(GTK_BIN(m_widget
)->child
);
155 GtkEditable
*wxComboBox::GetEditable() const
157 return GTK_EDITABLE( GTK_BIN(m_widget
)->child
);
160 wxComboBox::~wxComboBox()
167 void wxComboBox::SetFocus()
171 // don't do anything if we already have focus
175 gtk_widget_grab_focus( m_focusWidget
);
178 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
180 void **clientData
, wxClientDataType type
)
182 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
184 wxASSERT_MSG( !IsSorted() || (pos
== GetCount()),
185 _T("In a sorted combobox data could only be appended"));
187 const int count
= items
.GetCount();
191 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
192 for( int i
= 0; i
< count
; ++i
)
195 // If sorted, use this wxSortedArrayStrings to determine
196 // the right insertion point
198 n
= m_strings
->Add(items
[i
]);
200 gtk_combo_box_insert_text( combobox
, n
, wxGTK_CONV( items
[i
] ) );
202 m_clientData
.Insert( NULL
, n
);
203 AssignNewItemClientData(n
, clientData
, i
, type
);
206 InvalidateBestSize();
211 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
213 m_clientData
[n
] = clientData
;
216 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
218 return m_clientData
[n
];
221 void wxComboBox::DoClear()
223 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
227 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
228 const unsigned int count
= GetCount();
229 for (unsigned int i
= 0; i
< count
; i
++)
230 gtk_combo_box_remove_text( combobox
, 0 );
232 m_clientData
.Clear();
239 InvalidateBestSize();
242 void wxComboBox::DoDeleteOneItem(unsigned int n
)
244 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
246 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
248 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
249 gtk_combo_box_remove_text( combobox
, n
);
251 m_clientData
.RemoveAt( n
);
253 m_strings
->RemoveAt( n
);
255 InvalidateBestSize();
258 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
260 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
262 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
263 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
265 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
267 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
269 GValue value
= { 0, };
270 g_value_init( &value
, G_TYPE_STRING
);
271 g_value_set_string( &value
, wxGTK_CONV( text
) );
272 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, 0, &value
);
273 g_value_unset( &value
);
276 InvalidateBestSize();
279 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
281 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
283 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
284 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
286 gtk_tree_model_get_iter_first( model
, &iter
);
287 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
292 GValue value
= { 0, };
293 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
294 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
295 g_value_unset( &value
);
297 if (item
.IsSameAs( str
, bCase
) )
302 while ( gtk_tree_model_iter_next(model
, &iter
) );
307 int wxComboBox::GetSelection() const
309 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
310 return gtk_combo_box_get_active( combobox
);
313 int wxComboBox::GetCurrentSelection() const
315 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
317 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
318 return gtk_combo_box_get_active( combobox
);
321 wxString
wxComboBox::GetString(unsigned int n
) const
323 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
327 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
328 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
330 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
332 GValue value
= { 0, };
333 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
334 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
335 g_value_unset( &value
);
342 unsigned int wxComboBox::GetCount() const
344 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
346 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
347 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
349 gtk_tree_model_get_iter_first( model
, &iter
);
350 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
352 unsigned int ret
= 1;
353 while (gtk_tree_model_iter_next( model
, &iter
))
358 void wxComboBox::SetSelection( int n
)
360 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
364 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
365 gtk_combo_box_set_active( combobox
, n
);
370 void wxComboBox::OnChar( wxKeyEvent
&event
)
372 switch ( event
.GetKeyCode() )
375 if ( HasFlag(wxTE_PROCESS_ENTER
) )
377 // GTK automatically selects an item if its in the list
378 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
379 eventEnter
.SetString( GetValue() );
380 eventEnter
.SetInt( GetSelection() );
381 eventEnter
.SetEventObject( this );
383 if ( GetEventHandler()->ProcessEvent(eventEnter
) )
385 // Catch GTK event so that GTK doesn't open the drop
386 // down list upon RETURN.
396 void wxComboBox::DisableEvents()
398 g_signal_handlers_block_by_func(GTK_BIN(m_widget
)->child
,
399 (gpointer
)gtkcombobox_text_changed_callback
, this);
401 g_signal_handlers_block_by_func(m_widget
,
402 (gpointer
)gtkcombobox_changed_callback
, this);
405 void wxComboBox::EnableEvents()
407 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget
)->child
,
408 (gpointer
)gtkcombobox_text_changed_callback
, this);
410 g_signal_handlers_unblock_by_func(m_widget
,
411 (gpointer
)gtkcombobox_changed_callback
, this);
414 GtkWidget
* wxComboBox::GetConnectWidget()
416 return GTK_WIDGET( GetEntry() );
419 GdkWindow
*wxComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
421 wxUnusedVar(windows
);
423 return GetEntry()->text_area
;
426 wxSize
wxComboBox::DoGetBestSize() const
428 wxSize
ret( wxControl::DoGetBestSize() );
430 // we know better our horizontal extent: it depends on the longest string
435 unsigned int count
= GetCount();
436 for ( unsigned int n
= 0; n
< count
; n
++ )
438 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
444 // empty combobox should have some reasonable default size too
454 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
456 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
459 // ----------------------------------------------------------------------------
460 // standard event handling
461 // ----------------------------------------------------------------------------
463 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
468 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
473 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
478 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
483 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
488 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
491 GetSelection(& from
, & to
);
492 if (from
!= -1 && to
!= -1)
496 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
498 SetSelection(-1, -1);
501 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
503 event
.Enable( CanCut() );
506 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
508 event
.Enable( CanCopy() );
511 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
513 event
.Enable( CanPaste() );
516 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
518 event
.Enable( CanUndo() );
521 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
523 event
.Enable( CanRedo() );
526 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
528 event
.Enable(HasSelection() && IsEditable()) ;
531 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
533 event
.Enable(GetLastPosition() > 0);
536 #endif // wxUSE_COMBOBOX