1 #
2 # tk/grid.rb : control grid geometry manager
3 #
4 require 'tk'
5
6 module 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 alias column columnconfigure
108
109 def rowconfigure(master, index, args)
110 # master = master.epath if master.kind_of?(TkObject)
111 master = _epath(master)
112 tk_call_without_enc("grid", 'rowconfigure', master, index, *hash_kv(args))
113 end
114 alias row rowconfigure
115
116 def columnconfiginfo(master, index, slot=nil)
117 # master = master.epath if master.kind_of?(TkObject)
118 master = _epath(master)
119 if slot
120 case slot
121 when 'uniform', :uniform
122 tk_call_without_enc('grid', 'columnconfigure',
123 master, index, "-#{slot}")
124 else
125 num_or_str(tk_call_without_enc('grid', 'columnconfigure',
126 master, index, "-#{slot}"))
127 end
128 else
129 #ilist = list(tk_call_without_enc('grid','columnconfigure',master,index))
130 ilist = simplelist(tk_call_without_enc('grid', 'columnconfigure',
131 master, index))
132 info = {}
133 while key = ilist.shift
134 case key
135 when 'uniform'
136 info[key[1..-1]] = ilist.shift
137 else
138 info[key[1..-1]] = tk_tcl2ruby(ilist.shift)
139 end
140 end
141 info
142 end
143 end
144
145 def rowconfiginfo(master, index, slot=nil)
146 # master = master.epath if master.kind_of?(TkObject)
147 master = _epath(master)
148 if slot
149 case slot
150 when 'uniform', :uniform
151 tk_call_without_enc('grid', 'rowconfigure',
152 master, index, "-#{slot}")
153 else
154 num_or_str(tk_call_without_enc('grid', 'rowconfigure',
155 master, index, "-#{slot}"))
156 end
157 else
158 #ilist = list(tk_call_without_enc('grid', 'rowconfigure', master, index))
159 ilist = simplelist(tk_call_without_enc('grid', 'rowconfigure',
160 master, index))
161 info = {}
162 while key = ilist.shift
163 case key
164 when 'uniform'
165 info[key[1..-1]] = ilist.shift
166 else
167 info[key[1..-1]] = tk_tcl2ruby(ilist.shift)
168 end
169 end
170 info
171 end
172 end
173
174 def add(widget, *args)
175 configure(widget, *args)
176 end
177
178 def forget(*args)
179 return '' if args.size == 0
180 wins = args.collect{|win|
181 # (win.kind_of?(TkObject))? win.epath: win
182 _epath(win)
183 }
184 tk_call_without_enc('grid', 'forget', *wins)
185 end
186
187 def info(slave)
188 # slave = slave.epath if slave.kind_of?(TkObject)
189 slave = _epath(slave)
190 #ilist = list(tk_call_without_enc('grid', 'info', slave))
191 ilist = simplelist(tk_call_without_enc('grid', 'info', slave))
192 info = {}
193 while key = ilist.shift
194 #info[key[1..-1]] = ilist.shift
195 info[key[1..-1]] = tk_tcl2ruby(ilist.shift)
196 end
197 return info
198 end
199
200 def location(master, x, y)
201 # master = master.epath if master.kind_of?(TkObject)
202 master = _epath(master)
203 list(tk_call_without_enc('grid', 'location', master, x, y))
204 end
205
206 def propagate(master, mode=None)
207 # master = master.epath if master.kind_of?(TkObject)
208 master = _epath(master)
209 if mode == None
210 bool(tk_call_without_enc('grid', 'propagate', master))
211 else
212 tk_call_without_enc('grid', 'propagate', master, mode)
213 end
214 end
215
216 def remove(*args)
217 return '' if args.size == 0
218 wins = args.collect{|win|
219 # (win.kind_of?(TkObject))? win.epath: win
220 _epath(win)
221 }
222 tk_call_without_enc('grid', 'remove', *wins)
223 end
224
225 def size(master)
226 # master = master.epath if master.kind_of?(TkObject)
227 master = _epath(master)
228 list(tk_call_without_enc('grid', 'size', master))
229 end
230
231 def slaves(master, args)
232 # master = master.epath if master.kind_of?(TkObject)
233 master = _epath(master)
234 list(tk_call_without_enc('grid', 'slaves', master, *hash_kv(args)))
235 end
236
237 module_function :anchor, :bbox, :add, :forget, :propagate, :info
238 module_function :remove, :size, :slaves, :location
239 module_function :grid, :configure, :columnconfigure, :rowconfigure
240 module_function :column, :row, :columnconfiginfo, :rowconfiginfo
241 end
242 =begin
243 def TkGrid(win, *args)
244 if args[-1].kind_of?(Hash)
245 opts = args.pop
246 else
247 opts = {}
248 end
249 params = []
250 params.push((win.kind_of?(TkObject))? win.epath: win)
251 args.each{|win|
252 case win
253 when '-', 'x', '^' # RELATIVE PLACEMENT
254 params.push(win)
255 else
256 params.push((win.kind_of?(TkObject))? win.epath: win)
257 end
258 }
259 opts.each{|k, v|
260 params.push("-#{k}")
261 params.push((v.kind_of?(TkObject))? v.epath: v)
262 }
263 tk_call_without_enc("grid", *params)
264 end
265 =end