File Coverage

blib/lib/Gtk2/Ex/TreeViewBits.pm
Criterion Covered Total %
statement 12 38 31.5
branch 0 10 0.0
condition 0 9 0.0
subroutine 4 7 57.1
pod 3 3 100.0
total 19 67 28.3


line stmt bran cond sub pod time code
1             # Copyright 2007, 2008, 2009, 2010, 2011, 2012 Kevin Ryde
2              
3             # This file is part of Gtk2-Ex-WidgetBits.
4             #
5             # Gtk2-Ex-WidgetBits is free software; you can redistribute it and/or modify
6             # it under the terms of the GNU General Public License as published by the
7             # Free Software Foundation; either version 3, or (at your option) any later
8             # version.
9             #
10             # Gtk2-Ex-WidgetBits is distributed in the hope that it will be useful, but
11             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12             # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with Gtk2-Ex-WidgetBits. If not, see .
17              
18             package Gtk2::Ex::TreeViewBits;
19 1     1   760 use 5.008;
  1         3  
  1         29  
20 1     1   4 use strict;
  1         2  
  1         21  
21 1     1   4 use warnings;
  1         1  
  1         20  
22 1     1   6 use Carp;
  1         1  
  1         3061  
23              
24             # uncomment this to run the ### lines
25             #use Smart::Comments;
26              
27             our $VERSION = 48;
28              
29              
30             sub toggle_expand_row {
31 0     0 1   my ($treeview, $path, $open_all) = @_;
32 0 0         if ($treeview->row_expanded ($path)) {
33 0           $treeview->collapse_row ($path);
34             } else {
35 0           $treeview->expand_row ($path, $open_all);
36             }
37             }
38              
39             sub remove_selected_rows {
40 0     0 1   my ($treeview) = @_;
41 0           my $model = $treeview->get_model;
42              
43             # foreach converting path to rowref frees each path as converted, whereas
44             # a "map" keeps them all until the end, to perhaps save a couple of bytes
45             # of peak memory use.
46             #
47 0           my @rows = $treeview->get_selection->get_selected_rows; # paths
48 0           foreach (@rows) {
49 0           $_ = Gtk2::TreeRowReference->new($model,$_); # rowrefs
50             }
51              
52             # shifting frees each rowref as it's processed, to save
53             # gtk_tree_row_ref_deleted() going through now removed rowrefs
54             #
55 0           while (my $rowref = shift @rows) {
56 0   0       my $path = $rowref->get_path || next; # if somehow gone away
57 0 0         if (my $iter = $model->get_iter ($path)) {
58 0           $model->remove ($iter);
59             } else {
60 0           carp 'Oops, selected row path "',$path->to_string,'" does not exist';
61             }
62             }
63             }
64              
65             # In Gtk 2.12.12, when the row is bigger than the window, set_cursor()
66             # somehow likes to scroll to the opposite end of the row, presumably as a
67             # way of showing you the extents. So if already positioned at the start of
68             # the row then set_cursor() scrolls to the end of it. The scroll_to_cell()
69             # here moves back to the start of the row, but not before an unattractive
70             # bit of flashing. There doesn't seem any clean way to avoid that. It'd be
71             # much better if TreeView didn't draw immediately, but went through the
72             # queue_redraw / process_updates so as to collapse multiple programmatic
73             # changes.
74             #
75             sub scroll_cursor_to_path {
76 0     0 1   my ($treeview, $path) = @_;
77             ### scroll_cursor_to_path() path: $path->to_string
78 0   0       my $model = $treeview->get_model || return; # nothing to make visible
79              
80             # check path exists, in particular since ->scroll_to_cell() gives an
81             # unsightly warning if the path is invalid
82 0 0         $model->get_iter($path) or return;
83              
84 0           $treeview->expand_to_path ($path);
85 0           $treeview->set_cursor ($path);
86              
87 0   0       my $bin_window = $treeview->get_bin_window || return; # if unrealized
88              
89 0           my ($bin_width, $bin_height) = $bin_window->get_size;
90             ### $bin_height
91              
92 0           my $rect = $treeview->get_cell_area ($path, undef);
93             ### path: "y=".$rect->y." height=".$rect->height." end=".($rect->y + $rect->height)
94              
95 0 0 0       if ($rect->y >= 0 && $rect->y + $rect->height <= $bin_height) {
96             ### fully visible, don't scroll
97 0           return;
98             }
99 0 0         my $row_align = ($rect->height > $bin_height ? 0 : 0.5);
100             ### scroll align to: $row_align
101 0           $treeview->scroll_to_cell ($path,
102             undef, # no column scroll
103             1, # use_align
104             $row_align,
105             0); # col_align
106             }
107              
108             1;
109             __END__