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