| 1 | |||
| 2 | package Data::TreeDumper::Renderer::GTK ; | ||
| 3 | |||
| 4 | 1 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | 1 | ▶ | use AutoLoader qw(AUTOLOAD); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | ||
| 12 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 13 | our @EXPORT = qw(); | ||
| 14 | |||
| 15 | our $VERSION = '0.03'; | ||
| 16 | |||
| 17 | 1 | ▶ | use Data::TreeDumper ; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | |||
| 19 | 0 | ▶ | use Gtk3 -init; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 20 | use Glib ':constants'; | ||
| 21 | |||
| 22 | use base qw(Gtk3::TreeView Exporter); | ||
| 23 | |||
| 24 | sub new | ||
| 25 | { | ||
| 26 | my $class = shift; | ||
| 27 | my %args = (data => undef, @_); | ||
| 28 | |||
| 29 | my $self = bless Gtk3::TreeView->new, $class; | ||
| 30 | |||
| 31 | $self->insert_column_with_attributes(0, 'Data', Gtk3::CellRendererText->new, text => 0); | ||
| 32 | $self->set_data ($args{data}, $args{dumper_setup}) if exists $args{data} ; | ||
| 33 | $self->set_title ($args{title}); | ||
| 34 | |||
| 35 | $self->signal_connect | ||
| 36 | ( | ||
| 37 | button_press_event => | ||
| 38 | sub | ||
| 39 | { | ||
| 40 | my ($widget, $event) = @_; | ||
| 41 | if ($event->button == 3) { | ||
| 42 | _do_context_menu ($widget, $event); | ||
| 43 | return TRUE; | ||
| 44 | } | ||
| 45 | |||
| 46 | return FALSE; | ||
| 47 | } | ||
| 48 | ); | ||
| 49 | |||
| 50 | |||
| 51 | return $self; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub _do_context_menu | ||
| 55 | { | ||
| 56 | my ($self, $event) = @_; | ||
| 57 | my $menu = Gtk3::Menu->new; | ||
| 58 | foreach my $method ('expand_all', 'collapse_all') { | ||
| 59 | my $label = join ' ', map { ucfirst $_ } split /_/, $method; | ||
| 60 | my $item = Gtk3::MenuItem->new ($label); | ||
| 61 | $menu->append ($item); | ||
| 62 | $item->show; | ||
| 63 | $item->signal_connect (activate => sub { | ||
| 64 | $self->$method; | ||
| 65 | }); | ||
| 66 | } | ||
| 67 | $menu->popup (undef, undef, undef, undef, $event->button, $event->time); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub set_data | ||
| 71 | { | ||
| 72 | my ($self, $data, $dumper_setup) = @_; | ||
| 73 | |||
| 74 | my $model = Gtk3::TreeStore->new ('Glib::String'); | ||
| 75 | |||
| 76 | DumpTree | ||
| 77 | ( | ||
| 78 | $data | ||
| 79 | , 'GTK-perl data dump' | ||
| 80 | , %$dumper_setup | ||
| 81 | , RENDERER => | ||
| 82 | { | ||
| 83 | NODE => \&RenderNode | ||
| 84 | |||
| 85 | # data needed by the renderer | ||
| 86 | , PREVIOUS_LEVEL => 0 | ||
| 87 | , MODEL => $model | ||
| 88 | , PARENT => [Gtk3::TreePath->new()] | ||
| 89 | } | ||
| 90 | ) ; | ||
| 91 | |||
| 92 | $self->set_model ($model); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub set_title | ||
| 96 | { | ||
| 97 | my ($self, $title) = @_; | ||
| 98 | |||
| 99 | if (defined $title and length $title) { | ||
| 100 | $self->get_column (0)->set_title ($title); | ||
| 101 | $self->set_headers_visible (TRUE); | ||
| 102 | } else { | ||
| 103 | $self->set_headers_visible (FALSE); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | #------------------------------------------------------------------------------------------- | ||
| 109 | |||
| 110 | sub RenderNode | ||
| 111 | { | ||
| 112 | my | ||
| 113 | ( | ||
| 114 | $element | ||
| 115 | , $level | ||
| 116 | , $is_terminal | ||
| 117 | , $previous_level_separator | ||
| 118 | , $separator | ||
| 119 | , $element_name | ||
| 120 | , $element_value | ||
| 121 | , $td_address | ||
| 122 | , $address_link | ||
| 123 | , $perl_size | ||
| 124 | , $perl_address | ||
| 125 | , $setup | ||
| 126 | ) = @_ ; | ||
| 127 | |||
| 128 | my $model = $setup->{RENDERER}{MODEL} ; | ||
| 129 | my $parents = $setup->{RENDERER}{PARENT} ; | ||
| 130 | my $previous_level = $setup->{RENDERER}{PREVIOUS_LEVEL} ; | ||
| 131 | |||
| 132 | # wind up the parents list if necessary | ||
| 133 | splice @$parents, 0, ($previous_level - $level) if($level < $previous_level) ; | ||
| 134 | |||
| 135 | my $path = $parents->[0] ; | ||
| 136 | my $parent = $model->get_iter($path) if($path->get_depth() > 0) ; | ||
| 137 | |||
| 138 | $element_value = " = $element_value" if($element_value ne '') ; | ||
| 139 | |||
| 140 | my $address = $td_address ; | ||
| 141 | $address .= "-> $address_link" if defined $address_link ; | ||
| 142 | |||
| 143 | $perl_size = "<$perl_size>" if $perl_size ne '' ; | ||
| 144 | |||
| 145 | my $rendering ; | ||
| 146 | if($setup->{DISPLAY_ADDRESS}) | ||
| 147 | { | ||
| 148 | $rendering = "$element_name$element_value [$address] $perl_size $perl_address" ; | ||
| 149 | } | ||
| 150 | else | ||
| 151 | { | ||
| 152 | $rendering = "$element_name$element_value $perl_size $perl_address" ; | ||
| 153 | } | ||
| 154 | |||
| 155 | unless($is_terminal) | ||
| 156 | { | ||
| 157 | my $parent = $model->append ($parent); | ||
| 158 | $model->set($parent, 0, $rendering); | ||
| 159 | |||
| 160 | my $path = $model->get_path($parent) ; | ||
| 161 | unshift @{$setup->{RENDERER}{PARENT}}, $path ; | ||
| 162 | } | ||
| 163 | else | ||
| 164 | { | ||
| 165 | $model->set($model->append($parent),0, $rendering); | ||
| 166 | } | ||
| 167 | |||
| 168 | $setup->{RENDERER}{PREVIOUS_LEVEL} = $level ; | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | 1; | ||
| 173 | |||
| 174 | __END__ | ||