Debian Codegraph @

artemis_parts/misc.pl

raw
1 # -*- cperl -*-
2 ## This file is part of Artemis, copyright (c) 2002-2008 Bruce Ravel
3 ##
4 ## MISCELLANEOUS FUNCTIONALITY FOR ARTEMIS
5
6
7 ## turn off the mouse-3 pop-up menu which is normal for a text widget
8 sub disable_mouse3 {
9 my $text = $_[0];
10 my @swap_bindtags = $text->bindtags;
11 $text -> bindtags([@swap_bindtags[1,0,2,3]]);
12 $text -> bind('<Button-3>' => sub{$_[0]->break});
13 };
14
15
16 sub get_index {
17 if ($_[0] =~ /data(\d+)(_(fit|bkg))?/) {
18 return $1;
19 } elsif ($_[0] =~ /feff(\d+)(\.\d+)?/) {
20 return $1;
21 };
22 return undef;
23 };
24
25 ## tell ifeffit to erase all groups that match $_[0] (e.g. all feff0...)
26 sub erase_many_groups {
27 my $match = $_[0];
28 $paths{gsd}->dispose("show \@groups", 1);
29 my ($lines, $response) = (Ifeffit::get_scalar('&echo_lines'), "");
30 my @list = map {Ifeffit::get_echo()} (1 .. $lines);
31 ## print $/, join(" ", @list), $/;
32 my $string = "";
33 foreach (@list) {
34 next unless (/^$match/);
35 $string .= "erase \@group $_\n";
36 };
37 $paths{gsd}->dispose($string, $dmode);
38 return 0;
39 };
40
41 sub set_status {
42 return if (Ifeffit::Tools->vstr < 1.02007);
43 my $val = $_[0] || 0;
44 $paths{gsd} -> dispose("set \&status = $val", $dmode);
45 };
46
47
48 ## splice words back together to make a multi-word string that is
49 ## almost, but not quite the same as the original line. take care to
50 ## drop end of line comments and to replace commas in two argument
51 ## math functions.
52 sub concat {
53 my @list = @_;
54 my $string = "";
55 while (@list) {
56 my $this = shift(@list);
57 if ($this =~ /^[%!*\#]/) {
58 @list = ();
59 } else {
60 $string .= " " . $this;
61 };
62 };
63 $string =~ s/^ //;
64 $string =~ s/(debye|eins|max|min) ?\((\w+) /$1\($2,/;
65 return $string;
66 };
67
68
69
70 ## multiplexer for renaming list entries, bound to Ctrl-n, on the GDS
71 ## page this is the same as clicking the New button
72 sub rename_this {
73 my $type = $paths{$current}->type;
74 &gds2_new, return if ($type eq 'gsd');
75 &rename_fit, return if (($type eq 'fit') and $paths{$current}->get('parent'));
76 Error("You cannot rename the head \"Fit\" entry"), return if ($type eq 'fit');
77 Error("You cannot rename a \"Background\" entry"), return if ($type eq 'bkg');
78 Error("You cannot rename a \"Residual\" entry"), return if ($type eq 'res');
79 Error("You cannot rename a \"Difference\" entry"), return if ($type eq 'diff');
80 &rename_feff(0), return if ($type eq 'feff');
81 &rename_path, return if ($type eq 'path');
82 &rename_data, return if ($type eq 'data');
83 };
84
85 ## multiplexer for functionality bound to Ctrl-d
86 sub keyboard_d {
87 gds2_define($widgets{gds2list}, \%gds_selected) if ($current_canvas eq 'gsd');
88 };
89 ## multiplexer for functionality bound to Ctrl-e
90 sub keyboard_e {
91 if ($current_canvas eq 'gsd') {
92 if ($gds_selected{showing} eq 'show') {
93 $widgets{gds2_show}->packForget;
94 $widgets{gds2_editarea}->pack(-side=>=>'top', -fill=>'x', -padx=>4, -pady=>2);
95 $gds_selected{showing}="edit";
96 };
97 };
98 };
99 ## multiplexer for functionality bound to Ctrl-g
100 sub keyboard_g {
101 grab_gds2($widgets{gds2list}, \%gds_selected) if ($current_canvas eq 'gsd');
102 };
103
104 ## multiplexer for functionality bound to Alt-k
105 sub keyboard_alt_k {
106 if ($current_canvas eq 'gsd') {
107 gds2_up();
108 } elsif (($current_canvas eq 'feff') and ($fefftabs->raised() eq 'Atoms')) {
109 atoms_move('up');
110 };
111 };
112
113 ## multiplexer for functionality bound to Alt-j
114 sub keyboard_alt_j {
115 if ($current_canvas eq 'gsd') {
116 gds2_down();
117 } elsif (($current_canvas eq 'feff') and ($fefftabs->raised() eq 'Atoms')) {
118 atoms_move('down');
119 };
120 };
121
122
123 ## read a string from an entry box that temporarily replaces the echo
124 ## area. $label is the descriptive label to be written before the
125 ## entry box. $r_string is a ref to the string being prompted for.
126 ## $r_arrow_buffer is a ref to a array containing a buffer of
127 ## responses accessed via the up and down arrows
128 sub get_string {
129 my ($mode, $label, $r_string, $r_arrow_buffer) = @_;
130 $top -> packPropagate(0);
131 $echo -> packForget;
132 my $prior = $top -> focusCurrent;
133 my $ren = $ebar -> Frame()
134 -> pack(-side=>'left', -expand=>1, -fill=>'x', -ipadx=>3);
135 $top -> update();
136 $ren -> grab();
137 $ren -> Label(-text=>$label,
138 -foreground=>$config{colors}{activehighlightcolor})
139 -> pack(-side=>'left');
140 my $entry = $ren -> Entry(-justify=>'center', -background=>$config{colors}{current},
141 -textvariable=>$r_string)
142 -> pack(-side=>'left', -expand=>1, -fill=>'x', -padx=>10);
143 if ($r_arrow_buffer and @$r_arrow_buffer) {
144 my $pointer = $#{$r_arrow_buffer} + 1;
145 $entry->bind("<KeyPress-Up>", # previous command in history
146 sub{ --$pointer; ($pointer<0) and ($pointer=0);
147 $entry->delete(0,'end');
148 $entry->insert(0, $$r_arrow_buffer[$pointer]); });
149 $entry->bind("<KeyPress-Down>", # next command in history
150 sub{ ++$pointer; ($pointer>$#{$r_arrow_buffer}) and
151 ($pointer= $#{$r_arrow_buffer});
152 $entry->delete(0,'end');
153 $entry->insert(0, $$r_arrow_buffer[$pointer]); });
154 };
155 my $pad = 0;
156 $entry -> bind("<KeyPress-Return>", sub{&restore_echo($ren, $mode, $entry, $prior)});
157 $ren -> Button(-text=>'OK', @button2_list,
158 -font=>$config{fonts}{small},
159 -borderwidth=>1,
160 -width=>10,
161 -command=>[\&restore_echo, $ren, $mode, $entry, $prior])
162 -> pack(-side=>'left');
163 foreach ($ren, $entry) {
164 my $this = $_;
165 $this -> bindtags([($this->bindtags)[1,0,2,3]]);
166 map {$this -> bind("<Control-$_>" => sub{$this->break;})}
167 qw(a h l n o p r s t u v
168 slash period semicolon minus equal
169 Key-1 Key-2 Key-3 Key-4 Key-5 Key-6);
170 };
171
172 $entry -> selectionRange(qw(0 end));
173 $entry -> icursor('end');
174 $top -> update;
175 $entry -> focus;
176 return $ren;
177 };
178 ## destroy the get_string dialog and return the echo area
179 sub restore_echo {
180 my ($ren, $mode, $entry, $prior) = @_;
181 $ren -> grabRelease;
182 $ren -> packForget;
183 $ren -> destroy;
184 $echo -> pack(-side=>'left', -expand=>1, -fill=>'x', -pady=>2);
185 $prior -> focus;
186 };
187
188
189
190 ## return an nleg value
191 sub get_nlegs {
192 my $d = $top->Dialog(-title => "Artemis: select paths",
193 -text => "Maximum number of legs in fit:",
194 -buttons => ["2", "3", "4", "Cancel"],
195 -font => $config{fonts}{med},
196 -popover => 'cursor');
197 my $str = sprintf("+%d+%d", 0.4*$top->screenwidth(), 0.4*$top->screenheight());
198 $d -> geometry($str);
199 &posted_Dialog;
200 my $val = $d -> Show();
201 return $d -> Show();
202 };
203
204 ## return a value for R_eff
205 sub get_r {
206 my $crit;
207 my $label = "Maximum Reff of included paths: ";
208 my $dialog = get_string($dmode, $label, \$crit);
209 $dialog -> waitWindow; # the get_string dialog will be
210 # destroyed once the user hits ok,
211 # then we can move on...
212 Error("Maximum Reff must be a positive number."), return 'Cancel'
213 unless ($crit =~/^\s*(\d+\.?\d*|\.\d+)\s*$/);
214 return $crit;
215 };
216
217 ## return a value for ZCWIF
218 sub get_zcwif {
219 my $crit;
220 my $label = "Manimum amplitude for included paths: ";
221 my $dialog = get_string($dmode, $label, \$crit);
222 $dialog -> waitWindow; # the get_string dialog will be
223 # destroyed once the user hits ok,
224 # then we can move on...
225 Error("An amplitude factor must be a positive number."), return 'Cancel'
226 unless ($crit =~/^\s*(\d+\.?\d*|\.\d+)\s*$/);
227 return $crit;
228 };
229
230
231 sub set_fit_button {
232 SWITCH: {
233 ($_[0] eq 'fit') and do {
234 $fit_button -> configure(-state=>'normal', -text=>'Fit',
235 -command=>[\&generate_script, 1]);
236 last SWITCH;
237 };
238 ($_[0] eq 'apa') and do {
239 $fit_button -> configure(-state=>'normal', -text=>'Start the Project Assistant',
240 -command=>sub{Echo("Uh oh! APA is not built in anymore")}); #\&apa);
241 last SWITCH;
242 };
243 ($_[0] eq 'disable') and do {
244 $fit_button -> configure(-state=>'disabled', -text=>'Fit',);
245 last SWITCH;
246 };
247 };
248 };
249
250
251 sub layout {
252 LAYOUT: {
253 ($config{general}{layout} eq 'mlp') and do {
254 $fat -> pack(-side=>'left', -fill=>'y');
255 $skinny -> pack(-side=>'left', -expand=>1, -fill=>'both');
256 $skinny2 -> pack(-side=>'left', -fill=>'y');
257 last LAYOUT;
258 };
259 ($config{general}{layout} eq 'mpl') and do {
260 $fat -> pack(-side=>'left', -fill=>'y');
261 $skinny2 -> pack(-side=>'left', -fill=>'y');
262 $skinny -> pack(-side=>'left', -expand=>1, -fill=>'both');
263 last LAYOUT;
264 };
265 ($config{general}{layout} eq 'lmp') and do {
266 $skinny -> pack(-side=>'left', -expand=>1, -fill=>'both');
267 $fat -> pack(-side=>'left', -fill=>'y');
268 $skinny2 -> pack(-side=>'left', -fill=>'y');
269 last LAYOUT;
270 };
271 ($config{general}{layout} eq 'lpm') and do {
272 $skinny -> pack(-side=>'left', -expand=>1, -fill=>'both');
273 $skinny2 -> pack(-side=>'left', -fill=>'y');
274 $fat -> pack(-side=>'left', -fill=>'y');
275 last LAYOUT;
276 };
277 ($config{general}{layout} eq 'pml') and do {
278 $skinny2 -> pack(-side=>'left', -fill=>'y');
279 $fat -> pack(-side=>'left', -fill=>'y');
280 $skinny -> pack(-side=>'left', -expand=>1, -fill=>'both');
281 last LAYOUT;
282 };
283 ($config{general}{layout} eq 'plm') and do {
284 $skinny2 -> pack(-side=>'left', -fill=>'y');
285 $skinny -> pack(-side=>'left', -expand=>1, -fill=>'both');
286 $fat -> pack(-side=>'left', -fill=>'y');
287 last LAYOUT;
288 };
289 };
290 };
291
292 ## convert backslashes to foreward slashes, remove multiple slashes,
293 ## and remove the trailing slash from each string
294 sub normalize_directory {
295 my $a = $_[0];
296 $a =~ s/\\+/\//g; # multiple backslashes
297 $a =~ s/\/{2,}/\//g; # 2 or more foreslashes
298 $a =~ s/\/$//; # trailing slash
299 return $a;
300 };
301
302 sub same_directory {
303 my ($a, $b) = @_;
304 return 0 unless $a;
305 return 0 unless $b;
306 if ($is_windows) {
307 $a=normalize_directory($a);
308 $b=normalize_directory($b);
309 return ($a eq $b);
310 } else {
311 my @a = stat $a; # compare the inodes
312 my @b = stat $b;
313 return ($a[1] == $b[1]);
314 };
315 };
316
317
318 ## is $a a subdirectory of $b, sub_directory("foo/bar", "/foo")
319 ## returns true
320 sub sub_directory {
321 my ($a, $b) = @_;
322 return 0 unless $a;
323 return 0 unless $b;
324 ##print "$a $b\n";
325 $a=normalize_directory($a);
326 $b=normalize_directory($b);
327 ##print "$a $b\n";
328 return ($a =~ /^$b/);
329 };
330
331
332 ## move rc and mru files from their 0.6.001 and earlier locations to
333 ## the .horae directory
334 sub convert_config_files {
335 my $horae_dir = $setup -> find('artemis', 'horae');
336 (-d $horae_dir) or mkpath($horae_dir);
337 my $rcfile = $setup -> find('artemis', 'oldrc');
338 my $rctarget = $setup -> find('artemis', 'rc_personal');
339 my $mrufile = $setup -> find('artemis', 'oldmru');
340 my $mrutarget = $setup -> find('artemis', 'mru');
341 ##print join(" ", $horae_dir, $rcfile, $rmrufile), $/;
342 move($rcfile, $rctarget) if (-e $rcfile);
343 move($mrufile, $mrutarget) if (-e $mrufile);
344 };
345
346
347 sub pod_display {
348 my $file = $_[0];
349 my $p = $top->Pod(-file=>$file);
350 $p->zoom_in foreach (1 .. $config{general}{doc_zoom});
351 };
352
353
354 ## display $str in echo area, $app true means to append $str to what
355 ## is already there
356 sub Echo {
357 my ($string, $append) = @_;
358 my ($bg, $fn, $bt) = ($config{colors}{background},
359 $config{fonts}{small},
360 $config{colors}{button});
361 $ebar -> configure(-background => $bg);
362 $echo -> configure(-font => $fn,
363 -foreground => $bt,
364 -background => $bg);
365 return unless $string;
366 ($append) and ($string = $echo -> cget('-text') . $string);
367 $echo -> configure(-text=>(length($string) > 137) ? substr($string, 0, 137) : $string);
368 ## push @echo_history, $string;
369 ## ($#echo_history > 2000) and shift @echo_history;
370
371 ## next line corrects a hierarchy problem on windows
372 my $widg = (ref($notes{echo}) =~ m{Frame}) ? $notes{echo}->Subwidget('rotext') : $notes{echo};
373 $widg -> insert('end', $string."\n");
374 $widg -> yviewMoveto(1);
375 $top -> update;
376 };
377 sub Running {
378 Echo(@_);
379 ## bold white on green
380 my ($bg, $fn, $bt) = ($config{colors}{fitbutton}, $config{fonts}{smbold}, $config{colors}{warning_fg});
381 $ebar -> configure(-background => $bg);
382 $echo -> configure(-font => $fn,
383 -foreground => $bt,
384 -background => $bg);
385 $top -> update;
386 };
387 sub Attention {
388 Echo(@_);
389 ## bold white on red
390 my ($bg, $fn, $bt) = ($config{colors}{warning_bg}, $config{fonts}{smbold}, $config{colors}{warning_fg});
391 $ebar -> configure(-background => $bg);
392 $echo -> configure(-font => $fn,
393 -foreground => $bt,
394 -background => $bg);
395 $top -> update;
396 };
397 sub Echo_nosave {
398 my ($string, $append) = @_;
399 return unless $string;
400 ($append) and ($string = $echo -> cget('-text') . $string);
401 $echo -> configure(-text=>$string);
402 $top -> update;
403 };
404
405 sub Error {
406 $top -> bell;
407 Echo(@_);
408 };
409
410 sub posted_Dialog {
411 Attention("You must respond to the posted dialog. (These dialogs sometimes get hidden beneath other windows.)");
412 };
413
414 sub show_hint {
415 Echo("Hints file was not found"), return unless @hints;
416 $hint_n = int(rand $hint_x);
417 Echo("HINT: " . $hints[$hint_n]);
418 #++$hint_n;
419 #($hint_n > $#hints) and $hint_n = 0;
420 };
421
422 sub track {
423 no warnings;
424 my %hash = %{ $_[0] };
425 my @caller = caller(1);
426 my $called_from = $caller[3];
427 print "-" x 60, $/;
428 print "called from: $called_from\n";
429 foreach (keys %hash) {
430 if (ref($hash{$_}) =~ /CODE/) {
431 print &{ $hash{$_} };
432 } else {
433 print "$_ : $hash{$_}\n";
434 };
435 };
436 };
437
438 sub dump_paths {
439 Echo("Dumping paths to \`artemis.dump\'");
440 $Data::Dumper::Indent = 2;
441 ##read_gds2(0);
442 read_titles;
443 $paths{journal} = $notes{journal}->get(qw(1.0 end));
444 open DUMP, ">artemis.dump" or die $!;
445 print DUMP Data::Dumper->Dump([\%paths], [qw/paths/]);
446 print DUMP Data::Dumper->Dump([\%temp], [qw/temp/]);
447 close DUMP;
448 delete $paths{journal};
449 $Data::Dumper::Indent = 0;
450 Echo(@done);
451 };
452
453 sub swap_panels {
454 Error("Swapping panels is temporarily disabled.");
455 ## if (grep {$_ eq 'right'} ($skinny -> packInfo())) {
456 ## $skinny -> pack(-side=>'left');
457 ## $list -> configure(-scrollbars=>'w');
458 ## } else {
459 ## $skinny -> pack(-side=>'right');
460 ## $list -> configure(-scrollbars=>'e');
461 ## };
462 };
463
464 sub BindMouseWheel { # Mastering Perl/Tk ch. 15, p. 370
465 my ($w) = @_;
466 if ($^O eq 'MSWin32') {
467 $w->bind('<MouseWheel>' =>
468 [ sub { $_[0]->yview('scroll', -($_[1]/120)*3, 'units') },
469 Ev('D') ]
470 );
471 } elsif ($^O eq 'linux') {
472 ## on linux the mousewheel works by mapping to buttons 4 and 5
473 $w->bind('<4>' => sub { $_[0]->yview('scroll', -1, 'units') unless $Tk::strictMotif; });
474 $w->bind('<5>' => sub { $_[0]->yview('scroll', +1, 'units') unless $Tk::strictMotif; });
475 };
476 };
477
478
479 ## respond to a mouse-3 event in the paths list by posting the
480 ## context-appropriate menu
481 sub list_mouse_menu {
482 return if ($list->entrycget('gsd', '-state') eq 'disabled');
483 &anchor_display;
484 my ($X, $Y) = @_;
485 SWITCH: {
486 $menubar -> Post($X, $Y, 4), last SWITCH if
487 ($paths{$current}->{type} eq 'gsd');
488 $menubar -> Post($X, $Y, 5), last SWITCH if
489 ($paths{$current}->{type} =~ /(bkg|data|res)/);
490 $menubar -> Post($X, $Y, 7), last SWITCH if
491 ($paths{$current}->{type} eq 'fit');
492 $menubar -> Post($X, $Y, 8), last SWITCH if
493 ($paths{$current}->{type} eq 'feff');
494 $menubar -> Post($X, $Y, 9), last SWITCH if
495 ($paths{$current}->{type} eq 'path');
496 };
497 }
498
499
500
501 sub quit_artemis {
502 if ($config{general}{query_save} and !$project_saved) {
503 my $dialog =
504 $top -> Dialog(-bitmap => 'questhead',
505 -text => "Would you like to save this project before exiting?",
506 -title => 'Artemis: Exiting...',
507 -buttons => [qw/Yes No Cancel/],
508 -default_button => 'Yes',
509 -font => $config{fonts}{med},
510 -popover => 'cursor');
511 &posted_Dialog;
512 my $response = $dialog->Show();
513 Echo("Not quitting."), return if ($response eq 'Cancel');
514 Echo("Preparing to quit ...");
515 ($response eq 'Yes') and &save_project('all',0);
516 };
517 SWITCH: {
518 $opparams -> packForget(), last SWITCH if ($current_canvas eq 'op');
519 $gsd -> packForget(), last SWITCH if ($current_canvas eq 'gsd');
520 $feff -> packForget(), last SWITCH if ($current_canvas eq 'feff');
521 $path -> packForget(), last SWITCH if ($current_canvas eq 'path');
522 };
523 $top -> update;
524 ## clean up project directory
525 (-d $project_folder) and rmtree($project_folder);
526 ## delete autosave file
527 unlink $autosave_filename if (-e $autosave_filename);
528 opendir C, $stash_dir;
529 map { my $f = File::Spec->catfile($stash_dir, $_);
530 -f $f and unlink $f}
531 (grep !/(^\.{1,2}|TRAP)$/, readdir C);
532 closedir C;
533
534 $mru{config}{last_working_directory} = $current_data_dir;
535
536 ## remember the geometry, save it in the mru file
537 my ($width, $height, $x, $y) = split(/[x+]/, $top->geometry);
538 $mru{geometry}{height} = $height;
539 $mru{geometry}{width} = $width;
540 $mru{geometry}{'x'} = $x;
541 $mru{geometry}{'y'} = $y;
542 ($width, $height, $x, $y) = split(/[x+]/, $update->geometry);
543 $mru{geometry}{uheight} = $height;
544 $mru{geometry}{uwidth} = $width;
545 $mru{geometry}{'ux'} = $x;
546 $mru{geometry}{'uy'} = $y;
547 tied(%mru) -> WriteConfig($mrufile);
548
549
550 ## bye bye!
551 $top->destroy();
552 exit;
553 };
554
555 sub splash_message {
556 my ($message) = @_;
557 $splash_status -> configure(-text=>$message);
558 $top -> update;
559 #sleep 1;
560 };
561
562
563
564 ## I got this off of Usenet. Do a search at groups.google.com for the
565 ## package to find discussions of slow dialog boxes. The text of this
566 ## will be among the discussions.
567 package Patch::SREZIC::Tk::Wm;
568
569 use Tk::Wm;
570 package Tk::Wm;
571
572 sub Post
573 {
574 my ($w,$X,$Y) = @_;
575 $X = int($X);
576 $Y = int($Y);
577 $w->positionfrom('user');
578 # $w->geometry("+$X+$Y");
579 $w->MoveToplevelWindow($X,$Y);
580 $w->deiconify;
581 # $w->idletasks; # to prevent problems with KDE's kwm etc.
582 # $w->raise;
583 }
584
585 1;