Class: RubyCurses::MessageBox

Inherits:
Object
  • Object
show all
Includes:
DSL, Utils
Defined in:
lib/rbcurse/rmessagebox.rb

Overview

dimensions of window should be derived on content

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#_process_key, #bind_key, #clean_string!, #get_color, #keycode_tos, #repeatm, #wrap_text

Methods included from DSL

#OLD_method_missing

Constructor Details

#initialize(form = nil, aconfig = {}, &block) ⇒ MessageBox

Returns a new instance of MessageBox.



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
# File 'lib/rbcurse/rmessagebox.rb', line 40

def initialize form=nil, aconfig={}, &block
  @form = form
  @config = aconfig
  @buttons = []
  #@keys = {}
  @bcol = 5
  @selected_index = -1
  @config.each_pair { |k,v| instance_variable_set("@#{k}",v) }
  instance_eval &block if block_given?
  if @layout.nil? 
    case @type.to_s
    when "input"
      layout(10,60, 10, 20) 
    when "list"
      height = [5, @list.length].min 
      layout(10+height, 60, 5, 20)
    when "field_list"
      height = @field_list.length
      layout(10+height, 60, 5, 20)
    when "override"
      $log.debug " override: #{@height},#{@width}, #{@top}, #{@left} "
      layout(@height,@width, @top, @left) 
      $log.debug " override: #{@layout.inspect}"
    else
      height = @form && @form.widgets.length ## quick fix. FIXME
      height ||= 0
      layout(10+height,60, 10, 20) 
    end
  end
  @window = VER::Window.new(@layout)
  if @form.nil?
    @form = RubyCurses::Form.new @window
  else
    @form.window = @window
  end
  acolor = get_color $reversecolor
  $log.debug " MESSAGE BOX #{@bgcolor} , #{@color} , #{acolor}"
  @window.bkgd(Ncurses.COLOR_PAIR(acolor));
  @window.wrefresh
  @panel = @window.panel
  Ncurses::Panel.update_panels
  process_field_list
  print_borders
  print_title
  print_message unless @message.nil?
  print_input
  create_buttons
  @form.repaint
  @window.wrefresh
  handle_keys
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



27
28
29
# File 'lib/rbcurse/rmessagebox.rb', line 27

def config
  @config
end

#selected_indexObject (readonly)

button index selected by user



28
29
30
# File 'lib/rbcurse/rmessagebox.rb', line 28

def selected_index
  @selected_index
end

#windowObject (readonly)

required for keyboard



29
30
31
# File 'lib/rbcurse/rmessagebox.rb', line 29

def window
  @window
end

Instance Method Details

#center_column(textlen) ⇒ Object



235
236
237
238
# File 'lib/rbcurse/rmessagebox.rb', line 235

def center_column textlen
  width = @layout[:width]
  return (width-textlen)/2
end

#cget(param) ⇒ Object



325
326
327
# File 'lib/rbcurse/rmessagebox.rb', line 325

def cget param
  @config[param]
end

#configure(*val, &block) ⇒ Object



315
316
317
318
319
320
321
322
323
324
# File 'lib/rbcurse/rmessagebox.rb', line 315

def configure(*val , &block)
  case val.size
  when 1
    return @config[val[0]]
  when 2
    @config[val[0]] = val[1]
    instance_variable_set("@#{val[0]}", val[1]) 
  end
  instance_eval &block if block_given?
end

#create_buttonsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rbcurse/rmessagebox.rb', line 108

def create_buttons
  case @button_type.to_s.downcase
  when "ok"
    make_buttons ["&OK"]
  when "ok_cancel" #, "input", "list", "field_list"
    make_buttons %w[&OK &Cancel]
    # experience 2009-02-22 12:52 trapping y and n - hopefully wont cause an edit problem
    @form.bind_key(?o){ press(?\M-o) }   # called method takes care of getbyte 1.9
    @form.bind_key(?c){ press(?\M-c) }   # called method takes care of getbyte 1.9
  when "yes_no"
    make_buttons %w[&Yes &No]
    # experience 2009-02-22 12:52 trapping y and n - hopefully wont cause an edit problem
    @form.bind_key(?y){ press(?\M-y) }   # called method takes care of getbyte 1.9
    @form.bind_key(?n){ press(?\M-n) }   # called method takes care of getbyte 1.9
  when "yes_no_cancel"
    make_buttons ["&Yes", "&No", "&Cancel"]
  when "custom"
    raise "Blank list of buttons passed to custom" if @buttons.nil? or @buttons.size == 0
    make_buttons @buttons
  else
    $log.debug "No type passed for creating messagebox. Using default (OK)"
    make_buttons ["&OK"]
  end
end

#default_button(offset0) ⇒ Object



99
100
101
# File 'lib/rbcurse/rmessagebox.rb', line 99

def default_button offset0
  @selected_index = offset0
end

#destroyObject



332
333
334
335
336
337
# File 'lib/rbcurse/rmessagebox.rb', line 332

def destroy
  $log.debug "DESTROY : widget"
  panel = @window.panel
  Ncurses::Panel.del_panel(panel) if !panel.nil?   
  @window.delwin if !@window.nil?
end

#handle_keysObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rbcurse/rmessagebox.rb', line 162

def handle_keys
  begin
    while((ch = @window.getchar()) != 999 )
      case ch
      when -1
        next
      else
        press ch
        break if @stop
      end
    end
  ensure
    destroy  
  end
  return @selected_index
end

#input_valueObject

value entered by user if type = input



104
105
106
107
# File 'lib/rbcurse/rmessagebox.rb', line 104

def input_value
  return @input.buffer if !@input.nil?
  return @listbox.getvalue if !@listbox.nil?
end

#layout(height = 0, width = 0, top = 0, left = 0) ⇒ Object



329
330
331
# File 'lib/rbcurse/rmessagebox.rb', line 329

def layout(height=0, width=0, top=0, left=0)
  @layout = { :height => height, :width => width, :top => top, :left => left } 
end

#make_buttons(names) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rbcurse/rmessagebox.rb', line 132

def make_buttons names
  total = names.inject(0) {|total, item| total + item.length + 4}
  bcol = center_column total

  brow = @layout[:height]-3
  button_ct=0
  names.each_with_index do |bname, ix|
    text = bname
    #underline = @underlines[ix] if [email protected]?

    button = Button.new @form do
      text text
      name bname
      row brow
      col bcol
      #underline underline
      highlight_background $datacolor 
      color $reversecolor
      bgcolor $reversecolor
    end
    index = button_ct
    button.command { |form| @selected_index = index; @stop = true; $log.debug "Pressed Button #{bname}";}
    button_ct += 1
    bcol += text.length+6
  end
end

#press(ch) ⇒ Object



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
# File 'lib/rbcurse/rmessagebox.rb', line 178

def press ch
  ch = ch.getbyte(0) if ch.class==String ## 1.9
    case ch
    when -1
      return
    when KEY_F1, 27, ?\C-q.getbyte(0)   
      @selected_index = -1
      @stop = true
      return
    when KEY_ENTER, 10, 13
      field =  @form.get_current_field
      if field.respond_to? :fire
        field.fire
      end
      #$log.debug "popup ENTER : #{@selected_index} "
      #$log.debug "popup ENTER :  #{field.name}" if !field.nil?
      @stop = true
      return
    when 9
      @form.select_next_field
    when 353
      @form.select_prev_field
    else
      # fields must return unhandled else we will miss hotkeys. 
      # On messageboxes, often if no edit field, then O and C are hot.
      field =  @form.get_current_field
      handled = field.handle_key ch

      if handled == :UNHANDLED
        ret = @form.process_key ch, self ## trying out trigger button
      end
    end
    @form.repaint
    Ncurses::Panel.update_panels();
    Ncurses.doupdate();
    @window.wrefresh
end


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/rbcurse/rmessagebox.rb', line 215

def print_borders
  width = @layout[:width]
  height = @layout[:height]
  @window.print_border_mb 1,2, height, width, $normalcolor, A_REVERSE
=begin
  start = 2
  hline = "+%s+" % [ "-"*(width-((start+1)*2)) ]
  hline2 = "|%s|" % [ " "*(width-((start+1)*2)) ]
  @window.printstring(row=1, col=start, hline, color=$reversecolor)
  (start).upto(height-2) do |row|
    @window.printstring row, col=start, hline2, color=$normalcolor, A_REVERSE
  end
  @window.printstring(height-2, col=start, hline, color=$reversecolor)
=end
end


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
# File 'lib/rbcurse/rmessagebox.rb', line 273

def print_input
  #return if @type.to_s != "input"
  @message_height ||= 0
  @message_row ||= 2
  @message_col ||= 2
  r = @message_row + @message_height + 1
  c = @message_col
  disp_len = @layout[:width]-8
  defaultvalue = @default_value || ""
  input_config = @config["input_config"] || {}
  case @type.to_s 
  when "input"
    @input = RubyCurses::Field.new @form, input_config do
      name   "input" 
      row  r 
      col  c 
      display_length disp_len
      set_buffer defaultvalue
    end
  when "list"
    list = @list
    selection_mode = @list_selection_mode 
    default_values = @default_values
    $log.debug " value of select_mode #{selection_mode}"
    @listbox = RubyCurses::Listbox.new @form do
      name   "input" 
      row  r 
      col  c 
#         attr 'reverse'
      color 'black'
      bgcolor 'white'
      width 30
      height 6
      list  list
      # ?? display_length  30
      #set_buffer defaultvalue
      selection_mode selection_mode
      default_values default_values
      is_popup false
    end
  end
end


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/rbcurse/rmessagebox.rb', line 239

def print_message message=@message, row=nil
  @message_row = @message_col = 2
  display_length = @layout[:width]-8
    $log.debug " print_message: dl:#{display_length} "
  # XXX this needs to go up and decide height of window
  if @message_height.nil?
    @message_height = (message.length/display_length)+1
    $log.debug " print_message: mh:#{@message_height}, ml: #{message.length}"
  end
  @message_height ||= 1
  width = @layout[:width]
  return if message.nil?
  case @type.to_s
  when "input" 
    row=(@layout[:height]/3) if row.nil?
    @message_col = 4
  when "list" 
    row=3
    @message_col = 4 
  else
    row=(@layout[:height]/3) if row.nil?
    @message_col = (width-message.length)/2
  end
  @message_row = row
  # added 2009-11-05 14:53 to fix erasure of border
  display_length -= @message_col
  # FIXME : wont print if newline at end of message !!!
  #@window.printstring( row, @message_col , message, color=$reversecolor)
  # 2008-12-30 19:45 experimenting with label so we can get justify and wrapping.
  #@window.printstring( row, @message_col , message, color=$reversecolor)
    $log.debug " print_message: row #{row}, col #{@message_col} "
  message_label = RubyCurses::Label.new @form, {'text' => message, "name"=>"message_label","row" => row, "col" => @message_col, "display_length" => display_length,  "height" => @message_height, "attr"=>"reverse"}

end

start = 2

hline = "+%s+" % [ "-"*(width-((start+1)*2)) ]
hline2 = "|%s|" % [ " "*(width-((start+1)*2)) ]
@window.printstring(row=1, col=start, hline, color=$reversecolor)
(start).upto(height-2) do |row|
  @window.printstring row, col=start, hline2, color=$normalcolor, A_REVERSE
end
@window.printstring(height-2, col=start, hline, color=$reversecolor)


230
231
232
233
234
# File 'lib/rbcurse/rmessagebox.rb', line 230

def print_title title=@title
  width = @layout[:width]
  title = " "+title+" "
  @window.printstring(row=1,col=(width-title.length)/2,title, color=$normalcolor)
end

#process_field_listObject

takes care of a field list sent in



93
94
95
96
97
98
# File 'lib/rbcurse/rmessagebox.rb', line 93

def process_field_list
  return if @field_list.nil? or @field_list.length == 0
  @field_list.each do |f|
    f.set_form @form
  end
end

#stopping?Boolean

message box

Returns:

  • (Boolean)


159
160
161
# File 'lib/rbcurse/rmessagebox.rb', line 159

def stopping?
  @stop
end