| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: listbox.cpp |
| 3 | // Purpose: |
| 4 | // Author: Robert Roebling |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 1998 Robert Roebling |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | |
| 11 | #ifdef __GNUG__ |
| 12 | #pragma implementation "listbox.h" |
| 13 | #endif |
| 14 | |
| 15 | #include "wx/listbox.h" |
| 16 | |
| 17 | #if wxUSE_LISTBOX |
| 18 | |
| 19 | #include "wx/dynarray.h" |
| 20 | #include "wx/utils.h" |
| 21 | #include "wx/intl.h" |
| 22 | #include "wx/checklst.h" |
| 23 | #include "wx/settings.h" |
| 24 | #include "wx/gtk/private.h" |
| 25 | |
| 26 | #if wxUSE_TOOLTIPS |
| 27 | #include "wx/tooltip.h" |
| 28 | #endif |
| 29 | |
| 30 | #include <gdk/gdk.h> |
| 31 | #include <gtk/gtk.h> |
| 32 | #include <gdk/gdkkeysyms.h> |
| 33 | |
| 34 | //----------------------------------------------------------------------------- |
| 35 | // idle system |
| 36 | //----------------------------------------------------------------------------- |
| 37 | |
| 38 | extern void wxapp_install_idle_handler(); |
| 39 | extern bool g_isIdle; |
| 40 | |
| 41 | //----------------------------------------------------------------------------- |
| 42 | // data |
| 43 | //----------------------------------------------------------------------------- |
| 44 | |
| 45 | extern bool g_blockEventsOnDrag; |
| 46 | extern bool g_blockEventsOnScroll; |
| 47 | extern wxCursor g_globalCursor; |
| 48 | extern wxWindowGTK *g_delayedFocus; |
| 49 | |
| 50 | static bool g_hasDoubleClicked = FALSE; |
| 51 | |
| 52 | //----------------------------------------------------------------------------- |
| 53 | // idle callback for SetFirstItem |
| 54 | //----------------------------------------------------------------------------- |
| 55 | |
| 56 | struct wxlistbox_idle_struct |
| 57 | { |
| 58 | wxListBox *m_listbox; |
| 59 | int m_item; |
| 60 | gint m_tag; |
| 61 | }; |
| 62 | |
| 63 | extern "C" gint wxlistbox_idle_callback( gpointer gdata ) |
| 64 | { |
| 65 | wxlistbox_idle_struct* data = (wxlistbox_idle_struct*) gdata; |
| 66 | gdk_threads_enter(); |
| 67 | |
| 68 | gtk_idle_remove( data->m_tag ); |
| 69 | |
| 70 | // check that the items haven't been deleted from the listbox since we had |
| 71 | // installed this callback |
| 72 | wxListBox *lbox = data->m_listbox; |
| 73 | if ( data->m_item < lbox->GetCount() ) |
| 74 | { |
| 75 | lbox->SetFirstItem( data->m_item ); |
| 76 | } |
| 77 | |
| 78 | delete data; |
| 79 | |
| 80 | gdk_threads_leave(); |
| 81 | |
| 82 | return TRUE; |
| 83 | } |
| 84 | |
| 85 | //----------------------------------------------------------------------------- |
| 86 | // "button_release_event" |
| 87 | //----------------------------------------------------------------------------- |
| 88 | |
| 89 | /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once |
| 90 | a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the |
| 91 | listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event. |
| 92 | this can lead to race conditions so that we emit the dclick event |
| 93 | after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */ |
| 94 | |
| 95 | static gint |
| 96 | gtk_listbox_button_release_callback( GtkWidget * WXUNUSED(widget), |
| 97 | GdkEventButton * WXUNUSED(gdk_event), |
| 98 | wxListBox *listbox ) |
| 99 | { |
| 100 | if (g_isIdle) wxapp_install_idle_handler(); |
| 101 | |
| 102 | if (g_blockEventsOnDrag) return FALSE; |
| 103 | if (g_blockEventsOnScroll) return FALSE; |
| 104 | |
| 105 | if (!listbox->m_hasVMT) return FALSE; |
| 106 | |
| 107 | if (!g_hasDoubleClicked) return FALSE; |
| 108 | |
| 109 | wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() ); |
| 110 | event.SetEventObject( listbox ); |
| 111 | |
| 112 | wxArrayInt aSelections; |
| 113 | int n, count = listbox->GetSelections(aSelections); |
| 114 | if ( count > 0 ) |
| 115 | { |
| 116 | n = aSelections[0]; |
| 117 | if ( listbox->HasClientObjectData() ) |
| 118 | event.SetClientObject( listbox->GetClientObject(n) ); |
| 119 | else if ( listbox->HasClientUntypedData() ) |
| 120 | event.SetClientData( listbox->GetClientData(n) ); |
| 121 | event.SetString( listbox->GetString(n) ); |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | n = -1; |
| 126 | } |
| 127 | |
| 128 | event.m_commandInt = n; |
| 129 | |
| 130 | listbox->GetEventHandler()->ProcessEvent( event ); |
| 131 | |
| 132 | return FALSE; |
| 133 | } |
| 134 | |
| 135 | //----------------------------------------------------------------------------- |
| 136 | // "button_press_event" |
| 137 | //----------------------------------------------------------------------------- |
| 138 | |
| 139 | static gint |
| 140 | gtk_listbox_button_press_callback( GtkWidget *widget, |
| 141 | GdkEventButton *gdk_event, |
| 142 | wxListBox *listbox ) |
| 143 | { |
| 144 | if (g_isIdle) wxapp_install_idle_handler(); |
| 145 | |
| 146 | if (g_blockEventsOnDrag) return FALSE; |
| 147 | if (g_blockEventsOnScroll) return FALSE; |
| 148 | |
| 149 | if (!listbox->m_hasVMT) return FALSE; |
| 150 | |
| 151 | int sel = listbox->GtkGetIndex( widget ); |
| 152 | |
| 153 | #if wxUSE_CHECKLISTBOX |
| 154 | if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS)) |
| 155 | { |
| 156 | wxCheckListBox *clb = (wxCheckListBox *)listbox; |
| 157 | |
| 158 | clb->Check( sel, !clb->IsChecked(sel) ); |
| 159 | |
| 160 | wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() ); |
| 161 | event.SetEventObject( listbox ); |
| 162 | event.SetInt( sel ); |
| 163 | listbox->GetEventHandler()->ProcessEvent( event ); |
| 164 | } |
| 165 | #endif // wxUSE_CHECKLISTBOX |
| 166 | |
| 167 | /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */ |
| 168 | g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS); |
| 169 | |
| 170 | return FALSE; |
| 171 | } |
| 172 | |
| 173 | //----------------------------------------------------------------------------- |
| 174 | // "key_press_event" |
| 175 | //----------------------------------------------------------------------------- |
| 176 | |
| 177 | static gint |
| 178 | gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox ) |
| 179 | { |
| 180 | if (g_isIdle) |
| 181 | wxapp_install_idle_handler(); |
| 182 | |
| 183 | if (g_blockEventsOnDrag) |
| 184 | return FALSE; |
| 185 | |
| 186 | bool ret = FALSE; |
| 187 | |
| 188 | if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab)) |
| 189 | { |
| 190 | wxNavigationKeyEvent new_event; |
| 191 | /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */ |
| 192 | new_event.SetDirection( (gdk_event->keyval == GDK_Tab) ); |
| 193 | /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ |
| 194 | new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) ); |
| 195 | new_event.SetCurrentFocus( listbox ); |
| 196 | ret = listbox->GetEventHandler()->ProcessEvent( new_event ); |
| 197 | } |
| 198 | |
| 199 | if ((gdk_event->keyval == GDK_Return) && (!ret)) |
| 200 | { |
| 201 | // eat return in all modes |
| 202 | ret = TRUE; |
| 203 | } |
| 204 | |
| 205 | #if wxUSE_CHECKLISTBOX |
| 206 | if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret)) |
| 207 | { |
| 208 | int sel = listbox->GtkGetIndex( widget ); |
| 209 | |
| 210 | wxCheckListBox *clb = (wxCheckListBox *)listbox; |
| 211 | |
| 212 | clb->Check( sel, !clb->IsChecked(sel) ); |
| 213 | |
| 214 | wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() ); |
| 215 | new_event.SetEventObject( listbox ); |
| 216 | new_event.SetInt( sel ); |
| 217 | ret = listbox->GetEventHandler()->ProcessEvent( new_event ); |
| 218 | } |
| 219 | #endif // wxUSE_CHECKLISTBOX |
| 220 | |
| 221 | if (ret) |
| 222 | { |
| 223 | gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" ); |
| 224 | return TRUE; |
| 225 | } |
| 226 | |
| 227 | return FALSE; |
| 228 | } |
| 229 | |
| 230 | //----------------------------------------------------------------------------- |
| 231 | // "select" and "deselect" |
| 232 | //----------------------------------------------------------------------------- |
| 233 | |
| 234 | static void gtk_listitem_select_cb( GtkWidget *widget, wxListBox *listbox, bool is_selection ); |
| 235 | |
| 236 | static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox ) |
| 237 | { |
| 238 | gtk_listitem_select_cb( widget, listbox, TRUE ); |
| 239 | } |
| 240 | |
| 241 | static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox ) |
| 242 | { |
| 243 | gtk_listitem_select_cb( widget, listbox, FALSE ); |
| 244 | } |
| 245 | |
| 246 | static void gtk_listitem_select_cb( GtkWidget *widget, |
| 247 | wxListBox *listbox, |
| 248 | bool is_selection ) |
| 249 | { |
| 250 | if (g_isIdle) wxapp_install_idle_handler(); |
| 251 | |
| 252 | if (!listbox->m_hasVMT) return; |
| 253 | if (g_blockEventsOnDrag) return; |
| 254 | |
| 255 | if (listbox->m_blockEvent) return; |
| 256 | |
| 257 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() ); |
| 258 | event.SetEventObject( listbox ); |
| 259 | |
| 260 | // indicate whether this is a selection or a deselection |
| 261 | event.SetExtraLong( is_selection ); |
| 262 | |
| 263 | if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0) |
| 264 | { |
| 265 | int sel = listbox->GtkGetIndex( widget ); |
| 266 | |
| 267 | if (listbox->m_prevSelection != sel) |
| 268 | gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection ); |
| 269 | |
| 270 | listbox->m_prevSelection = sel; |
| 271 | } |
| 272 | |
| 273 | wxArrayInt aSelections; |
| 274 | int n, count = listbox->GetSelections(aSelections); |
| 275 | if ( count > 0 ) |
| 276 | { |
| 277 | n = aSelections[0]; |
| 278 | if ( listbox->HasClientObjectData() ) |
| 279 | event.SetClientObject( listbox->GetClientObject(n) ); |
| 280 | else if ( listbox->HasClientUntypedData() ) |
| 281 | event.SetClientData( listbox->GetClientData(n) ); |
| 282 | event.SetString( listbox->GetString(n) ); |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | n = -1; |
| 287 | } |
| 288 | |
| 289 | event.m_commandInt = n; |
| 290 | |
| 291 | // No longer required with new code in wxLB_SINGLE |
| 292 | // listbox->GetEventHandler()->AddPendingEvent( event ); |
| 293 | listbox->GetEventHandler()->ProcessEvent( event ); |
| 294 | } |
| 295 | |
| 296 | //----------------------------------------------------------------------------- |
| 297 | // wxListBox |
| 298 | //----------------------------------------------------------------------------- |
| 299 | |
| 300 | IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl) |
| 301 | |
| 302 | // ---------------------------------------------------------------------------- |
| 303 | // construction |
| 304 | // ---------------------------------------------------------------------------- |
| 305 | |
| 306 | wxListBox::wxListBox() |
| 307 | { |
| 308 | m_list = (GtkList *) NULL; |
| 309 | #if wxUSE_CHECKLISTBOX |
| 310 | m_hasCheckBoxes = FALSE; |
| 311 | #endif // wxUSE_CHECKLISTBOX |
| 312 | } |
| 313 | |
| 314 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, |
| 315 | const wxPoint &pos, const wxSize &size, |
| 316 | int n, const wxString choices[], |
| 317 | long style, const wxValidator& validator, |
| 318 | const wxString &name ) |
| 319 | { |
| 320 | m_needParent = TRUE; |
| 321 | m_acceptsFocus = TRUE; |
| 322 | m_prevSelection = 0; // or -1 ?? |
| 323 | m_blockEvent = FALSE; |
| 324 | |
| 325 | if (!PreCreation( parent, pos, size ) || |
| 326 | !CreateBase( parent, id, pos, size, style, validator, name )) |
| 327 | { |
| 328 | wxFAIL_MSG( wxT("wxListBox creation failed") ); |
| 329 | return FALSE; |
| 330 | } |
| 331 | |
| 332 | m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL ); |
| 333 | if (style & wxLB_ALWAYS_SB) |
| 334 | { |
| 335 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), |
| 336 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS ); |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), |
| 341 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); |
| 342 | } |
| 343 | |
| 344 | m_list = GTK_LIST( gtk_list_new() ); |
| 345 | |
| 346 | GtkSelectionMode mode; |
| 347 | if (style & wxLB_MULTIPLE) |
| 348 | { |
| 349 | mode = GTK_SELECTION_MULTIPLE; |
| 350 | } |
| 351 | else if (style & wxLB_EXTENDED) |
| 352 | { |
| 353 | mode = GTK_SELECTION_EXTENDED; |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | // if style was 0 set single mode |
| 358 | m_windowStyle |= wxLB_SINGLE; |
| 359 | mode = GTK_SELECTION_MULTIPLE; |
| 360 | } |
| 361 | |
| 362 | gtk_list_set_selection_mode( GTK_LIST(m_list), mode ); |
| 363 | |
| 364 | gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) ); |
| 365 | |
| 366 | /* make list scroll when moving the focus down using cursor keys */ |
| 367 | gtk_container_set_focus_vadjustment( |
| 368 | GTK_CONTAINER(m_list), |
| 369 | gtk_scrolled_window_get_vadjustment( |
| 370 | GTK_SCROLLED_WINDOW(m_widget))); |
| 371 | |
| 372 | gtk_widget_show( GTK_WIDGET(m_list) ); |
| 373 | |
| 374 | if ( style & wxLB_SORT ) |
| 375 | { |
| 376 | // this will change DoAppend() behaviour |
| 377 | m_strings = new wxSortedArrayString; |
| 378 | } |
| 379 | else |
| 380 | { |
| 381 | m_strings = (wxSortedArrayString *)NULL; |
| 382 | } |
| 383 | |
| 384 | for (int i = 0; i < n; i++) |
| 385 | { |
| 386 | // add one by one |
| 387 | DoAppend(choices[i]); |
| 388 | } |
| 389 | |
| 390 | // call it after appending the strings to the listbox, otherwise it doesn't |
| 391 | // work correctly |
| 392 | SetBestSize( size ); |
| 393 | |
| 394 | m_parent->DoAddChild( this ); |
| 395 | |
| 396 | PostCreation(); |
| 397 | |
| 398 | SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX ) ); |
| 399 | SetForegroundColour( parent->GetForegroundColour() ); |
| 400 | SetFont( parent->GetFont() ); |
| 401 | |
| 402 | Show( TRUE ); |
| 403 | |
| 404 | return TRUE; |
| 405 | } |
| 406 | |
| 407 | wxListBox::~wxListBox() |
| 408 | { |
| 409 | m_hasVMT = FALSE; |
| 410 | |
| 411 | Clear(); |
| 412 | |
| 413 | if (m_strings) |
| 414 | delete m_strings; |
| 415 | } |
| 416 | |
| 417 | // ---------------------------------------------------------------------------- |
| 418 | // adding items |
| 419 | // ---------------------------------------------------------------------------- |
| 420 | |
| 421 | void wxListBox::DoInsertItems(const wxArrayString& items, int pos) |
| 422 | { |
| 423 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
| 424 | |
| 425 | // VZ: notice that InsertItems knows nothing about sorting, so calling it |
| 426 | // from outside (and not from our own Append) is likely to break |
| 427 | // everything |
| 428 | |
| 429 | // code elsewhere supposes we have as many items in m_clientList as items |
| 430 | // in the listbox |
| 431 | wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(), |
| 432 | wxT("bug in client data management") ); |
| 433 | |
| 434 | GList *children = m_list->children; |
| 435 | int length = g_list_length(children); |
| 436 | |
| 437 | wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") ); |
| 438 | |
| 439 | size_t nItems = items.GetCount(); |
| 440 | int index; |
| 441 | |
| 442 | if (m_strings) |
| 443 | { |
| 444 | for (size_t n = 0; n < nItems; n++) |
| 445 | { |
| 446 | index = m_strings->Add( items[n] ); |
| 447 | |
| 448 | if (index != GetCount()) |
| 449 | { |
| 450 | GtkAddItem( items[n], index ); |
| 451 | wxNode *node = m_clientList.Item( index ); |
| 452 | m_clientList.Insert( node, (wxObject*) NULL ); |
| 453 | } |
| 454 | else |
| 455 | { |
| 456 | GtkAddItem( items[n] ); |
| 457 | m_clientList.Append( (wxObject*) NULL ); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | else |
| 462 | { |
| 463 | if (pos == length) |
| 464 | { |
| 465 | for ( size_t n = 0; n < nItems; n++ ) |
| 466 | { |
| 467 | GtkAddItem( items[n] ); |
| 468 | |
| 469 | m_clientList.Append((wxObject *)NULL); |
| 470 | } |
| 471 | } |
| 472 | else |
| 473 | { |
| 474 | wxNode *node = m_clientList.Item( pos ); |
| 475 | for ( size_t n = 0; n < nItems; n++ ) |
| 476 | { |
| 477 | GtkAddItem( items[n], pos+n ); |
| 478 | |
| 479 | m_clientList.Insert( node, (wxObject *)NULL ); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(), |
| 485 | wxT("bug in client data management") ); |
| 486 | } |
| 487 | |
| 488 | int wxListBox::DoAppend( const wxString& item ) |
| 489 | { |
| 490 | if (m_strings) |
| 491 | { |
| 492 | // need to determine the index |
| 493 | int index = m_strings->Add( item ); |
| 494 | |
| 495 | // only if not at the end anyway |
| 496 | if (index != GetCount()) |
| 497 | { |
| 498 | GtkAddItem( item, index ); |
| 499 | |
| 500 | wxNode *node = m_clientList.Item( index ); |
| 501 | m_clientList.Insert( node, (wxObject *)NULL ); |
| 502 | |
| 503 | return index; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | GtkAddItem(item); |
| 508 | |
| 509 | m_clientList.Append((wxObject *)NULL); |
| 510 | |
| 511 | return GetCount() - 1; |
| 512 | } |
| 513 | |
| 514 | void wxListBox::GtkAddItem( const wxString &item, int pos ) |
| 515 | { |
| 516 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
| 517 | |
| 518 | GtkWidget *list_item; |
| 519 | |
| 520 | wxString label(item); |
| 521 | #if wxUSE_CHECKLISTBOX |
| 522 | if (m_hasCheckBoxes) |
| 523 | { |
| 524 | label.Prepend(wxCHECKLBOX_STRING); |
| 525 | } |
| 526 | #endif // wxUSE_CHECKLISTBOX |
| 527 | |
| 528 | list_item = gtk_list_item_new_with_label( wxGTK_CONV( label ) ); |
| 529 | |
| 530 | GList *gitem_list = g_list_alloc (); |
| 531 | gitem_list->data = list_item; |
| 532 | |
| 533 | if (pos == -1) |
| 534 | gtk_list_append_items( GTK_LIST (m_list), gitem_list ); |
| 535 | else |
| 536 | gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos ); |
| 537 | |
| 538 | gtk_signal_connect( GTK_OBJECT(list_item), "select", |
| 539 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); |
| 540 | |
| 541 | if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED)) |
| 542 | gtk_signal_connect( GTK_OBJECT(list_item), "deselect", |
| 543 | GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this ); |
| 544 | |
| 545 | gtk_signal_connect( GTK_OBJECT(list_item), |
| 546 | "button_press_event", |
| 547 | (GtkSignalFunc)gtk_listbox_button_press_callback, |
| 548 | (gpointer) this ); |
| 549 | |
| 550 | gtk_signal_connect_after( GTK_OBJECT(list_item), |
| 551 | "button_release_event", |
| 552 | (GtkSignalFunc)gtk_listbox_button_release_callback, |
| 553 | (gpointer) this ); |
| 554 | |
| 555 | gtk_signal_connect( GTK_OBJECT(list_item), |
| 556 | "key_press_event", |
| 557 | (GtkSignalFunc)gtk_listbox_key_press_callback, |
| 558 | (gpointer)this ); |
| 559 | |
| 560 | ConnectWidget( list_item ); |
| 561 | |
| 562 | gtk_widget_show( list_item ); |
| 563 | |
| 564 | if (GTK_WIDGET_REALIZED(m_widget)) |
| 565 | { |
| 566 | gtk_widget_realize( list_item ); |
| 567 | gtk_widget_realize( GTK_BIN(list_item)->child ); |
| 568 | |
| 569 | // Apply current widget style to the new list_item |
| 570 | if (m_widgetStyle) |
| 571 | { |
| 572 | gtk_widget_set_style( GTK_WIDGET( list_item ), m_widgetStyle ); |
| 573 | GtkBin *bin = GTK_BIN( list_item ); |
| 574 | GtkWidget *label = GTK_WIDGET( bin->child ); |
| 575 | gtk_widget_set_style( label, m_widgetStyle ); |
| 576 | } |
| 577 | |
| 578 | #if wxUSE_TOOLTIPS |
| 579 | if (m_tooltip) m_tooltip->Apply( this ); |
| 580 | #endif |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | void wxListBox::DoSetItems( const wxArrayString& items, |
| 585 | void **clientData) |
| 586 | { |
| 587 | Clear(); |
| 588 | |
| 589 | DoInsertItems(items, 0); |
| 590 | |
| 591 | if ( clientData ) |
| 592 | { |
| 593 | size_t count = items.GetCount(); |
| 594 | for ( size_t n = 0; n < count; n++ ) |
| 595 | { |
| 596 | SetClientData(n, clientData[n]); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | // ---------------------------------------------------------------------------- |
| 602 | // deleting items |
| 603 | // ---------------------------------------------------------------------------- |
| 604 | |
| 605 | void wxListBox::Clear() |
| 606 | { |
| 607 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
| 608 | |
| 609 | gtk_list_clear_items( m_list, 0, GetCount() ); |
| 610 | |
| 611 | if ( GTK_LIST(m_list)->last_focus_child != NULL ) |
| 612 | { |
| 613 | // This should be NULL, I think. |
| 614 | GTK_LIST(m_list)->last_focus_child = NULL; |
| 615 | } |
| 616 | |
| 617 | if ( HasClientObjectData() ) |
| 618 | { |
| 619 | // destroy the data (due to Robert's idea of using wxList<wxObject> |
| 620 | // and not wxList<wxClientData> we can't just say |
| 621 | // m_clientList.DeleteContents(TRUE) - this would crash! |
| 622 | wxNode *node = m_clientList.GetFirst(); |
| 623 | while ( node ) |
| 624 | { |
| 625 | delete (wxClientData *)node->GetData(); |
| 626 | node = node->GetNext(); |
| 627 | } |
| 628 | } |
| 629 | m_clientList.Clear(); |
| 630 | |
| 631 | if ( m_strings ) |
| 632 | m_strings->Clear(); |
| 633 | } |
| 634 | |
| 635 | void wxListBox::Delete( int n ) |
| 636 | { |
| 637 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
| 638 | |
| 639 | GList *child = g_list_nth( m_list->children, n ); |
| 640 | |
| 641 | wxCHECK_RET( child, wxT("wrong listbox index") ); |
| 642 | |
| 643 | GList *list = g_list_append( (GList*) NULL, child->data ); |
| 644 | gtk_list_remove_items( m_list, list ); |
| 645 | g_list_free( list ); |
| 646 | |
| 647 | wxNode *node = m_clientList.Item( n ); |
| 648 | if ( node ) |
| 649 | { |
| 650 | if ( m_clientDataItemsType == wxClientData_Object ) |
| 651 | { |
| 652 | wxClientData *cd = (wxClientData*)node->GetData(); |
| 653 | delete cd; |
| 654 | } |
| 655 | |
| 656 | m_clientList.DeleteNode( node ); |
| 657 | } |
| 658 | |
| 659 | if ( m_strings ) |
| 660 | m_strings->Remove(n); |
| 661 | } |
| 662 | |
| 663 | // ---------------------------------------------------------------------------- |
| 664 | // client data |
| 665 | // ---------------------------------------------------------------------------- |
| 666 | |
| 667 | void wxListBox::DoSetItemClientData( int n, void* clientData ) |
| 668 | { |
| 669 | wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); |
| 670 | |
| 671 | wxNode *node = m_clientList.Item( n ); |
| 672 | wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") ); |
| 673 | |
| 674 | node->SetData( (wxObject*) clientData ); |
| 675 | } |
| 676 | |
| 677 | void* wxListBox::DoGetItemClientData( int n ) const |
| 678 | { |
| 679 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") ); |
| 680 | |
| 681 | wxNode *node = m_clientList.Item( n ); |
| 682 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") ); |
| 683 | |
| 684 | return node->GetData(); |
| 685 | } |
| 686 | |
| 687 | void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData ) |
| 688 | { |
| 689 | wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); |
| 690 | |
| 691 | wxNode *node = m_clientList.Item( n ); |
| 692 | wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") ); |
| 693 | |
| 694 | // wxItemContainer already deletes data for us |
| 695 | |
| 696 | node->SetData( (wxObject*) clientData ); |
| 697 | } |
| 698 | |
| 699 | wxClientData* wxListBox::DoGetItemClientObject( int n ) const |
| 700 | { |
| 701 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") ); |
| 702 | |
| 703 | wxNode *node = m_clientList.Item( n ); |
| 704 | wxCHECK_MSG( node, (wxClientData *)NULL, |
| 705 | wxT("invalid index in wxListBox::DoGetItemClientObject") ); |
| 706 | |
| 707 | return (wxClientData*) node->GetData(); |
| 708 | } |
| 709 | |
| 710 | // ---------------------------------------------------------------------------- |
| 711 | // string list access |
| 712 | // ---------------------------------------------------------------------------- |
| 713 | |
| 714 | wxString wxListBox::GetRealLabel(GList *item) const |
| 715 | { |
| 716 | GtkBin *bin = GTK_BIN( item->data ); |
| 717 | GtkLabel *label = GTK_LABEL( bin->child ); |
| 718 | |
| 719 | wxString str; |
| 720 | |
| 721 | #ifdef __WXGTK20__ |
| 722 | str = wxGTK_CONV_BACK( gtk_label_get_text( label ) ); |
| 723 | #else |
| 724 | str = wxString( label->label ); |
| 725 | #endif |
| 726 | |
| 727 | #if wxUSE_CHECKLISTBOX |
| 728 |