1#
2# tk/grid.rb : control grid geometry manager
3#
4require 'tk'
5
6module TkGrid
7  include Tk
8  extend Tk
9
10  TkCommandNames = ['grid'.freeze].freeze
11
12  def anchor(master, anchor=None)
13    # master = master.epath if master.kind_of?(TkObject)
14    master = _epath(master)
15    tk_call_without_enc('grid', 'anchor', master, anchor)
16  end
17
18  def bbox(master, *args)
19    # master = master.epath if master.kind_of?(TkObject)
20    master = _epath(master)
21    args.unshift(master)
22    list(tk_call_without_enc('grid', 'bbox', *args))
23  end
24
25=begin
26  def configure(win, *args)
27    if args[-1].kind_of?(Hash)
28      opts = args.pop
29    else
30      opts = {}
31    end
32    params = []
33    params.push(_epath(win))
34    args.each{|win|
35      case win
36      when '-', 'x', '^'  # RELATIVE PLACEMENT
37        params.push(win)
38      else
39        params.push(_epath(win))
40      end
41    }
42    opts.each{|k, v|
43      params.push("-#{k}")
44      params.push((v.kind_of?(TkObject))? v.epath: v)
45    }
46    if Tk::TCL_MAJOR_VERSION < 8 ||
47        (Tk::TCL_MAJOR_VERSION == 8 && Tk::TCL_MINOR_VERSION <= 3)
48      if params[0] == '-' || params[0] == 'x' || params[0] == '^'
49        tk_call_without_enc('grid', *params)
50      else
51        tk_call_without_enc('grid', 'configure', *params)
52      end
53    else
54      tk_call_without_enc('grid', 'configure', *params)
55    end
56  end
57=end
58  def configure(*args)
59    if args[-1].kind_of?(Hash)
60      opts = args.pop
61    else
62      opts = {}
63    end
64    fail ArgumentError, 'no widget is given' if args.empty?
65    params = []
66    args.flatten(1).each{|win|
67      case win
68      when '-', ?-              # RELATIVE PLACEMENT (increase columnspan)
69        params.push('-')
70      when /^-+$/             # RELATIVE PLACEMENT (increase columnspan)
71        params.concat(win.to_s.split(//))
72      when '^', ?^              # RELATIVE PLACEMENT (increase rowspan)
73        params.push('^')
74      when /^\^+$/             # RELATIVE PLACEMENT (increase rowspan)
75        params.concat(win.to_s.split(//))
76      when 'x', :x, ?x, nil, '' # RELATIVE PLACEMENT (empty column)
77        params.push('x')
78      when /^x+$/             # RELATIVE PLACEMENT (empty column)
79        params.concat(win.to_s.split(//))
80      else
81        params.push(_epath(win))
82      end
83    }
84    opts.each{|k, v|
85      params.push("-#{k}")
86      params.push(_epath(v))  # have to use 'epath' (hash_kv() is unavailable)
87    }
88    if Tk::TCL_MAJOR_VERSION < 8 ||
89        (Tk::TCL_MAJOR_VERSION == 8 && Tk::TCL_MINOR_VERSION <= 3)
90      if params[0] == '-' || params[0] == 'x' || params[0] == '^'
91        tk_call_without_enc('grid', *params)
92      else
93        tk_call_without_enc('grid', 'configure', *params)
94      end
95    else
96      tk_call_without_enc('grid', 'configure', *params)
97    end
98  end
99  alias grid configure
100
101  def columnconfigure(master, index, args)
102    # master = master.epath if master.kind_of?(TkObject)
103    master = _epath(master)
104    tk_call_without_enc("grid", 'columnconfigure',
105                        master, index, *hash_kv(args))
106  end
107
108  def rowconfigure(master, index, args)
109    # master = master.epath if master.kind_of?(TkObject)
110    master = _epath(master)
111    tk_call_without_enc("grid", 'rowconfigure', master, index, *hash_kv(args))
112  end
113
114  def columnconfiginfo(master, index, slot=nil)
115    # master = master.epath if master.kind_of?(TkObject)
116    master = _epath(master)
117    if slot
118      case slot
119      when 'uniform', :uniform
120        tk_call_without_enc('grid', 'columnconfigure',
121                            master, index, "-#{slot}")
122      else
123        num_or_str(tk_call_without_enc('grid', 'columnconfigure',
124                                       master, index, "-#{slot}"))
125      end
126    else
127      #ilist = list(tk_call_without_enc('grid','columnconfigure',master,index))
128      ilist = simplelist(tk_call_without_enc('grid', 'columnconfigure',
129                                             master, index))
130      info = {}
131      while key = ilist.shift
132        case key
133        when 'uniform'
134          info[key[1..-1]] = ilist.shift
135        else
136          info[key[1..-1]] = tk_tcl2ruby(ilist.shift)
137        end
138      end
139      info
140    end
141  end
142
143  def rowconfiginfo(master, index, slot=nil)
144    # master = master.epath if master.kind_of?(TkObject)
145    master = _epath(master)
146    if slot
147      case slot
148      when 'uniform', :uniform
149        tk_call_without_enc('grid', 'rowconfigure',
150                            master, index, "-#{slot}")
151      else
152        num_or_str(tk_call_without_enc('grid', 'rowconfigure',
153                                       master, index, "-#{slot}"))
154      end
155    else
156      #ilist = list(tk_call_without_enc('grid', 'rowconfigure', master, index))
157      ilist = simplelist(tk_call_without_enc('grid', 'rowconfigure',
158                                             master, index))
159      info = {}
160      while key = ilist.shift
161        case key
162        when 'uniform'
163          info[key[1..-1]] = ilist.shift
164        else
165          info[key[1..-1]] = tk_tcl2ruby(ilist.shift)
166        end
167      end
168      info
169    end
170  end
171
172  def column(master, index, keys=nil)
173    if keys.kind_of?(Hash)
174      columnconfigure(master, index, keys)
175    else
176      columnconfiginfo(master, index, keys)
177    end
178  end
179
180  def row(master, index, keys=nil)
181    if keys.kind_of?(Hash)
182      rowconfigure(master, index, keys)
183    else
184      rowconfiginfo(master, index, keys)
185    end
186  end
187
188  def add(widget, *args)
189    configure(widget, *args)
190  end
191
192  def forget(*args)
193    return '' if args.size == 0
194    wins = args.collect{|win|
195      # (win.kind_of?(TkObject))? win.epath: win
196      _epath(win)
197    }
198    tk_call_without_enc('grid', 'forget', *wins)
199  end
200
201  def info(slave)
202    # slave = slave.epath if slave.kind_of?(TkObject)
203    slave = _epath(slave)
204    #ilist = list(tk_call_without_enc('grid', 'info', slave))
205    ilist = simplelist(tk_call_without_enc('grid', 'info', slave))
206    info = {}
207    while key = ilist.shift
208      #info[key[1..-1]] = ilist.shift
209      info[key[1..-1]] = tk_tcl2ruby(ilist.shift)
210    end
211    return info
212  end
213
214  def location(master, x, y)
215    # master = master.epath if master.kind_of?(TkObject)
216    master = _epath(master)
217    list(tk_call_without_enc('grid', 'location', master, x, y))
218  end
219
220  def propagate(master, mode=None)
221    # master = master.epath if master.kind_of?(TkObject)
222    master = _epath(master)
223    if mode == None
224      bool(tk_call_without_enc('grid', 'propagate', master))
225    else
226      tk_call_without_enc('grid', 'propagate', master, mode)
227    end
228  end
229
230  def remove(*args)
231    return '' if args.size == 0
232    wins = args.collect{|win|
233      # (win.kind_of?(TkObject))? win.epath: win
234      _epath(win)
235    }
236    tk_call_without_enc('grid', 'remove', *wins)
237  end
238
239  def size(master)
240    # master = master.epath if master.kind_of?(TkObject)
241    master = _epath(master)
242    list(tk_call_without_enc('grid', 'size', master))
243  end
244
245  def slaves(master, keys=nil)
246    # master = master.epath if master.kind_of?(TkObject)
247    master = _epath(master)
248    list(tk_call_without_enc('grid', 'slaves', master, *hash_kv(args)))
249  end
250
251  module_function :anchor, :bbox, :add, :forget, :propagate, :info
252  module_function :remove, :size, :slaves, :location
253  module_function :grid, :configure, :columnconfigure, :rowconfigure
254  module_function :column, :row, :columnconfiginfo, :rowconfiginfo
255end
256=begin
257def TkGrid(win, *args)
258  if args[-1].kind_of?(Hash)
259    opts = args.pop
260  else
261    opts = {}
262  end
263  params = []
264  params.push((win.kind_of?(TkObject))? win.epath: win)
265  args.each{|win|
266    case win
267    when '-', 'x', '^'  # RELATIVE PLACEMENT
268      params.push(win)
269    else
270      params.push((win.kind_of?(TkObject))? win.epath: win)
271    end
272  }
273  opts.each{|k, v|
274    params.push("-#{k}")
275    params.push((v.kind_of?(TkObject))? v.epath: v)
276  }
277  tk_call_without_enc("grid", *params)
278end
279=end
280