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

Inherits:
Object
  • Object
show all
Includes:
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Widget.



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

def initialize(config: {}, childs: [])
  @config = TkWrapper::Widgets::Base::Configuration.new(config)
  @childs = childs.is_a?(Array) ? childs : [childs]
  @id = config[:id]
end

Instance Attribute Details

#childsObject (readonly)

Returns the value of attribute childs.



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

def childs
  @childs
end

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/widgets/base/widget.rb', line 12

def config
  @config
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Class Method Details

.config(matcher, configuration) ⇒ Object



21
22
23
# File 'lib/widgets/base/widget.rb', line 21

def self.config(matcher, configuration)
  manager.add_configuration(matcher, configuration)
end

.managerObject



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

def self.manager
  @manager ||= TkWrapper::Widgets::Base::Manager.new
end

.modify(matcher, &callback) ⇒ Object



25
26
27
# File 'lib/widgets/base/widget.rb', line 25

def self.modify(matcher, &callback)
  manager.add_modification(matcher, &callback)
end

Instance Method Details

#build(parent, configure: true) ⇒ Object



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

def build(parent, configure: true)
  @parent = parent
  tk_widget # creates the widget if possible and not yet created
  self.configure if configure
  manager.execute_modifications(self)
  @childs.each { |child| child.build(self) }
end

#check_match(matcher) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/widgets/base/widget.rb', line 69

def check_match(matcher)
  case matcher
  when Regexp
    matcher.match(@id)
  when String, Symbol
    matcher == @id
  when nil
    true
  else
    is_a?(matcher)
  end
end

#configureObject



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

def configure
  @config.merge_global_configurations(manager, self)
  @config.configure_tk_widget(tk_widget)
  @config.configure_tearoff
end

#create_tk_widget(parent) ⇒ Object



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

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

#find(matcher) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/widgets/base/widget.rb', line 82

def find(matcher)
  nodes_to_scan = [self]
  until nodes_to_scan.empty?
    node = nodes_to_scan.pop
    return node if node.check_match(matcher)

    nodes_to_scan = node.childs + nodes_to_scan
  end
end

#find_all(matcher) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/widgets/base/widget.rb', line 92

def find_all(matcher)
  found_nodes = []
  nodes_to_scan = [self]

  until nodes_to_scan.empty?
    node = nodes_to_scan.pop
    found_nodes.push(node) if node.check_match(matcher)

    nodes_to_scan = node.childs + nodes_to_scan
  end

  found_nodes
end

#managerObject



29
30
31
# File 'lib/widgets/base/widget.rb', line 29

def manager
  TkWrapper::Widgets::Base::Widget.manager
end

#tk_classObject



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

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



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

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

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