2003-12-08 12:55:24 -05:00
|
|
|
#
|
|
|
|
# tkalignbox.rb : align widgets with same width/height
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-08 12:55:24 -05:00
|
|
|
# by Hidetoshi NAGAI
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# The box size depends on 'reqheight' and 'reqwidth' of contained widgets.
|
|
|
|
# If you want to give the box size when those requested sizes are 0,
|
|
|
|
# please set box.propagate = false (See the test routine at the tail of
|
2003-12-08 12:55:24 -05:00
|
|
|
# this file).
|
|
|
|
|
|
|
|
require 'tk'
|
|
|
|
|
2009-07-12 19:08:32 -04:00
|
|
|
module Tk
|
|
|
|
module RbWidget
|
|
|
|
class AlignBox < TkFrame
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Tk::RbWidget::AlignBox < TkFrame
|
2003-12-08 12:55:24 -05:00
|
|
|
def initialize(*args)
|
2009-07-12 19:08:32 -04:00
|
|
|
if self.class == Tk::RbWidget::AlignBox
|
|
|
|
fail RuntimeError, "Tk::AlignBox is an abstract class"
|
2003-12-09 09:38:15 -05:00
|
|
|
end
|
|
|
|
@padx = 0
|
|
|
|
@pady = 0
|
|
|
|
if args[-1].kind_of? Hash
|
|
|
|
keys = _symbolkey2str(args.pop)
|
|
|
|
@padx = keys.delete('padx') || 0
|
|
|
|
@pady = keys.delete('pady') || 0
|
|
|
|
args.push(keys)
|
|
|
|
end
|
2003-12-08 12:55:24 -05:00
|
|
|
super(*args)
|
|
|
|
@max_width = 0
|
|
|
|
@max_height = 0
|
|
|
|
@propagate = true
|
|
|
|
@widgets = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def _set_framesize
|
2009-07-12 19:08:32 -04:00
|
|
|
fail RuntimeError, "Tk::AlignBox is an abstract class"
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
|
|
|
private :_set_framesize
|
|
|
|
|
|
|
|
def _place_config(widget, idx, cnt)
|
2009-07-12 19:08:32 -04:00
|
|
|
fail RuntimeError, "Tk::AlignBox is an abstract class"
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
|
|
|
private :_place_config
|
|
|
|
|
|
|
|
def align
|
|
|
|
widgets = []
|
|
|
|
@widgets.each{|w| widgets << w if w.winfo_exist?}
|
|
|
|
@widgets = widgets
|
|
|
|
cnt = @widgets.size.to_f
|
|
|
|
@widgets.each_with_index{|w, idx| _place_config(w, idx, cnt)}
|
|
|
|
@widgets = widgets
|
|
|
|
_set_framesize if @propagate
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(*widgets)
|
|
|
|
widgets.each{|w|
|
|
|
|
unless w.kind_of? TkWindow
|
2004-10-11 00:51:21 -04:00
|
|
|
fail RuntimeError, "#{w.inspect} is not a widget instance."
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
|
|
|
@widgets.delete(w)
|
|
|
|
@widgets << w
|
|
|
|
sz = w.winfo_reqwidth
|
|
|
|
@max_width = sz if @max_width < sz
|
|
|
|
sz = w.winfo_reqheight
|
|
|
|
@max_height = sz if @max_height < sz
|
|
|
|
}
|
|
|
|
align
|
|
|
|
end
|
|
|
|
|
|
|
|
def <<(widget)
|
|
|
|
add(widget)
|
|
|
|
end
|
|
|
|
|
|
|
|
def insert(idx, widget)
|
|
|
|
unless widget.kind_of? TkWindow
|
|
|
|
fail RuntimeError, "#{widget.inspect} is not a widget instance."
|
|
|
|
end
|
|
|
|
@widgets.delete(widget)
|
|
|
|
@widgets[idx,0] = widget
|
|
|
|
sz = widget.winfo_reqwidth
|
|
|
|
@max_width = sz if @max_width < sz
|
|
|
|
sz = widget.winfo_reqheight
|
|
|
|
@max_height = sz if @max_height < sz
|
|
|
|
align
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(idx)
|
|
|
|
ret = @widgets.delete_at(idx)
|
|
|
|
@req_size = 0
|
|
|
|
@widget.each{|w|
|
|
|
|
sz = w.winfo_reqwidth
|
|
|
|
@max_width = sz if @max_width < sz
|
|
|
|
sz = w.winfo_reqheight
|
|
|
|
@max_height = sz if @max_height < sz
|
|
|
|
}
|
|
|
|
align
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2003-12-09 09:38:15 -05:00
|
|
|
def padx(size = nil)
|
|
|
|
if size
|
|
|
|
@padx = size
|
|
|
|
align
|
|
|
|
else
|
|
|
|
@padx
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def pady(size = nil)
|
|
|
|
if size
|
|
|
|
@pady = size
|
|
|
|
align
|
|
|
|
else
|
|
|
|
@pady
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-12-08 12:55:24 -05:00
|
|
|
attr_accessor :propagate
|
|
|
|
end
|
|
|
|
|
2009-07-12 19:08:32 -04:00
|
|
|
class Tk::RbWidget::HBox < Tk::RbWidget::AlignBox
|
2003-12-08 12:55:24 -05:00
|
|
|
def _set_framesize
|
|
|
|
bd = self.borderwidth
|
2003-12-09 09:38:15 -05:00
|
|
|
self.width((@max_width + 2*@padx) * @widgets.size + 2*bd)
|
|
|
|
self.height(@max_height + 2*@pady + 2*bd)
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
2003-12-09 09:38:15 -05:00
|
|
|
private :_set_framesize
|
|
|
|
|
2003-12-08 12:55:24 -05:00
|
|
|
def _place_config(widget, idx, cnt)
|
2009-03-05 22:56:38 -05:00
|
|
|
widget.place_in(self,
|
|
|
|
'relx'=>idx/cnt, 'x'=>@padx,
|
|
|
|
'rely'=>0, 'y'=>@pady,
|
|
|
|
'relwidth'=>1.0/cnt, 'width'=>-2*@padx,
|
2004-10-11 00:51:21 -04:00
|
|
|
'relheight'=>1.0, 'height'=>-2*@pady)
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
2003-12-09 09:38:15 -05:00
|
|
|
private :_place_config
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
2009-07-12 19:08:32 -04:00
|
|
|
Tk::RbWidget::HLBox = Tk::RbWidget::HBox
|
2003-12-08 12:55:24 -05:00
|
|
|
|
2009-07-12 19:08:32 -04:00
|
|
|
class Tk::RbWidget::HRBox < Tk::RbWidget::HBox
|
2003-12-08 12:55:24 -05:00
|
|
|
def _place_config(widget, idx, cnt)
|
2009-03-05 22:56:38 -05:00
|
|
|
widget.place_in(self,
|
|
|
|
'relx'=>(cnt - idx - 1)/cnt, 'x'=>@padx,
|
|
|
|
'rely'=>0, 'y'=>@pady,
|
|
|
|
'relwidth'=>1.0/cnt, 'width'=>-2*@padx,
|
2004-10-11 00:51:21 -04:00
|
|
|
'relheight'=>1.0, 'height'=>-2*@pady)
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
2003-12-09 09:38:15 -05:00
|
|
|
private :_place_config
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
|
|
|
|
2009-07-12 19:08:32 -04:00
|
|
|
class Tk::RbWidget::VBox < Tk::RbWidget::AlignBox
|
2003-12-08 12:55:24 -05:00
|
|
|
def _set_framesize
|
2003-12-09 09:38:15 -05:00
|
|
|
bd = self.borderwidth
|
|
|
|
self.width(@max_width + 2*@padx + 2*bd)
|
|
|
|
self.height((@max_height + 2*@pady) * @widgets.size + 2*bd)
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
2003-12-09 09:38:15 -05:00
|
|
|
private :_set_framesize
|
|
|
|
|
2003-12-08 12:55:24 -05:00
|
|
|
def _place_config(widget, idx, cnt)
|
2009-03-05 22:56:38 -05:00
|
|
|
widget.place_in(self,
|
|
|
|
'relx'=>0, 'x'=>@padx,
|
|
|
|
'rely'=>idx/cnt, 'y'=>@pady,
|
|
|
|
'relwidth'=>1.0, 'width'=>-2*@padx,
|
2004-10-11 00:51:21 -04:00
|
|
|
'relheight'=>1.0/cnt, 'height'=>-2*@pady)
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
2003-12-09 09:38:15 -05:00
|
|
|
private :_place_config
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
2009-07-12 19:08:32 -04:00
|
|
|
Tk::RbWidget::VTBox = Tk::RbWidget::VBox
|
2003-12-08 12:55:24 -05:00
|
|
|
|
2009-07-12 19:08:32 -04:00
|
|
|
class Tk::RbWidget::VBBox < Tk::RbWidget::VBox
|
2003-12-08 12:55:24 -05:00
|
|
|
def _place_config(widget, idx, cnt)
|
2009-03-05 22:56:38 -05:00
|
|
|
widget.place_in(self,
|
|
|
|
'relx'=>0, 'x'=>@padx,
|
|
|
|
'rely'=>(cnt - idx - 1)/cnt, 'y'=>@pady,
|
|
|
|
'relwidth'=>1.0, 'width'=>-2*@padx,
|
2004-10-11 00:51:21 -04:00
|
|
|
'relheight'=>1.0/cnt, 'height'=>-2*@pady)
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
2003-12-09 09:38:15 -05:00
|
|
|
private :_place_config
|
2003-12-08 12:55:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
################################################
|
|
|
|
# test
|
|
|
|
################################################
|
|
|
|
if __FILE__ == $0
|
2009-07-12 19:08:32 -04:00
|
|
|
f = Tk::RbWidget::HBox.new(:borderwidth=>3, :relief=>'ridge').pack
|
2003-12-09 09:38:15 -05:00
|
|
|
f.add(TkButton.new(f, :text=>'a'),
|
2004-10-11 00:51:21 -04:00
|
|
|
TkButton.new(f, :text=>'aa', :font=>'Helvetica 16'),
|
|
|
|
TkButton.new(f, :text=>'aaa'),
|
|
|
|
TkButton.new(f, :text=>'aaaa'))
|
2003-12-09 09:38:15 -05:00
|
|
|
|
2009-07-12 19:08:32 -04:00
|
|
|
f = Tk::RbWidget::HBox.new(:borderwidth=>3, :relief=>'ridge',
|
|
|
|
:padx=>7, :pady=>3, :background=>'yellow').pack
|
2003-12-08 12:55:24 -05:00
|
|
|
f.add(TkButton.new(f, :text=>'a'),
|
2004-10-11 00:51:21 -04:00
|
|
|
TkButton.new(f, :text=>'aa', :font=>'Helvetica 16'),
|
|
|
|
TkButton.new(f, :text=>'aaa'),
|
|
|
|
TkButton.new(f, :text=>'aaaa'))
|
2003-12-08 12:55:24 -05:00
|
|
|
|
2009-07-12 19:08:32 -04:00
|
|
|
f = Tk::RbWidget::VBox.new(:borderwidth=>5,
|
|
|
|
:relief=>'groove').pack(:fill=>:y, :expand=>true)
|
2003-12-08 12:55:24 -05:00
|
|
|
f.add(TkButton.new(f, :text=>'a'),
|
2004-10-11 00:51:21 -04:00
|
|
|
TkButton.new(f, :text=>'aa', :font=>'Helvetica 30'),
|
|
|
|
TkButton.new(f, :text=>'aaa'),
|
|
|
|
TkButton.new(f, :text=>'aaaa'))
|
2003-12-08 12:55:24 -05:00
|
|
|
|
2009-07-12 19:08:32 -04:00
|
|
|
f = Tk::RbWidget::HRBox.new(:borderwidth=>3,
|
|
|
|
:relief=>'raised').pack(:fill=>:x)
|
2003-12-08 12:55:24 -05:00
|
|
|
f.add(TkButton.new(f, :text=>'a'),
|
2009-03-05 22:56:38 -05:00
|
|
|
TkButton.new(f, :text=>'aa'),
|
2004-10-11 00:51:21 -04:00
|
|
|
TkButton.new(f, :text=>'aaa'))
|
2003-12-08 12:55:24 -05:00
|
|
|
|
2009-07-12 19:08:32 -04:00
|
|
|
f = Tk::RbWidget::VBBox.new(:borderwidth=>3,
|
|
|
|
:relief=>'ridge').pack(:fill=>:x)
|
2003-12-08 12:55:24 -05:00
|
|
|
f.propagate = false
|
|
|
|
f.height 100
|
2009-03-05 22:56:38 -05:00
|
|
|
f.add(TkFrame.new(f){|ff|
|
|
|
|
TkButton.new(ff, :text=>'a').pack(:pady=>4, :padx=>6,
|
2004-10-11 00:51:21 -04:00
|
|
|
:fill=>:both, :expand=>true)
|
2009-03-05 22:56:38 -05:00
|
|
|
},
|
|
|
|
TkFrame.new(f){|ff|
|
|
|
|
TkButton.new(ff, :text=>'aa').pack(:pady=>4, :padx=>6,
|
2004-10-11 00:51:21 -04:00
|
|
|
:fill=>:both, :expand=>true)
|
2009-03-05 22:56:38 -05:00
|
|
|
},
|
|
|
|
TkFrame.new(f){|ff|
|
|
|
|
TkButton.new(ff, :text=>'aaaa').pack(:pady=>4, :padx=>6,
|
2004-10-11 00:51:21 -04:00
|
|
|
:fill=>:both, :expand=>true)
|
|
|
|
})
|
2003-12-08 12:55:24 -05:00
|
|
|
|
|
|
|
Tk.mainloop
|
|
|
|
end
|