Files
harbour-core/harbour/source/rtl/gtapi.c

555 lines
12 KiB
C

/*
* $Id$
*/
/*
* GTAPI.C: Generic Terminal for Harbour
*/
#include <gtapi.h>
/* TODO: functions not implemented yet
int hb_gtPostExt(void);
int hb_gtPreExt(void);
int hb_gtSetBlink(BOOL bBlink);
int hb_gtSetMode(USHORT uiRows, USHORT uiCols);
*/
static USHORT s_uiCurrentRow = 0;
static USHORT s_uiCurrentCol = 0;
static USHORT s_uiDispCount = 0;
static USHORT s_uiColorIndex = 0;
static char s_szColorStr[CLR_STRLEN] = {"W/N, N/W, N/N, N/N, N/W"};
/* gt API functions */
void hb_gtInit(void)
{
gtInit();
}
int hb_gtBox(USHORT uiTop, USHORT uiLeft, USHORT uiBottom, USHORT uiRight, char * fpBoxString)
{
int iCount;
if(uiTop > hb_gtMaxRow() || uiBottom > hb_gtMaxRow() ||
uiLeft > hb_gtMaxCol() || uiRight > hb_gtMaxCol() ||
uiTop > uiBottom || uiLeft > uiRight)
{
return(1);
}
hb_gtDispBegin();
/* upper left corner */
hb_gtSetPos(uiTop, uiLeft);
hb_gtWrite(&fpBoxString[0], 1);
/* top line */
hb_gtRepChar(uiTop, uiLeft+1, fpBoxString[1], uiRight-uiLeft-1);
/* upper right corner */
hb_gtSetPos(uiTop, uiRight);
hb_gtWrite(&fpBoxString[2], 1);
/* left and right sides */
for(iCount = uiTop + 1; iCount<uiBottom; iCount++)
{
hb_gtSetPos(iCount, uiLeft);
hb_gtWrite(&fpBoxString[3], 1);
hb_gtSetPos(iCount, uiRight);
hb_gtWrite(&fpBoxString[3], 1);
}
/* left bottom corner */
hb_gtSetPos(uiBottom, uiLeft);
hb_gtWrite(&fpBoxString[6], 1);
/* bottom line */
hb_gtRepChar(uiBottom, uiLeft+1, fpBoxString[5], uiRight-uiLeft-1);
/* right bottom corner */
hb_gtSetPos(uiBottom, uiRight);
hb_gtWrite(&fpBoxString[4], 1);
hb_gtDispEnd();
return(0);
}
int hb_gtBoxD(USHORT uiTop, USHORT uiLeft, USHORT uiBottom, USHORT uiRight)
{
return(hb_gtBox(uiTop, uiLeft, uiBottom, uiRight, _B_DOUBLE));
}
int hb_gtBoxS(USHORT uiTop, USHORT uiLeft, USHORT uiBottom, USHORT uiRight)
{
return(hb_gtBox(uiTop, uiLeft, uiBottom, uiRight, _B_SINGLE));
}
int hb_gtColorSelect(USHORT uiColorIndex)
{
if(uiColorIndex > _CLR_LASTCOLOR)
{
return(1);
}
else
{
s_uiColorIndex = uiColorIndex;
}
return(0);
}
int hb_gtDispBegin(void)
{
/* TODO: need to add buffering here */
s_uiDispCount++;
return(0);
}
USHORT hb_gtDispCount(void)
{
return(s_uiDispCount);
}
int hb_gtDispEnd(void)
{
/* TODO: need to add buffering here */
s_uiDispCount--;
return(0);
}
int hb_gtGetColorStr(char * fpColorString)
{
int iDestLen = strlen(fpColorString);
int iSrcLen = strlen(s_szColorStr);
if(iSrcLen > iDestLen)
{
return(1);
}
else
{
strncpy(fpColorString, s_szColorStr, iSrcLen);
}
return(0);
}
int hb_gtGetCursor(USHORT * uipCursorShape)
{
int i=gtGetCursorStyle();
int rc=0;
if(i <= _SC_SPECIAL2)
{
*uipCursorShape = i;
}
else
{
rc=i;
}
return(rc);
}
int hb_gtGetPos(USHORT * uipRow, USHORT * uipCol)
{
*uipRow = s_uiCurrentRow;
*uipCol = s_uiCurrentCol;
return(0);
}
BOOL hb_gtIsColor(void)
{
/* TODO: need to call something to do this instead of returning TRUE */
return(TRUE);
}
USHORT hb_gtMaxCol(void)
{
return(gtGetScreenWidth() - 1);
}
USHORT hb_gtMaxRow(void)
{
return(gtGetScreenHeight() - 1);
}
int hb_gtPostExt(void)
{
return(0);
}
int hb_gtPreExt(void)
{
return(0);
}
int hb_gtRectSize(USHORT uiTop, USHORT uiLeft, USHORT uiBottom, USHORT uiRight, USHORT * uipBuffSize)
{
if(uiTop > hb_gtMaxRow() || uiBottom > hb_gtMaxRow() ||
uiLeft > hb_gtMaxCol() || uiRight > hb_gtMaxCol() ||
uiTop > uiBottom || uiLeft > uiRight)
{
return(1);
}
*uipBuffSize = (uiBottom - uiTop) * (uiRight - uiLeft) * 2;
return(0);
}
int hb_gtRepChar(USHORT uiRow, USHORT uiCol, USHORT uiChar, USHORT uiCount)
{
int rc;
char buff[255];
memset(buff, uiChar, uiCount);
buff[uiCount] = 0x0;
rc=hb_gtSetPos(uiRow, uiCol);
if(rc != 0)
return(rc);
rc=hb_gtWrite(buff, uiCount);
return(rc);
}
int hb_gtRest(USHORT uiTop, USHORT uiLeft, USHORT uiBottom, USHORT uiRight, char * vlpScrBuff)
{
gtPutText(uiLeft, uiTop, uiRight, uiBottom, vlpScrBuff);
return(0);
}
int hb_gtSave(USHORT uiTop, USHORT uiLeft, USHORT uiBottom, USHORT uiRight, char * vlpScrBuff)
{
USHORT bufsiz;
int rc;
rc=hb_gtRectSize(uiTop, uiLeft, uiBottom, uiRight, &bufsiz);
if(rc != 0)
{
return(rc);
}
gtGetText(uiLeft, uiTop, uiRight, uiBottom, vlpScrBuff);
return(0);
}
int hb_gtScrDim(USHORT * uipHeight, USHORT * uipWidth)
{
*uipHeight = hb_gtMaxRow();
*uipWidth = hb_gtMaxCol();
return(0);
}
int hb_gtSetBlink(BOOL bBlink)
{
return(0);
}
int hb_gtSetColorStr(char * fpColorString)
{
int iLength = strlen(fpColorString);
if(iLength > CLR_STRLEN)
{
return(1);
}
else
{
strncpy(s_szColorStr, fpColorString, iLength);
}
return(0);
}
int hb_gtSetCursor(USHORT uiCursorShape)
{
gtSetCursorStyle(uiCursorShape);
return(0);
}
int hb_gtSetMode(USHORT uiRows, USHORT uiCols)
{
return(0);
}
int hb_gtSetPos(USHORT uiRow, USHORT uiCol)
{
if(uiRow > hb_gtMaxRow() || uiCol > hb_gtMaxCol())
return(1);
s_uiCurrentRow = uiRow;
s_uiCurrentCol = uiCol;
gtGotoXY(uiCol, uiRow);
return(0);
}
int hb_gtSetSnowFlag(BOOL bNoSnow)
{
return(0);
}
int hb_gtWrite(char * fpStr, USHORT uiLen)
{
/* TODO: need to get current color setting from s_szColorString and
s_uiColorIndex
*/
int iRow, iCol, iMaxCol, iMaxRow, iSize, iTemp;
char attr=7, *fpPointer = fpStr;
/* Determine where the cursor is going to end up */
iRow = s_uiCurrentRow;
iCol = s_uiCurrentCol;
iMaxCol = hb_gtMaxCol();
iMaxRow = hb_gtMaxRow();
iSize = uiLen;
if (iCol + iSize > iMaxCol)
{
/* Calculate eventual row position and the remainder size for the column adjust */
iRow += (iSize / (iMaxCol + 1));
iSize = iSize % (iMaxCol + 1);
}
iCol += iSize;
if (iCol > iMaxCol)
{
/* Column movement overflows onto next row */
iRow++;
iCol -= (iMaxCol + 1);
}
/* If needed, prescroll the display to the new position and adjust the current row
position to account for the prescroll */
if (iRow > iMaxRow)
{
hb_gtScroll(0, 0, iMaxRow, iMaxCol, iRow - iMaxRow, 0);
iTemp = s_uiCurrentRow - (iRow - iMaxRow);
if (iTemp < 0)
{
/* The string is too long to fit on the screen. Only display part of it. */
fpPointer += iMaxCol * abs (iTemp);
iTemp = 0;
if (s_uiCurrentCol > 0)
{
/* Ensure that the truncated text will fill the screen */
fpPointer -= s_uiCurrentCol;
s_uiCurrentCol = 0;
}
}
else iSize = uiLen;
/* Save the new starting row and the new ending row */
s_uiCurrentRow = iTemp;
iRow = iMaxRow;
}
else iSize = uiLen;
/* Now the text string can be displayed */
gtPuts(s_uiCurrentCol, s_uiCurrentRow, attr, fpPointer, iSize);
/* Finally, save the new cursor position */
hb_gtSetPos (iRow, iCol);
return(0);
}
int hb_gtWriteAt(USHORT uiRow, USHORT uiCol, char * fpStr, USHORT uiLen)
{
int rc;
if((rc=hb_gtSetPos(uiRow, uiCol)) != 0)
return(rc);
return(hb_gtWrite(fpStr, uiLen));
}
int hb_gtWriteCon(char * fpStr, USHORT uiLen)
{
int rc = 0;
USHORT uiCount, uiRow = s_uiCurrentRow, uiCol = s_uiCurrentCol;
char ch[2];
ch[1] = 0;
for(uiCount = 0; uiCount < uiLen; uiCount++)
{
ch [0] = fpStr[uiCount];
switch(ch [0])
{
case 7:
break;
case 8:
if(uiRow > 0) uiCol--;
else if(uiRow > 0)
{
uiRow--;
uiCol=hb_gtMaxCol();
}
else
{
hb_gtScroll(0, 0, hb_gtMaxRow(), hb_gtMaxCol(), -1, 0);
uiCol=hb_gtMaxCol();
}
hb_gtSetPos (uiRow, uiCol);
break;
case 10:
if(uiRow < hb_gtMaxRow()) uiRow++;
else
{
hb_gtScroll(0, 0, hb_gtMaxRow(), hb_gtMaxCol(), 1, 0);
}
hb_gtSetPos (uiRow, uiCol);
break;
case 13:
uiCol = 0;
hb_gtSetPos (uiRow, uiCol);
break;
default:
rc = hb_gtWrite(ch, 1);
hb_gtGetPos (&uiRow, &uiCol);
}
if(rc)
return(rc);
}
return(0);
}
int hb_gtScroll(USHORT uiTop, USHORT uiLeft, USHORT uiBottom, USHORT uiRight, SHORT iRows, SHORT iCols)
{
USHORT uiRow = s_uiCurrentRow, uiCol = s_uiCurrentCol, uiSize;
int iLength = (uiRight - uiLeft) + 1;
int iCount, iColOld, iColNew, iColSize;
if (hb_gtRectSize (uiTop, uiLeft, uiBottom, uiRight, &uiSize) == 0)
{
char * fpBlank = (char *)hb_xgrab (iLength);
char * fpBuff = (char *)hb_xgrab (iLength * 2);
if (fpBlank && fpBuff)
{
for (iCount = 0; iCount < iLength; iCount++)
fpBlank [iCount] = 0;
if (iCols >= 0)
{
iColOld = iColNew = uiLeft;
iColOld += iCols;
iColSize = uiRight - uiLeft;
iColSize -= iCols;
}
else
{
iColOld = iColNew = uiLeft;
iColNew -= iCols;
iColSize = uiRight - uiLeft;
iColSize += iCols;
}
for (iCount = (iRows >= 0 ? uiTop : uiBottom);
(iRows >= 0 ? iCount <= uiBottom : iCount >= uiTop);
(iRows >= 0 ? iCount++ : iCount--))
{
/* TODO: need to get current color setting from s_szColorString and
s_uiColorIndex
*/
int iRowPos = iCount + iRows;
char attr=7;
/* Blank the scroll region in the current row */
gtPuts (uiLeft, iCount, attr, fpBlank, iLength);
if ((iRows || iCols) && iRowPos <= uiBottom && iRowPos >= uiTop)
{
/* Read the text to be scrolled into the current row */
gtGetText (iColOld, iRowPos, iColOld + iColSize, iRowPos, fpBuff);
/* Write the scrolled text to the current row */
gtPutText (iColNew, iCount, iColNew + iColSize, iCount, fpBuff);
}
}
}
if (fpBlank) hb_xfree (fpBlank);
if (fpBuff) hb_xfree (fpBuff);
}
s_uiCurrentRow = uiRow;
s_uiCurrentCol = uiCol;
return(0);
}
#ifdef TEST
void main(void)
{
char *test="Testing GT API Functions";
char *test2="This message wraps!";
int iRow, iCol;
/* NOTE: always have to initialze video subsystem */
gtInit();
/* save screen (doesn't work under DOS) */
/*
char *scr;
USHORT size;
hb_gtRectSize(1, 1, hb_gtMaxRow(), hb_gtMaxCol(), &size);
scr=(char *)malloc(size);
hb_gtSave(1, 1, hb_gtMaxRow()-1, hb_gtMaxCol()-1, scr);
*/
/* writing text */
hb_gtSetPos(3, 3);
hb_gtWrite(test, strlen(test));
hb_gtSetPos(12, 42);
hb_gtWrite(test, strlen(test));
/* wrapping text */
hb_gtSetPos(7, 70);
hb_gtWrite(test2, strlen(test2));
/* writing color text (not implemented yet) */
/*
hb_gtSetColorStr( "W+/B, B/W" );
hb_gtColorSelect(_CLR_STANDARD);
hb_gtWrite( "Enhanced color (B/W)", 20 );
hb_gtSetPos(22, 62);
hb_gtColorSelect(_CLR_ENHANCED);
hb_gtWrite( "Standard Color (W+/B)", 21 );
*/
/* boxes */
hb_gtBoxS(10, 10, 20, 20);
hb_gtBoxD(10, 40, 15, 45);
/* cursor functions */
hb_gtSetPos(12, 1);
/* none */
hb_gtSetCursor(_SC_NONE);
getch();
/* underline */
hb_gtSetCursor(_SC_NORMAL);
getch();
/* lower half block */
hb_gtSetCursor(_SC_INSERT);
getch();
/* full block */
hb_gtSetCursor(_SC_SPECIAL1);
getch();
/* upper half block */
hb_gtSetCursor(_SC_SPECIAL2);
getch();
/* restore screen (doesn't work under DOS) */
/*
hb_gtRest(1, 1, hb_gtMaxRow()-1, hb_gtMaxCol()-1, scr);
free(scr);
*/
}
#endif