Class: RNDK::Label

Inherits:
Widget show all
Defined in:
lib/rndk/label.rb

Overview

Customizable text on the screen.

Instance Attribute Summary collapse

Attributes inherited from Widget

#BXAttr, #HZChar, #LLChar, #LRChar, #ULChar, #URChar, #VTChar, #accepts_focus, #binding_list, #border_size, #box, #exit_type, #has_focus, #is_visible, #screen, #screen_index, #supported_signals, #widget_type

Instance Method Summary collapse

Methods inherited from Widget

#Screen_XPOS, #Screen_YPOS, #after_processing, #before_processing, #bind_key, #bind_signal, #bindable_widget, #clean_bindings, #clean_title, #draw_title, #focus, #get_box, #getch, #inject, #is_bound?, #move, #refresh_data, #run_key_binding, #run_signal_binding, #save_data, #setBXattr, #setHZchar, #setLLchar, #setLRchar, #setULchar, #setURchar, #setVTchar, #set_box, #set_exit_type, #set_title, #unbind_key, #unfocus, #valid?, #valid_type?

Constructor Details

#initialize(screen, config = {}) ⇒ Label

Creates a Label Widget.

  • x is the x position - can be an integer or RNDK::LEFT, RNDK::RIGHT, RNDK::CENTER.
  • y is the y position - can be an integer or RNDK::TOP, RNDK::BOTTOM, RNDK::CENTER.
  • message is an Array of Strings with all the lines you'd want to show. RNDK markup applies (see RNDK#Markup).
  • box if the Widget is drawn with a box outside it.
  • shadow turns on/off the shadow around the Widget.

If the Widget cannot be created, returns nil.



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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rndk/label.rb', line 24

def initialize(screen, config={})
  super()
  @widget_type = :label
  @supported_signals += [:before_message_change]

  # This is UGLY AS HELL
  # But I don't have time to clean this up right now
  # (lots of widgets, you know)  :(
  x      = 0
  y      = 0
  text   = "label"
  box    = true
  shadow = false

  config.each do |key, val|
    x      = val if key == :x
    y      = val if key == :y
    text   = val if key == :text
    box    = val if key == :box
    shadow = val if key == :shadow
  end
  # End of darkness

  # Adjusting if the user sent us a String
  text = [text] if text.class == String
  return nil if text.class != Array or text.empty?
  rows = text.size

  parent_width  = Ncurses.getmaxx screen.window
  parent_height = Ncurses.getmaxy screen.window
  box_width  = -2**30  # -INFINITY
  box_height = 0
  x = [x]
  y = [y]

  self.set_box box
  box_height = rows + 2*@border_size

  @text = []
  @text_len = []
  @text_pos = []

  # Determine the box width.
  (0...rows).each do |x|

    # Translate the string to a chtype array
    text_len = []
    text_pos = []
    @text << RNDK.char2Chtype(text[x], text_len, text_pos)
    @text_len << text_len[0]
    @text_pos << text_pos[0]

    box_width = [box_width, @text_len[x]].max
  end
  box_width += 2 * @border_size

  # Create the string alignments.
  (0...rows).each do |x|
    @text_pos[x] = RNDK.justifyString(box_width - 2*@border_size,
                                      @text_len[x],
                                      @text_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
  RNDK.alignxy(screen.window, x, y, box_width, box_height)

  @screen = screen
  @parent = screen.window
  @win    = Ncurses.newwin(box_height,
                           box_width,
                           y[0],
                           x[0])
  @shadow_win   = nil
  @x            = x[0]
  @y            = y[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

  Ncurses.keypad(@win, true)

  # If a shadow was requested, then create the shadow window.
  if shadow
    @shadow_win = Ncurses.newwin(box_height,
                                 box_width,
                                 y[0] + 1,
                                 x[0] + 1)
  end

  # Register this
  screen.register(@widget_type, self)
end

Instance Attribute Details

#winObject

Raw Ncurses window.



10
11
12
# File 'lib/rndk/label.rb', line 10

def win
  @win
end

Instance Method Details

#activate(actions = []) ⇒ Object

Obsolete entrypoint which calls Label#draw.



136
137
138
# File 'lib/rndk/label.rb', line 136

def activate(actions=[])
  self.draw
end

#destroyObject

Removes the Widget from the Screen, deleting it's internal windows.



240
241
242
243
244
245
246
247
# File 'lib/rndk/label.rb', line 240

def destroy
  RNDK.window_delete @shadow_win
  RNDK.window_delete @win

  self.clean_bindings

  @screen.unregister self
end

#draw(box = false) ⇒ Object

Draws the Label Widget on the Screen.

If box is true, the Widget is drawn with a box.



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rndk/label.rb', line 210

def draw(box=false)

  # Is there a shadow?
  Draw.drawShadow(@shadow_win) unless @shadow_win.nil?

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

  # Draw in the message.
  (0...@rows).each do |x|
    Draw.writeChtype(@win,
                     @text_pos[x] + @border_size,
                     x + @border_size,
                     @text[x],
                     RNDK::HORIZONTAL,
                     0,
                     @text_len[x])
  end

  Ncurses.wrefresh @win
end

#eraseObject

This erases the label widget



233
234
235
236
# File 'lib/rndk/label.rb', line 233

def erase
  RNDK.window_erase @win
  RNDK.window_erase @shadow_win
end

#get_messageObject

Returns current contents of the Widget.



198
199
200
# File 'lib/rndk/label.rb', line 198

def get_message
  @text
end

#positionObject



268
269
270
# File 'lib/rndk/label.rb', line 268

def position
  super(@win)
end

#set(config) ⇒ Object

Sets multiple attributes of the Widget.

See Label#initialize.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rndk/label.rb', line 143

def set(config)
  # This is UGLY ATTRIBUTESS HELL
  # But I don't have time to clean this up right now
  # (lots of widgets, you know)  :(
  text   = @text
  box    = @box
  shadow = @shadow

  config.each do |key, val|
    text   = val if key == :text
    box    = val if key == :box
    shadow = val if key == :shadow
  end

  self.set_message text if text != @text
  self.set_box box      if box  != @box
end

#set_bg_color(attrib) ⇒ Object

Sets the background attribute/color of the widget.



203
204
205
# File 'lib/rndk/label.rb', line 203

def set_bg_color attrib
  Ncurses.wbkgd(@win, attrib)
end

#set_message(text) ⇒ Object

Note:

text is an Array of Strings.

Sets the contents of the Label Widget.



163
164
165
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
# File 'lib/rndk/label.rb', line 163

def set_message text
  return if text.class != Array or text.empty?

  keep_going = self.run_signal_binding(:before_message_change)
  return if not keep_going

  # Clean out the old message.
  (0...@rows).each do |x|
    @text[x]     = ''
    @text_pos[x] = 0
    @text_len[x] = 0
  end

  @rows = if text.size < @rows
          then text.size
          else @rows
          end

  # Copy in the new message.
  (0...@rows).each do |x|
    text_len = []
    text_pos = []
    @text[x] = RNDK.char2Chtype(text[x], text_len, text_pos)
    @text_len[x] = text_len[0]
    @text_pos[x] = RNDK.justifyString(@box_width - 2 * @border_size,
                                      @text_len[x],
                                      text_pos[0])
  end

  # Redraw the label widget.
  self.erase
  self.draw
end

#wait(key = 0) ⇒ Object

Waits for the user to press a key.

If no key is provided, waits for a single keypress of any key.



253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rndk/label.rb', line 253

def wait(key=0)

  if key.ord == 0
    code = self.getch
    return code
  end

  # Only exit when a specific key is hit
  loop do
    code = self.getch
    break if code == key.ord
  end
  code
end