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
->HandleWindowEvent( 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
->HandleWindowEvent( 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 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
175 void **clientData
, wxClientDataType type
)
177 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
179 wxASSERT_MSG( !IsSorted() || (pos
== GetCount()),
180 _T("In a sorted combobox data could only be appended"));
182 const int count
= items
.GetCount();
186 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
187 for( int i
= 0; i
< count
; ++i
)
190 // If sorted, use this wxSortedArrayStrings to determine
191 // the right insertion point
193 n
= m_strings
->Add(items
[i
]);
195 gtk_combo_box_insert_text( combobox
, n
, wxGTK_CONV( items
[i
] ) );
197 m_clientData
.Insert( NULL
, n
);
198 AssignNewItemClientData(n
, clientData
, i
, type
);
201 InvalidateBestSize();
206 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
208 m_clientData
[n
] = clientData
;
211 void* wxComboBox::DoGetItemClientData(unsigned int n
) const
213 return m_clientData
[n
];
216 void wxComboBox::DoClear()
218 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
222 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
223 const unsigned int count
= GetCount();
224 for (unsigned int i
= 0; i
< count
; i
++)
225 gtk_combo_box_remove_text( combobox
, 0 );
227 m_clientData
.Clear();
234 InvalidateBestSize();
237 void wxComboBox::DoDeleteOneItem(unsigned int n
)
239 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
241 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
243 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
244 gtk_combo_box_remove_text( combobox
, n
);
246 m_clientData
.RemoveAt( n
);
248 m_strings
->RemoveAt( n
);
250 InvalidateBestSize();
253 void wxComboBox::SetString(unsigned int n
, const wxString
&text
)
255 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
257 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
258 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
260 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
262 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
264 GValue value
= { 0, };
265 g_value_init( &value
, G_TYPE_STRING
);
266 g_value_set_string( &value
, wxGTK_CONV( text
) );
267 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, 0, &value
);
268 g_value_unset( &value
);
271 InvalidateBestSize();
274 int wxComboBox::FindString( const wxString
&item
, bool bCase
) const
276 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid combobox") );
278 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
279 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
281 gtk_tree_model_get_iter_first( model
, &iter
);
282 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
287 GValue value
= { 0, };
288 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
289 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
290 g_value_unset( &value
);
292 if (item
.IsSameAs( str
, bCase
) )
297 while ( gtk_tree_model_iter_next(model
, &iter
) );
302 int wxComboBox::GetSelection() const
304 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
305 return gtk_combo_box_get_active( combobox
);
308 int wxComboBox::GetCurrentSelection() const
310 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid combobox") );
312 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
313 return gtk_combo_box_get_active( combobox
);
316 wxString
wxComboBox::GetString(unsigned int n
) const
318 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid combobox") );
322 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
323 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
325 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
327 GValue value
= { 0, };
328 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
329 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
330 g_value_unset( &value
);
337 unsigned int wxComboBox::GetCount() const
339 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid combobox") );
341 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
342 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
344 gtk_tree_model_get_iter_first( model
, &iter
);
345 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
347 unsigned int ret
= 1;
348 while (gtk_tree_model_iter_next( model
, &iter
))
353 void wxComboBox::SetSelection( int n
)
355 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid combobox") );
359 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
360 gtk_combo_box_set_active( combobox
, n
);
365 void wxComboBox::OnChar( wxKeyEvent
&event
)
367 switch ( event
.GetKeyCode() )
370 if ( HasFlag(wxTE_PROCESS_ENTER
) )
372 // GTK automatically selects an item if its in the list
373 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
374 eventEnter
.SetString( GetValue() );
375 eventEnter
.SetInt( GetSelection() );
376 eventEnter
.SetEventObject( this );
378 if ( HandleWindowEvent(eventEnter
) )
380 // Catch GTK event so that GTK doesn't open the drop
381 // down list upon RETURN.
386 // On enter key press, we must give a signal to default control,
387 // Otherwise, nothing happens when pressing Enter from inside a
388 // combo box in a dialog.
389 wxWindow
*top_frame
= wxGetTopLevelParent(this);
390 if( top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
) )
392 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
393 if ( window
->default_widget
)
394 gtk_widget_activate( window
->default_widget
);
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
))
499 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
504 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
506 event
.Enable( CanCut() );
509 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
511 event
.Enable( CanCopy() );
514 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
516 event
.Enable( CanPaste() );
519 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
521 event
.Enable( CanUndo() );
524 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
526 event
.Enable( CanRedo() );
529 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
531 event
.Enable(HasSelection() && IsEditable()) ;
534 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
536 event
.Enable(!wxTextEntry::IsEmpty());
539 #endif // wxUSE_COMBOBOX