]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
Fix deprecating warning introduced in r72446.
[wxWidgets.git] / src / gtk / combobox.cpp
CommitLineData
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
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
32extern "C" {
33static void
34gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
35{
590f50d6
RR
36 if (!combo->m_hasVMT) return;
37
38 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
39 event.SetString( combo->GetValue() );
40 event.SetEventObject( combo );
937013e0 41 combo->HandleWindowEvent( event );
590f50d6 42}
590f50d6 43
590f50d6
RR
44static void
45gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
46{
a2c94110 47 combo->SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
590f50d6 48}
8933fbc6
VZ
49
50static void
51gtkcombobox_popupshown_callback(GObject *WXUNUSED(gobject),
52 GParamSpec *WXUNUSED(param_spec),
53 wxComboBox *combo)
54{
55 gboolean isShown;
56 g_object_get( combo->m_widget, "popup-shown", &isShown, NULL );
57 wxCommandEvent event( isShown ? wxEVT_COMMAND_COMBOBOX_DROPDOWN
58 : wxEVT_COMMAND_COMBOBOX_CLOSEUP,
59 combo->GetId() );
60 event.SetEventObject( combo );
61 combo->HandleWindowEvent( event );
62}
10434560 63
590f50d6 64}
a73ae836 65
e1e955e1
RR
66//-----------------------------------------------------------------------------
67// wxComboBox
53010e52
RR
68//-----------------------------------------------------------------------------
69
a2c94110 70BEGIN_EVENT_TABLE(wxComboBox, wxChoice)
8a85884a 71 EVT_CHAR(wxComboBox::OnChar)
150e31d2
JS
72
73 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
74 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
75 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
76 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
77 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
78 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
79 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
80
81 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
82 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
83 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
84 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
85 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
86 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
87 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
b4071e91
RR
88END_EVENT_TABLE()
89
e78c1d78
RR
90void wxComboBox::Init()
91{
92 m_entry = NULL;
93}
94
584ad2a3
MB
95bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
96 const wxString& value,
97 const wxPoint& pos, const wxSize& size,
98 const wxArrayString& choices,
99 long style, const wxValidator& validator,
100 const wxString& name )
101{
102 wxCArrayString chs(choices);
103
104 return Create( parent, id, value, pos, size, chs.GetCount(),
105 chs.GetStrings(), style, validator, name );
106}
107
fd0eed64
RR
108bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
109 const wxPoint& pos, const wxSize& size,
110 int n, const wxString choices[],
805dd538
VZ
111 long style, const wxValidator& validator,
112 const wxString& name )
53010e52 113{
db434467 114 if (!PreCreation( parent, pos, size ) ||
4dcaf11a
RR
115 !CreateBase( parent, id, pos, size, style, validator, name ))
116 {
223d09f6 117 wxFAIL_MSG( wxT("wxComboBox creation failed") );
7d8268a1 118 return false;
4dcaf11a 119 }
6de97a3b 120
2f515791 121 if (HasFlag(wxCB_SORT))
c272f12f 122 m_strings = new wxGtkCollatedArrayString();
a236aa20 123
e78c1d78 124 GTKCreateComboBoxWidget();
a2c94110 125
2f515791
RR
126 if (HasFlag(wxBORDER_NONE))
127 {
128 // Doesn't seem to work
129 // g_object_set (m_widget, "has-frame", FALSE, NULL);
130 }
131
ff654490 132 GtkEntry * const entry = GetEntry();
3ca6a5f0 133
e78c1d78
RR
134 if ( entry )
135 {
136 // Set it up to trigger default item on enter key press
137 gtk_entry_set_activates_default( entry,
138 !HasFlag(wxTE_PROCESS_ENTER) );
139
385e8575 140 gtk_editable_set_editable(GTK_EDITABLE(entry), true);
e78c1d78 141 }
805dd538 142
a236aa20 143 Append(n, choices);
590f50d6 144
f03fc89f 145 m_parent->DoAddChild( this );
30ed6e5c 146
e78c1d78
RR
147 if ( entry )
148 m_focusWidget = GTK_WIDGET( entry );
805dd538 149
abdeb9e7 150 PostCreation(size);
53010e52 151
e78c1d78
RR
152 if ( entry )
153 {
e78c1d78 154 if (style & wxCB_READONLY)
a3281dbc
VZ
155 {
156 // this will assert and do nothing if the value is not in our list
157 // of strings which is the desired behaviour (for consistency with
158 // wxMSW and also because it doesn't make sense to have a string
159 // which is not a possible choice in a read-only combobox)
160 SetStringSelection(value);
385e8575 161 gtk_editable_set_editable(GTK_EDITABLE(entry), false);
a3281dbc
VZ
162 }
163 else // editable combobox
164 {
165 // any value is accepted, even if it's not in our list
166 gtk_entry_set_text( entry, wxGTK_CONV(value) );
167 }
8228b893 168
e78c1d78
RR
169 g_signal_connect_after (entry, "changed",
170 G_CALLBACK (gtkcombobox_text_changed_callback), this);
10434560
VZ
171
172 GTKConnectClipboardSignals(GTK_WIDGET(entry));
e78c1d78 173 }
8228b893 174
ff654490
VZ
175 g_signal_connect_after (m_widget, "changed",
176 G_CALLBACK (gtkcombobox_changed_callback), this);
f4322df6 177
9dc44eff 178#ifndef __WXGTK3__
42628481 179 if ( !gtk_check_version(2,10,0) )
9dc44eff 180#endif
8933fbc6
VZ
181 {
182 g_signal_connect (m_widget, "notify::popup-shown",
183 G_CALLBACK (gtkcombobox_popupshown_callback), this);
184 }
185
170acdc9 186 SetInitialSize(size); // need this too because this is a wxControlWithItems
805dd538 187
7d8268a1 188 return true;
fd0eed64
RR
189}
190
e78c1d78 191void wxComboBox::GTKCreateComboBoxWidget()
ff654490 192{
9dc44eff
PC
193#ifdef __WXGTK3__
194 m_widget = gtk_combo_box_text_new_with_entry();
195#else
e78c1d78 196 m_widget = gtk_combo_box_entry_new_text();
9dc44eff 197#endif
9ff9d30c 198 g_object_ref(m_widget);
e78c1d78 199
385e8575 200 m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget)));
ff654490
VZ
201}
202
0ec1179b
VZ
203GtkEditable *wxComboBox::GetEditable() const
204{
385e8575 205 return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget)));
0ec1179b
VZ
206}
207
8a85884a
VZ
208void wxComboBox::OnChar( wxKeyEvent &event )
209{
643c973b 210 switch ( event.GetKeyCode() )
8a85884a 211 {
643c973b 212 case WXK_RETURN:
e78c1d78 213 if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() )
3352cfff 214 {
643c973b
VZ
215 // GTK automatically selects an item if its in the list
216 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
217 eventEnter.SetString( GetValue() );
218 eventEnter.SetInt( GetSelection() );
219 eventEnter.SetEventObject( this );
220
937013e0 221 if ( HandleWindowEvent(eventEnter) )
643c973b
VZ
222 {
223 // Catch GTK event so that GTK doesn't open the drop
224 // down list upon RETURN.
225 return;
226 }
3352cfff 227 }
643c973b 228 break;
8a85884a 229 }
30ed6e5c 230
7cf8cb48 231 event.Skip();
8a85884a
VZ
232}
233
b526f9d6 234void wxComboBox::EnableTextChangedEvents(bool enable)
953704c1 235{
b526f9d6
VZ
236 if ( !GetEntry() )
237 return;
238
239 if ( enable )
240 {
385e8575 241 g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
b526f9d6
VZ
242 (gpointer)gtkcombobox_text_changed_callback, this);
243 }
244 else // disable
245 {
385e8575 246 g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
e78c1d78 247 (gpointer)gtkcombobox_text_changed_callback, this);
b526f9d6
VZ
248 }
249}
250
251void wxComboBox::GTKDisableEvents()
252{
253 EnableTextChangedEvents(false);
8228b893 254
ff654490
VZ
255 g_signal_handlers_block_by_func(m_widget,
256 (gpointer)gtkcombobox_changed_callback, this);
8933fbc6
VZ
257 g_signal_handlers_block_by_func(m_widget,
258 (gpointer)gtkcombobox_popupshown_callback, this);
953704c1
RR
259}
260
dec7b5a8 261void wxComboBox::GTKEnableEvents()
953704c1 262{
b526f9d6 263 EnableTextChangedEvents(true);
ea2d542c 264
ff654490
VZ
265 g_signal_handlers_unblock_by_func(m_widget,
266 (gpointer)gtkcombobox_changed_callback, this);
8933fbc6
VZ
267 g_signal_handlers_unblock_by_func(m_widget,
268 (gpointer)gtkcombobox_popupshown_callback, this);
868a2826 269}
b4071e91 270
fd0eed64 271GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 272{
ff654490 273 return GTK_WIDGET( GetEntry() );
97b3455a
RR
274}
275
65391c8f 276GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const
b4071e91 277{
9dc44eff
PC
278#ifdef __WXGTK3__
279 // no access to internal GdkWindows
280 return NULL;
281#else
385e8575 282 return gtk_entry_get_text_window(GetEntry());
9dc44eff 283#endif
b4071e91 284}
ac57418f 285
9d522606
RD
286// static
287wxVisualAttributes
288wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
289{
9dc44eff
PC
290#ifdef __WXGTK3__
291 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new_with_entry, true);
292#else
ff654490 293 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
9dc44eff 294#endif
9d522606
RD
295}
296
9572bf1d
RR
297void wxComboBox::SetValue(const wxString& value)
298{
299 if ( HasFlag(wxCB_READONLY) )
300 SetStringSelection(value);
301 else
302 wxTextEntry::SetValue(value);
303}
304
a9ed8caa
VZ
305void wxComboBox::SetString(unsigned int n, const wxString& text)
306{
307 wxChoice::SetString(n, text);
308
309 if ( static_cast<int>(n) == GetSelection() )
310 {
311 // We also need to update the currently shown text, for consistency
312 // with wxMSW and also because it makes sense as leaving the old string
313 // in the text but not in the list would be confusing to the user.
314 SetValue(text);
182cad34
VZ
315
316 // And we need to keep the selection unchanged, modifying the item is
317 // not supposed to deselect it.
318 SetSelection(n);
a9ed8caa
VZ
319 }
320}
321
150e31d2
JS
322// ----------------------------------------------------------------------------
323// standard event handling
324// ----------------------------------------------------------------------------
325
326void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
327{
328 Cut();
329}
330
331void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
332{
333 Copy();
334}
335
336void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
337{
338 Paste();
339}
340
341void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
342{
343 Undo();
344}
345
346void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
347{
348 Redo();
349}
350
351void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
352{
5a25f858 353 RemoveSelection();
150e31d2
JS
354}
355
356void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
357{
e976429d 358 SelectAll();
150e31d2
JS
359}
360
361void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
362{
363 event.Enable( CanCut() );
364}
365
366void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
367{
368 event.Enable( CanCopy() );
369}
370
371void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
372{
373 event.Enable( CanPaste() );
374}
375
376void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
377{
378 event.Enable( CanUndo() );
379}
380
381void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
382{
383 event.Enable( CanRedo() );
384}
385
386void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
387{
388 event.Enable(HasSelection() && IsEditable()) ;
389}
390
391void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
392{
e976429d 393 event.Enable(!wxTextEntry::IsEmpty());
150e31d2
JS
394}
395
d1d1f817
VZ
396void wxComboBox::Popup()
397{
6008ff4a 398 gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) );
d1d1f817
VZ
399}
400
401void wxComboBox::Dismiss()
402{
403 gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) );
404}
0ec1179b 405#endif // wxUSE_COMBOBOX