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