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 305 306 307 308 309 310 311 312 313
|
#!/usr/bin/env ruby
#
# This script implements the "hv" application. Type "hv FILE" to
# view FILE as HTML.
#
# This application is used for testing the HTML widget. It can
# also server as an example of how to use the HTML widget.
#
require 'tk'
require 'tkextlib/tkHTML'
root = TkRoot.new(:title=>'HTML File Viewer', :iconname=>'HV')
file = ARGV[0]
#
# These images are used in place of GIFs or of form elements
#
biggray = TkPhotoImage.new(:data=><<'EOD')
R0lGODdhPAA+APAAALi4uAAAACwAAAAAPAA+AAACQISPqcvtD6OctNqLs968+w+G4kiW5omm
6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNFgsAO///
EOD
smgray = TkPhotoImage.new(:data=><<'EOD')
R0lGODdhOAAYAPAAALi4uAAAACwAAAAAOAAYAAACI4SPqcvtD6OctNqLs968+w+G4kiW5omm
6sq27gvH8kzX9m0VADv/
EOD
nogifbig = TkPhotoImage.new(:data=><<'EOD')
R0lGODdhJAAkAPEAAACQkADQ0PgAAAAAACwAAAAAJAAkAAACmISPqcsQD6OcdJqKM71PeK15
AsSJH0iZY1CqqKSurfsGsex08XuTuU7L9HywHWZILAaVJssvgoREk5PolFo1XrHZ29IZ8oo0
HKEYVDYbyc/jFhz2otvdcyZdF68qeKh2DZd3AtS0QWcDSDgWKJXY+MXS9qY4+JA2+Vho+YPp
FzSjiTIEWslDQ1rDhPOY2sXVOgeb2kBbu1AAADv/
EOD
nogifsm = TkPhotoImage.new(:data=><<'EOD')
R0lGODdhEAAQAPEAAACQkADQ0PgAAAAAACwAAAAAEAAQAAACNISPacHtD4IQz80QJ60as25d
3idKZdR0IIOm2ta0Lhw/Lz2S1JqvK8ozbTKlEIVYceWSjwIAO///
EOD
#
# define variables
#
ul_hyper = TkVariable.new(0)
show_tbl = TkVariable.new(0)
show_img = TkVariable.new(1)
#
# A font chooser routine.
#
# html[:fontcommand] = pick_font
pick_font = proc{|size, attrs|
puts "FontCmd: #{size} #{attrs}"
[ ((attrs =~ /fixed/)? 'courier': 'charter'),
(12 * (1.2**(size.to_f - 4.0))).to_i,
((attrs =~ /italic/)? 'italic': 'roman'),
((attrs =~ /bold/)? 'bold': 'normal') ].join(' ')
}
#
# This routine is called for each form element
#
form_cmd = proc{|n, cmd, style, *args|
# puts "FormCmd: $n $cmd $args"
case cmd
when 'select', 'textarea', 'input'
TkLabel.new(:widgetname=>args[0], :image=>nogifsm)
end
}
#
# This routine is called for every <IMG> markup
#
images = {}
old_imgs = {}
big_imgs = {}
hotkey = {}
move_big_image = proc{|b|
if big_imgs.key?(b)
b.copy(big_imgs[b])
big_imgs[b].delete
big_imgs.delete(b)
Tk.update
end
}
image_cmd = proc{|*args|
if show_img.bool
smgray
else
fn = args[0]
if old_imgs.key?(fn)
images[fn] = old_imgs[fn]
old_imgs.delete(fn)
images[fn]
else
begin
img = TkPhotoImage.new(:file=>fn)
rescue
smgray
else
if img.width * img.height > 20000
b = TkPhotoImage.new(:width=>img.width, :height=>img.height)
big_imgs[b] = img
img = b
Tk.after_idle(proc{ move_big_image.call(b) })
end
images[fn] = img
img
end
end
end
}
#
# This routine is called for every <SCRIPT> markup
#
script_cmd = proc{|*args|
# puts "ScriptCmd: #{args.inspect}"
}
# This routine is called for every <APPLET> markup
#
applet_cmd = proc{|w, arglist|
# puts "AppletCmd: w=#{w} arglist=#{arglist}"
TkLabel.new(w, :text=>"The Applet #{w}", :bd=>2, :relief=>raised)
}
#
# Construct the main HTML viewer
#
html = Tk::HTML_Widget.new(:padx=>5, :pady=>9,
:formcommand=>form_cmd,
:imagecommand=>image_cmd,
:scriptcommand=>script_cmd,
:appletcommand=>applet_cmd,
:underlinehyperlinks=>0,
:bg=>'white', :tablerelief=>:raised)
vscr = html.yscrollbar(TkScrollbar.new)
hscr = html.xscrollbar(TkScrollbar.new)
Tk.grid(html, vscr, :sticky=>:news)
Tk.grid(hscr, :sticky=>:ew)
Tk.root.grid_columnconfigure(0, :weight=>1)
Tk.root.grid_columnconfigure(1, :weight=>0)
Tk.root.grid_rowconfigure(0, :weight=>1)
Tk.root.grid_rowconfigure(1, :weight=>0)
#
# This procedure is called when the user clicks on a hyperlink.
#
priv = {}
last_file = ''
# Read a file
#
read_file = proc{|name|
begin
fp = open(name, 'r')
ret = fp.read(File.size(name))
rescue
ret = nil
fp = nil
Tk.messageBox(:icon=>'error', :message=>"fail to open '#{name}'",
:type=>:ok)
ensure
fp.close if fp
end
ret
}
# Clear the screen.
#
clear_screen = proc{
html.clear
old_imgs.clear
big_imgs.clear
hotkey.clear
images.each{|k, v| old_imgs[k] = v }
images.clear
}
# Load a file into the HTML widget
#
load_file = proc{|name|
if (doc = read_file.call(name))
clear_screen.call
last_file = name
html.configure(:base=>name)
html.parse(doc)
old_imgs.clear
end
}
href_binding = proc{|x, y|
# koba & dg marking text
html.selection_clear
priv['mark'] = "@#{x},#{y}"
lst = html.href(x, y)
unless lst.size.zero?
lnk, target = lst
if lnk != ""
if lnk =~ /^#{last_file}#(.*)$/
html.yview($1)
else
load_file.call(lnk)
end
end
end
}
html.clipping_window.bind('1', href_binding, '%x %y')
# marking text with the mouse and copying to the clipboard just with tkhtml2.0 working
html.clipping_window.bind('B1-Motion', proc{|w, x, y|
w.selection_set(priv['mark'], "@#{x},#{y}")
TkClipboard.clear
# avoid tkhtml0.0 errors
# anyone can fix this for tkhtml0.0
begin
TkClipboard.append(TkSelection.get)
rescue
end
}, '%W %x %y')
# This procedure is called when the user selects the File/Open
# menu option.
#
last_dir = Dir.pwd
sel_load = proc{
filetypes = [
['Html Files', ['.html', '.htm']],
['All Files', '*']
]
f = Tk.getOpenFile(:initialdir=>last_dir, :filetypes=>filetypes)
if f != ''
load_file.call(f)
last_dir = File.dirname(f)
end
}
# Refresh the current file.
#
refresh = proc{|*args|
load_file.call(last_file)
}
# This binding changes the cursor when the mouse move over
# top of a hyperlink.
#
Tk::HTML_Widget::ClippingWindow.bind('Motion', proc{|w, x, y|
parent = w.winfo_parent
url = parent.href(x, y)
unless url.empty?
parent[:cursor] = 'hand2'
else
parent[:cursor] = ''
end
}, '%W %x %y')
#
# Setup menu
#
menu_spec = [
[['File', 0],
['Open', sel_load, 0],
['Refresh', refresh, 0],
'---',
['Exit', proc{exit}, 1]],
[['View', 0],
['Underline Hyperlinks', ul_hyper],
['Show Table Structure', show_tbl],
['Show Images', show_img]]
]
mbar = Tk.root.add_menubar(menu_spec)
#
# Setup trace
#
ul_hyper.trace('w', proc{
html[:underlinehyperlinks] = ul_hyper.value
refresh.call
})
show_tbl.trace('w', proc{
if show_tbl.bool
html[:tablerelief] = :flat
else
html[:tablerelief] = :raised
end
refresh.call
})
show_img.trace('w', refresh)
# If an arguent was specified, read it into the HTML widget.
#
Tk.update
if file && file != ""
load_file.call(file)
end
#####################################
Tk.mainloop
|