]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
wxMSW: Use TBSTYLE_AUTOSIZE for toolbar buttons with horizontal text.
[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 if (!combo->m_hasVMT) return;
37
38 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
39 event.SetString( combo->GetValue() );
40 event.SetEventObject( combo );
41 combo->HandleWindowEvent( event );
42 }
43
44 static void
45 gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
46 {
47 combo->SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
48 }
49
50 static void
51 gtkcombobox_popupshown_callback(GObject *WXUNUSED(gobject),
52 GParamSpec *WXUNUSED(param_spec),
53 wxComboBox *combo)
54 {
55 gboolean isShown;
56 g_object_get( combo->m_widget, "popup-shown", &isShown, NULL );
57 wxCommandEvent event( isShown ? wxEVT_COMMAND_COMBOBOX_DROPDOWN
58 : wxEVT_COMMAND_COMBOBOX_CLOSEUP,
59 combo->GetId() );
60 event.SetEventObject( combo );
61 combo->HandleWindowEvent( event );
62 }
63 }
64
65 //-----------------------------------------------------------------------------
66 // wxComboBox
67 //-----------------------------------------------------------------------------
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 wxGtkCollatedArrayString();
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_editable_set_editable(GTK_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_editable_set_editable(GTK_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 #ifndef __WXGTK3__
176 if ( !gtk_check_version(2,10,0) )
177 #endif
178 {
179 g_signal_connect (m_widget, "notify::popup-shown",
180 G_CALLBACK (gtkcombobox_popupshown_callback), this);
181 }
182
183 SetInitialSize(size); // need this too because this is a wxControlWithItems
184
185 return true;
186 }
187
188 void wxComboBox::GTKCreateComboBoxWidget()
189 {
190 #ifdef __WXGTK3__
191 m_widget = gtk_combo_box_text_new_with_entry();
192 #else
193 m_widget = gtk_combo_box_entry_new_text();
194 #endif
195 g_object_ref(m_widget);
196
197 m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget)));
198 }
199
200 GtkEditable *wxComboBox::GetEditable() const
201 {
202 return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget)));
203 }
204
205 void wxComboBox::OnChar( wxKeyEvent &event )
206 {
207 switch ( event.GetKeyCode() )
208 {
209 case WXK_RETURN:
210 if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() )
211 {
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 );
217
218 if ( HandleWindowEvent(eventEnter) )
219 {
220 // Catch GTK event so that GTK doesn't open the drop
221 // down list upon RETURN.
222 return;
223 }
224 }
225 break;
226 }
227
228 event.Skip();
229 }
230
231 void wxComboBox::EnableTextChangedEvents(bool enable)
232 {
233 if ( !GetEntry() )
234 return;
235
236 if ( enable )
237 {
238 g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
239 (gpointer)gtkcombobox_text_changed_callback, this);
240 }
241 else // disable
242 {
243 g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
244 (gpointer)gtkcombobox_text_changed_callback, this);
245 }
246 }
247
248 void wxComboBox::GTKDisableEvents()
249 {
250 EnableTextChangedEvents(false);
251
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);
256 }
257
258 void wxComboBox::GTKEnableEvents()
259 {
260 EnableTextChangedEvents(true);
261
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);
266 }
267
268 GtkWidget* wxComboBox::GetConnectWidget()
269 {
270 return GTK_WIDGET( GetEntry() );
271 }
272
273 GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const
274 {
275 #ifdef __WXGTK3__
276 // no access to internal GdkWindows
277 return NULL;
278 #else
279 return gtk_entry_get_text_window(GetEntry());
280 #endif
281 }
282
283 // static
284 wxVisualAttributes
285 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
286 {
287 #ifdef __WXGTK3__
288 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new_with_entry, true);
289 #else
290 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
291 #endif
292 }
293
294 void wxComboBox::SetValue(const wxString& value)
295 {
296 if ( HasFlag(wxCB_READONLY) )
297 SetStringSelection(value);
298 else
299 wxTextEntry::SetValue(value);
300 }
301
302 void wxComboBox::SetString(unsigned int n, const wxString& text)
303 {
304 wxChoice::SetString(n, text);
305
306 if ( static_cast<int>(n) == GetSelection() )
307 {
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.
311 SetValue(text);
312
313 // And we need to keep the selection unchanged, modifying the item is
314 // not supposed to deselect it.
315 SetSelection(n);
316 }
317 }
318
319 // ----------------------------------------------------------------------------
320 // standard event handling
321 // ----------------------------------------------------------------------------
322
323 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
324 {
325 Cut();
326 }
327
328 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
329 {
330 Copy();
331 }
332
333 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
334 {
335 Paste();
336 }
337
338 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
339 {
340 Undo();
341 }
342
343 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
344 {
345 Redo();
346 }
347
348 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
349 {
350 RemoveSelection();
351 }
352
353 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
354 {
355 SelectAll();
356 }
357
358 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
359 {
360 event.Enable( CanCut() );
361 }
362
363 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
364 {
365 event.Enable( CanCopy() );
366 }
367
368 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
369 {
370 event.Enable( CanPaste() );
371 }
372
373 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
374 {
375 event.Enable( CanUndo() );
376 }
377
378 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
379 {
380 event.Enable( CanRedo() );
381 }
382
383 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
384 {
385 event.Enable(HasSelection() && IsEditable()) ;
386 }
387
388 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
389 {
390 event.Enable(!wxTextEntry::IsEmpty());
391 }
392
393 void wxComboBox::Popup()
394 {
395 gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) );
396 }
397
398 void wxComboBox::Dismiss()
399 {
400 gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) );
401 }
402 #endif // wxUSE_COMBOBOX