Include wx/arrstr.h according to precompiled headers of wx/wx.h (with other minor...
[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 extern wxCursor g_globalCursor;
46
47
48
49 //-----------------------------------------------------------------------------
50 // Macro to tell which row the strings are in (1 if native checklist, 0 if not)
51 //-----------------------------------------------------------------------------
52
53 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
54 # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
55 #else
56 # define WXLISTBOX_DATACOLUMN_ARG(x) (0)
57 #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
58
59 #define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
60
61 //-----------------------------------------------------------------------------
62 // "row-activated"
63 //-----------------------------------------------------------------------------
64
65 extern "C" {
66 static void
67 gtk_listbox_row_activated_callback(GtkTreeView *treeview,
68 GtkTreePath *path,
69 GtkTreeViewColumn *col,
70 wxListBox *listbox)
71 {
72 if (g_isIdle) wxapp_install_idle_handler();
73
74 if (g_blockEventsOnDrag) return;
75 if (g_blockEventsOnScroll) return;
76
77 if (!listbox->m_hasVMT) return;
78
79 //Notes:
80 //1) This is triggered by either a double-click or a space press
81 //2) We handle both here because
82 //2a) in the case of a space/keypress we can't really know
83 // which item was pressed on because we can't get coords
84 // from a keyevent
85 //2b) It seems more correct
86
87 int sel = gtk_tree_path_get_indices(path)[0];
88
89 if(!listbox->m_spacePressed)
90 {
91 //Assume it was double-click
92 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
93 event.SetEventObject( listbox );
94
95 if(listbox->IsSelected(sel))
96 {
97 GtkTreeEntry* entry = listbox->GtkGetEntry(sel);
98
99 if(entry)
100 {
101 event.SetInt(sel);
102 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
103
104 if ( listbox->HasClientObjectData() )
105 event.SetClientObject(
106 (wxClientData*) gtk_tree_entry_get_userdata(entry) );
107 else if ( listbox->HasClientUntypedData() )
108 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
109 g_object_unref (entry);
110 }
111 else
112 {
113 wxLogSysError(wxT("Internal error - could not get entry for double-click"));
114 event.SetInt(-1);
115 }
116 }
117 else
118 event.SetInt(-1);
119
120 listbox->GetEventHandler()->ProcessEvent( event );
121 }
122 else
123 {
124 listbox->m_spacePressed = false; //don't block selection behaviour anymore
125
126 //Space was pressed - toggle the appropriate checkbox and the selection
127 #if wxUSE_CHECKLISTBOX //Do it for both native and non-native
128 if (listbox->m_hasCheckBoxes)
129 {
130 wxCheckListBox *clb = (wxCheckListBox *)listbox;
131
132 clb->Check( sel, !clb->IsChecked(sel) );
133
134 wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
135 new_event.SetEventObject( listbox );
136 new_event.SetInt( sel );
137 listbox->GetEventHandler()->ProcessEvent( new_event );
138 }
139 #endif // wxUSE_CHECKLISTBOX
140
141 if( (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) ||
142 ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) )
143 {
144 //toggle the selection + send event
145 listbox->GtkSetSelection(sel, !listbox->IsSelected( sel ), false);
146 }
147 }
148 }
149 }
150
151 //-----------------------------------------------------------------------------
152 // "button_press_event"
153 //-----------------------------------------------------------------------------
154
155 extern "C" {
156 static gint
157 gtk_listbox_button_press_callback( GtkWidget *widget,
158 GdkEventButton *gdk_event,
159 wxListBox *listbox )
160 {
161 if (g_isIdle) wxapp_install_idle_handler();
162
163 if (g_blockEventsOnDrag) return FALSE;
164 if (g_blockEventsOnScroll) return FALSE;
165
166 if (!listbox->m_hasVMT) return FALSE;
167
168 //Just to be on the safe side - it should be unset in the activate callback
169 //but we don't want any obscure bugs if it doesn't get called somehow...
170 listbox->m_spacePressed = false;
171
172 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
173 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
174 {
175 GtkTreePath* path;
176 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget),
177 (gint)gdk_event->x, (gint)gdk_event->y,
178 &path, NULL, NULL, NULL);
179 int sel = gtk_tree_path_get_indices(path)[0];
180 gtk_tree_path_free(path);
181
182 wxCheckListBox *clb = (wxCheckListBox *)listbox;
183
184 clb->Check( sel, !clb->IsChecked(sel) );
185
186 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
187 event.SetEventObject( listbox );
188 event.SetInt( sel );
189 listbox->GetEventHandler()->ProcessEvent( event );
190 }
191 #endif // wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
192
193 return FALSE;
194 }
195 }
196
197 //-----------------------------------------------------------------------------
198 // "key_press_event"
199 //-----------------------------------------------------------------------------
200
201 extern "C" {
202 static gint
203 gtk_listbox_key_press_callback( GtkWidget *widget,
204 GdkEventKey *gdk_event,
205 wxListBox *listbox )
206 {
207 if (g_isIdle) wxapp_install_idle_handler();
208
209 if (g_blockEventsOnDrag) return FALSE;
210
211
212 bool ret = false;
213
214 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
215 {
216 wxNavigationKeyEvent new_event;
217 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
218 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
219 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
220 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
221 new_event.SetCurrentFocus( listbox );
222 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
223 }
224
225 if ((gdk_event->keyval == GDK_Return) && (!ret))
226 {
227 // eat return in all modes (RN:WHY?)
228 ret = true;
229 }
230
231 // Check or uncheck item with SPACE
232 if (gdk_event->keyval == ' ')
233 {
234 //In the keyevent we don't know the index of the item
235 //and the activated event gets called anyway...
236 //
237 //Also, activating with the space causes the treeview to
238 //unselect all the items and then select the item in question
239 //wx's behaviour is to just toggle the item's selection state
240 //and leave the others alone
241 listbox->m_spacePressed = true;
242 }
243
244 if (ret)
245 {
246 g_signal_stop_emission_by_name (widget, "key_press_event");
247 return TRUE;
248 }
249
250 return FALSE;
251 }
252 }
253
254 //-----------------------------------------------------------------------------
255 // "select" and "deselect"
256 //-----------------------------------------------------------------------------
257
258 extern "C" {
259 static gboolean gtk_listitem_select_cb( GtkTreeSelection* selection,
260 GtkTreeModel* model,
261 GtkTreePath* path,
262 gboolean is_selected,
263 wxListBox *listbox )
264 {
265 if (g_isIdle) wxapp_install_idle_handler();
266
267 if (!listbox->m_hasVMT) return TRUE;
268 if (g_blockEventsOnDrag) return TRUE;
269
270 if (listbox->m_spacePressed) return FALSE; //see keyevent callback
271 if (listbox->m_blockEvent) return TRUE;
272
273 // NB: wxdocs explicitly say that this event only gets sent when
274 // something is actually selected, plus the controls example
275 // assumes so and passes -1 to the dogetclientdata funcs if not
276
277 // OK, so basically we need to do a bit of a run-around here as
278 // 1) is_selected says whether the item(s?) are CURRENTLY selected -
279 // i.e. if is_selected is FALSE then the item is going to be
280 // selected right now!
281 // 2) However, since it is not already selected and the user
282 // will expect it to be we need to manually select it and
283 // return FALSE telling GTK we handled the selection
284 if (is_selected) return TRUE;
285
286 int nIndex = gtk_tree_path_get_indices(path)[0];
287 GtkTreeEntry* entry = listbox->GtkGetEntry(nIndex);
288
289 if(entry)
290 {
291 //Now, as mentioned above, we manually select the row that is/was going
292 //to be selected anyway by GTK
293 listbox->m_blockEvent = true; //if we don't block events we will lock the
294 //app due to recursion!!
295
296 GtkTreeSelection* selection =
297 gtk_tree_view_get_selection(listbox->m_treeview);
298 GtkTreeIter iter;
299 gtk_tree_model_get_iter(GTK_TREE_MODEL(listbox->m_liststore), &iter, path);
300 gtk_tree_selection_select_iter(selection, &iter);
301
302 listbox->m_blockEvent = false;
303
304 //Finally, send the wx event
305 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
306 event.SetEventObject( listbox );
307
308 // indicate whether this is a selection or a deselection
309 event.SetExtraLong( 1 );
310
311 event.SetInt(nIndex);
312 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
313
314 if ( listbox->HasClientObjectData() )
315 event.SetClientObject(
316 (wxClientData*) gtk_tree_entry_get_userdata(entry)
317 );
318 else if ( listbox->HasClientUntypedData() )
319 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
320
321 listbox->GetEventHandler()->ProcessEvent( event );
322
323 g_object_unref (entry);
324 return FALSE; //We handled it/did it manually
325 }
326
327 return TRUE; //allow selection to change
328 }
329 }
330
331 //-----------------------------------------------------------------------------
332 // GtkTreeEntry destruction (to destroy client data)
333 //-----------------------------------------------------------------------------
334
335 extern "C" {
336 static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry,
337 wxListBox* listbox)
338 {
339 if(listbox->HasClientObjectData())
340 {
341 gpointer userdata = gtk_tree_entry_get_userdata(entry);
342 if(userdata)
343 delete (wxClientData *)userdata;
344 }
345 }
346 }
347
348 //-----------------------------------------------------------------------------
349 // Sorting callback (standard CmpNoCase return value)
350 //-----------------------------------------------------------------------------
351
352 extern "C" {
353 static gint gtk_listbox_sort_callback(GtkTreeModel *model,
354 GtkTreeIter *a,
355 GtkTreeIter *b,
356 wxListBox *listbox)
357 {
358 GtkTreeEntry* entry;
359 GtkTreeEntry* entry2;
360
361 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
362 a,
363 WXLISTBOX_DATACOLUMN_ARG(listbox),
364 &entry, -1);
365 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
366 b,
367 WXLISTBOX_DATACOLUMN_ARG(listbox),
368 &entry2, -1);
369 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
370 wxCHECK_MSG(entry2, 0, wxT("Could not get entry2"));
371
372 //We compare collate keys here instead of calling g_utf8_collate
373 //as it is rather slow (and even the docs reccommend this)
374 int ret = strcasecmp(gtk_tree_entry_get_collate_key(entry),
375 gtk_tree_entry_get_collate_key(entry2));
376
377 g_object_unref (entry);
378 g_object_unref (entry2);
379
380 return ret;
381 }
382 }
383
384 //-----------------------------------------------------------------------------
385 // Searching callback (TRUE == not equal, FALSE == equal)
386 //-----------------------------------------------------------------------------
387
388 extern "C" {
389 static gboolean gtk_listbox_searchequal_callback(GtkTreeModel* model,
390 gint column,
391 const gchar* key,
392 GtkTreeIter* iter,
393 wxListBox* listbox)
394 {
395 GtkTreeEntry* entry;
396
397 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
398 iter,
399 WXLISTBOX_DATACOLUMN_ARG(listbox),
400 &entry, -1);
401 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
402 gchar* keycollatekey = g_utf8_collate_key(key, -1);
403
404 int ret = strcasecmp(keycollatekey,
405 gtk_tree_entry_get_collate_key(entry));
406
407 g_free(keycollatekey);
408 g_object_unref (entry);
409
410 return ret != 0;
411 }
412 }
413
414 //-----------------------------------------------------------------------------
415 // wxListBox
416 //-----------------------------------------------------------------------------
417
418 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
419
420 // ----------------------------------------------------------------------------
421 // construction
422 // ----------------------------------------------------------------------------
423
424 void wxListBox::Init()
425 {
426 m_treeview = (GtkTreeView*) NULL;
427 #if wxUSE_CHECKLISTBOX
428 m_hasCheckBoxes = false;
429 #endif // wxUSE_CHECKLISTBOX
430 m_spacePressed = false;
431 }
432
433 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
434 const wxPoint &pos, const wxSize &size,
435 const wxArrayString& choices,
436 long style, const wxValidator& validator,
437 const wxString &name )
438 {
439 wxCArrayString chs(choices);
440
441 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
442 style, validator, name );
443 }
444
445 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
446 const wxPoint &pos, const wxSize &size,
447 int n, const wxString choices[],
448 long style, const wxValidator& validator,
449 const wxString &name )
450 {
451 m_needParent = true;
452 m_acceptsFocus = true;
453 m_blockEvent = false;
454
455 if (!PreCreation( parent, pos, size ) ||
456 !CreateBase( parent, id, pos, size, style, validator, name ))
457 {
458 wxFAIL_MSG( wxT("wxListBox creation failed") );
459 return false;
460 }
461
462 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
463 if (style & wxLB_ALWAYS_SB)
464 {
465 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
466 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
467 }
468 else
469 {
470 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
471 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
472 }
473
474
475 GtkScrolledWindowSetBorder(m_widget, style);
476
477 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
478
479 //wxListBox doesn't have a header :)
480 //NB: If enabled SetFirstItem doesn't work correctly
481 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
482
483 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
484 if(m_hasCheckBoxes)
485 ((wxCheckListBox*)this)->DoCreateCheckList();
486 #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
487
488 // Create the data column
489 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
490 gtk_cell_renderer_text_new(),
491 "text",
492 WXLISTBOX_DATACOLUMN, NULL);
493
494 // Now create+set the model (GtkListStore) - first argument # of columns
495 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
496 if(m_hasCheckBoxes)
497 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
498 GTK_TYPE_TREE_ENTRY);
499 else
500 #endif
501 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
502
503 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
504
505 g_object_unref (m_liststore); //free on treeview destruction
506
507 // Disable the pop-up textctrl that enables searching - note that
508 // the docs specify that even if this disabled (which we are doing)
509 // the user can still have it through the start-interactive-search
510 // key binding...either way we want to provide a searchequal callback
511 // NB: If this is enabled a doubleclick event (activate) gets sent
512 // on a successful search
513 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
514 gtk_tree_view_set_search_equal_func(m_treeview,
515 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
516 this,
517 NULL);
518
519 gtk_tree_view_set_enable_search(m_treeview, FALSE);
520
521
522 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
523 gtk_tree_selection_set_select_function(selection,
524 (GtkTreeSelectionFunc)gtk_listitem_select_cb,
525 this, NULL); //NULL == destroycb
526
527 GtkSelectionMode mode;
528 if (style & wxLB_MULTIPLE)
529 {
530 mode = GTK_SELECTION_MULTIPLE;
531 }
532 else if (style & wxLB_EXTENDED)
533 {
534 mode = GTK_SELECTION_EXTENDED;
535 }
536 else
537 {
538 // if style was 0 set single mode
539 m_windowStyle |= wxLB_SINGLE;
540 mode = GTK_SELECTION_SINGLE;
541 }
542
543 gtk_tree_selection_set_mode( selection, mode );
544
545 //Handle sortable stuff
546 if(style & wxLB_SORT)
547 {
548 //Setup sorting in ascending (wx) order
549 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
550 WXLISTBOX_DATACOLUMN,
551 GTK_SORT_ASCENDING);
552
553 //Set the sort callback
554 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
555 WXLISTBOX_DATACOLUMN,
556 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
557 this, //userdata
558 NULL //"destroy notifier"
559 );
560 }
561
562
563 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
564
565 gtk_widget_show( GTK_WIDGET(m_treeview) );
566
567 wxListBox::DoInsertItems(wxArrayString(n, choices), 0); // insert initial items
568
569 //treeview-specific events
570 g_signal_connect(m_treeview, "row-activated",
571 G_CALLBACK(gtk_listbox_row_activated_callback), this);
572
573 // other events
574 g_signal_connect (m_treeview, "button_press_event",
575 G_CALLBACK (gtk_listbox_button_press_callback),
576 this);
577 g_signal_connect (m_treeview, "key_press_event",
578 G_CALLBACK (gtk_listbox_key_press_callback),
579 this);
580
581 m_parent->DoAddChild( this );
582
583 PostCreation(size);
584 SetBestSize(size); // need this too because this is a wxControlWithItems
585
586 return true;
587 }
588
589 wxListBox::~wxListBox()
590 {
591 m_hasVMT = false;
592
593 Clear();
594 }
595
596 // ----------------------------------------------------------------------------
597 // adding items
598 // ----------------------------------------------------------------------------
599
600 void wxListBox::GtkInsertItems(const wxArrayString& items,
601 void** clientData, unsigned int pos)
602 {
603 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
604
605 InvalidateBestSize();
606
607 // Create and set column ids and GValues
608
609 unsigned int nNum = items.GetCount();
610 unsigned int nCurCount = wxListBox::GetCount();
611 wxASSERT_MSG(pos <= nCurCount, wxT("Invalid index passed to wxListBox"));
612
613 GtkTreeIter* pIter = NULL; // append by default
614 GtkTreeIter iter;
615 if (pos != nCurCount)
616 {
617 gboolean res = gtk_tree_model_iter_nth_child(
618 GTK_TREE_MODEL(m_liststore),
619 &iter, NULL, //NULL = parent = get first
620 (int)pos );
621 if(!res)
622 {
623 wxLogSysError(wxT("internal wxListBox error in insertion"));
624 return;
625 }
626
627 pIter = &iter;
628 }
629
630 for (unsigned int i = 0; i < nNum; ++i)
631 {
632 wxString label = items[i];
633
634 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
635 if (m_hasCheckBoxes)
636 {
637 label.Prepend(wxCHECKLBOX_STRING);
638 }
639 #endif // wxUSE_CHECKLISTBOX
640
641
642 GtkTreeEntry* entry = gtk_tree_entry_new();
643 gtk_tree_entry_set_label(entry, wxConvUTF8.cWX2MB(label));
644 gtk_tree_entry_set_destroy_func(entry,
645 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
646 this);
647
648 if (clientData)
649 gtk_tree_entry_set_userdata(entry, clientData[i]);
650
651 GtkTreeIter itercur;
652 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
653
654 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
655 if (m_hasCheckBoxes)
656 {
657 gtk_list_store_set(m_liststore, &itercur,
658 0, FALSE, //FALSE == not toggled
659 1, entry, -1);
660 }
661 else
662 #endif
663 gtk_list_store_set(m_liststore, &itercur,
664 0, entry, -1);
665
666 g_object_unref (entry); //liststore always refs :)
667 }
668 }
669
670 void wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
671 {
672 wxCHECK_RET( IsValidInsert(pos), wxT("invalid index in wxListBox::InsertItems") );
673
674 GtkInsertItems(items, NULL, pos);
675 }
676
677 int wxListBox::DoAppend( const wxString& item )
678 {
679 // Call DoInsertItems
680 unsigned int nWhere = wxListBox::GetCount();
681 wxArrayString aItems;
682 aItems.Add(item);
683 wxListBox::DoInsertItems(aItems, nWhere);
684 return nWhere;
685 }
686
687 void wxListBox::DoSetItems( const wxArrayString& items,
688 void **clientData)
689 {
690 Clear();
691 GtkInsertItems(items, clientData, 0);
692 }
693
694 // ----------------------------------------------------------------------------
695 // deleting items
696 // ----------------------------------------------------------------------------
697
698 void wxListBox::Clear()
699 {
700 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
701
702 InvalidateBestSize();
703
704 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
705 }
706
707 void wxListBox::Delete(unsigned int n)
708 {
709 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
710
711 InvalidateBestSize();
712
713 GtkTreeIter iter;
714 gboolean res = gtk_tree_model_iter_nth_child(
715 GTK_TREE_MODEL(m_liststore),
716 &iter, NULL, //NULL = parent = get first
717 n
718 );
719
720 wxCHECK_RET( res, wxT("wrong listbox index") );
721
722 //this returns false if iter is invalid (i.e. deleting item
723 //at end) but since we don't use iter, well... :)
724 gtk_list_store_remove(m_liststore, &iter);
725 }
726
727 // ----------------------------------------------------------------------------
728 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
729 // ----------------------------------------------------------------------------
730
731 struct _GtkTreeEntry* wxListBox::GtkGetEntry(int n) const
732 {
733 GtkTreeIter iter;
734 gboolean res = gtk_tree_model_iter_nth_child(
735 GTK_TREE_MODEL(m_liststore),
736 &iter, NULL, //NULL = parent = get first
737 n );
738
739 if (!res)
740 {
741 wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n")
742 wxT("Passed in value was:[%i] List size:[%u]"),
743 n, wxListBox::GetCount() );
744 return NULL;
745 }
746
747
748 GtkTreeEntry* entry = NULL;
749 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore), &iter,
750 WXLISTBOX_DATACOLUMN, &entry, -1);
751
752 return entry;
753 }
754
755 // ----------------------------------------------------------------------------
756 // client data
757 // ----------------------------------------------------------------------------
758
759 void* wxListBox::DoGetItemClientData(unsigned int n) const
760 {
761 wxCHECK_MSG( IsValid(n), NULL,
762 wxT("Invalid index passed to GetItemClientData") );
763
764 GtkTreeEntry* entry = GtkGetEntry(n);
765 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
766
767 void* userdata = gtk_tree_entry_get_userdata( entry );
768 g_object_unref (entry);
769 return userdata;
770 }
771
772 wxClientData* wxListBox::DoGetItemClientObject(unsigned int n) const
773 {
774 return (wxClientData*) wxListBox::DoGetItemClientData(n);
775 }
776
777 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
778 {
779 wxCHECK_RET( IsValid(n),
780 wxT("Invalid index passed to SetItemClientData") );
781
782 GtkTreeEntry* entry = GtkGetEntry(n);
783 wxCHECK_RET(entry, wxT("could not get entry"));
784
785 gtk_tree_entry_set_userdata( entry, clientData );
786 g_object_unref (entry);
787 }
788
789 void wxListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
790 {
791 // wxItemContainer already deletes data for us
792 wxListBox::DoSetItemClientData(n, (void*) clientData);
793 }
794
795 // ----------------------------------------------------------------------------
796 // string list access
797 // ----------------------------------------------------------------------------
798
799 void wxListBox::SetString(unsigned int n, const wxString &string)
800 {
801 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
802 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
803
804 GtkTreeEntry* entry = GtkGetEntry(n);
805 wxCHECK_RET( entry, wxT("wrong listbox index") );
806
807 wxString label = string;
808
809 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
810 if (m_hasCheckBoxes)
811 label.Prepend(wxCHECKLBOX_STRING);
812 #endif // wxUSE_CHECKLISTBOX
813
814 // RN: This may look wierd but the problem is that the TreeView
815 // doesn't resort or update when changed above and there is no real
816 // notification function...
817 void* userdata = gtk_tree_entry_get_userdata(entry);
818 gtk_tree_entry_set_userdata(entry, NULL); //don't delete on destroy
819 g_object_unref (entry);
820
821 bool bWasSelected = wxListBox::IsSelected(n);
822 wxListBox::Delete(n);
823
824 wxArrayString aItems;
825 aItems.Add(label);
826 GtkInsertItems(aItems, &userdata, n);
827 if (bWasSelected)
828 wxListBox::GtkSetSelection(n, true, true);
829 }
830
831 wxString wxListBox::GetString(unsigned int n) const
832 {
833 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
834
835 GtkTreeEntry* entry = GtkGetEntry(n);
836 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
837
838 wxString label = wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
839
840 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
841 // checklistboxes have "[±] " prepended to their lables, remove it
842 //
843 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
844 if ( m_hasCheckBoxes )
845 label.erase(0, 4);
846 #endif // wxUSE_CHECKLISTBOX
847
848 g_object_unref (entry);
849 return label;
850 }
851
852 unsigned int wxListBox::GetCount() const
853 {
854 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
855
856 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
857 }
858
859 int wxListBox::FindString( const wxString &item, bool bCase ) const
860 {
861 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
862
863 //Sort of hackish - maybe there is a faster way
864 unsigned int nCount = wxListBox::GetCount();
865
866 for(unsigned int i = 0; i < nCount; ++i)
867 {
868 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
869 return (int)i;
870 }
871
872
873 // it's not an error if the string is not found -> no wxCHECK
874 return wxNOT_FOUND;
875 }
876
877 // ----------------------------------------------------------------------------
878 // selection
879 // ----------------------------------------------------------------------------
880
881 int wxListBox::GetSelection() const
882 {
883 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
884 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
885 wxT("must be single selection listbox"));
886
887 GtkTreeIter iter;
888 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
889
890 // only works on single-sel
891 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
892 return wxNOT_FOUND;
893
894 GtkTreePath* path =
895 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
896
897 int sel = gtk_tree_path_get_indices(path)[0];
898
899 gtk_tree_path_free(path);
900
901 return sel;
902 }
903
904 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
905 {
906 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
907
908 aSelections.Empty();
909
910 int i = 0;
911 GtkTreeIter iter;
912 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
913
914 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
915 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
916 do
917 {
918 if (gtk_tree_selection_iter_is_selected(selection, &iter))
919 aSelections.Add(i);
920
921 i++;
922 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
923 }
924
925 return aSelections.GetCount();
926 }
927
928 bool wxListBox::IsSelected( int n ) const
929 {
930 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
931
932 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
933
934 GtkTreeIter iter;
935 gboolean res = gtk_tree_model_iter_nth_child(
936 GTK_TREE_MODEL(m_liststore),
937 &iter, NULL, //NULL = parent = get first
938 n );
939
940 wxCHECK_MSG( res, false, wxT("Invalid index") );
941
942 return gtk_tree_selection_iter_is_selected(selection, &iter);
943 }
944
945 void wxListBox::DoSetSelection( int n, bool select )
946 {
947 return GtkSetSelection(n, select, true); //docs say no events here
948 }
949
950 void wxListBox::GtkSetSelection(int n, const bool select, const bool blockEvent)
951 {
952 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
953
954 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
955
956 GtkTreeIter iter;
957 gboolean res = gtk_tree_model_iter_nth_child(
958 GTK_TREE_MODEL(m_liststore),
959 &iter, NULL, //NULL = parent = get first
960 n
961 );
962 wxCHECK_RET( res, wxT("Invalid index") );
963
964 m_blockEvent = blockEvent;
965
966 if (select)
967 gtk_tree_selection_select_iter(selection, &iter);
968 else
969 gtk_tree_selection_unselect_iter(selection, &iter);
970
971 m_blockEvent = false;
972 }
973
974 void wxListBox::DoSetFirstItem( int n )
975 {
976 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
977 wxCHECK_RET( IsValid(n), wxT("invalid index"));
978
979 //RN: I have no idea why this line is needed...
980 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
981 return;
982
983 GtkTreeIter iter;
984 gtk_tree_model_iter_nth_child(
985 GTK_TREE_MODEL(m_liststore),
986 &iter,
987 NULL, //NULL = parent = get first
988 n
989 );
990
991 GtkTreePath* path = gtk_tree_model_get_path(
992 GTK_TREE_MODEL(m_liststore), &iter);
993
994 // Scroll to the desired cell (0.0 == topleft alignment)
995 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
996 TRUE, 0.0f, 0.0f);
997
998 gtk_tree_path_free(path);
999 }
1000
1001 // ----------------------------------------------------------------------------
1002 // hittest
1003 // ----------------------------------------------------------------------------
1004
1005 int wxListBox::DoListHitTest(const wxPoint& point) const
1006 {
1007 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
1008 // we only want visible items we need to check for it manually here
1009 if ( !GetClientRect().Inside(point) )
1010 return wxNOT_FOUND;
1011
1012 // need to translate from master window since it is in client coords
1013 gint binx, biny;
1014 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
1015 &binx, &biny, NULL, NULL, NULL);
1016
1017 GtkTreePath* path;
1018 if ( !gtk_tree_view_get_path_at_pos
1019 (
1020 m_treeview,
1021 point.x - binx,
1022 point.y - biny,
1023 &path,
1024 NULL, // [out] column (always 0 here)
1025 NULL, // [out] x-coord relative to the cell (not interested)
1026 NULL // [out] y-coord relative to the cell
1027 ) )
1028 {
1029 return wxNOT_FOUND;
1030 }
1031
1032 int index = gtk_tree_path_get_indices(path)[0];
1033 gtk_tree_path_free(path);
1034
1035 return index;
1036 }
1037
1038 // ----------------------------------------------------------------------------
1039 // helpers
1040 // ----------------------------------------------------------------------------
1041
1042 #if wxUSE_TOOLTIPS
1043 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
1044 {
1045 // RN: Is this needed anymore?
1046 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), wxGTK_CONV(tip), (gchar*) NULL );
1047 }
1048 #endif // wxUSE_TOOLTIPS
1049
1050 GtkWidget *wxListBox::GetConnectWidget()
1051 {
1052 // the correct widget for listbox events (such as mouse clicks for example)
1053 // is m_treeview, not the parent scrolled window
1054 return GTK_WIDGET(m_treeview);
1055 }
1056
1057 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
1058 {
1059 return (window == gtk_tree_view_get_bin_window(m_treeview));
1060 }
1061
1062 void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
1063 {
1064 if (m_hasBgCol && m_backgroundColour.Ok())
1065 {
1066 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
1067 if (window)
1068 {
1069 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
1070 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1071 gdk_window_clear( window );
1072 }
1073 }
1074
1075 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
1076 }
1077
1078 void wxListBox::OnInternalIdle()
1079 {
1080 //RN: Is this needed anymore?
1081 wxCursor cursor = m_cursor;
1082 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1083
1084 if (GTK_WIDGET(m_treeview)->window && cursor.Ok())
1085 {
1086 /* I now set the cursor the anew in every OnInternalIdle call
1087 as setting the cursor in a parent window also effects the
1088 windows above so that checking for the current cursor is
1089 not possible. */
1090 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
1091 gdk_window_set_cursor( window, cursor.GetCursor() );
1092 }
1093
1094 if (wxUpdateUIEvent::CanUpdate(this))
1095 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1096 }
1097
1098 wxSize wxListBox::DoGetBestSize() const
1099 {
1100 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
1101
1102 // Start with a minimum size that's not too small
1103 int cx, cy;
1104 GetTextExtent( wxT("X"), &cx, &cy);
1105 int lbWidth = 3 * cx;
1106 int lbHeight = 10;
1107
1108 // Get the visible area of the tree view (limit to the 10th item
1109 // so that it isn't too big)
1110 unsigned int count = GetCount();
1111 if (count)
1112 {
1113 int wLine;
1114
1115 // Find the widest line
1116 for(unsigned int i = 0; i < count; i++) {
1117 wxString str(GetString(i));
1118 GetTextExtent(str, &wLine, NULL);
1119 lbWidth = wxMax(lbWidth, wLine);
1120 }
1121
1122 lbWidth += 3 * cx;
1123
1124 // And just a bit more for the checkbox if present and then some
1125 // (these are rough guesses)
1126 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
1127 if ( m_hasCheckBoxes )
1128 {
1129 lbWidth += 35;
1130 cy = cy > 25 ? cy : 25; // rough height of checkbox
1131 }
1132 #endif
1133
1134 // don't make the listbox too tall (limit height to around 10 items) but don't
1135 // make it too small neither
1136 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
1137 }
1138
1139 // Add room for the scrollbar
1140 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1141
1142 wxSize best(lbWidth, lbHeight);
1143 CacheBestSize(best);
1144 return best;
1145 }
1146
1147 // static
1148 wxVisualAttributes
1149 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1150 {
1151 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
1152 }
1153
1154 #endif // wxUSE_LISTBOX