blib/lib/ChordPro/Wx/Main.pm untested

filter: Statementstmt 0.0% 0 / 304 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 517 CC 6 SCAR 22.3
CC
6
Cov
0%
OnInit
42.0
run
2.0
::sys
2.0
CRAP 9.3
1 #! perl
2  
3 use v5.26;
4 use feature 'signatures';
5 no warnings 'experimental::signatures';
6 use utf8;
7  
8 ################ Entry ################
9  
10 our $options;
11 our $Wx_min = "3.004";
12  
13 package ChordPro::Wx::WxChordPro;
14  
15 use parent qw( Wx::App ChordPro::Wx::Main );
16  
17 use ChordPro::Paths;
18 use ChordPro::Wx::Config;
19  
20 use Wx qw( wxACCEL_CTRL WXK_CONTROL_Q wxID_EXIT );
21  
22 sub run( $self, $opts ) {
23  
24 $options = $opts;
25  
26 #### Start ################
27  
28 ChordPro::Wx::WxChordPro->new->MainLoop();
29  
30 }
31  
32 sub OnInit( $self ) {
33  
34 $self->SetAppName("ChordPro");
35 $self->SetVendorName("ChordPro.ORG");
36 Wx::InitAllImageHandlers();
37 ChordPro::Wx::Config->Setup($options);
38 ChordPro::Wx::Config->Load($options);
39  
40 my $main = ChordPro::Wx::Main->new;
41 return 0 unless $main->init($options);
42  
43 $self->SetTopWindow($main);
44 $main->Show(1);
45  
46 if ( $options->{maximize} ) {
47 $main->Maximize(1);
48 }
49  
50 elsif ( $options->{geometry}
51 && $options->{geometry} =~ /^(?:(\d+)x(\d+))?(?:([+-]\d+)([+-]\d+))?$/ ) {
52 $main->SetSize( $1, $2 )
53 if defined($1) && defined($2);
54 $main->Move( $3+0, $4+0 )
55 if defined($3) && defined($4);
56 }
57  
58 return 1;
59 }
60  
61 ################ Static & Overrides ################
62  
63 use Wx qw[:everything];
64 use ChordPro::Wx::Utils;
65  
66 # Synchronous system call. Used in ChordPro::Utils module.
67 sub ::sys { Wx::ExecuteArgs( \@_, wxEXEC_SYNC | wxEXEC_HIDE_CONSOLE ); }
68  
69 use warnings 'redefine';
70  
71 ################ Main ################
72  
73 use Object::Pad;
74  
75 class ChordPro::Wx::Main :isa(ChordPro::Wx::Main_wxg);
76  
77 use ChordPro; our $VERSION = $ChordPro::VERSION;
78 use ChordPro::Files;
79 use ChordPro::Paths;
80 use ChordPro::Output::Common;
81 use ChordPro::Utils qw( demarkup );
82  
83 use Wx qw[:everything];
84 use Wx::Locale gettext => '_T';
85  
86 use ChordPro::Wx::Config;
87 use ChordPro::Wx::Utils;
88  
89 use Encode qw(decode_utf8);
90  
91 method log( $level, $msg ) {
92 $msg =~ s/\n+$//;
93  
94 # $msg = "[$level] $msg";
95 if ( $level eq 'I' ) {
96 Wx::LogMessage( "%s", $msg);
97 }
98 if ( $level eq 'S' ) {
99 Wx::LogMessage( "%s", $msg );
100 }
101 elsif ( $level eq 'W' ) {
102 Wx::LogWarning( "%s", $msg);
103 }
104 elsif ( $level eq 'E' ) {
105 Wx::LogError( "%s", $msg);
106 }
107 elsif ( $level eq 'F' ) {
108 Wx::LogFatal( "%s", $msg);
109 }
110 }
111  
112 BUILD {
113  
114 Wx::Event::EVT_IDLE($self, $self->can('OnIdle'));
115 Wx::Event::EVT_CLOSE($self, $self->can('OnClose'));
116  
117 # Later, the panels will take over logging.
118 Wx::Log::SetActiveTarget( Wx::LogStderr->new );
119 $self->SetTitle("ChordPro");
120 $self->SetIcon( Wx::Icon->new(CP->findres( "chordpro-icon.png", class => "icons" ), wxBITMAP_TYPE_ANY) );
121  
122 $self->attach_events;
123  
124 # MacOS file dialogs always filters with all wildcards. So if there is
125 # an "All files|*.*" at the end, all file will match.
126 # So either remove the *.* or use the following code:
127 Wx::SystemOptions::SetOption("osx.openfiledialog.always-show-types", 1)
128 if 0 && is_macos;
129 }
130  
131 method attach_events() {
132  
133 # To select actions, we use a panel with a bitmap and a text.
134 # We need to attach a mouse click (EVT_LEFT_UP) to the panel and
135 # all of its children.
136  
137 my %panels =
138 ( new => "OnNew",
139 open => "OnOpen",
140 sbexport => "OnExportFolder",
141 example => "OnHelp_Example",
142 site => "OnHelp_Site",
143 help => "OnHelp_Docs",
144 );
145 while ( my ( $p, $handler ) = each %panels ) {
146 my $panel = $self->{"pn_$p"};
147 #$handler = "OnI".ucfirst($p);
148 my $h = $self->can($handler);
149 warn("Missing pn handler $handler") unless $h;
150 $handler = sub { &$h( $self, undef ) };
151 Wx::Event::EVT_LEFT_UP( $panel, $handler );
152 foreach my $n ( $panel->GetChildren ) {
153 Wx::Event::EVT_LEFT_UP( $n, $handler );
154 }
155 }
156  
157 Wx::Event::EVT_MAXIMIZE( $self, $self->can("OnMaximized") );
158 }
159  
160 method select_mode( $mode ) {
161 my @panels = panels;
162  
163 if ( $mode eq "initial" ) {
164 $self->{$_}->Show(0) for @panels;
165 $self->{p_initial}->Show(1);
166 $self->refresh;
167 $self->SetTitle("ChordPro");
168 }
169 else {
170 $self->{p_initial}->Show(0);
171 $self->{$_}->Show( $_ eq "p_$mode" ) for @panels;
172 $self->{"p_$mode"}->refresh;
173 }
174 $self->{sz_main}->Layout;
175 $state{mode} = $mode;
176 return $state{panel} = $self->{"p_$mode"};
177 }
178  
179 # Explicit (re)initialisation of this class.
180 method init( $options ) {
181  
182 $state{mode} = "initial";
183  
184 # General runtime options.
185 $state{verbose} = $options->{verbose};
186 $state{trace} = $options->{trace};
187 $state{debug} = $options->{debug};
188  
189 # For development/debugging.
190 $state{logstderr} = $options->{logstderr};
191  
192 $self->SetStatusBar(undef);
193 $self->get_preferences;
194 $self->setup_menubar;
195 $self->setup_statusbar;
196 Wx::Event::EVT_SYS_COLOUR_CHANGED( $self,
197 $self->can("OnSysColourChanged") );
198 $self->init_theme;
199  
200 if ( @ARGV ) {
201 # use DDP;
202 use charnames ':full';
203 require _charnames;
204 push( @{$state{msgs}},
205 "ARGV: " . np(@ARGV,
206 show_unicode => 1,
207 escape_chars => 'nonascii',
208 unicode_charnames => 1 ) ) if 0;
209 my $arg = shift(@ARGV); # ignore rest
210 $arg = decode_utf8($arg);
211 push( @{$state{msgs}},
212 'DECODED: ' . np($arg,
213 show_unicode => 1,
214 escape_chars => 'nonascii',
215 unicode_charnames => 1 ) ) if 0;
216  
217 if ( fs_test( 'd', $arg ) ) {
218 return 1 if $self->select_mode("sbexport")->open_dir($arg);
219 }
220 elsif ( !fs_test( 'r', $arg ) ) {
221 return 1 if $self->select_mode("editor")->newfile($arg);
222 Wx::MessageDialog->new( $self, "Error opening $arg",
223 "File Open Error",
224 wxOK | wxICON_ERROR )->ShowModal;
225 }
226 else {
227 return $self->select_mode("editor")->openfile($arg);
228 }
229 }
230 if ( $options->{new} ) {
231 $self->select_mode("editor")->newfile;
232 }
233 else {
234 $self->select_mode("initial");
235 }
236  
237 return 1;
238 }
239  
240 method refresh() {
241 $self->init_recents;
242 $self->update_menubar(M_MAIN);
243 }
244  
245 method init_recents() {
246  
247 my $r = $state{recents};
248  
249 if ( defined $r->[0] ) {
250 my $ctl = $self->{lb_recent};
251 $ctl->Clear;
252 $ctl->Enable(1);
253 my $i = 0;
254 for my $file ( @$r ) {
255 next unless fs_test( s => $file );
256 last unless defined $file;
257 $ctl->Append( fn_basename($file) );
258 $ctl->SetClientData( $i, $file );
259 $i++;
260 }
261 }
262 }
263  
264 method init_theme() {
265 # Command line always overrides. Once.
266 if ( !defined $state{editortheme} && defined $options->{dark} ) {
267 $state{editortheme} = $options->{dark} ? "dark" : "light";
268 }
269 elsif ( $preferences{editortheme} eq "auto" ) {
270 if ( Wx::SystemSettings->can("GetAppearance") ) {
271 my $a = Wx::SystemSettings::GetAppearance();
272 if ( $a->IsDark ) {
273 $state{editortheme} = "dark";
274 }
275 else {
276 $state{editortheme} = "light";
277 }
278 }
279 else {
280 # Auto and no clue? Use light.
281 $preferences{editortheme} = $state{editortheme} = "light";
282 }
283 }
284 else {
285 $state{editortheme} = $preferences{editortheme};
286 }
287 push( @{$state{msgs}}, "Using $state{editortheme} theme" );
288 }
289  
290 method get_preferences() {
291  
292 restorewinpos( $self, "main" );
293 $self->Show(1);
294 }
295  
296 method save_preferences() {
297 savewinpos( $self, "main" );
298 ChordPro::Wx::Config->Store;
299 }
300  
301 method aboutmsg() {
302 my $firstyear = 2016;
303 my $year = 1900 + (localtime(time))[5];
304 if ( $year != $firstyear ) {
305 $year = "$firstyear,$year";
306 }
307  
308 # Sometimes version numbers are localized...
309 my $dd = sub { my $v = $_[0]; $v =~ s/,/./g; $v };
310  
311 local $ENV{CHORDPRO_LIB} =
312 $preferences{enable_customlib} ? $preferences{customlib} : "";
313 CP->setup_resdirs;
314 my $msg = join
315 ( "",
316 "ChordPro version ",
317 $dd->($ChordPro::VERSION),
318 "\n",
319 "https://www.chordpro.org\n",
320 "Copyright $year Johan Vromans <jvromans\@squirrel.nl>\n",
321 "\n",
322 "GUI designed with wxGlade by the ChordPro Team\n\n",
323 "Run-time information:\n",
324 ::runtimeinfo() =~ s/CHORDPRO_LIB/Custom lib /rm
325 );
326  
327 return $msg;
328 }
329  
330 method check_saved() {
331 for ( panels ) {
332 return unless $self->{$_}->check_source_saved;
333 return unless $self->{$_}->check_preview_saved;
334 }
335 # Panels may save prefs to preferences.
336 $self->{$_}->save_preferences for panels;
337 1;
338 }
339  
340 method setup_statusbar() {
341 # Add statusbar.
342 return unless $preferences{expert};
343 $self->{f_main_statusbar} = $self->CreateStatusBar(1);
344 $self->{f_main_statusbar}->SetStatusWidths(-1);
345 }
346  
347 ################ Event handlers (alphabetic order) ################
348  
349 # This method is called from the helper panels.
350 method OnAbout($event) {
351  
352 my $info = Wx::AboutDialogInfo->new;
353 my $year = 1900 + (localtime(time))[5];
354 $info->SetName("ChordPro");
355 $info->SetVersion( $VERSION .
356 ( $VERSION =~ /_/ ? " (unsupported development snapshot)" : "" ) );
357 $info->SetDescription("ChordPro is free software");
358 $info->SetCopyright("Ⓒ 2016-$year Johan Vromans\nThe ChordPro Team");
359 $info->SetWebSite( "https://www.chordpro.org",
360 "Visit the ChordPro web site");
361 my $icon = Wx::Icon->new;
362 $info->SetIcon($icon)
363 if $icon->LoadFile( CP->findres("chordpro-splash.png",class=>"icons"),wxBITMAP_TYPE_PNG );
364 Wx::AboutBox($info);
365 }
366  
367 method OnClose($event) {
368 return unless $self->check_saved;
369 # Save preferences to persistent storage.
370 $self->save_preferences;
371 # Stop webview process (GTK).
372 if ( $self->{panel} && $self->{panel}->{webview} ) {
373 $self->{panel}->{webview}->Close;
374 }
375 $self->Destroy;
376 }
377  
378 # SHow the create buttons, or the recents list.
379 method OnCreateRecent($event) {
380 $self->create_or_recent( $self->{rb_createrecent}->GetSelection );
381 }
382  
383 method OnExportFolder($event) {
384  
385 # We handle this here for the same reasons as OnOpen.
386 my $fd = Wx::DirDialog->new
387 ( $self,
388 _T("Select the folder with the songs"),
389 $state{sbe_folder} // $state{songbookexport}{folder} // "",
390 wxDD_DIR_MUST_EXIST );
391 my $ret = $fd->ShowModal;
392 if ( $ret == wxID_OK ) {
393 $self->select_mode("sbexport")->open_dir( $fd->GetPath );
394 }
395 $fd->Destroy;
396 }
397  
398 method OnIdle($event) {
399 # Cannot check from init. Do it here.
400 unless ( ChordPro::Wx::Config->Ok ) {
401 ChordPro::Wx::Config->SetOk;
402 my $md = Wx::MessageDialog->new
403 ( undef,
404 "Your Settings have been migrated.\n".
405 "Some Settings may have been reset to default values.\n".
406 "\n".
407 "Sorry for the inconvenience.",
408 "Check your Settings",
409 Wx::wxOK|Wx::wxICON_WARNING|Wx::wxDIALOG_NO_PARENT );
410 $md->ShowModal;
411 }
412 return if $self->{p_initial}->IsShown;
413 my $mod = $self->{p_editor}->{t_editor}->IsModified;
414 my $f = fn_basename($state{windowtitle} // "ChordPro");
415 if ( is_macos ) {
416 wxTheApp->GetTopWindow->OSXSetModified($mod);
417 }
418 else {
419 $f .= " (modified)" if $mod;
420 }
421 $f = "ChordPro — $f" if $state{windowtitle};
422 $self->SetTitle($f);
423  
424 if ( $state{mode} eq "editor") {
425 my $t = $self->{p_editor}->{t_editor}->GetText;
426  
427 if ( $t =~ /^\{\s*t(?:itle)?[: ]+([^\}]*)\}/m ) {
428 $self->{p_editor}->{l_status}->SetLabel(demarkup($1));
429 }
430  
431 if ( $state{editchanged}
432 && $self->{p_editor}->{sw_lr}->IsSplit ) {
433  
434 if ( $state{have_webview}
435 && $preferences{enable_htmlviewer} ) {
436 $self->{p_editor}->preview([]);
437 $state{editchanged} = 0;
438 }
439 else {
440 # Preview is no longer actual -- how to signal?
441 }
442 }
443 }
444  
445 }
446  
447 # From menu.
448 method OnHelp_ChordPro($event) {
449 Wx::LaunchDefaultBrowser("https://www.chordpro.org/chordpro/chordpro-introduction/");
450 }
451  
452 # From menu.
453 method OnHelp_Config($event) {
454 Wx::LaunchDefaultBrowser("https://www.chordpro.org/chordpro/chordpro-configuration/");
455 }
456  
457 method OnHelp_Example($event) {
458 $self->select_mode("editor");
459 $self->{p_editor}->openfile( CP->findres( $preferences{expert}
460 ? "mollymalone.cho"
461 : "swinglow.cho",
462 class => "examples" ),
463 1, " example.cho " );
464 }
465  
466 method OnExpertLineEndings($event) {
467 $state{vieweol} = wxTheApp->GetTopWindow->GetMenuBar->FindItem($event->GetId)->IsChecked;
468 if ( $state{mode} eq "editor" ) {
469 $self->{p_editor}->{t_editor}->SetViewEOL($state{vieweol});
470 }
471 }
472  
473 method OnExpertWhiteSpace($event) {
474 $state{viewws} = wxTheApp->GetTopWindow->GetMenuBar->FindItem($event->GetId)->IsChecked;
475 if ( $state{mode} eq "editor" ) {
476 $self->{p_editor}->{t_editor}->SetViewWhiteSpace($state{viewws});
477 }
478 }
479  
480 # From opening panel.
481 method OnHelp_Docs($event) {
482 Wx::LaunchDefaultBrowser("https://www.chordpro.org/chordpro/home/");
483 }
484  
485 # From opening panel.
486 method OnHelp_Site($event) {
487 Wx::LaunchDefaultBrowser("https://www.chordpro.org/");
488 }
489  
490 method OnMaximize($event) {
491 my $top = wxTheApp->GetTopWindow;
492 # Note that ShowFullScreen on macOS isn't really Full Screen.
493 # https://github.com/ChordPro/chordpro/issues/373#issuecomment-2501855028
494 my $full = $top->IsMaximized;
495 $top->Maximize( !$full );
496 }
497  
498 method OnNew($event) {
499 if ( $state{mode} eq "initial" ) {
500 $self->select_mode("editor");
501 }
502 else {
503 return unless $self->check_saved;
504 }
505 $self->{p_editor}->newfile;
506 }
507  
508 method OnOpen($event) {
509 return unless $self->check_saved;
510  
511 # In case it is a synthetic event.
512 if ( $event && ( my $arg = $event->GetClientData ) ) {
513 $self->select_mode("editor")->openfile( $arg, 1 );
514 return;
515 }
516  
517 # We handle the dialog here, so we do not have to switch to the editor
518 # unless there's real editing to do.
519  
520 my $fd = Wx::FileDialog->new
521 ( $self,
522 _T("Choose ChordPro file"),
523 fn_dirname($state{recents}[0]//""),
524 "",
525 $state{ffilters},
526 wxFD_OPEN|wxFD_FILE_MUST_EXIST );
527 my $ret = $fd->ShowModal;
528 if ( $ret == wxID_OK ) {
529 $self->select_mode("editor")->openfile( $fd->GetPath, 1 );
530 }
531 $fd->Destroy;
532 }
533  
534 method OnPreferences($event) {
535 unless ( $self->{d_prefs} ) {
536 require ChordPro::Wx::SettingsDialog;
537 $self->{d_prefs} = ChordPro::Wx::SettingsDialog->new
538 ( $self, wxID_ANY, "Settings" );
539 restorewinpos( $self->{d_prefs}, "prefs" );
540 }
541 else {
542 $self->{d_prefs}->refresh;
543 }
544  
545 # The Settings dialog operates on the current $preferences.
546 my $ret = $self->{d_prefs}->ShowModal;
547 savewinpos( $self->{d_prefs}, "prefs" );
548 $state{panel}->set_focus unless $state{mode} eq "initial";
549 return unless $ret == wxID_OK;
550  
551 # $preferences may have changed.
552 $self->save_preferences;
553  
554 # Update the requestor.
555 $state{panel}->update_preferences unless $state{mode} eq "initial";
556  
557 $self->setup_menubar;
558 }
559  
560 # On the recents list, click selects and displays the file name.
561 # Double click selects the entry for processing.
562  
563 method OnRecentDclick($event) {
564 my $file = $self->{l_recent}->GetLabel;
565 $self->select_mode("editor");
566 $self->{p_editor}->openfile( $file, 0 );
567 }
568  
569 method OnRecentSelect($event) {
570 my $n = $self->{lb_recent}->GetSelection;
571 my $file = $self->{lb_recent}->GetClientData($n);
572 $self->{l_recent}->SetLabel($file);
573 $self->{l_recent}->SetToolTip($file);
574 }
575  
576 method OnStart($event) {
577 return unless $self->check_saved;
578 $self->select_mode("initial");
579 }
580  
581 method OnSysColourChanged($event) {
582 $self->init_theme;
583 $state{panel}->{t_editor}->refresh unless $state{mode} eq "initial";
584 }
585  
586 ################ End of Event handlers ################
587  
588 1;