Ruby/tk HTML_Widget and href's


#1

Consider the following goal:

  • Parse the html “click me” and display.
  • Detect user clicks convert to href.

The following code does not work:


require ‘tk’
require ‘tkextlib/tkHTML’

def do_click(s)
puts s + “\n”
end

html = Tk::HTML_Widget.new(TkRoot.new,‘hyperlinkcommand’=>proc {|s|
do_click(s)})
html.parse(“click me”)
html.pack

Tk.mainloop

The following code does work but is rather inelgant (note the Tk.bind):


require ‘tk’
require ‘tkextlib/tkHTML’

def do_click(x,y)
href = $HTML.href(x,y)[0]
print href + “\n” if href
end

$HTML = Tk::HTML_Widget.new(TkRoot.new)
Tk.bind($HTML.path + “.x”,‘1’,proc {|x,y| do_click(x,y)},"%x %y")
$HTML.parse(“click me”)
$HTML.pack

Tk.mainloop

Does anyone know what the ‘hyperlinkcommand’ property of HTML_Widget
does? Anyone have other ways to make clicking on hyperlinks work?

BTW, HTML_Widget corresponds (at least on my system) to tkHTML 2.0.

c.


#2

From: “Chris A.” removed_email_address@domain.invalid
Subject: ruby/tk HTML_Widget and href’s
Date: Fri, 14 Apr 2006 05:34:49 +0900
Message-ID: removed_email_address@domain.invalid

Consider the following goal:

  • Parse the html “click me” and display.
  • Detect user clicks convert to href.

The following code does not work:

I’m very sorry, but I don’t know why.
Tcl/Tk gives (probably) same result for same operations as your ruby’s.
It means that it is not a bug on Ruby/Tk.
Maybe, people who have answers for your question are members of
Tcl/Tk community.


#3

Yes, sorry. I should have said as much. I’ve run into similar issues
using Tcl/Tk.

Perhaps it would be worth adding bind_x, bind_append_x, bindtags_x,
etc. to the HTML_Widget class to bind to the .x subwidget. It is,
incidentally, the only subwidget. What do you think?

c.


#4

From: “Chris A.” removed_email_address@domain.invalid
Subject: Re: ruby/tk HTML_Widget and href’s
Date: Fri, 14 Apr 2006 12:16:10 +0900
Message-ID: removed_email_address@domain.invalid

Perhaps it would be worth adding bind_x, bind_append_x, bindtags_x,
etc. to the HTML_Widget class to bind to the .x subwidget. It is,
incidentally, the only subwidget. What do you think?

I don’t think so.
The .x subwidget is a widget of ‘HtmlClip’ class.
On Ruby/Tk, you can get a Tk::HTML_Widget::ClippingWindow object
for the subwidget.


#5

From: Hidetoshi NAGAI removed_email_address@domain.invalid
Subject: Re: ruby/tk HTML_Widget and href’s
Date: Fri, 14 Apr 2006 12:32:11 +0900
Message-ID: removed_email_address@domain.invalid


h = Tk::HTML_Widget.new
cw = Tk::HTML_Widget::ClippingWindow.new(h)
cw.bind(‘2’, proc{puts "click Button-2 ! "})

Or,


#6

Ah, I had not realized. I saw the ClippingWindow code but hadn’t
realized how it related. Thanks!

c.


#7

For anyone following this thread and interested, here’s the third
version of the sample code. This does work:


require ‘tk’
require ‘tkextlib/tkHTML’

def do_click(x,y)
href = $HTML.href(x,y)[0]
print href + “\n” if href
end

$HTML = Tk::HTML_Widget.new(TkRoot.new)
Tk::HTML_Widget::ClippingWindow.new($HTML).bind(‘1’,
proc {|x,y| do_click(x,y)},"%x %y")
$HTML.parse(“click me”)
$HTML.pack

Tk.mainloop