📄 textbox.c
字号:
if (hscrollbox != wnd->HScrollBox ||
vscrollbox != wnd->VScrollBox) {
wnd->HScrollBox = hscrollbox;
wnd->VScrollBox = vscrollbox;
SendMessage(wnd, BORDER, p1, 0);
}
}
if (!p2 && wnd != inFocus)
--ClipString;
}
/* ------------ CLOSE_WINDOW Message -------------- */
static void CloseWindowMsg(WINDOW wnd)
{
SendMessage(wnd, CLEARTEXT, 0, 0);
if (wnd->TextPointers != NULL) {
free(wnd->TextPointers);
wnd->TextPointers = NULL;
}
}
/* ----------- TEXTBOX Message-processing Module ----------- */
int TextBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
switch (msg) {
case CREATE_WINDOW:
wnd->HScrollBox = wnd->VScrollBox = 1;
ClearTextPointers(wnd);
break;
case ADDTEXT:
return AddTextMsg(wnd, (char *) p1);
case DELETETEXT:
DeleteTextMsg(wnd, (int) p1);
return TRUE;
case INSERTTEXT:
InsertTextMsg(wnd, (char *) p1, (int) p2);
return TRUE;
case SETTEXT:
SetTextMsg(wnd, (char *) p1);
return TRUE;
case CLEARTEXT:
ClearTextMsg(wnd);
break;
case KEYBOARD:
if (WindowMoving || WindowSizing)
break;
if (KeyboardMsg(wnd, p1))
return TRUE;
break;
case LEFT_BUTTON:
if (WindowSizing || WindowMoving)
return FALSE;
if (LeftButtonMsg(wnd, p1, p2))
return TRUE;
break;
case MOUSE_MOVED:
if (MouseMovedMsg(wnd, p1, p2))
return TRUE;
break;
case BUTTON_RELEASED:
ButtonReleasedMsg(wnd);
break;
case SCROLL:
return ScrollMsg(wnd, p1);
case HORIZSCROLL:
return HorizScrollMsg(wnd, p1);
case SCROLLPAGE:
ScrollPageMsg(wnd, p1);
return TRUE;
case HORIZPAGE:
HorizScrollPageMsg(wnd, p1);
return TRUE;
case SCROLLDOC:
ScrollDocMsg(wnd, p1);
return TRUE;
case PAINT:
if (isVisible(wnd)) {
PaintMsg(wnd, p1, p2);
return FALSE;
}
break;
case CLOSE_WINDOW:
CloseWindowMsg(wnd);
break;
default:
break;
}
return BaseWndProc(TEXTBOX, wnd, msg, p1, p2);
}
/* ------ compute the vertical scroll box position from
the text pointers --------- */
static int ComputeVScrollBox(WINDOW wnd)
{
int pagelen = wnd->wlines - ClientHeight(wnd);
int barlen = ClientHeight(wnd)-2;
int lines_tick;
int vscrollbox;
if (pagelen < 1 || barlen < 1)
vscrollbox = 1;
else {
if (pagelen > barlen)
lines_tick = pagelen / barlen;
else
lines_tick = barlen / pagelen;
vscrollbox = 1 + (wnd->wtop / lines_tick);
if (vscrollbox > ClientHeight(wnd)-2 ||
wnd->wtop + ClientHeight(wnd) >= wnd->wlines)
vscrollbox = ClientHeight(wnd)-2;
}
return vscrollbox;
}
/* ---- compute top text line from scroll box position ---- */
static void ComputeWindowTop(WINDOW wnd)
{
int pagelen = wnd->wlines - ClientHeight(wnd);
if (wnd->VScrollBox == 0)
wnd->wtop = 0;
else if (wnd->VScrollBox == ClientHeight(wnd)-2)
wnd->wtop = pagelen;
else {
int barlen = ClientHeight(wnd)-2;
int lines_tick;
if (pagelen > barlen)
lines_tick = barlen ? (pagelen / barlen) : 0;
else
lines_tick = pagelen ? (barlen / pagelen) : 0;
wnd->wtop = (wnd->VScrollBox-1) * lines_tick;
if (wnd->wtop + ClientHeight(wnd) > wnd->wlines)
wnd->wtop = pagelen;
}
if (wnd->wtop < 0)
wnd->wtop = 0;
}
/* ------ compute the horizontal scroll box position from
the text pointers --------- */
static int ComputeHScrollBox(WINDOW wnd)
{
int pagewidth = wnd->textwidth - ClientWidth(wnd);
int barlen = ClientWidth(wnd)-2;
int chars_tick;
int hscrollbox;
if (pagewidth < 1 || barlen < 1)
hscrollbox = 1;
else {
if (pagewidth > barlen)
chars_tick = barlen ? (pagewidth / barlen) : 0;
else
chars_tick = pagewidth ? (barlen / pagewidth) : 0;
hscrollbox = 1 + (chars_tick ? (wnd->wleft / chars_tick) : 0);
if (hscrollbox > ClientWidth(wnd)-2 ||
wnd->wleft + ClientWidth(wnd) >= wnd->textwidth)
hscrollbox = ClientWidth(wnd)-2;
}
return hscrollbox;
}
/* ---- compute left column from scroll box position ---- */
static void ComputeWindowLeft(WINDOW wnd)
{
int pagewidth = wnd->textwidth - ClientWidth(wnd);
if (wnd->HScrollBox == 0)
wnd->wleft = 0;
else if (wnd->HScrollBox == ClientWidth(wnd)-2)
wnd->wleft = pagewidth;
else {
int barlen = ClientWidth(wnd)-2;
int chars_tick;
if (pagewidth > barlen)
chars_tick = pagewidth / barlen;
else
chars_tick = barlen / pagewidth;
wnd->wleft = (wnd->HScrollBox-1) * chars_tick;
if (wnd->wleft + ClientWidth(wnd) > wnd->textwidth)
wnd->wleft = pagewidth;
}
if (wnd->wleft < 0)
wnd->wleft = 0;
}
/* ----- get the text to a specified line ----- */
static char *GetTextLine(WINDOW wnd, int selection)
{
char *line;
int len = 0;
char *cp, *cp1;
cp = cp1 = TextLine(wnd, selection);
while (*cp && *cp != '\n') {
len++;
cp++;
}
line = DFmalloc(len+7);
memmove(line, cp1, len);
line[len] = '\0';
return line;
}
/* ------- write a line of text to a textbox window ------- */
void WriteTextLine(WINDOW wnd, RECT *rcc, int y, BOOL reverse)
{
int len = 0;
int dif = 0;
unsigned char line[200];
RECT rc;
unsigned char *lp, *svlp;
int lnlen;
int i;
BOOL trunc = FALSE;
/* ------ make sure y is inside the window ----- */
if (y < wnd->wtop || y >= wnd->wtop+ClientHeight(wnd))
return;
/* ---- build the retangle within which can write ---- */
if (rcc == NULL) {
rc = RelativeWindowRect(wnd, WindowRect(wnd));
if (TestAttribute(wnd, HASBORDER) &&
RectRight(rc) >= WindowWidth(wnd)-1)
RectRight(rc) = WindowWidth(wnd)-2;
}
else
rc = *rcc;
/* ----- make sure rectangle is within window ------ */
if (RectLeft(rc) >= WindowWidth(wnd)-1)
return;
if (RectRight(rc) == 0)
return;
rc = AdjustRectangle(wnd, rc);
if (y-wnd->wtop<RectTop(rc) || y-wnd->wtop>RectBottom(rc))
return;
/* --- get the text and length of the text line --- */
lp = svlp = GetTextLine(wnd, y);
if (svlp == NULL)
return;
lnlen = LineLength(lp);
if (wnd->protect) {
char *pp = lp;
while (*pp) {
if (isprint(*pp))
*pp = '*';
pp++;
}
}
/* -------- insert block color change controls ------- */
if (TextBlockMarked(wnd)) {
int bbl = wnd->BlkBegLine;
int bel = wnd->BlkEndLine;
int bbc = wnd->BlkBegCol;
int bec = wnd->BlkEndCol;
int by = y;
/* ----- put lowest marker first ----- */
if (bbl > bel) {
swap(bbl, bel);
swap(bbc, bec);
}
if (bbl == bel && bbc > bec)
swap(bbc, bec);
if (by >= bbl && by <= bel) {
/* ------ the block includes this line ----- */
int blkbeg = 0;
int blkend = lnlen;
if (!(by > bbl && by < bel)) {
/* --- the entire line is not in the block -- */
if (by == bbl)
/* ---- the block begins on this line --- */
blkbeg = bbc;
if (by == bel)
/* ---- the block ends on this line ---- */
blkend = bec;
}
if (blkend == 0 && lnlen == 0) {
strcpy(lp, " ");
blkend++;
}
/* ----- insert the reset color token ----- */
memmove(lp+blkend+1,lp+blkend,strlen(lp+blkend)+1);
lp[blkend] = RESETCOLOR;
/* ----- insert the change color token ----- */
memmove(lp+blkbeg+3,lp+blkbeg,strlen(lp+blkbeg)+1);
lp[blkbeg] = CHANGECOLOR;
/* ----- insert the color tokens ----- */
SetReverseColor(wnd);
lp[blkbeg+1] = foreground | 0x80;
lp[blkbeg+2] = background | 0x80;
lnlen += 4;
}
}
/* - make sure left margin doesn't overlap color change - */
for (i = 0; i < wnd->wleft+3; i++) {
if (*(lp+i) == '\0')
break;
if (*(unsigned char *)(lp + i) == RESETCOLOR)
break;
}
if (*(lp+i) && i < wnd->wleft+3) {
if (wnd->wleft+4 > lnlen)
trunc = TRUE;
else
lp += 4;
}
else {
/* --- it does, shift the color change over --- */
for (i = 0; i < wnd->wleft; i++) {
if (*(lp+i) == '\0')
break;
if (*(unsigned char *)(lp + i) == CHANGECOLOR) {
*(lp+wnd->wleft+2) = *(lp+i+2);
*(lp+wnd->wleft+1) = *(lp+i+1);
*(lp+wnd->wleft) = *(lp+i);
break;
}
}
}
/* ------ build the line to display -------- */
if (!trunc) {
if (lnlen < wnd->wleft)
lnlen = 0;
else
lp += wnd->wleft;
if (lnlen > RectLeft(rc)) {
/* ---- the line exceeds the rectangle ---- */
int ct = RectLeft(rc);
char *initlp = lp;
/* --- point to end of clipped line --- */
while (ct) {
if (*(unsigned char *)lp == CHANGECOLOR)
lp += 3;
else if (*(unsigned char *)lp == RESETCOLOR)
lp++;
else
lp++, --ct;
}
if (RectLeft(rc)) {
char *lpp = lp;
while (*lpp) {
if (*(unsigned char*)lpp==CHANGECOLOR)
break;
if (*(unsigned char*)lpp==RESETCOLOR) {
lpp = lp;
while (lpp >= initlp) {
if (*(unsigned char *)lpp ==
CHANGECOLOR) {
lp -= 3;
memmove(lp,lpp,3);
break;
}
--lpp;
}
break;
}
lpp++;
}
}
lnlen = LineLength(lp);
len = min(lnlen, RectWidth(rc));
dif = strlen(lp) - lnlen;
len += dif;
if (len > 0)
strncpy(line, lp, len);
}
}
/* -------- pad the line --------- */
while (len < RectWidth(rc)+dif)
line[len++] = ' ';
line[len] = '\0';
dif = 0;
/* ------ establish the line's main color ----- */
if (reverse) {
char *cp = line;
SetReverseColor(wnd);
while ((cp = strchr(cp, CHANGECOLOR)) != NULL) {
cp += 2;
*cp++ = background | 0x80;
}
if (*(unsigned char *)line == CHANGECOLOR)
dif = 3;
}
else
SetStandardColor(wnd);
/* ------- display the line -------- */
writeline(wnd, line+dif,
RectLeft(rc)+BorderAdj(wnd),
y-wnd->wtop+TopBorderAdj(wnd), FALSE);
free(svlp);
}
void MarkTextBlock(WINDOW wnd, int BegLine, int BegCol,
int EndLine, int EndCol)
{
wnd->BlkBegLine = BegLine;
wnd->BlkEndLine = EndLine;
wnd->BlkBegCol = BegCol;
wnd->BlkEndCol = EndCol;
}
/* ----- clear and initialize text line pointer array ----- */
void ClearTextPointers(WINDOW wnd)
{
wnd->TextPointers = DFrealloc(wnd->TextPointers, sizeof(int));
*(wnd->TextPointers) = 0;
}
#define INITLINES 100
/* ---- build array of pointers to text lines ---- */
void BuildTextPointers(WINDOW wnd)
{
char *cp = wnd->text, *cp1;
int incrs = INITLINES;
unsigned int off;
wnd->textwidth = wnd->wlines = 0;
while (*cp) {
if (incrs == INITLINES) {
incrs = 0;
wnd->TextPointers = DFrealloc(wnd->TextPointers,
(wnd->wlines + INITLINES) * sizeof(int));
}
off = (unsigned int) (cp - wnd->text);
*((wnd->TextPointers) + wnd->wlines) = off;
wnd->wlines++;
incrs++;
cp1 = cp;
while (*cp && *cp != '\n')
cp++;
wnd->textwidth = max(wnd->textwidth,
(unsigned int) (cp - cp1));
if (*cp)
cp++;
}
}
static void MoveScrollBox(WINDOW wnd, int vscrollbox)
{
foreground = FrameForeground(wnd);
background = FrameBackground(wnd);
wputch(wnd, SCROLLBARCHAR, WindowWidth(wnd)-1,
wnd->VScrollBox+1);
wputch(wnd, SCROLLBOXCHAR, WindowWidth(wnd)-1,
vscrollbox+1);
wnd->VScrollBox = vscrollbox;
}
int TextLineNumber(WINDOW wnd, char *lp)
{
int lineno;
char *cp;
for (lineno = 0; lineno < wnd->wlines; lineno++) {
cp = wnd->text + *((wnd->TextPointers) + lineno);
if (cp == lp)
return lineno;
if (cp > lp)
break;
}
return lineno-1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -