]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/listbox.cpp
rebaked after adding new string-related headers
[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 85 GtkTreeEntry* entry = listbox->GtkGetEntry(sel);
f4322df6 86
470402b9 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) );
f4322df6 96
470402b9 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 150 if (g_blockEventsOnDrag) return;
f4322df6 151
470402b9 152 if (listbox->m_blockEvent) return;
f4322df6 153
470402b9
RR
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 );
f4322df6 161
470402b9
RR
162 if (selections.GetCount() == 0)
163 {
164 // indicate that this is a deselection
165 event.SetExtraLong( 0 );
166 event.SetInt( -1 );
f4322df6 167
470402b9 168 listbox->GetEventHandler()->ProcessEvent( event );
f4322df6 169
470402b9
RR
170 return;
171 }
172 else
173 {
174 // indicate that this is a selection
175 event.SetExtraLong( 1 );
176 event.SetInt( selections[0] );
f4322df6 177
470402b9
RR
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 );
f4322df6 189
470402b9 190 listbox->GetEventHandler()->ProcessEvent( event );
f4322df6 191
470402b9
RR
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 337 m_needParent = true;
caf6e6de 338 m_blockEvent = false;
dcf40a56 339
4dcaf11a
RR
340 if (!PreCreation( parent, pos, size ) ||
341 !CreateBase( parent, id, pos, size, style, validator, name ))
342 {
223d09f6 343 wxFAIL_MSG( wxT("wxListBox creation failed") );
caf6e6de 344 return false;
4dcaf11a 345 }
6de97a3b 346
fd0eed64 347 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
034be888
RR
348 if (style & wxLB_ALWAYS_SB)
349 {
350 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
351 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
352 }
353 else
354 {
355 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
356 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
357 }
ff8bfdbb 358
6493aaca
VZ
359
360 GtkScrolledWindowSetBorder(m_widget, style);
361
4a46cbe8
RR
362 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
363
364 //wxListBox doesn't have a header :)
365 //NB: If enabled SetFirstItem doesn't work correctly
366 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
367
470402b9 368#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
369 if(m_hasCheckBoxes)
370 ((wxCheckListBox*)this)->DoCreateCheckList();
470402b9 371#endif // wxUSE_CHECKLISTBOX
4a46cbe8
RR
372
373 // Create the data column
caf6e6de 374 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
4a46cbe8 375 gtk_cell_renderer_text_new(),
caf6e6de 376 "text",
4a46cbe8
RR
377 WXLISTBOX_DATACOLUMN, NULL);
378
379 // Now create+set the model (GtkListStore) - first argument # of columns
470402b9 380#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
381 if(m_hasCheckBoxes)
382 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
383 GTK_TYPE_TREE_ENTRY);
384 else
385#endif
386 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
387
388 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
caf6e6de 389
3fe39b0c 390 g_object_unref (m_liststore); //free on treeview destruction
caf6e6de 391
4a46cbe8
RR
392 // Disable the pop-up textctrl that enables searching - note that
393 // the docs specify that even if this disabled (which we are doing)
394 // the user can still have it through the start-interactive-search
395 // key binding...either way we want to provide a searchequal callback
caf6e6de 396 // NB: If this is enabled a doubleclick event (activate) gets sent
4a46cbe8
RR
397 // on a successful search
398 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
caf6e6de 399 gtk_tree_view_set_search_equal_func(m_treeview,
4a46cbe8
RR
400 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
401 this,
402 NULL);
403
404 gtk_tree_view_set_enable_search(m_treeview, FALSE);
405
406
407 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
f4322df6 408
470402b9
RR
409 g_signal_connect_after (selection, "changed",
410 G_CALLBACK (gtk_listitem_changed_callback), this);
dcf40a56 411
4a82abc8 412 GtkSelectionMode mode;
fd0eed64 413 if (style & wxLB_MULTIPLE)
4a82abc8 414 {
fd0eed64 415 mode = GTK_SELECTION_MULTIPLE;
4a82abc8 416 }
fd0eed64 417 else if (style & wxLB_EXTENDED)
4a82abc8 418 {
fd0eed64 419 mode = GTK_SELECTION_EXTENDED;
4a82abc8
RR
420 }
421 else
422 {
423 // if style was 0 set single mode
424 m_windowStyle |= wxLB_SINGLE;
4fcfa27c 425 mode = GTK_SELECTION_SINGLE;
4a82abc8 426 }
6a6d4eed 427
4a46cbe8
RR
428 gtk_tree_selection_set_mode( selection, mode );
429
470402b9 430 // Handle sortable stuff
4a46cbe8
RR
431 if(style & wxLB_SORT)
432 {
470402b9 433 // Setup sorting in ascending (wx) order
caf6e6de
WS
434 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
435 WXLISTBOX_DATACOLUMN,
4a46cbe8
RR
436 GTK_SORT_ASCENDING);
437
470402b9 438 // Set the sort callback
4a46cbe8
RR
439 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
440 WXLISTBOX_DATACOLUMN,
441 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
442 this, //userdata
443 NULL //"destroy notifier"
444 );
445 }
446
dcf40a56 447
dcd3c79c 448 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
caf6e6de 449
4a46cbe8 450 gtk_widget_show( GTK_WIDGET(m_treeview) );
0bb3fc29 451 m_focusWidget = GTK_WIDGET(m_treeview);
dcf40a56 452
4a46cbe8 453 wxListBox::DoInsertItems(wxArrayString(n, choices), 0); // insert initial items
17a1ebd1 454
470402b9
RR
455 // generate dclick events
456 g_signal_connect_after(m_treeview, "row-activated",
4a46cbe8 457 G_CALLBACK(gtk_listbox_row_activated_callback), this);
2ee3ee1b 458
470402b9 459 // for panel navigation
4a46cbe8
RR
460 g_signal_connect (m_treeview, "key_press_event",
461 G_CALLBACK (gtk_listbox_key_press_callback),
462 this);
dcf40a56 463
f03fc89f 464 m_parent->DoAddChild( this );
ff8bfdbb 465
abdeb9e7 466 PostCreation(size);
170acdc9 467 SetInitialSize(size); // need this too because this is a wxControlWithItems
dcf40a56 468
caf6e6de 469 return true;
6de97a3b 470}
c801d85f 471
fd0eed64 472wxListBox::~wxListBox()
c801d85f 473{
caf6e6de 474 m_hasVMT = false;
4a82abc8 475
caaa4cfd 476 Clear();
6de97a3b 477}
c801d85f 478
8161b5b9
VZ
479// ----------------------------------------------------------------------------
480// adding items
481// ----------------------------------------------------------------------------
482
caf6e6de 483void wxListBox::GtkInsertItems(const wxArrayString& items,
aa61d352 484 void** clientData, unsigned int pos)
bb0ca8df 485{
4a46cbe8 486 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
2ee3ee1b 487
9f884528
RD
488 InvalidateBestSize();
489
4a46cbe8 490 // Create and set column ids and GValues
9ef6ff8c 491
aa61d352
VZ
492 unsigned int nNum = items.GetCount();
493 unsigned int nCurCount = wxListBox::GetCount();
4a46cbe8 494 wxASSERT_MSG(pos <= nCurCount, wxT("Invalid index passed to wxListBox"));
bb0ca8df 495
2eb1512a
MR
496 GtkTreeIter* pIter = NULL; // append by default
497 GtkTreeIter iter;
498 if (pos != nCurCount)
bb0ca8df 499 {
4a46cbe8
RR
500 gboolean res = gtk_tree_model_iter_nth_child(
501 GTK_TREE_MODEL(m_liststore),
502 &iter, NULL, //NULL = parent = get first
8228b893 503 (int)pos );
4a46cbe8 504 if(!res)
0a07a7d8 505 {
4a46cbe8 506 wxLogSysError(wxT("internal wxListBox error in insertion"));
caf6e6de 507 return;
0a07a7d8 508 }
bb0ca8df 509
4a46cbe8 510 pIter = &iter;
bb0ca8df 511 }
2ee3ee1b 512
aa61d352 513 for (unsigned int i = 0; i < nNum; ++i)
2ee3ee1b 514 {
4a46cbe8 515 wxString label = items[i];
ff8bfdbb 516
4a46cbe8 517 GtkTreeEntry* entry = gtk_tree_entry_new();
d723a1ba 518 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
caf6e6de 519 gtk_tree_entry_set_destroy_func(entry,
4a46cbe8 520 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
9fa72bd2 521 this);
dcf40a56 522
4a46cbe8
RR
523 if (clientData)
524 gtk_tree_entry_set_userdata(entry, clientData[i]);
9fa72bd2 525
4a46cbe8
RR
526 GtkTreeIter itercur;
527 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
c9433ea9 528
470402b9 529#if wxUSE_CHECKLISTBOX
4a46cbe8
RR
530 if (m_hasCheckBoxes)
531 {
caf6e6de 532 gtk_list_store_set(m_liststore, &itercur,
4a46cbe8
RR
533 0, FALSE, //FALSE == not toggled
534 1, entry, -1);
535 }
536 else
537#endif
caf6e6de 538 gtk_list_store_set(m_liststore, &itercur,
4a46cbe8 539 0, entry, -1);
fd0eed64 540
3fe39b0c 541 g_object_unref (entry); //liststore always refs :)
4a46cbe8
RR
542 }
543}
17a1ebd1 544
aa61d352 545void wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
4a46cbe8 546{
8228b893
WS
547 wxCHECK_RET( IsValidInsert(pos), wxT("invalid index in wxListBox::InsertItems") );
548
aa61d352 549 GtkInsertItems(items, NULL, pos);
4a46cbe8 550}
014b0d06 551
4a46cbe8
RR
552int wxListBox::DoAppend( const wxString& item )
553{
be4bb86e
RR
554 wxCHECK_MSG( m_treeview != NULL, -1, wxT("invalid listbox") );
555
556 InvalidateBestSize();
557
558 GtkTreeEntry* entry = gtk_tree_entry_new();
559 gtk_tree_entry_set_label( entry, wxGTK_CONV(item) );
560 gtk_tree_entry_set_destroy_func(entry,
561 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
562 this);
563
564 GtkTreeIter itercur;
565 gtk_list_store_insert_before( m_liststore, &itercur, NULL );
566
567#if wxUSE_CHECKLISTBOX
568 if (m_hasCheckBoxes)
569 {
570 gtk_list_store_set( m_liststore, &itercur,
571 0, FALSE, //FALSE == not toggled
572 1, entry, -1);
573 }
574 else
575#endif
576 gtk_list_store_set(m_liststore, &itercur,
577 0, entry, -1);
578
579 g_object_unref (entry); //liststore always refs :)
580
581 GtkTreePath* path = gtk_tree_model_get_path(
582 GTK_TREE_MODEL(m_liststore),
583 &itercur);
584
585 gint* pIntPath = gtk_tree_path_get_indices(path);
586
587 if (pIntPath == NULL)
588 {
589 wxLogSysError(wxT("internal wxListBox error in insertion"));
590 return wxNOT_FOUND;
591 }
592
593 int index = pIntPath[0];
594
595 gtk_tree_path_free( path );
596
597 return index;
fd0eed64
RR
598}
599
2ee3ee1b
VZ
600void wxListBox::DoSetItems( const wxArrayString& items,
601 void **clientData)
fd0eed64 602{
2ee3ee1b 603 Clear();
4a46cbe8 604 GtkInsertItems(items, clientData, 0);
fd0eed64 605}
dcf40a56 606
2ee3ee1b 607// ----------------------------------------------------------------------------
8161b5b9 608// deleting items
2ee3ee1b 609// ----------------------------------------------------------------------------
ff8bfdbb 610
fd0eed64
RR
611void wxListBox::Clear()
612{
4a46cbe8 613 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 614
cdff1318
RD
615 InvalidateBestSize();
616
4a46cbe8 617 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
6de97a3b 618}
c801d85f 619
aa61d352 620void wxListBox::Delete(unsigned int n)
c801d85f 621{
4a46cbe8 622 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 623
cdff1318
RD
624 InvalidateBestSize();
625
4a46cbe8
RR
626 GtkTreeIter iter;
627 gboolean res = gtk_tree_model_iter_nth_child(
628 GTK_TREE_MODEL(m_liststore),
629 &iter, NULL, //NULL = parent = get first
630 n
aa61d352 631 );
dcf40a56 632
4a46cbe8 633 wxCHECK_RET( res, wxT("wrong listbox index") );
dcf40a56 634
4a46cbe8
RR
635 //this returns false if iter is invalid (i.e. deleting item
636 //at end) but since we don't use iter, well... :)
637 gtk_list_store_remove(m_liststore, &iter);
638}
dcf40a56 639
4a46cbe8
RR
640// ----------------------------------------------------------------------------
641// get GtkTreeEntry from position (note: you need to g_unref it if valid)
642// ----------------------------------------------------------------------------
2ee3ee1b 643
4a46cbe8
RR
644struct _GtkTreeEntry* wxListBox::GtkGetEntry(int n) const
645{
646 GtkTreeIter iter;
647 gboolean res = gtk_tree_model_iter_nth_child(
648 GTK_TREE_MODEL(m_liststore),
649 &iter, NULL, //NULL = parent = get first
650 n );
651
652 if (!res)
653 {
654 wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n")
8228b893 655 wxT("Passed in value was:[%i] List size:[%u]"),
4a46cbe8
RR
656 n, wxListBox::GetCount() );
657 return NULL;
f5e27805 658 }
ff8bfdbb 659
4a46cbe8
RR
660
661 GtkTreeEntry* entry = NULL;
662 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore), &iter,
663 WXLISTBOX_DATACOLUMN, &entry, -1);
664
665 return entry;
2ee3ee1b
VZ
666}
667
8161b5b9
VZ
668// ----------------------------------------------------------------------------
669// client data
670// ----------------------------------------------------------------------------
671
aa61d352 672void* wxListBox::DoGetItemClientData(unsigned int n) const
8161b5b9 673{
aa61d352 674 wxCHECK_MSG( IsValid(n), NULL,
4a46cbe8 675 wxT("Invalid index passed to GetItemClientData") );
8161b5b9 676
4a46cbe8
RR
677 GtkTreeEntry* entry = GtkGetEntry(n);
678 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
8161b5b9 679
4a46cbe8 680 void* userdata = gtk_tree_entry_get_userdata( entry );
3fe39b0c 681 g_object_unref (entry);
4a46cbe8 682 return userdata;
8161b5b9
VZ
683}
684
aa61d352 685wxClientData* wxListBox::DoGetItemClientObject(unsigned int n) const
8161b5b9 686{
4a46cbe8 687 return (wxClientData*) wxListBox::DoGetItemClientData(n);
8161b5b9
VZ
688}
689
aa61d352 690void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
8161b5b9 691{
aa61d352 692 wxCHECK_RET( IsValid(n),
4a46cbe8 693 wxT("Invalid index passed to SetItemClientData") );
8161b5b9 694
4a46cbe8
RR
695 GtkTreeEntry* entry = GtkGetEntry(n);
696 wxCHECK_RET(entry, wxT("could not get entry"));
8161b5b9 697
4a46cbe8 698 gtk_tree_entry_set_userdata( entry, clientData );
3fe39b0c 699 g_object_unref (entry);
8161b5b9
VZ
700}
701
aa61d352 702void wxListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
8161b5b9 703{
4a46cbe8
RR
704 // wxItemContainer already deletes data for us
705 wxListBox::DoSetItemClientData(n, (void*) clientData);
8161b5b9
VZ
706}
707
2ee3ee1b
VZ
708// ----------------------------------------------------------------------------
709// string list access
710// ----------------------------------------------------------------------------
711
aa61d352 712void wxListBox::SetString(unsigned int n, const wxString &string)
2ee3ee1b 713{
8228b893 714 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
4a46cbe8 715 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
2ee3ee1b 716
4a46cbe8
RR
717 GtkTreeEntry* entry = GtkGetEntry(n);
718 wxCHECK_RET( entry, wxT("wrong listbox index") );
2ee3ee1b 719
4a46cbe8
RR
720 wxString label = string;
721
caf6e6de 722 // RN: This may look wierd but the problem is that the TreeView
4a46cbe8
RR
723 // doesn't resort or update when changed above and there is no real
724 // notification function...
725 void* userdata = gtk_tree_entry_get_userdata(entry);
726 gtk_tree_entry_set_userdata(entry, NULL); //don't delete on destroy
3fe39b0c 727 g_object_unref (entry);
4a46cbe8
RR
728
729 bool bWasSelected = wxListBox::IsSelected(n);
730 wxListBox::Delete(n);
731
732 wxArrayString aItems;
733 aItems.Add(label);
aa61d352 734 GtkInsertItems(aItems, &userdata, n);
4a46cbe8 735 if (bWasSelected)
caf6e6de 736 wxListBox::GtkSetSelection(n, true, true);
6de97a3b 737}
c801d85f 738
aa61d352 739wxString wxListBox::GetString(unsigned int n) const
c801d85f 740{
4a46cbe8 741 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
fc54776e 742
4a46cbe8
RR
743 GtkTreeEntry* entry = GtkGetEntry(n);
744 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
745
746 wxString label = wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
2ee3ee1b 747
3fe39b0c 748 g_object_unref (entry);
4a46cbe8 749 return label;
2ee3ee1b
VZ
750}
751
aa61d352 752unsigned int wxListBox::GetCount() const
2ee3ee1b 753{
8228b893 754 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
2ee3ee1b 755
aa61d352 756 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
6de97a3b 757}
c801d85f 758
11e62fe6 759int wxListBox::FindString( const wxString &item, bool bCase ) const
c801d85f 760{
4a46cbe8 761 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
ff8bfdbb 762
4a46cbe8 763 //Sort of hackish - maybe there is a faster way
aa61d352 764 unsigned int nCount = wxListBox::GetCount();
ff8bfdbb 765
aa61d352 766 for(unsigned int i = 0; i < nCount; ++i)
4a46cbe8
RR
767 {
768 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
8228b893 769 return (int)i;
fd0eed64
RR
770 }
771
dcf40a56 772
4a46cbe8 773 // it's not an error if the string is not found -> no wxCHECK
2ee3ee1b 774 return wxNOT_FOUND;
6de97a3b 775}
c801d85f 776
2ee3ee1b
VZ
777// ----------------------------------------------------------------------------
778// selection
779// ----------------------------------------------------------------------------
780
fd0eed64 781int wxListBox::GetSelection() const
c801d85f 782{
e6e83933
WS
783 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
784 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
4a46cbe8 785 wxT("must be single selection listbox"));
ff8bfdbb 786
caf6e6de 787 GtkTreeIter iter;
4a46cbe8
RR
788 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
789
caf6e6de 790 // only works on single-sel
4a46cbe8 791 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
e6e83933 792 return wxNOT_FOUND;
4a46cbe8
RR
793
794 GtkTreePath* path =
795 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
796
797 int sel = gtk_tree_path_get_indices(path)[0];
798
799 gtk_tree_path_free(path);
800
801 return sel;
6de97a3b 802}
c801d85f 803
fd0eed64 804int wxListBox::GetSelections( wxArrayInt& aSelections ) const
c801d85f 805{
e6e83933 806 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
c801d85f 807
fd0eed64
RR
808 aSelections.Empty();
809
fd0eed64 810 int i = 0;
caf6e6de 811 GtkTreeIter iter;
4a46cbe8
RR
812 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
813
814 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
815 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
816 do
fd0eed64 817 {
4a46cbe8
RR
818 if (gtk_tree_selection_iter_is_selected(selection, &iter))
819 aSelections.Add(i);
820
821 i++;
822 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
6a6d4eed 823 }
dcf40a56 824
4a46cbe8 825 return aSelections.GetCount();
6de97a3b 826}
c801d85f 827
2ee3ee1b 828bool wxListBox::IsSelected( int n ) const
c801d85f 829{
caf6e6de 830 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
ff8bfdbb 831
4a46cbe8 832 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
9ef6ff8c 833
4a46cbe8
RR
834 GtkTreeIter iter;
835 gboolean res = gtk_tree_model_iter_nth_child(
836 GTK_TREE_MODEL(m_liststore),
837 &iter, NULL, //NULL = parent = get first
838 n );
caf6e6de
WS
839
840 wxCHECK_MSG( res, false, wxT("Invalid index") );
9ef6ff8c 841
4a46cbe8 842 return gtk_tree_selection_iter_is_selected(selection, &iter);
6de97a3b 843}
c801d85f 844
c6179a84 845void wxListBox::DoSetSelection( int n, bool select )
c801d85f 846{
ef33382e
VZ
847 // passing -1 to SetSelection() is documented to deselect all items
848 if ( n == wxNOT_FOUND )
849 {
850 // ... and not generate any events in the process
851 GtkDeselectAll();
470402b9 852 return;
ef33382e
VZ
853 }
854
855 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
856
857 // don't generate the selection event
858 GtkSetSelection(n, select, true);
859}
860
861void wxListBox::GtkDeselectAll()
862{
863 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
864
865 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
866
867 m_blockEvent = true;
868
869 gtk_tree_selection_unselect_all(selection);
870
871 m_blockEvent = false;
4a46cbe8
RR
872}
873
874void wxListBox::GtkSetSelection(int n, const bool select, const bool blockEvent)
875{
876 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
877
878 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
879
880 GtkTreeIter iter;
881 gboolean res = gtk_tree_model_iter_nth_child(
882 GTK_TREE_MODEL(m_liststore),
883 &iter, NULL, //NULL = parent = get first
884 n
885 );
886 wxCHECK_RET( res, wxT("Invalid index") );
ff8bfdbb 887
4a46cbe8 888 m_blockEvent = blockEvent;
953704c1 889
fd0eed64 890 if (select)
4a46cbe8 891 gtk_tree_selection_select_iter(selection, &iter);
fd0eed64 892 else
4a46cbe8 893 gtk_tree_selection_unselect_iter(selection, &iter);
014b0d06 894
caf6e6de 895 m_blockEvent = false;
6de97a3b 896}
c801d85f 897
9ef6ff8c 898void wxListBox::DoSetFirstItem( int n )
c801d85f 899{
4a46cbe8 900 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
8228b893 901 wxCHECK_RET( IsValid(n), wxT("invalid index"));
9ef6ff8c 902
4a46cbe8
RR
903 //RN: I have no idea why this line is needed...
904 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
9ef6ff8c 905 return;
f96b15a3 906
28354d90
VZ
907 GtkTreeIter iter;
908 gtk_tree_model_iter_nth_child(
909 GTK_TREE_MODEL(m_liststore),
910 &iter,
911 NULL, //NULL = parent = get first
912 n
913 );
4a46cbe8 914
28354d90
VZ
915 GtkTreePath* path = gtk_tree_model_get_path(
916 GTK_TREE_MODEL(m_liststore), &iter);
9ef6ff8c 917
28354d90
VZ
918 // Scroll to the desired cell (0.0 == topleft alignment)
919 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
920 TRUE, 0.0f, 0.0f);
4a46cbe8 921
28354d90 922 gtk_tree_path_free(path);
6de97a3b 923}
c801d85f 924
c00fed0e
VZ
925// ----------------------------------------------------------------------------
926// hittest
927// ----------------------------------------------------------------------------
928
57772185 929int wxListBox::DoListHitTest(const wxPoint& point) const
c00fed0e 930{
a183ddba
VZ
931 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
932 // we only want visible items we need to check for it manually here
22a35096 933 if ( !GetClientRect().Contains(point) )
a183ddba
VZ
934 return wxNOT_FOUND;
935
57772185 936 // need to translate from master window since it is in client coords
c00fed0e 937 gint binx, biny;
caf6e6de 938 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
c00fed0e
VZ
939 &binx, &biny, NULL, NULL, NULL);
940
941 GtkTreePath* path;
57772185
VZ
942 if ( !gtk_tree_view_get_path_at_pos
943 (
caf6e6de 944 m_treeview,
57772185
VZ
945 point.x - binx,
946 point.y - biny,
947 &path,
948 NULL, // [out] column (always 0 here)
949 NULL, // [out] x-coord relative to the cell (not interested)
950 NULL // [out] y-coord relative to the cell
951 ) )
c00fed0e
VZ
952 {
953 return wxNOT_FOUND;
954 }
955
956 int index = gtk_tree_path_get_indices(path)[0];
957 gtk_tree_path_free(path);
958
959 return index;
960}
4a46cbe8 961
2ee3ee1b
VZ
962// ----------------------------------------------------------------------------
963// helpers
964// ----------------------------------------------------------------------------
c801d85f 965
ff8bfdbb 966#if wxUSE_TOOLTIPS
b5be07d4 967void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
b1170810 968{
4a46cbe8
RR
969 // RN: Is this needed anymore?
970 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), wxGTK_CONV(tip), (gchar*) NULL );
b1170810 971}
ff8bfdbb 972#endif // wxUSE_TOOLTIPS
b1170810 973
fd0eed64 974GtkWidget *wxListBox::GetConnectWidget()
e3e65dac 975{
e5e41cfe
VZ
976 // the correct widget for listbox events (such as mouse clicks for example)
977 // is m_treeview, not the parent scrolled window
978 return GTK_WIDGET(m_treeview);
6de97a3b 979}
e3e65dac 980
1c2b98d6 981GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
868a2826 982{
1c2b98d6 983 return gtk_tree_view_get_bin_window(m_treeview);
868a2826 984}
e3e65dac 985
f40fdaa3 986void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
c058d771 987{
f40fdaa3 988 if (m_hasBgCol && m_backgroundColour.Ok())
e380f72b 989 {
04e044c4 990 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
4a46cbe8 991 if (window)
f03fc89f 992 {
08f60019 993 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
f03fc89f
VZ
994 gdk_window_set_background( window, m_backgroundColour.GetColor() );
995 gdk_window_clear( window );
996 }
e380f72b 997 }
ff8bfdbb 998
4a46cbe8 999 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
68dda785 1000}
dcf924a3 1001
f68586e5
VZ
1002wxSize wxListBox::DoGetBestSize() const
1003{
4a46cbe8
RR
1004 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
1005
cdff1318 1006 // Start with a minimum size that's not too small
f96b15a3 1007 int cx, cy;
2b5f62a0 1008 GetTextExtent( wxT("X"), &cx, &cy);
c64371de
RD
1009 int lbWidth = 3 * cx;
1010 int lbHeight = 10;
cdff1318 1011
caf6e6de 1012 // Get the visible area of the tree view (limit to the 10th item
cdff1318 1013 // so that it isn't too big)
aa61d352 1014 unsigned int count = GetCount();
cdff1318
RD
1015 if (count)
1016 {
1017 int wLine;
1018
1019 // Find the widest line
aa61d352 1020 for(unsigned int i = 0; i < count; i++) {
cdff1318
RD
1021 wxString str(GetString(i));
1022 GetTextExtent(str, &wLine, NULL);
1023 lbWidth = wxMax(lbWidth, wLine);
1024 }
1025
1026 lbWidth += 3 * cx;
1027
1028 // And just a bit more for the checkbox if present and then some
1029 // (these are rough guesses)
470402b9 1030#if wxUSE_CHECKLISTBOX
cdff1318
RD
1031 if ( m_hasCheckBoxes )
1032 {
1033 lbWidth += 35;
1034 cy = cy > 25 ? cy : 25; // rough height of checkbox
1035 }
1036#endif
f96b15a3 1037
aa61d352
VZ
1038 // don't make the listbox too tall (limit height to around 10 items) but don't
1039 // make it too small neither
cdff1318
RD
1040 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
1041 }
caf6e6de 1042
cdff1318
RD
1043 // Add room for the scrollbar
1044 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
f96b15a3 1045
9f884528 1046 wxSize best(lbWidth, lbHeight);
17a1ebd1 1047 CacheBestSize(best);
9f884528 1048 return best;
f68586e5
VZ
1049}
1050
9d522606
RD
1051// static
1052wxVisualAttributes
1053wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1054{
4a46cbe8 1055 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
9d522606
RD
1056}
1057
3ae4c570 1058#endif // wxUSE_LISTBOX