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