| 1 | #! perl | ||
| 2 | |||
| 3 | use v5.26; | ||
| 4 | use Object::Pad; | ||
| 5 | use utf8; | ||
| 6 | |||
| 7 | class ChordPro::Wx::SettingsDialog | ||
| 8 | :repr(HASH) | ||
| 9 | :isa(ChordPro::Wx::SettingsDialog_wxg); | ||
| 10 | |||
| 11 | use Wx qw[:everything]; | ||
| 12 | use Wx::Locale gettext => '_T'; | ||
| 13 | use ChordPro::Files; | ||
| 14 | use ChordPro::Paths; | ||
| 15 | use ChordPro::Wx::Config; | ||
| 16 | use ChordPro::Wx::Utils; | ||
| 17 | use File::Basename; | ||
| 18 | use Ref::Util qw( is_arrayref ); | ||
| 19 | use List::Util qw( first ); | ||
| 20 | |||
| 21 | BUILD ( $parent, $id, $title ) { | ||
| 22 | $self->refresh; | ||
| 23 | $self->{sz_prefs_outer}->Fit($self); | ||
| 24 | $self->Layout; | ||
| 25 | Wx::Event::EVT_SYS_COLOUR_CHANGED( $self, | ||
| 26 | $self->can("OnSysColourChanged") ); | ||
| 27 | |||
| 28 | Wx::Event::EVT_ENTER_WINDOW( $self->{ch_stylemods}, | ||
| 29 | $self->{ch_stylemods}->can("OnEnter") ); | ||
| 30 | Wx::Event::EVT_LEAVE_WINDOW( $self->{ch_stylemods}, | ||
| 31 | $self->{ch_stylemods}->can("OnLeave") ); | ||
| 32 | Wx::Event::EVT_MOTION( $self->{ch_stylemods}, | ||
| 33 | $self->{ch_stylemods}->can("OnMotion") ); | ||
| 34 | |||
| 35 | # Do not DeletePage until we're sure none of the widgets are referenced. | ||
| 36 | $self->{nb_preferences}->RemovePage(5) # HTML viewer | ||
| 37 | unless $preferences{expert}; | ||
| 38 | $self->{nb_preferences}->RemovePage(4) # PDF viewer | ||
| 39 | unless $preferences{pdfviewer} || $preferences{enable_pdfviewer}; | ||
| 40 | |||
| 41 | unless ( has_appearance() ) { | ||
| 42 | $self->{ch_theme}->Delete(2); # Follow System | ||
| 43 | } | ||
| 44 | |||
| 45 | $self; | ||
| 46 | } | ||
| 47 | |||
| 48 | my $checkpfx = "✔ "; | ||
| 49 | |||
| 50 | method refresh() { | ||
| 51 | $self->fetch_prefs; | ||
| 52 | $self->{t_editor}->refresh; | ||
| 53 | $self->{t_editor}->SetText(<<EOD); | ||
| 54 | {title: St. James Infirmary Blues} | ||
| 55 | {subtitle: Traditional} | ||
| 56 | |||
| 57 | # Song starts here. | ||
| 58 | I went [Em]down to the [Am]St James In[Em]firmary | ||
| 59 | I found my [Am]baby [B7]there | ||
| 60 | EOD | ||
| 61 | } | ||
| 62 | |||
| 63 | method enablecustom() { | ||
| 64 | my $n = $self->{cb_configfile}->IsChecked; | ||
| 65 | $self->{fp_customconfig}->Enable($n); | ||
| 66 | $self->{b_createconfig}->Enable($n); | ||
| 67 | |||
| 68 | $n = $self->{cb_customlib}->IsChecked; | ||
| 69 | $self->{dp_customlibrary}->Enable($n); | ||
| 70 | |||
| 71 | $n = $self->{cb_tmplfile}->IsChecked; | ||
| 72 | $self->{fp_tmplfile}->Enable($n); | ||
| 73 | |||
| 74 | # Add instruments. | ||
| 75 | for my $ctl ( $self->{ch_instrument} ) { | ||
| 76 | $ctl->Clear; | ||
| 77 | for ( sort keys %{$state{presets}{instruments}} ) { | ||
| 78 | $ctl->Append( $state{presets}{instruments}->{$_}->{title}, | ||
| 79 | $state{presets}{instruments}->{$_}, | ||
| 80 | ); | ||
| 81 | } | ||
| 82 | my $first = 0; | ||
| 83 | my $def = | ||
| 84 | is_arrayref($preferences{preset_instruments}) | ||
| 85 | && @{$preferences{preset_instruments}} | ||
| 86 | ? $preferences{preset_instruments}[0]->{title} | ||
| 87 | : "Guitar"; | ||
| 88 | my $n = $ctl->FindString($def); | ||
| 89 | $first = $n unless $n == wxNOT_FOUND; | ||
| 90 | $ctl->SetSelection($first); | ||
| 91 | $self->set_instrument_desc( $ctl->GetClientData($first)->{desc} ); | ||
| 92 | } | ||
| 93 | |||
| 94 | # Add styles. | ||
| 95 | for my $ctl ( $self->{ch_style} ) { | ||
| 96 | $ctl->Clear; | ||
| 97 | $ctl->Append( "Default", | ||
| 98 | { title => "Default", | ||
| 99 | desc => "Default ChordPro style.", | ||
| 100 | preview => "style_default-small.jpg", | ||
| 101 | } ); | ||
| 102 | for ( sort keys %{$state{presets}{styles}} ) { | ||
| 103 | $ctl->Append( $state{presets}{styles}->{$_}->{title}, | ||
| 104 | $state{presets}{styles}->{$_}, | ||
| 105 | ); | ||
| 106 | } | ||
| 107 | my $first = 0; | ||
| 108 | my $def = | ||
| 109 | is_arrayref($preferences{preset_styles}) | ||
| 110 | && @{$preferences{preset_styles}} | ||
| 111 | ? $preferences{preset_styles}[0]->{title} | ||
| 112 | : "Default"; | ||
| 113 | my $n = $ctl->FindString($def); | ||
| 114 | $first = $n unless $n == wxNOT_FOUND; | ||
| 115 | $ctl->SetSelection($n); | ||
| 116 | $self->set_style_desc( $ctl->GetClientData($n)->{desc} ); | ||
| 117 | } | ||
| 118 | |||
| 119 | # Add the styles to the presets. | ||
| 120 | for my $ctl ( $self->{ch_stylemods} ) { | ||
| 121 | $ctl->Clear; | ||
| 122 | for ( sort keys %{$state{presets}{stylemods}} ) { | ||
| 123 | $ctl->Append( $state{presets}{stylemods}->{$_}->{title}, | ||
| 124 | $state{presets}{stylemods}->{$_}, | ||
| 125 | ); | ||
| 126 | } | ||
| 127 | |||
| 128 | # Check the presets that were selected. | ||
| 129 | my $p = $preferences{preset_stylemods} // []; | ||
| 130 | my $desc = ""; | ||
| 131 | my $first = 0; | ||
| 132 | foreach ( sort { $a->{title} cmp $b->{title} } @$p ) { | ||
| 133 | my $n = $ctl->FindString( $_->{title} ); | ||
| 134 | next if $n == wxNOT_FOUND; | ||
| 135 | $ctl->Check( $n, 1 ); | ||
| 136 | if ( $_->{desc} ) { | ||
| 137 | $desc .= $checkpfx; | ||
| 138 | $desc .= ucfirst($_->{src}) . ": " | ||
| 139 | unless $_->{src} eq "std"; | ||
| 140 | $desc .= $_->{desc} . "\n"; | ||
| 141 | } | ||
| 142 | $first //= $n; | ||
| 143 | } | ||
| 144 | if ( $desc ) { | ||
| 145 | $desc =~ s/\n+$//; | ||
| 146 | $self->set_stylemods_desc($desc); | ||
| 147 | $ctl->SetFirstItem($first); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | # Notation systems. | ||
| 152 | for my $ctl ( $self->{ch_notation} ) { | ||
| 153 | $ctl->Clear; | ||
| 154 | my $n = 0; | ||
| 155 | my $check; | ||
| 156 | for ( sort keys %{$state{presets}{notations}} ) { | ||
| 157 | $ctl->Append( $state{presets}{notations}->{$_}->{title} . | ||
| 158 | " (" . $state{presets}{notations}->{$_}->{desc} . ")", | ||
| 159 | $state{presets}{notations}->{$_}, | ||
| 160 | ); | ||
| 161 | $check = $n | ||
| 162 | if $_ eq lc($preferences{preset_notations}[0]->{title}); | ||
| 163 | $check //= $n | ||
| 164 | if $state{presets}{notations}->{$_}->{default}; | ||
| 165 | $n++; | ||
| 166 | } | ||
| 167 | |||
| 168 | unless ( defined $check ) { | ||
| 169 | use DDP; p $state{presets}{notations}, as => "Missing default in notations"; | ||
| 170 | } | ||
| 171 | $ctl->SetSelection($check); | ||
| 172 | } | ||
| 173 | |||
| 174 | # Transcodings | ||
| 175 | for my $ctl ( $self->{ch_xcode} ) { | ||
| 176 | $ctl->Clear; | ||
| 177 | my $n = 0; | ||
| 178 | my $check; | ||
| 179 | for ( sort keys %{$state{presets}{notations}} ) { | ||
| 180 | $ctl->Append( $state{presets}{notations}->{$_}->{title} . | ||
| 181 | " (" . $state{presets}{notations}->{$_}->{desc} . ")", | ||
| 182 | $state{presets}{notations}->{$_}, | ||
| 183 | ); | ||
| 184 | $check = $n | ||
| 185 | if $_ eq lc($preferences{preset_xcodes}[0]->{title}); | ||
| 186 | $check //= $n | ||
| 187 | if $state{presets}{notations}->{$_}->{default}; | ||
| 188 | $n++; | ||
| 189 | } | ||
| 190 | |||
| 191 | $ctl->SetSelection($check); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | method fetch_prefs() { | ||
| 196 | |||
| 197 | # Transfer preferences to the dialog. | ||
| 198 | |||
| 199 | # Skip default (system, user, song) configs. | ||
| 200 | $self->{cb_usestdcfg}->SetValue(!$preferences{skipstdcfg}); | ||
| 201 | |||
| 202 | if ( is_arrayref($preferences{preset_instruments}) | ||
| 203 | && @{$preferences{preset_instruments}} ) { | ||
| 204 | for ( $self->{ch_instrument} ) { | ||
| 205 | my $n = $_->FindString($preferences{preset_instruments}[0]); | ||
| 206 | $_->SetSelection($n) | ||
| 207 | unless $n == wxNOT_FOUND; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | # Custom config file. | ||
| 212 | $self->{cb_configfile}->SetValue($preferences{enable_configfile}); | ||
| 213 | $self->{fp_customconfig}->SetPath($preferences{configfile}) | ||
| 214 | if $preferences{configfile}; | ||
| 215 | |||
| 216 | # Custom library. | ||
| 217 | $self->{cb_customlib}->SetValue($preferences{enable_customlib}); | ||
| 218 | $self->{dp_customlibrary}->SetPath($preferences{customlib}) | ||
| 219 | if $preferences{customlib}; | ||
| 220 | |||
| 221 | # New song template. | ||
| 222 | $self->{cb_tmplfile}->SetValue($preferences{enable_tmplfile}); | ||
| 223 | $self->{fp_tmplfile}->SetPath($preferences{tmplfile}) | ||
| 224 | if $preferences{tmplfile}; | ||
| 225 | |||
| 226 | # Preferred filename extension. | ||
| 227 | $self->{t_prefext}->SetValue( $preferences{chordproext} ); | ||
| 228 | |||
| 229 | # Editor. | ||
| 230 | $self->{fp_editor}->SetSelectedFont( Wx::Font->new($preferences{editfont}) ); | ||
| 231 | $self->prefs2colours; | ||
| 232 | $self->{cb_editorwrap}->SetValue($preferences{editorwrap}); | ||
| 233 | $self->{sp_editorwrap}->SetValue($preferences{editorwrapindent}); | ||
| 234 | |||
| 235 | # Messages. | ||
| 236 | $self->{fp_messages}->SetSelectedFont( Wx::Font->new($preferences{msgsfont}) ); | ||
| 237 | |||
| 238 | # Transcode. | ||
| 239 | $self->{cb_xcode}->SetValue( $preferences{enable_xcode} ); | ||
| 240 | $self->OnCbTranscode(undef); | ||
| 241 | |||
| 242 | # PDF Viewer. | ||
| 243 | $self->{cb_pdfviewer}->SetValue($preferences{enable_pdfviewer}); | ||
| 244 | $self->{t_pdfviewer}->SetValue($preferences{pdfviewer}) | ||
| 245 | if $preferences{pdfviewer}; | ||
| 246 | $self->{t_pdfviewer}->Enable($self->{cb_pdfviewer}->IsChecked); | ||
| 247 | |||
| 248 | # HTML Viewer. | ||
| 249 | $self->{cb_htmlviewer}->SetValue($preferences{enable_htmlviewer}); | ||
| 250 | |||
| 251 | $self->enablecustom; | ||
| 252 | $state{_prefs} = clone(\%preferences); | ||
| 253 | |||
| 254 | # use DDP; p %preferences, as => "Fetched"; | ||
| 255 | } | ||
| 256 | |||
| 257 | # C D E F G A B C | ||
| 258 | my @xpmap = qw( 0 1 1 2 3 3 4 5 6 6 7 8 8 9 10 10 11 12 ); | ||
| 259 | my @sfmap = qw( 0 7 -5 2 9 -3 4 -1 6 -6 1 8 -4 3 10 -2 5 0 ); | ||
| 260 | |||
| 261 | method store_prefs() { | ||
| 262 | |||
| 263 | # Transfer all preferences to the state. | ||
| 264 | |||
| 265 | my $parent = $self->GetParent; | ||
| 266 | |||
| 267 | # Skip default (system, user, song) configs. | ||
| 268 | $preferences{skipstdcfg} = !$self->{cb_usestdcfg}->IsChecked; | ||
| 269 | |||
| 270 | # Preset instrument. | ||
| 271 | my $n = $self->{ch_instrument}->GetSelection; | ||
| 272 | $preferences{preset_instruments} = | ||
| 273 | [ $self->{ch_instrument}->GetClientData($n) ]; | ||
| 274 | |||
| 275 | # Preset style. | ||
| 276 | $n = $self->{ch_style}->GetSelection; | ||
| 277 | $preferences{preset_styles} = | ||
| 278 | [ $self->{ch_style}->GetClientData($n) ]; | ||
| 279 | |||
| 280 | # Preset stylemods. | ||
| 281 | my $ctl = $self->{ch_stylemods}; | ||
| 282 | my $cnt = $ctl->GetCount; | ||
| 283 | $preferences{preset_stylemods} = []; | ||
| 284 | for ( my $n = 0; $n < $cnt; $n++ ) { | ||
| 285 | next unless $ctl->IsChecked($n); | ||
| 286 | push( @{$preferences{preset_stylemods}}, $ctl->GetClientData($n) ); | ||
| 287 | } | ||
| 288 | |||
| 289 | # Custom config file. | ||
| 290 | $preferences{enable_configfile} = $self->{cb_configfile}->IsChecked; | ||
| 291 | $preferences{configfile} = $self->{fp_customconfig}->GetPath; | ||
| 292 | $preferences{enable_configfile} = 0 if $preferences{configfile} eq ""; | ||
| 293 | |||
| 294 | # Custom library. | ||
| 295 | $preferences{enable_customlib} = $self->{cb_customlib}->IsChecked; | ||
| 296 | $preferences{customlib} = $self->{dp_customlibrary}->GetPath; | ||
| 297 | $preferences{enable_customlib} = 0 if $preferences{customlib} eq ""; | ||
| 298 | |||
| 299 | # New song template. | ||
| 300 | $preferences{enable_tmplfile} = $self->{cb_tmplfile}->IsChecked; | ||
| 301 | $preferences{tmplfile} = $self->{fp_tmplfile}->GetPath; | ||
| 302 | $preferences{enable_tmplfile} = 0 if $preferences{tmplfile} eq ""; | ||
| 303 | |||
| 304 | # Preferred filename extension. | ||
| 305 | $preferences{chordproext} = $self->{t_prefext}->GetValue; | ||
| 306 | |||
| 307 | # Editor. | ||
| 308 | $preferences{editfont} = $self->{fp_editor}->GetSelectedFont->GetNativeFontInfoDesc; | ||
| 309 | $self->colours2prefs; | ||
| 310 | $preferences{editorwrap} = $self->{cb_editorwrap}->IsChecked; | ||
| 311 | $preferences{editorwrapindent} = $self->{sp_editorwrap}->GetValue; | ||
| 312 | |||
| 313 | # Messages. | ||
| 314 | $preferences{msgsfont} = $self->{fp_messages}->GetSelectedFont->GetNativeFontInfoDesc; | ||
| 315 | |||
| 316 | # Notation. | ||
| 317 | $n = $self->{ch_notation}->GetSelection; | ||
| 318 | if ( $n != wxNOT_FOUND ) { | ||
| 319 | $preferences{preset_notations} = | ||
| 320 | [ $self->{ch_notation}->GetClientData($n) ]; | ||
| 321 | $preferences{preset_notations} = [] | ||
| 322 | if $preferences{preset_notations}->[0]->{default}; | ||
| 323 | } | ||
| 324 | else { | ||
| 325 | $preferences{preset_notations} = []; | ||
| 326 | } | ||
| 327 | |||
| 328 | # Transcode. | ||
| 329 | $preferences{enable_xcode} = $self->{cb_xcode}->IsChecked; | ||
| 330 | $n = $self->{ch_xcode}->GetSelection; | ||
| 331 | if ( $n >= 0 ) { | ||
| 332 | $preferences{preset_xcodes} = | ||
| 333 | [ $self->{ch_xcode}->GetClientData($n) ]; | ||
| 334 | } | ||
| 335 | else { | ||
| 336 | $preferences{preset_xcodes} = [ $state{default_notation} ]; | ||
| 337 | } | ||
| 338 | |||
| 339 | # PDF Viewer. | ||
| 340 | $preferences{enable_pdfviewer} = $self->{cb_pdfviewer}->IsChecked; | ||
| 341 | $preferences{pdfviewer} = $self->{t_pdfviewer}->GetValue; | ||
| 342 | |||
| 343 | # HTML Viewer. | ||
| 344 | $preferences{enable_htmlviewer} = $self->{cb_htmlviewer}->IsChecked; | ||
| 345 | |||
| 346 | # use DDP; p %preferences, as => "Stored"; | ||
| 347 | } | ||
| 348 | |||
| 349 | method restore_prefs() { | ||
| 350 | %preferences = %{ $state{_prefs} }; | ||
| 351 | # use DDP; p %preferences, as => "Restored"; | ||
| 352 | } | ||
| 353 | |||
| 354 | method reload() { | ||
| 355 | # Temporary store dialog values into preferences. | ||
| 356 | local $preferences{skipstdcfg} = !$self->{cb_usestdcfg}->IsChecked; | ||
| 357 | local $preferences{customlib} = $self->{dp_customlibrary}->GetPath; | ||
| 358 | local $preferences{enable_customlib} = $self->{cb_customlib}->IsChecked; | ||
| 359 | |||
| 360 | # Rebuild the lists of config styles. | ||
| 361 | ChordPro::Wx::Config::setup_styles(1); | ||
| 362 | |||
| 363 | # Update the dialog. | ||
| 364 | $self->enablecustom; | ||
| 365 | } | ||
| 366 | |||
| 367 | method get_selected_theme() { | ||
| 368 | (qw(light dark auto))[$self->{ch_theme}->GetSelection]; | ||
| 369 | } | ||
| 370 | |||
| 371 | method set_selected_theme($theme) { | ||
| 372 | $self->{ch_theme}->SetSelection | ||
| 373 | ( $theme eq "light" ? 0 : $theme eq "dark" ? 1 : 2 ); | ||
| 374 | } | ||
| 375 | |||
| 376 | method colours2prefs { | ||
| 377 | my $theme = $state{editortheme}; | ||
| 378 | $self->GetParent->init_theme; | ||
| 379 | die("INTERNAL ERROR: invalid theme1\n") | ||
| 380 | unless $theme eq "light" || $theme eq "dark"; | ||
| 381 | $preferences{editcolour}{$theme}{fg} = $self->{cp_fg}->GetAsHTML; | ||
| 382 | $preferences{editcolour}{$theme}{bg} = $self->{cp_bg}->GetAsHTML; | ||
| 383 | if ( $state{have_stc} ) { | ||
| 384 | $preferences{editcolour}{$theme}{"s$_"} = $self->{"cp_s$_"}->GetAsHTML for 1..6; | ||
| 385 | $preferences{editcolour}{$theme}{annfg} = $self->{cp_annfg}->GetAsHTML; | ||
| 386 | $preferences{editcolour}{$theme}{annbg} = $self->{cp_annbg}->GetAsHTML; | ||
| 387 | } | ||
| 388 | $self->{t_editor}->refresh; | ||
| 389 | } | ||
| 390 | |||
| 391 | method prefs2colours() { | ||
| 392 | |||
| 393 | $self->set_selected_theme( $preferences{editortheme} ); | ||
| 394 | if ( $preferences{editortheme} eq "auto" ) { | ||
| 395 | $self->GetParent->init_theme; | ||
| 396 | $self->{l_theme}->SetLabel( ucfirst $state{editortheme} ); | ||
| 397 | } | ||
| 398 | else { | ||
| 399 | $state{editortheme} = $preferences{editortheme}; | ||
| 400 | $self->{l_theme}->SetLabel(""); | ||
| 401 | } | ||
| 402 | |||
| 403 | my $theme = $state{editortheme}; | ||
| 404 | die("INTERNAL ERROR: invalid theme2\n") | ||
| 405 | unless $theme eq "light" || $theme eq "dark"; | ||
| 406 | |||
| 407 | $self->{cp_fg}->SetColour($preferences{editcolour}{$theme}{fg}); | ||
| 408 | $self->{cp_bg}->SetColour($preferences{editcolour}{$theme}{bg}); | ||
| 409 | if ( $state{have_stc} ) { | ||
| 410 | for my $c ( "annfg", "annbg", map { "s$_" } 1..6 ) { | ||
| 411 | $self->{"cp_$c"}->Enable(1); | ||
| 412 | $self->{"l_$c"}->Enable(1); | ||
| 413 | } | ||
| 414 | $self->{"cp_s$_"}->SetColour($preferences{editcolour}{$theme}{"s$_"}) | ||
| 415 | for 1..6; | ||
| 416 | $self->{cp_annfg}->SetColour($preferences{editcolour}{$theme}{annfg}); | ||
| 417 | $self->{cp_annbg}->SetColour($preferences{editcolour}{$theme}{annbg}); | ||
| 418 | } | ||
| 419 | else { | ||
| 420 | my $grey = "#e0e0e0"; | ||
| 421 | for my $c ( "annfg", "annbg", map { "s$_" } 1..6 ) { | ||
| 422 | $self->{"cp_$c"}->SetColour($grey); | ||
| 423 | $self->{"cp_$c"}->Enable(0); | ||
| 424 | $self->{"l_$c"}->Enable(0); | ||
| 425 | } | ||
| 426 | } | ||
| 427 | $self->{t_editor}->refresh; | ||
| 428 | } | ||
| 429 | |||
| 430 | ################ Event handlers ################ | ||
| 431 | |||
| 432 | #### General. | ||
| 433 | |||
| 434 | method OnAccept($event) { | ||
| 435 | $self->store_prefs; | ||
| 436 | $event->Skip; | ||
| 437 | } | ||
| 438 | |||
| 439 | method OnCancel($event) { | ||
| 440 | $self->restore_prefs; | ||
| 441 | $event->Skip; | ||
| 442 | } | ||
| 443 | |||
| 444 | # Only required for custom button. | ||
| 445 | # method OnIBDismiss($e) { | ||
| 446 | # $self->{w_infobar}->Dismiss; | ||
| 447 | # } | ||
| 448 | |||
| 449 | #### Configs etc. | ||
| 450 | |||
| 451 | method OnConfigFile($event) { | ||
| 452 | my $n = $self->{cb_configfile}->IsChecked; | ||
| 453 | $self->{fp_customconfig}->Enable($n); | ||
| 454 | $self->{b_createconfig}->Enable($n); | ||
| 455 | } | ||
| 456 | |||
| 457 | method OnCreateConfig($event) { | ||
| 458 | $self->_OnCreateConfig( $event ); | ||
| 459 | } | ||
| 460 | |||
| 461 | method _OnCreateConfig( $event, $fn = undef ) { | ||
| 462 | unless ( defined $fn ) { | ||
| 463 | my $fd = Wx::FileDialog->new( $self, | ||
| 464 | "Select a new configuration file", | ||
| 465 | "", "customconfig", | ||
| 466 | "*.json", | ||
| 467 | wxFD_SAVE|wxFD_OVERWRITE_PROMPT | ||
| 468 | ); | ||
| 469 | my $ret = $fd->ShowModal; | ||
| 470 | return unless $ret == wxID_OK; | ||
| 471 | $fn = $fd->GetPath; | ||
| 472 | $fd->Destroy; | ||
| 473 | } | ||
| 474 | use File::Copy; | ||
| 475 | my $cfg = fn_catfile( CP->findresdirs("config")->[-1], | ||
| 476 | $state{expert} | ||
| 477 | ? "chordpro.json" | ||
| 478 | : "config.tmpl" ); | ||
| 479 | if ( fs_copy( $cfg, $fn ) ) { | ||
| 480 | $self->{fp_customconfig}->SetPath( fn_rel2abs($fn) ); | ||
| 481 | } | ||
| 482 | else { | ||
| 483 | my $md = Wx::MessageDialog->new | ||
| 484 | ( $self, | ||
| 485 | "Error creating $fn: $!", | ||
| 486 | "File open error", | ||
| 487 | wxOK | wxICON_ERROR ); | ||
| 488 | $md->ShowModal; | ||
| 489 | $md->Destroy; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | |||
| 493 | method OnCustomConfigChanged($event) { | ||
| 494 | my $path = $self->{fp_customconfig}->GetPath; | ||
| 495 | |||
| 496 | unless ( $path =~ /\.\w+$/ ) { | ||
| 497 | $self->{fp_customconfig}->SetPath( $path .= ".json" ); | ||
| 498 | } | ||
| 499 | return if fs_test( s => $path ); # existing config | ||
| 500 | |||
| 501 | my $md = Wx::MessageDialog->new | ||
| 502 | ( $self, | ||
| 503 | "The configuration file ".basename($path)." does not exist.". | ||
| 504 | "Create it?", | ||
| 505 | "Missing Configuration", | ||
| 506 | wxYES | wxICON_QUESTION ); | ||
| 507 | my $ret = $md->ShowModal; | ||
| 508 | $md->Destroy; | ||
| 509 | return unless $ret == wxID_YES; | ||
| 510 | $self->_OnCreateConfig( $event, $path ); | ||
| 511 | } | ||
| 512 | |||
| 513 | method OnCustomLib($event) { | ||
| 514 | my $n = $self->{cb_customlib}->IsChecked; | ||
| 515 | $self->{dp_customlibrary}->Enable($n); | ||
| 516 | $self->reload; | ||
| 517 | } | ||
| 518 | |||
| 519 | method OnCustomLibChanged($event) { | ||
| 520 | $self->reload; | ||
| 521 | } | ||
| 522 | |||
| 523 | method OnUseStdCfg($event) { | ||
| 524 | $event->Skip; | ||
| 525 | $self->reload; | ||
| 526 | } | ||
| 527 | |||
| 528 | method OnPresets($event) { | ||
| 529 | # $self->{ch_presets}->Enable( $self->{cb_presets}->GetValue ); | ||
| 530 | $event->Skip; | ||
| 531 | } | ||
| 532 | |||
| 533 | method OnPrefExtChanged($event) { | ||
| 534 | $preferences{chordproext} = $self->{t_prefext}->GetValue; | ||
| 535 | $preferences{chordproext} =~ s;^\.*(\w+)?$;sprintf(".%s",$1//substr($state{_prefs}{chordproext},1));e | ||
| 536 | && $self->{t_prefext}->ChangeValue($preferences{chordproext}); | ||
| 537 | ChordPro::Wx::Config::setup_filters(); | ||
| 538 | $event->Skip; | ||
| 539 | } | ||
| 540 | |||
| 541 | #### Notations and Transcode. | ||
| 542 | |||
| 543 | method OnChNotation($event) { | ||
| 544 | my $n = $self->{ch_notation}->GetSelection; | ||
| 545 | $event->Skip; | ||
| 546 | } | ||
| 547 | |||
| 548 | method OnChTranscode($event) { | ||
| 549 | my $n = $self->{ch_xcode}->GetSelection; | ||
| 550 | $event->Skip; | ||
| 551 | } | ||
| 552 | |||
| 553 | method OnCbTranscode($event) { | ||
| 554 | $self->{ch_xcode}->Enable( $self->{cb_xcode}->IsChecked ); | ||
| 555 | } | ||
| 556 | |||
| 557 | #### Editor. | ||
| 558 | |||
| 559 | method OnEditorFontPickerChanged($event) { | ||
| 560 | my $ctl = $self->{t_editor}; | ||
| 561 | return unless $ctl; | ||
| 562 | my $font = $self->{fp_editor}->GetSelectedFont; | ||
| 563 | $preferences{editfont} = $font->GetNativeFontInfoDesc; | ||
| 564 | $ctl->refresh; | ||
| 565 | } | ||
| 566 | |||
| 567 | method OnColourFGChanged( $event ) { | ||
| 568 | $self->colourchanged("fg"); | ||
| 569 | } | ||
| 570 | |||
| 571 | method OnColourBGChanged( $event ) { | ||
| 572 | $self->colourchanged("bg"); | ||
| 573 | } | ||
| 574 | |||
| 575 | method OnColourS1Changed( $event ) { | ||
| 576 | $self->colourchanged("s1"); | ||
| 577 | } | ||
| 578 | |||
| 579 | method OnColourS2Changed( $event ) { | ||
| 580 | $self->colourchanged("s2"); | ||
| 581 | } | ||
| 582 | |||
| 583 | method OnColourS3Changed( $event ) { | ||
| 584 | $self->colourchanged("s3"); | ||
| 585 | } | ||
| 586 | |||
| 587 | method OnColourS4Changed( $event ) { | ||
| 588 | $self->colourchanged("s4"); | ||
| 589 | } | ||
| 590 | |||
| 591 | method OnColourS5Changed( $event ) { | ||
| 592 | $self->colourchanged("s5"); | ||
| 593 | } | ||
| 594 | |||
| 595 | method OnColourS6Changed( $event ) { | ||
| 596 | $self->colourchanged("s6"); | ||
| 597 | } | ||
| 598 | |||
| 599 | method OnColourAnnFGChanged( $event ) { | ||
| 600 | $self->colourchanged("annfg"); | ||
| 601 | } | ||
| 602 | |||
| 603 | method OnColourAnnBGChanged( $event ) { | ||
| 604 | $self->colourchanged("annbg"); | ||
| 605 | } | ||
| 606 | |||
| 607 | method OnThemeChanged( $event ) { | ||
| 608 | $preferences{editortheme} = $self->get_selected_theme; | ||
| 609 | $self->prefs2colours; | ||
| 610 | } | ||
| 611 | |||
| 612 | method OnEditorWrap( $event ) { | ||
| 613 | $self->{$_}->Enable( $self->{cb_editorwrap}->IsChecked ) | ||
| 614 | for qw( l_editorwrap sp_editorwrap ); | ||
| 615 | $preferences{editorwrap} = $self->{cb_editorwrap}->IsChecked; | ||
| 616 | $preferences{editorwrapindent} = $self->{sp_editorwrap}->GetValue; | ||
| 617 | $self->{t_editor}->refresh; | ||
| 618 | } | ||
| 619 | |||
| 620 | method OnEditorWrapIndent( $event ) { | ||
| 621 | $preferences{editorwrapindent} = $self->{sp_editorwrap}->GetValue; | ||
| 622 | $self->{t_editor}->refresh; | ||
| 623 | } | ||
| 624 | |||
| 625 | method OnCbTmplFile($event) { | ||
| 626 | my $n = $self->{cb_tmplfile}->IsChecked; | ||
| 627 | $self->{fp_tmplfile}->Enable($n); | ||
| 628 | } | ||
| 629 | |||
| 630 | method OnTmplFileChanged($event) { | ||
| 631 | # my $file = $self->{fp_tmplfile}->GetPath; | ||
| 632 | # ellipsize( $self->{t_tmplfile}, text => $file ); | ||
| 633 | } | ||
| 634 | |||
| 635 | #### Messages. | ||
| 636 | |||
| 637 | method OnMessagesFontPickerChanged($event) { | ||
| 638 | my $parent = $self->GetParent; | ||
| 639 | my $ctl = $parent->{t_messages}; | ||
| 640 | return unless $ctl; | ||
| 641 | my $font = $self->{fp_messages}->GetSelectedFont; | ||
| 642 | $ctl->SetFont($font); | ||
| 643 | $preferences{msgsfont} = $font->GetString(wxC2S_HTML_SYNTAX); | ||
| 644 | } | ||
| 645 | |||
| 646 | # Previewer. | ||
| 647 | |||
| 648 | method OnPDFViewer($event) { | ||
| 649 | $self->{t_pdfviewer}->Enable( $self->{cb_pdfviewer}->GetValue ); | ||
| 650 | } | ||
| 651 | method OnHTMLViewer($event) { | ||
| 652 | } | ||
| 653 | |||
| 654 | # System | ||
| 655 | |||
| 656 | method OnSysColourChanged($event) { | ||
| 657 | $self->GetParent->init_theme; | ||
| 658 | $self->OnThemeChanged($event); | ||
| 659 | $event->Skip; | ||
| 660 | } | ||
| 661 | |||
| 662 | method OnChangeInstrument( $event ) { | ||
| 663 | my $c = $event->GetClientData; | ||
| 664 | $self->set_instrument_desc($c->{desc}); | ||
| 665 | } | ||
| 666 | |||
| 667 | method OnChangeStyle( $event ) { | ||
| 668 | my $c = $event->GetClientData; | ||
| 669 | $self->set_style_desc($c->{desc}); | ||
| 670 | # $self->set_style_preview($c->{preview}); | ||
| 671 | } | ||
| 672 | |||
| 673 | method OnChangeStylemods( $event ) { | ||
| 674 | my $n = $event->GetInt; | ||
| 675 | my $ctl = $self->{ch_stylemods}; | ||
| 676 | my $data = $ctl->GetClientData($n); | ||
| 677 | my $desc = ""; | ||
| 678 | my $xid = $ctl->IsChecked($n) ? $data->{exclude_id} : ""; | ||
| 679 | |||
| 680 | # Collect descriptions. | ||
| 681 | # If checking a choice has an exclude_id, uncheck checked choices | ||
| 682 | # that use the same exclude_id. | ||
| 683 | for ( my $i = 0; $i < $ctl->GetCount; $i++ ) { | ||
| 684 | next unless $ctl->IsChecked($i); | ||
| 685 | $data = $ctl->GetClientData($i); | ||
| 686 | if ( $i != $n and $xid and ($data->{exclude_id}//"") eq $xid ) { | ||
| 687 | $ctl->Check( $i, 0 ); | ||
| 688 | next; | ||
| 689 | } | ||
| 690 | |||
| 691 | if ( $data->{desc} ) { | ||
| 692 | $desc .= $checkpfx; | ||
| 693 | $desc .= ucfirst($data->{src}) . ": " | ||
| 694 | unless $data->{src} eq "std"; | ||
| 695 | $desc .= $data->{desc} . "\n"; | ||
| 696 | } | ||
| 697 | } | ||
| 698 | $self->set_stylemods_desc($desc); | ||
| 699 | } | ||
| 700 | |||
| 701 | ################ Helpers ################ | ||
| 702 | |||
| 703 | method set_style_desc( $desc ) { | ||
| 704 | $self->{l_style_desc}->SetLabel($desc); | ||
| 705 | # $self->{l_style_desc}->Wrap(($self->{ch_style}->GetSizeWH)[0]); | ||
| 706 | } | ||
| 707 | |||
| 708 | =for later | ||
| 709 | |||
| 710 | method set_style_preview( $preview ) { | ||
| 711 | $preview //= "style_nopreview-small.png"; | ||
| 712 | warn("XX1 $preview\n"); | ||
| 713 | $preview = CP->findres( $preview, class => "images" ) | ||
| 714 | || CP->findres( "style_nopreview-small.png", class => "images" ); | ||
| 715 | warn("XX2 $preview\n"); | ||
| 716 | return unless $preview; | ||
| 717 | $self->{bm_style_preview}->SetBitmap | ||
| 718 | ( Wx::Bitmap->new( $preview, wxBITMAP_TYPE_ANY ) ); | ||
| 719 | } | ||
| 720 | |||
| 721 | =cut | ||
| 722 | |||
| 723 | method set_stylemods_desc( $desc ) { | ||
| 724 | $self->{l_stylemods_desc}->SetLabel($desc); | ||
| 725 | # $self->{l_stylemods_desc}->Wrap(($self->{ch_stylemods}->GetSizeWH)[0]); | ||
| 726 | } | ||
| 727 | |||
| 728 | method set_instrument_desc( $desc ) { | ||
| 729 | $self->{l_instrument_desc}->SetLabel($desc); | ||
| 730 | # $self->{l_instrument_desc}->Wrap(($self->{ch_instrument}->GetSizeWH)[0]); | ||
| 731 | } | ||
| 732 | |||
| 733 | method colourchanged($index) { | ||
| 734 | $self->colours2prefs; | ||
| 735 | } | ||
| 736 | |||
| 737 | method setnomod( $ctl, $code ) { | ||
| 738 | Carp::confess("WHOAH!") unless $ctl; | ||
| 739 | my $mod = $ctl->IsModified; | ||
| 740 | $code->($self, $ctl); | ||
| 741 | $ctl->SetModified($mod); | ||
| 742 | } | ||
| 743 | |||
| 744 | 1; |