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 if (HasFlag(wxBORDER_NONE
))
121 // Doesn't seem to work
122 // g_object_set (m_widget, "has-frame", FALSE, NULL);
125 GtkEntry
* const entry
= GetEntry();
127 gtk_entry_set_editable( entry
, TRUE
);
131 m_parent
->DoAddChild( this );
133 m_focusWidget
= GTK_WIDGET( entry
);
137 ConnectWidget( m_widget
);
139 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
141 if (style
& wxCB_READONLY
)
142 gtk_entry_set_editable( entry
, FALSE
);
144 g_signal_connect_after (entry
, "changed",
145 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
147 g_signal_connect_after (m_widget
, "changed",
148 G_CALLBACK (gtkcombobox_changed_callback
), this);
151 SetInitialSize(size
); // need this too because this is a wxControlWithItems
156 GtkEntry
*wxComboBox::GetEntry() const
158 return GTK_ENTRY(GTK_BIN(m_widget
)->child
);
161 GtkEditable
*wxComboBox::GetEditable() const
163 return GTK_EDITABLE( GTK_BIN(m_widget
)->child
);
166 wxComboBox::~wxComboBox()
173 void wxComboBox::SetFocus()
177 // don't do anything if we already have focus
181 gtk_widget_grab_focus( m_focusWidget
);
184 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
186 void **clientData
, wxClientDataType type
)
188 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
190 wxASSERT_MSG( !IsSorted() || (pos
== GetCount()),
191 _T("In a sorted combobox data could only be appended"));
193 const int count
= items
.GetCount();
197 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
198 for( int i
= 0; i
< count
; ++i
)
201 // If sorted, use this wxSortedArrayStrings to determine
202 // the right insertion point
204 n
= m_strings
->Add(items
[i
]);
206 gtk_combo_box_insert_text( combobox
, n
, wxGTK_CONV( items
[i
] ) );
208 m_clientData
.Insert( NULL
, n
);
209 AssignNewItemClientData(n
, clientData
, i
, type
);
212 InvalidateBestSize();
217 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
219 m_clientData
[n
] = clientData
;
222 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
224 return m_clientData
[n
];
227 void wxComboBox::DoClear()
229 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
233 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
234 const unsigned int count
= GetCount();
235 for (unsigned int i
= 0; i
< count
; i
++)
236 gtk_combo_box_remove_text( combobox
, 0 );
238 m_clientData
.Clear();
245 InvalidateBestSize();
248 void wxComboBox::DoDeleteOneItem(unsigned int n
)
250 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
252 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
254 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
255 gtk_combo_box_remove_text( combobox
, n
);
257 m_clientData
.RemoveAt( n
);
259 m_strings
->RemoveAt( n
);
261 InvalidateBestSize();
264 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
266 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
268 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
269 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
271 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
273 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
275 GValue value
= { 0, };
276 g_value_init( &value
, G_TYPE_STRING
);
277 g_value_set_string( &value
, wxGTK_CONV( text
) );
278 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, 0, &value
);
279 g_value_unset( &value
);
282 InvalidateBestSize();
285 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
287 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
289 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
290 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
292 gtk_tree_model_get_iter_first( model
, &iter
);
293 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
298 GValue value
= { 0, };
299 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
300 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
301 g_value_unset( &value
);
303 if (item
.IsSameAs( str
, bCase
) )
308 while ( gtk_tree_model_iter_next(model
, &iter
) );
313 int wxComboBox::GetSelection() const
315 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
316 return gtk_combo_box_get_active( combobox
);
319 int wxComboBox::GetCurrentSelection() const
321 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
323 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
324 return gtk_combo_box_get_active( combobox
);
327 wxString
wxComboBox::GetString(unsigned int n
) const
329 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
333 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
334 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
336 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
338 GValue value
= { 0, };
339 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
340 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
341 g_value_unset( &value
);
348 unsigned int wxComboBox::GetCount() const
350 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
352 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
353 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
355 gtk_tree_model_get_iter_first( model
, &iter
);
356 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
358 unsigned int ret
= 1;
359 while (gtk_tree_model_iter_next( model
, &iter
))
364 void wxComboBox::SetSelection( int n
)
366 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
370 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
371 gtk_combo_box_set_active( combobox
, n
);
376 void wxComboBox::OnChar( wxKeyEvent
&event
)
378 switch ( event
.GetKeyCode() )
381 if ( HasFlag(wxTE_PROCESS_ENTER
) )
383 // GTK automatically selects an item if its in the list
384 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
385 eventEnter
.SetString( GetValue() );
386 eventEnter
.SetInt( GetSelection() );
387 eventEnter
.SetEventObject( this );
389 if ( GetEventHandler()->ProcessEvent(eventEnter
) )
391 // Catch GTK event so that GTK doesn't open the drop
392 // down list upon RETURN.
402 void wxComboBox::DisableEvents()
404 g_signal_handlers_block_by_func(GTK_BIN(m_widget
)->child
,
405 (gpointer
)gtkcombobox_text_changed_callback
, this);
407 g_signal_handlers_block_by_func(m_widget
,
408 (gpointer
)gtkcombobox_changed_callback
, this);
411 void wxComboBox::EnableEvents()
413 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget
)->child
,
414 (gpointer
)gtkcombobox_text_changed_callback
, this);
416 g_signal_handlers_unblock_by_func(m_widget
,
417 (gpointer
)gtkcombobox_changed_callback
, this);
420 GtkWidget
* wxComboBox::GetConnectWidget()
422 return GTK_WIDGET( GetEntry() );
425 GdkWindow
*wxComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
427 wxUnusedVar(windows
);
429 return GetEntry()->text_area
;
432 wxSize
wxComboBox::DoGetBestSize() const
434 wxSize
ret( wxControl::DoGetBestSize() );
436 // we know better our horizontal extent: it depends on the longest string
441 unsigned int count
= GetCount();
442 for ( unsigned int n
= 0; n
< count
; n
++ )
444 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
450 // empty combobox should have some reasonable default size too
460 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
462 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
465 // ----------------------------------------------------------------------------
466 // standard event handling
467 // ----------------------------------------------------------------------------
469 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
474 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
479 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
484 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
489 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
494 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
497 GetSelection(& from
, & to
);
498 if (from
!= -1 && to
!= -1)
502 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
504 SetSelection(-1, -1);
507 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
509 event
.Enable( CanCut() );
512 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
514 event
.Enable( CanCopy() );
517 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
519 event
.Enable( CanPaste() );
522 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
524 event
.Enable( CanUndo() );
527 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
529 event
.Enable( CanRedo() );
532 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
534 event
.Enable(HasSelection() && IsEditable()) ;
537 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
539 event
.Enable(GetLastPosition() > 0);
542 #endif // wxUSE_COMBOBOX