modify the listbox item in place instead of deleting and inserting it back in SetStri...
[wxWidgets.git] / src / gtk / listbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/listbox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Modified By: Ryan Norton (GtkTreeView implementation)
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #if wxUSE_LISTBOX
15
16 #include "wx/listbox.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/dynarray.h"
20 #include "wx/intl.h"
21 #include "wx/log.h"
22 #include "wx/utils.h"
23 #include "wx/settings.h"
24 #include "wx/checklst.h"
25 #include "wx/arrstr.h"
26 #endif
27
28 #include "wx/gtk/private.h"
29 #include "wx/gtk/treeentry_gtk.h"
30
31 #if wxUSE_TOOLTIPS
32 #include "wx/tooltip.h"
33 #endif
34
35 #include <gdk/gdk.h>
36 #include <gtk/gtk.h>
37 #include <gdk/gdkkeysyms.h>
38
39 //-----------------------------------------------------------------------------
40 // data
41 //-----------------------------------------------------------------------------
42
43 extern bool g_blockEventsOnDrag;
44 extern bool g_blockEventsOnScroll;
45
46
47
48 //-----------------------------------------------------------------------------
49 // Macro to tell which row the strings are in (1 if native checklist, 0 if not)
50 //-----------------------------------------------------------------------------
51
52 #if wxUSE_CHECKLISTBOX
53 # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
54 #else
55 # define WXLISTBOX_DATACOLUMN_ARG(x) (0)
56 #endif // wxUSE_CHECKLISTBOX
57
58 #define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
59
60 //-----------------------------------------------------------------------------
61 // "row-activated"
62 //-----------------------------------------------------------------------------
63
64 extern "C" {
65 static void
66 gtk_listbox_row_activated_callback(GtkTreeView *treeview,
67 GtkTreePath *path,
68 GtkTreeViewColumn *col,
69 wxListBox *listbox)
70 {
71 if (g_isIdle) wxapp_install_idle_handler();
72
73 if (g_blockEventsOnDrag) return;
74 if (g_blockEventsOnScroll) return;
75
76 // This is triggered by either a double-click or a space press
77
78 int sel = gtk_tree_path_get_indices(path)[0];
79
80 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
81 event.SetEventObject( listbox );
82
83 if (listbox->IsSelected(sel))
84 {
85 GtkTreeEntry* entry = listbox->GtkGetEntry(sel);
86
87 if (entry)
88 {
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);
98 }
99 else
100 {
101 wxLogSysError(wxT("Internal error - could not get entry for double-click"));
102 event.SetInt(-1);
103 }
104 }
105 else
106 {
107 event.SetInt(-1);
108 }
109
110 listbox->GetEventHandler()->ProcessEvent( event );
111 }
112 }
113
114 //-----------------------------------------------------------------------------
115 // "key_press_event"
116 //-----------------------------------------------------------------------------
117
118 extern "C" {
119 static gint
120 gtk_listbox_key_press_callback( GtkWidget *widget,
121 GdkEventKey *gdk_event,
122 wxListBox *listbox )
123 {
124 if (g_blockEventsOnDrag) return FALSE;
125
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 );
134 if (listbox->GetEventHandler()->ProcessEvent( new_event ))
135 return TRUE;
136 }
137
138 return FALSE;
139 }
140 }
141
142 //-----------------------------------------------------------------------------
143 // "changed"
144 //-----------------------------------------------------------------------------
145
146 extern "C" {
147 static void
148 gtk_listitem_changed_callback( GtkTreeSelection* selection, wxListBox *listbox )
149 {
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 );
156
157 if (listbox->HasFlag(wxLB_MULTIPLE) || listbox->HasFlag(wxLB_EXTENDED))
158 {
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 );
197
198 // indicate that this is a selection
199 event.SetExtraLong( 1 );
200
201 event.SetInt( index );
202 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
203
204 if ( listbox->HasClientObjectData() )
205 event.SetClientObject(
206 (wxClientData*) gtk_tree_entry_get_userdata(entry)
207 );
208 else if ( listbox->HasClientUntypedData() )
209 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
210
211 listbox->GetEventHandler()->ProcessEvent( event );
212
213 g_object_unref (entry);
214 }
215 }
216 }
217 }
218
219 //-----------------------------------------------------------------------------
220 // GtkTreeEntry destruction (to destroy client data)
221 //-----------------------------------------------------------------------------
222
223 extern "C" {
224 static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry,
225 wxListBox* listbox)
226 {
227 if (listbox->HasClientObjectData())
228 {
229 gpointer userdata = gtk_tree_entry_get_userdata(entry);
230 if (userdata)
231 delete (wxClientData *)userdata;
232 }
233 }
234 }
235
236 //-----------------------------------------------------------------------------
237 // Sorting callback (standard CmpNoCase return value)
238 //-----------------------------------------------------------------------------
239
240 extern "C" {
241 static gint gtk_listbox_sort_callback(GtkTreeModel *model,
242 GtkTreeIter *a,
243 GtkTreeIter *b,
244 wxListBox *listbox)
245 {
246 GtkTreeEntry* entry;
247 GtkTreeEntry* entry2;
248
249 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
250 a,
251 WXLISTBOX_DATACOLUMN_ARG(listbox),
252 &entry, -1);
253 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
254 b,
255 WXLISTBOX_DATACOLUMN_ARG(listbox),
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
265 g_object_unref (entry);
266 g_object_unref (entry2);
267
268 return ret;
269 }
270 }
271
272 //-----------------------------------------------------------------------------
273 // Searching callback (TRUE == not equal, FALSE == equal)
274 //-----------------------------------------------------------------------------
275
276 extern "C" {
277 static gboolean gtk_listbox_searchequal_callback(GtkTreeModel* model,
278 gint column,
279 const gchar* key,
280 GtkTreeIter* iter,
281 wxListBox* listbox)
282 {
283 GtkTreeEntry* entry;
284
285 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
286 iter,
287 WXLISTBOX_DATACOLUMN_ARG(listbox),
288 &entry, -1);
289 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
290 wxGtkString keycollatekey(g_utf8_collate_key(key, -1));
291
292 int ret = strcasecmp(keycollatekey,
293 gtk_tree_entry_get_collate_key(entry));
294
295 g_object_unref (entry);
296
297 return ret != 0;
298 }
299 }
300
301 //-----------------------------------------------------------------------------
302 // wxListBox
303 //-----------------------------------------------------------------------------
304
305 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
306
307 // ----------------------------------------------------------------------------
308 // construction
309 // ----------------------------------------------------------------------------
310
311 void wxListBox::Init()
312 {
313 m_treeview = (GtkTreeView*) NULL;
314 #if wxUSE_CHECKLISTBOX
315 m_hasCheckBoxes = false;
316 #endif // wxUSE_CHECKLISTBOX
317 }
318
319 bool 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
331 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
332 const wxPoint &pos, const wxSize &size,
333 int n, const wxString choices[],
334 long style, const wxValidator& validator,
335 const wxString &name )
336 {
337 m_needParent = true;
338 m_blockEvent = false;
339
340 if (!PreCreation( parent, pos, size ) ||
341 !CreateBase( parent, id, pos, size, style, validator, name ))
342 {
343 wxFAIL_MSG( wxT("wxListBox creation failed") );
344 return false;
345 }
346
347 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
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 }
358
359
360 GtkScrolledWindowSetBorder(m_widget, style);
361
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
368 #if wxUSE_CHECKLISTBOX
369 if(m_hasCheckBoxes)
370 ((wxCheckListBox*)this)->DoCreateCheckList();
371 #endif // wxUSE_CHECKLISTBOX
372
373 // Create the data column
374 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
375 gtk_cell_renderer_text_new(),
376 "text",
377 WXLISTBOX_DATACOLUMN, NULL);
378
379 // Now create+set the model (GtkListStore) - first argument # of columns
380 #if wxUSE_CHECKLISTBOX
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));
389
390 g_object_unref (m_liststore); //free on treeview destruction
391
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
396 // NB: If this is enabled a doubleclick event (activate) gets sent
397 // on a successful search
398 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
399 gtk_tree_view_set_search_equal_func(m_treeview,
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 );
408
409 g_signal_connect_after (selection, "changed",
410 G_CALLBACK (gtk_listitem_changed_callback), this);
411
412 GtkSelectionMode mode;
413 if (style & wxLB_MULTIPLE)
414 {
415 mode = GTK_SELECTION_MULTIPLE;
416 }
417 else if (style & wxLB_EXTENDED)
418 {
419 mode = GTK_SELECTION_EXTENDED;
420 }
421 else
422 {
423 // if style was 0 set single mode
424 m_windowStyle |= wxLB_SINGLE;
425 mode = GTK_SELECTION_SINGLE;
426 }
427
428 gtk_tree_selection_set_mode( selection, mode );
429
430 // Handle sortable stuff
431 if(style & wxLB_SORT)
432 {
433 // Setup sorting in ascending (wx) order
434 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
435 WXLISTBOX_DATACOLUMN,
436 GTK_SORT_ASCENDING);
437
438 // Set the sort callback
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
447
448 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
449
450 gtk_widget_show( GTK_WIDGET(m_treeview) );
451 m_focusWidget = GTK_WIDGET(m_treeview);
452
453 wxListBox::DoInsertItems(wxArrayString(n, choices), 0); // insert initial items
454
455 // generate dclick events
456 g_signal_connect_after(m_treeview, "row-activated",
457 G_CALLBACK(gtk_listbox_row_activated_callback), this);
458
459 // for panel navigation
460 g_signal_connect (m_treeview, "key_press_event",
461 G_CALLBACK (gtk_listbox_key_press_callback),
462 this);
463
464 m_parent->DoAddChild( this );
465
466 PostCreation(size);
467 SetInitialSize(size); // need this too because this is a wxControlWithItems
468
469 return true;
470 }
471
472 wxListBox::~wxListBox()
473 {
474 m_hasVMT = false;
475
476 Clear();
477 }
478
479 // ----------------------------------------------------------------------------
480 // adding items
481 // ----------------------------------------------------------------------------
482
483 void wxListBox::GtkInsertItems(const wxArrayString& items,
484 void** clientData, unsigned int pos)
485 {
486 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
487
488 InvalidateBestSize();
489
490 // Create and set column ids and GValues
491
492 unsigned int nNum = items.GetCount();
493 unsigned int nCurCount = wxListBox::GetCount();
494 wxASSERT_MSG(pos <= nCurCount, wxT("Invalid index passed to wxListBox"));
495
496 GtkTreeIter* pIter = NULL; // append by default
497 GtkTreeIter iter;
498 if (pos != nCurCount)
499 {
500 gboolean res = gtk_tree_model_iter_nth_child(
501 GTK_TREE_MODEL(m_liststore),
502 &iter, NULL, //NULL = parent = get first
503 (int)pos );
504 if(!res)
505 {
506 wxLogSysError(wxT("internal wxListBox error in insertion"));
507 return;
508 }
509
510 pIter = &iter;
511 }
512
513 for (unsigned int i = 0; i < nNum; ++i)
514 {
515 wxString label = items[i];
516
517 GtkTreeEntry* entry = gtk_tree_entry_new();
518 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
519 gtk_tree_entry_set_destroy_func(entry,
520 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
521 this);
522
523 if (clientData)
524 gtk_tree_entry_set_userdata(entry, clientData[i]);
525
526 GtkTreeIter itercur;
527 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
528
529 #if wxUSE_CHECKLISTBOX
530 if (m_hasCheckBoxes)
531 {
532 gtk_list_store_set(m_liststore, &itercur,
533 0, FALSE, //FALSE == not toggled
534 1, entry, -1);
535 }
536 else
537 #endif
538 gtk_list_store_set(m_liststore, &itercur,
539 0, entry, -1);
540
541 g_object_unref (entry); //liststore always refs :)
542 }
543 }
544
545 void wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
546 {
547 wxCHECK_RET( IsValidInsert(pos), wxT("invalid index in wxListBox::InsertItems") );
548
549 GtkInsertItems(items, NULL, pos);
550 }
551
552 int wxListBox::DoAppend( const wxString& item )
553 {
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;
598 }
599
600 void wxListBox::DoSetItems( const wxArrayString& items,
601 void **clientData)
602 {
603 Clear();
604 GtkInsertItems(items, clientData, 0);
605 }
606
607 // ----------------------------------------------------------------------------
608 // deleting items
609 // ----------------------------------------------------------------------------
610
611 void wxListBox::Clear()
612 {
613 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
614
615 InvalidateBestSize();
616
617 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
618 }
619
620 void wxListBox::Delete(unsigned int n)
621 {
622 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
623
624 InvalidateBestSize();
625
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
631 );
632
633 wxCHECK_RET( res, wxT("wrong listbox index") );
634
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 }
639
640 // ----------------------------------------------------------------------------
641 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
642 // ----------------------------------------------------------------------------
643
644 struct _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")
655 wxT("Passed in value was:[%i] List size:[%u]"),
656 n, wxListBox::GetCount() );
657 return NULL;
658 }
659
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;
666 }
667
668 // ----------------------------------------------------------------------------
669 // client data
670 // ----------------------------------------------------------------------------
671
672 void* wxListBox::DoGetItemClientData(unsigned int n) const
673 {
674 wxCHECK_MSG( IsValid(n), NULL,
675 wxT("Invalid index passed to GetItemClientData") );
676
677 GtkTreeEntry* entry = GtkGetEntry(n);
678 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
679
680 void* userdata = gtk_tree_entry_get_userdata( entry );
681 g_object_unref (entry);
682 return userdata;
683 }
684
685 wxClientData* wxListBox::DoGetItemClientObject(unsigned int n) const
686 {
687 return (wxClientData*) wxListBox::DoGetItemClientData(n);
688 }
689
690 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
691 {
692 wxCHECK_RET( IsValid(n),
693 wxT("Invalid index passed to SetItemClientData") );
694
695 GtkTreeEntry* entry = GtkGetEntry(n);
696 wxCHECK_RET(entry, wxT("could not get entry"));
697
698 gtk_tree_entry_set_userdata( entry, clientData );
699 g_object_unref (entry);
700 }
701
702 void wxListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
703 {
704 // wxItemContainer already deletes data for us
705 wxListBox::DoSetItemClientData(n, (void*) clientData);
706 }
707
708 // ----------------------------------------------------------------------------
709 // string list access
710 // ----------------------------------------------------------------------------
711
712 void wxListBox::SetString(unsigned int n, const wxString& label)
713 {
714 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
715 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
716
717 GtkTreeEntry* entry = GtkGetEntry(n);
718 wxCHECK_RET( entry, wxT("wrong listbox index") );
719
720 // update the item itself
721 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
722
723 // and update the model which will refresh the tree too
724 GtkTreeIter iter;
725 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
726 &iter, NULL, n) )
727 {
728 wxFAIL_MSG( wxT("failed to get iterator") );
729 return;
730 }
731
732 #if wxUSE_CHECKLISTBOX
733 if (m_hasCheckBoxes)
734 {
735 gtk_list_store_set(m_liststore, &iter,
736 0, FALSE, //FALSE == not toggled
737 1, entry, -1);
738 }
739 else
740 #endif
741 gtk_list_store_set(m_liststore, &iter, 0, entry, -1);
742 }
743
744 wxString wxListBox::GetString(unsigned int n) const
745 {
746 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
747
748 GtkTreeEntry* entry = GtkGetEntry(n);
749 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
750
751 wxString label = wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
752
753 g_object_unref (entry);
754 return label;
755 }
756
757 unsigned int wxListBox::GetCount() const
758 {
759 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
760
761 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
762 }
763
764 int wxListBox::FindString( const wxString &item, bool bCase ) const
765 {
766 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
767
768 //Sort of hackish - maybe there is a faster way
769 unsigned int nCount = wxListBox::GetCount();
770
771 for(unsigned int i = 0; i < nCount; ++i)
772 {
773 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
774 return (int)i;
775 }
776
777
778 // it's not an error if the string is not found -> no wxCHECK
779 return wxNOT_FOUND;
780 }
781
782 // ----------------------------------------------------------------------------
783 // selection
784 // ----------------------------------------------------------------------------
785
786 int wxListBox::GetSelection() const
787 {
788 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
789 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
790 wxT("must be single selection listbox"));
791
792 GtkTreeIter iter;
793 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
794
795 // only works on single-sel
796 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
797 return wxNOT_FOUND;
798
799 GtkTreePath* path =
800 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
801
802 int sel = gtk_tree_path_get_indices(path)[0];
803
804 gtk_tree_path_free(path);
805
806 return sel;
807 }
808
809 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
810 {
811 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
812
813 aSelections.Empty();
814
815 int i = 0;
816 GtkTreeIter iter;
817 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
818
819 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
820 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
821 do
822 {
823 if (gtk_tree_selection_iter_is_selected(selection, &iter))
824 aSelections.Add(i);
825
826 i++;
827 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
828 }
829
830 return aSelections.GetCount();
831 }
832
833 bool wxListBox::IsSelected( int n ) const
834 {
835 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
836
837 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
838
839 GtkTreeIter iter;
840 gboolean res = gtk_tree_model_iter_nth_child(
841 GTK_TREE_MODEL(m_liststore),
842 &iter, NULL, //NULL = parent = get first
843 n );
844
845 wxCHECK_MSG( res, false, wxT("Invalid index") );
846
847 return gtk_tree_selection_iter_is_selected(selection, &iter);
848 }
849
850 void wxListBox::DoSetSelection( int n, bool select )
851 {
852 // passing -1 to SetSelection() is documented to deselect all items
853 if ( n == wxNOT_FOUND )
854 {
855 // ... and not generate any events in the process
856 GtkDeselectAll();
857 return;
858 }
859
860 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
861
862 // don't generate the selection event
863 GtkSetSelection(n, select, true);
864 }
865
866 void wxListBox::GtkDeselectAll()
867 {
868 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
869
870 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
871
872 m_blockEvent = true;
873
874 gtk_tree_selection_unselect_all(selection);
875
876 m_blockEvent = false;
877 }
878
879 void wxListBox::GtkSetSelection(int n, const bool select, const bool blockEvent)
880 {
881 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
882
883 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
884
885 GtkTreeIter iter;
886 gboolean res = gtk_tree_model_iter_nth_child(
887 GTK_TREE_MODEL(m_liststore),
888 &iter, NULL, //NULL = parent = get first
889 n
890 );
891 wxCHECK_RET( res, wxT("Invalid index") );
892
893 m_blockEvent = blockEvent;
894
895 if (select)
896 gtk_tree_selection_select_iter(selection, &iter);
897 else
898 gtk_tree_selection_unselect_iter(selection, &iter);
899
900 m_blockEvent = false;
901 }
902
903 void wxListBox::DoSetFirstItem( int n )
904 {
905 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
906 wxCHECK_RET( IsValid(n), wxT("invalid index"));
907
908 //RN: I have no idea why this line is needed...
909 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
910 return;
911
912 GtkTreeIter iter;
913 gtk_tree_model_iter_nth_child(
914 GTK_TREE_MODEL(m_liststore),
915 &iter,
916 NULL, //NULL = parent = get first
917 n
918 );
919
920 GtkTreePath* path = gtk_tree_model_get_path(
921 GTK_TREE_MODEL(m_liststore), &iter);
922
923 // Scroll to the desired cell (0.0 == topleft alignment)
924 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
925 TRUE, 0.0f, 0.0f);
926
927 gtk_tree_path_free(path);
928 }
929
930 // ----------------------------------------------------------------------------
931 // hittest
932 // ----------------------------------------------------------------------------
933
934 int wxListBox::DoListHitTest(const wxPoint& point) const
935 {
936 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
937 // we only want visible items we need to check for it manually here
938 if ( !GetClientRect().Contains(point) )
939 return wxNOT_FOUND;
940
941 // need to translate from master window since it is in client coords
942 gint binx, biny;
943 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
944 &binx, &biny, NULL, NULL, NULL);
945
946 GtkTreePath* path;
947 if ( !gtk_tree_view_get_path_at_pos
948 (
949 m_treeview,
950 point.x - binx,
951 point.y - biny,
952 &path,
953 NULL, // [out] column (always 0 here)
954 NULL, // [out] x-coord relative to the cell (not interested)
955 NULL // [out] y-coord relative to the cell
956 ) )
957 {
958 return wxNOT_FOUND;
959 }
960
961 int index = gtk_tree_path_get_indices(path)[0];
962 gtk_tree_path_free(path);
963
964 return index;
965 }
966
967 // ----------------------------------------------------------------------------
968 // helpers
969 // ----------------------------------------------------------------------------
970
971 #if wxUSE_TOOLTIPS
972 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
973 {
974 // RN: Is this needed anymore?
975 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), wxGTK_CONV(tip), (gchar*) NULL );
976 }
977 #endif // wxUSE_TOOLTIPS
978
979 GtkWidget *wxListBox::GetConnectWidget()
980 {
981 // the correct widget for listbox events (such as mouse clicks for example)
982 // is m_treeview, not the parent scrolled window
983 return GTK_WIDGET(m_treeview);
984 }
985
986 GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
987 {
988 return gtk_tree_view_get_bin_window(m_treeview);
989 }
990
991 void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
992 {
993 if (m_hasBgCol && m_backgroundColour.Ok())
994 {
995 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
996 if (window)
997 {
998 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
999 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1000 gdk_window_clear( window );
1001 }
1002 }
1003
1004 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
1005 }
1006
1007 wxSize wxListBox::DoGetBestSize() const
1008 {
1009 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
1010
1011 // Start with a minimum size that's not too small
1012 int cx, cy;
1013 GetTextExtent( wxT("X"), &cx, &cy);
1014 int lbWidth = 3 * cx;
1015 int lbHeight = 10;
1016
1017 // Get the visible area of the tree view (limit to the 10th item
1018 // so that it isn't too big)
1019 unsigned int count = GetCount();
1020 if (count)
1021 {
1022 int wLine;
1023
1024 // Find the widest line
1025 for(unsigned int i = 0; i < count; i++) {
1026 wxString str(GetString(i));
1027 GetTextExtent(str, &wLine, NULL);
1028 lbWidth = wxMax(lbWidth, wLine);
1029 }
1030
1031 lbWidth += 3 * cx;
1032
1033 // And just a bit more for the checkbox if present and then some
1034 // (these are rough guesses)
1035 #if wxUSE_CHECKLISTBOX
1036 if ( m_hasCheckBoxes )
1037 {
1038 lbWidth += 35;
1039 cy = cy > 25 ? cy : 25; // rough height of checkbox
1040 }
1041 #endif
1042
1043 // don't make the listbox too tall (limit height to around 10 items) but don't
1044 // make it too small neither
1045 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
1046 }
1047
1048 // Add room for the scrollbar
1049 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1050
1051 wxSize best(lbWidth, lbHeight);
1052 CacheBestSize(best);
1053 return best;
1054 }
1055
1056 // static
1057 wxVisualAttributes
1058 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1059 {
1060 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
1061 }
1062
1063 #endif // wxUSE_LISTBOX