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
);
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
69 BEGIN_EVENT_TABLE(wxComboBox
, wxChoice
)
70 EVT_CHAR(wxComboBox::OnChar
)
72 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
73 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
74 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
75 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
76 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
77 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
78 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
80 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
81 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
82 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
83 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
84 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
85 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
86 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
89 void wxComboBox::Init()
94 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
95 const wxString
& value
,
96 const wxPoint
& pos
, const wxSize
& size
,
97 const wxArrayString
& choices
,
98 long style
, const wxValidator
& validator
,
99 const wxString
& name
)
101 wxCArrayString
chs(choices
);
103 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
104 chs
.GetStrings(), style
, validator
, name
);
107 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
108 const wxPoint
& pos
, const wxSize
& size
,
109 int n
, const wxString choices
[],
110 long style
, const wxValidator
& validator
,
111 const wxString
& name
)
113 if (!PreCreation( parent
, pos
, size
) ||
114 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
116 wxFAIL_MSG( wxT("wxComboBox creation failed") );
120 if (HasFlag(wxCB_SORT
))
121 m_strings
= new wxGtkCollatedArrayString();
123 GTKCreateComboBoxWidget();
125 if (HasFlag(wxBORDER_NONE
))
127 // Doesn't seem to work
128 // g_object_set (m_widget, "has-frame", FALSE, NULL);
131 GtkEntry
* const entry
= GetEntry();
135 // Set it up to trigger default item on enter key press
136 gtk_entry_set_activates_default( entry
,
137 !HasFlag(wxTE_PROCESS_ENTER
) );
139 gtk_editable_set_editable(GTK_EDITABLE(entry
), true);
144 m_parent
->DoAddChild( this );
147 m_focusWidget
= GTK_WIDGET( entry
);
153 if (style
& wxCB_READONLY
)
155 // this will assert and do nothing if the value is not in our list
156 // of strings which is the desired behaviour (for consistency with
157 // wxMSW and also because it doesn't make sense to have a string
158 // which is not a possible choice in a read-only combobox)
159 SetStringSelection(value
);
160 gtk_editable_set_editable(GTK_EDITABLE(entry
), false);
162 else // editable combobox
164 // any value is accepted, even if it's not in our list
165 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
168 g_signal_connect_after (entry
, "changed",
169 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
172 g_signal_connect_after (m_widget
, "changed",
173 G_CALLBACK (gtkcombobox_changed_callback
), this);
176 if ( !gtk_check_version(2,10,0) )
179 g_signal_connect (m_widget
, "notify::popup-shown",
180 G_CALLBACK (gtkcombobox_popupshown_callback
), this);
183 SetInitialSize(size
); // need this too because this is a wxControlWithItems
188 void wxComboBox::GTKCreateComboBoxWidget()
191 m_widget
= gtk_combo_box_text_new_with_entry();
193 m_widget
= gtk_combo_box_entry_new_text();
195 g_object_ref(m_widget
);
197 m_entry
= GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget
)));
200 GtkEditable
*wxComboBox::GetEditable() const
202 return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget
)));
205 void wxComboBox::OnChar( wxKeyEvent
&event
)
207 switch ( event
.GetKeyCode() )
210 if ( HasFlag(wxTE_PROCESS_ENTER
) && GetEntry() )
212 // GTK automatically selects an item if its in the list
213 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
214 eventEnter
.SetString( GetValue() );
215 eventEnter
.SetInt( GetSelection() );
216 eventEnter
.SetEventObject( this );
218 if ( HandleWindowEvent(eventEnter
) )
220 // Catch GTK event so that GTK doesn't open the drop
221 // down list upon RETURN.
231 void wxComboBox::EnableTextChangedEvents(bool enable
)
238 g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(m_widget
)),
239 (gpointer
)gtkcombobox_text_changed_callback
, this);
243 g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(m_widget
)),
244 (gpointer
)gtkcombobox_text_changed_callback
, this);
248 void wxComboBox::GTKDisableEvents()
250 EnableTextChangedEvents(false);
252 g_signal_handlers_block_by_func(m_widget
,
253 (gpointer
)gtkcombobox_changed_callback
, this);
254 g_signal_handlers_block_by_func(m_widget
,
255 (gpointer
)gtkcombobox_popupshown_callback
, this);
258 void wxComboBox::GTKEnableEvents()
260 EnableTextChangedEvents(true);
262 g_signal_handlers_unblock_by_func(m_widget
,
263 (gpointer
)gtkcombobox_changed_callback
, this);
264 g_signal_handlers_unblock_by_func(m_widget
,
265 (gpointer
)gtkcombobox_popupshown_callback
, this);
268 GtkWidget
* wxComboBox::GetConnectWidget()
270 return GTK_WIDGET( GetEntry() );
273 GdkWindow
* wxComboBox::GTKGetWindow(wxArrayGdkWindows
& /* windows */) const
276 // no access to internal GdkWindows
279 return gtk_entry_get_text_window(GetEntry());
285 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
288 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new_with_entry
, true);
290 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
294 void wxComboBox::SetValue(const wxString
& value
)
296 if ( HasFlag(wxCB_READONLY
) )
297 SetStringSelection(value
);
299 wxTextEntry::SetValue(value
);
302 void wxComboBox::SetString(unsigned int n
, const wxString
& text
)
304 wxChoice::SetString(n
, text
);
306 if ( static_cast<int>(n
) == GetSelection() )
308 // We also need to update the currently shown text, for consistency
309 // with wxMSW and also because it makes sense as leaving the old string
310 // in the text but not in the list would be confusing to the user.
313 // And we need to keep the selection unchanged, modifying the item is
314 // not supposed to deselect it.
319 // ----------------------------------------------------------------------------
320 // standard event handling
321 // ----------------------------------------------------------------------------
323 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
328 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
333 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
338 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
343 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
348 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
353 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
358 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
360 event
.Enable( CanCut() );
363 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
365 event
.Enable( CanCopy() );
368 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
370 event
.Enable( CanPaste() );
373 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
375 event
.Enable( CanUndo() );
378 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
380 event
.Enable( CanRedo() );
383 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
385 event
.Enable(HasSelection() && IsEditable()) ;
388 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
390 event
.Enable(!wxTextEntry::IsEmpty());
393 void wxComboBox::Popup()
395 gtk_combo_box_popup( GTK_COMBO_BOX(m_widget
) );
398 void wxComboBox::Dismiss()
400 gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget
) );
402 #endif // wxUSE_COMBOBOX