/* 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. * $Log: scrollbar.cc,v $ * Revision 1.1.1.1 2000/12/16 20:45:06 stuart * Released TUIPeer sources * * Revision 1.2 2000/12/14 21:01:20 stuart * handle visible property * */ #pragma implementation #include #include #include "textdraw.h" #include "scrollbar.h" Scrollbar::Scrollbar(Orientation dir) { this->dir = dir; setValues(0,10,0,100); } Scrollbar::~Scrollbar() { } void Scrollbar::paint(DrawingContext *dc) { CHTYPE bar = ACS_CKBOARD + A_DIM; //CHTYPE tab = ACS_BLOCK; CHTYPE tab = ' ' + A_REVERSE; #if 0 // many terminals can't handle codes 0-31 static CHTYPE left = 17 + A_REVERSE; static CHTYPE right = 16 + A_REVERSE; static CHTYPE top = 30 + A_REVERSE; static CHTYPE bot = 31 + A_REVERSE; #else static CHTYPE left = '<' + A_REVERSE; static CHTYPE right = '>' + A_REVERSE; static CHTYPE top = '^' + A_REVERSE; static CHTYPE bot = 'v' + A_REVERSE; #endif dc->fillRect(dc->clipRect(),bar); if (dir == HORZ) { dc->writeAttr(0,0,1,&left); dc->writeAttr(size+1,0,1,&right); dc->fillRect(Rect(0,taboffset + 1,0,taboffset + tabsize),tab); } else { dc->writeAttr(0,0,1,&top); dc->writeAttr(0,size+1,1,&bot); dc->fillRect(Rect(taboffset + 1,0,taboffset + tabsize,0),tab); } } void Scrollbar::key(int k) { switch (k) { case KEY_NPAGE: case KEY_SRIGHT: case 'L': case 'K': // vi mode keys toolkit->callMethod(compID,SCROLL_PAGEINC); return; case KEY_PPAGE: case KEY_SLEFT: case 'H': case 'J': toolkit->callMethod(compID,SCROLL_PAGEDEC); return; case KEY_UP: case KEY_LEFT: case 'h': case 'j': toolkit->callMethod(compID,SCROLL_LINEDEC); return; case KEY_DOWN: case KEY_RIGHT: case 'l': case 'k': toolkit->callMethod(compID,SCROLL_LINEINC); return; case KEY_HOME: case '0': toolkit->callMethod(compID,SETVALUE,0); return; case KEY_END: case '$': toolkit->callMethod(compID,SETVALUE,max-1); return; } Component::key(k); } void Scrollbar::remoteMethod(int cmd) { switch (cmd) { case SETVALUE: setValue(readShort()); return; case SETVALUES: { int val = readShort(); int vis = readShort(); int nmin = readShort(); int nmax = readShort(); setValues(val,vis,nmin,nmax); return; } } Component::remoteMethod(cmd); } void Scrollbar::reshape(int x,int y,int w,int h) { if (dir == HORZ) Component::reshape(x,y,size = w,1); else Component::reshape(x,y,1,size = h); if (size > 2) size -= 2; // room for end markers setValue(value); } void Scrollbar::setValue(int val) { if (val < min) val = min; if (val > max - visible) val = max - visible; value = val; int lsize = max - min; tabsize = visible * size / lsize; val -= min; taboffset = (val * size + val) / lsize; invalidate(); // put cursor at scroll tab int curoff = taboffset + tabsize / 2 + 1; if (dir == HORZ) setCursor(curoff,0); else setCursor(0,curoff); } void Scrollbar::setValues(int val,int vis,int nmin,int nmax) { if (nmin < nmax) { min = nmin; max = nmax; } else { min = nmin; max = nmin + size; } if (vis < 0) vis = 0; if (vis >= max - min) vis = max - min - 1; visible = vis; setValue(val); } void Scrollbar::getfocus() { setValue(value); Component::getfocus(); }