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 // Set it up to trigger default item on enter key press
120 GtkWidget
*widget
= gtk_bin_get_child(GTK_BIN(m_widget
));
121 gtk_entry_set_activates_default(GTK_ENTRY(widget
),
122 !HasFlag(wxTE_PROCESS_ENTER
));
124 if (HasFlag(wxBORDER_NONE
))
126 // Doesn't seem to work
127 // g_object_set (m_widget, "has-frame", FALSE, NULL);
130 GtkEntry
* const entry
= GetEntry();
132 gtk_entry_set_editable( entry
, TRUE
);
136 m_parent
->DoAddChild( this );
138 m_focusWidget
= GTK_WIDGET( entry
);
142 ConnectWidget( m_widget
);
144 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
146 if (style
& wxCB_READONLY
)
147 gtk_entry_set_editable( entry
, FALSE
);
149 g_signal_connect_after (entry
, "changed",
150 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
152 g_signal_connect_after (m_widget
, "changed",
153 G_CALLBACK (gtkcombobox_changed_callback
), this);
156 SetInitialSize(size
); // need this too because this is a wxControlWithItems
161 GtkEntry
*wxComboBox::GetEntry() const
163 return GTK_ENTRY(GTK_BIN(m_widget
)->child
);
166 GtkEditable
*wxComboBox::GetEditable() const
168 return GTK_EDITABLE( GTK_BIN(m_widget
)->child
);
171 wxComboBox::~wxComboBox()
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 ( HandleWindowEvent(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 // strangely, this returns a width of 188 pixels from GTK+ (?)
429 wxSize
ret( wxControl::DoGetBestSize() );
431 // we know better our horizontal extent: it depends on the longest string
435 ret
.x
= 60; // start with something "sensible"
437 unsigned int count
= GetCount();
438 for ( unsigned int n
= 0; n
< count
; n
++ )
440 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
441 if ( width
+ 40 > ret
.x
) // 40 for drop down arrow and space around text
446 // empty combobox should have some reasonable default size too
447 if ((GetCount() == 0) && (ret
.x
< 80))
456 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
458 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
461 // ----------------------------------------------------------------------------
462 // standard event handling
463 // ----------------------------------------------------------------------------
465 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
470 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
475 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
480 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
485 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
490 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
495 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
500 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
502 event
.Enable( CanCut() );
505 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
507 event
.Enable( CanCopy() );
510 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
512 event
.Enable( CanPaste() );
515 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
517 event
.Enable( CanUndo() );
520 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
522 event
.Enable( CanRedo() );
525 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
527 event
.Enable(HasSelection() && IsEditable()) ;
530 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
532 event
.Enable(!wxTextEntry::IsEmpty());
535 #endif // wxUSE_COMBOBOX