]>
Commit | Line | Data |
---|---|---|
53010e52 | 1 | ///////////////////////////////////////////////////////////////////////////// |
11e62fe6 | 2 | // Name: src/gtk/combobox.cpp |
53010e52 RR |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
dbf858b5 | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 7 | // Licence: wxWindows licence |
53010e52 RR |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
dcf924a3 RR |
13 | #if wxUSE_COMBOBOX |
14 | ||
8228b893 WS |
15 | #include "wx/combobox.h" |
16 | ||
88a7a4e1 WS |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/intl.h" | |
9eddec69 | 19 | #include "wx/settings.h" |
fec9cc08 | 20 | #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED |
aaa6d89a | 21 | #include "wx/arrstr.h" |
88a7a4e1 WS |
22 | #endif |
23 | ||
9e691f46 | 24 | #include "wx/gtk/private.h" |
83624f79 | 25 | |
ff654490 VZ |
26 | // ---------------------------------------------------------------------------- |
27 | // GTK callbacks | |
28 | // ---------------------------------------------------------------------------- | |
461573cc | 29 | |
590f50d6 RR |
30 | extern "C" { |
31 | static void | |
32 | gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) | |
33 | { | |
590f50d6 RR |
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 ); | |
937013e0 | 39 | combo->HandleWindowEvent( event ); |
590f50d6 | 40 | } |
590f50d6 | 41 | |
590f50d6 RR |
42 | static void |
43 | gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) | |
44 | { | |
a2c94110 | 45 | combo->SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED); |
590f50d6 | 46 | } |
8933fbc6 VZ |
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 | } | |
590f50d6 | 61 | } |
a73ae836 | 62 | |
e1e955e1 RR |
63 | //----------------------------------------------------------------------------- |
64 | // wxComboBox | |
53010e52 RR |
65 | //----------------------------------------------------------------------------- |
66 | ||
a2c94110 | 67 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxChoice) |
53010e52 | 68 | |
a2c94110 | 69 | BEGIN_EVENT_TABLE(wxComboBox, wxChoice) |
8a85884a | 70 | EVT_CHAR(wxComboBox::OnChar) |
150e31d2 JS |
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) | |
b4071e91 RR |
87 | END_EVENT_TABLE() |
88 | ||
e78c1d78 RR |
89 | void wxComboBox::Init() |
90 | { | |
91 | m_entry = NULL; | |
92 | } | |
93 | ||
584ad2a3 MB |
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 | ||
fd0eed64 RR |
107 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, |
108 | const wxPoint& pos, const wxSize& size, | |
109 | int n, const wxString choices[], | |
805dd538 VZ |
110 | long style, const wxValidator& validator, |
111 | const wxString& name ) | |
53010e52 | 112 | { |
db434467 | 113 | if (!PreCreation( parent, pos, size ) || |
4dcaf11a RR |
114 | !CreateBase( parent, id, pos, size, style, validator, name )) |
115 | { | |
223d09f6 | 116 | wxFAIL_MSG( wxT("wxComboBox creation failed") ); |
7d8268a1 | 117 | return false; |
4dcaf11a | 118 | } |
6de97a3b | 119 | |
2f515791 | 120 | if (HasFlag(wxCB_SORT)) |
a236aa20 VZ |
121 | m_strings = new wxSortedArrayString(); |
122 | ||
e78c1d78 | 123 | GTKCreateComboBoxWidget(); |
a2c94110 | 124 | |
2f515791 RR |
125 | if (HasFlag(wxBORDER_NONE)) |
126 | { | |
127 | // Doesn't seem to work | |
128 | // g_object_set (m_widget, "has-frame", FALSE, NULL); | |
129 | } | |
130 | ||
ff654490 | 131 | GtkEntry * const entry = GetEntry(); |
3ca6a5f0 | 132 | |
e78c1d78 RR |
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 | } | |
805dd538 | 141 | |
a236aa20 | 142 | Append(n, choices); |
590f50d6 | 143 | |
f03fc89f | 144 | m_parent->DoAddChild( this ); |
30ed6e5c | 145 | |
e78c1d78 RR |
146 | if ( entry ) |
147 | m_focusWidget = GTK_WIDGET( entry ); | |
805dd538 | 148 | |
abdeb9e7 | 149 | PostCreation(size); |
53010e52 | 150 | |
e78c1d78 RR |
151 | if ( entry ) |
152 | { | |
e78c1d78 | 153 | if (style & wxCB_READONLY) |
a3281dbc VZ |
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); | |
e78c1d78 | 160 | gtk_entry_set_editable( entry, FALSE ); |
a3281dbc VZ |
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 | } | |
8228b893 | 167 | |
e78c1d78 RR |
168 | g_signal_connect_after (entry, "changed", |
169 | G_CALLBACK (gtkcombobox_text_changed_callback), this); | |
170 | } | |
8228b893 | 171 | |
ff654490 VZ |
172 | g_signal_connect_after (m_widget, "changed", |
173 | G_CALLBACK (gtkcombobox_changed_callback), this); | |
f4322df6 | 174 | |
42628481 | 175 | if ( !gtk_check_version(2,10,0) ) |
8933fbc6 VZ |
176 | { |
177 | g_signal_connect (m_widget, "notify::popup-shown", | |
178 | G_CALLBACK (gtkcombobox_popupshown_callback), this); | |
179 | } | |
180 | ||
170acdc9 | 181 | SetInitialSize(size); // need this too because this is a wxControlWithItems |
805dd538 | 182 | |
7d8268a1 | 183 | return true; |
fd0eed64 RR |
184 | } |
185 | ||
e78c1d78 | 186 | void wxComboBox::GTKCreateComboBoxWidget() |
ff654490 | 187 | { |
e78c1d78 | 188 | m_widget = gtk_combo_box_entry_new_text(); |
9ff9d30c | 189 | g_object_ref(m_widget); |
e78c1d78 RR |
190 | |
191 | m_entry = GTK_ENTRY(GTK_BIN(m_widget)->child); | |
ff654490 VZ |
192 | } |
193 | ||
0ec1179b VZ |
194 | GtkEditable *wxComboBox::GetEditable() const |
195 | { | |
ff654490 | 196 | return GTK_EDITABLE( GTK_BIN(m_widget)->child ); |
0ec1179b VZ |
197 | } |
198 | ||
8a85884a VZ |
199 | void wxComboBox::OnChar( wxKeyEvent &event ) |
200 | { | |
643c973b | 201 | switch ( event.GetKeyCode() ) |
8a85884a | 202 | { |
643c973b | 203 | case WXK_RETURN: |
e78c1d78 | 204 | if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() ) |
3352cfff | 205 | { |
643c973b VZ |
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 | ||
937013e0 | 212 | if ( HandleWindowEvent(eventEnter) ) |
643c973b VZ |
213 | { |
214 | // Catch GTK event so that GTK doesn't open the drop | |
215 | // down list upon RETURN. | |
216 | return; | |
217 | } | |
3352cfff | 218 | } |
643c973b | 219 | break; |
8a85884a | 220 | } |
30ed6e5c | 221 | |
7cf8cb48 | 222 | event.Skip(); |
8a85884a VZ |
223 | } |
224 | ||
b526f9d6 | 225 | void wxComboBox::EnableTextChangedEvents(bool enable) |
953704c1 | 226 | { |
b526f9d6 VZ |
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 | { | |
e78c1d78 RR |
237 | g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child, |
238 | (gpointer)gtkcombobox_text_changed_callback, this); | |
b526f9d6 VZ |
239 | } |
240 | } | |
241 | ||
242 | void wxComboBox::GTKDisableEvents() | |
243 | { | |
244 | EnableTextChangedEvents(false); | |
8228b893 | 245 | |
ff654490 VZ |
246 | g_signal_handlers_block_by_func(m_widget, |
247 | (gpointer)gtkcombobox_changed_callback, this); | |
8933fbc6 VZ |
248 | g_signal_handlers_block_by_func(m_widget, |
249 | (gpointer)gtkcombobox_popupshown_callback, this); | |
953704c1 RR |
250 | } |
251 | ||
dec7b5a8 | 252 | void wxComboBox::GTKEnableEvents() |
953704c1 | 253 | { |
b526f9d6 | 254 | EnableTextChangedEvents(true); |
ea2d542c | 255 | |
ff654490 VZ |
256 | g_signal_handlers_unblock_by_func(m_widget, |
257 | (gpointer)gtkcombobox_changed_callback, this); | |
8933fbc6 VZ |
258 | g_signal_handlers_unblock_by_func(m_widget, |
259 | (gpointer)gtkcombobox_popupshown_callback, this); | |
868a2826 | 260 | } |
b4071e91 | 261 | |
fd0eed64 | 262 | GtkWidget* wxComboBox::GetConnectWidget() |
97b3455a | 263 | { |
ff654490 | 264 | return GTK_WIDGET( GetEntry() ); |
97b3455a RR |
265 | } |
266 | ||
65391c8f | 267 | GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const |
b4071e91 | 268 | { |
ff654490 | 269 | return GetEntry()->text_area; |
b4071e91 | 270 | } |
ac57418f | 271 | |
9d522606 RD |
272 | // static |
273 | wxVisualAttributes | |
274 | wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
275 | { | |
ff654490 | 276 | return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true); |
9d522606 RD |
277 | } |
278 | ||
150e31d2 JS |
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 | { | |
5a25f858 | 310 | RemoveSelection(); |
150e31d2 JS |
311 | } |
312 | ||
313 | void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
314 | { | |
e976429d | 315 | SelectAll(); |
150e31d2 JS |
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 | { | |
e976429d | 350 | event.Enable(!wxTextEntry::IsEmpty()); |
150e31d2 JS |
351 | } |
352 | ||
d1d1f817 VZ |
353 | void wxComboBox::Popup() |
354 | { | |
355 | gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) ); | |
356 | } | |
357 | ||
358 | void wxComboBox::Dismiss() | |
359 | { | |
360 | gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) ); | |
361 | } | |
0ec1179b | 362 | #endif // wxUSE_COMBOBOX |