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