Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Modifying KTable
Modifying KTable [message #465598] |
Thu, 15 December 2005 18:31 |
Greg Messages: 21 Registered: July 2009 |
Junior Member |
|
|
I've been wrestling with this for a few days; and I'm almost ready to give
up.
I'd like to add "Syntax" coloring to the cells on a KTable.
I thought I could overwrite the DefaultCellRenderer.drawCellContent, but
that is not working as I'd expect.
To color individual letters, I take the rect that drew the cell, and write
individual rects for each letter (setting foreground color appropriately).
But it looks like there is a conflict between the rect that cell needs and
the individual rects that I'm creating.
Here's my drawCellContect
Please tell me there is a way to do this (Eclipse 3.1; KTable 2.1.2; Win XP)
Any help is greatly appreciated
protected void drawCellContent(GC gc, Rectangle rect, String text,
Image img, Color textColor, Color backColor) {
if (text.length() == 0) {
gc.setForeground(this.COLOR_TEXT);
gc.setBackground(backColor);
gc.fillRectangle(rect);
SWTX.drawTextImage(gc, text, getAlignment(), img, getAlignment(),
rect.x + 3, rect.y + 2, rect.width - 6, rect.height - 4);
} else {
int printedCharacters = 0 ;
while (printedCharacters < text.length()) {
AsmBasedScanner scanner = new AsmBasedScanner(text) ;
IToken token = scanner.nextToken() ;
Rectangle tokenRect = new Rectangle(rect.x+3, rect.y+2, rect.height-4,
rect.width-6) ;
gc.setBackground(backColor);
while (token!=Token.EOF) {
Color color = ((TextAttribute)token.getData()).getForeground();
int tokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() ;
String textBlock = text.substring(scanner.getTokenOffset(), tokenEnd) ;
System.out.println("Scanning " + text + "\n" +
"from " + scanner.getTokenOffset() + " to " + tokenEnd + ".\n" +
"Drawing " + textBlock );
//figure out where to draw based on textblock size
int fontWidth = gc.textExtent(textBlock).x ;
int fontOffset = printedCharacters*fontWidth ;
//offset the font by the width of previous written text.
//reduce the tokenRec so it's just big enough to contain this token
text
gc.setForeground(color);
gc.setBackground(backColor);
tokenRect.x = tokenRect.x + fontOffset ;
tokenRect.width = textBlock.length()*fontWidth ;
System.out.println("Drawing image bounded by " + tokenRect.x + ", " +
tokenRect.y + ", " + tokenRect.width + ", " + tokenRect.height) ;
SWTX.drawTextImage(gc, textBlock, getAlignment(), img, getAlignment(),
tokenRect.x, tokenRect.y , tokenRect.width ,
tokenRect.height);
printedCharacters += textBlock.length() ;
token = scanner.nextToken() ;
}
}
}
}
|
|
| | |
Re: Modifying KTable [message #465648 is a reply to message #465645] |
Fri, 16 December 2005 07:58 |
Lorenz Maierhofer Messages: 88 Registered: July 2009 |
Member |
|
|
Hi,
> The behavior I get varies depending on whether I paint the background of
> the full rect (after painting the rect for each character).
You would have to fillRect() the cell area first with the background
color. Then draw your text letter by letter and switch the foreground
color as needed. Try not to use SWTX.drawTextImage(), but check out that
implementation for some more basic stuff (handling GC directly). I
assume the SWTX class makes assumptions that might not be correct for
your case.
> I think the main issue is that I'm creating text over top of the widget
> that is actually getting keyboard input for the cell. When it partially
> works, the text appears after entering the first time, but disappears
> when I refresh the screen (by minimizing, or hiding) or by returning
> focus to the cell.
OK, this sounds interesting.
First, note that there are 2 different classes involved in showing the
content.
- The renderer draws the content for the table cell.
- The celleditor is invoked as soon as you start to edit your cell.
Since you probably use KTableCellEditorText or something similar when
you talk about getting keyboard input, you can't get colors there. The
class uses a Text widget, but it should be possible to use a StyledText
instead.
You described that the text is rendererd correctly the first time, but
on subsequent paint events it disappears. Check if you are really using
the same renderer class (KTableModel.doGetCellRenderer()), and if the
correct cell content can be retrieved from the model.
Lorenz
|
|
| |
Goto Forum:
Current Time: Sat Dec 14 17:47:44 GMT 2024
Powered by FUDForum. Page generated in 0.03731 seconds
|