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

Last change on this file since 1930 was 1930, checked in by gah, 14 years ago
File size: 50.5 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            puts stderr [$_clone $oper configure]
324        }
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    # Grid component
335    CopyOptions {grid configure} $orig $_clone
336
337    # Create markers representing lines at zero for the x and y axis.
338    $_clone marker create line -name x-zero \
339        -coords "0 -Inf 0 Inf" -dashes 1 -hide yes
340    $_clone marker create line -name y-zero \
341        -coords "-Inf 0 Inf 0" -dashes 1 -hide yes
342}
343
344itcl::body Rappture::XyPrint::InitClone {} {
345   
346    $_clone configure -width 3.4i -height 3.4i -background white \
347        -borderwidth 0 \
348        -leftmargin 0 \
349        -rightmargin 0 \
350        -topmargin 0 \
351        -bottommargin 0
352   
353    # Kill the title and create a border around the plot
354    $_clone configure \
355        -title "" \
356        -plotborderwidth 1 -plotrelief solid  \
357        -plotbackground white -plotpadx 0 -plotpady 0
358
359    set _settings($this-layout-width) [Pixels2Inches [$_clone cget -width]]
360    set _settings($this-layout-height) [Pixels2Inches [$_clone cget -height]]
361    set _fonts(legend) [font create legend \
362                            -family helvetica -size 10 -weight normal]
363    update
364    $_clone legend configure \
365        -position right \
366        -font $_fonts(legend) \
367        -hide yes -borderwidth 0 -background white -relief solid \
368        -anchor nw -activeborderwidth 0
369    #
370    foreach axis [$_clone axis names] {
371        if { [$_clone axis cget $axis -hide] } {
372            continue
373        }
374        set _fonts($axis-ticks) [font create $axis-ticks \
375                                     -family helvetica -size 10 \
376                                     -weight normal -slant roman]
377        set _fonts($axis-title) [font create $axis-title \
378                                     -family helvetica -size 10 \
379                                     -weight normal -slant roman]
380        update
381        $_clone axis configure $axis -ticklength 5  \
382            -majorticks {} -minorticks {}
383        $_clone axis configure $axis \
384            -tickfont $_fonts($axis-ticks) \
385            -titlefont $_fonts($axis-title)
386    }
387    foreach elem [$_clone element names] {
388        if { [$_clone element type $elem] == "bar" } {
389            continue
390        }
391        if { [$_clone element cget $elem -linewidth] > 1 } {
392            $_clone element configure $elem -linewidth 1 -pixels 3
393        }
394    }
395}
396
397itcl::body Rappture::XyPrint::SetOption { opt } {
398    set new $_settings($this-graph$opt)
399    set old [$_clone cget $opt]
400    set code [catch [list $_clone configure $opt $new] err]
401    if { $code != 0 } {
402        bell
403        global errorInfo
404        puts stderr "$err: $errorInfo"
405        set _settings($this-graph$opt) $old
406        $_clone configure $opt $old
407    }
408}
409
410itcl::body Rappture::XyPrint::SetComponentOption { comp opt } {
411    set new $_settings($this-$comp$opt)
412    set old [$_clone $comp cget $opt]
413    set code [catch [list $_clone $comp configure $opt $new] err]
414    if { $code != 0 } {
415        bell
416        global errorInfo
417        puts stderr "$err: $errorInfo"
418        set _settings($this-$comp$opt) $old
419        $_clone $comp configure $opt $old
420    }
421}
422
423itcl::body Rappture::XyPrint::SetNamedComponentOption { comp name opt } {
424    set new $_settings($this-$comp$opt)
425    set old [$_clone $comp cget $name $opt]
426    set code [catch [list $_clone $comp configure $name $opt $new] err]
427    if { $code != 0 } {
428        bell
429        global errorInfo
430        puts stderr "$err: $errorInfo"
431        set _settings($this-$comp$opt) $old
432        $_clone $comp configure $name $opt $old
433    }
434}
435
436itcl::body Rappture::XyPrint::RegeneratePreview {} {
437    update
438    set img [image create photo]
439    set w [Inches2Pixels $_settings($this-layout-width) 3.4]
440    set h [Inches2Pixels $_settings($this-layout-height) 3.4]
441    set pixelsPerInch [winfo pixels . 1i]
442    set sx [expr 2.5*$pixelsPerInch/$w]
443    set sy [expr 2.0*$pixelsPerInch/$h]
444    set s [expr min($sx,$sy)]
445    $_clone snap $img -width $w -height $h
446
447    if 0 {
448    if { ![winfo exists .labeltest] } {
449        toplevel .labeltest -bg red
450        label .labeltest.label -image $img
451        pack .labeltest.label -fill both
452    }
453    }
454    set pw [expr int(round($s * $w))]
455    set ph [expr int(round($s * $h))]
456    $_preview configure -width $pw -height $ph
457    #.labeltest.label configure -image $img
458    blt::winop resample $img $_preview box
459    image delete $img
460}
461
462itcl::body Rappture::XyPrint::Pixels2Inches { pixels } {
463    if { [llength $pixels] == 2 } {
464        set pixels [lindex $pixels 0]
465    }
466    set pixelsPerInch [winfo pixels . 1i]
467    set inches [expr { double($pixels) / $pixelsPerInch }]
468    return [format %.3g ${inches}]
469}
470
471itcl::body Rappture::XyPrint::Inches2Pixels { inches {defValue ""}} {
472    set n [scan $inches %g dummy]
473    if { $n != 1  && $defValue != "" } {
474        set inches $defValue
475    }
476    return  [winfo pixels . ${inches}i]
477}
478
479itcl::body Rappture::XyPrint::Color2RGB { color } {
480    foreach { r g b } [winfo rgb $_clone $color] {
481        set r [expr round($r / 257.0)]
482        set g [expr round($g / 257.0)]
483        set b [expr round($b / 257.0)]
484    }
485    return [format "\#%02x%02x%02x" $r $g $b]
486}
487
488itcl::body Rappture::XyPrint::GetAxisType { axis } {
489    foreach type { x y x2 y2 } {
490        set axes [$_clone ${type}axis use]
491        if { [lsearch $axes $axis] >= 0 } {
492            return [string range $type 0 0]
493        }
494    }
495    return ""
496}
497
498itcl::body Rappture::XyPrint::GetAxis {} {
499    set axis [$itk_component(axis_combo) current]
500    foreach option { -min -max -loose -title -stepsize -subdivisions } {
501        set _settings($this-axis$option) [$_clone axis cget $axis $option]
502    }
503    set type [GetAxisType $axis]
504    if { [$_clone grid cget -map${type}] == $axis } {
505        set _settings($this-axis-grid) 1
506    }  else {
507        set _settings($this-axis-grid) 0
508    }
509    set _settings($this-axis-plotpad${type}) \
510        [Pixels2Inches [$_clone cget -plotpad${type}]]
511    set _settings($this-axis-zero) [$_clone marker cget ${type}-zero -hide]
512}
513
514itcl::body Rappture::XyPrint::GetElement { args } {
515    set index 1
516    array unset _settings $this-element-*
517    foreach elem [$_clone element show] {
518        set _settings($this-element-$index) $elem
519        incr index
520    }
521    set index [$itk_component(element_slider) get]
522    set elem $_settings($this-element-$index)
523    set _settings($this-element-label) [$_clone element cget $elem -label]
524    set _settings($this-element-color) [$_clone element cget $elem -color]
525    if { [$_clone element type $elem] != "bar" } {
526        set _settings($this-element-symbol) [$_clone element cget $elem -symbol]
527        set _settings($this-element-dashes) [$_clone element cget $elem -dashes]
528    }
529    set page $itk_component(legend_page)
530    set color [$page.color label $_settings($this-element-color)]
531    if { $color == "" } {
532        set color [Color2RGB $_settings($this-element-color)]
533        $page.color choices insert end $color $color
534        $page.color value $color
535    } else {
536        $page.color value [$page.color label $_settings($this-element-color)]
537    }
538    if { [$_clone element type $elem] != "bar" } {
539        $page.symbol value [$page.symbol label $_settings($this-element-symbol)]
540        $page.dashes value [$page.dashes label $_settings($this-element-dashes)]
541    }
542    #FixElement
543}
544
545itcl::body Rappture::XyPrint::BuildLayoutTab {} {
546    itk_component add layout_page {
547        frame $itk_component(tabs).layout_page
548    }
549    set page $itk_component(layout_page)
550    $itk_component(tabs) insert end "layout" \
551        -text "Layout" -padx 2 -pady 2 -window $page -fill both
552
553    label $page.width_l -text "width" 
554    entry $page.width -width 6 \
555        -textvariable [itcl::scope _settings($this-layout-width)]
556    bind  $page.width <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
557    Rappture::Tooltip::for $page.width \
558        "Set the width (inches) of the output image. Must be a positive number."
559
560    label $page.height_l -text "height"
561    entry $page.height -width 6 \
562        -textvariable [itcl::scope _settings($this-layout-height)]
563    bind  $page.height <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
564    Rappture::Tooltip::for $page.height \
565        "Set the height (inches) of the output image. Must be a positive number"
566
567    label $page.margin_l -text "Margins" 
568
569    label $page.left_l -text "left"
570    entry $page.left -width 6 \
571        -textvariable [itcl::scope _settings($this-layout-leftmargin)]
572    bind  $page.left <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
573    Rappture::Tooltip::for $page.left \
574        "Set the size (inches) of the left margin. If zero, the size is automatically determined."
575
576    label $page.right_l -text "right"
577    entry $page.right -width 6 \
578        -textvariable [itcl::scope _settings($this-layout-rightmargin)]
579    bind  $page.right <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
580    Rappture::Tooltip::for $page.right \
581        "Set the size (inches) of the right margin. If zero, the size is automatically determined."
582
583
584    label $page.top_l -text "top"
585    entry $page.top -width 6 \
586        -textvariable [itcl::scope _settings($this-layout-topmargin)]
587    bind  $page.top <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
588    Rappture::Tooltip::for $page.top \
589        "Set the size (inches) of the top margin. If zero, the size is automatically determined."
590
591    label $page.bottom_l -text "bottom"
592    entry $page.bottom -width 6 \
593        -textvariable [itcl::scope _settings($this-layout-bottommargin)]
594    bind  $page.bottom <KeyPress-Return> [itcl::code $this ApplyLayoutSettings]
595    Rappture::Tooltip::for $page.bottom \
596        "Set the size (inches) of the bottom margin. If zero, the size is automatically determined."
597
598
599    label $page.map -image [Rappture::icon graphmargins]
600    blt::table $page \
601        0,0 $page.width_l -anchor e \
602        0,1 $page.width -fill x  \
603        1,0 $page.height_l -anchor e \
604        1,1 $page.height -fill x \
605        3,0 $page.left_l -anchor e \
606        3,1 $page.left -fill x  \
607        4,0 $page.right_l -anchor e \
608        4,1 $page.right -fill x \
609        5,0 $page.top_l -anchor e \
610        5,1 $page.top -fill x \
611        6,0 $page.bottom_l -anchor e \
612        6,1 $page.bottom -fill x  \
613        0,2 $page.map -fill both -rspan 7 -padx 2
614
615    blt::table configure $page c0 r* -resize none
616    blt::table configure $page c1 r2 -resize both
617}
618
619
620itcl::body Rappture::XyPrint::BuildGeneralTab {} {
621    itk_component add graph_page {
622        frame $itk_component(tabs).graph_page
623    }
624    set page $itk_component(graph_page)
625    $itk_component(tabs) insert end "graph" \
626        -text "General" -padx 2 -pady 2 -window $page -fill both
627
628    label $page.format_l -text "format"
629    Rappture::Combobox $page.format -width 30 -editable no
630    $page.format choices insert end \
631        "pdf" "PDF Portable Document Format" \
632        "ps"  "PS PostScript Format"  \
633        "eps" "EPS Encapsulated PostScript"  \
634        "jpg" "JPEG Joint Photographic Experts Group Format" \
635        "png" "PNG Portable Network Graphics Format"         
636
637    bind $page.format <<Value>> [itcl::code $this ApplyGeneralSettings]
638    Rappture::Tooltip::for $page.format \
639        "Set the format of the image."
640
641    label $page.style_l -text "style"
642    Rappture::Combobox $page.style -width 20 -editable no
643    $page.style choices insert end \
644        "ieee" "IEEE Journal"  \
645        "gekco" "G Klimeck" 
646    bind $page.style <<Value>> [itcl::code $this ApplyGeneralSettings]
647    Rappture::Tooltip::for $page.format \
648        "Set the style template."
649
650    checkbutton $page.remember -text "remember settings" \
651        -variable [itcl::scope _settings($this-general-remember)] 
652    Rappture::Tooltip::for $page.remember \
653        "Remember the settings. They will be override the default settings."
654
655    button $page.revert -text "revert" -image [Rappture::icon revert] \
656        -compound left -padx 3 -pady 1 -overrelief raised -relief flat \
657        -command [itcl::code $this ResetSettings]
658    Rappture::Tooltip::for $page.revert \
659        "Revert to the default settings."
660
661    blt::table $page \
662        2,0 $page.format_l -anchor e \
663        2,1 $page.format -fill x -cspan 2 \
664        3,0 $page.style_l -anchor e \
665        3,1 $page.style -fill x -cspan 2 \
666        5,0 $page.remember -cspan 2 -anchor w \
667        5,2 $page.revert -anchor e
668    blt::table configure $page r* -resize none  -pady { 0 2 }
669    blt::table configure $page r4 -resize both
670
671}
672   
673itcl::body Rappture::XyPrint::ApplyLegendSettings {} {
674    set page $itk_component(legend_page)
675    set _settings($this-legend-anchor)    [$page.anchor current]
676    if { $_clone != "" } {
677        font configure $_fonts(legend) \
678            -family $_settings($this-legend-font-family) \
679            -size $_settings($this-legend-font-size) \
680            -weight $_settings($this-legend-font-weight) \
681            -slant $_settings($this-legend-font-slant)
682        foreach option { -hide -position -anchor -borderwidth } {
683            SetComponentOption legend $option
684        }
685        $_clone legend configure -font fixed -font $_fonts(legend)
686    }
687    ApplyElementSettings
688}
689
690itcl::body Rappture::XyPrint::BuildLegendTab {} {
691    itk_component add legend_page {
692        frame $itk_component(tabs).legend_page
693    }
694    set page $itk_component(legend_page)
695    $itk_component(tabs) insert end "legend" \
696        -text "Legend" -padx 2 -pady 2 -window $page -fill both
697
698    checkbutton $page.show -text "show legend" \
699        -offvalue 1 -onvalue 0 \
700        -variable [itcl::scope _settings($this-legend-hide)]  \
701        -command [itcl::code $this ApplyLegendSettings]
702    Rappture::Tooltip::for $page.show \
703        "Display the legend."
704
705    label $page.position_l -text "position"
706    Rappture::Combobox $page.position -width 15 -editable no
707    $page.position choices insert end \
708        "leftmargin" "left margin"  \
709        "rightmargin" "right margin"  \
710        "bottommargin" "bottom margin"  \
711        "topmargin" "top margin"  \
712        "plotarea" "inside plot"
713    bind $page.position <<Value>> [itcl::code $this ApplyLegendSettings]
714    Rappture::Tooltip::for $page.position \
715        "Set the position of the legend.  This option and the anchor determine the legend's location."
716
717    Rappture::Combobox $page.anchor -width 10 -editable no
718    $page.anchor choices insert end \
719        "nw" "northwest"  \
720        "n" "north"  \
721        "ne" "northeast"  \
722        "sw" "southwest"  \
723        "s" "south"  \
724        "se" "southeast"  \
725        "c" "center"  \
726        "e" "east"  \
727        "w" "west" 
728    bind $page.anchor <<Value>> [itcl::code $this ApplyLegendSettings]
729    Rappture::Tooltip::for $page.anchor \
730        "Set the anchor of the legend.  This option and the anchor determine the legend's location."
731
732    checkbutton $page.border -text "border" \
733        -variable [itcl::scope _settings($this-legend-borderwidth)] \
734        -onvalue 1 -offvalue 0 \
735        -command [itcl::code $this ApplyLegendSettings]
736    Rappture::Tooltip::for $page.border \
737        "Display a solid border around the legend."
738
739    label $page.slider_l -text "legend\nentry"  -justify right
740    itk_component add element_slider {
741        ::scale $page.slider -from 1 -to 1 \
742            -orient horizontal -width 12 \
743            -command [itcl::code $this GetElement]
744    }
745    Rappture::Tooltip::for $page.slider \
746        "Select the current entry."
747
748    label $page.label_l -text "label"
749    entry $page.label \
750        -background white \
751        -textvariable [itcl::scope _settings($this-element-label)]
752    bind  $page.label <KeyPress-Return> [itcl::code $this ApplyElementSettings]
753    Rappture::Tooltip::for $page.label \
754        "Set the label of the current entry in the legend."
755
756    label $page.color_l -text "color "
757    Rappture::Combobox $page.color -width 15 -editable no
758    $page.color choices insert end \
759        "#000000" "black" \
760        "#ffffff" "white" \
761        "#0000cd" "blue" \
762        "#cd0000" "red" \
763        "#00cd00" "green" \
764        "#3a5fcd" "royal blue" \
765        "#cdcd00" "yellow" \
766        "#cd1076" "deep pink" \
767        "#009acd" "deep sky blue" \
768        "#00c5cd" "torquise" \
769        "#a2b5cd" "light steel blue" \
770        "#7ac5cd" "cadet blue" \
771        "#66cdaa" "aquamarine" \
772        "#a2cd5a" "dark olive green" \
773        "#cd9b9b" "rosy brown" \
774        "#0000ff" "blue1" \
775        "#ff0000" "red1" \
776        "#00ff00" "green1"
777    bind $page.color <<Value>> [itcl::code $this ApplyElementSettings]
778    Rappture::Tooltip::for $page.color \
779        "Set the color of the current entry."
780
781    label $page.dashes_l -text "line style"
782    Rappture::Combobox $page.dashes -width 15 -editable no
783    $page.dashes choices insert end \
784        "" "solid"  \
785        "1" "dot" \
786        "5 2" "dash" \
787        "2 4 2" "dashdot" \
788        "2 4 2 2" "dashdotdot"
789    bind $page.dashes <<Value>> [itcl::code $this ApplyElementSettings]
790    Rappture::Tooltip::for $page.dashes \
791        "Set the line style of the current entry."
792
793    label $page.symbol_l -text "symbol"
794    Rappture::Combobox $page.symbol -editable no
795    $page.symbol choices insert end \
796        "none" "none"  \
797        "square" "square" \
798        "circle" "circle" \
799        "diamond" "diamond" \
800        "plus" "plus" \
801        "cross" "cross" \
802        "splus" "skinny plus"  \
803        "scross" "skinny cross" \
804        "triangle" "triangle"
805    bind $page.symbol <<Value>> [itcl::code $this ApplyElementSettings]
806    Rappture::Tooltip::for $page.symbol \
807        "Set the symbol of the current entry. \"none\" display no symbols."
808
809    label $page.font_l -text "font"
810    Rappture::Combobox $page.fontfamily -width 10 -editable no
811    $page.fontfamily choices insert end \
812        "courier" "courier" \
813        "helvetica" "helvetica"  \
814        "new*century*schoolbook"  "new century schoolbook" \
815        "symbol"  "symbol" \
816        "times"  "times"         
817    bind $page.fontfamily <<Value>> [itcl::code $this ApplyLegendSettings]
818    Rappture::Tooltip::for $page.fontfamily \
819        "Set the font of entries in the legend."
820
821    Rappture::Combobox $page.fontsize -width 4 -editable no
822    $page.fontsize choices insert end \
823        "8" "8" \
824        "9" "9" \
825        "10" "10" \
826        "11" "11" \
827        "12" "12" \
828        "14" "14" \
829        "17" "17" \
830        "18" "18" \
831        "20" "20"
832    bind  $page.fontsize <<Value>> [itcl::code $this ApplyLegendSettings]
833    Rappture::Tooltip::for $page.fontsize \
834        "Set the size (points) of the font."
835
836    Rappture::PushButton $page.fontweight \
837        -width 18 -height 18 \
838        -onimage [Rappture::icon font-bold] \
839        -offimage [Rappture::icon font-bold] \
840        -onvalue "bold" -offvalue "normal" \
841        -command [itcl::code $this ApplyLegendSettings] \
842        -variable [itcl::scope _settings($this-legend-font-weight)]
843    Rappture::Tooltip::for $page.fontweight \
844        "Use the bold version of the font."
845
846    Rappture::PushButton $page.fontslant \
847        -width 18 -height 18 \
848        -onimage [Rappture::icon font-italic] \
849        -offimage [Rappture::icon font-italic] \
850        -onvalue "italic" -offvalue "roman" \
851        -command [itcl::code $this ApplyLegendSettings] \
852        -variable [itcl::scope _settings($this-legend-font-slant)]
853    Rappture::Tooltip::for $page.fontslant \
854        "Use the italic version of the font."
855
856    blt::table $page \
857        1,0 $page.show -cspan 2 -anchor w \
858        1,2 $page.border -cspan 2 -anchor w \
859        2,0 $page.position_l -anchor e \
860        2,1 $page.position -fill x \
861        2,2 $page.anchor -fill x  -cspan 3 \
862        3,0 $page.font_l -anchor e \
863        3,1 $page.fontfamily -fill x \
864        3,2 $page.fontsize -fill x \
865        3,3 $page.fontweight -anchor e \
866        3,4 $page.fontslant -anchor e \
867        4,0 $page.slider_l -anchor e \
868        4,1 $page.slider -fill x -cspan 5 \
869        5,0 $page.label_l -anchor e \
870        5,1 $page.label -fill x -cspan 5 \
871        6,0 $page.color_l -anchor e \
872        6,1 $page.color -fill x \
873        6,2 $page.symbol_l -anchor e \
874        6,3 $page.symbol -fill both -cspan 3 \
875        7,0 $page.dashes_l -anchor e \
876        7,1 $page.dashes -fill x \
877
878    blt::table configure $page r* -resize none -pady { 0 2 }
879    blt::table configure $page r8 -resize both
880
881}
882
883itcl::body Rappture::XyPrint::BuildAxisTab {} {
884    itk_component add axis_page {
885        frame $itk_component(tabs).axis_page
886    }
887    set page $itk_component(axis_page)
888    $itk_component(tabs) insert end "axis" \
889        -text "Axis" -padx 2 -pady 2 -window $page -fill both
890   
891    label $page.axis_l -text "axis"
892    itk_component add axis_combo {
893        Rappture::Combobox $page.axis -width 20 -editable no
894    }
895    bind $itk_component(axis_combo) <<Value>> [itcl::code $this GetAxis]
896    Rappture::Tooltip::for $page.axis \
897        "Select the current axis."
898
899    label $page.title_l -text "title"
900    entry $page.title \
901        -textvariable [itcl::scope _settings($this-axis-title)]
902    bind  $page.title <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
903    Rappture::Tooltip::for $page.title \
904        "Set the title of the current axis."
905
906    label $page.min_l -text "min"
907    entry $page.min -width 10 \
908        -textvariable [itcl::scope _settings($this-axis-min)]
909    bind  $page.min <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
910    Rappture::Tooltip::for $page.min \
911        "Set the minimum limit for the current axis. If empty, the minimum is automatically determined."
912
913    label $page.max_l -text "max"
914    entry $page.max -width 10 \
915        -textvariable [itcl::scope _settings($this-axis-max)]
916    bind  $page.max <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
917    Rappture::Tooltip::for $page.max \
918        "Set the maximum limit for the current axis. If empty, the maximum is automatically determined."
919
920    label $page.subdivisions_l -text "subdivisions"
921    entry $page.subdivisions \
922        -textvariable [itcl::scope _settings($this-axis-subdivisions)]
923    bind  $page.subdivisions <KeyPress-Return> \
924        [itcl::code $this ApplyAxisSettings]
925    Rappture::Tooltip::for $page.subdivisions \
926        "Set the number of subdivisions (minor ticks) for the current axis."
927
928    label $page.stepsize_l -text "step size"
929    entry $page.stepsize \
930        -textvariable [itcl::scope _settings($this-axis-stepsize)]
931    bind  $page.stepsize <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
932    Rappture::Tooltip::for $page.stepsize \
933        "Set the interval between major ticks for the current axis. If zero, the interval is automatically determined."
934
935    checkbutton $page.loose -text "loose limits" \
936        -onvalue "always" -offvalue "0" \
937        -variable [itcl::scope _settings($this-axis-loose)] \
938        -command [itcl::code $this ApplyAxisSettings]
939    Rappture::Tooltip::for $page.loose \
940        "Set major ticks outside of the limits for the current axis."
941
942    label $page.plotpad_l -text "pad" 
943    entry $page.plotpad -width 6 \
944        -textvariable [itcl::scope _settings($this-axis-plotpad)]
945    bind  $page.plotpad <KeyPress-Return> [itcl::code $this ApplyAxisSettings]
946    Rappture::Tooltip::for $page.plotpad \
947        "Set padding (points) between the current axis and the plot."
948
949    checkbutton $page.grid -text "show grid lines" \
950        -variable [itcl::scope _settings($this-axis-grid)] \
951        -command [itcl::code $this ApplyAxisSettings]
952    Rappture::Tooltip::for $page.grid \
953        "Display grid lines for the current axis."
954
955    checkbutton $page.zero -text "mark zero" \
956        -offvalue 1 -onvalue 0 \
957        -variable [itcl::scope _settings($this-axis-zero)] \
958        -command [itcl::code $this ApplyAxisSettings]
959    Rappture::Tooltip::for $page.zero \
960        "Display a line at zero for the current axis."
961
962    label $page.tickfont_l -text "tick font"
963    Rappture::Combobox $page.tickfontfamily -width 10 -editable no
964    $page.tickfontfamily choices insert end \
965        "courier" "courier" \
966        "helvetica" "helvetica"  \
967        "new*century*schoolbook"  "new century schoolbook" \
968        "symbol"  "symbol" \
969        "times"  "times"         
970    bind $page.tickfontfamily <<Value>> [itcl::code $this ApplyAxisSettings]
971    Rappture::Tooltip::for $page.tickfontfamily \
972        "Set the font of the ticks for the current axis."
973
974    Rappture::Combobox $page.tickfontsize -width 4 -editable no
975    $page.tickfontsize choices insert end \
976        "8" "8" \
977        "9" "9" \
978        "10" "10" \
979        "11" "11" \
980        "12" "12" \
981        "14" "14" \
982        "17" "17" \
983        "18" "18" \
984        "20" "20"
985    bind $page.tickfontsize <<Value>> [itcl::code $this ApplyAxisSettings]
986    Rappture::Tooltip::for $page.tickfontsize \
987        "Set the size (points) of the tick font."
988
989    Rappture::PushButton $page.tickfontweight \
990        -width 18 -height 18 \
991        -onimage [Rappture::icon font-bold] \
992        -offimage [Rappture::icon font-bold] \
993        -onvalue "bold" -offvalue "normal" \
994        -command [itcl::code $this ApplyAxisSettings] \
995        -variable [itcl::scope _settings($this-axis-tickfont-weight)]
996    Rappture::Tooltip::for $page.tickfontweight \
997        "Use the bold version of the tick font."
998
999    Rappture::PushButton $page.tickfontslant \
1000        -width 18 -height 18 \
1001        -onimage [Rappture::icon font-italic] \
1002        -offimage [Rappture::icon font-italic] \
1003        -onvalue "italic" -offvalue "roman" \
1004        -command [itcl::code $this ApplyAxisSettings] \
1005        -variable [itcl::scope _settings($this-axis-tickfont-slant)]
1006    Rappture::Tooltip::for $page.tickfontslant \
1007        "Use the italic version of the tick font."
1008
1009    label $page.titlefont_l -text "title font"
1010    Rappture::Combobox $page.titlefontfamily -width 10 -editable no
1011    $page.titlefontfamily choices insert end \
1012        "courier" "courier" \
1013        "helvetica" "helvetica"  \
1014        "new*century*schoolbook"  "new century schoolbook" \
1015        "symbol"  "symbol" \
1016        "times"  "times"         
1017    bind $page.titlefontfamily <<Value>> [itcl::code $this ApplyAxisSettings]
1018    Rappture::Tooltip::for $page.titlefontfamily \
1019        "Set the font of the title for the current axis."
1020
1021    Rappture::Combobox $page.titlefontsize -width 4 -editable no
1022    $page.titlefontsize choices insert end \
1023        "8" "8" \
1024        "9" "9" \
1025        "10" "10" \
1026        "11" "11" \
1027        "12" "12" \
1028        "14" "14" \
1029        "17" "17" \
1030        "18" "18" \
1031        "20" "20"
1032    bind $page.titlefontsize <<Value>> [itcl::code $this ApplyAxisSettings]
1033    Rappture::Tooltip::for $page.titlefontsize \
1034        "Set the size (point) of the title font."
1035
1036    Rappture::PushButton $page.titlefontweight \
1037        -width 18 -height 18 \
1038        -onimage [Rappture::icon font-bold] \
1039        -offimage [Rappture::icon font-bold] \
1040        -onvalue "bold" -offvalue "normal" \
1041        -command [itcl::code $this ApplyAxisSettings] \
1042        -variable [itcl::scope _settings($this-axis-titlefont-weight)]
1043    Rappture::Tooltip::for $page.titlefontweight \
1044        "Use the bold version of the title font."
1045
1046    Rappture::PushButton $page.titlefontslant \
1047        -width 18 -height 18 \
1048        -onimage [Rappture::icon font-italic] \
1049        -offimage [Rappture::icon font-italic] \
1050        -onvalue "italic" -offvalue "roman" \
1051        -command [itcl::code $this ApplyAxisSettings] \
1052        -variable [itcl::scope _settings($this-axis-titlefont-slant)]
1053    Rappture::Tooltip::for $page.titlefontslant \
1054        "Use the italic version of the title font."
1055
1056    blt::table $page \
1057        1,1 $page.axis_l -anchor e  -pady 6 \
1058        1,2 $page.axis -fill x -cspan 6 \
1059        2,1 $page.title_l -anchor e \
1060        2,2 $page.title -fill x -cspan 5 \
1061        3,1 $page.min_l -anchor e \
1062        3,2 $page.min -fill x \
1063        3,3 $page.stepsize_l -anchor e \
1064        3,4 $page.stepsize -fill both -cspan 3 \
1065        4,1 $page.max_l -anchor e \
1066        4,2 $page.max -fill both \
1067        4,3 $page.subdivisions_l -anchor e \
1068        4,4 $page.subdivisions -fill both -cspan 3  \
1069        5,1 $page.titlefont_l -anchor e \
1070        5,2 $page.titlefontfamily -fill x -cspan 2 \
1071        5,4 $page.titlefontsize -fill x \
1072        5,5 $page.titlefontweight -anchor e \
1073        5,6 $page.titlefontslant -anchor e \
1074        6,1 $page.tickfont_l -anchor e \
1075        6,2 $page.tickfontfamily -fill x -cspan 2 \
1076        6,4 $page.tickfontsize -fill x \
1077        6,5 $page.tickfontweight -anchor e \
1078        6,6 $page.tickfontslant -anchor e \
1079        7,1 $page.loose -cspan 2 -anchor w \
1080        7,3 $page.grid -anchor w -cspan 2 \
1081        8,1 $page.zero -cspan 2 -anchor w \
1082        8,3 $page.plotpad_l -anchor e \
1083        8,4 $page.plotpad -fill both -cspan 3
1084
1085    blt::table configure $page  c0 c7 c8 -resize none
1086}
1087
1088itcl::body Rappture::XyPrint::ApplyGeneralSettings {} {
1089    RegeneratePreview
1090}
1091
1092itcl::body Rappture::XyPrint::ApplyLegendSettings {} {
1093    set page $itk_component(legend_page)
1094    set _settings($this-legend-position)  [$page.position current]
1095    set _settings($this-legend-anchor)    [$page.anchor current]
1096
1097    foreach option { -hide -position -anchor -borderwidth } {
1098        SetComponentOption legend $option
1099    }
1100    font configure $_fonts(legend) \
1101        -family [$page.fontfamily current] \
1102        -size [$page.fontsize current] \
1103        -weight $_settings($this-legend-font-weight) \
1104        -slant $_settings($this-legend-font-slant)
1105    $_clone legend configure -font fixed -font $_fonts(legend)
1106    ApplyElementSettings
1107}
1108
1109itcl::body Rappture::XyPrint::ApplyAxisSettings {} {
1110    set axis [$itk_component(axis_combo) current]
1111    set type [GetAxisType $axis]
1112    set page $itk_component(axis_page)
1113    if { $_settings($this-axis-grid) } {
1114        $_clone grid configure -hide no -map${type} ${axis}
1115    } else {
1116        $_clone grid configure -hide no -map${type} ""
1117    }
1118    $_clone configure -plotpad${type} $_settings($this-axis-plotpad)
1119    foreach option { -min -max -loose -title -stepsize -subdivisions } {
1120        SetNamedComponentOption axis $axis $option
1121    }
1122    $_clone marker configure ${type}-zero -hide $_settings($this-axis-zero)
1123    font configure $axis-title \
1124        -family [$page.titlefontfamily current] \
1125        -size   [$page.titlefontsize current] \
1126        -weight $_settings($this-axis-titlefont-weight) \
1127        -slant  $_settings($this-axis-titlefont-slant)
1128    font configure $axis-ticks \
1129        -family [$page.tickfontfamily current] \
1130        -size   [$page.tickfontsize current] \
1131        -weight $_settings($this-axis-tickfont-weight) \
1132        -slant  $_settings($this-axis-tickfont-slant)
1133    $_clone axis configure $axis -tickfont $axis-ticks -titlefont $axis-title
1134    GetAxis
1135    RegeneratePreview
1136}
1137
1138itcl::body Rappture::XyPrint::ApplyElementSettings {} {
1139    set index [$itk_component(element_slider) get]
1140    set page $itk_component(legend_page)
1141    set _settings($this-element-color)  [$page.color current]
1142    if { $_clone != "" } {
1143        set elem $_settings($this-element-$index)
1144        if { [$_clone element type $elem] != "bar" } {
1145            set _settings($this-element-symbol) [$page.symbol current]
1146            set _settings($this-element-dashes) [$page.dashes current]
1147            foreach option { -symbol -dashes } {
1148                SetNamedComponentOption element $elem $option
1149            }
1150        }
1151        foreach option { -color -label } {
1152            SetNamedComponentOption element $elem $option
1153        }
1154        RegeneratePreview
1155    }
1156}
1157
1158itcl::body Rappture::XyPrint::SetLayoutOption { opt } {
1159    set new [Inches2Pixels $_settings($this-layout$opt)]
1160    $_clone configure $opt $new
1161}
1162
1163itcl::body Rappture::XyPrint::ApplyLayoutSettings {} {
1164    foreach opt { -width -height -leftmargin -rightmargin -topmargin
1165        -bottommargin } {
1166        set old [$_clone cget $opt]
1167        set code [catch { SetLayoutOption $opt } err]
1168        if { $code != 0 } {
1169            bell
1170            global errorInfo
1171            puts stderr "$err: $errorInfo"
1172            set _settings($this-layout$opt) [Pixels2Inches $old]
1173            $_clone configure $opt [Pixels2Inches $old]
1174        }
1175    }
1176    RegeneratePreview
1177}
1178
1179
1180itcl::body Rappture::XyPrint::InitializeSettings {} {
1181    # General settings
1182
1183    # Always set to "ps" "ieee"
1184    set _settings($this-general-format) ps
1185    set _settings($this-general-style) ieee
1186    set _settings($this-general-remember) 0
1187    set page $itk_component(graph_page)
1188
1189    # Layout settings
1190    set _settings($this-layout-width) [Pixels2Inches [$_clone cget -width]]
1191    set _settings($this-layout-height) [Pixels2Inches [$_clone cget -height]]
1192    set _settings($this-layout-leftmargin) \
1193        [Pixels2Inches [$_clone cget -leftmargin]]
1194    set _settings($this-layout-rightmargin) \
1195        [Pixels2Inches [$_clone cget -rightmargin]]
1196    set _settings($this-layout-topmargin) \
1197        [Pixels2Inches [$_clone cget -topmargin]]
1198    set _settings($this-layout-bottommargin) \
1199        [Pixels2Inches [$_clone cget -bottommargin]]
1200
1201    # Legend settings
1202    set page $itk_component(legend_page)
1203
1204    set names [$_clone element show]
1205    $itk_component(element_slider) configure -from 1 -to [llength $names]
1206    set state [expr  { ([llength $names] < 2) ? "disabled" : "normal" } ]
1207    $page.slider configure -state $state
1208    $page.slider_l configure -state $state
1209    # Always initialize the slider to the first entry in the element list.
1210    $itk_component(element_slider) set 1
1211    # Always set the borderwidth to be not displayed
1212    set _settings($this-legend-borderwidth) 0
1213
1214    array unset info
1215    array set info [font configure legend]
1216    $page.fontfamily value $info(-family)
1217    $page.fontsize value $info(-size)
1218    set _settings($this-legend-font-weight) $info(-weight)
1219    set _settings($this-legend-font-slant) $info(-slant)
1220    if { $info(-weight) == "bold" } {
1221        set _settings($this-legend-font-bold) 1
1222    }
1223    set _settings($this-legend-hide) [$_clone legend cget -hide]
1224    set _settings($this-legend-position) [$_clone legend cget -position]
1225    set _settings($this-legend-anchor) [$_clone legend cget -anchor]
1226    $page.position value \
1227        [$page.position label $_settings($this-legend-position)]
1228    $page.anchor value [$page.anchor label $_settings($this-legend-anchor)]
1229    GetElement
1230
1231    # Axis settings
1232    set page $itk_component(axis_page)
1233    set names [lsort [$_clone axis names]]
1234    $itk_component(axis_combo) choices delete 0 end
1235    foreach axis $names {
1236        if { ![$_clone axis cget $axis -hide] } {
1237            $itk_component(axis_combo) choices insert end $axis $axis
1238        }
1239        lappend axisnames $axis
1240    }
1241    set axis [lindex $names 0]
1242
1243    array set info [font configure $axis-title]
1244    $page.titlefontfamily value $info(-family)
1245    $page.titlefontsize value $info(-size)
1246    set _settings($this-axis-titlefont-weight) $info(-weight)
1247    set _settings($this-axis-titlefont-slant) $info(-slant)
1248
1249    array set info [font configure $axis-ticks]
1250    $page.tickfontfamily value $info(-family)
1251    $page.tickfontsize value $info(-size)
1252    set _settings($this-axis-tickfont-weight) $info(-weight)
1253    set _settings($this-axis-tickfont-slant) $info(-slant)
1254
1255    # Always hide the zero line.
1256    set _settings($this-axis-zero) 1
1257    set _settings($this-axis-plotpad) [Pixels2Inches [$_clone cget -plotpadx]]
1258    # Pick the first axis to initially display
1259    set axis [lindex $axisnames 0]
1260    $itk_component(axis_combo) value $axis
1261    GetAxis
1262}
1263
1264
1265itcl::body Rappture::XyPrint::restore { toolName plotName data } {
1266    set key [list $toolName $plotName]
1267    set _savedSettings($key) $data
1268}
1269
1270itcl::body Rappture::XyPrint::RestoreSettings { toolName plotName } {
1271    if { ![file readable $_settingsFile] } {
1272        return;                         # No file or not readable
1273    }
1274    if { [file exists $_oldSettingsFile] } {
1275        file delete $_oldSettingsFile
1276    }
1277    # Read the file by sourcing it into a safe interpreter The only commands
1278    # executed will be "xyprint" which will simply add the data into an array
1279    # _savedSettings.
1280    set parser [interp create -safe]
1281    $parser alias xyprint [itcl::code $this restore]
1282    $parser alias font font
1283    set f [open $_settingsFile "r"]
1284    set code [read $f]
1285    close $f
1286    $parser eval $code
1287   
1288    # Now see if there's an entry for this tool/plot combination.  The data
1289    # associated with the variable is itself code to update the graph (clone).
1290    set key [list $toolName $plotName]
1291    if { [info exists _savedSettings($key)] }  {
1292        $parser alias "preview" $_clone
1293        $parser eval $_savedSettings($key)
1294    }
1295    foreach {name value} [$parser eval "array get general"] {
1296        set _settings($this-graph-$name) $value
1297    }
1298    interp delete $parser
1299}
1300
1301itcl::body Rappture::XyPrint::ResetSettings {} {
1302    set graph $_graph
1303    DestroySettings
1304    set _graph $graph
1305    CloneGraph $graph
1306    InitClone
1307    InitializeSettings
1308}
1309
1310itcl::body Rappture::XyPrint::SaveSettings { toolName plotName } {
1311    if { !$_settings($this-general-remember) } {
1312        return
1313    }
1314    if { [catch { open $_settingsFile "w" 0600 } f ] != 0 } {
1315        puts stderr "$_settingsFile isn't writable: $f"
1316        bell
1317        return
1318    }
1319    set key [list $toolName $plotName]
1320    set _savedSettings($key) [CreateSettings $toolName $plotName]
1321    # Write the settings out
1322    foreach key [lsort [array names _savedSettings]] {
1323        set tool [lindex $key 0]
1324        set plot [lindex $key 1]
1325        puts $f "xyprint \"$tool\" \"$plot\" \{"
1326        set settings [string trim $_savedSettings($key) \n]
1327        puts $f "$settings"
1328        puts $f "\}\n"
1329    }
1330    close $f
1331}
1332
1333itcl::body Rappture::XyPrint::CreateSettings { toolName plotName } {
1334    # Create stanza associated with tool and plot title.
1335    # General settings
1336    append out "    set general(format) $_settings($this-general-format)\n"
1337    append out "    set general(style) $_settings($this-general-style)\n"
1338
1339    foreach font [array names _fonts] {
1340        append out "    font configure $font"
1341        array unset info
1342        array set info [font configure $font]
1343        foreach opt { -family -size -weight -slant } {
1344            set value [list $info($opt)]
1345            append out " $opt $value"
1346        }
1347        append out "\n"
1348    }
1349    append out "\n"
1350
1351    # Layout settings
1352    append out "    preview configure"
1353    foreach opt { -width -height -leftmargin -rightmargin -topmargin
1354        -bottommargin -plotpadx -plotpady } {
1355        set value [list [$_clone cget $opt]]
1356        append out " $opt $value"
1357    }
1358    append out "\n"
1359
1360    # Legend settings
1361    append out "    preview legend configure"
1362    foreach opt { -position -anchor -borderwidth -hide } {
1363        set value [list [$_clone legend cget $opt]]
1364        append out " $opt $value"
1365    }
1366    append out " -font legend\n"
1367
1368    # Element settings
1369    foreach elem [$_clone element show] {
1370        set label [$_clone element cget $elem -label]
1371        if { $label == "" } {
1372            continue
1373        }
1374        append out "    if \{ \[preview element exists \"$label\"\] \} \{\n"
1375        append out "        preview element configure \"$label\""
1376        if { [$_clone element type $elem] != "bar" } {
1377            set options { -symbol -color -dashes -label }
1378        } else {
1379            set options { -color -label }
1380        }
1381        foreach opt $options {
1382            set value [list [$_clone element cget $elem $opt]]
1383            append out " $opt $value"
1384        }
1385        append out "    \}\n"
1386    }
1387   
1388    # Axis settings
1389    foreach axis [$_clone axis names] {
1390        if { [$_clone axis cget $axis -hide] } {
1391            continue
1392        }
1393        append out "    if \{ \[preview axis names \"$axis\"\] == \"$axis\" \} \{\n"
1394        append out "        preview axis configure \"$axis\""
1395        foreach opt { -hide -min -max -loose -title -stepsize -subdivisions } {
1396            set value [list [$_clone axis cget $axis $opt]]
1397            append out " $opt $value"
1398        }
1399        append out " -tickfont \"$axis-ticks\""
1400        append out " -titlefont \"$axis-title\"\n"
1401        set hide [$_clone marker cget ${axis}-zero -hide]
1402        append out "        preview marker configure \"${axis}-zero\" -hide $hide\n"
1403        append out "    \}\n"
1404    }   
1405
1406    append out "    preview grid configure"
1407    append out " -hide \"[$_clone grid cget -hide]\""
1408    append out " -mapx \"[$_clone grid cget -mapx]\""
1409    append out " -mapy \"[$_clone grid cget -mapy]\""
1410    return $out
1411}
Note: See TracBrowser for help on using the repository browser.