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 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
,
76 const wxString
& value
,
77 const wxPoint
& pos
, const wxSize
& size
,
78 const wxArrayString
& choices
,
79 long style
, const wxValidator
& validator
,
80 const wxString
& name
)
82 wxCArrayString
chs(choices
);
84 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
85 chs
.GetStrings(), style
, validator
, name
);
88 bool wxComboBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& value
,
89 const wxPoint
& pos
, const wxSize
& size
,
90 int n
, const wxString choices
[],
91 long style
, const wxValidator
& validator
,
92 const wxString
& name
)
96 if (!PreCreation( parent
, pos
, size
) ||
97 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
99 wxFAIL_MSG( wxT("wxComboBox creation failed") );
103 if (HasFlag(wxCB_SORT
))
104 m_strings
= new wxSortedArrayString();
106 m_widget
= gtk_combo_box_entry_new_text();
108 // Set it up to trigger default item on enter key press
109 GtkWidget
*widget
= gtk_bin_get_child(GTK_BIN(m_widget
));
110 gtk_entry_set_activates_default(GTK_ENTRY(widget
),
111 !HasFlag(wxTE_PROCESS_ENTER
));
113 if (HasFlag(wxBORDER_NONE
))
115 // Doesn't seem to work
116 // g_object_set (m_widget, "has-frame", FALSE, NULL);
119 GtkEntry
* const entry
= GetEntry();
121 gtk_entry_set_editable( entry
, TRUE
);
125 m_parent
->DoAddChild( this );
127 m_focusWidget
= GTK_WIDGET( entry
);
131 gtk_entry_set_text( entry
, wxGTK_CONV(value
) );
133 if (style
& wxCB_READONLY
)
134 gtk_entry_set_editable( entry
, FALSE
);
136 g_signal_connect_after (entry
, "changed",
137 G_CALLBACK (gtkcombobox_text_changed_callback
), this);
139 g_signal_connect_after (m_widget
, "changed",
140 G_CALLBACK (gtkcombobox_changed_callback
), this);
142 SetInitialSize(size
); // need this too because this is a wxControlWithItems
147 GtkEntry
*wxComboBox::GetEntry() const
149 return GTK_ENTRY(GTK_BIN(m_widget
)->child
);
152 GtkEditable
*wxComboBox::GetEditable() const
154 return GTK_EDITABLE( GTK_BIN(m_widget
)->child
);
157 void wxComboBox::OnChar( wxKeyEvent
&event
)
159 switch ( event
.GetKeyCode() )
162 if ( HasFlag(wxTE_PROCESS_ENTER
) )
164 // GTK automatically selects an item if its in the list
165 wxCommandEvent
eventEnter(wxEVT_COMMAND_TEXT_ENTER
, GetId());
166 eventEnter
.SetString( GetValue() );
167 eventEnter
.SetInt( GetSelection() );
168 eventEnter
.SetEventObject( this );
170 if ( HandleWindowEvent(eventEnter
) )
172 // Catch GTK event so that GTK doesn't open the drop
173 // down list upon RETURN.
183 void wxComboBox::DisableEvents()
185 g_signal_handlers_block_by_func(GTK_BIN(m_widget
)->child
,
186 (gpointer
)gtkcombobox_text_changed_callback
, this);
188 g_signal_handlers_block_by_func(m_widget
,
189 (gpointer
)gtkcombobox_changed_callback
, this);
192 void wxComboBox::EnableEvents()
194 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget
)->child
,
195 (gpointer
)gtkcombobox_text_changed_callback
, this);
197 g_signal_handlers_unblock_by_func(m_widget
,
198 (gpointer
)gtkcombobox_changed_callback
, this);
201 GtkWidget
* wxComboBox::GetConnectWidget()
203 return GTK_WIDGET( GetEntry() );
206 GdkWindow
*wxComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
208 wxUnusedVar(windows
);
210 return GetEntry()->text_area
;
215 wxComboBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
217 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new
, true);
220 // ----------------------------------------------------------------------------
221 // standard event handling
222 // ----------------------------------------------------------------------------
224 void wxComboBox::OnCut(wxCommandEvent
& WXUNUSED(event
))
229 void wxComboBox::OnCopy(wxCommandEvent
& WXUNUSED(event
))
234 void wxComboBox::OnPaste(wxCommandEvent
& WXUNUSED(event
))
239 void wxComboBox::OnUndo(wxCommandEvent
& WXUNUSED(event
))
244 void wxComboBox::OnRedo(wxCommandEvent
& WXUNUSED(event
))
249 void wxComboBox::OnDelete(wxCommandEvent
& WXUNUSED(event
))
254 void wxComboBox::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
259 void wxComboBox::OnUpdateCut(wxUpdateUIEvent
& event
)
261 event
.Enable( CanCut() );
264 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent
& event
)
266 event
.Enable( CanCopy() );
269 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent
& event
)
271 event
.Enable( CanPaste() );
274 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent
& event
)
276 event
.Enable( CanUndo() );
279 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent
& event
)
281 event
.Enable( CanRedo() );
284 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent
& event
)
286 event
.Enable(HasSelection() && IsEditable()) ;
289 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
291 event
.Enable(!wxTextEntry::IsEmpty());
294 #endif // wxUSE_COMBOBOX