]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
d29e2acb48d87f1bcb1f85e8d8f097ba1dc26ba6
[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 <gtk/gtk.h>
25 #include "wx/gtk/private.h"
26 #include "wx/gtk/private/gtk2-compat.h"
27
28 // ----------------------------------------------------------------------------
29 // GTK callbacks
30 // ----------------------------------------------------------------------------
31
32 extern "C" {
33 static void
34 gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
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 //-----------------------------------------------------------------------------
65 // wxComboBox
66 //-----------------------------------------------------------------------------
67
68 BEGIN_EVENT_TABLE(wxComboBox, wxChoice)
69 EVT_CHAR(wxComboBox::OnChar)
70
71 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
72 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
73 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
74 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
75 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
76 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
77 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
78
79 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
80 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
81 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
82 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
83 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
84 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
85 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
86 END_EVENT_TABLE()
87
88 wxComboBox::~wxComboBox()
89 {
90 if (m_entry)
91 GTKDisconnect(m_entry);
92 }
93
94 void wxComboBox::Init()
95 {
96 m_entry = NULL;
97 }
98
99 bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
100 const wxString& value,
101 const wxPoint& pos, const wxSize& size,
102 const wxArrayString& choices,
103 long style, const wxValidator& validator,
104 const wxString& name )
105 {
106 wxCArrayString chs(choices);
107
108 return Create( parent, id, value, pos, size, chs.GetCount(),
109 chs.GetStrings(), style, validator, name );
110 }
111
112 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
113 const wxPoint& pos, const wxSize& size,
114 int n, const wxString choices[],
115 long style, const wxValidator& validator,
116 const wxString& name )
117 {
118 if (!PreCreation( parent, pos, size ) ||
119 !CreateBase( parent, id, pos, size, style, validator, name ))
120 {
121 wxFAIL_MSG( wxT("wxComboBox creation failed") );
122 return false;
123 }
124
125 if (HasFlag(wxCB_SORT))
126 m_strings = new wxGtkCollatedArrayString();
127
128 GTKCreateComboBoxWidget();
129
130 if (HasFlag(wxBORDER_NONE))
131 {
132 // Doesn't seem to work
133 // g_object_set (m_widget, "has-frame", FALSE, NULL);
134 }
135
136 GtkEntry * const entry = GetEntry();
137
138 if ( entry )
139 {
140 // Set it up to trigger default item on enter key press
141 gtk_entry_set_activates_default( entry,
142 !HasFlag(wxTE_PROCESS_ENTER) );
143
144 gtk_editable_set_editable(GTK_EDITABLE(entry), true);
145 }
146
147 Append(n, choices);
148
149 m_parent->DoAddChild( this );
150
151 if ( entry )
152 m_focusWidget = GTK_WIDGET( entry );
153
154 PostCreation(size);
155
156 if ( entry )
157 {
158 if (style & wxCB_READONLY)
159 {
160 // this will assert and do nothing if the value is not in our list
161 // of strings which is the desired behaviour (for consistency with
162 // wxMSW and also because it doesn't make sense to have a string
163 // which is not a possible choice in a read-only combobox)
164 SetStringSelection(value);
165 gtk_editable_set_editable(GTK_EDITABLE(entry), false);
166 }
167 else // editable combobox
168 {
169 // any value is accepted, even if it's not in our list
170 gtk_entry_set_text( entry, wxGTK_CONV(value) );
171 }
172
173 g_signal_connect_after (entry, "changed",
174 G_CALLBACK (gtkcombobox_text_changed_callback), this);
175
176 GTKConnectClipboardSignals(GTK_WIDGET(entry));
177 }
178
179 g_signal_connect_after (m_widget, "changed",
180 G_CALLBACK (gtkcombobox_changed_callback), this);
181
182 #ifndef __WXGTK3__
183 if ( !gtk_check_version(2,10,0) )
184 #endif
185 {
186 g_signal_connect (m_widget, "notify::popup-shown",
187 G_CALLBACK (gtkcombobox_popupshown_callback), this);
188 }
189
190 SetInitialSize(size); // need this too because this is a wxControlWithItems
191
192 return true;
193 }
194
195 void wxComboBox::GTKCreateComboBoxWidget()
196 {
197 #ifdef __WXGTK3__
198 m_widget = gtk_combo_box_text_new_with_entry();
199 #else
200 m_widget = gtk_combo_box_entry_new_text();
201 #endif
202 g_object_ref(m_widget);
203
204 m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget)));
205 }
206
207 GtkEditable *wxComboBox::GetEditable() const
208 {
209 return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget)));
210 }
211
212 void wxComboBox::OnChar( wxKeyEvent &event )
213 {
214 switch ( event.GetKeyCode() )
215 {
216 case WXK_RETURN:
217 if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() )
218 {
219 // GTK automatically selects an item if its in the list
220 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
221 eventEnter.SetString( GetValue() );
222 eventEnter.SetInt( GetSelection() );
223 eventEnter.SetEventObject( this );
224
225 if ( HandleWindowEvent(eventEnter) )
226 {
227 // Catch GTK event so that GTK doesn't open the drop
228 // down list upon RETURN.
229 return;
230 }
231 }
232 break;
233 }
234
235 event.Skip();
236 }
237
238 void wxComboBox::EnableTextChangedEvents(bool enable)
239 {
240 if ( !GetEntry() )
241 return;
242
243 if ( enable )
244 {
245 g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
246 (gpointer)gtkcombobox_text_changed_callback, this);
247 }
248 else // disable
249 {
250 g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
251 (gpointer)gtkcombobox_text_changed_callback, this);
252 }
253 }
254
255 void wxComboBox::GTKDisableEvents()
256 {
257 EnableTextChangedEvents(false);
258
259 g_signal_handlers_block_by_func(m_widget,
260 (gpointer)gtkcombobox_changed_callback, this);
261 g_signal_handlers_block_by_func(m_widget,
262 (gpointer)gtkcombobox_popupshown_callback, this);
263 }
264
265 void wxComboBox::GTKEnableEvents()
266 {
267 EnableTextChangedEvents(true);
268
269 g_signal_handlers_unblock_by_func(m_widget,
270 (gpointer)gtkcombobox_changed_callback, this);
271 g_signal_handlers_unblock_by_func(m_widget,
272 (gpointer)gtkcombobox_popupshown_callback, this);
273 }
274
275 GtkWidget* wxComboBox::GetConnectWidget()
276 {
277 return GTK_WIDGET( GetEntry() );
278 }
279
280 GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const
281 {
282 #ifdef __WXGTK3__
283 // no access to internal GdkWindows
284 return NULL;
285 #else
286 return gtk_entry_get_text_window(GetEntry());
287 #endif
288 }
289
290 // static
291 wxVisualAttributes
292 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
293 {
294 #ifdef __WXGTK3__
295 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new_with_entry, true);
296 #else
297 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
298 #endif
299 }
300
301 void wxComboBox::SetValue(const wxString& value)
302 {
303 if ( HasFlag(wxCB_READONLY) )
304 SetStringSelection(value);
305 else
306 wxTextEntry::SetValue(value);
307 }
308
309 void wxComboBox::SetString(unsigned int n, const wxString& text)
310 {
311 wxChoice::SetString(n, text);
312
313 if ( static_cast<int>(n) == GetSelection() )
314 {
315 // We also need to update the currently shown text, for consistency
316 // with wxMSW and also because it makes sense as leaving the old string
317 // in the text but not in the list would be confusing to the user.
318 SetValue(text);
319
320 // And we need to keep the selection unchanged, modifying the item is
321 // not supposed to deselect it.
322 SetSelection(n);
323 }
324 }
325
326 // ----------------------------------------------------------------------------
327 // standard event handling
328 // ----------------------------------------------------------------------------
329
330 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
331 {
332 Cut();
333 }
334
335 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
336 {
337 Copy();
338 }
339
340 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
341 {
342 Paste();
343 }
344
345 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
346 {
347 Undo();
348 }
349
350 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
351 {
352 Redo();
353 }
354
355 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
356 {
357 RemoveSelection();
358 }
359
360 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
361 {
362 SelectAll();
363 }
364
365 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
366 {
367 event.Enable( CanCut() );
368 }
369
370 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
371 {
372 event.Enable( CanCopy() );
373 }
374
375 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
376 {
377 event.Enable( CanPaste() );
378 }
379
380 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
381 {
382 event.Enable( CanUndo() );
383 }
384
385 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
386 {
387 event.Enable( CanRedo() );
388 }
389
390 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
391 {
392 event.Enable(HasSelection() && IsEditable()) ;
393 }
394
395 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
396 {
397 event.Enable(!wxTextEntry::IsEmpty());
398 }
399
400 void wxComboBox::Popup()
401 {
402 gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) );
403 }
404
405 void wxComboBox::Dismiss()
406 {
407 gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) );
408 }
409 #endif // wxUSE_COMBOBOX