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