]>
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" |
ce7fe42e | 20 | #include "wx/textctrl.h" // for wxEVT_TEXT |
aaa6d89a | 21 | #include "wx/arrstr.h" |
88a7a4e1 WS |
22 | #endif |
23 | ||
9dc44eff | 24 | #include <gtk/gtk.h> |
9e691f46 | 25 | #include "wx/gtk/private.h" |
9dc44eff | 26 | #include "wx/gtk/private/gtk2-compat.h" |
83624f79 | 27 | |
ff654490 VZ |
28 | // ---------------------------------------------------------------------------- |
29 | // GTK callbacks | |
30 | // ---------------------------------------------------------------------------- | |
461573cc | 31 | |
590f50d6 RR |
32 | extern "C" { |
33 | static void | |
34 | gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) | |
35 | { | |
ce7fe42e | 36 | wxCommandEvent event( wxEVT_TEXT, combo->GetId() ); |
590f50d6 RR |
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 | { | |
ce7fe42e | 45 | combo->SendSelectionChangedEvent(wxEVT_COMBOBOX); |
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 ); | |
ce7fe42e VZ |
55 | wxCommandEvent event( isShown ? wxEVT_COMBOBOX_DROPDOWN |
56 | : wxEVT_COMBOBOX_CLOSEUP, | |
8933fbc6 VZ |
57 | combo->GetId() ); |
58 | event.SetEventObject( combo ); | |
59 | combo->HandleWindowEvent( event ); | |
60 | } | |
10434560 | 61 | |
590f50d6 | 62 | } |
a73ae836 | 63 | |
e1e955e1 RR |
64 | //----------------------------------------------------------------------------- |
65 | // wxComboBox | |
53010e52 RR |
66 | //----------------------------------------------------------------------------- |
67 | ||
a2c94110 | 68 | BEGIN_EVENT_TABLE(wxComboBox, wxChoice) |
8a85884a | 69 | EVT_CHAR(wxComboBox::OnChar) |
150e31d2 JS |
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) | |
b4071e91 RR |
86 | END_EVENT_TABLE() |
87 | ||
8ab75332 PC |
88 | wxComboBox::~wxComboBox() |
89 | { | |
90 | if (m_entry) | |
91 | GTKDisconnect(m_entry); | |
92 | } | |
93 | ||
e78c1d78 RR |
94 | void wxComboBox::Init() |
95 | { | |
96 | m_entry = NULL; | |
97 | } | |
98 | ||
584ad2a3 MB |
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 | ||
fd0eed64 RR |
112 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, |
113 | const wxPoint& pos, const wxSize& size, | |
114 | int n, const wxString choices[], | |
805dd538 VZ |
115 | long style, const wxValidator& validator, |
116 | const wxString& name ) | |
53010e52 | 117 | { |
db434467 | 118 | if (!PreCreation( parent, pos, size ) || |
4dcaf11a RR |
119 | !CreateBase( parent, id, pos, size, style, validator, name )) |
120 | { | |
223d09f6 | 121 | wxFAIL_MSG( wxT("wxComboBox creation failed") ); |
7d8268a1 | 122 | return false; |
4dcaf11a | 123 | } |
6de97a3b | 124 | |
2f515791 | 125 | if (HasFlag(wxCB_SORT)) |
c272f12f | 126 | m_strings = new wxGtkCollatedArrayString(); |
a236aa20 | 127 | |
e78c1d78 | 128 | GTKCreateComboBoxWidget(); |
a2c94110 | 129 | |
2f515791 RR |
130 | if (HasFlag(wxBORDER_NONE)) |
131 | { | |
132 | // Doesn't seem to work | |
133 | // g_object_set (m_widget, "has-frame", FALSE, NULL); | |
134 | } | |
135 | ||
ff654490 | 136 | GtkEntry * const entry = GetEntry(); |
3ca6a5f0 | 137 | |
e78c1d78 RR |
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 | ||
385e8575 | 144 | gtk_editable_set_editable(GTK_EDITABLE(entry), true); |
e78c1d78 | 145 | } |
805dd538 | 146 | |
a236aa20 | 147 | Append(n, choices); |
590f50d6 | 148 | |
f03fc89f | 149 | m_parent->DoAddChild( this ); |
30ed6e5c | 150 | |
e78c1d78 RR |
151 | if ( entry ) |
152 | m_focusWidget = GTK_WIDGET( entry ); | |
805dd538 | 153 | |
abdeb9e7 | 154 | PostCreation(size); |
53010e52 | 155 | |
e78c1d78 RR |
156 | if ( entry ) |
157 | { | |
e78c1d78 | 158 | if (style & wxCB_READONLY) |
a3281dbc VZ |
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); | |
385e8575 | 165 | gtk_editable_set_editable(GTK_EDITABLE(entry), false); |
a3281dbc VZ |
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 | } | |
8228b893 | 172 | |
e78c1d78 RR |
173 | g_signal_connect_after (entry, "changed", |
174 | G_CALLBACK (gtkcombobox_text_changed_callback), this); | |
10434560 | 175 | |
b2c35774 | 176 | GTKConnectInsertTextSignal(entry); |
10434560 | 177 | GTKConnectClipboardSignals(GTK_WIDGET(entry)); |
e78c1d78 | 178 | } |
8228b893 | 179 | |
ff654490 VZ |
180 | g_signal_connect_after (m_widget, "changed", |
181 | G_CALLBACK (gtkcombobox_changed_callback), this); | |
f4322df6 | 182 | |
9dc44eff | 183 | #ifndef __WXGTK3__ |
42628481 | 184 | if ( !gtk_check_version(2,10,0) ) |
9dc44eff | 185 | #endif |
8933fbc6 VZ |
186 | { |
187 | g_signal_connect (m_widget, "notify::popup-shown", | |
188 | G_CALLBACK (gtkcombobox_popupshown_callback), this); | |
189 | } | |
190 | ||
7d8268a1 | 191 | return true; |
fd0eed64 RR |
192 | } |
193 | ||
e78c1d78 | 194 | void wxComboBox::GTKCreateComboBoxWidget() |
ff654490 | 195 | { |
9dc44eff PC |
196 | #ifdef __WXGTK3__ |
197 | m_widget = gtk_combo_box_text_new_with_entry(); | |
198 | #else | |
e78c1d78 | 199 | m_widget = gtk_combo_box_entry_new_text(); |
9dc44eff | 200 | #endif |
9ff9d30c | 201 | g_object_ref(m_widget); |
e78c1d78 | 202 | |
385e8575 | 203 | m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget))); |
ff654490 VZ |
204 | } |
205 | ||
0ec1179b VZ |
206 | GtkEditable *wxComboBox::GetEditable() const |
207 | { | |
385e8575 | 208 | return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget))); |
0ec1179b VZ |
209 | } |
210 | ||
8a85884a VZ |
211 | void wxComboBox::OnChar( wxKeyEvent &event ) |
212 | { | |
643c973b | 213 | switch ( event.GetKeyCode() ) |
8a85884a | 214 | { |
643c973b | 215 | case WXK_RETURN: |
e78c1d78 | 216 | if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() ) |
3352cfff | 217 | { |
643c973b | 218 | // GTK automatically selects an item if its in the list |
ce7fe42e | 219 | wxCommandEvent eventEnter(wxEVT_TEXT_ENTER, GetId()); |
643c973b VZ |
220 | eventEnter.SetString( GetValue() ); |
221 | eventEnter.SetInt( GetSelection() ); | |
222 | eventEnter.SetEventObject( this ); | |
223 | ||
937013e0 | 224 | if ( HandleWindowEvent(eventEnter) ) |
643c973b VZ |
225 | { |
226 | // Catch GTK event so that GTK doesn't open the drop | |
227 | // down list upon RETURN. | |
228 | return; | |
229 | } | |
3352cfff | 230 | } |
643c973b | 231 | break; |
8a85884a | 232 | } |
30ed6e5c | 233 | |
7cf8cb48 | 234 | event.Skip(); |
8a85884a VZ |
235 | } |
236 | ||
b526f9d6 | 237 | void wxComboBox::EnableTextChangedEvents(bool enable) |
953704c1 | 238 | { |
b526f9d6 VZ |
239 | if ( !GetEntry() ) |
240 | return; | |
241 | ||
242 | if ( enable ) | |
243 | { | |
385e8575 | 244 | g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(m_widget)), |
b526f9d6 VZ |
245 | (gpointer)gtkcombobox_text_changed_callback, this); |
246 | } | |
247 | else // disable | |
248 | { | |
385e8575 | 249 | g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(m_widget)), |
e78c1d78 | 250 | (gpointer)gtkcombobox_text_changed_callback, this); |
b526f9d6 VZ |
251 | } |
252 | } | |
253 | ||
254 | void wxComboBox::GTKDisableEvents() | |
255 | { | |
256 | EnableTextChangedEvents(false); | |
8228b893 | 257 | |
ff654490 VZ |
258 | g_signal_handlers_block_by_func(m_widget, |
259 | (gpointer)gtkcombobox_changed_callback, this); | |
8933fbc6 VZ |
260 | g_signal_handlers_block_by_func(m_widget, |
261 | (gpointer)gtkcombobox_popupshown_callback, this); | |
953704c1 RR |
262 | } |
263 | ||
dec7b5a8 | 264 | void wxComboBox::GTKEnableEvents() |
953704c1 | 265 | { |
b526f9d6 | 266 | EnableTextChangedEvents(true); |
ea2d542c | 267 | |
ff654490 VZ |
268 | g_signal_handlers_unblock_by_func(m_widget, |
269 | (gpointer)gtkcombobox_changed_callback, this); | |
8933fbc6 VZ |
270 | g_signal_handlers_unblock_by_func(m_widget, |
271 | (gpointer)gtkcombobox_popupshown_callback, this); | |
868a2826 | 272 | } |
b4071e91 | 273 | |
fd0eed64 | 274 | GtkWidget* wxComboBox::GetConnectWidget() |
97b3455a | 275 | { |
ff654490 | 276 | return GTK_WIDGET( GetEntry() ); |
97b3455a RR |
277 | } |
278 | ||
65391c8f | 279 | GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const |
b4071e91 | 280 | { |
9dc44eff PC |
281 | #ifdef __WXGTK3__ |
282 | // no access to internal GdkWindows | |
283 | return NULL; | |
284 | #else | |
385e8575 | 285 | return gtk_entry_get_text_window(GetEntry()); |
9dc44eff | 286 | #endif |
b4071e91 | 287 | } |
ac57418f | 288 | |
9d522606 RD |
289 | // static |
290 | wxVisualAttributes | |
291 | wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
292 | { | |
9dc44eff | 293 | #ifdef __WXGTK3__ |
7fff16b8 | 294 | return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new_with_entry(), true); |
9dc44eff | 295 | #else |
7fff16b8 | 296 | return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new(), true); |
9dc44eff | 297 | #endif |
9d522606 RD |
298 | } |
299 | ||
9572bf1d RR |
300 | void wxComboBox::SetValue(const wxString& value) |
301 | { | |
302 | if ( HasFlag(wxCB_READONLY) ) | |
303 | SetStringSelection(value); | |
304 | else | |
305 | wxTextEntry::SetValue(value); | |
306 | } | |
307 | ||
a9ed8caa VZ |
308 | void wxComboBox::SetString(unsigned int n, const wxString& text) |
309 | { | |
310 | wxChoice::SetString(n, text); | |
311 | ||
312 | if ( static_cast<int>(n) == GetSelection() ) | |
313 | { | |
314 | // We also need to update the currently shown text, for consistency | |
315 | // with wxMSW and also because it makes sense as leaving the old string | |
316 | // in the text but not in the list would be confusing to the user. | |
317 | SetValue(text); | |
182cad34 VZ |
318 | |
319 | // And we need to keep the selection unchanged, modifying the item is | |
320 | // not supposed to deselect it. | |
321 | SetSelection(n); | |
a9ed8caa VZ |
322 | } |
323 | } | |
324 | ||
150e31d2 JS |
325 | // ---------------------------------------------------------------------------- |
326 | // standard event handling | |
327 | // ---------------------------------------------------------------------------- | |
328 | ||
329 | void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event)) | |
330 | { | |
331 | Cut(); | |
332 | } | |
333 | ||
334 | void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
335 | { | |
336 | Copy(); | |
337 | } | |
338 | ||
339 | void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
340 | { | |
341 | Paste(); | |
342 | } | |
343 | ||
344 | void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
345 | { | |
346 | Undo(); | |
347 | } | |
348 | ||
349 | void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
350 | { | |
351 | Redo(); | |
352 | } | |
353 | ||
354 | void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event)) | |
355 | { | |
5a25f858 | 356 | RemoveSelection(); |
150e31d2 JS |
357 | } |
358 | ||
359 | void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
360 | { | |
e976429d | 361 | SelectAll(); |
150e31d2 JS |
362 | } |
363 | ||
364 | void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event) | |
365 | { | |
366 | event.Enable( CanCut() ); | |
367 | } | |
368 | ||
369 | void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event) | |
370 | { | |
371 | event.Enable( CanCopy() ); | |
372 | } | |
373 | ||
374 | void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event) | |
375 | { | |
376 | event.Enable( CanPaste() ); | |
377 | } | |
378 | ||
379 | void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event) | |
380 | { | |
381 | event.Enable( CanUndo() ); | |
382 | } | |
383 | ||
384 | void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event) | |
385 | { | |
386 | event.Enable( CanRedo() ); | |
387 | } | |
388 | ||
389 | void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event) | |
390 | { | |
391 | event.Enable(HasSelection() && IsEditable()) ; | |
392 | } | |
393 | ||
394 | void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
395 | { | |
e976429d | 396 | event.Enable(!wxTextEntry::IsEmpty()); |
150e31d2 JS |
397 | } |
398 | ||
d1d1f817 VZ |
399 | void wxComboBox::Popup() |
400 | { | |
6008ff4a | 401 | gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) ); |
d1d1f817 VZ |
402 | } |
403 | ||
404 | void wxComboBox::Dismiss() | |
405 | { | |
406 | gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) ); | |
407 | } | |
7a78a937 VZ |
408 | |
409 | wxSize wxComboBox::DoGetSizeFromTextSize(int xlen, int ylen) const | |
410 | { | |
411 | wxSize tsize( wxChoice::DoGetSizeFromTextSize(xlen, ylen) ); | |
412 | ||
6968a3b8 PC |
413 | GtkEntry* entry = GetEntry(); |
414 | if (entry) | |
415 | { | |
416 | // Add the margins we have previously set, but only the horizontal border | |
417 | // as vertical one has been taken account in the previous call. | |
418 | // Also get other GTK+ margins. | |
419 | tsize.IncBy(GTKGetEntryMargins(entry).x, 0); | |
420 | } | |
7a78a937 VZ |
421 | |
422 | return tsize; | |
423 | } | |
424 | ||
0ec1179b | 425 | #endif // wxUSE_COMBOBOX |