📄 medit.c
字号:
}
}
#endif
return FALSE;
}
static void str2attr(const char* str,int len)
{
int i=0;
do
{
if (edtIsACCharAtPosition(str,len,i))
{
attr[i]=ATTCHL;
attr[i+1]=ATTCHR;
i+=2;
}
else
{
attr[i]=ATTENG;
i++;
}
}while(i<len);
}
static BOOL edtIsACCharBeforePosition (const char* string,int len, int pos)
{
if (pos < 2)
return FALSE;
/* 1st:gb:a1-f7,big5:a1-f9 2nd:gb:a1-fe,big5:40-7e,a1-fe */
#ifndef USE_BIG5
/* FIXME #ifdef GB2312?*/
if ((BYTE)string [pos - 2] > 0xA0 && (BYTE)string [pos - 1] > 0xA0)
return TRUE;
#else
#if 0
if ((BYTE)string [pos - 2] > 0xA0)
{
if ( ((BYTE)string [pos - 1] >= 0x40 && (BYTE)string[pos - 1] <= 0x7e) ||
((BYTE)string [pos - 1] >= 0xa1 && (BYTE)string[pos - 1] <= 0xfe))
return TRUE;
}
#else
str2attr(string,len);
if (attr[pos-1]==ATTENG) return FALSE;
else return TRUE;
#endif
#endif
return FALSE;
}
static BOOL edtIsACCharFromBegin(const char* string,int len,int pos)
{
int i;
if(pos == 0)
return TRUE;
if(len == 0)
return FALSE;
for(i=0;i<len;)
{
if( edtIsACCharAtPosition(string,len,i) )
i += 2;
else
i++;
if(i==pos)
return TRUE;
}
return FALSE;
}
int GetRETURNPos(char *str)
{
int i;
for(i=0;i<strlen(str);i++)
{
if(str[i]==10)
return i;
}
return -1;
}
void MLEditInitBuffer (PMLEDITDATA pMLEditData,char *spcaption)
{
char *caption=spcaption;
int off1;
int lineNO=0;
PLINEDATA pLineData;
if (!(pMLEditData->head = malloc (sizeof (LINEDATA)))) {
fprintf (stderr, "EDITLINE: malloc error!\n");
return ;
}
pMLEditData->head->previous = NULL;
pLineData=pMLEditData->head;
while ( (off1 = GetRETURNPos(caption)) != -1)
{
off1 = min(off1, LEN_MLEDIT_BUFFER);
memcpy(pLineData->buffer,caption,off1);
pLineData->buffer[off1] = '\0';
caption+=min(off1,LEN_MLEDIT_BUFFER)+1;
pLineData->lineNO = lineNO;
pMLEditData->dispPos = 0;
pLineData->dataEnd = strlen(pLineData->buffer);
pLineData->next = malloc (sizeof (LINEDATA));
pLineData->next->previous = pLineData;
pLineData = pLineData->next;
lineNO++;
}
off1 = min(strlen(caption),LEN_MLEDIT_BUFFER);
memcpy(pLineData->buffer,caption,off1);
pLineData->buffer[off1] = '\0';
pLineData->lineNO = lineNO++;
pMLEditData->dispPos = 0;
pLineData->dataEnd = strlen(pLineData->buffer);
pLineData->next = NULL;
pMLEditData->lines = lineNO ;
}
PLINEDATA GetLineData(PMLEDITDATA pMLEditData,int lineNO)
{
PLINEDATA pLineData=pMLEditData->head;
while(pLineData)
{
if(pLineData->lineNO==lineNO)
return pLineData;
pLineData = pLineData->next;
}
return NULL;
}
int MLEditCtrlProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
DWORD dwStyle;
DWORD dw;
HDC hdc;
PLINEDATA pLineData;
RECT clientRect;
PMLEDITDATA pMLEditData;
dwStyle = GetWindowStyle(hWnd);
switch (message)
{
case WM_CREATE:
{
if (!(pMLEditData = malloc (sizeof (MLEDITDATA)))) {
fprintf (stderr, "EDIT: malloc error!\n");
return -1;
}
pMLEditData->totalLen = LEN_MLEDIT_BUFFER;
pMLEditData->editPos = 0;
pMLEditData->editLine = 0;
pMLEditData->caretPos = 0;
MLEditInitBuffer(pMLEditData,GetWindowCaption(hWnd));
GetClientRect(hWnd,&clientRect);
pMLEditData->MaxlinesDisp = (clientRect.bottom-clientRect.top)/GetSysCharHeight(hWnd);
pMLEditData->linesDisp = min(pMLEditData->MaxlinesDisp,pMLEditData->lines);
pMLEditData->StartlineDisp = 0;
pMLEditData->EndlineDisp = pMLEditData->StartlineDisp + pMLEditData->linesDisp - 1;
pMLEditData->selStartPos = 0;
pMLEditData->selEndPos = 0;
pMLEditData->passwdChar = '*';
pMLEditData->leftMargin = MARGIN_MEDIT_LEFT;
pMLEditData->topMargin = MARGIN_MEDIT_TOP;
pMLEditData->rightMargin = MARGIN_MEDIT_RIGHT;
pMLEditData->bottomMargin = MARGIN_MEDIT_BOTTOM;
pMLEditData->hardLimit = -1;
/* undo information */
pMLEditData->lastOp = MEDIT_OP_NONE;
pMLEditData->lastPos = 0;
pMLEditData->affectedLen = 0;
pMLEditData->undoBufferLen = LEN_MLEDIT_UNDOBUFFER;
pMLEditData->undoBuffer [0] = '\0';
SetWindowAdditionalData2(hWnd,(DWORD)pMLEditData);
SetWindowAdditionalData(hWnd,(DWORD)0);
break;
}
case WM_DESTROY:
{
PLINEDATA temp;
pMLEditData =(PMLEDITDATA) GetWindowAdditionalData2(hWnd);
DestroyCaret ();
pLineData = pMLEditData->head;
while(pLineData)
{
/*printf("delete lineNO = %d,buffer=%s\n",pLineData->lineNO,pLineData->buffer);*/
temp = pLineData->next;
free(pLineData);
pLineData = temp;
}
free(pMLEditData);
}
break;
case WM_SETFONT:
break;
case WM_GETFONT:
break;
#if 0 /* fix: no WM_SETCURSOR */
case WM_SETCURSOR:
if (dwStyle & WS_DISABLED) {
SetCursor (GetSystemCursor (IDC_ARROW));
return 0;
}
break;
case WM_SIZECHANGED:
{
}
return 0;
#endif
case WM_KILLFOCUS:
{
dw= GetWindowAdditionalData(hWnd);
dw&= ~EST_FOCUSED;
SetWindowAdditionalData(hWnd,dw);
HideCaret (hWnd);
DestroyCaret ();
SendMessage (GetParent (hWnd),
WM_COMMAND,
(WPARAM) MAKELONG (GetDlgCtrlID(hWnd), EN_KILLFOCUS),
(LPARAM)hWnd);
}
break;
case WM_SETFOCUS:
{
dw= GetWindowAdditionalData(hWnd);
if (dw & EST_FOCUSED)
return 0;
dw |= EST_FOCUSED;
SetWindowAdditionalData(hWnd,dw);
pMLEditData =(PMLEDITDATA) GetWindowAdditionalData2(hWnd);
/* only implemented for ES_LEFT align format. */
CreateCaret (hWnd, NULL, 1, /*GetSysCharWidth(hWnd)+1,*/
hWnd->clirect.bottom-hWnd->clirect.top-2);
SetCaretPos (pMLEditData->caretPos * GetSysCharWidth (hWnd)
+ pMLEditData->leftMargin, pMLEditData->topMargin);
ShowCaret(hWnd);
SendMessage (GetParent (hWnd),
WM_COMMAND,
(WPARAM) MAKELONG (GetDlgCtrlID(hWnd), EN_SETFOCUS),
(LPARAM) hWnd);
}
break;
case WM_ENABLE:
if ( (!(dwStyle & WS_DISABLED) && !wParam)
|| ((dwStyle & WS_DISABLED) && wParam) ) {
if (wParam)
ExcludeWindowStyle(hWnd,WS_DISABLED);
else
IncludeWindowStyle(hWnd,WS_DISABLED);
InvalidateRect (hWnd, NULL, FALSE);
}
return 0;
case WM_NCPAINT:
{
RECT rc;
#if 0
if (wParam)
hdc = (HDC)wParam;
else
hdc = GetDC (hWnd);
#if 0 /* fix: no ClipRectIntersect() */
if (lParam)
ClipRectIntersect (hdc, (RECT*)lParam);
#endif
#else
hdc = wParam? (HDC)wParam: GetWindowDC (hWnd);
GetWindowRect(hWnd, &rc);
#endif
if (dwStyle & WS_BORDER)
{
#if 0
RECT rc;
GetWindowRect(hWnd,&rc);
Draw3DDownFrame (hdc, 0, 0,
rc.right - rc.left - 1,
rc.bottom - rc.top - 1,
PIXEL_invalid);
#else
Draw3dInset(hdc, rc.left, rc.top,
rc.right-rc.left, rc.bottom-rc.top);
#endif
}
if (!wParam) {
ReleaseDC(hWnd,hdc);
}
}
return 0;
case WM_PAINT:
{
int dispLen,i;
char* dispBuffer;
RECT rect,rc;
PAINTSTRUCT ps;
HGDIOBJ oldfont=NULL;
hdc = BeginPaint (hWnd,&ps);
GetClientRect (hWnd, &rect);
if (dwStyle & WS_DISABLED)
{
rc.left=0; rc.top=0; rc.bottom=rect.bottom; rc.right=rect.right;
FillRect(hdc,&rc,GetStockObject(LTGRAY_BRUSH));
SetBkColor (hdc, LTGRAY/*PIXEL_lightgray*/);
}
else {
rc.left=0; rc.top=0; rc.bottom=rect.bottom; rc.right=rect.right;
FillRect(hdc,&rc,GetStockObject(WHITE_BRUSH));
SetBkColor (hdc, WHITE/*PIXEL_lightwhite*/);
}
SetTextColor (hdc, BLACK/*PIXEL_black*/);
pMLEditData =(PMLEDITDATA) GetWindowAdditionalData2(hWnd);
for(i = pMLEditData->StartlineDisp; i <= pMLEditData->EndlineDisp; i++)
{
pLineData= GetLineData(pMLEditData,i);
dispLen = edtGetDispLen (hWnd,pLineData);
if (dispLen == 0 && pMLEditData->EndlineDisp >= pMLEditData->lines) {
continue;
}
#ifdef _DEBUG
if (pMLEditData->dispPos > pLineData->dataEnd)
fprintf (stderr, "ASSERT failure: %s.\n", "Edit Paint");
#endif
dispBuffer = alloca (LEN_MLEDIT_BUFFER+1);
if (dwStyle & ES_PASSWORD)
memset (dispBuffer, '*', pLineData->dataEnd);
memcpy (dispBuffer,
pLineData->buffer, /* +pMLEditData->dispPos, */
pLineData->dataEnd); /* - pMLEditData->dispPos); */
dispBuffer[pLineData->dataEnd] = '\0';
/* only implemented ES_LEFT align format for single line edit. */
rect.left = pMLEditData->leftMargin;
rect.top = pMLEditData->topMargin ;
rect.right = pMLEditData->rightMargin;
rect.bottom = pMLEditData->bottomMargin;
#if 0
printf("lineNO=%d,lines=%d,editLine=%d\n",pLineData->lineNO,pMLEditData->lines,
pMLEditData->editLine);
printf("--dispBuffer=%s--\n",dispBuffer);
ClipRectIntersect (hdc, &rect); /* fix: no ClipRectIntersect() */
#endif
#ifdef USE_BIG5
oldfont=SelectObject(hdc,CreateFont(12,
0,0,0,0,0,0,0,0,0,0,0,
FF_DONTCARE|DEFAULT_PITCH,
"HZXFONT"));
#endif
TextOut (hdc,
pMLEditData->leftMargin - pMLEditData->dispPos * GetSysCharWidth(hWnd) ,
GetSysCharHeight(hWnd)*(pLineData->lineNO - pMLEditData->StartlineDisp)
+ pMLEditData->topMargin,
dispBuffer,-1);
}
#ifdef USE_BIG5
DeleteObject(SelectObject(hdc,oldfont));
#endif
EndPaint (hWnd, &ps);
}
break;
case WM_KEYDOWN:
{
BOOL bChange = FALSE;
int i;
int deleted;
PLINEDATA temp = NULL;
char * tempP = NULL;
pMLEditData =(PMLEDITDATA) GetWindowAdditionalData2(hWnd);
switch (LOWORD (wParam))
{
case VK_RETURN: /* SCANCODE_ENTER: */
{
pLineData = GetLineData(pMLEditData,pMLEditData->editLine);
if (pMLEditData->editPos < pLineData->dataEnd)
tempP = pLineData->buffer + pMLEditData->editPos;
temp = pLineData->next;
pLineData->next = malloc( sizeof(LINEDATA) );
pLineData->next->previous = pLineData;
pLineData->next->next = temp;
if(temp)
{
temp->previous = pLineData->next;
}
temp = pLineData->next;
temp->lineNO = pMLEditData->editLine + 1;
if(tempP)
{
memcpy(temp->buffer,tempP,strlen(tempP));
temp->dataEnd = strlen(tempP);
}
else
temp->dataEnd = 0;
temp->buffer[temp->dataEnd] = '\0';
pLineData->dataEnd = pMLEditData->editPos;
pLineData->buffer[pLineData->dataEnd]='\0';
temp = temp->next;
while (temp)
{
temp->lineNO++;
temp = temp->next;
}
pMLEditData->editPos = 0;
pMLEditData->caretPos= 0;
pMLEditData->dispPos = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -