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