1#
2#  tkextlib/tcllib/autoscroll.rb
3#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4#
5#   * Part of tcllib extension
6#   * Provides for a scrollbar to automatically mapped and unmapped as needed
7#
8# (The following is the original description of the library.)
9#
10# This package allows scrollbars to be mapped and unmapped as needed
11# depending on the size and content of the scrollbars scrolled widget.
12# The scrollbar must be managed by either pack or grid, other geometry
13# managers are not supported.
14#
15# When managed by pack, any geometry changes made in the scrollbars parent
16# between the time a scrollbar is unmapped, and when it is mapped will be
17# lost. It is an error to destroy any of the scrollbars siblings while the
18# scrollbar is unmapped. When managed by grid, if anything becomes gridded
19# in the same row and column the scrollbar occupied it will be replaced by
20# the scrollbar when remapped.
21#
22# This package may be used on any scrollbar-like widget as long as it
23# supports the set subcommand in the same style as scrollbar. If the set
24# subcommand is not used then this package will have no effect.
25#
26
27require 'tk'
28require 'tk/scrollbar'
29require 'tkextlib/tcllib.rb'
30
31module Tk
32  module Tcllib
33    module Autoscroll
34      PACKAGE_NAME = 'autoscroll'.freeze
35      def self.package_name
36        PACKAGE_NAME
37      end
38
39      def self.package_version
40        begin
41          TkPackage.require('autoscroll')
42        rescue
43          ''
44        end
45      end
46
47      def self.not_available
48        fail RuntimeError, "'tkextlib/tcllib/autoscroll' extension is not available on your current environment."
49      end
50
51      def self.autoscroll(win)
52        Tk::Tcllib::Autoscroll.not_available
53      end
54
55      def self.unautoscroll(win)
56        Tk::Tcllib::Autoscroll.not_available
57      end
58    end
59  end
60end
61
62module Tk
63  module Scrollable
64    def autoscroll(mode = nil)
65      case mode
66      when :x, 'x'
67        if @xscrollbar
68          Tk::Tcllib::Autoscroll.autoscroll(@xscrollbar)
69        end
70      when :y, 'y'
71        if @yscrollbar
72          Tk::Tcllib::Autoscroll.autoscroll(@yscrollbar)
73        end
74      when nil, :both, 'both'
75        if @xscrollbar
76          Tk::Tcllib::Autoscroll.autoscroll(@xscrollbar)
77        end
78        if @yscrollbar
79          Tk::Tcllib::Autoscroll.autoscroll(@yscrollbar)
80        end
81      else
82        fail ArgumentError, "'x', 'y' or 'both' (String or Symbol) is expected"
83      end
84      self
85    end
86    def unautoscroll(mode = nil)
87      case mode
88      when :x, 'x'
89        if @xscrollbar
90          Tk::Tcllib::Autoscroll.unautoscroll(@xscrollbar)
91        end
92      when :y, 'y'
93        if @yscrollbar
94          Tk::Tcllib::Autoscroll.unautoscroll(@yscrollbar)
95        end
96      when nil, :both, 'both'
97        if @xscrollbar
98          Tk::Tcllib::Autoscroll.unautoscroll(@xscrollbar)
99        end
100        if @yscrollbar
101          Tk::Tcllib::Autoscroll.unautoscroll(@yscrollbar)
102        end
103      else
104        fail ArgumentError, "'x', 'y' or 'both' (String or Symbol) is expected"
105      end
106      self
107    end
108  end
109end
110
111class Tk::Scrollbar
112  def autoscroll
113    # Arranges for the already existing scrollbar to be mapped
114    # and unmapped as needed.
115    #tk_call_without_enc('::autoscroll::autoscroll', @path)
116    Tk::Tcllib::Autoscroll.autoscroll(self)
117    self
118  end
119  def unautoscroll
120    #     Returns the scrollbar to its original static state.
121    #tk_call_without_enc('::autoscroll::unautoscroll', @path)
122    Tk::Tcllib::Autoscroll.unautoscroll(self)
123    self
124  end
125end
126
127# TkPackage.require('autoscroll', '1.0')
128# TkPackage.require('autoscroll', '1.1')
129TkPackage.require('autoscroll')
130
131module Tk
132  module Tcllib
133    class << Autoscroll
134      undef not_available
135    end
136
137    module Autoscroll
138      extend TkCore
139      def self.autoscroll(win)
140        tk_call_without_enc('::autoscroll::autoscroll', win.path)
141      end
142
143      def self.unautoscroll(win)
144        tk_call_without_enc('::autoscroll::unautoscroll', win.path)
145      end
146
147      def self.wrap
148        # v1.1
149        tk_call_without_enc('::autoscroll::wrap')
150      end
151
152      def self.unwrap
153        # v1.1
154        tk_call_without_enc('::autoscroll::unwrap')
155      end
156    end
157  end
158end
159