Class: Canis::MessageBox

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#ORIG_process_key, #ORIGbind_key, #ORIGkeycode_tos, #_process_key, #bind_composite_mapping, #bind_key, #bind_keys, #check_composite_mapping, #create_logger, #define_key, #define_prefix_command, #execute_mapping, #get_attrib, #get_color, #key, #key_tos, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #view, #xxxbind_composite_mapping

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
72
73
74
75
# File 'lib/canis/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 = Canis::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
  # 2014-05-31 - 11:44 adding form color 
  #   try not to set buttons color in this program, let button pick up user or form colors
  @form.color = @color
  @form.bgcolor = @bgcolor
  @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/canis/core/widgets/rmessagebox.rb', line 33

def form
  @form
end

#windowObject (readonly)

Returns the value of attribute window.



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

def window
  @window
end

Instance Method Details

#command(*args, &blk) ⇒ Object



414
415
416
417
# File 'lib/canis/core/widgets/rmessagebox.rb', line 414

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

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



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

def item widget
  # # normalcolor gives a white on black stark title like links and elinks
  # You can also do 'acolor' to give you a sober title that does not take attention away, like mc
  # remove from existing form if set, problem with this is mnemonics -- rare situation.
  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
  # 2014-05-31 - 12:40 CANIS BUTTONCOLOR i have commented out since it should take from form
  #   to see effect
  if false
    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 !
  end
  @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



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

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 = Canis::Field.new @form, {:text => message, :name=>"message_label",
      :row => r, :col => message_col, :width => display_length,  
      :bgcolor => bgclr , :color => clr, :editable => false}
  else
    message_label = Canis::Label.new @form, {:text => message, :name=>"message_label",
      :row => r, :col => message_col, :width => display_length,  
      :height => message_height, :bgcolor => bgclr , :color => clr}
  end
  @maxrow = 3
  yield message_label if block_given?
end

#repaintObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/canis/core/widgets/rmessagebox.rb', line 124

def repaint
  _create_window unless @window
  acolor = get_color $reverscolor, @color, @bgcolor 
  $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, @color, @bgcolor)
  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_color ||= $normalcolor
  title = " "+@title+" "
  # normalcolor gives a white on black stark title like links and elinks
  # You can also do 'acolor' to give you a sober title that does not take attention away, like mc
  @window.printstring(@row=1,@col=(@width-title.length)/2,title, color=@title_color)
  #print_message if @message
  create_action_buttons unless @action_buttons
end

#runObject

returns button index Call this after instantiating the window



118
119
120
121
122
123
# File 'lib/canis/core/widgets/rmessagebox.rb', line 118

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)


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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/canis/core/widgets/rmessagebox.rb', line 185

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
  # now that we have moved to textpad that +8 was causing black lines to remain after the text
  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
  # replaced 2014-04-14 - 23:51 
  message_label = Canis::TextPad.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

#tree(message) {|tree| ... } ⇒ Object

similar to text but to view a hash using a tree object, so one can

drill down.

Added on 2014-08-30 - 17:39 to view an objects instance_variables and public_methods.

Yields:



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

def tree message
  require 'canis/core/widgets/tree'
  @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

  # now that we have moved to textpad that +8 was causing black lines to remain after the text
  #message_height = message.size #+ 8
  message_height = 20
  # 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
  # replaced 2014-04-14 - 23:51 
  tree = Canis::Tree.new @form, {:name=>"treedialogtree", :data => message,
    :row => r, :col => message_col, :width => display_length, :suppress_borders => true,
    :height => message_height, :bgcolor => bgclr , :color => clr}
  #message_label.set_content message
  tree.treemodel.root_visible = false
  yield tree 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



424
425
426
427
428
429
430
431
432
433
# File 'lib/canis/core/widgets/rmessagebox.rb', line 424

def widget n
  case n
  when Integer
    @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



419
420
421
# File 'lib/canis/core/widgets/rmessagebox.rb', line 419

def widgets
  @form.widgets
end