]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/listbox.cpp
check that the version of __sync_sub_and_fetch that returns a value is supported...
[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
937013e0 107 listbox->HandleWindowEvent( 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
937013e0 138 listbox->HandleWindowEvent( 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
937013e0 148 listbox->HandleWindowEvent( event );
470402b9
RR
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
937013e0 160 listbox->HandleWindowEvent( 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
937013e0 181 listbox->HandleWindowEvent( 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//-----------------------------------------------------------------------------
209
865bb325 210extern "C" {
e4161a2a 211static gint gtk_listbox_sort_callback(GtkTreeModel * WXUNUSED(model),
4a46cbe8
RR
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" {
e4161a2a
VZ
247static gboolean gtk_listbox_searchequal_callback(GtkTreeModel * WXUNUSED(model),
248 gint WXUNUSED(column),
4a46cbe8
RR
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,
e4161a2a 450 wxClientDataType WXUNUSED(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 486 return pos + numItems - 1;
fd0eed64 487}
dcf40a56 488
2ee3ee1b 489// ----------------------------------------------------------------------------
8161b5b9 490// deleting items
2ee3ee1b 491// ----------------------------------------------------------------------------
ff8bfdbb 492
a236aa20 493void wxListBox::DoClear()
fd0eed64 494{
4a46cbe8 495 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 496
cdff1318
RD
497 InvalidateBestSize();
498
4a46cbe8 499 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
6de97a3b 500}
c801d85f 501
a236aa20 502void wxListBox::DoDeleteOneItem(unsigned int n)
c801d85f 503{
4a46cbe8 504 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 505
cdff1318
RD
506 InvalidateBestSize();
507
4a46cbe8 508 GtkTreeIter iter;
0d5f4ba3 509 wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("wrong listbox index") );
dcf40a56 510
0d5f4ba3
VZ
511 // this returns false if iter is invalid (e.g. deleting item at end) but
512 // since we don't use iter, we ignore the return value
4a46cbe8
RR
513 gtk_list_store_remove(m_liststore, &iter);
514}
dcf40a56 515
4a46cbe8 516// ----------------------------------------------------------------------------
0d5f4ba3 517// helper functions for working with iterators
4a46cbe8 518// ----------------------------------------------------------------------------
2ee3ee1b 519
0d5f4ba3 520bool wxListBox::GtkGetIteratorFor(unsigned pos, GtkTreeIter *iter) const
4a46cbe8 521{
0d5f4ba3
VZ
522 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
523 iter, NULL, pos) )
4a46cbe8 524 {
0d5f4ba3
VZ
525 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos);
526 return false;
f5e27805 527 }
ff8bfdbb 528
0d5f4ba3
VZ
529 return true;
530}
531
532int wxListBox::GtkGetIndexFor(GtkTreeIter& iter) const
533{
534 GtkTreePath *path =
535 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
536
537 gint* pIntPath = gtk_tree_path_get_indices(path);
538
539 wxCHECK_MSG( pIntPath, wxNOT_FOUND, _T("failed to get iterator path") );
540
541 int idx = pIntPath[0];
542
543 gtk_tree_path_free( path );
544
545 return idx;
546}
547
548// get GtkTreeEntry from position (note: you need to g_unref it if valid)
549GtkTreeEntry *wxListBox::GtkGetEntry(unsigned n) const
550{
551 GtkTreeIter iter;
552 if ( !GtkGetIteratorFor(n, &iter) )
553 return NULL;
554
4a46cbe8
RR
555
556 GtkTreeEntry* entry = NULL;
557 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore), &iter,
558 WXLISTBOX_DATACOLUMN, &entry, -1);
559
560 return entry;
2ee3ee1b
VZ
561}
562
0d5f4ba3
VZ
563void wxListBox::GtkSetItem(GtkTreeIter& iter, const GtkTreeEntry *entry)
564{
565#if wxUSE_CHECKLISTBOX
566 if ( m_hasCheckBoxes )
567 {
568 gtk_list_store_set(m_liststore, &iter,
569 0, FALSE, // FALSE == not toggled
570 1, entry,
571 -1);
572 }
573 else
574#endif // wxUSE_CHECKLISTBOX
575 {
576 gtk_list_store_set(m_liststore, &iter, 0, entry, -1);
577 }
578}
579
8161b5b9
VZ
580// ----------------------------------------------------------------------------
581// client data
582// ----------------------------------------------------------------------------
583
aa61d352 584void* wxListBox::DoGetItemClientData(unsigned int n) const
8161b5b9 585{
aa61d352 586 wxCHECK_MSG( IsValid(n), NULL,
4a46cbe8 587 wxT("Invalid index passed to GetItemClientData") );
8161b5b9 588
4a46cbe8
RR
589 GtkTreeEntry* entry = GtkGetEntry(n);
590 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
8161b5b9 591
4a46cbe8 592 void* userdata = gtk_tree_entry_get_userdata( entry );
3fe39b0c 593 g_object_unref (entry);
4a46cbe8 594 return userdata;
8161b5b9
VZ
595}
596
aa61d352 597void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
8161b5b9 598{
aa61d352 599 wxCHECK_RET( IsValid(n),
4a46cbe8 600 wxT("Invalid index passed to SetItemClientData") );
8161b5b9 601
4a46cbe8
RR
602 GtkTreeEntry* entry = GtkGetEntry(n);
603 wxCHECK_RET(entry, wxT("could not get entry"));
8161b5b9 604
4a46cbe8 605 gtk_tree_entry_set_userdata( entry, clientData );
3fe39b0c 606 g_object_unref (entry);
8161b5b9
VZ
607}
608
2ee3ee1b
VZ
609// ----------------------------------------------------------------------------
610// string list access
611// ----------------------------------------------------------------------------
612
33c6e437 613void wxListBox::SetString(unsigned int n, const wxString& label)
2ee3ee1b 614{
8228b893 615 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
4a46cbe8 616 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
2ee3ee1b 617
4a46cbe8
RR
618 GtkTreeEntry* entry = GtkGetEntry(n);
619 wxCHECK_RET( entry, wxT("wrong listbox index") );
2ee3ee1b 620
33c6e437
VZ
621 // update the item itself
622 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
4a46cbe8 623
33c6e437
VZ
624 // and update the model which will refresh the tree too
625 GtkTreeIter iter;
0d5f4ba3 626 wxCHECK_RET( GtkGetIteratorFor(n, &iter), _T("failed to get iterator") );
4a46cbe8 627
0d5f4ba3
VZ
628 // FIXME: this resets the checked status of a wxCheckListBox item
629
630 GtkSetItem(iter, entry);
6de97a3b 631}
c801d85f 632
aa61d352 633wxString wxListBox::GetString(unsigned int n) const
c801d85f 634{
4a46cbe8 635 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
fc54776e 636
4a46cbe8
RR
637 GtkTreeEntry* entry = GtkGetEntry(n);
638 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
639
640 wxString label = wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
2ee3ee1b 641
3fe39b0c 642 g_object_unref (entry);
4a46cbe8 643 return label;
2ee3ee1b
VZ
644}
645
aa61d352 646unsigned int wxListBox::GetCount() const
2ee3ee1b 647{
8228b893 648 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
2ee3ee1b 649
aa61d352 650 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
6de97a3b 651}
c801d85f 652
11e62fe6 653int wxListBox::FindString( const wxString &item, bool bCase ) const
c801d85f 654{
4a46cbe8 655 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
ff8bfdbb 656
4a46cbe8 657 //Sort of hackish - maybe there is a faster way
aa61d352 658 unsigned int nCount = wxListBox::GetCount();
ff8bfdbb 659
aa61d352 660 for(unsigned int i = 0; i < nCount; ++i)
4a46cbe8
RR
661 {
662 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
8228b893 663 return (int)i;
fd0eed64
RR
664 }
665
dcf40a56 666
4a46cbe8 667 // it's not an error if the string is not found -> no wxCHECK
2ee3ee1b 668 return wxNOT_FOUND;
6de97a3b 669}
c801d85f 670
2ee3ee1b
VZ
671// ----------------------------------------------------------------------------
672// selection
673// ----------------------------------------------------------------------------
674
fd0eed64 675int wxListBox::GetSelection() const
c801d85f 676{
e6e83933
WS
677 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
678 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
4a46cbe8 679 wxT("must be single selection listbox"));
ff8bfdbb 680
caf6e6de 681 GtkTreeIter iter;
4a46cbe8
RR
682 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
683
caf6e6de 684 // only works on single-sel
4a46cbe8 685 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
e6e83933 686 return wxNOT_FOUND;
4a46cbe8 687
0d5f4ba3 688 return GtkGetIndexFor(iter);
6de97a3b 689}
c801d85f 690
fd0eed64 691int wxListBox::GetSelections( wxArrayInt& aSelections ) const
c801d85f 692{
e6e83933 693 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
c801d85f 694
fd0eed64
RR
695 aSelections.Empty();
696
fd0eed64 697 int i = 0;
caf6e6de 698 GtkTreeIter iter;
4a46cbe8
RR
699 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
700
701 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
702 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
703 do
fd0eed64 704 {
4a46cbe8
RR
705 if (gtk_tree_selection_iter_is_selected(selection, &iter))
706 aSelections.Add(i);
707
708 i++;
709 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
6a6d4eed 710 }
dcf40a56 711
4a46cbe8 712 return aSelections.GetCount();
6de97a3b 713}
c801d85f 714
2ee3ee1b 715bool wxListBox::IsSelected( int n ) const
c801d85f 716{
caf6e6de 717 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
ff8bfdbb 718
4a46cbe8 719 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
9ef6ff8c 720
4a46cbe8 721 GtkTreeIter iter;
0d5f4ba3 722 wxCHECK_MSG( GtkGetIteratorFor(n, &iter), false, wxT("Invalid index") );
9ef6ff8c 723
4a46cbe8 724 return gtk_tree_selection_iter_is_selected(selection, &iter);
6de97a3b 725}
c801d85f 726
c6179a84 727void wxListBox::DoSetSelection( int n, bool select )
c801d85f 728{
ef33382e
VZ
729 // passing -1 to SetSelection() is documented to deselect all items
730 if ( n == wxNOT_FOUND )
731 {
732 // ... and not generate any events in the process
733 GtkDeselectAll();
470402b9 734 return;
ef33382e
VZ
735 }
736
737 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
738
739 // don't generate the selection event
740 GtkSetSelection(n, select, true);
741}
742
743void wxListBox::GtkDeselectAll()
744{
745 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
746
747 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
748
749 m_blockEvent = true;
750
751 gtk_tree_selection_unselect_all(selection);
752
753 m_blockEvent = false;
4a46cbe8
RR
754}
755
756void wxListBox::GtkSetSelection(int n, const bool select, const bool blockEvent)
757{
758 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
759
760 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
761
762 GtkTreeIter iter;
0d5f4ba3 763 wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("Invalid index") );
ff8bfdbb 764
4a46cbe8 765 m_blockEvent = blockEvent;
953704c1 766
fd0eed64 767 if (select)
4a46cbe8 768 gtk_tree_selection_select_iter(selection, &iter);
fd0eed64 769 else
4a46cbe8 770 gtk_tree_selection_unselect_iter(selection, &iter);
e4161a2a 771
0b2e06f9
RR
772 GtkTreePath* path = gtk_tree_model_get_path(
773 GTK_TREE_MODEL(m_liststore), &iter);
774
775 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, FALSE, 0.0f, 0.0f);
776
777 gtk_tree_path_free(path);
014b0d06 778
caf6e6de 779 m_blockEvent = false;
6de97a3b 780}
c801d85f 781
9ef6ff8c 782void wxListBox::DoSetFirstItem( int n )
c801d85f 783{
4a46cbe8 784 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
8228b893 785 wxCHECK_RET( IsValid(n), wxT("invalid index"));
9ef6ff8c 786
4a46cbe8
RR
787 //RN: I have no idea why this line is needed...
788 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
9ef6ff8c 789 return;
f96b15a3 790
28354d90 791 GtkTreeIter iter;
0d5f4ba3
VZ
792 if ( !GtkGetIteratorFor(n, &iter) )
793 return;
4a46cbe8 794
28354d90
VZ
795 GtkTreePath* path = gtk_tree_model_get_path(
796 GTK_TREE_MODEL(m_liststore), &iter);
9ef6ff8c 797
28354d90
VZ
798 // Scroll to the desired cell (0.0 == topleft alignment)
799 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
800 TRUE, 0.0f, 0.0f);
4a46cbe8 801
28354d90 802 gtk_tree_path_free(path);
6de97a3b 803}
c801d85f 804
c00fed0e
VZ
805// ----------------------------------------------------------------------------
806// hittest
807// ----------------------------------------------------------------------------
808
57772185 809int wxListBox::DoListHitTest(const wxPoint& point) const
c00fed0e 810{
a183ddba
VZ
811 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
812 // we only want visible items we need to check for it manually here
22a35096 813 if ( !GetClientRect().Contains(point) )
a183ddba
VZ
814 return wxNOT_FOUND;
815
57772185 816 // need to translate from master window since it is in client coords
c00fed0e 817 gint binx, biny;
caf6e6de 818 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
c00fed0e
VZ
819 &binx, &biny, NULL, NULL, NULL);
820
821 GtkTreePath* path;
57772185
VZ
822 if ( !gtk_tree_view_get_path_at_pos
823 (
caf6e6de 824 m_treeview,
57772185
VZ
825 point.x - binx,
826 point.y - biny,
827 &path,
828 NULL, // [out] column (always 0 here)
829 NULL, // [out] x-coord relative to the cell (not interested)
830 NULL // [out] y-coord relative to the cell
831 ) )
c00fed0e
VZ
832 {
833 return wxNOT_FOUND;
834 }
835
836 int index = gtk_tree_path_get_indices(path)[0];
837 gtk_tree_path_free(path);
838
839 return index;
840}
4a46cbe8 841
2ee3ee1b
VZ
842// ----------------------------------------------------------------------------
843// helpers
844// ----------------------------------------------------------------------------
c801d85f 845
ff8bfdbb 846#if wxUSE_TOOLTIPS
6e5f6c7c 847void wxListBox::ApplyToolTip( GtkTooltips *tips, const gchar *tip )
b1170810 848{
4a46cbe8 849 // RN: Is this needed anymore?
6e5f6c7c 850 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), tip, (gchar*) NULL );
b1170810 851}
ff8bfdbb 852#endif // wxUSE_TOOLTIPS
b1170810 853
fd0eed64 854GtkWidget *wxListBox::GetConnectWidget()
e3e65dac 855{
e5e41cfe
VZ
856 // the correct widget for listbox events (such as mouse clicks for example)
857 // is m_treeview, not the parent scrolled window
858 return GTK_WIDGET(m_treeview);
6de97a3b 859}
e3e65dac 860
1c2b98d6 861GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
868a2826 862{
1c2b98d6 863 return gtk_tree_view_get_bin_window(m_treeview);
868a2826 864}
e3e65dac 865
f40fdaa3 866void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
c058d771 867{
f40fdaa3 868 if (m_hasBgCol && m_backgroundColour.Ok())
e380f72b 869 {
04e044c4 870 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
4a46cbe8 871 if (window)
f03fc89f 872 {
08f60019 873 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
f03fc89f
VZ
874 gdk_window_set_background( window, m_backgroundColour.GetColor() );
875 gdk_window_clear( window );
876 }
e380f72b 877 }
ff8bfdbb 878
4a46cbe8 879 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
68dda785 880}
dcf924a3 881
f68586e5
VZ
882wxSize wxListBox::DoGetBestSize() const
883{
4a46cbe8
RR
884 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
885
cdff1318 886 // Start with a minimum size that's not too small
f96b15a3 887 int cx, cy;
2b5f62a0 888 GetTextExtent( wxT("X"), &cx, &cy);
c64371de
RD
889 int lbWidth = 3 * cx;
890 int lbHeight = 10;
cdff1318 891
caf6e6de 892 // Get the visible area of the tree view (limit to the 10th item
cdff1318 893 // so that it isn't too big)
aa61d352 894 unsigned int count = GetCount();
cdff1318
RD
895 if (count)
896 {
897 int wLine;
898
899 // Find the widest line
aa61d352 900 for(unsigned int i = 0; i < count; i++) {
cdff1318
RD
901 wxString str(GetString(i));
902 GetTextExtent(str, &wLine, NULL);
903 lbWidth = wxMax(lbWidth, wLine);
904 }
905
906 lbWidth += 3 * cx;
907
908 // And just a bit more for the checkbox if present and then some
909 // (these are rough guesses)
470402b9 910#if wxUSE_CHECKLISTBOX
cdff1318
RD
911 if ( m_hasCheckBoxes )
912 {
913 lbWidth += 35;
914 cy = cy > 25 ? cy : 25; // rough height of checkbox
915 }
916#endif
f96b15a3 917
aa61d352
VZ
918 // don't make the listbox too tall (limit height to around 10 items) but don't
919 // make it too small neither
cdff1318
RD
920 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
921 }
caf6e6de 922
cdff1318
RD
923 // Add room for the scrollbar
924 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
f96b15a3 925
9f884528 926 wxSize best(lbWidth, lbHeight);
17a1ebd1 927 CacheBestSize(best);
9f884528 928 return best;
f68586e5
VZ
929}
930
9d522606
RD
931// static
932wxVisualAttributes
933wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
934{
4a46cbe8 935 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
9d522606
RD
936}
937
3ae4c570 938#endif // wxUSE_LISTBOX