]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
Simplify SetValue() with empty string
[wxWidgets.git] / src / gtk / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/combobox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_COMBOBOX
14
15 #include "wx/combobox.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/settings.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
21 #include "wx/arrstr.h"
22 #endif
23
24 #include "wx/gtk/private.h"
25
26 // ----------------------------------------------------------------------------
27 // GTK callbacks
28 // ----------------------------------------------------------------------------
29
30 extern "C" {
31 static void
32 gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
33 {
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 );
39 combo->HandleWindowEvent( event );
40 }
41
42 static void
43 gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
44 {
45 combo->SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
46 }
47 }
48
49 //-----------------------------------------------------------------------------
50 // wxComboBox
51 //-----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxChoice)
54
55 BEGIN_EVENT_TABLE(wxComboBox, wxChoice)
56 EVT_CHAR(wxComboBox::OnChar)
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)
73 END_EVENT_TABLE()
74
75 bool 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
88 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
89 const wxPoint& pos, const wxSize& size,
90 int n, const wxString choices[],
91 long style, const wxValidator& validator,
92 const wxString& name )
93 {
94 m_strings = NULL;
95
96 if (!PreCreation( parent, pos, size ) ||
97 !CreateBase( parent, id, pos, size, style, validator, name ))
98 {
99 wxFAIL_MSG( wxT("wxComboBox creation failed") );
100 return false;
101 }
102
103 if (HasFlag(wxCB_SORT))
104 m_strings = new wxSortedArrayString();
105
106 m_widget = gtk_combo_box_entry_new_text();
107
108 // Set it up to trigger default item on enter key press
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));
112
113 if (HasFlag(wxBORDER_NONE))
114 {
115 // Doesn't seem to work
116 // g_object_set (m_widget, "has-frame", FALSE, NULL);
117 }
118
119 GtkEntry * const entry = GetEntry();
120
121 gtk_entry_set_editable( entry, TRUE );
122
123 Append(n, choices);
124
125 m_parent->DoAddChild( this );
126
127 m_focusWidget = GTK_WIDGET( entry );
128
129 PostCreation(size);
130
131 gtk_entry_set_text( entry, wxGTK_CONV(value) );
132
133 if (style & wxCB_READONLY)
134 gtk_entry_set_editable( entry, FALSE );
135
136 g_signal_connect_after (entry, "changed",
137 G_CALLBACK (gtkcombobox_text_changed_callback), this);
138
139 g_signal_connect_after (m_widget, "changed",
140 G_CALLBACK (gtkcombobox_changed_callback), this);
141
142 SetInitialSize(size); // need this too because this is a wxControlWithItems
143
144 return true;
145 }
146
147 GtkEntry *wxComboBox::GetEntry() const
148 {
149 return GTK_ENTRY(GTK_BIN(m_widget)->child);
150 }
151
152 GtkEditable *wxComboBox::GetEditable() const
153 {
154 return GTK_EDITABLE( GTK_BIN(m_widget)->child );
155 }
156
157 void wxComboBox::OnChar( wxKeyEvent &event )
158 {
159 switch ( event.GetKeyCode() )
160 {
161 case WXK_RETURN:
162 if ( HasFlag(wxTE_PROCESS_ENTER) )
163 {
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
170 if ( HandleWindowEvent(eventEnter) )
171 {
172 // Catch GTK event so that GTK doesn't open the drop
173 // down list upon RETURN.
174 return;
175 }
176 }
177 break;
178 }
179
180 event.Skip();
181 }
182
183 void wxComboBox::DisableEvents()
184 {
185 g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
186 (gpointer)gtkcombobox_text_changed_callback, this);
187
188 g_signal_handlers_block_by_func(m_widget,
189 (gpointer)gtkcombobox_changed_callback, this);
190 }
191
192 void wxComboBox::EnableEvents()
193 {
194 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child,
195 (gpointer)gtkcombobox_text_changed_callback, this);
196
197 g_signal_handlers_unblock_by_func(m_widget,
198 (gpointer)gtkcombobox_changed_callback, this);
199 }
200
201 GtkWidget* wxComboBox::GetConnectWidget()
202 {
203 return GTK_WIDGET( GetEntry() );
204 }
205
206 GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
207 {
208 wxUnusedVar(windows);
209
210 return GetEntry()->text_area;
211 }
212
213 // static
214 wxVisualAttributes
215 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
216 {
217 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
218 }
219
220 // ----------------------------------------------------------------------------
221 // standard event handling
222 // ----------------------------------------------------------------------------
223
224 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
225 {
226 Cut();
227 }
228
229 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
230 {
231 Copy();
232 }
233
234 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
235 {
236 Paste();
237 }
238
239 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
240 {
241 Undo();
242 }
243
244 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
245 {
246 Redo();
247 }
248
249 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
250 {
251 RemoveSelection();
252 }
253
254 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
255 {
256 SelectAll();
257 }
258
259 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
260 {
261 event.Enable( CanCut() );
262 }
263
264 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
265 {
266 event.Enable( CanCopy() );
267 }
268
269 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
270 {
271 event.Enable( CanPaste() );
272 }
273
274 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
275 {
276 event.Enable( CanUndo() );
277 }
278
279 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
280 {
281 event.Enable( CanRedo() );
282 }
283
284 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
285 {
286 event.Enable(HasSelection() && IsEditable()) ;
287 }
288
289 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
290 {
291 event.Enable(!wxTextEntry::IsEmpty());
292 }
293
294 #endif // wxUSE_COMBOBOX