📄 editbox.c
字号:
/* ------ change all text lines in block to \n ----- */
while (bbl < bel) {
char *cp = strchr(bbl, '\n');
if (cp > bel)
cp = bel;
strcpy(bbl, cp);
bel -= (int) (cp - bbl);
bbl++;
}
DfClearTextBlock(wnd);
DfBuildTextPointers(wnd);
DfSendMessage(wnd, DFM_KEYBOARD_CURSOR, DfWndCol, wnd->WndRow);
wnd->TextChanged = TRUE;
}
}
/* ----------- DF_ID_UNDO Command ---------- */
static void UndoCmd(DFWINDOW wnd)
{
if (wnd->DeletedText != NULL) {
DfPasteText(wnd, wnd->DeletedText, wnd->DeletedLength);
free(wnd->DeletedText);
wnd->DeletedText = NULL;
wnd->DeletedLength = 0;
DfSendMessage(wnd, DFM_PAINT, 0, 0);
}
}
/* ----------- DF_ID_PARAGRAPH Command ---------- */
static void ParagraphCmd(DFWINDOW wnd)
{
int bc, fl;
char *bl, *bbl, *bel, *bb;
DfClearTextBlock(wnd);
/* ---- forming paragraph from DfCursor position --- */
fl = wnd->wtop + wnd->WndRow;
bbl = bel = bl = DfTextLine(wnd, wnd->CurrLine);
if ((bc = wnd->CurrCol) >= DfClientWidth(wnd))
bc = 0;
Home(wnd);
/* ---- locate the end of the paragraph ---- */
while (*bel) {
int blank = TRUE;
char *bll = bel;
/* --- blank line marks end of paragraph --- */
while (*bel && *bel != '\n') {
if (*bel != ' ')
blank = FALSE;
bel++;
}
if (blank) {
bel = bll;
break;
}
if (*bel)
bel++;
}
if (bel == bbl) {
DfSendMessage(wnd, DFM_KEYBOARD, DF_DN, 0);
return;
}
if (*bel == '\0')
--bel;
if (*bel == '\n')
--bel;
/* --- change all newlines in block to spaces --- */
while ((char*)DfCurrChar < bel) {
if (*DfCurrChar == '\n') {
*DfCurrChar = ' ';
wnd->CurrLine++;
wnd->CurrCol = 0;
}
else
wnd->CurrCol++;
}
/* ---- insert newlines at new margin boundaries ---- */
bb = bbl;
while ((char*)bbl < (char*)bel) {
bbl++;
if ((int)(bbl - bb) == DfClientWidth(wnd)-1) {
while (*bbl != ' ' && bbl > bb)
--bbl;
if (*bbl != ' ') {
bbl = strchr(bbl, ' ');
if (bbl == NULL || bbl >= bel)
break;
}
*bbl = '\n';
bb = bbl+1;
}
}
DfBuildTextPointers(wnd);
/* --- put DfCursor back at beginning --- */
wnd->CurrLine = DfTextLineNumber(wnd, bl);
wnd->CurrCol = bc;
if (fl < wnd->wtop)
wnd->wtop = fl;
wnd->WndRow = fl - wnd->wtop;
DfSendMessage(wnd, DFM_PAINT, 0, 0);
DfSendMessage(wnd, DFM_KEYBOARD_CURSOR, DfWndCol, wnd->WndRow);
wnd->TextChanged = TRUE;
DfBuildTextPointers(wnd);
}
/* ----------- COMMAND Message ---------- */
static int CommandMsg(DFWINDOW wnd, DF_PARAM p1)
{
switch ((int)p1) {
case DF_ID_DELETETEXT:
DeleteTextCmd(wnd);
return TRUE;
case DF_ID_CLEAR:
ClearCmd(wnd);
return TRUE;
case DF_ID_UNDO:
UndoCmd(wnd);
return TRUE;
case DF_ID_PARAGRAPH:
ParagraphCmd(wnd);
return TRUE;
default:
break;
}
return FALSE;
}
/* ---------- DFM_CLOSE_WINDOW Message ----------- */
static int CloseWindowMsg(DFWINDOW wnd, DF_PARAM p1, DF_PARAM p2)
{
int rtn;
DfSendMessage(NULL, DFM_HIDE_CURSOR, 0, 0);
if (wnd->DeletedText != NULL)
free(wnd->DeletedText);
if (wnd->text != NULL)
{
free(wnd->text);
wnd->text = NULL;
}
rtn = DfBaseWndProc(DF_EDITBOX, wnd, DFM_CLOSE_WINDOW, p1, p2);
return rtn;
}
/* ------- Window processing module for DF_EDITBOX class ------ */
int DfEditBoxProc(DFWINDOW wnd, DFMESSAGE msg, DF_PARAM p1, DF_PARAM p2)
{
int rtn;
switch (msg) {
case DFM_CREATE_WINDOW:
return CreateWindowMsg(wnd);
case DFM_ADDTEXT:
return AddTextMsg(wnd, p1, p2);
case DFM_SETTEXT:
return SetTextMsg(wnd, p1);
case DFM_CLEARTEXT:
return ClearTextMsg(wnd);
case DFM_GETTEXT:
return GetTextMsg(wnd, p1, p2);
case DFM_SETTEXTLENGTH:
return SetTextLengthMsg(wnd, (unsigned) p1);
case DFM_KEYBOARD_CURSOR:
KeyboardCursorMsg(wnd, p1, p2);
return TRUE;
case DFM_SETFOCUS:
if (!(int)p1)
DfSendMessage(NULL, DFM_HIDE_CURSOR, 0, 0);
case DFM_PAINT:
case DFM_MOVE:
rtn = DfBaseWndProc(DF_EDITBOX, wnd, msg, p1, p2);
DfSendMessage(wnd,DFM_KEYBOARD_CURSOR,DfWndCol,wnd->WndRow);
return rtn;
case DFM_DFM_SIZE:
return SizeMsg(wnd, p1, p2);
case DFM_SCROLL:
return ScrollMsg(wnd, p1);
case DFM_HORIZSCROLL:
return HorizScrollMsg(wnd, p1);
case DFM_SCROLLPAGE:
return ScrollPageMsg(wnd, p1);
case DFM_HORIZPAGE:
return HorizPageMsg(wnd, p1);
case DFM_LEFT_BUTTON:
if (LeftButtonMsg(wnd, p1, p2))
return TRUE;
break;
case MOUSE_MOVED:
if (MouseMovedMsg(wnd, p1, p2))
return TRUE;
break;
case DFM_BUTTON_RELEASED:
if (ButtonReleasedMsg(wnd))
return TRUE;
break;
case DFM_KEYBOARD:
if (KeyboardMsg(wnd, p1, p2))
return TRUE;
break;
case DFM_SHIFT_CHANGED:
ShiftChangedMsg(wnd, p1);
break;
case DFM_COMMAND:
if (CommandMsg(wnd, p1))
return TRUE;
break;
case DFM_CLOSE_WINDOW:
return CloseWindowMsg(wnd, p1, p2);
default:
break;
}
return DfBaseWndProc(DF_EDITBOX, wnd, msg, p1, p2);
}
/* ------ save deleted text for the Undo command ------ */
static void SaveDeletedText(DFWINDOW wnd, char *bbl, int len)
{
wnd->DeletedLength = len;
wnd->DeletedText=DfRealloc(wnd->DeletedText,len);
memmove(wnd->DeletedText, bbl, len);
}
/* ---- DfCursor right key: right one character position ---- */
static void Forward(DFWINDOW wnd)
{
char *cc = DfCurrChar+1;
if (*cc == '\0')
return;
if (*DfCurrChar == '\n') {
Home(wnd);
Downward(wnd);
}
else {
wnd->CurrCol++;
if (DfWndCol == DfClientWidth(wnd))
DfSendMessage(wnd, DFM_HORIZSCROLL, TRUE, 0);
}
}
/* ----- stick the moving DfCursor to the end of the line ---- */
static void StickEnd(DFWINDOW wnd)
{
char *cp = DfTextLine(wnd, wnd->CurrLine);
char *cp1 = strchr(cp, '\n');
int len = cp1 ? (int) (cp1 - cp) : 0;
wnd->CurrCol = min(len, wnd->CurrCol);
if (wnd->wleft > wnd->CurrCol) {
wnd->wleft = max(0, wnd->CurrCol - 4);
DfSendMessage(wnd, DFM_PAINT, 0, 0);
}
else if (wnd->CurrCol-wnd->wleft >= DfClientWidth(wnd)) {
wnd->wleft = wnd->CurrCol - (DfClientWidth(wnd)-1);
DfSendMessage(wnd, DFM_PAINT, 0, 0);
}
}
/* --------- DfCursor down key: down one line --------- */
static void Downward(DFWINDOW wnd)
{
if (DfIsMultiLine(wnd) &&
wnd->WndRow+wnd->wtop+1 < wnd->wlines) {
wnd->CurrLine++;
if (wnd->WndRow == DfClientHeight(wnd)-1)
DfBaseWndProc(DF_EDITBOX, wnd, DFM_SCROLL, TRUE, 0);
else
wnd->WndRow++;
StickEnd(wnd);
}
}
/* -------- DfCursor up key: up one line ------------ */
static void Upward(DFWINDOW wnd)
{
if (DfIsMultiLine(wnd) && wnd->CurrLine != 0) {
--wnd->CurrLine;
if (wnd->WndRow == 0)
DfBaseWndProc(DF_EDITBOX, wnd, DFM_SCROLL, FALSE, 0);
else
--wnd->WndRow;
StickEnd(wnd);
}
}
/* ---- DfCursor left key: left one character position ---- */
static void Backward(DFWINDOW wnd)
{
if (wnd->CurrCol) {
--wnd->CurrCol;
if (wnd->CurrCol < wnd->wleft)
DfSendMessage(wnd, DFM_HORIZSCROLL, FALSE, 0);
}
else if (DfIsMultiLine(wnd) && wnd->CurrLine != 0) {
Upward(wnd);
End(wnd);
}
}
/* -------- End key: to end of line ------- */
static void End(DFWINDOW wnd)
{
while (*DfCurrChar && *DfCurrChar != '\n')
++wnd->CurrCol;
if (DfWndCol >= DfClientWidth(wnd)) {
wnd->wleft = wnd->CurrCol - (DfClientWidth(wnd)-1);
DfSendMessage(wnd, DFM_PAINT, 0, 0);
}
}
/* -------- Home key: to beginning of line ------- */
static void Home(DFWINDOW wnd)
{
wnd->CurrCol = 0;
if (wnd->wleft != 0) {
wnd->wleft = 0;
DfSendMessage(wnd, DFM_PAINT, 0, 0);
}
}
/* -- Ctrl+DfCursor right key: to beginning of next word -- */
static void NextWord(DFWINDOW wnd)
{
int savetop = wnd->wtop;
int saveleft = wnd->wleft;
DfClearVisible(wnd);
while (!isWhite(*DfCurrChar)) {
char *cc = DfCurrChar+1;
if (*cc == '\0')
break;
Forward(wnd);
}
while (isWhite(*DfCurrChar)) {
char *cc = DfCurrChar+1;
if (*cc == '\0')
break;
Forward(wnd);
}
DfSetVisible(wnd);
DfSendMessage(wnd, DFM_KEYBOARD_CURSOR, DfWndCol, wnd->WndRow);
if (wnd->wtop != savetop || wnd->wleft != saveleft)
DfSendMessage(wnd, DFM_PAINT, 0, 0);
}
/* -- Ctrl+DfCursor left key: to beginning of previous word -- */
static void PrevWord(DFWINDOW wnd)
{
int savetop = wnd->wtop;
int saveleft = wnd->wleft;
DfClearVisible(wnd);
Backward(wnd);
while (isWhite(*DfCurrChar)) {
if (wnd->CurrLine == 0 && wnd->CurrCol == 0)
break;
Backward(wnd);
}
while (wnd->CurrCol != 0 && !isWhite(*DfCurrChar))
Backward(wnd);
if (isWhite(*DfCurrChar))
Forward(wnd);
DfSetVisible(wnd);
if (wnd->wleft != saveleft)
if (wnd->CurrCol >= saveleft)
if (wnd->CurrCol - saveleft < DfClientWidth(wnd))
wnd->wleft = saveleft;
DfSendMessage(wnd, DFM_KEYBOARD_CURSOR, DfWndCol, wnd->WndRow);
if (wnd->wtop != savetop || wnd->wleft != saveleft)
DfSendMessage(wnd, DFM_PAINT, 0, 0);
}
/* ----- modify text pointers from a specified position
by a specified plus or minus amount ----- */
static void ModTextPointers(DFWINDOW wnd, int lineno, int var)
{
while (lineno < wnd->wlines)
*((wnd->TextPointers) + lineno++) += var;
}
/* ----- set anchor point for marking text block ----- */
static void SetAnchor(DFWINDOW wnd, int mx, int my)
{
DfClearTextBlock(wnd);
/* ------ set the anchor ------ */
wnd->BlkBegLine = wnd->BlkEndLine = my;
wnd->BlkBegCol = wnd->BlkEndCol = mx;
DfSendMessage(wnd, DFM_PAINT, 0, 0);
}
/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -