Class: Glimmer::Tk::RootProxy

Inherits:
WidgetProxy show all
Defined in:
lib/glimmer/tk/root_proxy.rb

Overview

Proxy for TkRoot

Follows the Proxy Design Pattern

Constant Summary collapse

REGEX_GEOMETRY =
/[x+-]/
DEFAULT_WIDTH =
190
DEFAULT_HEIGHT =
95

Instance Attribute Summary

Attributes inherited from WidgetProxy

#args, #children, #keyword, #parent_proxy, #tk

Instance Method Summary collapse

Methods inherited from WidgetProxy

#add_observer, #attribute_setter, create, #grid, #has_attributes_attribute?, #has_state?, #image_argument, #method_missing, #post_initialize_child, #respond_to?, tk_widget_class_for, #tk_widget_has_attribute_getter_setter?, #tk_widget_has_attribute_setter?, #widget_attribute_listener_installers, #widget_custom_attribute_mapping, widget_exists?, widget_proxy_class

Constructor Details

#initialize(*args) ⇒ RootProxy

Returns a new instance of RootProxy.



34
35
36
37
# File 'lib/glimmer/tk/root_proxy.rb', line 34

def initialize(*args)
  @tk = ::TkRoot.new
  @tk.minsize = DEFAULT_WIDTH, DEFAULT_HEIGHT
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Glimmer::Tk::WidgetProxy

Instance Method Details

#content(&block) ⇒ Object



47
48
49
# File 'lib/glimmer/tk/root_proxy.rb', line 47

def content(&block)
  Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Tk::RootExpression.new, keyword, *args, &block)
end

#get_attribute(attribute) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/glimmer/tk/root_proxy.rb', line 81

def get_attribute(attribute)
  attribute = attribute.to_s
  case attribute
  when 'width'
    geometry.split(REGEX_GEOMETRY)[0].to_i
  when 'height'
    geometry.split(REGEX_GEOMETRY)[1].to_i
  when 'x'
    sign_number(x_sign, geometry.split(REGEX_GEOMETRY)[2].to_i)
  when 'y'
    sign_number(y_sign, geometry.split(REGEX_GEOMETRY)[3].to_i)
  else
    super
  end
end

#handle_listener(listener_name, &listener) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/glimmer/tk/root_proxy.rb', line 129

def handle_listener(listener_name, &listener)
  case listener_name.to_s.upcase
  when 'WM_DELETE_WINDOW', 'DELETE_WINDOW'
    listener_name = 'WM_DELETE_WINDOW'
    @tk.protocol(listener_name, &listener)
  when 'WM_OPEN_WINDOW', 'OPEN_WINDOW'
    @on_open_window_procs ||= []
    @on_open_window_procs << listener
  else
    super
  end
end

#has_attribute?(attribute, *args) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/glimmer/tk/root_proxy.rb', line 51

def has_attribute?(attribute, *args)
  %w[width height x y].include?(attribute.to_s) || super
end

#heightObject



101
102
103
# File 'lib/glimmer/tk/root_proxy.rb', line 101

def height
  get_attribute(:height)
end

#height=(value) ⇒ Object



117
118
119
# File 'lib/glimmer/tk/root_proxy.rb', line 117

def height=(value)
  set_attribute(:height, value)
end

#openObject



43
44
45
# File 'lib/glimmer/tk/root_proxy.rb', line 43

def open
  start_event_loop
end

#post_add_contentObject



39
40
41
# File 'lib/glimmer/tk/root_proxy.rb', line 39

def post_add_content
  set_attribute('iconphoto', File.expand_path('../../../icons/glimmer.png', __dir__)) if @tk.iconphoto.nil?
end

#set_attribute(attribute, *args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/glimmer/tk/root_proxy.rb', line 55

def set_attribute(attribute, *args)
  case attribute.to_s
  when 'iconphoto'
    args[0..-1] = [image_argument(args)]
    super
  when 'width'
    @width = args.first.to_i
    self.geometry = "#{args.first.to_i}x#{@height || DEFAULT_HEIGHT}#{x_sign}#{abs_x}#{y_sign}#{abs_y}"
  when 'height'
    @height = args.first.to_i
    self.geometry = "#{@width || DEFAULT_WIDTH}x#{args.first.to_i}#{x_sign}#{abs_x}#{y_sign}#{abs_y}"
  when 'x'
    self.geometry = "#{@width || DEFAULT_WIDTH}x#{@height || DEFAULT_HEIGHT}#{args.first.to_i > 0 ? '+' : '-'}#{args.first.to_i.abs}#{y_sign}#{abs_y}"
  when 'y'
    self.geometry = "#{@width || DEFAULT_WIDTH}x#{@height || DEFAULT_HEIGHT}#{x_sign}#{abs_x}#{args.first.to_i > 0 ? '+' : '-'}#{args.first.to_i.abs}"
  when 'resizable'
    if args.size == 1 && !args.first.is_a?(Array)
      self.resizable = [args.first]*2
    else
      super
    end
  else
    super
  end
end

#start_event_loopObject

Starts Tk mainloop



143
144
145
146
147
148
149
150
151
152
# File 'lib/glimmer/tk/root_proxy.rb', line 143

def start_event_loop
  if @on_open_window_procs.to_a.any?
    ::Tk.after(100) do # ensure root window showed up first
      @on_open_window_procs.to_a.each do |on_open_window|
        ::Tk.after(0, on_open_window)
      end
    end
  end
  ::Tk.mainloop
end

#widthObject



97
98
99
# File 'lib/glimmer/tk/root_proxy.rb', line 97

def width
  get_attribute(:width)
end

#width=(value) ⇒ Object



113
114
115
# File 'lib/glimmer/tk/root_proxy.rb', line 113

def width=(value)
  set_attribute(:width, value)
end

#xObject



105
106
107
# File 'lib/glimmer/tk/root_proxy.rb', line 105

def x
  get_attribute(:x)
end

#x=(value) ⇒ Object



121
122
123
# File 'lib/glimmer/tk/root_proxy.rb', line 121

def x=(value)
  set_attribute(:x, value)
end

#yObject



109
110
111
# File 'lib/glimmer/tk/root_proxy.rb', line 109

def y
  get_attribute(:y)
end

#y=(value) ⇒ Object



125
126
127
# File 'lib/glimmer/tk/root_proxy.rb', line 125

def y=(value)
  set_attribute(:y, value)
end