Class: RubyCurses::MessageBox

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#OLDdefine_key, #_process_key, #bind_key, #bind_keys, #clean_string!, #define_key, #define_prefix_command, #display_app_help, #get_attrib, #get_color, #keycode_tos, #last_line, #one_line_window, #parse_formatted_text, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #view, #wrap_text

Methods included from BorderTitle

#bordertitle_init, #print_borders, #print_title

Constructor Details

#initialize(config = {}, &block) ⇒ MessageBox

dsl_accessor :message you can also set button_orientation : :right, :left, :center



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

def initialize config={}, &block

  h = config.fetch(:height, nil)
  w = config.fetch(:width, nil)
  t = config.fetch(:row, nil)
  l = config.fetch(:col, nil)
  if h && w && t && l
    @window = VER::Window.new :height => h, :width => w, :top => t, :left => l
    @graphic = @window
  end
  @form = Form.new @window

  config.each_pair { |k,v| instance_variable_set("@#{k}",v) }
  @config = config
  @row = 0
  @col = 0
  @row_offset = 1
  @col_offset = 2
  #bordertitle_init
  @color ||= :black
  @bgcolor ||= :white
  @maxrow = 3

  #instance_eval &block if block_given?
  yield_or_eval &block if block_given?

end

Instance Attribute Details

#formObject (readonly)

Returns the value of attribute form.



33
34
35
# File 'lib/rbcurse/core/widgets/rmessagebox.rb', line 33

def form
  @form
end

#windowObject (readonly)

Returns the value of attribute window.



34
35
36
# File 'lib/rbcurse/core/widgets/rmessagebox.rb', line 34

def window
  @window
end

Instance Method Details

#command(*args, &blk) ⇒ Object



352
353
354
355
# File 'lib/rbcurse/core/widgets/rmessagebox.rb', line 352

def command *args, &blk
  @command = blk
  @args = args
end

#item(widget) ⇒ Object Also known as: add



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

def item widget
  # remove from existing form if set FIXME
  if widget.form
    f = widget.form
    f.remove_widget widget
  end
  @maxrow ||= 3
  widget.set_form @form
  widget.row ||= 0
  widget.col ||= 0
  if widget.row == 0
    widget.row = [@maxrow+1, 3].max if widget.row == 0
  else
    widget.row += @row_offset 
  end
  if widget.col === 0
    widget.col = 5 if widget.col === 0
  else
    # i don't know button_offset as yet
    widget.col += @col_offset 
  end
  # in most cases this override is okay, but what if user has set it
  # The problem is that widget and field are doing a default setting so i don't know
  # if user has set or widget has done a default setting. NOTE
  widget.color ||= @color    # we are overriding colors, how to avoid since widget sets it
  widget.bgcolor  ||= @bgcolor
  widget.attr = @attr if @attr # we are overriding what user has put. DARN !
  @maxrow = widget.row if widget.row > @maxrow
  @suggested_h = @height || @maxrow+6
  @suggested_w ||= 0
  @suggested_w = widget.col + 15 if widget.col > @suggested_w
  # if w's given col is > width then add to suggested_w or text.length
end

#message(message) { ... } ⇒ Object Also known as: message=

Pass a short message to be printed. This creates a label for a short message, and a field for a long one.

Parameters:

  • text (String)

    to display

Yields:

  • field created



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rbcurse/core/widgets/rmessagebox.rb', line 138

def message message # yield label or field being used for display for further customization
  @suggested_h = @height || 10
  message = message.gsub(/[\n\r\t]/,' ') rescue message
  message_col = 5
  @suggested_w = @width || [message.size + 8 + message_col , FFI::NCurses.COLS-2].min
  r = 3
  len = message.length
  @suggested_w = len + 8 + message_col if len < @suggested_w - 8 - message_col

  display_length = @suggested_w-8
  display_length -= message_col
  message_height = 2
  clr = @color || :white
  bgclr = @bgcolor || :black

  # trying this out. sometimes very long labels get truncated, so i give a field in wchich user
  # can use arrow key or C-a and C-e
  if message.size > display_length
    message_label = RubyCurses::Field.new @form, {:text => message, :name=>"message_label",
      :row => r, :col => message_col, :display_length => display_length,  
      :bgcolor => bgclr , :color => clr, :editable => false}
  else
    message_label = RubyCurses::Label.new @form, {:text => message, :name=>"message_label",
      :row => r, :col => message_col, :display_length => display_length,  
      :height => message_height, :bgcolor => bgclr , :color => clr}
  end
  @maxrow = 3
  yield message_label if block_given?
end

#repaintObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rbcurse/core/widgets/rmessagebox.rb', line 114

def repaint
  _create_window unless @window
  acolor = get_color $reverscolor # this implicitly uses color and bgcolor fooling me often
  $log.debug " MESSAGE BOX bg:#{@bgcolor} , co:#{@color} , colorpair:#{acolor}"
  @window.wbkgd(Ncurses.COLOR_PAIR(acolor)); #  does not work on xterm-256color

  #print_borders unless @suppress_borders # do this once only, unless everything changes
  #@window.print_border_mb 1,2, @height, @width, $normalcolor, FFI::NCurses::A_REVERSE
  @color_pair = get_color($datacolor)
  bordercolor = @border_color || @color_pair
  borderatt = @border_attrib || Ncurses::A_NORMAL
  @window.wattron(Ncurses.COLOR_PAIR(bordercolor) | (borderatt || FFI::NCurses::A_NORMAL))
  @window.print_border_mb 1,2, @height, @width, bordercolor, borderatt
  @window.wattroff(Ncurses.COLOR_PAIR(bordercolor) | (borderatt || FFI::NCurses::A_NORMAL))
  @title ||= "+-+"
  title = " "+@title+" "
  @window.printstring(@row=1,@col=(@width-title.length)/2,title, color=$normalcolor)
  #print_message if @message
  create_action_buttons unless @action_buttons
end

#runObject

returns button index Call this after instantiating the window



108
109
110
111
112
113
# File 'lib/rbcurse/core/widgets/rmessagebox.rb', line 108

def run
  repaint
  @form.repaint
  @window.wrefresh
  return handle_keys
end

#text(message) {|message_label| ... } ⇒ Object Also known as: text=

This is for larger messages, or messages where the size is not known. A textview object is created and yielded.

Yields:

  • (message_label)


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
218
# File 'lib/rbcurse/core/widgets/rmessagebox.rb', line 172

def text message
  @suggested_w = @width || (FFI::NCurses.COLS * 0.80).floor
  @suggested_h = @height || (FFI::NCurses.LINES * 0.80).floor

  message_col = 3
  r = 2
  display_length = @suggested_w-4
  display_length -= message_col
  clr = @color || :white
  bgclr = @bgcolor || :black

  if message.is_a? Array
    l = longest_in_list message
    if l > @suggested_w 
      if l < FFI::NCurses.COLS
        #@suggested_w = l
        @suggested_w = FFI::NCurses.COLS-2 
      else
        @suggested_w = FFI::NCurses.COLS-2 
      end
      display_length = @suggested_w-6
    end
    # reduce width and height if you can based on array contents
  else
    message = wrap_text(message, display_length).split("\n")
  end
  message_height = message.size + 8
  # reduce if possible if its not required.
  #
  r1 = (FFI::NCurses.LINES-@suggested_h)/2
  r1 = r1.floor
  w = @suggested_w
  c1 = (FFI::NCurses.COLS-w)/2
  c1 = c1.floor
  @suggested_row = r1
  @suggested_col = c1
  brow = @button_row || @suggested_h-4
  available_ht = brow - r + 1
  message_height = [message_height, available_ht].min
  require 'rbcurse/core/widgets/rtextview'
  message_label = RubyCurses::TextView.new @form, {:name=>"message_label", :text => message,
    :row => r, :col => message_col, :width => display_length, :suppress_borders => true,
    :height => message_height, :bgcolor => bgclr , :color => clr}
  #message_label.set_content message
  yield message_label if block_given?

end

#widget(n) ⇒ Object

returns a widget based on offset, or name (if you gave it a :name) e.g. a list was declared and we wish to know the selected_indices



362
363
364
365
366
367
368
369
370
371
# File 'lib/rbcurse/core/widgets/rmessagebox.rb', line 362

def widget n
  case n
  when Fixnum
    @form.widgets[n]
  when String, Symbol
    @form.by_name[n] 
  else
    raise "messagebox.widget can't handle #{n.class} "
  end
end

#widgetsObject

returns array of widgets declared for querying



357
358
359
# File 'lib/rbcurse/core/widgets/rmessagebox.rb', line 357

def widgets
  @form.widgets
end