Class: RNDK::Widget

Inherits:
Object
  • Object
show all
Defined in:
lib/rndk/core/widget.rb,
lib/rndk/core/widget_bind.rb

Overview

Wrapper on common functionality between all RNDK Widgets.

Constant Summary collapse

@@g_paste_buffer =
''

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWidget

Returns a new instance of Widget.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rndk/core/widget.rb', line 24

def initialize
  @has_focus  = true
  @is_visible = true

  RNDK::ALL_WIDGETS << self

  # set default line-drawing characters
  @ULChar = Ncurses::ACS_ULCORNER
  @URChar = Ncurses::ACS_URCORNER
  @LLChar = Ncurses::ACS_LLCORNER
  @LRChar = Ncurses::ACS_LRCORNER
  @HZChar = Ncurses::ACS_HLINE
  @VTChar = Ncurses::ACS_VLINE
  @BXAttr = RNDK::Color[:normal]

  # set default exit-types
  @exit_type  = :NEVER_ACTIVATED
  @early_exit = :NEVER_ACTIVATED

  @accepts_focus = false

  # Bound functions
  @binding_list = {}

  # Actions to be executed at certain signals
  @actions = {}

  @supported_signals = []
  @supported_signals << :destroy
end

Instance Attribute Details

#accepts_focusObject (readonly)

Returns the value of attribute accepts_focus.



16
17
18
# File 'lib/rndk/core/widget.rb', line 16

def accepts_focus
  @accepts_focus
end

#binding_listObject (readonly)

Returns the value of attribute binding_list.



16
17
18
# File 'lib/rndk/core/widget.rb', line 16

def binding_list
  @binding_list
end

#border_sizeObject (readonly)

Returns the value of attribute border_size.



16
17
18
# File 'lib/rndk/core/widget.rb', line 16

def border_size
  @border_size
end

#boxObject

Returns the value of attribute box.



14
15
16
# File 'lib/rndk/core/widget.rb', line 14

def box
  @box
end

#BXAttrObject

Returns the value of attribute BXAttr.



15
16
17
# File 'lib/rndk/core/widget.rb', line 15

def BXAttr
  @BXAttr
end

#exit_typeObject (readonly)

Returns the value of attribute exit_type.



16
17
18
# File 'lib/rndk/core/widget.rb', line 16

def exit_type
  @exit_type
end

#has_focusObject

Returns the value of attribute has_focus.



14
15
16
# File 'lib/rndk/core/widget.rb', line 14

def has_focus
  @has_focus
end

#HZCharObject

Returns the value of attribute HZChar.



15
16
17
# File 'lib/rndk/core/widget.rb', line 15

def HZChar
  @HZChar
end

#is_visibleObject

Returns the value of attribute is_visible.



14
15
16
# File 'lib/rndk/core/widget.rb', line 14

def is_visible
  @is_visible
end

#LLCharObject

Returns the value of attribute LLChar.



15
16
17
# File 'lib/rndk/core/widget.rb', line 15

def LLChar
  @LLChar
end

#LRCharObject

Returns the value of attribute LRChar.



15
16
17
# File 'lib/rndk/core/widget.rb', line 15

def LRChar
  @LRChar
end

#screenObject

Returns the value of attribute screen.



14
15
16
# File 'lib/rndk/core/widget.rb', line 14

def screen
  @screen
end

#screen_indexObject

Returns the value of attribute screen_index.



14
15
16
# File 'lib/rndk/core/widget.rb', line 14

def screen_index
  @screen_index
end

#supported_signalsObject (readonly)

All the signals the current widget supports. Use them on Widget#bind_signal



20
21
22
# File 'lib/rndk/core/widget.rb', line 20

def supported_signals
  @supported_signals
end

#ULCharObject

Returns the value of attribute ULChar.



15
16
17
# File 'lib/rndk/core/widget.rb', line 15

def ULChar
  @ULChar
end

#URCharObject

Returns the value of attribute URChar.



15
16
17
# File 'lib/rndk/core/widget.rb', line 15

def URChar
  @URChar
end

#VTCharObject

Returns the value of attribute VTChar.



15
16
17
# File 'lib/rndk/core/widget.rb', line 15

def VTChar
  @VTChar
end

#widget_typeObject (readonly)

Which widget this is. It's the name of the widget lowercased. Example: :label, :calendar, :alphalist



12
13
14
# File 'lib/rndk/core/widget.rb', line 12

def widget_type
  @widget_type
end

Instance Method Details

#after_processing(data = nil, &block) ⇒ Object

Makes block execute right after processing input on the Widget.

block is called with the following arguments:

  • The Widget type (:scroll, :calendar, etc)
  • The Widget itself (self)
  • That data you send as an argument to after_processing.
  • The input character the Widget just received.

Make good use of them when making your callback.



82
83
84
85
# File 'lib/rndk/core/widget.rb', line 82

def after_processing(data=nil, &block)
  @post_process_data = data
  @post_process_func = block
end

#before_processing(data = nil, &block) ⇒ Object

Makes block execute right before processing input on the Widget.

block is called with the following arguments:

  • The Widget type (:scroll, :calendar, etc)
  • The Widget itself (self)
  • That data you send as an argument to before_processing.
  • The input character the Widget just received.

Make good use of them when making your callback.



66
67
68
69
# File 'lib/rndk/core/widget.rb', line 66

def before_processing(data=nil, &block)
  @pre_process_data = data
  @pre_process_func = block
end

#bind_key(key, data = nil, &action) ⇒ Object

Binds a function to a key.



28
29
30
31
32
33
34
35
36
# File 'lib/rndk/core/widget_bind.rb', line 28

def bind_key(key, data=nil, &action)
  obj = self.bindable_widget @widget_type

  if (key.ord >= Ncurses::KEY_MAX) or (key.ord.zero?) or (obj.nil?)
    return
  end

  obj.binding_list[key.ord] = [action, data]
end

#bind_signal(signal, &action) ⇒ Object

Binds an action to be executed when signal is activated.



81
82
83
84
85
86
87
# File 'lib/rndk/core/widget_bind.rb', line 81

def bind_signal(signal, &action)
  return unless @supported_signals.include? signal

  @actions[signal] = [] if @actions[signal].nil?

  @actions[signal] << action
end

#bindable_widget(rndktype) ⇒ Object

Returns the internal Widget that accepts key bindings.

Some widgets are made of others. So when we bind keys to those complex widgets, we can specify which internal Widget accepts keybindings.

For example, on an Alphalist (which is composed of Scroll and Entry), we bind keys to the Entry, not Scroll.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rndk/core/widget_bind.rb', line 14

def bindable_widget rndktype
  return nil if rndktype != @widget_type


  if [:Fselect, :alphalist].include? @widget_type
    return @entry_field

  else
    return self
  end
end

#clean_bindingsObject



44
45
46
47
48
49
50
# File 'lib/rndk/core/widget_bind.rb', line 44

def clean_bindings
  obj = self.bindable_widget @widget_type

  if (not obj.nil?) and (not obj.binding_list.nil?)
    obj.binding_list.clear
  end
end

#clean_titleObject

Remove storage for the widget's title.



291
292
293
# File 'lib/rndk/core/widget.rb', line 291

def clean_title
  @title_lines = ''
end

#destroyObject

Destroys all windows inside the Widget and removes it from the Screen.



232
233
# File 'lib/rndk/core/widget.rb', line 232

def destroy
end

#draw(a) ⇒ Object



100
101
# File 'lib/rndk/core/widget.rb', line 100

def draw(a)
end

#draw_title(win) ⇒ Object

Draw the widget's title



174
175
176
177
178
179
180
# File 'lib/rndk/core/widget.rb', line 174

def draw_title(win)
  (0...@title_lines).each do |x|
    Draw.writeChtype(@win, @title_pos[x] + @border_size,
        x + @border_size, @title[x], RNDK::HORIZONTAL, 0,
        @title_len[x])
  end
end

#eraseObject

Note:

It does not destroy the Widget.

Erases the Widget from the Screen.



105
106
# File 'lib/rndk/core/widget.rb', line 105

def erase
end

#focusObject



205
206
# File 'lib/rndk/core/widget.rb', line 205

def focus
end

#get_boxObject

Tells if the widget has borders.



201
202
203
# File 'lib/rndk/core/widget.rb', line 201

def get_box
  return @box
end

#getch(function_key = []) ⇒ Object

FIXME TODO What does function_key does?



315
316
317
318
319
320
# File 'lib/rndk/core/widget.rb', line 315

def getch(function_key=[])
  key = self.getc
  function_key << (key >= Ncurses::KEY_MIN && key <= Ncurses::KEY_MAX)

  key
end

#inject(input) ⇒ Object

Makes the Widget react to char just as if the user had pressed it.

Nice to simulate batch actions on a Widget.

Besides normal keybindings (arrow keys and such), see Widget#set_exit_type to see how the Widget exits.



190
191
# File 'lib/rndk/core/widget.rb', line 190

def inject input
end

#is_bound?(key) ⇒ Boolean

Tells if the key binding for the key exists.

Returns:

  • (Boolean)


72
73
74
75
76
77
# File 'lib/rndk/core/widget_bind.rb', line 72

def is_bound? key
  obj = self.bindable_widget @widget_type
  return false if obj.nil?

  obj.binding_list.include? key
end

#move(x, y, relative, refresh_flag) ⇒ Object

Moves the Widget to the given position.

  • x and y are the new position of the Widget.

  • x may be an integer or one of the pre-defined values RNDK::TOP, RNDK::BOTTOM, and RNDK::CENTER.

  • y may be an integer or one of the pre-defined values RNDK::LEFT, RNDK::RIGHT, and RNDK::CENTER.

  • relative states whether the x/y pair is a relative move over it's current position or an absolute move over the Screen's top.

For example, if x = 1 and y = 2 and relative = true, the Widget would move one row down and two columns right.

If the value of relative was false then the widget would move to the position (1,2).

Do not use the values TOP, BOTTOM, LEFT, RIGHT, or CENTER when relative = true - weird things may happen.

  • refresh_flag is a boolean value which states whether the Widget will get refreshed after the move.


134
135
136
# File 'lib/rndk/core/widget.rb', line 134

def move(x, y, relative, refresh_flag)
  self.move_specific(x, y, relative, refresh_flag, [@win, @shadow_win], [])
end

#position(win) ⇒ Object

Allows the user to move the Widget around the screen via the cursor/keypad keys.

win is the main window of the Widget - from which subwins derive.

The following key bindings can be used to move the Widget around the screen:

Up Arrow:: Moves the widget up one row. Down Arrow:: Moves the widget down one row. Left Arrow:: Moves the widget left one column Right Arrow:: Moves the widget right one column 1:: Moves the widget down one row and left one column. 2:: Moves the widget down one row. 3:: Moves the widget down one row and right one column. 4:: Moves the widget left one column. 5:: Centers the widget both vertically and horizontally. 6:: Moves the widget right one column 7:: Moves the widget up one row and left one column. 8:: Moves the widget up one row. 9:: Moves the widget up one row and right one column. t:: Moves the widget to the top of the screen. b:: Moves the widget to the bottom of the screen. l:: Moves the widget to the left of the screen. r:: Moves the widget to the right of the screen. c:: Centers the widget between the left and right of the window. C:: Centers the widget between the top and bottom of the window. Escape:: Returns the widget to its original position. Return:: Exits the function and leaves the Widget where it was.



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/rndk/core/widget.rb', line 353

def position win
  parent = @screen.window
  orig_x = Ncurses.getbegx win
  orig_y = Ncurses.getbegy win
  beg_x = Ncurses.getbegx parent
  beg_y = Ncurses.getbegy parent
  end_x = beg_x + Ncurses.getmaxx(@screen.window)
  end_y = beg_y + Ncurses.getmaxy(@screen.window)

  loop do
    key = self.getch

    # Let them move the widget around until they hit return.
    break if [RNDK::KEY_RETURN, Ncurses::KEY_ENTER].include? key

    case key
    when Ncurses::KEY_UP, '8'.ord
      if Ncurses.getbegy(win) > beg_y
        self.move(0, -1, true, true)
      else
        RNDK.beep
      end
    when Ncurses::KEY_DOWN, '2'.ord
      if (Ncurses.getbegy(win) + Ncurses.getmaxy(win)) < end_y
        self.move(0, 1, true, true)
      else
        RNDK.beep
      end
    when Ncurses::KEY_LEFT, '4'.ord
      if Ncurses.getbegx(win) > beg_x
        self.move(-1, 0, true, true)
      else
        RNDK.beep
      end
    when Ncurses::KEY_RIGHT, '6'.ord
      if (Ncurses.getbegx(win) + Ncurses.getmaxx(win)) < end_x
        self.move(1, 0, true, true)
      else
        RNDK.beep
      end
    when '7'.ord
      if Ncurses.getbegy(win) > beg_y && Ncurses.getbegx(win) > beg_x
        self.move(-1, -1, true, true)
      else
        RNDK.beep
      end
    when '9'.ord
      if (Ncurses.getbegx(win) + Ncurses.getmaxx(win)) < end_x && Ncurses.getbegy(win) > beg_y
        self.move(1, -1, true, true)
      else
        RNDK.beep
      end
    when '1'.ord
      if Ncurses.getbegx(win) > beg_x && (Ncurses.getbegy(win) + Ncurses.getmaxy(win)) < end_y
        self.move(-1, 1, true, true)
      else
        RNDK.beep
      end
    when '3'.ord
      if (Ncurses.getbegx(win) + Ncurses.getmaxx(win)) < end_x &&
          (Ncurses.getbegy(win) + Ncurses.getmaxy(win)) < end_y
        self.move(1, 1, true, true)
      else
        RNDK.beep
      end

    when '5'.ord
      self.move(RNDK::CENTER, RNDK::CENTER, false, true)

    when 't'.ord
      self.move(Ncurses.getbegx(win), RNDK::TOP, false, true)

    when 'b'.ord
      self.move(Ncurses.getbegx(win), RNDK::BOTTOM, false, true)

    when 'l'.ord
      self.move(RNDK::LEFT, Ncurses.getbegy(win), false, true)

    when 'r'.ord
      self.move(RNDK::RIGHT, Ncurses.getbegy(win), false, true)

    when 'c'.ord
      self.move(RNDK::CENTER, Ncurses.getbegy(win), false, true)

    when 'C'.ord
      self.move(Ncurses.getbegx(win), RNDK::CENTER, false, true)

    when RNDK::REFRESH
      @screen.erase
      @screen.refresh

    when RNDK::KEY_ESC
      self.move(orig_x, orig_y, false, true)
    else
      RNDK.beep
    end
  end
end

#refresh_dataObject

Note:

This method isn't called whatsoever! It only exists at Traverse module.

Somehow refreshes all data within this Widget.

TODO Find out how can I insert this on Widgets.



227
228
# File 'lib/rndk/core/widget.rb', line 227

def refresh_data
end

#run_key_binding(key) ⇒ Object

Runs any binding that's set for key.

Returns:

  • The binding's return value or false, if no keybinding for key exists.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rndk/core/widget_bind.rb', line 57

def run_key_binding key
  obj = self.bindable_widget @widget_type

  return false if obj.nil? or (not obj.binding_list.include? key)

  function = obj.binding_list[key][0]
  data     = obj.binding_list[key][1]

  # What the heck is this?
  return data if function == :getc

  function.call data
end

#run_signal_binding(signal, data = nil) ⇒ Object

Note:

It interrupts executing when any action returns false.

Runs all actions based on signal, passing data as an argument.

Returns:

  • true if all actions were executed or no actions exist, false if they were interrupted.



96
97
98
99
100
101
102
103
104
# File 'lib/rndk/core/widget_bind.rb', line 96

def run_signal_binding(signal, data=nil)
  return true if @actions[signal].nil?

  @actions[signal].each do |action|
    return false if action.call(data) == false
  end

  true
end

#save_dataObject

Note:

This method isn't called whatsoever! It only exists at Traverse module.

Somehow saves all data within this Widget.

TODO Find out how can I insert this on Widgets.



217
218
# File 'lib/rndk/core/widget.rb', line 217

def save_data
end

#Screen_XPOS(n) ⇒ Object



92
93
94
# File 'lib/rndk/core/widget.rb', line 92

def Screen_XPOS(n)
  n + @border_size
end

#Screen_YPOS(n) ⇒ Object



96
97
98
# File 'lib/rndk/core/widget.rb', line 96

def Screen_YPOS(n)
  n + @border_size + @title_lines
end

#set_bg_color(color) ⇒ Object

This sets the background color of the widget.

FIXME BUG



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/rndk/core/widget.rb', line 273

def set_bg_color color
  return if color.nil? || color == ''

  junk1 = []
  junk2 = []

  # Convert the value of the environment variable to a chtype
  holder = RNDK.char2Chtype(color, junk1, junk2)

  # Set the widget's background color

  ## FIXME BUG WTF
  ## What does this function do?
  ## Couldn't find anything on it
  self.SetBackAttrObj(holder[0])
end

#set_box(box) ⇒ Object

Makes the widget have a border if box is true, otherwise, cancel it.



195
196
197
198
# File 'lib/rndk/core/widget.rb', line 195

def set_box box
  @box = box
  @border_size = if @box then 1 else 0 end
end

#set_exit_type(char) ⇒ Object

Set the Widget#exit_type based on the input char.

According to default keybindings, if char is:

RETURN or TAB:: Sets :NORMAL. ESCAPE:: Sets :ESCAPE_HIT. Otherwise:: Unless treated specifically by the Widget, sets :EARLY_EXIT.



304
305
306
307
308
309
310
311
312
# File 'lib/rndk/core/widget.rb', line 304

def set_exit_type char
  case char
  when Ncurses::ERR  then @exit_type = :ERROR
  when RNDK::KEY_ESC then @exit_type = :ESCAPE_HIT
  when 0             then @exit_type = :EARLY_EXIT
  when RNDK::KEY_TAB, Ncurses::KEY_ENTER, RNDK::KEY_RETURN
    @exit_type = :NORMAL
  end
end

#set_title(title, box_width) ⇒ Object

Set the widget's title.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rndk/core/widget.rb', line 139

def set_title(title, box_width)
  return if title.nil?

  temp = title.split "\n"
  @title_lines = temp.size

  if box_width >= 0
    max_width = 0
    temp.each do |line|
      len = []
      align = []
      holder = RNDK.char2Chtype(line, len, align)
      max_width = [len[0], max_width].max
    end
    box_width = [box_width, max_width + 2 * @border_size].max
  else
    box_width = -(box_width - 1)
  end

  # For each line in the title convert from string to chtype array
  title_width = box_width - (2 * @border_size)
  @title = []
  @title_pos = []
  @title_len = []
  (0...@title_lines).each do |x|
    len_x = []
    pos_x = []
    @title << RNDK.char2Chtype(temp[x], len_x, pos_x)
    @title_len.concat(len_x)
    @title_pos << RNDK.justifyString(title_width, len_x[0], pos_x[0])
  end
  box_width
end

#setBXattr(ch) ⇒ Object

Set the widget's box-attributes.



266
267
268
# File 'lib/rndk/core/widget.rb', line 266

def setBXattr(ch)
  @BXAttr = ch
end

#setHZchar(ch) ⇒ Object

Set the widget's horizontal line-drawing character



256
257
258
# File 'lib/rndk/core/widget.rb', line 256

def setHZchar(ch)
  @HZChar = ch
end

#setLLchar(ch) ⇒ Object

Set the widget's lower-left-corner line-drawing character.



246
247
248
# File 'lib/rndk/core/widget.rb', line 246

def setLLchar(ch)
  @LLChar = ch
end

#setLRchar(ch) ⇒ Object

Set the widget's upper-right-corner line-drawing character.



251
252
253
# File 'lib/rndk/core/widget.rb', line 251

def setLRchar(ch)
  @LRChar = ch
end

#setULchar(ch) ⇒ Object

Set the widget's upper-left-corner line-drawing character.



236
237
238
# File 'lib/rndk/core/widget.rb', line 236

def setULchar(ch)
  @ULChar = ch
end

#setURchar(ch) ⇒ Object

Set the widget's upper-right-corner line-drawing character.



241
242
243
# File 'lib/rndk/core/widget.rb', line 241

def setURchar(ch)
  @URChar = ch
end

#setVTchar(ch) ⇒ Object

Set the widget's vertical line-drawing character



261
262
263
# File 'lib/rndk/core/widget.rb', line 261

def setVTchar(ch)
  @VTChar = ch
end

#unbind_key(key) ⇒ Object



38
39
40
41
42
# File 'lib/rndk/core/widget_bind.rb', line 38

def unbind_key key
  obj = self.bindable_widget @widget_type

  obj.binding_list.delete(key) unless obj.nil?
end

#unfocusObject



208
209
# File 'lib/rndk/core/widget.rb', line 208

def unfocus
end

#valid?Boolean

Tells if a widget is valid.

Returns:

  • (Boolean)


453
454
455
# File 'lib/rndk/core/widget.rb', line 453

def valid?
  RNDK::ALL_WIDGETS.include?(self) and self.valid_type?
end

#valid_type?Boolean

Tells if current widget's type is the type of an existing Widget.

Returns:

  • (Boolean)


459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/rndk/core/widget.rb', line 459

def valid_type?
  [:graph,
   :histogram,
   :label,
   :marquee,
   :viewer,
   :alphalist,
   :button,
   :buttonbox,
   :calendar,
   :dialog,
   :dscale,
   :entry,
   :fscale,
   :fselect,
   :fslider,
   :itemlist,
   :matrix,
   :mentry,
   :radio,
   :scale,
   :scroll,
   :selection,
   :slider,
   :swindow,
   :template,
   :uscale,
   :uslider].include? @widget_type
end