Class: RETerm::Window

Inherits:
Object
  • Object
show all
Includes:
EventDispatcher
Defined in:
lib/reterm/window.rb

Overview

Windows are areas rendered on screen, associated with components to be rendered in them. They specify the position to start drawing component as well as the maximum width and height. A border may be drawn around a window and a ColorPair associated.

If Layout is added to a Window, children may subsequently be added. This should be performed via the Layout#add_child method.

Examples:

adding a layout to a window

init_reterm {
  win = Window.new :rows => 50, :cols => 30
  layout = Layouts::Horizontal.new
  win.component = layout

  child = layout.add_child :rows => 5, :cols => 10
  child.class # => RETerm::Window

  label = Components::Label.new :text => "Hello World!"
  child.component = label

  update_reterm
  sleep(5)
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventDispatcher

#dispatch, #handle

Constructor Details

#initialize(args = {}) ⇒ Window

Instantiate Window with given args. None are required, but unless :rows, :cols, :x, or :y is specified, window will be created in it’s default position.

This method will generate a unique id for each window and add it to a static registery for subsequent tracking.

Parameters:

  • args (Hash) (defaults to: {})

    arguments used to instantiate window

Options Hash (args):

  • :rows (Integer)

    number of rows to assign to window

  • :cols (Integer)

    number of cols to assign to window

  • :x (Integer)

    starting x position of window

  • :y (Integer)

    starting y position of window

  • :vborder (Integer)

    vertical border char

  • :hborder (Integer)

    horizontal border char

  • :component (Component)

    component to assign to window

  • :parent (Window)

    parent to assign to window, if set window will be created a a child, else it will be independently created & tracked.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/reterm/window.rb', line 80

def initialize(args={})
  @@registry ||= []
  @@registry  << self

  @rows = args[:rows] || (Terminal.rows - 1)
  @cols = args[:cols] || (Terminal.cols - 1)

  @x    = args[:x] || 0
  @y    = args[:y] || 0

  @vborder = args[:vborder] || 0
  @hborder = args[:hborder] || 0

  self.component = args[:component] if args.key?(:component)

  if args[:parent]
    @parent = args[:parent]
    @win = parent.win.derwin(@rows, @cols, @y, @x)

  else
    @parent = nil
    @win = Ncurses::WINDOW.new(@rows, @cols, @y, @x)
  end

  Ncurses::keypad(@win, true)

  @children = []


  @@wid ||= 0
  @@wid  += 1

  @window_id = @@wid
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



40
41
42
# File 'lib/reterm/window.rb', line 40

def children
  @children
end

#colsObject

Return window cols



246
247
248
# File 'lib/reterm/window.rb', line 246

def cols
  @cols
end

#componentObject

Returns the value of attribute component.



35
36
37
# File 'lib/reterm/window.rb', line 35

def component
  @component
end

#hborderObject

Returns the value of attribute hborder.



33
34
35
# File 'lib/reterm/window.rb', line 33

def hborder
  @hborder
end

#parentObject (readonly)

Returns the value of attribute parent.



39
40
41
# File 'lib/reterm/window.rb', line 39

def parent
  @parent
end

#rowsObject

Return window rows



241
242
243
# File 'lib/reterm/window.rb', line 241

def rows
  @rows
end

#vborderObject

Returns the value of attribute vborder.



32
33
34
# File 'lib/reterm/window.rb', line 32

def vborder
  @vborder
end

#winObject (readonly)

Returns the value of attribute win.



37
38
39
# File 'lib/reterm/window.rb', line 37

def win
  @win
end

#xObject

Returns the value of attribute x.



30
31
32
# File 'lib/reterm/window.rb', line 30

def x
  @x
end

#yObject

Returns the value of attribute y.



30
31
32
# File 'lib/reterm/window.rb', line 30

def y
  @y
end

Class Method Details

.allObject

Static method returning all tracked windows



137
138
139
140
# File 'lib/reterm/window.rb', line 137

def self.all
  @@registry ||= []
  @@registry
end

.topObject

Static method returning top level windows



143
144
145
146
# File 'lib/reterm/window.rb', line 143

def self.top
  @@registry ||= []
  @@registry.select { |w| !w.parent? }
end

Instance Method Details

#activate!Object

Activate window component



256
257
258
# File 'lib/reterm/window.rb', line 256

def activate!
  component.activate!
end

#border!Object

Draw Border around window



196
197
198
# File 'lib/reterm/window.rb', line 196

def border!
  @win.box(@vborder, @hborder)
end

#cdk?Boolean

Return bool indicating if cdk is enabled for this window/component

Returns:

  • (Boolean)


132
133
134
# File 'lib/reterm/window.rb', line 132

def cdk?
  !!@cdk_scr
end

#cdk_scrObject

Return cdk screen (only used by CDK components)



126
127
128
129
# File 'lib/reterm/window.rb', line 126

def cdk_scr
  enable_cdk!
  @cdk_scr ||= CDK::SCREEN.new(@win)
end

#clear!Object

Clear window by removing all children and reinitializing window space



168
169
170
171
172
173
174
175
# File 'lib/reterm/window.rb', line 168

def clear!
  children.each { |c|
    del_child(c)
  }

  @children = []
  erase
end

#colored?Boolean

Return bool indiciating if colors are set

Returns:

  • (Boolean)


211
212
213
# File 'lib/reterm/window.rb', line 211

def colored?
  !!@colors
end

#colors=(c) ⇒ Object

Set window color



216
217
218
219
220
221
222
223
224
225
# File 'lib/reterm/window.rb', line 216

def colors=(c)
  @colors = c.is_a?(ColorPair) ? c : ColorPair.for(c)
  @win.bkgd(Ncurses.COLOR_PAIR(@colors.id))

  component.colors = @colors if component?

  children.each { |ch|
    ch.colors = c
  }
end

#component?Boolean

Return bool indicating if this window has a component associated with it

Returns:

  • (Boolean)


44
45
46
# File 'lib/reterm/window.rb', line 44

def component?
  !!@component
end

#create_child(h = {}) ⇒ Object

Create child window, this method should not be invoked by end-user, rather it is is invoked the Layout#add_child is called.



151
152
153
154
155
156
# File 'lib/reterm/window.rb', line 151

def create_child(h={})
  c = self.class.new h.merge(:parent => self)
  c.colors = @colors if colored?
  children << c
  c
end

#del_child(child) ⇒ Object

Remove child window, like #add_child, this is used internally and should not be invoked by the end user



160
161
162
163
164
165
# File 'lib/reterm/window.rb', line 160

def del_child(child)
  @children.delete(child)
  @@registry.delete(child)
  child.finalize!
  child.win.delwin
end

#dimensionsObject

Return window dimensions as an array containing rows & cols



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/reterm/window.rb', line 228

def dimensions
  @dimensions ||=
    begin
      rows = []
      cols = []
      @win.getmaxyx(rows, cols)
      rows = rows.first
      cols = cols.first
      [rows, cols]
    end
end

#draw!Object

Draw component in window



251
252
253
# File 'lib/reterm/window.rb', line 251

def draw!
  component.draw! if component?
end

#eraseObject

Erase window drawing area



178
179
180
# File 'lib/reterm/window.rb', line 178

def erase
  @win.werase
end

#finalize!Object

Invoke window finalization routine by destroying it and all children



117
118
119
120
121
122
123
# File 'lib/reterm/window.rb', line 117

def finalize!
  children.each { |c|
    del_child c
    cdk_scr.destroy if cdk?
    component.finalize! if component?
  }
end

#getchObject

Blocking call to capture next character from window



201
202
203
# File 'lib/reterm/window.rb', line 201

def getch
  @win.getch
end

#mvaddstr(*a) ⇒ Object

Write string at specified loc



206
207
208
# File 'lib/reterm/window.rb', line 206

def mvaddstr(*a)
  @win.mvaddstr(*a)
end

#no_border!Object

Remove Border around window



191
192
193
# File 'lib/reterm/window.rb', line 191

def no_border!
  @win.border(' '.ord, ' '.ord, ' '.ord, ' '.ord, ' '.ord, ' '.ord, ' '.ord, ' '.ord)
end

#parent?Boolean

Return boolean if this window is a child of another

Returns:

  • (Boolean)


57
58
59
# File 'lib/reterm/window.rb', line 57

def parent?
  !!@parent
end

#refreshObject

Refresh / resynchronize window and all children



183
184
185
186
187
188
# File 'lib/reterm/window.rb', line 183

def refresh
  @win.refresh
  children.each { |c|
    c.refresh
  }
end