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
@@ -20,6 +20,7 @@ module RubyCurses
20
20
  @name.freeze
21
21
  @enabled = true
22
22
  config_setup config # @config.each_pair { |k,v| variable_set(k,v) }
23
+ @_events = [:FIRE]
23
24
  end
24
25
  def call
25
26
  return unless @enabled
@@ -0,0 +1,1294 @@
1
+ =begin
2
+ * Name: App
3
+ * Description: Experimental Application class
4
+ * Author: rkumar (arunachalesha)
5
+ * file created 2010-09-04 22:10
6
+ Todo:
7
+ --------
8
+ * License:
9
+ Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
10
+
11
+ =end
12
+ require 'ncurses'
13
+ require 'logger'
14
+ require 'rbcurse'
15
+
16
+ require 'rbcurse/extras/bottomline'
17
+ $tt ||= RubyCurses::Bottomline.new
18
+ $tt.name = "$tt"
19
+ require 'forwardable'
20
+ module Kernel
21
+ extend Forwardable
22
+ def_delegators :$tt, :ask, :say, :agree, :choose, :numbered_menu, :display_text, :display_text_interactive, :display_list, :say_with_pause
23
+ end
24
+ include Ncurses
25
+ include RubyCurses
26
+ include RubyCurses::Utils
27
+ include Io
28
+ module RubyCurses
29
+ extend self
30
+
31
+ ##
32
+ #
33
+ # @since 1.2.0
34
+ # TODO -
35
+ # / combo
36
+ # - popup
37
+ # - promptmenu
38
+ # - stack and flow should be objects in Form/App?, put in widget when creating
39
+ # - box / rect
40
+ # - para looks like a label that is more than one line, and calculates rows itself based on text
41
+ # - multicontainer
42
+ # - multitextview, multisplit
43
+ # - tabbedpane
44
+ # / table - more work regarding vim keys, also editable
45
+ # - margin - is left offset
46
+ # http://lethain.com/entry/2007/oct/15/getting-started-shoes-os-x/
47
+ #
48
+
49
+ class Widget
50
+ def changed *args, &block
51
+ bind :CHANGED, *args, &block
52
+ end
53
+ def leave *args, &block
54
+ bind :LEAVE, *args, &block
55
+ end
56
+ def enter *args, &block
57
+ bind :ENTER, *args, &block
58
+ end
59
+ # actually we already have command() for buttons
60
+ def click *args, &block
61
+ bind :PRESS, *args, &block
62
+ end
63
+ end
64
+ class CheckBox
65
+ # a little dicey XXX
66
+ def text(*val)
67
+ if val.empty?
68
+ @value ? @onvalue : @offvalue
69
+ else
70
+ super
71
+ end
72
+ end
73
+ end
74
+ # This is the Application class which does the job of setting up the
75
+ # environment, and closing it at the end.
76
+ class App
77
+ attr_reader :config
78
+ attr_reader :form
79
+ attr_reader :window
80
+ attr_writer :quit_key
81
+ # the row on which to prompt user for any inputs
82
+ attr_accessor :prompt_row
83
+
84
+ extend Forwardable
85
+ def_delegators :$tt, :ask, :say, :agree, :choose, :numbered_menu, :display_text, :display_text_interactive, :display_list
86
+ #@tt = Bottomline.new @window, @message_row
87
+ #extend Forwardable
88
+ #def_delegators :@tt, :ask, :say, :agree, :choose
89
+
90
+ # TODO: i should be able to pass window coords here in config
91
+ # :title
92
+ def initialize config={}, &block
93
+ #$log.debug " inside constructor of APP #{config} "
94
+ @config = config
95
+ @app_row = @app_col = 0
96
+ @stack = [] # stack's coordinates
97
+ @flowstack = []
98
+ @variables = {}
99
+ # if we are creating child objects then we will not use outer form. this object will manage
100
+ @current_object = []
101
+ @_system_commands = %w{ bind_global bind_component }
102
+
103
+ init_vars
104
+ $log.debug "XXX APP CONFIG: #{@config} " if $log.debug?
105
+ run &block
106
+ end
107
+ def init_vars
108
+ @quit_key ||= KEY_F1
109
+ # actually this should be maintained inside ncurses pack, so not loaded 2 times.
110
+ # this way if we call an app from existing program, App won't start ncurses.
111
+ unless $ncurses_started
112
+ init_ncurses
113
+ end
114
+ $lastline = Ncurses.LINES - 1
115
+ @message_row = Ncurses.LINES-1
116
+ @prompt_row = @message_row # hope to use for ask etc
117
+ unless $log
118
+ $log = Logger.new((File.join(ENV["LOGDIR"] || "./" ,"view.log")))
119
+ $log.level = Logger::DEBUG
120
+ colors = Ncurses.COLORS
121
+ $log.debug "START #{colors} colors --------- #{$0} win: #{@window} "
122
+ end
123
+ require 'rbcurse/extras/stdscrwindow'
124
+ awin = StdscrWindow.new
125
+ $tt.window = awin; $tt.message_row = @message_row
126
+ # window created in run !!!
127
+ #$tt.window = @window; $tt.message_row = @message_row
128
+ end
129
+ def logger; return $log; end
130
+ def close
131
+ $log.debug " INSIDE CLOSE, #{@stop_ncurses_on_close} "
132
+ @window.destroy if !@window.nil?
133
+ $log.debug " INSIDE CLOSE, #{@stop_ncurses_on_close} "
134
+ if @stop_ncurses_on_close
135
+ VER::stop_ncurses
136
+ $log.debug " CLOSING NCURSES"
137
+ end
138
+ #p $error_message.value unless $error_message.value.nil?
139
+ $log.debug " CLOSING APP"
140
+ #end
141
+ end
142
+ # not sure, but user shuld be able to trap keystrokes if he wants
143
+ # but do i still call handle_key if he does, or give him total control.
144
+ # But loop is already called by framework
145
+ def loop &block
146
+ @form.repaint
147
+ @window.wrefresh
148
+ Ncurses::Panel.update_panels
149
+ while((ch = @window.getchar()) != @quit_key )
150
+ str = keycode_tos ch
151
+ @keyblock.call(str.gsub(/-/, "_").to_sym) if @keyblock
152
+ $log.debug "#{ch} got (#{str})"
153
+ yield ch if block # <<<----
154
+ @form.handle_key ch
155
+ @form.repaint
156
+ @window.wrefresh
157
+ end
158
+ end
159
+ # if calling loop separately better to call this, since it will shut off ncurses
160
+ # and print error on screen.
161
+ def safe_loop &block
162
+ begin
163
+ loop &block
164
+ rescue => ex
165
+ $log.debug( "APP.rb rescue reached ")
166
+ $log.debug( ex) if ex
167
+ $log.debug(ex.backtrace.join("\n")) if ex
168
+ ensure
169
+ close
170
+ # putting it here allows it to be printed on screen, otherwise it was not showing at all.
171
+ if ex
172
+ puts "========== EXCEPTION =========="
173
+ p ex
174
+ puts "==============================="
175
+ puts(ex.backtrace.join("\n"))
176
+ end
177
+ end
178
+ end
179
+ # returns a symbol of the key pressed
180
+ # e.g. :C_c for Ctrl-C
181
+ # :Space, :bs, :M_d etc
182
+ def keypress &block
183
+ @keyblock = block
184
+ end
185
+ # updates a global var with text. Calling app has to set up a Variable with that name and attach to
186
+ # a label so it can be printed.
187
+ def message text
188
+ @message.value = text
189
+ end
190
+ def message_row row
191
+ @message_label.row = row
192
+ end
193
+ # during a process, when you wish to update status, since ordinarily the thread is busy
194
+ # and form does not get control back, so the window won't refresh.
195
+ # NOTE: use this only if +message+ is not working
196
+ def message_immediate text
197
+ message text
198
+ @message_label.repaint
199
+ @window.refresh
200
+ end
201
+ # NOTE XXX using stdscr results in the screen going black if a dialog
202
+ # or other window is popped up, this was great but has not worked out.
203
+ # print directly onto stdscr so that form or window does not require repainting
204
+ # and cursor not messed. however, once form paints then this will be overwritten
205
+ # so at end of printing raw_messages, use message() for final status.
206
+ # Usage: application is inside a long processing loop and wishes to print ongoing status
207
+ # (similar to message_immediate) but faster and less involved
208
+ def raw_message text
209
+ # experimentally trying stdscr instead of label
210
+ scr = Ncurses.stdscr
211
+ text = "%-80s" % text
212
+ Ncurses.mvprintw @message_label.row ,0, text
213
+ #@_stext ||= ""
214
+ #@_stext << text
215
+ ## appending is quite a pain, maybe we should make it separate.
216
+ #stext = "%-80s" % @_stext
217
+ #Ncurses.mvprintw @message_label.row ,0, stext[-80..-1]
218
+ scr.refresh()
219
+ end
220
+ # shows a simple progress bar on last row, using stdscr
221
+ # @param [Float, Array<Fixnum,Fixnum>] percentage, or part/total
222
+ # If Array of two numbers is given then also print part/total on left of bar
223
+ def raw_progress arg
224
+ s = nil
225
+ case arg
226
+ when Array
227
+ #calculate percentage
228
+ pc = (arg[0]*1.0)/arg[1]
229
+ # print items/total also
230
+ s = "%-10s" % "(#{arg[0]}/#{arg[1]})"
231
+ when
232
+ Float
233
+ pc = arg
234
+ end
235
+ scr = Ncurses.stdscr
236
+ endcol = Ncurses.COLS-1
237
+ startcol = endcol - 12
238
+ stext = ("=" * (pc*10).to_i)
239
+ text = "[" + "%-10s" % stext + "]"
240
+ Ncurses.mvprintw( @message_label.row ,startcol-10, s) if s
241
+ Ncurses.mvprintw @message_label.row ,startcol, text
242
+ scr.refresh()
243
+
244
+ end
245
+ # used only by LiveConsole, if enables in an app, usually only during testing.
246
+ def get_binding
247
+ return binding()
248
+ end
249
+ #
250
+ # suspends curses so you can play around on the shell
251
+ # or in cooked mode like Vim does. Expects a block to be passed.
252
+ # Purpose: you can print some stuff without creating a window, or
253
+ # just run shell commands without coming out.
254
+ # NOTE: if you pass clear as true, then the screen will be cleared
255
+ # and you can use puts or print to print. You may have to flush.
256
+ # However, with clear as false, the screen will not be cleared. You
257
+ # will have to print using printw, and if you expect user input
258
+ # you must do a "system /bin/stty sane"
259
+ # If you print stuff, you will have to put a getch() or system("read")
260
+ # to pause the screen.
261
+ def suspend clear=true
262
+ return unless block_given?
263
+ Ncurses.def_prog_mode
264
+ if clear
265
+ Ncurses.endwin
266
+ # NOTE: avoid false since screen remains half off
267
+ # too many issues
268
+ else
269
+ system "/bin/stty sane"
270
+ end
271
+ yield if block_given?
272
+ Ncurses.reset_prog_mode
273
+ if !clear
274
+ # Hope we don't screw your terminal up with this constantly.
275
+ VER::stop_ncurses
276
+ VER::start_ncurses
277
+ #@form.reset_all # not required
278
+ end
279
+ @form.repaint
280
+ @window.wrefresh
281
+ Ncurses::Panel.update_panels
282
+ end
283
+ def get_all_commands
284
+ opts = @_system_commands.dup
285
+ if respond_to? :get_commands
286
+ opts.push(*get_commands())
287
+ end
288
+ opts
289
+ end
290
+ # bind a key to a method at global (form) level
291
+ # Note that individual component may be overriding this.
292
+ def bind_global
293
+ opts = get_all_commands
294
+ cmd = ask("Select a command (TAB for choices) : ", opts)
295
+ if cmd.nil? || cmd == ""
296
+ raw_message "Aborted."
297
+ return
298
+ end
299
+ key = []
300
+ str = ""
301
+ raw_message "Enter one or 2 keys. Finish with ENTER:"
302
+ raw_message "Enter first key:"
303
+ ch = @window.getchar()
304
+ if [KEY_ENTER, 10, 13, ?\C-g.getbyte(0)].include? ch
305
+ raw_message "Aborted."
306
+ return
307
+ end
308
+ key << ch
309
+ str << keycode_tos(ch)
310
+ raw_message "Enter second key or hit return:"
311
+ ch = @window.getchar()
312
+ if ch == 3 || ch == ?\C-g.getbyte(0)
313
+ raw_message "Aborted."
314
+ return
315
+ end
316
+ if ch == 10 || ch == KEY_ENTER || ch == 13
317
+ else
318
+ key << ch
319
+ str << keycode_tos(ch)
320
+ end
321
+ if !key.empty?
322
+ raw_message "Binding #{cmd} to #{str} "
323
+ key = key[0] if key.size == 1
324
+ @form.bind_key(key, cmd.to_sym)
325
+ end
326
+ #message "Bound #{str} to #{cmd} "
327
+ end
328
+ def bind_component
329
+ # the idea here is to get the current component
330
+ # and bind some keys to some methods.
331
+ # however, how do we divine the methods we can map to
332
+ # and also in some cases the components itself has multiple components
333
+ end
334
+ # prompts user for a command. we need to get this back to the calling app
335
+ # or have some block stuff TODO
336
+ # Actually, this is naive, you would want to pass some values in like current data value
337
+ # or lines ??
338
+ # Also may want command completion, or help so all commands can be displayed
339
+ def get_command_from_user choices=nil
340
+ #code, str = rbgetstr(@window, $lastline, 0, "", 80, :default => ":")
341
+ #return unless code == 0
342
+ @_command_history ||= Array.new
343
+ str = ask("Cmd: ", choices) { |q| q.default = @_previous_command; q.history = @_command_history }
344
+ @_command_history << str unless @_command_history.include? str
345
+ # shell the command
346
+ if str =~ /^!/
347
+ str = str[1..-1]
348
+ suspend(false) {
349
+ #system(str);
350
+ $log.debug "XXX STR #{str} " if $log.debug?
351
+
352
+ output=`#{str}`
353
+ system("echo ' ' ");
354
+ $log.debug "XXX output #{output} " if $log.debug?
355
+ system("echo '#{output}' ");
356
+ system("echo Press Enter to continue.");
357
+ system("read");
358
+ }
359
+ return nil # i think
360
+ else
361
+ # TODO
362
+ # here's where we can take internal commands
363
+ #alert "[#{str}] string did not match :!"
364
+ str = str.to_s #= str[1..-1]
365
+ cmdline = str.split
366
+ cmd = cmdline.shift #.to_sym
367
+ if respond_to?(cmd, true)
368
+ send cmd, *cmdline
369
+ else
370
+ alert "#{self.class} does not respond to #{cmd} "
371
+ ret = false
372
+ ret = execute_this(cmd, *cmdline) if respond_to?(:execute_this, true)
373
+ say("#{self.class} does not respond to #{cmd} ", :color_pair => $promptcolor) unless ret
374
+ # should be able to say in red as error
375
+ end
376
+ end
377
+ end
378
+ #
379
+ # @group methods to create widgets easily
380
+ #
381
+ # process arguments based on datatype, perhaps making configuration
382
+ # of some components easier for caller avoiding too much boiler plate code
383
+ #
384
+ # create a field
385
+ def field *args, &block
386
+ config = {}
387
+ events = [ :CHANGED, :LEAVE, :ENTER, :CHANGE ]
388
+ block_event = :CHANGED # LEAVE, ENTER, CHANGE
389
+
390
+ _process_args args, config, block_event, events
391
+ config.delete(:title)
392
+ _position config
393
+ # hope next line doesn't bonk anything
394
+ config[:display_length] ||= @stack.last.width if @stack.last # added here not sure 2010-11-17 18:43
395
+ field = Field.new @form, config
396
+ # shooz uses CHANGED, which is equivalent to our CHANGE. Our CHANGED means modified and exited
397
+ if block
398
+ field.bind(block_event, &block)
399
+ end
400
+ return field
401
+ end
402
+ #instance_eval &block if block_given?
403
+ # or
404
+ #@blk = block # for later execution using @blk.call()
405
+ #colorlabel = Label.new @form, {'text' => "Select a color:", "row" => row, "col" => col, "color"=>"cyan", "mnemonic" => 'S'}
406
+ #var = RubyCurses::Label.new @form, {'text_variable' => $results, "row" => r, "col" => fc}
407
+
408
+ def label *args
409
+ events = block_event = nil
410
+ config = {}
411
+ _process_args args, config, block_event, events
412
+ config[:text] ||= config[:name]
413
+ config[:height] ||= 1
414
+ config.delete(:title)
415
+ _position(config)
416
+ label = Label.new @form, config
417
+ # shooz uses CHANGED, which is equivalent to our CHANGE. Our CHANGED means modified and exited
418
+ return label
419
+ end
420
+ alias :text :label
421
+ def button *args, &block
422
+ config = {}
423
+ events = [ :PRESS, :LEAVE, :ENTER ]
424
+ block_event = :PRESS
425
+
426
+ _process_args args, config, block_event, events
427
+ config[:text] ||= config[:name]
428
+ config.delete(:title)
429
+ # flow gets precedence over stack
430
+ _position(config)
431
+ button = Button.new @form, config
432
+ # shooz uses CHANGED, which is equivalent to our CHANGE. Our CHANGED means modified and exited
433
+ if block
434
+ button.bind(block_event, &block)
435
+ end
436
+ return button
437
+ end
438
+ #
439
+ # create a list
440
+ # Since we are mouseless, one can traverse without selection. So we have a different
441
+ # way of selecting row/s and traversal. XXX this aspect of LB's has always troubled me hugely.
442
+ def list_box *args, &block
443
+ config = {}
444
+ # TODO confirm events
445
+ # listdataevent has interval added and interval removed, due to multiple
446
+ # selection, we have to make that simple for user here.
447
+ events = [ :LEAVE, :ENTER, :ENTER_ROW, :LEAVE_ROW, :LIST_DATA_EVENT ]
448
+ # TODO how to do this so he gets selected row easily
449
+ block_event = :ENTER_ROW
450
+
451
+ _process_args args, config, block_event, events
452
+ # naive defaults, since list could be large or have very long items
453
+ # usually user will provide
454
+ if !config.has_key? :height
455
+ ll = 0
456
+ ll = config[:list].length + 2 if config.has_key? :list
457
+ config[:height] ||= ll
458
+ config[:height] = 15 if config[:height] > 20
459
+ end
460
+ if @current_object.empty?
461
+ $log.debug "1 APP LB w: #{config[:width]} ,#{config[:name]} "
462
+ config[:width] ||= @stack.last.width if @stack.last
463
+ $log.debug "2 APP LB w: #{config[:width]} "
464
+ config[:width] ||= longest_in_list(config[:list])+2
465
+ $log.debug "3 APP LB w: #{config[:width]} "
466
+ end
467
+ # if no width given, expand to flows width XXX SHOULD BE NOT EXPAND ?
468
+ #config[:width] ||= @stack.last.width if @stack.last
469
+ #if config.has_key? :choose
470
+ config[:default_values] = config.delete :choose
471
+ # we make the default single unless specified
472
+ config[:selection_mode] = :single unless config.has_key? :selection_mode
473
+ if @current_object.empty?
474
+ if @instack
475
+ # most likely you won't have row and col. should we check or just go ahead
476
+ col = @stack.last.margin
477
+ config[:row] = @app_row
478
+ config[:col] = col
479
+ @app_row += config[:height] # this needs to take into account height of prev object
480
+ end
481
+ end
482
+ useform = nil
483
+ useform = @form if @current_object.empty?
484
+ field = Listbox.new useform, config
485
+ # shooz uses CHANGED, which is equivalent to our CHANGE. Our CHANGED means modified and exited
486
+ if block
487
+ # this way you can't pass params to the block
488
+ field.bind(block_event, &block)
489
+ end
490
+ return field
491
+ end
492
+
493
+ # toggle button
494
+ def toggle *args, &block
495
+ config = {}
496
+ # TODO confirm events
497
+ events = [ :PRESS, :LEAVE, :ENTER ]
498
+ block_event = :PRESS
499
+ _process_args args, config, block_event, events
500
+ config[:text] ||= longest_in_list2( [config[:onvalue], config[:offvalue]])
501
+ #config[:onvalue] # needed for flow, we need a better way FIXME
502
+ _position(config)
503
+ toggle = ToggleButton.new @form, config
504
+ if block
505
+ toggle.bind(block_event, &block)
506
+ end
507
+ return toggle
508
+ end
509
+ # check button
510
+ def check *args, &block
511
+ config = {}
512
+ # TODO confirm events
513
+ events = [ :PRESS, :LEAVE, :ENTER ]
514
+ block_event = :PRESS
515
+ _process_args args, config, block_event, events
516
+ _position(config)
517
+ toggle = CheckBox.new @form, config
518
+ if block
519
+ toggle.bind(block_event, &block)
520
+ end
521
+ return toggle
522
+ end
523
+ # radio button
524
+ def radio *args, &block
525
+ config = {}
526
+ # TODO confirm events
527
+ events = [ :PRESS, :LEAVE, :ENTER ]
528
+ block_event = :PRESS
529
+ _process_args args, config, block_event, events
530
+ a = config[:group]
531
+ # FIXME we should check if user has set a varialbe in :variable.
532
+ # we should create a variable, so he can use it if he wants.
533
+ if @variables.has_key? a
534
+ v = @variables[a]
535
+ else
536
+ v = Variable.new
537
+ @variables[a] = v
538
+ end
539
+ config[:variable] = v
540
+ config.delete(:group)
541
+ _position(config)
542
+ radio = RadioButton.new @form, config
543
+ if block
544
+ radio.bind(block_event, &block)
545
+ end
546
+ return radio
547
+ end
548
+ # editable text area
549
+ def textarea *args, &block
550
+ require 'rbcurse/rtextarea'
551
+ config = {}
552
+ # TODO confirm events many more
553
+ events = [ :CHANGE, :LEAVE, :ENTER ]
554
+ block_event = events[0]
555
+ _process_args args, config, block_event, events
556
+ config[:width] = config[:display_length] unless config.has_key? :width
557
+ _position(config)
558
+ # if no width given, expand to flows width
559
+ config[:width] ||= @stack.last.width if @stack.last
560
+ useform = nil
561
+ useform = @form if @current_object.empty?
562
+ w = TextArea.new useform, config
563
+ if block
564
+ w.bind(block_event, &block)
565
+ end
566
+ return w
567
+ end
568
+ def textview *args, &block
569
+ require 'rbcurse/rtextview'
570
+ config = {}
571
+ # TODO confirm events many more
572
+ events = [ :PRESS, :LEAVE, :ENTER ]
573
+ block_event = events[0]
574
+ _process_args args, config, block_event, events
575
+ config[:width] = config[:display_length] unless config.has_key? :width
576
+ _position(config)
577
+ # if no width given, expand to flows width
578
+ config[:width] ||= @stack.last.width if @stack.last
579
+ raise "height needed for textview" if !config.has_key? :height
580
+ useform = nil
581
+ useform = @form if @current_object.empty?
582
+ w = TextView.new useform, config
583
+ if block
584
+ w.bind(block_event, &block)
585
+ end
586
+ return w
587
+ end
588
+ # progress bar
589
+ def progress *args, &block
590
+ require 'rbcurse/rprogress'
591
+ config = {}
592
+ # TODO confirm events many more
593
+ events = [ :CHANGE, :LEAVE, :ENTER ]
594
+ block_event = nil
595
+ _process_args args, config, block_event, events
596
+ config[:width] = config[:display_length] || 10 unless config.has_key? :width
597
+ _position(config)
598
+ w = Progress.new @form, config
599
+ #if block
600
+ #w.bind(block_event, &block)
601
+ #end
602
+ return w
603
+ end
604
+
605
+ # table widget
606
+ # @example
607
+ # data = [["Roger",16,"SWI"], ["Phillip",1, "DEU"]]
608
+ # colnames = ["Name", "Wins", "Place"]
609
+ # t = table :width => 40, :height => 10, :columns => colnames, :data => data, :estimate_widths => true
610
+ # other options are :column_widths => [12,4,12]
611
+ # :size_to_fit => true
612
+ def table *args, &block
613
+ require 'rbcurse/rtable'
614
+ config = {}
615
+ # TODO confirm events many more
616
+ events = [ :ENTER_ROW, :LEAVE, :ENTER ]
617
+ block_event = events[0]
618
+ _process_args args, config, block_event, events
619
+ # if user is leaving out width, then we don't want it in config
620
+ # else Widget will put a value of 10 as default, overriding what we've calculated
621
+ if config.has_key? :display_length
622
+ config[:width] = config[:display_length] unless config.has_key? :width
623
+ end
624
+ ext = config.delete :extended_keys
625
+
626
+ model = nil
627
+ _position(config)
628
+ # if no width given, expand to flows width
629
+ config[:width] ||= @stack.last.width if @stack.last
630
+ w = Table.new @form, config
631
+ if ext
632
+ require 'rbcurse/extras/tableextended'
633
+ # so we can increase and decrease column width using keys
634
+ w.extend TableExtended
635
+ w.bind_key(?w){ w.next_column }
636
+ w.bind_key(?b){ w.previous_column }
637
+ w.bind_key(?+) { w.increase_column }
638
+ w.bind_key(?-) { w.decrease_column }
639
+ w.bind_key([?d, ?d]) { w.table_model.delete_at w.current_index }
640
+ w.bind_key(?u) { w.table_model.undo w.current_index}
641
+ end
642
+ if block
643
+ w.bind(block_event, &block)
644
+ end
645
+ return w
646
+ end
647
+ # print a title on first row
648
+ def title string, config={}
649
+ ## TODO center it
650
+ @window.printstring 1, 30, string, $normalcolor, 'reverse'
651
+ end
652
+ # print a sutitle on second row
653
+ def subtitle string, config={}
654
+ @window.printstring 2, 30, string, $datacolor, 'normal'
655
+ end
656
+ # menu bar
657
+ def menubar &block
658
+ require 'rbcurse/rmenu'
659
+ RubyCurses::MenuBar.new &block
660
+ end
661
+
662
+ # creates a blank row
663
+ def blank rows=1, config={}
664
+ @app_row += rows
665
+ end
666
+ # displays a horizontal line
667
+ # takes col (column to start from) from current stack
668
+ # take row from app_row
669
+ #
670
+ # requires width to be passed in config, else defaults to 20
671
+ # @example
672
+ # hline :width => 55
673
+ def hline config={}
674
+ row = config[:row] || @app_row
675
+ width = config[:width] || 20
676
+ _position config
677
+ col = config[:col] || 1
678
+ @color_pair = config[:color_pair] || $datacolor
679
+ @attrib = config[:attrib] || Ncurses::A_NORMAL
680
+ @window.attron(Ncurses.COLOR_PAIR(@color_pair) | @attrib)
681
+ @window.mvwhline( row, col, ACS_HLINE, width)
682
+ @window.attron(Ncurses.COLOR_PAIR(@color_pair) | @attrib)
683
+ @app_row += 1
684
+ end
685
+ def app_header title, config={}, &block
686
+ require 'rbcurse/applicationheader'
687
+ header = ApplicationHeader.new @form, title, config, &block
688
+ end
689
+ def dock labels, config={}, &block
690
+ require 'rbcurse/keylabelprinter'
691
+ klp = RubyCurses::KeyLabelPrinter.new @form, labels, config, &block
692
+ end
693
+ def link *args, &block
694
+ require 'rbcurse/extras/rlink'
695
+ config = {}
696
+ events = [ :PRESS, :LEAVE, :ENTER ]
697
+ block_event = :PRESS
698
+ _process_args args, config, block_event, events
699
+ _position(config)
700
+ config[:text] ||= config.delete :title
701
+ config[:highlight_foreground] = "yellow"
702
+ config[:highlight_background] = "red"
703
+ toggle = Link.new @form, config
704
+ if block
705
+ toggle.bind(block_event, toggle, &block)
706
+ end
707
+ return toggle
708
+ end
709
+ def menulink *args, &block
710
+ require 'rbcurse/extras/rmenulink'
711
+ config = {}
712
+ events = [ :PRESS, :LEAVE, :ENTER ]
713
+ block_event = :PRESS
714
+ _process_args args, config, block_event, events
715
+ _position(config)
716
+ config[:text] ||= config.delete :title
717
+ config[:highlight_foreground] = "yellow"
718
+ config[:highlight_background] = "red"
719
+ toggle = MenuLink.new @form, config
720
+ if block
721
+ toggle.bind(block_event, toggle, &block)
722
+ end
723
+ return toggle
724
+ end
725
+ def splitpane *args, &block
726
+ require 'rbcurse/rsplitpane2'
727
+ config = {}
728
+ events = [ :PROPERTY_CHANGE, :LEAVE, :ENTER ]
729
+ block_event = events[0]
730
+ _process_args args, config, block_event, events
731
+ _position(config)
732
+ # if no width given, expand to flows width
733
+ config[:width] ||= @stack.last.width if @stack.last
734
+ config.delete :title
735
+ useform = nil
736
+ useform = @form if @current_object.empty?
737
+
738
+ w = SplitPane.new useform, config
739
+ #if block
740
+ #w.bind(block_event, w, &block)
741
+ #end
742
+ if block_given?
743
+ @current_object << w
744
+ #instance_eval &block if block_given?
745
+ yield w
746
+ @current_object.pop
747
+ end
748
+ return w
749
+ end
750
+ def multisplit *args, &block
751
+ require 'rbcurse/rmultisplit'
752
+ config = {}
753
+ events = [ :PROPERTY_CHANGE, :LEAVE, :ENTER ]
754
+ block_event = events[0]
755
+ _process_args args, config, block_event, events
756
+ _position(config)
757
+ # if no width given, expand to flows width
758
+ config[:width] ||= @stack.last.width if @stack.last
759
+ config.delete :title
760
+ useform = nil
761
+ useform = @form if @current_object.empty?
762
+
763
+ w = MultiSplit.new useform, config
764
+ #if block
765
+ #w.bind(block_event, w, &block)
766
+ #end
767
+ if block_given?
768
+ @current_object << w
769
+ #instance_eval &block if block_given?
770
+ yield w
771
+ @current_object.pop
772
+ end
773
+ return w
774
+ end
775
+ def tree *args, &block
776
+ require 'rbcurse/rtree'
777
+ config = {}
778
+ events = [:TREE_WILL_EXPAND_EVENT, :TREE_EXPANDED_EVENT, :TREE_SELECTION_EVENT, :PROPERTY_CHANGE, :LEAVE, :ENTER ]
779
+ block_event = nil
780
+ _process_args args, config, block_event, events
781
+ config[:height] ||= 10
782
+ _position(config)
783
+ # if no width given, expand to flows width
784
+ config[:width] ||= @stack.last.width if @stack.last
785
+ #config.delete :title
786
+ useform = nil
787
+ useform = @form if @current_object.empty?
788
+
789
+ w = Tree.new useform, config, &block
790
+ return w
791
+ end
792
+ def vimsplit *args, &block
793
+ require 'rbcurse/rvimsplit'
794
+ config = {}
795
+ #TODO check these
796
+ events = [:PROPERTY_CHANGE, :LEAVE, :ENTER ]
797
+ block_event = nil
798
+ _process_args args, config, block_event, events
799
+ config[:height] ||= 10
800
+ _position(config)
801
+ # if no width given, expand to flows width
802
+ config[:width] ||= @stack.last.width if @stack.last
803
+ #config.delete :title
804
+ useform = nil
805
+ useform = @form if @current_object.empty?
806
+
807
+ w = VimSplit.new useform, config # NO BLOCK GIVEN
808
+ if block_given?
809
+ @current_object << w
810
+ #instance_eval &block if block_given?
811
+ yield w
812
+ @current_object.pop
813
+ end
814
+ return w
815
+ end
816
+ # create a readonly list
817
+ def basiclist *args, &block
818
+ require 'rbcurse/rbasiclistbox'
819
+ config = {}
820
+ #TODO check these
821
+ events = [ :LEAVE, :ENTER, :ENTER_ROW, :LEAVE_ROW, :LIST_DATA_EVENT ]
822
+ # TODO how to do this so he gets selected row easily
823
+ block_event = :ENTER_ROW
824
+ _process_args args, config, block_event, events
825
+ # some guesses at a sensible height for listbox
826
+ if !config.has_key? :height
827
+ ll = 0
828
+ ll = config[:list].length + 2 if config.has_key? :list
829
+ config[:height] ||= ll
830
+ config[:height] = 15 if config[:height] > 20
831
+ end
832
+ _position(config)
833
+ # if no width given, expand to flows width
834
+ config[:width] ||= @stack.last.width if @stack.last
835
+ config[:width] ||= longest_in_list(config[:list])+2
836
+ #config.delete :title
837
+ #config[:default_values] = config.delete :choose
838
+ config[:selection_mode] = :single unless config.has_key? :selection_mode
839
+ useform = nil
840
+ useform = @form if @current_object.empty?
841
+
842
+ w = BasicListbox.new useform, config # NO BLOCK GIVEN
843
+ if block_given?
844
+ field.bind(block_event, &block)
845
+ end
846
+ return w
847
+ end
848
+ def master_detail *args, &block
849
+ require 'rbcurse/extras/masterdetail'
850
+ config = {}
851
+ events = [:PROPERTY_CHANGE, :LEAVE, :ENTER ]
852
+ block_event = nil
853
+ _process_args args, config, block_event, events
854
+ #config[:height] ||= 10
855
+ _position(config)
856
+ # if no width given, expand to flows width
857
+ config[:width] ||= @stack.last.width if @stack.last
858
+ #config.delete :title
859
+ useform = nil
860
+ useform = @form if @current_object.empty?
861
+
862
+ w = MasterDetail.new useform, config # NO BLOCK GIVEN
863
+ if block_given?
864
+ @current_object << w
865
+ yield_or_eval &block
866
+ @current_object.pop
867
+ end
868
+ return w
869
+ end
870
+ # creates a simple readonly table, that allows users to click on rows
871
+ # and also on the header. Header clicking is for column-sorting.
872
+ def tabular_widget *args, &block
873
+ require 'rbcurse/extras/tabularwidget'
874
+ config = {}
875
+ events = [:PROPERTY_CHANGE, :LEAVE, :ENTER, :CHANGE, :ENTER_ROW, :PRESS ]
876
+ block_event = nil
877
+ _process_args args, config, block_event, events
878
+ config[:height] ||= 10 # not sure if this should be here
879
+ _position(config)
880
+ # if no width given, expand to stack width
881
+ config[:width] ||= @stack.last.width if @stack.last
882
+ #config.delete :title
883
+ useform = nil
884
+ useform = @form if @current_object.empty?
885
+
886
+ w = TabularWidget.new useform, config # NO BLOCK GIVEN
887
+ if block_given?
888
+ @current_object << w
889
+ yield_or_eval &block
890
+ @current_object.pop
891
+ end
892
+ return w
893
+ end
894
+ # scrollbar attached to the right of a parent object
895
+ def scrollbar *args, &block
896
+ require 'rbcurse/extras/scrollbar'
897
+ config = {}
898
+ events = [:PROPERTY_CHANGE, :LEAVE, :ENTER ] # # none really at present
899
+ block_event = nil
900
+ _process_args args, config, block_event, events
901
+ raise "parent needed for scrollbar" if !config.has_key? :parent
902
+ useform = nil
903
+ useform = @form if @current_object.empty?
904
+ sb = Scrollbar.new useform, config
905
+ end
906
+ # divider used to resize neighbouring components TOTEST XXX
907
+ def divider *args, &block
908
+ require 'rbcurse/extras/divider'
909
+ config = {}
910
+ events = [:PROPERTY_CHANGE, :LEAVE, :ENTER, :DRAG_EVENT ] # # none really at present
911
+ block_event = nil
912
+ _process_args args, config, block_event, events
913
+ useform = nil
914
+ useform = @form if @current_object.empty?
915
+ sb = Divider.new useform, config
916
+ end
917
+ # creates a simple readonly table, that allows users to click on rows
918
+ # and also on the header. Header clicking is for column-sorting.
919
+ def combo *args, &block
920
+ require 'rbcurse/rcombo'
921
+ config = {}
922
+ events = [:PROPERTY_CHANGE, :LEAVE, :ENTER, :CHANGE, :ENTER_ROW, :PRESS ] # XXX
923
+ block_event = nil
924
+ _process_args args, config, block_event, events
925
+ _position(config)
926
+ # if no width given, expand to flows width
927
+ config[:width] ||= @stack.last.width if @stack.last
928
+ #config.delete :title
929
+ useform = nil
930
+ useform = @form if @current_object.empty?
931
+
932
+ w = ComboBox.new useform, config # NO BLOCK GIVEN
933
+ if block_given?
934
+ @current_object << w
935
+ yield_or_eval &block
936
+ @current_object.pop
937
+ end
938
+ return w
939
+ end
940
+
941
+ # ADD new widget above this
942
+
943
+ # @endgroup
944
+
945
+ # @group positioning of components
946
+
947
+ # line up vertically whatever comes in, ignoring r and c
948
+ # margin_top to add to margin of existing stack (if embedded) such as extra spacing
949
+ # margin to add to margin of existing stack, or window (0)
950
+ # NOTE: since these coordins are calculated at start
951
+ # therefore if window resized i can't recalculate.
952
+ Stack = Struct.new(:margin_top, :margin, :width)
953
+ def stack config={}, &block
954
+ @instack = true
955
+ mt = config[:margin_top] || 1
956
+ mr = config[:margin] || 0
957
+ # must take into account margin
958
+ defw = Ncurses.COLS - mr
959
+ config[:width] = defw if config[:width] == :EXPAND
960
+ w = config[:width] || [50, defw].min
961
+ s = Stack.new(mt, mr, w)
962
+ @app_row += mt
963
+ mr += @stack.last.margin if @stack.last
964
+ #@stack << mr
965
+ @stack << s
966
+ #instance_eval &block if block_given?
967
+ yield_or_eval &block if block_given? # modified 2010-11-17 20:36
968
+ @stack.pop
969
+ @instack = false if @stack.empty?
970
+ @app_row = 0 if @stack.empty?
971
+ end
972
+ # keep adding to right of previous and when no more space
973
+ # move down and continue fitting in.
974
+ # Useful for button positioning. Currently, we can use a second flow
975
+ # to get another row.
976
+ # TODO: move down when row filled
977
+ # TODO: align right, center
978
+ def flow config={}, &block
979
+ @inflow = true
980
+ mt = config[:margin_top] || 0
981
+ @app_row += mt
982
+ col = @flowstack.last || @stack.last.margin || @app_col
983
+ col += config[:margin] || 0
984
+ @flowstack << col
985
+ @flowcol = col
986
+ #instance_eval &block if block_given?
987
+ yield_or_eval &block if block_given? # modified 2010-11-17 20:36
988
+ @flowstack.pop
989
+ @inflow = false if @flowstack.empty?
990
+ end
991
+
992
+ private
993
+ def quit
994
+ throw(:close)
995
+ end
996
+ # Initialize curses
997
+ def init_ncurses
998
+ VER::start_ncurses # this is initializing colors via ColorMap.setup
999
+ #$ncurses_started = true
1000
+ @stop_ncurses_on_close = true
1001
+ end
1002
+
1003
+ # returns length of longest
1004
+ def longest_in_list list #:nodoc:
1005
+ longest = list.inject(0) do |memo,word|
1006
+ memo >= word.length ? memo : word.length
1007
+ end
1008
+ longest
1009
+ end
1010
+ # returns longest item
1011
+ def longest_in_list2 list #:nodoc:
1012
+ longest = list.inject(list[0]) do |memo,word|
1013
+ memo.length >= word.length ? memo : word
1014
+ end
1015
+ longest
1016
+ end
1017
+
1018
+ # if partial command entered then returns matches
1019
+ def _resolve_command opts, cmd
1020
+ return cmd if opts.include? cmd
1021
+ matches = opts.grep Regexp.new("^#{cmd}")
1022
+ end
1023
+ def run &block
1024
+ begin
1025
+
1026
+ # check if user has passed window coord in config, else root window
1027
+ @window = VER::Window.root_window
1028
+ #$tt.window = @window; $tt.message_row = @message_row
1029
+ awin = @window
1030
+ #require 'rbcurse/extras/stdscrwindow'
1031
+ #awin = StdscrWindow.new
1032
+ #$tt.window = awin; $tt.message_row = @message_row
1033
+ catch(:close) do
1034
+ @form = Form.new @window
1035
+ @form.bind_key([?\C-x, ?c]) { suspend(false) do
1036
+ system("tput cup 26 0")
1037
+ system("tput ed")
1038
+ system("echo Enter C-d to return to application")
1039
+ system (ENV['PS1']='\s-\v\$ ')
1040
+ system(ENV['SHELL']);
1041
+ end
1042
+ }
1043
+ # this is a very rudimentary default command executer, it does not
1044
+ # allow tab completion. App should use M-x with names of commands
1045
+ # as in appgmail
1046
+ @form.bind_key(?:) {
1047
+ str = get_command_from_user
1048
+ }
1049
+
1050
+ @form.bind_key(?\M-x){
1051
+ # TODO previous command to be default
1052
+ opts = get_all_commands()
1053
+ @_command_history ||= Array.new
1054
+ # previous command should be in opts, otherwise it is not in this context
1055
+ cmd = ask("Command: ", opts){ |q| q.default = @_previous_command; q.history = @_command_history }
1056
+ if cmd == ""
1057
+ else
1058
+ @_command_history << cmd unless @_command_history.include? cmd
1059
+ cmdline = cmd.split
1060
+ cmd = cmdline.shift
1061
+ # check if command is a substring of a larger command
1062
+ if !opts.include?(cmd)
1063
+ rcmd = _resolve_command(opts, cmd) if !opts.include?(cmd)
1064
+ if rcmd.size == 1
1065
+ cmd = rcmd.first
1066
+ else
1067
+ say_with_pause "Cannot resolve #{cmd}. Matches are: #{rcmd} "
1068
+ end
1069
+ end
1070
+ if respond_to?(cmd, true)
1071
+ @_previous_command = cmd
1072
+ raw_message "calling #{cmd} "
1073
+ begin
1074
+ send cmd, *cmdline
1075
+ rescue => exc
1076
+ $log.debug "ERR EXC: send throwing an exception now. Duh. IMAP keeps crashing haha !! #{exc} " if $log.debug?
1077
+ if exc
1078
+ $log.debug( exc)
1079
+ $log.debug(exc.backtrace.join("\n"))
1080
+ say_with_pause exc.to_s
1081
+ end
1082
+ end
1083
+ else
1084
+ say("Command [#{cmd}] not supported by #{self.class} ")
1085
+ end
1086
+ end
1087
+ }
1088
+ @message = Variable.new
1089
+ @message.value = ""
1090
+ @message_label = RubyCurses::Label.new @form, {:text_variable => @message, :name=>"message_label",:row => Ncurses.LINES-1, :col => 0, :display_length => Ncurses.COLS, :height => 1, :color => :white}
1091
+ $error_message.update_command { @message.set_value($error_message.value) }
1092
+ if block
1093
+ begin
1094
+ #yield(self, @window, @form)
1095
+ #instance_eval &block if block_given?
1096
+ yield_or_eval &block if block_given? # modified 2010-11-17 20:36
1097
+ loop
1098
+ rescue => ex
1099
+ $log.debug( "APP.rb rescue reached ")
1100
+ $log.debug( ex) if ex
1101
+ $log.debug(ex.backtrace.join("\n")) if ex
1102
+ ensure
1103
+ close
1104
+ # putting it here allows it to be printed on screen, otherwise it was not showing at all.
1105
+ if ex
1106
+ puts "========== EXCEPTION =========="
1107
+ p ex
1108
+ puts "==============================="
1109
+ puts(ex.backtrace.join("\n"))
1110
+ end
1111
+ end
1112
+ nil
1113
+ else
1114
+ #@close_on_terminate = true
1115
+ self
1116
+ end #if block
1117
+ end
1118
+ end
1119
+ end
1120
+ # TODO
1121
+ # process args, all widgets should call this
1122
+ def _process_args args, config, block_event, events #:nodoc:
1123
+ args.each do |arg|
1124
+ case arg
1125
+ when Array
1126
+ # we can use r,c, w, h
1127
+ row, col, display_length, height = arg
1128
+ config[:row] = row
1129
+ config[:col] = col
1130
+ config[:display_length] = display_length if display_length
1131
+ config[:width] = display_length if display_length
1132
+ # width for most XXX ?
1133
+ config[:height] = height if height
1134
+ when Hash
1135
+ config.merge!(arg)
1136
+ if block_event
1137
+ block_event = config.delete(:block_event){ block_event }
1138
+ raise "Invalid event. Use #{events}" unless events.include? block_event
1139
+ end
1140
+ when String
1141
+ config[:name] = arg
1142
+ config[:title] = arg # some may not have title
1143
+ #config[:text] = arg # some may not have title
1144
+ end
1145
+ end
1146
+ end # _process
1147
+ # position object based on whether in a flow or stack.
1148
+ # @app_row is prepared for next object based on this objects ht
1149
+ def _position config #:nodoc:
1150
+ unless @current_object.empty?
1151
+ $log.debug " WWWW returning from position #{@current_object.last} "
1152
+ return
1153
+ end
1154
+ if @inflow
1155
+ #col = @flowstack.last
1156
+ config[:row] = @app_row
1157
+ config[:col] = @flowcol
1158
+ $log.debug " YYYY config #{config} "
1159
+ if config[:text]
1160
+ @flowcol += config[:text].length + 5 # 5 came from buttons
1161
+ else
1162
+ @flowcol += (config[:length] || 10) + 5 # trying out for combo
1163
+ end
1164
+ elsif @instack
1165
+ # most likely you won't have row and col. should we check or just go ahead
1166
+ col = @stack.last.margin
1167
+ config[:row] = @app_row
1168
+ config[:col] = col
1169
+ @app_row += config[:height] || 1
1170
+ # TODO need to allow stack to have its spacing, but we don't have an object as yet.
1171
+ end
1172
+ end
1173
+ end # class
1174
+ end # module
1175
+ if $0 == __FILE__
1176
+ include RubyCurses
1177
+ #app = App.new
1178
+ #window = app.window
1179
+ #window.printstring 2, 30, "Demo of Listbox - rbcurse", $normalcolor, 'reverse'
1180
+ #app.logger.info "beforegetch"
1181
+ #window.getch
1182
+ #app.close
1183
+ # this was the yield example, but now we've moved to instance eval
1184
+ App.new do
1185
+ @window.printstring 0, 30, "Demo of Listbox - rbcurse", $normalcolor, 'reverse'
1186
+ @window.printstring 1, 30, "Hit F1 to quit", $datacolor, 'normal'
1187
+ form = @form
1188
+ fname = "Search"
1189
+ r, c = 7, 30
1190
+ c += fname.length + 1
1191
+ #field1 = field( [r,c, 30], fname, :bgcolor => "cyan", :block_event => :CHANGE) do |fld|
1192
+ stack :margin_top => 2, :margin => 10 do
1193
+ lbl = label({:text => fname, :color=>'white',:bgcolor=>'red', :mnemonic=> 's'})
1194
+ field1 = field( [r,c, 30], fname, :bgcolor => "cyan",:block_event => :CHANGE) do |fld|
1195
+ message("You entered #{fld.getvalue}. To quit enter quit and tab out")
1196
+ if fld.getvalue == "quit"
1197
+ logger.info "you typed quit!"
1198
+ throw :close
1199
+ end
1200
+ end
1201
+ #field1.set_label Label.new @form, {:text => fname, :color=>'white',:bgcolor=>'red', :mnemonic=> 's'}
1202
+ field1.set_label( lbl )
1203
+ field1.enter do
1204
+ message "you entered this field"
1205
+ end
1206
+
1207
+ stack :margin_top => 2, :margin => 0 do
1208
+ #label( [8, 30, 60],{:text => "A label", :color=>'white',:bgcolor=>'blue'} )
1209
+ end
1210
+
1211
+ @bluelabel = label( [8, 30, 60],{:text => "B label", :color=>'white',:bgcolor=>'blue'} )
1212
+
1213
+ stack :margin_top => 2, :margin => 0 do
1214
+ toggle :onvalue => " Toggle Down ", :offvalue => " Untoggle ", :mnemonic => 'T', :value => true
1215
+
1216
+ toggle :onvalue => " On ", :offvalue => " Off ", :value => true do |e|
1217
+ alert "You pressed me #{e.state}"
1218
+ end
1219
+ check :text => "Check me!", :onvalue => "Checked", :offvalue => "Unchecked", :value => true do |e|
1220
+ # this works but long and complicated
1221
+ #@bluelabel.text = e.item.getvalue ? e.item.onvalue : e.item.offvalue
1222
+ @bluelabel.text = e.item.text
1223
+ end
1224
+ radio :text => "red", :value => "RED", :color => "red", :group => :colors
1225
+ radio :text => "green", :value => "GREEN", :color => "green", :group => :colors
1226
+ flow do
1227
+ button_row = 17
1228
+ ok_button = button( [button_row,30], "OK", {:mnemonic => 'O'}) do
1229
+ alert("About to dump data into log file!")
1230
+ message "Dumped data to log file"
1231
+ end
1232
+
1233
+ # using ampersand to set mnemonic
1234
+ cancel_button = button( [button_row, 40], "&Cancel" ) do
1235
+ if confirm("Do your really want to quit?")== :YES
1236
+ #throw(:close);
1237
+ quit
1238
+ else
1239
+ message "Quit aborted"
1240
+ end
1241
+ end # cancel
1242
+ button "Don't know"
1243
+ end
1244
+ flow :margin_top => 2 do
1245
+ button "Another"
1246
+ button "Line"
1247
+ end
1248
+ stack :margin_top => 2, :margin => 0 do
1249
+ @pbar = progress :width => 20, :bgcolor => 'white', :color => 'red'
1250
+ @pbar1 = progress :width => 20, :style => :old
1251
+ end
1252
+ end
1253
+ end # stack
1254
+ # lets make another column
1255
+ stack :margin_top => 2, :margin => 70 do
1256
+ l = label "Column 2"
1257
+ f1 = field "afield", :bgcolor => 'white', :color => 'black'
1258
+ list_box "A list", :list => ["Square", "Oval", "Rectangle", "Somethinglarge"], :choose => ["Square"]
1259
+ lb = list_box "Another", :list => ["Square", "Oval", "Rectangle", "Somethinglarge"] do |list|
1260
+ #f1.set_buffer list.text
1261
+ #f1.text list.text
1262
+ f1.text = list.text
1263
+ l.text = list.text.upcase
1264
+ end
1265
+ t = textarea :height => 10 do |e|
1266
+ #@bluelabel.text = e.to_s.tr("\n",' ')
1267
+ @bluelabel.text = e.text.gsub("\n"," ")
1268
+ len = e.source.get_text.length
1269
+ len = len % 20 if len > 20
1270
+ $log.debug " PBAR len of text is #{len}: #{len/20.0} "
1271
+ @pbar.fraction(len/20.0)
1272
+ @pbar1.fraction(len/20.0)
1273
+ i = ((len/20.0)*100).to_i
1274
+ @pbar.text = "completed:#{i}"
1275
+ end
1276
+ t.leave do |c|
1277
+ @bluelabel.text = c.get_text.gsub("\n"," ")
1278
+ end
1279
+
1280
+ end
1281
+
1282
+ # Allow user to get the keys
1283
+ keypress do |key|
1284
+ if key == :C_c
1285
+ message "You tried to cancel"
1286
+ #throw :close
1287
+ quit
1288
+ else
1289
+ #app.message "You pressed #{key}, #{char} "
1290
+ message "You pressed #{key}"
1291
+ end
1292
+ end
1293
+ end
1294
+ end