]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
remove unnecessary gtk_widget_show(m_widget) calls, PostCreation() takes care of...
[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 67BEGIN_EVENT_TABLE(wxComboBox, wxChoice)
8a85884a 68 EVT_CHAR(wxComboBox::OnChar)
150e31d2
JS
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)
b4071e91
RR
85END_EVENT_TABLE()
86
e78c1d78
RR
87void wxComboBox::Init()
88{
89 m_entry = NULL;
90}
91
584ad2a3
MB
92bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
93 const wxString& value,
94 const wxPoint& pos, const wxSize& size,
95 const wxArrayString& choices,
96 long style, const wxValidator& validator,
97 const wxString& name )
98{
99 wxCArrayString chs(choices);
100
101 return Create( parent, id, value, pos, size, chs.GetCount(),
102 chs.GetStrings(), style, validator, name );
103}
104
fd0eed64
RR
105bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
106 const wxPoint& pos, const wxSize& size,
107 int n, const wxString choices[],
805dd538
VZ
108 long style, const wxValidator& validator,
109 const wxString& name )
53010e52 110{
db434467 111 if (!PreCreation( parent, pos, size ) ||
4dcaf11a
RR
112 !CreateBase( parent, id, pos, size, style, validator, name ))
113 {
223d09f6 114 wxFAIL_MSG( wxT("wxComboBox creation failed") );
7d8268a1 115 return false;
4dcaf11a 116 }
6de97a3b 117
2f515791 118 if (HasFlag(wxCB_SORT))
c272f12f 119 m_strings = new wxGtkCollatedArrayString();
a236aa20 120
e78c1d78 121 GTKCreateComboBoxWidget();
a2c94110 122
2f515791
RR
123 if (HasFlag(wxBORDER_NONE))
124 {
125 // Doesn't seem to work
126 // g_object_set (m_widget, "has-frame", FALSE, NULL);
127 }
128
ff654490 129 GtkEntry * const entry = GetEntry();
3ca6a5f0 130
e78c1d78
RR
131 if ( entry )
132 {
133 // Set it up to trigger default item on enter key press
134 gtk_entry_set_activates_default( entry,
135 !HasFlag(wxTE_PROCESS_ENTER) );
136
385e8575 137 gtk_editable_set_editable(GTK_EDITABLE(entry), true);
e78c1d78 138 }
805dd538 139
a236aa20 140 Append(n, choices);
590f50d6 141
f03fc89f 142 m_parent->DoAddChild( this );
30ed6e5c 143
e78c1d78
RR
144 if ( entry )
145 m_focusWidget = GTK_WIDGET( entry );
805dd538 146
abdeb9e7 147 PostCreation(size);
53010e52 148
e78c1d78
RR
149 if ( entry )
150 {
e78c1d78 151 if (style & wxCB_READONLY)
a3281dbc
VZ
152 {
153 // this will assert and do nothing if the value is not in our list
154 // of strings which is the desired behaviour (for consistency with
155 // wxMSW and also because it doesn't make sense to have a string
156 // which is not a possible choice in a read-only combobox)
157 SetStringSelection(value);
385e8575 158 gtk_editable_set_editable(GTK_EDITABLE(entry), false);
a3281dbc
VZ
159 }
160 else // editable combobox
161 {
162 // any value is accepted, even if it's not in our list
163 gtk_entry_set_text( entry, wxGTK_CONV(value) );
164 }
8228b893 165
e78c1d78
RR
166 g_signal_connect_after (entry, "changed",
167 G_CALLBACK (gtkcombobox_text_changed_callback), this);
168 }
8228b893 169
ff654490
VZ
170 g_signal_connect_after (m_widget, "changed",
171 G_CALLBACK (gtkcombobox_changed_callback), this);
f4322df6 172
42628481 173 if ( !gtk_check_version(2,10,0) )
8933fbc6
VZ
174 {
175 g_signal_connect (m_widget, "notify::popup-shown",
176 G_CALLBACK (gtkcombobox_popupshown_callback), this);
177 }
178
170acdc9 179 SetInitialSize(size); // need this too because this is a wxControlWithItems
805dd538 180
7d8268a1 181 return true;
fd0eed64
RR
182}
183
e78c1d78 184void wxComboBox::GTKCreateComboBoxWidget()
ff654490 185{
e78c1d78 186 m_widget = gtk_combo_box_entry_new_text();
9ff9d30c 187 g_object_ref(m_widget);
e78c1d78 188
385e8575 189 m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget)));
ff654490
VZ
190}
191
0ec1179b
VZ
192GtkEditable *wxComboBox::GetEditable() const
193{
385e8575 194 return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget)));
0ec1179b
VZ
195}
196
8a85884a
VZ
197void wxComboBox::OnChar( wxKeyEvent &event )
198{
643c973b 199 switch ( event.GetKeyCode() )
8a85884a 200 {
643c973b 201 case WXK_RETURN:
e78c1d78 202 if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() )
3352cfff 203 {
643c973b
VZ
204 // GTK automatically selects an item if its in the list
205 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
206 eventEnter.SetString( GetValue() );
207 eventEnter.SetInt( GetSelection() );
208 eventEnter.SetEventObject( this );
209
937013e0 210 if ( HandleWindowEvent(eventEnter) )
643c973b
VZ
211 {
212 // Catch GTK event so that GTK doesn't open the drop
213 // down list upon RETURN.
214 return;
215 }
3352cfff 216 }
643c973b 217 break;
8a85884a 218 }
30ed6e5c 219
7cf8cb48 220 event.Skip();
8a85884a
VZ
221}
222
b526f9d6 223void wxComboBox::EnableTextChangedEvents(bool enable)
953704c1 224{
b526f9d6
VZ
225 if ( !GetEntry() )
226 return;
227
228 if ( enable )
229 {
385e8575 230 g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
b526f9d6
VZ
231 (gpointer)gtkcombobox_text_changed_callback, this);
232 }
233 else // disable
234 {
385e8575 235 g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
e78c1d78 236 (gpointer)gtkcombobox_text_changed_callback, this);
b526f9d6
VZ
237 }
238}
239
240void wxComboBox::GTKDisableEvents()
241{
242 EnableTextChangedEvents(false);
8228b893 243
ff654490
VZ
244 g_signal_handlers_block_by_func(m_widget,
245 (gpointer)gtkcombobox_changed_callback, this);
8933fbc6
VZ
246 g_signal_handlers_block_by_func(m_widget,
247 (gpointer)gtkcombobox_popupshown_callback, this);
953704c1
RR
248}
249
dec7b5a8 250void wxComboBox::GTKEnableEvents()
953704c1 251{
b526f9d6 252 EnableTextChangedEvents(true);
ea2d542c 253
ff654490
VZ
254 g_signal_handlers_unblock_by_func(m_widget,
255 (gpointer)gtkcombobox_changed_callback, this);
8933fbc6
VZ
256 g_signal_handlers_unblock_by_func(m_widget,
257 (gpointer)gtkcombobox_popupshown_callback, this);
868a2826 258}
b4071e91 259
fd0eed64 260GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 261{
ff654490 262 return GTK_WIDGET( GetEntry() );
97b3455a
RR
263}
264
65391c8f 265GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const
b4071e91 266{
385e8575 267 return gtk_entry_get_text_window(GetEntry());
b4071e91 268}
ac57418f 269
9d522606
RD
270// static
271wxVisualAttributes
272wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
273{
ff654490 274 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
9d522606
RD
275}
276
9572bf1d
RR
277void wxComboBox::SetValue(const wxString& value)
278{
279 if ( HasFlag(wxCB_READONLY) )
280 SetStringSelection(value);
281 else
282 wxTextEntry::SetValue(value);
283}
284
150e31d2
JS
285// ----------------------------------------------------------------------------
286// standard event handling
287// ----------------------------------------------------------------------------
288
289void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
290{
291 Cut();
292}
293
294void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
295{
296 Copy();
297}
298
299void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
300{
301 Paste();
302}
303
304void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
305{
306 Undo();
307}
308
309void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
310{
311 Redo();
312}
313
314void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
315{
5a25f858 316 RemoveSelection();
150e31d2
JS
317}
318
319void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
320{
e976429d 321 SelectAll();
150e31d2
JS
322}
323
324void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
325{
326 event.Enable( CanCut() );
327}
328
329void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
330{
331 event.Enable( CanCopy() );
332}
333
334void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
335{
336 event.Enable( CanPaste() );
337}
338
339void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
340{
341 event.Enable( CanUndo() );
342}
343
344void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
345{
346 event.Enable( CanRedo() );
347}
348
349void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
350{
351 event.Enable(HasSelection() && IsEditable()) ;
352}
353
354void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
355{
e976429d 356 event.Enable(!wxTextEntry::IsEmpty());
150e31d2
JS
357}
358
d1d1f817
VZ
359void wxComboBox::Popup()
360{
6008ff4a 361 gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) );
d1d1f817
VZ
362}
363
364void wxComboBox::Dismiss()
365{
366 gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) );
367}
0ec1179b 368#endif // wxUSE_COMBOBOX