| 1 | package Gtk2::Ex::MindMapView::Border::RoundedRect; | ||
| 2 | |||
| 3 | our $VERSION = '0.000001'; | ||
| 4 | |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use List::Util; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 0 | ▶ | use Gnome2::Canvas; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | |||
| 13 | use Gtk2::Ex::MindMapView::ArgUtils; | ||
| 14 | |||
| 15 | use base 'Gtk2::Ex::MindMapView::Border'; | ||
| 16 | |||
| 17 | sub new | ||
| 18 | { | ||
| 19 | my $class = shift(@_); | ||
| 20 | |||
| 21 | my $self = $class->SUPER::new(@_); | ||
| 22 | |||
| 23 | my %attributes = @_; | ||
| 24 | |||
| 25 | args_valid(\%attributes, qw(group content x y width height radius width_pixels | ||
| 26 | padding_pixels fill_color_gdk outline_color_gdk)); | ||
| 27 | |||
| 28 | arg_default($self, "radius", 10); | ||
| 29 | |||
| 30 | arg_default($self, "fill_color_gdk", Gtk2::Gdk::Color->parse('white')); | ||
| 31 | |||
| 32 | arg_default($self, "outline_color_gdk", Gtk2::Gdk::Color->parse('gray')); | ||
| 33 | |||
| 34 | $self->{content}->set(anchor=>'north-west'); | ||
| 35 | |||
| 36 | my ($top, $left, $bottom, $right) = $self->border_insets(); | ||
| 37 | |||
| 38 | $self->{width} = $self->{content}->get('width') + ($left + $right); | ||
| 39 | |||
| 40 | $self->{height} = $self->{content}->get('height') + ($top + $bottom); | ||
| 41 | |||
| 42 | $self->{border} = $self->border_get_image(); | ||
| 43 | |||
| 44 | return $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | # $border->border_get_image(); | ||
| 49 | |||
| 50 | sub border_get_image | ||
| 51 | { | ||
| 52 | my $self = shift(@_); | ||
| 53 | |||
| 54 | my $border = Gnome2::Canvas::Item->new($self->{group}, 'Gnome2::Canvas::Shape', | ||
| 55 | 'fill-color-gdk'=>$self->{fill_color_gdk}, | ||
| 56 | 'outline-color-gdk'=>$self->{outline_color_gdk}); | ||
| 57 | |||
| 58 | $border->set_path_def(_rounded_rect($self)); | ||
| 59 | |||
| 60 | return $border; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | # $border->border_set_x($value); | ||
| 65 | |||
| 66 | sub border_set_x | ||
| 67 | { | ||
| 68 | my ($self, $value) = @_; | ||
| 69 | |||
| 70 | $self->{border}->set_path_def(_rounded_rect($self)); | ||
| 71 | |||
| 72 | $self->{border}->request_update(); | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | # $border->border_set_y($value); | ||
| 77 | |||
| 78 | sub border_set_y | ||
| 79 | { | ||
| 80 | my ($self, $value) = @_; | ||
| 81 | |||
| 82 | $self->{border}->set_path_def(_rounded_rect($self)); | ||
| 83 | |||
| 84 | $self->{border}->request_update(); | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | # $border->border_set_width($value); | ||
| 89 | |||
| 90 | sub border_set_width | ||
| 91 | { | ||
| 92 | my ($self, $value) = @_; | ||
| 93 | |||
| 94 | $self->{border}->set_path_def(_rounded_rect($self)); | ||
| 95 | |||
| 96 | $self->{border}->request_update(); | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | # $border->border_set_height($value); | ||
| 101 | |||
| 102 | sub border_set_height | ||
| 103 | { | ||
| 104 | my ($self, $value) = @_; | ||
| 105 | |||
| 106 | $self->{border}->set_path_def(_rounded_rect($self)); | ||
| 107 | |||
| 108 | $self->{border}->request_update(); | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | # $border->border_set_param($name, $value); | ||
| 113 | |||
| 114 | sub border_set_param | ||
| 115 | { | ||
| 116 | my ($self, $name, $value) = @_; | ||
| 117 | |||
| 118 | $self->{border}->set($name=>$value); | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | sub _bezier | ||
| 123 | { | ||
| 124 | my ($self, $corner, $x, $y) = @_; | ||
| 125 | |||
| 126 | my $r = _radius($self); | ||
| 127 | |||
| 128 | if ($corner eq 'upper_left') | ||
| 129 | { | ||
| 130 | return ($x,$y+$r, $x,$y+($r/2), $x+($r/2),$y, $x+$r, $y); | ||
| 131 | } | ||
| 132 | |||
| 133 | if ($corner eq 'upper_right') | ||
| 134 | { | ||
| 135 | return ($x-$r,$y, $x-($r/2),$y, $x,$y+($r/2), $x, $y+$r); | ||
| 136 | } | ||
| 137 | |||
| 138 | if ($corner eq 'lower_right') | ||
| 139 | { | ||
| 140 | return ($x,$y-$r, $x,$y-($r/2), $x-($r/2),$y, $x-$r, $y); | ||
| 141 | } | ||
| 142 | |||
| 143 | if ($corner eq 'lower_left') | ||
| 144 | { | ||
| 145 | return ($x+$r,$y, $x+($r/2),$y, $x,$y-($r/2), $x, $y-$r); | ||
| 146 | } | ||
| 147 | |||
| 148 | croak "Invalid corner argument: $corner\n"; | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | sub _radius | ||
| 153 | { | ||
| 154 | my $self = shift(@_); | ||
| 155 | |||
| 156 | my $max_radius = List::Util::min($self->{width}, $self->{height}) * 3 / 8; | ||
| 157 | |||
| 158 | return List::Util::max(0, List::Util::min($self->{radius}, $max_radius)); | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | sub _rounded_rect | ||
| 163 | { | ||
| 164 | my $self = shift(@_); | ||
| 165 | |||
| 166 | my $x1 = $self->{x}; | ||
| 167 | |||
| 168 | my $y1 = $self->{y}; | ||
| 169 | |||
| 170 | my $x2 = $self->{x} + $self->{width}; | ||
| 171 | |||
| 172 | my $y2 = $self->{y} + $self->{height}; | ||
| 173 | |||
| 174 | # print "_rounded_rect, x: $self->{x} y: $self->{y} width: $self->{width} height: $self->{height}\n"; | ||
| 175 | |||
| 176 | my @p = (); | ||
| 177 | |||
| 178 | push @p, _bezier($self, 'upper_left', $x1, $y1); | ||
| 179 | |||
| 180 | push @p, _bezier($self, 'upper_right', $x2, $y1); | ||
| 181 | |||
| 182 | push @p, _bezier($self, 'lower_right', $x2, $y2); | ||
| 183 | |||
| 184 | push @p, _bezier($self, 'lower_left', $x1, $y2); | ||
| 185 | |||
| 186 | my $pathdef = Gnome2::Canvas::PathDef->new(); | ||
| 187 | |||
| 188 | $pathdef->moveto ($p[0], $p[1]); | ||
| 189 | |||
| 190 | $pathdef->curveto ($p[2], $p[3], $p[4], $p[5], $p[6], $p[7]); | ||
| 191 | |||
| 192 | $pathdef->lineto ($p[8], $p[9]); | ||
| 193 | |||
| 194 | $pathdef->curveto ($p[10], $p[11], $p[12], $p[13], $p[14], $p[15]); | ||
| 195 | |||
| 196 | $pathdef->lineto ($p[16], $p[17]); | ||
| 197 | |||
| 198 | $pathdef->curveto ($p[18], $p[19], $p[20], $p[21], $p[22], $p[23]); | ||
| 199 | |||
| 200 | $pathdef->lineto ($p[24], $p[25]); | ||
| 201 | |||
| 202 | $pathdef->curveto ($p[26], $p[27], $p[28], $p[29], $p[30], $p[31]); | ||
| 203 | |||
| 204 | $pathdef->lineto ($p[0], $p[1]); | ||
| 205 | |||
| 206 | # Close the path so that 'fill-color' will work. | ||
| 207 | $pathdef->closepath_current; | ||
| 208 | |||
| 209 | return $pathdef; | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | |||
| 214 | 1; # Magic true value required at end of module | ||
| 215 | __END__ | ||