1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/combobox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/combobox.h"
19 #include "wx/settings.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
21 #include "wx/arrstr.h"
24 #include "wx/gtk/private.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
32 gtkcombobox_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
34 if (!combo
->m_hasVMT
) return;
36 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, combo
->GetId() );
37 event
.SetString( combo
->GetValue() );
38 event
.SetEventObject( combo
);
39 combo
->HandleWindowEvent( event
);
43 gtkcombobox_changed_callback( GtkWidget
*WXUNUSED(widget
), wxComboBox
*combo
)
45 combo
->SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED
);
49 gtkcombobox_popupshown_callback(GObject
*WXUNUSED(gobject
),
50 GParamSpec
*WXUNUSED(param_spec
),
54 g_object_get( combo
->m_widget
, "popup-shown", &isShown
, NULL
);
55 wxCommandEvent
event( isShown
? wxEVT_COMMAND_COMBOBOX_DROPDOWN
56 : wxEVT_COMMAND_COMBOBOX_CLOSEUP
,
58 event
.SetEventObject( combo
);
59 combo
->HandleWindowEvent( event
);
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxChoice
)
69 BEGIN_EVENT_TABLE(wxComboBox
, wxChoice
)
70 EVT_CHAR(wxComboBox::OnChar
)
72 EVT_MENU(wxID_CUT
, wxComboBox::OnCut
)
73 EVT_MENU(wxID_COPY
, wxComboBox::OnCopy
)
74 EVT_MENU(wxID_PASTE
, wxComboBox::OnPaste
)
75 EVT_MENU(wxID_UNDO
, wxComboBox::OnUndo
)
76 EVT_MENU(wxID_REDO
, wxComboBox::OnRedo
)
77 EVT_MENU(wxID_CLEAR
, wxComboBox::OnDelete
)
78 EVT_MENU(wxID_SELECTALL
, wxComboBox::OnSelectAll
)
80 EVT_UPDATE_UI(wxID_CUT
, wxComboBox::OnUpdateCut
)
81 EVT_UPDATE_UI(wxID_COPY
, wxComboBox::OnUpdateCopy
)
82 EVT_UPDATE_UI(wxID_PASTE
, wxComboBox::OnUpdatePaste
)
83 EVT_UPDATE_UI(wxID_UNDO
, wxComboBox::OnUpdateUndo
)
84 EVT_UPDATE_UI(wxID_REDO
, wxComboBox::OnUpdateRedo
)
85 EVT_UPDATE_UI(wxID_CLEAR
, wxComboBox::OnUpdateDelete
)
86 EVT_UPDATE_UI(wxID_SELECTALL
, wxComboBox::OnUpdateSelectAll
)
89 void wxComboBox::Init()
94 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
95 const wxString
& value
,
96 const wxPoint
& pos
, const wxSize
& size
,
97 const wxArrayString
& choices
,
98 long style
, const wxValidator
& validator
,
99 const wxString
& name
)
101 wxCArrayString
chs(choices
);
103 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
104 chs
.GetStrings(), style
, validator
, name
);
107 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
108 const wxPoint
& pos
, const wxSize
& size
,
109 int n
, const wxString choices
[],
110 long style
, const wxValidator
& validator
,
111 const wxString
& name
)
113 if (!PreCreation( parent
, pos
, size
) ||
114 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
116 wxFAIL_MSG( wxT("wxComboBox creation failed") );
120 if (HasFlag(wxCB_SORT
))
121 m_strings
= new wxGtkCollatedArrayString();
123 GTKCreateComboBoxWidget();
125 if (HasFlag(wxBORDER_NONE
))
127 // Doesn't seem to work
128 // g_object_set (m_widget, "has-frame", FALSE, NULL);
131 GtkEntry
* const entry
= GetEntry();
135 // Set it up to trigger default item on enter key press
136 gtk_entry_set_activates_default( entry
,
137 !HasFlag(wxTE_PROCESS_ENTER
) );
139 gtk_entry_set_editable( entry
, TRUE
);
144 m_parent
->DoAddChild( this );
147 m_focusWidget
= GTK_WIDGET( entry
);
153 if (style
& wxCB_READONLY
)
155 // this will assert and do nothing if the value is not in our list
156 // of strings which is the desired behaviour (for consistency with
157 // wxMSW and also because it doesn't make sense to have a string
158 // which is not a possible choice in a read-only combobox)
159 SetStringSelection(value
);
160 gtk_entry_set_editable( entry
, FALSE
);
162 else // editable combobox
164 // any value is accepted, even if it's not in our list
165 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
168 g_signal_connect_after (entry
, "changed",
169 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
172 g_signal_connect_after (m_widget
, "changed",
173 G_CALLBACK (gtkcombobox_changed_callback
), this);
175 if ( !gtk_check_version(2,10,0) )
177 g_signal_connect (m_widget
, "notify::popup-shown",
178 G_CALLBACK (gtkcombobox_popupshown_callback
), this);
181 SetInitialSize(size
); // need this too because this is a wxControlWithItems
186 void wxComboBox::GTKCreateComboBoxWidget()
188 m_widget
= gtk_combo_box_entry_new_text();
189 g_object_ref(m_widget
);
191 m_entry
= GTK_ENTRY(GTK_BIN(m_widget
)->child
);
194 GtkEditable
*wxComboBox::GetEditable() const
196 return GTK_EDITABLE( GTK_BIN(m_widget
)->child
);
199 void wxComboBox::OnChar( wxKeyEvent
&event
)
201 switch ( event
.GetKeyCode() )
204 if ( HasFlag(wxTE_PROCESS_ENTER
) && GetEntry() )
206 // GTK automatically selects an item if its in the list
207 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
208 eventEnter
.SetString( GetValue() );
209 eventEnter
.SetInt( GetSelection() );
210 eventEnter
.SetEventObject( this );
212 if ( HandleWindowEvent(eventEnter
) )
214 // Catch GTK event so that GTK doesn't open the drop
215 // down list upon RETURN.
225 void wxComboBox::EnableTextChangedEvents(bool enable
)
232 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget
)->child
,
233 (gpointer
)gtkcombobox_text_changed_callback
, this);
237 g_signal_handlers_block_by_func(GTK_BIN(m_widget
)->child
,
238 (gpointer
)gtkcombobox_text_changed_callback
, this);
242 void wxComboBox::GTKDisableEvents()
244 EnableTextChangedEvents(false);
246 g_signal_handlers_block_by_func(m_widget
,
247 (gpointer
)gtkcombobox_changed_callback
, this);
248 g_signal_handlers_block_by_func(m_widget
,
249 (gpointer
)gtkcombobox_popupshown_callback
, this);
252 void wxComboBox::GTKEnableEvents()
254 EnableTextChangedEvents(true);
256 g_signal_handlers_unblock_by_func(m_widget
,
257 (gpointer
)gtkcombobox_changed_callback
, this);
258 g_signal_handlers_unblock_by_func(m_widget
,
259 (gpointer
)gtkcombobox_popupshown_callback
, this);
262 GtkWidget
* wxComboBox::GetConnectWidget()
264 return GTK_WIDGET( GetEntry() );
267 GdkWindow
* wxComboBox::GTKGetWindow(wxArrayGdkWindows
& /* windows */) const
269 return GetEntry()->text_area
;
274 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
276 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
279 void wxComboBox::SetValue(const wxString
& value
)
281 if ( HasFlag(wxCB_READONLY
) )
282 SetStringSelection(value
);
284 wxTextEntry::SetValue(value
);
287 // ----------------------------------------------------------------------------
288 // standard event handling
289 // ----------------------------------------------------------------------------
291 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
296 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
301 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
306 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
311 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
316 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
321 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
326 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
328 event
.Enable( CanCut() );
331 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
333 event
.Enable( CanCopy() );
336 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
338 event
.Enable( CanPaste() );
341 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
343 event
.Enable( CanUndo() );
346 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
348 event
.Enable( CanRedo() );
351 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
353 event
.Enable(HasSelection() && IsEditable()) ;
356 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
358 event
.Enable(!wxTextEntry::IsEmpty());
361 void wxComboBox::Popup()
363 gtk_combo_box_popup( GTK_COMBO_BOX(m_widget
) );
366 void wxComboBox::Dismiss()
368 gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget
) );
370 #endif // wxUSE_COMBOBOX