]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/listbox.cpp
typo fix
[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 // passing -1 to SetSelection() is documented to deselect all items
948 if ( n == wxNOT_FOUND )
949 {
950 // ... and not generate any events in the process
951 GtkDeselectAll();
952 }
953
954 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
955
956 // don't generate the selection event
957 GtkSetSelection(n, select, true);
958 }
959
960 void wxListBox::GtkDeselectAll()
961 {
962 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
963
964 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
965
966 m_blockEvent = true;
967
968 gtk_tree_selection_unselect_all(selection);
969
970 m_blockEvent = false;
971 }
972
973 void wxListBox::GtkSetSelection(int n, const bool select, const bool blockEvent)
974 {
975 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
976
977 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
978
979 GtkTreeIter iter;
980 gboolean res = gtk_tree_model_iter_nth_child(
981 GTK_TREE_MODEL(m_liststore),
982 &iter, NULL, //NULL = parent = get first
983 n
984 );
985 wxCHECK_RET( res, wxT("Invalid index") );
986
987 m_blockEvent = blockEvent;
988
989 if (select)
990 gtk_tree_selection_select_iter(selection, &iter);
991 else
992 gtk_tree_selection_unselect_iter(selection, &iter);
993
994 m_blockEvent = false;
995 }
996
997 void wxListBox::DoSetFirstItem( int n )
998 {
999 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
1000 wxCHECK_RET( IsValid(n), wxT("invalid index"));
1001
1002 //RN: I have no idea why this line is needed...
1003 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
1004 return;
1005
1006 GtkTreeIter iter;
1007 gtk_tree_model_iter_nth_child(
1008 GTK_TREE_MODEL(m_liststore),
1009 &iter,
1010 NULL, //NULL = parent = get first
1011 n
1012 );
1013
1014 GtkTreePath* path = gtk_tree_model_get_path(
1015 GTK_TREE_MODEL(m_liststore), &iter);
1016
1017 // Scroll to the desired cell (0.0 == topleft alignment)
1018 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
1019 TRUE, 0.0f, 0.0f);
1020
1021 gtk_tree_path_free(path);
1022 }
1023
1024 // ----------------------------------------------------------------------------
1025 // hittest
1026 // ----------------------------------------------------------------------------
1027
1028 int wxListBox::DoListHitTest(const wxPoint& point) const
1029 {
1030 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
1031 // we only want visible items we need to check for it manually here
1032 if ( !GetClientRect().Inside(point) )
1033 return wxNOT_FOUND;
1034
1035 // need to translate from master window since it is in client coords
1036 gint binx, biny;
1037 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
1038 &binx, &biny, NULL, NULL, NULL);
1039
1040 GtkTreePath* path;
1041 if ( !gtk_tree_view_get_path_at_pos
1042 (
1043 m_treeview,
1044 point.x - binx,
1045 point.y - biny,
1046 &path,
1047 NULL, // [out] column (always 0 here)
1048 NULL, // [out] x-coord relative to the cell (not interested)
1049 NULL // [out] y-coord relative to the cell
1050 ) )
1051 {
1052 return wxNOT_FOUND;
1053 }
1054
1055 int index = gtk_tree_path_get_indices(path)[0];
1056 gtk_tree_path_free(path);
1057
1058 return index;
1059 }
1060
1061 // ----------------------------------------------------------------------------
1062 // helpers
1063 // ----------------------------------------------------------------------------
1064
1065 #if wxUSE_TOOLTIPS
1066 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
1067 {
1068 // RN: Is this needed anymore?
1069 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), wxGTK_CONV(tip), (gchar*) NULL );
1070 }
1071 #endif // wxUSE_TOOLTIPS
1072
1073 GtkWidget *wxListBox::GetConnectWidget()
1074 {
1075 // the correct widget for listbox events (such as mouse clicks for example)
1076 // is m_treeview, not the parent scrolled window
1077 return GTK_WIDGET(m_treeview);
1078 }
1079
1080 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
1081 {
1082 return (window == gtk_tree_view_get_bin_window(m_treeview));
1083 }
1084
1085 void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
1086 {
1087 if (m_hasBgCol && m_backgroundColour.Ok())
1088 {
1089 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
1090 if (window)
1091 {
1092 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
1093 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1094 gdk_window_clear( window );
1095 }
1096 }
1097
1098 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
1099 }
1100
1101 void wxListBox::OnInternalIdle()
1102 {
1103 //RN: Is this needed anymore?
1104 wxCursor cursor = m_cursor;
1105 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1106
1107 if (GTK_WIDGET(m_treeview)->window && cursor.Ok())
1108 {
1109 /* I now set the cursor the anew in every OnInternalIdle call
1110 as setting the cursor in a parent window also effects the
1111 windows above so that checking for the current cursor is
1112 not possible. */
1113 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
1114 gdk_window_set_cursor( window, cursor.GetCursor() );
1115 }
1116
1117 if (wxUpdateUIEvent::CanUpdate(this))
1118 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1119 }
1120
1121 wxSize wxListBox::DoGetBestSize() const
1122 {
1123 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
1124
1125 // Start with a minimum size that's not too small
1126 int cx, cy;
1127 GetTextExtent( wxT("X"), &cx, &cy);
1128 int lbWidth = 3 * cx;
1129 int lbHeight = 10;
1130
1131 // Get the visible area of the tree view (limit to the 10th item
1132 // so that it isn't too big)
1133 unsigned int count = GetCount();
1134 if (count)
1135 {
1136 int wLine;
1137
1138 // Find the widest line
1139 for(unsigned int i = 0; i < count; i++) {
1140 wxString str(GetString(i));
1141 GetTextExtent(str, &wLine, NULL);
1142 lbWidth = wxMax(lbWidth, wLine);
1143 }
1144
1145 lbWidth += 3 * cx;
1146
1147 // And just a bit more for the checkbox if present and then some
1148 // (these are rough guesses)
1149 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
1150 if ( m_hasCheckBoxes )
1151 {
1152 lbWidth += 35;
1153 cy = cy > 25 ? cy : 25; // rough height of checkbox
1154 }
1155 #endif
1156
1157 // don't make the listbox too tall (limit height to around 10 items) but don't
1158 // make it too small neither
1159 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
1160 }
1161
1162 // Add room for the scrollbar
1163 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1164
1165 wxSize best(lbWidth, lbHeight);
1166 CacheBestSize(best);
1167 return best;
1168 }
1169
1170 // static
1171 wxVisualAttributes
1172 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1173 {
1174 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
1175 }
1176
1177 #endif // wxUSE_LISTBOX