📄 textbox.c
字号:
/* ------------- textbox.c ------------ */
#include "dflat.h"
static void ComputeWindowTop(DFWINDOW);
static void ComputeWindowLeft(DFWINDOW);
static int ComputeVScrollBox(DFWINDOW);
static int ComputeHScrollBox(DFWINDOW);
static void MoveScrollBox(DFWINDOW, int);
static char *GetTextLine(DFWINDOW, int);
BOOL DfVSliding;
BOOL DfHSliding;
/* ------------ DFM_ADDTEXT Message -------------- */
static BOOL AddTextMsg(DFWINDOW wnd, char *txt)
{
/* --- append text to the textbox's buffer --- */
unsigned adln = strlen(txt);
if (adln > (unsigned)0xfff0)
return FALSE;
if (wnd->text != NULL) {
/* ---- appending to existing text ---- */
unsigned txln = strlen(wnd->text);
if ((long)txln+adln > (unsigned) 0xfff0)
return FALSE;
if (txln+adln > wnd->textlen) {
wnd->text = DfRealloc(wnd->text, txln+adln+3);
wnd->textlen = txln+adln+1;
}
}
else {
/* ------ 1st text appended ------ */
wnd->text = DfCalloc(1, adln+3);
wnd->textlen = adln+1;
}
if (wnd->text != NULL) {
/* ---- append the text ---- */
strcat(wnd->text, txt);
strcat(wnd->text, "\n");
DfBuildTextPointers(wnd);
return TRUE;
}
return FALSE;
}
/* ------------ DFM_DELETETEXT Message -------------- */
static void DeleteTextMsg(DFWINDOW wnd, int lno)
{
char *cp1 = DfTextLine(wnd, lno);
--wnd->wlines;
if (lno == wnd->wlines)
*cp1 = '\0';
else {
char *cp2 = DfTextLine(wnd, lno+1);
memmove(cp1, cp2, strlen(cp2)+1);
}
DfBuildTextPointers(wnd);
}
/* ------------ DFM_INSERTTEXT Message -------------- */
static void InsertTextMsg(DFWINDOW wnd, char *txt, int lno)
{
if (AddTextMsg(wnd, txt)) {
int len = strlen(txt);
char *cp2 = DfTextLine(wnd, lno);
char *cp1 = cp2+len+1;
memmove(cp1, cp2, strlen(cp2)-len);
strcpy(cp2, txt);
*(cp2+len) = '\n';
DfBuildTextPointers(wnd);
}
}
/* ------------ DFM_SETTEXT Message -------------- */
static void SetTextMsg(DFWINDOW wnd, char *txt)
{
/* -- assign new text value to textbox buffer -- */
unsigned int len = strlen(txt)+1;
DfSendMessage(wnd, DFM_CLEARTEXT, 0, 0);
wnd->textlen = len;
wnd->text=DfRealloc(wnd->text, len+1);
wnd->text[len] = '\0';
strcpy(wnd->text, txt);
DfBuildTextPointers(wnd);
}
/* ------------ DFM_CLEARTEXT Message -------------- */
static void ClearTextMsg(DFWINDOW wnd)
{
/* ----- clear text from textbox ----- */
if (wnd->text != NULL)
free(wnd->text);
wnd->text = NULL;
wnd->textlen = 0;
wnd->wlines = 0;
wnd->textwidth = 0;
wnd->wtop = wnd->wleft = 0;
DfClearTextBlock(wnd);
DfClearTextPointers(wnd);
}
/* ------------ DFM_KEYBOARD Message -------------- */
static int KeyboardMsg(DFWINDOW wnd, DF_PARAM p1)
{
switch ((int) p1) {
case DF_UP:
return DfSendMessage(wnd,DFM_SCROLL,FALSE,0);
case DF_DN:
return DfSendMessage(wnd,DFM_SCROLL,TRUE,0);
case DF_FWD:
return DfSendMessage(wnd,DFM_HORIZSCROLL,TRUE,0);
case DF_BS:
return DfSendMessage(wnd,DFM_HORIZSCROLL,FALSE,0);
case DF_PGUP:
return DfSendMessage(wnd,DFM_SCROLLPAGE,FALSE,0);
case DF_PGDN:
return DfSendMessage(wnd,DFM_SCROLLPAGE,TRUE,0);
case DF_CTRL_PGUP:
return DfSendMessage(wnd,DFM_HORIZPAGE,FALSE,0);
case DF_CTRL_PGDN:
return DfSendMessage(wnd,DFM_HORIZPAGE,TRUE,0);
case DF_HOME:
return DfSendMessage(wnd,DFM_SCROLLDOC,TRUE,0);
case DF_END:
return DfSendMessage(wnd,DFM_SCROLLDOC,FALSE,0);
default:
break;
}
return FALSE;
}
/* ------------ DFM_LEFT_BUTTON Message -------------- */
static int LeftButtonMsg(DFWINDOW wnd, DF_PARAM p1, DF_PARAM p2)
{
int mx = (int) p1 - DfGetLeft(wnd);
int my = (int) p2 - DfGetTop(wnd);
if (DfTestAttribute(wnd, DF_VSCROLLBAR) &&
mx == DfWindowWidth(wnd)-1) {
/* -------- in the right border ------- */
if (my == 0 || my == DfClientHeight(wnd)+1)
/* --- above or below the scroll bar --- */
return FALSE;
if (my == 1)
/* -------- top scroll button --------- */
return DfSendMessage(wnd, DFM_SCROLL, FALSE, 0);
if (my == DfClientHeight(wnd))
/* -------- bottom scroll button --------- */
return DfSendMessage(wnd, DFM_SCROLL, TRUE, 0);
/* ---------- in the scroll bar ----------- */
if (!DfVSliding && my-1 == wnd->VScrollBox) {
DFRECT rc;
DfVSliding = TRUE;
rc.lf = rc.rt = DfGetRight(wnd);
rc.tp = DfGetTop(wnd)+2;
rc.bt = DfGetBottom(wnd)-2;
return DfSendMessage(NULL, DFM_MOUSE_TRAVEL,
(DF_PARAM) &rc, 0);
}
if (my-1 < wnd->VScrollBox)
return DfSendMessage(wnd,DFM_SCROLLPAGE,FALSE,0);
if (my-1 > wnd->VScrollBox)
return DfSendMessage(wnd,DFM_SCROLLPAGE,TRUE,0);
}
if (DfTestAttribute(wnd, DF_HSCROLLBAR) &&
my == DfWindowHeight(wnd)-1) {
/* -------- in the bottom border ------- */
if (mx == 0 || my == DfClientWidth(wnd)+1)
/* ------ outside the scroll bar ---- */
return FALSE;
if (mx == 1)
return DfSendMessage(wnd, DFM_HORIZSCROLL,FALSE,0);
if (mx == DfWindowWidth(wnd)-2)
return DfSendMessage(wnd, DFM_HORIZSCROLL,TRUE,0);
if (!DfHSliding && mx-1 == wnd->HScrollBox) {
/* --- hit the scroll box --- */
DFRECT rc;
rc.lf = DfGetLeft(wnd)+2;
rc.rt = DfGetRight(wnd)-2;
rc.tp = rc.bt = DfGetBottom(wnd);
/* - keep the mouse in the scroll bar - */
DfSendMessage(NULL,DFM_MOUSE_TRAVEL,(DF_PARAM)&rc,0);
DfHSliding = TRUE;
return TRUE;
}
if (mx-1 < wnd->HScrollBox)
return DfSendMessage(wnd,DFM_HORIZPAGE,FALSE,0);
if (mx-1 > wnd->HScrollBox)
return DfSendMessage(wnd,DFM_HORIZPAGE,TRUE,0);
}
return FALSE;
}
/* ------------ MOUSE_MOVED Message -------------- */
static BOOL MouseMovedMsg(DFWINDOW wnd, DF_PARAM p1, DF_PARAM p2)
{
int mx = (int) p1 - DfGetLeft(wnd);
int my = (int) p2 - DfGetTop(wnd);
if (DfVSliding) {
/* ---- dragging the vertical scroll box --- */
if (my-1 != wnd->VScrollBox) {
DfForeground = DfFrameForeground(wnd);
DfBackground = DfFrameBackground(wnd);
DfWPutch(wnd, DF_SCROLLBARCHAR, DfWindowWidth(wnd)-1,
wnd->VScrollBox+1);
wnd->VScrollBox = my-1;
DfWPutch(wnd, DF_SCROLLBOXCHAR, DfWindowWidth(wnd)-1,
my);
}
return TRUE;
}
if (DfHSliding) {
/* --- dragging the horizontal scroll box --- */
if (mx-1 != wnd->HScrollBox) {
DfForeground = DfFrameForeground(wnd);
DfBackground = DfFrameBackground(wnd);
DfWPutch(wnd, DF_SCROLLBARCHAR, wnd->HScrollBox+1,
DfWindowHeight(wnd)-1);
wnd->HScrollBox = mx-1;
DfWPutch(wnd, DF_SCROLLBOXCHAR, mx, DfWindowHeight(wnd)-1);
}
return TRUE;
}
return FALSE;
}
/* ------------ BUTTON_RELEASED Message -------------- */
static void ButtonReleasedMsg(DFWINDOW wnd)
{
if (DfHSliding || DfVSliding) {
/* release the mouse ouside the scroll bar */
DfSendMessage(NULL, DFM_MOUSE_TRAVEL, 0, 0);
DfVSliding ? ComputeWindowTop(wnd):ComputeWindowLeft(wnd);
DfSendMessage(wnd, DFM_PAINT, 0, 0);
DfSendMessage(wnd, DFM_KEYBOARD_CURSOR, 0, 0);
DfVSliding = DfHSliding = FALSE;
}
}
/* ------------ DFM_SCROLL Message -------------- */
static BOOL ScrollMsg(DFWINDOW wnd, DF_PARAM p1)
{
/* ---- vertical scroll one line ---- */
if (p1) {
/* ----- scroll one line up ----- */
if (wnd->wtop+DfClientHeight(wnd) >= wnd->wlines)
return FALSE;
wnd->wtop++;
}
else {
/* ----- scroll one line down ----- */
if (wnd->wtop == 0)
return FALSE;
--wnd->wtop;
}
if (DfIsVisible(wnd)) {
DFRECT rc;
rc = DfClipRectangle(wnd, DfClientRect(wnd));
if (DfValidRect(rc)) {
/* ---- scroll the window ----- */
if (wnd != DfInFocus)
DfSendMessage(wnd, DFM_PAINT, 0, 0);
else {
DfScrollWindow(wnd, rc, (int)p1);
if (!(int)p1)
/* -- write top line (down) -- */
DfWriteTextLine(wnd,NULL,wnd->wtop,FALSE);
else {
/* -- write bottom line (up) -- */
int y=DfRectBottom(rc)-DfGetClientTop(wnd);
DfWriteTextLine(wnd, NULL,
wnd->wtop+y, FALSE);
}
}
}
/* ---- reset the scroll box ---- */
if (DfTestAttribute(wnd, DF_VSCROLLBAR)) {
int vscrollbox = ComputeVScrollBox(wnd);
if (vscrollbox != wnd->VScrollBox)
MoveScrollBox(wnd, vscrollbox);
}
}
return TRUE;
}
/* ------------ DFM_HORIZSCROLL Message -------------- */
static BOOL HorizScrollMsg(DFWINDOW wnd, DF_PARAM p1)
{
/* --- horizontal scroll one column --- */
if (p1) {
/* --- scroll left --- */
if (wnd->wleft + DfClientWidth(wnd)-1 >= wnd->textwidth)
return FALSE;
wnd->wleft++;
}
else {
/* --- scroll right --- */
if (wnd->wleft == 0)
return FALSE;
--wnd->wleft;
}
DfSendMessage(wnd, DFM_PAINT, 0, 0);
return TRUE;
}
/* ------------ DFM_SCROLLPAGE Message -------------- */
static void ScrollPageMsg(DFWINDOW wnd, DF_PARAM p1)
{
/* --- vertical scroll one page --- */
if ((int) p1 == FALSE) {
/* ---- page up ---- */
if (wnd->wtop)
wnd->wtop -= DfClientHeight(wnd);
}
else {
/* ---- page down ---- */
if (wnd->wtop+DfClientHeight(wnd) < wnd->wlines) {
wnd->wtop += DfClientHeight(wnd);
if (wnd->wtop>wnd->wlines-DfClientHeight(wnd))
wnd->wtop=wnd->wlines-DfClientHeight(wnd);
}
}
if (wnd->wtop < 0)
wnd->wtop = 0;
DfSendMessage(wnd, DFM_PAINT, 0, 0);
}
/* ------------ HORIZSCROLLPAGE Message -------------- */
static void HorizScrollPageMsg(DFWINDOW wnd, DF_PARAM p1)
{
/* --- horizontal scroll one page --- */
if ((int) p1 == FALSE)
/* ---- page left ----- */
wnd->wleft -= DfClientWidth(wnd);
else {
/* ---- page right ----- */
wnd->wleft += DfClientWidth(wnd);
if (wnd->wleft > wnd->textwidth-DfClientWidth(wnd))
wnd->wleft = wnd->textwidth-DfClientWidth(wnd);
}
if (wnd->wleft < 0)
wnd->wleft = 0;
DfSendMessage(wnd, DFM_PAINT, 0, 0);
}
/* ------------ DFM_SCROLLDOC Message -------------- */
static void ScrollDocMsg(DFWINDOW wnd, DF_PARAM p1)
{
/* --- scroll to beginning or end of document --- */
if ((int) p1)
wnd->wtop = wnd->wleft = 0;
else if (wnd->wtop+DfClientHeight(wnd) < wnd->wlines){
wnd->wtop = wnd->wlines-DfClientHeight(wnd);
wnd->wleft = 0;
}
DfSendMessage(wnd, DFM_PAINT, 0, 0);
}
/* ------------ DFM_PAINT Message -------------- */
static void PaintMsg(DFWINDOW wnd, DF_PARAM p1, DF_PARAM p2)
{
/* ------ paint the client area ----- */
DFRECT rc, rcc;
int y;
char blankline[201];
/* ----- build the rectangle to paint ----- */
if ((DFRECT *)p1 == NULL)
rc=DfRelativeWindowRect(wnd, DfWindowRect(wnd));
else
rc= *(DFRECT *)p1;
if (DfTestAttribute(wnd, DF_HASBORDER) &&
DfRectRight(rc) >= DfWindowWidth(wnd)-1) {
if (DfRectLeft(rc) >= DfWindowWidth(wnd)-1)
return;
DfRectRight(rc) = DfWindowWidth(wnd)-2;
}
rcc = DfAdjustRectangle(wnd, rc);
if (!p2 && wnd != DfInFocus)
DfClipString++;
/* ----- blank line for padding ----- */
memset(blankline, ' ', DfGetScreenWidth());
blankline[DfRectRight(rcc)+1] = '\0';
/* ------- each line DfWithin rectangle ------ */
for (y = DfRectTop(rc); y <= DfRectBottom(rc); y++){
int yy;
/* ---- test outside of Client area ---- */
if (DfTestAttribute(wnd,
DF_HASBORDER | DF_HASTITLEBAR)) {
if (y < DfTopBorderAdj(wnd))
continue;
if (y > DfWindowHeight(wnd)-2)
continue;
}
yy = y-DfTopBorderAdj(wnd);
if (yy < wnd->wlines-wnd->wtop)
/* ---- paint a text line ---- */
DfWriteTextLine(wnd, &rc,
yy+wnd->wtop, FALSE);
else {
/* ---- paint a blank line ---- */
DfSetStandardColor(wnd);
DfWriteLine(wnd, blankline+DfRectLeft(rcc),
DfRectLeft(rcc)+DfBorderAdj(wnd), y, FALSE);
}
}
/* ------- position the scroll box ------- */
if (DfTestAttribute(wnd, DF_VSCROLLBAR|DF_HSCROLLBAR)) {
int hscrollbox = ComputeHScrollBox(wnd);
int vscrollbox = ComputeVScrollBox(wnd);
if (hscrollbox != wnd->HScrollBox ||
vscrollbox != wnd->VScrollBox) {
wnd->HScrollBox = hscrollbox;
wnd->VScrollBox = vscrollbox;
DfSendMessage(wnd, DFM_BORDER, p1, 0);
}
}
if (!p2 && wnd != DfInFocus)
--DfClipString;
}
/* ------------ DFM_CLOSE_WINDOW Message -------------- */
static void CloseWindowMsg(DFWINDOW wnd)
{
DfSendMessage(wnd, DFM_CLEARTEXT, 0, 0);
if (wnd->TextPointers != NULL) {
free(wnd->TextPointers);
wnd->TextPointers = NULL;
}
}
/* ----------- DF_TEXTBOX Message-processing Module ----------- */
int DfTextBoxProc(DFWINDOW wnd, DFMESSAGE msg, DF_PARAM p1, DF_PARAM p2)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -