source: branches/blt4/gui/scripts/xyprint.tcl @ 1725

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