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
@@ -0,0 +1,67 @@
1
+ =begin
2
+ * Name: ActionEvent
3
+ * Description: Event used to notify interested parties that an action has happened on component
4
+ Usually a button press. Nothing more.
5
+ * Author: rkumar (arunachalesha)
6
+
7
+ --------
8
+ * Date: 2010-09-12 18:53
9
+ * License:
10
+ Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
11
+
12
+ =end
13
+
14
+ # Event created when state changed (as in ViewPort)
15
+ module RubyCurses
16
+ # source - as always is the object whose event has been fired
17
+ # id - event identifier (seems redundant since we bind events often separately.
18
+ # event - is :PRESS
19
+ # action_command - command string associated with event (such as title of button that changed
20
+ ActionEvent = Struct.new(:source, :event, :action_command) do
21
+ # This should always return the most relevant text associated with this object
22
+ # so the user does not have to go through the source object's documentation.
23
+ # It should be a user-friendly string
24
+ # @return text associated with source (label of button)
25
+ def text
26
+ source.text
27
+ end
28
+
29
+ # This is similar to text and can often be just an alias.
30
+ # However, i am putting this for backward compatibility with programs
31
+ # that received the object and called it's getvalue. It is better to use text.
32
+ # @return text associated with source (label of button)
33
+ def getvalue
34
+ source.getvalue
35
+ end
36
+ end
37
+ # a derivative of Action Event for textviews
38
+ # We allow a user to press ENTER on a row and use that for processing.
39
+ # We are basically using TextView as a list in which user can scroll around
40
+ # and move cursor at will.
41
+ class TextActionEvent < ActionEvent
42
+ # current_index or line number starting 0
43
+ attr_accessor :current_index
44
+ # cursor position on the line
45
+ attr_accessor :curpos
46
+ def initialize source, event, action_command, current_index, curpos
47
+ super source, event, action_command
48
+ @current_index = current_index
49
+ @curpos = curpos
50
+ end
51
+ # the text of the line on which the user is
52
+ def text
53
+ source.current_value
54
+ end
55
+ # the word under the cursor TODO
56
+ # if its a text with pipe delim, then ??
57
+ def word_under_cursor line=text(), pos=@curpos, delim=" "
58
+ line ||= text()
59
+ pos ||= @curpos
60
+ finish = line.index(delim, pos)
61
+ start = line.rindex(delim,pos)
62
+ finish = -1 if finish.nil?
63
+ start = 0 if start.nil?
64
+ return line[start..finish]
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,688 @@
1
+ =begin
2
+ * Name: rlistbox: basic scrollable lists - no editing, see editablelistbox of more
3
+ * Description
4
+ * Author: rkumar (arunachalesha)
5
+ * Date: 2010-09-26 16:00
6
+ * License: Same as Ruby's License (http://www.ruby-lang.org/LICENSE.txt)
7
+ *
8
+ TODO
9
+ [x] removed Popup, ListDataEvent and ListDataModel !
10
+ [x] XXX Can we separate editing out. Make a ReadonlyList, and extend it as EditableList. This way the usual
11
+ use case remains cleaner.
12
+ =end
13
+ require 'rbcurse'
14
+ require 'rbcurse/listcellrenderer'
15
+ #require 'rbcurse/listkeys'
16
+ require 'forwardable'
17
+
18
+
19
+ include Ncurses
20
+ module RubyCurses
21
+ extend self
22
+ ##
23
+ ##
24
+ # scrollable, selectable list of items
25
+ # - @selected contains indices of selected objects.
26
+ ##
27
+ ##
28
+ # A readonly control for displaying a list of data or values.
29
+ # Although user editing is not allowed, but the list may be repopulated
30
+ # as in a directory listing, or a list dependent on some other control's value.
31
+ # This is not a drop-in replacement for Listbox as it drops many methods that are redundant.
32
+ # Default selection is single, as opposed to Listbox.
33
+ #
34
+ class BasicListbox < Widget
35
+
36
+ require 'rbcurse/listscrollable'
37
+ #require 'rbcurse/listselectable'
38
+ #require 'rbcurse/defaultlistselectionmodel'
39
+ include ListScrollable
40
+ extend Forwardable
41
+ dsl_accessor :height
42
+ dsl_accessor :title
43
+ dsl_property :title_attrib # bold, reverse, normal
44
+ # dsl_accessor :list # the array of data to be sent by user
45
+ attr_reader :toprow
46
+ #dsl_accessor :default_values # array of default values
47
+ dsl_accessor :is_popup # if it is in a popup and single select, selection closes
48
+ attr_accessor :current_index
49
+ dsl_accessor :selection_mode
50
+ dsl_accessor :selected_color, :selected_bgcolor, :selected_attr
51
+ dsl_accessor :max_visible_items # how many to display 2009-01-11 16:15
52
+ #dsl_accessor :cell_editing_allowed
53
+ dsl_property :show_selector # boolean
54
+ dsl_property :row_selected_symbol # 2009-01-12 12:01 changed from selector to selected
55
+ dsl_property :row_unselected_symbol # added 2009-01-12 12:00
56
+ dsl_property :left_margin
57
+ # please set these in he constructor block. Settin them later will have no effect
58
+ # since i would have bound them to actions
59
+ attr_accessor :one_key_selection # will pressing a single key select or not
60
+ dsl_accessor :border_attrib, :border_color #
61
+ # set to true if data could have newlines, tabs, and other stuff, def true
62
+ dsl_accessor :sanitization_required
63
+ # set to true if cell-renderer data can exceed width of listbox, default true
64
+ # if you are absolutely sure that data is constant width, set to false.
65
+ dsl_accessor :truncation_required
66
+ dsl_accessor :suppress_borders #to_print_borders
67
+ dsl_accessor :justify # will be picked up by renderer
68
+ # index of selected row
69
+ attr_accessor :selected_index
70
+ # index of selected rows, if multiple selection asked for
71
+ attr_reader :selected_indices
72
+
73
+
74
+ # basic listbox constructor
75
+ #
76
+ def initialize form, config={}, &block
77
+ @focusable = true
78
+ @editable = false
79
+ @sanitization_required = true # cleanup control and non print chars
80
+ @truncation_required = true
81
+ @suppress_borders = false #to_print_borders = 1
82
+ @row_selected_symbol = ''
83
+ @row = 0
84
+ @col = 0
85
+ # data of listbox this is not an array, its a pointer to the listdatamodel
86
+ @list = nil
87
+ # any special attribs such as status to be printed in col1, or color (selection)
88
+ @list_attribs = {}
89
+ @current_index = 0
90
+ @selected_indices = []
91
+ @selected_index = nil
92
+ @row_offset = @col_offset = 1
93
+ super
94
+ @_events.push(*[:ENTER_ROW, :LEAVE_ROW, :LIST_SELECTION_EVENT, :PRESS])
95
+ @selection_mode ||= :multiple # default is multiple, anything else given becomes single
96
+ @win = @graphic # 2010-01-04 12:36 BUFFERED replace form.window with graphic
97
+ # moving down to repaint so that scrollpane can set should_buffered
98
+ # added 2010-02-17 23:05 RFED16 so we don't need a form.
99
+ @win_left = 0
100
+ @win_top = 0
101
+
102
+ # next 2 lines carry a redundancy
103
+ #select_default_values
104
+ # when the combo box has a certain row in focus, the popup should have the same row in focus
105
+
106
+ init_vars
107
+
108
+ if @list && !@selected_index.nil? # XXX
109
+ set_focus_on @selected_index # the new version
110
+ end
111
+ end
112
+ # this is called several times, from constructor
113
+ # and when list data changed, so only put relevant resets here.
114
+ def init_vars
115
+ @repaint_required = true
116
+ @toprow = @pcol = 0
117
+ if @show_selector
118
+ @row_selected_symbol ||= '>'
119
+ @row_unselected_symbol ||= ' '
120
+ @left_margin ||= @row_selected_symbol.length
121
+ end
122
+ #@left_margin ||= 0
123
+ @one_key_selection = false if @one_key_selection.nil?
124
+ @row_offset = @col_offset = 0 if @suppress_borders
125
+
126
+ end
127
+ def map_keys
128
+ return if @keys_mapped
129
+ bind_key(?f){ ask_selection_for_char() }
130
+ bind_key(?\M-v){ @one_key_selection = false }
131
+ bind_key(?j){ next_row() }
132
+ bind_key(?k){ previous_row() }
133
+ bind_key(?\C-d){ scroll_forward() }
134
+ bind_key(?\C-b){ scroll_backward() }
135
+ bind_key(?G){ goto_bottom() }
136
+ bind_key([?g,?g]){ goto_top() }
137
+ bind_key([?',?']){ goto_last_position() }
138
+ bind_key(?/){ ask_search() }
139
+ bind_key(?n){ find_more() }
140
+ bind_key(32){ toggle_row_selection() }
141
+ bind_key(10){ fire_action_event }
142
+ bind_key(13){ fire_action_event }
143
+ @keys_mapped = true
144
+
145
+ end
146
+
147
+ ## returns count of row, needed by scrollbar and others.
148
+ def row_count
149
+ return 0 if @list.nil?
150
+ @list.length
151
+ end
152
+ # start scrolling when user reaches this row
153
+ def scrollatrow #:nodoc:
154
+ if @suppress_borders
155
+ return @height - 1
156
+ else
157
+ return @height - 3
158
+ end
159
+ end
160
+ # provide data to List in the form of an Array or Variable or
161
+ # ListDataModel. This will create a default ListSelectionModel.
162
+ #
163
+ # CHANGE as on 2010-09-21 12:53:
164
+ # If explicit nil passed then dummy datamodel and selection model created
165
+ # From now on, constructor will call this, so this can always
166
+ # happen.
167
+ #
168
+ # NOTE: sometimes this can be added much after its painted.
169
+ # Do not expect this to be called from constructor, although that
170
+ # is the usual case. it can be dependent on some other list or tree.
171
+ # @param [Array, Variable, ListDataModel] data to populate list with
172
+ # @return [ListDataModel] just created or assigned
173
+
174
+ def list *val
175
+ return @list if val.empty?
176
+ alist = val[0]
177
+ case alist
178
+ when Array
179
+ @list = alist
180
+ @current_index = 0
181
+ when NilClass
182
+ @list = [] # or nil ?
183
+ when Variable
184
+ @list = alist.value
185
+ else
186
+ raise ArgumentError, "Listbox list(): do not know how to handle #{alist.class} "
187
+ end
188
+ # added on 2009-01-13 23:19 since updates are not automatic now
189
+ #create_default_list_selection_model
190
+ #@list_selection_model.selection_mode = @tmp_selection_mode if @tmp_selection_mode
191
+ @repaint_required = true
192
+ @list
193
+ end
194
+ # get element at
195
+ # @param [Fixnum] index for element
196
+ # @return [Object] element
197
+ # @since 1.2.0 2010-09-06 14:33 making life easier for others.
198
+ def [](off0)
199
+ @list[off0]
200
+ end
201
+ # return object under cursor
202
+ # Note: this should not be confused with selected row/s. User may not have selected this.
203
+ # This is only useful since in some demos we like to change a status bar as a user scrolls down
204
+ # @since 1.2.0 2010-09-06 14:33 making life easier for others.
205
+ def current_value
206
+ @list[@current_index]
207
+ end
208
+ def remove_all
209
+ return if @list.nil? || @list.empty?
210
+ @list = []
211
+ init_vars
212
+ end
213
+ # avoid using "row", i'd rather stick with "index" and "value".
214
+ alias :current_row :current_value
215
+ alias :text :current_value # thanks to shoes, not sure how this will impact since widget has text.
216
+
217
+ def print_borders #:nodoc:
218
+ width = @width
219
+ height = @height-1 # 2010-01-04 15:30 BUFFERED HEIGHT
220
+ window = @graphic # 2010-01-04 12:37 BUFFERED
221
+ startcol = @col
222
+ startrow = @row
223
+ #@color_pair = get_color($datacolor)
224
+ bordercolor = @border_color || $datacolor
225
+ borderatt = @border_attrib || Ncurses::A_NORMAL
226
+
227
+ window.print_border startrow, startcol, height, width, bordercolor, borderatt
228
+ print_title
229
+ end
230
+ def print_title #:nodoc:
231
+ @color_pair ||= get_color($datacolor)
232
+ # TODO check title.length and truncate if exceeds width
233
+ @graphic.printstring( @row, @col+(@width-@title.length)/2, @title, @color_pair, @title_attrib) unless @title.nil?
234
+ end
235
+ ### START FOR scrollable ###
236
+ def get_content
237
+ @list
238
+ #@list_variable && @list_variable.value || @list
239
+ end
240
+ def get_window #:nodoc:
241
+ @graphic
242
+ end
243
+ ### END FOR scrollable ###
244
+ # override widgets text
245
+ # returns indices of selected rows
246
+ def getvalue
247
+ selected_rows
248
+ end
249
+ # Listbox
250
+ def handle_key(ch) #:nodoc:
251
+ map_keys unless @keys_mapped
252
+ @current_index ||= 0
253
+ @toprow ||= 0
254
+ h = scrollatrow()
255
+ rc = row_count
256
+ $log.debug " basiclistbox got ch #{ch}"
257
+ #$log.debug " when kps #{@KEY_PREV_SELECTION} "
258
+ case ch
259
+ when KEY_UP # show previous value
260
+ return previous_row
261
+ when KEY_DOWN # show previous value
262
+ return next_row
263
+ when 32
264
+ return if is_popup and @selection_mode == 'single' # not allowing select this way since there will be a difference
265
+ toggle_row_selection @current_index #, @current_index
266
+ @repaint_required = true
267
+ when 0 # c-space
268
+ add_to_selection
269
+ when @KEY_NEXT_SELECTION # ?'
270
+ $log.debug "insdie next selection"
271
+ @oldrow = @current_index
272
+ do_next_selection
273
+ bounds_check
274
+ when @KEY_PREV_SELECTION # ?"
275
+ @oldrow = @current_index
276
+ $log.debug "insdie prev selection"
277
+ do_prev_selection
278
+ bounds_check
279
+ when @KEY_CLEAR_SELECTION
280
+ clear_selection
281
+ @repaint_required = true
282
+ when 27, ?\C-c.getbyte(0)
283
+ #editing_canceled @current_index if @cell_editing_allowed
284
+ cancel_block # block
285
+ $multiplier = 0
286
+ when @KEY_ASK_FIND_FORWARD
287
+ # ask_search_forward
288
+ when @KEY_ASK_FIND_BACKWARD
289
+ # ask_search_backward
290
+ when @KEY_FIND_NEXT
291
+ # find_next
292
+ when @KEY_FIND_PREV
293
+ # find_prev
294
+ when @KEY_ASK_FIND
295
+ ask_search
296
+ when @KEY_FIND_MORE
297
+ find_more
298
+ when @KEY_BLOCK_SELECTOR
299
+ mark_block #selection
300
+ #when ?\C-u.getbyte(0)
301
+ # multiplier. Series is 4 16 64
302
+ # TESTING @multiplier = (@multiplier == 0 ? 4 : @multiplier *= 4)
303
+ # return 0
304
+ when ?\C-c.getbyte(0)
305
+ @multiplier = 0
306
+ return 0
307
+ else
308
+ # this has to be fixed, if compo does not handle key it has to continue into next part FIXME
309
+ ret = :UNHANDLED # changed on 2009-01-27 13:14 not going into unhandled, tab not released
310
+ if ret == :UNHANDLED
311
+ # beware one-key eats up numbers. we'll be wondering why
312
+ if @one_key_selection
313
+ case ch
314
+ #when ?A.getbyte(0)..?Z.getbyte(0), ?a.getbyte(0)..?z.getbyte(0), ?0.getbyte(0)..?9.getbyte(0)
315
+ when ?A.getbyte(0)..?Z.getbyte(0), ?a.getbyte(0)..?z.getbyte(0)
316
+ # simple motion, key press defines motion
317
+ ret = set_selection_for_char ch.chr
318
+ else
319
+ ret = process_key ch, self
320
+ @multiplier = 0
321
+ return :UNHANDLED if ret == :UNHANDLED
322
+ end
323
+ else
324
+ # no motion on single key, we can freak out like in vim, pref f <char> for set_selection
325
+ case ch
326
+ when ?0.getbyte(0)..?9.getbyte(0)
327
+ $multiplier *= 10 ; $multiplier += (ch-48)
328
+ #$log.debug " setting mult to #{$multiplier} in list "
329
+ return 0
330
+ end
331
+ ret = process_key ch, self
332
+ return :UNHANDLED if ret == :UNHANDLED
333
+ end
334
+ end
335
+ end
336
+ $multiplier = 0
337
+ end
338
+ def fire_action_event
339
+ require 'rbcurse/ractionevent'
340
+ # should have been callled :ACTION_EVENT !!!
341
+ fire_handler :PRESS, ActionEvent.new(self, :PRESS, text)
342
+ end
343
+ # get a keystroke from user and go to first item starting with that key
344
+ def ask_selection_for_char
345
+ ch = @graphic.getch
346
+ if ch < 0 || ch > 255
347
+ return :UNHANDLED
348
+ end
349
+ ret = set_selection_for_char ch.chr
350
+ end
351
+ def ask_search_forward
352
+ regex = get_string("Enter regex to search")
353
+ ix = @list.find_match regex
354
+ if ix.nil?
355
+ alert("No matching data for: #{regex}")
356
+ else
357
+ set_focus_on(ix)
358
+ end
359
+ end
360
+ # gets string to search and calls data models find prev
361
+ def ask_search_backward
362
+ regex = get_string("Enter regex to search (backward)")
363
+ @last_regex = regex
364
+ ix = @list.find_prev regex, @current_index
365
+ if ix.nil?
366
+ alert("No matching data for: #{regex}")
367
+ else
368
+ set_focus_on(ix)
369
+ end
370
+ end
371
+ # please check for error before proceeding
372
+ # @return [Boolean] false if no data
373
+ def on_enter
374
+ if @list.nil? || @list.size == 0
375
+ Ncurses.beep
376
+ return :UNHANDLED
377
+ end
378
+ on_enter_row @current_index
379
+ set_form_row # added 2009-01-11 23:41
380
+ true
381
+ end
382
+ def on_enter_row arow
383
+ fire_handler :ENTER_ROW, self
384
+ @repaint_required = true
385
+ end
386
+ def on_leave_row arow
387
+ fire_handler :LEAVE_ROW, self
388
+ end
389
+ # getter and setter for cell_renderer
390
+ def cell_renderer(*val)
391
+ if val.empty?
392
+ @cell_renderer ||= create_default_cell_renderer
393
+ else
394
+ @cell_renderer = val[0]
395
+ end
396
+ end
397
+ def create_default_cell_renderer
398
+ return ListCellRenderer.new "", {"color"=>@color, "bgcolor"=>@bgcolor, "parent" => self, "display_length"=> @width-2-@left_margin}
399
+ #return BasicListCellRenderer.new "", {"color"=>@color, "bgcolor"=>@bgcolor, "parent" => self, "display_length"=> @width-2-@left_margin}
400
+ end
401
+ ##
402
+ # this method chops the data to length before giving it to the
403
+ # renderer, this can cause problems if the renderer does some
404
+ # processing. also, it pans the data horizontally giving the renderer
405
+ # a section of it.
406
+ def repaint #:nodoc:
407
+ #safe_create_buffer # 2010-01-04 12:36 BUFFERED moved here 2010-01-05 18:07
408
+ return unless @repaint_required
409
+ # not sure where to put this, once for all or repeat 2010-02-17 23:07 RFED16
410
+ my_win = @form ? @form.window : @target_window
411
+ @graphic = my_win unless @graphic
412
+ raise " #{@name} neither form, nor target window given LB paint " unless my_win
413
+ raise " #{@name} NO GRAPHIC set as yet LB paint " unless @graphic
414
+ @win_left = my_win.left
415
+ @win_top = my_win.top
416
+ @left_margin ||= @row_selected_symbol.length
417
+
418
+ $log.debug "basicrlistbox repaint #{@name} graphic #{@graphic}"
419
+ #$log.debug "XXX repaint to_print #{@to_print_borders} "
420
+ print_borders unless @suppress_borders # do this once only, unless everything changes
421
+ #maxlen = @maxlen ||= @width-2
422
+ tm = list()
423
+ rc = row_count
424
+ @longest_line = @width
425
+ $log.debug " rlistbox #{row_count} "
426
+ if rc > 0 # just added in case no data passed
427
+ tr = @toprow
428
+ acolor = get_color $datacolor
429
+ h = scrollatrow()
430
+ r,c = rowcol
431
+ 0.upto(h) do |hh|
432
+ crow = tr+hh
433
+ if crow < rc
434
+ _focussed = @current_index == crow ? true : false # row focussed ?
435
+ focus_type = _focussed
436
+ focus_type = :SOFT_FOCUS if _focussed && !@focussed
437
+ selected = is_row_selected crow
438
+ content = tm[crow] # 2009-01-17 18:37 chomp giving error in some cases says frozen
439
+ content = convert_value_to_text content, crow # 2010-09-23 20:12
440
+ # by now it has to be a String
441
+ if content.is_a? String
442
+ content = content.dup
443
+ sanitize content if @sanitization_required
444
+ truncate content if @truncation_required
445
+ end
446
+ ## set the selector symbol if requested
447
+ selection_symbol = ''
448
+ if @show_selector
449
+ if selected
450
+ selection_symbol = @row_selected_symbol
451
+ else
452
+ selection_symbol = @row_unselected_symbol
453
+ end
454
+ @graphic.printstring r+hh, c, selection_symbol, acolor,@attr
455
+ end
456
+ #renderer = get_default_cell_renderer_for_class content.class.to_s
457
+ renderer = cell_renderer()
458
+ renderer.repaint @graphic, r+hh, c+@left_margin, crow, content, focus_type, selected
459
+ else
460
+ # clear rows
461
+ @graphic.printstring r+hh, c, " " * (@width-2), acolor,@attr
462
+ end
463
+ end
464
+ end # rc == 0
465
+ #@table_changed = false
466
+ @repaint_required = false
467
+ #@buffer_modified = true # required by form to call buffer_to_screen BUFFERED
468
+ #buffer_to_window # RFED16 2010-02-17 23:16
469
+ end
470
+ # the idea here is to allow users who subclass Listbox to easily override parts of the cumbersome repaint
471
+ # method. This assumes your List has some data, but you print a lot more. Now you don't need to
472
+ # change the data in the renderer, or keep formatted data in the list itself.
473
+ # e.g. @list contains file names, or File objects, and this converts to a long listing.
474
+ # If the renderer did that, the truncation would be on wrong data.
475
+ # @since 1.2.0
476
+ def convert_value_to_text value, crow
477
+ case value
478
+ when TrueClass, FalseClass
479
+ value
480
+ else
481
+ value.to_s if value
482
+ end
483
+ end
484
+ # takes a block, this way anyone extending this class can just pass a block to do his job
485
+ # This modifies the string
486
+ def sanitize content #:nodoc:
487
+ if content.is_a? String
488
+ content.chomp!
489
+ content.gsub!(/\t/, ' ') # don't display tab
490
+ content.gsub!(/[^[:print:]]/, '') # don't display non print characters
491
+ else
492
+ content
493
+ end
494
+ end
495
+ # returns only the visible portion of string taking into account display length
496
+ # and horizontal scrolling. MODIFIES STRING
497
+ def truncate content # :nodoc:
498
+ maxlen = @maxlen ||= @width-2
499
+ if !content.nil?
500
+ if content.length > maxlen # only show maxlen
501
+ @longest_line = content.length if content.length > @longest_line
502
+ #content = content[@pcol..@pcol+maxlen-1]
503
+ content.replace content[@pcol..@pcol+maxlen-1]
504
+ else
505
+ # can this be avoided if pcol is 0 XXX
506
+ content.replace content[@pcol..-1] if @pcol > 0
507
+ end
508
+ end
509
+ content
510
+ end
511
+
512
+ # be informed when data has changed. required here, was being called by listdatamodel earlier
513
+ def list_data_changed
514
+ if row_count == 0 # added on 2009-02-02 17:13 so cursor not hanging on last row which could be empty
515
+ init_vars
516
+ @current_index = 0
517
+ set_form_row
518
+ end
519
+ @repaint_required = true
520
+ end
521
+
522
+ # set cursor column position
523
+ # if i set col1 to @curpos, i can move around left right if key mapped
524
+ def set_form_col col1=0 #:nodoc:
525
+ @cols_panned ||= 0
526
+ # editable listboxes will involve changing cursor and the form issue
527
+ ## added win_col on 2010-01-04 23:28 for embedded forms BUFFERED TRYING OUT
528
+ #win_col=@form.window.left
529
+ win_col = 0
530
+ col2 = win_col + @col + @col_offset + col1 + @cols_panned + @left_margin
531
+ $log.debug " set_form_col in rlistbox #{@col}+ left_margin #{@left_margin} ( #{col2} ) "
532
+ setrowcol nil, col2
533
+ end
534
+
535
+ # @group selection related
536
+
537
+ # change selection of current row on pressing space bar
538
+ # If mode is multiple, then other selections are cleared and this is added
539
+ def toggle_row_selection crow=@current_index
540
+ @repaint_required = true
541
+ case @selection_mode
542
+ when :multiple
543
+ clear_selection
544
+ @selected_indices[0] = crow #@current_index
545
+ else
546
+ if @selected_index == crow #@current_index
547
+ @selected_index = nil
548
+ else
549
+ @selected_index = crow #@current_index
550
+ end
551
+ end
552
+ end
553
+ #
554
+ # Only for multiple mode.
555
+ # add an item to selection, if selection mode is multiple
556
+ # if item already selected, it is deselected, else selected
557
+ # typically bound to Ctrl-Space
558
+ def add_to_selection
559
+ case @selection_mode
560
+ when :multiple
561
+ if @selected_indices.include? @current_index
562
+ @selected_indices.delete @current_index
563
+ else
564
+ @selected_indices << @current_index
565
+ end
566
+ else
567
+ end
568
+ @repaint_required = true
569
+ end
570
+ # clears selected indices
571
+ def clear_selection
572
+ @selected_indices = []
573
+ @repaint_required = true
574
+ end
575
+ def is_row_selected crow=@current_index
576
+ case @selection_mode
577
+ when :multiple
578
+ @selected_indices.include? crow
579
+ else
580
+ crow == @selected_index
581
+ end
582
+ end
583
+ alias :is_selected? is_row_selected
584
+ def goto_next_selection
585
+ return if selected_rows().length == 0
586
+ row = selected_rows().sort.find { |i| i > @current_index }
587
+ row ||= @current_index
588
+ @current_index = row
589
+ @repaint_required = true # fire list_select XXX
590
+ end
591
+ def goto_prev_selection
592
+ return if selected_rows().length == 0
593
+ row = selected_rows().sort{|a,b| b <=> a}.find { |i| i < @current_index }
594
+ row ||= @current_index
595
+ @current_index = row
596
+ @repaint_required = true # fire list_select XXX
597
+ end
598
+
599
+
600
+
601
+ # ADD HERE
602
+ end # class listb
603
+
604
+ ##
605
+ # This is a basic list cell renderer that will render the to_s value of anything.
606
+ # Using alignment one can use for numbers too.
607
+ # However, for booleans it will print true and false. If editing, you may want checkboxes
608
+ # I've copied this into ListCellRenderer and added justify, so use that.
609
+ class BasicListCellRenderer
610
+ include RubyCurses::ConfigSetup
611
+ include RubyCurses::Utils
612
+ #dsl_accessor :justify # :right, :left, :center # added 2008-12-22 19:02
613
+ dsl_accessor :display_length # please give this to ensure the we only print this much
614
+ dsl_accessor :height # if you want a multiline label.
615
+ dsl_accessor :text # text of label
616
+ dsl_accessor :color, :bgcolor
617
+ dsl_accessor :row, :col
618
+ dsl_accessor :parent #usuall the table to get colors and other default info
619
+
620
+ def initialize text="", config={}, &block
621
+ @text = text
622
+ @editable = false
623
+ @focusable = false
624
+ config_setup config # @config.each_pair { |k,v| variable_set(k,v) }
625
+ instance_eval &block if block_given?
626
+ init_vars
627
+ end
628
+ def init_vars
629
+ #@justify ||= :left
630
+ #str = @justify.to_sym == :right ? "%*s" : "%-*s" # added 2008-12-22 19:05
631
+ @display_length ||= 10
632
+ # create color pairs once for this 2010-09-26 20:53
633
+ @color_pair = get_color $datacolor
634
+ @pairs = Hash.new(@color_pair)
635
+ @attrs = Hash.new(Ncurses::A_NORMAL)
636
+ color_pair = get_color $selectedcolor, @parent.selected_color, @parent.selected_bgcolor
637
+ @pairs[:normal] = @color_pair
638
+ @pairs[:selected] = color_pair
639
+ @pairs[:focussed] = @pairs[:normal]
640
+ @attrs[:selected] = $row_selected_attr
641
+ @attrs[:focussed] = $row_focussed_attr
642
+
643
+ end
644
+ def getvalue
645
+ @text
646
+ end
647
+ ##
648
+ # sets @color_pair and @attr
649
+ def select_colors focussed, selected
650
+ @color_pair = @pairs[:normal]
651
+ @attr = $row_attr
652
+ # give precedence to a selected row
653
+ if selected
654
+ @color_pair = @pairs[:selected]
655
+ @attr = @attrs[:selected]
656
+ elsif focussed
657
+ @color_pair = @pairs[:focussed]
658
+ @attr = @attrs[:focussed]
659
+ end
660
+ end
661
+
662
+ ##
663
+ # paint a list box cell
664
+ #
665
+ # @param [Buffer] window or buffer object used for printing
666
+ # @param [Fixnum] row
667
+ # @param [Fixnum] column
668
+ # @param [Fixnum] actual index into data, some lists may have actual data elsewhere and
669
+ # display data separate. e.g. rfe_renderer (directory listing)
670
+ # @param [String] text to print in cell
671
+ # @param [Boolean, cell focussed, not focussed
672
+ # @param [Boolean] cell selected or not
673
+ def repaint graphic, r=@row,c=@col, row_index=-1,value=@text, focussed=false, selected=false
674
+
675
+ select_colors focussed, selected
676
+
677
+ value=value.to_s
678
+ if !@display_length.nil?
679
+ if value.length > @display_length
680
+ value = value[0..@display_length-1]
681
+ end
682
+ end
683
+ len = @display_length || value.length
684
+ graphic.printstring r, c, "%-*s" % [len, value], @color_pair, @attr
685
+ end # repaint
686
+ end # class
687
+
688
+ end # module