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