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

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

Direct Known Subclasses

Entry, Frame, Grid, Label, Menu, Menu::Cascade, Menu::Command, Root, Text

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Widget.



19
20
21
22
23
24
25
26
# File 'lib/widgets/base/widget.rb', line 19

def initialize(config: {}, childs: [], manager: nil, ids: [])
  @cell = TkWrapper::Util::Tk::Cell.new(self)
  @finder = TkWrapper::Util::Tk::Finder.new(widgets: self)
  @config = TkWrapper::Widgets::Base::Configuration.new(config)
  @childs = childs.is_a?(Array) ? childs : [childs]
  @manager = manager
  @ids = ids.is_a?(Array) ? ids : [ids]
end

Instance Attribute Details

#cellObject (readonly)

Returns the value of attribute cell.



15
16
17
# File 'lib/widgets/base/widget.rb', line 15

def cell
  @cell
end

#childsObject (readonly)

Returns the value of attribute childs.



15
16
17
# File 'lib/widgets/base/widget.rb', line 15

def childs
  @childs
end

#configObject

Returns the value of attribute config.



14
15
16
# File 'lib/widgets/base/widget.rb', line 14

def config
  @config
end

#idsObject (readonly)

Returns the value of attribute ids.



15
16
17
# File 'lib/widgets/base/widget.rb', line 15

def ids
  @ids
end

#managerObject (readonly)

Returns the value of attribute manager.



15
16
17
# File 'lib/widgets/base/widget.rb', line 15

def manager
  @manager
end

#parentObject (readonly)

Returns the value of attribute parent.



15
16
17
# File 'lib/widgets/base/widget.rb', line 15

def parent
  @parent
end

Instance Method Details

#build(parent, configure: true, manager: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/widgets/base/widget.rb', line 44

def build(parent, configure: true, manager: nil)
  @parent = parent
  tk_widget # creates the widget if possible and not yet created
  @font = TkWrapper::Util::Tk::Font.new(tk_widget)
  @manager ||= manager
  @config.merge(*@manager.configurations(self)) if @manager
  self.configure if configure
  @manager&.execute_modifications(self)
  @manager&.widgets&.push(self)
  @childs.each { |child| child.build(self, manager: @manager) }
end

#configureObject



56
57
58
59
# File 'lib/widgets/base/widget.rb', line 56

def configure
  @config.configure_tk_widget(tk_widget)
  @config.configure_tearoff
end

#create_tk_widget(parent) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/widgets/base/widget.rb', line 28

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

  return unless tk_class

  parent&.tk_widget ? tk_class.new(parent.tk_widget) : tk_class.new
end

#each(&block) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/widgets/base/widget.rb', line 61

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

#push(child) ⇒ Object



70
71
72
73
# File 'lib/widgets/base/widget.rb', line 70

def push(child)
  @childs.push(child)
  child.build(self)
end

#tk_classObject



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

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



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

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

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