| 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 | // We use GtkCombo which has been deprecated since GTK+ 2.3.0 |
| 25 | // in favour of GtkComboBox for <GTK2.4 runtime |
| 26 | // We also use GtkList |
| 27 | #ifdef GTK_DISABLE_DEPRECATED |
| 28 | #undef GTK_DISABLE_DEPRECATED |
| 29 | #endif |
| 30 | #include "wx/gtk/private.h" |
| 31 | |
| 32 | //----------------------------------------------------------------------------- |
| 33 | // data |
| 34 | //----------------------------------------------------------------------------- |
| 35 | |
| 36 | extern bool g_blockEventsOnDrag; |
| 37 | static int g_SelectionBeforePopup = wxID_NONE; // this means the popup is hidden |
| 38 | |
| 39 | //----------------------------------------------------------------------------- |
| 40 | // "changed" - typing and list item matches get changed, select-child |
| 41 | // if it doesn't match an item then just get a single changed |
| 42 | //----------------------------------------------------------------------------- |
| 43 | |
| 44 | extern "C" { |
| 45 | static void |
| 46 | gtkcombo_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
| 47 | { |
| 48 | if (combo->m_ignoreNextUpdate) |
| 49 | { |
| 50 | combo->m_ignoreNextUpdate = false; |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if (!combo->m_hasVMT) return; |
| 55 | |
| 56 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); |
| 57 | event.SetString( combo->GetValue() ); |
| 58 | event.SetEventObject( combo ); |
| 59 | combo->GetEventHandler()->ProcessEvent( event ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | extern "C" { |
| 64 | static void |
| 65 | gtkcombo_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo)) |
| 66 | { |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | extern "C" { |
| 71 | static void |
| 72 | gtkcombo_popup_hide_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo) |
| 73 | { |
| 74 | // when the popup is hidden, throw a SELECTED event only if the combobox |
| 75 | // selection changed. |
| 76 | const int curSelection = combo->GetCurrentSelection(); |
| 77 | |
| 78 | const bool hasChanged = curSelection != g_SelectionBeforePopup; |
| 79 | |
| 80 | // reset the selection flag to value meaning that it is hidden and do it |
| 81 | // now, before generating the events, so that GetSelection() returns the |
| 82 | // new value from the event handler |
| 83 | g_SelectionBeforePopup = wxID_NONE; |
| 84 | |
| 85 | if ( hasChanged ) |
| 86 | { |
| 87 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); |
| 88 | event.SetInt( curSelection ); |
| 89 | event.SetString( combo->GetStringSelection() ); |
| 90 | event.SetEventObject( combo ); |
| 91 | combo->GetEventHandler()->ProcessEvent( event ); |
| 92 | |
| 93 | // for consistency with the other ports, send TEXT event |
| 94 | wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); |
| 95 | event2.SetString( combo->GetStringSelection() ); |
| 96 | event2.SetEventObject( combo ); |
| 97 | combo->GetEventHandler()->ProcessEvent( event2 ); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | extern "C" { |
| 103 | static void |
| 104 | gtkcombo_popup_show_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo) |
| 105 | { |
| 106 | // store the combobox selection value before the popup is shown |
| 107 | g_SelectionBeforePopup = combo->GetCurrentSelection(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | //----------------------------------------------------------------------------- |
| 112 | // "select-child" - click/cursor get select-child, changed, select-child |
| 113 | //----------------------------------------------------------------------------- |
| 114 | |
| 115 | extern "C" { |
| 116 | static void |
| 117 | gtkcombo_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
| 118 | { |
| 119 | if (!combo->m_hasVMT) return; |
| 120 | |
| 121 | if (g_blockEventsOnDrag) return; |
| 122 | |
| 123 | int curSelection = combo->GetCurrentSelection(); |
| 124 | |
| 125 | if (combo->m_prevSelection == curSelection) return; |
| 126 | |
| 127 | GtkWidget *list = GTK_COMBO(combo->m_widget)->list; |
| 128 | gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection ); |
| 129 | |
| 130 | combo->m_prevSelection = curSelection; |
| 131 | |
| 132 | // Quickly set the value of the combo box |
| 133 | // as GTK+ does that only AFTER the event |
| 134 | // is sent. |
| 135 | g_signal_handlers_disconnect_by_func (GTK_COMBO (combo->GetHandle())->entry, |
| 136 | (gpointer) gtkcombo_text_changed_callback, |
| 137 | combo); |
| 138 | combo->SetValue( combo->GetStringSelection() ); |
| 139 | g_signal_connect_after (GTK_COMBO (combo->GetHandle())->entry, "changed", |
| 140 | G_CALLBACK (gtkcombo_text_changed_callback), combo); |
| 141 | |
| 142 | // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE) |
| 143 | // because when combobox popup is shown, gtkcombo_combo_select_child_callback is |
| 144 | // called each times the mouse is over an item with a pressed button so a lot |
| 145 | // of SELECTED event could be generated if the user keep the mouse button down |
| 146 | // and select other items ... |
| 147 | if (g_SelectionBeforePopup == wxID_NONE) |
| 148 | { |
| 149 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); |
| 150 | event.SetInt( curSelection ); |
| 151 | event.SetString( combo->GetStringSelection() ); |
| 152 | event.SetEventObject( combo ); |
| 153 | combo->GetEventHandler()->ProcessEvent( event ); |
| 154 | |
| 155 | // for consistency with the other ports, don't generate text update |
| 156 | // events while the user is browsing the combobox neither |
| 157 | wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); |
| 158 | event2.SetString( combo->GetValue() ); |
| 159 | event2.SetEventObject( combo ); |
| 160 | combo->GetEventHandler()->ProcessEvent( event2 ); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | #ifdef __WXGTK24__ |
| 166 | extern "C" { |
| 167 | static void |
| 168 | gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
| 169 | { |
| 170 | if (!combo->m_hasVMT) return; |
| 171 | |
| 172 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); |
| 173 | event.SetString( combo->GetValue() ); |
| 174 | event.SetEventObject( combo ); |
| 175 | combo->GetEventHandler()->ProcessEvent( event ); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | extern "C" { |
| 180 | static void |
| 181 | gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
| 182 | { |
| 183 | if (!combo->m_hasVMT) return; |
| 184 | |
| 185 | if (combo->GetSelection() == -1) |
| 186 | return; |
| 187 | |
| 188 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); |
| 189 | event.SetInt( combo->GetSelection() ); |
| 190 | event.SetString( combo->GetStringSelection() ); |
| 191 | event.SetEventObject( combo ); |
| 192 | combo->GetEventHandler()->ProcessEvent( event ); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | #endif |
| 197 | |
| 198 | //----------------------------------------------------------------------------- |
| 199 | // wxComboBox |
| 200 | //----------------------------------------------------------------------------- |
| 201 | |
| 202 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl) |
| 203 | |
| 204 | BEGIN_EVENT_TABLE(wxComboBox, wxControl) |
| 205 | EVT_SIZE(wxComboBox::OnSize) |
| 206 | EVT_CHAR(wxComboBox::OnChar) |
| 207 | |
| 208 | EVT_MENU(wxID_CUT, wxComboBox::OnCut) |
| 209 | EVT_MENU(wxID_COPY, wxComboBox::OnCopy) |
| 210 | EVT_MENU(wxID_PASTE, wxComboBox::OnPaste) |
| 211 | EVT_MENU(wxID_UNDO, wxComboBox::OnUndo) |
| 212 | EVT_MENU(wxID_REDO, wxComboBox::OnRedo) |
| 213 | EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete) |
| 214 | EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll) |
| 215 | |
| 216 | EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut) |
| 217 | EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy) |
| 218 | EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste) |
| 219 | EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo) |
| 220 | EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo) |
| 221 | EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete) |
| 222 | EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll) |
| 223 | END_EVENT_TABLE() |
| 224 | |
| 225 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, |
| 226 | const wxString& value, |
| 227 | const wxPoint& pos, const wxSize& size, |
| 228 | const wxArrayString& choices, |
| 229 | long style, const wxValidator& validator, |
| 230 | const wxString& name ) |
| 231 | { |
| 232 | wxCArrayString chs(choices); |
| 233 | |
| 234 | return Create( parent, id, value, pos, size, chs.GetCount(), |
| 235 | chs.GetStrings(), style, validator, name ); |
| 236 | } |
| 237 | |
| 238 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, |
| 239 | const wxPoint& pos, const wxSize& size, |
| 240 | int n, const wxString choices[], |
| 241 | long style, const wxValidator& validator, |
| 242 | const wxString& name ) |
| 243 | { |
| 244 | m_ignoreNextUpdate = false; |
| 245 | m_prevSelection = 0; |
| 246 | |
| 247 | if (!PreCreation( parent, pos, size ) || |
| 248 | !CreateBase( parent, id, pos, size, style, validator, name )) |
| 249 | { |
| 250 | wxFAIL_MSG( wxT("wxComboBox creation failed") ); |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | #ifdef __WXGTK24__ |
| 255 | if (!gtk_check_version(2,4,0)) |
| 256 | { |
| 257 | m_widget = gtk_combo_box_entry_new_text(); |
| 258 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 259 | |
| 260 | gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget)->child ), TRUE ); |
| 261 | |
| 262 | for (int i = 0; i < n; i++) |
| 263 | { |
| 264 | gtk_combo_box_append_text( combobox, wxGTK_CONV( choices[i] ) ); |
| 265 | |
| 266 | m_clientDataList.Append( (wxObject*)NULL ); |
| 267 | m_clientObjectList.Append( (wxObject*)NULL ); |
| 268 | } |
| 269 | } |
| 270 | else |
| 271 | #endif |
| 272 | { |
| 273 | m_widget = gtk_combo_new(); |
| 274 | GtkCombo* combo = GTK_COMBO(m_widget); |
| 275 | |
| 276 | // Disable GTK's broken events ... |
| 277 | g_signal_handler_disconnect (combo->entry, combo->entry_change_id); |
| 278 | // ... and add surrogate handler. |
| 279 | combo->entry_change_id = g_signal_connect (combo->entry, "changed", |
| 280 | G_CALLBACK (gtkcombo_dummy_callback), |
| 281 | combo); |
| 282 | |
| 283 | // make it more useable |
| 284 | gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE ); |
| 285 | |
| 286 | // and case-sensitive |
| 287 | gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE ); |
| 288 | |
| 289 | if (style & wxNO_BORDER) |
| 290 | g_object_set (combo->entry, "has-frame", FALSE, NULL ); |
| 291 | |
| 292 | GtkWidget *list = combo->list; |
| 293 | |
| 294 | for (int i = 0; i < n; i++) |
| 295 | { |
| 296 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) ); |
| 297 | |
| 298 | m_clientDataList.Append( (wxObject*)NULL ); |
| 299 | m_clientObjectList.Append( (wxObject*)NULL ); |
| 300 | |
| 301 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
| 302 | |
| 303 | gtk_widget_show( list_item ); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | |
| 308 | m_parent->DoAddChild( this ); |
| 309 | |
| 310 | GtkEntry *entry = NULL; |
| 311 | #ifdef __WXGTK24__ |
| 312 | if (!gtk_check_version(2,4,0)) |
| 313 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 314 | else |
| 315 | #endif |
| 316 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 317 | |
| 318 | m_focusWidget = GTK_WIDGET( entry ); |
| 319 | |
| 320 | PostCreation(size); |
| 321 | |
| 322 | #ifdef __WXGTK24__ |
| 323 | if (!gtk_check_version(2,4,0)) |
| 324 | ConnectWidget( m_widget ); |
| 325 | else |
| 326 | #endif |
| 327 | ConnectWidget( GTK_COMBO(m_widget)->button ); |
| 328 | |
| 329 | #ifdef __WXGTK24__ |
| 330 | if (!gtk_check_version(2,4,0)) |
| 331 | { |
| 332 | gtk_entry_set_text( entry, wxGTK_CONV(value) ); |
| 333 | |
| 334 | if (style & wxCB_READONLY) |
| 335 | gtk_entry_set_editable( entry, FALSE ); |
| 336 | |
| 337 | g_signal_connect_after (entry, "changed", |
| 338 | G_CALLBACK (gtkcombobox_text_changed_callback), this); |
| 339 | |
| 340 | g_signal_connect_after (m_widget, "changed", |
| 341 | G_CALLBACK (gtkcombobox_changed_callback), this); |
| 342 | |
| 343 | } |
| 344 | else |
| 345 | #endif |
| 346 | { |
| 347 | GtkCombo *combo = GTK_COMBO(m_widget); |
| 348 | // MSW's combo box shows the value and the selection is -1 |
| 349 | gtk_entry_set_text( entry, wxGTK_CONV(value) ); |
| 350 | gtk_list_unselect_all( GTK_LIST(combo->list) ); |
| 351 | |
| 352 | if (style & wxCB_READONLY) |
| 353 | gtk_entry_set_editable( entry, FALSE ); |
| 354 | |
| 355 | // "show" and "hide" events are generated when user click on the combobox button which popups a list |
| 356 | // this list is the "popwin" gtk widget |
| 357 | g_signal_connect (GTK_COMBO(combo)->popwin, "hide", |
| 358 | G_CALLBACK (gtkcombo_popup_hide_callback), this); |
| 359 | g_signal_connect (GTK_COMBO(combo)->popwin, "show", |
| 360 | G_CALLBACK (gtkcombo_popup_show_callback), this); |
| 361 | g_signal_connect_after (combo->list, "select-child", |
| 362 | G_CALLBACK (gtkcombo_combo_select_child_callback), |
| 363 | this); |
| 364 | g_signal_connect_after (entry, "changed", |
| 365 | G_CALLBACK (gtkcombo_text_changed_callback), this); |
| 366 | } |
| 367 | |
| 368 | SetInitialSize(size); // need this too because this is a wxControlWithItems |
| 369 | |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | wxComboBox::~wxComboBox() |
| 374 | { |
| 375 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
| 376 | while (node) |
| 377 | { |
| 378 | wxClientData *cd = (wxClientData*)node->GetData(); |
| 379 | if (cd) delete cd; |
| 380 | node = node->GetNext(); |
| 381 | } |
| 382 | m_clientObjectList.Clear(); |
| 383 | |
| 384 | m_clientDataList.Clear(); |
| 385 | } |
| 386 | |
| 387 | void wxComboBox::SetFocus() |
| 388 | { |
| 389 | if ( m_hasFocus ) |
| 390 | { |
| 391 | // don't do anything if we already have focus |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | gtk_widget_grab_focus( m_focusWidget ); |
| 396 | } |
| 397 | |
| 398 | int wxComboBox::DoAppend( const wxString &item ) |
| 399 | { |
| 400 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
| 401 | |
| 402 | #ifdef __WXGTK24__ |
| 403 | if (!gtk_check_version(2,4,0)) |
| 404 | { |
| 405 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 406 | gtk_combo_box_append_text( combobox, wxGTK_CONV( item ) ); |
| 407 | } |
| 408 | else |
| 409 | #endif |
| 410 | { |
| 411 | DisableEvents(); |
| 412 | |
| 413 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 414 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); |
| 415 | |
| 416 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
| 417 | |
| 418 | if (GTK_WIDGET_REALIZED(m_widget)) |
| 419 | { |
| 420 | gtk_widget_realize( list_item ); |
| 421 | gtk_widget_realize( GTK_BIN(list_item)->child ); |
| 422 | } |
| 423 | |
| 424 | // Apply current widget style to the new list_item |
| 425 | GtkRcStyle *style = CreateWidgetStyle(); |
| 426 | if (style) |
| 427 | { |
| 428 | gtk_widget_modify_style(list_item, style); |
| 429 | GtkBin *bin = GTK_BIN( list_item ); |
| 430 | GtkWidget *label = bin->child; |
| 431 | gtk_widget_modify_style( label, style ); |
| 432 | gtk_rc_style_unref( style ); |
| 433 | } |
| 434 | |
| 435 | gtk_widget_show( list_item ); |
| 436 | |
| 437 | EnableEvents(); |
| 438 | } |
| 439 | |
| 440 | const unsigned int count = GetCount(); |
| 441 | |
| 442 | if ( m_clientDataList.GetCount() < count ) |
| 443 | m_clientDataList.Append( (wxObject*) NULL ); |
| 444 | if ( m_clientObjectList.GetCount() < count ) |
| 445 | m_clientObjectList.Append( (wxObject*) NULL ); |
| 446 | |
| 447 | InvalidateBestSize(); |
| 448 | |
| 449 | return count - 1; |
| 450 | } |
| 451 | |
| 452 | int wxComboBox::DoInsert(const wxString &item, unsigned int pos) |
| 453 | { |
| 454 | wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, |
| 455 | wxT("can't insert into sorted list")); |
| 456 | |
| 457 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
| 458 | wxCHECK_MSG( IsValidInsert(pos), -1, wxT("invalid index") ); |
| 459 | |
| 460 | unsigned int count = GetCount(); |
| 461 | |
| 462 | if (pos == count) |
| 463 | return Append(item); |
| 464 | |
| 465 | #ifdef __WXGTK24__ |
| 466 | if (!gtk_check_version(2,4,0)) |
| 467 | { |
| 468 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 469 | gtk_combo_box_insert_text( combobox, pos, wxGTK_CONV( item ) ); |
| 470 | } |
| 471 | else |
| 472 | #endif |
| 473 | { |
| 474 | DisableEvents(); |
| 475 | |
| 476 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 477 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); |
| 478 | |
| 479 | GList *gitem_list = g_list_alloc (); |
| 480 | gitem_list->data = list_item; |
| 481 | gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); |
| 482 | |
| 483 | if (GTK_WIDGET_REALIZED(m_widget)) |
| 484 | { |
| 485 | gtk_widget_realize( list_item ); |
| 486 | gtk_widget_realize( GTK_BIN(list_item)->child ); |
| 487 | |
| 488 | ApplyWidgetStyle(); |
| 489 | } |
| 490 | |
| 491 | gtk_widget_show( list_item ); |
| 492 | |
| 493 | EnableEvents(); |
| 494 | } |
| 495 | |
| 496 | count = GetCount(); |
| 497 | |
| 498 | if ( m_clientDataList.GetCount() < count ) |
| 499 | m_clientDataList.Insert( pos, (wxObject*) NULL ); |
| 500 | if ( m_clientObjectList.GetCount() < count ) |
| 501 | m_clientObjectList.Insert( pos, (wxObject*) NULL ); |
| 502 | |
| 503 | InvalidateBestSize(); |
| 504 | |
| 505 | return pos; |
| 506 | } |
| 507 | |
| 508 | void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData) |
| 509 | { |
| 510 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 511 | |
| 512 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
| 513 | if (!node) return; |
| 514 | |
| 515 | node->SetData( (wxObject*) clientData ); |
| 516 | } |
| 517 | |
| 518 | void* wxComboBox::DoGetItemClientData(unsigned int n) const |
| 519 | { |
| 520 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); |
| 521 | |
| 522 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
| 523 | |
| 524 | return node ? node->GetData() : NULL; |
| 525 | } |
| 526 | |
| 527 | void wxComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData) |
| 528 | { |
| 529 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 530 | |
| 531 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
| 532 | if (!node) return; |
| 533 | |
| 534 | // wxItemContainer already deletes data for us |
| 535 | |
| 536 | node->SetData( (wxObject*) clientData ); |
| 537 | } |
| 538 | |
| 539 | wxClientData* wxComboBox::DoGetItemClientObject(unsigned int n) const |
| 540 | { |
| 541 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); |
| 542 | |
| 543 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
| 544 | |
| 545 | return node ? (wxClientData*) node->GetData() : NULL; |
| 546 | } |
| 547 | |
| 548 | void wxComboBox::Clear() |
| 549 | { |
| 550 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 551 | |
| 552 | DisableEvents(); |
| 553 | |
| 554 | #ifdef __WXGTK24__ |
| 555 | if (!gtk_check_version(2,4,0)) |
| 556 | { |
| 557 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 558 | const unsigned int count = GetCount(); |
| 559 | for (unsigned int i = 0; i < count; i++) |
| 560 | gtk_combo_box_remove_text( combobox, 0 ); |
| 561 | } |
| 562 | else // GTK+ < 2.4.0 |
| 563 | #endif // __WXGTK24__ |
| 564 | { |
| 565 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 566 | gtk_list_clear_items( GTK_LIST(list), 0, GetCount() ); |
| 567 | } |
| 568 | |
| 569 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
| 570 | while (node) |
| 571 | { |
| 572 | wxClientData *cd = (wxClientData*)node->GetData(); |
| 573 | delete cd; |
| 574 | node = node->GetNext(); |
| 575 | } |
| 576 | m_clientObjectList.Clear(); |
| 577 | |
| 578 | m_clientDataList.Clear(); |
| 579 | |
| 580 | EnableEvents(); |
| 581 | |
| 582 | InvalidateBestSize(); |
| 583 | } |
| 584 | |
| 585 | void wxComboBox::Delete(unsigned int n) |
| 586 | { |
| 587 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 588 | |
| 589 | #ifdef __WXGTK24__ |
| 590 | if (!gtk_check_version(2,4,0)) |
| 591 | { |
| 592 | wxCHECK_RET( IsValid(n), wxT("invalid index") ); |
| 593 | |
| 594 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 595 | gtk_combo_box_remove_text( combobox, n ); |
| 596 | } |
| 597 | else |
| 598 | #endif |
| 599 | { |
| 600 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); |
| 601 | |
| 602 | GList *child = g_list_nth( listbox->children, n ); |
| 603 | |
| 604 | if (!child) |
| 605 | { |
| 606 | wxFAIL_MSG(wxT("wrong index")); |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | DisableEvents(); |
| 611 | |
| 612 | GList *list = g_list_append( (GList*) NULL, child->data ); |
| 613 | gtk_list_remove_items( listbox, list ); |
| 614 | g_list_free( list ); |
| 615 | |
| 616 | EnableEvents(); |
| 617 | } |
| 618 | |
| 619 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
| 620 | if (node) |
| 621 | { |
| 622 | wxClientData *cd = (wxClientData*)node->GetData(); |
| 623 | if (cd) delete cd; |
| 624 | m_clientObjectList.Erase( node ); |
| 625 | } |
| 626 | |
| 627 | node = m_clientDataList.Item( n ); |
| 628 | if (node) |
| 629 | m_clientDataList.Erase( node ); |
| 630 | |
| 631 | InvalidateBestSize(); |
| 632 | } |
| 633 | |
| 634 | void wxComboBox::SetString(unsigned int n, const wxString &text) |
| 635 | { |
| 636 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 637 | |
| 638 | #ifdef __WXGTK24__ |
| 639 | if (!gtk_check_version(2,4,0)) |
| 640 | { |
| 641 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 642 | wxCHECK_RET( IsValid(n), wxT("invalid index") ); |
| 643 | |
| 644 | GtkTreeModel *model = gtk_combo_box_get_model( combobox ); |
| 645 | GtkTreeIter iter; |
| 646 | if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n)) |
| 647 | { |
| 648 | GValue value = { 0, }; |
| 649 | g_value_init( &value, G_TYPE_STRING ); |
| 650 | g_value_set_string( &value, wxGTK_CONV( text ) ); |
| 651 | gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, 0, &value ); |
| 652 | g_value_unset( &value ); |
| 653 | } |
| 654 | } |
| 655 | else |
| 656 | #endif |
| 657 | { |
| 658 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 659 | |
| 660 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); |
| 661 | if (child) |
| 662 | { |
| 663 | GtkBin *bin = GTK_BIN( child->data ); |
| 664 | GtkLabel *label = GTK_LABEL( bin->child ); |
| 665 | gtk_label_set_text(label, wxGTK_CONV(text)); |
| 666 | } |
| 667 | else |
| 668 | { |
| 669 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | InvalidateBestSize(); |
| 674 | } |
| 675 | |
| 676 | int wxComboBox::FindString( const wxString &item, bool bCase ) const |
| 677 | { |
| 678 | wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") ); |
| 679 | |
| 680 | #ifdef __WXGTK24__ |
| 681 | if (!gtk_check_version(2,4,0)) |
| 682 | { |
| 683 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 684 | GtkTreeModel* model = gtk_combo_box_get_model( combobox ); |
| 685 | GtkTreeIter iter; |
| 686 | gtk_tree_model_get_iter_first( model, &iter ); |
| 687 | if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter )) |
| 688 | return -1; |
| 689 | int count = 0; |
| 690 | do |
| 691 | { |
| 692 | GValue value = { 0, }; |
| 693 | gtk_tree_model_get_value( model, &iter, 0, &value ); |
| 694 | wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) ); |
| 695 | g_value_unset( &value ); |
| 696 | |
| 697 | if (item.IsSameAs( str, bCase ) ) |
| 698 | return count; |
| 699 | |
| 700 | count++; |
| 701 | |
| 702 | } while (gtk_tree_model_iter_next( model, &iter )); |
| 703 | } |
| 704 | else |
| 705 | #endif |
| 706 | { |
| 707 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 708 | |
| 709 | GList *child = GTK_LIST(list)->children; |
| 710 | int count = 0; |
| 711 | while (child) |
| 712 | { |
| 713 | GtkBin *bin = GTK_BIN( child->data ); |
| 714 | GtkLabel *label = GTK_LABEL( bin->child ); |
| 715 | wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); |
| 716 | |
| 717 | if (item.IsSameAs( str , bCase ) ) |
| 718 | return count; |
| 719 | |
| 720 | count++; |
| 721 | child = child->next; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | return wxNOT_FOUND; |
| 726 | } |
| 727 | |
| 728 | int wxComboBox::GetSelection() const |
| 729 | { |
| 730 | #ifdef __WXGTK24__ |
| 731 | if (!gtk_check_version(2,4,0)) |
| 732 | { |
| 733 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 734 | return gtk_combo_box_get_active( combobox ); |
| 735 | } |
| 736 | else |
| 737 | #endif |
| 738 | // if the popup is currently opened, use the selection as it had been |
| 739 | // before it dropped down |
| 740 | return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection() |
| 741 | : g_SelectionBeforePopup; |
| 742 | } |
| 743 | |
| 744 | int wxComboBox::GetCurrentSelection() const |
| 745 | { |
| 746 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
| 747 | |
| 748 | #ifdef __WXGTK24__ |
| 749 | if (!gtk_check_version(2,4,0)) |
| 750 | { |
| 751 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 752 | return gtk_combo_box_get_active( combobox ); |
| 753 | } |
| 754 | else |
| 755 | #endif |
| 756 | { |
| 757 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 758 | |
| 759 | GList *selection = GTK_LIST(list)->selection; |
| 760 | if (selection) |
| 761 | { |
| 762 | GList *child = GTK_LIST(list)->children; |
| 763 | int count = 0; |
| 764 | while (child) |
| 765 | { |
| 766 | if (child->data == selection->data) return count; |
| 767 | count++; |
| 768 | child = child->next; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | return -1; |
| 774 | } |
| 775 | |
| 776 | wxString wxComboBox::GetString(unsigned int n) const |
| 777 | { |
| 778 | wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") ); |
| 779 | |
| 780 | wxString str; |
| 781 | |
| 782 | #ifdef __WXGTK24__ |
| 783 | if (!gtk_check_version(2,4,0)) |
| 784 | { |
| 785 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 786 | GtkTreeModel *model = gtk_combo_box_get_model( combobox ); |
| 787 | GtkTreeIter iter; |
| 788 | if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n)) |
| 789 | { |
| 790 | GValue value = { 0, }; |
| 791 | gtk_tree_model_get_value( model, &iter, 0, &value ); |
| 792 | wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) ); |
| 793 | g_value_unset( &value ); |
| 794 | return tmp; |
| 795 | } |
| 796 | } |
| 797 | else |
| 798 | #endif |
| 799 | { |
| 800 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 801 | |
| 802 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); |
| 803 | if (child) |
| 804 | { |
| 805 | GtkBin *bin = GTK_BIN( child->data ); |
| 806 | GtkLabel *label = GTK_LABEL( bin->child ); |
| 807 | str = wxGTK_CONV_BACK( gtk_label_get_text(label) ); |
| 808 | } |
| 809 | else |
| 810 | { |
| 811 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | return str; |
| 816 | } |
| 817 | |
| 818 | wxString wxComboBox::GetStringSelection() const |
| 819 | { |
| 820 | wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") ); |
| 821 | |
| 822 | #ifdef __WXGTK24__ |
| 823 | if (!gtk_check_version(2,4,0)) |
| 824 | { |
| 825 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 826 | int sel = gtk_combo_box_get_active( combobox ); |
| 827 | if (sel == -1) |
| 828 | return wxEmptyString; |
| 829 | return GetString(sel); |
| 830 | } |
| 831 | else |
| 832 | #endif |
| 833 | { |
| 834 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 835 | |
| 836 | GList *selection = GTK_LIST(list)->selection; |
| 837 | if (selection) |
| 838 | { |
| 839 | GtkBin *bin = GTK_BIN( selection->data ); |
| 840 | GtkLabel *label = GTK_LABEL( bin->child ); |
| 841 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); |
| 842 | return tmp; |
| 843 | } |
| 844 | |
| 845 | wxFAIL_MSG( wxT("wxComboBox: no selection") ); |
| 846 | } |
| 847 | |
| 848 | return wxEmptyString; |
| 849 | } |
| 850 | |
| 851 | unsigned int wxComboBox::GetCount() const |
| 852 | { |
| 853 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); |
| 854 | |
| 855 | #ifdef __WXGTK24__ |
| 856 | if (!gtk_check_version(2,4,0)) |
| 857 | { |
| 858 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 859 | GtkTreeModel* model = gtk_combo_box_get_model( combobox ); |
| 860 | GtkTreeIter iter; |
| 861 | gtk_tree_model_get_iter_first( model, &iter ); |
| 862 | if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter )) |
| 863 | return 0; |
| 864 | unsigned int ret = 1; |
| 865 | while (gtk_tree_model_iter_next( model, &iter )) |
| 866 | ret++; |
| 867 | return ret; |
| 868 | } |
| 869 | else |
| 870 | #endif |
| 871 | { |
| 872 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 873 | |
| 874 | GList *child = GTK_LIST(list)->children; |
| 875 | unsigned int count = 0; |
| 876 | while (child) |
| 877 | { |
| 878 | count++; |
| 879 | child = child->next; |
| 880 | } |
| 881 | return count; |
| 882 | } |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | void wxComboBox::SetSelection( int n ) |
| 888 | { |
| 889 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 890 | |
| 891 | DisableEvents(); |
| 892 | |
| 893 | #ifdef __WXGTK24__ |
| 894 | if (!gtk_check_version(2,4,0)) |
| 895 | { |
| 896 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
| 897 | gtk_combo_box_set_active( combobox, n ); |
| 898 | } |
| 899 | else |
| 900 | #endif |
| 901 | { |
| 902 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 903 | gtk_list_unselect_item( GTK_LIST(list), m_prevSelection ); |
| 904 | gtk_list_select_item( GTK_LIST(list), n ); |
| 905 | m_prevSelection = n; |
| 906 | } |
| 907 | |
| 908 | EnableEvents(); |
| 909 | } |
| 910 | |
| 911 | wxString wxComboBox::GetValue() const |
| 912 | { |
| 913 | GtkEntry *entry = NULL; |
| 914 | #ifdef __WXGTK24__ |
| 915 | if (!gtk_check_version(2,4,0)) |
| 916 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 917 | else |
| 918 | #endif |
| 919 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 920 | |
| 921 | wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) ); |
| 922 | |
| 923 | #if 0 |
| 924 | for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++) |
| 925 | { |
| 926 | wxChar c = tmp[i]; |
| 927 | printf( "%d ", (int) (c) ); |
| 928 | } |
| 929 | printf( "\n" ); |
| 930 | #endif |
| 931 | |
| 932 | return tmp; |
| 933 | } |
| 934 | |
| 935 | void wxComboBox::SetValue( const wxString& value ) |
| 936 | { |
| 937 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 938 | |
| 939 | GtkEntry *entry = NULL; |
| 940 | #ifdef __WXGTK24__ |
| 941 | if (!gtk_check_version(2,4,0)) |
| 942 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 943 | else |
| 944 | #endif |
| 945 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 946 | |
| 947 | wxString tmp; |
| 948 | if (!value.IsNull()) tmp = value; |
| 949 | |
| 950 | DisableEvents(); |
| 951 | gtk_entry_set_text( entry, wxGTK_CONV( tmp ) ); |
| 952 | EnableEvents(); |
| 953 | |
| 954 | InvalidateBestSize(); |
| 955 | } |
| 956 | |
| 957 | void wxComboBox::Copy() |
| 958 | { |
| 959 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 960 | |
| 961 | GtkEntry *entry = NULL; |
| 962 | #ifdef __WXGTK24__ |
| 963 | if (!gtk_check_version(2,4,0)) |
| 964 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 965 | else |
| 966 | #endif |
| 967 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 968 | |
| 969 | gtk_editable_copy_clipboard(GTK_EDITABLE(entry)); |
| 970 | } |
| 971 | |
| 972 | void wxComboBox::Cut() |
| 973 | { |
| 974 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 975 | |
| 976 | GtkEntry *entry = NULL; |
| 977 | #ifdef __WXGTK24__ |
| 978 | if (!gtk_check_version(2,4,0)) |
| 979 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 980 | else |
| 981 | #endif |
| 982 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 983 | |
| 984 | gtk_editable_cut_clipboard(GTK_EDITABLE(entry)); |
| 985 | } |
| 986 | |
| 987 | void wxComboBox::Paste() |
| 988 | { |
| 989 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 990 | |
| 991 | GtkEntry *entry = NULL; |
| 992 | #ifdef __WXGTK24__ |
| 993 | if (!gtk_check_version(2,4,0)) |
| 994 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 995 | else |
| 996 | #endif |
| 997 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 998 | |
| 999 | gtk_editable_paste_clipboard(GTK_EDITABLE(entry)); |
| 1000 | } |
| 1001 | |
| 1002 | void wxComboBox::Undo() |
| 1003 | { |
| 1004 | // TODO |
| 1005 | } |
| 1006 | |
| 1007 | void wxComboBox::Redo() |
| 1008 | { |
| 1009 | // TODO |
| 1010 | } |
| 1011 | |
| 1012 | void wxComboBox::SelectAll() |
| 1013 | { |
| 1014 | SetSelection(0, GetLastPosition()); |
| 1015 | } |
| 1016 | |
| 1017 | bool wxComboBox::CanUndo() const |
| 1018 | { |
| 1019 | // TODO |
| 1020 | return false; |
| 1021 | } |
| 1022 | |
| 1023 | bool wxComboBox::CanRedo() const |
| 1024 | { |
| 1025 | // TODO |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
| 1029 | bool wxComboBox::HasSelection() const |
| 1030 | { |
| 1031 | long from, to; |
| 1032 | GetSelection(&from, &to); |
| 1033 | return from != to; |
| 1034 | } |
| 1035 | |
| 1036 | bool wxComboBox::CanCopy() const |
| 1037 | { |
| 1038 | // Can copy if there's a selection |
| 1039 | return HasSelection(); |
| 1040 | } |
| 1041 | |
| 1042 | bool wxComboBox::CanCut() const |
| 1043 | { |
| 1044 | return CanCopy() && IsEditable(); |
| 1045 | } |
| 1046 | |
| 1047 | bool wxComboBox::CanPaste() const |
| 1048 | { |
| 1049 | // TODO: check for text on the clipboard |
| 1050 | return IsEditable() ; |
| 1051 | } |
| 1052 | |
| 1053 | bool wxComboBox::IsEditable() const |
| 1054 | { |
| 1055 | return !HasFlag(wxCB_READONLY); |
| 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | void wxComboBox::SetInsertionPoint( long pos ) |
| 1060 | { |
| 1061 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 1062 | |
| 1063 | if ( pos == GetLastPosition() ) |
| 1064 | pos = -1; |
| 1065 | |
| 1066 | GtkEntry *entry = NULL; |
| 1067 | #ifdef __WXGTK24__ |
| 1068 | if (!gtk_check_version(2,4,0)) |
| 1069 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 1070 | else |
| 1071 | #endif |
| 1072 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 1073 | |
| 1074 | gtk_entry_set_position( entry, (int)pos ); |
| 1075 | } |
| 1076 | |
| 1077 | long wxComboBox::GetInsertionPoint() const |
| 1078 | { |
| 1079 | GtkEntry *entry = NULL; |
| 1080 | #ifdef __WXGTK24__ |
| 1081 | if (!gtk_check_version(2,4,0)) |
| 1082 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 1083 | else |
| 1084 | #endif |
| 1085 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 1086 | |
| 1087 | return (long) gtk_editable_get_position(GTK_EDITABLE(entry)); |
| 1088 | } |
| 1089 | |
| 1090 | wxTextPos wxComboBox::GetLastPosition() const |
| 1091 | { |
| 1092 | GtkEntry *entry = NULL; |
| 1093 | #ifdef __WXGTK24__ |
| 1094 | if (!gtk_check_version(2,4,0)) |
| 1095 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 1096 | else |
| 1097 | #endif |
| 1098 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 1099 | |
| 1100 | int pos = entry->text_length; |
| 1101 | return (long) pos-1; |
| 1102 | } |
| 1103 | |
| 1104 | void wxComboBox::Replace( long from, long to, const wxString& value ) |
| 1105 | { |
| 1106 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 1107 | |
| 1108 | GtkEntry *entry = NULL; |
| 1109 | #ifdef __WXGTK24__ |
| 1110 | if (!gtk_check_version(2,4,0)) |
| 1111 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 1112 | else |
| 1113 | #endif |
| 1114 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 1115 | |
| 1116 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); |
| 1117 | if (value.IsNull()) return; |
| 1118 | gint pos = (gint)to; |
| 1119 | |
| 1120 | #if wxUSE_UNICODE |
| 1121 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( value ); |
| 1122 | gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos ); |
| 1123 | #else |
| 1124 | gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.length(), &pos ); |
| 1125 | #endif |
| 1126 | } |
| 1127 | |
| 1128 | void wxComboBox::SetSelection( long from, long to ) |
| 1129 | { |
| 1130 | GtkEntry *entry = NULL; |
| 1131 | #ifdef __WXGTK24__ |
| 1132 | if (!gtk_check_version(2,4,0)) |
| 1133 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 1134 | else |
| 1135 | #endif |
| 1136 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 1137 | |
| 1138 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); |
| 1139 | } |
| 1140 | |
| 1141 | void wxComboBox::GetSelection( long* from, long* to ) const |
| 1142 | { |
| 1143 | GtkEntry *entry = NULL; |
| 1144 | #ifdef __WXGTK24__ |
| 1145 | if (!gtk_check_version(2,4,0)) |
| 1146 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 1147 | else |
| 1148 | #endif |
| 1149 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 1150 | |
| 1151 | if (IsEditable()) |
| 1152 | { |
| 1153 | GtkEditable *editable = GTK_EDITABLE(entry); |
| 1154 | gint start, end; |
| 1155 | gtk_editable_get_selection_bounds(editable, & start, & end); |
| 1156 | *from = start; |
| 1157 | *to = end; |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | void wxComboBox::SetEditable( bool editable ) |
| 1162 | { |
| 1163 | GtkEntry *entry = NULL; |
| 1164 | #ifdef __WXGTK24__ |
| 1165 | if (!gtk_check_version(2,4,0)) |
| 1166 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 1167 | else |
| 1168 | #endif |
| 1169 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 1170 | |
| 1171 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); |
| 1172 | } |
| 1173 | |
| 1174 | void wxComboBox::OnChar( wxKeyEvent &event ) |
| 1175 | { |
| 1176 | if ( event.GetKeyCode() == WXK_RETURN ) |
| 1177 | { |
| 1178 | // GTK automatically selects an item if its in the list |
| 1179 | wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId()); |
| 1180 | eventEnter.SetString( GetValue() ); |
| 1181 | eventEnter.SetInt( GetSelection() ); |
| 1182 | eventEnter.SetEventObject( this ); |
| 1183 | |
| 1184 | if (!GetEventHandler()->ProcessEvent( eventEnter )) |
| 1185 | { |
| 1186 | // This will invoke the dialog default action, such |
| 1187 | // as the clicking the default button. |
| 1188 | |
| 1189 | wxWindow *top_frame = m_parent; |
| 1190 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
| 1191 | top_frame = top_frame->GetParent(); |
| 1192 | |
| 1193 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) |
| 1194 | { |
| 1195 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
| 1196 | |
| 1197 | if (window->default_widget) |
| 1198 | gtk_widget_activate (window->default_widget); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | // Catch GTK event so that GTK doesn't open the drop |
| 1203 | // down list upon RETURN. |
| 1204 | return; |
| 1205 | } |
| 1206 | |
| 1207 | event.Skip(); |
| 1208 | } |
| 1209 | |
| 1210 | void wxComboBox::DisableEvents() |
| 1211 | { |
| 1212 | #ifdef __WXGTK24__ |
| 1213 | if (!gtk_check_version(2,4,0)) |
| 1214 | { |
| 1215 | g_signal_handlers_disconnect_by_func (GTK_BIN(m_widget)->child, |
| 1216 | (gpointer)gtkcombobox_text_changed_callback, this); |
| 1217 | |
| 1218 | g_signal_handlers_disconnect_by_func (m_widget, |
| 1219 | (gpointer)gtkcombobox_changed_callback, this); |
| 1220 | } |
| 1221 | else |
| 1222 | #endif |
| 1223 | { |
| 1224 | g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget)->list, |
| 1225 | (gpointer) gtkcombo_combo_select_child_callback, this); |
| 1226 | |
| 1227 | g_signal_handlers_disconnect_by_func (GTK_COMBO(m_widget)->entry, |
| 1228 | (gpointer) gtkcombo_text_changed_callback, this); |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | void wxComboBox::EnableEvents() |
| 1233 | { |
| 1234 | #ifdef __WXGTK24__ |
| 1235 | if (!gtk_check_version(2,4,0)) |
| 1236 | { |
| 1237 | g_signal_connect_after (GTK_BIN(m_widget)->child, "changed", |
| 1238 | G_CALLBACK (gtkcombobox_text_changed_callback), this); |
| 1239 | |
| 1240 | g_signal_connect_after (m_widget, "changed", |
| 1241 | G_CALLBACK (gtkcombobox_changed_callback), this); |
| 1242 | } |
| 1243 | else |
| 1244 | #endif |
| 1245 | { |
| 1246 | g_signal_connect_after (GTK_COMBO(m_widget)->list, "select-child", |
| 1247 | G_CALLBACK (gtkcombo_combo_select_child_callback), |
| 1248 | this); |
| 1249 | g_signal_connect_after (GTK_COMBO(m_widget)->entry, "changed", |
| 1250 | G_CALLBACK (gtkcombo_text_changed_callback), |
| 1251 | this ); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | void wxComboBox::OnSize( wxSizeEvent &event ) |
| 1256 | { |
| 1257 | #ifdef __WXGTK24__ |
| 1258 | if (!gtk_check_version(2,4,0)) |
| 1259 | { |
| 1260 | // Do nothing |
| 1261 | } |
| 1262 | else |
| 1263 | #endif |
| 1264 | { |
| 1265 | // NB: In some situations (e.g. on non-first page of a wizard, if the |
| 1266 | // size used is default size), GtkCombo widget is resized correctly, |
| 1267 | // but it's look is not updated, it's rendered as if it was much wider. |
| 1268 | // No other widgets are affected, so it looks like a bug in GTK+. |
| 1269 | // Manually requesting resize calculation (as gtk_pizza_set_size does) |
| 1270 | // fixes it. |
| 1271 | if (GTK_WIDGET_VISIBLE(m_widget)) |
| 1272 | gtk_widget_queue_resize(m_widget); |
| 1273 | } |
| 1274 | |
| 1275 | event.Skip(); |
| 1276 | } |
| 1277 | |
| 1278 | void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style) |
| 1279 | { |
| 1280 | #ifdef __WXGTK24__ |
| 1281 | if (!gtk_check_version(2,4,0)) |
| 1282 | { |
| 1283 | // Do nothing |
| 1284 | } |
| 1285 | else |
| 1286 | #endif |
| 1287 | { |
| 1288 | // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle ); |
| 1289 | |
| 1290 | gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style ); |
| 1291 | gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style ); |
| 1292 | |
| 1293 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); |
| 1294 | GList *child = list->children; |
| 1295 | while (child) |
| 1296 | { |
| 1297 | gtk_widget_modify_style( GTK_WIDGET(child->data), style ); |
| 1298 | |
| 1299 | GtkBin *bin = GTK_BIN(child->data); |
| 1300 | gtk_widget_modify_style( bin->child, style ); |
| 1301 | |
| 1302 | child = child->next; |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | GtkWidget* wxComboBox::GetConnectWidget() |
| 1308 | { |
| 1309 | GtkEntry *entry = NULL; |
| 1310 | #ifdef __WXGTK24__ |
| 1311 | if (!gtk_check_version(2,4,0)) |
| 1312 | entry = GTK_ENTRY( GTK_BIN(m_widget)->child ); |
| 1313 | else |
| 1314 | #endif |
| 1315 | entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 1316 | |
| 1317 | return GTK_WIDGET( entry ); |
| 1318 | } |
| 1319 | |
| 1320 | GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const |
| 1321 | { |
| 1322 | #ifdef __WXGTK24__ |
| 1323 | if (!gtk_check_version(2,4,0)) |
| 1324 | { |
| 1325 | wxUnusedVar(windows); |
| 1326 | |
| 1327 | return GTK_ENTRY(GTK_BIN(m_widget)->child)->text_area; |
| 1328 | } |
| 1329 | else |
| 1330 | #endif // GTK+ 2.4 |
| 1331 | { |
| 1332 | windows.push_back(GTK_ENTRY(GTK_COMBO(m_widget)->entry)->text_area); |
| 1333 | windows.push_back(GTK_COMBO(m_widget)->button->window); |
| 1334 | |
| 1335 | // indicate that we return multiple windows in the windows array |
| 1336 | return NULL; |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | wxSize wxComboBox::DoGetBestSize() const |
| 1341 | { |
| 1342 | wxSize ret( wxControl::DoGetBestSize() ); |
| 1343 | |
| 1344 | // we know better our horizontal extent: it depends on the longest string |
| 1345 | // in the combobox |
| 1346 | if ( m_widget ) |
| 1347 | { |
| 1348 | int width; |
| 1349 | unsigned int count = GetCount(); |
| 1350 | for ( unsigned int n = 0; n < count; n++ ) |
| 1351 | { |
| 1352 | GetTextExtent(GetString(n), &width, NULL, NULL, NULL ); |
| 1353 | if ( width > ret.x ) |
| 1354 | ret.x = width; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | // empty combobox should have some reasonable default size too |
| 1359 | if ( ret.x < 100 ) |
| 1360 | ret.x = 100; |
| 1361 | |
| 1362 | CacheBestSize(ret); |
| 1363 | return ret; |
| 1364 | } |
| 1365 | |
| 1366 | // static |
| 1367 | wxVisualAttributes |
| 1368 | wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
| 1369 | { |
| 1370 | #ifdef __WXGTK24__ |
| 1371 | if (!gtk_check_version(2,4,0)) |
| 1372 | return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true); |
| 1373 | else |
| 1374 | #endif |
| 1375 | return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true); |
| 1376 | } |
| 1377 | |
| 1378 | // ---------------------------------------------------------------------------- |
| 1379 | // standard event handling |
| 1380 | // ---------------------------------------------------------------------------- |
| 1381 | |
| 1382 | void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event)) |
| 1383 | { |
| 1384 | Cut(); |
| 1385 | } |
| 1386 | |
| 1387 | void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event)) |
| 1388 | { |
| 1389 | Copy(); |
| 1390 | } |
| 1391 | |
| 1392 | void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event)) |
| 1393 | { |
| 1394 | Paste(); |
| 1395 | } |
| 1396 | |
| 1397 | void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event)) |
| 1398 | { |
| 1399 | Undo(); |
| 1400 | } |
| 1401 | |
| 1402 | void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event)) |
| 1403 | { |
| 1404 | Redo(); |
| 1405 | } |
| 1406 | |
| 1407 | void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event)) |
| 1408 | { |
| 1409 | long from, to; |
| 1410 | GetSelection(& from, & to); |
| 1411 | if (from != -1 && to != -1) |
| 1412 | Remove(from, to); |
| 1413 | } |
| 1414 | |
| 1415 | void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event)) |
| 1416 | { |
| 1417 | SetSelection(-1, -1); |
| 1418 | } |
| 1419 | |
| 1420 | void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event) |
| 1421 | { |
| 1422 | event.Enable( CanCut() ); |
| 1423 | } |
| 1424 | |
| 1425 | void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event) |
| 1426 | { |
| 1427 | event.Enable( CanCopy() ); |
| 1428 | } |
| 1429 | |
| 1430 | void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event) |
| 1431 | { |
| 1432 | event.Enable( CanPaste() ); |
| 1433 | } |
| 1434 | |
| 1435 | void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event) |
| 1436 | { |
| 1437 | event.Enable( CanUndo() ); |
| 1438 | } |
| 1439 | |
| 1440 | void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event) |
| 1441 | { |
| 1442 | event.Enable( CanRedo() ); |
| 1443 | } |
| 1444 | |
| 1445 | void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event) |
| 1446 | { |
| 1447 | event.Enable(HasSelection() && IsEditable()) ; |
| 1448 | } |
| 1449 | |
| 1450 | void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event) |
| 1451 | { |
| 1452 | event.Enable(GetLastPosition() > 0); |
| 1453 | } |
| 1454 | |
| 1455 | #endif |