[fxruby-users] Text color for individual items in FXTable

Tim Smith tsmith at tektone.net
Tue Feb 26 11:45:11 EST 2008


Sorry for the mangled code formatting in my last post.  Trying again:

require 'fox16'; include Fox

class CustomTableItem < FXTableItem
  def drawContent table, dc, x, y, w, h
    # adapted from FXTableItem::drawContent in FXTable.cpp

    hg=table.isHorzGridShown();
    vg=table.isVertGridShown();
    ml=table.getMarginLeft()+(vg ? 1 : 0);
    mt=table.getMarginTop()+(hg ? 1 : 0);
    mr=table.getMarginRight();
    mb=table.getMarginBottom();
    font=dc.getFont();
    lbl=getText();
    icn=getIcon();

    # Text width and height
    beg=tw=th=0;
    begin
      _end=beg;
      _end+=1 while(_end<lbl.length() && lbl[_end].chr!='\n') 
      t=font.getTextWidth(lbl[beg..._end])
      tw=t if t>tw
      th+=font.getFontHeight();
      beg=_end+1;
    end while(_end<lbl.length());

    # Icon size
    iw=ih=0;
    if(icn)
      iw=icn.getWidth();
      ih=icn.getHeight();
    end

    # Icon-text spacing
    s=0;
    s=4 if(iw && tw) 

    # Fix x coordinate
    if(justify&LEFT == 1)
      if(iconPosition == BEFORE) then ix=x+ml; tx=ix+iw+s;
      elsif(iconPosition == AFTER) then tx=x+ml; ix=tx+tw+s;
      else ix=x+ml; tx=x+ml; end
    elsif(justify&RIGHT == 1)
      if(iconPosition == BEFORE) then
        tx=x+w-mr-tw; ix=tx-iw-s;
      elsif(iconPosition == AFTER) then
        ix=x+w-mr-iw; tx=ix-tw-s;
      else ix=x+w-mr-iw; tx=x+w-mr-tw; end
    else
      if(iconPosition == BEFORE) then
        ix=x+(ml+w-mr)/2-(tw+iw+s)/2; tx=ix+iw+s;
      elsif(iconPosition == AFTER) then
        tx=x+(ml+w-mr)/2-(tw+iw+s)/2; ix=tx+tw+s;
      else ix=x+(ml+w-mr)/2-iw/2; tx=x+(ml+w-mr)/2-tw/2; end
    end

    # Fix y coordinate
    if(justify&TOP == 1)
      if(iconPosition == ABOVE) then iy=y+mt; ty=iy+ih;
      elsif(iconPosition == BELOW) then ty=y+mt; iy=ty+th;
      else iy=y+mt; ty=y+mt; end
    elsif(justify&BOTTOM == 1)
      if(iconPosition == ABOVE) then ty=y+h-mb-th; iy=ty-ih;
      elsif(iconPosition == BELOW) then iy=y+h-mb-ih; ty=iy-th;
      else iy=y+h-mb-ih; ty=y+h-mb-th; end
    else
      if(iconPosition == ABOVE) then
        iy=y+(mt+h-mb)/2-(th+ih)/2; ty=iy+ih;
      elsif(iconPosition == BELOW) then
        ty=y+(mt+h-mb)/2-(th+ih)/2; iy=ty+th;
      else iy=y+(mt+h-mb)/2-ih/2; ty=y+(mt+h-mb)/2-th/2; end
    end

    # Paint icon
    dc.drawIcon(icn,ix,iy) if(icn)
      
    # Text color
    if(selected?)
      dc.setForeground(table.getSelTextColor());
    else
      dc.setForeground(table.getTextColor());
    end

    # Draw text
    yy=ty+font.getFontAscent();
    beg=0;
    begin
      _end=beg;
      _end+=1 while(_end<lbl.length() && lbl[_end].chr !='\n') 
      if(justify&LEFT == 1) then xx=tx;
      elsif(justify&RIGHT == 1) then
        xx=tx+tw-font.getTextWidth(lbl[beg..._end]);
      else xx=tx+(tw-font.getTextWidth(lbl[beg..._end]))/2; end
      dc.drawText(xx,yy,lbl[beg..._end]);
      yy+=font.getFontHeight();
      beg=_end+1;
    end while(_end<lbl.length());
  end
end

class CustomTable < FXTable
  def createItem *parameters
    CustomTableItem.new *parameters
  end
end

app = FXApp.new
main = FXMainWindow.new app, 'Test'

table = CustomTable.new main
table.setTableSize 2, 2
table.visibleRows = 2
table.visibleColumns = 2

table.setItemText 0, 0, 'one'
table.setItemText 0, 1, 'two'
table.setItemText 1, 0, 'three'
table.setItemText 1, 1, 'four'

app.create
main.show PLACEMENT_SCREEN
app.run


More information about the fxruby-users mailing list