]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
new file added
[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}
590f50d6 63}
a73ae836 64
e1e955e1
RR
65//-----------------------------------------------------------------------------
66// wxComboBox
53010e52
RR
67//-----------------------------------------------------------------------------
68
a2c94110 69BEGIN_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
87END_EVENT_TABLE()
88
e78c1d78
RR
89void wxComboBox::Init()
90{
91 m_entry = NULL;
92}
93
584ad2a3
MB
94bool 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
107bool 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))
c272f12f 121 m_strings = new wxGtkCollatedArrayString();
a236aa20 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
385e8575 139 gtk_editable_set_editable(GTK_EDITABLE(entry), true);
e78c1d78 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);
385e8575 160 gtk_editable_set_editable(GTK_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
9dc44eff 175#ifndef __WXGTK3__
42628481 176 if ( !gtk_check_version(2,10,0) )
9dc44eff 177#endif
8933fbc6
VZ
178 {
179 g_signal_connect (m_widget, "notify::popup-shown",
180 G_CALLBACK (gtkcombobox_popupshown_callback), this);
181 }
182
170acdc9 183 SetInitialSize(size); // need this too because this is a wxControlWithItems
805dd538 184
7d8268a1 185 return true;
fd0eed64
RR
186}
187
e78c1d78 188void wxComboBox::GTKCreateComboBoxWidget()
ff654490 189{
9dc44eff
PC
190#ifdef __WXGTK3__
191 m_widget = gtk_combo_box_text_new_with_entry();
192#else
e78c1d78 193 m_widget = gtk_combo_box_entry_new_text();
9dc44eff 194#endif
9ff9d30c 195 g_object_ref(m_widget);
e78c1d78 196
385e8575 197 m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget)));
ff654490
VZ
198}
199
0ec1179b
VZ
200GtkEditable *wxComboBox::GetEditable() const
201{
385e8575 202 return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget)));
0ec1179b
VZ
203}
204
8a85884a
VZ
205void wxComboBox::OnChar( wxKeyEvent &event )
206{
643c973b 207 switch ( event.GetKeyCode() )
8a85884a 208 {
643c973b 209 case WXK_RETURN:
e78c1d78 210 if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() )
3352cfff 211 {
643c973b
VZ
212 // GTK automatically selects an item if its in the list
213 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
214 eventEnter.SetString( GetValue() );
215 eventEnter.SetInt( GetSelection() );
216 eventEnter.SetEventObject( this );
217
937013e0 218 if ( HandleWindowEvent(eventEnter) )
643c973b
VZ
219 {
220 // Catch GTK event so that GTK doesn't open the drop
221 // down list upon RETURN.
222 return;
223 }
3352cfff 224 }
643c973b 225 break;
8a85884a 226 }
30ed6e5c 227
7cf8cb48 228 event.Skip();
8a85884a
VZ
229}
230
b526f9d6 231void wxComboBox::EnableTextChangedEvents(bool enable)
953704c1 232{
b526f9d6
VZ
233 if ( !GetEntry() )
234 return;
235
236 if ( enable )
237 {
385e8575 238 g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
b526f9d6
VZ
239 (gpointer)gtkcombobox_text_changed_callback, this);
240 }
241 else // disable
242 {
385e8575 243 g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
e78c1d78 244 (gpointer)gtkcombobox_text_changed_callback, this);
b526f9d6
VZ
245 }
246}
247
248void wxComboBox::GTKDisableEvents()
249{
250 EnableTextChangedEvents(false);
8228b893 251
ff654490
VZ
252 g_signal_handlers_block_by_func(m_widget,
253 (gpointer)gtkcombobox_changed_callback, this);
8933fbc6
VZ
254 g_signal_handlers_block_by_func(m_widget,
255 (gpointer)gtkcombobox_popupshown_callback, this);
953704c1
RR
256}
257
dec7b5a8 258void wxComboBox::GTKEnableEvents()
953704c1 259{
b526f9d6 260 EnableTextChangedEvents(true);
ea2d542c 261
ff654490
VZ
262 g_signal_handlers_unblock_by_func(m_widget,
263 (gpointer)gtkcombobox_changed_callback, this);
8933fbc6
VZ
264 g_signal_handlers_unblock_by_func(m_widget,
265 (gpointer)gtkcombobox_popupshown_callback, this);
868a2826 266}
b4071e91 267
fd0eed64 268GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 269{
ff654490 270 return GTK_WIDGET( GetEntry() );
97b3455a
RR
271}
272
65391c8f 273GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const
b4071e91 274{
9dc44eff
PC
275#ifdef __WXGTK3__
276 // no access to internal GdkWindows
277 return NULL;
278#else
385e8575 279 return gtk_entry_get_text_window(GetEntry());
9dc44eff 280#endif
b4071e91 281}
ac57418f 282
9d522606
RD
283// static
284wxVisualAttributes
285wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
286{
9dc44eff
PC
287#ifdef __WXGTK3__
288 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new_with_entry, true);
289#else
ff654490 290 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
9dc44eff 291#endif
9d522606
RD
292}
293
9572bf1d
RR
294void wxComboBox::SetValue(const wxString& value)
295{
296 if ( HasFlag(wxCB_READONLY) )
297 SetStringSelection(value);
298 else
299 wxTextEntry::SetValue(value);
300}
301
a9ed8caa
VZ
302void wxComboBox::SetString(unsigned int n, const wxString& text)
303{
304 wxChoice::SetString(n, text);
305
306 if ( static_cast<int>(n) == GetSelection() )
307 {
308 // We also need to update the currently shown text, for consistency
309 // with wxMSW and also because it makes sense as leaving the old string
310 // in the text but not in the list would be confusing to the user.
311 SetValue(text);
182cad34
VZ
312
313 // And we need to keep the selection unchanged, modifying the item is
314 // not supposed to deselect it.
315 SetSelection(n);
a9ed8caa
VZ
316 }
317}
318
150e31d2
JS
319// ----------------------------------------------------------------------------
320// standard event handling
321// ----------------------------------------------------------------------------
322
323void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
324{
325 Cut();
326}
327
328void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
329{
330 Copy();
331}
332
333void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
334{
335 Paste();
336}
337
338void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
339{
340 Undo();
341}
342
343void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
344{
345 Redo();
346}
347
348void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
349{
5a25f858 350 RemoveSelection();
150e31d2
JS
351}
352
353void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
354{
e976429d 355 SelectAll();
150e31d2
JS
356}
357
358void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
359{
360 event.Enable( CanCut() );
361}
362
363void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
364{
365 event.Enable( CanCopy() );
366}
367
368void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
369{
370 event.Enable( CanPaste() );
371}
372
373void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
374{
375 event.Enable( CanUndo() );
376}
377
378void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
379{
380 event.Enable( CanRedo() );
381}
382
383void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
384{
385 event.Enable(HasSelection() && IsEditable()) ;
386}
387
388void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
389{
e976429d 390 event.Enable(!wxTextEntry::IsEmpty());
150e31d2
JS
391}
392
d1d1f817
VZ
393void wxComboBox::Popup()
394{
6008ff4a 395 gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) );
d1d1f817
VZ
396}
397
398void wxComboBox::Dismiss()
399{
400 gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) );
401}
0ec1179b 402#endif // wxUSE_COMBOBOX