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"
25 #include "wx/gtk/private.h"
26 #include "wx/gtk/private/gtk2-compat.h"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
34 gtkcombobox_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
36 if (!combo
->m_hasVMT
) return;
38 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
39 event
.SetString( combo
->GetValue() );
40 event
.SetEventObject( combo
);
41 combo
->HandleWindowEvent( event
);
45 gtkcombobox_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
47 combo
->SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED
);
51 gtkcombobox_popupshown_callback(GObject
*WXUNUSED(gobject
),
52 GParamSpec
*WXUNUSED(param_spec
),
56 g_object_get( combo
->m_widget
, "popup-shown", &isShown
, NULL
);
57 wxCommandEvent
event( isShown
? wxEVT_COMMAND_COMBOBOX_DROPDOWN
58 : wxEVT_COMMAND_COMBOBOX_CLOSEUP
,
60 event
.SetEventObject( combo
);
61 combo
->HandleWindowEvent( event
);
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
70 BEGIN_EVENT_TABLE(wxComboBox
, wxChoice
)
71 EVT_CHAR(wxComboBox::OnChar
)
73 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
74 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
75 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
76 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
77 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
78 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
79 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
81 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
82 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
83 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
84 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
85 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
86 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
87 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
90 void wxComboBox::Init()
95 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
96 const wxString
& value
,
97 const wxPoint
& pos
, const wxSize
& size
,
98 const wxArrayString
& choices
,
99 long style
, const wxValidator
& validator
,
100 const wxString
& name
)
102 wxCArrayString
chs(choices
);
104 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
105 chs
.GetStrings(), style
, validator
, name
);
108 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
109 const wxPoint
& pos
, const wxSize
& size
,
110 int n
, const wxString choices
[],
111 long style
, const wxValidator
& validator
,
112 const wxString
& name
)
114 if (!PreCreation( parent
, pos
, size
) ||
115 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
117 wxFAIL_MSG( wxT("wxComboBox creation failed") );
121 if (HasFlag(wxCB_SORT
))
122 m_strings
= new wxGtkCollatedArrayString();
124 GTKCreateComboBoxWidget();
126 if (HasFlag(wxBORDER_NONE
))
128 // Doesn't seem to work
129 // g_object_set (m_widget, "has-frame", FALSE, NULL);
132 GtkEntry
* const entry
= GetEntry();
136 // Set it up to trigger default item on enter key press
137 gtk_entry_set_activates_default( entry
,
138 !HasFlag(wxTE_PROCESS_ENTER
) );
140 gtk_editable_set_editable(GTK_EDITABLE(entry
), true);
145 m_parent
->DoAddChild( this );
148 m_focusWidget
= GTK_WIDGET( entry
);
154 if (style
& wxCB_READONLY
)
156 // this will assert and do nothing if the value is not in our list
157 // of strings which is the desired behaviour (for consistency with
158 // wxMSW and also because it doesn't make sense to have a string
159 // which is not a possible choice in a read-only combobox)
160 SetStringSelection(value
);
161 gtk_editable_set_editable(GTK_EDITABLE(entry
), false);
163 else // editable combobox
165 // any value is accepted, even if it's not in our list
166 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
169 g_signal_connect_after (entry
, "changed",
170 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
172 GTKConnectClipboardSignals(GTK_WIDGET(entry
));
175 g_signal_connect_after (m_widget
, "changed",
176 G_CALLBACK (gtkcombobox_changed_callback
), this);
179 if ( !gtk_check_version(2,10,0) )
182 g_signal_connect (m_widget
, "notify::popup-shown",
183 G_CALLBACK (gtkcombobox_popupshown_callback
), this);
186 SetInitialSize(size
); // need this too because this is a wxControlWithItems
191 void wxComboBox::GTKCreateComboBoxWidget()
194 m_widget
= gtk_combo_box_text_new_with_entry();
196 m_widget
= gtk_combo_box_entry_new_text();
198 g_object_ref(m_widget
);
200 m_entry
= GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget
)));
203 GtkEditable
*wxComboBox::GetEditable() const
205 return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget
)));
208 void wxComboBox::OnChar( wxKeyEvent
&event
)
210 switch ( event
.GetKeyCode() )
213 if ( HasFlag(wxTE_PROCESS_ENTER
) && GetEntry() )
215 // GTK automatically selects an item if its in the list
216 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
217 eventEnter
.SetString( GetValue() );
218 eventEnter
.SetInt( GetSelection() );
219 eventEnter
.SetEventObject( this );
221 if ( HandleWindowEvent(eventEnter
) )
223 // Catch GTK event so that GTK doesn't open the drop
224 // down list upon RETURN.
234 void wxComboBox::EnableTextChangedEvents(bool enable
)
241 g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(m_widget
)),
242 (gpointer
)gtkcombobox_text_changed_callback
, this);
246 g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(m_widget
)),
247 (gpointer
)gtkcombobox_text_changed_callback
, this);
251 void wxComboBox::GTKDisableEvents()
253 EnableTextChangedEvents(false);
255 g_signal_handlers_block_by_func(m_widget
,
256 (gpointer
)gtkcombobox_changed_callback
, this);
257 g_signal_handlers_block_by_func(m_widget
,
258 (gpointer
)gtkcombobox_popupshown_callback
, this);
261 void wxComboBox::GTKEnableEvents()
263 EnableTextChangedEvents(true);
265 g_signal_handlers_unblock_by_func(m_widget
,
266 (gpointer
)gtkcombobox_changed_callback
, this);
267 g_signal_handlers_unblock_by_func(m_widget
,
268 (gpointer
)gtkcombobox_popupshown_callback
, this);
271 GtkWidget
* wxComboBox::GetConnectWidget()
273 return GTK_WIDGET( GetEntry() );
276 GdkWindow
* wxComboBox::GTKGetWindow(wxArrayGdkWindows
& /* windows */) const
279 // no access to internal GdkWindows
282 return gtk_entry_get_text_window(GetEntry());
288 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
291 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new_with_entry
, true);
293 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
297 void wxComboBox::SetValue(const wxString
& value
)
299 if ( HasFlag(wxCB_READONLY
) )
300 SetStringSelection(value
);
302 wxTextEntry::SetValue(value
);
305 void wxComboBox::SetString(unsigned int n
, const wxString
& text
)
307 wxChoice::SetString(n
, text
);
309 if ( static_cast<int>(n
) == GetSelection() )
311 // We also need to update the currently shown text, for consistency
312 // with wxMSW and also because it makes sense as leaving the old string
313 // in the text but not in the list would be confusing to the user.
316 // And we need to keep the selection unchanged, modifying the item is
317 // not supposed to deselect it.
322 // ----------------------------------------------------------------------------
323 // standard event handling
324 // ----------------------------------------------------------------------------
326 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
331 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
336 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
341 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
346 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
351 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
356 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
361 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
363 event
.Enable( CanCut() );
366 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
368 event
.Enable( CanCopy() );
371 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
373 event
.Enable( CanPaste() );
376 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
378 event
.Enable( CanUndo() );
381 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
383 event
.Enable( CanRedo() );
386 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
388 event
.Enable(HasSelection() && IsEditable()) ;
391 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
393 event
.Enable(!wxTextEntry::IsEmpty());
396 void wxComboBox::Popup()
398 gtk_combo_box_popup( GTK_COMBO_BOX(m_widget
) );
401 void wxComboBox::Dismiss()
403 gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget
) );
405 #endif // wxUSE_COMBOBOX