📄 edit.c
字号:
// $Id: edit.c,v 1.10 2000/11/28 04:39:24 ymwei Exp $//// edit.c: the Single Line Edit Control module.//// Copyright (C) 1999, 2000, Wei Yongming.// // Current maintainer: Wei Yongming.///*** This library is free software; you can redistribute it and/or** modify it under the terms of the GNU Library General Public** License as published by the Free Software Foundation; either** version 2 of the License, or (at your option) any later version.**** This library is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU** Library General Public License for more details.**** You should have received a copy of the GNU Library General Public** License along with this library; if not, write to the Free** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,** MA 02111-1307, USA*//*** Alternatively, the contents of this file may be used under the terms ** of the Mozilla Public License (the "MPL License") in which case the** provisions of the MPL License are applicable instead of those above.*/// Note:// Although there was a version by Zhao Jianghua, this version of// EDIT control is written by Wei Yongming from scratch.//// Create date: 1999/8/26//// Modify records://// Who When Where For What Status//-----------------------------------------------------------------------------// WEI Yongming 2000/02/24 Tsinghua Add MPL License Finished////// TODO:// * Selection.// * Undo.#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <semaphore.h>#include "common.h"#include "minigui.h"#include "gdi.h"#include "window.h"#include "control.h"#include "cliprect.h"#include "internals.h"#include "ctrlclass.h"#include "ctrlmisc.h"#include "edit.h"#ifndef lintstatic char fileid[] = "$Id: edit.c,v 1.10 2000/11/28 04:39:24 ymwei Exp $";#endifint SLEditCtrlProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam);BOOL RegisterSLEditControl (void){ // This control class has two names: "edit" and "sledit" WNDCLASS WndClass; WndClass.spClassName = "edit"; WndClass.dwStyle = 0; WndClass.hCursor = GetSystemCursor (IDC_IBEAM); WndClass.iBkColor = PIXEL_lightwhite; WndClass.WinProc = SLEditCtrlProc; if (AddNewControlClass (&WndClass) != ERR_OK) return FALSE; WndClass.spClassName = "sledit"; WndClass.dwStyle = 0; WndClass.hCursor = GetSystemCursor (IDC_IBEAM); WndClass.iBkColor = PIXEL_lightwhite; WndClass.WinProc = SLEditCtrlProc; return AddNewControlClass (&WndClass) == ERR_OK;}void SLEditControlCleanup (void){ // do nothing return;}static inline int edtGetOutWidth (const CONTROL* pCtrl){ return pCtrl->cr - pCtrl->cl - ((PSLEDITDATA)(pCtrl->dwAddData2))->leftMargin - ((PSLEDITDATA)(pCtrl->dwAddData2))->rightMargin;}static int edtGetStartDispPosAtEnd (const CONTROL* pCtrl, PSLEDITDATA pSLEditData){ int nOutWidth = edtGetOutWidth (pCtrl); int endPos = pSLEditData->dataEnd; int newStartPos = pSLEditData->startPos; const char* buffer = pSLEditData->buffer; while (TRUE) { if ((endPos - newStartPos) * GetSysCharWidth () < nOutWidth) break; if ((BYTE)buffer [newStartPos] > 0xA0) { newStartPos ++; if (newStartPos < pSLEditData->dataEnd) { if ((BYTE)buffer [newStartPos] > 0xA0) newStartPos ++; } } else newStartPos ++; } return newStartPos;}static int edtGetDispLen (const CONTROL* pCtrl){ int i, n = 0; int nOutWidth = edtGetOutWidth (pCtrl); int nTextWidth = 0; PSLEDITDATA pSLEditData = (PSLEDITDATA)(pCtrl->dwAddData2); const char* buffer = pSLEditData->buffer; for (i = pSLEditData->startPos; i < pSLEditData->dataEnd; i++) { if ((BYTE)buffer [i] > 0xA0) { i++; if (i < pSLEditData->dataEnd) { if ((BYTE)buffer [i] > 0xA0) { nTextWidth += GetSysCCharWidth (); n += 2; } else i--; } else { nTextWidth += GetSysCharWidth (); n++; } } else { nTextWidth += GetSysCharWidth (); n++; } if (nTextWidth > nOutWidth) break; } return n;}static int edtGetOffset (const SLEDITDATA* pSLEditData, int x){ int i; int newOff = 0; int nTextWidth = 0; const char* buffer = pSLEditData->buffer; x -= pSLEditData->leftMargin; for (i = pSLEditData->startPos; i < pSLEditData->dataEnd; i++) { if ((nTextWidth + (GetSysCharWidth() >> 1)) >= x) break; if ((BYTE)buffer [i] > 0xA0) { i++; if (i < pSLEditData->dataEnd) { if ((BYTE)buffer [i] > 0xA0) { nTextWidth += GetSysCCharWidth (); newOff += 2; } else i --; } else { nTextWidth += GetSysCharWidth (); newOff ++; } } else { nTextWidth += GetSysCharWidth (); newOff ++; } } return newOff;}static BOOL edtIsACCharBeforePosition (const char* string, int pos){ if (pos < 2) return FALSE; if ((BYTE)string [pos - 2] > 0xA0 && (BYTE)string [pos - 1] > 0xA0) return TRUE; return FALSE;}static BOOL edtIsACCharAtPosition (const char* string, int len, int pos){ if (pos > (len - 2)) return FALSE; if ((BYTE)string [pos] > 0xA0 && (BYTE)string [pos + 1] > 0xA0) return TRUE; return FALSE;}int SLEditCtrlProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam){ PCONTROL pCtrl; DWORD dwStyle; HDC hdc; PSLEDITDATA pSLEditData; pCtrl = Control(hWnd); dwStyle = pCtrl->dwStyle; switch (message) { case MSG_CREATE: if (!(pSLEditData = malloc (sizeof (SLEDITDATA)))) { fprintf (stderr, "EDIT: malloc error!\n"); return -1; } if (!CreateCaret (hWnd, NULL, 1, GetSysCharHeight ())) { fprintf (stderr, "EDIT: Create Caret error!\n"); free (pSLEditData); return -1; } pSLEditData->status = 0; pSLEditData->bufferLen = LEN_SLEDIT_BUFFER; pSLEditData->editPos = 0; pSLEditData->caretOff = 0; pSLEditData->startPos = 0; pSLEditData->selStart = 0; pSLEditData->selEnd = 0; pSLEditData->passwdChar = '*'; pSLEditData->leftMargin = MARGIN_EDIT_LEFT; pSLEditData->topMargin = MARGIN_EDIT_TOP; pSLEditData->rightMargin = MARGIN_EDIT_RIGHT; pSLEditData->bottomMargin = MARGIN_EDIT_BOTTOM; pSLEditData->hardLimit = -1; // undo information pSLEditData->lastOp = EDIT_OP_NONE; pSLEditData->lastPos = 0; pSLEditData->affectedLen = 0; pSLEditData->undoBufferLen = LEN_SLEDIT_UNDOBUFFER; pSLEditData->undoBuffer [0] = '\0'; pSLEditData->dataEnd = strlen (pCtrl->spCaption); memcpy (pSLEditData->buffer, pCtrl->spCaption, min (LEN_SLEDIT_BUFFER, pSLEditData->dataEnd)); pSLEditData->logfont = NULL; pCtrl->dwAddData2 = (DWORD) pSLEditData; break; case MSG_DESTROY: DestroyCaret (hWnd); free ((void*)pCtrl->dwAddData2); break; case MSG_SIZECHANGED: pCtrl->cl = pCtrl->left + WIDTH_EDIT_BORDER; pCtrl->ct = pCtrl->top + WIDTH_EDIT_BORDER; pCtrl->cr = pCtrl->right - WIDTH_EDIT_BORDER; pCtrl->cb = pCtrl->bottom - WIDTH_EDIT_BORDER; break; case MSG_SETFONT: { PLOGFONT old; pSLEditData = (PSLEDITDATA) (pCtrl->dwAddData2); old = pSLEditData->logfont; pSLEditData->logfont = (PLOGFONT)lParam; InvalidateRect (hWnd, NULL, TRUE); return (int)old; } break; case MSG_GETFONT: pSLEditData = (PSLEDITDATA) (pCtrl->dwAddData2); return (int)pSLEditData->logfont; break; case MSG_SETCURSOR: if (dwStyle & WS_DISABLED) { SetCursor (GetSystemCursor (IDC_ARROW)); return 0; } break; case MSG_KILLFOCUS: pSLEditData = (PSLEDITDATA) (pCtrl->dwAddData2); pSLEditData->status &= ~EST_FOCUSED; HideCaret (hWnd); NotifyParent (hWnd, pCtrl->id, EN_KILLFOCUS); break; case MSG_SETFOCUS: pSLEditData = (PSLEDITDATA) (pCtrl->dwAddData2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -