| 1 | package Gtk2::Ex::MindMapView::Border; | ||
| 2 | |||
| 3 | our $VERSION = '0.000001'; | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | use List::Util; | ||
| 10 | |||
| 11 | use Gnome2::Canvas; | ||
| 12 | |||
| 13 | use Gtk2::Ex::MindMapView::ArgUtils; | ||
| 14 | |||
| 15 | sub new | ||
| 16 | { | ||
| 17 | my $class = shift(@_); | ||
| 18 | |||
| 19 | my $self = {}; | ||
| 20 | |||
| 21 | bless $self, $class; | ||
| 22 | |||
| 23 | my %attributes = @_; | ||
| 24 | |||
| 25 | args_required(\%attributes, qw(group content)); | ||
| 26 | |||
| 27 | args_store($self, \%attributes); | ||
| 28 | |||
| 29 | if (!$self->{group}->isa('Gnome2::Canvas::Group')) | ||
| 30 | { | ||
| 31 | croak "Unexpected value for group. Valid is: Gnome2::Canvas::Group\n"; | ||
| 32 | } | ||
| 33 | |||
| 34 | if (!$self->{content}->isa('Gtk2::Ex::MindMapView::Content')) | ||
| 35 | { | ||
| 36 | croak "Unexpected value for content. Valid is: " . | ||
| 37 | "Gtk2::Ex::MindMapView::Content\n"; | ||
| 38 | } | ||
| 39 | |||
| 40 | arg_default($self, "x", 0); | ||
| 41 | |||
| 42 | arg_default($self, "y", 0); | ||
| 43 | |||
| 44 | arg_default($self, "width", 0); | ||
| 45 | |||
| 46 | arg_default($self, "height", 0); | ||
| 47 | |||
| 48 | arg_default($self, "width_pixels", 2); | ||
| 49 | |||
| 50 | arg_default($self, "padding_pixels", 5); | ||
| 51 | |||
| 52 | return $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | # my $image = $border->border_get_image(); | ||
| 57 | |||
| 58 | sub border_get_image | ||
| 59 | { | ||
| 60 | my $self = shift(@_); | ||
| 61 | |||
| 62 | croak "You must supply a border image by overriding 'border_get_image'.\n"; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | # $self->border_set_x($value); | ||
| 67 | |||
| 68 | sub border_set_x | ||
| 69 | { | ||
| 70 | my ($self, $value) = @_; | ||
| 71 | |||
| 72 | croak "You must set the border x coordinate by overriding 'border_set_x'.\n"; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | # $self->border_set_y($value); | ||
| 77 | |||
| 78 | sub border_set_y | ||
| 79 | { | ||
| 80 | my ($self, $value) = @_; | ||
| 81 | |||
| 82 | croak "You must set the border y coordinate by overriding 'border_set_y'.\n"; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | # $self->border_set_width($value); | ||
| 87 | |||
| 88 | sub border_set_width | ||
| 89 | { | ||
| 90 | my ($self, $value) = @_; | ||
| 91 | |||
| 92 | croak "You must set the border width by overriding 'border_set_width'.\n"; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | # $self->border_set_height($value); | ||
| 97 | |||
| 98 | sub border_set_height | ||
| 99 | { | ||
| 100 | my ($self, $value) = @_; | ||
| 101 | |||
| 102 | croak "You must set the border height by overriding 'border_set_height'.\n"; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | # $self->border_set_param($param_name, $value); | ||
| 107 | |||
| 108 | sub border_set_param | ||
| 109 | { | ||
| 110 | my ($self, $param_name, $value) = @_; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | # my ($top, $left, $bottom, $right) = $self->border_insets(); | ||
| 115 | |||
| 116 | sub border_insets | ||
| 117 | { | ||
| 118 | my $self = shift(@_); | ||
| 119 | |||
| 120 | my ($width, $padding) = $self->get( qw(width_pixels padding_pixels)); | ||
| 121 | |||
| 122 | my $inset = $width + $padding; | ||
| 123 | |||
| 124 | return ($inset, $inset, $inset, $inset); | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | # my $value = $border->get(qw(x y width height)); | ||
| 129 | |||
| 130 | sub get | ||
| 131 | { | ||
| 132 | my $self = shift(@_); | ||
| 133 | |||
| 134 | return undef if (scalar @_ == 0); | ||
| 135 | |||
| 136 | return _get($self,shift(@_)) if (scalar @_ == 1); | ||
| 137 | |||
| 138 | my @values = (); | ||
| 139 | |||
| 140 | foreach my $param_name (@_) { push @values, _get($self, $param_name); } | ||
| 141 | |||
| 142 | return @values; | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | # $border->reparent($group); | ||
| 147 | |||
| 148 | sub reparent | ||
| 149 | { | ||
| 150 | my ($self, $group) = @_; | ||
| 151 | |||
| 152 | $self->{group} = $group; | ||
| 153 | |||
| 154 | $self->{border}->reparent($group); | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | # $border->set(x=>0, y=>10, width=>20, height=>30); | ||
| 159 | |||
| 160 | sub set | ||
| 161 | { | ||
| 162 | my $self = shift(@_); | ||
| 163 | |||
| 164 | my %attributes = @_; | ||
| 165 | |||
| 166 | foreach my $param_name (keys %attributes) | ||
| 167 | { | ||
| 168 | my $value = $attributes{$param_name}; | ||
| 169 | |||
| 170 | if ($param_name eq 'x') | ||
| 171 | { | ||
| 172 | $self->{x} = $value; | ||
| 173 | |||
| 174 | $self->{content}->set(x=>$value + ($self->border_insets())[1]); | ||
| 175 | |||
| 176 | $self->border_set_x($value); | ||
| 177 | |||
| 178 | next; | ||
| 179 | } | ||
| 180 | |||
| 181 | if ($param_name eq 'y') | ||
| 182 | { | ||
| 183 | $self->{y} = $value; | ||
| 184 | |||
| 185 | $self->{content}->set(y=>$value + ($self->border_insets())[0]); | ||
| 186 | |||
| 187 | $self->border_set_y($value); | ||
| 188 | |||
| 189 | next; | ||
| 190 | } | ||
| 191 | |||
| 192 | if ($param_name eq 'width') | ||
| 193 | { | ||
| 194 | my $min_width = $self->{content}->get_min_width(); | ||
| 195 | |||
| 196 | my ($top, $left, $bottom, $right) = $self->border_insets(); | ||
| 197 | |||
| 198 | my $width = List::Util::max($value, $min_width + ($left + $right)); | ||
| 199 | |||
| 200 | $self->{width} = $width; | ||
| 201 | |||
| 202 | $self->{content}->set(width=>($width - ($left + $right))); | ||
| 203 | |||
| 204 | $self->border_set_width($width); | ||
| 205 | |||
| 206 | next; | ||
| 207 | } | ||
| 208 | |||
| 209 | if ($param_name eq 'height') | ||
| 210 | { | ||
| 211 | my $min_height = $self->{content}->get_min_height(); | ||
| 212 | |||
| 213 | my ($top, $left, $bottom, $right) = $self->border_insets(); | ||
| 214 | |||
| 215 | my $height = List::Util::max($value, $min_height + ($top + $bottom)); | ||
| 216 | |||
| 217 | $self->{height} = $height; | ||
| 218 | |||
| 219 | $self->{content}->set(height=>($height - ($top + $bottom))); | ||
| 220 | |||
| 221 | $self->border_set_height($height); | ||
| 222 | |||
| 223 | next; | ||
| 224 | } | ||
| 225 | |||
| 226 | $self->border_set_param($param_name=>$value); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | # my ($x, $y) = $border->get_connection_point('left'); | ||
| 232 | |||
| 233 | sub get_connection_point | ||
| 234 | { | ||
| 235 | my ($self, $side) = @_; | ||
| 236 | |||
| 237 | my $y = $self->{y} + ($self->{height} / 2); | ||
| 238 | |||
| 239 | my $x = ($side eq 'left') ? $self->{x} : $self->{x} + $self->{width}; | ||
| 240 | |||
| 241 | return ($x, $y); | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | sub get_min_height | ||
| 246 | { | ||
| 247 | my $self = shift(@_); | ||
| 248 | |||
| 249 | my ($top, $left, $bottom, $right) = $self->border_insets(); | ||
| 250 | |||
| 251 | return ($self->{content}->get_min_height() + ($top + $bottom)); | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | sub get_min_width | ||
| 256 | { | ||
| 257 | my $self = shift(@_); | ||
| 258 | |||
| 259 | my ($top, $left, $bottom, $right) = $self->border_insets(); | ||
| 260 | |||
| 261 | return ($self->{content}->get_min_width() + ($left + $right)); | ||
| 262 | } | ||
| 263 | |||
| 264 | |||
| 265 | sub _get | ||
| 266 | { | ||
| 267 | my ($self, $param_name) = @_; | ||
| 268 | |||
| 269 | my $value = $self->{$param_name}; | ||
| 270 | |||
| 271 | croak "Undefined value for key $param_name.\n" if (!defined $value); | ||
| 272 | |||
| 273 | return $value; | ||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | |||
| 278 | 1; # Magic true value required at end of module | ||
| 279 | __END__ |