1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/combobox.cpp
3 // Purpose:
4 // Author:      Robert Roebling
5 // Copyright:   (c) 1998 Robert Roebling
6 // Licence:     wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11 
12 #if wxUSE_COMBOBOX
13 
14 #include "wx/combobox.h"
15 
16 #ifndef WX_PRECOMP
17     #include "wx/intl.h"
18     #include "wx/settings.h"
19     #include "wx/textctrl.h"    // for wxEVT_TEXT
20     #include "wx/arrstr.h"
21 #endif
22 
23 #include <gtk/gtk.h>
24 #include "wx/gtk/private.h"
25 #include "wx/gtk/private/gtk2-compat.h"
26 
27 // ----------------------------------------------------------------------------
28 // GTK callbacks
29 // ----------------------------------------------------------------------------
30 
31 extern "C" {
32 static void
gtkcombobox_text_changed_callback(GtkWidget * WXUNUSED (widget),wxComboBox * combo)33 gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
34 {
35     wxCommandEvent event( wxEVT_TEXT, combo->GetId() );
36     event.SetString( combo->GetValue() );
37     event.SetEventObject( combo );
38     combo->HandleWindowEvent( event );
39 }
40 
41 static void
gtkcombobox_changed_callback(GtkWidget * WXUNUSED (widget),wxComboBox * combo)42 gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
43 {
44     combo->SendSelectionChangedEvent(wxEVT_COMBOBOX);
45 }
46 
47 static void
gtkcombobox_popupshown_callback(GObject * WXUNUSED (gobject),GParamSpec * WXUNUSED (param_spec),wxComboBox * combo)48 gtkcombobox_popupshown_callback(GObject *WXUNUSED(gobject),
49                                 GParamSpec *WXUNUSED(param_spec),
50                                 wxComboBox *combo)
51 {
52     gboolean isShown;
53     g_object_get( combo->m_widget, "popup-shown", &isShown, NULL );
54     wxCommandEvent event( isShown ? wxEVT_COMBOBOX_DROPDOWN
55                                   : wxEVT_COMBOBOX_CLOSEUP,
56                           combo->GetId() );
57     event.SetEventObject( combo );
58     combo->HandleWindowEvent( event );
59 }
60 
61 }
62 
63 //-----------------------------------------------------------------------------
64 // wxComboBox
65 //-----------------------------------------------------------------------------
66 
BEGIN_EVENT_TABLE(wxComboBox,wxChoice)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 wxComboBox::~wxComboBox()
88 {
89     if (m_entry)
90     {
91         GTKDisconnect(m_entry);
92         g_object_remove_weak_pointer(G_OBJECT(m_entry), (void**)&m_entry);
93     }
94 }
95 
Init()96 void wxComboBox::Init()
97 {
98     m_entry = NULL;
99 }
100 
Create(wxWindow * parent,wxWindowID id,const wxString & value,const wxPoint & pos,const wxSize & size,const wxArrayString & choices,long style,const wxValidator & validator,const wxString & name)101 bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
102                          const wxString& value,
103                          const wxPoint& pos, const wxSize& size,
104                          const wxArrayString& choices,
105                          long style, const wxValidator& validator,
106                          const wxString& name )
107 {
108     wxCArrayString chs(choices);
109 
110     return Create( parent, id, value, pos, size, chs.GetCount(),
111                    chs.GetStrings(), style, validator, name );
112 }
113 
Create(wxWindow * parent,wxWindowID id,const wxString & value,const wxPoint & pos,const wxSize & size,int n,const wxString choices[],long style,const wxValidator & validator,const wxString & name)114 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
115                          const wxPoint& pos, const wxSize& size,
116                          int n, const wxString choices[],
117                          long style, const wxValidator& validator,
118                          const wxString& name )
119 {
120     if (!PreCreation( parent, pos, size ) ||
121         !CreateBase( parent, id, pos, size, style, validator, name ))
122     {
123         wxFAIL_MSG( wxT("wxComboBox creation failed") );
124         return false;
125     }
126 
127     if (HasFlag(wxCB_SORT))
128         m_strings = new wxGtkCollatedArrayString();
129 
130     GTKCreateComboBoxWidget();
131 
132     if (HasFlag(wxBORDER_NONE))
133     {
134         // Doesn't seem to work
135         // g_object_set (m_widget, "has-frame", FALSE, NULL);
136     }
137 
138     GtkEntry * const entry = GetEntry();
139 
140     if ( entry )
141     {
142         // Set it up to trigger default item on enter key press
143         gtk_entry_set_activates_default( entry,
144                                          !HasFlag(wxTE_PROCESS_ENTER) );
145 
146         gtk_editable_set_editable(GTK_EDITABLE(entry), true);
147 #ifdef __WXGTK3__
148         gtk_entry_set_width_chars(entry, 0);
149 #endif
150     }
151 
152     Append(n, choices);
153 
154     m_parent->DoAddChild( this );
155 
156     if ( entry )
157         m_focusWidget = GTK_WIDGET( entry );
158 
159     PostCreation(size);
160 
161     if ( entry )
162     {
163         if (style & wxCB_READONLY)
164         {
165             // this will assert and do nothing if the value is not in our list
166             // of strings which is the desired behaviour (for consistency with
167             // wxMSW and also because it doesn't make sense to have a string
168             // which is not a possible choice in a read-only combobox)
169             SetStringSelection(value);
170             gtk_editable_set_editable(GTK_EDITABLE(entry), false);
171         }
172         else // editable combobox
173         {
174             // any value is accepted, even if it's not in our list
175             gtk_entry_set_text( entry, wxGTK_CONV(value) );
176         }
177 
178         g_signal_connect_after (entry, "changed",
179                                 G_CALLBACK (gtkcombobox_text_changed_callback), this);
180 
181         GTKConnectInsertTextSignal(entry);
182         GTKConnectClipboardSignals(GTK_WIDGET(entry));
183     }
184 
185     g_signal_connect_after (m_widget, "changed",
186                         G_CALLBACK (gtkcombobox_changed_callback), this);
187 
188 #ifndef __WXGTK3__
189     if ( !gtk_check_version(2,10,0) )
190 #endif
191     {
192         g_signal_connect (m_widget, "notify::popup-shown",
193                           G_CALLBACK (gtkcombobox_popupshown_callback), this);
194     }
195 
196     return true;
197 }
198 
GTKCreateComboBoxWidget()199 void wxComboBox::GTKCreateComboBoxWidget()
200 {
201 #ifdef __WXGTK3__
202     m_widget = gtk_combo_box_text_new_with_entry();
203 #else
204     m_widget = gtk_combo_box_entry_new_text();
205 #endif
206     g_object_ref(m_widget);
207 
208     m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget)));
209     g_object_add_weak_pointer(G_OBJECT(m_entry), (void**)&m_entry);
210 }
211 
GetEditable() const212 GtkEditable *wxComboBox::GetEditable() const
213 {
214     return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget)));
215 }
216 
OnChar(wxKeyEvent & event)217 void wxComboBox::OnChar( wxKeyEvent &event )
218 {
219     switch ( event.GetKeyCode() )
220     {
221         case WXK_RETURN:
222             if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() )
223             {
224                 // GTK automatically selects an item if its in the list
225                 wxCommandEvent eventEnter(wxEVT_TEXT_ENTER, GetId());
226                 eventEnter.SetString( GetValue() );
227                 eventEnter.SetInt( GetSelection() );
228                 eventEnter.SetEventObject( this );
229 
230                 if ( HandleWindowEvent(eventEnter) )
231                 {
232                     // Catch GTK event so that GTK doesn't open the drop
233                     // down list upon RETURN.
234                     return;
235                 }
236             }
237             break;
238     }
239 
240     event.Skip();
241 }
242 
EnableTextChangedEvents(bool enable)243 void wxComboBox::EnableTextChangedEvents(bool enable)
244 {
245     if ( !GetEntry() )
246         return;
247 
248     if ( enable )
249     {
250         g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
251             (gpointer)gtkcombobox_text_changed_callback, this);
252     }
253     else // disable
254     {
255         g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
256             (gpointer)gtkcombobox_text_changed_callback, this);
257     }
258 }
259 
GTKDisableEvents()260 void wxComboBox::GTKDisableEvents()
261 {
262     EnableTextChangedEvents(false);
263 
264     g_signal_handlers_block_by_func(m_widget,
265         (gpointer)gtkcombobox_changed_callback, this);
266     g_signal_handlers_block_by_func(m_widget,
267         (gpointer)gtkcombobox_popupshown_callback, this);
268 }
269 
GTKEnableEvents()270 void wxComboBox::GTKEnableEvents()
271 {
272     EnableTextChangedEvents(true);
273 
274     g_signal_handlers_unblock_by_func(m_widget,
275         (gpointer)gtkcombobox_changed_callback, this);
276     g_signal_handlers_unblock_by_func(m_widget,
277         (gpointer)gtkcombobox_popupshown_callback, this);
278 }
279 
GetConnectWidget()280 GtkWidget* wxComboBox::GetConnectWidget()
281 {
282     return GTK_WIDGET( GetEntry() );
283 }
284 
GTKGetWindow(wxArrayGdkWindows &) const285 GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const
286 {
287 #ifdef __WXGTK3__
288     GdkWindow* wxGTKFindWindow(GtkWidget* widget);
289     return wxGTKFindWindow(GTK_WIDGET(GetEntry()));
290 #else
291     return gtk_entry_get_text_window(GetEntry());
292 #endif
293 }
294 
295 // static
296 wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant WXUNUSED (variant))297 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
298 {
299 #ifdef __WXGTK3__
300     return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new_with_entry(), true);
301 #else
302     return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new(), true);
303 #endif
304 }
305 
SetValue(const wxString & value)306 void wxComboBox::SetValue(const wxString& value)
307 {
308     if ( HasFlag(wxCB_READONLY) )
309         SetStringSelection(value);
310     else
311         wxTextEntry::SetValue(value);
312 }
313 
SetString(unsigned int n,const wxString & text)314 void wxComboBox::SetString(unsigned int n, const wxString& text)
315 {
316     wxChoice::SetString(n, text);
317 
318     if ( static_cast<int>(n) == GetSelection() )
319     {
320         // We also need to update the currently shown text, for consistency
321         // with wxMSW and also because it makes sense as leaving the old string
322         // in the text but not in the list would be confusing to the user.
323         SetValue(text);
324 
325         // And we need to keep the selection unchanged, modifying the item is
326         // not supposed to deselect it.
327         SetSelection(n);
328     }
329 }
330 
331 // ----------------------------------------------------------------------------
332 // standard event handling
333 // ----------------------------------------------------------------------------
334 
OnCut(wxCommandEvent & WXUNUSED (event))335 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
336 {
337     Cut();
338 }
339 
OnCopy(wxCommandEvent & WXUNUSED (event))340 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
341 {
342     Copy();
343 }
344 
OnPaste(wxCommandEvent & WXUNUSED (event))345 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
346 {
347     Paste();
348 }
349 
OnUndo(wxCommandEvent & WXUNUSED (event))350 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
351 {
352     Undo();
353 }
354 
OnRedo(wxCommandEvent & WXUNUSED (event))355 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
356 {
357     Redo();
358 }
359 
OnDelete(wxCommandEvent & WXUNUSED (event))360 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
361 {
362     RemoveSelection();
363 }
364 
OnSelectAll(wxCommandEvent & WXUNUSED (event))365 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
366 {
367     SelectAll();
368 }
369 
OnUpdateCut(wxUpdateUIEvent & event)370 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
371 {
372     event.Enable( CanCut() );
373 }
374 
OnUpdateCopy(wxUpdateUIEvent & event)375 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
376 {
377     event.Enable( CanCopy() );
378 }
379 
OnUpdatePaste(wxUpdateUIEvent & event)380 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
381 {
382     event.Enable( CanPaste() );
383 }
384 
OnUpdateUndo(wxUpdateUIEvent & event)385 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
386 {
387     event.Enable( CanUndo() );
388 }
389 
OnUpdateRedo(wxUpdateUIEvent & event)390 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
391 {
392     event.Enable( CanRedo() );
393 }
394 
OnUpdateDelete(wxUpdateUIEvent & event)395 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
396 {
397     event.Enable(HasSelection() && IsEditable()) ;
398 }
399 
OnUpdateSelectAll(wxUpdateUIEvent & event)400 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
401 {
402     event.Enable(!wxTextEntry::IsEmpty());
403 }
404 
Popup()405 void wxComboBox::Popup()
406 {
407      gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) );
408 }
409 
Dismiss()410 void wxComboBox::Dismiss()
411 {
412     gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) );
413 }
414 
DoGetSizeFromTextSize(int xlen,int ylen) const415 wxSize wxComboBox::DoGetSizeFromTextSize(int xlen, int ylen) const
416 {
417     wxSize tsize( wxChoice::DoGetSizeFromTextSize(xlen, ylen) );
418 
419     GtkEntry* entry = GetEntry();
420     if (entry)
421     {
422         // Add the margins we have previously set, but only the horizontal border
423         // as vertical one has been taken account in the previous call.
424         // Also get other GTK+ margins.
425         tsize.IncBy(GTKGetEntryMargins(entry).x, 0);
426     }
427 
428     return tsize;
429 }
430 
431 #endif // wxUSE_COMBOBOX
432