]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/listbox.cpp
add wxDataViewIconText
[wxWidgets.git] / src / gtk / listbox.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/gtk/listbox.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
4a46cbe8 5// Modified By: Ryan Norton (GtkTreeView implementation)
f96aa4d9
RR
6// Id: $Id$
7// Copyright: (c) 1998 Robert Roebling
65571936 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
14f355c2
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
dcf924a3
RR
14#if wxUSE_LISTBOX
15
88a7a4e1
WS
16#include "wx/listbox.h"
17
ad9835c9
WS
18#ifndef WX_PRECOMP
19 #include "wx/dynarray.h"
88a7a4e1 20 #include "wx/intl.h"
e4db172a 21 #include "wx/log.h"
de6185e2 22 #include "wx/utils.h"
9eddec69 23 #include "wx/settings.h"
e6e83933 24 #include "wx/checklst.h"
aaa6d89a 25 #include "wx/arrstr.h"
ad9835c9
WS
26#endif
27
fab591c5 28#include "wx/gtk/private.h"
4a46cbe8 29#include "wx/gtk/treeentry_gtk.h"
291a8f20
RR
30
31#if wxUSE_TOOLTIPS
ad9835c9 32 #include "wx/tooltip.h"
291a8f20 33#endif
c801d85f 34
4a46cbe8 35#include <gtk/gtk.h>
16c1f79c 36#include <gdk/gdkkeysyms.h>
83624f79 37
66bd6b93
RR
38//-----------------------------------------------------------------------------
39// data
40//-----------------------------------------------------------------------------
41
d7fa7eaa
RR
42extern bool g_blockEventsOnDrag;
43extern bool g_blockEventsOnScroll;
caaa4cfd 44
7a30ee8f 45
4a82abc8 46
c9433ea9 47//-----------------------------------------------------------------------------
4a46cbe8 48// Macro to tell which row the strings are in (1 if native checklist, 0 if not)
c9433ea9
RR
49//-----------------------------------------------------------------------------
50
470402b9 51#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
52# define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
53#else
54# define WXLISTBOX_DATACOLUMN_ARG(x) (0)
470402b9 55#endif // wxUSE_CHECKLISTBOX
17a1ebd1 56
4a46cbe8 57#define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
c9433ea9
RR
58
59//-----------------------------------------------------------------------------
4a46cbe8 60// "row-activated"
c9433ea9
RR
61//-----------------------------------------------------------------------------
62
865bb325 63extern "C" {
4a46cbe8
RR
64static void
65gtk_listbox_row_activated_callback(GtkTreeView *treeview,
66 GtkTreePath *path,
67 GtkTreeViewColumn *col,
68 wxListBox *listbox)
c9433ea9 69{
4a46cbe8
RR
70 if (g_blockEventsOnDrag) return;
71 if (g_blockEventsOnScroll) return;
c9433ea9 72
470402b9 73 // This is triggered by either a double-click or a space press
c9433ea9 74
4a46cbe8 75 int sel = gtk_tree_path_get_indices(path)[0];
c9433ea9 76
470402b9
RR
77 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
78 event.SetEventObject( listbox );
acfd422a 79
470402b9 80 if (listbox->IsSelected(sel))
4a46cbe8 81 {
470402b9 82 GtkTreeEntry* entry = listbox->GtkGetEntry(sel);
f4322df6 83
470402b9 84 if (entry)
4a46cbe8 85 {
470402b9
RR
86 event.SetInt(sel);
87 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
88
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) );
f4322df6 93
470402b9 94 g_object_unref (entry);
4a46cbe8 95 }
470402b9 96 else
c64371de 97 {
470402b9
RR
98 wxLogSysError(wxT("Internal error - could not get entry for double-click"));
99 event.SetInt(-1);
4a46cbe8 100 }
6c8a980f 101 }
470402b9 102 else
7a30ee8f 103 {
470402b9 104 event.SetInt(-1);
4f22cf8d
RR
105 }
106
470402b9 107 listbox->GetEventHandler()->ProcessEvent( event );
caaa4cfd 108}
865bb325 109}
66bd6b93 110
c801d85f 111//-----------------------------------------------------------------------------
470402b9 112// "changed"
c801d85f
KB
113//-----------------------------------------------------------------------------
114
4a46cbe8 115extern "C" {
470402b9
RR
116static void
117gtk_listitem_changed_callback( GtkTreeSelection* selection, wxListBox *listbox )
c801d85f 118{
470402b9 119 if (g_blockEventsOnDrag) return;
f4322df6 120
470402b9 121 if (listbox->m_blockEvent) return;
f4322df6 122
470402b9
RR
123 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
124 event.SetEventObject( listbox );
159b66c0 125
470402b9 126 if (listbox->HasFlag(wxLB_MULTIPLE) || listbox->HasFlag(wxLB_EXTENDED))
159b66c0 127 {
470402b9
RR
128 wxArrayInt selections;
129 listbox->GetSelections( selections );
f4322df6 130
470402b9
RR
131 if (selections.GetCount() == 0)
132 {
133 // indicate that this is a deselection
134 event.SetExtraLong( 0 );
135 event.SetInt( -1 );
f4322df6 136
470402b9 137 listbox->GetEventHandler()->ProcessEvent( event );
f4322df6 138
470402b9
RR
139 return;
140 }
141 else
142 {
143 // indicate that this is a selection
144 event.SetExtraLong( 1 );
145 event.SetInt( selections[0] );
f4322df6 146
470402b9
RR
147 listbox->GetEventHandler()->ProcessEvent( event );
148 }
149 }
150 else
151 {
152 int index = listbox->GetSelection();
153 if (index == wxNOT_FOUND)
154 {
155 // indicate that this is a deselection
156 event.SetExtraLong( 0 );
157 event.SetInt( -1 );
f4322df6 158
470402b9 159 listbox->GetEventHandler()->ProcessEvent( event );
f4322df6 160
470402b9
RR
161 return;
162 }
163 else
164 {
165 GtkTreeEntry* entry = listbox->GtkGetEntry( index );
4a46cbe8 166
470402b9
RR
167 // indicate that this is a selection
168 event.SetExtraLong( 1 );
4a46cbe8 169
470402b9
RR
170 event.SetInt( index );
171 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
dcf40a56 172
470402b9
RR
173 if ( listbox->HasClientObjectData() )
174 event.SetClientObject(
caf6e6de 175 (wxClientData*) gtk_tree_entry_get_userdata(entry)
4a46cbe8 176 );
470402b9
RR
177 else if ( listbox->HasClientUntypedData() )
178 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
09cf7c58 179
470402b9 180 listbox->GetEventHandler()->ProcessEvent( event );
4a46cbe8 181
470402b9
RR
182 g_object_unref (entry);
183 }
4a46cbe8 184 }
4a46cbe8 185}
6de97a3b 186}
c801d85f 187
4a46cbe8
RR
188//-----------------------------------------------------------------------------
189// GtkTreeEntry destruction (to destroy client data)
190//-----------------------------------------------------------------------------
191
865bb325 192extern "C" {
caf6e6de 193static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry,
4a46cbe8 194 wxListBox* listbox)
865bb325 195{
470402b9 196 if (listbox->HasClientObjectData())
4a46cbe8
RR
197 {
198 gpointer userdata = gtk_tree_entry_get_userdata(entry);
470402b9 199 if (userdata)
4a46cbe8
RR
200 delete (wxClientData *)userdata;
201 }
865bb325
VZ
202}
203}
204
4a46cbe8
RR
205//-----------------------------------------------------------------------------
206// Sorting callback (standard CmpNoCase return value)
207//-----------------------------------------------------------------------------
a236aa20 208#include <iostream>
4a46cbe8 209
865bb325 210extern "C" {
4a46cbe8
RR
211static gint gtk_listbox_sort_callback(GtkTreeModel *model,
212 GtkTreeIter *a,
213 GtkTreeIter *b,
214 wxListBox *listbox)
865bb325 215{
4a46cbe8
RR
216 GtkTreeEntry* entry;
217 GtkTreeEntry* entry2;
218
219 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
220 a,
caf6e6de 221 WXLISTBOX_DATACOLUMN_ARG(listbox),
4a46cbe8
RR
222 &entry, -1);
223 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
224 b,
caf6e6de 225 WXLISTBOX_DATACOLUMN_ARG(listbox),
4a46cbe8
RR
226 &entry2, -1);
227 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
228 wxCHECK_MSG(entry2, 0, wxT("Could not get entry2"));
229
230 //We compare collate keys here instead of calling g_utf8_collate
231 //as it is rather slow (and even the docs reccommend this)
a236aa20
VZ
232 int ret = strcmp(gtk_tree_entry_get_collate_key(entry),
233 gtk_tree_entry_get_collate_key(entry2));
4a46cbe8 234
3fe39b0c
MR
235 g_object_unref (entry);
236 g_object_unref (entry2);
4a46cbe8
RR
237
238 return ret;
865bb325
VZ
239}
240}
241
a60c99e6 242//-----------------------------------------------------------------------------
4a46cbe8 243// Searching callback (TRUE == not equal, FALSE == equal)
c801d85f
KB
244//-----------------------------------------------------------------------------
245
865bb325 246extern "C" {
4a46cbe8
RR
247static gboolean gtk_listbox_searchequal_callback(GtkTreeModel* model,
248 gint column,
249 const gchar* key,
250 GtkTreeIter* iter,
251 wxListBox* listbox)
f2e93537 252{
4a46cbe8
RR
253 GtkTreeEntry* entry;
254
255 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
256 iter,
caf6e6de 257 WXLISTBOX_DATACOLUMN_ARG(listbox),
4a46cbe8
RR
258 &entry, -1);
259 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
e808cf8a 260 wxGtkString keycollatekey(g_utf8_collate_key(key, -1));
17a1ebd1 261
a236aa20
VZ
262 int ret = strcmp(keycollatekey,
263 gtk_tree_entry_get_collate_key(entry));
17a1ebd1 264
3fe39b0c 265 g_object_unref (entry);
4a46cbe8
RR
266
267 return ret != 0;
f2e93537 268}
865bb325 269}
f2e93537
RR
270
271//-----------------------------------------------------------------------------
272// wxListBox
273//-----------------------------------------------------------------------------
274
b1294ada 275IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems)
c801d85f 276
2ee3ee1b
VZ
277// ----------------------------------------------------------------------------
278// construction
279// ----------------------------------------------------------------------------
280
b6e5dfef 281void wxListBox::Init()
c801d85f 282{
4a46cbe8 283 m_treeview = (GtkTreeView*) NULL;
88ac883a 284#if wxUSE_CHECKLISTBOX
caf6e6de 285 m_hasCheckBoxes = false;
88ac883a 286#endif // wxUSE_CHECKLISTBOX
6de97a3b 287}
c801d85f 288
584ad2a3
MB
289bool wxListBox::Create( wxWindow *parent, wxWindowID id,
290 const wxPoint &pos, const wxSize &size,
291 const wxArrayString& choices,
292 long style, const wxValidator& validator,
293 const wxString &name )
294{
295 wxCArrayString chs(choices);
296
297 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
298 style, validator, name );
299}
300
dcf40a56 301bool wxListBox::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
302 const wxPoint &pos, const wxSize &size,
303 int n, const wxString choices[],
2ee3ee1b
VZ
304 long style, const wxValidator& validator,
305 const wxString &name )
c801d85f 306{
caf6e6de 307 m_blockEvent = false;
dcf40a56 308
4dcaf11a
RR
309 if (!PreCreation( parent, pos, size ) ||
310 !CreateBase( parent, id, pos, size, style, validator, name ))
311 {
223d09f6 312 wxFAIL_MSG( wxT("wxListBox creation failed") );
caf6e6de 313 return false;
4dcaf11a 314 }
6de97a3b 315
fd0eed64 316 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
034be888
RR
317 if (style & wxLB_ALWAYS_SB)
318 {
319 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
320 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
321 }
322 else
323 {
324 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
325 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
326 }
ff8bfdbb 327
6493aaca
VZ
328
329 GtkScrolledWindowSetBorder(m_widget, style);
330
4a46cbe8
RR
331 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
332
333 //wxListBox doesn't have a header :)
334 //NB: If enabled SetFirstItem doesn't work correctly
335 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
336
470402b9 337#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
338 if(m_hasCheckBoxes)
339 ((wxCheckListBox*)this)->DoCreateCheckList();
470402b9 340#endif // wxUSE_CHECKLISTBOX
4a46cbe8
RR
341
342 // Create the data column
caf6e6de 343 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
4a46cbe8 344 gtk_cell_renderer_text_new(),
caf6e6de 345 "text",
4a46cbe8
RR
346 WXLISTBOX_DATACOLUMN, NULL);
347
348 // Now create+set the model (GtkListStore) - first argument # of columns
470402b9 349#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
350 if(m_hasCheckBoxes)
351 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
352 GTK_TYPE_TREE_ENTRY);
353 else
354#endif
355 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
356
357 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
caf6e6de 358
3fe39b0c 359 g_object_unref (m_liststore); //free on treeview destruction
caf6e6de 360
4a46cbe8
RR
361 // Disable the pop-up textctrl that enables searching - note that
362 // the docs specify that even if this disabled (which we are doing)
363 // the user can still have it through the start-interactive-search
364 // key binding...either way we want to provide a searchequal callback
caf6e6de 365 // NB: If this is enabled a doubleclick event (activate) gets sent
4a46cbe8
RR
366 // on a successful search
367 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
caf6e6de 368 gtk_tree_view_set_search_equal_func(m_treeview,
4a46cbe8
RR
369 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
370 this,
371 NULL);
372
373 gtk_tree_view_set_enable_search(m_treeview, FALSE);
374
375
376 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
f4322df6 377
470402b9
RR
378 g_signal_connect_after (selection, "changed",
379 G_CALLBACK (gtk_listitem_changed_callback), this);
dcf40a56 380
4a82abc8 381 GtkSelectionMode mode;
fd0eed64 382 if (style & wxLB_MULTIPLE)
4a82abc8 383 {
fd0eed64 384 mode = GTK_SELECTION_MULTIPLE;
4a82abc8 385 }
fd0eed64 386 else if (style & wxLB_EXTENDED)
4a82abc8 387 {
fd0eed64 388 mode = GTK_SELECTION_EXTENDED;
4a82abc8
RR
389 }
390 else
391 {
392 // if style was 0 set single mode
393 m_windowStyle |= wxLB_SINGLE;
4fcfa27c 394 mode = GTK_SELECTION_SINGLE;
4a82abc8 395 }
6a6d4eed 396
4a46cbe8
RR
397 gtk_tree_selection_set_mode( selection, mode );
398
470402b9 399 // Handle sortable stuff
a236aa20 400 if(HasFlag(wxLB_SORT))
4a46cbe8 401 {
470402b9 402 // Setup sorting in ascending (wx) order
caf6e6de
WS
403 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
404 WXLISTBOX_DATACOLUMN,
4a46cbe8
RR
405 GTK_SORT_ASCENDING);
406
470402b9 407 // Set the sort callback
4a46cbe8
RR
408 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
409 WXLISTBOX_DATACOLUMN,
410 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
411 this, //userdata
412 NULL //"destroy notifier"
413 );
414 }
415
dcf40a56 416
dcd3c79c 417 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
caf6e6de 418
4a46cbe8 419 gtk_widget_show( GTK_WIDGET(m_treeview) );
0bb3fc29 420 m_focusWidget = GTK_WIDGET(m_treeview);
dcf40a56 421
a236aa20 422 Append(n, choices); // insert initial items
17a1ebd1 423
470402b9
RR
424 // generate dclick events
425 g_signal_connect_after(m_treeview, "row-activated",
4a46cbe8 426 G_CALLBACK(gtk_listbox_row_activated_callback), this);
2ee3ee1b 427
f03fc89f 428 m_parent->DoAddChild( this );
ff8bfdbb 429
abdeb9e7 430 PostCreation(size);
170acdc9 431 SetInitialSize(size); // need this too because this is a wxControlWithItems
dcf40a56 432
caf6e6de 433 return true;
6de97a3b 434}
c801d85f 435
fd0eed64 436wxListBox::~wxListBox()
c801d85f 437{
caf6e6de 438 m_hasVMT = false;
4a82abc8 439
caaa4cfd 440 Clear();
6de97a3b 441}
c801d85f 442
8161b5b9
VZ
443// ----------------------------------------------------------------------------
444// adding items
445// ----------------------------------------------------------------------------
446
a236aa20
VZ
447int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
448 unsigned int pos,
449 void **clientData,
450 wxClientDataType type)
bb0ca8df 451{
a236aa20 452 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
2ee3ee1b 453
9f884528
RD
454 InvalidateBestSize();
455
2eb1512a
MR
456 GtkTreeIter* pIter = NULL; // append by default
457 GtkTreeIter iter;
a236aa20 458 if ( pos != GetCount() )
bb0ca8df 459 {
a236aa20 460 wxCHECK_MSG( GtkGetIteratorFor(pos, &iter), wxNOT_FOUND,
0d5f4ba3 461 wxT("internal wxListBox error in insertion") );
bb0ca8df 462
4a46cbe8 463 pIter = &iter;
bb0ca8df 464 }
2ee3ee1b 465
a236aa20
VZ
466 const unsigned int numItems = items.GetCount();
467 for ( unsigned int i = 0; i < numItems; ++i )
2ee3ee1b 468 {
4a46cbe8 469 GtkTreeEntry* entry = gtk_tree_entry_new();
a236aa20 470 gtk_tree_entry_set_label(entry, wxGTK_CONV(items[i]));
caf6e6de 471 gtk_tree_entry_set_destroy_func(entry,
4a46cbe8 472 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
9fa72bd2 473 this);
dcf40a56 474
4a46cbe8
RR
475 if (clientData)
476 gtk_tree_entry_set_userdata(entry, clientData[i]);
9fa72bd2 477
4a46cbe8
RR
478 GtkTreeIter itercur;
479 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
c9433ea9 480
0d5f4ba3 481 GtkSetItem(itercur, entry);
fd0eed64 482
0d5f4ba3 483 g_object_unref (entry);
4a46cbe8 484 }
17a1ebd1 485
a236aa20
VZ
486 if ( !HasClientData() )
487 m_clientDataItemsType = type;
8228b893 488
a236aa20 489 return pos + numItems - 1;
fd0eed64 490}
dcf40a56 491
2ee3ee1b 492// ----------------------------------------------------------------------------
8161b5b9 493// deleting items
2ee3ee1b 494// ----------------------------------------------------------------------------
ff8bfdbb 495
a236aa20 496void wxListBox::DoClear()
fd0eed64 497{
4a46cbe8 498 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 499
cdff1318
RD
500 InvalidateBestSize();
501
4a46cbe8 502 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
6de97a3b 503}
c801d85f 504
a236aa20 505void wxListBox::DoDeleteOneItem(unsigned int n)
c801d85f 506{
4a46cbe8 507 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 508
cdff1318
RD
509 InvalidateBestSize();
510
4a46cbe8 511 GtkTreeIter iter;
0d5f4ba3 512 wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("wrong listbox index") );
dcf40a56 513
0d5f4ba3
VZ
514 // this returns false if iter is invalid (e.g. deleting item at end) but
515 // since we don't use iter, we ignore the return value
4a46cbe8
RR
516 gtk_list_store_remove(m_liststore, &iter);
517}
dcf40a56 518
4a46cbe8 519// ----------------------------------------------------------------------------
0d5f4ba3 520// helper functions for working with iterators
4a46cbe8 521// ----------------------------------------------------------------------------
2ee3ee1b 522
0d5f4ba3 523bool wxListBox::GtkGetIteratorFor(unsigned pos, GtkTreeIter *iter) const
4a46cbe8 524{
0d5f4ba3
VZ
525 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
526 iter, NULL, pos) )
4a46cbe8 527 {
0d5f4ba3
VZ
528 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos);
529 return false;
f5e27805 530 }
ff8bfdbb 531
0d5f4ba3
VZ
532 return true;
533}
534
535int wxListBox::GtkGetIndexFor(GtkTreeIter& iter) const
536{
537 GtkTreePath *path =
538 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
539
540 gint* pIntPath = gtk_tree_path_get_indices(path);
541
542 wxCHECK_MSG( pIntPath, wxNOT_FOUND, _T("failed to get iterator path") );
543
544 int idx = pIntPath[0];
545
546 gtk_tree_path_free( path );
547
548 return idx;
549}
550
551// get GtkTreeEntry from position (note: you need to g_unref it if valid)
552GtkTreeEntry *wxListBox::GtkGetEntry(unsigned n) const
553{
554 GtkTreeIter iter;
555 if ( !GtkGetIteratorFor(n, &iter) )
556 return NULL;
557
4a46cbe8
RR
558
559 GtkTreeEntry* entry = NULL;
560 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore), &iter,
561 WXLISTBOX_DATACOLUMN, &entry, -1);
562
563 return entry;
2ee3ee1b
VZ
564}
565
0d5f4ba3
VZ
566void wxListBox::GtkSetItem(GtkTreeIter& iter, const GtkTreeEntry *entry)
567{
568#if wxUSE_CHECKLISTBOX
569 if ( m_hasCheckBoxes )
570 {
571 gtk_list_store_set(m_liststore, &iter,
572 0, FALSE, // FALSE == not toggled
573 1, entry,
574 -1);
575 }
576 else
577#endif // wxUSE_CHECKLISTBOX
578 {
579 gtk_list_store_set(m_liststore, &iter, 0, entry, -1);
580 }
581}
582
8161b5b9
VZ
583// ----------------------------------------------------------------------------
584// client data
585// ----------------------------------------------------------------------------
586
aa61d352 587void* wxListBox::DoGetItemClientData(unsigned int n) const
8161b5b9 588{
aa61d352 589 wxCHECK_MSG( IsValid(n), NULL,
4a46cbe8 590 wxT("Invalid index passed to GetItemClientData") );
8161b5b9 591
4a46cbe8
RR
592 GtkTreeEntry* entry = GtkGetEntry(n);
593 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
8161b5b9 594
4a46cbe8 595 void* userdata = gtk_tree_entry_get_userdata( entry );
3fe39b0c 596 g_object_unref (entry);
4a46cbe8 597 return userdata;
8161b5b9
VZ
598}
599
aa61d352 600void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
8161b5b9 601{
aa61d352 602 wxCHECK_RET( IsValid(n),
4a46cbe8 603 wxT("Invalid index passed to SetItemClientData") );
8161b5b9 604
4a46cbe8
RR
605 GtkTreeEntry* entry = GtkGetEntry(n);
606 wxCHECK_RET(entry, wxT("could not get entry"));
8161b5b9 607
4a46cbe8 608 gtk_tree_entry_set_userdata( entry, clientData );
3fe39b0c 609 g_object_unref (entry);
8161b5b9
VZ
610}
611
2ee3ee1b
VZ
612// ----------------------------------------------------------------------------
613// string list access
614// ----------------------------------------------------------------------------
615
33c6e437 616void wxListBox::SetString(unsigned int n, const wxString& label)
2ee3ee1b 617{
8228b893 618 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
4a46cbe8 619 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
2ee3ee1b 620
4a46cbe8
RR
621 GtkTreeEntry* entry = GtkGetEntry(n);
622 wxCHECK_RET( entry, wxT("wrong listbox index") );
2ee3ee1b 623
33c6e437
VZ
624 // update the item itself
625 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
4a46cbe8 626
33c6e437
VZ
627 // and update the model which will refresh the tree too
628 GtkTreeIter iter;
0d5f4ba3 629 wxCHECK_RET( GtkGetIteratorFor(n, &iter), _T("failed to get iterator") );
4a46cbe8 630
0d5f4ba3
VZ
631 // FIXME: this resets the checked status of a wxCheckListBox item
632
633 GtkSetItem(iter, entry);
6de97a3b 634}
c801d85f 635
aa61d352 636wxString wxListBox::GetString(unsigned int n) const
c801d85f 637{
4a46cbe8 638 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
fc54776e 639
4a46cbe8
RR
640 GtkTreeEntry* entry = GtkGetEntry(n);
641 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
642
643 wxString label = wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
2ee3ee1b 644
3fe39b0c 645 g_object_unref (entry);
4a46cbe8 646 return label;
2ee3ee1b
VZ
647}
648
aa61d352 649unsigned int wxListBox::GetCount() const
2ee3ee1b 650{
8228b893 651 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
2ee3ee1b 652
aa61d352 653 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
6de97a3b 654}
c801d85f 655
11e62fe6 656int wxListBox::FindString( const wxString &item, bool bCase ) const
c801d85f 657{
4a46cbe8 658 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
ff8bfdbb 659
4a46cbe8 660 //Sort of hackish - maybe there is a faster way
aa61d352 661 unsigned int nCount = wxListBox::GetCount();
ff8bfdbb 662
aa61d352 663 for(unsigned int i = 0; i < nCount; ++i)
4a46cbe8
RR
664 {
665 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
8228b893 666 return (int)i;
fd0eed64
RR
667 }
668
dcf40a56 669
4a46cbe8 670 // it's not an error if the string is not found -> no wxCHECK
2ee3ee1b 671 return wxNOT_FOUND;
6de97a3b 672}
c801d85f 673
2ee3ee1b
VZ
674// ----------------------------------------------------------------------------
675// selection
676// ----------------------------------------------------------------------------
677
fd0eed64 678int wxListBox::GetSelection() const
c801d85f 679{
e6e83933
WS
680 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
681 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
4a46cbe8 682 wxT("must be single selection listbox"));
ff8bfdbb 683
caf6e6de 684 GtkTreeIter iter;
4a46cbe8
RR
685 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
686
caf6e6de 687 // only works on single-sel
4a46cbe8 688 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
e6e83933 689 return wxNOT_FOUND;
4a46cbe8 690
0d5f4ba3 691 return GtkGetIndexFor(iter);
6de97a3b 692}
c801d85f 693
fd0eed64 694int wxListBox::GetSelections( wxArrayInt& aSelections ) const
c801d85f 695{
e6e83933 696 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
c801d85f 697
fd0eed64
RR
698 aSelections.Empty();
699
fd0eed64 700 int i = 0;
caf6e6de 701 GtkTreeIter iter;
4a46cbe8
RR
702 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
703
704 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
705 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
706 do
fd0eed64 707 {
4a46cbe8
RR
708 if (gtk_tree_selection_iter_is_selected(selection, &iter))
709 aSelections.Add(i);
710
711 i++;
712 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
6a6d4eed 713 }
dcf40a56 714
4a46cbe8 715 return aSelections.GetCount();
6de97a3b 716}
c801d85f 717
2ee3ee1b 718bool wxListBox::IsSelected( int n ) const
c801d85f 719{
caf6e6de 720 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
ff8bfdbb 721
4a46cbe8 722 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
9ef6ff8c 723
4a46cbe8 724 GtkTreeIter iter;
0d5f4ba3 725 wxCHECK_MSG( GtkGetIteratorFor(n, &iter), false, wxT("Invalid index") );
9ef6ff8c 726
4a46cbe8 727 return gtk_tree_selection_iter_is_selected(selection, &iter);
6de97a3b 728}
c801d85f 729
c6179a84 730void wxListBox::DoSetSelection( int n, bool select )
c801d85f 731{
ef33382e
VZ
732 // passing -1 to SetSelection() is documented to deselect all items
733 if ( n == wxNOT_FOUND )
734 {
735 // ... and not generate any events in the process
736 GtkDeselectAll();
470402b9 737 return;
ef33382e
VZ
738 }
739
740 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
741
742 // don't generate the selection event
743 GtkSetSelection(n, select, true);
744}
745
746void wxListBox::GtkDeselectAll()
747{
748 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
749
750 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
751
752 m_blockEvent = true;
753
754 gtk_tree_selection_unselect_all(selection);
755
756 m_blockEvent = false;
4a46cbe8
RR
757}
758
759void wxListBox::GtkSetSelection(int n, const bool select, const bool blockEvent)
760{
761 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
762
763 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
764
765 GtkTreeIter iter;
0d5f4ba3 766 wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("Invalid index") );
ff8bfdbb 767
4a46cbe8 768 m_blockEvent = blockEvent;
953704c1 769
fd0eed64 770 if (select)
4a46cbe8 771 gtk_tree_selection_select_iter(selection, &iter);
fd0eed64 772 else
4a46cbe8 773 gtk_tree_selection_unselect_iter(selection, &iter);
014b0d06 774
caf6e6de 775 m_blockEvent = false;
6de97a3b 776}
c801d85f 777
9ef6ff8c 778void wxListBox::DoSetFirstItem( int n )
c801d85f 779{
4a46cbe8 780 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
8228b893 781 wxCHECK_RET( IsValid(n), wxT("invalid index"));
9ef6ff8c 782
4a46cbe8
RR
783 //RN: I have no idea why this line is needed...
784 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
9ef6ff8c 785 return;
f96b15a3 786
28354d90 787 GtkTreeIter iter;
0d5f4ba3
VZ
788 if ( !GtkGetIteratorFor(n, &iter) )
789 return;
4a46cbe8 790
28354d90
VZ
791 GtkTreePath* path = gtk_tree_model_get_path(
792 GTK_TREE_MODEL(m_liststore), &iter);
9ef6ff8c 793
28354d90
VZ
794 // Scroll to the desired cell (0.0 == topleft alignment)
795 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
796 TRUE, 0.0f, 0.0f);
4a46cbe8 797
28354d90 798 gtk_tree_path_free(path);
6de97a3b 799}
c801d85f 800
c00fed0e
VZ
801// ----------------------------------------------------------------------------
802// hittest
803// ----------------------------------------------------------------------------
804
57772185 805int wxListBox::DoListHitTest(const wxPoint& point) const
c00fed0e 806{
a183ddba
VZ
807 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
808 // we only want visible items we need to check for it manually here
22a35096 809 if ( !GetClientRect().Contains(point) )
a183ddba
VZ
810 return wxNOT_FOUND;
811
57772185 812 // need to translate from master window since it is in client coords
c00fed0e 813 gint binx, biny;
caf6e6de 814 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
c00fed0e
VZ
815 &binx, &biny, NULL, NULL, NULL);
816
817 GtkTreePath* path;
57772185
VZ
818 if ( !gtk_tree_view_get_path_at_pos
819 (
caf6e6de 820 m_treeview,
57772185
VZ
821 point.x - binx,
822 point.y - biny,
823 &path,
824 NULL, // [out] column (always 0 here)
825 NULL, // [out] x-coord relative to the cell (not interested)
826 NULL // [out] y-coord relative to the cell
827 ) )
c00fed0e
VZ
828 {
829 return wxNOT_FOUND;
830 }
831
832 int index = gtk_tree_path_get_indices(path)[0];
833 gtk_tree_path_free(path);
834
835 return index;
836}
4a46cbe8 837
2ee3ee1b
VZ
838// ----------------------------------------------------------------------------
839// helpers
840// ----------------------------------------------------------------------------
c801d85f 841
ff8bfdbb 842#if wxUSE_TOOLTIPS
6e5f6c7c 843void wxListBox::ApplyToolTip( GtkTooltips *tips, const gchar *tip )
b1170810 844{
4a46cbe8 845 // RN: Is this needed anymore?
6e5f6c7c 846 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), tip, (gchar*) NULL );
b1170810 847}
ff8bfdbb 848#endif // wxUSE_TOOLTIPS
b1170810 849
fd0eed64 850GtkWidget *wxListBox::GetConnectWidget()
e3e65dac 851{
e5e41cfe
VZ
852 // the correct widget for listbox events (such as mouse clicks for example)
853 // is m_treeview, not the parent scrolled window
854 return GTK_WIDGET(m_treeview);
6de97a3b 855}
e3e65dac 856
1c2b98d6 857GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
868a2826 858{
1c2b98d6 859 return gtk_tree_view_get_bin_window(m_treeview);
868a2826 860}
e3e65dac 861
f40fdaa3 862void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
c058d771 863{
f40fdaa3 864 if (m_hasBgCol && m_backgroundColour.Ok())
e380f72b 865 {
04e044c4 866 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
4a46cbe8 867 if (window)
f03fc89f 868 {
08f60019 869 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
f03fc89f
VZ
870 gdk_window_set_background( window, m_backgroundColour.GetColor() );
871 gdk_window_clear( window );
872 }
e380f72b 873 }
ff8bfdbb 874
4a46cbe8 875 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
68dda785 876}
dcf924a3 877
f68586e5
VZ
878wxSize wxListBox::DoGetBestSize() const
879{
4a46cbe8
RR
880 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
881
cdff1318 882 // Start with a minimum size that's not too small
f96b15a3 883 int cx, cy;
2b5f62a0 884 GetTextExtent( wxT("X"), &cx, &cy);
c64371de
RD
885 int lbWidth = 3 * cx;
886 int lbHeight = 10;
cdff1318 887
caf6e6de 888 // Get the visible area of the tree view (limit to the 10th item
cdff1318 889 // so that it isn't too big)
aa61d352 890 unsigned int count = GetCount();
cdff1318
RD
891 if (count)
892 {
893 int wLine;
894
895 // Find the widest line
aa61d352 896 for(unsigned int i = 0; i < count; i++) {
cdff1318
RD
897 wxString str(GetString(i));
898 GetTextExtent(str, &wLine, NULL);
899 lbWidth = wxMax(lbWidth, wLine);
900 }
901
902 lbWidth += 3 * cx;
903
904 // And just a bit more for the checkbox if present and then some
905 // (these are rough guesses)
470402b9 906#if wxUSE_CHECKLISTBOX
cdff1318
RD
907 if ( m_hasCheckBoxes )
908 {
909 lbWidth += 35;
910 cy = cy > 25 ? cy : 25; // rough height of checkbox
911 }
912#endif
f96b15a3 913
aa61d352
VZ
914 // don't make the listbox too tall (limit height to around 10 items) but don't
915 // make it too small neither
cdff1318
RD
916 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
917 }
caf6e6de 918
cdff1318
RD
919 // Add room for the scrollbar
920 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
f96b15a3 921
9f884528 922 wxSize best(lbWidth, lbHeight);
17a1ebd1 923 CacheBestSize(best);
9f884528 924 return best;
f68586e5
VZ
925}
926
9d522606
RD
927// static
928wxVisualAttributes
929wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
930{
4a46cbe8 931 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
9d522606
RD
932}
933
3ae4c570 934#endif // wxUSE_LISTBOX