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, #apply_style, #attribute_setter, create, #font=, #get_attribute, #grid, #has_attributes_attribute?, #has_state?, #image_argument, #method_missing, #post_initialize_child, #respond_to?, #style=, 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, &block) ⇒ RootProxy

Returns a new instance of RootProxy.



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

def initialize(*args, &block)
  @tk = ::TkRoot.new
  @tk.minsize = DEFAULT_WIDTH, DEFAULT_HEIGHT
  initialize_defaults
  post_add_content if block.nil?
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



49
50
51
# File 'lib/glimmer/tk/root_proxy.rb', line 49

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

#handle_listener(listener_name, &listener) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/glimmer/tk/root_proxy.rb', line 107

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)


53
54
55
# File 'lib/glimmer/tk/root_proxy.rb', line 53

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

#heightObject



77
78
79
# File 'lib/glimmer/tk/root_proxy.rb', line 77

def height
  geometry.split(REGEX_GEOMETRY)[1].to_i
end

#height=(value) ⇒ Object



94
95
96
97
# File 'lib/glimmer/tk/root_proxy.rb', line 94

def height=(value)
  @height = value.to_i
  self.geometry = "#{@width || DEFAULT_WIDTH}x#{value.to_i}#{x_sign}#{abs_x}#{y_sign}#{abs_y}"
end

#openObject



45
46
47
# File 'lib/glimmer/tk/root_proxy.rb', line 45

def open
  start_event_loop
end

#post_add_contentObject



41
42
43
# File 'lib/glimmer/tk/root_proxy.rb', line 41

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

#set_attribute(attribute, *args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/glimmer/tk/root_proxy.rb', line 57

def set_attribute(attribute, *args)
  case attribute.to_s
  when 'iconphoto'
    args[0..-1] = [image_argument(args)]
    super
  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



121
122
123
124
125
126
127
128
129
130
# File 'lib/glimmer/tk/root_proxy.rb', line 121

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



73
74
75
# File 'lib/glimmer/tk/root_proxy.rb', line 73

def width
  geometry.split(REGEX_GEOMETRY)[0].to_i
end

#width=(value) ⇒ Object



89
90
91
92
# File 'lib/glimmer/tk/root_proxy.rb', line 89

def width=(value)
  @width = value.to_i
  self.geometry = "#{value.to_i}x#{@height || DEFAULT_HEIGHT}#{x_sign}#{abs_x}#{y_sign}#{abs_y}"
end

#xObject



81
82
83
# File 'lib/glimmer/tk/root_proxy.rb', line 81

def x
  sign_number(x_sign, geometry.split(REGEX_GEOMETRY)[2].to_i)
end

#x=(value) ⇒ Object



99
100
101
# File 'lib/glimmer/tk/root_proxy.rb', line 99

def x=(value)
  self.geometry = "#{@width || DEFAULT_WIDTH}x#{@height || DEFAULT_HEIGHT}#{value.to_i > 0 ? '+' : '-'}#{value.to_i.abs}#{y_sign}#{abs_y}"
end

#yObject



85
86
87
# File 'lib/glimmer/tk/root_proxy.rb', line 85

def y
  sign_number(y_sign, geometry.split(REGEX_GEOMETRY)[3].to_i)
end

#y=(value) ⇒ Object



103
104
105
# File 'lib/glimmer/tk/root_proxy.rb', line 103

def y=(value)
  self.geometry = "#{@width || DEFAULT_WIDTH}x#{@height || DEFAULT_HEIGHT}#{x_sign}#{abs_x}#{value.to_i > 0 ? '+' : '-'}#{value.to_i.abs}"
end