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