Class: TkWrapper::Widgets::Base::Widget

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/widgets/base/widget.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil, config: {}, childs: [], manager: nil, ids: [], id: []) ⇒ Widget

Returns a new instance of Widget.



22
23
24
25
26
27
28
29
30
31
# File 'lib/widgets/base/widget.rb', line 22

def initialize(parent: nil, config: {}, childs: [], manager: nil, ids: [], id: [])
  initialize_utilities
  @config = TkWrapper::Widgets::Base::Configuration.new(config)
  @manager = manager
  @ids = init_id(id) + init_id(ids)
  @parent = parent
  modify_configuration(@config)
  @childs = normalize_childs(childs)
  parent&.push(self)
end

Instance Attribute Details

#cellObject (readonly)

Returns the value of attribute cell.



18
19
20
# File 'lib/widgets/base/widget.rb', line 18

def cell
  @cell
end

#childsObject (readonly)

Returns the value of attribute childs.



18
19
20
# File 'lib/widgets/base/widget.rb', line 18

def childs
  @childs
end

#configObject

Returns the value of attribute config.



17
18
19
# File 'lib/widgets/base/widget.rb', line 17

def config
  @config
end

#fontObject (readonly)

Returns the value of attribute font.



18
19
20
# File 'lib/widgets/base/widget.rb', line 18

def font
  @font
end

#idsObject (readonly)

Returns the value of attribute ids.



18
19
20
# File 'lib/widgets/base/widget.rb', line 18

def ids
  @ids
end

#managerObject (readonly)

Returns the value of attribute manager.



18
19
20
# File 'lib/widgets/base/widget.rb', line 18

def manager
  @manager
end

#optsObject (readonly)

Returns the value of attribute opts.



18
19
20
# File 'lib/widgets/base/widget.rb', line 18

def opts
  @opts
end

#parentObject (readonly)

Returns the value of attribute parent.



18
19
20
# File 'lib/widgets/base/widget.rb', line 18

def parent
  @parent
end

#winfoObject (readonly)

Returns the value of attribute winfo.



18
19
20
# File 'lib/widgets/base/widget.rb', line 18

def winfo
  @winfo
end

Instance Method Details

#create_tk_widget(parent) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/widgets/base/widget.rb', line 49

def create_tk_widget(parent)
  tk_class = @config[:tk_class] || self.tk_class

  return unless tk_class

  tk_class.new(parent&.tk_widget)
end

#each(&block) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/widgets/base/widget.rb', line 65

def each(&block)
  nodes_to_walk = [self]
  until nodes_to_walk.empty?
    node = nodes_to_walk.pop
    block.call(node)
    nodes_to_walk = node.childs + nodes_to_walk
  end
end

#init_id(id) ⇒ Object



40
41
42
# File 'lib/widgets/base/widget.rb', line 40

def init_id(id)
  id.is_a?(Array) ? id : [id]
end

#initialize_utilitiesObject



33
34
35
36
37
38
# File 'lib/widgets/base/widget.rb', line 33

def initialize_utilities
  @cell   = TkWrapper::Util::Tk::Cell.new(self)
  @winfo  = TkWrapper::Widgets::Base::WindowInfo.new(self)
  @opts   = TkWrapper::Widgets::Base::TkOptions.new(self)
  @finder = TkWrapper::Util::Tk::Finder.new(widgets: self)
end

#normalize_childs(childs) ⇒ Object



44
45
46
47
# File 'lib/widgets/base/widget.rb', line 44

def normalize_childs(childs)
  childs = create_childs || childs
  childs.is_a?(Array) ? childs : [childs]
end

#push(child) ⇒ Object



74
75
76
77
# File 'lib/widgets/base/widget.rb', line 74

def push(child)
  @childs.push(child)
  child.build(self, manager: @manager)
end

#tk_classObject



20
# File 'lib/widgets/base/widget.rb', line 20

def tk_class() end

#tk_widget(parent = @parent) ⇒ Object

if parent is provided and self has no tk_class, the tk_widget of the parent is returned, if parent is not nil



59
60
61
62
63
# File 'lib/widgets/base/widget.rb', line 59

def tk_widget(parent = @parent)
  return @tk_widget if @tk_widget

  (@tk_widget = create_tk_widget(parent)) || parent&.tk_widget
end