]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
using on all OSX variants
[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
9e691f46 24#include "wx/gtk/private.h"
83624f79 25
ff654490
VZ
26// ----------------------------------------------------------------------------
27// GTK callbacks
28// ----------------------------------------------------------------------------
461573cc 29
590f50d6
RR
30extern "C" {
31static void
32gtkcombobox_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
42static void
43gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
44{
a2c94110 45 combo->SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
590f50d6 46}
8933fbc6
VZ
47
48static void
49gtkcombobox_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 67IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxChoice)
53010e52 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))
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 186void 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
194GtkEditable *wxComboBox::GetEditable() const
195{
ff654490 196 return GTK_EDITABLE( GTK_BIN(m_widget)->child );
0ec1179b
VZ
197}
198
8a85884a
VZ
199void 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
953704c1
RR
225void wxComboBox::DisableEvents()
226{
e78c1d78
RR
227 if ( GetEntry() )
228 g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
229 (gpointer)gtkcombobox_text_changed_callback, this);
8228b893 230
ff654490
VZ
231 g_signal_handlers_block_by_func(m_widget,
232 (gpointer)gtkcombobox_changed_callback, this);
8933fbc6
VZ
233 g_signal_handlers_block_by_func(m_widget,
234 (gpointer)gtkcombobox_popupshown_callback, this);
953704c1
RR
235}
236
237void wxComboBox::EnableEvents()
238{
e78c1d78
RR
239 if ( GetEntry() )
240 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child,
241 (gpointer)gtkcombobox_text_changed_callback, this);
ea2d542c 242
ff654490
VZ
243 g_signal_handlers_unblock_by_func(m_widget,
244 (gpointer)gtkcombobox_changed_callback, this);
8933fbc6
VZ
245 g_signal_handlers_unblock_by_func(m_widget,
246 (gpointer)gtkcombobox_popupshown_callback, this);
868a2826 247}
b4071e91 248
fd0eed64 249GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 250{
ff654490 251 return GTK_WIDGET( GetEntry() );
97b3455a
RR
252}
253
65391c8f 254GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const
b4071e91 255{
ff654490 256 return GetEntry()->text_area;
b4071e91 257}
ac57418f 258
9d522606
RD
259// static
260wxVisualAttributes
261wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
262{
ff654490 263 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
9d522606
RD
264}
265
150e31d2
JS
266// ----------------------------------------------------------------------------
267// standard event handling
268// ----------------------------------------------------------------------------
269
270void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
271{
272 Cut();
273}
274
275void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
276{
277 Copy();
278}
279
280void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
281{
282 Paste();
283}
284
285void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
286{
287 Undo();
288}
289
290void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
291{
292 Redo();
293}
294
295void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
296{
5a25f858 297 RemoveSelection();
150e31d2
JS
298}
299
300void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
301{
e976429d 302 SelectAll();
150e31d2
JS
303}
304
305void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
306{
307 event.Enable( CanCut() );
308}
309
310void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
311{
312 event.Enable( CanCopy() );
313}
314
315void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
316{
317 event.Enable( CanPaste() );
318}
319
320void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
321{
322 event.Enable( CanUndo() );
323}
324
325void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
326{
327 event.Enable( CanRedo() );
328}
329
330void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
331{
332 event.Enable(HasSelection() && IsEditable()) ;
333}
334
335void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
336{
e976429d 337 event.Enable(!wxTextEntry::IsEmpty());
150e31d2
JS
338}
339
0ec1179b 340#endif // wxUSE_COMBOBOX