Class: TkHTML_File_Viewer

Inherits:
Object show all
Includes:
TkComm
Defined in:
sample/tkextlib/tkHTML/ss.rb

Constant Summary collapse

@@biggray =

These are images to use with the actual image specified in a “<img>” markup can't be found.

TkPhotoImage.new(:data=><<'EOD')
    R0lGODdhPAA+APAAALi4uAAAACwAAAAAPAA+AAACQISPqcvtD6OctNqLs968+w+G4kiW5omm
    6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNFgsAO///
EOD
@@smgray =
TkPhotoImage.new(:data=><<'EOD')
    R0lGODdhOAAYAPAAALi4uAAAACwAAAAAOAAYAAACI4SPqcvtD6OctNqLs968+w+G4kiW5omm
    6sq27gvH8kzX9m0VADv/
EOD

Constants included from TkComm

TkComm::GET_CONFIGINFO_AS_ARRAY, TkComm::GET_CONFIGINFOwoRES_AS_ARRAY, TkComm::TkExtlibAutoloadModule, TkComm::Tk_CMDTBL, TkComm::Tk_IDs, TkComm::Tk_WINDOWS, TkComm::USE_TCLs_LIST_FUNCTIONS, TkComm::WidgetClassNames

Constants included from TkUtil

TkUtil::None, TkUtil::RELEASE_DATE

Instance Method Summary collapse

Methods included from TkComm

_at, _callback_entry?, _callback_entry_class?, _curr_cmd_id, _fromUTF8, _genobj_for_tkwidget, _next_cmd_id, _toUTF8, array2tk_list, #bind, #bind_all, #bind_append, #bind_append_all, #bind_remove, #bind_remove_all, #bindinfo, #bindinfo_all, bool, image_obj, #install_cmd, install_cmd, list, num_or_nil, num_or_str, number, procedure, simplelist, slice_ary, string, #subst, tk_tcl2ruby, uninstall_cmd, #uninstall_cmd, window

Methods included from TkEvent

#install_bind, #install_bind_for_event_class

Methods included from TkUtil

_conv_args, #_conv_args, #_fromUTF8, #_get_eval_enc_str, _get_eval_enc_str, #_get_eval_string, _get_eval_string, #_symbolkey2str, _symbolkey2str, #_toUTF8, #bool, bool, callback, eval_cmd, hash_kv, #hash_kv, install_cmd, num_or_nil, #num_or_nil, #num_or_str, num_or_str, #number, number, string, #string, uninstall_cmd, untrust

Constructor Details

#initialize(file = nil) ⇒ TkHTML_File_Viewer

Returns a new instance of TkHTML_File_Viewer.


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
# File 'sample/tkextlib/tkHTML/ss.rb', line 28

def initialize(file = nil)
  @root  = TkRoot.new(:title=>'HTML File Viewer', :iconname=>'HV')
  @fswin = nil

  @html = nil
  @html_fs = nil

  @hotkey = {}

  @applet_arg = TkVarAccess.new_hash('AppletArg')

  @images   = {}
  @old_imgs = {}
  @big_imgs = {}

  @last_dir = Dir.pwd

  @last_file = ''

  @key_block = false

  Tk::HTML_Widget::ClippingWindow.bind('1',
                                       proc{|w, ksym| key_press(w, ksym)},
                                       '%W Down')
  Tk::HTML_Widget::ClippingWindow.bind('3',
                                       proc{|w, ksym| key_press(w, ksym)},
                                       '%W Up')
  Tk::HTML_Widget::ClippingWindow.bind('2',
                                       proc{|w, ksym| key_press(w, ksym)},
                                       '%W Down')

  Tk::HTML_Widget::ClippingWindow.bind('KeyPress',
                                       proc{|w, ksym| key_press(w, ksym)},
                                       '%W %K')

  ############################################
  #
  # Build the half-size view of the page
  #
  menu_spec = [
    [['File', 0],
      ['Open',        proc{sel_load()},   0],
      ['Full Screen', proc{fullscreen()}, 0],
      ['Refresh',     proc{refresh()},    0],
      '---',
      ['Exit', proc{exit}, 1]]
  ]

  mbar = @root.add_menubar(menu_spec)

  @html = Tk::HTML_Widget.new(:width=>512, :height=>384,
                              :padx=>5, :pady=>9,
                              :formcommand=>proc{|*args| form_cmd(*args)},
                              :imagecommand=>proc{|*args|
                                image_cmd(1, *args)
                              },
                              :scriptcommand=>proc{|*args|
                                script_cmd(*args)
                              },
                              :appletcommand=>proc{|*args|
                                applet_cmd(*args)
                              },
                              :hyperlinkcommand=>proc{|*args|
                                hyper_cmd(*args)
                              },
                              :fontcommand=>proc{|*args|
                                pick_font(*args)
                              },
                              :appletcommand=>proc{|*args|
                                run_applet('small', *args)
                              },
                              :bg=>'white', :tablerelief=>:raised)

  @html.token_handler('meta', proc{|*args| meta(@html, *args)})

  vscr = @html.yscrollbar(TkScrollbar.new)
  hscr = @html.xscrollbar(TkScrollbar.new)

  Tk.grid(@html, vscr, :sticky=>:news)
  Tk.grid(hscr,       :sticky=>:ew)
  @root.grid_columnconfigure(0, :weight=>1)
  @root.grid_columnconfigure(1, :weight=>0)
  @root.grid_rowconfigure(0, :weight=>1)
  @root.grid_rowconfigure(1, :weight=>0)

  ############################################

  @html.clipwin.focus

  # If an arguent was specified, read it into the HTML widget.
  #
  Tk.update
  if file && file != ""
    load_file(file)
  end
end

Instance Method Details

#applet_cmd(w, arglist) ⇒ Object

This routine is called for every <APPLET> markup


230
231
232
233
# File 'sample/tkextlib/tkHTML/ss.rb', line 230

def applet_cmd(w, arglist)
  # puts "AppletCmd: w=#{w} arglist=#{arglist}"
  #TkLabel.new(w, :text=>"The Applet #{w}", :bd=>2, :relief=>raised)
end

#clear_screenObject

Clear the screen.


261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'sample/tkextlib/tkHTML/ss.rb', line 261

def clear_screen
  if @html_fs && @html_fs.exist?
    w = @html_fs
  else
    w = @html
  end
  w.clear
  @old_imgs.clear
  @big_imgs.clear
  @hotkey.clear
  @images.each{|k, v| @old_imgs[k] = v }
  @images.clear
end

#form_cmd(n, cmd, *args) ⇒ Object


176
177
178
# File 'sample/tkextlib/tkHTML/ss.rb', line 176

def form_cmd(n, cmd, *args)
  # p [n, cmd, *args]
end

#fullscreenObject

Go from window mode to full-screen mode.


365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'sample/tkextlib/tkHTML/ss.rb', line 365

def fullscreen
  if @fswin && @fswin.exist?
    @fswin.deiconify
    Tk.update
    @fswin.raise
    return
  end

  width  =  @root.winfo_screenwidth
  height =  @root.winfo_screenheight
  @fswin = TkToplevel.new(:overrideredirect=>true,
                          :geometry=>"#{width}x#{height}+0+0")

  @html_fs = Tk::HTML_Widget.new(@fswin, :padx=>5, :pady=>9,
                                 :formcommand=>proc{|*args|
                                   form_cmd(*args)
                                 },
                                 :imagecommand=>proc{|*args|
                                   image_cmd(0, *args)
                                 },
                                 :scriptcommand=>proc{|*args|
                                   script_cmd(*args)
                                 },
                                 :appletcommand=>proc{|*args|
                                   applet_cmd(*args)
                                 },
                                 :hyperlinkcommand=>proc{|*args|
                                   hyper_cmd(*args)
                                 },
                                 :appletcommand=>proc{|*args|
                                   run_applet('big', *args)
                                 },
                                 :fontcommand=>proc{|*args|
                                   pick_font_fs(*args)
                                 },
                                 :bg=>'white', :tablerelief=>:raised,
                                 :cursor=>:tcross) {
    pack(:fill=>:both, :expand=>true)
    token_handler('meta', proc{|*args| meta(self, *args)})
  }

  clear_screen()
  @old_imgs.clear
  refresh()
  Tk.update
  @html_fs.clipwin.focus
end

#fullscreen_offObject

Go from full-screen mode back to window mode.


352
353
354
355
356
357
358
359
360
361
# File 'sample/tkextlib/tkHTML/ss.rb', line 352

def fullscreen_off
  @fswin.destroy
  @root.deiconify
  Tk.update
  @root.raise
  @html.clipwin.focus
  clear_screen()
  @old_imgs.clear
  refresh()
end

#href_binding(w, x, y) ⇒ Object

This binding fires when there is a click on a hyperlink


237
238
239
240
241
242
# File 'sample/tkextlib/tkHTML/ss.rb', line 237

def href_binding(w, x, y)
  lst = w.href(x, y)
  unless lst.empty?
    process_url(lst)
  end
end

#hyper_cmd(*args) ⇒ Object


151
152
153
# File 'sample/tkextlib/tkHTML/ss.rb', line 151

def hyper_cmd(*args)
  puts "HyperlinkCommand: #{args.inspect}"
end

#image_cmd(hs, *args) ⇒ Object


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
# File 'sample/tkextlib/tkHTML/ss.rb', line 189

def image_cmd(hs, *args)
  fn = args[0]

  if @old_imgs.key?(fn)
    return (@images[fn] = @old_imgs.delete(fn))
  end

  begin
    img = TkPhotoImage.new(:file=>fn)
  rescue
    return ((hs)? @@smallgray: @@biggray)
  end

  if hs
    img2 = TkPhotoImage.new
    img2.copy(img, :subsample=>[2,2])
    img.delete
    img = img2
  end

  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(b) })
  end

  @images[fn] = img

  img
end

#key_press(w, keysym) ⇒ Object


415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'sample/tkextlib/tkHTML/ss.rb', line 415

def key_press(w, keysym)
  return if @key_block
  @key_block = true
  Tk.after(250, proc{@key_block = false})

  if @hotkey.key?(keysym)
    process_url(@hotkey[keysym])
  end
  case keysym
  when 'Escape'
    if @fswin && @fswin.exist?
      fullscreen_off()
    else
      fullscreen()
    end
  end
end

#load_file(name) ⇒ Object

Load a file into the HTML widget


307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'sample/tkextlib/tkHTML/ss.rb', line 307

def load_file(name)
  return unless (doc = read_file(name))
  clear_screen()
  @last_file = name
  if @html_fs && @html_fs.exist?
    w = @html_fs
  else
    w = @html
  end
  w.configure(:base=>name)
  w.parse(doc)
  w.configure(:cursor=>'top_left_arrow')
  @old_imgs.clear
end

#meta(w, tag, alist) ⇒ Object

This routine is called whenever a “<meta>” markup is seen.


330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'sample/tkextlib/tkHTML/ss.rb', line 330

def meta(w, tag, alist)
  v = Hash[*simplelist(alist)]

  if v.key?('key') && v.key?('href')
    @hotkey[v['key']] = w.resolve(v['href'])
  end

  if v.key?('next')
    @hotkey['Down'] =v['next']
  end

  if v.key?('prev')
    @hotkey['Up'] =v['prev']
  end

  if v.key?('other')
    @hotkey['o'] =v['other']
  end
end

#move_big_image(b) ⇒ Object


182
183
184
185
186
187
# File 'sample/tkextlib/tkHTML/ss.rb', line 182

def move_big_image(b)
  return unless @big_imgs.key?(b)
  b.copy(@big_imgs[b])
  @big_imgs[b].delete
  @big_imgs.delete(b)
end

#pick_font(size, attrs) ⇒ Object

A font chooser routine.

html = pick_font


129
130
131
132
133
134
135
# File 'sample/tkextlib/tkHTML/ss.rb', line 129

def pick_font(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(' ')
end

#pick_font_fs(size, attrs) ⇒ Object

This routine is called to pick fonts for the fullscreen view.


139
140
141
142
143
144
145
146
147
# File 'sample/tkextlib/tkHTML/ss.rb', line 139

def pick_font_fs(size, attrs)
  baseFontSize = 24

  # puts "FontCmd: #{size} #{attrs}"
  [ ((attrs =~ /fixed/)? 'courier': 'charter'),
    (baseFontSize * (1.2**(size.to_f - 4.0))).to_i,
    ((attrs =~ /italic/)? 'italic': 'roman'),
    ((attrs =~ /bold/)? 'bold': 'normal')  ].join(' ')
end

#process_url(url) ⇒ Object

Process the given URL


294
295
296
297
298
299
300
301
302
303
# File 'sample/tkextlib/tkHTML/ss.rb', line 294

def process_url(url)
  case url[0]
  when /^file:/
    load_file(url[0][5..-1])
  when /^exec:/
    Tk.ip_eval(url[0][5..-1].tr('\\', ' '))
  else
    load_file(url[0])
  end
end

#read_file(name) ⇒ Object

Read a file


277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'sample/tkextlib/tkHTML/ss.rb', line 277

def read_file(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
end

#refresh(*args) ⇒ Object

Refresh the current file.


324
325
326
# File 'sample/tkextlib/tkHTML/ss.rb', line 324

def refresh(*args)
  load_file(@last_file) if @last_file
end

#run_applet(size, w, arglist) ⇒ Object

This routine is called to run an applet


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'sample/tkextlib/tkHTML/ss.rb', line 157

def run_applet(size, w, arglist)
  applet_arg.value = Hash[*simplelist(arglist)]

  return unless @applet_arg.key?('src')

  src = @html.remove(@applet_arg['src'])

  @applet_arg['window'] = w
  @applet_arg['fontsize'] = size

  begin
    Tk.load_tclscript(src)
  rescue => e
    puts "Applet error: #{e.message}"
  end
end

#script_cmd(*args) ⇒ Object

This routine is called for every <SCRIPT> markup


224
225
226
# File 'sample/tkextlib/tkHTML/ss.rb', line 224

def script_cmd(*args)
  # puts "ScriptCmd: #{args.inspect}"
end

#sel_loadObject


246
247
248
249
250
251
252
253
254
255
256
257
# File 'sample/tkextlib/tkHTML/ss.rb', line 246

def sel_load
  filetypes = [
    ['Html Files', ['.html', '.htm']],
    ['All Files', '*']
  ]

  f = Tk.getOpenFile(:initialdir=>@last_dir, :filetypes=>filetypes)
  if f != ''
    load_file(f)
    @last_dir = File.dirname(f)
  end
end