1#
2# tk/scrollable.rb : module for scrollable widget
3#
4require 'tk'
5
6module Tk
7  module XScrollable
8    def xscrollcommand(cmd=Proc.new)
9      configure_cmd 'xscrollcommand', cmd
10      # Tk.update  # avoid scrollbar trouble
11      self
12    end
13
14    def xview(*index)
15      if index.size == 0
16        list(tk_send_without_enc('xview'))
17      else
18        tk_send_without_enc('xview', *index)
19        self
20      end
21    end
22    def xview_moveto(*index)
23      xview('moveto', *index)
24    end
25    def xview_scroll(*index)
26      xview('scroll', *index)
27    end
28
29    def xscrollbar(bar=nil)
30      if bar
31        @xscrollbar = bar
32        @xscrollbar.orient 'horizontal'
33        self.xscrollcommand {|*arg| @xscrollbar.set(*arg)}
34        @xscrollbar.command {|*arg| self.xview(*arg)}
35        Tk.update  # avoid scrollbar trouble
36      end
37      @xscrollbar
38    end
39  end
40
41  module YScrollable
42    def yscrollcommand(cmd=Proc.new)
43      configure_cmd 'yscrollcommand', cmd
44      # Tk.update  # avoid scrollbar trouble
45      self
46    end
47
48    def yview(*index)
49      if index.size == 0
50        list(tk_send_without_enc('yview'))
51      else
52        tk_send_without_enc('yview', *index)
53        self
54      end
55    end
56    def yview_moveto(*index)
57      yview('moveto', *index)
58    end
59    def yview_scroll(*index)
60      yview('scroll', *index)
61    end
62
63    def yscrollbar(bar=nil)
64      if bar
65        @yscrollbar = bar
66        @yscrollbar.orient 'vertical'
67        self.yscrollcommand {|*arg| @yscrollbar.set(*arg)}
68        @yscrollbar.command {|*arg| self.yview(*arg)}
69        Tk.update  # avoid scrollbar trouble
70      end
71      @yscrollbar
72    end
73  end
74
75  X_Scrollable = XScrollable
76  Y_Scrollable = YScrollable
77
78  module Scrollable
79    include XScrollable
80    include YScrollable
81  end
82end
83