]>
Commit | Line | Data |
---|---|---|
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/private/object.h" | |
30 | #include "wx/gtk/treeentry_gtk.h" | |
31 | ||
32 | #if wxUSE_TOOLTIPS | |
33 | #include "wx/tooltip.h" | |
34 | #endif | |
35 | ||
36 | #include <gtk/gtk.h> | |
37 | #include <gdk/gdkkeysyms.h> | |
38 | ||
39 | //----------------------------------------------------------------------------- | |
40 | // data | |
41 | //----------------------------------------------------------------------------- | |
42 | ||
43 | extern bool g_blockEventsOnDrag; | |
44 | extern bool g_blockEventsOnScroll; | |
45 | ||
46 | ||
47 | ||
48 | //----------------------------------------------------------------------------- | |
49 | // Macro to tell which row the strings are in (1 if native checklist, 0 if not) | |
50 | //----------------------------------------------------------------------------- | |
51 | ||
52 | #if wxUSE_CHECKLISTBOX | |
53 | # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0) | |
54 | #else | |
55 | # define WXLISTBOX_DATACOLUMN_ARG(x) (0) | |
56 | #endif // wxUSE_CHECKLISTBOX | |
57 | ||
58 | #define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this) | |
59 | ||
60 | // ---------------------------------------------------------------------------- | |
61 | // helper functions | |
62 | // ---------------------------------------------------------------------------- | |
63 | ||
64 | namespace | |
65 | { | |
66 | ||
67 | // Return the entry for the given listbox item. | |
68 | // | |
69 | // Return value must be released by caller if non-NULL. | |
70 | GtkTreeEntry * | |
71 | GetEntry(GtkListStore *store, GtkTreeIter *iter, const wxListBox *listbox) | |
72 | { | |
73 | GtkTreeEntry* entry; | |
74 | gtk_tree_model_get(GTK_TREE_MODEL(store), | |
75 | iter, | |
76 | WXLISTBOX_DATACOLUMN_ARG(listbox), | |
77 | &entry, | |
78 | -1); | |
79 | ||
80 | return entry; | |
81 | } | |
82 | ||
83 | } // anonymous namespace | |
84 | ||
85 | //----------------------------------------------------------------------------- | |
86 | // "row-activated" | |
87 | //----------------------------------------------------------------------------- | |
88 | ||
89 | extern "C" { | |
90 | static void | |
91 | gtk_listbox_row_activated_callback(GtkTreeView * WXUNUSED(treeview), | |
92 | GtkTreePath *path, | |
93 | GtkTreeViewColumn * WXUNUSED(col), | |
94 | wxListBox *listbox) | |
95 | { | |
96 | if (g_blockEventsOnDrag) return; | |
97 | if (g_blockEventsOnScroll) return; | |
98 | ||
99 | // This is triggered by either a double-click or a space press | |
100 | ||
101 | int sel = gtk_tree_path_get_indices(path)[0]; | |
102 | ||
103 | listbox->GTKOnActivated(sel); | |
104 | } | |
105 | } | |
106 | ||
107 | //----------------------------------------------------------------------------- | |
108 | // "changed" | |
109 | //----------------------------------------------------------------------------- | |
110 | ||
111 | extern "C" { | |
112 | static void | |
113 | gtk_listitem_changed_callback(GtkTreeSelection * WXUNUSED(selection), | |
114 | wxListBox *listbox ) | |
115 | { | |
116 | if (g_blockEventsOnDrag) return; | |
117 | ||
118 | listbox->GTKOnSelectionChanged(); | |
119 | } | |
120 | ||
121 | } | |
122 | ||
123 | //----------------------------------------------------------------------------- | |
124 | // "key_press_event" | |
125 | //----------------------------------------------------------------------------- | |
126 | ||
127 | extern "C" { | |
128 | static gboolean | |
129 | gtk_listbox_key_press_callback( GtkWidget *WXUNUSED(widget), | |
130 | GdkEventKey *gdk_event, | |
131 | wxListBox *listbox ) | |
132 | { | |
133 | if ((gdk_event->keyval == GDK_Return) || | |
134 | (gdk_event->keyval == GDK_ISO_Enter) || | |
135 | (gdk_event->keyval == GDK_KP_Enter)) | |
136 | { | |
137 | int index = -1; | |
138 | if (!listbox->HasMultipleSelection()) | |
139 | index = listbox->GetSelection(); | |
140 | else | |
141 | { | |
142 | wxArrayInt sels; | |
143 | if (listbox->GetSelections( sels ) < 1) | |
144 | return FALSE; | |
145 | index = sels[0]; | |
146 | } | |
147 | ||
148 | if (index != wxNOT_FOUND) | |
149 | { | |
150 | listbox->GTKOnActivated(index); | |
151 | ||
152 | // wxMac and wxMSW always invoke default action | |
153 | // if (!ret) | |
154 | { | |
155 | // DClick not handled -> invoke default action | |
156 | wxWindow *tlw = wxGetTopLevelParent( listbox ); | |
157 | if (tlw) | |
158 | { | |
159 | GtkWindow *gtk_window = GTK_WINDOW( tlw->GetHandle() ); | |
160 | if (gtk_window) | |
161 | gtk_window_activate_default( gtk_window ); | |
162 | } | |
163 | } | |
164 | ||
165 | // Always intercept, otherwise we'd get another dclick | |
166 | // event from row_activated | |
167 | return TRUE; | |
168 | } | |
169 | } | |
170 | ||
171 | return FALSE; | |
172 | } | |
173 | } | |
174 | ||
175 | //----------------------------------------------------------------------------- | |
176 | // GtkTreeEntry destruction (to destroy client data) | |
177 | //----------------------------------------------------------------------------- | |
178 | ||
179 | extern "C" { | |
180 | static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry, | |
181 | wxListBox* listbox) | |
182 | { | |
183 | if (listbox->HasClientObjectData()) | |
184 | { | |
185 | gpointer userdata = gtk_tree_entry_get_userdata(entry); | |
186 | if (userdata) | |
187 | delete (wxClientData *)userdata; | |
188 | } | |
189 | } | |
190 | } | |
191 | ||
192 | //----------------------------------------------------------------------------- | |
193 | // Sorting callback (standard CmpNoCase return value) | |
194 | //----------------------------------------------------------------------------- | |
195 | ||
196 | extern "C" { | |
197 | static gint gtk_listbox_sort_callback(GtkTreeModel * WXUNUSED(model), | |
198 | GtkTreeIter *a, | |
199 | GtkTreeIter *b, | |
200 | wxListBox *listbox) | |
201 | { | |
202 | wxGtkObject<GtkTreeEntry> entry1(GetEntry(listbox->m_liststore, a, listbox)); | |
203 | wxCHECK_MSG(entry1, 0, wxT("Could not get first entry")); | |
204 | ||
205 | wxGtkObject<GtkTreeEntry> entry2(GetEntry(listbox->m_liststore, b, listbox)); | |
206 | wxCHECK_MSG(entry2, 0, wxT("Could not get second entry")); | |
207 | ||
208 | //We compare collate keys here instead of calling g_utf8_collate | |
209 | //as it is rather slow (and even the docs reccommend this) | |
210 | return strcmp(gtk_tree_entry_get_collate_key(entry1), | |
211 | gtk_tree_entry_get_collate_key(entry2)) >= 0; | |
212 | } | |
213 | } | |
214 | ||
215 | //----------------------------------------------------------------------------- | |
216 | // Searching callback (TRUE == not equal, FALSE == equal) | |
217 | //----------------------------------------------------------------------------- | |
218 | ||
219 | extern "C" { | |
220 | static gboolean gtk_listbox_searchequal_callback(GtkTreeModel * WXUNUSED(model), | |
221 | gint WXUNUSED(column), | |
222 | const gchar* key, | |
223 | GtkTreeIter* iter, | |
224 | wxListBox* listbox) | |
225 | { | |
226 | wxGtkObject<GtkTreeEntry> | |
227 | entry(GetEntry(listbox->m_liststore, iter, listbox)); | |
228 | wxCHECK_MSG(entry, 0, wxT("Could not get entry")); | |
229 | ||
230 | wxGtkString keycollatekey(g_utf8_collate_key(key, -1)); | |
231 | ||
232 | return strcmp(keycollatekey, gtk_tree_entry_get_collate_key(entry)) != 0; | |
233 | } | |
234 | } | |
235 | ||
236 | //----------------------------------------------------------------------------- | |
237 | // wxListBox | |
238 | //----------------------------------------------------------------------------- | |
239 | ||
240 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems) | |
241 | ||
242 | // ---------------------------------------------------------------------------- | |
243 | // construction | |
244 | // ---------------------------------------------------------------------------- | |
245 | ||
246 | void wxListBox::Init() | |
247 | { | |
248 | m_treeview = NULL; | |
249 | #if wxUSE_CHECKLISTBOX | |
250 | m_hasCheckBoxes = false; | |
251 | #endif // wxUSE_CHECKLISTBOX | |
252 | } | |
253 | ||
254 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, | |
255 | const wxPoint &pos, const wxSize &size, | |
256 | const wxArrayString& choices, | |
257 | long style, const wxValidator& validator, | |
258 | const wxString &name ) | |
259 | { | |
260 | wxCArrayString chs(choices); | |
261 | ||
262 | return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
263 | style, validator, name ); | |
264 | } | |
265 | ||
266 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, | |
267 | const wxPoint &pos, const wxSize &size, | |
268 | int n, const wxString choices[], | |
269 | long style, const wxValidator& validator, | |
270 | const wxString &name ) | |
271 | { | |
272 | if (!PreCreation( parent, pos, size ) || | |
273 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
274 | { | |
275 | wxFAIL_MSG( wxT("wxListBox creation failed") ); | |
276 | return false; | |
277 | } | |
278 | ||
279 | m_widget = gtk_scrolled_window_new( NULL, NULL ); | |
280 | g_object_ref(m_widget); | |
281 | if (style & wxLB_ALWAYS_SB) | |
282 | { | |
283 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), | |
284 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS ); | |
285 | } | |
286 | else | |
287 | { | |
288 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), | |
289 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
290 | } | |
291 | ||
292 | ||
293 | GTKScrolledWindowSetBorder(m_widget, style); | |
294 | ||
295 | m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) ); | |
296 | ||
297 | //wxListBox doesn't have a header :) | |
298 | //NB: If enabled SetFirstItem doesn't work correctly | |
299 | gtk_tree_view_set_headers_visible(m_treeview, FALSE); | |
300 | ||
301 | #if wxUSE_CHECKLISTBOX | |
302 | if(m_hasCheckBoxes) | |
303 | ((wxCheckListBox*)this)->DoCreateCheckList(); | |
304 | #endif // wxUSE_CHECKLISTBOX | |
305 | ||
306 | // Create the data column | |
307 | gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "", | |
308 | gtk_cell_renderer_text_new(), | |
309 | "text", | |
310 | WXLISTBOX_DATACOLUMN, NULL); | |
311 | ||
312 | // Now create+set the model (GtkListStore) - first argument # of columns | |
313 | #if wxUSE_CHECKLISTBOX | |
314 | if(m_hasCheckBoxes) | |
315 | m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN, | |
316 | GTK_TYPE_TREE_ENTRY); | |
317 | else | |
318 | #endif | |
319 | m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY); | |
320 | ||
321 | gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore)); | |
322 | ||
323 | g_object_unref (m_liststore); //free on treeview destruction | |
324 | ||
325 | // Disable the pop-up textctrl that enables searching - note that | |
326 | // the docs specify that even if this disabled (which we are doing) | |
327 | // the user can still have it through the start-interactive-search | |
328 | // key binding...either way we want to provide a searchequal callback | |
329 | // NB: If this is enabled a doubleclick event (activate) gets sent | |
330 | // on a successful search | |
331 | gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN); | |
332 | gtk_tree_view_set_search_equal_func(m_treeview, | |
333 | (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback, | |
334 | this, | |
335 | NULL); | |
336 | ||
337 | gtk_tree_view_set_enable_search(m_treeview, FALSE); | |
338 | ||
339 | GtkSelectionMode mode; | |
340 | // GTK_SELECTION_EXTENDED is a deprecated synonym for GTK_SELECTION_MULTIPLE | |
341 | if ( style & (wxLB_MULTIPLE | wxLB_EXTENDED) ) | |
342 | { | |
343 | mode = GTK_SELECTION_MULTIPLE; | |
344 | } | |
345 | else // no multi-selection flags specified | |
346 | { | |
347 | m_windowStyle |= wxLB_SINGLE; | |
348 | ||
349 | // Notice that we must use BROWSE and not GTK_SELECTION_SINGLE because | |
350 | // the latter allows to not select any items at all while a single | |
351 | // selection listbox is supposed to always have a selection (at least | |
352 | // once the user selected something, it might not have any initially). | |
353 | mode = GTK_SELECTION_BROWSE; | |
354 | } | |
355 | ||
356 | GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview ); | |
357 | gtk_tree_selection_set_mode( selection, mode ); | |
358 | ||
359 | // Handle sortable stuff | |
360 | if(HasFlag(wxLB_SORT)) | |
361 | { | |
362 | // Setup sorting in ascending (wx) order | |
363 | gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore), | |
364 | WXLISTBOX_DATACOLUMN, | |
365 | GTK_SORT_ASCENDING); | |
366 | ||
367 | // Set the sort callback | |
368 | gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore), | |
369 | WXLISTBOX_DATACOLUMN, | |
370 | (GtkTreeIterCompareFunc) gtk_listbox_sort_callback, | |
371 | this, //userdata | |
372 | NULL //"destroy notifier" | |
373 | ); | |
374 | } | |
375 | ||
376 | ||
377 | gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) ); | |
378 | ||
379 | gtk_widget_show( GTK_WIDGET(m_treeview) ); | |
380 | m_focusWidget = GTK_WIDGET(m_treeview); | |
381 | ||
382 | Append(n, choices); // insert initial items | |
383 | ||
384 | // generate dclick events | |
385 | g_signal_connect_after(m_treeview, "row-activated", | |
386 | G_CALLBACK(gtk_listbox_row_activated_callback), this); | |
387 | ||
388 | // for intercepting dclick generation by <ENTER> | |
389 | g_signal_connect (m_treeview, "key_press_event", | |
390 | G_CALLBACK (gtk_listbox_key_press_callback), | |
391 | this); | |
392 | m_parent->DoAddChild( this ); | |
393 | ||
394 | PostCreation(size); | |
395 | SetInitialSize(size); // need this too because this is a wxControlWithItems | |
396 | ||
397 | g_signal_connect_after (selection, "changed", | |
398 | G_CALLBACK (gtk_listitem_changed_callback), this); | |
399 | ||
400 | return true; | |
401 | } | |
402 | ||
403 | wxListBox::~wxListBox() | |
404 | { | |
405 | m_hasVMT = false; | |
406 | ||
407 | Clear(); | |
408 | } | |
409 | ||
410 | void wxListBox::GTKDisableEvents() | |
411 | { | |
412 | GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview ); | |
413 | ||
414 | g_signal_handlers_block_by_func(selection, | |
415 | (gpointer) gtk_listitem_changed_callback, this); | |
416 | } | |
417 | ||
418 | void wxListBox::GTKEnableEvents() | |
419 | { | |
420 | GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview ); | |
421 | ||
422 | g_signal_handlers_unblock_by_func(selection, | |
423 | (gpointer) gtk_listitem_changed_callback, this); | |
424 | ||
425 | UpdateOldSelections(); | |
426 | } | |
427 | ||
428 | ||
429 | void wxListBox::Update() | |
430 | { | |
431 | wxWindow::Update(); | |
432 | ||
433 | if (m_treeview) | |
434 | gdk_window_process_updates(GTK_WIDGET(m_treeview)->window, TRUE); | |
435 | } | |
436 | ||
437 | // ---------------------------------------------------------------------------- | |
438 | // adding items | |
439 | // ---------------------------------------------------------------------------- | |
440 | ||
441 | int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items, | |
442 | unsigned int pos, | |
443 | void **clientData, | |
444 | wxClientDataType type) | |
445 | { | |
446 | wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") ); | |
447 | ||
448 | InvalidateBestSize(); | |
449 | ||
450 | GtkTreeIter* pIter = NULL; // append by default | |
451 | GtkTreeIter iter; | |
452 | if ( pos != GetCount() ) | |
453 | { | |
454 | wxCHECK_MSG( GTKGetIteratorFor(pos, &iter), wxNOT_FOUND, | |
455 | wxT("internal wxListBox error in insertion") ); | |
456 | ||
457 | pIter = &iter; | |
458 | } | |
459 | ||
460 | const unsigned int numItems = items.GetCount(); | |
461 | for ( unsigned int i = 0; i < numItems; ++i ) | |
462 | { | |
463 | wxGtkObject<GtkTreeEntry> entry(gtk_tree_entry_new()); | |
464 | gtk_tree_entry_set_label(entry, wxGTK_CONV(items[i])); | |
465 | gtk_tree_entry_set_destroy_func(entry, | |
466 | (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb, | |
467 | this); | |
468 | ||
469 | GtkTreeIter itercur; | |
470 | gtk_list_store_insert_before(m_liststore, &itercur, pIter); | |
471 | ||
472 | GTKSetItem(itercur, entry); | |
473 | ||
474 | if (clientData) | |
475 | AssignNewItemClientData(GTKGetIndexFor(itercur), clientData, i, type); | |
476 | } | |
477 | ||
478 | UpdateOldSelections(); | |
479 | ||
480 | return pos + numItems - 1; | |
481 | } | |
482 | ||
483 | // ---------------------------------------------------------------------------- | |
484 | // deleting items | |
485 | // ---------------------------------------------------------------------------- | |
486 | ||
487 | void wxListBox::DoClear() | |
488 | { | |
489 | wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") ); | |
490 | ||
491 | GTKDisableEvents(); // just in case | |
492 | ||
493 | InvalidateBestSize(); | |
494 | ||
495 | gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */ | |
496 | ||
497 | GTKEnableEvents(); | |
498 | } | |
499 | ||
500 | void wxListBox::DoDeleteOneItem(unsigned int n) | |
501 | { | |
502 | wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") ); | |
503 | ||
504 | InvalidateBestSize(); | |
505 | ||
506 | GTKDisableEvents(); // just in case | |
507 | ||
508 | GtkTreeIter iter; | |
509 | wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("wrong listbox index") ); | |
510 | ||
511 | // this returns false if iter is invalid (e.g. deleting item at end) but | |
512 | // since we don't use iter, we ignore the return value | |
513 | gtk_list_store_remove(m_liststore, &iter); | |
514 | ||
515 | GTKEnableEvents(); | |
516 | } | |
517 | ||
518 | // ---------------------------------------------------------------------------- | |
519 | // helper functions for working with iterators | |
520 | // ---------------------------------------------------------------------------- | |
521 | ||
522 | bool wxListBox::GTKGetIteratorFor(unsigned pos, GtkTreeIter *iter) const | |
523 | { | |
524 | if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore), | |
525 | iter, NULL, pos) ) | |
526 | { | |
527 | wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos); | |
528 | return false; | |
529 | } | |
530 | ||
531 | return true; | |
532 | } | |
533 | ||
534 | int wxListBox::GTKGetIndexFor(GtkTreeIter& iter) const | |
535 | { | |
536 | GtkTreePath *path = | |
537 | gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter); | |
538 | ||
539 | gint* pIntPath = gtk_tree_path_get_indices(path); | |
540 | ||
541 | wxCHECK_MSG( pIntPath, wxNOT_FOUND, wxT("failed to get iterator path") ); | |
542 | ||
543 | int idx = pIntPath[0]; | |
544 | ||
545 | gtk_tree_path_free( path ); | |
546 | ||
547 | return idx; | |
548 | } | |
549 | ||
550 | // get GtkTreeEntry from position (note: you need to g_unref it if valid) | |
551 | GtkTreeEntry *wxListBox::GTKGetEntry(unsigned n) const | |
552 | { | |
553 | GtkTreeIter iter; | |
554 | if ( !GTKGetIteratorFor(n, &iter) ) | |
555 | return NULL; | |
556 | ||
557 | return GetEntry(m_liststore, &iter, this); | |
558 | } | |
559 | ||
560 | void wxListBox::GTKSetItem(GtkTreeIter& iter, const GtkTreeEntry *entry) | |
561 | { | |
562 | #if wxUSE_CHECKLISTBOX | |
563 | if ( m_hasCheckBoxes ) | |
564 | { | |
565 | gtk_list_store_set(m_liststore, &iter, | |
566 | 0, FALSE, // FALSE == not toggled | |
567 | 1, entry, | |
568 | -1); | |
569 | } | |
570 | else | |
571 | #endif // wxUSE_CHECKLISTBOX | |
572 | { | |
573 | gtk_list_store_set(m_liststore, &iter, 0, entry, -1); | |
574 | } | |
575 | } | |
576 | ||
577 | // ---------------------------------------------------------------------------- | |
578 | // client data | |
579 | // ---------------------------------------------------------------------------- | |
580 | ||
581 | void* wxListBox::DoGetItemClientData(unsigned int n) const | |
582 | { | |
583 | wxCHECK_MSG( IsValid(n), NULL, | |
584 | wxT("Invalid index passed to GetItemClientData") ); | |
585 | ||
586 | wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n)); | |
587 | wxCHECK_MSG(entry, NULL, wxT("could not get entry")); | |
588 | ||
589 | return gtk_tree_entry_get_userdata( entry ); | |
590 | } | |
591 | ||
592 | void wxListBox::DoSetItemClientData(unsigned int n, void* clientData) | |
593 | { | |
594 | wxCHECK_RET( IsValid(n), | |
595 | wxT("Invalid index passed to SetItemClientData") ); | |
596 | ||
597 | wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n)); | |
598 | wxCHECK_RET(entry, wxT("could not get entry")); | |
599 | ||
600 | gtk_tree_entry_set_userdata( entry, clientData ); | |
601 | } | |
602 | ||
603 | // ---------------------------------------------------------------------------- | |
604 | // string list access | |
605 | // ---------------------------------------------------------------------------- | |
606 | ||
607 | void wxListBox::SetString(unsigned int n, const wxString& label) | |
608 | { | |
609 | wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") ); | |
610 | wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") ); | |
611 | ||
612 | GtkTreeEntry* entry = GTKGetEntry(n); | |
613 | wxCHECK_RET( entry, wxT("wrong listbox index") ); | |
614 | ||
615 | // update the item itself | |
616 | gtk_tree_entry_set_label(entry, wxGTK_CONV(label)); | |
617 | ||
618 | // and update the model which will refresh the tree too | |
619 | GtkTreeIter iter; | |
620 | wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("failed to get iterator") ); | |
621 | ||
622 | // FIXME: this resets the checked status of a wxCheckListBox item | |
623 | ||
624 | GTKSetItem(iter, entry); | |
625 | } | |
626 | ||
627 | wxString wxListBox::GetString(unsigned int n) const | |
628 | { | |
629 | wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") ); | |
630 | ||
631 | wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n)); | |
632 | wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") ); | |
633 | ||
634 | return wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) ); | |
635 | } | |
636 | ||
637 | unsigned int wxListBox::GetCount() const | |
638 | { | |
639 | wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") ); | |
640 | ||
641 | return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL); | |
642 | } | |
643 | ||
644 | int wxListBox::FindString( const wxString &item, bool bCase ) const | |
645 | { | |
646 | wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") ); | |
647 | ||
648 | //Sort of hackish - maybe there is a faster way | |
649 | unsigned int nCount = wxListBox::GetCount(); | |
650 | ||
651 | for(unsigned int i = 0; i < nCount; ++i) | |
652 | { | |
653 | if( item.IsSameAs( wxListBox::GetString(i), bCase ) ) | |
654 | return (int)i; | |
655 | } | |
656 | ||
657 | ||
658 | // it's not an error if the string is not found -> no wxCHECK | |
659 | return wxNOT_FOUND; | |
660 | } | |
661 | ||
662 | // ---------------------------------------------------------------------------- | |
663 | // selection | |
664 | // ---------------------------------------------------------------------------- | |
665 | ||
666 | void wxListBox::GTKOnActivated(int item) | |
667 | { | |
668 | SendEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, item, IsSelected(item)); | |
669 | } | |
670 | ||
671 | void wxListBox::GTKOnSelectionChanged() | |
672 | { | |
673 | if ( HasFlag(wxLB_MULTIPLE | wxLB_EXTENDED) ) | |
674 | { | |
675 | CalcAndSendEvent(); | |
676 | } | |
677 | else // single selection | |
678 | { | |
679 | const int item = GetSelection(); | |
680 | if ( DoChangeSingleSelection(item) ) | |
681 | SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED, item, true); | |
682 | } | |
683 | } | |
684 | ||
685 | int wxListBox::GetSelection() const | |
686 | { | |
687 | wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox")); | |
688 | wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND, | |
689 | wxT("must be single selection listbox")); | |
690 | ||
691 | GtkTreeIter iter; | |
692 | GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview); | |
693 | ||
694 | // only works on single-sel | |
695 | if (!gtk_tree_selection_get_selected(selection, NULL, &iter)) | |
696 | return wxNOT_FOUND; | |
697 | ||
698 | return GTKGetIndexFor(iter); | |
699 | } | |
700 | ||
701 | int wxListBox::GetSelections( wxArrayInt& aSelections ) const | |
702 | { | |
703 | wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") ); | |
704 | ||
705 | aSelections.Empty(); | |
706 | ||
707 | int i = 0; | |
708 | GtkTreeIter iter; | |
709 | GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview); | |
710 | ||
711 | if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter)) | |
712 | { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead | |
713 | do | |
714 | { | |
715 | if (gtk_tree_selection_iter_is_selected(selection, &iter)) | |
716 | aSelections.Add(i); | |
717 | ||
718 | i++; | |
719 | } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter)); | |
720 | } | |
721 | ||
722 | return aSelections.GetCount(); | |
723 | } | |
724 | ||
725 | bool wxListBox::IsSelected( int n ) const | |
726 | { | |
727 | wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") ); | |
728 | ||
729 | GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview); | |
730 | ||
731 | GtkTreeIter iter; | |
732 | wxCHECK_MSG( GTKGetIteratorFor(n, &iter), false, wxT("Invalid index") ); | |
733 | ||
734 | return gtk_tree_selection_iter_is_selected(selection, &iter); | |
735 | } | |
736 | ||
737 | void wxListBox::DoSetSelection( int n, bool select ) | |
738 | { | |
739 | wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") ); | |
740 | ||
741 | GTKDisableEvents(); | |
742 | ||
743 | GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview); | |
744 | ||
745 | // passing -1 to SetSelection() is documented to deselect all items | |
746 | if ( n == wxNOT_FOUND ) | |
747 | { | |
748 | gtk_tree_selection_unselect_all(selection); | |
749 | GTKEnableEvents(); | |
750 | return; | |
751 | } | |
752 | ||
753 | wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") ); | |
754 | ||
755 | ||
756 | GtkTreeIter iter; | |
757 | wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("Invalid index") ); | |
758 | ||
759 | if (select) | |
760 | gtk_tree_selection_select_iter(selection, &iter); | |
761 | else | |
762 | gtk_tree_selection_unselect_iter(selection, &iter); | |
763 | ||
764 | GtkTreePath* path = gtk_tree_model_get_path( | |
765 | GTK_TREE_MODEL(m_liststore), &iter); | |
766 | ||
767 | gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, FALSE, 0.0f, 0.0f); | |
768 | ||
769 | gtk_tree_path_free(path); | |
770 | ||
771 | GTKEnableEvents(); | |
772 | } | |
773 | ||
774 | void wxListBox::DoScrollToCell(int n, float alignY, float alignX) | |
775 | { | |
776 | wxCHECK_RET( m_treeview, wxT("invalid listbox") ); | |
777 | wxCHECK_RET( IsValid(n), wxT("invalid index")); | |
778 | ||
779 | //RN: I have no idea why this line is needed... | |
780 | if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview)) | |
781 | return; | |
782 | ||
783 | GtkTreeIter iter; | |
784 | if ( !GTKGetIteratorFor(n, &iter) ) | |
785 | return; | |
786 | ||
787 | GtkTreePath* path = gtk_tree_model_get_path( | |
788 | GTK_TREE_MODEL(m_liststore), &iter); | |
789 | ||
790 | // Scroll to the desired cell (0.0 == topleft alignment) | |
791 | gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, | |
792 | TRUE, alignY, alignX); | |
793 | ||
794 | gtk_tree_path_free(path); | |
795 | } | |
796 | ||
797 | void wxListBox::DoSetFirstItem(int n) | |
798 | { | |
799 | DoScrollToCell(n, 0, 0); | |
800 | } | |
801 | ||
802 | void wxListBox::EnsureVisible(int n) | |
803 | { | |
804 | DoScrollToCell(n, 0.5, 0); | |
805 | } | |
806 | ||
807 | // ---------------------------------------------------------------------------- | |
808 | // hittest | |
809 | // ---------------------------------------------------------------------------- | |
810 | ||
811 | int wxListBox::DoListHitTest(const wxPoint& point) const | |
812 | { | |
813 | // gtk_tree_view_get_path_at_pos() also gets items that are not visible and | |
814 | // we only want visible items we need to check for it manually here | |
815 | if ( !GetClientRect().Contains(point) ) | |
816 | return wxNOT_FOUND; | |
817 | ||
818 | // need to translate from master window since it is in client coords | |
819 | gint binx, biny; | |
820 | gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview), | |
821 | &binx, &biny, NULL, NULL, NULL); | |
822 | ||
823 | GtkTreePath* path; | |
824 | if ( !gtk_tree_view_get_path_at_pos | |
825 | ( | |
826 | m_treeview, | |
827 | point.x - binx, | |
828 | point.y - biny, | |
829 | &path, | |
830 | NULL, // [out] column (always 0 here) | |
831 | NULL, // [out] x-coord relative to the cell (not interested) | |
832 | NULL // [out] y-coord relative to the cell | |
833 | ) ) | |
834 | { | |
835 | return wxNOT_FOUND; | |
836 | } | |
837 | ||
838 | int index = gtk_tree_path_get_indices(path)[0]; | |
839 | gtk_tree_path_free(path); | |
840 | ||
841 | return index; | |
842 | } | |
843 | ||
844 | // ---------------------------------------------------------------------------- | |
845 | // helpers | |
846 | // ---------------------------------------------------------------------------- | |
847 | ||
848 | #if wxUSE_TOOLTIPS | |
849 | void wxListBox::GTKApplyToolTip( GtkTooltips *tips, const gchar *tip ) | |
850 | { | |
851 | #if GTK_CHECK_VERSION(2, 12, 0) | |
852 | if (!gtk_check_version(2, 12, 0)) | |
853 | { | |
854 | gtk_widget_set_tooltip_text(GTK_WIDGET( m_treeview ), tip); | |
855 | } | |
856 | else | |
857 | #endif | |
858 | { | |
859 | // RN: Is this needed anymore? | |
860 | gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), tip, NULL ); | |
861 | } | |
862 | } | |
863 | #endif // wxUSE_TOOLTIPS | |
864 | ||
865 | GtkWidget *wxListBox::GetConnectWidget() | |
866 | { | |
867 | // the correct widget for listbox events (such as mouse clicks for example) | |
868 | // is m_treeview, not the parent scrolled window | |
869 | return GTK_WIDGET(m_treeview); | |
870 | } | |
871 | ||
872 | GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const | |
873 | { | |
874 | return gtk_tree_view_get_bin_window(m_treeview); | |
875 | } | |
876 | ||
877 | void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style) | |
878 | { | |
879 | if (m_hasBgCol && m_backgroundColour.Ok()) | |
880 | { | |
881 | GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview); | |
882 | if (window) | |
883 | { | |
884 | m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) ); | |
885 | gdk_window_set_background( window, m_backgroundColour.GetColor() ); | |
886 | gdk_window_clear( window ); | |
887 | } | |
888 | } | |
889 | ||
890 | gtk_widget_modify_style( GTK_WIDGET(m_treeview), style ); | |
891 | } | |
892 | ||
893 | wxSize wxListBox::DoGetBestSize() const | |
894 | { | |
895 | wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view")); | |
896 | ||
897 | // Start with a minimum size that's not too small | |
898 | int cx, cy; | |
899 | GetTextExtent( wxT("X"), &cx, &cy); | |
900 | int lbWidth = 0; | |
901 | int lbHeight = 10; | |
902 | ||
903 | // Find the widest string. | |
904 | const unsigned int count = GetCount(); | |
905 | if ( count ) | |
906 | { | |
907 | int wLine; | |
908 | for ( unsigned int i = 0; i < count; i++ ) | |
909 | { | |
910 | GetTextExtent(GetString(i), &wLine, NULL); | |
911 | if ( wLine > lbWidth ) | |
912 | lbWidth = wLine; | |
913 | } | |
914 | } | |
915 | ||
916 | lbWidth += 3 * cx; | |
917 | ||
918 | // And just a bit more for the checkbox if present and then some | |
919 | // (these are rough guesses) | |
920 | #if wxUSE_CHECKLISTBOX | |
921 | if ( m_hasCheckBoxes ) | |
922 | { | |
923 | lbWidth += 35; | |
924 | cy = cy > 25 ? cy : 25; // rough height of checkbox | |
925 | } | |
926 | #endif | |
927 | ||
928 | // Add room for the scrollbar | |
929 | lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
930 | ||
931 | // Don't make the listbox too tall but don't make it too small neither | |
932 | lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10); | |
933 | ||
934 | wxSize best(lbWidth, lbHeight); | |
935 | CacheBestSize(best); | |
936 | return best; | |
937 | } | |
938 | ||
939 | // static | |
940 | wxVisualAttributes | |
941 | wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
942 | { | |
943 | return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true); | |
944 | } | |
945 | ||
946 | #endif // wxUSE_LISTBOX |