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