Make wxComboBox::Popup() and ::Dismiss() emit events as per the docs, fixes (wxComboB...
[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::EnableTextChangedEvents(bool enable)
226 {
227 if ( !GetEntry() )
228 return;
229
230 if ( enable )
231 {
232 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child,
233 (gpointer)gtkcombobox_text_changed_callback, this);
234 }
235 else // disable
236 {
237 g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
238 (gpointer)gtkcombobox_text_changed_callback, this);
239 }
240 }
241
242 void wxComboBox::GTKDisableEvents()
243 {
244 EnableTextChangedEvents(false);
245
246 g_signal_handlers_block_by_func(m_widget,
247 (gpointer)gtkcombobox_changed_callback, this);
248 g_signal_handlers_block_by_func(m_widget,
249 (gpointer)gtkcombobox_popupshown_callback, this);
250 }
251
252 void wxComboBox::GTKEnableEvents()
253 {
254 EnableTextChangedEvents(true);
255
256 g_signal_handlers_unblock_by_func(m_widget,
257 (gpointer)gtkcombobox_changed_callback, this);
258 g_signal_handlers_unblock_by_func(m_widget,
259 (gpointer)gtkcombobox_popupshown_callback, this);
260 }
261
262 GtkWidget* wxComboBox::GetConnectWidget()
263 {
264 return GTK_WIDGET( GetEntry() );
265 }
266
267 GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const
268 {
269 return GetEntry()->text_area;
270 }
271
272 // static
273 wxVisualAttributes
274 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
275 {
276 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
277 }
278
279 // ----------------------------------------------------------------------------
280 // standard event handling
281 // ----------------------------------------------------------------------------
282
283 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
284 {
285 Cut();
286 }
287
288 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
289 {
290 Copy();
291 }
292
293 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
294 {
295 Paste();
296 }
297
298 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
299 {
300 Undo();
301 }
302
303 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
304 {
305 Redo();
306 }
307
308 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
309 {
310 RemoveSelection();
311 }
312
313 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
314 {
315 SelectAll();
316 }
317
318 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
319 {
320 event.Enable( CanCut() );
321 }
322
323 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
324 {
325 event.Enable( CanCopy() );
326 }
327
328 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
329 {
330 event.Enable( CanPaste() );
331 }
332
333 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
334 {
335 event.Enable( CanUndo() );
336 }
337
338 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
339 {
340 event.Enable( CanRedo() );
341 }
342
343 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
344 {
345 event.Enable(HasSelection() && IsEditable()) ;
346 }
347
348 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
349 {
350 event.Enable(!wxTextEntry::IsEmpty());
351 }
352
353 void wxComboBox::Popup()
354 {
355 gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) );
356
357 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_DROPDOWN, GetId() );
358 event.SetEventObject( this );
359 HandleWindowEvent( event );
360 }
361
362 void wxComboBox::Dismiss()
363 {
364 gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) );
365
366 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_CLOSEUP, GetId() );
367 event.SetEventObject( this );
368 HandleWindowEvent( event );
369 }
370 #endif // wxUSE_COMBOBOX