Class: RubyCurses::ListCellRenderer

Inherits:
Object
  • Object
show all
Includes:
ConfigSetup, Utils
Defined in:
lib/rbcurse/listcellrenderer.rb

Overview

2010-09-27 11:06 : i have modified this quite a bit, to calculate some stuff

once in the init, to reduce work in repaint

This is a basic list cell renderer that will render the to_s value of anything. Using alignment one can use for numbers too. However, for booleans it will print true and false. If editing, you may want checkboxes NOTE: this class is being extended by many other classes. Careful while making sweeping changes.

Since:

  • 1.2.0 UNTESTED

Instance Method Summary collapse

Methods included from Utils

#_process_key, #bind_key, #clean_string!, #get_color, #keycode_tos, #repeatm, #view, #wrap_text

Methods included from ConfigSetup

#cget, #config_setup, #configure, #variable_set

Constructor Details

#initialize(text = "", config = {}, &block) ⇒ ListCellRenderer

Returns a new instance of ListCellRenderer.

Since:

  • 1.2.0 UNTESTED



23
24
25
26
27
28
29
30
# File 'lib/rbcurse/listcellrenderer.rb', line 23

def initialize text="", config={}, &block
  @text = text
  @editable = false
  @focusable = false
  config_setup config # @config.each_pair { |k,v| variable_set(k,v) }
  instance_eval &block if block_given?
  init_vars
end

Instance Method Details

#create_color_pairsObject

creates pairs of colors at start since often classes are overriding init_vars, so not gettin created

Since:

  • 1.2.0 UNTESTED



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rbcurse/listcellrenderer.rb', line 48

def create_color_pairs
  @color_pair = get_color $datacolor
  @pairs = Hash.new(@color_pair)
  @attrs = Hash.new(Ncurses::A_NORMAL)
  color_pair = get_color $selectedcolor, @parent.selected_color, @parent.selected_bgcolor
  @pairs[:normal] = @color_pair
  @pairs[:selected] = color_pair
  @pairs[:focussed] = @pairs[:normal]
  @attrs[:selected] = $row_selected_attr
  @attrs[:focussed] = $row_focussed_attr

end

#getvalueObject

Since:

  • 1.2.0 UNTESTED



60
61
62
# File 'lib/rbcurse/listcellrenderer.rb', line 60

def getvalue
  @text
end

#init_varsObject

NOTE: please call super() if you override this

Since:

  • 1.2.0 UNTESTED



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rbcurse/listcellrenderer.rb', line 32

def init_vars  #:nodoc:
  # omg, some classes won't have justify !!
  #@justify ||= (@parent.justify || :left)
  unless @justify
    if @parent.respond_to? :justify
      @justify ||= (@parent.justify || :left)
    else
      @justify ||= :left
    end
  end
  @format = @justify.to_sym == :right ? "%*s" : "%-*s"  
  @display_length ||= 10
  # create color pairs once for this 2010-09-26 20:53 
end

#prepare_default_colors(focussed, selected) ⇒ Object

Deprecated.

only for older code that may have extended this.

Since:

  • 1.2.0 UNTESTED



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rbcurse/listcellrenderer.rb', line 116

def prepare_default_colors focussed, selected
    @color_pair = get_color $datacolor
    @attr = @row_attr || Ncurses::A_NORMAL


    ## determine bg and fg and attr
    if selected
      @attr = Ncurses::A_BOLD if selected
      @color_pair =get_color $selectedcolor, @parent.selected_color, @parent.selected_bgcolor unless @parent.nil?
    end
    case focussed
    when :SOFT_FOCUS
      @attr |= Ncurses::A_BOLD
    when true
      @attr |= Ncurses::A_REVERSE
    when false
    end
    #if focussed 
      #@attr |= Ncurses::A_REVERSE
    #end
end

#repaint(graphic, r = @row, c = @col, row_index = -1,, value = @text, focussed = false, selected = false) ⇒ Object

paint a list box cell

@param [Buffer] window or buffer object used for printing
@param [Fixnum] row
@param [Fixnum] column
@param [Fixnum] actual index into data, some lists may have actual data elsewhere and
                display data separate. e.g. rfe_renderer (directory listing)
@param [String] text to print in cell
@param [Boolean, cell focussed, not focussed
@param [Boolean] cell selected or not

Since:

  • 1.2.0 UNTESTED



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rbcurse/listcellrenderer.rb', line 91

def repaint graphic, r=@row,c=@col, row_index=-1,value=@text, focussed=false, selected=false

  select_colors focussed, selected 
  # if listboxes width is reduced, display_len remains the same
  # XXX FIXME parent may not be the list but a container like rfe !!
  # maybe caller should update at start of repain loop.
  #@display_length = @parent.width - 2 - @parent.left_margin

  value=value.to_s
  if !@display_length.nil?
    if value.length > @display_length
      value = value[0..@display_length-1]
    end
    # added 2010-09-27 11:05 TO UNCOMMENT AND TEST IT OUT
    if @justify == :center
      value = value.center(@display_length)
    end
  end
  len = @display_length || value.length
  #$log.debug " XXX @display_length: #{@display_length}, #{value.length}, L:#{len}, pw:#{@parent.width} ::attr:: #{@attr} "
  graphic.printstring r, c, @format % [len, value], @color_pair, @attr
end

#select_colors(focussed, selected) ⇒ Object

sets @color_pair and @attr

Raises:

  • (ArgumentError)

Since:

  • 1.2.0 UNTESTED



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rbcurse/listcellrenderer.rb', line 65

def select_colors focussed, selected
  create_color_pairs unless @pairs
  raise ArgumentError, "pairs hash is null. Changes have happened in listcellrenderer" unless @pairs
  @color_pair = @pairs[:normal]
  @attr = $row_attr
  # give precedence to a selected row
  if selected
    @color_pair = @pairs[:selected]
    @attr       = @attrs[:selected]
  elsif focussed
    @color_pair = @pairs[:focussed]
    @attr       = @attrs[:focussed]
  end
end