home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activetcltk / ActiveTcl8.3.4.1-8.win32-ix86.exe / ActiveTcl8.3.4.1-win32-ix86 / demos / Tkhtml / ss.tcl < prev   
Encoding:
Text File  |  2001-10-22  |  9.4 KB  |  426 lines

  1. # @(#) $Id: ss.tcl,v 1.11 2001/06/17 22:45:35 peter Exp $
  2. #
  3. # This script implements the "ss" application.  "ss" implements
  4. # a presentation slide-show based on HTML slides.
  5. wm title . {Tk Slide Show}
  6. wm iconname . {SlideShow}
  7.  
  8. package require Tkhtml
  9.  
  10. # Attempt to load the HTML widget if it isn't already part
  11. # of the interpreter
  12. #
  13. if {[info command html]==""} {
  14.   foreach f {
  15.     ./tkhtml.so
  16.     /usr/lib/tkhtml.so
  17.     /usr/local/lib/tkhtml.so
  18.     ./tkhtml.dll
  19.   } {
  20.     if {[file exists $f]} {
  21.       if {[catch {load $f Tkhtml}]==0} break
  22.     }
  23.   }
  24. }
  25.  
  26. # Pick the initial filename from the command line
  27. #
  28. set HtmlTraceMask 0
  29. set file {}
  30. foreach a $argv {
  31.   if {[regexp {^debug=} $a]} {
  32.     scan $a "debug=0x%x" HtmlTraceMask
  33.   } else {
  34.     set file $a
  35.   }
  36. }
  37.  
  38. # These are images to use with the actual image specified in a
  39. # "<img>" markup can't be found.
  40. #
  41. image create photo biggray -data {
  42.     R0lGODdhPAA+APAAALi4uAAAACwAAAAAPAA+AAACQISPqcvtD6OctNqLs968+w+G4kiW5omm
  43.     6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNFgsAO///
  44. }
  45. image create photo smgray -data {
  46.     R0lGODdhOAAYAPAAALi4uAAAACwAAAAAOAAYAAACI4SPqcvtD6OctNqLs968+w+G4kiW5omm
  47.     6sq27gvH8kzX9m0VADv/
  48. }
  49.  
  50. # Build the half-size view of the page
  51. #
  52. frame .mbar -bd 2 -relief raised
  53. pack .mbar -side top -fill x
  54. menubutton .mbar.help -text File -underline 0 -menu .mbar.help.m
  55. pack .mbar.help -side left -padx 5
  56. set m [menu .mbar.help.m]
  57. $m add command -label Open -underline 0 -command Load
  58. $m add command -label {Full Screen} -underline 0 -command FullScreen
  59. $m add command -label Refresh -underline 0 -command Refresh
  60. $m add separator
  61. $m add command -label Exit -underline 1 -command exit
  62.  
  63. frame .h
  64. pack .h -side top -fill both -expand 1
  65. html .h.h \
  66.   -width 512 -height 384 \
  67.   -yscrollcommand {.h.vsb set} \
  68.   -xscrollcommand {.f2.hsb set} \
  69.   -padx 5 \
  70.   -pady 9 \
  71.   -formcommand FormCmd \
  72.   -imagecommand "ImageCmd 1" \
  73.   -scriptcommand ScriptCmd \
  74.   -appletcommand AppletCmd \
  75.   -hyperlinkcommand HyperCmd \
  76.   -fontcommand pickFont \
  77.   -appletcommand {runApplet small} \
  78.   -bg white -tablerelief raised
  79. .h.h token handler meta "Meta .h.h"
  80.  
  81. if {$HtmlTraceMask} {
  82.   .h.h config -tablerelief flat
  83. }
  84.  
  85. # This routine is called to pick fonts for the half-size window.
  86. #
  87. proc pickFont {size attrs} { 
  88.   # puts "FontCmd: $size $attrs"
  89.   set a [expr {-1<[lsearch $attrs fixed]?{courier}:{charter}}]
  90.   set b [expr {-1<[lsearch $attrs italic]?{italic}:{roman}}]
  91.   set c [expr {-1<[lsearch $attrs bold]?{bold}:{normal}}]
  92.   set d [expr {int(12*pow(1.2,$size-4))}]
  93.   list $a $d $b $c
  94. }
  95.  
  96. # This routine is called to pick fonts for the fullscreen view.
  97. #
  98. set baseFontSize 24
  99. proc pickFontFS {size attrs} { 
  100.   # puts "FontCmd: $size $attrs"
  101.   set a [expr {-1<[lsearch $attrs fixed]?{courier}:{charter}}]
  102.   set b [expr {-1<[lsearch $attrs italic]?{italic}:{roman}}]
  103.   set c [expr {-1<[lsearch $attrs bold]?{bold}:{normal}}]
  104.   global baseFontSize
  105.   set d [expr {int($baseFontSize*pow(1.2,$size-4))}]
  106.   list $a $d $b $c
  107.  
  108. proc HyperCmd {args} {
  109.    puts "HyperlinkCommand: $args"
  110. }
  111.  
  112. # This routine is called to run an applet
  113. #
  114. proc runApplet {size w arglist} {
  115.   global AppletArg
  116.   catch {unset AppletArg}
  117.   foreach {name value} $arglist {
  118.     set AppletArg([string tolower $name]) $value
  119.   }
  120.   if {![info exists AppletArg(src)]} return
  121.   set src [.h.h resolve $AppletArg(src)]
  122.   set AppletArg(window) $w
  123.   set AppletArg(fontsize) $size
  124.   if {[catch {uplevel #0 "source $src"} msg]} {
  125.     puts "Applet error: $msg"
  126.   }
  127. }
  128.  
  129. proc FormCmd {n cmd args} {
  130.  # puts "FormCmd: $n $cmd $args"
  131.  #  switch $cmd {
  132.  #   select -
  133.  #   textarea -
  134.  #   input {
  135.  #     set w [lindex $args 0]
  136.  #     label $w -image smgray
  137.  #   }
  138.  # }
  139. }
  140. proc ImageCmd {hs args} {
  141.   global OldImages Images
  142.   set fn [lindex $args 0]
  143.   if {[info exists OldImages($fn)]} {
  144.     set Images($fn) $OldImages($fn)
  145.     unset OldImages($fn)
  146.     return $Images($fn)
  147.   }
  148.   if {[catch {image create photo -file $fn} img]} {
  149.     if {$hs} {
  150.       return smallgray
  151.     } else {
  152.       return biggray
  153.     }
  154.   }
  155.   if {$hs} {
  156.     set img2 [image create photo]
  157.     $img2 copy $img -subsample 2 2
  158.     image delete $img
  159.     set img $img2
  160.   }
  161.   if {[image width $img]*[image height $img]>20000} {
  162.     global BigImages
  163.     set b [image create photo -width [image width $img] \
  164.            -height [image height $img]]
  165.     set BigImages($b) $img
  166.     set img $b
  167.     after idle "MoveBigImage $b"
  168.   }
  169.   set Images($fn) $img
  170.   return $img
  171. }
  172. proc MoveBigImage b {
  173.   global BigImages
  174.   if {![info exists BigImages($b)]} return
  175.   $b copy $BigImages($b)
  176.   image delete $BigImages($b)
  177.   unset BigImages($b)
  178. }
  179.   
  180. proc ScriptCmd {args} {
  181.   # puts "ScriptCmd: $args"
  182. }
  183. proc AppletCmd {w arglist} {
  184.   # puts "AppletCmd: w=$w arglist=$arglist"
  185.   # label $w -text "The Applet $w" -bd 2 -relief raised
  186. }
  187.  
  188. # This binding fires when there is a click on a hyperlink
  189. #
  190. proc HrefBinding {w x y} {
  191.   set new [$w href $x $y]
  192.   # puts "link to [list $new]";
  193.   if {$new!=""} {
  194.     ProcessUrl $new
  195.   }
  196. }
  197. bind HtmlClip <1> {KeyPress %W Down}
  198. bind HtmlClip <3> {KeyPress %W Up}
  199. bind HtmlClip <2> {KeyPress %w Down}
  200.  
  201. # Clicking button three on the small screen causes the full-screen view
  202. # to appear.
  203. #
  204. # bind .h.h.x <3> FullScreen
  205.  
  206. # Handle all keypress events on the screen
  207. #
  208. bind HtmlClip <KeyPress> {KeyPress %W %K}
  209. proc KeyPress {w keysym} {
  210.   global hotkey key_block
  211.   if {[info exists key_block]} return
  212.   set key_block 1
  213.   after 250 {catch {unset key_block}}
  214.   if {[info exists hotkey($keysym)]} {
  215.     ProcessUrl $hotkey($keysym)
  216.   }
  217.   switch -- $keysym {
  218.     Escape {
  219.       if {[winfo exists .fs]} {FullScreenOff} {FullScreen}
  220.     }
  221.   }
  222. }
  223.  
  224.  
  225. # Finish building the half-size screen
  226. #
  227. pack .h.h -side left -fill both -expand 1
  228. scrollbar .h.vsb -orient vertical -command {.h.h yview}
  229. pack .h.vsb -side left -fill y
  230.  
  231. frame .f2
  232. pack .f2 -side top -fill x
  233. frame .f2.sp -width [winfo reqwidth .h.vsb] -bd 2 -relief raised
  234. pack .f2.sp -side right -fill y
  235. scrollbar .f2.hsb -orient horizontal -command {.h.h xview}
  236. pack .f2.hsb -side top -fill x
  237.  
  238. #proc FontCmd {args} {
  239. #  puts "FontCmd: $args"
  240. #  return {Times 12}
  241. #}
  242. #proc ResolverCmd {args} {
  243. #  puts "Resolver: $args"
  244. #  return [lindex $args 0]
  245. #}
  246.  
  247. set lastDir [pwd]
  248. proc Load {} {
  249.   set filetypes {
  250.     {{Html Files} {.html .htm}}
  251.     {{All Files} *}
  252.   }
  253.   global lastDir htmltext
  254.   set f [tk_getOpenFile -initialdir $lastDir -filetypes $filetypes]
  255.   if {$f!=""} {
  256.     LoadFile $f
  257.     set lastDir [file dirname $f]
  258.   }
  259. }
  260.  
  261. # Clear the screen.
  262. #
  263. proc Clear {} {
  264.   global Images OldImages hotkey
  265.   if {[winfo exists .fs.h]} {set w .fs.h} {set w .h.h}
  266.   $w clear
  267.   catch {unset hotkey}
  268.   ClearBigImages
  269.   ClearOldImages
  270.   foreach fn [array names Images] {
  271.     set OldImages($fn) $Images($fn)
  272.   }
  273.   catch {unset Images}
  274. }
  275. proc ClearOldImages {} {
  276.   global OldImages
  277.   foreach fn [array names OldImages] {
  278.     image delete $OldImages($fn)
  279.   }
  280.   catch {unset OldImages}
  281. }
  282. proc ClearBigImages {} {
  283.   global BigImages
  284.   foreach b [array names BigImages] {
  285.     image delete $BigImages($b)
  286.   }
  287.   catch {unset BigImages}
  288. }
  289.  
  290. # Read a file
  291. #
  292. proc ReadFile {name} {
  293.   if {[catch {open $name r} fp]} {
  294.     tk_messageBox -icon error -message $fp -type ok
  295.     return {}
  296.   } else {
  297.     set r [read $fp [file size $name]]
  298.     close $fp
  299.     return $r
  300.   }
  301. }
  302.  
  303. # Process the given URL
  304. #
  305. proc ProcessUrl {url} {
  306.   switch -glob -- $url {
  307.     file:* {
  308.       LoadFile [string range $url 5 end]
  309.     }
  310.     exec:* {
  311.       regsub -all \\+ [string range $url 5 end] { } url
  312.       eval exec $url &
  313.     }
  314.     default {
  315.       LoadFile $url
  316.     }
  317.   }
  318. }
  319.  
  320. # Load a file into the HTML widget
  321. #
  322. proc LoadFile {name} {
  323.   set html [ReadFile $name]
  324.   if {$html==""} return
  325.   Clear
  326.   global LastFile
  327.   set LastFile $name
  328.   if {[winfo exists .fs.h]} {set w .fs.h} {set w .h.h}
  329.   $w config -base $name -cursor watch
  330.   $w parse $html
  331.   $w config -cursor top_left_arrow
  332.   ClearOldImages
  333. }
  334.  
  335. # Refresh the current file.
  336. #
  337. proc Refresh {} {
  338.   global LastFile
  339.   if {![info exists LastFile]} return
  340.   LoadFile $LastFile
  341. }
  342.  
  343. # This routine is called whenever a "<meta>" markup is seen.
  344. #
  345. proc Meta {w tag alist} {
  346.   foreach {name value} $alist {
  347.     set v($name) $value
  348.   }
  349.   if {[info exists v(key)] && [info exists v(href)]} {
  350.     global hotkey
  351.     set hotkey($v(key)) [$w resolve $v(href)]
  352.   }
  353.   if {[info exists v(next)]} {
  354.     global hotkey
  355.     set hotkey(Down) $v(next)
  356.   }
  357.   if {[info exists v(prev)]} {
  358.     global hotkey
  359.     set hotkey(Up) $v(next)
  360.   }
  361.   if {[info exists v(other)]} {
  362.     global hotkey
  363.     set hotkey(o) $v(other)
  364.   }
  365. }
  366.  
  367. # Go from full-screen mode back to window mode.
  368. #
  369. proc FullScreenOff {} {
  370.   destroy .fs
  371.   wm deiconify .
  372.   update
  373.   raise .
  374.   focus .h.h.x
  375.   Clear
  376.   ClearOldImages
  377.   Refresh 
  378. }
  379.  
  380. # Go from window mode to full-screen mode.
  381. #
  382. proc FullScreen {} {
  383.   if {[winfo exists .fs]} {
  384.     wm deiconify .fs
  385.     update
  386.     raise .fs
  387.     return
  388.   }
  389.   toplevel .fs
  390.   wm overrideredirect .fs 1
  391.   set w [winfo screenwidth .]
  392.   set h [winfo screenheight .]
  393.   wm geometry .fs ${w}x$h+0+0
  394.   # bind .fs <3> FullScreenOff
  395.   html .fs.h \
  396.     -padx 5 \
  397.     -pady 9 \
  398.     -formcommand FormCmd \
  399.     -imagecommand "ImageCmd 0" \
  400.     -scriptcommand ScriptCmd \
  401.     -appletcommand AppletCmd \
  402.     -hyperlinkcommand HyperCmd \
  403.     -bg white -tablerelief raised \
  404.     -appletcommand {runApplet big} \
  405.     -fontcommand pickFontFS \
  406.     -cursor tcross
  407.   pack .fs.h -fill both -expand 1
  408.   .fs.h token handler meta "Meta .fs.h"
  409.   Clear
  410.   ClearOldImages
  411.   Refresh
  412.   update
  413.   focus .fs.h.x
  414. }
  415. focus .h.h.x
  416.  
  417. # Load the file named on the command-line, if there is
  418. # one.
  419. #
  420. update
  421. if {$file!=""} {
  422.   LoadFile $file
  423. }
  424.