1#
2#  tkextlib/tkHTML/htmlwidget.rb
3#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4#
5
6require 'tk'
7
8# call setup script for general 'tkextlib' libraries
9require 'tkextlib/setup.rb'
10
11# call setup script
12require 'tkextlib/tkHTML/setup.rb'
13
14# TkPackage.require('Tkhtml', '2.0')
15TkPackage.require('Tkhtml')
16
17module Tk
18  class HTML_Widget < TkWindow
19    PACKAGE_NAME = 'Tkhtml'.freeze
20    def self.package_name
21      PACKAGE_NAME
22    end
23
24    def self.package_version
25      begin
26        TkPackage.require('Tkhtml')
27      rescue
28        ''
29      end
30    end
31
32    class ClippingWindow < TkWindow
33    end
34  end
35end
36
37class Tk::HTML_Widget::ClippingWindow
38  WidgetClassName = 'HtmlClip'.freeze
39  WidgetClassNames[WidgetClassName] ||= self
40
41  HtmlClip_TBL = TkCore::INTERP.create_table
42
43  TkCore::INTERP.init_ip_env{
44    HtmlClip_TBL.mutex.synchronize{ HtmlClip_TBL.clear }
45  }
46
47  def self.new(parent, keys={})
48    if parent.kind_of?(Hash)
49      keys = TkComm._symbolkey2str(parent)
50      parent = keys.delete('parent')
51    end
52
53    if parent.kind_of?(String)
54      ppath = parent.path
55    elsif parent
56      ppath = parent
57    else
58      ppath = ''
59    end
60    HtmlClip_TBL.mutex.synchronize{
61      return HtmlClip_TBL[ppath] if HtmlClip_TBL[ppath]
62    }
63
64    widgetname = keys.delete('widgetname')
65    if widgetname =~ /^(.*)\.[^.]+$/
66      ppath2 = $1
67      if ppath2[0] != ?.
68        ppath2 = ppath + '.' + ppath2
69      end
70      HtmlClip_TBL.mutex.synchronize{
71        return HtmlClip_TBL[ppath2] if HtmlClip_TBL[ppath2]
72      }
73
74      ppath = ppath2
75    end
76
77    parent = TkComm._genobj_for_tkwidget(ppath)
78    unless parent.kind_of?(Tk::HTML_Widget)
79      fail ArgumentError, "parent must be a Tk::HTML_Widget instance"
80    end
81
82    super(parent)
83  end
84
85  def initialize(parent)
86    @parent = parent
87    @ppath = parent.path
88    @path = @id = @ppath + '.x'
89    HtmlClip_TBL.mutex.synchronize{
90      HtmlClip_TBL[@ppath] = self
91    }
92  end
93
94  def method_missing(m, *args, &b)
95    @parent.__send__(m, *args, &b)
96  end
97end
98
99class Tk::HTML_Widget
100  include Scrollable
101
102  TkCommandNames = ['html'.freeze].freeze
103  WidgetClassName = 'Html'.freeze
104  WidgetClassNames[WidgetClassName] ||= self
105
106  def create_self(keys)
107    if keys and keys != None
108      tk_call_without_enc(self.class::TkCommandNames[0], @path,
109                          *hash_kv(keys, true))
110    else
111      tk_call_without_enc(self.class::TkCommandNames[0], @path)
112    end
113  end
114  private :create_self
115
116  def __strval_optkeys
117    super() << 'base' << 'selectioncolor' << 'unvisitedcolor' << 'visitedcolor'
118  end
119  private :__strval_optkeys
120
121  ###################################
122  #  class methods
123  ###################################
124  def self.reformat(src, dst, txt)
125    tk_call('html', 'reformat', src, dst, txt)
126  end
127
128  def self.url_join(*args) # args := sheme authority path query fragment
129    tk_call('html', 'urljoin', *args)
130  end
131
132  def self.url_split(uri)
133    tk_call('html', 'urlsplit', uri)
134  end
135
136  def self.lockcopy(src, dst)
137    tk_call('html', 'lockcopy', src, dst)
138  end
139
140  def self.gzip_file(file, dat)
141    tk_call('html', 'gzip', 'file', file, dat)
142  end
143
144  def self.gunzip_file(file, dat)
145    tk_call('html', 'gunzip', 'file', filet)
146  end
147
148  def self.gzip_data(dat)
149    tk_call('html', 'gzip', 'data', file, dat)
150  end
151
152  def self.gunzip_data(dat)
153    tk_call('html', 'gunzip', 'data', filet)
154  end
155
156  def self.base64_encode(dat)
157    tk_call('html', 'base64', 'encode', dat)
158  end
159
160  def self.base64_decode(dat)
161    tk_call('html', 'base64', 'encode', dat)
162  end
163
164  def self.text_format(dat, len)
165    tk_call('html', 'text', 'format', dat, len)
166  end
167
168  def self.xor(cmd, *args)
169    tk_call('html', 'xor', cmd, *args)
170  end
171
172  def self.stdchan(cmd, channel)
173    tk_call('html', 'stdchan', cmd, channel)
174  end
175
176  def self.crc32(data)
177    tk_call('html', 'crc32', data)
178  end
179
180  ###################################
181  #  instance methods
182  ###################################
183  def clipping_window
184    ClippingWindow.new(self)
185  end
186  alias clipwin  clipping_window
187  alias htmlclip clipping_window
188
189  def bgimage(image, tid=None)
190    tk_send('bgimage', image, tid)
191    self
192  end
193
194  def clear()
195    tk_send('clear')
196    self
197  end
198
199  def coords(index=None, percent=None)
200    tk_send('coords', index, percent)
201  end
202
203  def forminfo(*args)
204    tk_send('forminfo', *args)
205  end
206  alias form_info forminfo
207
208  def href(x, y)
209    simplelist(tk_send('href', x, y))
210  end
211
212  def image_add(id, img)
213    tk_send('imageadd', id, img)
214    self
215  end
216
217  def image_at(x, y)
218    tk_send('imageat', x, y)
219  end
220
221  def images()
222    list(tk_send('images'))
223  end
224
225  def image_set(id, num)
226    tk_send('imageset', id, num)
227    self
228  end
229
230  def image_update(id, imgs)
231    tk_send('imageupdate', id, imgs)
232    self
233  end
234
235  def index(idx, count=None, unit=None)
236    tk_send('index', idx, count, unit)
237  end
238
239  def insert_cursor(idx)
240    tk_send('insert', idx)
241  end
242
243  def names()
244    simple_list(tk_send('names'))
245  end
246
247  def on_screen(id, x, y)
248    bool(tk_send('onscreen', id, x, y))
249  end
250
251  def over(x, y)
252    list(tk_send('over', x, y))
253  end
254
255  def over_markup(x, y)
256    list(tk_send('over', x, y, '-muponly'))
257  end
258
259  def over_attr(x, y, attrs)
260    list(tk_send('overattr', x, y, attrs))
261  end
262
263  def parse(txt)
264    tk_send('parse', txt)
265    self
266  end
267
268  def resolver(*uri_args)
269    tk_send('resolver', *uri_args)
270  end
271
272  def selection_clear()
273    tk_send('selection', 'clear')
274    self
275  end
276
277  def selection_set(first, last)
278    tk_send('selection', 'set', first, last)
279    self
280  end
281
282  def refresh(*opts)
283    tk_send('refresh', *opts)
284  end
285
286  def layout()
287    tk_send('layout')
288  end
289
290  def sizewindow(*args)
291    tk_send('sizewindow', *args)
292  end
293
294  def postscript(*args)
295    tk_send('postscript', *args)
296  end
297
298  def source()
299    tk_send('source')
300  end
301
302  def plain_text(first, last)
303    tk_send('text', 'ascii', first, last)
304  end
305  alias ascii_text plain_text
306  alias text_ascii plain_text
307
308  def text_delete(first, last)
309    tk_send('text', 'delete', first, last)
310    self
311  end
312
313  def html_text(first, last)
314    tk_send('text', 'html', first, last)
315  end
316  alias text_html html_text
317
318  def text_insert(idx, txt)
319    tk_send('text', 'insert', idx, txt)
320    self
321  end
322
323  def break_text(idx)
324    tk_send('text', 'break', idx)
325  end
326  alias text_break break_text
327
328  def text_find(txt, *args)
329    tk_send('text', 'find', txt, *args)
330  end
331
332  def text_table(idx, imgs=None, attrs=None)
333    tk_send('text', 'table', idx, imgs, attrs)
334  end
335
336  def token_append(tag, *args)
337    tk_send('token', 'append', tag, *args)
338    self
339  end
340
341  def token_delete(first, last=None)
342    tk_send('token', 'delete', first, last)
343    self
344  end
345
346  def token_define(*args)
347    tk_send('token', 'defile', *args)
348    self
349  end
350
351  def token_find(tag, *args)
352    list(tk_send('token', 'find', tag, *args))
353  end
354
355  def token_get(first, last=None)
356    list(tk_send('token', 'get', first, last))
357  end
358
359  def token_list(first, last=None)
360    list(tk_send('token', 'list', first, last))
361  end
362
363  def token_markup(first, last=None)
364    list(tk_send('token', 'markup', first, last))
365  end
366
367  def token_DOM(first, last=None)
368    list(tk_send('token', 'domtokens', first, last))
369  end
370  alias token_dom token_DOM
371  alias token_domtokens token_DOM
372  alias token_dom_tokens token_DOM
373
374  def token_get_end(idx)
375    tk_send('token', 'getend', idx)
376  end
377  alias token_getend token_get_end
378
379  def token_offset(start, num1, num2)
380    list(tk_send('token', 'offset', start, num1, num2))
381  end
382
383  def token_get_attr(idx, name=None)
384    list(tk_send('token', 'attr', idx, name))
385  end
386
387  def token_set_attr(idx, name=None, val=None)
388    tk_send('token', 'attr', idx, name, val)
389    self
390  end
391
392  def token_handler(tag, cmd=nil, &b)
393    cmd = Proc.new(&b) if !cmd && b
394    if cmd
395      tk_send('token', 'handler', tag, cmd)
396      return self
397    else
398      return tk_send('token', 'handler', tag)
399    end
400  end
401
402  def token_insert(idx, tag, *args)
403    tk_send('token', 'insert', idx, tag, *args)
404    self
405  end
406
407  def token_attrs(*args)
408    list(tk_send('token', 'attrs', *args))
409  end
410
411  def token_unique(*args)
412    list(tk_send('token', 'unique', *args))
413  end
414
415  def token_on_events(*args)
416    list(tk_send('token', 'onEvents', *args))
417  end
418
419  def dom_nameidx(tag, name)
420    number(tk_send('dom', 'nameidx', tag, name))
421  end
422  alias dom_name_index dom_nameidx
423
424  def dom_radioidx(tag, name)
425    number(tk_send('dom', 'radioidx', tag, name))
426  end
427  alias dom_radio_index dom_radioidx
428
429  def dom_id(*spec)
430    tk_send('dom', 'id', *spec)
431  end
432
433  def dom_ids(*spec)
434    list(tk_send('dom', 'ids', *spec))
435  end
436
437  def dom_value(*spec)
438    list(tk_send('dom', 'value', *spec))
439  end
440
441  def dom_attr(idx)
442    tk_send('dom', 'attr', idx)
443  end
444
445  def dom_formel(name)
446    tk_send('dom', 'formel', name)
447  end
448  alias dom_form_element dom_formel
449
450  def dom_tree(idx, val)
451    list(tk_send('dom', 'tree', idx, val))
452  end
453end
454