ncumbra 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,9 +4,12 @@
4
4
  # Author: j kepler http://github.com/mare-imbrium/canis/
5
5
  # Date: 2018-04-13 - 23:10
6
6
  # License: MIT
7
- # Last update: 2018-04-21 14:44
7
+ # Last update: 2018-05-17 12:33
8
+ # ----------------------------------------------------------------------------- #
9
+ # messagebox.rb Copyright (C) 2012-2018 j kepler
10
+ # BUGS:
11
+ # - hangs if we add more than 23 items using +add+. Fixed. Don't let window size exceed LINES
8
12
  # ----------------------------------------------------------------------------- #
9
- # YFF Copyright (C) 2012-2018 j kepler
10
13
  require 'umbra/window'
11
14
  require 'umbra/form'
12
15
  require 'umbra/widget'
@@ -16,6 +19,17 @@ require 'umbra/label'
16
19
  require 'umbra/textbox'
17
20
 
18
21
  module Umbra
22
+
23
+
24
+ ###################################################################################################
25
+ ## Class: MessageBox
26
+ ##
27
+ ## Description: A configurable dialog box. Creates a window which allows caller to add
28
+ ## widgets such as fields and buttons to it. Returns the offset of the button pressed
29
+ ## so the caller knows if Ok or Cancel etc was pressed.
30
+ ##
31
+ ###################################################################################################
32
+
19
33
  class MessageBox
20
34
 
21
35
  attr_reader :form
@@ -44,7 +58,7 @@ module Umbra
44
58
  # repaint
45
59
  end
46
60
  @form = Form.new @window
47
- @buttons = ["Ok", "Cancel"]
61
+ @buttons = ["Ok", "Cancel"] ## default button, can be overridden
48
62
 
49
63
  config.each_pair { |k,v| instance_variable_set("@#{k}",v) }
50
64
  @config = config
@@ -53,56 +67,62 @@ module Umbra
53
67
  @row_offset = 1
54
68
  @col_offset = 2
55
69
 
56
- #@color ||= :black
57
- #@bgcolor ||= :white
58
70
  @color_pair = CP_BLACK
59
- # 2014-05-31 - 11:44 adding form color
60
- # try not to set buttons color in this program, let button pick up user or form colors
61
- #@form.color = @color
62
- #@form.bgcolor = @bgcolor
63
- #@form.color_pair = @color_pair
71
+
64
72
  @maxrow = 3
65
73
 
66
74
  instance_eval &block if block_given?
67
75
  #yield_or_eval &block if block_given? TODO
68
76
 
69
77
  end
78
+
79
+
80
+
81
+ ###########################################################################
82
+ ## Add a widget to the messagebox
83
+ ##
84
+ ## Example: item( field )
85
+ ## or add ( field )
86
+ ##
87
+ ## If row is not specified, then each call will add 1 to the row
88
+ ## If height of the messagebox is not specified, then it will be computed
89
+ ## from the row of the last widget.
90
+ ##
91
+ ###########################################################################
92
+
70
93
  def item widget
71
94
  # # normalcolor gives a white on black stark title like links and elinks
72
95
  # You can also do 'acolor' to give you a sober title that does not take attention away, like mc
73
96
  # remove from existing form if set, problem with this is mnemonics -- rare situation.
74
- #if widget.form
75
- #f = widget.form
76
- #f.remove_widget widget
77
- #end
78
97
  @maxrow ||= 3
79
- #widget.set_form @form
80
98
  @form.add_widget widget
81
99
  widget.row ||= 0
82
100
  widget.col ||= 0
101
+
102
+ ## if caller does not specify row, then keep incrementing
83
103
  if widget.row == 0
84
- widget.row = [@maxrow+1, 3].max if widget.row == 0
104
+ widget.row = [@maxrow+1, 3].max
85
105
  else
86
- widget.row += @row_offset
106
+ widget.row += @row_offset ## add one to row if stated by user
87
107
  end
108
+
88
109
  if widget.col == 0
89
110
  widget.col = 5
90
111
  else
91
112
  # i don't know button_offset as yet
92
113
  widget.col += @col_offset
93
114
  end
94
- # in most cases this override is okay, but what if user has set it
95
- # The problem is that widget and field are doing a default setting so i don't know
96
- # if user has set or widget has done a default setting. NOTE
97
- # 2014-05-31 - 12:40 CANIS BUTTONCOLOR i have commented out since it should take from form
98
- # to see effect
99
- if false
100
- widget.color ||= @color # we are overriding colors, how to avoid since widget sets it
101
- widget.bgcolor ||= @bgcolor
102
- widget.attr = @attr if @attr # we are overriding what user has put. DARN !
103
- end
115
+
104
116
  @maxrow = widget.row if widget.row > @maxrow
105
117
  @suggested_h = @height || @maxrow+6
118
+
119
+ ## check that window does not exceed LINES else program will hang
120
+ lines = FFI::NCurses.LINES
121
+ @suggested_h = lines if @suggested_h > lines
122
+ if widget.row > lines
123
+ $log.warning "MESSAGEBOX placing widget at row (#{widget.row} > #{lines}. You are placing too many items."
124
+ end
125
+
106
126
  @suggested_w ||= 0
107
127
  ww = widget.width || 5 # some widgets do no set a default width, and could be null
108
128
  _w = [ww + 5, 15].max
@@ -110,19 +130,35 @@ module Umbra
110
130
  if ww >= @suggested_w
111
131
  @suggested_w = ww + widget.col + 10
112
132
  end
113
- $log.debug " MESSAGEBOX add suggested_w #{@suggested_w} "
133
+ #$log.debug " MESSAGEBOX add suggested_w #{@suggested_w} , suggested_h : #{@suggested_h}, maxrow #{@maxrow}, LINES= #{FFI::NCurses.LINES} "
114
134
  # if w's given col is > width then add to suggested_w or text.length
115
135
  end
116
136
  alias :add :item
117
- # returns button index
137
+
138
+
139
+
140
+ ###########################################################################
141
+ ## Method: run
142
+ #
143
+ # Description: creates the window, paints all the objects, creates the
144
+ # buttons and catches input in a loop.
145
+ #
146
+ # Example: key - mb.run
147
+ #
148
+ # Returns: offset of button pressed (starting 0)
149
+ ###########################################################################
150
+
118
151
  # Call this after instantiating the window
119
152
  def run
120
153
  repaint
121
154
  @form.pack # needs window
155
+ @form.select_first_field ## otherwise on_enter of first won't fire
122
156
  @form.repaint
123
157
  @window.wrefresh
124
158
  return handle_keys
125
159
  end
160
+
161
+ ## paints the messagebox and creates the buttons (INTERNAL)
126
162
  def repaint
127
163
  _create_window unless @window
128
164
  #acolor = get_color $reverscolor, @color, @bgcolor
@@ -146,6 +182,9 @@ module Umbra
146
182
  #print_message if @message
147
183
  create_action_buttons(*@buttons) unless @action_buttons
148
184
  end
185
+
186
+
187
+ ## creates the buttons (INTERNAL)
149
188
  def create_action_buttons *labels
150
189
  @action_buttons = []
151
190
  _row = @height-3
@@ -162,12 +201,24 @@ module Umbra
162
201
  throw(:close, ix)
163
202
  end
164
203
  end
204
+ ## 2018-05-17 - associate RETURN ENTER key with first button (FIXME) should be Ok or Okay or user
205
+ ## should have some say in this. Same for associating ESC with Cancel or Quit.
206
+ @form.bind_key(10, "Fire Ok button") { @action_buttons.first.fire }
165
207
  end
208
+
209
+ ####################################################################################################
210
+ ##
211
+ ## Method: message (String)
212
+ ##
213
+ ## Description: prints a short message in a messagebox.
214
+ ## This creates a label for a short message, and a scrollable field for a long one.
215
+ ##
216
+ ## @yield field created
217
+ ## @param [String] text to display
166
218
  # CLEAN THIS UP TODO
167
- # Pass a short message to be printed.
168
- # This creates a label for a short message, and a field for a long one.
169
- # @yield field created
170
- # @param [String] text to display
219
+ ####################################################################################################
220
+
221
+
171
222
  def message message # yield label or field being used for display for further customization
172
223
  @suggested_h = @height || 10
173
224
  message = message.gsub(/[\n\r\t]/,' ') rescue message
@@ -182,8 +233,6 @@ module Umbra
182
233
  display_length = @suggested_w-_pad
183
234
  display_length -= message_col
184
235
  message_height = 2
185
- #clr = @color || :white
186
- #bgclr = @bgcolor || :black
187
236
 
188
237
  color_pair = CP_WHITE
189
238
  # trying this out. sometimes very long labels get truncated, so i give a field in wchich user
@@ -297,16 +346,18 @@ module Umbra
297
346
  while((ch = @window.getch()) != FFI::NCurses::KEY_F10 )
298
347
  break if ch == ?\C-q.getbyte(0) || ch == 2727 # added double esc
299
348
  begin
300
- # trying out repaint of window also if repaint all asked for. 12 is C-l
301
- if ch == 1000 or ch == 12
302
- repaint
349
+ # trying out repaint of window also if repaint all asked for. 18 is C-r
350
+ if ch == 1000 or ch == 18
351
+ repaint_all_widgets
303
352
  end
304
353
  @form.handle_key(ch)
305
354
  @window.wrefresh
306
355
  rescue => err
307
- $log.debug( err) if err
308
- $log.debug(err.backtrace.join("\n")) if err
309
- #textdialog ["Error in Messagebox: #{err} ", *err.backtrace], :title => "Exception" # TODO
356
+ if $log
357
+ $log.debug( err) if err
358
+ $log.debug(err.backtrace.join("\n")) if err
359
+ end
360
+ textdialog ["Error in Messagebox: #{err} ", *err.backtrace], :title => "Exception"
310
361
  @window.refresh # otherwise the window keeps showing (new FFI-ncurses issue)
311
362
  ensure
312
363
  end
@@ -314,7 +365,7 @@ module Umbra
314
365
  end # while loop
315
366
  end # close
316
367
  $log.debug "MESSAGEBOX: CALLING PROGRAM BEING RETURNED: #{buttonindex} "
317
- @window.destroy
368
+ @window.destroy ## 2018-05-17 - this should come in ensure block ???
318
369
  # added 2014-05-01 - 18:10 hopefully to refresh root_window.
319
370
  #Window.refresh_all
320
371
  return buttonindex