File: canvas.tcl

package info (click to toggle)
tk-html3 3.0~fossil20110109-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,644 kB
  • ctags: 5,882
  • sloc: ansic: 48,994; tcl: 26,030; sh: 1,190; yacc: 161; makefile: 24
file content (304 lines) | stat: -rw-r--r-- 6,598 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

# catch {memory init on}
# catch {memory onexit mem.out}
# catch {memory validate on}

# Required packages
set auto_path [concat . $auto_path]
package require Tkhtml
catch {
  package require Img
}

# Procedure to return the contents of a file-system entry
proc readFile {fname} {
  set ret {}
  catch {
    set fd [open $fname]
    set ret [read $fd]
    close $fd
  }
  return $ret
}

proc tree_to_html {indent tree} {
  set in [string repeat " " $indent]
  if {[regexp {^TEXT} $tree]} {
    puts -nonewline "$in"
    puts $tree
  } else {
    set tag [lindex $tree 0]
    puts "$in<$tag>"
    foreach child [lindex $tree 1] {
      tree_to_html [expr $indent + 2] $child
    }
    puts "$in</$tag>"
  }
}

proc scriptcommand {line_number tag tarargs script} {
  if {$tag=="style"} {
    append ::STYLE_TEXT $script
    puts $script
  }
  return ""
}

proc stylecmd {style} {
  append ::STYLE_TEXT $style
  append ::STYLE_TEXT "\n"
  return ""
}

proc scriptcmd {script} {
  return ""
}

proc linkcmd {node} {
  set rel [string tolower [$node attr rel]]
  set media [string tolower [$node attr media]]
  set media_list [list all visual screen ""]
  if {[string compare $rel stylesheet]==0 && [lsearch $media_list $media]!=-1} {
    set href [$node attr href]
    set filename [file join $::BASE $href]
    lappend ::STYLESHEET_FILES $filename
  }
}

set HTML .h
proc main {document css} {

  html $::HTML

  $::HTML handler script script dummycmd
  $::HTML handler script style stylecmd
  $::HTML handler node link linkcmd

  set ::STYLESHEET_FILES {}
  set ::STYLE_TEXT {}
  set parsetime [time {
      $::HTML parse $document
      $::HTML style parse agent $css
      while {[llength $::STYLESHEET_FILES]>0} {
        set ss [lindex $::STYLESHEET_FILES 0]
        set ::STYLESHEET_FILES [lrange $::STYLESHEET_FILES 1 end]
        $::HTML style parse author [readFile $ss]
      }
      $::HTML style parse author $::STYLE_TEXT
  }]
  puts "Parse time [lrange $parsetime 0 1]"

  $::HTML style parse author.1 { 
    img    { -tkhtml-replace: tcl(replace_img) }
    object { -tkhtml-replace: tcl(replace_img) }
    input  { -tkhtml-replace: tcl(replace_input) }
    select { -tkhtml-replace: tcl(replace_select) }
  }

  set s [$::HTML style syntax_errs]
  puts "$s syntax errors in style sheet"

  set styletime [time {
      $::HTML style apply
  }]
  puts "Style time [lrange $styletime 0 1]"
}

set W 800
proc redraw {{w 0}} {
  if {$w==0} {
    set w $::W
  } else {
    set ::W $w
  } 
  set codetime [time {$::HTML layout force -width $w -win .c}]
  puts "Layout time [lrange $codetime 0 1]"
  set tclizetime [time {set code [$::HTML layout primitives]}]
  puts "Tclize time [lrange $tclizetime 0 1]"
  set drawtime [time {draw_to_canvas $code .c 0 0}]
  puts "Draw time   [lrange $drawtime 0 1]"
}

proc replace_img {node} {
  if {[$node tag]=="object"} {
    set filename [file join $::BASE [$node attr data]]
  } else {
    set filename [file join $::BASE [$node attr src]]
  }
  if [catch { set img [image create photo -file $filename] } msg] {
    puts "Error: $msg"
    error $msg
  } 
  return $img
}

set CONTROL 0
proc replace_input {node} {
  set tkname ".control[incr ::CONTROL]"
  set width [$node attr width]
  if {$width==""} {
    set width 20
  }

  switch -exact [$node attr type] {
    image {
      return [replace_img $node]
    }
    hidden {
      return ""
    }
    checkbox {
      return [checkbutton $tkname]
    }
    radio {
      return [checkbutton $tkname]
    }
    submit {
      return [button $tkname -text Submit]
    }
    default {
      entry $tkname -width $width
      return $tkname
    }
  }
  return ""
}

proc replace_select {node} {
  set tkname ".control[incr ::CONTROL]"
  button $tkname -text Select
  return $tkname
}

proc draw_origin {x y} {
  upvar X X
  upvar Y Y
  upvar C C

  incr X $x
  incr Y $y
}

proc draw_text {x y font color string} {
  upvar X X
  upvar Y Y
  upvar C C

  incr x $X
  incr y $Y

  # The Y coordinate supplied by the layout code is for the baseline of the
  # text item. The canvas widget doesn't support this, so decrement Y by
  # the font metric 'ascent' value and anchor the nw corner of the text to
  # simulate it.
  set ascent [font metrics $font -ascent]
  incr y [expr -1*$ascent]

  $C create text $x $y -font $font -fill $color -text $string -anchor nw 
}

proc draw_quad {x1 y1 x2 y2 x3 y3 x4 y4 color} {
  upvar X X
  upvar Y Y
  upvar C C

  foreach v {x1 x2 x3 x4} {incr $v $X}
  foreach v {y1 y2 y3 y4} {incr $v $Y}
  $C create polygon $x1 $y1 $x2 $y2 $x3 $y3 $x4 $y4 -fill $color
}

proc draw_image {x y image} {
  upvar X X
  upvar Y Y
  upvar C C

  incr x $X
  incr y $Y
  $C create image $x $y -image $image -anchor nw
}

proc draw_window {x y window} {
  upvar X X
  upvar Y Y
  upvar C C

  incr x $X
  incr y $Y
  $C create window $x $y -window $window -anchor nw
}

proc draw_background {color} {
  upvar C C
  $C configure -background $color
}

proc draw_to_canvas {code c x y} {
  $c delete all

  set X $x
  set Y $y
  set C $c

  foreach instruction $code {
    # puts $instruction
    eval $instruction
  }

  set scrollregion [$c bbox all]
  if {[llength $scrollregion]==4} {
    lset scrollregion 0 0
    lset scrollregion 1 0
    $c configure -scrollregion $scrollregion
  }
  puts "Scrollregion: $scrollregion"
}

proc new_document {{r 1}} {
  # catch {destroy $::HTML}
  if {[llength $::DOCS]==0} {
    rename $::HTML {}
    exit
  }
  set ::BASE [file dirname [lindex $::DOCS 0]]
  main [readFile [lindex $::DOCS 0]] $::CSS
  set ::DOCS [lrange $::DOCS 1 end]
  if {$r} redraw
}

wm geometry . 800x600

frame .buttons
button .buttons.correct    -text Incorrect -command new_document
button .buttons.incorrect  -text Correct   -command new_document

pack .buttons.correct .buttons.incorrect -side left
pack .buttons -side bottom -fill x

scrollbar .s -orient vertical
scrollbar .s2 -orient horizontal
canvas .c -background grey
pack .s -side right -fill y
pack .s2 -side bottom -fill x
pack .c -fill both -expand true

.c configure -yscrollcommand {.s set}
.c configure -xscrollcommand {.s2 set}
.s configure -command {.c yview}
.s2 configure -command {.c xview}

bind .c <Configure> {redraw [expr %w - 5]}
bind .c <KeyPress-Down> {.c yview scroll 1 units} 
bind .c <KeyPress-Up> {.c yview scroll -1 units} 
focus .c

set cssfile [file join [file dirname [info script]] html.css]
set CSS [readFile $cssfile]

set arg [lindex $argv 0]
if {[file isdirectory $arg]} {
  set DOCS [lsort [glob [file join [lindex $argv 0] *.html]]]
} else {
  set DOCS $arg
}

new_document 0