| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/gtk/listbox.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 | #include "wx/defs.h" |
| 14 | |
| 15 | #if wxUSE_LISTBOX |
| 16 | |
| 17 | #include "wx/listbox.h" |
| 18 | #include "wx/dynarray.h" |
| 19 | #include "wx/arrstr.h" |
| 20 | #include "wx/utils.h" |
| 21 | #include "wx/intl.h" |
| 22 | #include "wx/checklst.h" |
| 23 | #include "wx/settings.h" |
| 24 | |
| 25 | // FIXME: Use GtkTreeView/GtkListModel instead? |
| 26 | #include <gtk/gtkversion.h> |
| 27 | #ifdef GTK_DISABLE_DEPRECATED |
| 28 | #undef GTK_DISABLE_DEPRECATED |
| 29 | #endif |
| 30 | |
| 31 | #include "wx/gtk/private.h" |
| 32 | |
| 33 | #if wxUSE_TOOLTIPS |
| 34 | #include "wx/tooltip.h" |
| 35 | #endif |
| 36 | |
| 37 | #include <gdk/gdkkeysyms.h> |
| 38 | |
| 39 | //----------------------------------------------------------------------------- |
| 40 | // idle system |
| 41 | //----------------------------------------------------------------------------- |
| 42 | |
| 43 | extern void wxapp_install_idle_handler(); |
| 44 | extern bool g_isIdle; |
| 45 | |
| 46 | //----------------------------------------------------------------------------- |
| 47 | // data |
| 48 | //----------------------------------------------------------------------------- |
| 49 | |
| 50 | extern bool g_blockEventsOnDrag; |
| 51 | extern bool g_blockEventsOnScroll; |
| 52 | extern wxCursor g_globalCursor; |
| 53 | extern wxWindowGTK *g_delayedFocus; |
| 54 | extern wxWindowGTK *g_focusWindow; |
| 55 | extern wxWindowGTK *g_focusWindowLast; |
| 56 | |
| 57 | static bool g_hasDoubleClicked = FALSE; |
| 58 | |
| 59 | //----------------------------------------------------------------------------- |
| 60 | // idle callback for SetFirstItem |
| 61 | //----------------------------------------------------------------------------- |
| 62 | |
| 63 | struct wxlistbox_idle_struct |
| 64 | { |
| 65 | wxListBox *m_listbox; |
| 66 | int m_item; |
| 67 | gint m_tag; |
| 68 | }; |
| 69 | |
| 70 | extern "C" { |
| 71 | static gint wxlistbox_idle_callback( gpointer gdata ) |
| 72 | { |
| 73 | wxlistbox_idle_struct* data = (wxlistbox_idle_struct*) gdata; |
| 74 | gdk_threads_enter(); |
| 75 | |
| 76 | g_source_remove( data->m_tag ); |
| 77 | |
| 78 | // check that the items haven't been deleted from the listbox since we had |
| 79 | // installed this callback |
| 80 | wxListBox *lbox = data->m_listbox; |
| 81 | if ( data->m_item < lbox->GetCount() ) |
| 82 | { |
| 83 | lbox->SetFirstItem( data->m_item ); |
| 84 | } |
| 85 | |
| 86 | delete data; |
| 87 | |
| 88 | gdk_threads_leave(); |
| 89 | |
| 90 | return TRUE; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | //----------------------------------------------------------------------------- |
| 95 | // "focus_in_event" |
| 96 | //----------------------------------------------------------------------------- |
| 97 | |
| 98 | extern "C" { |
| 99 | static gint gtk_listitem_focus_in_callback( GtkWidget *widget, |
| 100 | GdkEvent *WXUNUSED(event), |
| 101 | wxWindow *win ) |
| 102 | { |
| 103 | if (g_isIdle) |
| 104 | wxapp_install_idle_handler(); |
| 105 | |
| 106 | g_focusWindowLast = |
| 107 | g_focusWindow = win; |
| 108 | |
| 109 | // does the window itself think that it has the focus? |
| 110 | if ( !win->m_hasFocus ) |
| 111 | { |
| 112 | // not yet, notify it |
| 113 | win->m_hasFocus = TRUE; |
| 114 | |
| 115 | wxChildFocusEvent eventChildFocus(win); |
| 116 | (void)win->GetEventHandler()->ProcessEvent(eventChildFocus); |
| 117 | |
| 118 | wxFocusEvent eventFocus(wxEVT_SET_FOCUS, win->GetId()); |
| 119 | eventFocus.SetEventObject(win); |
| 120 | |
| 121 | (void)win->GetEventHandler()->ProcessEvent(eventFocus); |
| 122 | } |
| 123 | |
| 124 | return FALSE; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | //----------------------------------------------------------------------------- |
| 129 | // "focus_out_event" |
| 130 | //----------------------------------------------------------------------------- |
| 131 | |
| 132 | extern "C" { |
| 133 | static gint gtk_listitem_focus_out_callback( GtkWidget *widget, GdkEventFocus *gdk_event, wxWindowGTK *win ) |
| 134 | { |
| 135 | if (g_isIdle) |
| 136 | wxapp_install_idle_handler(); |
| 137 | |
| 138 | g_focusWindow = (wxWindowGTK *)NULL; |
| 139 | |
| 140 | // don't send the window a kill focus event if it thinks that it doesn't |
| 141 | // have focus already |
| 142 | if ( win->m_hasFocus ) |
| 143 | { |
| 144 | win->m_hasFocus = FALSE; |
| 145 | |
| 146 | wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() ); |
| 147 | event.SetEventObject( win ); |
| 148 | |
| 149 | // even if we did process the event in wx code, still let GTK itself |
| 150 | // process it too as otherwise bad things happen, especially in GTK2 |
| 151 | // where the text control simply aborts the program if it doesn't get |
| 152 | // the matching focus out event |
| 153 | (void)win->GetEventHandler()->ProcessEvent( event ); |
| 154 | } |
| 155 | |
| 156 | return FALSE; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | //----------------------------------------------------------------------------- |
| 161 | // "button_release_event" |
| 162 | //----------------------------------------------------------------------------- |
| 163 | |
| 164 | /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once |
| 165 | a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the |
| 166 | listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event. |
| 167 | this can lead to race conditions so that we emit the dclick event |
| 168 | after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */ |
| 169 | |
| 170 | extern "C" { |
| 171 | static gint |
| 172 | gtk_listbox_button_release_callback( GtkWidget * WXUNUSED(widget), |
| 173 | GdkEventButton * WXUNUSED(gdk_event), |
| 174 | wxListBox *listbox ) |
| 175 | { |
| 176 | if (g_isIdle) wxapp_install_idle_handler(); |
| 177 | |
| 178 | if (g_blockEventsOnDrag) return FALSE; |
| 179 | if (g_blockEventsOnScroll) return FALSE; |
| 180 | |
| 181 | if (!listbox->m_hasVMT) return FALSE; |
| 182 | |
| 183 | if (!g_hasDoubleClicked) return FALSE; |
| 184 | |
| 185 | wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() ); |
| 186 | event.SetEventObject( listbox ); |
| 187 | |
| 188 | wxArrayInt aSelections; |
| 189 | int n, count = listbox->GetSelections(aSelections); |
| 190 | if ( count > 0 ) |
| 191 | { |
| 192 | n = aSelections[0]; |
| 193 | if ( listbox->HasClientObjectData() ) |
| 194 | event.SetClientObject( listbox->GetClientObject(n) ); |
| 195 | else if ( listbox->HasClientUntypedData() ) |
| 196 | event.SetClientData( listbox->GetClientData(n) ); |
| 197 | event.SetString( listbox->GetString(n) ); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | n = -1; |
| 202 | } |
| 203 | |
| 204 | event.SetInt(n); |
| 205 | |
| 206 | listbox->GetEventHandler()->ProcessEvent( event ); |
| 207 | |
| 208 | return FALSE; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | //----------------------------------------------------------------------------- |
| 213 | // "button_press_event" |
| 214 | //----------------------------------------------------------------------------- |
| 215 | |
| 216 | extern "C" { |
| 217 | static gint |
| 218 | gtk_listbox_button_press_callback( GtkWidget *widget, |
| 219 | GdkEventButton *gdk_event, |
| 220 | wxListBox *listbox ) |
| 221 | { |
| 222 | if (g_isIdle) wxapp_install_idle_handler(); |
| 223 | |
| 224 | if (g_blockEventsOnDrag) return FALSE; |
| 225 | if (g_blockEventsOnScroll) return FALSE; |
| 226 | |
| 227 | if (!listbox->m_hasVMT) return FALSE; |
| 228 | |
| 229 | int sel = listbox->GtkGetIndex( widget ); |
| 230 | |
| 231 | #if wxUSE_CHECKLISTBOX |
| 232 | if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS)) |
| 233 | { |
| 234 | wxCheckListBox *clb = (wxCheckListBox *)listbox; |
| 235 | |
| 236 | clb->Check( sel, !clb->IsChecked(sel) ); |
| 237 | |
| 238 | wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() ); |
| 239 | event.SetEventObject( listbox ); |
| 240 | event.SetInt( sel ); |
| 241 | listbox->GetEventHandler()->ProcessEvent( event ); |
| 242 | } |
| 243 | #endif // wxUSE_CHECKLISTBOX |
| 244 | |
| 245 | if ((gdk_event->state == 0) && |
| 246 | (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) || |
| 247 | ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) ) |
| 248 | { |
| 249 | listbox->m_blockEvent = TRUE; |
| 250 | |
| 251 | int i; |
| 252 | for (i = 0; i < (int)listbox->GetCount(); i++) |
| 253 | if (i != sel) |
| 254 | gtk_list_unselect_item( GTK_LIST(listbox->m_list), i ); |
| 255 | |
| 256 | listbox->m_blockEvent = FALSE; |
| 257 | |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */ |
| 262 | g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS); |
| 263 | |
| 264 | return FALSE; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | //----------------------------------------------------------------------------- |
| 269 | // "key_press_event" |
| 270 | //----------------------------------------------------------------------------- |
| 271 | |
| 272 | extern "C" { |
| 273 | static gint |
| 274 | gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox ) |
| 275 | { |
| 276 | if (g_isIdle) |
| 277 | wxapp_install_idle_handler(); |
| 278 | |
| 279 | if (g_blockEventsOnDrag) |
| 280 | return FALSE; |
| 281 | |
| 282 | bool ret = FALSE; |
| 283 | |
| 284 | if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab)) |
| 285 | { |
| 286 | wxNavigationKeyEvent new_event; |
| 287 | /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */ |
| 288 | new_event.SetDirection( (gdk_event->keyval == GDK_Tab) ); |
| 289 | /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ |
| 290 | new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) ); |
| 291 | new_event.SetCurrentFocus( listbox ); |
| 292 | ret = listbox->GetEventHandler()->ProcessEvent( new_event ); |
| 293 | } |
| 294 | |
| 295 | if ((gdk_event->keyval == GDK_Return) && (!ret)) |
| 296 | { |
| 297 | // eat return in all modes |
| 298 | ret = TRUE; |
| 299 | } |
| 300 | |
| 301 | #if wxUSE_CHECKLISTBOX |
| 302 | if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret)) |
| 303 | { |
| 304 | int sel = listbox->GtkGetIndex( widget ); |
| 305 | |
| 306 | wxCheckListBox *clb = (wxCheckListBox *)listbox; |
| 307 | |
| 308 | clb->Check( sel, !clb->IsChecked(sel) ); |
| 309 | |
| 310 | wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() ); |
| 311 | new_event.SetEventObject( listbox ); |
| 312 | new_event.SetInt( sel ); |
| 313 | ret = listbox->GetEventHandler()->ProcessEvent( new_event ); |
| 314 | } |
| 315 | #endif // wxUSE_CHECKLISTBOX |
| 316 | |
| 317 | // Check or uncheck item with SPACE |
| 318 | if ((gdk_event->keyval == ' ') && (!ret) && |
| 319 | (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) || |
| 320 | ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) ) |
| 321 | { |
| 322 | int sel = listbox->GtkGetIndex( widget ); |
| 323 | |
| 324 | if (sel != -1) |
| 325 | { |
| 326 | ret = TRUE; |
| 327 | |
| 328 | if (listbox->IsSelected( sel )) |
| 329 | gtk_list_unselect_item( listbox->m_list, sel ); |
| 330 | else |
| 331 | gtk_list_select_item( listbox->m_list, sel ); |
| 332 | |
| 333 | wxCommandEvent new_event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() ); |
| 334 | new_event.SetEventObject( listbox ); |
| 335 | wxArrayInt aSelections; |
| 336 | int n, count = listbox->GetSelections(aSelections); |
| 337 | if ( count > 0 ) |
| 338 | { |
| 339 | n = aSelections[0]; |
| 340 | if ( listbox->HasClientObjectData() ) |
| 341 | new_event.SetClientObject( listbox->GetClientObject(n) ); |
| 342 | else if ( listbox->HasClientUntypedData() ) |
| 343 | new_event.SetClientData( listbox->GetClientData(n) ); |
| 344 | new_event.SetString( listbox->GetString(n) ); |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | n = -1; |
| 349 | } |
| 350 | new_event.SetInt(n); |
| 351 | listbox->GetEventHandler()->ProcessEvent( new_event ); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if (ret) |
| 356 | { |
| 357 | g_signal_stop_emission_by_name (widget, "key_press_event"); |
| 358 | return TRUE; |
| 359 | } |
| 360 | |
| 361 | return FALSE; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | //----------------------------------------------------------------------------- |
| 366 | // "select" and "deselect" |
| 367 | //----------------------------------------------------------------------------- |
| 368 | |
| 369 | static void gtk_listitem_select_cb( GtkWidget *widget, |
| 370 | wxListBox *listbox, |
| 371 | bool is_selection ) |
| 372 | { |
| 373 | if (g_isIdle) wxapp_install_idle_handler(); |
| 374 | |
| 375 | if (!listbox->m_hasVMT) return; |
| 376 | if (g_blockEventsOnDrag) return; |
| 377 | |
| 378 | if (listbox->m_blockEvent) return; |
| 379 | |
| 380 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() ); |
| 381 | event.SetEventObject( listbox ); |
| 382 | |
| 383 | // indicate whether this is a selection or a deselection |
| 384 | event.SetExtraLong( is_selection ); |
| 385 | |
| 386 | if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0) |
| 387 | { |
| 388 | int sel = listbox->GtkGetIndex( widget ); |
| 389 | |
| 390 | if (listbox->m_prevSelection != sel) |
| 391 | gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection ); |
| 392 | |
| 393 | listbox->m_prevSelection = sel; |
| 394 | } |
| 395 | |
| 396 | wxArrayInt aSelections; |
| 397 | int n, count = listbox->GetSelections(aSelections); |
| 398 | if ( count > 0 ) |
| 399 | { |
| 400 | n = aSelections[0]; |
| 401 | if ( listbox->HasClientObjectData() ) |
| 402 | event.SetClientObject( listbox->GetClientObject(n) ); |
| 403 | else if ( listbox->HasClientUntypedData() ) |
| 404 | event.SetClientData( listbox->GetClientData(n) ); |
| 405 | event.SetString( listbox->GetString(n) ); |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | n = -1; |
| 410 | } |
| 411 | |
| 412 | event.SetInt(n); |
| 413 | |
| 414 | // No longer required with new code in wxLB_SINGLE |
| 415 | // listbox->GetEventHandler()->AddPendingEvent( event ); |
| 416 | listbox->GetEventHandler()->ProcessEvent( event ); |
| 417 | } |
| 418 | |
| 419 | extern "C" { |
| 420 | static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox ) |
| 421 | { |
| 422 | gtk_listitem_select_cb( widget, listbox, TRUE ); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | extern "C" { |
| 427 | static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox ) |
| 428 | { |
| 429 | gtk_listitem_select_cb( widget, listbox, FALSE ); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | //----------------------------------------------------------------------------- |
| 434 | // wxListBox |
| 435 | //----------------------------------------------------------------------------- |
| 436 | |
| 437 | extern "C" { |
| 438 | static gint |
| 439 | gtk_listbox_realized_callback( GtkWidget *m_widget, wxListBox *win ) |
| 440 | { |
| 441 | if (g_isIdle) |
| 442 | wxapp_install_idle_handler(); |
| 443 | |
| 444 | GList *child = win->m_list->children; |
| 445 | for (child = win->m_list->children; child != NULL; child = child->next) |
| 446 | gtk_widget_show( GTK_WIDGET(child->data) ); |
| 447 | |
| 448 | return false; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | //----------------------------------------------------------------------------- |
| 453 | // wxListBox |
| 454 | //----------------------------------------------------------------------------- |
| 455 | |
| 456 | IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl) |
| 457 | |
| 458 | // ---------------------------------------------------------------------------- |
| 459 | // construction |
| 460 | // ---------------------------------------------------------------------------- |
| 461 | |
| 462 | wxListBox::wxListBox() |
| 463 | { |
| 464 | m_list = (GtkList *) NULL; |
| 465 | #if wxUSE_CHECKLISTBOX |
| 466 | m_hasCheckBoxes = FALSE; |
| 467 | #endif // wxUSE_CHECKLISTBOX |
| 468 | } |
| 469 | |
| 470 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, |
| 471 | const wxPoint &pos, const wxSize &size, |
| 472 | const wxArrayString& choices, |
| 473 | long style, const wxValidator& validator, |
| 474 | const wxString &name ) |
| 475 | { |
| 476 | wxCArrayString chs(choices); |
| 477 | |
| 478 | return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), |
| 479 | style, validator, name ); |
| 480 | } |
| 481 | |
| 482 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, |
| 483 | const wxPoint &pos, const wxSize &size, |
| 484 | int n, const wxString choices[], |
| 485 | long style, const wxValidator& validator, |
| 486 | const wxString &name ) |
| 487 | { |
| 488 | m_needParent = TRUE; |
| 489 | m_acceptsFocus = TRUE; |
| 490 | m_prevSelection = 0; // or -1 ?? |
| 491 | m_blockEvent = FALSE; |
| 492 | |
| 493 | if (!PreCreation( parent, pos, size ) || |
| 494 | !CreateBase( parent, id, pos, size, style, validator, name )) |
| 495 | { |
| 496 | wxFAIL_MSG( wxT("wxListBox creation failed") ); |
| 497 | return FALSE; |
| 498 | } |
| 499 | |
| 500 | m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL ); |
| 501 | if (style & wxLB_ALWAYS_SB) |
| 502 | { |
| 503 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), |
| 504 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS ); |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), |
| 509 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); |
| 510 | } |
| 511 | |
| 512 | m_list = GTK_LIST( gtk_list_new() ); |
| 513 | |
| 514 | GtkSelectionMode mode; |
| 515 | if (style & wxLB_MULTIPLE) |
| 516 | { |
| 517 | mode = GTK_SELECTION_MULTIPLE; |
| 518 | } |
| 519 | else if (style & wxLB_EXTENDED) |
| 520 | { |
| 521 | mode = GTK_SELECTION_EXTENDED; |
| 522 | } |
| 523 | else |
| 524 | { |
| 525 | // if style was 0 set single mode |
| 526 | m_windowStyle |= wxLB_SINGLE; |
| 527 | mode = GTK_SELECTION_SINGLE; |
| 528 | } |
| 529 | |
| 530 | gtk_list_set_selection_mode( GTK_LIST(m_list), mode ); |
| 531 | |
| 532 | gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) ); |
| 533 | |
| 534 | /* make list scroll when moving the focus down using cursor keys */ |
| 535 | gtk_container_set_focus_vadjustment( |
| 536 | GTK_CONTAINER(m_list), |
| 537 | gtk_scrolled_window_get_vadjustment( |
| 538 | GTK_SCROLLED_WINDOW(m_widget))); |
| 539 | |
| 540 | gtk_widget_show( GTK_WIDGET(m_list) ); |
| 541 | |
| 542 | g_signal_connect (m_list, "realize", |
| 543 | G_CALLBACK (gtk_listbox_realized_callback), this); |
| 544 | |
| 545 | if ( style & wxLB_SORT ) |
| 546 | { |
| 547 | // this will change DoAppend() behaviour |
| 548 | m_strings = new wxSortedArrayString; |
| 549 | } |
| 550 | else |
| 551 | { |
| 552 | m_strings = (wxSortedArrayString *)NULL; |
| 553 | } |
| 554 | |
| 555 | for (int i = 0; i < n; i++) |
| 556 | { |
| 557 | // add one by one |
| 558 | DoAppend(choices[i]); |
| 559 | } |
| 560 | |
| 561 | m_parent->DoAddChild( this ); |
| 562 | |
| 563 | PostCreation(size); |
| 564 | SetBestSize(size); // need this too because this is a wxControlWithItems |
| 565 | |
| 566 | return TRUE; |
| 567 | } |
| 568 | |
| 569 | wxListBox::~wxListBox() |
| 570 | { |
| 571 | m_hasVMT = FALSE; |
| 572 | |
| 573 | Clear(); |
| 574 | |
| 575 | if (m_strings) |
| 576 | delete m_strings; |
| 577 | } |
| 578 | |
| 579 | // ---------------------------------------------------------------------------- |
| 580 | // adding items |
| 581 | // ---------------------------------------------------------------------------- |
| 582 | |
| 583 | void wxListBox::DoInsertItems(const wxArrayString& items, int pos) |
| 584 | { |
| 585 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
| 586 | |
| 587 | // VZ: notice that InsertItems knows nothing about sorting, so calling it |
| 588 | // from outside (and not from our own Append) is likely to break |
| 589 | // everything |
| 590 | |
| 591 | // code elsewhere supposes we have as many items in m_clientList as items |
| 592 | // in the listbox |
| 593 | wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(), |
| 594 | wxT("bug in client data management") ); |
| 595 | |
| 596 | InvalidateBestSize(); |
| 597 | |
| 598 | GList *children = m_list->children; |
| 599 | int length = g_list_length(children); |
| 600 | |
| 601 | wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") ); |
| 602 | |
| 603 | size_t nItems = items.GetCount(); |
| 604 | int index; |
| 605 | |
| 606 | if (m_strings) |
| 607 | { |
| 608 | for (size_t n = 0; n < nItems; n++) |
| 609 | { |
| 610 | index = m_strings->Add( items[n] ); |
| 611 | |
| 612 | if (index != GetCount()) |
| 613 | { |
| 614 | GtkAddItem( items[n], index ); |
| 615 | wxList::compatibility_iterator node = m_clientList.Item( index ); |
| 616 | m_clientList.Insert( node, (wxObject*) NULL ); |
| 617 | } |
| 618 | else |
| 619 | { |
| 620 | GtkAddItem( items[n] ); |
| 621 | m_clientList.Append( (wxObject*) NULL ); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | else |
| 626 | { |
| 627 | if (pos == length) |
| 628 | { |
| 629 | for ( size_t n = 0; n < nItems; n++ ) |
| 630 | { |
| 631 | GtkAddItem( items[n] ); |
| 632 | |
| 633 | m_clientList.Append((wxObject *)NULL); |
| 634 | } |
| 635 | } |
| 636 | else |
| 637 | { |
| 638 | wxList::compatibility_iterator node = m_clientList.Item( pos ); |
| 639 | for ( size_t n = 0; n < nItems; n++ ) |
| 640 | { |
| 641 | GtkAddItem( items[n], pos+n ); |
| 642 | |
| 643 | m_clientList.Insert( node, (wxObject *)NULL ); |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(), |
| 649 | wxT("bug in client data management") ); |
| 650 | } |
| 651 | |
| 652 | int wxListBox::DoAppend( const wxString& item ) |
| 653 | { |
| 654 | InvalidateBestSize(); |
| 655 | |
| 656 | if (m_strings) |
| 657 | { |
| 658 | // need to determine the index |
| 659 | int index = m_strings->Add( item ); |
| 660 | |
| 661 | // only if not at the end anyway |
| 662 | if (index != GetCount()) |
| 663 | { |
| 664 | GtkAddItem( item, index ); |
| 665 | |
| 666 | wxList::compatibility_iterator node = m_clientList.Item( index ); |
| 667 | m_clientList.Insert( node, (wxObject *)NULL ); |
| 668 | |
| 669 | return index; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | GtkAddItem(item); |
| 674 | |
| 675 | m_clientList.Append((wxObject *)NULL); |
| 676 | |
| 677 | return GetCount() - 1; |
| 678 | } |
| 679 | |
| 680 | void wxListBox::GtkAddItem( const wxString &item, int pos ) |
| 681 | { |
| 682 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
| 683 | |
| 684 | GtkWidget *list_item; |
| 685 | |
| 686 | wxString label(item); |
| 687 | #if wxUSE_CHECKLISTBOX |
| 688 | if (m_hasCheckBoxes) |
| 689 | { |
| 690 | label.Prepend(wxCHECKLBOX_STRING); |
| 691 | } |
| 692 | #endif // wxUSE_CHECKLISTBOX |
| 693 | |
| 694 | list_item = gtk_list_item_new_with_label( wxGTK_CONV( label ) ); |
| 695 | |
| 696 | GList *gitem_list = g_list_alloc (); |
| 697 | gitem_list->data = list_item; |
| 698 | |
| 699 | if (pos == -1) |
| 700 | gtk_list_append_items( GTK_LIST (m_list), gitem_list ); |
| 701 | else |
| 702 | gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos ); |
| 703 | |
| 704 | g_signal_connect_after (list_item, "select", |
| 705 | G_CALLBACK (gtk_listitem_select_callback), |
| 706 | this); |
| 707 | |
| 708 | if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED)) |
| 709 | g_signal_connect_after (list_item, "deselect", |
| 710 | G_CALLBACK (gtk_listitem_deselect_callback), |
| 711 | this); |
| 712 | |
| 713 | g_signal_connect (list_item, "button_press_event", |
| 714 | G_CALLBACK (gtk_listbox_button_press_callback), |
| 715 | this); |
| 716 | g_signal_connect_after (list_item, "button_release_event", |
| 717 | G_CALLBACK (gtk_listbox_button_release_callback), |
| 718 | this); |
| 719 | g_signal_connect (list_item, "key_press_event", |
| 720 | G_CALLBACK (gtk_listbox_key_press_callback), |
| 721 | this); |
| 722 | g_signal_connect (list_item, "focus_in_event", |
| 723 | G_CALLBACK (gtk_listitem_focus_in_callback), |
| 724 | this); |
| 725 | g_signal_connect (list_item, "focus_out_event", |
| 726 | G_CALLBACK (gtk_listitem_focus_out_callback), |
| 727 | this); |
| 728 | |
| 729 | ConnectWidget( list_item ); |
| 730 | |
| 731 | if (GTK_WIDGET_REALIZED(m_widget)) |
| 732 | { |
| 733 | gtk_widget_show( list_item ); |
| 734 | |
| 735 | gtk_widget_realize( list_item ); |
| 736 | gtk_widget_realize( GTK_BIN(list_item)->child ); |
| 737 | |
| 738 | #if wxUSE_TOOLTIPS |
| 739 | if (m_tooltip) m_tooltip->Apply( this ); |
| 740 | #endif |
| 741 | } |
| 742 | |
| 743 | // Apply current widget style to the new list_item |
| 744 | GtkRcStyle *style = CreateWidgetStyle(); |
| 745 | if (style) |
| 746 | { |
| 747 | gtk_widget_modify_style( GTK_WIDGET( list_item ), style ); |
| 748 | GtkBin *bin = GTK_BIN( list_item ); |
| 749 | gtk_widget_modify_style( GTK_WIDGET( bin->child ), style ); |
| 750 | gtk_rc_style_unref( style ); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | void wxListBox::DoSetItems( const wxArrayString& items, |
| 755 | void **clientData) |
| 756 | { |
| 757 | Clear(); |
| 758 | |
| 759 | DoInsertItems(items, 0); |
| 760 | |
| 761 | if ( clientData ) |
| 762 | { |
| 763 | size_t count = items.GetCount(); |
| 764 | for ( size_t n = 0; n < count; n++ ) |
| 765 | { |
| 766 | SetClientData(n, clientData[n]); |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | // ---------------------------------------------------------------------------- |
| 772 | // deleting items |
| 773 | // ---------------------------------------------------------------------------- |
| 774 | |
| 775 | void wxListBox::Clear() |
| 776 | { |
| 777 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
| 778 | |
| 779 | gtk_list_clear_items( m_list, 0, GetCount() ); |
| 780 | |
| 781 | if ( GTK_LIST(m_list)->last_focus_child != NULL ) |
| 782 | { |
| 783 | // This should be NULL, I think. |
| 784 | GTK_LIST(m_list)->last_focus_child = NULL; |
| 785 | } |
| 786 | |
| 787 | if ( HasClientObjectData() ) |
| 788 | { |
| 789 | // destroy the data (due to Robert's idea of using wxList<wxObject> |
| 790 | // and not wxList<wxClientData> we can't just say |
| 791 | // m_clientList.DeleteContents(TRUE) - this would crash! |
| 792 | wxList::compatibility_iterator node = m_clientList.GetFirst(); |
| 793 | while ( node ) |
| 794 | { |
| 795 | delete (wxClientData *)node->GetData(); |
| 796 | node = node->GetNext(); |
| 797 | } |
| 798 | } |
| 799 | m_clientList.Clear(); |
| 800 | |
| 801 | if ( m_strings ) |
| 802 | m_strings->Clear(); |
| 803 | } |
| 804 | |
| 805 | void wxListBox::Delete( int n ) |
| 806 | { |
| 807 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
| 808 | |
| 809 | GList *child = g_list_nth( m_list->children, n ); |
| 810 | |
| 811 | wxCHECK_RET( child, wxT("wrong listbox index") ); |
| 812 | |
| 813 | GList *list = g_list_append( (GList*) NULL, child->data ); |
| 814 | gtk_list_remove_items( m_list, list ); |
| 815 | g_list_free( list ); |
| 816 | |
| 817 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
| 818 | if ( node ) |
| 819 | { |
| 820 | if ( m_clientDataItemsType == wxClientData_Object ) |
| 821 | { |
| 822 | wxClientData *cd = (wxClientData*)node->GetData(); |
| 823 | delete cd; |
| 824 | } |
| 825 | |
| 826 | m_clientList.Erase( node ); |
| 827 | } |
| 828 | |
| 829 | if ( m_strings ) |
| 830 | m_strings->RemoveAt(n); |
| 831 | } |
| 832 | |
| 833 | // ---------------------------------------------------------------------------- |
| 834 | // client data |
| 835 | // ---------------------------------------------------------------------------- |
| 836 | |
| 837 | void wxListBox::DoSetItemClientData( int n, void* clientData ) |
| 838 | { |
| 839 | wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); |
| 840 | |
| 841 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
| 842 | wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") ); |
| 843 | |
| 844 | node->SetData( (wxObject*) clientData ); |
| 845 | } |
| 846 | |
| 847 | void* wxListBox::DoGetItemClientData( int n ) const |
| 848 | { |
| 849 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") ); |
| 850 | |
| 851 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
| 852 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") ); |
| 853 | |
| 854 | return node->GetData(); |
| 855 | } |
| 856 | |
| 857 | void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData ) |
| 858 | { |
| 859 | wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); |
| 860 | |
| 861 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
| 862 | wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") ); |
| 863 | |
| 864 | // wxItemContainer already deletes data for us |
| 865 | |
| 866 | node->SetData( (wxObject*) clientData ); |
| 867 | } |
| 868 | |
| 869 | wxClientData* wxListBox::DoGetItemClientObject( int n ) const |
| 870 | { |
| 871 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") ); |
| 872 | |
| 873 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
| 874 | wxCHECK_MSG( node, (wxClientData *)NULL, |
| 875 | wxT("invalid index in wxListBox::DoGetItemClientObject") ); |
| 876 | |
| 877 | return (wxClientData*) node->GetData(); |
| 878 | } |
| 879 | |
| 880 | // ---------------------------------------------------------------------------- |
| 881 | // string list access |
| 882 | // ---------------------------------------------------------------------------- |
| 883 | |
| 884 | wxString wxListBox::GetRealLabel(GList *item) const |
| 885 | { |
| 886 | GtkBin *bin = GTK_BIN( item->data ); |
| 887 | GtkLabel *label = GTK_LABEL( bin->child ); |
| 888 | |
| 889 | wxString str; |
| 890 | |
| 891 | str = wxGTK_CONV_BACK( gtk_label_get_text( label ) ); |
| 892 | |
| 893 | #if wxUSE_CHECKLISTBOX |
| 894 |