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