📄 tkcanvas.rb
字号:
@c.itemtype @id end # Followings operators supports logical expressions of canvas tags # (for Tk8.3+). # If tag1.path is 't1' and tag2.path is 't2', then # ltag = tag1 & tag2; ltag.path => "(t1)&&(t2)" # ltag = tag1 | tag2; ltag.path => "(t1)||(t2)" # ltag = tag1 ^ tag2; ltag.path => "(t1)^(t2)" # ltag = - tag1; ltag.path => "!(t1)" def & (tag) if tag.kind_of? TkObject TkcTagString.new(@c, '(' + @id + ')&&(' + tag.path + ')') else TkcTagString.new(@c, '(' + @id + ')&&(' + tag.to_s + ')') end end def | (tag) if tag.kind_of? TkObject TkcTagString.new(@c, '(' + @id + ')||(' + tag.path + ')') else TkcTagString.new(@c, '(' + @id + ')||(' + tag.to_s + ')') end end def ^ (tag) if tag.kind_of? TkObject TkcTagString.new(@c, '(' + @id + ')^(' + tag.path + ')') else TkcTagString.new(@c, '(' + @id + ')^(' + tag.to_s + ')') end end def -@ TkcTagString.new(@c, '!(' + @id + ')') endendclass TkcTag<TkObject include TkcTagAccess CTagID_TBL = {} def TkcTag.id2obj(canvas, id) cpath = canvas.path return id unless CTagID_TBL[cpath] CTagID_TBL[cpath][id]? CTagID_TBL[cpath][id]: id end Tk_CanvasTag_ID = ['ctag0000'] def initialize(parent, mode=nil, *args) if not parent.kind_of?(TkCanvas) fail format("%s need to be TkCanvas", parent.inspect) end @c = parent @cpath = parent.path @path = @id = Tk_CanvasTag_ID[0] CTagID_TBL[@cpath] = {} unless CTagID_TBL[@cpath] CTagID_TBL[@cpath][@id] = self Tk_CanvasTag_ID[0] = Tk_CanvasTag_ID[0].succ if mode tk_call @c.path, "addtag", @id, mode, *args end end def id return @id end def delete @c.delete @id CTagID_TBL[@path][@id] = nil if CTagID_TBL[@path] end alias remove delete alias destroy delete def set_to_above(target) @c.addtag_above(@id, target) end alias above set_to_above def set_to_all @c.addtag_all(@id) end alias all set_to_all def set_to_below(target) @c.addtag_below(@id, target) end alias below set_to_below def set_to_closest(x, y, halo=None, start=None) @c.addtag_closest(@id, x, y, halo, start) end alias closest set_to_closest def set_to_enclosed(x1, y1, x2, y2) @c.addtag_enclosed(@id, x1, y1, x2, y2) end alias enclosed set_to_enclosed def set_to_overlapping(x1, y1, x2, y2) @c.addtag_overlapping(@id, x1, y1, x2, y2) end alias overlapping set_to_overlapping def set_to_withtag(target) @c.addtag_withtag(@id, target) end alias withtag set_to_withtagendclass TkcTagString<TkcTag def self.new(parent, name, *args) if CTagID_TBL[parent.path] && CTagID_TBL[parent.path][name] return CTagID_TBL[parent.path][name] else super(parent, name, *args) end end def initialize(parent, name, mode=nil, *args) if not parent.kind_of?(TkCanvas) fail format("%s need to be TkCanvas", parent.inspect) end @c = parent @cpath = parent.path @path = @id = name CTagID_TBL[@cpath] = {} unless CTagID_TBL[@cpath] CTagID_TBL[@cpath][@id] = self if mode tk_call @c.path, "addtag", @id, mode, *args end endendclass TkcTagAll<TkcTag def initialize(parent) if not parent.kind_of?(TkCanvas) fail format("%s need to be TkCanvas", parent.inspect) end @c = parent @cpath = parent.path @path = @id = 'all' CTagID_TBL[@cpath] = {} unless CTagID_TBL[@cpath] CTagID_TBL[@cpath][@id] = self endendclass TkcTagCurrent<TkcTag def initialize(parent) if not parent.kind_of?(TkCanvas) fail format("%s need to be TkCanvas", parent.inspect) end @c = parent @cpath = parent.path @path = @id = 'current' CTagID_TBL[@cpath] = {} unless CTagID_TBL[@cpath] CTagID_TBL[@cpath][@id] = self endendclass TkcGroup<TkcTag Tk_cGroup_ID = ['tkcg00000'] def create_self(parent, *args) if not parent.kind_of?(TkCanvas) fail format("%s need to be TkCanvas", parent.inspect) end @c = parent @cpath = parent.path @path = @id = Tk_cGroup_ID[0] CTagID_TBL[@cpath] = {} unless CTagID_TBL[@cpath] CTagID_TBL[@cpath][@id] = self Tk_cGroup_ID[0] = Tk_cGroup_ID[0].succ add(*args) if args != [] end def include(*tags) for i in tags i.addtag @id end end def exclude(*tags) for i in tags i.delete @id end endendclass TkcItem<TkObject include TkcTagAccess CItemTypeToClass = {} CItemID_TBL = {} def TkcItem.type2class(type) CItemTypeToClass[type] end def TkcItem.id2obj(canvas, id) cpath = canvas.path return id unless CItemID_TBL[cpath] CItemID_TBL[cpath][id]? CItemID_TBL[cpath][id]: id end def initialize(parent, *args) if not parent.kind_of?(TkCanvas) fail format("%s need to be TkCanvas", parent.inspect) end @parent = @c = parent @path = parent.path fontkeys = {} if args[-1].kind_of? Hash args = args.dup keys = args.pop ['font', 'kanjifont', 'latinfont', 'asciifont'].each{|key| fontkeys[key] = keys.delete(key) if keys.key?(key) } args += hash_kv(keys) end @id = create_self(*args).to_i ;# 'canvas item id' is integer number CItemID_TBL[@path] = {} unless CItemID_TBL[@path] CItemID_TBL[@path][@id] = self font_configure(fontkeys) unless fontkeys.empty?######## old version# if args[-1].kind_of? Hash# keys = args.pop# end# @id = create_self(*args).to_i ;# 'canvas item id' is integer number# CItemID_TBL[@path] = {} unless CItemID_TBL[@path]# CItemID_TBL[@path][@id] = self# if keys# # tk_call @path, 'itemconfigure', @id, *hash_kv(keys)# configure(keys) if keys# end######## end def create_self(*args); end private :create_self def id return @id end def delete @c.delete @id CItemID_TBL[@path][@id] = nil if CItemID_TBL[@path] end alias remove delete alias destroy deleteendclass TkcArc<TkcItem CItemTypeToClass['arc'] = self def create_self(*args) tk_call(@path, 'create', 'arc', *args) endendclass TkcBitmap<TkcItem CItemTypeToClass['bitmap'] = self def create_self(*args) tk_call(@path, 'create', 'bitmap', *args) endendclass TkcImage<TkcItem CItemTypeToClass['image'] = self def create_self(*args) tk_call(@path, 'create', 'image', *args) endendclass TkcLine<TkcItem CItemTypeToClass['line'] = self def create_self(*args) tk_call(@path, 'create', 'line', *args) endendclass TkcOval<TkcItem CItemTypeToClass['oval'] = self def create_self(*args) tk_call(@path, 'create', 'oval', *args) endendclass TkcPolygon<TkcItem CItemTypeToClass['polygon'] = self def create_self(*args) tk_call(@path, 'create', 'polygon', *args) endendclass TkcRectangle<TkcItem CItemTypeToClass['rectangle'] = self def create_self(*args) tk_call(@path, 'create', 'rectangle', *args) endendclass TkcText<TkcItem CItemTypeToClass['text'] = self def create_self(*args) tk_call(@path, 'create', 'text', *args) endendclass TkcWindow<TkcItem CItemTypeToClass['window'] = self def create_self(*args) tk_call(@path, 'create', 'window', *args) endendclass TkImage<TkObject include Tk Tk_IMGTBL = {} Tk_Image_ID = ['i00000'] def initialize(keys=nil) @path = Tk_Image_ID[0] Tk_Image_ID[0] = Tk_Image_ID[0].succ tk_call 'image', 'create', @type, @path, *hash_kv(keys) Tk_IMGTBL[@path] = self end def delete Tk_IMGTBL[@id] = nil if @id tk_call('image', 'delete', @path) end def height number(tk_call('image', 'height', @path)) end def inuse bool(tk_call('image', 'inuse', @path)) end def itemtype tk_call('image', 'type', @path) end def width number(tk_call('image', 'width', @path)) end def TkImage.names Tk.tk_call('image', 'names').split.collect!{|id| (Tk_IMGTBL[id])? Tk_IMGTBL[id] : id } end def TkImage.types Tk.tk_call('image', 'types').split endendclass TkBitmapImage<TkImage def initialize(*args) @type = 'bitmap' super endendclass TkPhotoImage<TkImage def initialize(*args) @type = 'photo' super end def blank tk_send 'blank' end def cget(option) case option when 'data', 'file' tk_send 'cget', option else tk_tcl2ruby tk_send 'cget', option end end def copy(source, *opts) args = opts.collect{|term| if term.kind_of?(String) && term.include?(?\s) term.split else term end }.flatten tk_send 'copy', source, *args end def data(keys=nil) tk_send 'data', *hash_kv(keys) end def get(x, y) tk_send 'get', x, y end def put(data, *to) if to == [] tk_send 'put', data else tk_send 'put', data, '-to', *to end end def read(file, *opts) args = opts.collect{|term| if term.kind_of?(String) && term.include?(?\s) term.split else term end }.flatten tk_send 'read', file, *args end def redither tk_send 'redither' end def write(file, *opts) args = opts.collect{|term| if term.kind_of?(String) && term.include?(?\s) term.split else term end }.flatten tk_send 'write', file, *args endend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -