App/Chart/Gtk2/GUI.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« App/Chart/Gtk2/Ex/TreeRowPosition.pm ↑ Summary App/Chart/Suffix/NZ.pm »
1 # GUI shared functions.
2  
3 # Copyright 2007, 2008, 2009, 2010, 2011, 2018 Kevin Ryde
4  
5 # This file is part of Chart.
6 #
7 # Chart is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3, or (at your option) any later version.
10 #
11 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 # details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Chart. If not, see <http://www.gnu.org/licenses/>.
18  
19 package App::Chart::Gtk2::GUI;
20 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Locale::TextDomain ('App-Chart');
Subroutine BEGIN: called CC=1 SCAR=0.0
26 0 ▶use Glib;
Subroutine BEGIN: called CC=1 SCAR=2.6
27 use Gtk2;
28 use Gtk2::Pango;
29  
30 use App::Chart;
31  
32 # uncomment this to run the ### lines
33 #use Smart::Comments;
34  
35 Glib::set_application_name (__('Chart'));
36 Gtk2::Window->set_default_icon_from_file
37 (App::Chart::datafilename ('chart.xpm'));
38 Gtk2->CHECK_VERSION(2,12,0)
39 or die "App::Chart needs Gtk 2.12 or higher";
40  
41  
42 #------------------------------------------------------------------------------
43  
44 # Perl-Gtk 1.221 no undef
45 sub menu_set_screen {
46 my ($menu, $screen) = @_;
47 # $screen ||= do {
48 # my $display;
49 # ($display = Gtk2::Gdk::Display->get_default)
50 # && $display->get_default_screen;
51 # };
52 if ($screen) {
53 $menu->set_screen ($screen);
54 }
55 }
56  
57 #------------------------------------------------------------------------------
58  
59 sub string_width {
60 my ($widget_or_layout, $str) = @_;
61 ### string_width(): "$widget_or_layout"
62 ### $str
63 my $layout;
64 if ($widget_or_layout->can ('create_pango_layout')) {
65 # if widget instead of layout
66 $layout = $widget_or_layout->create_pango_layout ($str);
67 } else {
68 $layout = $widget_or_layout;
69 $layout->set_text ($str);
70 }
71 my ($width, $height) = $layout->get_pixel_size;
72 return $width;
73 }
74  
75  
76 #------------------------------------------------------------------------------
77  
78 # FIXME: really want to clip to the region, not an enclosing rect. The
79 # style paint might be some nice drawing, but the default is a pretty simple
80 # gdk_draw_layout().
81 #
82 sub draw_text_centred {
83 my ($widget, $region_or_event, $str) = @_;
84 my $win = $widget->window || return; # unrealized
85  
86 my $clip_rect = $region_or_event && do {
87 if (my $func = $region_or_event->can('get_clipbox')) {
88 $func->($region_or_event);
89 } else {
90 $region_or_event->area;
91 }
92 };
93  
94 my $alloc = $widget->allocation;
95 my $layout = $widget->create_pango_layout ($str);
96 $layout->set_wrap ('word-char');
97 $layout->set_width ($alloc->width * Gtk2::Pango::PANGO_SCALE);
98 my ($str_width, $str_height) = $layout->get_pixel_size;
99 my $x = max (0, ($alloc->width - $str_width) / 2);
100 my $y = max (0, ($alloc->height - $str_height) / 2);
101 if ($widget->get_flags & 'no-window') {
102 $x += $alloc->x;
103 $y += $alloc->y;
104 }
105 my $style = $widget->get_style;
106 $style->paint_layout ($win,
107 $widget->state,
108 1, # use text gc
109 $clip_rect,
110 $widget,
111 'centred-text',
112 $x, $y, $layout);
113 }
114  
115  
116 #------------------------------------------------------------------------------
117  
118 sub chart_style_class {
119 my ($class) = @_;
120 _chart_style_parse();
121 $class =~ s/:/_/g;
122 Gtk2::Rc->parse_string
123 ("class \"$class\" style:gtk \"Chart_style\"");
124 }
125 sub chart_style_widget {
126 my ($name) = @_;
127 _chart_style_parse();
128 Gtk2::Rc->parse_string
129 ("widget \"*.$name\" style:application \"Chart_style\"");
130 }
131 use constant::defer _chart_style_parse => sub {
132 Gtk2::Rc->parse_string (<<'HERE');
133 style "Chart_style" {
134 # white on black
135 fg[ACTIVE] = { 1.0, 1.0, 1.0 }
136 fg[NORMAL] = { 1.0, 1.0, 1.0 }
137 fg[PRELIGHT] = { 1.0, 1.0, 1.0 }
138 fg[SELECTED] = { 1.0, 1.0, 1.0 }
139 fg[INSENSITIVE] = { 1.0, 1.0, 1.0 }
140 text[ACTIVE] = { 1.0, 1.0, 1.0 }
141 text[NORMAL] = { 1.0, 1.0, 1.0 }
142 text[PRELIGHT] = { 1.0, 1.0, 1.0 }
143 text[SELECTED] = { 1.0, 1.0, 1.0 }
144 text[INSENSITIVE] = { 1.0, 1.0, 1.0 }
145  
146 bg[ACTIVE] = { 0, 0, 0 }
147 bg[NORMAL] = { 0, 0, 0 }
148 bg[PRELIGHT] = { 0, 0, 0 }
149 bg[SELECTED] = { 0, 0, 0 }
150 bg[INSENSITIVE] = { 0, 0, 0 }
151 base[ACTIVE] = { 0, 0, 0 }
152 base[NORMAL] = { 0, 0, 0 }
153 base[PRELIGHT] = { 0, 0, 0 }
154 base[SELECTED] = { 0, 0, 0 }
155 base[INSENSITIVE] = { 0, 0, 0 }
156 }
157 HERE
158 return; # nothing
159 };
160  
161  
162 #------------------------------------------------------------------------------
163  
164 # $uri is either a string or a URI object
165 sub browser_open {
166 my ($uri, $parent_widget) = @_;
167 $uri = "$uri"; # stringize URI object
168 ### browser_open(): $uri
169  
170 if (Gtk2->can('show_uri')) { # new in Gtk 2.14
171 my $screen = $parent_widget && $parent_widget->get_screen;
172 if (eval { Gtk2::show_uri ($screen, $uri); 1 }) {
173 return;
174 }
175 # possible Glib::Error "operation not supported" on http urls
176 ### show_uri() error: $@
177 }
178 {
179 # Debian "sensible-browser" program which looks at $BROWSER and various
180 # other things.
181 my @command = ('sensible-browser', $uri);
182 if (_spawn (@command)) { return }
183 }
184 {
185 # The quoting, or lack thereof, expected of the url in openURL is in
186 # mozilla XRemoteService.cpp. It looks for ( ) delims, then the last
187 # "," is the last arg to take off new-window, new-tab, noraise, etc.
188 my @command = ('mozilla', '-remote', "openURL($uri,new-window)");
189 ### run: @command
190 if (system (@command) == 0) {
191 return;
192 }
193 ### run status: $?
194 ### error: "$!"
195 }
196 {
197 my @command = ('mozilla', $uri);
198 if (_spawn (@command)) { return }
199 }
200 warn "Cannot run browser: none of show_uri, sensible-browser or mozilla work";
201 }
202 sub _spawn {
203 my @command = @_;
204 ### spawn(): @command
205 require Proc::SyncExec;
206 my $pid = Proc::SyncExec::sync_exec (\&_spawn_detach, @command);
207 if (! defined $pid) {
208 ### cannot run: $!
209 return 0;
210 }
211 if (waitpid ($pid, 0) != $pid) {
212 warn "Error waiting spawned $pid: $!\n";
213 }
214 return 1;
215 }
216 sub _spawn_detach {
217 if (my $pid = Proc::SyncExec::fork_retry()) {
218 POSIX::_exit (0); # parent
219 } else {
220 ## no critic (RequireCheckingReturnValueOfEval)
221 eval { POSIX::setsid() };
222 return 1; # ok, child continues
223 }
224 }
225  
226 #-----------------------------------------------------------------------------
227  
228 sub color_object {
229 my ($widget, $colour_str) = @_;
230 if (defined $colour_str) {
231 require App::Chart::Gtk2::Ex::GdkColorAlloc;
232 return ($colour_str,
233 $widget->{'color'}->{$colour_str}
234 ||= App::Chart::Gtk2::Ex::GdkColorAlloc->new (widget => $widget,
235 color => $colour_str));
236 } else {
237 return ('fg', $widget->style->fg($widget->state));
238 }
239 }
240  
241 sub gc_for_colour {
242 my ($widget, $colour_str) = @_;
243 ($colour_str, my $color_obj) = color_object ($widget, $colour_str);
244 return ($widget->{'gc_solid'}->{$colour_str} ||= do {
245 require App::Chart::Gtk2::Ex::GtkGCBits;
246 App::Chart::Gtk2::Ex::GtkGCBits->get_for_widget
247 ($widget, { foreground => $color_obj,
248 line_style => 'solid',
249 line_width => 0 })
250 });
251 }
252 sub gc_for_colour_dashed {
253 my ($widget, $colour_str) = @_;
254 ($colour_str, my $color_obj) = color_object ($widget, $colour_str);
255 return ($widget->{'gc_dash'}->{$colour_str} ||= do {
256 require App::Chart::Gtk2::Ex::GtkGCBits;
257 App::Chart::Gtk2::Ex::GtkGCBits->get_for_widget
258 ($widget, { foreground => $color_obj,
259 line_style => 'on_off_dash',
260 line_width => 0 })
261 });
262 }
263  
264 1;
265 __END__
« App/Chart/Gtk2/Ex/TreeRowPosition.pm ↑ Summary App/Chart/Suffix/NZ.pm »