]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/listbox.cpp
the wx part knows better whether it has the focus (may be the embedded NSView, not...
[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"
00d45d04 29#include "wx/gtk/private/object.h"
4a46cbe8 30#include "wx/gtk/treeentry_gtk.h"
291a8f20
RR
31
32#if wxUSE_TOOLTIPS
ad9835c9 33 #include "wx/tooltip.h"
291a8f20 34#endif
c801d85f 35
4a46cbe8 36#include <gtk/gtk.h>
a625c5b6 37#include <gdk/gdkkeysyms.h>
83624f79 38
66bd6b93
RR
39//-----------------------------------------------------------------------------
40// data
41//-----------------------------------------------------------------------------
42
d7fa7eaa
RR
43extern bool g_blockEventsOnDrag;
44extern bool g_blockEventsOnScroll;
caaa4cfd 45
7a30ee8f 46
4a82abc8 47
c9433ea9 48//-----------------------------------------------------------------------------
4a46cbe8 49// Macro to tell which row the strings are in (1 if native checklist, 0 if not)
c9433ea9
RR
50//-----------------------------------------------------------------------------
51
470402b9 52#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
53# define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
54#else
55# define WXLISTBOX_DATACOLUMN_ARG(x) (0)
470402b9 56#endif // wxUSE_CHECKLISTBOX
17a1ebd1 57
4a46cbe8 58#define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
c9433ea9 59
00d45d04
VZ
60// ----------------------------------------------------------------------------
61// helper functions
62// ----------------------------------------------------------------------------
63
64namespace
65{
66
67// Return the entry for the given listbox item.
68//
69// Return value must be released by caller if non-NULL.
70GtkTreeEntry *
71GetEntry(GtkListStore *store, GtkTreeIter *iter, const wxListBox *listbox)
72{
73 GtkTreeEntry* entry;
74 gtk_tree_model_get(GTK_TREE_MODEL(store),
75 iter,
76 WXLISTBOX_DATACOLUMN_ARG(listbox),
77 &entry,
78 -1);
79
80 return entry;
81}
82
83} // anonymous namespace
84
c9433ea9 85//-----------------------------------------------------------------------------
4a46cbe8 86// "row-activated"
c9433ea9
RR
87//-----------------------------------------------------------------------------
88
865bb325 89extern "C" {
4a46cbe8 90static void
e4161a2a 91gtk_listbox_row_activated_callback(GtkTreeView * WXUNUSED(treeview),
4a46cbe8 92 GtkTreePath *path,
e4161a2a 93 GtkTreeViewColumn * WXUNUSED(col),
4a46cbe8 94 wxListBox *listbox)
c9433ea9 95{
4a46cbe8
RR
96 if (g_blockEventsOnDrag) return;
97 if (g_blockEventsOnScroll) return;
c9433ea9 98
470402b9 99 // This is triggered by either a double-click or a space press
c9433ea9 100
4a46cbe8 101 int sel = gtk_tree_path_get_indices(path)[0];
c9433ea9 102
09e744f5 103 listbox->GTKOnActivated(sel);
caaa4cfd 104}
865bb325 105}
66bd6b93 106
c801d85f 107//-----------------------------------------------------------------------------
470402b9 108// "changed"
c801d85f
KB
109//-----------------------------------------------------------------------------
110
4a46cbe8 111extern "C" {
470402b9 112static void
e4161a2a
VZ
113gtk_listitem_changed_callback(GtkTreeSelection * WXUNUSED(selection),
114 wxListBox *listbox )
c801d85f 115{
470402b9 116 if (g_blockEventsOnDrag) return;
f4322df6 117
24ee1bef 118 listbox->GTKOnSelectionChanged();
4a46cbe8 119}
24ee1bef 120
6de97a3b 121}
c801d85f 122
a625c5b6
RR
123//-----------------------------------------------------------------------------
124// "key_press_event"
125//-----------------------------------------------------------------------------
126
127extern "C" {
9ff9d30c 128static gboolean
a625c5b6
RR
129gtk_listbox_key_press_callback( GtkWidget *WXUNUSED(widget),
130 GdkEventKey *gdk_event,
131 wxListBox *listbox )
132{
03647350 133 if ((gdk_event->keyval == GDK_Return) ||
a625c5b6
RR
134 (gdk_event->keyval == GDK_ISO_Enter) ||
135 (gdk_event->keyval == GDK_KP_Enter))
136 {
f796e8f0
RR
137 int index = -1;
138 if (!listbox->HasMultipleSelection())
139 index = listbox->GetSelection();
140 else
a625c5b6 141 {
f796e8f0
RR
142 wxArrayInt sels;
143 if (listbox->GetSelections( sels ) < 1)
144 return FALSE;
145 index = sels[0];
146 }
03647350 147
f796e8f0
RR
148 if (index != wxNOT_FOUND)
149 {
09e744f5 150 listbox->GTKOnActivated(index);
03647350 151
9f400412
RR
152// wxMac and wxMSW always invoke default action
153// if (!ret)
a625c5b6
RR
154 {
155 // DClick not handled -> invoke default action
156 wxWindow *tlw = wxGetTopLevelParent( listbox );
157 if (tlw)
158 {
159 GtkWindow *gtk_window = GTK_WINDOW( tlw->GetHandle() );
160 if (gtk_window)
161 gtk_window_activate_default( gtk_window );
162 }
163 }
03647350 164
a625c5b6
RR
165 // Always intercept, otherwise we'd get another dclick
166 // event from row_activated
167 return TRUE;
168 }
169 }
03647350 170
a625c5b6
RR
171 return FALSE;
172}
173}
174
4a46cbe8
RR
175//-----------------------------------------------------------------------------
176// GtkTreeEntry destruction (to destroy client data)
177//-----------------------------------------------------------------------------
178
865bb325 179extern "C" {
caf6e6de 180static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry,
4a46cbe8 181 wxListBox* listbox)
865bb325 182{
470402b9 183 if (listbox->HasClientObjectData())
4a46cbe8
RR
184 {
185 gpointer userdata = gtk_tree_entry_get_userdata(entry);
470402b9 186 if (userdata)
4a46cbe8
RR
187 delete (wxClientData *)userdata;
188 }
865bb325
VZ
189}
190}
191
4a46cbe8
RR
192//-----------------------------------------------------------------------------
193// Sorting callback (standard CmpNoCase return value)
194//-----------------------------------------------------------------------------
195
865bb325 196extern "C" {
e4161a2a 197static gint gtk_listbox_sort_callback(GtkTreeModel * WXUNUSED(model),
4a46cbe8
RR
198 GtkTreeIter *a,
199 GtkTreeIter *b,
200 wxListBox *listbox)
865bb325 201{
00d45d04
VZ
202 wxGtkObject<GtkTreeEntry> entry1(GetEntry(listbox->m_liststore, a, listbox));
203 wxCHECK_MSG(entry1, 0, wxT("Could not get first entry"));
204
205 wxGtkObject<GtkTreeEntry> entry2(GetEntry(listbox->m_liststore, b, listbox));
206 wxCHECK_MSG(entry2, 0, wxT("Could not get second entry"));
4a46cbe8
RR
207
208 //We compare collate keys here instead of calling g_utf8_collate
209 //as it is rather slow (and even the docs reccommend this)
00d45d04
VZ
210 return strcmp(gtk_tree_entry_get_collate_key(entry1),
211 gtk_tree_entry_get_collate_key(entry2)) >= 0;
865bb325
VZ
212}
213}
214
a60c99e6 215//-----------------------------------------------------------------------------
4a46cbe8 216// Searching callback (TRUE == not equal, FALSE == equal)
c801d85f
KB
217//-----------------------------------------------------------------------------
218
865bb325 219extern "C" {
e4161a2a
VZ
220static gboolean gtk_listbox_searchequal_callback(GtkTreeModel * WXUNUSED(model),
221 gint WXUNUSED(column),
4a46cbe8
RR
222 const gchar* key,
223 GtkTreeIter* iter,
224 wxListBox* listbox)
f2e93537 225{
00d45d04
VZ
226 wxGtkObject<GtkTreeEntry>
227 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
00d45d04 232 return strcmp(keycollatekey, gtk_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,
314 GTK_TYPE_TREE_ENTRY);
315 else
316#endif
317 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
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{
caf6e6de 403 m_hasVMT = false;
4a82abc8 404
caaa4cfd 405 Clear();
6de97a3b 406}
c801d85f 407
dec7b5a8 408void wxListBox::GTKDisableEvents()
1e6ffd66
RR
409{
410 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
411
412 g_signal_handlers_block_by_func(selection,
413 (gpointer) gtk_listitem_changed_callback, this);
414}
415
dec7b5a8 416void wxListBox::GTKEnableEvents()
1e6ffd66
RR
417{
418 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
419
420 g_signal_handlers_unblock_by_func(selection,
421 (gpointer) gtk_listitem_changed_callback, this);
03647350 422
05d790f8 423 UpdateOldSelections();
1e6ffd66
RR
424}
425
937fc7db
RR
426
427void wxListBox::Update()
428{
429 wxWindow::Update();
03647350 430
937fc7db 431 if (m_treeview)
1975440b 432 gdk_window_process_updates(GTK_WIDGET(m_treeview)->window, TRUE);
937fc7db
RR
433}
434
8161b5b9
VZ
435// ----------------------------------------------------------------------------
436// adding items
437// ----------------------------------------------------------------------------
438
a236aa20
VZ
439int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
440 unsigned int pos,
441 void **clientData,
cfe8a907 442 wxClientDataType type)
bb0ca8df 443{
a236aa20 444 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
2ee3ee1b 445
9f884528
RD
446 InvalidateBestSize();
447
2eb1512a
MR
448 GtkTreeIter* pIter = NULL; // append by default
449 GtkTreeIter iter;
a236aa20 450 if ( pos != GetCount() )
bb0ca8df 451 {
dec7b5a8 452 wxCHECK_MSG( GTKGetIteratorFor(pos, &iter), wxNOT_FOUND,
0d5f4ba3 453 wxT("internal wxListBox error in insertion") );
bb0ca8df 454
4a46cbe8 455 pIter = &iter;
bb0ca8df 456 }
2ee3ee1b 457
a236aa20
VZ
458 const unsigned int numItems = items.GetCount();
459 for ( unsigned int i = 0; i < numItems; ++i )
2ee3ee1b 460 {
00d45d04 461 wxGtkObject<GtkTreeEntry> entry(gtk_tree_entry_new());
a236aa20 462 gtk_tree_entry_set_label(entry, wxGTK_CONV(items[i]));
caf6e6de 463 gtk_tree_entry_set_destroy_func(entry,
4a46cbe8 464 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
9fa72bd2 465 this);
dcf40a56 466
4a46cbe8
RR
467 GtkTreeIter itercur;
468 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
c9433ea9 469
dec7b5a8 470 GTKSetItem(itercur, entry);
fd0eed64 471
cfe8a907 472 if (clientData)
dec7b5a8 473 AssignNewItemClientData(GTKGetIndexFor(itercur), clientData, i, type);
4a46cbe8 474 }
17a1ebd1 475
bc60925f
RR
476 UpdateOldSelections();
477
a236aa20 478 return pos + numItems - 1;
fd0eed64 479}
dcf40a56 480
2ee3ee1b 481// ----------------------------------------------------------------------------
8161b5b9 482// deleting items
2ee3ee1b 483// ----------------------------------------------------------------------------
ff8bfdbb 484
a236aa20 485void wxListBox::DoClear()
fd0eed64 486{
4a46cbe8 487 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 488
dec7b5a8 489 GTKDisableEvents(); // just in case
2154130c 490
cdff1318
RD
491 InvalidateBestSize();
492
4a46cbe8 493 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
03647350 494
dec7b5a8 495 GTKEnableEvents();
6de97a3b 496}
c801d85f 497
a236aa20 498void wxListBox::DoDeleteOneItem(unsigned int n)
c801d85f 499{
4a46cbe8 500 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 501
cdff1318
RD
502 InvalidateBestSize();
503
dec7b5a8 504 GTKDisableEvents(); // just in case
2154130c 505
4a46cbe8 506 GtkTreeIter iter;
dec7b5a8 507 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("wrong listbox index") );
dcf40a56 508
0d5f4ba3
VZ
509 // this returns false if iter is invalid (e.g. deleting item at end) but
510 // since we don't use iter, we ignore the return value
4a46cbe8 511 gtk_list_store_remove(m_liststore, &iter);
03647350 512
dec7b5a8 513 GTKEnableEvents();
4a46cbe8 514}
dcf40a56 515
4a46cbe8 516// ----------------------------------------------------------------------------
0d5f4ba3 517// helper functions for working with iterators
4a46cbe8 518// ----------------------------------------------------------------------------
2ee3ee1b 519
dec7b5a8 520bool wxListBox::GTKGetIteratorFor(unsigned pos, GtkTreeIter *iter) const
4a46cbe8 521{
0d5f4ba3
VZ
522 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
523 iter, NULL, pos) )
4a46cbe8 524 {
0d5f4ba3
VZ
525 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos);
526 return false;
f5e27805 527 }
ff8bfdbb 528
0d5f4ba3
VZ
529 return true;
530}
531
dec7b5a8 532int wxListBox::GTKGetIndexFor(GtkTreeIter& iter) const
0d5f4ba3
VZ
533{
534 GtkTreePath *path =
535 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
536
537 gint* pIntPath = gtk_tree_path_get_indices(path);
538
9a83f860 539 wxCHECK_MSG( pIntPath, wxNOT_FOUND, wxT("failed to get iterator path") );
0d5f4ba3
VZ
540
541 int idx = pIntPath[0];
542
543 gtk_tree_path_free( path );
544
545 return idx;
546}
547
548// get GtkTreeEntry from position (note: you need to g_unref it if valid)
dec7b5a8 549GtkTreeEntry *wxListBox::GTKGetEntry(unsigned n) const
0d5f4ba3
VZ
550{
551 GtkTreeIter iter;
dec7b5a8 552 if ( !GTKGetIteratorFor(n, &iter) )
0d5f4ba3
VZ
553 return NULL;
554
00d45d04 555 return GetEntry(m_liststore, &iter, this);
2ee3ee1b
VZ
556}
557
dec7b5a8 558void wxListBox::GTKSetItem(GtkTreeIter& iter, const GtkTreeEntry *entry)
0d5f4ba3
VZ
559{
560#if wxUSE_CHECKLISTBOX
561 if ( m_hasCheckBoxes )
562 {
563 gtk_list_store_set(m_liststore, &iter,
564 0, FALSE, // FALSE == not toggled
565 1, entry,
566 -1);
567 }
568 else
569#endif // wxUSE_CHECKLISTBOX
570 {
571 gtk_list_store_set(m_liststore, &iter, 0, entry, -1);
572 }
573}
574
8161b5b9
VZ
575// ----------------------------------------------------------------------------
576// client data
577// ----------------------------------------------------------------------------
578
aa61d352 579void* wxListBox::DoGetItemClientData(unsigned int n) const
8161b5b9 580{
00d45d04 581 wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n));
4a46cbe8 582 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
8161b5b9 583
00d45d04 584 return gtk_tree_entry_get_userdata( entry );
8161b5b9
VZ
585}
586
aa61d352 587void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
8161b5b9 588{
00d45d04 589 wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n));
4a46cbe8 590 wxCHECK_RET(entry, wxT("could not get entry"));
8161b5b9 591
4a46cbe8 592 gtk_tree_entry_set_userdata( entry, clientData );
8161b5b9
VZ
593}
594
2ee3ee1b
VZ
595// ----------------------------------------------------------------------------
596// string list access
597// ----------------------------------------------------------------------------
598
33c6e437 599void wxListBox::SetString(unsigned int n, const wxString& label)
2ee3ee1b 600{
8228b893 601 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
4a46cbe8 602 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
2ee3ee1b 603
dec7b5a8 604 GtkTreeEntry* entry = GTKGetEntry(n);
4a46cbe8 605 wxCHECK_RET( entry, wxT("wrong listbox index") );
2ee3ee1b 606
33c6e437
VZ
607 // update the item itself
608 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
4a46cbe8 609
33c6e437
VZ
610 // and update the model which will refresh the tree too
611 GtkTreeIter iter;
dec7b5a8 612 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("failed to get iterator") );
4a46cbe8 613
0d5f4ba3
VZ
614 // FIXME: this resets the checked status of a wxCheckListBox item
615
dec7b5a8 616 GTKSetItem(iter, entry);
6de97a3b 617}
c801d85f 618
aa61d352 619wxString wxListBox::GetString(unsigned int n) const
c801d85f 620{
4a46cbe8 621 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
fc54776e 622
00d45d04 623 wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n));
4a46cbe8
RR
624 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
625
00d45d04 626 return wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
2ee3ee1b
VZ
627}
628
aa61d352 629unsigned int wxListBox::GetCount() const
2ee3ee1b 630{
8228b893 631 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
2ee3ee1b 632
aa61d352 633 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
6de97a3b 634}
c801d85f 635
11e62fe6 636int wxListBox::FindString( const wxString &item, bool bCase ) const
c801d85f 637{
4a46cbe8 638 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
ff8bfdbb 639
4a46cbe8 640 //Sort of hackish - maybe there is a faster way
aa61d352 641 unsigned int nCount = wxListBox::GetCount();
ff8bfdbb 642
aa61d352 643 for(unsigned int i = 0; i < nCount; ++i)
4a46cbe8
RR
644 {
645 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
8228b893 646 return (int)i;
fd0eed64
RR
647 }
648
dcf40a56 649
4a46cbe8 650 // it's not an error if the string is not found -> no wxCHECK
2ee3ee1b 651 return wxNOT_FOUND;
6de97a3b 652}
c801d85f 653
2ee3ee1b
VZ
654// ----------------------------------------------------------------------------
655// selection
656// ----------------------------------------------------------------------------
657
09e744f5
VZ
658void wxListBox::GTKOnActivated(int item)
659{
660 SendEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, item, IsSelected(item));
661}
662
24ee1bef
VZ
663void wxListBox::GTKOnSelectionChanged()
664{
665 if ( HasFlag(wxLB_MULTIPLE | wxLB_EXTENDED) )
666 {
667 CalcAndSendEvent();
668 }
669 else // single selection
670 {
09e744f5
VZ
671 const int item = GetSelection();
672 if ( DoChangeSingleSelection(item) )
673 SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED, item, true);
24ee1bef
VZ
674 }
675}
676
fd0eed64 677int wxListBox::GetSelection() const
c801d85f 678{
e6e83933
WS
679 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
680 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
4a46cbe8 681 wxT("must be single selection listbox"));
ff8bfdbb 682
caf6e6de 683 GtkTreeIter iter;
4a46cbe8
RR
684 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
685
caf6e6de 686 // only works on single-sel
4a46cbe8 687 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
e6e83933 688 return wxNOT_FOUND;
4a46cbe8 689
dec7b5a8 690 return GTKGetIndexFor(iter);
6de97a3b 691}
c801d85f 692
fd0eed64 693int wxListBox::GetSelections( wxArrayInt& aSelections ) const
c801d85f 694{
e6e83933 695 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
c801d85f 696
fd0eed64
RR
697 aSelections.Empty();
698
05d790f8 699 int i = 0;
caf6e6de 700 GtkTreeIter iter;
4a46cbe8
RR
701 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
702
703 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
704 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
705 do
fd0eed64 706 {
4a46cbe8
RR
707 if (gtk_tree_selection_iter_is_selected(selection, &iter))
708 aSelections.Add(i);
709
710 i++;
711 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
6a6d4eed 712 }
dcf40a56 713
4a46cbe8 714 return aSelections.GetCount();
6de97a3b 715}
c801d85f 716
2ee3ee1b 717bool wxListBox::IsSelected( int n ) const
c801d85f 718{
caf6e6de 719 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
ff8bfdbb 720
4a46cbe8 721 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
9ef6ff8c 722
4a46cbe8 723 GtkTreeIter iter;
dec7b5a8 724 wxCHECK_MSG( GTKGetIteratorFor(n, &iter), false, wxT("Invalid index") );
9ef6ff8c 725
4a46cbe8 726 return gtk_tree_selection_iter_is_selected(selection, &iter);
6de97a3b 727}
c801d85f 728
c6179a84 729void wxListBox::DoSetSelection( int n, bool select )
c801d85f 730{
1e6ffd66
RR
731 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
732
dec7b5a8 733 GTKDisableEvents();
03647350 734
1e6ffd66
RR
735 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
736
ef33382e
VZ
737 // passing -1 to SetSelection() is documented to deselect all items
738 if ( n == wxNOT_FOUND )
739 {
1e6ffd66 740 gtk_tree_selection_unselect_all(selection);
dec7b5a8 741 GTKEnableEvents();
470402b9 742 return;
ef33382e
VZ
743 }
744
745 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
746
03647350 747
4a46cbe8 748 GtkTreeIter iter;
dec7b5a8 749 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("Invalid index") );
ff8bfdbb 750
fd0eed64 751 if (select)
4a46cbe8 752 gtk_tree_selection_select_iter(selection, &iter);
fd0eed64 753 else
4a46cbe8 754 gtk_tree_selection_unselect_iter(selection, &iter);
e4161a2a 755
0b2e06f9
RR
756 GtkTreePath* path = gtk_tree_model_get_path(
757 GTK_TREE_MODEL(m_liststore), &iter);
758
759 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, FALSE, 0.0f, 0.0f);
760
761 gtk_tree_path_free(path);
014b0d06 762
dec7b5a8 763 GTKEnableEvents();
1e6ffd66
RR
764}
765
0d29a482 766void wxListBox::DoScrollToCell(int n, float alignY, float alignX)
c801d85f 767{
4a46cbe8 768 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
8228b893 769 wxCHECK_RET( IsValid(n), wxT("invalid index"));
9ef6ff8c 770
4a46cbe8
RR
771 //RN: I have no idea why this line is needed...
772 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
9ef6ff8c 773 return;
f96b15a3 774
28354d90 775 GtkTreeIter iter;
dec7b5a8 776 if ( !GTKGetIteratorFor(n, &iter) )
0d5f4ba3 777 return;
4a46cbe8 778
28354d90
VZ
779 GtkTreePath* path = gtk_tree_model_get_path(
780 GTK_TREE_MODEL(m_liststore), &iter);
9ef6ff8c 781
28354d90
VZ
782 // Scroll to the desired cell (0.0 == topleft alignment)
783 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
0d29a482 784 TRUE, alignY, alignX);
4a46cbe8 785
28354d90 786 gtk_tree_path_free(path);
6de97a3b 787}
c801d85f 788
0d29a482
VZ
789void wxListBox::DoSetFirstItem(int n)
790{
791 DoScrollToCell(n, 0, 0);
792}
793
794void wxListBox::EnsureVisible(int n)
795{
796 DoScrollToCell(n, 0.5, 0);
797}
798
c00fed0e
VZ
799// ----------------------------------------------------------------------------
800// hittest
801// ----------------------------------------------------------------------------
802
57772185 803int wxListBox::DoListHitTest(const wxPoint& point) const
c00fed0e 804{
a183ddba
VZ
805 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
806 // we only want visible items we need to check for it manually here
22a35096 807 if ( !GetClientRect().Contains(point) )
a183ddba
VZ
808 return wxNOT_FOUND;
809
57772185 810 // need to translate from master window since it is in client coords
c00fed0e 811 gint binx, biny;
caf6e6de 812 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
c00fed0e
VZ
813 &binx, &biny, NULL, NULL, NULL);
814
815 GtkTreePath* path;
57772185
VZ
816 if ( !gtk_tree_view_get_path_at_pos
817 (
caf6e6de 818 m_treeview,
57772185
VZ
819 point.x - binx,
820 point.y - biny,
821 &path,
822 NULL, // [out] column (always 0 here)
823 NULL, // [out] x-coord relative to the cell (not interested)
824 NULL // [out] y-coord relative to the cell
825 ) )
c00fed0e
VZ
826 {
827 return wxNOT_FOUND;
828 }
829
830 int index = gtk_tree_path_get_indices(path)[0];
831 gtk_tree_path_free(path);
832
833 return index;
834}
4a46cbe8 835
2ee3ee1b
VZ
836// ----------------------------------------------------------------------------
837// helpers
838// ----------------------------------------------------------------------------
c801d85f 839
ff8bfdbb 840#if wxUSE_TOOLTIPS
7fc8b9a4 841void wxListBox::GTKApplyToolTip( GtkTooltips *tips, const gchar *tip )
b1170810 842{
85314957
VZ
843#if GTK_CHECK_VERSION(2, 12, 0)
844 if (!gtk_check_version(2, 12, 0))
845 {
846 gtk_widget_set_tooltip_text(GTK_WIDGET( m_treeview ), tip);
847 }
848 else
849#endif
850 {
851 // RN: Is this needed anymore?
852 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), tip, NULL );
853 }
b1170810 854}
ff8bfdbb 855#endif // wxUSE_TOOLTIPS
b1170810 856
fd0eed64 857GtkWidget *wxListBox::GetConnectWidget()
e3e65dac 858{
e5e41cfe
VZ
859 // the correct widget for listbox events (such as mouse clicks for example)
860 // is m_treeview, not the parent scrolled window
861 return GTK_WIDGET(m_treeview);
6de97a3b 862}
e3e65dac 863
1c2b98d6 864GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
868a2826 865{
1c2b98d6 866 return gtk_tree_view_get_bin_window(m_treeview);
868a2826 867}
e3e65dac 868
f40fdaa3 869void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
c058d771 870{
f40fdaa3 871 if (m_hasBgCol && m_backgroundColour.Ok())
e380f72b 872 {
04e044c4 873 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
4a46cbe8 874 if (window)
f03fc89f 875 {
08f60019 876 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
f03fc89f
VZ
877 gdk_window_set_background( window, m_backgroundColour.GetColor() );
878 gdk_window_clear( window );
879 }
e380f72b 880 }
ff8bfdbb 881
4a46cbe8 882 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
68dda785 883}
dcf924a3 884
f68586e5
VZ
885wxSize wxListBox::DoGetBestSize() const
886{
4a46cbe8
RR
887 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
888
cdff1318 889 // Start with a minimum size that's not too small
f96b15a3 890 int cx, cy;
2b5f62a0 891 GetTextExtent( wxT("X"), &cx, &cy);
47179317 892 int lbWidth = 0;
c64371de 893 int lbHeight = 10;
cdff1318 894
47179317
VZ
895 // Find the widest string.
896 const unsigned int count = GetCount();
897 if ( count )
cdff1318
RD
898 {
899 int wLine;
47179317
VZ
900 for ( unsigned int i = 0; i < count; i++ )
901 {
902 GetTextExtent(GetString(i), &wLine, NULL);
903 if ( wLine > lbWidth )
904 lbWidth = wLine;
cdff1318 905 }
47179317 906 }
cdff1318 907
47179317 908 lbWidth += 3 * cx;
cdff1318 909
47179317
VZ
910 // And just a bit more for the checkbox if present and then some
911 // (these are rough guesses)
470402b9 912#if wxUSE_CHECKLISTBOX
47179317
VZ
913 if ( m_hasCheckBoxes )
914 {
915 lbWidth += 35;
916 cy = cy > 25 ? cy : 25; // rough height of checkbox
cdff1318 917 }
47179317 918#endif
caf6e6de 919
cdff1318
RD
920 // Add room for the scrollbar
921 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
f96b15a3 922
47179317
VZ
923 // Don't make the listbox too tall but don't make it too small neither
924 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
925
9f884528 926 wxSize best(lbWidth, lbHeight);
17a1ebd1 927 CacheBestSize(best);
9f884528 928 return best;
f68586e5
VZ
929}
930
9d522606
RD
931// static
932wxVisualAttributes
933wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
934{
4a46cbe8 935 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
9d522606
RD
936}
937
3ae4c570 938#endif // wxUSE_LISTBOX