1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/gtk/listbox.cpp 
   4 // Author:      Robert Roebling 
   5 // Modified By: Ryan Norton (GtkTreeView implementation) 
   7 // Copyright:   (c) 1998 Robert Roebling 
   8 // Licence:     wxWindows licence 
   9 ///////////////////////////////////////////////////////////////////////////// 
  11 // For compilers that support precompilation, includes "wx.h". 
  12 #include "wx/wxprec.h" 
  16 #include "wx/listbox.h" 
  19     #include "wx/dynarray.h" 
  23     #include "wx/settings.h" 
  24     #include "wx/checklst.h" 
  25     #include "wx/arrstr.h" 
  28 #include "wx/gtk/private.h" 
  29 #include "wx/gtk/treeentry_gtk.h" 
  32     #include "wx/tooltip.h" 
  36 #include <gdk/gdkkeysyms.h> 
  38 //----------------------------------------------------------------------------- 
  40 //----------------------------------------------------------------------------- 
  42 extern bool           g_blockEventsOnDrag
; 
  43 extern bool           g_blockEventsOnScroll
; 
  47 //----------------------------------------------------------------------------- 
  48 // Macro to tell which row the strings are in (1 if native checklist, 0 if not) 
  49 //----------------------------------------------------------------------------- 
  51 #if wxUSE_CHECKLISTBOX 
  52 #   define WXLISTBOX_DATACOLUMN_ARG(x)  (x->m_hasCheckBoxes ? 1 : 0) 
  54 #   define WXLISTBOX_DATACOLUMN_ARG(x)  (0) 
  55 #endif // wxUSE_CHECKLISTBOX 
  57 #define WXLISTBOX_DATACOLUMN    WXLISTBOX_DATACOLUMN_ARG(this) 
  59 //----------------------------------------------------------------------------- 
  61 //----------------------------------------------------------------------------- 
  65 gtk_listbox_row_activated_callback(GtkTreeView        
* WXUNUSED(treeview
), 
  67                                    GtkTreeViewColumn  
* WXUNUSED(col
), 
  70     if (g_blockEventsOnDrag
) return; 
  71     if (g_blockEventsOnScroll
) return; 
  73     // This is triggered by either a double-click or a space press 
  75     int sel 
= gtk_tree_path_get_indices(path
)[0]; 
  77     wxCommandEvent 
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() ); 
  78     event
.SetEventObject( listbox 
); 
  80     if (listbox
->IsSelected(sel
)) 
  82         GtkTreeEntry
* entry 
= listbox
->GtkGetEntry(sel
); 
  87             event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
))); 
  89             if ( listbox
->HasClientObjectData() ) 
  90                 event
.SetClientObject( (wxClientData
*) gtk_tree_entry_get_userdata(entry
) ); 
  91             else if ( listbox
->HasClientUntypedData() ) 
  92                 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) ); 
  94             g_object_unref (entry
); 
  98             wxLogSysError(wxT("Internal error - could not get entry for double-click")); 
 107     listbox
->HandleWindowEvent( event 
); 
 111 //----------------------------------------------------------------------------- 
 113 //----------------------------------------------------------------------------- 
 117 gtk_listitem_changed_callback(GtkTreeSelection 
* WXUNUSED(selection
), 
 120     if (g_blockEventsOnDrag
) return; 
 122     wxCommandEvent 
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() ); 
 123     event
.SetEventObject( listbox 
); 
 125     if (listbox
->HasFlag(wxLB_MULTIPLE
) || listbox
->HasFlag(wxLB_EXTENDED
)) 
 127         wxArrayInt selections
; 
 128         listbox
->GetSelections( selections 
); 
 130         if (selections
.GetCount() == 0) 
 132             // indicate that this is a deselection 
 133             event
.SetExtraLong( 0 ); 
 136             listbox
->HandleWindowEvent( event 
); 
 142             // indicate that this is a selection 
 143             event
.SetExtraLong( 1 ); 
 144             event
.SetInt( selections
[0] ); 
 146             listbox
->HandleWindowEvent( event 
); 
 151         int index 
= listbox
->GetSelection(); 
 152         if (index 
== wxNOT_FOUND
) 
 154             // indicate that this is a deselection 
 155             event
.SetExtraLong( 0 ); 
 158             listbox
->HandleWindowEvent( event 
); 
 164             GtkTreeEntry
* entry 
= listbox
->GtkGetEntry( index 
); 
 166             // indicate that this is a selection 
 167             event
.SetExtraLong( 1 ); 
 169             event
.SetInt( index 
); 
 170             event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
))); 
 172             if ( listbox
->HasClientObjectData() ) 
 173                 event
.SetClientObject( 
 174                     (wxClientData
*) gtk_tree_entry_get_userdata(entry
) 
 176             else if ( listbox
->HasClientUntypedData() ) 
 177                 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) ); 
 179             listbox
->HandleWindowEvent( event 
); 
 181             g_object_unref (entry
); 
 187 //----------------------------------------------------------------------------- 
 189 //----------------------------------------------------------------------------- 
 193 gtk_listbox_key_press_callback( GtkWidget 
*WXUNUSED(widget
), 
 194                                 GdkEventKey 
*gdk_event
, 
 197     if ((gdk_event
->keyval 
== GDK_Return
) ||  
 198         (gdk_event
->keyval 
== GDK_ISO_Enter
) || 
 199         (gdk_event
->keyval 
== GDK_KP_Enter
)) 
 201         int index 
= listbox
->GetSelection(); 
 202         if (index 
!= wxNOT_FOUND
) 
 205             wxCommandEvent 
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() ); 
 206             event
.SetEventObject( listbox 
); 
 208             GtkTreeEntry
* entry 
= listbox
->GtkGetEntry( index 
); 
 210             // indicate that this is a selection 
 211             event
.SetExtraLong( 1 ); 
 213             event
.SetInt( index 
); 
 214             event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
))); 
 216             if ( listbox
->HasClientObjectData() ) 
 217                 event
.SetClientObject( 
 218                     (wxClientData
*) gtk_tree_entry_get_userdata(entry
) 
 220             else if ( listbox
->HasClientUntypedData() ) 
 221                 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) ); 
 223             /* bool ret = */ listbox
->HandleWindowEvent( event 
); 
 225             g_object_unref (entry
); 
 227 //          wxMac and wxMSW always invoke default action 
 230                 // DClick not handled -> invoke default action 
 231                 wxWindow 
*tlw 
= wxGetTopLevelParent( listbox 
); 
 234                     GtkWindow 
*gtk_window 
= GTK_WINDOW( tlw
->GetHandle() ); 
 236                         gtk_window_activate_default( gtk_window 
); 
 240             // Always intercept, otherwise we'd get another dclick 
 241             // event from row_activated 
 250 //----------------------------------------------------------------------------- 
 251 // GtkTreeEntry destruction (to destroy client data) 
 252 //----------------------------------------------------------------------------- 
 255 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
, 
 258     if (listbox
->HasClientObjectData()) 
 260         gpointer userdata 
= gtk_tree_entry_get_userdata(entry
); 
 262             delete (wxClientData 
*)userdata
; 
 267 //----------------------------------------------------------------------------- 
 268 // Sorting callback (standard CmpNoCase return value) 
 269 //----------------------------------------------------------------------------- 
 272 static gint 
gtk_listbox_sort_callback(GtkTreeModel 
* WXUNUSED(model
), 
 278     GtkTreeEntry
* entry2
; 
 280     gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
), 
 282                              WXLISTBOX_DATACOLUMN_ARG(listbox
), 
 284     gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
), 
 286                              WXLISTBOX_DATACOLUMN_ARG(listbox
), 
 288     wxCHECK_MSG(entry
, 0, wxT("Could not get entry")); 
 289     wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2")); 
 291     //We compare collate keys here instead of calling g_utf8_collate 
 292     //as it is rather slow (and even the docs reccommend this) 
 293     int ret 
= strcmp(gtk_tree_entry_get_collate_key(entry
), 
 294                      gtk_tree_entry_get_collate_key(entry2
)); 
 296     g_object_unref (entry
); 
 297     g_object_unref (entry2
); 
 303 //----------------------------------------------------------------------------- 
 304 // Searching callback (TRUE == not equal, FALSE == equal) 
 305 //----------------------------------------------------------------------------- 
 308 static gboolean 
gtk_listbox_searchequal_callback(GtkTreeModel 
* WXUNUSED(model
), 
 309                                                  gint 
WXUNUSED(column
), 
 316     gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
), 
 318                              WXLISTBOX_DATACOLUMN_ARG(listbox
), 
 320     wxCHECK_MSG(entry
, 0, wxT("Could not get entry")); 
 321     wxGtkString 
keycollatekey(g_utf8_collate_key(key
, -1)); 
 323     int ret 
= strcmp(keycollatekey
, 
 324                      gtk_tree_entry_get_collate_key(entry
)); 
 326     g_object_unref (entry
); 
 332 //----------------------------------------------------------------------------- 
 334 //----------------------------------------------------------------------------- 
 336 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControlWithItems
) 
 338 // ---------------------------------------------------------------------------- 
 340 // ---------------------------------------------------------------------------- 
 342 void wxListBox::Init() 
 344     m_treeview 
= (GtkTreeView
*) NULL
; 
 345 #if wxUSE_CHECKLISTBOX 
 346     m_hasCheckBoxes 
= false; 
 347 #endif // wxUSE_CHECKLISTBOX 
 350 bool wxListBox::Create( wxWindow 
*parent
, wxWindowID id
, 
 351                         const wxPoint 
&pos
, const wxSize 
&size
, 
 352                         const wxArrayString
& choices
, 
 353                         long style
, const wxValidator
& validator
, 
 354                         const wxString 
&name 
) 
 356     wxCArrayString 
chs(choices
); 
 358     return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(), 
 359                    style
, validator
, name 
); 
 362 bool wxListBox::Create( wxWindow 
*parent
, wxWindowID id
, 
 363                         const wxPoint 
&pos
, const wxSize 
&size
, 
 364                         int n
, const wxString choices
[], 
 365                         long style
, const wxValidator
& validator
, 
 366                         const wxString 
&name 
) 
 368     if (!PreCreation( parent
, pos
, size 
) || 
 369         !CreateBase( parent
, id
, pos
, size
, style
, validator
, name 
)) 
 371         wxFAIL_MSG( wxT("wxListBox creation failed") ); 
 375     m_widget 
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL 
); 
 376     if (style 
& wxLB_ALWAYS_SB
) 
 378       gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
), 
 379         GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS 
); 
 383       gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
), 
 384         GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC 
); 
 388     GtkScrolledWindowSetBorder(m_widget
, style
); 
 390     m_treeview 
= GTK_TREE_VIEW( gtk_tree_view_new( ) ); 
 392     //wxListBox doesn't have a header :) 
 393     //NB: If enabled SetFirstItem doesn't work correctly 
 394     gtk_tree_view_set_headers_visible(m_treeview
, FALSE
); 
 396 #if wxUSE_CHECKLISTBOX 
 398         ((wxCheckListBox
*)this)->DoCreateCheckList(); 
 399 #endif // wxUSE_CHECKLISTBOX 
 401     // Create the data column 
 402     gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "", 
 403                                                 gtk_cell_renderer_text_new(), 
 405                                                 WXLISTBOX_DATACOLUMN
, NULL
); 
 407     // Now create+set the model (GtkListStore) - first argument # of columns 
 408 #if wxUSE_CHECKLISTBOX 
 410         m_liststore 
= gtk_list_store_new(2, G_TYPE_BOOLEAN
, 
 411                                             GTK_TYPE_TREE_ENTRY
); 
 414         m_liststore 
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
); 
 416     gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
)); 
 418     g_object_unref (m_liststore
); //free on treeview destruction 
 420     // Disable the pop-up textctrl that enables searching - note that 
 421     // the docs specify that even if this disabled (which we are doing) 
 422     // the user can still have it through the start-interactive-search 
 423     // key binding...either way we want to provide a searchequal callback 
 424     // NB: If this is enabled a doubleclick event (activate) gets sent 
 425     //     on a successful search 
 426     gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
); 
 427     gtk_tree_view_set_search_equal_func(m_treeview
, 
 428        (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
, 
 432     gtk_tree_view_set_enable_search(m_treeview
, FALSE
); 
 434     GtkSelectionMode mode
; 
 435     if (style 
& wxLB_MULTIPLE
) 
 437         mode 
= GTK_SELECTION_MULTIPLE
; 
 439     else if (style 
& wxLB_EXTENDED
) 
 441         mode 
= GTK_SELECTION_EXTENDED
; 
 445         // if style was 0 set single mode 
 446         m_windowStyle 
|= wxLB_SINGLE
; 
 447         mode 
= GTK_SELECTION_SINGLE
; 
 450     GtkTreeSelection
* selection 
= gtk_tree_view_get_selection( m_treeview 
); 
 451     gtk_tree_selection_set_mode( selection
, mode 
); 
 453     // Handle sortable stuff 
 454     if(HasFlag(wxLB_SORT
)) 
 456         // Setup sorting in ascending (wx) order 
 457         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
), 
 458                                              WXLISTBOX_DATACOLUMN
, 
 461         // Set the sort callback 
 462         gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
), 
 463                                         WXLISTBOX_DATACOLUMN
, 
 464                    (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
, 
 466                                         NULL 
//"destroy notifier" 
 471     gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) ); 
 473     gtk_widget_show( GTK_WIDGET(m_treeview
) ); 
 474     m_focusWidget 
= GTK_WIDGET(m_treeview
); 
 476     Append(n
, choices
); // insert initial items 
 478     // generate dclick events 
 479     g_signal_connect_after(m_treeview
, "row-activated", 
 480                      G_CALLBACK(gtk_listbox_row_activated_callback
), this); 
 482     // for intercepting dclick generation by <ENTER> 
 483     g_signal_connect (m_treeview
, "key_press_event", 
 484                       G_CALLBACK (gtk_listbox_key_press_callback
), 
 486     m_parent
->DoAddChild( this ); 
 489     SetInitialSize(size
); // need this too because this is a wxControlWithItems 
 491     g_signal_connect_after (selection
, "changed", 
 492                             G_CALLBACK (gtk_listitem_changed_callback
), this); 
 497 wxListBox::~wxListBox() 
 504 void wxListBox::GtkDisableEvents() 
 506     GtkTreeSelection
* selection 
= gtk_tree_view_get_selection( m_treeview 
); 
 508     g_signal_handlers_block_by_func(selection
, 
 509                                 (gpointer
) gtk_listitem_changed_callback
, this); 
 512 void wxListBox::GtkEnableEvents() 
 514     GtkTreeSelection
* selection 
= gtk_tree_view_get_selection( m_treeview 
); 
 516     g_signal_handlers_unblock_by_func(selection
, 
 517                                 (gpointer
) gtk_listitem_changed_callback
, this); 
 519     GtkUpdateOldSelection(); 
 522 // ---------------------------------------------------------------------------- 
 524 // ---------------------------------------------------------------------------- 
 526 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
, 
 529                              wxClientDataType type
) 
 531     wxCHECK_MSG( m_treeview 
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") ); 
 533     InvalidateBestSize(); 
 535     GtkTreeIter
* pIter 
= NULL
; // append by default 
 537     if ( pos 
!= GetCount() ) 
 539         wxCHECK_MSG( GtkGetIteratorFor(pos
, &iter
), wxNOT_FOUND
, 
 540                      wxT("internal wxListBox error in insertion") ); 
 545     const unsigned int numItems 
= items
.GetCount(); 
 546     for ( unsigned int i 
= 0; i 
< numItems
; ++i 
) 
 548         GtkTreeEntry
* entry 
= gtk_tree_entry_new(); 
 549         gtk_tree_entry_set_label(entry
, wxGTK_CONV(items
[i
])); 
 550         gtk_tree_entry_set_destroy_func(entry
, 
 551                 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
, 
 555         gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
); 
 557         GtkSetItem(itercur
, entry
); 
 559         g_object_unref (entry
); 
 562             AssignNewItemClientData(GtkGetIndexFor(itercur
), clientData
, i
, type
); 
 565     return pos 
+ numItems 
- 1; 
 568 // ---------------------------------------------------------------------------- 
 570 // ---------------------------------------------------------------------------- 
 572 void wxListBox::DoClear() 
 574     wxCHECK_RET( m_treeview 
!= NULL
, wxT("invalid listbox") ); 
 576     InvalidateBestSize(); 
 578     gtk_list_store_clear( m_liststore 
); /* well, THAT was easy :) */ 
 581 void wxListBox::DoDeleteOneItem(unsigned int n
) 
 583     wxCHECK_RET( m_treeview 
!= NULL
, wxT("invalid listbox") ); 
 585     InvalidateBestSize(); 
 588     wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("wrong listbox index") ); 
 590     // this returns false if iter is invalid (e.g. deleting item at end) but 
 591     // since we don't use iter, we ignore the return value 
 592     gtk_list_store_remove(m_liststore
, &iter
); 
 595 // ---------------------------------------------------------------------------- 
 596 // helper functions for working with iterators 
 597 // ---------------------------------------------------------------------------- 
 599 bool wxListBox::GtkGetIteratorFor(unsigned pos
, GtkTreeIter 
*iter
) const 
 601     if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore
), 
 604         wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos
); 
 611 int wxListBox::GtkGetIndexFor(GtkTreeIter
& iter
) const 
 614         gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
); 
 616     gint
* pIntPath 
= gtk_tree_path_get_indices(path
); 
 618     wxCHECK_MSG( pIntPath
, wxNOT_FOUND
, _T("failed to get iterator path") ); 
 620     int idx 
= pIntPath
[0]; 
 622     gtk_tree_path_free( path 
); 
 627 // get GtkTreeEntry from position (note: you need to g_unref it if valid) 
 628 GtkTreeEntry 
*wxListBox::GtkGetEntry(unsigned n
) const 
 631     if ( !GtkGetIteratorFor(n
, &iter
) ) 
 635     GtkTreeEntry
* entry 
= NULL
; 
 636     gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
, 
 637                        WXLISTBOX_DATACOLUMN
, &entry
, -1); 
 642 void wxListBox::GtkSetItem(GtkTreeIter
& iter
, const GtkTreeEntry 
*entry
) 
 644 #if wxUSE_CHECKLISTBOX 
 645     if ( m_hasCheckBoxes 
) 
 647         gtk_list_store_set(m_liststore
, &iter
, 
 648                            0, FALSE
, // FALSE == not toggled 
 653 #endif // wxUSE_CHECKLISTBOX 
 655         gtk_list_store_set(m_liststore
, &iter
, 0, entry
, -1); 
 659 // ---------------------------------------------------------------------------- 
 661 // ---------------------------------------------------------------------------- 
 663 void* wxListBox::DoGetItemClientData(unsigned int n
) const 
 665     wxCHECK_MSG( IsValid(n
), NULL
, 
 666                  wxT("Invalid index passed to GetItemClientData") ); 
 668     GtkTreeEntry
* entry 
= GtkGetEntry(n
); 
 669     wxCHECK_MSG(entry
, NULL
, wxT("could not get entry")); 
 671     void* userdata 
= gtk_tree_entry_get_userdata( entry 
); 
 672     g_object_unref (entry
); 
 676 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
) 
 678     wxCHECK_RET( IsValid(n
), 
 679                  wxT("Invalid index passed to SetItemClientData") ); 
 681     GtkTreeEntry
* entry 
= GtkGetEntry(n
); 
 682     wxCHECK_RET(entry
, wxT("could not get entry")); 
 684     gtk_tree_entry_set_userdata( entry
, clientData 
); 
 685     g_object_unref (entry
); 
 688 // ---------------------------------------------------------------------------- 
 689 // string list access 
 690 // ---------------------------------------------------------------------------- 
 692 void wxListBox::SetString(unsigned int n
, const wxString
& label
) 
 694     wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetString") ); 
 695     wxCHECK_RET( m_treeview 
!= NULL
, wxT("invalid listbox") ); 
 697     GtkTreeEntry
* entry 
= GtkGetEntry(n
); 
 698     wxCHECK_RET( entry
, wxT("wrong listbox index") ); 
 700     // update the item itself 
 701     gtk_tree_entry_set_label(entry
, wxGTK_CONV(label
)); 
 703     // and update the model which will refresh the tree too 
 705     wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), _T("failed to get iterator") ); 
 707     // FIXME: this resets the checked status of a wxCheckListBox item 
 709     GtkSetItem(iter
, entry
); 
 712 wxString 
wxListBox::GetString(unsigned int n
) const 
 714     wxCHECK_MSG( m_treeview 
!= NULL
, wxEmptyString
, wxT("invalid listbox") ); 
 716     GtkTreeEntry
* entry 
= GtkGetEntry(n
); 
 717     wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") ); 
 719     wxString label 
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) ); 
 721     g_object_unref (entry
); 
 725 unsigned int wxListBox::GetCount() const 
 727     wxCHECK_MSG( m_treeview 
!= NULL
, 0, wxT("invalid listbox") ); 
 729     return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
); 
 732 int wxListBox::FindString( const wxString 
&item
, bool bCase 
) const 
 734     wxCHECK_MSG( m_treeview 
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") ); 
 736     //Sort of hackish - maybe there is a faster way 
 737     unsigned int nCount 
= wxListBox::GetCount(); 
 739     for(unsigned int i 
= 0; i 
< nCount
; ++i
) 
 741         if( item
.IsSameAs( wxListBox::GetString(i
), bCase 
) ) 
 746     // it's not an error if the string is not found -> no wxCHECK 
 750 // ---------------------------------------------------------------------------- 
 752 // ---------------------------------------------------------------------------- 
 754 int wxListBox::GetSelection() const 
 756     wxCHECK_MSG( m_treeview 
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox")); 
 757     wxCHECK_MSG( HasFlag(wxLB_SINGLE
), wxNOT_FOUND
, 
 758                     wxT("must be single selection listbox")); 
 761     GtkTreeSelection
* selection 
= gtk_tree_view_get_selection(m_treeview
); 
 763     // only works on single-sel 
 764     if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
)) 
 767     return GtkGetIndexFor(iter
); 
 770 int wxListBox::GetSelections( wxArrayInt
& aSelections 
) const 
 772     wxCHECK_MSG( m_treeview 
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") ); 
 778     GtkTreeSelection
* selection 
= gtk_tree_view_get_selection(m_treeview
); 
 780     if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
)) 
 781     { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead 
 784             if (gtk_tree_selection_iter_is_selected(selection
, &iter
)) 
 788         } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
)); 
 791     return aSelections
.GetCount(); 
 794 bool wxListBox::IsSelected( int n 
) const 
 796     wxCHECK_MSG( m_treeview 
!= NULL
, false, wxT("invalid listbox") ); 
 798     GtkTreeSelection
* selection 
= gtk_tree_view_get_selection(m_treeview
); 
 801     wxCHECK_MSG( GtkGetIteratorFor(n
, &iter
), false, wxT("Invalid index") ); 
 803     return gtk_tree_selection_iter_is_selected(selection
, &iter
); 
 806 void wxListBox::DoSetSelection( int n
, bool select 
) 
 808     wxCHECK_RET( m_treeview 
!= NULL
, wxT("invalid listbox") ); 
 812     GtkTreeSelection
* selection 
= gtk_tree_view_get_selection(m_treeview
); 
 814     // passing -1 to SetSelection() is documented to deselect all items 
 815     if ( n 
== wxNOT_FOUND 
) 
 817         gtk_tree_selection_unselect_all(selection
); 
 822     wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetSelection") ); 
 826     wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("Invalid index") ); 
 829         gtk_tree_selection_select_iter(selection
, &iter
); 
 831         gtk_tree_selection_unselect_iter(selection
, &iter
); 
 833     GtkTreePath
* path 
= gtk_tree_model_get_path( 
 834                         GTK_TREE_MODEL(m_liststore
), &iter
); 
 836     gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
, FALSE
, 0.0f
, 0.0f
); 
 838     gtk_tree_path_free(path
); 
 843 void wxListBox::GtkUpdateOldSelection() 
 845     if (HasFlag(wxLB_MULTIPLE
)) 
 846         GetSelections( m_oldSelection 
); 
 849 void wxListBox::DoScrollToCell(int n
, float alignY
, float alignX
) 
 851     wxCHECK_RET( m_treeview
, wxT("invalid listbox") ); 
 852     wxCHECK_RET( IsValid(n
), wxT("invalid index")); 
 854     //RN: I have no idea why this line is needed... 
 855     if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
)) 
 859     if ( !GtkGetIteratorFor(n
, &iter
) ) 
 862     GtkTreePath
* path 
= gtk_tree_model_get_path( 
 863                             GTK_TREE_MODEL(m_liststore
), &iter
); 
 865     // Scroll to the desired cell (0.0 == topleft alignment) 
 866     gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
, 
 867                                  TRUE
, alignY
, alignX
); 
 869     gtk_tree_path_free(path
); 
 872 void wxListBox::DoSetFirstItem(int n
) 
 874     DoScrollToCell(n
, 0, 0); 
 877 void wxListBox::EnsureVisible(int n
) 
 879     DoScrollToCell(n
, 0.5, 0); 
 882 // ---------------------------------------------------------------------------- 
 884 // ---------------------------------------------------------------------------- 
 886 int wxListBox::DoListHitTest(const wxPoint
& point
) const 
 888     // gtk_tree_view_get_path_at_pos() also gets items that are not visible and 
 889     // we only want visible items we need to check for it manually here 
 890     if ( !GetClientRect().Contains(point
) ) 
 893     // need to translate from master window since it is in client coords 
 895     gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
), 
 896                             &binx
, &biny
, NULL
, NULL
, NULL
); 
 899     if ( !gtk_tree_view_get_path_at_pos
 
 905             NULL
,   // [out] column (always 0 here) 
 906             NULL
,   // [out] x-coord relative to the cell (not interested) 
 907             NULL    
// [out] y-coord relative to the cell 
 913     int index 
= gtk_tree_path_get_indices(path
)[0]; 
 914     gtk_tree_path_free(path
); 
 919 // ---------------------------------------------------------------------------- 
 921 // ---------------------------------------------------------------------------- 
 924 void wxListBox::ApplyToolTip( GtkTooltips 
*tips
, const gchar 
*tip 
) 
 926     // RN: Is this needed anymore? 
 927     gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview 
), tip
, (gchar
*) NULL 
); 
 929 #endif // wxUSE_TOOLTIPS 
 931 GtkWidget 
*wxListBox::GetConnectWidget() 
 933     // the correct widget for listbox events (such as mouse clicks for example) 
 934     // is m_treeview, not the parent scrolled window 
 935     return GTK_WIDGET(m_treeview
); 
 938 GdkWindow 
*wxListBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const 
 940     return gtk_tree_view_get_bin_window(m_treeview
); 
 943 void wxListBox::DoApplyWidgetStyle(GtkRcStyle 
*style
) 
 945     if (m_hasBgCol 
&& m_backgroundColour
.Ok()) 
 947         GdkWindow 
*window 
= gtk_tree_view_get_bin_window(m_treeview
); 
 950             m_backgroundColour
.CalcPixel( gdk_drawable_get_colormap( window 
) ); 
 951             gdk_window_set_background( window
, m_backgroundColour
.GetColor() ); 
 952             gdk_window_clear( window 
); 
 956     gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style 
); 
 959 wxSize 
wxListBox::DoGetBestSize() const 
 961     wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view")); 
 963     // Start with a minimum size that's not too small 
 965     GetTextExtent( wxT("X"), &cx
, &cy
); 
 969     // Find the widest string. 
 970     const unsigned int count 
= GetCount(); 
 974         for ( unsigned int i 
= 0; i 
< count
; i
++ ) 
 976             GetTextExtent(GetString(i
), &wLine
, NULL
); 
 977             if ( wLine 
> lbWidth 
) 
 984     // And just a bit more for the checkbox if present and then some 
 985     // (these are rough guesses) 
 986 #if wxUSE_CHECKLISTBOX 
 987     if ( m_hasCheckBoxes 
) 
 990         cy 
= cy 
> 25 ? cy 
: 25; // rough height of checkbox 
 994     // Add room for the scrollbar 
 995     lbWidth 
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
); 
 997     // Don't make the listbox too tall but don't make it too small neither 
 998     lbHeight 
= (cy
+4) * wxMin(wxMax(count
, 3), 10); 
1000     wxSize 
best(lbWidth
, lbHeight
); 
1001     CacheBestSize(best
); 
1007 wxListBox::GetClassDefaultAttributes(wxWindowVariant 
WXUNUSED(variant
)) 
1009     return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true); 
1012 #endif // wxUSE_LISTBOX