Class: CDK::LABEL

Inherits:
CDKOBJS show all
Defined in:
lib/cdk/components/label.rb

Instance Attribute Summary collapse

Attributes included from HasTitle

#title_attrib

Attributes included from HasScreen

#is_visible, #screen, #screen_index

Attributes included from ExitConditions

#exit_type

Attributes included from Bindings

#binding_list

Attributes included from Focusable

#accepts_focus, #has_focus

Attributes included from Borders

#BXAttr, #HZChar, #LLChar, #LRChar, #ULChar, #URChar, #VTChar, #border_size, #box

Instance Method Summary collapse

Methods inherited from CDKOBJS

#setBackgroundColor, #timeout, #validCDKObject, #validObjType

Methods included from WindowHooks

#refreshData, #saveData

Methods included from WindowInput

#getc, #getch, #inject, #setPostProcess, #setPreProcess

Methods included from HasTitle

#cleanTitle, #drawTitle, #init_title, #setTitle

Methods included from HasScreen

#SCREEN_XPOS, #SCREEN_YPOS, #init_screen, #wrefresh

Methods included from ExitConditions

#init_exit_conditions, #resetExitType, #setExitType

Methods included from Bindings

#bind, #bindableObject, #checkBind, #cleanBindings, #init_bindings, #isBind, #unbind

Methods included from Focusable

#focus, #init_focus, #unfocus

Methods included from Borders

#getBox, #init_borders, #setBXattr, #setBox, #setHZchar, #setLLchar, #setLRchar, #setULchar, #setURchar, #setVTchar

Methods included from Movement

#move, #move_specific

Methods included from Converters

#char2Chtype, #charOf, #chtype2Char, #chtype2String, #decode_attribute, #encode_attribute

Methods included from Justifications

#justify_string

Methods included from Alignments

#alignxy

Constructor Details

#initialize(cdkscreen, xplace, yplace, mesg, rows, box, shadow) ⇒ LABEL

Returns a new instance of LABEL.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
54
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
80
81
82
83
84
85
86
# File 'lib/cdk/components/label.rb', line 7

def initialize(cdkscreen, xplace, yplace, mesg, rows, box, shadow)
  super()
  parent_width = cdkscreen::window.getmaxx
  parent_height = cdkscreen::window.getmaxy
  box_width = -2**30  # -INFINITY
  box_height = 0
  xpos = [xplace]
  ypos = [yplace]
  x = 0

  if rows <= 0
    return nil
  end

  self.setBox(box)
  box_height = rows + 2 * @border_size

  @info = []
  @info_len = []
  @info_pos = []

  # Determine the box width.
  (0...rows).each do |x|
    #Translate the string to a chtype array
    info_len = []
    info_pos = []
    @info << char2Chtype(mesg[x], info_len, info_pos)
    @info_len << info_len[0]
    @info_pos << info_pos[0]
    box_width = [box_width, @info_len[x]].max
  end
  box_width += 2 * @border_size

  # Create the string alignments.
  (0...rows).each do |x|
    @info_pos[x] = justify_string(box_width - 2 * @border_size,
        @info_len[x], @info_pos[x])
  end

  # Make sure we didn't extend beyond the dimensions of the window.
  box_width = if box_width > parent_width
              then parent_width
              else box_width
              end
  box_height = if box_height > parent_height
               then parent_height
               else box_height
               end

  # Rejustify the x and y positions if we need to
  alignxy(cdkscreen.window, xpos, ypos, box_width, box_height)
  @screen = cdkscreen
  @parent = cdkscreen.window
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos[0], xpos[0])
  @shadow_win = nil
  @xpos = xpos[0]
  @ypos = ypos[0]
  @rows = rows
  @box_width = box_width
  @box_height = box_height
  @input_window = @win
  @has_focus = false
  @shadow = shadow

  if @win.nil?
    self.destroy
    return nil
  end

  @win.keypad(true)

  # If a shadow was requested, then create the shadow window.
  if shadow
    @shadow_win = Ncurses::WINDOW.new(box_height, box_width,
        ypos[0] + 1, xpos[0] + 1)
  end

  # Register this
  cdkscreen.register(:LABEL, self)
end

Instance Attribute Details

#winObject

Returns the value of attribute win.



5
6
7
# File 'lib/cdk/components/label.rb', line 5

def win
  @win
end

Instance Method Details

#activate(actions) ⇒ Object

This was added for the builder.



89
90
91
# File 'lib/cdk/components/label.rb', line 89

def activate(actions)
  self.draw(@box)
end

#destroyObject

This destroys the label object pointer.



181
182
183
184
185
186
187
188
# File 'lib/cdk/components/label.rb', line 181

def destroy
  CDK.deleteCursesWindow(@shadow_win)
  CDK.deleteCursesWindow(@win)

  self.cleanBindings(:LABEL)

  CDK::SCREEN.unregister(:LABEL, self)
end

#draw(box) ⇒ Object

This draws the label widget.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/cdk/components/label.rb', line 147

def draw(box)
  # Is there a shadow?
  unless @shadow_win.nil?
    Draw.drawShadow(@shadow_win)
  end

  # Box the widget if asked.
  if @box
    Draw.drawObjBox(@win, self)
  end

  # Draw in the message.
  (0...@rows).each do |x|
    Draw.writeChtype(@win,
        @info_pos[x] + @border_size, x + @border_size,
        @info[x], CDK::HORIZONTAL, 0, @info_len[x])
  end

  # Refresh the window
  wrefresh
end

#eraseObject

This erases the label widget



170
171
172
173
# File 'lib/cdk/components/label.rb', line 170

def erase
  CDK.eraseCursesWindow(@win)
  CDK.eraseCursesWindow(@shadow_win)
end

#getMessage(size) ⇒ Object



128
129
130
131
# File 'lib/cdk/components/label.rb', line 128

def getMessage(size)
  size << @rows
  return @info
end

#object_typeObject



133
134
135
# File 'lib/cdk/components/label.rb', line 133

def object_type
  :LABEL
end

#positionObject



137
138
139
# File 'lib/cdk/components/label.rb', line 137

def position
  super(@win)
end

#set(mesg, lines, box) ⇒ Object

This sets multiple attributes of the widget



94
95
96
97
# File 'lib/cdk/components/label.rb', line 94

def set(mesg, lines, box)
  self.setMessage(mesg, lines)
  self.setBox(box)
end

#setBKattr(attrib) ⇒ Object

This sets the background attribute of the widget.



142
143
144
# File 'lib/cdk/components/label.rb', line 142

def setBKattr(attrib)
  @win.wbkgd(attrib)
end

#setMessage(info, info_size) ⇒ Object

This sets the information within the label.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cdk/components/label.rb', line 100

def setMessage(info, info_size)
  # Clean out the old message.`
  (0...@rows).each do |x|
    @info[x] = ''
    @info_pos[x] = 0
    @info_len[x] = 0
  end

  @rows = if info_size < @rows
          then info_size
          else @rows
          end

  # Copy in the new message.
  (0...@rows).each do |x|
    info_len = []
    info_pos = []
    @info[x] = char2Chtype(info[x], info_len, info_pos)
    @info_len[x] = info_len[0]
    @info_pos[x] = justify_string(@box_width - 2 * @border_size,
        @info_len[x], info_pos[0])
  end

  # Redraw the label widget.
  self.erase
  self.draw(@box)
end

#wait(key) ⇒ Object

This pauses until a user hits a key…



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/cdk/components/label.rb', line 191

def wait(key)
  function_key = []
  if key.ord == 0
    code = self.getch(function_key)
  else
    # Only exit when a specific key is hit
    while true
      code = self.getch(function_key)
      if code == key.ord
        break
      end
    end
  end
  return code
end