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