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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Widget.



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

def initialize(config: {}, childs: [], id: nil)
  @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]
  @ids = []
  add_ids(id)
  add_ids(config[:id])
end

Instance Attribute Details

#cellObject (readonly)

Returns the value of attribute cell.



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

def cell
  @cell
end

#childsObject (readonly)

Returns the value of attribute childs.



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

def childs
  @childs
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#idsObject (readonly)

Returns the value of attribute ids.



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

def ids
  @ids
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Class Method Details

.config(matcher = nil, configuration = nil, **configurations) ⇒ Object



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

def self.config(matcher = nil, configuration = nil, **configurations)
  manager.add_configurations(matcher, configuration, **configurations)
end

.managerObject



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

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

.modify(matcher, &callback) ⇒ Object



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

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

Instance Method Details

#add_ids(ids) ⇒ Object



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

def add_ids(ids)
  return unless ids

  ids = [ids] unless ids.is_a?(Array)
  @ids.concat(ids)
end

#build(parent, configure: true) ⇒ Object



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

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

#configureObject



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

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

#create_tk_widget(parent) ⇒ Object



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

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



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

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

#managerObject



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

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

#modify(matchers, &callback) ⇒ Object



103
104
105
106
107
# File 'lib/widgets/base/widget.rb', line 103

def modify(matchers, &callback)
  items = find_all(matchers)

  callback.call(items)
end

#modify_each(matchers, &callback) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/widgets/base/widget.rb', line 109

def modify_each(matchers, &callback)
  items = find_all(matchers)

  return unless items

  with_match = items[0].is_a?(Array)

  items.each do |item|
    with_match ? callback.call(item[0], item[1]) : callback.call(item)
  end
end

#push(child) ⇒ Object



92
93
94
95
# File 'lib/widgets/base/widget.rb', line 92

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

#tk_classObject



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

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



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

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

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