home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CSAPE32.ARJ
/
SOURCE
/
CSSRC
/
TEDIT.C
< prev
next >
Wrap
Text File
|
1990-12-15
|
15KB
|
639 lines
/*
tedit.c
% commmon routines used in text editting
C-scape 3.2
Copyright (c) 1988 by Oakland Group, Inc.
ALL RIGHTS RESERVED.
Revision History:
-----------------
6/17/88 jdc created
9/22/88 jmd Fixed ted_reset... now calls sed_Update
12/18/88 jdc added check before BDM_SCROLL in ted_DeleteString
2/08/89 jmd added args
3/24/89 jmd added sed_ macros
4/06/89 jmd made this match teddecl (int to boolean)
5/19/89 jdc removed cursor from ted_DeleteString
5/23/89 jdc made tb_setcursor menu_setcursor
5/24/89 jdc added clipping to sed_Paint, now takes menu coords
5/25/89 jdc removed sed_Paint, now uses sd_paintbox
5/28/89 jdc fixed ted_PageDown for LNF
6/26/89 jmd removed unused yoffset from ted_PageDown
7/03/89 jdc fixed ted_reset
7/21/89 jdc fixed ted_reset again, off_text case
7/22/89 jdc moved ted_SetMoveMethod to tedsetmv.c
8/14/89 jmd Added call to disp_evcheck() macro
9/08/89 jmd Added LNF stuff
11/04/89 jdc added paint optimization
12/04/89 jmd added cursor color
12/20/89 jdc preened
1/29/90 jdc removed lnf_GotoField() from ted_SetCursor()
2/22/90 jdc created ted_MoveChar removed LeftChar & RightChar
3/04/90 jdc fixed MoveChar
3/15/90 jmd added Cache/Flush to ted_AddString and ted_MoveChar
3/28/90 jmd ansi-fied
5/02/90 jdc added return values to PageUp, PageDown and GoBottom
5/03/90 jmd added ifdef for Zortech
6/14/90 jdc preened, tweeked VWidth stuff in ted_reset
6/15/90 jdc fixed a preening
8/06/90 jdc fixed GoBottom on last row bug
10/13/90 jdc renamed global lnf_type to lnfglobal
10/15/90 jdc removed LNF stuff
10/28/90 jdc fixed boolean/int ret conflict
11/01/90 jmd added fix for delete in middle of tab
12/15/90 jdc preened color operation
*/
#ifdef LNF
# include "lnf.h"
# include "teddecl.h"
# include "tbpriv.h"
#else
# include "ted.h"
#endif
OSTATIC boolean mark_GetUpdate(int arow, int acol, int lrow, int lcol, int row, int col, ocbox *Abox, ocbox *Bbox);
void ted_StartWorking(sed_type sed)
{
#ifdef Z2
/* compensate for Zortech 2.0 Compiler */
if (sed_GetMoveMethod(sed) == (VOID *) default_move) {
sed_SetMove(sed, ted_Follow);
}
#else
if (sed_GetMoveMethod(sed) == default_move) {
sed_SetMove(sed, ted_Follow);
}
#endif
if (ted_GetWrapWidth(sed) < 32000) {
sed_SetMenuVWidth(sed, ted_GetWrapWidth(sed));
}
/* adjust cursor color */
win_SetCursorColor(sed, win_GetFgColor(sed));
ted_GotoPosition(sed, sed_GetCRow(sed), sed_GetCCol(sed));
ted_SetCursor(sed, sed_GetCRow(sed), sed_GetCCol(sed));
win_ShowCursor(sed);
}
void ted_GetPosition(sed_type sed, int *row, int *col)
{
*row = sed_GetCRow(sed);
*col = sed_GetCCol(sed);
}
boolean ted_MoveChar(sed_type sed, int r, int c, int tab_mode)
/*
r and c are offsets and should only be 1 or -1's
if tab_mode == TED_TABJUMP then move by whole tabs,
else internal to tab.
*/
{
int row, col;
boolean ret;
tb_type tb;
ted_GetPosition(sed, &row, &col);
row += r;
col += c;
if (win_IsEmployed(sed)) {
disp_Cache();
}
if ((ret = ted_GotoPosition(sed, row, col)) == TRUE
&& tab_mode == TED_TABJUMP) {
tb = sed_GetTextbuf(sed);
while (row == tb_GetRow(tb) && col < tb->exp_len
&& col != tb_GetCol(tb)) {
col += c;
ted_GotoPosition(sed, row, col);
}
}
if (win_IsEmployed(sed)) {
disp_Flush();
}
return(ret);
}
boolean ted_GoEnd(sed_type sed)
{
tb_type tb;
int row, col;
ted_GetPosition(sed, &row, &col);
tb = sed_GetTextbuf(sed);
if (row > tb_GetRow(tb)) {
col = 0;
}
else {
col = tb->exp_len - 1;
}
return(ted_GotoPosition(sed, row, col));
}
boolean ted_PageUp(sed_type sed)
{
int row, col, yoffset;
ted_GetPosition(sed, &row, &col);
if (row == 0) {
return(FALSE);
}
else if (sed_GetYoffset(sed) == 0) {
row = 0;
}
else {
yoffset = (sed_GetYoffset(sed) > sed_GetHeight(sed)) ?
sed_GetHeight(sed) : sed_GetYoffset(sed);
row -= yoffset;
if (ted_GetMark(sed) != TED_NOMARK) {
/* cheat to prevent marking flash */
if (!((sed_GetTextbuf(sed))->m_stop)) {
(sed_GetTextbuf(sed))->markbox.cleat_row -= yoffset;
}
}
sd_scroll(sed, -yoffset, 0, FALSE);
}
ted_GotoPosition(sed, row, col);
return(TRUE);
}
boolean ted_PageDown(sed_type sed)
{
int refresh;
int row, col, yoffset;
ted_GetPosition(sed, &row, &col);
row += sed_GetHeight(sed);
refresh = ted_GetRefresh(sed);
ted_SetRefresh(sed, TED_NOREFRESH);
yoffset = sed_GetYoffset(sed);
if (!ted_GotoPosition(sed, row, col)) {
ted_SetRefresh(sed, refresh);
return(ted_GoBottom(sed));
}
else {
yoffset = sed_GetHeight(sed) - (sed_GetYoffset(sed) - yoffset);
sd_scroll(sed, yoffset, 0, FALSE);
if (ted_SetRefresh(sed, refresh) == TED_REFRESH) {
win_Paint(sed);
}
ted_SetCursor(sed, row, col);
}
return(TRUE);
}
boolean ted_GoBottom(sed_type sed)
/*
moves the cursor to the last row
*/
{
int row, startrow, startcol;
tb_type tb;
tb = sed_GetTextbuf(sed);
startrow = row = sed_GetCRow(sed);
startcol = sed_GetCCol(sed);
if (!tb_FindPosition(tb, row, 0)) {
/* find last row backwards */
while (!tb_FindPosition(tb, row, 0)) {
row--;
}
}
else {
/* find last row forwards */
while (tb_FindPosition(tb, row, 0)) {
row++;
}
row--;
}
if (row == startrow && startcol == tb->exp_len - 1) {
return(FALSE);
}
return(ted_GotoPosition(sed, row, tb->exp_len - 1));
}
void ted_reset(sed_type sed, int i, int mode)
/*
resets wrap width (mode == 0) or tab size (mode == 1), and hints
*/
{
tb_type tb;
int row, col, off_text = FALSE;
long cursor;
tb = sed_GetTextbuf(sed);
ted_GetPosition(sed, &row, &col);
cursor = tb->cursor;
if (!tb_FindPosition(tb, row, col)) {
off_text = TRUE;
}
if (mode == 0) {
tb_SetWrapWidth(tb, i);
if (i < 32000) {
/* teds' width are their wrap width */
sed_SetMenuVWidth(sed, i);
}
else {
sed_SetMenuVWidth(sed, -1);
}
/* Send Scroll message to the border */
sed_SendBorderMsg(sed, BDM_SCROLL, NULL, NULL);
}
else {
tb_SetTabSize(tb, i);
}
tb_Rewind(tb);
if (off_text) {
tb_FindPosition(tb, row, col);
}
else {
tb->cursor = cursor;
menu_setcursor(sed_GetMenu(sed));
if (ted_GetRefresh(sed)) {
ted_GotoPosition(sed, tb_GetRow(tb), tb_GetCol(tb));
}
}
}
void ted_SetCursor(sed_type sed, int row, int col)
/*
sets the window cursor from the sed's (menu!) cursor position
*/
{
tb_type tb;
int lrow, lcol, len;
ocbox Abox, Bbox;
lrow = sed_GetCRow(sed);
lcol = sed_GetCCol(sed);
sed_SetTrow(sed, row);
sed_SetTcol(sed, col);
tb = sed_GetTextbuf(sed);
box_sort(&Abox, &(tb->markbox), BOXSORT_ROW);
if (ted_GetMark(sed) != TED_NOMARK) {
if (!(tb->m_stop)) {
tb->markbox.cleat_row = row;
tb->markbox.cleat_col = col;
tb->cleat = tb->cursor;
}
}
if (ted_GetRefresh(sed)) {
ted_Scroll(sed);
win_SetCursorPos(sed, row - sed_GetYoffset(sed), col - sed_GetXoffset(sed));
if (tb->m_stop || tb->mark == TED_NOMARK) {
;
}
else if (tb->mark == TED_MARK) {
if (row == lrow) {
if ((len = col - lcol) < 0) {
len = -len;
lcol = col;
}
sed_RepaintRow(sed, row, lcol, len + 1);
}
else if (row <= Abox.toprow) {
if (lrow <= tb->markbox.anchor_row) {
sed_RepaintRows(sed, row, Abox.toprow);
}
else {
sed_RepaintRows(sed, row, Abox.botrow);
}
}
else if (row <= Abox.botrow) {
if (lrow <= tb->markbox.anchor_row) {
sed_RepaintRows(sed, Abox.toprow, row);
}
else {
sed_RepaintRows(sed, row, Abox.botrow);
}
}
else {
if (lrow < tb->markbox.anchor_row) {
sed_RepaintRows(sed, Abox.toprow, Abox.botrow);
}
else {
sed_RepaintRows(sed, Abox.botrow, row);
}
}
}
else if (mark_GetUpdate(tb->markbox.anchor_row,
tb->markbox.anchor_col, lrow, lcol, row, col, &Abox, &Bbox)) {
win_Paint(sed);
}
else {
if ((Abox.toprow <= Abox.botrow) && (Abox.leftcol <= Abox.rightcol)) {
Abox.toprow -= sed_GetYoffset(sed);
Abox.botrow -= sed_GetYoffset(sed);
Abox.leftcol -= sed_GetXoffset(sed);
Abox.rightcol -= sed_GetXoffset(sed);
#ifdef LNF /* funny field marking */
Abox.leftcol = 0;
Abox.rightcol = win_GetWidth(sed);
#endif
win_PaintBox(sed, &Abox);
}
if ((Bbox.toprow <= Bbox.botrow) && (Bbox.leftcol <= Bbox.rightcol)) {
Bbox.toprow -= sed_GetYoffset(sed);
Bbox.botrow -= sed_GetYoffset(sed);
Bbox.leftcol -= sed_GetXoffset(sed);
Bbox.rightcol -= sed_GetXoffset(sed);
#ifdef LNF
Bbox.leftcol = 0;
Bbox.rightcol = win_GetWidth(sed);
#endif
win_PaintBox(sed, &Bbox);
}
}
}
}
static boolean mark_GetUpdate(int arow, int acol, int lrow, int lcol, int row, int col, ocbox *Abox, ocbox *Bbox)
/*
finds the two minimum box paint
passes back minimum paint boxes Abox & Bbox
return value: TRUE == just paint the whole window
FALSE == paint the boxes
*/
{
int y, z;
boolean paint = TRUE; /* paint flag */
/* compute repaint regions by quadrant */
if (row <= arow && lrow <= arow) {
/* 1 */
paint = FALSE;
y = (row > lrow) ? row : lrow;
z = (row < lrow) ? row : lrow;
Abox->toprow = z;
Abox->botrow = arow;
Bbox->toprow = Abox->toprow;
Bbox->botrow = y - 1;
}
else if (row >= arow && lrow >= arow) {
/* 2 */
paint = FALSE;
y = (row < lrow) ? row : lrow;
z = (row > lrow) ? row : lrow;
Abox->toprow = arow;
Abox->botrow = z;
Bbox->toprow = y + 1;
Bbox->botrow = Abox->botrow;
}
if (!paint) {
if (col <= acol && lcol <= acol) {
/* 3 */
paint = FALSE;
y = (col > lcol) ? col : lcol;
z = (col < lcol) ? col : lcol;
Abox->leftcol = z;
Abox->rightcol = y - 1;
Bbox->leftcol = y;
Bbox->rightcol = acol;
}
else if (col >= acol && lcol >= acol) {
/* 4 */
paint = FALSE;
y = (col < lcol) ? col : lcol;
z = (col > lcol) ? col : lcol;
Abox->leftcol = y + 1;
Abox->rightcol = z;
Bbox->leftcol = acol;
Bbox->rightcol = y;
}
else {
paint = TRUE;
}
}
return(paint);
}
#define HORZ_JUMP 5
boolean ted_Scroll(sed_type sed)
/*
checks whether the cursor is outside the window and scrolls appropriately
*/
{
int offset, row, col;
int deltaY = 0, deltaX = 0;
boolean ret = FALSE;
row = sed_GetCRow(sed) - sed_GetYoffset(sed);
col = sed_GetCCol(sed) - sed_GetXoffset(sed);
if ((offset = row) < 0) {
deltaY = offset;
ret = TRUE;
}
else if ((offset = row - (sed_GetHeight(sed) - 1)) > 0){
deltaY = offset;
ret = TRUE;
}
if ((offset = col) < 0) {
deltaX = (offset/HORZ_JUMP - 1) * HORZ_JUMP;
deltaX = (deltaX + sed_GetXoffset(sed) < 0) ? (-sed_GetXoffset(sed)) : deltaX;
ret = TRUE;
}
else if ((offset = col - (sed_GetWidth(sed) - 1)) > 0){
deltaX = (offset/HORZ_JUMP + 1) * HORZ_JUMP;
ret = TRUE;
}
if (ret && ted_GetRefresh(sed)) {
sd_scroll(sed, deltaY, deltaX, FALSE);
}
return(ret);
}
boolean ted_AddChar(sed_type sed, char c)
{
return(ted_AddString(sed, &c, 1));
}
boolean ted_AddString(sed_type sed, char *s, unsigned int len)
/*
places s at the cursor, resets hints, and refreshes display
*/
{
tb_type tb;
int row, col, refresh_row, refresh_col, x, mode = TB_SAMECOLOR;
boolean ret;
boolean wrap;
byte attr;
bblock_type b;
ted_GetPosition(sed, &row, &col);
/* check for color change */
tb = sed_GetTextbuf(sed);
b = tb->bbc->b;
x = b->off + (int)(tb->cursor - tb->offset);
while (x > 0 && b->next != NULL) {
x -= b->len;
b = b->next;
}
if (x == 0) {
mode = TB_COLORNNL;
attr = b->attr;
}
if ((refresh_row = tb_GetRow(tb)) > row - 1 &&
((*s == ' ' || *s == '\t') && tb->width < 32000)) {
refresh_row = (row > 0) ? row - 1 : 0;
}
refresh_col = (col >= tb->exp_len) ? tb->exp_len - 1 : col;
if (row == tb_GetRow(tb) && col != tb->col && col < tb->exp_len) {
/* in middle of tab */
if (!menu_putTB(sed_GetMenu(sed), row, tb->col, NULL, ' ',
col - tb->col, attr, attr, mode)) {
ret = FALSE;
}
}
if (tb_FindLine(sed_GetTextbuf(sed), row) <= 0) {
wrap = FALSE;
}
else if (ted_GetInsert(sed)) {
wrap = (tb->width >= 32000 || tb->exp_len + len < tb->width)
? FALSE : TRUE;
}
else {
wrap = TRUE;
}
ret = menu_putTB(sed_GetMenu(sed), row, col, s, 0x00, len,
attr, attr, mode);
if (win_IsEmployed(sed)) {
disp_Cache();
}
ted_GotoPosition(sed, tb_GetRow(tb), tb_GetCol(tb));
if (ted_GetRefresh(sed)) {
if (wrap || *s == '\n') {
sed_RepaintBelow(sed, refresh_row);
}
else if (refresh_row < row) {
sed_RepaintRows(sed, refresh_row, tb_GetRow(tb));
}
else {
sed_RepaintRow(sed, row, refresh_col, tb->exp_len - refresh_col);
}
if (tb_GetRow(tb) > row) {
sed_SendBorderMsg(sed, BDM_SCROLL, NULL, NULL);
}
}
if (win_IsEmployed(sed)) {
disp_Flush();
}
tb->xcol = sed_GetCCol(sed);
return(ret);
}
char ted_DeleteString(sed_type sed, int row, int col, unsigned int len)
/*
deletes the string at the row, col.
resets hints, and refreshes display
*/
{
tb_type tb;
int spaces, rowcount, before_len, xlen;
char oldchr[2];
tb = sed_GetTextbuf(sed);
if (!tb_FindPosition(tb, row, col)) {
return('\0');
}
before_len = tb->len;
xlen = tb->exp_len;
ted_GetString(sed, oldchr, 1); /* remember the first deleted char */
if ((spaces = col - (tb->exp_len - 1)) > 0) {
menu_Addc(sed_GetMenu(sed), row, tb->len, ' ', spaces);
tb_SetLineLen(tb);
}
rowcount = (sed_GetMenu(sed))->rowcount;
if (menu_strdel(sed_GetMenu(sed), row, col, (long)len) == 0L) {
*oldchr = '\0';
}
ted_GotoPosition(sed, tb_GetRow(tb), tb_GetCol(tb));
if (row == tb_GetRow(tb) && col != tb->col && col < tb->exp_len) {
col = tb->col;
}
if (ted_GetRefresh(sed)) {
if (row != tb_GetRow(tb) || before_len - len != tb->len) {
sed_RepaintBelow(sed, (row <= 1) ? 0 : row - 1);
}
else {
sed_RepaintRow(sed, row, col, xlen - col);
}
/* size change */
if (rowcount != (sed_GetMenu(sed))->rowcount) {
sed_SendBorderMsg(sed, BDM_SCROLL, NULL, NULL);
}
}
return(*oldchr);
}