don't allow setting readonly combobox value to a string which is not one of the valid...
[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 void wxComboBox::Init()
76 {
77 m_entry = NULL;
78 }
79
80 bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
81 const wxString& value,
82 const wxPoint& pos, const wxSize& size,
83 const wxArrayString& choices,
84 long style, const wxValidator& validator,
85 const wxString& name )
86 {
87 wxCArrayString chs(choices);
88
89 return Create( parent, id, value, pos, size, chs.GetCount(),
90 chs.GetStrings(), style, validator, name );
91 }
92
93 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
94 const wxPoint& pos, const wxSize& size,
95 int n, const wxString choices[],
96 long style, const wxValidator& validator,
97 const wxString& name )
98 {
99 if (!PreCreation( parent, pos, size ) ||
100 !CreateBase( parent, id, pos, size, style, validator, name ))
101 {
102 wxFAIL_MSG( wxT("wxComboBox creation failed") );
103 return false;
104 }
105
106 if (HasFlag(wxCB_SORT))
107 m_strings = new wxSortedArrayString();
108
109 GTKCreateComboBoxWidget();
110
111 if (HasFlag(wxBORDER_NONE))
112 {
113 // Doesn't seem to work
114 // g_object_set (m_widget, "has-frame", FALSE, NULL);
115 }
116
117 GtkEntry * const entry = GetEntry();
118
119 if ( entry )
120 {
121 // Set it up to trigger default item on enter key press
122 gtk_entry_set_activates_default( entry,
123 !HasFlag(wxTE_PROCESS_ENTER) );
124
125 gtk_entry_set_editable( entry, TRUE );
126 }
127
128 Append(n, choices);
129
130 m_parent->DoAddChild( this );
131
132 if ( entry )
133 m_focusWidget = GTK_WIDGET( entry );
134
135 PostCreation(size);
136
137 if ( entry )
138 {
139 if (style & wxCB_READONLY)
140 {
141 // this will assert and do nothing if the value is not in our list
142 // of strings which is the desired behaviour (for consistency with
143 // wxMSW and also because it doesn't make sense to have a string
144 // which is not a possible choice in a read-only combobox)
145 SetStringSelection(value);
146 gtk_entry_set_editable( entry, FALSE );
147 }
148 else // editable combobox
149 {
150 // any value is accepted, even if it's not in our list
151 gtk_entry_set_text( entry, wxGTK_CONV(value) );
152 }
153
154 g_signal_connect_after (entry, "changed",
155 G_CALLBACK (gtkcombobox_text_changed_callback), this);
156 }
157
158 g_signal_connect_after (m_widget, "changed",
159 G_CALLBACK (gtkcombobox_changed_callback), this);
160
161 SetInitialSize(size); // need this too because this is a wxControlWithItems
162
163 return true;
164 }
165
166 void wxComboBox::GTKCreateComboBoxWidget()
167 {
168 m_widget = gtk_combo_box_entry_new_text();
169 g_object_ref(m_widget);
170
171 m_entry = GTK_ENTRY(GTK_BIN(m_widget)->child);
172 }
173
174 GtkEditable *wxComboBox::GetEditable() const
175 {
176 return GTK_EDITABLE( GTK_BIN(m_widget)->child );
177 }
178
179 void wxComboBox::OnChar( wxKeyEvent &event )
180 {
181 switch ( event.GetKeyCode() )
182 {
183 case WXK_RETURN:
184 if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() )
185 {
186 // GTK automatically selects an item if its in the list
187 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
188 eventEnter.SetString( GetValue() );
189 eventEnter.SetInt( GetSelection() );
190 eventEnter.SetEventObject( this );
191
192 if ( HandleWindowEvent(eventEnter) )
193 {
194 // Catch GTK event so that GTK doesn't open the drop
195 // down list upon RETURN.
196 return;
197 }
198 }
199 break;
200 }
201
202 event.Skip();
203 }
204
205 void wxComboBox::DisableEvents()
206 {
207 if ( GetEntry() )
208 g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
209 (gpointer)gtkcombobox_text_changed_callback, this);
210
211 g_signal_handlers_block_by_func(m_widget,
212 (gpointer)gtkcombobox_changed_callback, this);
213 }
214
215 void wxComboBox::EnableEvents()
216 {
217 if ( GetEntry() )
218 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child,
219 (gpointer)gtkcombobox_text_changed_callback, this);
220
221 g_signal_handlers_unblock_by_func(m_widget,
222 (gpointer)gtkcombobox_changed_callback, this);
223 }
224
225 GtkWidget* wxComboBox::GetConnectWidget()
226 {
227 return GTK_WIDGET( GetEntry() );
228 }
229
230 GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
231 {
232 wxUnusedVar(windows);
233
234 return GetEntry()->text_area;
235 }
236
237 // static
238 wxVisualAttributes
239 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
240 {
241 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
242 }
243
244 // ----------------------------------------------------------------------------
245 // standard event handling
246 // ----------------------------------------------------------------------------
247
248 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
249 {
250 Cut();
251 }
252
253 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
254 {
255 Copy();
256 }
257
258 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
259 {
260 Paste();
261 }
262
263 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
264 {
265 Undo();
266 }
267
268 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
269 {
270 Redo();
271 }
272
273 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
274 {
275 RemoveSelection();
276 }
277
278 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
279 {
280 SelectAll();
281 }
282
283 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
284 {
285 event.Enable( CanCut() );
286 }
287
288 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
289 {
290 event.Enable( CanCopy() );
291 }
292
293 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
294 {
295 event.Enable( CanPaste() );
296 }
297
298 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
299 {
300 event.Enable( CanUndo() );
301 }
302
303 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
304 {
305 event.Enable( CanRedo() );
306 }
307
308 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
309 {
310 event.Enable(HasSelection() && IsEditable()) ;
311 }
312
313 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
314 {
315 event.Enable(!wxTextEntry::IsEmpty());
316 }
317
318 #endif // wxUSE_COMBOBOX