]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
Initialize wxCSConv immediately instead of deferring it.
[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 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_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 void wxComboBox::SetValue(const wxString& value)
280 {
281 if ( HasFlag(wxCB_READONLY) )
282 SetStringSelection(value);
283 else
284 wxTextEntry::SetValue(value);
285 }
286
287 // ----------------------------------------------------------------------------
288 // standard event handling
289 // ----------------------------------------------------------------------------
290
291 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
292 {
293 Cut();
294 }
295
296 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
297 {
298 Copy();
299 }
300
301 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
302 {
303 Paste();
304 }
305
306 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
307 {
308 Undo();
309 }
310
311 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
312 {
313 Redo();
314 }
315
316 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
317 {
318 RemoveSelection();
319 }
320
321 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
322 {
323 SelectAll();
324 }
325
326 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
327 {
328 event.Enable( CanCut() );
329 }
330
331 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
332 {
333 event.Enable( CanCopy() );
334 }
335
336 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
337 {
338 event.Enable( CanPaste() );
339 }
340
341 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
342 {
343 event.Enable( CanUndo() );
344 }
345
346 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
347 {
348 event.Enable( CanRedo() );
349 }
350
351 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
352 {
353 event.Enable(HasSelection() && IsEditable()) ;
354 }
355
356 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
357 {
358 event.Enable(!wxTextEntry::IsEmpty());
359 }
360
361 void wxComboBox::Popup()
362 {
363 gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) );
364 }
365
366 void wxComboBox::Dismiss()
367 {
368 gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) );
369 }
370 #endif // wxUSE_COMBOBOX