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

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


Right - I'm trying to set text colors for individual cells, not
background colors for alternating rows and columns.  In the code below,
I don't even try to set colors, but just override drawContent with a
Ruby translation of FXTableItem::drawContent from FXTable.cpp in the FOX
distribution.  But no text appears in the cells.  Am I missing
something?

Thanks, Tim

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

-----Original Message-----
From: fxruby-users-bounces at rubyforge.org
[mailto:fxruby-users-bounces at rubyforge.org] On Behalf Of Thomas, Jason M
(Software)
Sent: Monday, February 25, 2008 6:07 PM
To: fxruby-users at rubyforge.org
Subject: Re: [fxruby-users] Text color for individual items in FXTable

You can use the FXTable's setCellColor to acheive a nice alternating
look as shown in the screenshot on the FXRuby website. However, it
sounds like you're trying to set individual cells which I haven't tried.

    # Table
    @table = FXTable.new(frame,
      :opts =>
TABLE_COL_SIZABLE|TABLE_ROW_SIZABLE|LAYOUT_FILL_X|LAYOUT_FILL_Y,
      :padding => 2)
      
    @table.visibleRows = 20
    @table.visibleColumns = 8

    @table.setTableSize(50, 14)

    @table.setBackColor(FXRGB(255, 255, 255))
    @table.setCellColor(0, 0, FXRGB(255, 255, 255))
    @table.setCellColor(0, 1, FXRGB(255, 240, 240))
    @table.setCellColor(1, 0, FXRGB(240, 255, 240))
    @table.setCellColor(1, 1, FXRGB(240, 240, 255))
 
________________________________

From: fxruby-users-bounces at rubyforge.org
[mailto:fxruby-users-bounces at rubyforge.org] On Behalf Of Tim Smith
Sent: Monday, February 25, 2008 3:58 PM
To: fxruby-users at rubyforge.org
Subject: [fxruby-users] Text color for individual items in FXTable

Hello,

I would like to set text colors for individual items in an FXTable.
FXTable has a textColor accessor, but it applies to every item, not to
individual items.  So I subclassed FXTable and overrode createItem to
return a subclass of FXTableItem in which I override drawContent.  One
of drawContent's parameters is the device context dc, but when I call
dc.drawText in my customized drawContent, nothing is drawn on the
screen.  I know the method is being called.  What might be wrong?  Has
anyone else managed to set text colors for individual table items?

Thanks for any help,

Tim


This message and any enclosures are intended only for the addressee.
Please  
notify the sender by email if you are not the intended recipient.  If
you are  
not the intended recipient, you may not use, copy, disclose, or
distribute this  
message or its contents or enclosures to any other person and any such
actions  
may be unlawful.  Ball reserves the right to monitor and review all
messages  
and enclosures sent to or from this email address.
_______________________________________________
fxruby-users mailing list
fxruby-users at rubyforge.org
http://rubyforge.org/mailman/listinfo/fxruby-users


More information about the fxruby-users mailing list