add combobox drop down/close up events (closes #10587)
[wxWidgets.git] / src / gtk / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/combobox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_COMBOBOX
14
15 #include "wx/combobox.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/settings.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
21 #include "wx/arrstr.h"
22 #endif
23
24 #include "wx/gtk/private.h"
25
26 // ----------------------------------------------------------------------------
27 // GTK callbacks
28 // ----------------------------------------------------------------------------
29
30 extern "C" {
31 static void
32 gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
33 {
34 if (!combo->m_hasVMT) return;
35
36 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
37 event.SetString( combo->GetValue() );
38 event.SetEventObject( combo );
39 combo->HandleWindowEvent( event );
40 }
41
42 static void
43 gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
44 {
45 combo->SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
46 }
47
48 static void
49 gtkcombobox_popupshown_callback(GObject *WXUNUSED(gobject),
50 GParamSpec *WXUNUSED(param_spec),
51 wxComboBox *combo)
52 {
53 gboolean isShown;
54 g_object_get( combo->m_widget, "popup-shown", &isShown, NULL );
55 wxCommandEvent event( isShown ? wxEVT_COMMAND_COMBOBOX_DROPDOWN
56 : wxEVT_COMMAND_COMBOBOX_CLOSEUP,
57 combo->GetId() );
58 event.SetEventObject( combo );
59 combo->HandleWindowEvent( event );
60 }
61 }
62
63 //-----------------------------------------------------------------------------
64 // wxComboBox
65 //-----------------------------------------------------------------------------
66
67 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxChoice)
68
69 BEGIN_EVENT_TABLE(wxComboBox, wxChoice)
70 EVT_CHAR(wxComboBox::OnChar)
71
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)
79
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)
87 END_EVENT_TABLE()
88
89 void wxComboBox::Init()
90 {
91 m_entry = NULL;
92 }
93
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 )
100 {
101 wxCArrayString chs(choices);
102
103 return Create( parent, id, value, pos, size, chs.GetCount(),
104 chs.GetStrings(), style, validator, name );
105 }
106
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 )
112 {
113 if (!PreCreation( parent, pos, size ) ||
114 !CreateBase( parent, id, pos, size, style, validator, name ))
115 {
116 wxFAIL_MSG( wxT("wxComboBox creation failed") );
117 return false;
118 }
119
120 if (HasFlag(wxCB_SORT))
121 m_strings = new wxSortedArrayString();
122
123 GTKCreateComboBoxWidget();
124
125 if (HasFlag(wxBORDER_NONE))
126 {
127 // Doesn't seem to work
128 // g_object_set (m_widget, "has-frame", FALSE, NULL);
129 }
130
131 GtkEntry * const entry = GetEntry();
132
133 if ( entry )
134 {
135 // Set it up to trigger default item on enter key press
136 gtk_entry_set_activates_default( entry,
137 !HasFlag(wxTE_PROCESS_ENTER) );
138
139 gtk_entry_set_editable( entry, TRUE );
140 }
141
142 Append(n, choices);
143
144 m_parent->DoAddChild( this );
145
146 if ( entry )
147 m_focusWidget = GTK_WIDGET( entry );
148
149 PostCreation(size);
150
151 if ( entry )
152 {
153 if (style & wxCB_READONLY)
154 {
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 );
161 }
162 else // editable combobox
163 {
164 // any value is accepted, even if it's not in our list
165 gtk_entry_set_text( entry, wxGTK_CONV(value) );
166 }
167
168 g_signal_connect_after (entry, "changed",
169 G_CALLBACK (gtkcombobox_text_changed_callback), this);
170 }
171
172 g_signal_connect_after (m_widget, "changed",
173 G_CALLBACK (gtkcombobox_changed_callback), this);
174
175 if ( gtk_check_version(2,10,0) )
176 {
177 g_signal_connect (m_widget, "notify::popup-shown",
178 G_CALLBACK (gtkcombobox_popupshown_callback), this);
179 }
180
181 SetInitialSize(size); // need this too because this is a wxControlWithItems
182
183 return true;
184 }
185
186 void wxComboBox::GTKCreateComboBoxWidget()
187 {
188 m_widget = gtk_combo_box_entry_new_text();
189 g_object_ref(m_widget);
190
191 m_entry = GTK_ENTRY(GTK_BIN(m_widget)->child);
192 }
193
194 GtkEditable *wxComboBox::GetEditable() const
195 {
196 return GTK_EDITABLE( GTK_BIN(m_widget)->child );
197 }
198
199 void wxComboBox::OnChar( wxKeyEvent &event )
200 {
201 switch ( event.GetKeyCode() )
202 {
203 case WXK_RETURN:
204 if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() )
205 {
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 );
211
212 if ( HandleWindowEvent(eventEnter) )
213 {
214 // Catch GTK event so that GTK doesn't open the drop
215 // down list upon RETURN.
216 return;
217 }
218 }
219 break;
220 }
221
222 event.Skip();
223 }
224
225 void wxComboBox::DisableEvents()
226 {
227 if ( GetEntry() )
228 g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
229 (gpointer)gtkcombobox_text_changed_callback, this);
230
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);
235 }
236
237 void wxComboBox::EnableEvents()
238 {
239 if ( GetEntry() )
240 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child,
241 (gpointer)gtkcombobox_text_changed_callback, this);
242
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);
247 }
248
249 GtkWidget* wxComboBox::GetConnectWidget()
250 {
251 return GTK_WIDGET( GetEntry() );
252 }
253
254 GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
255 {
256 wxUnusedVar(windows);
257
258 return GetEntry()->text_area;
259 }
260
261 // static
262 wxVisualAttributes
263 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
264 {
265 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
266 }
267
268 // ----------------------------------------------------------------------------
269 // standard event handling
270 // ----------------------------------------------------------------------------
271
272 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
273 {
274 Cut();
275 }
276
277 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
278 {
279 Copy();
280 }
281
282 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
283 {
284 Paste();
285 }
286
287 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
288 {
289 Undo();
290 }
291
292 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
293 {
294 Redo();
295 }
296
297 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
298 {
299 RemoveSelection();
300 }
301
302 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
303 {
304 SelectAll();
305 }
306
307 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
308 {
309 event.Enable( CanCut() );
310 }
311
312 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
313 {
314 event.Enable( CanCopy() );
315 }
316
317 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
318 {
319 event.Enable( CanPaste() );
320 }
321
322 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
323 {
324 event.Enable( CanUndo() );
325 }
326
327 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
328 {
329 event.Enable( CanRedo() );
330 }
331
332 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
333 {
334 event.Enable(HasSelection() && IsEditable()) ;
335 }
336
337 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
338 {
339 event.Enable(!wxTextEntry::IsEmpty());
340 }
341
342 #endif // wxUSE_COMBOBOX