From bbcbe0b7e5d5969d6a55cc885a323107e415ce44 Mon Sep 17 00:00:00 2001 From: nagai Date: Wed, 30 Mar 2005 08:44:19 +0000 Subject: * ext/tk/tcltklib.c: add TclTkIp#_create_console() method to create a Tcl/Tk's console window. * ext/tk/lib/multi-tk.rb: support TclTkIp#_create_console() method. * ext/tk/lib/remote-tk.rb: ditto. * ext/tk/lib/tk/console.rb: ditto. * ext/tk/lib/tk.rb: update RELEASE_DATE * ext/tk/sample/demo-*/check2.rb: use 'return' in the Proc object. * ext/tk/sample/tkextlib/**: ditto. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@8212 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/tk/sample/tkextlib/tkHTML/hv.rb | 85 ++--- ext/tk/sample/tkextlib/tkHTML/ss.rb | 682 +++++++++++++++++++----------------- 2 files changed, 402 insertions(+), 365 deletions(-) (limited to 'ext/tk/sample/tkextlib/tkHTML') diff --git a/ext/tk/sample/tkextlib/tkHTML/hv.rb b/ext/tk/sample/tkextlib/tkHTML/hv.rb index d9f3ea7d7..a4d78ea5b 100644 --- a/ext/tk/sample/tkextlib/tkHTML/hv.rb +++ b/ext/tk/sample/tkextlib/tkHTML/hv.rb @@ -78,38 +78,43 @@ big_imgs = {} hotkey = {} move_big_image = proc{|b| - return unless big_imgs.key?(b) - b.copy(big_imgs[b]) - big_imgs[b].delete - big_imgs.delete(b) - Tk.update + 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| - return smgray unless show_img.bool - fn = args[0] - if old_imgs.key?(fn) - images[fn] = old_imgs[fn] - old_imgs.delete(fn) - return images[fn] - end + if show_img.bool + smgray + else + fn = args[0] - begin - img = TkPhotoImage.new(:file=>fn) - rescue - return smgray - end + if old_imgs.key?(fn) + images[fn] = old_imgs[fn] + old_imgs.delete(fn) + images[fn] - 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) }) + 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 - - images[fn] = img - - img } # @@ -183,12 +188,13 @@ clear_screen = proc{ # Load a file into the HTML widget # load_file = proc{|name| - return unless (doc = read_file.call(name)) - clear_screen.call - last_file = name - html.configure(:base=>name) - html.parse(doc) - old_imgs.clear + 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| @@ -196,15 +202,16 @@ href_binding = proc{|x, y| html.selection_clear priv['mark'] = "@#{x},#{y}" lst = html.href(x, y) - return if lst.size.zero? - lnk, target = lst + unless lst.size.zero? + lnk, target = lst - if lnk != "" - if lnk =~ /^#{last_file}#(.*)$/ - html.yview($1) - else - load_file.call(lnk) + if lnk != "" + if lnk =~ /^#{last_file}#(.*)$/ + html.yview($1) + else + load_file.call(lnk) + end end end } diff --git a/ext/tk/sample/tkextlib/tkHTML/ss.rb b/ext/tk/sample/tkextlib/tkHTML/ss.rb index 3dbb7ec71..1c13d7ac3 100644 --- a/ext/tk/sample/tkextlib/tkHTML/ss.rb +++ b/ext/tk/sample/tkextlib/tkHTML/ss.rb @@ -6,401 +6,431 @@ require 'tk' require 'tkextlib/tkHTML' -root = TkRoot.new(:title=>'HTML File Viewer', :iconname=>'HV') -fswin = nil - -html = nil -html_fs = nil - -hotkey = {} - file = ARGV[0] +class TkHTML_File_Viewer + include TkComm # These are images to use with the actual image specified in a # "" markup can't be found. # -biggray = TkPhotoImage.new(:data=><<'EOD') +@@biggray = TkPhotoImage.new(:data=><<'EOD') R0lGODdhPAA+APAAALi4uAAAACwAAAAAPAA+AAACQISPqcvtD6OctNqLs968+w+G4kiW5omm 6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNFgsAO/// EOD -smgray = TkPhotoImage.new(:data=><<'EOD') +@@smgray = TkPhotoImage.new(:data=><<'EOD') R0lGODdhOAAYAPAAALi4uAAAACwAAAAAOAAYAAACI4SPqcvtD6OctNqLs968+w+G4kiW5omm 6sq27gvH8kzX9m0VADv/ EOD + 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) -# -# 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 to pick fonts for the fullscreen view. -# -baseFontSize = 24 -pick_font_fs = proc{|size, attrs| - # 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(' ') -} + @html.token_handler('meta', proc{|*args| meta(@html, *args)}) -# -# -hyper_cmd = proc{|*args| - puts "HyperlinkCommand: #{args.inspect}" -} + vscr = @html.yscrollbar(TkScrollbar.new) + hscr = @html.xscrollbar(TkScrollbar.new) -# This routine is called to run an applet -# -applet_arg = TkVarAccess.new_hash('AppletArg') -run_applet = proc{|size, w, arglist| - applet_arg.value = Hash[*simplelist(arglist)] + 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) - return unless applet_arg.key?('src') + ############################################ - src = html.remove(applet_arg['src']) + @html.clipwin.focus - applet_arg['window'] = w - applet_arg['fontsize'] = size + # If an arguent was specified, read it into the HTML widget. + # + Tk.update + if file && file != "" + load_file(file) + end + end - begin - Tk.load_tclscript(src) - rescue => e - puts "Applet error: #{e.message}" + # + # A font chooser routine. + # + # html[:fontcommand] = pick_font + 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 -} -# -# -form_cmd = proc{|n, cmd, *args| -} + # This routine is called to pick fonts for the fullscreen view. + # + def pick_font_fs(size, attrs) + baseFontSize = 24 -# -# -images = {} -old_imgs = {} -big_imgs = {} + # 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 -# -# -move_big_image = proc{|b| - return unless big_imgs.key?(b) - b.copy(big_imgs[b]) - big_imgs[b].delete - big_imgs.delete(b) -} - -image_cmd = proc{|hs, *args| - fn = args[0] - - if old_imgs.key?(fn) - return (images[fn] = old_imgs.delete(fn)) + # + # + def hyper_cmd(*args) + puts "HyperlinkCommand: #{args.inspect}" end - begin - img = TkPhotoImage.new(:file=>fn) - rescue - return ((hs)? smallgray: biggray) + # This routine is called to run an applet + # + 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 - if hs - img2 = TkPhotoImage.new - img2.copy(img, :subsample=>[2,2]) - img.delete - img = img2 + # + # + def form_cmd(n, cmd, *args) + # p [n, cmd, *args] 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.call(b) }) + # + # + 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 - images[fn] = img + def image_cmd(hs, *args) + fn = args[0] - img -} + 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 -# -# This routine is called for every