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