class Canis::ListCellRenderer

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.

Public Class Methods

new(text="", config={}) click to toggle source
# File lib/canis/core/include/deprecated/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

Public Instance Methods

create_color_pairs() click to toggle source

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

# File lib/canis/core/include/deprecated/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
getvalue() click to toggle source
# File lib/canis/core/include/deprecated/listcellrenderer.rb, line 60
def getvalue
  @text
end
prepare_default_colors(focussed, selected) click to toggle source

@deprecated only for older code that may have extended this.

# File lib/canis/core/include/deprecated/listcellrenderer.rb, line 117
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) click to toggle source

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

# File lib/canis/core/include/deprecated/listcellrenderer.rb, line 92
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) click to toggle source

sets @color_pair and @attr

# File lib/canis/core/include/deprecated/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
  @attr = @row_attr || $row_attr # changed 2011-10-15 since we seem to be ignoring row_attr changes
  # 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