-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathSourceList.vala
More file actions
2945 lines (2493 loc) · 111 KB
/
Copy pathSourceList.vala
File metadata and controls
2945 lines (2493 loc) · 111 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2019 elementary, Inc. (https://elementary.io)
* Copyright 2012-2014 Victor Martinez <victoreduardm@gmail.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
namespace Code.Widgets {
/**
* An interface for sorting items.
*
* @since 0.3
*/
public interface SourceListSortable : SourceList.ExpandableItem {
/**
* Emitted after a user has re-ordered an item via DnD.
*
* @param moved The item that was moved to a different position by the user.
* @since 0.3
*/
public signal void user_moved_item (SourceList.Item moved);
/**
* Whether this item will allow users to re-arrange its children via DnD.
*
* This feature can co-exist with a sort algorithm (implemented
* by {@link Code.Widgets.SourceListSortable.compare}), but
* the actual order of the items in the list will always
* honor that method. The sort function has to be compatible with
* the kind of DnD reordering the item wants to allow, since the user can
* only reorder those items for which //compare// returns 0.
*
* @return Whether the item's children can be re-arranged by users.
* @since 0.3
*/
public abstract bool allow_dnd_sorting ();
/**
* Should return a negative integer, zero, or a positive integer if ''a''
* sorts //before// ''b'', ''a'' sorts //with// ''b'', or ''a'' sorts
* //after// ''b'' respectively. If two items compare as equal, their
* order in the sorted source list is undefined.
*
* In order to ensure that the source list behaves as expected, this
* method must define a partial order on the source list tree; i.e. it
* must be reflexive, antisymmetric and transitive. Not complying with
* those requirements could make the program fall into an infinite loop
* and freeze the user interface.
*
* Should return //0// to allow any pair of items to be sortable via DnD.
*
* @param a First item.
* @param b Second item.
* @return A //negative// integer if //a// sorts before //b//,
* //zero// if //a// equals //b//, or a //positive//
* integer if //a// sorts after //b//.
* @since 0.3
*/
public abstract int compare (SourceList.Item a, SourceList.Item b);
}
/**
* An interface for dragging items out of the source list widget.
*
* @since 0.3
*/
public interface SourceListDragSource : SourceList.Item {
/**
* Determines whether this item can be dragged outside the source list widget.
*
* Even if this method returns //false//, the item could still be dragged around
* within the source list if its parent allows DnD reordering. This only happens
* when the parent implements {@link Code.Widgets.SourceListSortable}.
*
* @return //true// if the item can be dragged; //false// otherwise.
* @since 0.3
* @see Code.Widgets.SourceListSortable
*/
public abstract bool draggable ();
/**
* This method is called when the drop site requests the data which is dragged.
*
* It is the responsibility of this method to fill //selection_data// with the
* data in the format which is indicated by {@link Gtk.SelectionData.get_target}.
*
* @param selection_data {@link Gtk.SelectionData} containing source data.
* @since 0.3
* @see Gtk.SelectionData.set
* @see Gtk.SelectionData.set_uris
* @see Gtk.SelectionData.set_text
*/
public abstract void prepare_selection_data (Gtk.SelectionData selection_data);
}
/**
* An interface for receiving data from other widgets via drag-and-drop.
*
* @since 0.3
*/
public interface SourceListDragDest : SourceList.Item {
/**
* Determines whether //data// can be dropped into this item.
*
* @param context The drag context.
* @param data {@link Gtk.SelectionData} containing source data.
* @return //true// if the drop is possible; //false// otherwise.
* @since 0.3
*/
public abstract bool data_drop_possible (Gdk.DragContext context, Gtk.SelectionData data);
/**
* If a data drop is deemed possible, then this method is called
* when the data is actually dropped into this item. Any actions
* consequence of the data received should be handled here.
*
* @param context The drag context.
* @param data {@link Gtk.SelectionData} containing source data.
* @return The action taken, or //0// to indicate that the dropped data was not accepted.
* @since 0.3
*/
public abstract Gdk.DragAction data_received (Gdk.DragContext context, Gtk.SelectionData data);
}
/**
* A widget that can display a list of items organized in categories.
*
* The source list widget consists of a collection of items, some of which are also expandable (and
* thus can contain more items). All the items displayed in the source list are children of the widget's
* root item. The API is meant to be used as follows:
*
* 1. Create the items you want to display in the source list, setting the appropriate values for their
* properties. The desired hierarchy is achieved by creating expandable items and adding items to them.
* These will be displayed as descendants in the widget's tree structure. The expandable items that are
* not nested inside any other item are considered to be at root level, and should be added to
* the widget's root item.<<BR>>
*
* Expandable items located at the root level are treated as categories, and only support text.
*
* ''Example''<<BR>>
* The final tree will have the following structure:
* {{{
* Libraries
* Music
* Stores
* My Store
* Music
* Podcasts
* Devices
* Player 1
* Player 2
* }}}
*
* {{{
* var library_category = new Code.Widgets.SourceList.ExpandableItem ("Libraries");
* var store_category = new Code.Widgets.SourceList.ExpandableItem ("Stores");
* var device_category = new Code.Widgets.SourceList.ExpandableItem ("Devices");
*
* var music_item = new Code.Widgets.SourceList.Item ("Music");
*
* // "Libraries" will be the parent category of "Music"
* library_category.add (music_item);
*
* // We plan to add sub-items to the store, so let's use an expandable item
* var my_store_item = new Code.Widgets.SourceList.ExpandableItem ("My Store");
* store_category.add (my_store_item);
*
* var my_store_podcast_item = new Code.Widgets.SourceList.Item ("Podcasts");
* var my_store_music_item = new Code.Widgets.SourceList.Item ("Music");
*
* my_store_item.add (my_store_music_item);
* my_store_item.add (my_store_podcast_item);
*
* var player1_item = new Code.Widgets.SourceList.Item ("Player 1");
* var player2_item = new Code.Widgets.SourceList.Item ("Player 2");
*
* device_category.add (player1_item);
* device_category.add (player2_item);
* }}}
*
* 2. Create a source list widget.<<BR>>
* {{{
* var source_list = new Code.Widgets.SourceList ();
* }}}
*
* 3. Add root-level items to the {@link Code.Widgets.SourceList.root} item.
* This item only serves as a container, and all its properties are ignored by the widget.
*
* {{{
* // This will add the main categories (including their children) to the source list. After
* // having being added to be widget, any other item added to any of these items
* // (or any other child item in a deeper level) will be automatically added too.
* // There's no need to deal with the source list widget directly.
*
* var root = source_list.root;
*
* root.add (library_category);
* root.add (store_category);
* root.add (device_category);
* }}}
*
* The steps mentioned above are enough for initializing the source list. Future changes to the items'
* properties are ''automatically'' reflected by the widget.
*
* Final steps would involve connecting handlers to the source list events, being
* {@link Code.Widgets.SourceList.item_selected} the most important, as it indicates that
* the selection was modified.
*
* Pack the source list into the GUI using the {@link Gtk.Paned} widget.
* This is usually done as follows:
* {{{
* var pane = new Gtk.Paned (Gtk.Orientation.HORIZONTAL);
* pane.pack1 (source_list, false, false);
* pane.pack2 (content_area, true, false);
* }}}
*
* @since 0.2
* @see Gtk.Paned
*/
public class SourceList : Gtk.ScrolledWindow {
/**
* = WORKING INTERNALS =
*
* In order to offer a transparent Item-based API, and avoid the need of providing methods
* to deal with items directly on the SourceList widget, it was decided to follow a monitor-like
* implementation, where the source list permanently monitors its root item and any other
* child item added to it. The task of monitoring the properties of the items has been
* divided among different objects, as shown below:
*
* Monitored by: Object::method that receives the signals indicating the property change.
* Applied by: Object::method that actually updates the tree to reflect the property changes
* (directly or indirectly, as in the case of the tree data model).
*
* ---------------------------------------------------------------------------------------------
* PROPERTY | MONITORED BY | APPLIED BY
* ---------------------------------------------------------------------------------------------
* + Item | |
* - parent | Not monitored | N/A
* - name | DataModel::on_item_prop_changed | Tree::name_cell_data_func
* - editable | DataModel::on_item_prop_changed | Queried when needed (See Tree::start_editing_item)
* - visible | DataModel::on_item_prop_changed | DataModel::filter_visible_func
* - icon | DataModel::on_item_prop_changed | Tree::icon_cell_data_func
* - activatable | Same as @icon | Same as @icon
* + ExpandableItem | |
* - collapsible | DataModel::on_item_prop_changed | Tree::update_expansion
* | | Tree::expander_cell_data_func
* - expanded | Same as @collapsible | Same as @collapsible
* ---------------------------------------------------------------------------------------------
* * Only automatic properties are monitored. ExpandableItem's additions/removals are handled by
* DataModel::add_item() and DataModel::remove_item()
*
* Other features:
* - Sorting: this happens on the tree-model level (DataModel).
*/
/**
* A source list entry.
*
* Any change made to any of its properties will be ''automatically'' reflected
* by the {@link Code.Widgets.SourceList} widget.
*
* @since 0.2
*/
public class Item : Object {
/**
* Emitted when the user has finished editing the item's name.
*
* By default, if the name doesn't consist of white space, it is automatically assigned
* to the {@link Code.Widgets.SourceList.Item.name} property. The default behavior can
* be changed by overriding this signal.
* @param new_name The item's new name (result of editing.)
* @since 0.2
*/
public virtual signal void edited (string new_name) {
if (editable && new_name.strip () != "")
this.name = new_name;
}
/**
* The {@link Code.Widgets.SourceList.Item.activatable} icon was activated.
*
* @see Code.Widgets.SourceList.Item.activatable
* @since 0.2
*/
public virtual signal void action_activated () { }
/**
* Emitted when the item is double-clicked or when it is selected and one of the keys:
* Space, Shift+Space, Return or Enter is pressed. This signal is //also// for
* editable items.
*
* @since 0.2
*/
public virtual signal void activated () { }
/**
* Parent {@link Code.Widgets.SourceList.ExpandableItem} of the item.
* ''Must not'' be modified.
*
* @since 0.2
*/
public ExpandableItem parent { get; internal set; }
/**
* The item's name. Primary and most important information.
*
* @since 0.2
*/
public string name { get; set; default = ""; }
/**
* The item's tooltip. If set to null (default), the tooltip for the item will be the
* contents of the {@link Code.Widgets.SourceList.Item.name} property.
*
* @since 5.3
*/
public string? tooltip { get; set; default = null; }
/**
* Markup to be used instead of {@link Code.Widgets.SourceList.ExpandableItem.name}
* This would mean that &, <, etc have to be escaped in the text, but basic formatting
* can be done on the item with HTML style tags.
*
* Note: Only the {@link Code.Widgets.SourceList.ExpandableItem.name} property
* is modified for editable items. So this property will be need to updated and
* reformatted with editable items.
*
* @since 5.0
*/
public string? markup { get; set; default = null; }
/**
* A badge shown next to the item's name.
*
* It can be used for displaying the number of unread messages in the "Inbox" item,
* for instance.
*
* @since 0.2
*/
public string badge { get; set; default = ""; }
/**
* Whether the item's name can be edited from within the source list.
*
* When this property is set to //true//, users can edit the item by pressing
* the F2 key, or by double-clicking its name.
*
* ''This property only works for selectable items''.
*
* @see Code.Widgets.SourceList.Item.selectable
* @see Code.Widgets.SourceList.start_editing_item
* @since 0.2
*/
public bool editable { get; set; default = false; }
/**
* Whether the item should appear in the source list's tree or not.
*
* @since 0.2
*/
public bool visible { get; set; default = true; }
/**
* Whether the item can be selected or not.
*
* Setting this property to true doesn't guarantee that the item will actually be
* selectable, since there are other external factors to take into account, like the
* item's {@link Code.Widgets.SourceList.Item.visible} property; whether the item is
* a category; the parent item is collapsed, etc.
*
* @see Code.Widgets.SourceList.Item.visible
* @since 0.2
*/
public bool selectable { get; set; default = true; }
/**
* Primary icon.
*
* This property should be used to give the user an idea of what the item represents
* (i.e. content type.)
*
* @since 0.2
*/
public Icon icon { get; set; }
/**
* An activatable icon that works like a button.
*
* It can be used for e.g. showing an //"eject"// icon on a device's item.
*
* @see Code.Widgets.SourceList.Item.action_activated
* @since 0.2
*/
public Icon activatable { get; set; }
/**
* The tooltip for the activatable icon.
*
* @since 5.0
*/
public string activatable_tooltip { get; set; default = ""; }
/**
* Creates a new {@link Code.Widgets.SourceList.Item}.
*
* @param name Name of the item.
* @return (transfer full) A new {@link Code.Widgets.SourceList.Item}.
* @since 0.2
*/
public Item (string name = "") {
this.name = name;
}
/**
* Invoked when the item is secondary-clicked or when the usual menu keys are pressed.
*
* Note that since Granite 5.0, right clicking on an item no longer selects/activates it, so
* any context menu items should be actioned on the item instance rather than the selected item
* in the SourceList
*
* @return A {@link GLib.Menu} or //null// if nothing should be displayed.
* @since 0.2
*/
public virtual GLib.Menu? get_context_menu () {
return null;
}
}
/**
* An item that can contain more items.
*
* It supports all the properties inherited from {@link Code.Widgets.SourceList.Item},
* and behaves like a normal item, except when it is located at the root level; in that case,
* the following properties are ignored by the widget:
*
* * {@link Code.Widgets.SourceList.Item.selectable}
* * {@link Code.Widgets.SourceList.Item.editable}
* * {@link Code.Widgets.SourceList.Item.icon}
* * {@link Code.Widgets.SourceList.Item.activatable}
* * {@link Code.Widgets.SourceList.Item.badge}
*
* Root-level expandable items (i.e. Main Categories) are ''not'' displayed when they contain
* zero visible children.
*
* @since 0.2
*/
public class ExpandableItem : Item {
/**
* Emitted when an item is added.
*
* @param item Item added.
* @see Code.Widgets.SourceList.ExpandableItem.add
* @since 0.2
*/
public signal void child_added (Item item);
/**
* Emitted when an item is removed.
*
* @param item Item removed.
* @see Code.Widgets.SourceList.ExpandableItem.remove
* @since 0.2
*/
public signal void child_removed (Item item);
/**
* Emitted when the item is expanded or collapsed.
*
* @since 0.2
*/
public virtual signal void toggled () { }
/**
* Whether the item is collapsible or not.
*
* When set to //false//, the item is //always// expanded and the expander is
* not shown. Please note that this will also affect the value returned by the
* {@link Code.Widgets.SourceList.ExpandableItem.expanded} property.
*
* @see Code.Widgets.SourceList.ExpandableItem.expanded
* @since 0.2
*/
public bool collapsible { get; set; default = true; }
/**
* Whether the item is expanded or not.
*
* The source list widget will obey the value of this property when possible.
*
* This property has no effect when {@link Code.Widgets.SourceList.ExpandableItem.collapsible}
* is set to //false//. Also keep in mind that, __when set to //true//__, this property
* doesn't always represent the actual expansion state of an item. For example, it might
* be the case that an expandable item is collapsed because it has zero visible children,
* but its //expanded// property value is still //true//; in such case, once one of the
* item's children becomes visible, the item will be expanded again. Same applies to items
* hidden behind a collapsed parent item.
*
* If obtaining the ''actual'' expansion state of an item is important,
* use {@link Code.Widgets.SourceList.is_item_expanded} instead.
*
* @see Code.Widgets.SourceList.ExpandableItem.collapsible
* @see Code.Widgets.SourceList.is_item_expanded
* @since 0.2
*/
private bool _expanded = false;
public bool expanded {
get { return _expanded || !collapsible; } // if not collapsible, always return true
set {
if (value != _expanded) {
_expanded = value;
toggled ();
}
}
}
/**
* Number of children contained by the item.
*
* @since 0.2
*/
public uint n_children {
get { return children_list.size; }
}
/**
* The item's children.
*
* This returns a newly-created list containing the children.
* It's safe to iterate it while removing items with
* {@link Code.Widgets.SourceList.ExpandableItem.remove}
*
* @since 0.2
*/
public Gee.Collection<Item> children {
owned get {
// Create a copy of the children so that it's safe to iterate it
// (e.g. by using foreach) while removing items.
var children_list_copy = new Gee.ArrayList<Item> ();
children_list_copy.add_all (children_list);
return children_list_copy;
}
}
private Gee.Collection<Item> children_list = new Gee.ArrayList<Item> ();
/**
* Creates a new {@link Code.Widgets.SourceList.ExpandableItem}
*
* @param name Title of the item.
* @return (transfer full) A new {@link Code.Widgets.SourceList.ExpandableItem}.
* @since 0.2
*/
public ExpandableItem (string name = "") {
base (name);
}
construct {
editable = false;
}
/**
* Checks whether the item contains the specified child.
*
* This method only considers the item's immediate children.
*
* @param item Item to search.
* @return Whether the item was found or not.
* @since 0.2
*/
public bool contains (Item item) {
return item in children_list;
}
/**
* Adds an item.
*
* {@link Code.Widgets.SourceList.ExpandableItem.child_added} is fired after the item is added.
*
* While adding a child item, //the item it's being added to will set itself as the parent//.
* Please note that items are required to have their //parent// property set to //null// before
* being added, so make sure the item is removed from its previous parent before attempting
* to add it to another item. For instance:
* {{{
* if (item.parent != null)
* item.parent.remove (item); // this will set item's parent to null
* new_parent.add (item);
* }}}
*
* @param item The item to add. Its parent __must__ be //null//.
* @see Code.Widgets.SourceList.ExpandableItem.child_added
* @see Code.Widgets.SourceList.ExpandableItem.remove
* @since 0.2
*/
public void add (Item item) requires (item.parent == null) {
item.parent = this;
children_list.add (item);
child_added (item);
}
/**
* Removes an item.
*
* The {@link Code.Widgets.SourceList.ExpandableItem.child_removed} signal is fired
* //after removing the item//. Finally (i.e. after all the handlers have been invoked),
* the item's {@link Code.Widgets.SourceList.Item.parent} property is set to //null//.
* This has the advantage of letting signal handlers know the parent from which //item//
* is being removed.
*
* @param item The item to remove. This will fail if item has a different parent.
* @see Code.Widgets.SourceList.ExpandableItem.child_removed
* @see Code.Widgets.SourceList.ExpandableItem.clear
* @since 0.2
*/
public void remove (Item item) requires (item.parent == this) {
children_list.remove (item);
child_removed (item);
item.parent = null;
}
/**
* Removes all the items contained by the item. It works similarly to
* {@link Code.Widgets.SourceList.ExpandableItem.remove}.
*
* @see Code.Widgets.SourceList.ExpandableItem.remove
* @see Code.Widgets.SourceList.ExpandableItem.child_removed
* @since 0.2
*/
public void clear () {
foreach (var item in children)
remove (item);
}
/**
* Expands the item and/or its children.
*
* @param inclusive Whether to also expand this item (true), or only its children (false).
* @param recursive Whether to recursively expand all the children (true), or only
* immediate children (false).
* @see Code.Widgets.SourceList.ExpandableItem.expanded
* @since 0.2
*/
public void expand_all (bool inclusive = true, bool recursive = true) {
set_expansion (this, inclusive, recursive, true);
}
/**
* Collapses the item and/or its children.
*
* @param inclusive Whether to also collapse this item (true), or only its children (false).
* @param recursive Whether to recursively collapse all the children (true), or only
* immediate children (false).
* @see Code.Widgets.SourceList.ExpandableItem.expanded
* @since 0.2
*/
public void collapse_all (bool inclusive = true, bool recursive = true) {
set_expansion (this, inclusive, recursive, false);
}
private static void set_expansion (ExpandableItem item, bool inclusive, bool recursive, bool expanded) {
if (inclusive)
item.expanded = expanded;
foreach (var child_item in item.children) {
var child_expandable_item = child_item as ExpandableItem;
if (child_expandable_item != null) {
if (recursive)
set_expansion (child_expandable_item, true, true, expanded);
else
child_expandable_item.expanded = expanded;
}
}
}
/**
* Recursively expands the item along with its parent(s).
*
* @see Code.Widgets.SourceList.ExpandableItem.expanded
* @since 0.2
*/
public void expand_with_parents () {
// Update parent items first due to GtkTreeView's working internals:
// Expanding children before their parents would not always work, because
// they could be obscured behind a collapsed row by the time the treeview
// tries to expand them, obviously failing.
if (parent != null)
parent.expand_with_parents ();
expanded = true;
}
/**
* Recursively collapses the item along with its parent(s).
*
* @see Code.Widgets.SourceList.ExpandableItem.expanded
* @since 0.2
*/
public void collapse_with_parents () {
if (parent != null)
parent.collapse_with_parents ();
expanded = false;
}
}
/**
* The model backing the SourceList tree.
*
* It monitors item property changes, and handles children additions and removals. It also controls
* the visibility of the items based on their "visible" property, and on their number of children,
* if they happen to be categories. Its main purpose is to provide an easy and practical interface
* for sorting, adding, removing and updating items, eliminating the need of repeatedly dealing with
* the Gtk.TreeModel API directly.
*/
private class DataModel : Gtk.TreeModelFilter, Gtk.TreeDragSource, Gtk.TreeDragDest {
/**
* An object that references a particular row in a model. This class is a wrapper built around
* Gtk.TreeRowReference, and exists with the purpose of ensuring we never use invalid tree paths
* or iters in the model, since most of these errors provoke failures due to GTK+ assertions
* or, even worse, unexpected behavior.
*/
private class NodeWrapper {
/**
* The actual reference to the node. If is is null, it is treated as invalid.
*/
private Gtk.TreeRowReference? row_reference;
/**
* A newly-created Gtk.TreeIter pointing to the node if it exists; null otherwise.
*/
public Gtk.TreeIter? iter {
owned get {
Gtk.TreeIter? rv = null;
if (valid) {
var _path = this.path;
if (_path != null) {
Gtk.TreeIter _iter;
if (row_reference.get_model ().get_iter (out _iter, _path))
rv = _iter;
}
}
return rv;
}
}
/**
* A newly-created Gtk.TreePath pointing to the node if it exists; null otherwise.
*/
public Gtk.TreePath? path {
owned get { return valid ? row_reference.get_path () : null; }
}
/**
* Whether the node is valid or not. When it is not valid, no valid references are
* returned by the object to avoid errors (null is returned instead).
*/
public bool valid {
get { return row_reference != null && row_reference.valid (); }
}
public NodeWrapper (Gtk.TreeModel model, Gtk.TreeIter iter) {
row_reference = new Gtk.TreeRowReference (model, model.get_path (iter));
}
}
/**
* Helper object used to monitor item property changes.
*/
private class ItemMonitor {
public signal void changed (Item self, string prop_name);
private Item item;
public ItemMonitor (Item item) {
this.item = item;
item.notify.connect_after (on_notify);
}
~ItemMonitor () {
item.notify.disconnect (on_notify);
}
private void on_notify (ParamSpec prop) {
changed (item, prop.name);
}
}
private enum Column {
ITEM,
N_COLUMNS;
public Type type () {
switch (this) {
case ITEM:
return typeof (Item);
default:
assert_not_reached (); // a Type must be returned for every valid column
}
}
}
public signal void item_updated (Item item);
/**
* Used by push_parent_update() as key to associate the respective data to the objects.
*/
private const string ITEM_PARENT_NEEDS_UPDATE = "item-parent-needs-update";
private ExpandableItem _root;
/**
* Root item.
*
* This item is not actually part of the model. It's only used as a proxy
* for adding and removing items.
*/
public ExpandableItem root {
get { return _root; }
set {
if (_root != null) {
remove_children_monitor (_root);
foreach (var item in _root.children)
remove_item (item);
}
_root = value;
add_children_monitor (_root);
foreach (var item in _root.children)
add_item (item);
}
}
// This hash map stores items and their respective child node references. For that reason, the
// references it contains should only be used on the child_tree model, or converted to filter
// iters/paths using convert_child_*_to_*() before using them with the filter (i.e. this) model.
private Gee.HashMap<Item, NodeWrapper> items = new Gee.HashMap<Item, NodeWrapper> ();
private Gee.HashMap<Item, ItemMonitor> monitors = new Gee.HashMap<Item, ItemMonitor> ();
private Gtk.TreeStore child_tree;
private unowned SourceList.VisibleFunc? filter_func;
public DataModel () {
}
construct {
child_tree = new Gtk.TreeStore (Column.N_COLUMNS, Column.ITEM.type ());
child_model = child_tree;
virtual_root = null;
child_tree.set_default_sort_func (child_model_sort_func);
resort ();
set_visible_func (filter_visible_func);
}
public bool has_item (Item item) {
return items.has_key (item);
}
public void update_item (Item item) {
if (!has_item (item)) {
return;
}
assert (root != null);
// Emitting row_changed() for this item's row in the child model causes the filter
// (i.e. this model) to re-evaluate whether a row is visible or not, calling
// filter_visible_func() for that row again, and that's exactly what we want.
var node_reference = items.get (item);
if (node_reference != null) {
var path = node_reference.path;
var iter = node_reference.iter;
if (path != null && iter != null) {
child_tree.row_changed (path, iter);
item_updated (item);
}
}
}
private void add_item (Item item) requires (!has_item (item)) {
assert (root != null);
// Find the parent iter
Gtk.TreeIter? parent_child_iter = null, child_iter;
var parent = item.parent;
if (parent != null && parent != root) {
// Add parent if it hasn't been added yet
if (!has_item (parent))
add_item (parent);
// Try to find the parent's iter
parent_child_iter = get_item_child_iter (parent);
// Parent must have been added prior to adding this item
assert (parent_child_iter != null);
}
child_tree.append (out child_iter, parent_child_iter);
child_tree.set (child_iter, Column.ITEM, item, -1);
items.set (item, new NodeWrapper (child_tree, child_iter));
// This is equivalent to a property change. The tree still needs to update
// some of the new item's properties through this signal's handler.
item_updated (item);
add_property_monitor (item);
push_parent_update (parent);
// If the item is expandable, also add children
var expandable = item as ExpandableItem;
if (expandable != null) {
foreach (var child_item in expandable.children)
add_item (child_item);
// Monitor future additions/removals through signal handlers
add_children_monitor (expandable);
}
}
private void remove_item (Item item) requires (has_item (item)) {
assert (root != null);
remove_property_monitor (item);
// get_item_child_iter() depends on items.get(item) for retrieving the right reference,
// so don't unset the item from @items yet! We first get the child iter and then
// unset the value.
var child_iter = get_item_child_iter (item);
// Now we remove the item from the table, because that way get_item_child_iter() and
// all the methods that depend on it won't return invalid iters or items when
// called. This is important because child_tree.remove() will emit row_deleted(),
// and its handlers could potentially depend on one of the methods mentioned above.
items.unset (item);
if (child_iter != null)
child_tree.remove (ref child_iter);
push_parent_update (item.parent);
// If the item is expandable, also remove children
var expandable = item as ExpandableItem;
if (expandable != null) {
// No longer monitor future additions or removals
remove_children_monitor (expandable);
foreach (var child_item in expandable.children)
remove_item (child_item);
}
}
private void add_property_monitor (Item item) {
var wrapper = new ItemMonitor (item);
monitors[item] = wrapper;
wrapper.changed.connect (on_item_prop_changed);
}
private void remove_property_monitor (Item item) {
var wrapper = monitors[item];
if (wrapper != null)
wrapper.changed.disconnect (on_item_prop_changed);
monitors.unset (item);
}
private void add_children_monitor (ExpandableItem item) {
item.child_added.connect_after (on_item_child_added);
item.child_removed.connect_after (on_item_child_removed);
}
private void remove_children_monitor (ExpandableItem item) {
item.child_added.disconnect (on_item_child_added);
item.child_removed.disconnect (on_item_child_removed);
}
private void on_item_child_added (Item item) {
add_item (item);
}
private void on_item_child_removed (Item item) {
remove_item (item);
}
private void on_item_prop_changed (Item item, string prop_name) {