]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
Simplify SetValue() with empty string
[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
RR
46}
47}
a73ae836 48
e1e955e1
RR
49//-----------------------------------------------------------------------------
50// wxComboBox
53010e52
RR
51//-----------------------------------------------------------------------------
52
a2c94110 53IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxChoice)
53010e52 54
a2c94110 55BEGIN_EVENT_TABLE(wxComboBox, wxChoice)
8a85884a 56 EVT_CHAR(wxComboBox::OnChar)
150e31d2
JS
57
58 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
59 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
60 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
61 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
62 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
63 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
64 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
65
66 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
67 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
68 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
69 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
70 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
71 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
72 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
b4071e91
RR
73END_EVENT_TABLE()
74
584ad2a3
MB
75bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
76 const wxString& value,
77 const wxPoint& pos, const wxSize& size,
78 const wxArrayString& choices,
79 long style, const wxValidator& validator,
80 const wxString& name )
81{
82 wxCArrayString chs(choices);
83
84 return Create( parent, id, value, pos, size, chs.GetCount(),
85 chs.GetStrings(), style, validator, name );
86}
87
fd0eed64
RR
88bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
89 const wxPoint& pos, const wxSize& size,
90 int n, const wxString choices[],
805dd538
VZ
91 long style, const wxValidator& validator,
92 const wxString& name )
53010e52 93{
a236aa20 94 m_strings = NULL;
805dd538 95
db434467 96 if (!PreCreation( parent, pos, size ) ||
4dcaf11a
RR
97 !CreateBase( parent, id, pos, size, style, validator, name ))
98 {
223d09f6 99 wxFAIL_MSG( wxT("wxComboBox creation failed") );
7d8268a1 100 return false;
4dcaf11a 101 }
6de97a3b 102
2f515791 103 if (HasFlag(wxCB_SORT))
a236aa20
VZ
104 m_strings = new wxSortedArrayString();
105
ff654490 106 m_widget = gtk_combo_box_entry_new_text();
30ed6e5c 107
a2c94110 108 // Set it up to trigger default item on enter key press
bfc262ae
RR
109 GtkWidget *widget = gtk_bin_get_child(GTK_BIN(m_widget));
110 gtk_entry_set_activates_default(GTK_ENTRY(widget),
111 !HasFlag(wxTE_PROCESS_ENTER));
a2c94110 112
2f515791
RR
113 if (HasFlag(wxBORDER_NONE))
114 {
115 // Doesn't seem to work
116 // g_object_set (m_widget, "has-frame", FALSE, NULL);
117 }
118
ff654490 119 GtkEntry * const entry = GetEntry();
3ca6a5f0 120
ff654490 121 gtk_entry_set_editable( entry, TRUE );
805dd538 122
a236aa20 123 Append(n, choices);
590f50d6 124
f03fc89f 125 m_parent->DoAddChild( this );
30ed6e5c 126
590f50d6 127 m_focusWidget = GTK_WIDGET( entry );
805dd538 128
abdeb9e7 129 PostCreation(size);
53010e52 130
ff654490 131 gtk_entry_set_text( entry, wxGTK_CONV(value) );
8228b893 132
ff654490
VZ
133 if (style & wxCB_READONLY)
134 gtk_entry_set_editable( entry, FALSE );
8228b893 135
ff654490
VZ
136 g_signal_connect_after (entry, "changed",
137 G_CALLBACK (gtkcombobox_text_changed_callback), this);
8228b893 138
ff654490
VZ
139 g_signal_connect_after (m_widget, "changed",
140 G_CALLBACK (gtkcombobox_changed_callback), this);
f4322df6 141
170acdc9 142 SetInitialSize(size); // need this too because this is a wxControlWithItems
805dd538 143
7d8268a1 144 return true;
fd0eed64
RR
145}
146
ff654490
VZ
147GtkEntry *wxComboBox::GetEntry() const
148{
149 return GTK_ENTRY(GTK_BIN(m_widget)->child);
150}
151
0ec1179b
VZ
152GtkEditable *wxComboBox::GetEditable() const
153{
ff654490 154 return GTK_EDITABLE( GTK_BIN(m_widget)->child );
0ec1179b
VZ
155}
156
8a85884a
VZ
157void wxComboBox::OnChar( wxKeyEvent &event )
158{
643c973b 159 switch ( event.GetKeyCode() )
8a85884a 160 {
643c973b
VZ
161 case WXK_RETURN:
162 if ( HasFlag(wxTE_PROCESS_ENTER) )
3352cfff 163 {
643c973b
VZ
164 // GTK automatically selects an item if its in the list
165 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
166 eventEnter.SetString( GetValue() );
167 eventEnter.SetInt( GetSelection() );
168 eventEnter.SetEventObject( this );
169
937013e0 170 if ( HandleWindowEvent(eventEnter) )
643c973b
VZ
171 {
172 // Catch GTK event so that GTK doesn't open the drop
173 // down list upon RETURN.
174 return;
175 }
3352cfff 176 }
643c973b 177 break;
8a85884a 178 }
30ed6e5c 179
7cf8cb48 180 event.Skip();
8a85884a
VZ
181}
182
953704c1
RR
183void wxComboBox::DisableEvents()
184{
ff654490
VZ
185 g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
186 (gpointer)gtkcombobox_text_changed_callback, this);
8228b893 187
ff654490
VZ
188 g_signal_handlers_block_by_func(m_widget,
189 (gpointer)gtkcombobox_changed_callback, this);
953704c1
RR
190}
191
192void wxComboBox::EnableEvents()
193{
ff654490
VZ
194 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child,
195 (gpointer)gtkcombobox_text_changed_callback, this);
ea2d542c 196
ff654490
VZ
197 g_signal_handlers_unblock_by_func(m_widget,
198 (gpointer)gtkcombobox_changed_callback, this);
868a2826 199}
b4071e91 200
fd0eed64 201GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 202{
ff654490 203 return GTK_WIDGET( GetEntry() );
97b3455a
RR
204}
205
ef5c70f9 206GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
b4071e91 207{
ff654490 208 wxUnusedVar(windows);
ef5c70f9 209
ff654490 210 return GetEntry()->text_area;
b4071e91 211}
ac57418f 212
9d522606
RD
213// static
214wxVisualAttributes
215wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
216{
ff654490 217 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
9d522606
RD
218}
219
150e31d2
JS
220// ----------------------------------------------------------------------------
221// standard event handling
222// ----------------------------------------------------------------------------
223
224void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
225{
226 Cut();
227}
228
229void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
230{
231 Copy();
232}
233
234void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
235{
236 Paste();
237}
238
239void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
240{
241 Undo();
242}
243
244void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
245{
246 Redo();
247}
248
249void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
250{
5a25f858 251 RemoveSelection();
150e31d2
JS
252}
253
254void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
255{
e976429d 256 SelectAll();
150e31d2
JS
257}
258
259void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
260{
261 event.Enable( CanCut() );
262}
263
264void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
265{
266 event.Enable( CanCopy() );
267}
268
269void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
270{
271 event.Enable( CanPaste() );
272}
273
274void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
275{
276 event.Enable( CanUndo() );
277}
278
279void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
280{
281 event.Enable( CanRedo() );
282}
283
284void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
285{
286 event.Enable(HasSelection() && IsEditable()) ;
287}
288
289void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
290{
e976429d 291 event.Enable(!wxTextEntry::IsEmpty());
150e31d2
JS
292}
293
0ec1179b 294#endif // wxUSE_COMBOBOX