rbcurse 1.1.5 → 1.2.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/CHANGELOG +45 -0
  2. data/Makefile +1 -1
  3. data/Manifest.txt +91 -0
  4. data/NOTES +349 -2
  5. data/README.markdown +12 -0
  6. data/VERSION +1 -1
  7. data/examples/abasiclist.rb +25 -0
  8. data/examples/alpmenu.rb +42 -0
  9. data/examples/app.rb +883 -0
  10. data/examples/appcombo.rb +17 -0
  11. data/examples/appdirtree.rb +73 -0
  12. data/examples/appemail.rb +164 -0
  13. data/examples/appemaillb.rb +308 -0
  14. data/examples/appgcompose.rb +303 -0
  15. data/examples/appgmail.rb +951 -0
  16. data/examples/atree.rb +56 -0
  17. data/examples/dirtree.rb +78 -0
  18. data/examples/focusmanager.rb +31 -0
  19. data/examples/imap.rb +48 -0
  20. data/examples/menu1.rb +79 -0
  21. data/examples/multispl.rb +86 -0
  22. data/examples/rfe.rb +3 -4
  23. data/examples/rmail.rb +188 -0
  24. data/examples/s.rb +10 -0
  25. data/examples/scrollbar.rb +104 -0
  26. data/examples/splitp.rb +56 -0
  27. data/examples/table1.rb +30 -0
  28. data/examples/term.rb +48 -0
  29. data/examples/term2.rb +54 -0
  30. data/examples/test1.rb +4 -2
  31. data/examples/test2.rb +9 -9
  32. data/examples/testapp.rb +44 -0
  33. data/examples/testapp2.rb +51 -0
  34. data/examples/testcombo.rb +2 -2
  35. data/examples/testgmail.rb +46 -0
  36. data/examples/testlistbox.rb +0 -1
  37. data/examples/testmultispl.rb +199 -0
  38. data/examples/testree.rb +127 -0
  39. data/examples/testscroller.rb +0 -1
  40. data/examples/testscrolllb.rb +1 -1
  41. data/examples/testscrollp.rb +2 -1
  42. data/examples/testscrollta.rb +1 -1
  43. data/examples/testscrolltable.rb +1 -2
  44. data/examples/testsplit.rb +1 -1
  45. data/examples/testsplit2.rb +1 -1
  46. data/examples/testsplit3.rb +1 -1
  47. data/examples/testsplit3_1.rb +1 -1
  48. data/examples/testsplit3a.rb +1 -1
  49. data/examples/testsplit3b.rb +1 -1
  50. data/examples/testsplitta.rb +1 -1
  51. data/examples/testsplittv.rb +1 -1
  52. data/examples/testsplittvv.rb +1 -1
  53. data/examples/testtodo.rb +491 -488
  54. data/examples/testvimsplit.rb +111 -0
  55. data/examples/todo.db +0 -0
  56. data/examples/todocsv.csv +28 -0
  57. data/examples/viewtodo.rb +408 -403
  58. data/lib/rbcurse/action.rb +1 -0
  59. data/lib/rbcurse/app.rb +1294 -0
  60. data/lib/rbcurse/applicationheader.rb +7 -2
  61. data/lib/rbcurse/checkboxcellrenderer.rb +0 -12
  62. data/lib/rbcurse/colormap.rb +34 -8
  63. data/lib/rbcurse/comboboxcellrenderer.rb +0 -11
  64. data/lib/rbcurse/defaultlistselectionmodel.rb +23 -7
  65. data/lib/rbcurse/extras/bottomline.rb +1681 -0
  66. data/lib/rbcurse/extras/directorylist.rb +445 -0
  67. data/lib/rbcurse/extras/directorytree.rb +69 -0
  68. data/lib/rbcurse/extras/divider.rb +310 -0
  69. data/lib/rbcurse/extras/focusmanager.rb +31 -0
  70. data/lib/rbcurse/extras/listselectable.rb +222 -0
  71. data/lib/rbcurse/extras/masterdetail.rb +164 -0
  72. data/lib/rbcurse/extras/menutree.rb +63 -0
  73. data/lib/rbcurse/extras/rlink.rb +27 -0
  74. data/lib/rbcurse/extras/rmenulink.rb +21 -0
  75. data/lib/rbcurse/extras/scrollbar.rb +134 -0
  76. data/lib/rbcurse/extras/stdscrwindow.rb +247 -0
  77. data/lib/rbcurse/extras/tabular.rb +258 -0
  78. data/lib/rbcurse/extras/tabularwidget.rb +1070 -0
  79. data/lib/rbcurse/extras/viewer.rb +106 -0
  80. data/lib/rbcurse/io.rb +137 -80
  81. data/lib/rbcurse/keylabelprinter.rb +4 -0
  82. data/lib/rbcurse/listcellrenderer.rb +91 -59
  83. data/lib/rbcurse/listscrollable.rb +93 -95
  84. data/lib/rbcurse/listselectable.rb +60 -7
  85. data/lib/rbcurse/ractionevent.rb +67 -0
  86. data/lib/rbcurse/rbasiclistbox.rb +688 -0
  87. data/lib/rbcurse/rcombo.rb +5 -5
  88. data/lib/rbcurse/rcommandwindow.rb +555 -0
  89. data/lib/rbcurse/rinputdataevent.rb +12 -0
  90. data/lib/rbcurse/rlistbox.rb +305 -124
  91. data/lib/rbcurse/rmenu.rb +99 -46
  92. data/lib/rbcurse/rmessagebox.rb +13 -6
  93. data/lib/rbcurse/rmulticontainer.rb +54 -93
  94. data/lib/rbcurse/rmultisplit.rb +731 -0
  95. data/lib/rbcurse/rmultitextview.rb +3 -2
  96. data/lib/rbcurse/rpopupmenu.rb +0 -1
  97. data/lib/rbcurse/rprogress.rb +117 -0
  98. data/lib/rbcurse/rscrollpane.rb +2 -1
  99. data/lib/rbcurse/rsplitpane.rb +94 -20
  100. data/lib/rbcurse/rsplitpane2.rb +1009 -0
  101. data/lib/rbcurse/rtabbedpane.rb +3 -2
  102. data/lib/rbcurse/rtabbedwindow.rb +0 -1
  103. data/lib/rbcurse/rtable.rb +92 -64
  104. data/lib/rbcurse/rtextarea.rb +91 -57
  105. data/lib/rbcurse/rtextview.rb +223 -70
  106. data/lib/rbcurse/rtree.rb +723 -0
  107. data/lib/rbcurse/rviewport.rb +2 -1
  108. data/lib/rbcurse/rvimsplit.rb +768 -0
  109. data/lib/rbcurse/rwidget.rb +524 -325
  110. data/lib/rbcurse/table/tablecellrenderer.rb +1 -1
  111. data/lib/rbcurse/table/tabledatecellrenderer.rb +0 -1
  112. data/lib/rbcurse/tree/treecellrenderer.rb +137 -0
  113. data/lib/rbcurse/tree/treemodel.rb +428 -0
  114. data/lib/rbcurse/vieditable.rb +14 -13
  115. data/lib/ver/ncurses.rb +6 -0
  116. data/lib/ver/window.rb +67 -32
  117. metadata +99 -23
  118. data/bin/rbcurse +0 -0
  119. data/examples/rvimsplit.rb +0 -376
  120. data/examples/todo.rb +0 -1
  121. data/lib/rbcurse/rform.rb +0 -845
  122. data/lib/rbcurse/selectable.rb +0 -94
  123. data/rbcurse.gemspec +0 -188
@@ -26,19 +26,26 @@ require 'logger'
26
26
  #require 'rbcurse/mapper'
27
27
  require 'rbcurse/colormap'
28
28
  require 'rbcurse/orderedhash'
29
+ require 'rbcurse/rinputdataevent' # for FIELD 2010-09-11 12:31
29
30
  require 'rbcurse/io'
30
31
 
31
- module DSL
32
- ## others may not want this, if = sent, it creates DSL and sets
33
- # using this resulted in time lost in bedebugging why some method was not working.
34
- def OLD_method_missing(sym, *args)
35
- $log.debug "METHOD MISSING : #{sym} #{self} "
36
- #if "#{sym}"[-1].chr=="="
37
- # sym = "#{sym}"[0..-2]
38
- #else
39
- self.class.dsl_accessor sym
40
- #end
41
- send(sym, *args)
32
+ # some of these will get overriden by ncurses when we include
33
+ KEY_TAB = 9
34
+ #KEY_BTAB = 353 # nc gives same
35
+ KEY_RETURN = 13 # Nc gives 343 for KEY_ENTER
36
+ KEY_DELETE = 330
37
+ KEY_BSPACE = 127 # Nc gives 263 for BACKSPACE
38
+ KEY_CC = 3 # C-c
39
+
40
+ class Object
41
+ # thanks to terminal-table for this method
42
+ def yield_or_eval &block
43
+ return unless block
44
+ if block.arity > 0
45
+ yield self
46
+ else
47
+ self.instance_eval(&block)
48
+ end
42
49
  end
43
50
  end
44
51
  class Module
@@ -52,6 +59,7 @@ class Module
52
59
  @#{sym}
53
60
  else
54
61
  @#{sym} = val.size == 1 ? val[0] : val
62
+ # i am itching to deprecate next line XXX
55
63
  @config["#{sym}"]=@#{sym}
56
64
  end
57
65
  end
@@ -59,6 +67,8 @@ class Module
59
67
  }
60
68
  }
61
69
  end
70
+ # Besides creating getters and setters, this also fires property change handler
71
+ # if the value changes, and after the object has been painted once.
62
72
  def dsl_property(*symbols)
63
73
  symbols.each { |sym|
64
74
  class_eval %{
@@ -69,14 +79,18 @@ class Module
69
79
  oldvalue = @#{sym}
70
80
  @#{sym} = val.size == 1 ? val[0] : val
71
81
  newvalue = @#{sym}
82
+ # i am itching to deprecate next line XXX
72
83
  @config["#{sym}"]=@#{sym}
84
+ return if oldvalue.nil? || @_object_created.nil?
73
85
  if oldvalue != newvalue
74
- fire_property_change("#{sym}", oldvalue, newvalue)
86
+ # trying to reduce calls to fire, when object is being created
87
+ fire_property_change("#{sym}", oldvalue, newvalue) if !oldvalue.nil?
75
88
  end
76
89
  end
77
90
  end
78
91
  #attr_writer sym
79
92
  def #{sym}=val
93
+ # TODO if Variable, take .value NEXT VERSION
80
94
  #{sym}(val)
81
95
  end
82
96
  }
@@ -121,12 +135,14 @@ module RubyCurses
121
135
  "\\1\\3\n")
122
136
  end
123
137
  def clean_string! content
124
- content.chomp! # don't display newline
125
- content.gsub!(/[\t\n]/, ' ') # don't display tab
126
- content.gsub!(/[^[:print:]]/, '') # don't display non print characters
127
- content
138
+ content.chomp! # don't display newline
139
+ content.gsub!(/[\t\n]/, ' ') # don't display tab
140
+ content.gsub!(/[^[:print:]]/, '') # don't display non print characters
141
+ content
128
142
  end
129
143
  # needs to move to a keystroke class
144
+ # please use these only for printing or debugging, not comparing
145
+ # I could soon return symbols instead 2010-09-07 14:14
130
146
  def keycode_tos keycode
131
147
  case keycode
132
148
  when 33..126
@@ -140,9 +156,9 @@ module RubyCurses
140
156
  when ?\M-0.getbyte(0)..?\M-9.getbyte(0)
141
157
  return "M-"+ (keycode-?\M-0.getbyte(0)).to_s
142
158
  when 32
143
- return "Space"
159
+ return "space" # changed to lowercase so consistent
144
160
  when 27
145
- return "Esc"
161
+ return "esc" # changed to lowercase so consistent
146
162
  when ?\C-].getbyte(0)
147
163
  return "C-]"
148
164
  when 258
@@ -167,6 +183,8 @@ module RubyCurses
167
183
  return "M-F"+ (keycode-392).to_s
168
184
  when 0
169
185
  return "C-space" # i hope this is correct, just guessing
186
+ when 160
187
+ return "M-space" # at least on OSX Leopard now (don't remember this working on PPC)
170
188
  else
171
189
  others=[?\M--,?\M-+,?\M-=,?\M-',?\M-",?\M-;,?\M-:,?\M-\,, ?\M-.,?\M-<,?\M->,?\M-?,?\M-/]
172
190
  others.collect! {|x| x.getbyte(0) } ## added 2009-10-04 14:25 for 1.9
@@ -180,12 +198,20 @@ module RubyCurses
180
198
  end
181
199
  end
182
200
 
201
+ # if passed a string in second or third param, will create a color
202
+ # and return, else it will return default color
203
+ # Use this in order to create a color pair with the colors
204
+ # provided, however, if user has not provided, use supplied
205
+ # default.
206
+ # @param [Fixnum] color_pair created by ncurses
207
+ # @param [Symbol] color name such as white black cyan magenta red green yellow
208
+ # @param [Symbol] bgcolor name such as white black cyan magenta red green yellow
209
+ # @example get_color $promptcolor, :white, :cyan
183
210
  def get_color default=$datacolor, color=@color, bgcolor=@bgcolor
184
- if bgcolor.is_a? String and color.is_a? String
185
- acolor = ColorMap.get_color(color, bgcolor)
186
- else
187
- acolor = default
188
- end
211
+ return default if color.nil? || bgcolor.nil?
212
+ raise ArgumentError, "Color not valid: #{color}: #{ColorMap.colors} " if !ColorMap.is_color? color
213
+ raise ArgumentError, "Bgolor not valid: #{bgcolor} : #{ColorMap.colors} " if !ColorMap.is_color? bgcolor
214
+ acolor = ColorMap.get_color(color, bgcolor)
189
215
  return acolor
190
216
  end
191
217
  ## repeats the given action based on how value of universal numerica argument
@@ -197,55 +223,59 @@ module RubyCurses
197
223
  $multiplier = 0
198
224
  $inside_multiplier_action = false
199
225
  end
200
-
201
- ##
202
- # bind an action to a key, required if you create a button which has a hotkey
203
- # or a field to be focussed on a key, or any other user defined action based on key
204
- # e.g. bind_key ?\C-x, object, block
205
- # added 2009-01-06 19:13 since widgets need to handle keys properly
206
- # 2010-02-24 12:43 trying to take in multiple key bindings, TODO unbind
207
- # TODO add symbol so easy to map from config file or mapping file
208
- def bind_key keycode, *args, &blk
209
- @key_handler ||= {}
210
- if !block_given?
211
- blk = args.pop
212
- raise "If block not passed, last arg should be a method symbol" if !blk.is_a? Symbol
213
- $log.debug " #{@name} bind_key received a symbol #{blk} "
214
- end
215
- case keycode
216
- when String
217
- keycode = keycode.getbyte(0) #if keycode.class==String ## 1.9 2009-10-05 19:40
218
- $log.debug " #{name} Widg String called bind_key BIND #{keycode}, #{keycode_tos(keycode)} "
219
- @key_handler[keycode] = blk
220
- when Array
221
- # for starters lets try with 2 keys only
222
- a0 = keycode[0]
223
- a0 = keycode[0].getbyte(0) if keycode[0].class == String
224
- a1 = keycode[1]
225
- a1 = keycode[1].getbyte(0) if keycode[1].class == String
226
- @key_handler[a0] ||= OrderedHash.new
227
- @key_handler[a0][a1] = blk
228
- else
229
- @key_handler[keycode] = blk
230
- end
231
- @key_args ||= {}
232
- @key_args[keycode] = args
233
- end
234
- # e.g. process_key ch, self
235
- # returns UNHANDLED if no block for it
236
- # after form handles basic keys, it gives unhandled key to current field, if current field returns
237
- # unhandled, then it checks this map.
238
- # added 2009-01-06 19:13 since widgets need to handle keys properly
239
- # added 2009-01-18 12:58 returns ret val of blk.call
240
- # so that if block does not handle, the key can still be handled
241
- # e.g. table last row, last col does not handle, so it will auto go to next field
242
- # 2010-02-24 13:45 handles 2 key combinations, copied from Form, must be identical in logic
243
- # except maybe for window pointer. TODO not tested
244
- def _process_key keycode, object, window
245
- return :UNHANDLED if @key_handler.nil?
246
- blk = @key_handler[keycode]
247
- return :UNHANDLED if blk.nil?
248
- if blk.is_a? OrderedHash
226
+
227
+ ##
228
+ # bind an action to a key, required if you create a button which has a hotkey
229
+ # or a field to be focussed on a key, or any other user defined action based on key
230
+ # e.g. bind_key ?\C-x, object, block
231
+ # added 2009-01-06 19:13 since widgets need to handle keys properly
232
+ # 2010-02-24 12:43 trying to take in multiple key bindings, TODO unbind
233
+ # TODO add symbol so easy to map from config file or mapping file
234
+ def bind_key keycode, *args, &blk
235
+ $log.debug " #{@name} bind_key received #{keycode} "
236
+ @key_handler ||= {}
237
+ if !block_given?
238
+ blk = args.pop
239
+ raise "If block not passed, last arg should be a method symbol" if !blk.is_a? Symbol
240
+ #$log.debug " #{@name} bind_key received a symbol #{blk} "
241
+ end
242
+ case keycode
243
+ when String
244
+ keycode = keycode.getbyte(0) #if keycode.class==String ## 1.9 2009-10-05 19:40
245
+ #$log.debug " #{name} Widg String called bind_key BIND #{keycode}, #{keycode_tos(keycode)} "
246
+ @key_handler[keycode] = blk
247
+ when Array
248
+ # for starters lets try with 2 keys only
249
+ raise "A one key array will not work. Pass without array" if keycode.size == 1
250
+ a0 = keycode[0]
251
+ a0 = keycode[0].getbyte(0) if keycode[0].class == String
252
+ a1 = keycode[1]
253
+ a1 = keycode[1].getbyte(0) if keycode[1].class == String
254
+ @key_handler[a0] ||= OrderedHash.new
255
+ @key_handler[a0][a1] = blk
256
+ #$log.debug "XXX XX assigning #{keycode} to key_handler " if $log.debug?
257
+ else
258
+ #$log.debug "XXX assigning #{keycode} to key_handler " if $log.debug?
259
+ @key_handler[keycode] = blk
260
+ end
261
+ @key_args ||= {}
262
+ @key_args[keycode] = args
263
+ end
264
+ # e.g. process_key ch, self
265
+ # returns UNHANDLED if no block for it
266
+ # after form handles basic keys, it gives unhandled key to current field, if current field returns
267
+ # unhandled, then it checks this map.
268
+ # added 2009-01-06 19:13 since widgets need to handle keys properly
269
+ # added 2009-01-18 12:58 returns ret val of blk.call
270
+ # so that if block does not handle, the key can still be handled
271
+ # e.g. table last row, last col does not handle, so it will auto go to next field
272
+ # 2010-02-24 13:45 handles 2 key combinations, copied from Form, must be identical in logic
273
+ # except maybe for window pointer. TODO not tested
274
+ def _process_key keycode, object, window
275
+ return :UNHANDLED if @key_handler.nil?
276
+ blk = @key_handler[keycode]
277
+ return :UNHANDLED if blk.nil?
278
+ if blk.is_a? OrderedHash
249
279
  ch = window.getch
250
280
  if ch < 0 || ch > 255
251
281
  #next
@@ -257,15 +287,22 @@ module RubyCurses
257
287
  return nil if blk1.nil?
258
288
  $log.debug " process_key: found block for #{keycode} , #{ch} "
259
289
  blk = blk1
290
+ end
291
+ #$log.debug "called process_key #{object}, kc: #{keycode}, args #{@key_args[keycode]}"
292
+ if blk.is_a? Symbol
293
+ $log.debug "SYMBOL " if $log.debug?
294
+ return send(blk, *@key_args[keycode])
295
+ else
296
+ $log.debug "BLOCJ " if $log.debug?
297
+ return blk.call object, *@key_args[keycode]
298
+ end
299
+ #0
260
300
  end
261
- #$log.debug "called process_key #{object}, kc: #{keycode}, args #{@key_args[keycode]}"
262
- if blk.is_a? Symbol
263
- return send(blk, *@key_args[keycode])
264
- else
265
- return blk.call object, *@key_args[keycode]
301
+ # view a file or array of strings
302
+ def view what, config={} # :yields: textview for further configuration
303
+ require 'rbcurse/extras/viewer'
304
+ RubyCurses::Viewer.view what, config
266
305
  end
267
- #0
268
- end
269
306
  end
270
307
 
271
308
  module EventHandler
@@ -273,10 +310,13 @@ module RubyCurses
273
310
  # bind an event to a block, optional args will also be passed when calling
274
311
  def bind event, *xargs, &blk
275
312
  #$log.debug "#{self} called EventHandler BIND #{event}, args:#{xargs} "
313
+ if @_events
314
+ raise ArgumentError, "#{self.class} does not support this event: #{event}. #{@_events} " if !@_events.include? event
315
+ else
316
+ $log.debug "BIND #{self.class} XXXXX no events defined in @_events. Please do so to avoid bugs and debugging. This will become a fatal error soon."
317
+ end
276
318
  @handler ||= {}
277
319
  @event_args ||= {}
278
- #@handler[event] = blk
279
- #@event_args[event] = xargs
280
320
  @handler[event] ||= []
281
321
  @handler[event] << blk
282
322
  @event_args[event] ||= []
@@ -290,19 +330,36 @@ module RubyCurses
290
330
  ##
291
331
  # Fire all bindings for given event
292
332
  # e.g. fire_handler :ENTER, self
293
- # currently object usually contains self which is perhaps a bit of a waste,
294
- # could contain an event object with source, and some relevant methods or values
333
+ # The first parameter passed to the calling block is either self, or some action event
334
+ # The second and beyond are any objects you passed when using `bind` or `command`.
335
+ # Exceptions are caught here itself, or else they prevent objects from updating, usually the error is
336
+ # in the block sent in by application, not our error.
337
+ # TODO: if an object throws a subclass of VetoException we should not catch it and throw it back for
338
+ # caller to catch and take care of, such as prevent LEAVE or update etc.
295
339
  def fire_handler event, object
296
- $log.debug " def fire_handler evt:#{event}, o: #{object.class}, hdnler:#{@handler}"
340
+ $log.debug "inside def fire_handler evt:#{event}, o: #{object.class}, hdnler:#{@handler}"
297
341
  if !@handler.nil?
298
- #blk = @handler[event]
342
+ if @_events
343
+ raise ArgumentError, "#{self.class} does not support this event: #{event}. #{@_events} " if !@_events.include? event
344
+ else
345
+ $log.debug "bIND #{self.class} XXXXX TEMPO no events defined in @_events "
346
+ end
299
347
  ablk = @handler[event]
300
348
  if !ablk.nil?
301
349
  aeve = @event_args[event]
302
350
  ablk.each_with_index do |blk, ix|
303
351
  #$log.debug "#{self} called EventHandler firehander #{@name}, #{event}, obj: #{object},args: #{aeve[ix]}"
304
352
  $log.debug "#{self} called EventHandler firehander #{@name}, #{event}"
305
- blk.call object, *aeve[ix]
353
+ begin
354
+ blk.call object, *aeve[ix]
355
+ rescue => ex
356
+ $log.error "======= Error ERROR in block event #{self}: #{name}, #{event}"
357
+ $log.error ex
358
+ $log.error(ex.backtrace.join("\n"))
359
+ #$error_message = "#{ex}" # changed 2010
360
+ $error_message.value = "#{ex}"
361
+ Ncurses.beep
362
+ end
306
363
  end
307
364
  end # if
308
365
  end # if
@@ -311,7 +368,9 @@ module RubyCurses
311
368
  # goes with dsl_property
312
369
  # Need to inform listeners - done 2010-02-25 23:09
313
370
  def fire_property_change text, oldvalue, newvalue
371
+ # should i return if oldvalue is nil ??? TODO XXX
314
372
  #$log.debug " FPC #{self}: #{text} #{oldvalue}, #{newvalue}"
373
+ return if oldvalue.nil? || @_object_created.nil? # added 2010-09-16 so if called by methods it is still effective
315
374
  if @pce.nil?
316
375
  @pce = PropertyChangeEvent.new(self, text, oldvalue, newvalue)
317
376
  else
@@ -326,8 +385,8 @@ module RubyCurses
326
385
  module ConfigSetup
327
386
  # private
328
387
  def variable_set var, val
329
- nvar = "@#{var}"
330
- send("#{var}", val) # 2009-01-08 01:30 BIG CHANGE calling methods too here.
388
+ #nvar = "@#{var}"
389
+ send("#{var}", val) #rescue send("#{var}=", val) # 2009-01-08 01:30 BIG CHANGE calling methods too here.
331
390
  #instance_variable_set(nvar, val) # we should not call this !!! bypassing
332
391
  end
333
392
  def configure(*val , &block)
@@ -361,7 +420,6 @@ module RubyCurses
361
420
  # NOTE: I may soon remove the config hash. I don't use it and its just making things heavy.
362
421
  # Unless someone convinces me otherwise.
363
422
  class Widget
364
- include DSL
365
423
  include EventHandler
366
424
  include ConfigSetup
367
425
  include RubyCurses::Utils
@@ -377,11 +435,12 @@ module RubyCurses
377
435
  dsl_accessor :focusable, :enabled # boolean
378
436
  dsl_property :row, :col # location of object
379
437
  dsl_property :color, :bgcolor # normal foreground and background
438
+ dsl_property :color_pair # instead of colors give just color_pair
380
439
  dsl_property :attr # attribute bold, normal, reverse
381
440
  dsl_accessor :name # name to refr to or recall object by_name
382
441
  attr_accessor :id #, :zorder
383
442
  attr_accessor :curpos # cursor position inside object - column, not row.
384
- attr_reader :config # COULD GET AXED SOON NOTE
443
+ attr_reader :config # can be used for popping user objects too
385
444
  attr_accessor :form # made accessor 2008-11-27 22:32 so menu can set
386
445
  attr_accessor :state # normal, selected, highlighted
387
446
  attr_reader :row_offset, :col_offset # where should the cursor be placed to start with
@@ -397,6 +456,7 @@ module RubyCurses
397
456
  ## 2010-01-05 13:27 create buffer conditionally, if enclosing component asks. Needs to be passed down
398
457
  ##+ to further children or editor components. Default false.
399
458
  attr_accessor :should_create_buffer # added 2010-01-05 13:16 BUFFERED, trying to create buffersonly where required.
459
+ attr_accessor :_object_created # 2010-09-16 12:12 to prevent needless property change firing when object being set
400
460
 
401
461
  ## I think parent_form was not a good idea since i can't add parent widget offsets
402
462
  ##+ thus we should use parent_comp and push up.
@@ -404,22 +464,40 @@ module RubyCurses
404
464
  # tired of getting the cursor wrong and guessing, i am now going to try to get absolute
405
465
  # coordinates - 2010-02-07 20:17 this should be updated by parent.
406
466
  attr_accessor :ext_col_offset, :ext_row_offset # 2010-02-07 20:16 to get abs position for cursor
407
- #attr_accessor :manages_cursor # does this widget manage cursor, or should form handle it 2010-02-07 20:54
408
467
  attr_accessor :rows_panned # moved from form, how many rows scrolled.panned 2010-02-11 15:26
409
468
  attr_accessor :cols_panned # moved from form, how many cols scrolled.panned 2010-02-11 15:26
410
469
 
470
+ # sometimes inside a container there's no way of knowing if an individual comp is in focus
471
+ # other than the explicitly set it and inquire . 2010-09-02 14:47 @since 1.1.5
472
+ attr_accessor :focussed # is this widget in focus, so they may paint differently
473
+
411
474
  def initialize form, aconfig={}, &block
412
475
  @form = form
413
- @bgcolor ||= "black" # 0
414
- @row_offset = @col_offset = 0
476
+ @row_offset ||= 0
477
+ @col_offset ||= 0
415
478
  @ext_row_offset = @ext_col_offset = 0 # 2010-02-07 20:18
416
479
  @state = :NORMAL
417
- @color ||= "white" # $datacolor
418
480
  @attr = nil
419
- @handler = {}
481
+
482
+ @handler = nil # we can avoid firing if nil
420
483
  @event_args = {}
421
484
  config_setup aconfig # @config.each_pair { |k,v| variable_set(k,v) }
422
- instance_eval &block if block_given?
485
+ #instance_eval &block if block_given?
486
+ if block_given?
487
+ if block.arity > 0
488
+ yield self
489
+ else
490
+ self.instance_eval(&block)
491
+ end
492
+ end
493
+ # 2010-09-20 13:12 moved down, so it does not create problems with other who want to set their
494
+ # own default
495
+ @bgcolor ||= "black" # 0
496
+ @color ||= "white" # $datacolor
497
+ # These are standard events for most widgets which will be fired by
498
+ # Form. In the case of CHANGED, form fires if it's editable property is set, so
499
+ # it does not apply to all widgets.
500
+ @_events = [:ENTER, :LEAVE, :CHANGED, :PROPERTY_CHANGE]
423
501
  # @id = form.add_widget(self) if !form.nil? and form.respond_to? :add_widget
424
502
  set_form(form) unless form.nil?
425
503
  end
@@ -456,11 +534,17 @@ module RubyCurses
456
534
 
457
535
  ## got left out by mistake 2008-11-26 20:20
458
536
  def on_enter
459
- fire_handler :ENTER, self
537
+ @focussed = true
538
+ if @handler && @handler.has_key?(:ENTER)
539
+ fire_handler :ENTER, self
540
+ end
460
541
  end
461
542
  ## got left out by mistake 2008-11-26 20:20
462
543
  def on_leave
463
- fire_handler :LEAVE, self
544
+ @focussed = false
545
+ if @handler && @handler.has_key?(:LEAVE)
546
+ fire_handler :LEAVE, self
547
+ end
464
548
  end
465
549
  ##
466
550
  # @return row and col of a widget where painting data actually starts
@@ -487,11 +571,12 @@ module RubyCurses
487
571
  $log.debug("widget repaint : r:#{r} c:#{c} col:#{@color}" )
488
572
  value = getvalue_for_paint
489
573
  len = @display_length || value.length
490
- if @bgcolor.is_a? String and @color.is_a? String
491
- acolor = ColorMap.get_color(@color, @bgcolor)
492
- else
493
- acolor = $datacolor
494
- end
574
+ acolor = @color_pair || get_color($datacolor, @color, @bgcolor)
575
+ #if @bgcolor.is_a? String and @color.is_a? String
576
+ #acolor = ColorMap.get_color(@color, @bgcolor)
577
+ #else
578
+ #acolor = $datacolor
579
+ #end
495
580
  @graphic.printstring r, c, "%-*s" % [len, value], acolor, @attr
496
581
  # next line should be in same color but only have @att so we can change att is nec
497
582
  #@form.window.mvchgat(y=r, x=c, max=len, Ncurses::A_NORMAL, @bgcolor, nil)
@@ -569,6 +654,7 @@ module RubyCurses
569
654
  def remove
570
655
  @form.remove_widget(self)
571
656
  end
657
+ # is this required can we remove
572
658
  def move row, col
573
659
  @row = row
574
660
  @col = col
@@ -582,14 +668,6 @@ module RubyCurses
582
668
  @form.select_field @id
583
669
  end
584
670
  end
585
- def get_color default=$datacolor, _color=@color, _bgcolor=@bgcolor
586
- if _bgcolor.is_a? String and _color.is_a? String
587
- acolor = ColorMap.get_color(_color, _bgcolor)
588
- else
589
- acolor = default
590
- end
591
- return acolor
592
- end
593
671
  ##
594
672
  # bind an action to a key, required if you create a button which has a hotkey
595
673
  # or a field to be focussed on a key, or any other user defined action based on key
@@ -648,6 +726,7 @@ module RubyCurses
648
726
  def handle_key(ch)
649
727
  ret = process_key ch, self
650
728
  return :UNHANDLED if ret == :UNHANDLED
729
+ 0
651
730
  end
652
731
  # @since 0.1.3
653
732
  def get_preferred_size
@@ -738,7 +817,7 @@ module RubyCurses
738
817
  ##+ so i put current window here.
739
818
  if screen == nil
740
819
  #$log.debug " XXX calling graphic.wrefresh 2010-01-03 12:27 (parent_buffer was nil) "
741
- $log.debug " XXX 2010-01-03 20:47 now copying pad #{@graphic} onto form.window"
820
+ #$log.debug " XXX 2010-01-03 20:47 now copying pad #{@graphic} onto form.window"
742
821
  ret = @graphic.wrefresh
743
822
  ## 2010-01-03 20:45 rather than writing to phys screen, i write to forms window
744
823
  ##+ so later updates to that window do not overwrite this widget.
@@ -793,17 +872,18 @@ module RubyCurses
793
872
  # @since 0.1.3
794
873
  #
795
874
  def width(*val)
796
- #$log.debug " inside XXX width() #{val[0]}"
875
+ #$log.debug " inside XXX width() #{val}"
797
876
  if val.empty?
798
877
  return @width
799
878
  else
800
879
  #$log.debug " inside XXX width()"
801
880
  oldvalue = @width || 0 # is this default okay, else later nil cries
802
- @width = val.size == 1 ? val[0] : val
881
+ #@width = val.size == 1 ? val[0] : val
882
+ @width = val[0]
803
883
  newvalue = @width
804
884
  @config["width"]=@width
805
885
  if oldvalue != newvalue
806
- fire_property_change("width", oldvalue, newvalue)
886
+ fire_property_change(:width, oldvalue, newvalue)
807
887
  repaint_all(true) # added 2010-01-08 18:51 so widgets can redraw everything.
808
888
  end
809
889
  if is_double_buffered? and newvalue != oldvalue
@@ -832,9 +912,9 @@ module RubyCurses
832
912
  oldvalue = @height || 0 # is this default okay, else later nil cries
833
913
  @height = val.size == 1 ? val[0] : val
834
914
  newvalue = @height
835
- @config["height"]=@height
915
+ @config[:height]=@height
836
916
  if oldvalue != newvalue
837
- fire_property_change("height", oldvalue, newvalue)
917
+ fire_property_change(:height, oldvalue, newvalue)
838
918
  $log.debug " widget #{@name} setting repaint_all to true"
839
919
  @repaint_all=true
840
920
  end
@@ -988,6 +1068,10 @@ module RubyCurses
988
1068
  #
989
1069
  end
990
1070
  end
1071
+ def event_list
1072
+ return @@events if defined? @@events
1073
+ nil
1074
+ end
991
1075
  ##
992
1076
  ## ADD HERE WIDGET
993
1077
  end
@@ -1040,11 +1124,11 @@ module RubyCurses
1040
1124
  @focusable = true
1041
1125
  @navigation_policy ||= :CYCLICAL
1042
1126
  instance_eval &block if block_given?
1043
- @firsttime = true # internal, don't touch
1127
+ @_firsttime = true # internal, don't touch
1044
1128
  ## I need some counter so a widget knows it has been panned and can send a correct
1045
1129
  ##+ cursor coordinate to system.
1046
1130
  @rows_panned = @cols_panned = 0 # how many rows were panned, typically at a higher level
1047
- @firsttime = true; # added on 2010-01-02 19:21 to prevent scrolling crash !
1131
+ @_firsttime = true; # added on 2010-01-02 19:21 to prevent scrolling crash !
1048
1132
  @name ||= ""
1049
1133
  $kill_ring ||= [] # 2010-03-09 22:42 so textarea and others can copy and paste emacs EMACS
1050
1134
  $kill_ring_pointer = 0 # needs to be incremented with each append, moved with yank-pop
@@ -1053,6 +1137,11 @@ module RubyCurses
1053
1137
 
1054
1138
  #@allow_alt_digits = true ; # capture Alt-1-9 as digit_args. Set to false if you wish to map
1055
1139
  # Alt-1-9 to buttons of tabs
1140
+ $last_key = 0 # last key pressed @since 1.1.5 (not used yet)
1141
+ $current_key = 0 # curr key pressed @since 1.1.5 (so some containers can behave based on whether
1142
+ # user tabbed in, or backtabbed in (rmultisplit)
1143
+ $error_message ||= Variable.new ""
1144
+ $key_map ||= :vim ## should this come here or in App or ??? 2010-09-20 23:50
1056
1145
  end
1057
1146
  ##
1058
1147
  # set this menubar as the form's menu bar.
@@ -1114,28 +1203,15 @@ module RubyCurses
1114
1203
  @widgets.each do |f|
1115
1204
  next if f.visible == false # added 2008-12-09 12:17
1116
1205
  f.repaint
1117
- # added 2009-10-29 20:11 for double buffered widgets
1118
- # this should only happen if painting actually happened
1119
- #$log.debug " #{self} form repaint parent_buffer (#{@parent_buffer}) if #{f.is_double_buffered?} : #{f.name} "
1120
- pb = @parent_buffer #|| @window
1121
- # is next line used 2010-02-05 00:04 its wiping off top in scrollpane in tabbedpane
1122
- # RFED16 - the next line should never execute now, since no outer object is buffered
1123
- #+ only those within containers are.
1124
- # Drat - this line is happeing since components inside a TP are double_buffered
1125
- #x f.buffer_to_screen(pb) if f.is_double_buffered?
1126
- end
1127
- @window.clear_error # suddenly throwing up on a small pad 2010-03-02 15:22 TPNEW
1128
- @window.print_status_message $status_message unless $status_message.nil?
1129
- @window.print_error_message $error_message unless $error_message.nil?
1130
- $error_message = $status_message = nil
1206
+ f._object_created = true # added 2010-09-16 13:02 now prop handlers can be fired
1207
+ end
1131
1208
  # this can bomb if someone sets row. We need a better way!
1132
- if @row == -1 and @firsttime == true
1209
+ if @row == -1 and @_firsttime == true
1133
1210
  #set_field_cursor 0
1134
1211
  # this part caused an endless loop on 2010-01-02 19:20 when scrollpane scrolled up
1135
- $log.debug "form repaint calling select field 0 SHOULD HAPPEN FIRST TIME ONLY"
1136
- #select_field 0
1137
- req_first_field
1138
- @firsttime = false
1212
+ #$log.debug "form repaint calling select field 0 SHOULD HAPPEN FIRST TIME ONLY"
1213
+ select_first_field
1214
+ @_firsttime = false
1139
1215
  end
1140
1216
  setpos
1141
1217
  # XXX this creates a problem if window is a pad
@@ -1144,21 +1220,7 @@ module RubyCurses
1144
1220
  if @window.window_type == :WINDOW
1145
1221
  $log.debug " formrepaint #{@name} calling window.wrefresh #{@window} "
1146
1222
  @window.wrefresh
1147
- else
1148
- # UGLY HACK TO MAKE TABBEDPANES WORK !!
1149
- # If the form is based on a Pad, then it would come here to write the Pad onto the parent_buffer
1150
- # However, I've obviated the need to handle anything here by adding a display_form after handle_key
1151
- # in TP.
1152
- #x if @parent_buffer!=nil
1153
- #x $log.debug " formrep coming to set backing window part #{@window} , type:#{@window.window_type}, #{@parent_buffer}, #{@parent_buffer.window_type} "
1154
- # XXX RFED19 do we need at all 2010-02-19 15:26
1155
- # this is required so that each key stroke registers on tabbedpane
1156
- # for this to work both have to be pads
1157
- #x @window.set_backing_window(@parent_buffer)
1158
- #x @window.copy_pad_to_win
1159
- #x @window.wrefresh #since the pads are writing onto window directly, i don't think we need this
1160
- #x $log.debug " DO I NEED TO DO SOMETHING HERE FOR TABBEDPANES now ? WARN ?? YES, else keystrokes won't be updated "
1161
- #x end
1223
+ Ncurses::Panel.update_panels ## added 2010-11-05 00:30 to see if clears the stdscr problems
1162
1224
  end
1163
1225
  end
1164
1226
  ##
@@ -1174,24 +1236,63 @@ module RubyCurses
1174
1236
  return if r<0 or c<0 # added 2010-01-02 18:49 stack too deep coming if goes above screen
1175
1237
  @window.wmove r,c
1176
1238
  end
1239
+ # @return [Widget, nil] current field, nil if no focusable field
1177
1240
  def get_current_field
1178
1241
  select_next_field if @active_index == -1
1179
1242
  return nil if @active_index.nil? # for forms that have no focusable field 2009-01-08 12:22
1180
1243
  @widgets[@active_index]
1181
1244
  end
1182
- def req_first_field
1183
- @active_index = -1 # FIXME HACK
1184
- select_next_field
1245
+ # take focus to first focussable field
1246
+ # we shoud not send to select_next. have a separate method to avoid bugs.
1247
+ # but check current_field, in case called from anotehr field TODO FIXME
1248
+ def select_first_field
1249
+ # this results in on_leave of last field being executed when form starts.
1250
+ #@active_index = -1 # FIXME HACK
1251
+ #select_next_field
1252
+ ix = index_of_first_focusable_field()
1253
+ return unless ix # no focussable field
1254
+
1255
+ # if the user is on a field other than current then fire on_leave
1256
+ if @active_index.nil? || @active_index < 0
1257
+ elsif @active_index != ix
1258
+ f = @widgets[@active_index]
1259
+ begin
1260
+ #$log.debug " XXX select first field, calling on_leave of #{f} #{@active_index} "
1261
+ on_leave f
1262
+ rescue => err
1263
+ $log.error " Caught EXCEPTION req_first_field on_leave #{err}"
1264
+ Ncurses.beep
1265
+ #$error_message = "#{err}"
1266
+ $error_message.value = "#{err}"
1267
+ return
1268
+ end
1269
+ end
1270
+ select_field ix
1185
1271
  end
1186
- def req_last_field
1272
+ # please do not use req_ i will deprecate it soon.
1273
+ alias :req_first_field :select_first_field
1274
+ # return the offset of first field that takes focus
1275
+ def index_of_first_focusable_field
1276
+ @widgets.each_with_index do |f, i|
1277
+ if focusable?(f)
1278
+ #select_field i
1279
+ return i
1280
+ end
1281
+ end
1282
+ nil
1283
+ end
1284
+ # take focus to last field on form
1285
+ def select_last_field
1187
1286
  @active_index = nil
1188
1287
  select_prev_field
1189
1288
  end
1289
+ # please do not use req_ i will deprecate it soon.
1290
+ alias :req_last_field :select_last_field
1190
1291
  ## do not override
1191
1292
  # form's trigger, fired when any widget loses focus
1192
1293
  # This wont get called in editor components in tables, since they are formless XXX
1193
1294
  def on_leave f
1194
- return if f.nil?
1295
+ return if f.nil? || !f.focusable # added focusable, else label was firing
1195
1296
  f.state = :NORMAL
1196
1297
  # on leaving update text_variable if defined. Should happen on modified only
1197
1298
  # should this not be f.text_var ... f.buffer ? XXX 2008-11-25 18:58
@@ -1205,7 +1306,7 @@ module RubyCurses
1205
1306
  end
1206
1307
  end
1207
1308
  def on_enter f
1208
- return if f.nil?
1309
+ return if f.nil? || !f.focusable # added focusable, else label was firing 2010-09
1209
1310
  f.state = :HIGHLIGHTED
1210
1311
  f.modified false
1211
1312
  #f.set_modified false
@@ -1246,9 +1347,10 @@ module RubyCurses
1246
1347
  begin
1247
1348
  on_leave f
1248
1349
  rescue => err
1249
- $log.debug "form: validate_field caught EXCEPTION #{err}"
1250
- $log.debug(err.backtrace.join("\n"))
1251
- $error_message = "#{err}"
1350
+ $log.error "form: validate_field caught EXCEPTION #{err}"
1351
+ $log.error(err.backtrace.join("\n"))
1352
+ # $error_message = "#{err}" # changed 2010
1353
+ $error_message.value = "#{err}"
1252
1354
  Ncurses.beep
1253
1355
  return -1
1254
1356
  end
@@ -1257,19 +1359,21 @@ module RubyCurses
1257
1359
  # put focus on next field
1258
1360
  # will cycle by default, unless navigation policy not :CYCLICAL
1259
1361
  # in which case returns :NO_NEXT_FIELD.
1362
+ # FIXME: in the beginning it comes in as -1 and does an on_leave of last field
1260
1363
  def select_next_field
1261
1364
  return if @widgets.nil? or @widgets.empty?
1262
1365
  $log.debug "insdie sele nxt field : #{@active_index} WL:#{@widgets.length}"
1263
- if @active_index.nil?
1366
+ if @active_index.nil? || @active_index == -1 # needs to be tested out A LOT
1264
1367
  @active_index = -1
1265
1368
  else
1266
1369
  f = @widgets[@active_index]
1267
1370
  begin
1268
1371
  on_leave f
1269
1372
  rescue => err
1270
- $log.debug "select_next_field: caught EXCEPTION #{err}"
1271
- $log.debug(err.backtrace.join("\n"))
1272
- $error_message = "#{err}"
1373
+ $log.error "select_next_field: caught EXCEPTION #{err}"
1374
+ $log.error(err.backtrace.join("\n"))
1375
+ # $error_message = "#{err}" # changed 2010
1376
+ $error_message.value = "#{err}"
1273
1377
  Ncurses.beep
1274
1378
  return
1275
1379
  end
@@ -1277,7 +1381,7 @@ module RubyCurses
1277
1381
  index = @active_index + 1
1278
1382
  index.upto(@widgets.length-1) do |i|
1279
1383
  f = @widgets[i]
1280
- $log.debug "insdie sele nxt field : i #{i} #{index} WL:#{@widgets.length}, field #{f}"
1384
+ #$log.debug "insdie sele nxt field : i #{i} #{index} WL:#{@widgets.length}, field #{f}"
1281
1385
  if focusable?(f)
1282
1386
  select_field i
1283
1387
  return
@@ -1305,6 +1409,8 @@ module RubyCurses
1305
1409
  # put focus on previous field
1306
1410
  # will cycle by default, unless navigation policy not :CYCLICAL
1307
1411
  # in which case returns :NO_PREV_FIELD.
1412
+ # @return [nil, :NO_PREV_FIELD] nil if cyclical and it finds a field
1413
+ # if not cyclical, and no more fields then :NO_PREV_FIELD
1308
1414
  def select_prev_field
1309
1415
  return if @widgets.nil? or @widgets.empty?
1310
1416
  #$log.debug "insdie sele prev field : #{@active_index} WL:#{@widgets.length}"
@@ -1315,8 +1421,10 @@ module RubyCurses
1315
1421
  begin
1316
1422
  on_leave f
1317
1423
  rescue => err
1318
- $log.debug " cauGHT EXCEPTION #{err}"
1424
+ $log.error " Caught EXCEPTION #{err}"
1319
1425
  Ncurses.beep
1426
+ # $error_message = "#{err}" # changed 2010
1427
+ $error_message.value = "#{err}"
1320
1428
  return
1321
1429
  end
1322
1430
  end
@@ -1379,27 +1487,6 @@ module RubyCurses
1379
1487
  end
1380
1488
  end
1381
1489
 
1382
- ## added 2009-12-29 15:46 BUFFERED
1383
- # Set forms row and col, so that the cursor can be displayed at that point.
1384
- # Widgets should call this rather than touch row and col
1385
- # directly. This should percolate the row and col
1386
- # upwards to parent forms, after comparing to prevent
1387
- # infinite recursion.
1388
- # This is being done for embedded objects so that the cursor
1389
- # can be maintained correctly.
1390
- def OLDsetrowcol r, c
1391
- @row = r unless r.nil?
1392
- @col = c unless c.nil?
1393
- r += @add_rows unless r.nil? # 2010-01-26 20:31
1394
- c += @add_cols unless c.nil? # 2010-01-26 20:31
1395
- $log.debug " addcols #{@add_cols} addrow #{@add_rows} : #{self} "
1396
- if !@parent_form.nil? and @parent_form != self
1397
- $log.debug " (#{@name}) calling parents setrowcol #{r}, #{c} : pare: #{@parent_form}; self: #{self}, #{self.class} "
1398
- r += @parent_form.window.top unless r.nil?
1399
- c += @parent_form.window.left unless c.nil?
1400
- @parent_form.setrowcol r, c
1401
- end
1402
- end
1403
1490
  ## Form
1404
1491
  # New attempt at setting cursor using absolute coordinates
1405
1492
  # Also, trying NOT to go up. let this pad or window print cursor.
@@ -1417,32 +1504,6 @@ module RubyCurses
1417
1504
  end
1418
1505
  end
1419
1506
  ##
1420
- # bind an action to a key, required if you create a button which has a hotkey
1421
- # or a field to be focussed on a key, or any other user defined action based on key
1422
- # e.g. bind_key ?\C-x, object, block
1423
- # 1.9 if string passed then getbyte so user does not need to change much and
1424
- # less chance of error 2009-10-04 16:08
1425
- def OLDbind_key keycode, *args, &blk
1426
- @key_handler ||= {}
1427
- case keycode
1428
- when String
1429
- $log.debug "FORM String called bind_key BIND #{keycode} #{keycode_tos(keycode)} "
1430
- keycode = keycode.getbyte(0) #if keycode.class==String ## 1.9 2009-10-05 19:40
1431
- @key_handler[keycode] = blk
1432
- when Array
1433
- # for starters lets try with 2 keys only
1434
- a0 = keycode[0]
1435
- a0 = keycode[0].getbyte(0) if keycode[0].class == String
1436
- a1 = keycode[1]
1437
- a1 = keycode[1].getbyte(0) if keycode[1].class == String
1438
- @key_handler[a0] ||= OrderedHash.new
1439
- @key_handler[a0][a1] = blk
1440
- else
1441
- @key_handler[keycode] = blk
1442
- end
1443
- @key_args ||= {}
1444
- @key_args[keycode] = args
1445
- end
1446
1507
 
1447
1508
  # e.g. process_key ch, self
1448
1509
  # returns UNHANDLED if no block for it
@@ -1452,29 +1513,6 @@ module RubyCurses
1452
1513
  def process_key keycode, object
1453
1514
  return _process_key keycode, object, @window
1454
1515
  end
1455
- #return :UNHANDLED if @key_handler.nil?
1456
- #blk = @key_handler[keycode]
1457
- #return :UNHANDLED if blk.nil?
1458
- #if blk.is_a? OrderedHash
1459
- ## Please note that this does not wait too long, you have to press next key fast
1460
- ## since i have set halfdelay in ncurses.rb, test this with getchar to get more keys TODO
1461
- #ch = @window.getch
1462
- #if ch < 0 || ch > 255
1463
- ##next
1464
- #return nil
1465
- #end
1466
- #$log.debug " process_key: got #{keycode} , #{ch} "
1467
- #yn = ch.chr
1468
- #blk1 = blk[ch]
1469
- #return nil if blk1.nil?
1470
- #$log.debug " process_key: found block for #{keycode} , #{ch} "
1471
- #blk = blk1
1472
- #end
1473
- #$log.debug "called process_key #{object}, kc: #{keycode}, args #{@key_args[keycode]}"
1474
- ## return blk.call object, *@key_args[keycode]
1475
- #blk.call object, *@key_args[keycode]
1476
- #0
1477
- #end
1478
1516
  # Defines how user can give numeric args to a command even in edit mode
1479
1517
  # User either presses universal_argument (C-u) which generates a series of 4 16 64.
1480
1518
  # Or he presses C-u and then types some numbers. Followed by the action.
@@ -1561,9 +1599,19 @@ module RubyCurses
1561
1599
  end
1562
1600
  end
1563
1601
 
1602
+ $current_key = ch
1564
1603
  case ch
1565
1604
  when -1
1566
1605
  return
1606
+ when Ncurses::KEY_RESIZE # SIGWINCH
1607
+ lines = Ncurses.LINES
1608
+ cols = Ncurses.COLS
1609
+ x = Ncurses.stdscr.getmaxy
1610
+ y = Ncurses.stdscr.getmaxx
1611
+ $log.debug " form RESIZE HK #{ch} #{self}, #{@name}, #{ch} "
1612
+ alert "SIGWINCH WE NEED TO RECALC AND REPAINT resize #{lines}, #{cols}: #{x}, #{y} "
1613
+ Ncurses.endwin
1614
+ @window.wrefresh
1567
1615
  else
1568
1616
  keycode = keycode_tos(ch)
1569
1617
  $log.debug " form HK #{ch} #{self}, #{@name}, #{keycode} "
@@ -1573,11 +1621,11 @@ module RubyCurses
1573
1621
  # some widgets like textarea and list handle up and down
1574
1622
  if handled == :UNHANDLED or handled == -1 or field.nil?
1575
1623
  case ch
1576
- when 9, ?\M-\C-i.getbyte(0) # tab and M-tab in case widget eats tab (such as Table)
1624
+ when KEY_TAB, ?\M-\C-i.getbyte(0) # tab and M-tab in case widget eats tab (such as Table)
1577
1625
  ret = select_next_field
1578
1626
  return ret if ret == :NO_NEXT_FIELD
1579
1627
  # alt-shift-tab or backtab (in case Table eats backtab)
1580
- when 353, 481 ## backtab added 2008-12-14 18:41
1628
+ when KEY_BTAB, 481 ## backtab added 2008-12-14 18:41
1581
1629
  ret = select_prev_field
1582
1630
  return ret if ret == :NO_PREV_FIELD
1583
1631
  when KEY_UP
@@ -1599,6 +1647,7 @@ module RubyCurses
1599
1647
  #when ?\M-K.getbyte(0)
1600
1648
  #field.height -= 1
1601
1649
  else
1650
+ #$log.debug "XXX before calling process_key in form #{ch} " if $log.debug?
1602
1651
  ret = process_key ch, self
1603
1652
  $log.debug " process_key #{ch} got ret #{ret} in #{self} "
1604
1653
  return :UNHANDLED if ret == :UNHANDLED
@@ -1607,6 +1656,7 @@ module RubyCurses
1607
1656
  end
1608
1657
  $log.debug " form before repaint #{self} , #{@name}, ret #{ret}"
1609
1658
  repaint
1659
+ $last_key = ch
1610
1660
  #return handled # TRYNG 2010-03-01 23:30 since TP returns NO_NEXT_FIELD sometimes
1611
1661
  #$multiplier = 0
1612
1662
  end
@@ -1641,6 +1691,49 @@ module RubyCurses
1641
1691
  # 2010-02-07 14:50 to aid in debugging and comparing log files.
1642
1692
  def to_s; @name || self; end
1643
1693
 
1694
+ # NOTE: very experimental, use at risk, can change location or be deprec
1695
+ # place given widget below given one, or last added one
1696
+ # Does not check for availability or overlap
1697
+ def place_below me, other=nil
1698
+ w = widgets
1699
+ if other.nil?
1700
+ other = w[-1]
1701
+ # if user calls this after placing this field
1702
+ other = w[-2] if other == me
1703
+ end
1704
+ if other.height.nil? || other.height == 0
1705
+ h = 1
1706
+ else
1707
+ h = other.height
1708
+ end
1709
+ me.row = other.row + h
1710
+ me.col = other.col
1711
+ me
1712
+ end
1713
+ # NOTE: very experimental, use at risk, can change location or be deprec
1714
+ # return location to place next widget (below)
1715
+ # Does not check for availability or overlap
1716
+ def next_position
1717
+ w = widgets.last
1718
+ if w.height.nil? || w.height == 0
1719
+ h = 1
1720
+ else
1721
+ h = w.height
1722
+ end
1723
+ row = w.row + h
1724
+ col = w.col
1725
+ return row, col
1726
+ end
1727
+ ## so if we come back from ncurses cooked mode we can repaint.
1728
+ # not required, we have to stop and restart ncurses.
1729
+ #def reset_all
1730
+ #$log.debug " form reset:#{self}, #{@name} , r #{@row} c #{@col} "
1731
+ #@widgets.each do |f|
1732
+ #next if f.visible == false # added 2008-12-09 12:17
1733
+ #f.repaint_all()
1734
+ #end
1735
+ #end
1736
+
1644
1737
  ## ADD HERE FORM
1645
1738
  end
1646
1739
  ## Created and sent to all listeners whenever a property is changed
@@ -1666,8 +1759,13 @@ module RubyCurses
1666
1759
 
1667
1760
  ##
1668
1761
  # Text edit field
1669
- # To get value use getvalue()
1762
+ # NOTE: To get value use getvalue()
1670
1763
  # TODO - test text_variable
1764
+ # TODO: some methods should return self, so chaining can be done. Not sure if the return value of the
1765
+ # fire_handler is being checked.
1766
+ # NOTE: i have just added repain_required check in Field before repaint
1767
+ # this may mean in some places field does not paint. repaint_require will have to be set
1768
+ # to true in those cases. this was since field was overriding a popup window that was not modal.
1671
1769
  #
1672
1770
  class Field < Widget
1673
1771
  dsl_accessor :maxlen # maximum length allowed into field
@@ -1699,24 +1797,37 @@ module RubyCurses
1699
1797
  @form = form
1700
1798
  @buffer = String.new
1701
1799
  #@type=config.fetch("type", :varchar)
1702
- @display_length = config.fetch("display_length", 20)
1703
- @maxlen=config.fetch("maxlen", @display_length)
1704
- @row = config.fetch("row", 0)
1705
- @col = config.fetch("col", 0)
1706
- @bgcolor = config.fetch("bgcolor", $def_bg_color)
1707
- @color = config.fetch("color", $def_fg_color)
1708
- @name = config.fetch("name", nil)
1709
- @editable = config.fetch("editable", true)
1710
- @focusable = config.fetch("focusable", true)
1711
- @handler = {}
1800
+ # 2010-10-19 12:26 slight change, hopefully won't bomb
1801
+ @display_length = 20
1802
+ @maxlen = @display_length
1803
+ @row = 0
1804
+ @col = 0
1805
+ @bgcolor = $def_bg_color
1806
+ @color = $def_fg_color
1807
+ #@name = config.fetch("name", nil)
1808
+ @editable = true
1809
+ @focusable = true
1810
+ # are these redundant ?
1811
+ #@display_length = config.fetch("display_length", 20)
1812
+ #@maxlen=config.fetch("maxlen", @display_length)
1813
+ #@row = config.fetch("row", 0)
1814
+ #@col = config.fetch("col", 0)
1815
+ #@bgcolor = config.fetch("bgcolor", $def_bg_color)
1816
+ #@color = config.fetch("color", $def_fg_color)
1817
+ #@name = config.fetch("name", nil)
1818
+ #@editable = config.fetch("editable", true)
1819
+ #@focusable = config.fetch("focusable", true)
1712
1820
  @event_args = {} # arguments passed at time of binding, to use when firing event
1821
+ map_keys
1713
1822
  init_vars
1714
1823
  super
1824
+ @_events.push(:CHANGE)
1715
1825
  end
1716
1826
  def init_vars
1717
1827
  @pcol = 0 # needed for horiz scrolling
1718
1828
  @curpos = 0 # current cursor position in buffer
1719
1829
  @modified = false
1830
+ @repaint_required = true
1720
1831
  end
1721
1832
  def text_variable tv
1722
1833
  @text_variable = tv
@@ -1725,18 +1836,25 @@ module RubyCurses
1725
1836
  ##
1726
1837
  # define a datatype, currently only influences chars allowed
1727
1838
  # integer and float. what about allowing a minus sign? XXX
1839
+ # 2010-09-10 20:59 changed string to symbol
1728
1840
  def type dtype
1841
+ return if !@chars_allowed.nil?
1729
1842
  case dtype.to_s.downcase
1730
- when 'integer'
1731
- @chars_allowed = /\d/ if @chars_allowed.nil?
1732
- when 'numeric'
1733
- @chars_allowed = /[\d\.]/ if @chars_allowed.nil?
1734
- when 'alpha'
1735
- @chars_allowed = /[a-zA-Z]/ if @chars_allowed.nil?
1736
- when 'alnum'
1737
- @chars_allowed = /[a-zA-Z0-9]/ if @chars_allowed.nil?
1738
- end
1739
- end
1843
+ when :integer
1844
+ @chars_allowed = /\d/
1845
+ when :numeric, :float
1846
+ @chars_allowed = /[\d\.]/
1847
+ when :alpha
1848
+ @chars_allowed = /[a-zA-Z]/
1849
+ when :alnum
1850
+ @chars_allowed = /[a-zA-Z0-9]/
1851
+ end
1852
+ end
1853
+ # add a char to field, and validate
1854
+ # NOTE: this should return self for chaining operations and throw an exception
1855
+ # if disabled or exceeding size
1856
+ # @param [char] a character to add
1857
+ # @return [Fixnum] 0 if okay, -1 if not editable or exceeding length
1740
1858
  def putch char
1741
1859
  return -1 if !@editable
1742
1860
  return -1 if !@overwrite_mode and @buffer.length >= @maxlen
@@ -1744,15 +1862,23 @@ module RubyCurses
1744
1862
  return if char.match(@chars_allowed).nil?
1745
1863
  end
1746
1864
  # added insert or overwrite mode 2010-03-17 20:11
1865
+ oldchar = nil
1747
1866
  if @overwrite_mode
1867
+ oldchar = @buffer[@curpos]
1748
1868
  @buffer[@curpos] = char
1749
1869
  else
1750
1870
  @buffer.insert(@curpos, char)
1751
1871
  end
1872
+ oldcurpos = @curpos
1752
1873
  @curpos += 1 if @curpos < @maxlen
1753
1874
  @modified = true
1754
- $log.debug " FIELD FIRING CHANGE: #{char} at new #{@curpos}: bl:#{@buffer.length} buff:[#{@buffer}]"
1755
- fire_handler :CHANGE, self # 2008-12-09 14:51
1875
+ #$log.debug " FIELD FIRING CHANGE: #{char} at new #{@curpos}: bl:#{@buffer.length} buff:[#{@buffer}]"
1876
+ # i have no way of knowing what change happened and what char was added deleted or changed
1877
+ #fire_handler :CHANGE, self # 2008-12-09 14:51
1878
+ if @overwrite_mode
1879
+ fire_handler :CHANGE, InputDataEvent.new(oldcurpos,@curpos, self, :DELETE, 0, oldchar) # 2010-09-11 12:43
1880
+ end
1881
+ fire_handler :CHANGE, InputDataEvent.new(oldcurpos,@curpos, self, :INSERT, 0, char) # 2010-09-11 12:43
1756
1882
  0
1757
1883
  end
1758
1884
 
@@ -1769,24 +1895,31 @@ module RubyCurses
1769
1895
  end
1770
1896
  end
1771
1897
  set_modified
1898
+ return 0 # 2010-09-11 12:59 else would always return -1
1772
1899
  end
1773
1900
  end
1774
1901
  return -1
1775
1902
  end
1776
1903
  def delete_at index=@curpos
1777
1904
  return -1 if !@editable
1778
- @buffer.slice!(index,1)
1779
- $log.debug " delete at #{index}: #{@buffer.length}: #{@buffer}"
1905
+ char = @buffer.slice!(index,1)
1906
+ #$log.debug " delete at #{index}: #{@buffer.length}: #{@buffer}"
1780
1907
  @modified = true
1781
- fire_handler :CHANGE, self # 2008-12-09 14:51
1908
+ #fire_handler :CHANGE, self # 2008-12-09 14:51
1909
+ fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos, self, :DELETE, 0, char) # 2010-09-11 13:01
1782
1910
  end
1783
1911
  ##
1784
- # should this do a dup ??
1912
+ # should this do a dup ?? YES
1785
1913
  def set_buffer value
1914
+ @repaint_required = true
1786
1915
  @datatype = value.class
1787
1916
  #$log.debug " FIELD DATA #{@datatype}"
1788
- @buffer = value.to_s
1917
+ @delete_buffer = @buffer.dup
1918
+ @buffer = value.to_s.dup
1789
1919
  @curpos = 0
1920
+ # XXX hope @delete_buffer is not overwritten
1921
+ fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos, self, :DELETE, 0, @delete_buffer) # 2010-09-11 13:01
1922
+ fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos, self, :INSERT, 0, @buffer) # 2010-09-11 13:01
1790
1923
  end
1791
1924
  # converts back into original type
1792
1925
  # changed to convert on 2009-01-06 23:39
@@ -1804,11 +1937,22 @@ module RubyCurses
1804
1937
  end
1805
1938
  end
1806
1939
 
1940
+ # create a label linked to this field
1941
+ # Typically one passes a Label, but now we can pass just a String, a label
1942
+ # is created
1943
+ # @param [Label, String] label object to be associated with this field
1807
1944
  def set_label label
1945
+ # added case for user just using a string
1946
+ case label
1947
+ when String
1948
+ # what if no form at this point
1949
+ label = Label.new @form, {:text => label}
1950
+ end
1808
1951
  @label = label
1809
1952
  label.row @row if label.row == -1
1810
1953
  label.col @col-(label.name.length+1) if label.col == -1
1811
1954
  label.label_for(self)
1955
+ label
1812
1956
  end
1813
1957
 
1814
1958
  ## Note that some older widgets like Field repaint every time the form.repaint
@@ -1816,7 +1960,8 @@ module RubyCurses
1816
1960
  ##+ currently I've not implemented events with these widgets. 2010-01-03 15:00
1817
1961
 
1818
1962
  def repaint
1819
- $log.debug("repaint FIELD: #{id}, #{name}, #{focusable}")
1963
+ return unless @repaint_required # 2010-11-20 13:13 its writing over a window i think TESTING
1964
+ #$log.debug("repaint FIELD: #{id}, #{name}, #{focusable}")
1820
1965
  #return if display_length <= 0 # added 2009-02-17 00:17 sometimes editor comp has 0 and that
1821
1966
  # becomes negative below, no because editing still happens
1822
1967
  @display_length = 1 if display_length == 0
@@ -1830,28 +1975,47 @@ module RubyCurses
1830
1975
  end
1831
1976
  end
1832
1977
  #printval = printval[0..display_length-1] if printval.length > display_length
1833
- if @bgcolor.is_a? String and @color.is_a? String
1834
- acolor = ColorMap.get_color(@color, @bgcolor)
1835
- else
1836
- acolor = $datacolor
1837
- end
1978
+ acolor = @color_pair || get_color($datacolor, @color, @bgcolor)
1979
+ #if @bgcolor.is_a? String and @color.is_a? String
1980
+ #acolor = ColorMap.get_color(@color, @bgcolor)
1981
+ #else
1982
+ #acolor = $datacolor
1983
+ #end
1838
1984
  @graphic = @form.window if @graphic.nil? ## cell editor listbox hack XXX fix in correct place
1839
- $log.debug " Field g:#{@graphic}. r,c,displen:#{@row}, #{@col}, #{@display_length} "
1985
+ #$log.debug " Field g:#{@graphic}. r,c,displen:#{@row}, #{@col}, #{@display_length} "
1840
1986
  @graphic.printstring row, col, sprintf("%-*s", display_length, printval), acolor, @attr
1987
+ @repaint_required = false # 2010-11-20 13:13
1841
1988
  end
1842
1989
  def set_focusable(tf)
1843
1990
  @focusable = tf
1844
1991
  end
1992
+ def map_keys
1993
+ return if @keys_mapped
1994
+ bind_key(KEY_LEFT){ cursor_backward }
1995
+ bind_key(KEY_RIGHT){ cursor_forward }
1996
+ bind_key(KEY_BACKSPACE){ delete_prev_char }
1997
+ bind_key(127){ delete_prev_char }
1998
+ bind_key(330){ delete_curr_char }
1999
+ bind_key(?\C-a){ cursor_home }
2000
+ bind_key(?\C-e){ cursor_end }
2001
+ bind_key(?\C-k){ delete_eol }
2002
+ bind_key(?\C-_){ undo_delete_eol }
2003
+ bind_key(27){ set_buffer @original_value }
2004
+ @keys_mapped = true
2005
+ end
1845
2006
 
1846
2007
  # field
2008
+ # # TODO bind these keys so they can be overridden
1847
2009
  def handle_key ch
2010
+ @repaint_required = true # added 2010-11-20 13:21
2011
+ #map_keys unless @keys_mapped # moved to init
1848
2012
  case ch
1849
- when KEY_LEFT
1850
- cursor_backward
1851
- when KEY_RIGHT
1852
- cursor_forward
1853
- when KEY_BACKSPACE, 127
1854
- delete_prev_char if @editable
2013
+ #when KEY_LEFT
2014
+ #cursor_backward
2015
+ #when KEY_RIGHT
2016
+ #cursor_forward
2017
+ #when KEY_BACKSPACE, 127
2018
+ #delete_prev_char if @editable
1855
2019
  #when KEY_UP
1856
2020
  # $log.debug " FIELD GOT KEY_UP, NOW IGNORING 2009-01-16 17:52 "
1857
2021
  #@form.select_prev_field # in a table this should not happen 2009-01-16 17:47
@@ -1860,32 +2024,39 @@ module RubyCurses
1860
2024
  # $log.debug " FIELD GOT KEY_DOWN, NOW IGNORING 2009-01-16 17:52 "
1861
2025
  #@form.select_next_field # in a table this should not happen 2009-01-16 17:47
1862
2026
  # return :UNHANDLED
1863
- when KEY_ENTER, 10, 13
1864
- if respond_to? :fire
1865
- fire
1866
- end
1867
- when 330
1868
- delete_curr_char if @editable
1869
- when ?\C-a.getbyte(0)
1870
- cursor_home
1871
- when ?\C-e.getbyte(0)
1872
- cursor_end
1873
- when ?\C-k.getbyte(0)
1874
- delete_eol if @editable
1875
- when ?\C-_.getbyte(0) # changed on 2010-02-26 14:44 so C-u can be used as numeric arg
1876
- @buffer.insert @curpos, @delete_buffer unless @delete_buffer.nil?
2027
+ # user cannot bind here or on form if i eat up
2028
+ #when KEY_ENTER, 10, 13
2029
+ #if respond_to? :fire
2030
+ #fire
2031
+ #end
2032
+ #when 330
2033
+ #delete_curr_char if @editable
2034
+ #when ?\C-a.getbyte(0)
2035
+ #cursor_home
2036
+ #when ?\C-e.getbyte(0)
2037
+ #cursor_end
2038
+ #when ?\C-k.getbyte(0)
2039
+ #delete_eol if @editable
2040
+ #when ?\C-_.getbyte(0) # changed on 2010-02-26 14:44 so C-u can be used as numeric arg
2041
+ #undo_delete_eol
1877
2042
  when 32..126
1878
2043
  #$log.debug("FIELD: ch #{ch} ,at #{@curpos}, buffer:[#{@buffer}] bl: #{@buffer.to_s.length}")
1879
2044
  putc ch
1880
- when 27 # escape
1881
- $log.debug " ADDED FIELD ESCAPE on 2009-01-18 12:27 XXX #{@original_value}"
1882
- set_buffer @original_value
2045
+ #when 27 # escape
2046
+ #set_buffer @original_value
1883
2047
  else
1884
2048
  ret = super
1885
2049
  return ret
1886
2050
  end
1887
2051
  0 # 2008-12-16 23:05 without this -1 was going back so no repaint
1888
2052
  end
2053
+ # does an undo on delete_eol, not a real undo
2054
+ def undo_delete_eol
2055
+ return if @delete_buffer.nil?
2056
+ #oldvalue = @buffer
2057
+ @buffer.insert @curpos, @delete_buffer
2058
+ fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos+@delete_buffer.length, self, :INSERT, 0, @delete_buffer) # 2010-09-11 13:01
2059
+ end
1889
2060
  ##
1890
2061
  # position cursor at start of field
1891
2062
  def cursor_home
@@ -1912,7 +2083,8 @@ module RubyCurses
1912
2083
  @delete_buffer = @buffer[@curpos..-1]
1913
2084
  # if pos is 0, pos-1 becomes -1, end of line!
1914
2085
  @buffer = pos == -1 ? "" : @buffer[0..pos]
1915
- fire_handler :CHANGE, self # 2008-12-09 14:51
2086
+ #fire_handler :CHANGE, self # 2008-12-09 14:51
2087
+ fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos+@delete_buffer.length, self, :DELETE, 0, @delete_buffer) # 2010-09-11 13:01
1916
2088
  return @delete_buffer
1917
2089
  end
1918
2090
  def cursor_forward
@@ -2009,6 +2181,21 @@ module RubyCurses
2009
2181
  def modified?
2010
2182
  getvalue() != @original_value
2011
2183
  end
2184
+ # field, a convenience method, since set_buffer sucks, it was "inspired" by ncurses itself
2185
+ # @since 1.2.0
2186
+ def text(*val)
2187
+ if val.empty?
2188
+ return getvalue()
2189
+ else
2190
+ return unless val # added 2010-11-17 20:11, dup will fail on nil
2191
+ s = val[0].dup
2192
+ set_buffer(s)
2193
+ end
2194
+ end
2195
+ def text=(val)
2196
+ return unless val # added 2010-11-17 20:11, dup will fail on nil
2197
+ set_buffer(val.dup)
2198
+ end
2012
2199
  # ADD HERE FIELD
2013
2200
  end
2014
2201
 
@@ -2116,6 +2303,7 @@ module RubyCurses
2116
2303
  ##
2117
2304
  # the preferred way of printing text on screen, esp if you want to modify it at run time.
2118
2305
  # Use display_length to ensure no spillage.
2306
+ # This can use text or text_variable for setting and getting data (inh from Widget).
2119
2307
  class Label < Widget
2120
2308
  #dsl_accessor :label_for # related field or buddy
2121
2309
  dsl_accessor :mnemonic # keyboard focus is passed to buddy based on this key (ALT mask)
@@ -2153,9 +2341,9 @@ module RubyCurses
2153
2341
  # for other widgets, attempt to change focus to that field
2154
2342
  def bind_hotkey
2155
2343
  if !@mnemonic.nil?
2156
- ch = @mnemonic.downcase()[0].ord ## FIXME 1.9 DONE
2344
+ ch = @mnemonic.downcase()[0].ord ## 1.9 DONE
2157
2345
  # meta key
2158
- mch = ?\M-a.getbyte(0) + (ch - ?a.getbyte(0)) ## FIXME 1.9
2346
+ mch = ?\M-a.getbyte(0) + (ch - ?a.getbyte(0)) ## 1.9
2159
2347
  if @label_for.is_a? RubyCurses::Button and @label_for.respond_to? :fire
2160
2348
  @form.bind_key(mch, @label_for) { |_form, _butt| _butt.fire }
2161
2349
  else
@@ -2170,7 +2358,8 @@ module RubyCurses
2170
2358
  def repaint
2171
2359
  return unless @repaint_required
2172
2360
  r,c = rowcol
2173
- value = getvalue_for_paint
2361
+ # value often nil so putting blank, but usually some application error
2362
+ value = getvalue_for_paint || ""
2174
2363
  lablist = []
2175
2364
  if @height && @height > 1
2176
2365
  lablist = wrap_text(value, @display_length).split("\n")
@@ -2191,7 +2380,7 @@ module RubyCurses
2191
2380
  str = @justify.to_sym == :right ? "%*s" : "%-*s" # added 2008-12-22 19:05
2192
2381
  # loop added for labels that are wrapped.
2193
2382
  # TODO clear separately since value can change in status like labels
2194
- $log.debug " RWID 1595 #{self.class} value: #{value} form: #{form} "
2383
+ #$log.debug " RWID 1595 #{self.class} value: #{value} form: #{form} "
2195
2384
  @graphic = @form.window if @graphic.nil? ## HACK messagebox givig this in repaint, 423 not working ??
2196
2385
  0.upto(_height-1) { |i|
2197
2386
  @graphic.printstring r+i, c, " " * len , acolor,@attr
@@ -2216,12 +2405,15 @@ module RubyCurses
2216
2405
  end
2217
2406
  ##
2218
2407
  # action buttons
2408
+ # NOTE: When firing event, an ActionEvent will be passed as the first parameter, followed by anything
2409
+ # you may have passed when binding, or calling the command() method.
2219
2410
  # TODO: phasing out underline, and giving mnemonic and ampersand preference
2220
2411
  # - Action: may have to listen to Action property changes so enabled, name etc change can be reflected
2221
2412
  class Button < Widget
2222
2413
  dsl_accessor :surround_chars # characters to use to surround the button, def is square brackets
2223
2414
  dsl_accessor :mnemonic
2224
2415
  def initialize form, config={}, &block
2416
+ require 'rbcurse/ractionevent'
2225
2417
  @focusable = true
2226
2418
  @editable = false
2227
2419
  @handler={} # event handler
@@ -2231,6 +2423,7 @@ module RubyCurses
2231
2423
  @color ||= $datacolor
2232
2424
  @surround_chars ||= ['[ ', ' ]']
2233
2425
  @col_offset = @surround_chars[0].length
2426
+ @_events.push :PRESS
2234
2427
  end
2235
2428
  ##
2236
2429
  # set button based on Action
@@ -2241,7 +2434,7 @@ module RubyCurses
2241
2434
  command { a.call }
2242
2435
  end
2243
2436
  ##
2244
- # sets text, checking for ampersand, uses that for hotkey and underlines
2437
+ # button: sets text, checking for ampersand, uses that for hotkey and underlines
2245
2438
  def text(*val)
2246
2439
  if val.empty?
2247
2440
  return @text
@@ -2271,15 +2464,6 @@ module RubyCurses
2271
2464
  @form.bind_key(mch, self) { |_form, _butt| _butt.fire }
2272
2465
  end
2273
2466
  ##
2274
- # which index to use as underline.
2275
- # Instead of using this to make a hotkey, I am thinking of giving this a new usage.
2276
- # If you wish to override the underline?
2277
- # @deprecated . use mnemonic or an ampersand in text.
2278
- def OLDunderline ix
2279
- _value = @text || getvalue # hack for Togglebutton FIXME
2280
- raise "#{self}: underline requires text to be set " if _value.nil?
2281
- mnemonic _value[ix]
2282
- end
2283
2467
  # bind hotkey to form keys. added 2008-12-15 20:19
2284
2468
  # use ampersand in name or underline
2285
2469
  def bind_hotkey
@@ -2287,7 +2471,7 @@ module RubyCurses
2287
2471
  _value = @text || getvalue # hack for Togglebutton FIXME
2288
2472
  #_value = getvalue
2289
2473
  $log.debug " bind hot #{_value} #{@underline}"
2290
- ch = _value[@underline,1].downcase()[0].ord ## XXX 1.9 2009-10-05 18:55 TOTEST
2474
+ ch = _value[@underline,1].downcase()[0].ord ## 1.9 2009-10-05 18:55 TOTEST
2291
2475
  @mnemonic = _value[@underline,1]
2292
2476
  # meta key
2293
2477
  mch = ?\M-a.getbyte(0) + (ch - ?a.getbyte(0))
@@ -2327,18 +2511,21 @@ module RubyCurses
2327
2511
  @graphic.printstring r, c, "%-*s" % [len, value], color, @attr
2328
2512
  # @form.window.mvchgat(y=r, x=c, max=len, Ncurses::A_NORMAL, bgcolor, nil)
2329
2513
  # in toggle buttons the underline can change as the text toggles
2330
- if !@underline.nil? or !@mnemonic.nil?
2514
+ if @underline || @mnemonic
2331
2515
  uline = @underline && (@underline + @text_offset) || value.index(@mnemonic) || value.index(@mnemonic.swapcase)
2332
- $log.debug " mvchgat UNDERLI r= #{r} - #{@graphic.top} c #{c} c+x #{c+uline}- #{@graphic.left} #{@graphic} "
2516
+ #$log.debug " mvchgat UNDERLI r= #{r} - #{@graphic.top} c #{c} c+x #{c+uline}- #{@graphic.left} #{@graphic} "
2333
2517
  #$log.debug " XXX HACK in next line related to UNDERLINES -graphic.top"
2334
- y=r #-@graphic.top
2335
- x=c+uline #-@graphic.left
2336
- if @graphic.window_type == :PAD
2337
- x -= @graphic.left
2338
- y -= @graphic.top
2518
+ # if the char is not found don't print it
2519
+ if uline
2520
+ y=r #-@graphic.top
2521
+ x=c+uline #-@graphic.left
2522
+ if @graphic.window_type == :PAD
2523
+ x -= @graphic.left
2524
+ y -= @graphic.top
2525
+ end
2526
+ raise "button underline location error #{x} , #{y} " if x < 0 or c < 0
2527
+ @graphic.mvchgat(y, x, max=1, Ncurses::A_BOLD|Ncurses::A_UNDERLINE, color, nil)
2339
2528
  end
2340
- raise "button underline location error #{x} , #{y} " if x < 0 or c < 0
2341
- @graphic.mvchgat(y, x, max=1, Ncurses::A_BOLD|Ncurses::A_UNDERLINE, color, nil)
2342
2529
  end
2343
2530
  end
2344
2531
  ## command of button (invoked on press, hotkey, space)
@@ -2350,7 +2537,9 @@ module RubyCurses
2350
2537
  ## fires PRESS event of button
2351
2538
  def fire
2352
2539
  $log.debug "firing PRESS #{text}"
2353
- fire_handler :PRESS, @form
2540
+ # why the .... am i passing form ? Pass a ActionEvent with source, text() and getvalue()
2541
+ #fire_handler :PRESS, @form XXX changed on 2010-09-12 19:22
2542
+ fire_handler :PRESS, ActionEvent.new(self, :PRESS, text)
2354
2543
  end
2355
2544
  # Button
2356
2545
  def handle_key ch
@@ -2431,6 +2620,9 @@ module RubyCurses
2431
2620
  def getvalue
2432
2621
  @value ? @onvalue : @offvalue
2433
2622
  end
2623
+ # added for some standardization 2010-09-07 20:28
2624
+ # alias :text :getvalue # NEXT VERSION
2625
+ # change existing text to label
2434
2626
  ##
2435
2627
  # is the button on or off
2436
2628
  # added 2008-12-09 19:05
@@ -2440,7 +2632,12 @@ module RubyCurses
2440
2632
  alias :selected? :checked?
2441
2633
 
2442
2634
  def getvalue_for_paint
2443
- buttontext = getvalue()
2635
+ unless @display_length
2636
+ if @onvalue && @offvalue
2637
+ @display_length = [ @onvalue.length, @offvalue.length ].max
2638
+ end
2639
+ end
2640
+ buttontext = getvalue().center(@display_length)
2444
2641
  @text_offset = @surround_chars[0].length
2445
2642
  @surround_chars[0] + buttontext + @surround_chars[1]
2446
2643
  end
@@ -2456,6 +2653,8 @@ module RubyCurses
2456
2653
  def toggle
2457
2654
  fire
2458
2655
  end
2656
+ # called on :PRESS event
2657
+ # caller should check state of itemevent passed to block
2459
2658
  def fire
2460
2659
  checked(!@value)
2461
2660
  # added ItemEvent on 2008-12-31 13:44