Class: VER::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/ver/window.rb

Direct Known Subclasses

Pad, SubWindow

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(layout) ⇒ Window

Returns a new instance of Window.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ver/window.rb', line 11

def initialize(layout)
  @visible = true
  reset_layout(layout)

  @window = Ncurses::WINDOW.new(height, width, top, left)
  @panel = Ncurses::Panel.new_panel(@window)
  init_vars
  ## eeks XXX next line will wreak havoc when multiple windows opened like a mb or popup
  #$error_message_row = $status_message_row = Ncurses.LINES-1
  $error_message_row ||= Ncurses.LINES-1
  $error_message_col ||= 1


end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



99
100
101
# File 'lib/ver/window.rb', line 99

def method_missing(meth, *args)
  @window.send(meth, *args)
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



4
5
6
# File 'lib/ver/window.rb', line 4

def height
  @height
end

#layoutObject

Returns the value of attribute layout.



5
6
7
# File 'lib/ver/window.rb', line 5

def layout
  @layout
end

#leftObject (readonly)

Returns the value of attribute left.



4
5
6
# File 'lib/ver/window.rb', line 4

def left
  @left
end

#modifiedObject

has it been modified and may need a refresh



9
10
11
# File 'lib/ver/window.rb', line 9

def modified
  @modified
end

#nameObject

more for debugging log files. 2010-02-02 19:58



8
9
10
# File 'lib/ver/window.rb', line 8

def name
  @name
end

#panelObject (readonly)

reader requires so he can del it in end



6
7
8
# File 'lib/ver/window.rb', line 6

def panel
  @panel
end

#topObject (readonly)

Returns the value of attribute top.



4
5
6
# File 'lib/ver/window.rb', line 4

def top
  @top
end

#widthObject (readonly)

Returns the value of attribute width.



4
5
6
# File 'lib/ver/window.rb', line 4

def width
  @width
end

#window_typeObject (readonly)

window or pad to distinguish 2009-11-02 23:11



7
8
9
# File 'lib/ver/window.rb', line 7

def window_type
  @window_type
end

Class Method Details

.create_window(h = 0, w = 0, t = 0, l = 0) ⇒ Object

2009-10-13 12:24 not used as yet this is an alternative constructor created if you don’t want to create a hash first



48
49
50
51
52
# File 'lib/ver/window.rb', line 48

def self.create_window(h=0, w=0, t=0, l=0)
  layout = { :height => h, :width => w, :top => t, :left => l }
  @window = Window.new(layout)
  return @window
end

.root_window(layout = { :height => 0, :width => 0, :top => 0, :left => 0 }) ⇒ Object

this is an alternative constructor



35
36
37
38
39
40
41
42
43
# File 'lib/ver/window.rb', line 35

def self.root_window(layout = { :height => 0, :width => 0, :top => 0, :left => 0 })
  #VER::start_ncurses
  @layout = layout
  @window = Window.new(@layout)
  @window.name = "Window::ROOTW"
  @window.wrefresh
  Ncurses::Panel.update_panels
  return @window
end

Instance Method Details

#_subwin(layout) ⇒ Object



443
444
445
446
447
448
449
450
451
452
# File 'lib/ver/window.rb', line 443

def _subwin(layout)
  t = @layout[:top]
  l = @layout[:left]
  layout[:top] = layout[:top] + t
  layout[:left] = layout[:left] + l
  $log.debug " #{self} EXP: returning a subwin in derwin. Adding #{t} and #{l} "
  v = VER::SubWindow.new(self, layout)
  $log.debug " #{self} EXP: returning a subwin in derwin: #{v} "
  return v
end

#clearObject



237
238
239
240
241
# File 'lib/ver/window.rb', line 237

def clear
  # return unless visible?
  move 0, 0
  puts *Array.new(height){ ' ' * (width - 1) }
end

#clear_error(r = $error_message_row, color = $datacolor) ⇒ Object

Clear error message printed I am not only clearing if something was printed. This is since certain small forms like TabbedForm top form throw an error on printstring.



373
374
375
376
377
378
379
# File 'lib/ver/window.rb', line 373

def clear_error r = $error_message_row, color = $datacolor
  return unless $error_message_clear_pending
  c = $error_message_col || (Ncurses.COLS-text.length)/2 
  sz = $error_message_size || Ncurses.COLS
  printstring(r, c, "%-*s" % [sz, " "], color)
  $error_message_clear_pending = false
end

#color=(color) ⇒ Object



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

def color=(color)
  @color = color
  @window.color_set(color, nil)
end

#default_for(name) ⇒ Object



261
262
263
264
265
266
267
268
269
270
# File 'lib/ver/window.rb', line 261

def default_for(name)
  case name
  when :height, :top
    Ncurses.stdscr.getmaxy
  when :width, :left
    Ncurses.stdscr.getmaxx
  else
    0
  end
end

#derwin(layout) ⇒ Object

Acco to most sources, derwin and subwin are not thoroughly tested, avoid usage subwin moving and resizing not functioning.



437
438
439
440
441
442
# File 'lib/ver/window.rb', line 437

def derwin(layout)
  $log.debug " #{self} EXP: returning a subwin in derwin"
  v = VER::SubWindow.new(self, layout)
  $log.debug " #{self} EXP: returning a subwin in derwin: #{v} "
  return v
end

#destroyObject

added by rk 2008-11-29 18:48 to see if we can clean up from within



297
298
299
300
301
302
303
304
305
306
307
# File 'lib/ver/window.rb', line 297

def destroy
  # typically the ensure block should have this
  # @panel = @window.panel if @window
  #Ncurses::Panel.del_panel(@panel) if [email protected]?   
  #@window.delwin if [email protected]?
  $log.debug "win destroy"

  #@panel = @window.panel if @window
  Ncurses::Panel.del_panel(@panel) if !@panel.nil?   
  @window.delwin if !@window.nil?
end

#get_windowObject



453
# File 'lib/ver/window.rb', line 453

def get_window; @window; end

#getchObject



156
157
158
159
160
# File 'lib/ver/window.rb', line 156

def getch
  @window.getch
rescue Interrupt => ex
  3 # is C-c
end

#getcharObject

returns control, alt, alt+ctrl, alt+control+shift, F1 .. etc ALT combinations also send a 27 before the actual key Please test with above combinations before using on your terminal added by rkumar 2008-12-12 23:07



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/ver/window.rb', line 166

def getchar 
  while 1 
    ch = getch
    #$log.debug "window getchar() GOT: #{ch}" if ch != -1
    if ch == -1
      # the returns escape 27 if no key followed it, so its SLOW if you want only esc
      if @stack.first == 27
        #$log.debug " -1 stack sizze #{@stack.size}: #{@stack.inspect}, ch #{ch}"
        case @stack.size
        when 1
          @stack.clear
          return 27
        when 2 # basically a ALT-O, this will be really slow since it waits for -1
          ch = 128 + @stack.last
          @stack.clear
          return ch
        when 3
          $log.debug " SHOULD NOT COME HERE getchar()"
        end
      end
      @stack.clear
      next
    end
    # this is the ALT combination
    if @stack.first == 27
      # experimental. 2 escapes in quick succession to make exit faster
      if ch == 27
        @stack.clear
        return ch
      end
      # possible F1..F3 on xterm-color
      if ch == 79 or ch == 91
        #$log.debug " got 27, #{ch}, waiting for one more"
        @stack << ch
        next
      end
      #$log.debug "stack SIZE  #{@stack.size}, #{@stack.inspect}, ch: #{ch}"
      if @stack == [27,79]
        # xterm-color
        case ch
        when 80
          ch = KEY_F1
        when 81
          ch = KEY_F2
        when 82
          ch = KEY_F3
        when 83
          ch = KEY_F4
        end
        @stack.clear
        return ch
      elsif @stack == [27, 91]
        if ch == 90
          @stack.clear
          return 353 # backtab
        end
      end
      # the usual Meta combos. (alt)
      ch = 128 + ch
      @stack.clear
      return ch
    end
    # append a 27 to stack, actually one can use a flag too
    if ch == 27
      @stack << 27
      next
    end
    return ch
  end
end

#hideObject

Ncurses panel



274
275
276
277
278
# File 'lib/ver/window.rb', line 274

def hide
  Ncurses::Panel.hide_panel @panel
  Ncurses.refresh # wnoutrefresh
  @visible = false
end

#highlight_line(color, y, x, max) ⇒ Object



148
149
150
# File 'lib/ver/window.rb', line 148

def highlight_line(color, y, x, max)
  @window.mvchgat(y, x, max, Ncurses::A_NORMAL, color, nil)
end

#init_varsObject



25
26
27
28
29
30
31
32
# File 'lib/ver/window.rb', line 25

def init_vars
  @window_type = :WINDOW
  Ncurses::keypad(@window, true)
  @stack = []
  @name ||="#{self}"
  @modified = true
  $catch_alt_digits ||= false # is this where is should put globals ? 2010-03-14 14:00 XXX
end

#layout_value(name) ⇒ Object



253
254
255
256
257
258
259
# File 'lib/ver/window.rb', line 253

def layout_value(name)
  value = @layout[name]
  default = default_for(name)

  value = value.call(default) if value.respond_to?(:call)
  return (value || default).to_i
end

#move(y, x) ⇒ Object



93
94
95
96
97
# File 'lib/ver/window.rb', line 93

def move(y, x)
  return unless @visible
#       Log.debug([y, x] => caller[0,4])
  @window.move(y, x)
end

#on_topObject



286
287
288
289
# File 'lib/ver/window.rb', line 286

def on_top
  Ncurses::Panel.top_panel @panel
  wnoutrefresh
end

#posObject

Ncurses



78
79
80
# File 'lib/ver/window.rb', line 78

def pos
  return y, x
end


103
104
105
106
# File 'lib/ver/window.rb', line 103

def print(string, width = width)
  return unless visible?
  @window.waddnstr(string.to_s, width)
end

prints a border around a widget, CLEARING the area.

If calling with a pad, you would typically use 0,0, h-1, w-1.


396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/ver/window.rb', line 396

def print_border row, col, height, width, color, att=Ncurses::A_NORMAL
  att ||= Ncurses::A_NORMAL

  $log.debug " inside window print_border r #{row} c #{col} h #{height} w #{width} "

  # 2009-11-02 00:45 made att nil for blanking out
  # FIXME - in tabbedpanes this clears one previous line ??? XXX when using a textarea/view
  # when using a pad this calls pads printstring which again reduces top and left !!! 2010-01-26 23:53 
  (row+1).upto(row+height-1) do |r|
    #printstring( r, col+1," "*(width-2) , $datacolor, nil)
    prv_printstring( r, col+1," "*(width-2) , $datacolor, nil)
  end
  prv_print_border_only row, col, height, width, color, att
end

CAUTION : FOR MESSAGEBOXES ONLY !!!! XXX



382
383
384
385
386
387
388
389
390
391
392
# File 'lib/ver/window.rb', line 382

def print_border_mb row, col, height, width, color, attr
  mvwaddch row, col, ACS_ULCORNER
  mvwhline( row, col+1, ACS_HLINE, width-6)
  mvwaddch row, col+width-5, Ncurses::ACS_URCORNER
  mvwvline( row+1, col, ACS_VLINE, height-4)

  mvwaddch row+height-3, col, Ncurses::ACS_LLCORNER
  mvwhline(row+height-3, col+1, ACS_HLINE, width-6)
  mvwaddch row+height-3, col+width-5, Ncurses::ACS_LRCORNER
  mvwvline( row+1, col+width-5, ACS_VLINE, height-4)
end


410
411
412
# File 'lib/ver/window.rb', line 410

def print_border_only row, col, height, width, color, att=Ncurses::A_NORMAL
  prv_print_border_only row, col, height, width, color, att
end


112
113
114
115
# File 'lib/ver/window.rb', line 112

def print_empty_line
  return unless visible?
  @window.printw(' ' * width)
end

added by rk 2008-11-29 19:01 Since these methods write directly to window they are not advised since clearing previous message we don’t know how much to clear. Best to map error_message to a label.



353
354
355
356
357
358
359
360
361
# File 'lib/ver/window.rb', line 353

def print_error_message text=$error_message
  r = $error_message_row || Ncurses.LINES-1
  c = $error_message_col || (Ncurses.COLS-text.length)/2 

  $log.debug "got ERROR MESSAGE #{text} row #{r} "
  clear_error r, $datacolor
  printstring r, c, text, color = $promptcolor
  $error_message_clear_pending = true
end


117
118
119
# File 'lib/ver/window.rb', line 117

def print_line(string)
  print(string.ljust(width))
end

added by rk 2008-11-29 19:01



363
364
365
366
367
368
# File 'lib/ver/window.rb', line 363

def print_status_message text=$status_message
  r = $status_message_row || Ncurses.LINES-1
  clear_error r, $datacolor
  # print it in centre
  printstring r, (Ncurses.COLS-text.length)/2, text, color = $promptcolor
end


108
109
110
# File 'lib/ver/window.rb', line 108

def print_yx(string, y = 0, x = 0)
  @window.mvwaddnstr(y, x, string, width)
end

#printstring(r, c, string, color, att = Ncurses::A_NORMAL) ⇒ Object

added by rk 2008-11-29 19:01 I usually use this, not the others ones here @ param att - ncurses attribute: normal, bold, reverse, blink, underline

Parameters:

  • r
    • row

  • c
    • col

  • string
    • text to print

  • color
    • color pair



317
318
319
# File 'lib/ver/window.rb', line 317

def printstring(r,c,string, color, att = Ncurses::A_NORMAL)
    prv_printstring(r,c,string, color, att )
end

#prv_print_border_only(row, col, height, width, color, att = Ncurses::A_NORMAL) ⇒ Object

XXX this reduces 1 from width but not height !!! FIXME



419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/ver/window.rb', line 419

def prv_print_border_only row, col, height, width, color, att=Ncurses::A_NORMAL
  att ||= Ncurses::A_NORMAL
  attron(Ncurses.COLOR_PAIR(color) | att)
  mvwaddch row, col, ACS_ULCORNER
  mvwhline( row, col+1, ACS_HLINE, width-2)
  mvwaddch row, col+width-1, Ncurses::ACS_URCORNER
  mvwvline( row+1, col, ACS_VLINE, height-1)

  mvwaddch row+height-0, col, Ncurses::ACS_LLCORNER
  mvwhline(row+height-0, col+1, ACS_HLINE, width-2)
  mvwaddch row+height-0, col+width-1, Ncurses::ACS_LRCORNER
  mvwvline( row+1, col+width-1, ACS_VLINE, height-1)
  attroff(Ncurses.COLOR_PAIR(color) | att)
end

#prv_printstring(r, c, string, color, att = Ncurses::A_NORMAL) ⇒ Object

name changed from printstring to prv_prinstring



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/ver/window.rb', line 322

def prv_printstring(r,c,string, color, att = Ncurses::A_NORMAL)

  #$log.debug " #{@name} inside window printstring r #{r} c #{c} #{string} "
  att = Ncurses::A_NORMAL if att.nil? 
  case att.to_s.downcase
  when 'normal'
    att = Ncurses::A_NORMAL
  when 'underline'
    att = Ncurses::A_UNDERLINE
  when 'bold'
    att = Ncurses::A_BOLD
  when 'blink'
    att = Ncurses::A_BLINK    # unlikely to work
  when 'reverse'
    att = Ncurses::A_REVERSE    
  end

  attron(Ncurses.COLOR_PAIR(color) | att)
  # we should not print beyond window coordinates
  # trying out on 2009-01-03 19:29 
  width = Ncurses.COLS
  # the next line won't ensure we don't write outside some bounds like table
  #string = string[0..(width-c)] if c + string.length > width
  #$log.debug "PRINT len:#{string.length}, #{Ncurses.COLS}, #{r}, #{c} w: #{@window} "
  mvprintw(r, c, "%s", string);
  attroff(Ncurses.COLOR_PAIR(color) | att)
end

#puts(*strings) ⇒ Object



129
130
131
# File 'lib/ver/window.rb', line 129

def puts(*strings)
  print(strings.join("\n") << "\n")
end

#rb_mvaddch(row, col, char) ⇒ Object

use in place of mvaddch if your widget could be using a pad or window



464
465
466
# File 'lib/ver/window.rb', line 464

def rb_mvaddch row, col, char
  mvaddch row, col, char
end

#rb_mvwhline(row, col, char, width) ⇒ Object

use in place of mvwhline if your widget could be using a pad or window



456
457
458
# File 'lib/ver/window.rb', line 456

def rb_mvwhline row, col, char, width
  mvwhline row, col, char, width
end

#rb_mvwvline(row, col, char, width) ⇒ Object

use in place of mvwvline if your widget could be using a pad or window



460
461
462
# File 'lib/ver/window.rb', line 460

def rb_mvwvline row, col, char, width
  mvwvline row, col, char, width
end

#refreshObject



133
134
135
136
# File 'lib/ver/window.rb', line 133

def refresh
  return unless visible?
  @window.refresh
end

#reset_layout(layout) ⇒ Object

setup and reset



245
246
247
248
249
250
251
# File 'lib/ver/window.rb', line 245

def reset_layout(layout)
  @layout = layout

  [:height, :width, :top, :left].each do |name|
    instance_variable_set("@#{name}", layout_value(name))
  end
end

#resizeObject



72
73
74
# File 'lib/ver/window.rb', line 72

def resize
  resize_with(@layout)
end

#resize_with(layout) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/ver/window.rb', line 54

def resize_with(layout)
  $log.debug " DARN ! This awready duz a resize!! if h or w or even top or left changed!!! XXX"
  reset_layout(layout)
  @window.wresize(height, width)
  # this is dicey since we often change top and left in pads only for panning !! XXX
  @window.mvwin(top, left)
end

#showObject



280
281
282
283
284
# File 'lib/ver/window.rb', line 280

def show
  Ncurses::Panel.show_panel @panel
  Ncurses.refresh # wnoutrefresh
  @visible = true
end

#show_colored_chunks(chunks) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/ver/window.rb', line 121

def show_colored_chunks(chunks)
  return unless visible?
  chunks.each do |color, chunk|
    color_set(color)
    print_line(chunk)
  end
end

#to_sObject



454
# File 'lib/ver/window.rb', line 454

def to_s; @name || self; end

#ungetch(ch) ⇒ Object



152
153
154
# File 'lib/ver/window.rb', line 152

def ungetch(ch)
  Ncurses.ungetch(ch)
end

#visible?Boolean

Returns:

  • (Boolean)


291
292
293
# File 'lib/ver/window.rb', line 291

def visible?
  @visible
end

#wnoutrefreshObject



138
139
140
141
# File 'lib/ver/window.rb', line 138

def wnoutrefresh
  return unless visible?
  @window.wnoutrefresh
end

#xObject



86
87
88
# File 'lib/ver/window.rb', line 86

def x
  Ncurses.getcurx(@window)
end

#x=(n) ⇒ Object



90
# File 'lib/ver/window.rb', line 90

def x=(n) move(y, n) end

#yObject



82
83
84
# File 'lib/ver/window.rb', line 82

def y
  Ncurses.getcury(@window)
end

#y=(n) ⇒ Object



91
# File 'lib/ver/window.rb', line 91

def y=(n) move(n, x) end