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,106 @@
1
+ require 'rbcurse/rtextview'
2
+ require 'fileutils'
3
+
4
+ # NOTE: experimental, not yet firmed up
5
+ # If you use in application, please copy to some application folder in case i change this.
6
+ # TODO: add vi_keys here
7
+ # SUGGESTIONS WELCOME.
8
+ # @since 1.2.0
9
+ module RubyCurses
10
+ # a data viewer for viewing some text or filecontents
11
+ # view filename, :close_key => KEY_RETURN
12
+ # send data in an array
13
+ # view Array, :close_key => KEY_RETURN, :layout => [0,0,23,80]
14
+ class Viewer
15
+ # @param filename as string or content as array
16
+ # @yield textview object for further configuration before display
17
+ # NOTE: i am experimentally yielding textview object so i could supress borders
18
+ # just for kicks, but on can also bind_keys or events if one wanted.
19
+ def self.view what, config={} #:yield: textview
20
+ case what
21
+ when String # we have a path
22
+ content = _get_contents(what)
23
+ when Array
24
+ content = what
25
+ else
26
+ raise ArgumentError, "Expecting Filename or Contents (array), but got #{what.class} "
27
+ end
28
+ wt = 0 # top margin
29
+ wl = 0 # left margin
30
+ wh = Ncurses.LINES-wt # height, goes to bottom of screen
31
+ ww = Ncurses.COLS-wl # width, goes to right end
32
+ wt, wl, wh, ww = config[:layout] if config.has_key? :layout
33
+
34
+ fp = config[:title] || ""
35
+ pf = config.fetch(:print_footer, true)
36
+ ta = config.fetch(:title_attrib, 'bold')
37
+ fa = config.fetch(:footer_attrib, 'bold')
38
+
39
+ layout = { :height => wh, :width => ww, :top => wt, :left => wl }
40
+ v_window = VER::Window.new(layout)
41
+ v_form = RubyCurses::Form.new v_window
42
+ textview = TextView.new v_form do
43
+ name "Viewer"
44
+ row 0
45
+ col 0
46
+ width ww
47
+ height wh-2
48
+ title fp
49
+ title_attrib ta
50
+ print_footer pf
51
+ footer_attrib fa
52
+ end
53
+ textview.set_content content #, :WRAP_WORD
54
+
55
+ # yielding textview so you may further configure or bind keys or events
56
+ yield textview if block_given? # tentative
57
+ v_form.repaint
58
+ v_window.wrefresh
59
+ Ncurses::Panel.update_panels
60
+ begin
61
+ while((ch = v_window.getchar()) != ?\C-q.getbyte(0) )
62
+ break if ch == config[:close_key]
63
+ # if you've asked for RETURN then i also check for 10 and 13
64
+ break if (ch == 10 || ch == 13) && config[:close_key] == KEY_RETURN
65
+ v_form.handle_key ch
66
+ v_form.repaint
67
+ end
68
+ ensure
69
+ v_window.destroy if !v_window.nil?
70
+ end
71
+ end
72
+ private
73
+ def self._get_contents fp
74
+ return nil unless File.readable? fp
75
+ return Dir.new(fp).entries if File.directory? fp
76
+ case File.extname(fp)
77
+ when '.tgz','.gz'
78
+ cmd = "tar -ztvf #{fp}"
79
+ content = %x[#{cmd}]
80
+ when '.zip'
81
+ cmd = "unzip -l #{fp}"
82
+ content = %x[#{cmd}]
83
+ else
84
+ content = File.open(fp,"r").readlines
85
+ end
86
+ end
87
+ end # class
88
+ end # module
89
+ if __FILE__ == $PROGRAM_NAME
90
+ require 'rbcurse/app'
91
+
92
+ App.new do
93
+ header = app_header "rbcurse 1.2.0", :text_center => "Viewer Demo", :text_right =>"New Improved!", :color => :black, :bgcolor => :white, :attr => :bold
94
+ message "Press F1 to exit from here"
95
+
96
+ RubyCurses::Viewer.view(ARGV[0] || $0, :close_key => KEY_RETURN, :title => "Enter to close") do |t|
97
+ # you may configure textview further here.
98
+ #t.suppress_borders true
99
+ #t.color = :black
100
+ #t.bgcolor = :white
101
+ # or
102
+ #t.attr = :reverse
103
+ end
104
+
105
+ end # app
106
+ end
data/lib/rbcurse/io.rb CHANGED
@@ -8,6 +8,18 @@
8
8
  # * rbgetstr (and those it calls)
9
9
  # * display_cmenu and create_mitem
10
10
  #*******************************************************#
11
+ ##
12
+ # added RK 2010-11-02 18:11 so can be used in widgets too
13
+ # maybe can be removed from app, if accessible there too.
14
+ require 'forwardable'
15
+ require 'rbcurse/extras/bottomline'
16
+ #$terminal = RubyCurses::Bottomline.new
17
+ #$terminal.name = "$terminal io.rb"
18
+ #module Kernel
19
+ #extend Forwardable
20
+ #def_delegators :$terminal, :agree, :ask, :choose, :say
21
+ #end
22
+ #$tt.window = @window; $tt.message_row = @message_row # <<-- TODO somewhere
11
23
  module Io
12
24
 
13
25
  # from which line to print in footer_win
@@ -20,6 +32,8 @@ module Io
20
32
  # complex version of get_string that allows for trappng of control character
21
33
  # such as C-c and C-h and TAB for completion
22
34
  # validints contains int codes not chars.
35
+ # NOTE: change. C-g was help key in alpine but abort key in emacs. C-c sometimes
36
+ # crashes app, so i am using C-g as abort. Help on M-h
23
37
  # TODO We should put a field there, make it visible and mv it to after the prompt
24
38
  # and handle all editing events on it.
25
39
  # @return status_code, string (0 if okay, 7 if help asked for, -1 for abort
@@ -43,71 +57,73 @@ module Io
43
57
  #x mylabels += labels if !labels.nil?
44
58
  begin
45
59
  Ncurses.echo();
46
- #x print_key_labels( 0, 0, mylabels)
47
- #curpos = 0
48
- curpos = str.length
49
- prevchar = 0
50
- entries = nil
51
- #win.mvwgetnstr(LINEONE-3,askstr.length,yn,maxlen)
52
- while true
53
- #ch=win.mvwgetch(r, len) # get to right of prompt - WHY NOT WORKING ???
54
- ch=win.getchar()
55
- $log.debug " rbgetstr got ch:#{ch}, str:#{str}. "
56
- case ch
57
- when 3 # -1 # C-c
58
- return -1, nil
59
- when 10, 13
60
- break
61
- when ?\C-h.getbyte(0), ?\C-?.getbyte(0), 127 # delete previous character/backspace
62
- len -= 1 if len > prompt.length
63
- curpos -= 1 if curpos > 0
64
- str.slice!(curpos)
65
- clear_this win, r, c, color, len+maxlen+1
66
- #print_this(win, prompt+str, color, r, c)
67
- when 330 # delete character on cursor
68
- #len -= 1 if len > prompt.length
69
- #curpos -= 1 if curpos > 0
70
- str.slice!(curpos) #rescue next
71
- clear_this win, r, c, color, len+maxlen+1
72
- when ?\C-g.getbyte(0)
73
- #x print_footer_help(helptext)
74
- helptext = config[:helptext] || "No help provided"
75
- print_help(win, r, c, color, helptext)
76
- return 7, nil
77
- when KEY_LEFT
78
- curpos -= 1 if curpos > 0
79
- len -= 1 if len > prompt.length
80
- win.wmove r, c+len # since getchar is not going back on del and bs
81
- next
82
- when KEY_RIGHT
83
- if curpos < str.length
84
- curpos += 1 #if curpos < str.length
85
- len += 1
60
+ #x print_key_labels( 0, 0, mylabels)
61
+ #curpos = 0
62
+ curpos = str.length
63
+ prevchar = 0
64
+ entries = nil
65
+ #win.mvwgetnstr(LINEONE-3,askstr.length,yn,maxlen)
66
+ while true
67
+ #ch=win.mvwgetch(r, len) # get to right of prompt - WHY NOT WORKING ???
68
+ ch=win.getchar()
69
+ $log.debug " rbgetstr got ch:#{ch}, str:#{str}. "
70
+ case ch
71
+ when 3 # -1 # C-c
72
+ return -1, nil
73
+ when ?\C-g.getbyte(0) # ABORT
74
+ return -1, nil
75
+ when 10, 13 # hits ENTER
76
+ break
77
+ when ?\C-h.getbyte(0), ?\C-?.getbyte(0), KEY_BSPACE # delete previous character/backspace
78
+ len -= 1 if len > prompt.length
79
+ curpos -= 1 if curpos > 0
80
+ str.slice!(curpos)
81
+ clear_this win, r, c, color, len+maxlen+1
82
+ #print_this(win, prompt+str, color, r, c)
83
+ when 330 # delete character on cursor
84
+ #len -= 1 if len > prompt.length
85
+ #curpos -= 1 if curpos > 0
86
+ str.slice!(curpos) #rescue next
87
+ clear_this win, r, c, color, len+maxlen+1
88
+ when ?\M-h.getbyte(0) # HELP KEY
89
+ #x print_footer_help(helptext)
90
+ helptext = config[:helptext] || "No help provided"
91
+ print_help(win, r, c, color, helptext)
92
+ return 7, nil
93
+ when KEY_LEFT
94
+ curpos -= 1 if curpos > 0
95
+ len -= 1 if len > prompt.length
86
96
  win.wmove r, c+len # since getchar is not going back on del and bs
87
- end
88
- next
89
- when ?\M-i.getbyte(0)
90
- ins_mode = !ins_mode
91
- next
92
- when 9 # TAB
93
- if config
94
- if prevchar == 9
95
- if !entries.nil? and !entries.empty?
96
- str = entries.delete_at(0)
97
+ next
98
+ when KEY_RIGHT
99
+ if curpos < str.length
100
+ curpos += 1 #if curpos < str.length
101
+ len += 1
102
+ win.wmove r, c+len # since getchar is not going back on del and bs
103
+ end
104
+ next
105
+ when ?\M-i.getbyte(0)
106
+ ins_mode = !ins_mode
107
+ next
108
+ when KEY_TAB # TAB
109
+ if config
110
+ if prevchar == 9
111
+ if !entries.nil? and !entries.empty?
112
+ str = entries.delete_at(0)
113
+ end
114
+ else
115
+ tabc = config[:tab_completion] unless tabc
116
+ next unless tabc
117
+ entries = tabc.call(str)
118
+ $log.debug " tab got #{entries} "
119
+ str = entries.delete_at(0) unless entries.nil? or entries.empty?
97
120
  end
98
- else
99
- tabc = config[:tab_completion] unless tabc
100
- next unless tabc
101
- entries = tabc.call(str)
102
- $log.debug " tab got #{entries} "
103
- str = entries.delete_at(0) unless entries.nil? or entries.empty?
104
121
  end
105
- end
106
- else
107
- #if validints.include?ch
122
+ else
123
+ #if validints.include?ch
108
124
  #print_status("Found in validints")
109
125
  #return ch, nil
110
- #else
126
+ #else
111
127
  if ch < 0 || ch > 255
112
128
  Ncurses.beep
113
129
  next
@@ -117,23 +133,23 @@ module Io
117
133
  Ncurses.beep
118
134
  next
119
135
  end
120
- # we need to trap KEY_LEFT and RIGHT and what of UP for history ?
121
- #end
122
- #str << ch.chr
136
+ # we need to trap KEY_LEFT and RIGHT and what of UP for history ?
137
+ #end
138
+ #str << ch.chr
123
139
  if ins_mode
124
140
  str[curpos] = ch.chr
125
141
  else
126
142
  str.insert(curpos, ch.chr)
127
143
  end
128
144
  len += 1
129
- curpos += 1
130
- break if str.length > maxlen
145
+ curpos += 1
146
+ break if str.length > maxlen
147
+ end
148
+ print_this(win, prompt+str, color, r, c)
149
+ win.wmove r, c+len # more for arrow keys, curpos may not be end
150
+ prevchar = ch
131
151
  end
132
- print_this(win, prompt+str, color, r, c)
133
- win.wmove r, c+len # more for arrow keys, curpos may not be end
134
- prevchar = ch
135
- end
136
- str = default if str == ""
152
+ str = default if str == ""
137
153
  ensure
138
154
  Ncurses.noecho();
139
155
  #x restore_application_key_labels # must be done after using print_key_labels
@@ -290,14 +306,14 @@ module Io
290
306
  # prints given text to window, in color at x and y coordinates
291
307
  # @param [Window] window to write to
292
308
  # @param [String] text to print
293
- # @param [int] color such as $datacolor or $promptcolor
294
- # @param [int] x
295
- # @param [int] y
309
+ # @param [int] color pair such as $datacolor or $promptcolor
310
+ # @param [int] x row
311
+ # @param [int] y col
296
312
  # @see Window#printstring
297
313
  # Consider using Window#printstring
298
314
  def print_this(win, text, color, x, y)
299
- if(win == nil)
300
- raise "win nil in printthis"
315
+ if !win
316
+ raise "win nil in print_this"
301
317
  end
302
318
  #$log.debug " printthis #{win} , #{text} , #{x} , #{y} "
303
319
  color=Ncurses.COLOR_PAIR(color);
@@ -550,6 +566,17 @@ module Io
550
566
  return yn == 'y'
551
567
  end
552
568
 
569
+ #
570
+ # warn user: currently flashes and places error in log file
571
+ # experimental, may change interface later
572
+ # it does not say anything on screen
573
+ # @param [String] text of error/warning to put in log
574
+ # @since 1.1.5
575
+ def warn string
576
+ $log.warn string
577
+ Ncurses.beep
578
+ end
579
+
553
580
  #def add_item hotkey, label, desc,action
554
581
  #
555
582
  ## A *simple* way of creating menus that will appear in a single row.
@@ -589,12 +616,36 @@ module Io
589
616
  @text = text
590
617
  @options = []
591
618
  end
592
- def add menuitem
593
- @options << menuitem
619
+ def add *menuitem
620
+ item = nil
621
+ case menuitem.first
622
+ when CMenuItem
623
+ item = menuitem.first
624
+ @options << item
625
+ else
626
+ item = CMenuItem.new(*menuitem.flatten)
627
+ @options << item
628
+ end
629
+ return item
594
630
  end
595
631
  def create_mitem *args
596
632
  item = CMenuItem.new(*args.flatten)
597
633
  end
634
+ # create the whole thing using a MenuTree which has minimal information.
635
+ # It uses a hotkey and a code only. We are supposed to resolve the display text
636
+ # and actual proc from the caller using this code.
637
+ def menu_tree mt, pm = self
638
+ mt.each_pair { |ch, code|
639
+ if code.is_a? RubyCurses::MenuTree
640
+ item = pm.add(ch, code.value, "")
641
+ current = PromptMenu.new @caller, code.value
642
+ item.action = current
643
+ menu_tree code, current
644
+ else
645
+ item = pm.add(ch, code.to_s, "", code)
646
+ end
647
+ }
648
+ end
598
649
  # Display the top level menu and accept user input
599
650
  # Calls actions or symbols upon selection, or traverses submenus
600
651
  # @return retvalue of last call or send, or 0
@@ -611,9 +662,10 @@ module Io
611
662
  h = {}
612
663
  valid = []
613
664
  menu.each{ |item|
614
- str << "(%c) %s " % [ item.hotkey, item.label ]
615
- h[item.hotkey] = item
616
- valid << item.hotkey
665
+ hk = item.hotkey.to_s
666
+ str << "(%c) %s " % [ hk, item.label ]
667
+ h[hk] = item
668
+ valid << hk
617
669
  }
618
670
  #$log.debug " valid are #{valid} "
619
671
  color = $datacolor
@@ -621,6 +673,11 @@ module Io
621
673
  ch=win.getchar()
622
674
  #$log.debug " got ch #{ch} "
623
675
  next if ch < 0 or ch > 255
676
+ if ch == 3 || ch == ?\C-g.getbyte(0)
677
+ clear_this win, r, c, color, str.length
678
+ print_this(win, "Aborted.", color, r,c)
679
+ break
680
+ end
624
681
  ch = ch.chr
625
682
  index = valid.index ch
626
683
  if index.nil?
@@ -116,11 +116,14 @@ module RubyCurses
116
116
  ##
117
117
  # updates existing label with a new one.
118
118
  # @return true if updated, else false
119
+ # @example update "C-x", "C-x", "Disable"
119
120
  def update_application_key_label(display_code, new_display_code, text)
120
121
  @repaint_required = true
121
122
  labels = key_labels()
123
+ raise "labels are nil !!!" unless labels
122
124
  labels.each_index do |ix|
123
125
  lab = labels[ix]
126
+ next if lab.nil?
124
127
  if lab[0] == display_code
125
128
  labels[ix] = [new_display_code , text]
126
129
  $log.debug("updated #{labels[ix]}")
@@ -129,6 +132,7 @@ module RubyCurses
129
132
  end
130
133
  return false
131
134
  end
135
+ alias :update :update_application_key_label
132
136
  ##
133
137
  # inserts an application label at given index
134
138
  # to add the key, use create_datakeys to add bindings
@@ -1,16 +1,15 @@
1
- require 'rubygems'
2
- require 'ncurses'
3
- require 'logger'
4
1
  require 'rbcurse/rwidget'
5
2
  module RubyCurses
6
3
 
7
4
  ##
5
+ # 2010-09-27 11:06 : i have modified this quite a bit, to calculate some stuff
6
+ # once in the init, to reduce work in repaint
8
7
  # This is a basic list cell renderer that will render the to_s value of anything.
9
8
  # Using alignment one can use for numbers too.
10
9
  # However, for booleans it will print true and false. If editing, you may want checkboxes
10
+ # NOTE: this class is being extended by many other classes. Careful while making
11
+ # sweeping changes.
11
12
  class ListCellRenderer
12
- #include DSL
13
- #include EventHandler
14
13
  include RubyCurses::ConfigSetup
15
14
  include RubyCurses::Utils
16
15
  dsl_accessor :justify # :right, :left, :center # added 2008-12-22 19:02
@@ -29,20 +28,93 @@ module RubyCurses
29
28
  instance_eval &block if block_given?
30
29
  init_vars
31
30
  end
32
- def init_vars
33
- @justify ||= :left
31
+ # NOTE: please call super() if you override this
32
+ def init_vars #:nodoc:
33
+ # omg, some classes won't have justify !!
34
+ #@justify ||= (@parent.justify || :left)
35
+ unless @justify
36
+ if @parent.respond_to? :justify
37
+ @justify ||= (@parent.justify || :left)
38
+ else
39
+ @justify ||= :left
40
+ end
41
+ end
42
+ @format = @justify.to_sym == :right ? "%*s" : "%-*s"
34
43
  @display_length ||= 10
44
+ # create color pairs once for this 2010-09-26 20:53
45
+ end
46
+ # creates pairs of colors at start
47
+ # since often classes are overriding init_vars, so not gettin created
48
+ def create_color_pairs
49
+ @color_pair = get_color $datacolor
50
+ @pairs = Hash.new(@color_pair)
51
+ @attrs = Hash.new(Ncurses::A_NORMAL)
52
+ color_pair = get_color $selectedcolor, @parent.selected_color, @parent.selected_bgcolor
53
+ @pairs[:normal] = @color_pair
54
+ @pairs[:selected] = color_pair
55
+ @pairs[:focussed] = @pairs[:normal]
56
+ @attrs[:selected] = $row_selected_attr
57
+ @attrs[:focussed] = $row_focussed_attr
58
+
35
59
  end
36
60
  def getvalue
37
61
  @text
38
62
  end
39
63
  ##
40
64
  # sets @color_pair and @attr
65
+ def select_colors focussed, selected
66
+ create_color_pairs unless @pairs
67
+ raise ArgumentError, "pairs hash is null. Changes have happened in listcellrenderer" unless @pairs
68
+ @color_pair = @pairs[:normal]
69
+ @attr = $row_attr
70
+ # give precedence to a selected row
71
+ if selected
72
+ @color_pair = @pairs[:selected]
73
+ @attr = @attrs[:selected]
74
+ elsif focussed
75
+ @color_pair = @pairs[:focussed]
76
+ @attr = @attrs[:focussed]
77
+ end
78
+ end
79
+
80
+ ##
81
+ # paint a list box cell
82
+ #
83
+ # @param [Buffer] window or buffer object used for printing
84
+ # @param [Fixnum] row
85
+ # @param [Fixnum] column
86
+ # @param [Fixnum] actual index into data, some lists may have actual data elsewhere and
87
+ # display data separate. e.g. rfe_renderer (directory listing)
88
+ # @param [String] text to print in cell
89
+ # @param [Boolean, cell focussed, not focussed
90
+ # @param [Boolean] cell selected or not
91
+ def repaint graphic, r=@row,c=@col, row_index=-1,value=@text, focussed=false, selected=false
92
+
93
+ select_colors focussed, selected
94
+ # if listboxes width is reduced, display_len remains the same
95
+ # XXX FIXME parent may not be the list but a container like rfe !!
96
+ # maybe caller should update at start of repain loop.
97
+ #@display_length = @parent.width - 2 - @parent.left_margin
98
+
99
+ value=value.to_s
100
+ if !@display_length.nil?
101
+ if value.length > @display_length
102
+ value = value[0..@display_length-1]
103
+ end
104
+ # added 2010-09-27 11:05 TO UNCOMMENT AND TEST IT OUT
105
+ if @justify == :center
106
+ value = value.center(@display_length)
107
+ end
108
+ end
109
+ len = @display_length || value.length
110
+ graphic.printstring r, c, @format % [len, value], @color_pair, @attr
111
+ end # repaint
112
+
113
+ # @deprecated
114
+ # only for older code that may have extended this.
41
115
  def prepare_default_colors focussed, selected
42
116
  @color_pair = get_color $datacolor
43
- #acolor =get_color $datacolor, @color || @parent.color, @bgcolor || @parent.bgcolor #unless @parent.nil?
44
117
  @attr = @row_attr || Ncurses::A_NORMAL
45
- ##@row_attr = Ncurses::A_NORMAL # added 2009-02-04 18:35 since overriding in rfe
46
118
 
47
119
 
48
120
  ## determine bg and fg and attr
@@ -50,57 +122,17 @@ module RubyCurses
50
122
  @attr = Ncurses::A_BOLD if selected
51
123
  @color_pair =get_color $selectedcolor, @parent.selected_color, @parent.selected_bgcolor unless @parent.nil?
52
124
  end
53
- if focussed
125
+ case focussed
126
+ when :SOFT_FOCUS
127
+ @attr |= Ncurses::A_BOLD
128
+ when true
54
129
  @attr |= Ncurses::A_REVERSE
130
+ when false
55
131
  end
132
+ #if focussed
133
+ #@attr |= Ncurses::A_REVERSE
134
+ #end
56
135
  end
136
+ end # class
57
137
 
58
- ##
59
- #
60
- def repaint graphic, r=@row,c=@col, row_index=-1,value=@text, focussed=false, selected=false
61
- #$log.debug "label :#{@text}, #{value}, #{r}, #{c} col= #{@color}, #{@bgcolor} acolor= #{acolor} j:#{@justify} dlL: #{@display_length} "
62
-
63
- prepare_default_colors focussed, selected
64
-
65
- lablist = []
66
- value=value.to_s # ??
67
- if @height && @height > 1
68
- lablist = wrap_text(value, @display_length).split("\n")
69
- else
70
- # ensure we do not exceed
71
- if !@display_length.nil?
72
- if value.length > @display_length
73
- value = value[0..@display_length-1]
74
- end
75
- end
76
- lablist << value
77
- end
78
- len = @display_length || value.length
79
- _height = @height || 1
80
- str = @justify.to_sym == :right ? "%*s" : "%-*s" # added 2008-12-22 19:05
81
- # loop added for labels that are wrapped.
82
- # clear separately since value can change in status like labels
83
- #len -= @left_margin
84
- 0.upto(_height-1) { |i|
85
- graphic.printstring r+i, c, ( " " * len) , @color_pair,@attr
86
- }
87
- lablist.each_with_index do |_value, ix|
88
- break if ix >= _height
89
- if @justify.to_sym == :center
90
- padding = (@display_length - _value.length)/2
91
- _value = " "*padding + _value + " "*padding # so its cleared if we change it midway
92
- end
93
- graphic.printstring r, c, str % [len, _value], @color_pair,@attr
94
- r += 1
95
- end
96
- =begin
97
- if @parent.search_found_ix == row_index
98
- if !@parent.find_offset.nil?
99
- graphic.mvchgat(y=r, x=@parent.find_offset, @parent.find_offset1, Ncurses::A_NORMAL, $reversecolor, nil)
100
- end
101
- end
102
- =end
103
- end
104
- # ADD HERE
105
- end
106
- end
138
+ end # module