Class: CDK::BUTTON

Inherits:
CDKOBJS show all
Includes:
Formattable
Defined in:
lib/cdk/components/button.rb

Instance Attribute Summary

Attributes included from Formattable

#skip_formatting

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 included from Formattable

#skip_formatting?

Methods inherited from CDKOBJS

#setBackgroundColor, #timeout, #validCDKObject, #validObjType

Methods included from WindowHooks

#refreshData, #saveData

Methods included from WindowInput

#getc, #getch, #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

#init_focus

Methods included from Borders

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

Methods included from Movement

#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, text, callback, box, shadow) ⇒ BUTTON

Returns a new instance of BUTTON.



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
87
# File 'lib/cdk/components/button.rb', line 7

def initialize(cdkscreen, xplace, yplace, text, callback, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  box_width = 0
  xpos = xplace
  ypos = yplace

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

  # Translate the string to a chtype array.
  info_len = []
  info_pos = []

  if skip_formatting?
    @info = text
    @info_len = @info.size
    @info_pos = 0

  else
    @orig_text = text
    @info = char2Chtype(text, info_len, info_pos)
    @info_len = info_len[0]
    @info_pos = info_pos[0]
  end

  box_width = [box_width, @info_len].max + 2 * @border_size

  # Create the string alignments.
  @info_pos = justify_string(box_width - 2 * @border_size,
      @info_len, @info_pos)

  # 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.
  xtmp = [xpos]
  ytmp = [ypos]
  alignxy(cdkscreen.window, xtmp, ytmp, box_width, box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Create the button.
  @screen = cdkscreen
  # ObjOf (button)->fn = &my_funcs;
  @parent = cdkscreen.window
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)
  @shadow_win = nil
  @xpos = xpos
  @ypos = ypos
  @box_width = box_width
  @box_height = box_height
  @callback = callback
  @input_window = @win
  @accepts_focus = true
  @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 + 1, xpos + 1)
  end

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

Instance Method Details

#activate(actions) ⇒ Object

This was added for the builder.



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
# File 'lib/cdk/components/button.rb', line 100

def activate(actions)
  self.draw(@box)
  ret = -1

  if actions.nil? || actions.size == 0
    while true
      input = self.getch([])

      # Inject the character into the widget.
      ret = self.inject(input)
      if @exit_type != :EARLY_EXIT
        return ret
      end
    end
  else
    # Inject each character one at a time.
    actions.each do |x|
      ret = self.inject(x)
      if @exit_type == :EARLY_EXIT
        return ret
      end
    end
  end

  # Set the exit type and exit
  self.setExitType(0)
  return -1
end

#destroyObject

This destroys the button object pointer.



341
342
343
344
345
346
347
348
# File 'lib/cdk/components/button.rb', line 341

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

  self.cleanBindings(:BUTTON)

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

#draw(box) ⇒ Object

This draws the button widget



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

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
  self.drawText
  wrefresh
end

#drawTextObject



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
# File 'lib/cdk/components/button.rb', line 165

def drawText
  # XXX : so we can draw unicode
  if skip_formatting?
    @win.mvaddstr(@border_size, @border_size, @info)
    return
  end

  box_width = @box_width

  # Draw in the message.
  (0...(box_width - 2 * @border_size)).each do |i|
    pos = @info_pos
    len = @info_len
    if i >= pos && (i - pos) < len
      c = @info[i - pos]
    else
      c = ' '
    end

    if @has_focus
      c = Ncurses::A_REVERSE | c
    end

    @win.mvwaddch(@border_size, i + @border_size, c)
  end
end

#eraseObject

This erases the button widget.



208
209
210
211
212
213
# File 'lib/cdk/components/button.rb', line 208

def erase
  if self.validCDKObject
    CDK.eraseCursesWindow(@win)
    CDK.eraseCursesWindow(@shadow_win)
  end
end

#focusObject



391
392
393
394
# File 'lib/cdk/components/button.rb', line 391

def focus
  self.drawText
  wrefresh
end

#getMessageObject



156
157
158
# File 'lib/cdk/components/button.rb', line 156

def getMessage
  return @info
end

#inject(input) ⇒ Object

This injects a single character into the widget.



351
352
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
# File 'lib/cdk/components/button.rb', line 351

def inject(input)
  ret = -1
  @complete = false

  self.setExitType(0)

  # Check a predefined binding.
  if self.checkBind(:BUTTON, input)
    @complete = true
  else
    case input
    when CDK::KEY_ESC
      self.setExitType(input)
      @complete = true
    when Ncurses::ERR
      self.setExitType(input)
      @complete = true
    when ' '.ord, CDK::KEY_RETURN, Ncurses::KEY_ENTER
      unless @callback.nil?
        @callback.call(self)
      end
      self.setExitType(Ncurses::KEY_ENTER)
      ret = 0
      @complete = true
    when CDK::REFRESH
      @screen.erase
      @screen.refresh
    else
      CDK.Beep
    end
  end

  unless @complete
    self.setExitType(0)
  end

  @result_data = ret
  return ret
end

#move(xplace, yplace, relative, refresh_flag) ⇒ Object

This moves the button field to the given location.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/cdk/components/button.rb', line 216

def move(xplace, yplace, relative, refresh_flag)
  current_x = @win.getbegx
  current_y = @win.getbegy
  xpos = xplace
  ypos = yplace

  # If this is a relative move, then we will adjust where we want
  # to move to.
  if relative
    xpos = @win.getbegx + xplace
    ypos = @win.getbegy + yplace
  end

  # Adjust the window if we need to.
  xtmp = [xpos]
  ytmp = [ypos]
  alignxy(@screen.window, xtmp, ytmp, @box_width, @box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Get the difference
  xdiff = current_x - xpos
  ydiff = current_y - ypos

  # Move the window to the new location.
  CDK.moveCursesWindow(@win, -xdiff, -ydiff)
  CDK.moveCursesWindow(@shadow_win, -xdiff, -ydiff)

  # Thouch the windows so they 'move'.
  CDK::SCREEN.refreshCDKWindow(@screen.window)

  # Redraw the window, if they asked for it.
  if refresh_flag
    self.draw(@box)
  end
end

#object_typeObject



401
402
403
# File 'lib/cdk/components/button.rb', line 401

def object_type
  :BUTTON
end

#positionObject

This allows the user to use the cursor keys to adjust the position of the widget.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/cdk/components/button.rb', line 255

def position
  # Declare some variables
  orig_x = @win.getbegx
  orig_y = @win.getbegy
  key = 0

  # Let them move the widget around until they hit return
  while key != Ncurses::KEY_ENTER && key != CDK::KEY_RETURN
    key = self.getch([])
    if key == Ncurses::KEY_UP || key == '8'.ord
      if @win.getbegy > 0
        self.move(0, -1, true, true)
      else
        CDK.Beep
      end
    elsif key == Ncurses::KEY_DOWN || key == '2'.ord
      if @win.getbegy + @win.getmaxy < @screen.window.getmaxy - 1
        self.move(0, 1, true, true)
      else
        CDK.Beep
      end
    elsif key == Ncurses::KEY_LEFT || key == '4'.ord
      if @win.getbegx > 0
        self.move(-1, 0, true, true)
      else
        CDK.Beep
      end
    elsif key == Ncurses::KEY_RIGHT || key == '6'.ord
      if @win.getbegx + @win.getmaxx < @screen.window.getmaxx - 1
        self.move(1, 0, true, true)
      else
        CDK.Beep
      end
    elsif key == '7'.ord
      if @win.getbegy > 0 && @win.getbegx > 0
        self.move(-1, -1, true, true)
      else
        CDK.Beep
      end
    elsif key == '9'.ord
      if @win.getbegx + @win.getmaxx < @screen.window.getmaxx - 1 &&
          @win.getbegy > 0
        self.move(1, -1, true, true)
      else
        CDK.Beep
      end
    elsif key == '1'.ord
      if @win.getbegx > 0 &&
          @win.getbegx + @win.getmaxx < @screen.window.getmaxx - 1
        self.move(-1, 1, true, true)
      else
        CDK.Beep
      end
    elsif key == '3'.ord
      if @win.getbegx + @win.getmaxx < @screen.window.getmaxx - 1 &&
          @win.getbegy + @win.getmaxy < @screen.window.getmaxy - 1
        self.move(1, 1, true, true)
      else
        CDK.Beep
      end
    elsif key == '5'.ord
      self.move(CDK::CENTER, CDK::CENTER, false, true)
    elsif key == 't'.ord
      self.move(@win.getbegx, CDK::TOP, false, true)
    elsif key == 'b'.ord
      self.move(@win.getbegx, CDK::BOTTOM, false, true)
    elsif key == 'l'.ord
      self.move(CDK::LEFT, @win.getbegy, false, true)
    elsif key == 'r'
      self.move(CDK::RIGHT, @win.getbegy, false, true)
    elsif key == 'c'
      self.move(CDK::CENTER, @win.getbegy, false, true)
    elsif key == 'C'
      self.move(@win.getbegx, CDK::CENTER, false, true)
    elsif key == CDK::REFRESH
      @screen.erase
      @screen.refresh
    elsif key == CDK::KEY_ESC
      self.move(orig_x, orig_y, false, true)
    elsif key != CDK::KEY_RETURN && key != Ncurses::KEY_ENTER
      CDK.Beep
    end
  end
end

#set(mesg, box) ⇒ Object

This sets multiple attributes of the widget.



130
131
132
133
# File 'lib/cdk/components/button.rb', line 130

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

#setBKattr(attrib) ⇒ Object

This sets the background attribute of the widget.



161
162
163
# File 'lib/cdk/components/button.rb', line 161

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

#setMessage(info) ⇒ Object

This sets the information within the button.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cdk/components/button.rb', line 136

def setMessage(info)
  info_len = []
  info_pos = []
  if skip_formatting?
    @info = info
    @info_len = info.size
    @info_pos = 0
  else
    @orig_text = info
    @info = char2Chtype(info, info_len, info_pos)
    @info_len = info_len[0]
    @info_pos = justify_string(@box_width - 2 * @border_size,
      @info_len, @info_pos)
  end

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

#skip_formatting=(b) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/cdk/components/button.rb', line 89

def skip_formatting=(b)
  super

  if skip_formatting?
    @info = @orig_text
    @info_len = @info.size
    @info_pos = 0
  end
end

#unfocusObject



396
397
398
399
# File 'lib/cdk/components/button.rb', line 396

def unfocus
  self.drawText
  wrefresh
end