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 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxChoice
)
55 BEGIN_EVENT_TABLE(wxComboBox
, wxChoice
)
56 EVT_CHAR(wxComboBox::OnChar
)
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
)
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
)
75 void wxComboBox::Init()
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
)
87 wxCArrayString
chs(choices
);
89 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
90 chs
.GetStrings(), style
, validator
, name
);
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
)
99 if (!PreCreation( parent
, pos
, size
) ||
100 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
102 wxFAIL_MSG( wxT("wxComboBox creation failed") );
106 if (HasFlag(wxCB_SORT
))
107 m_strings
= new wxSortedArrayString();
109 GTKCreateComboBoxWidget();
111 if (HasFlag(wxBORDER_NONE
))
113 // Doesn't seem to work
114 // g_object_set (m_widget, "has-frame", FALSE, NULL);
117 GtkEntry
* const entry
= GetEntry();
121 // Set it up to trigger default item on enter key press
122 gtk_entry_set_activates_default( entry
,
123 !HasFlag(wxTE_PROCESS_ENTER
) );
125 gtk_entry_set_editable( entry
, TRUE
);
130 m_parent
->DoAddChild( this );
133 m_focusWidget
= GTK_WIDGET( entry
);
139 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
141 if (style
& wxCB_READONLY
)
142 gtk_entry_set_editable( entry
, FALSE
);
144 g_signal_connect_after (entry
, "changed",
145 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
148 g_signal_connect_after (m_widget
, "changed",
149 G_CALLBACK (gtkcombobox_changed_callback
), this);
151 SetInitialSize(size
); // need this too because this is a wxControlWithItems
156 void wxComboBox::GTKCreateComboBoxWidget()
158 m_widget
= gtk_combo_box_entry_new_text();
159 g_object_ref(m_widget
);
161 m_entry
= GTK_ENTRY(GTK_BIN(m_widget
)->child
);
164 GtkEditable
*wxComboBox::GetEditable() const
166 return GTK_EDITABLE( GTK_BIN(m_widget
)->child
);
169 void wxComboBox::OnChar( wxKeyEvent
&event
)
171 switch ( event
.GetKeyCode() )
174 if ( HasFlag(wxTE_PROCESS_ENTER
) && GetEntry() )
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 );
182 if ( HandleWindowEvent(eventEnter
) )
184 // Catch GTK event so that GTK doesn't open the drop
185 // down list upon RETURN.
195 void wxComboBox::DisableEvents()
198 g_signal_handlers_block_by_func(GTK_BIN(m_widget
)->child
,
199 (gpointer
)gtkcombobox_text_changed_callback
, this);
201 g_signal_handlers_block_by_func(m_widget
,
202 (gpointer
)gtkcombobox_changed_callback
, this);
205 void wxComboBox::EnableEvents()
208 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget
)->child
,
209 (gpointer
)gtkcombobox_text_changed_callback
, this);
211 g_signal_handlers_unblock_by_func(m_widget
,
212 (gpointer
)gtkcombobox_changed_callback
, this);
215 GtkWidget
* wxComboBox::GetConnectWidget()
217 return GTK_WIDGET( GetEntry() );
220 GdkWindow
*wxComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
222 wxUnusedVar(windows
);
224 return GetEntry()->text_area
;
229 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
231 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
234 // ----------------------------------------------------------------------------
235 // standard event handling
236 // ----------------------------------------------------------------------------
238 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
243 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
248 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
253 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
258 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
263 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
268 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
273 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
275 event
.Enable( CanCut() );
278 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
280 event
.Enable( CanCopy() );
283 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
285 event
.Enable( CanPaste() );
288 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
290 event
.Enable( CanUndo() );
293 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
295 event
.Enable( CanRedo() );
298 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
300 event
.Enable(HasSelection() && IsEditable()) ;
303 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
305 event
.Enable(!wxTextEntry::IsEmpty());
308 #endif // wxUSE_COMBOBOX