]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
Fix wxHtmlHelpData::SetTempDir() to behave correctly without trailing slash.
[wxWidgets.git] / src / gtk / combobox.cpp
CommitLineData
53010e52 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/gtk/combobox.cpp
53010e52
RR
3// Purpose:
4// Author: Robert Roebling
01111366 5// Copyright: (c) 1998 Robert Roebling
65571936 6// Licence: wxWindows licence
53010e52
RR
7/////////////////////////////////////////////////////////////////////////////
8
14f355c2
VS
9// For compilers that support precompilation, includes "wx.h".
10#include "wx/wxprec.h"
11
dcf924a3
RR
12#if wxUSE_COMBOBOX
13
8228b893
WS
14#include "wx/combobox.h"
15
88a7a4e1
WS
16#ifndef WX_PRECOMP
17 #include "wx/intl.h"
9eddec69 18 #include "wx/settings.h"
ce7fe42e 19 #include "wx/textctrl.h" // for wxEVT_TEXT
aaa6d89a 20 #include "wx/arrstr.h"
88a7a4e1
WS
21#endif
22
9dc44eff 23#include <gtk/gtk.h>
9e691f46 24#include "wx/gtk/private.h"
9dc44eff 25#include "wx/gtk/private/gtk2-compat.h"
83624f79 26
ff654490
VZ
27// ----------------------------------------------------------------------------
28// GTK callbacks
29// ----------------------------------------------------------------------------
461573cc 30
590f50d6
RR
31extern "C" {
32static void
33gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
34{
ce7fe42e 35 wxCommandEvent event( wxEVT_TEXT, combo->GetId() );
590f50d6
RR
36 event.SetString( combo->GetValue() );
37 event.SetEventObject( combo );
937013e0 38 combo->HandleWindowEvent( event );
590f50d6 39}
590f50d6 40
590f50d6
RR
41static void
42gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
43{
ce7fe42e 44 combo->SendSelectionChangedEvent(wxEVT_COMBOBOX);
590f50d6 45}
8933fbc6
VZ
46
47static void
48gtkcombobox_popupshown_callback(GObject *WXUNUSED(gobject),
49 GParamSpec *WXUNUSED(param_spec),
50 wxComboBox *combo)
51{
52 gboolean isShown;
53 g_object_get( combo->m_widget, "popup-shown", &isShown, NULL );
ce7fe42e
VZ
54 wxCommandEvent event( isShown ? wxEVT_COMBOBOX_DROPDOWN
55 : wxEVT_COMBOBOX_CLOSEUP,
8933fbc6
VZ
56 combo->GetId() );
57 event.SetEventObject( combo );
58 combo->HandleWindowEvent( event );
59}
10434560 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
8ab75332
PC
87wxComboBox::~wxComboBox()
88{
89 if (m_entry)
90 GTKDisconnect(m_entry);
91}
92
e78c1d78
RR
93void wxComboBox::Init()
94{
95 m_entry = NULL;
96}
97
584ad2a3
MB
98bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
99 const wxString& value,
100 const wxPoint& pos, const wxSize& size,
101 const wxArrayString& choices,
102 long style, const wxValidator& validator,
103 const wxString& name )
104{
105 wxCArrayString chs(choices);
106
107 return Create( parent, id, value, pos, size, chs.GetCount(),
108 chs.GetStrings(), style, validator, name );
109}
110
fd0eed64
RR
111bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
112 const wxPoint& pos, const wxSize& size,
113 int n, const wxString choices[],
805dd538
VZ
114 long style, const wxValidator& validator,
115 const wxString& name )
53010e52 116{
db434467 117 if (!PreCreation( parent, pos, size ) ||
4dcaf11a
RR
118 !CreateBase( parent, id, pos, size, style, validator, name ))
119 {
223d09f6 120 wxFAIL_MSG( wxT("wxComboBox creation failed") );
7d8268a1 121 return false;
4dcaf11a 122 }
6de97a3b 123
2f515791 124 if (HasFlag(wxCB_SORT))
c272f12f 125 m_strings = new wxGtkCollatedArrayString();
a236aa20 126
e78c1d78 127 GTKCreateComboBoxWidget();
a2c94110 128
2f515791
RR
129 if (HasFlag(wxBORDER_NONE))
130 {
131 // Doesn't seem to work
132 // g_object_set (m_widget, "has-frame", FALSE, NULL);
133 }
134
ff654490 135 GtkEntry * const entry = GetEntry();
3ca6a5f0 136
e78c1d78
RR
137 if ( entry )
138 {
139 // Set it up to trigger default item on enter key press
140 gtk_entry_set_activates_default( entry,
141 !HasFlag(wxTE_PROCESS_ENTER) );
142
385e8575 143 gtk_editable_set_editable(GTK_EDITABLE(entry), true);
e78c1d78 144 }
805dd538 145
a236aa20 146 Append(n, choices);
590f50d6 147
f03fc89f 148 m_parent->DoAddChild( this );
30ed6e5c 149
e78c1d78
RR
150 if ( entry )
151 m_focusWidget = GTK_WIDGET( entry );
805dd538 152
abdeb9e7 153 PostCreation(size);
53010e52 154
e78c1d78
RR
155 if ( entry )
156 {
e78c1d78 157 if (style & wxCB_READONLY)
a3281dbc
VZ
158 {
159 // this will assert and do nothing if the value is not in our list
160 // of strings which is the desired behaviour (for consistency with
161 // wxMSW and also because it doesn't make sense to have a string
162 // which is not a possible choice in a read-only combobox)
163 SetStringSelection(value);
385e8575 164 gtk_editable_set_editable(GTK_EDITABLE(entry), false);
a3281dbc
VZ
165 }
166 else // editable combobox
167 {
168 // any value is accepted, even if it's not in our list
169 gtk_entry_set_text( entry, wxGTK_CONV(value) );
170 }
8228b893 171
e78c1d78
RR
172 g_signal_connect_after (entry, "changed",
173 G_CALLBACK (gtkcombobox_text_changed_callback), this);
10434560 174
b2c35774 175 GTKConnectInsertTextSignal(entry);
10434560 176 GTKConnectClipboardSignals(GTK_WIDGET(entry));
e78c1d78 177 }
8228b893 178
ff654490
VZ
179 g_signal_connect_after (m_widget, "changed",
180 G_CALLBACK (gtkcombobox_changed_callback), this);
f4322df6 181
9dc44eff 182#ifndef __WXGTK3__
42628481 183 if ( !gtk_check_version(2,10,0) )
9dc44eff 184#endif
8933fbc6
VZ
185 {
186 g_signal_connect (m_widget, "notify::popup-shown",
187 G_CALLBACK (gtkcombobox_popupshown_callback), this);
188 }
189
7d8268a1 190 return true;
fd0eed64
RR
191}
192
e78c1d78 193void wxComboBox::GTKCreateComboBoxWidget()
ff654490 194{
9dc44eff
PC
195#ifdef __WXGTK3__
196 m_widget = gtk_combo_box_text_new_with_entry();
197#else
e78c1d78 198 m_widget = gtk_combo_box_entry_new_text();
9dc44eff 199#endif
9ff9d30c 200 g_object_ref(m_widget);
e78c1d78 201
385e8575 202 m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget)));
ff654490
VZ
203}
204
0ec1179b
VZ
205GtkEditable *wxComboBox::GetEditable() const
206{
385e8575 207 return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget)));
0ec1179b
VZ
208}
209
8a85884a
VZ
210void wxComboBox::OnChar( wxKeyEvent &event )
211{
643c973b 212 switch ( event.GetKeyCode() )
8a85884a 213 {
643c973b 214 case WXK_RETURN:
e78c1d78 215 if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() )
3352cfff 216 {
643c973b 217 // GTK automatically selects an item if its in the list
ce7fe42e 218 wxCommandEvent eventEnter(wxEVT_TEXT_ENTER, GetId());
643c973b
VZ
219 eventEnter.SetString( GetValue() );
220 eventEnter.SetInt( GetSelection() );
221 eventEnter.SetEventObject( this );
222
937013e0 223 if ( HandleWindowEvent(eventEnter) )
643c973b
VZ
224 {
225 // Catch GTK event so that GTK doesn't open the drop
226 // down list upon RETURN.
227 return;
228 }
3352cfff 229 }
643c973b 230 break;
8a85884a 231 }
30ed6e5c 232
7cf8cb48 233 event.Skip();
8a85884a
VZ
234}
235
b526f9d6 236void wxComboBox::EnableTextChangedEvents(bool enable)
953704c1 237{
b526f9d6
VZ
238 if ( !GetEntry() )
239 return;
240
241 if ( enable )
242 {
385e8575 243 g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
b526f9d6
VZ
244 (gpointer)gtkcombobox_text_changed_callback, this);
245 }
246 else // disable
247 {
385e8575 248 g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(m_widget)),
e78c1d78 249 (gpointer)gtkcombobox_text_changed_callback, this);
b526f9d6
VZ
250 }
251}
252
253void wxComboBox::GTKDisableEvents()
254{
255 EnableTextChangedEvents(false);
8228b893 256
ff654490
VZ
257 g_signal_handlers_block_by_func(m_widget,
258 (gpointer)gtkcombobox_changed_callback, this);
8933fbc6
VZ
259 g_signal_handlers_block_by_func(m_widget,
260 (gpointer)gtkcombobox_popupshown_callback, this);
953704c1
RR
261}
262
dec7b5a8 263void wxComboBox::GTKEnableEvents()
953704c1 264{
b526f9d6 265 EnableTextChangedEvents(true);
ea2d542c 266
ff654490
VZ
267 g_signal_handlers_unblock_by_func(m_widget,
268 (gpointer)gtkcombobox_changed_callback, this);
8933fbc6
VZ
269 g_signal_handlers_unblock_by_func(m_widget,
270 (gpointer)gtkcombobox_popupshown_callback, this);
868a2826 271}
b4071e91 272
fd0eed64 273GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 274{
ff654490 275 return GTK_WIDGET( GetEntry() );
97b3455a
RR
276}
277
65391c8f 278GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const
b4071e91 279{
9dc44eff
PC
280#ifdef __WXGTK3__
281 // no access to internal GdkWindows
282 return NULL;
283#else
385e8575 284 return gtk_entry_get_text_window(GetEntry());
9dc44eff 285#endif
b4071e91 286}
ac57418f 287
9d522606
RD
288// static
289wxVisualAttributes
290wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
291{
9dc44eff 292#ifdef __WXGTK3__
7fff16b8 293 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new_with_entry(), true);
9dc44eff 294#else
7fff16b8 295 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new(), true);
9dc44eff 296#endif
9d522606
RD
297}
298
9572bf1d
RR
299void wxComboBox::SetValue(const wxString& value)
300{
301 if ( HasFlag(wxCB_READONLY) )
302 SetStringSelection(value);
303 else
304 wxTextEntry::SetValue(value);
305}
306
a9ed8caa
VZ
307void wxComboBox::SetString(unsigned int n, const wxString& text)
308{
309 wxChoice::SetString(n, text);
310
311 if ( static_cast<int>(n) == GetSelection() )
312 {
313 // We also need to update the currently shown text, for consistency
314 // with wxMSW and also because it makes sense as leaving the old string
315 // in the text but not in the list would be confusing to the user.
316 SetValue(text);
182cad34
VZ
317
318 // And we need to keep the selection unchanged, modifying the item is
319 // not supposed to deselect it.
320 SetSelection(n);
a9ed8caa
VZ
321 }
322}
323
150e31d2
JS
324// ----------------------------------------------------------------------------
325// standard event handling
326// ----------------------------------------------------------------------------
327
328void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
329{
330 Cut();
331}
332
333void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
334{
335 Copy();
336}
337
338void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
339{
340 Paste();
341}
342
343void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
344{
345 Undo();
346}
347
348void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
349{
350 Redo();
351}
352
353void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
354{
5a25f858 355 RemoveSelection();
150e31d2
JS
356}
357
358void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
359{
e976429d 360 SelectAll();
150e31d2
JS
361}
362
363void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
364{
365 event.Enable( CanCut() );
366}
367
368void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
369{
370 event.Enable( CanCopy() );
371}
372
373void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
374{
375 event.Enable( CanPaste() );
376}
377
378void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
379{
380 event.Enable( CanUndo() );
381}
382
383void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
384{
385 event.Enable( CanRedo() );
386}
387
388void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
389{
390 event.Enable(HasSelection() && IsEditable()) ;
391}
392
393void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
394{
e976429d 395 event.Enable(!wxTextEntry::IsEmpty());
150e31d2
JS
396}
397
d1d1f817
VZ
398void wxComboBox::Popup()
399{
6008ff4a 400 gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) );
d1d1f817
VZ
401}
402
403void wxComboBox::Dismiss()
404{
405 gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) );
406}
7a78a937
VZ
407
408wxSize wxComboBox::DoGetSizeFromTextSize(int xlen, int ylen) const
409{
410 wxSize tsize( wxChoice::DoGetSizeFromTextSize(xlen, ylen) );
411
6968a3b8
PC
412 GtkEntry* entry = GetEntry();
413 if (entry)
414 {
415 // Add the margins we have previously set, but only the horizontal border
416 // as vertical one has been taken account in the previous call.
417 // Also get other GTK+ margins.
418 tsize.IncBy(GTKGetEntryMargins(entry).x, 0);
419 }
7a78a937
VZ
420
421 return tsize;
422}
423
0ec1179b 424#endif // wxUSE_COMBOBOX