Merge branch 'master' into bug-4403-remove-polyfill
[maxima.git] / interfaces / xmaxima / Tkmaxima / EOpenplot.tcl
blobfbf5e7fb3032991bea3332f7352fec0c3c37d92f
1 ###### EOpenplot.tcl ############################################
3 # Copyright (C) 1998 William F. Schelter
4 # For distribution under GNU public License. See COPYING.tcl
6 # Time-stamp: "2024-03-20 15:13:35 villate"
8 #################################################################
10 #-----------------------------------------------------------------
12 # eval_openplot -- invoke OPENPLOT on the substring of Window given
13 # by thisRange, and substitute the result into resultRange, if the
14 # latter is not the empty list. If it is, then the window is placed
15 # on the next line from this command.
16 # Results:
18 # Side Effects:
20 #----------------------------------------------------------------
23 proc eval_openplot { program w thisRange resultRange } {
24 set tem [eval $w get $thisRange]
25 lappend tem -windowname $name
26 foreach v [getDimensions $w $name] { lappend tem $v }
27 set allowed "plot2d plotdf plot3d scene"
28 set f [lindex $tem 0]
29 if { [lsearch $allowed $f] >= 0 } {
30 $f {*}[lrange $tem 1 end]
31 ShowPlotWindow $w $name $thisRange $resultRange $desired
32 } else {
33 error [concat "$f" [mc "not allowed, only"] "{$allowed}"]}
34 set name [plotWindowName $w $f]
35 set desired [setDesiredDims $w $name $thisRange ]
36 return 0}
38 #-----------------------------------------------------------------
40 # plotWindowName -- checks preferences to see if separate or multiple
41 # or nontoplevel windows are desired, and chooses a name accordingly.
42 # in the first two cases it also assures that the toplevel window exists.
44 # Results: window name
46 # Side Effects: possibly make a new toplevel window.
48 #----------------------------------------------------------------
50 proc plotWindowName { w command } {
51 upvar #0 maxima_default(plotwindow) plot
52 upvar #0 maxima_priv(plot,count) count
53 set name ""
55 if { "$command" == "scene" } {
56 set name ".plotfr"
57 if { [winfo exists $name ] } {
58 after cancel PlayStep $name.plot.menubar.play
59 vtkCommand DeleteAllObjects
60 destroy $name
61 clearLocal $name.plot
63 toplevel $name
64 if { "[info proc setIcon]" != "" } {
65 after 1000 setIcon $name
67 } else {
68 if { ![info exists plot] || "$plot" == "embedded" } {
69 linkLocal $w counter
70 if { ![info exists counter] } {set counter 0}
71 return $w.plot[incr counter]
73 set name ".plotfr"
74 if { "$plot" == "multiple" } {
75 if { ![info exists count] } {
76 set count 1
77 } else {
78 incr count
80 append name $count
82 if { ![winfo exists $name ] } {
83 toplevel $name
84 set h [expr {round ([winfo screenheight $name]*.6) }]
85 set wid [expr round ($h * 1.2) ]
86 set r1 [expr {round(10+rand()*30)} ]
87 set r2 [expr {round(10+rand()*30)} ]
88 wm geometry $name ${wid}x${h}+${r1}+${r2}
89 if { "[info proc setIcon]" != "" } {
90 after 1000 setIcon $name
94 append name .plot
95 return $name
99 proc whereToPutPlot { w thisRange resultRange } {
100 if { "$resultRange" != "" } {
101 eval $w delete $resultRange
102 set at [lindex $resultRange 0]
103 $w insert $at " " { Tresult}
104 set at [$w index "$at + 1char"]
105 } else {
106 set at "[lindex $thisRange 1] lineend + 1 chars"
108 return $at
112 proc setDesiredDims { w name range } {
113 #puts "setDesiredDims $w $name $range"
114 foreach v [getTagsMatching $w "^(width|height):" $range] {
115 set tem [split $v :]
116 lappend ans [lindex $tem 0]Desired [lindex $tem 1]
118 if { [info exists ans] } {
119 oarraySet $name $ans
120 return $ans
122 return ""
125 proc getDimensions { w name } {
126 # puts "getDimensions $w $name"
127 set parent [winfo parent $w]
128 set scrollwidth 15
129 catch { set scrollwidth [ [winfo parent $parent].scroll cget -scrollwidth] }
130 set width [winfo width $w]
131 set height [winfo height $w]
132 #set width [getPercentDim [oget $name widthDesired] width $w]
133 catch {set width [getPercentDim [oget $name widthDesired] width $w] }
134 catch {set height [getPercentDim [oget $name heightDesired] height $w] }
136 set width [expr {round ($width-4) }]
137 set height [expr {round ($height-4)}]
138 #puts "using width,height=$width,$height"
140 if { $width <0 } {
141 set width [expr {[oget $parent width] - 2*$scrollwidth}]
142 set height [expr {round(.85*[oget $parent height])}]
144 return " -width $width -height $height"
148 proc insertResult_openplot {w args } {
149 puts "insert=[$w index insert]"
152 proc ShowPlotWindow { w name thisRange resultRange desired } {
153 if { "[winfo toplevel $w]" != "[winfo toplevel $name]" } {
154 $name config -relief sunken -borderwidth 2
155 pack $name -expand 1 -fill both
156 raise [winfo toplevel $name ]
157 return
159 oarraySet $name $desired
160 set at [whereToPutPlot $w $thisRange $resultRange]
161 set col [lindex [split $at .] 1]
163 if { $col > 0 } {
164 $w insert $at "\n \n" "$name"
165 set at [$w index "$at +1char"]
167 # compute where we will try to display.
168 # try to leave top of window where it is, but if not
169 # scroll lines up just the amount necessary to make the
170 # window visible.
172 set h1 [winfo height $w]
173 set h2 [oget $name height]
174 set begin [$w index @0,0]
175 set ind $at
176 set dl [$w dlineinfo $ind]
177 set y0 [lindex $dl 1]
178 set prev ""
179 if { "$y0" != "" } {
180 while { [$w compare $begin <= $ind] } {
181 set dl [$w dlineinfo $ind]
183 if { "$dl" == "" } { break }
184 if { $y0 - [lindex $dl 1] + $h2 +5 < $h1 } {
185 set prev $ind
186 set ind [$w index "$ind - 1 line" ]
187 } else {
188 break
193 bind $name <Destroy> "catch {$w yview [$w index @0,0] } ; eval $w delete \[$w tag nextrange $name 0.0 \]"
195 if { "$prev" != "" } { set ind $prev }
196 $w insert $at " " "$name center"
197 $w window create $at+1char -window $name
198 $w tag add "center $name" $at "$at+2char"
199 update
200 $w yview $ind
201 # somehow the single button click gets run positioning the cursor
202 # near where the
203 after 1 $w mark set insert [$w index insert]
204 return $ind
207 ## endsource eopenplot.tcl