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