App/Chart/Gtk2/Main.pm untested

filter: Statementstmt 0.0% 0 / 484 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 78 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 727 CC 19 SCAR 18.0
CC
19
Cov
0%
find_for_dialog
30.0
main
30.0
_do_delete_symbol
12.0
CRAP 6.0
1 # Copyright 2007, 2008, 2009, 2010, 2011, 2024 Kevin Ryde
2  
3 # This file is part of Chart.
4 #
5 # Chart is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3, or (at your option) any later version.
8 #
9 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Chart. If not, see <http://www.gnu.org/licenses/>.
16  
17 package App::Chart::Gtk2::Main;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Carp;
22 use File::Spec;
23 use List::Util qw(min max);
24 use POSIX ();
25 use Locale::TextDomain 1.19 ('App-Chart'); # 1.19 for N__() in scalar context
26  
27 use Glib 1.220;
28 use Gtk2 1.220;
29 use Glib::Ex::ConnectProperties;
30 use Gtk2::Ex::WidgetCursor;
31  
32 use Gtk2::Ex::ActionTooltips;
33 use Gtk2::Ex::History;
34 use Gtk2::Ex::History::Action;
35 use App::Chart::Gtk2::Ex::ToplevelBits;
36 use App::Chart;
37 use App::Chart::Database;
38 use App::Chart::Gtk2::GUI;
39 use App::Chart::Gtk2::Symlist;
40 use App::Chart::Gtk2::View;
41  
42 # uncomment this to run the ### lines
43 #use Smart::Comments;
44  
45 use base 'App::Chart::Gtk2::Ex::ToplevelSingleton';
46  
47 use Glib::Object::Subclass
48 'Gtk2::Window',
49 signals => { destroy => \&_do_destroy,
50 symbol_changed => { param_types => ['Glib::String'],
51 return_type => undef,
52 class_closure => \&_do_symbol_changed },
53 },
54 properties => [ Glib::ParamSpec->object
55 ('statusbar',
56 'statusbar',
57 'The Gtk2::Statusbar at the bottom of the window.',
58 'Gtk2::Statusbar',
59 ['readable']),
60 ];
61  
62 #------------------------------------------------------------------------------
63  
64 use constant _ACTION_DATA =>
65 [ # name, stock id, label, accelerator, tooltip
66
67 { name => 'FileMenu', label => '_File' },
68 { name => 'EditMenu', label => '_Edit' },
69 { name => 'ViewMenu', label => '_View' },
70 { name => 'ToolsMenu', label => '_Tools' },
71 { name => 'HelpMenu', label => '_Help' },
72
73 { name => 'Open',
74 stock_id => 'gtk-open',
75 accelerator => __p('Main-accelerator-key','O'),
76 tooltip => __('View chart for a symbol, or add new symbol to the database'),
77 callback => sub {
78 my ($action, $self) = @_;
79 App::Chart::Gtk2::Ex::ToplevelBits::popup
80 ('App::Chart::Gtk2::OpenDialog',
81 transient_for => $self,
82 hide_on_delete => 1);
83 },
84 },
85 { name => 'Delete',
86 stock_id => 'gtk-delete',
87 tooltip => __('Delete this symbol from the database.'),
88 callback => sub {
89 my ($action, $self) = @_;
90 require App::Chart::Gtk2::DeleteDialog;
91 App::Chart::Gtk2::DeleteDialog->popup ($self->get_symbol, $self);
92 },
93 },
94 { name => 'AddFavourite',
95 label => __('_Add Favourite'),
96 accelerator => __p('Main-accelerator-key','exclam'),
97 tooltip => __('Add this symbol to your Favourites list, or elevate in that list.'),
98 callback => sub {
99 my ($action, $self) = @_;
100 require App::Chart::Gtk2::Symlist::Favourites;
101 my $symlist = App::Chart::Gtk2::Symlist::Favourites->instance;
102 my $symbol = $self->get_symbol;
103 my $message = (($symlist->append_or_elevate($symbol) eq 'elevated')
104 ? N__('{symbol} raised in {symlist}')
105 : N__('{symbol} added to {symlist}'));
106 _message ($self, __x($message,
107 symbol => $symbol,
108 symlist => $symlist->name));
109 },
110 },
111 { name => 'RemoveFavourite',
112 label => __('_Remove Favourite'),
113 accelerator => __p('Main-accelerator-key','<Ctrl>exclam'),
114 tooltip => __('Remove this symbol from your Favourites list.'),
115 callback => sub {
116 my ($action, $self) = @_;
117 require App::Chart::Gtk2::Symlist::Favourites;
118 my $symlist = App::Chart::Gtk2::Symlist::Favourites->instance;
119 $symlist->delete_symbol ($self->get_symbol);
120 },
121 },
122 { name => 'Next',
123 stock_id => 'gtk-media-next',
124 accelerator => __p('Main-accelerator-key','N'),
125 tooltip => __('Go to the next symbol (in the current symlist, or the next symlist).'),
126 callback => sub {
127 my ($action, $self) = @_;
128 $self->goto_next;
129 },
130 },
131 { name => 'Prev',
132 stock_id => 'gtk-media-previous',
133 accelerator => __p('Main-accelerator-key','P'),
134 tooltip => __('Go to the previous symbol (in the current symlist, or the previous symlist).'),
135 callback => sub {
136 my ($action, $self) = @_;
137 $self->go_prev;
138 },
139 },
140 { name => 'Quit',
141 stock_id => 'gtk-quit',
142 # no accelerator -- don't really want the usual Control-Q
143 callback => sub {
144 my ($action, $self) = @_;
145 if (App::Chart::Gtk2::JobQueue->can('instance')) { # if loaded
146 require App::Chart::Gtk2::JobsRunningDialog;
147 App::Chart::Gtk2::JobsRunningDialog->query_and_quit ($self);
148 } else {
149 $self->destroy;
150 }
151 },
152 },
153 { name => 'Preferences',
154 stock_id => 'gtk-preferences',
155 callback => sub {
156 my ($action, $self) = @_;
157 App::Chart::Gtk2::Ex::ToplevelBits::popup
158 ('App::Chart::Gtk2::PreferencesDialog', screen => $self);
159 },
160 },
161
162 { name => 'Centre',
163 label => __('_Centre'),
164 accelerator => __p('Main-accelerator-key','<Ctrl>C'),
165 callback => sub { my ($action, $self) = @_;
166 $self->{'view'}->centre; }
167 },
168 { name => 'ZoomInX',
169 label => __('Zoom Wider'),
170 accelerator => __p('Main-accelerator-key','W'),
171 callback => sub { my ($action, $self) = @_;
172 $self->{'view'}->zoom (1.5, 1) }
173 },
174 { name => 'ZoomOutX',
175 label => __('Zoom Narrower'),
176 accelerator => __p('Main-accelerator-key','<Shift>W'),
177 callback => sub { my ($action, $self) = @_;
178 $self->{'view'}->zoom (0.75, 1) }
179 },
180 { name => 'ZoomInY',
181 stock_id => 'gtk-zoom-in',
182 accelerator => __p('Main-accelerator-key','Z'),
183 callback => sub { my ($action, $self) = @_;
184 $self->{'view'}->zoom (1, 1.5) }
185 },
186 { name => 'ZoomOutY',
187 stock_id => 'gtk-zoom-out',
188 accelerator => __p('Main-accelerator-key','<Shift>Z'),
189 callback => sub { my ($action, $self) = @_;
190 $self->{'view'}->zoom (1, 1/1.5) }
191 },
192 { name => 'Redraw',
193 label => __('_Redraw'),
194 accelerator => __p('Main-accelerator-key','<Ctrl>L'),
195 tooltip => __('Force a redraw of the entire Chart display.'),
196 callback => sub { my ($action, $self) = @_;
197 $self->queue_draw; }
198 },
199 { name => 'Intraday',
200 label => __('_Intraday'),
201 tooltip => __p('Main-accelerator-key','<Ctrl>I'),
202 callback => sub {
203 my ($action, $self) = @_;
204 require App::Chart::Gtk2::IntradayDialog;
205 App::Chart::Gtk2::IntradayDialog->popup ($self->get_symbol, $self);
206 },
207 },
208 { name => 'Annotations',
209 label => __('_Annotations'),
210 callback => sub {
211 my ($action, $self) = @_;
212 require App::Chart::Gtk2::AnnotationsDialog;
213 App::Chart::Gtk2::AnnotationsDialog->popup ($self->get_symbol, $self);
214 },
215 },
216 { name => 'ViewStyle',
217 label => __('_View Style'),
218 callback => sub { my ($action, $self) = @_;
219 Gtk2::Ex::WidgetCursor->busy;
220 require App::Chart::Gtk2::ViewStyleDialog;
221 my $vs = App::Chart::Gtk2::ViewStyleDialog->instance_for_screen($self);
222 $vs->present;
223 my $view = $self->{'view'};
224 $vs->set (view => $view,
225 viewstyle => $view->get('viewstyle'));
226 }
227 },
228 { name => 'Raw',
229 label => __('_Raw data'),
230 callback => sub { my ($action, $self) = @_;
231 require App::Chart::Gtk2::RawDialog;
232 App::Chart::Gtk2::RawDialog->popup ($self->get_symbol, $self); }
233 },
234 { name => "WebMenuItem",
235 label => __('_Web')
236 },
237
238 # Tools menu
239
240 { name => 'Watchlist',
241 label => __('_Watchlist'),
242 callback => sub {
243 my ($action, $self) = @_;
244 App::Chart::Gtk2::Ex::ToplevelBits::popup
245 ('App::Chart::Gtk2::WatchlistDialog',
246 screen => $self,
247 hide_on_delete => 1);
248 },
249 },
250 { name => 'Download',
251 label => __('_Download'),
252 callback => sub { my ($action, $self) = @_;
253 require App::Chart::Gtk2::DownloadDialog;
254 App::Chart::Gtk2::DownloadDialog->popup ($self->get_symbol, $self); }
255 },
256 { name => 'DownloadUpdate',
257 label => __('Download _Update'),
258 accelerator => __p('Main-accelerator-key','<Ctrl>U'),
259 callback => sub {
260 my ($action, $self) = @_;
261 # meant to be sensitive only with a symbol, but check anyway
262 my $symbol = $self->get_symbol || return;
263 require App::Chart::Gtk2::DownloadDialog;
264 my $dialog = App::Chart::Gtk2::DownloadDialog->instance_for_screen($self);
265 my $job = $dialog->start ($symbol, undef);
266 my $jobmsg = ($self->{'jobmsg'} ||= do {
267 require App::Chart::Gtk2::JobStatusbarMessage;
268 App::Chart::Gtk2::JobStatusbarMessage->new
269 (statusbar => $self->{'statusbar'});
270 });
271 $jobmsg->add_job ($job);
272 } },
273 { name => 'History',
274 label => __('_History'),
275 tooltip => __('Dialog of back/forward symbols.'),
276 callback => sub {
277 my ($action, $self) = @_;
278 App::Chart::Gtk2::Ex::ToplevelBits::popup
279 ('Gtk2::Ex::History::Dialog',
280 screen => $self,
281 properties => { history => $self->{'history'} });
282 },
283 },
284 { name => 'Vacuum',
285 label => __('_Vacuum'),
286 tooltip => __('Compact and clean up the database.'),
287 callback => sub {
288 my ($action, $self) = @_;
289 App::Chart::Gtk2::Ex::ToplevelBits::popup ('App::Chart::Gtk2::VacuumDialog',
290 screen => $self);
291 },
292 },
293 { name => 'Errors',
294 label => __('_Errors'),
295 tooltip => __('Open the errors dialog (it automatically popups up when there\'s an error to see)'),
296 callback => sub { my ($action, $self) = @_;
297 require Gtk2::Ex::ErrorTextDialog;
298 my $dialog = Gtk2::Ex::ErrorTextDialog->instance;
299 $dialog->set_screen ($self->get_screen);
300 $dialog->present;
301 }
302 },
303 { name => 'Diagnostics',
304 label => __('Dia_gnostics'),
305 tooltip => __('Some diagnostic information about the program and the databases.'),
306 callback => sub { my ($action, $self) = @_;
307 require App::Chart::Gtk2::Diagnostics;
308 App::Chart::Gtk2::Diagnostics->popup ($self); }
309 },
310
311 { name => 'About',
312 stock_id => 'gtk-about',
313 callback => sub {
314 my ($action, $self) = @_;
315 App::Chart::Gtk2::Ex::ToplevelBits::popup
316 ('App::Chart::Gtk2::AboutDialog', screen => $self);
317 },
318 },
319  
320 { name => 'Manual',
321 label => __('_Manual'),
322 accelerator => 'F1',
323 callback => sub { my ($action, $self) = @_;
324 require App::Chart::Manual;
325 App::Chart::Manual->open (undef, $self); },
326 },
327 { name => 'ManualDataSource',
328 label => __('This _Data Source'),
329 # tooltip generated dynamically
330 callback => sub { my ($action, $self) = @_;
331 require App::Chart::Manual;
332 App::Chart::Manual->open_for_symbol ($self->get_symbol, $self); }
333 },
334 { name => 'ManualMovingAverage',
335 label => __('This _Moving Average'),
336 tooltip => __('Open the manual at the section about the moving average shown in the upper graph'),
337 callback => sub {
338 my ($action, $self) = @_;
339 require App::Chart::Manual;
340 App::Chart::Manual->open(manual_for_graph_n($self,0), $self);
341 }
342 },
343 { name => 'ManualIndicator',
344 label => __('This _Indicator'),
345 tooltip => __('Open the manual at the section about the indicator shown in the lower graph'),
346 callback => sub {
347 my ($action, $self) = @_;
348 require App::Chart::Manual;
349 App::Chart::Manual->open(manual_for_graph_n($self,1), $self);
350 }
351 },
352 ];
353  
354 sub INIT_INSTANCE {
355 my ($self) = @_;
356  
357 $self->set_title (__('Chart'));
358  
359 App::Chart::chart_dirbroadcast()->connect_for_object
360 ('delete-symbol', \&_do_delete_symbol, $self);
361  
362 my $actiongroup = $self->{'actiongroup'} = Gtk2::ActionGroup->new ("main");
363 Gtk2::Ex::ActionTooltips::group_tooltips_to_menuitems ($actiongroup);
364 $actiongroup->set_translation_domain ('gtk20'); #fallback for _Edit and _Help
365 $actiongroup->add_actions (_ACTION_DATA, $self);
366  
367 my $history = $self->{'history'} = Gtk2::Ex::History->new;
368 $history->signal_connect (place_to_text => \&_history_place_to_text);
369 $history->signal_connect ('notify::current' => \&_history_notify_current,
370 $self);
371  
372 # sub new_in_actiongroup {
373 # my $class = shift;
374 # my $actiongroup = shift;
375 # my $self = $class->new (@_);
376 # $actiongroup->add_action_with_accel ($self, $self->accelerator);
377 # }
378 # sub accelerator {
379 # my ($self) = @_;
380 # return ($self->get('way') eq 'back'
381 # ? __p('accelerator-key','B')
382 # : __p('accelerator-key','F'));
383 # }
384 $actiongroup->add_action_with_accel
385 (Gtk2::Ex::History::Action->new (name => 'Back',
386 way => 'back',
387 history => $history),
388 __p('accelerator-key-back','B'));
389 $actiongroup->add_action_with_accel
390 (Gtk2::Ex::History::Action->new (name => 'Forward',
391 way => 'forward',
392 history => $history),
393 __p('accelerator-key-forward','F'));
394  
395 # initially nothing
396 $actiongroup->get_action('AddFavourite')->set_sensitive (0);
397 $actiongroup->get_action('RemoveFavourite')->set_sensitive (0);
398  
399 # these actions only sensitive when there's a symbol displayed
400 $self->{'symbol_sensitive_actions'} = ['AddFavourite', 'RemoveFavourite',
401 'Delete', 'Centre', 'DownloadUpdate',
402 'Annotations'];
403 _do_symbol_changed ($self, ''); # hack for initial insensitive
404  
405 Gtk2::Ex::ActionTooltips::action_tooltips_to_menuitems_dynamic
406 ($actiongroup->get_action('ManualDataSource'),
407 $actiongroup->get_action('ManualMovingAverage'),
408 $actiongroup->get_action('ManualIndicator'));
409  
410 $actiongroup->add_toggle_actions
411 # name, stock id, label, accel, tooltip, subr, is_active
412 ([{ name => 'Cross',
413 label => __('_Cross'),
414 accelerator => __p('Main-accelerator-key','C'),
415 tooltip => __('Show a crosshair of horizontal and vertical lines following the mouse pointer.'),
416 is_active => 0,
417 callback => sub {
418 my ($action, $self) = @_;
419 $self->{'crosshair_connect'} ||= do {
420 my $cross = $self->{'view'}->crosshair;
421 Glib::Ex::ConnectProperties->new ([$action,'active'],
422 [$cross,'active']);
423 };
424 },
425 },
426 { name => 'Ticker',
427 label => __('_Ticker'),
428 is_active => 0,
429 tooltip => __('Show a stock ticker of share prices at the bottom of the window.
430  
431 Mouse button-1 there drags it back or forward. Button-3 pops up a menu of options, including which symbol list it shows.'),
432 callback => sub {
433 my ($action, $self) = @_;
434 Gtk2::Ex::WidgetCursor->busy; # can take a moment to load
435 $self->get_or_create_ticker;
436 },
437 },
438 { name => 'Toolbar',
439 label => __('T_oolbar'),
440 tooltip => __('Show (or not) the toolbar.'),
441 is_active => 1,
442 },
443 ],
444 $self);
445  
446 my @timebases = qw(App::Chart::Timebase::Days
447 App::Chart::Timebase::Weeks
448 App::Chart::Timebase::Months);
449 $actiongroup->add_radio_actions
450 ([{ name => 'Daily',
451 label => __('_Daily'),
452 accelerator => __p('Main-accelerator-key','<Ctrl>D'),
453 tooltip => __('Display daily data.'),
454 value => 0,
455 },
456 { name => 'Weekly',
457 label =>__('_Weekly'),
458 accelerator => __p('Main-accelerator-key','<Ctrl>W'),
459 tooltip => __('Display weekly data.'),
460 value => 1,
461 },
462 { name => 'Monthly',
463 label =>__('_Monthly'),
464 accelerator => __p('Main-accelerator-key','<Ctrl>M'),
465 tooltip => __('Display monthly data.'),
466 value => 2,
467 }],
468 0, # initial
469 sub { my ($action, $current_action, $self) = @_;
470 Gtk2::Ex::WidgetCursor->busy;
471 $self->{'view'}->set (timebase_class =>
472 $timebases[$action->get_current_value]);
473 },
474 $self);
475  
476 my $ui = $self->{'ui'} = Gtk2::UIManager->new;
477 $ui->insert_action_group ($actiongroup, 0);
478 $self->add_accel_group ($ui->get_accel_group);
479 $ui->add_ui_from_string (<<'HERE');
480 <ui>
481 <menubar name='MenuBar'>
482 <menu action='FileMenu'>
483 <menuitem action='Open'/>
484 <menuitem action='Delete'/>
485 <menuitem action='AddFavourite'/>
486 <menuitem action='RemoveFavourite'/>
487 <separator/>
488 <menuitem action='Prev'/>
489 <menuitem action='Next'/>
490 <menuitem action='Back'/>
491 <menuitem action='Forward'/>
492 <separator/>
493 <menuitem action='Quit'/>
494 </menu>
495 <menu action='EditMenu'>
496 <menuitem action='Annotations'/>
497 <menuitem action='ViewStyle'/>
498 <menuitem action='Preferences'/>
499 <menuitem action='Raw'/>
500 </menu>
501 <menu action='ViewMenu'>
502 <menuitem action='Daily'/>
503 <menuitem action='Weekly'/>
504 <menuitem action='Monthly'/>
505 <separator/>
506 <menuitem action='ZoomInY'/>
507 <menuitem action='ZoomOutY'/>
508 <menuitem action='ZoomInX'/>
509 <menuitem action='ZoomOutX'/>
510 <menuitem action='Centre'/>
511 <menuitem action='Redraw'/>
512 <separator/>
513 <menuitem action='Intraday'/>
514 <menuitem action='WebMenuItem'/>
515 </menu>
516 <menu action='ToolsMenu'>
517 <menuitem action='Download'/>
518 <menuitem action='DownloadUpdate'/>
519 <menuitem action='Watchlist'/>
520 <menuitem action='Cross'/>
521 <menuitem action='Ticker'/>
522 <menuitem action='Toolbar'/>
523 <menuitem action='History'/>
524 <separator/>
525 <menuitem action='Vacuum'/>
526 <menuitem action='Errors'/>
527 <menuitem action='Diagnostics'/>
528 </menu>
529 <menu action='HelpMenu'>
530 <menuitem action='About'/>
531 <menuitem action='Manual'/>
532 <menuitem action='ManualDataSource'/>
533 <menuitem action='ManualMovingAverage'/>
534 <menuitem action='ManualIndicator'/>
535 </menu>
536 </menubar>
537 <toolbar name='ToolBar'>
538 <toolitem action='Prev'/>
539 <toolitem action='Next'/>
540 <toolitem action='Back'/>
541 <toolitem action='Forward'/>
542 </toolbar>
543 </ui>
544 HERE
545  
546 my $vbox = $self->{'vbox'} = Gtk2::VBox->new (0, 0);
547 $vbox->show;
548 $self->add ($vbox);
549  
550 my $menubar = $self->menubar;
551 $menubar->show;
552 $vbox->pack_start ($menubar, 0,0,0);
553 if (my $webitem = $ui->get_widget('/MenuBar/ViewMenu/WebMenuItem')) {
554 $webitem->signal_connect (map => \&_webmenuitem_submenu, $self);
555 }
556  
557 {
558 my $toolbar = $ui->get_widget ('/ToolBar');
559 $vbox->pack_start ($toolbar, 0,0,0);
560 my $action = $actiongroup->get_action ('Toolbar');
561 Glib::Ex::ConnectProperties->new ([$toolbar,'visible'],
562 [$action,'active']);
563 }
564  
565 my $statusbar = $self->{'statusbar'} = Gtk2::Statusbar->new;
566 $statusbar->show;
567  
568 my $view = $self->{'view'} = App::Chart::Gtk2::View->new (statusbar => $statusbar);
569 $view->show;
570 $vbox->pack_start ($view, 1,1,0);
571 $vbox->pack_end ($statusbar, 0,0,0);
572  
573 # default initial size 9/10 of screen, but restrict to aspect ratio 4:3 so
574 # as not to use whole width of a wide screen
575 my $screen = $self->get_screen;
576 my $width = 0.9 * $screen->get_width_mm;
577 my $height = 0.9 * $screen->get_height_mm;
578 # aspect ratio in millimetres
579 ($width, $height) = shrink_to_aspect_ratio ($width, $height, 4.0/3.0);
580 # convert to pixels
581 $width = x_mm_to_pixels ($self, $width);
582 $height = y_mm_to_pixels ($self, $height);
583 $self->set_default_size ($width, $height);
584  
585 foreach (@{$App::Chart::option{'main_hook'}}) { $_->($self) }
586 }
587  
588 # 'destroy' class closure
589 sub _do_destroy {
590 my ($self) = @_;
591 ### Main destroy
592  
593 # {
594 # foreach my $child ($self->get_children) {
595 # print "remove $child\n";
596 # $self->remove ($child);
597 # }
598 # }
599 # break circular references
600 delete $self->{'webmenu'};
601 delete $self->{'history'};
602 delete $self->{'ui'};
603 delete $self->{'actiongroup'};
604 return shift->signal_chain_from_overridden(@_);
605  
606 # delete $self->{'statusbar'};
607 # delete $self->{'view'};
608 # delete $self->{'symbol_sensitive_actions'};
609 # if (DEBUG) { print " keys left: ", join(',', keys %$self), "\n"; }
610 # %$self = ();
611 # print "children ",$self->get_children,"\n";
612  
613 }
614  
615 sub shrink_to_aspect_ratio {
616 my ($width, $height, $ratio) = @_;
617 $width = min ($width, $ratio * $height);
618 $height = min ($height, (1.0/$ratio) * $width);
619 return ($width, $height);
620 }
621 sub x_mm_to_pixels {
622 my ($widget, $x) = @_;
623 my $screen = $widget->get_screen
624 || croak 'x_mm_to_pixels(): widget not on a screen yet';
625 return $x * $screen->get_width / $screen->get_width_mm;
626 }
627 sub y_mm_to_pixels {
628 my ($widget, $y) = @_;
629 my $screen = $widget->get_screen
630 || croak 'y_mm_to_pixels(): widget not on a screen yet';
631 return $y * $screen->get_height / $screen->get_height_mm;
632 }
633  
634 sub _webmenuitem_submenu {
635 my ($webitem, $self) = @_;
636 ### _webmenuitem_submenu() have: $webitem->get_submenu
637 unless ($webitem->get_submenu) {
638 require App::Chart::Gtk2::WeblinkMenu;
639 my $webmenu = $self->{'webmenu'} = App::Chart::Gtk2::WeblinkMenu->new
640 (symbol => $self->{'symbol'});
641 $webitem->set_submenu ($webmenu);
642  
643 # gtk 2.18 bug -- set_submenu doesn't notify attach-widget
644 $webmenu->notify ('attach-widget');
645 }
646 }
647  
648 #------------------------------------------------------------------------------
649 # broadcast handlers
650  
651 # 'delete-symbol' broadcast handler
652 sub _do_delete_symbol {
653 my ($self, $symbol) = @_;
654 my $self_symbol = $self->{'symbol'};
655 ### Main delete-symbol: $symbol
656 ### showing: $self_symbol
657  
658 # when currently displayed symbol is deleted goto next, or if no next
659 # then previous, or if no previous then go to displaying nothing
660 if (defined $self_symbol && $self_symbol eq $symbol) {
661 Gtk2::Ex::WidgetCursor->busy;
662 ($symbol, my $symlist) = $self->smarker->next ('database');
663 if (! defined $symbol) {
664 ### next is undef
665 ($symbol, $symlist) = $self->smarker->prev ('database');
666 ### prev: $symbol
667 }
668 _goto_with_history ($self, $symbol, $symlist);
669 }
670 }
671  
672 #------------------------------------------------------------------------------
673 # misc
674  
675 sub get_or_create_ticker {
676 my ($self) = @_;
677 return ($self->{'ticker'} ||= do {
678 require App::Chart::Gtk2::Ticker;
679 my $ticker = App::Chart::Gtk2::Ticker->new;
680 $self->{'vbox'}->pack_start ($ticker, 0,0,0);
681 my $action = $self->{'actiongroup'}->get_action ('Ticker');
682 Glib::Ex::ConnectProperties->new ([$action,'active'],
683 [$ticker,'visible']);
684 $ticker;
685 });
686 }
687  
688 # 'symbol-changed' class closure
689 sub _do_symbol_changed {
690 my ($self, $symbol) = @_;
691 my $sensitive = (defined $symbol && $symbol ne '');
692 my $actiongroup = $self->{'actiongroup'};
693 foreach my $name (@{$self->{'symbol_sensitive_actions'}}) {
694 my $action = $actiongroup->get_action ($name);
695 $action->set_sensitive ($sensitive);
696 }
697  
698 { my $action = $actiongroup->get_action ('ManualDataSource');
699 my $node = $symbol && App::Chart::symbol_source_help ($symbol);
700 $action->set_sensitive ($node);
701 my $tip = $node && __x('Open the manual at the "{node}" section about this symbol\'s data source', node => $node);
702 $action->set (tooltip => $tip);
703 }
704  
705 { my $action = $actiongroup->get_action ('ManualMovingAverage');
706 my $node = manual_for_graph_n($self,0);
707 $action->set_sensitive ($node);
708 my $tip = $node && __x('Open the manual at the "{node}" section about this moving average', node => $node);
709 $action->set (tooltip => $tip);
710 }
711 { my $action = $actiongroup->get_action ('ManualIndicator');
712 my $node = manual_for_graph_n($self,1);
713 $action->set_sensitive ($node);
714 my $tip = $node && __x('Open the manual at the "{node}" section about this indicator', node => $node);
715 $action->set (tooltip => $tip);
716 }
717 }
718  
719 sub manual_for_graph_n {
720 my ($self, $n) = @_;
721 my $view = $self->{'view'};
722 my $graph = $view->{'graphs'}->[$n] || return;
723 my $series = (List::Util::first {! $_->isa('App::Chart::Series::Database')}
724 @{$graph->get('series-list')})
725 || return;
726 my $func = $series->can('manual') || return;
727 return $series->$func;
728 }
729  
730 #------------------------------------------------------------------------------
731 # navigation
732  
733 sub symbol_history {
734 my ($self) = @_;
735 return $self->{'history'};
736 }
737 sub _history_place_to_text {
738 my ($history, $place) = @_;
739 require App::Chart::Database;
740 my ($symbol, $symlist_key) = @$place;
741 if (defined (my $name = App::Chart::Database->symbol_name ($symbol))) {
742 $symbol .= " - $name";
743 }
744 return $symbol;
745 }
746 sub _history_place_to_symbol_symlist {
747 my ($place) = @_;
748 my ($symbol, $symlist_key) = @$place;
749 require App::Chart::Gtk2::Symlist;
750 my $symlist = App::Chart::Gtk2::Symlist->new_from_key_maybe ($symlist_key);
751 return ($symbol, $symlist);
752 }
753 sub _history_symbol_symlist_to_place {
754 my ($symbol, $symlist) = @_;
755 return [ $symbol, $symlist && $symlist->key ];
756 }
757  
758 sub _goto {
759 my ($self, $symbol, $symlist) = @_;
760 Gtk2::Ex::WidgetCursor->busy;
761 $self->{'symbol'} = $symbol;
762 $self->{'symlist'} = $symlist;
763 $self->{'view'}->set('symbol', $symbol);
764 if (my $webmenu = $self->{'webmenu'}) { $webmenu->set('symbol', $symbol); }
765 $self->signal_emit ('symbol-changed', $symbol);
766  
767 if ($symbol) {
768 require App::Chart::Gtk2::Job::Latest;
769 App::Chart::Gtk2::Job::Latest->start_for_view ($symbol, $symlist);
770 }
771 }
772 sub _goto_with_history {
773 my ($self, $symbol, $symlist) = @_;
774 ### _goto_with_history: $symbol, $symlist && $symlist->key
775 if ($symbol) {
776 my $history = $self->symbol_history;
777 $history->goto (_history_symbol_symlist_to_place ($symbol, $symlist));
778 }
779 }
780 sub _history_notify_current {
781 my ($history, $pspec, $self) = @_;
782 _goto ($self, _history_place_to_symbol_symlist($history->get('current')));
783 }
784  
785 sub smarker {
786 my ($self) = @_;
787 return ($self->{'smarker'} ||= do {
788 require App::Chart::Gtk2::Smarker;
789 App::Chart::Gtk2::Smarker->new;
790 });
791 }
792  
793 sub goto_symbol {
794 my ($self, $symbol, $symlist) = @_;
795 ### Main goto: $symbol, $symlist && $symlist->key
796 _goto_with_history ($self, $symbol, $symlist);
797 $self->smarker->goto ($symbol, $symlist);
798 }
799  
800 sub goto_next {
801 my ($self) = @_;
802 Gtk2::Ex::WidgetCursor->busy;
803 my ($symbol, $symlist) = $self->smarker->next ('database');
804  
805 # require App::Chart::Gtk2::Symlist;
806 # my ($symbol, $symlist)
807 # = App::Chart::Gtk2::Symlist::next ($self->{'symbol'}, $self->{'symlist'});
808 if ($symbol) {
809 _goto_with_history ($self, $symbol, $symlist);
810 } else {
811 _message ($self, __('At end of lists'));
812 }
813 }
814  
815 sub go_prev {
816 my ($self) = @_;
817 Gtk2::Ex::WidgetCursor->busy;
818 my ($symbol, $symlist) = $self->smarker->prev ('database');
819 # require App::Chart::Gtk2::Symlist;
820 # my ($symbol, $symlist)
821 # = App::Chart::Gtk2::Symlist::previous ($self->{'symbol'}, $self->{'symlist'});
822 if ($symbol) {
823 _goto_with_history ($self, $symbol, $symlist);
824 } else {
825 _message ($self, __('At start of lists'));
826 }
827 }
828  
829 sub go_back {
830 my ($self) = @_;
831 $self->symbol_history->back;
832 }
833 sub go_forward {
834 my ($self) = @_;
835 $self->symbol_history->forward;
836 }
837  
838  
839 #------------------------------------------------------------------------------
840 # other
841  
842 sub _message {
843 my ($self, $str) = @_;
844 require Gtk2::Ex::Statusbar::MessageUntilKey;
845 Gtk2::Ex::Statusbar::MessageUntilKey->message ($self->{'statusbar'}, $str);
846 }
847  
848 # return currently displayed symbol, or undef
849 sub get_symbol {
850 my ($self) = @_;
851 return $self->{'view'}->get('symbol');
852 }
853  
854 # return the menubar widget
855 sub menubar {
856 my ($self) = @_;
857 return $self->{'ui'}->get_widget('/MenuBar');
858 }
859  
860 # return App::Chart::Gtk2::Main widget, preferring the transient parent of $dialog,
861 # if $dialog is given
862 sub find_for_dialog {
863 my ($class, $dialog) = @_;
864 if ($dialog
865 && $dialog->can('get_transient_for')
866 && (my $parent = $dialog->get_transient_for)) {
867 if ($parent->isa($class)) {
868 return $parent;
869 }
870 }
871  
872 my $screen = $dialog->get_screen;
873 foreach my $toplevel (Gtk2::Window->list_toplevels) {
874 if ($toplevel->isa($class) && $toplevel->get_screen == $screen) {
875 return $toplevel;
876 }
877 }
878  
879 foreach my $toplevel (Gtk2::Window->list_toplevels) {
880 if ($toplevel->isa($class)) {
881 return $toplevel;
882 }
883 }
884 return $class->new;
885 }
886  
887 #------------------------------------------------------------------------------
888 # mainline
889  
890 sub main {
891 my ($class, $args) = @_;
892 ### Main main() args: $args
893 ## no critic (ProhibitExit, ProhibitExitInSubroutines)
894  
895 my $symbol = $args->[0];
896 my $symlist = undef;
897 if (@$args > 1 || (Scalar::Util::blessed($symbol)
898 && $symbol->isa('App::Chart::Gtk2::Symlist'))) {
899 print STDERR __"chart: only one symbol argument when starting the GUI\n";
900 exit 1;
901 }
902  
903 require Gtk2::Ex::ErrorTextDialog::Handler;
904 Glib->install_exception_handler
905 (\&Gtk2::Ex::ErrorTextDialog::Handler::exception_handler);
906 if (0) {
907 ## no critic (RequireLocalizedPunctuationVars)
908 $SIG{'__WARN__'} = \&Gtk2::Ex::ErrorTextDialog::Handler::exception_handler;
909 }
910  
911 Gtk2->disable_setlocale; # leave LC_NUMERIC alone for version nums
912 Gtk2->init;
913  
914 Glib::Idle->add
915 (sub {
916 my $self = $class->instance;
917 $self->signal_connect (destroy => sub { Gtk2->main_quit; });
918  
919 main::initfile (File::Spec->catfile(App::Chart::chart_directory(),
920 'gui.pl'));
921  
922 $self->show;
923 App::Chart::chart_dirbroadcast()->listen;
924  
925 if (defined $symbol) {
926 my $label = $self->{'view'}->{'initial'}->get_child;
927 $label->set_text(__x('Loading {symbol} ...', symbol => $symbol));
928 Glib::Idle->add
929 (sub {
930 require App::Chart::SymbolMatch;
931 my ($match_symbol, $match_symlist)
932 = App::Chart::SymbolMatch::find ($symbol);
933 if ($match_symbol) {
934 ($symbol, $symlist) = ($match_symbol, $match_symlist);
935 }
936 $self->goto_symbol ($symbol, $symlist);
937 return Glib::SOURCE_REMOVE;
938 });
939 }
940 return Glib::SOURCE_REMOVE;
941 },
942 Glib::G_PRIORITY_HIGH);
943 Gtk2->main;
944 }
945  
946 1;
947 __END__