Class: CDK::DIALOG

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

Constant Summary collapse

MIN_DIALOG_WIDTH =
10

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, #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, #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, button_label, button_count, highlight, separator, box, shadow) ⇒ DIALOG

Returns a new instance of DIALOG.



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cdk/components/dialog.rb', line 8

def initialize(cdkscreen, xplace, yplace, mesg, rows, button_label,
    button_count, highlight, separator, box, shadow)
  super()
  box_width = DIALOG::MIN_DIALOG_WIDTH
  max_message_width = -1
  button_width = 0
  xpos = xplace
  ypos = yplace
  temp = 0
  buttonadj = 0
  @info = []
  @info_len = []
  @info_pos = []
  @button_label = []
  @button_len = []
  @button_pos = []

  if rows <= 0 || button_count <= 0
    self.destroy
    return nil
  end

  self.setBox(box)
  box_height = if separator then 1 else 0 end
  box_height += rows + 2 * @border_size + 1

  # Translate the string message to a chtype array
  (0...rows).each do |x|
    info_len = []
    info_pos = []
    @info << char2Chtype(mesg[x], info_len, info_pos)
    @info_len << info_len[0]
    @info_pos << info_pos[0]
    max_message_width = [max_message_width, info_len[0]].max
  end

  # Translate the button label string to a chtype array
  (0...button_count).each do |x|
    button_len = []
    @button_label << char2Chtype(button_label[x], button_len, [])
    @button_len << button_len[0]
    button_width += button_len[0] + 1
  end

  button_width -= 1

  # Determine the final dimensions of the box.
  box_width = [box_width, max_message_width, button_width].max
  box_width = box_width + 2 + 2 * @border_size

  # Now we have to readjust the x and y positions.
  xtmp = [xpos]
  ytmp = [ypos]
  alignxy(cdkscreen.window, xtmp, ytmp, box_width, box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Set up the dialog box attributes.
  @screen = cdkscreen
  @parent = cdkscreen.window
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)
  @shadow_win = nil
  @button_count = button_count
  @current_button = 0
  @message_rows = rows
  @box_height = box_height
  @box_width = box_width
  @highlight = highlight
  @separator = separator
  @accepts_focus = true
  @input_window = @win
  @shadow = shadow

  # If we couldn't create the window, we should return a nil value.
  if @win.nil?
    self.destroy
    return nil
  end
  @win.keypad(true)

  # Find the button positions.
  buttonadj = (box_width - button_width) / 2
  (0...button_count).each do |x|
    @button_pos[x] = buttonadj
    buttonadj = buttonadj + @button_len[x] + @border_size
  end

  # 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

  # Was there a shadow?
  if shadow
    @shadow_win = Ncurses::WINDOW.new(box_height, box_width,
        ypos + 1, xpos + 1)
  end

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

Instance Attribute Details

#current_buttonObject (readonly)

Returns the value of attribute current_button.



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

def current_button
  @current_button
end

Instance Method Details

#activate(actions) ⇒ Object

This lets the user select the button.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cdk/components/dialog.rb', line 112

def activate(actions)
  input = 0

  # Draw the dialog box.
  self.draw(@box)

  # Lets move to the first button.
  Draw.writeChtypeAttrib(@win, @button_pos[@current_button],
      @box_height - 1 - @border_size, @button_label[@current_button],
      @highlight, CDK::HORIZONTAL, 0, @button_len[@current_button])
  wrefresh(@win)

  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 |action|
      ret = self.inject(action)
      if @exit_type != :EARLY_EXIT
        return ret
      end
    end
  end

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

#destroyObject

This function destroys the dialog widget.



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/cdk/components/dialog.rb', line 250

def destroy
  # Clean up the windows.
  CDK.deleteCursesWindow(@win)
  CDK.deleteCursesWindow(@shadow_win)

  # Clean the key bindings
  self.cleanBindings(:DIALOG)

  # Unregister this object
  CDK::SCREEN.unregister(:DIALOG, self)
end

#draw(box) ⇒ Object

This function draws the dialog widget.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/cdk/components/dialog.rb', line 225

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

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

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

  # Draw in the buttons.
  self.drawButtons

  wrefresh(@win)
end

#drawButtonsObject

This draws the dialog buttons and the separation line.



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

def drawButtons
  (0...@button_count).each do |x|
    Draw.writeChtype(@win, @button_pos[x],
        @box_height -1 - @border_size,
        @button_label[x], CDK::HORIZONTAL, 0,
        @button_len[x])
  end

  # Draw the separation line.
  if @separator
    boxattr = @BXAttr

    (1...@box_width).each do |x|
      @win.mvwaddch(@box_height - 2 - @border_size, x,
          Ncurses::ACS_HLINE | boxattr)
    end
    @win.mvwaddch(@box_height - 2 - @border_size, 0,
        Ncurses::ACS_LTEE | boxattr)
    @win.mvwaddch(@box_height - 2 - @border_size, @win.getmaxx - 1,
        Ncurses::ACS_RTEE | boxattr)
  end
  Draw.writeChtypeAttrib(@win, @button_pos[@current_button],
      @box_height - 1 - @border_size, @button_label[@current_button],
      @highlight, CDK::HORIZONTAL, 0, @button_len[@current_button])
end

#eraseObject

This function erases the dialog widget from the screen.



263
264
265
266
267
268
# File 'lib/cdk/components/dialog.rb', line 263

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

#focusObject



327
328
329
# File 'lib/cdk/components/dialog.rb', line 327

def focus
  self.draw(@box)
end

#getHighlightObject



282
283
284
# File 'lib/cdk/components/dialog.rb', line 282

def getHighlight
  return @highlight
end

#getSeparatorObject



291
292
293
# File 'lib/cdk/components/dialog.rb', line 291

def getSeparator
  return @separator
end

#inject(input) ⇒ Object

This injects a single character into the dialog widget



150
151
152
153
154
155
156
157
158
159
160
161
162
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/cdk/components/dialog.rb', line 150

def inject(input)
  first_button = 0
  last_button = @button_count - 1
  pp_return = 1
  ret = -1
  @complete = false

  # Set the exit type.
  self.setExitType(0)

  # Check if there is a pre-process function to be called.
  unless @pre_process_func.nil?
    pp_return = @pre_process_func.call(:DIALOG, self,
        @pre_process_data, input)
  end

  # Should we continue?
  if pp_return != 0
    # Check for a key binding.
    if self.checkBind(:DIALOG, input)
      @complete = true
    else
      case input
      when Ncurses::KEY_LEFT, Ncurses::KEY_BTAB, Ncurses::KEY_BACKSPACE
        if @current_button == first_button
          @current_button = last_button
        else
          @current_button -= 1
        end
      when Ncurses::KEY_RIGHT, CDK::KEY_TAB, ' '.ord
        if @current_button == last_button
          @current_button = first_button
        else
          @current_button += 1
        end
      when Ncurses::KEY_UP, Ncurses::KEY_DOWN
        CDK.Beep
      when CDK::REFRESH
        @screen.erase
        @screen.refresh
      when CDK::KEY_ESC
        self.setExitType(input)
        @complete = true
      when Ncurses::ERR
        self.setExitType(input)
      when Ncurses::KEY_ENTER, CDK::KEY_RETURN
        self.setExitType(input)
        ret = @current_button
        @complete = true
      end
    end

    # Should we call a post_process?
    if !@complete && !(@post_process_func.nil?)
      @post_process_func.call(:DIALOG, self,
          @post_process_data, input)
    end
  end

  unless @complete
    self.drawButtons
    wrefresh(@win)
    self.setExitType(0)
  end

  @result_data = ret
  return ret
end

#object_typeObject



335
336
337
# File 'lib/cdk/components/dialog.rb', line 335

def object_type
  :DIALOG
end

#positionObject



339
340
341
# File 'lib/cdk/components/dialog.rb', line 339

def position
  super(@win)
end

#set(highlight, separator, box) ⇒ Object

This sets attributes of the dialog box.



271
272
273
274
275
# File 'lib/cdk/components/dialog.rb', line 271

def set(highlight, separator, box)
  self.setHighlight(highlight)
  self.setSeparator(separator)
  self.setBox(box)
end

#setBKattr(attrib) ⇒ Object

This sets the background attribute of the widget.



296
297
298
# File 'lib/cdk/components/dialog.rb', line 296

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

#setHighlight(highlight) ⇒ Object

This sets the highlight attribute for the buttons.



278
279
280
# File 'lib/cdk/components/dialog.rb', line 278

def setHighlight(highlight)
  @highlight = highlight
end

#setSeparator(separator) ⇒ Object

This sets whether or not the dialog box will have a separator line.



287
288
289
# File 'lib/cdk/components/dialog.rb', line 287

def setSeparator(separator)
  @separator = separator
end

#unfocusObject



331
332
333
# File 'lib/cdk/components/dialog.rb', line 331

def unfocus
  self.draw(@box)
end