1 # mb-pres.tcl --
2 #
3 # Saving and loading of MediaBoard presentations
4 #
5 # Copyright (c) 1997-2002 The Regents of the University of California.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # A. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # B. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 # C. Neither the names of the copyright holders nor the names of its
17 # contributors may be used to endorse or promote products derived from this
18 # software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
24 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #
31 # @(#) $Header: /usr/mash/src/repository/mash/mash-1/tcl/mb/mb-pres.tcl,v 1.8 2002/02/03 04:27:17 lim Exp $
32
33
34 # An item in a mb presentation
35 Class MBPresItem
36
37 MBPresItem instproc init {coord prop} {
38 $self set coord_ $coord
39 $self set prop_ $prop
40 }
41
42 #
43 # A page in a mb presentation
44 # all it does right now is to save a marker and create a new page
45 Class MBPresItem/page -superclass MBPresItem
46
47 MBPresItem/page proc save {file canv item} {
48 puts $file {{page {} {}}}
49 }
50
51 MBPresItem/page instproc init {coord prop} {
52 $self next $coord $prop
53 }
54
55 MBPresItem/page instproc create {mgr sender pageidv} {
56 upvar $pageidv pageid
57 set pagemgr [$mgr page_manger]
58 set pageid [$pagemgr create_new_page]
59 $pagemgr switch_page_later $pageid
60 }
61
62 # An text item in a mb presentation
63 Class MBPresItem/text -superclass MBPresItem
64
65 MBPresItem/text instproc init {coord prop} {
66 $self next $coord $prop
67 }
68
69 MBPresItem/text proc save {file canv id} {
70 lappend result [$canv type $id]
71 lappend result [$canv coords $id]
72 set props {}
73 lappend props "-text"
74 lappend props [$canv itemcget $id -text]
75 # fonts are tricky due to the scaling effects
76 # the way we are doing it is will not work if the scale is one 1
77 set font [$canv itemcget $id -font]
78 set family [font actual $font -family]
79 set size [font actual $font -size]
80 if {$family=="Arial"} {
81 set family "helvetica"
82 }
83 set pixelsize [expr {int(($size*[tk scaling])+0.5)*10}]
84 lappend props "-font"
85 lappend props "-*-$family-bold-r-normal--*-$pixelsize-*-*-*-*-*-*"
86 set color [$canv itemcget $id -fill]
87 lappend props "-fill"
88 lappend props "$color"
89 lappend result $props
90 puts $file [list $result]
91 }
92
93 MBPresItem/text instproc create {mgr sender pageidv} {
94 upvar $pageidv pageid
95 $self instvar prop_ coord_
96 set index [lsearch -exact $prop_ "-text"]
97 puts "text index is $index"
98 incr index
99 set text [lindex $prop_ $index]
100 if {$text=={}} {
101 return
102 }
103 incr index -1
104 set props [lreplace $prop_ $index [expr {$index + 1}]]
105 set currText [eval $sender -page $pageid create_item text \
106 $coord_ $props]
107 puts "currText:$currText"
108 set canvas [$mgr page_manager current_canvas]
109 puts "text: $text"
110 for {set i 0} {$i < [string length $text]} {incr i} {
111 set char [string index $text $i]
112 set textLast [$sender -page $pageid insert $currText $i $char]
113 # puts "$sender -page $pageid insert $currText $i $char"
114 # puts "textLast is $textLast"
115 }
116 puts "c:$currText l:$textLast"
117 $sender -page $pageid create_item group text \
118 $currText $textLast
119 }
120
121 # A line item in a mb presentation
122 Class MBPresItem/line -superclass MBPresItem
123
124 MBPresItem/line instproc init {coord prop} {
125 }
126
127 MBPresItem/line proc save {file canv item} {
128
129 }
130
131
132 # Simple class to do a presentation, all it understand is text and pages
133 Class MBPres
134
135 #
136 # filename is the file name the presentation is associated with
137 #
138 MBPres instproc init {mgr filename} {
139 $self set mgr_ $mgr
140 puts "mbpres: $filename"
141 $self instvar f_
142 catch {file remove $filename}
143 set failed [catch {open $filename "r"} f_]
144 if {$failed} {
145 error "cannot open $filename"
146 }
147 $self read
148 $self set index_ 0
149 }
150
151 # clean up
152 MBPres instproc destroy {} {
153 close [$self set $f_]
154 }
155
156 # reads the whole file
157 # FIXME: we should be able to read in line by line
158 MBPres instproc read {} {
159 $self instvar lines_ len_ f_
160 set lines_ [read $f_]
161 set len_ [llength $lines_]
162 }
163
164 # parses a line and update the variables
165 MBPres instproc parse_line {index typevar coordvar propvar} {
166 $self instvar lines_
167 upvar $typevar type $coordvar coord $propvar prop
168 set line [lindex $lines_ $index]
169 set type [lindex $line 0]
170 set coord [lindex $line 1]
171 set prop [lindex $line 2]
172 }
173
174 # executes the line indexed by <u>index</u>
175 MBPres instproc exe {index pageidv} {
176 upvar $pageidv pageid
177 $self instvar index_
178 set type {}
179 set coord {}
180 set prop {}
181 $self parse_line $index type coord prop
182 puts "parse: $type $coord $prop"
183 if {$type!={}} {
184 set item [new MBPresItem/$type $coord $prop]
185 } else {
186 return
187 }
188 $self instvar mgr_
189 $item create $mgr_ [$mgr_ sender] pageid
190 delete $item
191 }
192
193 #
194 # executes the next line and put it into the pageid in the contents
195 # of <u>pageidv</u>
196 #
197 # side_effects: pgidvar might be updated (if the command is a page)
198 #
199 # if nopage is 1 functions returns (with value 2)
200 # when a <i>page</i> command is encountered
201 # otherwise it returns 1 when the execution is successful
202 # or 0 if it is the end of file
203 MBPres instproc exe_next {pageidv nopage} {
204 upvar $pageidv pageid
205 $self instvar index_ len_ lines_
206 puts "$index_ , $len_"
207 if {$index_ <= $len_} {
208 if {$nopage && [lindex [lindex $lines_ $index_] 0]=="page"} {
209 puts "exe_next returning 2"
210 return 2
211 }
212 $self exe $index_ pageid
213 incr index_
214 return 1
215 } else {
216 return 0
217 }
218 }
219
220 # create a new page and load items in it
221 MBPres instproc load_next_page {} {
222 $self instvar mgr_
223 set pageid [[$mgr_ page_manager] current_page]
224 set ok [$self exe_next pageid 0]
225 puts "$ok"
226 while {$ok==1} {
227 set ok [$self exe_next pageid 1]
228 }
229 }
230
231 #
232 # loads a whole file (regardless of how many pages there are)
233 #
234 MBPres instproc load {} {
235 $self instvar mgr_
236 set pageid [[$mgr_ page_manager] current_page]
237 set ok [$self exe_next pageid 0]
238 while $ok {
239 set ok [$self exe_next pageid 0]
240 }
241 $self instvar f_
242 close $f_
243 }
244
245 #
246 # Append the state of the canvas onto filename
247 #
248 MBPres proc save_canvas {canv filename} {
249 if [catch {open $filename "a+"} f] {
250 error "cannot save to $filename"
251 }
252 set elements [$canv find withtag all]
253 MBPresItem/page save $f $canv {}
254 foreach elt $elements {
255 set result {}
256 set tags [$canv gettags $elt]
257 if {-1 != [lsearch -exact $tags ignore]} {
258 continue
259 }
260 MBPresItem/[$canv type $elt] save $f $canv $elt
261 }
262 close $f
263 }
264
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.