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 combo
->SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED
);
49 gtkcombobox_popupshown_callback(GObject
*WXUNUSED(gobject
),
50 GParamSpec
*WXUNUSED(param_spec
),
54 g_object_get( combo
->m_widget
, "popup-shown", &isShown
, NULL
);
55 wxCommandEvent
event( isShown
? wxEVT_COMMAND_COMBOBOX_DROPDOWN
56 : wxEVT_COMMAND_COMBOBOX_CLOSEUP
,
58 event
.SetEventObject( combo
);
59 combo
->HandleWindowEvent( event
);
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxChoice
)
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 wxSortedArrayString();
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_entry_set_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_entry_set_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);
175 if ( !gtk_check_version(2,10,0) )
177 g_signal_connect (m_widget
, "notify::popup-shown",
178 G_CALLBACK (gtkcombobox_popupshown_callback
), this);
181 SetInitialSize(size
); // need this too because this is a wxControlWithItems
186 void wxComboBox::GTKCreateComboBoxWidget()
188 m_widget
= gtk_combo_box_entry_new_text();
189 g_object_ref(m_widget
);
191 m_entry
= GTK_ENTRY(GTK_BIN(m_widget
)->child
);
194 GtkEditable
*wxComboBox::GetEditable() const
196 return GTK_EDITABLE( GTK_BIN(m_widget
)->child
);
199 void wxComboBox::OnChar( wxKeyEvent
&event
)
201 switch ( event
.GetKeyCode() )
204 if ( HasFlag(wxTE_PROCESS_ENTER
) && GetEntry() )
206 // GTK automatically selects an item if its in the list
207 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
208 eventEnter
.SetString( GetValue() );
209 eventEnter
.SetInt( GetSelection() );
210 eventEnter
.SetEventObject( this );
212 if ( HandleWindowEvent(eventEnter
) )
214 // Catch GTK event so that GTK doesn't open the drop
215 // down list upon RETURN.
225 void wxComboBox::GTKDisableEvents()
228 g_signal_handlers_block_by_func(GTK_BIN(m_widget
)->child
,
229 (gpointer
)gtkcombobox_text_changed_callback
, this);
231 g_signal_handlers_block_by_func(m_widget
,
232 (gpointer
)gtkcombobox_changed_callback
, this);
233 g_signal_handlers_block_by_func(m_widget
,
234 (gpointer
)gtkcombobox_popupshown_callback
, this);
237 void wxComboBox::GTKEnableEvents()
240 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget
)->child
,
241 (gpointer
)gtkcombobox_text_changed_callback
, this);
243 g_signal_handlers_unblock_by_func(m_widget
,
244 (gpointer
)gtkcombobox_changed_callback
, this);
245 g_signal_handlers_unblock_by_func(m_widget
,
246 (gpointer
)gtkcombobox_popupshown_callback
, this);
249 GtkWidget
* wxComboBox::GetConnectWidget()
251 return GTK_WIDGET( GetEntry() );
254 GdkWindow
* wxComboBox::GTKGetWindow(wxArrayGdkWindows
& /* windows */) const
256 return GetEntry()->text_area
;
261 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
263 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
266 // ----------------------------------------------------------------------------
267 // standard event handling
268 // ----------------------------------------------------------------------------
270 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
275 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
280 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
285 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
290 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
295 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
300 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
305 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
307 event
.Enable( CanCut() );
310 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
312 event
.Enable( CanCopy() );
315 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
317 event
.Enable( CanPaste() );
320 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
322 event
.Enable( CanUndo() );
325 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
327 event
.Enable( CanRedo() );
330 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
332 event
.Enable(HasSelection() && IsEditable()) ;
335 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
337 event
.Enable(!wxTextEntry::IsEmpty());
340 void wxComboBox::Popup()
342 gtk_combo_box_popup( GTK_COMBO_BOX(m_widget
) );
345 void wxComboBox::Dismiss()
347 gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget
) );
349 #endif // wxUSE_COMBOBOX