/* TUIpeer works in conjunction with an implementation of java.awt.Toolkit to provide a Text User Interface for programs using the Java AWT. Copyright (C) 1997-2000 Stuart D. Gathman This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #pragma implementation "canvas.h" #include "canvas.h" #include "textdraw.h" #include "container.h" Canvas::Canvas(): TextImage(0,0) { localtabs = false; // always handle tab remotely } Canvas::~Canvas() { } ContainerComponent *Canvas::obscured() { ContainerComponent *p = getParent(); if (p) { int i = p->indexOf((Controller *)this); Rect r(bounds()); while (i-- > 0) { Component *c = p->getComponent(i); if (c->isVisible() && !c->bounds().intersect(r).isempty()) return p; } } return 0; } void Canvas::reshape(int x,int y,int w,int h) { resize(w,h); Component::reshape(x,y,w,h); invalidate(); } void Canvas::paint(DrawingContext *dc) { Rect r(dc->clipRect()); Rect r1(r.intersect(clipRect())); int w = r1.width(); for (int i = r1.r1; i <= r1.r2; ++i) { const CHTYPE *p = rowdata(i); for (int j = r1.c1; j <= r1.c2; ++j) dc->writeAttr(r1.c1,i,w,p + r1.c1); } if (r1.r2 < r.r2) dc->fillRect(Rect(r1.r2 + 1,r.c1,r.r2,r.c2),ACS_BLOCK); if (r1.c2 < r.c2) dc->fillRect(Rect(r.r1,r1.c2 + 1,r1.r2,r.c2),ACS_BLOCK); } void Canvas::writeAttr(int x,int y,int n,const CHTYPE *a) { TextImage::writeAttr(x,y,n,a); if (dc) { ContainerComponent *p = obscured(); if (p) repaint(Rect(y,x,y,x+n-1)); else dc->writeAttr(x,y,n,a); } } void Canvas::copyArea(const Rect &r,int dx,int dy) { TextImage::copyArea(r,dx,dy); if (dc) { ContainerComponent *p = obscured(); if (p) repaint(r.moverel(dy,dx)); else dc->copyArea(r,dx,dy); } } void Canvas::fillRect(const Rect &r,CHTYPE attr) { TextImage::fillRect(r,attr); if (dc) { ContainerComponent *p = obscured(); if (p) repaint(r); else dc->fillRect(r,attr); } } void Canvas::getfocus() { Component::getfocus(); setCursor(0,0); } void Canvas::remoteMethod(int cmd) { enum GraphicIDs { // from TUIGraphics.java DRAWFONT = 150, DRAWCOLOR = 151, CLIPRECT = 152, TRANSLATE = 153, COPYAREA = 154, FILLRECT = 155, DRAWRECT = 156, CLEARRECT = 157, DRAWROUNDRECT = 158, FILLROUNDRECT = 159, DRAWSTRING = 160, DRAWLINE = 161, FILL3DRECT = 162, DRAW3DRECT = 163, INITGRAPHICS = 164 }; switch (cmd) { case INITGRAPHICS: drawing = *this; dc = getDC(); drawing.setDC(dc ? new IndirectContext(this) : 0); return; case DRAWFONT: { drawing.fontname = readUTF(); drawing.textSize = readShort(); drawing.textStyle = readShort(); //drawing.textAttr = drawing.getAttr(); return; } case DRAWCOLOR: { drawing.fgColor = readShort(); //drawing.textAttr = drawing.getAttr(drawing.fgColor); return; } case DRAWSTRING: { string str(readUTF()); int x = descale(readShort(),toolkit->scalex); int y = descale(readShort(),toolkit->scalex); if (drawing.dc) drawing.dc->writeStr(x,y-1,str,drawing.drawAttr(x,y-1)); return; } case FILLRECT: { short x,y,w,h; x = descale(readShort(),toolkit->scalex); y = descale(readShort(),toolkit->scaley); w = descale(readShort(),toolkit->scalex); h = descale(readShort(),toolkit->scaley); if (drawing.dc) drawing.dc->fillRect(Rect(Point(y,x),w,h), ' '|drawing.getAttr(drawing.fgColor)); return; } case DRAWRECT: { short x,y,w,h; x = descale(readShort(),toolkit->scalex); y = descale(readShort(),toolkit->scaley); w = descale(readShort(),toolkit->scalex); h = descale(readShort(),toolkit->scaley); if (drawing.dc) drawing.dc->drawRect(x,y,w,h,drawing.drawAttr(x,y),dc->LD_SINGLE); return; } case CLEARRECT: { short x,y,w,h; x = descale(readShort(),toolkit->scalex); y = descale(readShort(),toolkit->scaley); w = descale(readShort(),toolkit->scalex); h = descale(readShort(),toolkit->scaley); if (drawing.dc) drawing.dc->fillRect(Rect(Point(y,x),w,h), ' '|drawing.getAttr(bgColor)); return; } case CLIPRECT: { short x,y,w,h; x = descale(readShort(),toolkit->scalex); y = descale(readShort(),toolkit->scaley); w = descale(readShort(),toolkit->scalex); h = descale(readShort(),toolkit->scaley); if (drawing.dc) drawing.dc->setClip(Rect(Point(y,x),w,h)); return; } case TRANSLATE: { short x,y; x = descale(readShort(),toolkit->scalex); y = descale(readShort(),toolkit->scaley); if (drawing.dc) drawing.dc->translate(x,y); return; } case DRAWLINE: { short x1,y1,x2,y2; x1 = descale(readShort(),toolkit->scalex); y1 = descale(readShort(),toolkit->scaley); x2 = descale(readShort(),toolkit->scalex); y2 = descale(readShort(),toolkit->scaley); if (drawing.dc) { CHTYPE a = drawing.drawAttr(x1,y1); if (y1 == y2) // draw horizontal line drawing.dc->fillRect(Rect(y1,x1,y2,x2),ACS_HLINE | a); if (x1 == x2) drawing.dc->fillRect(Rect(y1,x1,y2,x2),ACS_VLINE | a); } return; } case COPYAREA: { int x = descale(readShort(),toolkit->scalex); int y = descale(readShort(),toolkit->scaley); int w = descale(readShort(),toolkit->scalex); int h = descale(readShort(),toolkit->scaley); int dx = descale(readShort(),toolkit->scalex); int dy = descale(readShort(),toolkit->scaley); if (drawing.dc) drawing.dc->copyArea(Rect(Point(y,x),w,h),dx,dy); return; } } Component::remoteMethod(cmd); }