#include <xterm.h>
#include <data.h>
#include <menu.h>
#include <assert.h>
void
CursorSet(TScreen * screen, int row, int col, unsigned flags)
{
int use_row = row;
int max_row;
col = (col < 0 ? 0 : col);
set_cur_col(screen, (col <= screen->max_col ? col : screen->max_col));
max_row = screen->max_row;
if (flags & ORIGIN) {
use_row += screen->top_marg;
max_row = screen->bot_marg;
}
use_row = (use_row < 0 ? 0 : use_row);
set_cur_row(screen, (use_row <= max_row ? use_row : max_row));
screen->do_wrap = False;
TRACE(("CursorSet(%d,%d) margins [%d..%d] -> %d,%d %s\n",
row, col,
screen->top_marg,
screen->bot_marg,
screen->cur_row,
screen->cur_col,
(flags & ORIGIN ? "origin" : "normal")));
}
void
CursorBack(XtermWidget xw, int n)
{
TScreen *screen = &xw->screen;
int i, j, k, rev;
if ((rev = (xw->flags & (REVERSEWRAP | WRAPAROUND)) ==
(REVERSEWRAP | WRAPAROUND)) != 0
&& screen->do_wrap)
n--;
if ((screen->cur_col -= n) < 0) {
if (rev) {
if ((i = ((j = MaxCols(screen))
* screen->cur_row) + screen->cur_col) < 0) {
k = j * MaxRows(screen);
i += ((-i) / k + 1) * k;
}
set_cur_row(screen, i / j);
set_cur_col(screen, i % j);
do_xevents();
} else {
set_cur_col(screen, 0);
}
}
screen->do_wrap = False;
}
void
CursorForward(TScreen * screen, int n)
{
int next = screen->cur_col + n;
int max = CurMaxCol(screen, screen->cur_row);
if (next > max)
next = max;
set_cur_col(screen, next);
screen->do_wrap = False;
}
void
CursorDown(TScreen * screen, int n)
{
int max;
int next = screen->cur_row + n;
max = (screen->cur_row > screen->bot_marg ?
screen->max_row : screen->bot_marg);
if (next > max)
next = max;
if (next > screen->max_row)
next = screen->max_row;
set_cur_row(screen, next);
screen->do_wrap = False;
}
void
CursorUp(TScreen * screen, int n)
{
int min;
int next = screen->cur_row - n;
min = ((screen->cur_row < screen->top_marg)
? 0
: screen->top_marg);
if (next < min)
next = min;
if (next < 0)
next = 0;
set_cur_row(screen, next);
screen->do_wrap = False;
}
void
xtermIndex(XtermWidget xw, int amount)
{
TScreen *screen = &xw->screen;
int j;
if (screen->cur_row > screen->bot_marg
|| screen->cur_row + amount <= screen->bot_marg) {
CursorDown(screen, amount);
return;
}
CursorDown(screen, j = screen->bot_marg - screen->cur_row);
xtermScroll(xw, amount - j);
}
void
RevIndex(XtermWidget xw, int amount)
{
TScreen *screen = &xw->screen;
if (screen->cur_row < screen->top_marg
|| screen->cur_row - amount >= screen->top_marg) {
CursorUp(screen, amount);
return;
}
RevScroll(xw, amount - (screen->cur_row - screen->top_marg));
CursorUp(screen, screen->cur_row - screen->top_marg);
}
void
CarriageReturn(TScreen * screen)
{
set_cur_col(screen, 0);
screen->do_wrap = False;
do_xevents();
}
void
AdjustSavedCursor(XtermWidget xw, int adjust)
{
TScreen *screen = &xw->screen;
if (screen->alternate) {
SavedCursor *sc = &screen->sc[screen->alternate == False];
if (adjust > 0) {
TRACE(("AdjustSavedCursor %d -> %d\n", sc->row, sc->row - adjust));
sc->row += adjust;
}
}
}
void
CursorSave(XtermWidget xw)
{
TScreen *screen = &xw->screen;
SavedCursor *sc = &screen->sc[screen->alternate != False];
sc->saved = True;
sc->row = screen->cur_row;
sc->col = screen->cur_col;
sc->flags = xw->flags;
sc->curgl = screen->curgl;
sc->curgr = screen->curgr;
#if OPT_ISO_COLORS
sc->cur_foreground = xw->cur_foreground;
sc->cur_background = xw->cur_background;
sc->sgr_foreground = xw->sgr_foreground;
#endif
memmove(sc->gsets, screen->gsets, sizeof(screen->gsets));
}
#define DECSC_FLAGS (ATTRIBUTES|ORIGIN|WRAPAROUND|PROTECTED)
void
CursorRestore(XtermWidget xw)
{
TScreen *screen = &xw->screen;
SavedCursor *sc = &screen->sc[screen->alternate != False];
if (sc->saved) {
memmove(screen->gsets, sc->gsets, sizeof(screen->gsets));
screen->curgl = sc->curgl;
screen->curgr = sc->curgr;
} else {
resetCharsets(screen);
}
xw->flags &= ~DECSC_FLAGS;
xw->flags |= sc->flags & DECSC_FLAGS;
CursorSet(screen,
((xw->flags & ORIGIN)
? sc->row - screen->top_marg
: sc->row),
sc->col, xw->flags);
#if OPT_ISO_COLORS
xw->sgr_foreground = sc->sgr_foreground;
SGR_Foreground(xw, xw->flags & FG_COLOR ? sc->cur_foreground : -1);
SGR_Background(xw, xw->flags & BG_COLOR ? sc->cur_background : -1);
#endif
update_autowrap();
}
void
CursorNextLine(TScreen * screen, int count)
{
CursorDown(screen, count < 1 ? 1 : count);
CarriageReturn(screen);
do_xevents();
}
void
CursorPrevLine(TScreen * screen, int count)
{
CursorUp(screen, count < 1 ? 1 : count);
CarriageReturn(screen);
do_xevents();
}
#if OPT_TRACE
int
set_cur_row(TScreen * screen, int value)
{
assert(screen != 0);
assert(value >= 0);
assert(value <= screen->max_row);
screen->cur_row = value;
return value;
}
int
set_cur_col(TScreen * screen, int value)
{
assert(screen != 0);
assert(value >= 0);
assert(value <= screen->max_col);
screen->cur_col = value;
return value;
}
#endif