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.



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

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 = nil, configuration = nil, **configurations) ⇒ Object



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

def self.config(matcher = nil, configuration = nil, **configurations)
  manager.add_configurations(matcher, configuration, **configurations)
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



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

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



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

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



76
77
78
79
80
# File 'lib/widgets/base/widget.rb', line 76

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

#create_tk_widget(parent) ⇒ Object



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

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



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

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



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/widgets/base/widget.rb', line 105

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

#idsObject



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

def ids
  case @id
  when Array then @id
  when nil then   []
  else            [@id]
  end
end

#managerObject



37
38
39
# File 'lib/widgets/base/widget.rb', line 37

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

#push(child) ⇒ Object



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

def push(child)
  @childs.push(child)
  child.build(self)
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



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

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

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