1#
2# tk/scrollbar.rb : treat scrollbar widget
3#
4require 'tk'
5
6class Tk::Scrollbar<TkWindow
7  TkCommandNames = ['scrollbar'.freeze].freeze
8  WidgetClassName = 'Scrollbar'.freeze
9  WidgetClassNames[WidgetClassName] ||= self
10
11  def create_self(keys)
12    @assigned = []
13    @scroll_proc = proc{|*args|
14      if self.orient == 'horizontal'
15        @assigned.each{|w| w.xview(*args)}
16      else # 'vertical'
17        @assigned.each{|w| w.yview(*args)}
18      end
19    }
20
21    if keys and keys != None
22      unless TkConfigMethod.__IGNORE_UNKNOWN_CONFIGURE_OPTION__
23        #tk_call_without_enc('scrollbar', @path, *hash_kv(keys, true))
24        tk_call_without_enc(self.class::TkCommandNames[0], @path,
25                            *hash_kv(keys, true))
26      else
27        begin
28          tk_call_without_enc(self.class::TkCommandNames[0], @path,
29                              *hash_kv(keys, true))
30        rescue
31          tk_call_without_enc(self.class::TkCommandNames[0], @path)
32          keys = __check_available_configure_options(keys)
33          unless keys.empty?
34            begin
35              tk_call_without_enc('destroy', @path)
36            rescue
37              # cannot destroy
38              configure(keys)
39            else
40              # re-create widget
41              tk_call_without_enc(self.class::TkCommandNames[0], @path,
42                                  *hash_kv(keys, true))
43            end
44          end
45        end
46      end
47    else
48      #tk_call_without_enc('scrollbar', @path)
49      tk_call_without_enc(self.class::TkCommandNames[0], @path)
50    end
51  end
52  private :create_self
53
54  def propagate_set(src_win, first, last)
55    self.set(first, last)
56    if self.orient == 'horizontal'
57      @assigned.each{|w| w.xview('moveto', first) if w != src_win}
58    else # 'vertical'
59      @assigned.each{|w| w.yview('moveto', first) if w != src_win}
60    end
61  end
62
63  def assign(*wins)
64    begin
65      self.command(@scroll_proc) if self.cget('command').cmd != @scroll_proc
66    rescue Exception
67      self.command(@scroll_proc)
68    end
69    orient = self.orient
70    wins.each{|w|
71      @assigned << w unless @assigned.index(w)
72      if orient == 'horizontal'
73        w.xscrollcommand proc{|first, last| self.propagate_set(w, first, last)}
74      else # 'vertical'
75        w.yscrollcommand proc{|first, last| self.propagate_set(w, first, last)}
76      end
77    }
78    Tk.update  # avoid scrollbar trouble
79    self
80  end
81
82  def assigned_list
83    begin
84      return @assigned.dup if self.cget('command').cmd == @scroll_proc
85    rescue Exception
86    end
87    fail RuntimeError, "not depend on the assigned_list"
88  end
89
90  def configure(*args)
91    ret = super(*args)
92    # Tk.update  # avoid scrollbar trouble
93    ret
94  end
95
96  #def delta(deltax=None, deltay=None)
97  def delta(deltax, deltay)
98    number(tk_send_without_enc('delta', deltax, deltay))
99  end
100
101  #def fraction(x=None, y=None)
102  def fraction(x, y)
103    number(tk_send_without_enc('fraction', x, y))
104  end
105
106  def identify(x, y)
107    tk_send_without_enc('identify', x, y)
108  end
109
110  def get
111    #ary1 = tk_send('get').split
112    #ary2 = []
113    #for i in ary1
114    #  ary2.push number(i)
115    #end
116    #ary2
117    list(tk_send_without_enc('get'))
118  end
119
120  def set(first, last)
121    tk_send_without_enc('set', first, last)
122    self
123  end
124
125  def activate(element=None)
126    tk_send_without_enc('activate', element)
127  end
128
129  def moveto(fraction)
130    tk_send_without_enc('moveto', fraction)
131    self
132  end
133
134  def scroll(*args)
135    tk_send_without_enc('scroll', *args)
136    self
137  end
138
139  def scroll_units(num)
140    scroll(num, 'units')
141    self
142  end
143
144  def scroll_pages(num)
145    scroll(num, 'pages')
146    self
147  end
148end
149
150#TkScrollbar = Tk::Scrollbar unless Object.const_defined? :TkScrollbar
151#Tk.__set_toplevel_aliases__(:Tk, Tk::Scrollbar, :TkScrollbar)
152Tk.__set_loaded_toplevel_aliases__('tk/scrollbar.rb', :Tk, Tk::Scrollbar,
153                                   :TkScrollbar)
154
155
156class Tk::XScrollbar<Tk::Scrollbar
157  def create_self(keys)
158    keys = {} unless keys
159    keys['orient'] = 'horizontal'
160    super(keys)
161  end
162  private :create_self
163end
164
165#TkXScrollbar = Tk::XScrollbar unless Object.const_defined? :TkXScrollbar
166#Tk.__set_toplevel_aliases__(:Tk, Tk::XScrollbar, :TkXScrollbar)
167Tk.__set_loaded_toplevel_aliases__('tk/scrollbar.rb', :Tk, Tk::XScrollbar,
168                                   :TkXScrollbar)
169
170
171class Tk::YScrollbar<Tk::Scrollbar
172  def create_self(keys)
173    keys = {} unless keys
174    keys['orient'] = 'vertical'
175    super(keys)
176  end
177  private :create_self
178end
179
180#TkYScrollbar = Tk::YScrollbar unless Object.const_defined? :TkYScrollbar
181#Tk.__set_toplevel_aliases__(:Tk, Tk::YScrollbar, :TkYScrollbar)
182Tk.__set_loaded_toplevel_aliases__('tk/scrollbar.rb', :Tk, Tk::YScrollbar,
183                                   :TkYScrollbar)
184