]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/listbox.cpp
Added support for wxTEXT_ATTR_EFFECT_SMALL_CAPITALS.
[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
291a8f20 28#if wxUSE_TOOLTIPS
ad9835c9 29 #include "wx/tooltip.h"
291a8f20 30#endif
c801d85f 31
4a46cbe8 32#include <gtk/gtk.h>
9dc44eff
PC
33#include "wx/gtk/private.h"
34#include "wx/gtk/private/gtk2-compat.h"
35#include "wx/gtk/private/object.h"
36#include "wx/gtk/treeentry_gtk.h"
14b44999 37
a625c5b6 38#include <gdk/gdkkeysyms.h>
14b44999
VS
39#if GTK_CHECK_VERSION(3,0,0)
40#include <gdk/gdkkeysyms-compat.h>
41#endif
83624f79 42
66bd6b93
RR
43//-----------------------------------------------------------------------------
44// data
45//-----------------------------------------------------------------------------
46
d7fa7eaa
RR
47extern bool g_blockEventsOnDrag;
48extern bool g_blockEventsOnScroll;
caaa4cfd 49
7a30ee8f 50
4a82abc8 51
c9433ea9 52//-----------------------------------------------------------------------------
4a46cbe8 53// Macro to tell which row the strings are in (1 if native checklist, 0 if not)
c9433ea9
RR
54//-----------------------------------------------------------------------------
55
470402b9 56#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
57# define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
58#else
59# define WXLISTBOX_DATACOLUMN_ARG(x) (0)
470402b9 60#endif // wxUSE_CHECKLISTBOX
17a1ebd1 61
4a46cbe8 62#define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
c9433ea9 63
00d45d04
VZ
64// ----------------------------------------------------------------------------
65// helper functions
66// ----------------------------------------------------------------------------
67
68namespace
69{
70
71// Return the entry for the given listbox item.
72//
73// Return value must be released by caller if non-NULL.
74GtkTreeEntry *
75GetEntry(GtkListStore *store, GtkTreeIter *iter, const wxListBox *listbox)
76{
77 GtkTreeEntry* entry;
78 gtk_tree_model_get(GTK_TREE_MODEL(store),
79 iter,
80 WXLISTBOX_DATACOLUMN_ARG(listbox),
81 &entry,
82 -1);
83
84 return entry;
85}
86
87} // anonymous namespace
88
c9433ea9 89//-----------------------------------------------------------------------------
4a46cbe8 90// "row-activated"
c9433ea9
RR
91//-----------------------------------------------------------------------------
92
865bb325 93extern "C" {
4a46cbe8 94static void
e4161a2a 95gtk_listbox_row_activated_callback(GtkTreeView * WXUNUSED(treeview),
4a46cbe8 96 GtkTreePath *path,
e4161a2a 97 GtkTreeViewColumn * WXUNUSED(col),
4a46cbe8 98 wxListBox *listbox)
c9433ea9 99{
4a46cbe8
RR
100 if (g_blockEventsOnDrag) return;
101 if (g_blockEventsOnScroll) return;
c9433ea9 102
470402b9 103 // This is triggered by either a double-click or a space press
c9433ea9 104
4a46cbe8 105 int sel = gtk_tree_path_get_indices(path)[0];
c9433ea9 106
09e744f5 107 listbox->GTKOnActivated(sel);
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
24ee1bef 122 listbox->GTKOnSelectionChanged();
4a46cbe8 123}
24ee1bef 124
6de97a3b 125}
c801d85f 126
a625c5b6
RR
127//-----------------------------------------------------------------------------
128// "key_press_event"
129//-----------------------------------------------------------------------------
130
131extern "C" {
9ff9d30c 132static gboolean
a625c5b6
RR
133gtk_listbox_key_press_callback( GtkWidget *WXUNUSED(widget),
134 GdkEventKey *gdk_event,
135 wxListBox *listbox )
136{
03647350 137 if ((gdk_event->keyval == GDK_Return) ||
a625c5b6
RR
138 (gdk_event->keyval == GDK_ISO_Enter) ||
139 (gdk_event->keyval == GDK_KP_Enter))
140 {
f796e8f0
RR
141 int index = -1;
142 if (!listbox->HasMultipleSelection())
143 index = listbox->GetSelection();
144 else
a625c5b6 145 {
f796e8f0
RR
146 wxArrayInt sels;
147 if (listbox->GetSelections( sels ) < 1)
148 return FALSE;
149 index = sels[0];
150 }
03647350 151
f796e8f0
RR
152 if (index != wxNOT_FOUND)
153 {
09e744f5 154 listbox->GTKOnActivated(index);
03647350 155
9f400412
RR
156// wxMac and wxMSW always invoke default action
157// if (!ret)
a625c5b6
RR
158 {
159 // DClick not handled -> invoke default action
160 wxWindow *tlw = wxGetTopLevelParent( listbox );
161 if (tlw)
162 {
163 GtkWindow *gtk_window = GTK_WINDOW( tlw->GetHandle() );
164 if (gtk_window)
165 gtk_window_activate_default( gtk_window );
166 }
167 }
03647350 168
a625c5b6
RR
169 // Always intercept, otherwise we'd get another dclick
170 // event from row_activated
171 return TRUE;
172 }
173 }
03647350 174
a625c5b6
RR
175 return FALSE;
176}
177}
178
4a46cbe8
RR
179//-----------------------------------------------------------------------------
180// GtkTreeEntry destruction (to destroy client data)
181//-----------------------------------------------------------------------------
182
865bb325 183extern "C" {
caf6e6de 184static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry,
4a46cbe8 185 wxListBox* listbox)
865bb325 186{
470402b9 187 if (listbox->HasClientObjectData())
4a46cbe8
RR
188 {
189 gpointer userdata = gtk_tree_entry_get_userdata(entry);
470402b9 190 if (userdata)
4a46cbe8
RR
191 delete (wxClientData *)userdata;
192 }
865bb325
VZ
193}
194}
195
4a46cbe8
RR
196//-----------------------------------------------------------------------------
197// Sorting callback (standard CmpNoCase return value)
198//-----------------------------------------------------------------------------
199
865bb325 200extern "C" {
e4161a2a 201static gint gtk_listbox_sort_callback(GtkTreeModel * WXUNUSED(model),
4a46cbe8
RR
202 GtkTreeIter *a,
203 GtkTreeIter *b,
204 wxListBox *listbox)
865bb325 205{
00d45d04
VZ
206 wxGtkObject<GtkTreeEntry> entry1(GetEntry(listbox->m_liststore, a, listbox));
207 wxCHECK_MSG(entry1, 0, wxT("Could not get first entry"));
208
209 wxGtkObject<GtkTreeEntry> entry2(GetEntry(listbox->m_liststore, b, listbox));
210 wxCHECK_MSG(entry2, 0, wxT("Could not get second entry"));
4a46cbe8
RR
211
212 //We compare collate keys here instead of calling g_utf8_collate
eccace04 213 //as it is rather slow (and even the docs recommend this)
00d45d04
VZ
214 return strcmp(gtk_tree_entry_get_collate_key(entry1),
215 gtk_tree_entry_get_collate_key(entry2)) >= 0;
865bb325
VZ
216}
217}
218
a60c99e6 219//-----------------------------------------------------------------------------
4a46cbe8 220// Searching callback (TRUE == not equal, FALSE == equal)
c801d85f
KB
221//-----------------------------------------------------------------------------
222
865bb325 223extern "C" {
e4161a2a
VZ
224static gboolean gtk_listbox_searchequal_callback(GtkTreeModel * WXUNUSED(model),
225 gint WXUNUSED(column),
4a46cbe8
RR
226 const gchar* key,
227 GtkTreeIter* iter,
228 wxListBox* listbox)
f2e93537 229{
00d45d04
VZ
230 wxGtkObject<GtkTreeEntry>
231 entry(GetEntry(listbox->m_liststore, iter, listbox));
4a46cbe8 232 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
17a1ebd1 233
00d45d04 234 wxGtkString keycollatekey(g_utf8_collate_key(key, -1));
4a46cbe8 235
00d45d04 236 return strcmp(keycollatekey, gtk_tree_entry_get_collate_key(entry)) != 0;
f2e93537 237}
865bb325 238}
f2e93537
RR
239
240//-----------------------------------------------------------------------------
241// wxListBox
242//-----------------------------------------------------------------------------
243
2ee3ee1b
VZ
244// ----------------------------------------------------------------------------
245// construction
246// ----------------------------------------------------------------------------
247
b6e5dfef 248void wxListBox::Init()
c801d85f 249{
d3b9f782 250 m_treeview = NULL;
88ac883a 251#if wxUSE_CHECKLISTBOX
caf6e6de 252 m_hasCheckBoxes = false;
88ac883a 253#endif // wxUSE_CHECKLISTBOX
6de97a3b 254}
c801d85f 255
584ad2a3
MB
256bool wxListBox::Create( wxWindow *parent, wxWindowID id,
257 const wxPoint &pos, const wxSize &size,
258 const wxArrayString& choices,
259 long style, const wxValidator& validator,
260 const wxString &name )
261{
262 wxCArrayString chs(choices);
263
264 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
265 style, validator, name );
266}
267
dcf40a56 268bool wxListBox::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
269 const wxPoint &pos, const wxSize &size,
270 int n, const wxString choices[],
2ee3ee1b
VZ
271 long style, const wxValidator& validator,
272 const wxString &name )
c801d85f 273{
4dcaf11a
RR
274 if (!PreCreation( parent, pos, size ) ||
275 !CreateBase( parent, id, pos, size, style, validator, name ))
276 {
223d09f6 277 wxFAIL_MSG( wxT("wxListBox creation failed") );
caf6e6de 278 return false;
4dcaf11a 279 }
6de97a3b 280
d3b9f782 281 m_widget = gtk_scrolled_window_new( NULL, NULL );
9ff9d30c 282 g_object_ref(m_widget);
034be888
RR
283 if (style & wxLB_ALWAYS_SB)
284 {
285 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
286 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
287 }
288 else
289 {
290 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
291 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
292 }
ff8bfdbb 293
6493aaca 294
496e7ec6 295 GTKScrolledWindowSetBorder(m_widget, style);
6493aaca 296
4a46cbe8
RR
297 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
298
299 //wxListBox doesn't have a header :)
300 //NB: If enabled SetFirstItem doesn't work correctly
301 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
302
470402b9 303#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
304 if(m_hasCheckBoxes)
305 ((wxCheckListBox*)this)->DoCreateCheckList();
470402b9 306#endif // wxUSE_CHECKLISTBOX
4a46cbe8
RR
307
308 // Create the data column
caf6e6de 309 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
4a46cbe8 310 gtk_cell_renderer_text_new(),
caf6e6de 311 "text",
4a46cbe8
RR
312 WXLISTBOX_DATACOLUMN, NULL);
313
314 // Now create+set the model (GtkListStore) - first argument # of columns
470402b9 315#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
316 if(m_hasCheckBoxes)
317 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
318 GTK_TYPE_TREE_ENTRY);
319 else
320#endif
321 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
322
323 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
caf6e6de 324
3fe39b0c 325 g_object_unref (m_liststore); //free on treeview destruction
caf6e6de 326
4a46cbe8
RR
327 // Disable the pop-up textctrl that enables searching - note that
328 // the docs specify that even if this disabled (which we are doing)
329 // the user can still have it through the start-interactive-search
330 // key binding...either way we want to provide a searchequal callback
caf6e6de 331 // NB: If this is enabled a doubleclick event (activate) gets sent
4a46cbe8
RR
332 // on a successful search
333 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
caf6e6de 334 gtk_tree_view_set_search_equal_func(m_treeview,
4a46cbe8
RR
335 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
336 this,
337 NULL);
338
339 gtk_tree_view_set_enable_search(m_treeview, FALSE);
340
4a82abc8 341 GtkSelectionMode mode;
b4de6e0d
VZ
342 // GTK_SELECTION_EXTENDED is a deprecated synonym for GTK_SELECTION_MULTIPLE
343 if ( style & (wxLB_MULTIPLE | wxLB_EXTENDED) )
4a82abc8 344 {
fd0eed64 345 mode = GTK_SELECTION_MULTIPLE;
4a82abc8 346 }
b4de6e0d 347 else // no multi-selection flags specified
4a82abc8 348 {
4a82abc8 349 m_windowStyle |= wxLB_SINGLE;
1d919083
VZ
350
351 // Notice that we must use BROWSE and not GTK_SELECTION_SINGLE because
352 // the latter allows to not select any items at all while a single
353 // selection listbox is supposed to always have a selection (at least
354 // once the user selected something, it might not have any initially).
355 mode = GTK_SELECTION_BROWSE;
4a82abc8 356 }
6a6d4eed 357
1e6ffd66 358 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
4a46cbe8
RR
359 gtk_tree_selection_set_mode( selection, mode );
360
470402b9 361 // Handle sortable stuff
a236aa20 362 if(HasFlag(wxLB_SORT))
4a46cbe8 363 {
470402b9 364 // Setup sorting in ascending (wx) order
caf6e6de
WS
365 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
366 WXLISTBOX_DATACOLUMN,
4a46cbe8
RR
367 GTK_SORT_ASCENDING);
368
470402b9 369 // Set the sort callback
4a46cbe8
RR
370 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
371 WXLISTBOX_DATACOLUMN,
372 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
373 this, //userdata
374 NULL //"destroy notifier"
375 );
376 }
377
dcf40a56 378
dcd3c79c 379 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
caf6e6de 380
4a46cbe8 381 gtk_widget_show( GTK_WIDGET(m_treeview) );
0bb3fc29 382 m_focusWidget = GTK_WIDGET(m_treeview);
dcf40a56 383
a236aa20 384 Append(n, choices); // insert initial items
17a1ebd1 385
470402b9
RR
386 // generate dclick events
387 g_signal_connect_after(m_treeview, "row-activated",
4a46cbe8 388 G_CALLBACK(gtk_listbox_row_activated_callback), this);
2ee3ee1b 389
a625c5b6
RR
390 // for intercepting dclick generation by <ENTER>
391 g_signal_connect (m_treeview, "key_press_event",
392 G_CALLBACK (gtk_listbox_key_press_callback),
393 this);
f03fc89f 394 m_parent->DoAddChild( this );
ff8bfdbb 395
abdeb9e7 396 PostCreation(size);
170acdc9 397 SetInitialSize(size); // need this too because this is a wxControlWithItems
dcf40a56 398
1e6ffd66
RR
399 g_signal_connect_after (selection, "changed",
400 G_CALLBACK (gtk_listitem_changed_callback), this);
03647350 401
caf6e6de 402 return true;
6de97a3b 403}
c801d85f 404
fd0eed64 405wxListBox::~wxListBox()
c801d85f 406{
caf6e6de 407 m_hasVMT = false;
4a82abc8 408
caaa4cfd 409 Clear();
6de97a3b 410}
c801d85f 411
dec7b5a8 412void wxListBox::GTKDisableEvents()
1e6ffd66
RR
413{
414 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
415
416 g_signal_handlers_block_by_func(selection,
417 (gpointer) gtk_listitem_changed_callback, this);
418}
419
dec7b5a8 420void wxListBox::GTKEnableEvents()
1e6ffd66
RR
421{
422 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
423
424 g_signal_handlers_unblock_by_func(selection,
425 (gpointer) gtk_listitem_changed_callback, this);
03647350 426
05d790f8 427 UpdateOldSelections();
1e6ffd66
RR
428}
429
937fc7db
RR
430
431void wxListBox::Update()
432{
433 wxWindow::Update();
03647350 434
937fc7db 435 if (m_treeview)
385e8575 436 gdk_window_process_updates(gtk_widget_get_window(GTK_WIDGET(m_treeview)), true);
937fc7db
RR
437}
438
8161b5b9
VZ
439// ----------------------------------------------------------------------------
440// adding items
441// ----------------------------------------------------------------------------
442
a236aa20
VZ
443int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
444 unsigned int pos,
445 void **clientData,
cfe8a907 446 wxClientDataType type)
bb0ca8df 447{
a236aa20 448 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
2ee3ee1b 449
9f884528
RD
450 InvalidateBestSize();
451
2eb1512a
MR
452 GtkTreeIter* pIter = NULL; // append by default
453 GtkTreeIter iter;
a236aa20 454 if ( pos != GetCount() )
bb0ca8df 455 {
dec7b5a8 456 wxCHECK_MSG( GTKGetIteratorFor(pos, &iter), wxNOT_FOUND,
0d5f4ba3 457 wxT("internal wxListBox error in insertion") );
bb0ca8df 458
4a46cbe8 459 pIter = &iter;
bb0ca8df 460 }
2ee3ee1b 461
a236aa20
VZ
462 const unsigned int numItems = items.GetCount();
463 for ( unsigned int i = 0; i < numItems; ++i )
2ee3ee1b 464 {
00d45d04 465 wxGtkObject<GtkTreeEntry> entry(gtk_tree_entry_new());
a236aa20 466 gtk_tree_entry_set_label(entry, wxGTK_CONV(items[i]));
caf6e6de 467 gtk_tree_entry_set_destroy_func(entry,
4a46cbe8 468 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
9fa72bd2 469 this);
dcf40a56 470
4a46cbe8
RR
471 GtkTreeIter itercur;
472 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
c9433ea9 473
dec7b5a8 474 GTKSetItem(itercur, entry);
fd0eed64 475
cfe8a907 476 if (clientData)
dec7b5a8 477 AssignNewItemClientData(GTKGetIndexFor(itercur), clientData, i, type);
4a46cbe8 478 }
17a1ebd1 479
bc60925f
RR
480 UpdateOldSelections();
481
a236aa20 482 return pos + numItems - 1;
fd0eed64 483}
dcf40a56 484
2ee3ee1b 485// ----------------------------------------------------------------------------
8161b5b9 486// deleting items
2ee3ee1b 487// ----------------------------------------------------------------------------
ff8bfdbb 488
a236aa20 489void wxListBox::DoClear()
fd0eed64 490{
4a46cbe8 491 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 492
dec7b5a8 493 GTKDisableEvents(); // just in case
2154130c 494
cdff1318
RD
495 InvalidateBestSize();
496
4a46cbe8 497 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
03647350 498
dec7b5a8 499 GTKEnableEvents();
fb040ec1
VZ
500
501 UpdateOldSelections();
6de97a3b 502}
c801d85f 503
a236aa20 504void wxListBox::DoDeleteOneItem(unsigned int n)
c801d85f 505{
4a46cbe8 506 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 507
cdff1318
RD
508 InvalidateBestSize();
509
dec7b5a8 510 GTKDisableEvents(); // just in case
2154130c 511
4a46cbe8 512 GtkTreeIter iter;
dec7b5a8 513 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("wrong listbox index") );
dcf40a56 514
0d5f4ba3
VZ
515 // this returns false if iter is invalid (e.g. deleting item at end) but
516 // since we don't use iter, we ignore the return value
4a46cbe8 517 gtk_list_store_remove(m_liststore, &iter);
03647350 518
dec7b5a8 519 GTKEnableEvents();
4a46cbe8 520}
dcf40a56 521
4a46cbe8 522// ----------------------------------------------------------------------------
0d5f4ba3 523// helper functions for working with iterators
4a46cbe8 524// ----------------------------------------------------------------------------
2ee3ee1b 525
dec7b5a8 526bool wxListBox::GTKGetIteratorFor(unsigned pos, GtkTreeIter *iter) const
4a46cbe8 527{
0d5f4ba3
VZ
528 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
529 iter, NULL, pos) )
4a46cbe8 530 {
0d5f4ba3
VZ
531 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos);
532 return false;
f5e27805 533 }
ff8bfdbb 534
0d5f4ba3
VZ
535 return true;
536}
537
dec7b5a8 538int wxListBox::GTKGetIndexFor(GtkTreeIter& iter) const
0d5f4ba3
VZ
539{
540 GtkTreePath *path =
541 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
542
543 gint* pIntPath = gtk_tree_path_get_indices(path);
544
9a83f860 545 wxCHECK_MSG( pIntPath, wxNOT_FOUND, wxT("failed to get iterator path") );
0d5f4ba3
VZ
546
547 int idx = pIntPath[0];
548
549 gtk_tree_path_free( path );
550
551 return idx;
552}
553
554// get GtkTreeEntry from position (note: you need to g_unref it if valid)
dec7b5a8 555GtkTreeEntry *wxListBox::GTKGetEntry(unsigned n) const
0d5f4ba3
VZ
556{
557 GtkTreeIter iter;
dec7b5a8 558 if ( !GTKGetIteratorFor(n, &iter) )
0d5f4ba3
VZ
559 return NULL;
560
00d45d04 561 return GetEntry(m_liststore, &iter, this);
2ee3ee1b
VZ
562}
563
dec7b5a8 564void wxListBox::GTKSetItem(GtkTreeIter& iter, const GtkTreeEntry *entry)
0d5f4ba3
VZ
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{
00d45d04 587 wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n));
4a46cbe8 588 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
8161b5b9 589
00d45d04 590 return gtk_tree_entry_get_userdata( entry );
8161b5b9
VZ
591}
592
aa61d352 593void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
8161b5b9 594{
00d45d04 595 wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n));
4a46cbe8 596 wxCHECK_RET(entry, wxT("could not get entry"));
8161b5b9 597
4a46cbe8 598 gtk_tree_entry_set_userdata( entry, clientData );
8161b5b9
VZ
599}
600
2ee3ee1b
VZ
601// ----------------------------------------------------------------------------
602// string list access
603// ----------------------------------------------------------------------------
604
33c6e437 605void wxListBox::SetString(unsigned int n, const wxString& label)
2ee3ee1b 606{
8228b893 607 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
4a46cbe8 608 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
2ee3ee1b 609
dec7b5a8 610 GtkTreeEntry* entry = GTKGetEntry(n);
4a46cbe8 611 wxCHECK_RET( entry, wxT("wrong listbox index") );
2ee3ee1b 612
33c6e437
VZ
613 // update the item itself
614 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
4a46cbe8 615
33c6e437
VZ
616 // and update the model which will refresh the tree too
617 GtkTreeIter iter;
dec7b5a8 618 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("failed to get iterator") );
4a46cbe8 619
0d5f4ba3
VZ
620 // FIXME: this resets the checked status of a wxCheckListBox item
621
dec7b5a8 622 GTKSetItem(iter, entry);
6de97a3b 623}
c801d85f 624
aa61d352 625wxString wxListBox::GetString(unsigned int n) const
c801d85f 626{
4a46cbe8 627 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
fc54776e 628
00d45d04 629 wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n));
4a46cbe8
RR
630 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
631
00d45d04 632 return wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
2ee3ee1b
VZ
633}
634
aa61d352 635unsigned int wxListBox::GetCount() const
2ee3ee1b 636{
8228b893 637 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
2ee3ee1b 638
aa61d352 639 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
6de97a3b 640}
c801d85f 641
11e62fe6 642int wxListBox::FindString( const wxString &item, bool bCase ) const
c801d85f 643{
4a46cbe8 644 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
ff8bfdbb 645
4a46cbe8 646 //Sort of hackish - maybe there is a faster way
aa61d352 647 unsigned int nCount = wxListBox::GetCount();
ff8bfdbb 648
aa61d352 649 for(unsigned int i = 0; i < nCount; ++i)
4a46cbe8
RR
650 {
651 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
8228b893 652 return (int)i;
fd0eed64
RR
653 }
654
dcf40a56 655
4a46cbe8 656 // it's not an error if the string is not found -> no wxCHECK
2ee3ee1b 657 return wxNOT_FOUND;
6de97a3b 658}
c801d85f 659
2ee3ee1b
VZ
660// ----------------------------------------------------------------------------
661// selection
662// ----------------------------------------------------------------------------
663
09e744f5
VZ
664void wxListBox::GTKOnActivated(int item)
665{
666 SendEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, item, IsSelected(item));
667}
668
24ee1bef
VZ
669void wxListBox::GTKOnSelectionChanged()
670{
671 if ( HasFlag(wxLB_MULTIPLE | wxLB_EXTENDED) )
672 {
673 CalcAndSendEvent();
674 }
675 else // single selection
676 {
09e744f5
VZ
677 const int item = GetSelection();
678 if ( DoChangeSingleSelection(item) )
679 SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED, item, true);
24ee1bef
VZ
680 }
681}
682
fd0eed64 683int wxListBox::GetSelection() const
c801d85f 684{
e6e83933
WS
685 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
686 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
4a46cbe8 687 wxT("must be single selection listbox"));
ff8bfdbb 688
caf6e6de 689 GtkTreeIter iter;
4a46cbe8
RR
690 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
691
caf6e6de 692 // only works on single-sel
4a46cbe8 693 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
e6e83933 694 return wxNOT_FOUND;
4a46cbe8 695
dec7b5a8 696 return GTKGetIndexFor(iter);
6de97a3b 697}
c801d85f 698
fd0eed64 699int wxListBox::GetSelections( wxArrayInt& aSelections ) const
c801d85f 700{
e6e83933 701 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
c801d85f 702
fd0eed64
RR
703 aSelections.Empty();
704
05d790f8 705 int i = 0;
caf6e6de 706 GtkTreeIter iter;
4a46cbe8
RR
707 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
708
709 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
710 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
711 do
fd0eed64 712 {
4a46cbe8
RR
713 if (gtk_tree_selection_iter_is_selected(selection, &iter))
714 aSelections.Add(i);
715
716 i++;
717 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
6a6d4eed 718 }
dcf40a56 719
4a46cbe8 720 return aSelections.GetCount();
6de97a3b 721}
c801d85f 722
2ee3ee1b 723bool wxListBox::IsSelected( int n ) const
c801d85f 724{
caf6e6de 725 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
ff8bfdbb 726
4a46cbe8 727 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
9ef6ff8c 728
4a46cbe8 729 GtkTreeIter iter;
dec7b5a8 730 wxCHECK_MSG( GTKGetIteratorFor(n, &iter), false, wxT("Invalid index") );
9ef6ff8c 731
d5027818 732 return gtk_tree_selection_iter_is_selected(selection, &iter) != 0;
6de97a3b 733}
c801d85f 734
c6179a84 735void wxListBox::DoSetSelection( int n, bool select )
c801d85f 736{
1e6ffd66
RR
737 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
738
dec7b5a8 739 GTKDisableEvents();
03647350 740
1e6ffd66
RR
741 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
742
ef33382e
VZ
743 // passing -1 to SetSelection() is documented to deselect all items
744 if ( n == wxNOT_FOUND )
745 {
1e6ffd66 746 gtk_tree_selection_unselect_all(selection);
dec7b5a8 747 GTKEnableEvents();
470402b9 748 return;
ef33382e
VZ
749 }
750
751 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
752
03647350 753
4a46cbe8 754 GtkTreeIter iter;
dec7b5a8 755 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("Invalid index") );
ff8bfdbb 756
fd0eed64 757 if (select)
4a46cbe8 758 gtk_tree_selection_select_iter(selection, &iter);
fd0eed64 759 else
4a46cbe8 760 gtk_tree_selection_unselect_iter(selection, &iter);
e4161a2a 761
0b2e06f9
RR
762 GtkTreePath* path = gtk_tree_model_get_path(
763 GTK_TREE_MODEL(m_liststore), &iter);
764
765 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, FALSE, 0.0f, 0.0f);
766
767 gtk_tree_path_free(path);
014b0d06 768
dec7b5a8 769 GTKEnableEvents();
1e6ffd66
RR
770}
771
0d29a482 772void wxListBox::DoScrollToCell(int n, float alignY, float alignX)
c801d85f 773{
4a46cbe8 774 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
8228b893 775 wxCHECK_RET( IsValid(n), wxT("invalid index"));
9ef6ff8c 776
4a46cbe8 777 //RN: I have no idea why this line is needed...
fc9ab22a 778 if (gdk_pointer_is_grabbed () && gtk_widget_has_grab(GTK_WIDGET(m_treeview)))
9ef6ff8c 779 return;
f96b15a3 780
28354d90 781 GtkTreeIter iter;
dec7b5a8 782 if ( !GTKGetIteratorFor(n, &iter) )
0d5f4ba3 783 return;
4a46cbe8 784
28354d90
VZ
785 GtkTreePath* path = gtk_tree_model_get_path(
786 GTK_TREE_MODEL(m_liststore), &iter);
9ef6ff8c 787
28354d90
VZ
788 // Scroll to the desired cell (0.0 == topleft alignment)
789 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
0d29a482 790 TRUE, alignY, alignX);
4a46cbe8 791
28354d90 792 gtk_tree_path_free(path);
6de97a3b 793}
c801d85f 794
0d29a482
VZ
795void wxListBox::DoSetFirstItem(int n)
796{
797 DoScrollToCell(n, 0, 0);
798}
799
800void wxListBox::EnsureVisible(int n)
801{
802 DoScrollToCell(n, 0.5, 0);
803}
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),
9dc44eff 819 &binx, &biny, NULL, NULL);
c00fed0e
VZ
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
fd0eed64 846GtkWidget *wxListBox::GetConnectWidget()
e3e65dac 847{
e5e41cfe
VZ
848 // the correct widget for listbox events (such as mouse clicks for example)
849 // is m_treeview, not the parent scrolled window
850 return GTK_WIDGET(m_treeview);
6de97a3b 851}
e3e65dac 852
1c2b98d6 853GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
868a2826 854{
1c2b98d6 855 return gtk_tree_view_get_bin_window(m_treeview);
868a2826 856}
e3e65dac 857
f40fdaa3 858void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
c058d771 859{
9dc44eff
PC
860#ifdef __WXGTK3__
861 // don't know if this is even necessary, or how to do it
862#else
a1b806b9 863 if (m_hasBgCol && m_backgroundColour.IsOk())
e380f72b 864 {
04e044c4 865 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
4a46cbe8 866 if (window)
f03fc89f 867 {
08f60019 868 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
f03fc89f
VZ
869 gdk_window_set_background( window, m_backgroundColour.GetColor() );
870 gdk_window_clear( window );
871 }
e380f72b 872 }
9dc44eff 873#endif
ff8bfdbb 874
9dc44eff 875 GTKApplyStyle(GTK_WIDGET(m_treeview), style);
68dda785 876}
dcf924a3 877
f68586e5
VZ
878wxSize wxListBox::DoGetBestSize() const
879{
4a46cbe8
RR
880 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
881
cdff1318 882 // Start with a minimum size that's not too small
f96b15a3 883 int cx, cy;
2b5f62a0 884 GetTextExtent( wxT("X"), &cx, &cy);
47179317 885 int lbWidth = 0;
c64371de 886 int lbHeight = 10;
cdff1318 887
47179317
VZ
888 // Find the widest string.
889 const unsigned int count = GetCount();
890 if ( count )
cdff1318
RD
891 {
892 int wLine;
47179317
VZ
893 for ( unsigned int i = 0; i < count; i++ )
894 {
895 GetTextExtent(GetString(i), &wLine, NULL);
896 if ( wLine > lbWidth )
897 lbWidth = wLine;
cdff1318 898 }
47179317 899 }
cdff1318 900
47179317 901 lbWidth += 3 * cx;
cdff1318 902
47179317
VZ
903 // And just a bit more for the checkbox if present and then some
904 // (these are rough guesses)
470402b9 905#if wxUSE_CHECKLISTBOX
47179317
VZ
906 if ( m_hasCheckBoxes )
907 {
908 lbWidth += 35;
909 cy = cy > 25 ? cy : 25; // rough height of checkbox
cdff1318 910 }
47179317 911#endif
caf6e6de 912
cdff1318
RD
913 // Add room for the scrollbar
914 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
f96b15a3 915
47179317
VZ
916 // Don't make the listbox too tall but don't make it too small neither
917 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
918
9f884528 919 wxSize best(lbWidth, lbHeight);
17a1ebd1 920 CacheBestSize(best);
9f884528 921 return best;
f68586e5
VZ
922}
923
9d522606
RD
924// static
925wxVisualAttributes
926wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
927{
4a46cbe8 928 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
9d522606
RD
929}
930
3ae4c570 931#endif // wxUSE_LISTBOX