source: trunk/gui/scripts/xyprint.tcl @ 2035

Last change on this file since 2035 was 2006, checked in by gah, 13 years ago

remove broken print statement.

File size: 50.6 KB
Line 
1
2# ----------------------------------------------------------------------
3#  COMPONENT: xyprint - Print X-Y plot.
4#
5# ======================================================================
6#  AUTHOR:  Michael McLennan, Purdue University
7#  Copyright (c) 2004-2005  Purdue Research Foundation
8#
9#  See the file "license.terms" for information on usage and
10#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11# ======================================================================
12package require Itk
13package require BLT
14
15#option add *XyPrint.width 3i widgetDefault
16#option add *XyPrint.height 3i widgetDefault
17option add *XyPrint.gridColor #d9d9d9 widgetDefault
18option add *XyPrint.activeColor blue widgetDefault
19option add *XyPrint.dimColor gray widgetDefault
20option add *XyPrint.controlBackground gray widgetDefault
21option add *XyPrint*Font "Arial 9" widgetDefault
22option add *XyPrint*Entry*width "6" widgetDefault
23option add *XyPrint*Entry*background "white" widgetDefault
24
25itcl::class Rappture::XyPrint {
26    inherit itk::Widget
27
28    constructor {args} {}
29    destructor {}
30
31    private variable _graph "";         # Original graph.
32    private variable _clone "";         # Cloned graph.
33    private variable _preview "";       # Preview image.
34    private variable _savedSettings;    # Array of settings.
35
36    private common _oldSettingsFile "~/.rpsettings"
37    private common _settingsFile "~/.rp_settings"
38
39    public method print { graph toolName plotName }
40    public method reset {}
41
42    private method CopyOptions { cmd orig clone {exclude {}}}
43    private method CloneGraph { orig }
44
45    private method BuildGeneralTab {}
46    private method BuildLayoutTab {}
47    private method BuildLegendTab {}
48    private method BuildAxisTab {}
49    private method SetOption { opt }
50    private method SetComponentOption { comp opt }
51    private method SetNamedComponentOption { comp name opt }
52    private method GetAxis {}
53    private method GetElement { args }
54    private method RegeneratePreview {}
55    private method InitClone {}
56    private method Pixels2Inches { pixels }
57    private method Inches2Pixels { inches {defValue ""}}
58    private method Color2RGB { color }
59
60    private method ApplyGeneralSettings {}
61    private method ApplyLegendSettings {}
62    private method ApplyAxisSettings {}
63    private method ApplyElementSettings {}
64    private method ApplyLayoutSettings {}
65    private method InitializeSettings {}
66    private method CreateSettings { toolName plotName }
67    private method RestoreSettings { toolName plotName }
68    private method SaveSettings { toolName plotName }
69    private method DestroySettings {}
70    private method ResetSettings { }
71    private method GetOutput {}
72    private method SetWaitVariable { state }
73    private method SetLayoutOption { option }
74    private method GetAxisType { axis }
75    private method restore { toolName plotName data }
76    private common _settings
77    private common _fonts
78    private common _wait
79}
80
81# ----------------------------------------------------------------------
82# CONSTRUCTOR
83# ----------------------------------------------------------------------
84itcl::body Rappture::XyPrint::constructor { args } {
85    Rappture::dispatcher _dispatcher
86    $_dispatcher register !rebuild
87    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
88   
89    set w [winfo pixels . 2.5i]
90    set h [winfo pixels . 2i]
91    set _preview [image create photo -width $w -height $h]
92    itk_component add tabs {
93        blt::tabset $itk_interior.tabs \
94            -highlightthickness 0 -tearoff 0 -side top \
95            -bd 0 -gap 0 -tabborderwidth 1 \
96            -outerpad 1 -background grey
97    } {
98        keep -cursor
99        ignore -highlightthickness -borderwidth -background
100    }
101    itk_component add preview {
102        label $itk_interior.preview \
103            -highlightthickness 0 -bd 0 -image $_preview -width 2.5i \
104                -height 2.25i -background grey -padx 10 -pady 10
105    } {
106        ignore -background
107    }
108    itk_component add ok {
109        button $itk_interior.ok -text "Save" \
110            -highlightthickness 0 -pady 2 -padx 0 \
111            -command [itcl::code $this SetWaitVariable 1] \
112            -compound left \
113            -image [Rappture::icon download]
114    }
115    itk_component add cancel {
116        button $itk_interior.cancel -text "Cancel" \
117            -highlightthickness 0 -pady 2 -padx 0 \
118            -command [itcl::code $this SetWaitVariable 0] \
119            -compound left \
120            -image [Rappture::icon cancel]
121    }
122    blt::table $itk_interior \
123        0,0 $itk_component(preview) -cspan 2 -fill both \
124        1,0 $itk_component(tabs) -fill both -cspan 2 \
125        2,1 $itk_component(cancel) -padx 2 -pady 2 -width .9i -fill y \
126        2,0 $itk_component(ok) -padx 2 -pady 2 -width .9i -fill y
127    blt::table configure $itk_interior r1 -resize none
128    blt::table configure $itk_interior r1 -resize both
129
130    BuildGeneralTab
131    BuildAxisTab
132    BuildLegendTab
133    BuildLayoutTab
134    eval itk_initialize $args
135}
136
137# ----------------------------------------------------------------------
138# DESTRUCTOR
139# ----------------------------------------------------------------------
140itcl::body Rappture::XyPrint::destructor {} {
141    destroy $_clone
142    image delete $_preview
143    array unset _settings $this-*
144}
145
146itcl::body Rappture::XyPrint::DestroySettings {} {
147    destroy $_clone
148    set _clone ""
149    set _graph ""
150    foreach font [array names _fonts] {
151        font delete $font
152    }
153    array unset _fonts
154}
155
156itcl::body Rappture::XyPrint::reset {} {
157    SetWaitVariable 0
158}
159
160itcl::body Rappture::XyPrint::SetWaitVariable { state } {
161    set _wait($this) $state
162}
163
164itcl::body Rappture::XyPrint::print { graph toolName plotName } {
165    set _graph $graph
166    CloneGraph $graph
167    InitClone
168    RestoreSettings $toolName $plotName
169    InitializeSettings
170    SetWaitVariable 0
171    tkwait variable [itcl::scope _wait($this)]
172    SaveSettings $toolName $plotName
173    set output ""
174    if { $_wait($this) } {
175        set output [GetOutput]
176    }
177    DestroySettings
178    return $output
179}
180
181itcl::body Rappture::XyPrint::GetOutput {} {
182    # Get the selected format of the download.
183    set page $itk_component(graph_page)
184    set format [$page.format current]
185
186    if { $format == "jpg" } {
187        set img [image create photo]
188        $_clone snap $img
189        set bytes [$img data -format "jpeg -quality 100"]
190        set bytes [Rappture::encoding::decode -as b64 $bytes]
191        image delete $img
192        return [list .jpg $bytes]
193    } elseif { $format == "png" } {
194        set img [image create photo]
195        $_clone snap $img
196        set bytes [$img data -format "png"]
197        set bytes [Rappture::encoding::decode -as b64 $bytes]
198        image delete $img
199        return [list .png $bytes]
200    }
201
202    # Handle encapsulated postscript or portable document format.
203
204    # Append an "i" for the graph postscript component.
205    set w $_settings($this-layout-width)i
206    set h $_settings($this-layout-height)i
207
208    $_clone postscript configure \
209        -maxpect false \
210        -decoration yes \
211        -center yes \
212        -width $w -height $h
213   
214    set psdata [$_clone postscript output]
215
216    if 0 {
217        set f [open "junk.raw" "w"]
218        puts -nonewline $f $psdata
219        close $f
220    }
221    if { $format == "eps" } {
222        return [list .$format $psdata]
223    }
224
225    set cmd ""
226    # | eps2eps << $psdata
227    lappend cmd "|" "/usr/bin/gs" \
228        "-q" "-sDEVICE=epswrite" "-sstdout=%stderr" \
229        "-sOutputFile=-" "-dNOPAUSE" "-dBATCH" "-dSAFER" \
230        "-dDEVICEWIDTH=250000" "-dDEVICEHEIGHT=250000" "-" "<<" "$psdata"
231    if { $format == "pdf" } {
232        # | eps2eps << $psdata | ps2pdf
233        lappend cmd "|" "/usr/bin/gs" \
234            "-q" "-sDEVICE=pdfwrite" "-sstdout=%stderr" \
235            "-sOutputFile=-" "-dNOPAUSE" "-dBATCH" "-dSAFER" \
236            "-dCompatibilityLevel=1.4" "-c" ".setpdfwrite" "-f" "-"
237    }
238    if { [catch {
239        set f [open $cmd "r"]
240        fconfigure $f -translation binary -encoding binary
241        set output [read $f]
242        close $f
243    } err ] != 0 } {
244        global errorInfo
245        puts stderr "failed to generate file: $err\n$errorInfo"
246        return ""
247    }
248    if 0 {
249        set f [open "junk.$format" "w"]
250        fconfigure $f -translation binary -encoding binary
251        puts -nonewline $f $output
252        close $f
253    }
254    return [list .$format $output]
255}
256
257itcl::body Rappture::XyPrint::CopyOptions { cmd orig clone {exclude {}} } {
258    set all [eval $orig $cmd]
259    set configLine $clone
260    foreach name $exclude {
261        set ignore($name) 1
262    }
263    foreach arg $cmd {
264        lappend configLine $arg
265    }
266    foreach option $all {
267        if { [llength $option] != 5 } {
268            continue
269        }
270        set switch [lindex $option 0]
271        set initial [lindex $option 3]
272        set current [lindex $option 4]
273        if { [info exists ignore($switch)] } {
274            continue
275        }
276        if { [string compare $initial $current] == 0 } {
277            continue
278        }
279        lappend configLine $switch $current
280    }
281    eval $configLine
282}
283
284itcl::body Rappture::XyPrint::CloneGraph { orig } {
285    set top $itk_interior
286    if { [winfo exists $top.graph] } {
287        destroy $top.graph
288    }
289    set _clone [blt::graph $top.graph]
290    CopyOptions "configure" $orig $_clone
291    # Axis component
292    foreach axis [$orig axis names] {
293        if { [$orig axis cget $axis -hide] } {
294            continue
295        }
296        if { [$_clone axis name $axis] == "" } {
297            $_clone axis create $axis
298        }
299        CopyOptions [list axis configure $axis] $orig $_clone
300    }
301    foreach axis { x y x2 y2 } {
302        $_clone ${axis}axis use [$orig ${axis}axis use]
303    }
304    # Pen component
305    foreach pen [$orig pen names] {
306        if { [$_clone pen name $pen] == "" } {
307            $_clone pen create $pen
308        }
309        CopyOptions [list pen configure $pen] $orig $_clone
310    }
311    # Marker component
312    foreach marker [$orig marker names] {
313        $_clone marker create [$orig marker type $marker] -name $marker
314        CopyOptions [list marker configure $marker] $orig $_clone -name
315    }
316    # Element component
317    foreach elem [$orig element names] {
318        set oper [$orig element type $elem]
319        $_clone $oper create $elem
320        CopyOptions [list $oper configure $elem] $orig $_clone -data
321        if { [$_clone $oper cget $elem -hide] } {
322            $_clone $oper configure $elem -label ""
323        }
324    }
325    # Fix element display list
326    $_clone element show [$orig element show]
327    # Legend component
328    CopyOptions {legend configure} $orig $_clone
329    # Postscript component
330    CopyOptions {postscript configure} $orig $_clone
331    # Crosshairs component
332    CopyOptions {crosshairs configure} $orig $_clone
333    # Grid component
334    CopyOptions {grid configure} $orig $_clone
335
336    # Create markers representing lines at zero for the x and y axis.
337    $_clone marker create line -name x-zero \
338        -coords "0 -Inf 0 Inf" -dashes 1 -hide yes
339    $_clone marker create line -name y-zero \
340        -coords "-Inf 0 Inf 0" -dashes 1 -hide yes
341}
342
343itcl::body Rappture::XyPrint::InitClone {} {
344   
345    $_clone configure -width 3.4i -height 3.4i -background white \
346        -borderwidth 0 \
347        -leftmargin 0 \
348        -rightmargin 0 \
349        -topmargin 0 \
350        -bottommargin 0
351   
352    # Kill the title and create a border around the plot
353    $_clone configure \
354        -title "" \
355        -plotborderwidth 1 -plotrelief solid  \
356        -plotbackground white -plotpadx 0 -plotpady 0
357
358    set _settings($this-layout-width) [Pixels2Inches [$_clone cget -width]]
359    set _settings($this-layout-height) [Pixels2Inches [$_clone cget -height]]
360    set _fonts(legend) [font create legend \
361                            -family helvetica -size 10 -weight normal]
362    update
363    $_clone legend configure \
364        -position right \
365        -font $_fonts(legend) \
366        -hide yes -borderwidth 0 -background white -relief solid \
367        -anchor nw -activeborderwidth 0
368    #
369    foreach axis [$_clone axis names] {
370        if { [$_clone axis cget $axis -hide] } {
371            continue
372        }
373        set _fonts($axis-ticks) [font create $axis-ticks \
374                                     -family helvetica -size 10 \
375                                     -weight normal -slant roman]
376        set _fonts($axis-title) [font create $axis-title \
377                                     -family helvetica -size 10 \
378                                     -weight normal -slant roman]
379        update
380        $_clone axis configure $axis -ticklength 5  \
381            -majorticks {} -minorticks {}
382        $_clone axis configure $axis \
383            -tickfont $_fonts($axis-ticks) \
384            -titlefont $_fonts($axis-title)
385    }
386    foreach elem [$_clone element names] {
387        if { [$_clone element type $elem] == "bar" } {
388            continue
389        }
390        if { [$_clone element cget $elem -linewidth] > 1 } {
391            $_clone element configure $elem -linewidth 1 -pixels 3
392        }
393    }
394}
395
396itcl::body Rappture::XyPrint::SetOption { opt } {
397    set new $_settings($this-graph$opt)
398    set old [$_clone cget $opt]
399    set code [catch [list $_clone configure $opt $new] err]
400    if { $code != 0 } {
401        bell
402        global errorInfo
403        puts stderr "$err: $errorInfo"
404        set _settings($this-graph$opt) $old
405        $_clone configure $opt $old
406    }
407}
408
409itcl::body Rappture::XyPrint::SetComponentOption { comp opt } {
410    set new $_settings($this-$comp$opt)
411    set old [$_clone $comp cget $opt]
412    set code [catch [list $_clone $comp configure $opt $new] err]
413    if { $code != 0 } {
414        bell
415        global errorInfo
416        puts stderr "$err: $errorInfo"
417        set _settings($this-$comp$opt) $old
418        $_clone $comp configure $opt $old
419    }
420}
421
422itcl::body Rappture::XyPrint::SetNamedComponentOption { comp name opt } {
423    set new $_settings($this-$comp$opt)
424    set old [$_clone $comp cget $name $opt]
425    set code [catch [list $_clone $comp configure $name $opt $new] err]
426    if { $code != 0 } {
427        bell
428        global errorInfo
429        puts stderr "$err: $errorInfo"
430        set _settings($this-$comp$opt) $old
431        $_clone $comp configure $name $opt $old
432    }
433}
434
435itcl::body Rappture::XyPrint::RegeneratePreview {} {
436    update
437    set img [image create photo]
438    set w [Inches2Pixels $_settings($this-layout-width) 3.4]
439    set h [Inches2Pixels $_settings($this-layout-height) 3.4]
440    set pixelsPerInch [winfo pixels . 1i]
441    set sx [expr 2.5*$pixelsPerInch/$w]
442    set sy [expr 2.0*$pixelsPerInch/$h]
443    set s [expr min($sx,$sy)]
444    $_clone snap $img -width $w -height $h
445
446    if 0 {
447        if { ![winfo exists .labeltest] } {
448            toplevel .labeltest -bg red
449            label .labeltest.label -image $img
450            pack .labeltest.label -fill both
451        }
452    }
453    set pw [expr int(round($s * $w))]
454    set ph [expr int(round($s * $h))]
455    $_preview configure -width $pw -height $ph
456    if 0 {
457        .labeltest.label configure -image $img
458    }
459    blt::winop resample $img $_preview box
460    image delete $img
461}
462
463itcl::body Rappture::XyPrint::Pixels2Inches { pixels } {
464    if { [llength $pixels] == 2 } {
465        set pixels [lindex $pixels 0]
466    }
467    set pixelsPerInch [winfo pixels . 1i]
468    set inches [expr { double($pixels) / $pixelsPerInch }]
469    return [format %.3g ${inches}]
470}
471
472itcl::body Rappture::XyPrint::Inches2Pixels { inches {defValue ""}} {
473    set n [scan $inches %g dummy]
474    if { $n != 1  && $defValue != "" } {
475        set inches $defValue
476    }
477    return  [winfo pixels . ${inches}i]
478}
479
480itcl::body Rappture::XyPrint::Color2RGB { color } {
481    foreach { r g b } [winfo rgb $_clone $color] {
482        set r [expr round($r / 257.0)]
483        set g [expr round($g / 257.0)]
484        set b [expr round($b / 257.0)]
485    }
486    return [format "\#%02x%02x%02x" $r $g $b]
487}
488
489itcl::body Rappture::XyPrint::GetAxisType { axis } {
490    foreach type { x y x2 y2 } {
491        set axes [$_clone ${type}axis use]
492        if { [lsearch $axes $axis] >= 0 } {
493            return [string range $type 0 0]
494        }
495    }
496    return ""
497}
498
499itcl::body Rappture::XyPrint::GetAxis {} {
500    set axis [$itk_component(axis_combo) current]
501    foreach option { -min -max -loose -title -stepsize -subdivisions } {
502        set _settings($this-axis$option) [$_clone axis cget $axis $option]
503    }
504    set type [GetAxisType $axis]
505    if { [$_clone grid cget -map${type}] == $axis } {
506        set _settings($this-axis-grid) 1
507    }  else {
508        set _settings($this-axis-grid) 0
509    }
510    set _settings($this-axis-plotpad${type}) \
511        [Pixels2Inches [$_clone cget -plotpad${type}]]
512    set _settings($this-axis-zero) [$_clone marker cget ${type}-zero -hide]
513}
514
515itcl::body Rappture::XyPrint::GetElement { args } {
516    set index 1
517    array unset _settings $this-element-*
518    foreach elem [$_clone element show] {
519        set _settings($this-element-$index) $elem
520        incr index
521    }
522    set index [$itk_component(element_slider) get]
523    set elem $_settings($this-element-$index)
524    set _settings($this-element-label) [$_clone element cget $elem -label]
525    set _settings($this-element-color) [$_clone element cget $elem -color]
526    if { [$_clone element type $elem] != "bar" } {
527        set _settings($this-element-symbol) [$_clone element cget $elem -symbol]
528        set _settings($this-element-dashes) [$_clone element cget $elem -dashes]
529    }
530    set page $itk_component(legend_page)
531    set color [$page.color label $_settings($this-element-color)]
532    if { $color == "" } {
533        set color [Color2RGB $_settings($this-element-color)]
534        $page.color choices insert end $color $color
535        $page.color value $color
536    } else {
537        $page.color value [$page.color label $_settings($this-element-color)]
538    }
539    if { [$_clone element type $elem] != "bar" } {
540        $page.symbol value [$page.symbol label $_settings($this-element-symbol)]
541        $page.dashes value [$page.dashes label $_settings($this-element-dashes)]
542    }
543    #FixElement
544}
545
546itcl::body Rappture::XyPrint::BuildLayoutTab {} {
547    itk_component add layout_page {
548        frame $itk_component(tabs).layout_page
549    }
550    set page $itk_component(layout_page)
551    $itk_component(tabs) insert end "layout" \
552        -text "Layout" -padx 2 -pady 2 -window $page -fill both
553
554    label $page.width_l -text "width" 
555    entry $page.width -width 6 \
556        -textvariable [itcl::scope _settings($this-layout-width)]
557    bind  $page.width <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
558    Rappture::Tooltip::for $page.width \
559        "Set the width (inches) of the output image. Must be a positive number."
560
561    label $page.height_l -text "height"
562    entry $page.height -width 6 \
563        -textvariable [itcl::scope _settings($this-layout-height)]
564    bind  $page.height <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
565    Rappture::Tooltip::for $page.height \
566        "Set the height (inches) of the output image. Must be a positive number"
567
568    label $page.margin_l -text "Margins" 
569
570    label $page.left_l -text "left"
571    entry $page.left -width 6 \
572        -textvariable [itcl::scope _settings($this-layout-leftmargin)]
573    bind  $page.left <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
574    Rappture::Tooltip::for $page.left \
575        "Set the size (inches) of the left margin. If zero, the size is automatically determined."
576
577    label $page.right_l -text "right"
578    entry $page.right -width 6 \
579        -textvariable [itcl::scope _settings($this-layout-rightmargin)]
580    bind  $page.right <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
581    Rappture::Tooltip::for $page.right \
582        "Set the size (inches) of the right margin. If zero, the size is automatically determined."
583
584
585    label $page.top_l -text "top"
586    entry $page.top -width 6 \
587        -textvariable [itcl::scope _settings($this-layout-topmargin)]
588    bind  $page.top <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
589    Rappture::Tooltip::for $page.top \
590        "Set the size (inches) of the top margin. If zero, the size is automatically determined."
591
592    label $page.bottom_l -text "bottom"
593    entry $page.bottom -width 6 \
594        -textvariable [itcl::scope _settings($this-layout-bottommargin)]
595    bind  $page.bottom <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
596    Rappture::Tooltip::for $page.bottom \
597        "Set the size (inches) of the bottom margin. If zero, the size is automatically determined."
598
599
600    label $page.map -image [Rappture::icon graphmargins]
601    blt::table $page \
602        0,0 $page.width_l -anchor e \
603        0,1 $page.width -fill x  \
604        1,0 $page.height_l -anchor e \
605        1,1 $page.height -fill x \
606        3,0 $page.left_l -anchor e \
607        3,1 $page.left -fill x  \
608        4,0 $page.right_l -anchor e \
609        4,1 $page.right -fill x \
610        5,0 $page.top_l -anchor e \
611        5,1 $page.top -fill x \
612        6,0 $page.bottom_l -anchor e \
613        6,1 $page.bottom -fill x  \
614        0,2 $page.map -fill both -rspan 7 -padx 2
615
616    blt::table configure $page c0 r* -resize none
617    blt::table configure $page c1 r2 -resize both
618}
619
620
621itcl::body Rappture::XyPrint::BuildGeneralTab {} {
622    itk_component add graph_page {
623        frame $itk_component(tabs).graph_page
624    }
625    set page $itk_component(graph_page)
626    $itk_component(tabs) insert end "graph" \
627        -text "General" -padx 2 -pady 2 -window $page -fill both
628
629    label $page.format_l -text "format"
630    Rappture::Combobox $page.format -width 30 -editable no
631    $page.format choices insert end \
632        "pdf" "PDF Portable Document Format" \
633        "ps"  "PS PostScript Format"  \
634        "eps" "EPS Encapsulated PostScript"  \
635        "jpg" "JPEG Joint Photographic Experts Group Format" \
636        "png" "PNG Portable Network Graphics Format"         
637
638    bind $page.format <<Value>> [itcl::code $this ApplyGeneralSettings]
639    Rappture::Tooltip::for $page.format \
640        "Set the format of the image."
641
642    label $page.style_l -text "style"
643    Rappture::Combobox $page.style -width 20 -editable no
644    $page.style choices insert end \
645        "ieee" "IEEE Journal"  \
646        "gekco" "G Klimeck" 
647    bind $page.style <<Value>> [itcl::code $this ApplyGeneralSettings]
648    Rappture::Tooltip::for $page.format \
649        "Set the style template."
650
651    checkbutton $page.remember -text "remember settings" \
652        -variable [itcl::scope _settings($this-general-remember)] 
653    Rappture::Tooltip::for $page.remember \
654        "Remember the settings. They will be override the default settings."
655
656    button $page.revert -text "revert" -image [Rappture::icon revert] \
657        -compound left -padx 3 -pady 1 -overrelief raised -relief flat \
658        -command [itcl::code $this ResetSettings]
659    Rappture::Tooltip::for $page.revert \
660        "Revert to the default settings."
661
662    blt::table $page \
663        2,0 $page.format_l -anchor e \
664        2,1 $page.format -fill x -cspan 2 \
665        3,0 $page.style_l -anchor e \
666        3,1 $page.style -fill x -cspan 2 \
667        5,0 $page.remember -cspan 2 -anchor w \
668        5,2 $page.revert -anchor e
669    blt::table configure $page r* -resize none  -pady { 0 2 }
670    blt::table configure $page r4 -resize both
671
672}
673   
674itcl::body Rappture::XyPrint::ApplyLegendSettings {} {
675    set page $itk_component(legend_page)
676    set _settings($this-legend-anchor)    [$page.anchor current]
677    if { $_clone != "" } {
678        font configure $_fonts(legend) \
679            -family $_settings($this-legend-font-family) \
680            -size $_settings($this-legend-font-size) \
681            -weight $_settings($this-legend-font-weight) \
682            -slant $_settings($this-legend-font-slant)
683        foreach option { -hide -position -anchor -borderwidth } {
684            SetComponentOption legend $option
685        }
686        $_clone legend configure -font fixed -font $_fonts(legend)
687    }
688    ApplyElementSettings
689}
690
691itcl::body Rappture::XyPrint::BuildLegendTab {} {
692    itk_component add legend_page {
693        frame $itk_component(tabs).legend_page
694    }
695    set page $itk_component(legend_page)
696    $itk_component(tabs) insert end "legend" \
697        -text "Legend" -padx 2 -pady 2 -window $page -fill both
698
699    checkbutton $page.show -text "show legend" \
700        -offvalue 1 -onvalue 0 \
701        -variable [itcl::scope _settings($this-legend-hide)]  \
702        -command [itcl::code $this ApplyLegendSettings]
703    Rappture::Tooltip::for $page.show \
704        "Display the legend."
705
706    label $page.position_l -text "position"
707    Rappture::Combobox $page.position -width 15 -editable no
708    $page.position choices insert end \
709        "leftmargin" "left margin"  \
710        "rightmargin" "right margin"  \
711        "bottommargin" "bottom margin"  \
712        "topmargin" "top margin"  \
713        "plotarea" "inside plot"
714    bind $page.position <<Value>> [itcl::code $this ApplyLegendSettings]
715    Rappture::Tooltip::for $page.position \
716        "Set the position of the legend.  This option and the anchor determine the legend's location."
717
718    Rappture::Combobox $page.anchor -width 10 -editable no
719    $page.anchor choices insert end \
720        "nw" "northwest"  \
721        "n" "north"  \
722        "ne" "northeast"  \
723        "sw" "southwest"  \
724        "s" "south"  \
725        "se" "southeast"  \
726        "c" "center"  \
727        "e" "east"  \
728        "w" "west" 
729    bind $page.anchor <<Value>> [itcl::code $this ApplyLegendSettings]
730    Rappture::Tooltip::for $page.anchor \
731        "Set the anchor of the legend.  This option and the anchor determine the legend's location."
732
733    checkbutton $page.border -text "border" \
734        -variable [itcl::scope _settings($this-legend-borderwidth)] \
735        -onvalue 1 -offvalue 0 \
736        -command [itcl::code $this ApplyLegendSettings]
737    Rappture::Tooltip::for $page.border \
738        "Display a solid border around the legend."
739
740    label $page.slider_l -text "legend\nentry"  -justify right
741    itk_component add element_slider {
742        ::scale $page.slider -from 1 -to 1 \
743            -orient horizontal -width 12 \
744            -command [itcl::code $this GetElement]
745    }
746    Rappture::Tooltip::for $page.slider \
747        "Select the current entry."
748
749    label $page.label_l -text "label"
750    entry $page.label \
751        -background white \
752        -textvariable [itcl::scope _settings($this-element-label)]
753    bind  $page.label <KeyPress-Return> [itcl::code $this ApplyElementSettings]
754    Rappture::Tooltip::for $page.label \
755        "Set the label of the current entry in the legend."
756
757    label $page.color_l -text "color "
758    Rappture::Combobox $page.color -width 15 -editable no
759    $page.color choices insert end \
760        "#000000" "black" \
761        "#ffffff" "white" \
762        "#0000cd" "blue" \
763        "#cd0000" "red" \
764        "#00cd00" "green" \
765        "#3a5fcd" "royal blue" \
766        "#cdcd00" "yellow" \
767        "#cd1076" "deep pink" \
768        "#009acd" "deep sky blue" \
769        "#00c5cd" "torquise" \
770        "#a2b5cd" "light steel blue" \
771        "#7ac5cd" "cadet blue" \
772        "#66cdaa" "aquamarine" \
773        "#a2cd5a" "dark olive green" \
774        "#cd9b9b" "rosy brown" \
775        "#0000ff" "blue1" \
776        "#ff0000" "red1" \
777        "#00ff00" "green1"
778    bind $page.color <<Value>> [itcl::code $this ApplyElementSettings]
779    Rappture::Tooltip::for $page.color \
780        "Set the color of the current entry."
781
782    label $page.dashes_l -text "line style"
783    Rappture::Combobox $page.dashes -width 15 -editable no
784    $page.dashes choices insert end \
785        "" "solid"  \
786        "1" "dot" \
787        "5 2" "dash" \
788        "2 4 2" "dashdot" \
789        "2 4 2 2" "dashdotdot"
790    bind $page.dashes <<Value>> [itcl::code $this ApplyElementSettings]
791    Rappture::Tooltip::for $page.dashes \
792        "Set the line style of the current entry."
793
794    label $page.symbol_l -text "symbol"
795    Rappture::Combobox $page.symbol -editable no
796    $page.symbol choices insert end \
797        "none" "none"  \
798        "square" "square" \
799        "circle" "circle" \
800        "diamond" "diamond" \
801        "plus" "plus" \
802        "cross" "cross" \
803        "splus" "skinny plus"  \
804        "scross" "skinny cross" \
805        "triangle" "triangle"
806    bind $page.symbol <<Value>> [itcl::code $this ApplyElementSettings]
807    Rappture::Tooltip::for $page.symbol \
808        "Set the symbol of the current entry. \"none\" display no symbols."
809
810    label $page.font_l -text "font"
811    Rappture::Combobox $page.fontfamily -width 10 -editable no
812    $page.fontfamily choices insert end \
813        "courier" "courier" \
814        "helvetica" "helvetica"  \
815        "new*century*schoolbook"  "new century schoolbook" \
816        "symbol"  "symbol" \
817        "times"  "times"         
818    bind $page.fontfamily <<Value>> [itcl::code $this ApplyLegendSettings]
819    Rappture::Tooltip::for $page.fontfamily \
820        "Set the font of entries in the legend."
821
822    Rappture::Combobox $page.fontsize -width 4 -editable no
823    $page.fontsize choices insert end \
824        "8" "8" \
825        "9" "9" \
826        "10" "10" \
827        "11" "11" \
828        "12" "12" \
829        "14" "14" \
830        "17" "17" \
831        "18" "18" \
832        "20" "20"
833    bind  $page.fontsize <<Value>> [itcl::code $this ApplyLegendSettings]
834    Rappture::Tooltip::for $page.fontsize \
835        "Set the size (points) of the font."
836
837    Rappture::PushButton $page.fontweight \
838        -width 18 -height 18 \
839        -onimage [Rappture::icon font-bold] \
840        -offimage [Rappture::icon font-bold] \
841        -onvalue "bold" -offvalue "normal" \
842        -command [itcl::code $this ApplyLegendSettings] \
843        -variable [itcl::scope _settings($this-legend-font-weight)]
844    Rappture::Tooltip::for $page.fontweight \
845        "Use the bold version of the font."
846
847    Rappture::PushButton $page.fontslant \
848        -width 18 -height 18 \
849        -onimage [Rappture::icon font-italic] \
850        -offimage [Rappture::icon font-italic] \
851        -onvalue "italic" -offvalue "roman" \
852        -command [itcl::code $this ApplyLegendSettings] \
853        -variable [itcl::scope _settings($this-legend-font-slant)]
854    Rappture::Tooltip::for $page.fontslant \
855        "Use the italic version of the font."
856
857    blt::table $page \
858        1,0 $page.show -cspan 2 -anchor w \
859        1,2 $page.border -cspan 2 -anchor w \
860        2,0 $page.position_l -anchor e \
861        2,1 $page.position -fill x \
862        2,2 $page.anchor -fill x  -cspan 3 \
863        3,0 $page.font_l -anchor e \
864        3,1 $page.fontfamily -fill x \
865        3,2 $page.fontsize -fill x \
866        3,3 $page.fontweight -anchor e \
867        3,4 $page.fontslant -anchor e \
868        4,0 $page.slider_l -anchor e \
869        4,1 $page.slider -fill x -cspan 5 \
870        5,0 $page.label_l -anchor e \
871        5,1 $page.label -fill x -cspan 5 \
872        6,0 $page.color_l -anchor e \
873        6,1 $page.color -fill x \
874        6,2 $page.symbol_l -anchor e \
875        6,3 $page.symbol -fill both -cspan 3 \
876        7,0 $page.dashes_l -anchor e \
877        7,1 $page.dashes -fill x \
878
879    blt::table configure $page r* -resize none -pady { 0 2 }
880    blt::table configure $page r8 -resize both
881
882}
883
884itcl::body Rappture::XyPrint::BuildAxisTab {} {
885    itk_component add axis_page {
886        frame $itk_component(tabs).axis_page
887    }
888    set page $itk_component(axis_page)
889    $itk_component(tabs) insert end "axis" \
890        -text "Axis" -padx 2 -pady 2 -window $page -fill both
891   
892    label $page.axis_l -text "axis"
893    itk_component add axis_combo {
894        Rappture::Combobox $page.axis -width 20 -editable no
895    }
896    bind $itk_component(axis_combo) <<Value>> [itcl::code $this GetAxis]
897    Rappture::Tooltip::for $page.axis \
898        "Select the current axis."
899
900    label $page.title_l -text "title"
901    entry $page.title \
902        -textvariable [itcl::scope _settings($this-axis-title)]
903    bind  $page.title <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
904    Rappture::Tooltip::for $page.title \
905        "Set the title of the current axis."
906
907    label $page.min_l -text "min"
908    entry $page.min -width 10 \
909        -textvariable [itcl::scope _settings($this-axis-min)]
910    bind  $page.min <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
911    Rappture::Tooltip::for $page.min \
912        "Set the minimum limit for the current axis. If empty, the minimum is automatically determined."
913
914    label $page.max_l -text "max"
915    entry $page.max -width 10 \
916        -textvariable [itcl::scope _settings($this-axis-max)]
917    bind  $page.max <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
918    Rappture::Tooltip::for $page.max \
919        "Set the maximum limit for the current axis. If empty, the maximum is automatically determined."
920
921    label $page.subdivisions_l -text "subdivisions"
922    entry $page.subdivisions \
923        -textvariable [itcl::scope _settings($this-axis-subdivisions)]
924    bind  $page.subdivisions <KeyPress-Return> \
925        [itcl::code $this ApplyAxisSettings]
926    Rappture::Tooltip::for $page.subdivisions \
927        "Set the number of subdivisions (minor ticks) for the current axis."
928
929    label $page.stepsize_l -text "step size"
930    entry $page.stepsize \
931        -textvariable [itcl::scope _settings($this-axis-stepsize)]
932    bind  $page.stepsize <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
933    Rappture::Tooltip::for $page.stepsize \
934        "Set the interval between major ticks for the current axis. If zero, the interval is automatically determined."
935
936    checkbutton $page.loose -text "loose limits" \
937        -onvalue "always" -offvalue "0" \
938        -variable [itcl::scope _settings($this-axis-loose)] \
939        -command [itcl::code $this ApplyAxisSettings]
940    Rappture::Tooltip::for $page.loose \
941        "Set major ticks outside of the limits for the current axis."
942
943    label $page.plotpad_l -text "pad" 
944    entry $page.plotpad -width 6 \
945        -textvariable [itcl::scope _settings($this-axis-plotpad)]
946    bind  $page.plotpad <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
947    Rappture::Tooltip::for $page.plotpad \
948        "Set padding (points) between the current axis and the plot."
949
950    checkbutton $page.grid -text "show grid lines" \
951        -variable [itcl::scope _settings($this-axis-grid)] \
952        -command [itcl::code $this ApplyAxisSettings]
953    Rappture::Tooltip::for $page.grid \
954        "Display grid lines for the current axis."
955
956    checkbutton $page.zero -text "mark zero" \
957        -offvalue 1 -onvalue 0 \
958        -variable [itcl::scope _settings($this-axis-zero)] \
959        -command [itcl::code $this ApplyAxisSettings]
960    Rappture::Tooltip::for $page.zero \
961        "Display a line at zero for the current axis."
962
963    label $page.tickfont_l -text "tick font"
964    Rappture::Combobox $page.tickfontfamily -width 10 -editable no
965    $page.tickfontfamily choices insert end \
966        "courier" "courier" \
967        "helvetica" "helvetica"  \
968        "new*century*schoolbook"  "new century schoolbook" \
969        "symbol"  "symbol" \
970        "times"  "times"         
971    bind $page.tickfontfamily <<Value>> [itcl::code $this ApplyAxisSettings]
972    Rappture::Tooltip::for $page.tickfontfamily \
973        "Set the font of the ticks for the current axis."
974
975    Rappture::Combobox $page.tickfontsize -width 4 -editable no
976    $page.tickfontsize choices insert end \
977        "8" "8" \
978        "9" "9" \
979        "10" "10" \
980        "11" "11" \
981        "12" "12" \
982        "14" "14" \
983        "17" "17" \
984        "18" "18" \
985        "20" "20"
986    bind $page.tickfontsize <<Value>> [itcl::code $this ApplyAxisSettings]
987    Rappture::Tooltip::for $page.tickfontsize \
988        "Set the size (points) of the tick font."
989
990    Rappture::PushButton $page.tickfontweight \
991        -width 18 -height 18 \
992        -onimage [Rappture::icon font-bold] \
993        -offimage [Rappture::icon font-bold] \
994        -onvalue "bold" -offvalue "normal" \
995        -command [itcl::code $this ApplyAxisSettings] \
996        -variable [itcl::scope _settings($this-axis-tickfont-weight)]
997    Rappture::Tooltip::for $page.tickfontweight \
998        "Use the bold version of the tick font."
999
1000    Rappture::PushButton $page.tickfontslant \
1001        -width 18 -height 18 \
1002        -onimage [Rappture::icon font-italic] \
1003        -offimage [Rappture::icon font-italic] \
1004        -onvalue "italic" -offvalue "roman" \
1005        -command [itcl::code $this ApplyAxisSettings] \
1006        -variable [itcl::scope _settings($this-axis-tickfont-slant)]
1007    Rappture::Tooltip::for $page.tickfontslant \
1008        "Use the italic version of the tick font."
1009
1010    label $page.titlefont_l -text "title font"
1011    Rappture::Combobox $page.titlefontfamily -width 10 -editable no
1012    $page.titlefontfamily choices insert end \
1013        "courier" "courier" \
1014        "helvetica" "helvetica"  \
1015        "new*century*schoolbook"  "new century schoolbook" \
1016        "symbol"  "symbol" \
1017        "times"  "times"         
1018    bind $page.titlefontfamily <<Value>> [itcl::code $this ApplyAxisSettings]
1019    Rappture::Tooltip::for $page.titlefontfamily \
1020        "Set the font of the title for the current axis."
1021
1022    Rappture::Combobox $page.titlefontsize -width 4 -editable no
1023    $page.titlefontsize choices insert end \
1024        "8" "8" \
1025        "9" "9" \
1026        "10" "10" \
1027        "11" "11" \
1028        "12" "12" \
1029        "14" "14" \
1030        "17" "17" \
1031        "18" "18" \
1032        "20" "20"
1033    bind $page.titlefontsize <<Value>> [itcl::code $this ApplyAxisSettings]
1034    Rappture::Tooltip::for $page.titlefontsize \
1035        "Set the size (point) of the title font."
1036
1037    Rappture::PushButton $page.titlefontweight \
1038        -width 18 -height 18 \
1039        -onimage [Rappture::icon font-bold] \
1040        -offimage [Rappture::icon font-bold] \
1041        -onvalue "bold" -offvalue "normal" \
1042        -command [itcl::code $this ApplyAxisSettings] \
1043        -variable [itcl::scope _settings($this-axis-titlefont-weight)]
1044    Rappture::Tooltip::for $page.titlefontweight \
1045        "Use the bold version of the title font."
1046
1047    Rappture::PushButton $page.titlefontslant \
1048        -width 18 -height 18 \
1049        -onimage [Rappture::icon font-italic] \
1050        -offimage [Rappture::icon font-italic] \
1051        -onvalue "italic" -offvalue "roman" \
1052        -command [itcl::code $this ApplyAxisSettings] \
1053        -variable [itcl::scope _settings($this-axis-titlefont-slant)]
1054    Rappture::Tooltip::for $page.titlefontslant \
1055        "Use the italic version of the title font."
1056
1057    blt::table $page \
1058        1,1 $page.axis_l -anchor e  -pady 6 \
1059        1,2 $page.axis -fill x -cspan 6 \
1060        2,1 $page.title_l -anchor e \
1061        2,2 $page.title -fill x -cspan 5 \
1062        3,1 $page.min_l -anchor e \
1063        3,2 $page.min -fill x \
1064        3,3 $page.stepsize_l -anchor e \
1065        3,4 $page.stepsize -fill both -cspan 3 \
1066        4,1 $page.max_l -anchor e \
1067        4,2 $page.max -fill both \
1068        4,3 $page.subdivisions_l -anchor e \
1069        4,4 $page.subdivisions -fill both -cspan 3  \
1070        5,1 $page.titlefont_l -anchor e \
1071        5,2 $page.titlefontfamily -fill x -cspan 2 \
1072        5,4 $page.titlefontsize -fill x \
1073        5,5 $page.titlefontweight -anchor e \
1074        5,6 $page.titlefontslant -anchor e \
1075        6,1 $page.tickfont_l -anchor e \
1076        6,2 $page.tickfontfamily -fill x -cspan 2 \
1077        6,4 $page.tickfontsize -fill x \
1078        6,5 $page.tickfontweight -anchor e \
1079        6,6 $page.tickfontslant -anchor e \
1080        7,1 $page.loose -cspan 2 -anchor w \
1081        7,3 $page.grid -anchor w -cspan 2 \
1082        8,1 $page.zero -cspan 2 -anchor w \
1083        8,3 $page.plotpad_l -anchor e \
1084        8,4 $page.plotpad -fill both -cspan 3
1085
1086    blt::table configure $page  c0 c7 c8 -resize none
1087}
1088
1089itcl::body Rappture::XyPrint::ApplyGeneralSettings {} {
1090    RegeneratePreview
1091}
1092
1093itcl::body Rappture::XyPrint::ApplyLegendSettings {} {
1094    set page $itk_component(legend_page)
1095    set _settings($this-legend-position)  [$page.position current]
1096    set _settings($this-legend-anchor)    [$page.anchor current]
1097
1098    foreach option { -hide -position -anchor -borderwidth } {
1099        SetComponentOption legend $option
1100    }
1101    font configure $_fonts(legend) \
1102        -family [$page.fontfamily current] \
1103        -size [$page.fontsize current] \
1104        -weight $_settings($this-legend-font-weight) \
1105        -slant $_settings($this-legend-font-slant)
1106    $_clone legend configure -font fixed -font $_fonts(legend)
1107    ApplyElementSettings
1108}
1109
1110itcl::body Rappture::XyPrint::ApplyAxisSettings {} {
1111    set axis [$itk_component(axis_combo) current]
1112    set type [GetAxisType $axis]
1113    set page $itk_component(axis_page)
1114    if { $_settings($this-axis-grid) } {
1115        $_clone grid configure -hide no -map${type} ${axis}
1116    } else {
1117        $_clone grid configure -hide no -map${type} ""
1118    }
1119    $_clone configure -plotpad${type} $_settings($this-axis-plotpad)
1120    foreach option { -min -max -loose -title -stepsize -subdivisions } {
1121        SetNamedComponentOption axis $axis $option
1122    }
1123    $_clone marker configure ${type}-zero -hide $_settings($this-axis-zero)
1124    font configure $axis-title \
1125        -family [$page.titlefontfamily current] \
1126        -size   [$page.titlefontsize current] \
1127        -weight $_settings($this-axis-titlefont-weight) \
1128        -slant  $_settings($this-axis-titlefont-slant)
1129    font configure $axis-ticks \
1130        -family [$page.tickfontfamily current] \
1131        -size   [$page.tickfontsize current] \
1132        -weight $_settings($this-axis-tickfont-weight) \
1133        -slant  $_settings($this-axis-tickfont-slant)
1134    $_clone axis configure $axis -tickfont $axis-ticks -titlefont $axis-title
1135    GetAxis
1136    RegeneratePreview
1137}
1138
1139itcl::body Rappture::XyPrint::ApplyElementSettings {} {
1140    set index [$itk_component(element_slider) get]
1141    set page $itk_component(legend_page)
1142    set _settings($this-element-color)  [$page.color current]
1143    if { $_clone != "" } {
1144        set elem $_settings($this-element-$index)
1145        if { [$_clone element type $elem] != "bar" } {
1146            set _settings($this-element-symbol) [$page.symbol current]
1147            set _settings($this-element-dashes) [$page.dashes current]
1148            foreach option { -symbol -dashes } {
1149                SetNamedComponentOption element $elem $option
1150            }
1151        }
1152        foreach option { -color -label } {
1153            SetNamedComponentOption element $elem $option
1154        }
1155        RegeneratePreview
1156    }
1157}
1158
1159itcl::body Rappture::XyPrint::SetLayoutOption { opt } {
1160    set new [Inches2Pixels $_settings($this-layout$opt)]
1161    $_clone configure $opt $new
1162}
1163
1164itcl::body Rappture::XyPrint::ApplyLayoutSettings {} {
1165    foreach opt { -width -height -leftmargin -rightmargin -topmargin
1166        -bottommargin } {
1167        set old [$_clone cget $opt]
1168        set code [catch { SetLayoutOption $opt } err]
1169        if { $code != 0 } {
1170            bell
1171            global errorInfo
1172            puts stderr "$err: $errorInfo"
1173            set _settings($this-layout$opt) [Pixels2Inches $old]
1174            $_clone configure $opt [Pixels2Inches $old]
1175        }
1176    }
1177    RegeneratePreview
1178}
1179
1180
1181itcl::body Rappture::XyPrint::InitializeSettings {} {
1182    # General settings
1183
1184    # Always set to "ps" "ieee"
1185    set _settings($this-general-format) ps
1186    set _settings($this-general-style) ieee
1187    set _settings($this-general-remember) 0
1188    set page $itk_component(graph_page)
1189    $page.format value [$page.format label $_settings($this-general-format)]
1190    $page.style value [$page.style label $_settings($this-general-style)]
1191
1192    # Layout settings
1193    set _settings($this-layout-width) [Pixels2Inches [$_clone cget -width]]
1194    set _settings($this-layout-height) [Pixels2Inches [$_clone cget -height]]
1195    set _settings($this-layout-leftmargin) \
1196        [Pixels2Inches [$_clone cget -leftmargin]]
1197    set _settings($this-layout-rightmargin) \
1198        [Pixels2Inches [$_clone cget -rightmargin]]
1199    set _settings($this-layout-topmargin) \
1200        [Pixels2Inches [$_clone cget -topmargin]]
1201    set _settings($this-layout-bottommargin) \
1202        [Pixels2Inches [$_clone cget -bottommargin]]
1203
1204    # Legend settings
1205    set page $itk_component(legend_page)
1206
1207    set names [$_clone element show]
1208    $itk_component(element_slider) configure -from 1 -to [llength $names]
1209    set state [expr  { ([llength $names] < 2) ? "disabled" : "normal" } ]
1210    $page.slider configure -state $state
1211    $page.slider_l configure -state $state
1212    # Always initialize the slider to the first entry in the element list.
1213    $itk_component(element_slider) set 1
1214    # Always set the borderwidth to be not displayed
1215    set _settings($this-legend-borderwidth) 0
1216
1217    array unset info
1218    array set info [font configure legend]
1219    $page.fontfamily value $info(-family)
1220    $page.fontsize value $info(-size)
1221    set _settings($this-legend-font-weight) $info(-weight)
1222    set _settings($this-legend-font-slant) $info(-slant)
1223    if { $info(-weight) == "bold" } {
1224        set _settings($this-legend-font-bold) 1
1225    }
1226    set _settings($this-legend-hide) [$_clone legend cget -hide]
1227    set _settings($this-legend-position) [$_clone legend cget -position]
1228    set _settings($this-legend-anchor) [$_clone legend cget -anchor]
1229    $page.position value \
1230        [$page.position label $_settings($this-legend-position)]
1231    $page.anchor value [$page.anchor label $_settings($this-legend-anchor)]
1232    GetElement
1233
1234    # Axis settings
1235    set page $itk_component(axis_page)
1236    set names [lsort [$_clone axis names]]
1237    $itk_component(axis_combo) choices delete 0 end
1238    foreach axis $names {
1239        if { ![$_clone axis cget $axis -hide] } {
1240            $itk_component(axis_combo) choices insert end $axis $axis
1241        }
1242        lappend axisnames $axis
1243    }
1244    set axis [lindex $names 0]
1245
1246    array set info [font configure $axis-title]
1247    $page.titlefontfamily value $info(-family)
1248    $page.titlefontsize value $info(-size)
1249    set _settings($this-axis-titlefont-weight) $info(-weight)
1250    set _settings($this-axis-titlefont-slant) $info(-slant)
1251
1252    array set info [font configure $axis-ticks]
1253    $page.tickfontfamily value $info(-family)
1254    $page.tickfontsize value $info(-size)
1255    set _settings($this-axis-tickfont-weight) $info(-weight)
1256    set _settings($this-axis-tickfont-slant) $info(-slant)
1257
1258    # Always hide the zero line.
1259    set _settings($this-axis-zero) 1
1260    set _settings($this-axis-plotpad) [Pixels2Inches [$_clone cget -plotpadx]]
1261    # Pick the first axis to initially display
1262    set axis [lindex $axisnames 0]
1263    $itk_component(axis_combo) value $axis
1264    GetAxis
1265    RegeneratePreview
1266}
1267
1268
1269itcl::body Rappture::XyPrint::restore { toolName plotName data } {
1270    set key [list $toolName $plotName]
1271    set _savedSettings($key) $data
1272}
1273
1274itcl::body Rappture::XyPrint::RestoreSettings { toolName plotName } {
1275    if { ![file readable $_settingsFile] } {
1276        return;                         # No file or not readable
1277    }
1278    if { [file exists $_oldSettingsFile] } {
1279        file delete $_oldSettingsFile
1280    }
1281    # Read the file by sourcing it into a safe interpreter The only commands
1282    # executed will be "xyprint" which will simply add the data into an array
1283    # _savedSettings.
1284    set parser [interp create -safe]
1285    $parser alias xyprint [itcl::code $this restore]
1286    $parser alias font font
1287    set f [open $_settingsFile "r"]
1288    set code [read $f]
1289    close $f
1290    $parser eval $code
1291   
1292    # Now see if there's an entry for this tool/plot combination.  The data
1293    # associated with the variable is itself code to update the graph (clone).
1294    set key [list $toolName $plotName]
1295    if { [info exists _savedSettings($key)] }  {
1296        $parser alias "preview" $_clone
1297        $parser eval $_savedSettings($key)
1298    }
1299    foreach {name value} [$parser eval "array get general"] {
1300        set _settings($this-graph-$name) $value
1301    }
1302    interp delete $parser
1303}
1304
1305itcl::body Rappture::XyPrint::ResetSettings {} {
1306    set graph $_graph
1307    DestroySettings
1308    set _graph $graph
1309    CloneGraph $graph
1310    InitClone
1311    InitializeSettings
1312}
1313
1314itcl::body Rappture::XyPrint::SaveSettings { toolName plotName } {
1315    if { !$_settings($this-general-remember) } {
1316        return
1317    }
1318    if { [catch { open $_settingsFile "w" 0600 } f ] != 0 } {
1319        puts stderr "$_settingsFile isn't writable: $f"
1320        bell
1321        return
1322    }
1323    set key [list $toolName $plotName]
1324    set _savedSettings($key) [CreateSettings $toolName $plotName]
1325    # Write the settings out
1326    foreach key [lsort [array names _savedSettings]] {
1327        set tool [lindex $key 0]
1328        set plot [lindex $key 1]
1329        puts $f "xyprint \"$tool\" \"$plot\" \{"
1330        set settings [string trim $_savedSettings($key) \n]
1331        puts $f "$settings"
1332        puts $f "\}\n"
1333    }
1334    close $f
1335}
1336
1337itcl::body Rappture::XyPrint::CreateSettings { toolName plotName } {
1338    # Create stanza associated with tool and plot title.
1339    # General settings
1340    append out "    set general(format) $_settings($this-general-format)\n"
1341    append out "    set general(style) $_settings($this-general-style)\n"
1342
1343    foreach font [array names _fonts] {
1344        append out "    font configure $font"
1345        array unset info
1346        array set info [font configure $font]
1347        foreach opt { -family -size -weight -slant } {
1348            set value [list $info($opt)]
1349            append out " $opt $value"
1350        }
1351        append out "\n"
1352    }
1353    append out "\n"
1354
1355    # Layout settings
1356    append out "    preview configure"
1357    foreach opt { -width -height -leftmargin -rightmargin -topmargin
1358        -bottommargin -plotpadx -plotpady } {
1359        set value [list [$_clone cget $opt]]
1360        append out " $opt $value"
1361    }
1362    append out "\n"
1363
1364    # Legend settings
1365    append out "    preview legend configure"
1366    foreach opt { -position -anchor -borderwidth -hide } {
1367        set value [list [$_clone legend cget $opt]]
1368        append out " $opt $value"
1369    }
1370    append out " -font legend\n"
1371
1372    # Element settings
1373    foreach elem [$_clone element show] {
1374        set label [$_clone element cget $elem -label]
1375        if { $label == "" } {
1376            continue
1377        }
1378        append out "    if \{ \[preview element exists \"$label\"\] \} \{\n"
1379        append out "        preview element configure \"$label\""
1380        if { [$_clone element type $elem] != "bar" } {
1381            set options { -symbol -color -dashes -label }
1382        } else {
1383            set options { -color -label }
1384        }
1385        foreach opt $options {
1386            set value [list [$_clone element cget $elem $opt]]
1387            append out " $opt $value"
1388        }
1389        append out "    \}\n"
1390    }
1391   
1392    # Axis settings
1393    foreach axis [$_clone axis names] {
1394        if { [$_clone axis cget $axis -hide] } {
1395            continue
1396        }
1397        append out "    if \{ \[preview axis names \"$axis\"\] == \"$axis\" \} \{\n"
1398        append out "        preview axis configure \"$axis\""
1399        foreach opt { -hide -min -max -loose -title -stepsize -subdivisions } {
1400            set value [list [$_clone axis cget $axis $opt]]
1401            append out " $opt $value"
1402        }
1403        append out " -tickfont \"$axis-ticks\""
1404        append out " -titlefont \"$axis-title\"\n"
1405        set hide [$_clone marker cget ${axis}-zero -hide]
1406        append out "        preview marker configure \"${axis}-zero\" -hide $hide\n"
1407        append out "    \}\n"
1408    }   
1409
1410    append out "    preview grid configure"
1411    append out " -hide \"[$_clone grid cget -hide]\""
1412    append out " -mapx \"[$_clone grid cget -mapx]\""
1413    append out " -mapy \"[$_clone grid cget -mapy]\""
1414    return $out
1415}
Note: See TracBrowser for help on using the repository browser.