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