summaryrefslogtreecommitdiff
path: root/ext/tk/sample/scrollframe.rb
blob: e340e1da3c732d099223996ec9464d2236984941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#
#  Tk::RbWidget::ScrollFrame class
#
#    This widget class is a frame widget with scrollbars.
#    The ScrollFrame doesn't propagate the size of embedded widgets.
#    When it is configured, scrollregion of the container is changed.
#
#    Scrollbars can be toggled by Tk::RbWidget::ScrollFrame#vscroll & hscroll.
#    If horizontal or virtical scrollbar is turned off, the horizontal
#    or virtical size of embedded widgets is propagated.
#
#                         Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
#
require 'tk'

class Tk::RbWidget::ScrollFrame < TkFrame
  include TkComposite

  DEFAULT_WIDTH  = 200
  DEFAULT_HEIGHT = 200

  def initialize_composite(keys={})
    @frame.configure(:width=>DEFAULT_WIDTH, :height=>DEFAULT_HEIGHT)

    # create scrollbars
    @h_scroll = TkScrollbar.new(@frame, 'orient'=>'horizontal')
    @v_scroll = TkScrollbar.new(@frame, 'orient'=>'vertical')

    # create a canvas widget
    @canvas = TkCanvas.new(@frame,
                           :borderwidth=>0, :selectborderwidth=>0,
                           :highlightthickness=>0)

    # allignment
    TkGrid.rowconfigure(@frame, 0, 'weight'=>1, 'minsize'=>0)
    TkGrid.columnconfigure(@frame, 0, 'weight'=>1, 'minsize'=>0)
    @canvas.grid('row'=>0, 'column'=>0, 'sticky'=>'news')
    @frame.grid_propagate(false)

    # assign scrollbars
    @canvas.xscrollbar(@h_scroll)
    @canvas.yscrollbar(@v_scroll)

    # convert hash keys
    keys = _symbolkey2str(keys)

    # check options for the frame
    framekeys = {}
    if keys.key?('classname')
       keys['class'] = keys.delete('classname')
    end
    if @classname = keys.delete('class')
      framekeys['class'] = @classname
    end
    if @colormap  = keys.delete('colormap')
      framekeys['colormap'] = @colormap
    end
    if @container = keys.delete('container')
      framekeys['container'] = @container
    end
    if @visual    = keys.delete('visual')
      framekeys['visual'] = @visual
    end
    if @classname.kind_of? TkBindTag
      @db_class = @classname
      @classname = @classname.id
    elsif @classname
      @db_class = TkDatabaseClass.new(@classname)
    else
      @db_class = self.class
      @classname = @db_class::WidgetClassName
    end

    # create base frame
    @base = TkFrame.new(@canvas, framekeys)

    # embed base frame
    @cwin = TkcWindow.new(@canvas, [0, 0], :window=>@base, :anchor=>'nw')
    @canvas.scrollregion(@cwin.bbox)

    # binding to reset scrollregion
    @base.bind('Configure'){ _reset_scrollregion(nil, nil) }

    # set default receiver of method calls
    @path = @base.path

    # scrollbars ON
    vscroll(keys.delete('vscroll'){true})
    hscroll(keys.delete('hscroll'){true})

    # please check the differences of the following definitions
    option_methods(
      :scrollbarwidth
    )

    # set receiver widgets for configure methods (with alias)
    delegate_alias('scrollbarrelief', 'relief', @h_scroll, @v_scroll)

    # set receiver widgets for configure methods
    delegate('DEFAULT', @base)
    delegate('background', @frame, @base, @canvas, @h_scroll, @v_scroll)
    delegate('width', @frame)
    delegate('height', @frame)
    delegate('activebackground', @h_scroll, @v_scroll)
    delegate('troughcolor', @h_scroll, @v_scroll)
    delegate('repeatdelay', @h_scroll, @v_scroll)
    delegate('repeatinterval', @h_scroll, @v_scroll)
    delegate('borderwidth', @frame)
    delegate('relief', @frame)

    # do configure
    configure keys unless keys.empty?
  end

  # callback for Configure event
  def _reset_scrollregion(h_mod=nil, v_mod=nil)
    cx1, cy1, cx2, cy2 = @canvas.scrollregion
    x1, y1, x2, y2 = @cwin.bbox
    @canvas.scrollregion([x1, y1, x2, y2])

    if h_mod.nil? && v_mod.nil?
      if x2 != cx2 && TkGrid.info(@h_scroll).size == 0
        @frame.grid_propagate(true)
        @canvas.width  = x2
        Tk.update_idletasks
        @frame.grid_propagate(false)
      end
      if y2 != cy2 && TkGrid.info(@v_scroll).size == 0
        @frame.grid_propagate(true)
        @canvas.height = y2
        Tk.update_idletasks
        @frame.grid_propagate(false)
      end
    else
      @h_scroll.ungrid if h_mod == false
      @v_scroll.ungrid if v_mod == false

      h_flag = (TkGrid.info(@h_scroll).size == 0)
      v_flag = (TkGrid.info(@v_scroll).size == 0)

      @frame.grid_propagate(true)

      @canvas.width  = (h_flag)? x2: @canvas.winfo_width
      @canvas.height = (v_flag)? y2: @canvas.winfo_height

      @h_scroll.grid('row'=>1, 'column'=>0, 'sticky'=>'ew') if h_mod
      @v_scroll.grid('row'=>0, 'column'=>1, 'sticky'=>'ns') if v_mod

      Tk.update_idletasks

      @frame.grid_propagate(false)
    end
  end
  private :_reset_scrollregion

  # forbid to change binding of @base frame
  def bind(*args)
    @frame.bind(*args)
  end
  def bind_append(*args)
    @frame.bind_append(*args)
  end
  def bind_remove(*args)
    @frame.bind_remove(*args)
  end
  def bindinfo(*args)
    @frame.bindinfo(*args)
  end

  # set width of scrollbar
  def scrollbarwidth(width = nil)
    if width
      @h_scroll.width(width)
      @v_scroll.width(width)
    else
      @h_scroll.width
    end
  end

  # vertical scrollbar : ON/OFF
  def vscroll(mode)
    Tk.update_idletasks
    st = TkGrid.info(@v_scroll)
    if mode && st.size == 0 then
      @v_scroll.grid('row'=>0, 'column'=>1, 'sticky'=>'ns')
      _reset_scrollregion(nil, true)
    elsif !mode && st.size != 0 then
      _reset_scrollregion(nil, false)
    else
      _reset_scrollregion(nil, nil)
    end
    self
  end

  # horizontal scrollbar : ON/OFF
  def hscroll(mode)
    Tk.update_idletasks
    st = TkGrid.info(@h_scroll)
    if mode && st.size == 0 then
      _reset_scrollregion(true, nil)
    elsif !mode && st.size != 0 then
      _reset_scrollregion(false, nil)
    else
      _reset_scrollregion(nil, nil)
    end
    self
  end
end

# test
if __FILE__ == $0
  f = Tk::RbWidget::ScrollFrame.new(:scrollbarwidth=>10,
                                    :width=>300, :height=>200)
  f.pack(:expand=>true, :fill=>:both)

  TkButton.new(f, :text=>'foo button', :command=>proc{puts 'foo'}).pack
  TkButton.new(f, :text=>'baaar button', :command=>proc{puts 'baaar'}).pack
  TkButton.new(f, :text=>'baz button', :command=>proc{puts 'baz'}).pack
  TkButton.new(f, :text=>'hoge hoge button',
               :command=>proc{puts 'hoge hoge'}).pack(:side=>:bottom)

  # f.hscroll(false)

  # add a text widget
  Tk.after(3000){
    t = TkText.new(f).pack(:expand=>true, :fill=>:both)
    t.insert(:end, "An example of Tk::RbWidget::ScrollFrame widget.\n\n")
    t.insert(:end, "Here is a text widget.\n")
    t.insert(:end, "Please resize the application window, ")
    t.insert(:end, "and try the scrollbars ")
    t.insert(:end, "to move the view of packed widgets.\n")
  }

  # remove a vertical scrollbar, and then the scrollframe is not scrollable.
  Tk.after(6000){ f.vscroll(false) }

  # add a vertical scrollbar, and make the scrollframe scrollable.
  Tk.after(9000){ f.vscroll(true) }

  # remove a horizontal scrollbar, and then the scrollframe is not scrollable.
  Tk.after(12000){ f.hscroll(false) }

  # add a horizontal scrollbar, and make the scrollframe scrollable.
  Tk.after(15000){ f.hscroll(true) }

  Tk.mainloop
end