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 / hv.tcl next >
Encoding:
Text File  |  2001-10-22  |  8.4 KB  |  352 lines

  1. #
  2. # This script implements the "hv" application.  Type "hv FILE" to
  3. # view FILE as HTML.
  4. #
  5. # This application is used for testing the HTML widget.  It can
  6. # also server as an example of how to use the HTML widget.
  7. # @(#) $Id: hv.tcl,v 1.26 2001/06/17 22:45:35 peter Exp $
  8. #
  9. wm title . {HTML File Viewer}
  10. wm iconname . {HV}
  11.  
  12. # Make sure the html widget is loaded into
  13. # our interpreter
  14. #
  15. if {[info command html]==""} {
  16.   if {![package require Tkhtml]} return
  17.   foreach f {
  18.     ./tkhtml.so
  19.     /usr/lib/tkhtml.so
  20.     /usr/local/lib/tkhtml.so
  21.     ./tkhtml.dll
  22.   } {
  23.     if {[file exists $f]} {
  24.       if {[catch {load $f Tkhtml}]==0} break
  25.     }
  26.   }
  27. }
  28.  
  29. # The HtmlTraceMask only works if the widget was compiled with
  30. # the -DDEBUG=1 command-line option.  "file" is the name of the
  31. # first HTML file to be loaded.
  32. #
  33. set HtmlTraceMask 0
  34. set file {}
  35. foreach a $argv {
  36.   if {[regexp {^debug=} $a]} {
  37.     scan $a "debug=0x%x" HtmlTraceMask
  38.   } else {
  39.     set file $a
  40.   }
  41. }
  42.  
  43. # These images are used in place of GIFs or of form elements
  44. #
  45. image create photo biggray -data {
  46.     R0lGODdhPAA+APAAALi4uAAAACwAAAAAPAA+AAACQISPqcvtD6OctNqLs968+w+G4kiW5omm
  47.     6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNFgsAO///
  48. }
  49. image create photo smgray -data {
  50.     R0lGODdhOAAYAPAAALi4uAAAACwAAAAAOAAYAAACI4SPqcvtD6OctNqLs968+w+G4kiW5omm
  51.     6sq27gvH8kzX9m0VADv/
  52. }
  53. image create photo nogifbig -data {
  54.     R0lGODdhJAAkAPEAAACQkADQ0PgAAAAAACwAAAAAJAAkAAACmISPqcsQD6OcdJqKM71PeK15
  55.     AsSJH0iZY1CqqKSurfsGsex08XuTuU7L9HywHWZILAaVJssvgoREk5PolFo1XrHZ29IZ8oo0
  56.     HKEYVDYbyc/jFhz2otvdcyZdF68qeKh2DZd3AtS0QWcDSDgWKJXY+MXS9qY4+JA2+Vho+YPp
  57.     FzSjiTIEWslDQ1rDhPOY2sXVOgeb2kBbu1AAADv/
  58. }
  59. image create photo nogifsm -data {
  60.     R0lGODdhEAAQAPEAAACQkADQ0PgAAAAAACwAAAAAEAAQAAACNISPacHtD4IQz80QJ60as25d
  61.     3idKZdR0IIOm2ta0Lhw/Lz2S1JqvK8ozbTKlEIVYceWSjwIAO///
  62. }
  63.  
  64. # Construct the main window
  65. #
  66. frame .mbar -bd 2 -relief raised
  67. pack .mbar -side top -fill x
  68. menubutton .mbar.file -text File -underline 0 -menu .mbar.file.m
  69. pack .mbar.file -side left -padx 5
  70. set m [menu .mbar.file.m]
  71. $m add command -label Open -underline 0 -command Load
  72. $m add command -label Refresh -underline 0 -command Refresh
  73. $m add separator
  74. $m add command -label Exit -underline 1 -command exit
  75. menubutton .mbar.view -text View -underline 0 -menu .mbar.view.m
  76. pack .mbar.view -side left -padx 5
  77. set m [menu .mbar.view.m]
  78. set underlineHyper 0
  79. $m add checkbutton -label {Underline Hyperlinks} -variable underlineHyper
  80. trace variable underlineHyper w ChangeUnderline
  81. proc ChangeUnderline args {
  82.   global underlineHyper
  83.   .h.h config -underlinehyperlinks $underlineHyper
  84. }
  85. set showTableStruct 0
  86. $m add checkbutton -label {Show Table Structure} -variable showTableStruct
  87. trace variable showTableStruct w ShowTableStruct
  88. proc ShowTableStruct args {
  89.   global showTableStruct HtmlTraceMask
  90.   if {$showTableStruct} {
  91.     set HtmlTraceMask [expr {$HtmlTraceMask|0x8}]
  92.     .h.h config -tablerelief flat
  93.   } else {
  94.     set HtmlTraceMask [expr {$HtmlTraceMask&~0x8}]
  95.     .h.h config -tablerelief raised
  96.   }
  97.   Refresh
  98. }
  99. set showImages 1
  100. $m add checkbutton -label {Show Images} -variable showImages
  101. trace variable showImages w Refresh
  102.  
  103. # Construct the main HTML viewer
  104. #
  105. frame .h
  106. pack .h -side top -fill both -expand 1
  107. html .h.h \
  108.   -yscrollcommand {.h.vsb set} \
  109.   -xscrollcommand {.f2.hsb set} \
  110.   -padx 5 \
  111.   -pady 9 \
  112.   -formcommand FormCmd \
  113.   -imagecommand ImageCmd \
  114.   -scriptcommand ScriptCmd \
  115.   -appletcommand AppletCmd \
  116.   -underlinehyperlinks 0 \
  117.   -bg white -tablerelief raised
  118.  
  119. # If the tracemask is not 0, then draw the outline of all
  120. # tables as a blank line, not a 3D relief.
  121. #
  122. if {$HtmlTraceMask} {
  123.   .h.h config -tablerelief flat
  124. }
  125.  
  126. # A font chooser routine.
  127. #
  128. # .h.h config -fontcommand pickFont
  129. proc pickFont {size attrs} { 
  130.   puts "FontCmd: $size $attrs"
  131.   set a [expr {-1<[lsearch $attrs fixed]?{courier}:{charter}}]
  132.   set b [expr {-1<[lsearch $attrs italic]?{italic}:{roman}}]
  133.   set c [expr {-1<[lsearch $attrs bold]?{bold}:{normal}}]
  134.   set d [expr {int(12*pow(1.2,$size-4))}]
  135.   list $a $d $b $c
  136.  
  137. # This routine is called for each form element
  138. #
  139. proc FormCmd {n cmd args} {
  140.   # puts "FormCmd: $n $cmd $args"
  141.   switch $cmd {
  142.     select -
  143.     textarea -
  144.     input {
  145.       set w [lindex $args 0]
  146.       label $w -image nogifsm
  147.     }
  148.   }
  149. }
  150.  
  151. # This routine is called for every <IMG> markup
  152. #
  153. # proc ImageCmd {args} {
  154. # puts "image: $args"
  155. #   set fn [lindex $args 0]
  156. #   if {[catch {image create photo -file $fn} img]} {
  157. #     return nogifsm
  158. #   } else {
  159. #    global Images
  160. #    set Images($img) 1
  161. #    return $img
  162. #  }
  163. #}
  164. proc ImageCmd {args} {
  165.   global OldImages Images showImages
  166.   if {!$showImages} {
  167.     return smgray
  168.   }
  169.   set fn [lindex $args 0]
  170.   if {[info exists OldImages($fn)]} {
  171.     set Images($fn) $OldImages($fn)
  172.     unset OldImages($fn)
  173.     return $Images($fn)
  174.   }
  175.   if {[catch {image create photo -file $fn} img]} {
  176.     return smgray
  177.   }
  178.   if {[image width $img]*[image height $img]>20000} {
  179.     global BigImages
  180.     set b [image create photo -width [image width $img] \
  181.            -height [image height $img]]
  182.     set BigImages($b) $img
  183.     set img $b
  184.     after idle "MoveBigImage $b"
  185.   }
  186.   set Images($fn) $img
  187.   return $img
  188. }
  189. proc MoveBigImage b {
  190.   global BigImages
  191.   if {![info exists BigImages($b)]} return
  192.   $b copy $BigImages($b)
  193.   image delete $BigImages($b)
  194.   unset BigImages($b)
  195.   update
  196. }
  197.  
  198.  
  199. # This routine is called for every <SCRIPT> markup
  200. #
  201. proc ScriptCmd {args} {
  202.   # puts "ScriptCmd: $args"
  203. }
  204.  
  205. # This routine is called for every <APPLET> markup
  206. #
  207. proc AppletCmd {w arglist} {
  208.   # puts "AppletCmd: w=$w arglist=$arglist"
  209.   label $w -text "The Applet $w" -bd 2 -relief raised
  210. }
  211.  
  212. # This procedure is called when the user clicks on a hyperlink.
  213. # See the "bind .h.h.x" below for the binding that invokes this
  214. # procedure
  215. #
  216. proc HrefBinding {x y} {
  217.   set new [.h.h href $x $y]
  218.   # puts "link to [list $new]"; return
  219.   if {$new!=""} {
  220.     global LastFile
  221.     set pattern $LastFile#
  222.     set len [string length $pattern]
  223.     incr len -1
  224.     if {[string range $new 0 $len]==$pattern} {
  225.       incr len
  226.       .h.h yview [string range $new $len end]
  227.     } else {
  228.       LoadFile $new
  229.     }
  230.   }
  231. }
  232. bind .h.h.x <1> {HrefBinding %x %y}
  233.  
  234. # Pack the HTML widget into the main screen.
  235. #
  236. pack .h.h -side left -fill both -expand 1
  237. scrollbar .h.vsb -orient vertical -command {.h.h yview}
  238. pack .h.vsb -side left -fill y
  239.  
  240. frame .f2
  241. pack .f2 -side top -fill x
  242. frame .f2.sp -width [winfo reqwidth .h.vsb] -bd 2 -relief raised
  243. pack .f2.sp -side right -fill y
  244. scrollbar .f2.hsb -orient horizontal -command {.h.h xview}
  245. pack .f2.hsb -side top -fill x
  246.  
  247. # This procedure is called when the user selects the File/Open
  248. # menu option.
  249. #
  250. set lastDir [pwd]
  251. proc Load {} {
  252.   set filetypes {
  253.     {{Html Files} {.html .htm}}
  254.     {{All Files} *}
  255.   }
  256.   global lastDir htmltext
  257.   set f [tk_getOpenFile -initialdir $lastDir -filetypes $filetypes]
  258.   if {$f!=""} {
  259.     LoadFile $f
  260.     set lastDir [file dirname $f]
  261.   }
  262. }
  263.  
  264. # Clear the screen.
  265. #
  266. # Clear the screen.
  267. #
  268. proc Clear {} {
  269.   global Images OldImages hotkey
  270.   if {[winfo exists .fs.h]} {set w .fs.h} {set w .h.h}
  271.   $w clear
  272.   catch {unset hotkey}
  273.   ClearBigImages
  274.   ClearOldImages
  275.   foreach fn [array names Images] {
  276.     set OldImages($fn) $Images($fn)
  277.   }
  278.   catch {unset Images}
  279. }
  280. proc ClearOldImages {} {
  281.   global OldImages
  282.   foreach fn [array names OldImages] {
  283.     image delete $OldImages($fn)
  284.   }
  285.   catch {unset OldImages}
  286. }
  287. proc ClearBigImages {} {
  288.   global BigImages
  289.   foreach b [array names BigImages] {
  290.     image delete $BigImages($b)
  291.   }
  292.   catch {unset BigImages}
  293. }
  294.  
  295. # Read a file
  296. #
  297. proc ReadFile {name} {
  298.   if {[catch {open $name r} fp]} {
  299.     tk_messageBox -icon error -message $fp -type ok
  300.     return {}
  301.   } else {
  302.     fconfigure $fp -translation binary
  303.     set r [read $fp [file size $name]]
  304.     close $fp
  305.     return $r
  306.   }
  307. }
  308.  
  309. # Load a file into the HTML widget
  310. #
  311. proc LoadFile {name} {
  312.   set html [ReadFile $name]
  313.   if {$html==""} return
  314.   Clear
  315.   global LastFile
  316.   set LastFile $name
  317.    .h.h config -base $name
  318.   .h.h parse $html
  319.   ClearOldImages
  320. }
  321.  
  322. # Refresh the current file.
  323. #
  324. proc Refresh {args} {
  325.   global LastFile
  326.   if {![info exists LastFile]} return
  327.   LoadFile $LastFile
  328. }
  329.  
  330. # If an arguent was specified, read it into the HTML widget.
  331. #
  332. update
  333. if {$file!=""} {
  334.   LoadFile $file
  335. }
  336.  
  337.  
  338. # This binding changes the cursor when the mouse move over
  339. # top of a hyperlink.
  340. #
  341. bind HtmlClip <Motion> {
  342.   set parent [winfo parent %W]
  343.   set url [$parent href %x %y] 
  344.   if {[string length $url] > 0} {
  345.     $parent configure -cursor hand2
  346.   } else {
  347.     $parent configure -cursor {}
  348.   }
  349. }
  350.