| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/gtk/combobox.cpp |
| 3 | // Purpose: |
| 4 | // Author: Robert Roebling |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 1998 Robert Roebling |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | // For compilers that support precompilation, includes "wx.h". |
| 11 | #include "wx/wxprec.h" |
| 12 | |
| 13 | #if wxUSE_COMBOBOX |
| 14 | |
| 15 | #include "wx/combobox.h" |
| 16 | |
| 17 | #ifndef WX_PRECOMP |
| 18 | #include "wx/intl.h" |
| 19 | #include "wx/settings.h" |
| 20 | #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED |
| 21 | #include "wx/arrstr.h" |
| 22 | #endif |
| 23 | |
| 24 | #include <gtk/gtk.h> |
| 25 | #include "wx/gtk/private.h" |
| 26 | #include "wx/gtk/private/gtk2-compat.h" |
| 27 | |
| 28 | // ---------------------------------------------------------------------------- |
| 29 | // GTK callbacks |
| 30 | // ---------------------------------------------------------------------------- |
| 31 | |
| 32 | extern "C" { |
| 33 | static void |
| 34 | gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
| 35 | { |
| 36 | if (!combo->m_hasVMT) return; |
| 37 | |
| 38 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); |
| 39 | event.SetString( combo->GetValue() ); |
| 40 | event.SetEventObject( combo ); |
| 41 | combo->HandleWindowEvent( event ); |
| 42 | } |
| 43 | |
| 44 | static void |
| 45 | gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
| 46 | { |
| 47 | combo->SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED); |
| 48 | } |
| 49 | |
| 50 | static void |
| 51 | gtkcombobox_popupshown_callback(GObject *WXUNUSED(gobject), |
| 52 | GParamSpec *WXUNUSED(param_spec), |
| 53 | wxComboBox *combo) |
| 54 | { |
| 55 | gboolean isShown; |
| 56 | g_object_get( combo->m_widget, "popup-shown", &isShown, NULL ); |
| 57 | wxCommandEvent event( isShown ? wxEVT_COMMAND_COMBOBOX_DROPDOWN |
| 58 | : wxEVT_COMMAND_COMBOBOX_CLOSEUP, |
| 59 | combo->GetId() ); |
| 60 | event.SetEventObject( combo ); |
| 61 | combo->HandleWindowEvent( event ); |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | |
| 66 | //----------------------------------------------------------------------------- |
| 67 | // wxComboBox |
| 68 | //----------------------------------------------------------------------------- |
| 69 | |
| 70 | BEGIN_EVENT_TABLE(wxComboBox, wxChoice) |
| 71 | EVT_CHAR(wxComboBox::OnChar) |
| 72 | |
| 73 | EVT_MENU(wxID_CUT, wxComboBox::OnCut) |
| 74 | EVT_MENU(wxID_COPY, wxComboBox::OnCopy) |
| 75 | EVT_MENU(wxID_PASTE, wxComboBox::OnPaste) |
| 76 | EVT_MENU(wxID_UNDO, wxComboBox::OnUndo) |
| 77 | EVT_MENU(wxID_REDO, wxComboBox::OnRedo) |
| 78 | EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete) |
| 79 | EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll) |
| 80 | |
| 81 | EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut) |
| 82 | EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy) |
| 83 | EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste) |
| 84 | EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo) |
| 85 | EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo) |
| 86 | EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete) |
| 87 | EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll) |
| 88 | END_EVENT_TABLE() |
| 89 | |
| 90 | void wxComboBox::Init() |
| 91 | { |
| 92 | m_entry = NULL; |
| 93 | } |
| 94 | |
| 95 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, |
| 96 | const wxString& value, |
| 97 | const wxPoint& pos, const wxSize& size, |
| 98 | const wxArrayString& choices, |
| 99 | long style, const wxValidator& validator, |
| 100 | const wxString& name ) |
| 101 | { |
| 102 | wxCArrayString chs(choices); |
| 103 | |
| 104 | return Create( parent, id, value, pos, size, chs.GetCount(), |
| 105 | chs.GetStrings(), style, validator, name ); |
| 106 | } |
| 107 | |
| 108 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, |
| 109 | const wxPoint& pos, const wxSize& size, |
| 110 | int n, const wxString choices[], |
| 111 | long style, const wxValidator& validator, |
| 112 | const wxString& name ) |
| 113 | { |
| 114 | if (!PreCreation( parent, pos, size ) || |
| 115 | !CreateBase( parent, id, pos, size, style, validator, name )) |
| 116 | { |
| 117 | wxFAIL_MSG( wxT("wxComboBox creation failed") ); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | if (HasFlag(wxCB_SORT)) |
| 122 | m_strings = new wxGtkCollatedArrayString(); |
| 123 | |
| 124 | GTKCreateComboBoxWidget(); |
| 125 | |
| 126 | if (HasFlag(wxBORDER_NONE)) |
| 127 | { |
| 128 | // Doesn't seem to work |
| 129 | // g_object_set (m_widget, "has-frame", FALSE, NULL); |
| 130 | } |
| 131 | |
| 132 | GtkEntry * const entry = GetEntry(); |
| 133 | |
| 134 | if ( entry ) |
| 135 | { |
| 136 | // Set it up to trigger default item on enter key press |
| 137 | gtk_entry_set_activates_default( entry, |
| 138 | !HasFlag(wxTE_PROCESS_ENTER) ); |
| 139 | |
| 140 | gtk_editable_set_editable(GTK_EDITABLE(entry), true); |
| 141 | } |
| 142 | |
| 143 | Append(n, choices); |
| 144 | |
| 145 | m_parent->DoAddChild( this ); |
| 146 | |
| 147 | if ( entry ) |
| 148 | m_focusWidget = GTK_WIDGET( entry ); |
| 149 | |
| 150 | PostCreation(size); |
| 151 | |
| 152 | if ( entry ) |
| 153 | { |
| 154 | if (style & wxCB_READONLY) |
| 155 | { |
| 156 | // this will assert and do nothing if the value is not in our list |
| 157 | // of strings which is the desired behaviour (for consistency with |
| 158 | // wxMSW and also because it doesn't make sense to have a string |
| 159 | // which is not a possible choice in a read-only combobox) |
| 160 | SetStringSelection(value); |
| 161 | gtk_editable_set_editable(GTK_EDITABLE(entry), false); |
| 162 | } |
| 163 | else // editable combobox |
| 164 | { |
| 165 | // any value is accepted, even if it's not in our list |
| 166 | gtk_entry_set_text( entry, wxGTK_CONV(value) ); |
| 167 | } |
| 168 | |
| 169 | g_signal_connect_after (entry, "changed", |
| 170 | G_CALLBACK (gtkcombobox_text_changed_callback), this); |
| 171 | |
| 172 | GTKConnectClipboardSignals(GTK_WIDGET(entry)); |
| 173 | } |
| 174 | |
| 175 | g_signal_connect_after (m_widget, "changed", |
| 176 | G_CALLBACK (gtkcombobox_changed_callback), this); |
| 177 | |
| 178 | #ifndef __WXGTK3__ |
| 179 | if ( !gtk_check_version(2,10,0) ) |
| 180 | #endif |
| 181 | { |
| 182 | g_signal_connect (m_widget, "notify::popup-shown", |
| 183 | G_CALLBACK (gtkcombobox_popupshown_callback), this); |
| 184 | } |
| 185 | |
| 186 | SetInitialSize(size); // need this too because this is a wxControlWithItems |
| 187 | |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | void wxComboBox::GTKCreateComboBoxWidget() |
| 192 | { |
| 193 | #ifdef __WXGTK3__ |
| 194 | m_widget = gtk_combo_box_text_new_with_entry(); |
| 195 | #else |
| 196 | m_widget = gtk_combo_box_entry_new_text(); |
| 197 | #endif |
| 198 | g_object_ref(m_widget); |
| 199 | |
| 200 | m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget))); |
| 201 | } |
| 202 | |
| 203 | GtkEditable *wxComboBox::GetEditable() const |
| 204 | { |
| 205 | return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget))); |
| 206 | } |
| 207 | |
| 208 | void wxComboBox::OnChar( wxKeyEvent &event ) |
| 209 | { |
| 210 | switch ( event.GetKeyCode() ) |
| 211 | { |
| 212 | case WXK_RETURN: |
| 213 | if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() ) |
| 214 | { |
| 215 | // GTK automatically selects an item if its in the list |
| 216 | wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId()); |
| 217 | eventEnter.SetString( GetValue() ); |
| 218 | eventEnter.SetInt( GetSelection() ); |
| 219 | eventEnter.SetEventObject( this ); |
| 220 | |
| 221 | if ( HandleWindowEvent(eventEnter) ) |
| 222 | { |
| 223 | // Catch GTK event so that GTK doesn't open the drop |
| 224 | // down list upon RETURN. |
| 225 | return; |
| 226 | } |
| 227 | } |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | event.Skip(); |
| 232 | } |
| 233 | |
| 234 | void wxComboBox::EnableTextChangedEvents(bool enable) |
| 235 | { |
| 236 | if ( !GetEntry() ) |
| 237 | return; |
| 238 | |
| 239 | if ( enable ) |
| 240 | { |
| 241 | g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(m_widget)), |
| 242 | (gpointer)gtkcombobox_text_changed_callback, this); |
| 243 | } |
| 244 | else // disable |
| 245 | { |
| 246 | g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(m_widget)), |
| 247 | (gpointer)gtkcombobox_text_changed_callback, this); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void wxComboBox::GTKDisableEvents() |
| 252 | { |
| 253 | EnableTextChangedEvents(false); |
| 254 | |
| 255 | g_signal_handlers_block_by_func(m_widget, |
| 256 | (gpointer)gtkcombobox_changed_callback, this); |
| 257 | g_signal_handlers_block_by_func(m_widget, |
| 258 | (gpointer)gtkcombobox_popupshown_callback, this); |
| 259 | } |
| 260 | |
| 261 | void wxComboBox::GTKEnableEvents() |
| 262 | { |
| 263 | EnableTextChangedEvents(true); |
| 264 | |
| 265 | g_signal_handlers_unblock_by_func(m_widget, |
| 266 | (gpointer)gtkcombobox_changed_callback, this); |
| 267 | g_signal_handlers_unblock_by_func(m_widget, |
| 268 | (gpointer)gtkcombobox_popupshown_callback, this); |
| 269 | } |
| 270 | |
| 271 | GtkWidget* wxComboBox::GetConnectWidget() |
| 272 | { |
| 273 | return GTK_WIDGET( GetEntry() ); |
| 274 | } |
| 275 | |
| 276 | GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const |
| 277 | { |
| 278 | #ifdef __WXGTK3__ |
| 279 | // no access to internal GdkWindows |
| 280 | return NULL; |
| 281 | #else |
| 282 | return gtk_entry_get_text_window(GetEntry()); |
| 283 | #endif |
| 284 | } |
| 285 | |
| 286 | // static |
| 287 | wxVisualAttributes |
| 288 | wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
| 289 | { |
| 290 | #ifdef __WXGTK3__ |
| 291 | return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new_with_entry, true); |
| 292 | #else |
| 293 | return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true); |
| 294 | #endif |
| 295 | } |
| 296 | |
| 297 | void wxComboBox::SetValue(const wxString& value) |
| 298 | { |
| 299 | if ( HasFlag(wxCB_READONLY) ) |
| 300 | SetStringSelection(value); |
| 301 | else |
| 302 | wxTextEntry::SetValue(value); |
| 303 | } |
| 304 | |
| 305 | void wxComboBox::SetString(unsigned int n, const wxString& text) |
| 306 | { |
| 307 | wxChoice::SetString(n, text); |
| 308 | |
| 309 | if ( static_cast<int>(n) == GetSelection() ) |
| 310 | { |
| 311 | // We also need to update the currently shown text, for consistency |
| 312 | // with wxMSW and also because it makes sense as leaving the old string |
| 313 | // in the text but not in the list would be confusing to the user. |
| 314 | SetValue(text); |
| 315 | |
| 316 | // And we need to keep the selection unchanged, modifying the item is |
| 317 | // not supposed to deselect it. |
| 318 | SetSelection(n); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // ---------------------------------------------------------------------------- |
| 323 | // standard event handling |
| 324 | // ---------------------------------------------------------------------------- |
| 325 | |
| 326 | void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event)) |
| 327 | { |
| 328 | Cut(); |
| 329 | } |
| 330 | |
| 331 | void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event)) |
| 332 | { |
| 333 | Copy(); |
| 334 | } |
| 335 | |
| 336 | void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event)) |
| 337 | { |
| 338 | Paste(); |
| 339 | } |
| 340 | |
| 341 | void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event)) |
| 342 | { |
| 343 | Undo(); |
| 344 | } |
| 345 | |
| 346 | void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event)) |
| 347 | { |
| 348 | Redo(); |
| 349 | } |
| 350 | |
| 351 | void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event)) |
| 352 | { |
| 353 | RemoveSelection(); |
| 354 | } |
| 355 | |
| 356 | void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event)) |
| 357 | { |
| 358 | SelectAll(); |
| 359 | } |
| 360 | |
| 361 | void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event) |
| 362 | { |
| 363 | event.Enable( CanCut() ); |
| 364 | } |
| 365 | |
| 366 | void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event) |
| 367 | { |
| 368 | event.Enable( CanCopy() ); |
| 369 | } |
| 370 | |
| 371 | void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event) |
| 372 | { |
| 373 | event.Enable( CanPaste() ); |
| 374 | } |
| 375 | |
| 376 | void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event) |
| 377 | { |
| 378 | event.Enable( CanUndo() ); |
| 379 | } |
| 380 | |
| 381 | void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event) |
| 382 | { |
| 383 | event.Enable( CanRedo() ); |
| 384 | } |
| 385 | |
| 386 | void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event) |
| 387 | { |
| 388 | event.Enable(HasSelection() && IsEditable()) ; |
| 389 | } |
| 390 | |
| 391 | void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event) |
| 392 | { |
| 393 | event.Enable(!wxTextEntry::IsEmpty()); |
| 394 | } |
| 395 | |
| 396 | void wxComboBox::Popup() |
| 397 | { |
| 398 | gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) ); |
| 399 | } |
| 400 | |
| 401 | void wxComboBox::Dismiss() |
| 402 | { |
| 403 | gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) ); |
| 404 | } |
| 405 | #endif // wxUSE_COMBOBOX |