📄 edit.c
字号:
/*
*********************************************************************************************************
* uC/GUI V3.98
* Universal graphic software for embedded applications
*
* (c) Copyright 2002, Micrium Inc., Weston, FL
* (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
*
* 礐/GUI is protected by international copyright laws. Knowledge of the
* source code may not be used to write a similar product. This file may
* only be used in accordance with a license and should not be redistributed
* in any way. We appreciate your understanding and fairness.
*
----------------------------------------------------------------------
File : EDIT.c
Purpose : Implementation of edit widget
---------------------------END-OF-HEADER------------------------------
*/
#include <stdlib.h>
#include <string.h>
#define EDIT_C /* Required to generate intermodule data */
#include "EDIT.h"
#include "GUIDebug.h"
#include "GUI_Protected.h"
#include "EDIT_Private.h"
#if GUI_WINSUPPORT
/*********************************************************************
*
* Private config defaults
*
**********************************************************************
*/
/* Define default fonts */
#ifndef EDIT_FONT_DEFAULT
#if WIDGET_USE_SCHEME_SMALL
#define EDIT_FONT_DEFAULT &GUI_Font13_1
#elif WIDGET_USE_SCHEME_MEDIUM
#define EDIT_FONT_DEFAULT &GUI_Font16_1
#elif WIDGET_USE_SCHEME_LARGE
#define EDIT_FONT_DEFAULT &GUI_Font24_1
#endif
#endif
#ifndef EDIT_ALIGN_DEFAULT
#define EDIT_ALIGN_DEFAULT GUI_TA_LEFT | GUI_TA_VCENTER
#endif
/* Define colors */
#ifndef EDIT_BKCOLOR0_DEFAULT
#define EDIT_BKCOLOR0_DEFAULT 0xC0C0C0
#endif
#ifndef EDIT_BKCOLOR1_DEFAULT
#define EDIT_BKCOLOR1_DEFAULT GUI_WHITE
#endif
#ifndef EDIT_TEXTCOLOR0_DEFAULT
#define EDIT_TEXTCOLOR0_DEFAULT GUI_BLACK
#endif
#ifndef EDIT_TEXTCOLOR1_DEFAULT
#define EDIT_TEXTCOLOR1_DEFAULT GUI_BLACK
#endif
#ifndef EDIT_BORDER_DEFAULT
#define EDIT_BORDER_DEFAULT 1
#endif
/*********************************************************************
*
* Static data
*
**********************************************************************
*/
EDIT_PROPS EDIT__DefaultProps = {
EDIT_ALIGN_DEFAULT,
EDIT_BORDER_DEFAULT,
EDIT_FONT_DEFAULT,
EDIT_TEXTCOLOR0_DEFAULT,
EDIT_TEXTCOLOR1_DEFAULT,
EDIT_BKCOLOR0_DEFAULT,
EDIT_BKCOLOR1_DEFAULT
};
/*********************************************************************
*
* Static routines
*
**********************************************************************
*/
/*********************************************************************
*
* _Paint
*/
static void _Paint(EDIT_Obj* pObj, EDIT_Handle hObj) {
GUI_RECT rFillRect, rInside, rText, rInvert;
const char GUI_UNI_PTR * pText = NULL;
int IsEnabled, CursorWidth;
IsEnabled = WM__IsEnabled(hObj);
/* Set colors and font */
LCD_SetBkColor(pObj->Props.aBkColor[IsEnabled]);
LCD_SetColor(pObj->Props.aTextColor[0]);
GUI_SetFont(pObj->Props.pFont);
/* Calculate size */
WIDGET__GetInsideRect(&pObj->Widget, &rFillRect);
if (pObj->hpText) {
pText = (const char*) GUI_ALLOC_h2p(pObj->hpText);
}
rInside = rFillRect;
rInside.x0 += pObj->Props.Border + EDIT_XOFF;
rInside.x1 -= pObj->Props.Border + EDIT_XOFF;
GUI__CalcTextRect(pText, &rInside, &rText, pObj->Props.Align);
/* Calculate position and size of cursor */
if (pObj->Widget.State & WIDGET_STATE_FOCUS) {
int NumChars;
CursorWidth = ((pObj->XSizeCursor > 0) ? (pObj->XSizeCursor) : (1));
NumChars = GUI__GetNumChars(pText);
if (pText) {
U16 Char;
int i, IsRTL = 0;
if ((pObj->EditMode != GUI_EDIT_MODE_INSERT) || (pObj->SelSize)) {
if (pObj->CursorPos < NumChars) {
if (pObj->SelSize) {
CursorWidth = 0;
for (i = pObj->CursorPos; i < (int)(pObj->CursorPos + pObj->SelSize); i++) {
Char = GUI__GetCursorCharacter(pText, i, NumChars, 0);
CursorWidth += GUI_GetCharDistX(Char);
}
if (!CursorWidth) {
CursorWidth = 1;
}
} else {
Char = GUI__GetCursorCharacter(pText, pObj->CursorPos, NumChars, &IsRTL);
CursorWidth = GUI_GetCharDistX(Char);
}
}
}
rInvert = rText;
if (IsRTL) {
rInvert.x0 -= CursorWidth;
}
rInvert.x0 += GUI__GetCursorPosX(pText, pObj->CursorPos, NumChars);
}
}
/* WM loop */
WM_ITERATE_START(NULL) {
/* Set clipping rectangle */
WM_SetUserClipRect(&rFillRect);
/* Display text */
WIDGET__FillStringInRect(pText, &rFillRect, &rInside, &rText);
/* Display cursor if needed */
if (pObj->Widget.State & WIDGET_STATE_FOCUS) {
GUI_InvertRect(rInvert.x0, rInvert.y0, rInvert.x0 + CursorWidth - 1, rInvert.y1);
}
WM_SetUserClipRect(NULL);
/* Draw the 3D effect (if configured) */
WIDGET__EFFECT_DrawDown(&pObj->Widget);
} WM_ITERATE_END();
}
/*********************************************************************
*
* _Delete
*/
static void _Delete(EDIT_Obj* pObj) {
GUI_ALLOC_FreePtr(&pObj->hpText);
}
/*********************************************************************
*
* _IncrementBuffer
*
* Increments the buffer size by AddBytes.
*/
static int _IncrementBuffer(EDIT_Obj* pObj, unsigned AddBytes) {
WM_HMEM hNew;
int NewSize;
NewSize = pObj->BufferSize + AddBytes;
hNew = GUI_ALLOC_Realloc(pObj->hpText, NewSize);
if (hNew) {
if (!(pObj->hpText)) {
char* pText;
pText = (char*) GUI_ALLOC_h2p(hNew);
*pText = 0;
}
pObj->BufferSize = NewSize;
pObj->hpText = hNew;
return 1;
}
return 0;
}
/*********************************************************************
*
* _IsSpaceInBuffer
*
* Checks the available space in the buffer. If there is not enough
* space left this function attempts to get more.
*
* Returns:
* 1 = requested space is available
* 0 = failed to get enough space
*/
static int _IsSpaceInBuffer(EDIT_Obj* pObj, int BytesNeeded) {
int NumBytes = 0;
if (pObj->hpText) {
NumBytes = strlen((char*)GUI_ALLOC_h2p(pObj->hpText));
}
BytesNeeded = (BytesNeeded + NumBytes + 1) - pObj->BufferSize;
if (BytesNeeded > 0) {
if (!_IncrementBuffer(pObj, BytesNeeded + EDIT_REALLOC_SIZE)) {
return 0;
}
}
return 1;
}
/*********************************************************************
*
* _IsCharsAvailable
*
* Checks weither the maximum number of characters is reached or not.
*
* Returns:
* 1 = requested number of chars is available
* 0 = maximum number of chars have reached
*/
static int _IsCharsAvailable(EDIT_Obj* pObj, int CharsNeeded) {
if ((CharsNeeded > 0) && (pObj->MaxLen > 0)) {
int NumChars = 0;
if (pObj->hpText) {
NumChars = GUI__GetNumChars((char*)GUI_ALLOC_h2p(pObj->hpText));
}
if ((CharsNeeded + NumChars) > pObj->MaxLen) {
return 0;
}
}
return 1;
}
/*********************************************************************
*
* _DeleteChar
*
* Deletes a character at the current cursor position and moves
* all bytes after the cursor position.
*/
static void _DeleteChar(EDIT_Handle hObj, EDIT_Obj* pObj) {
if (pObj->hpText) {
unsigned CursorOffset;
char* pText;
pText = (char*) GUI_ALLOC_h2p(pObj->hpText);
CursorOffset = GUI_UC__NumChars2NumBytes(pText, pObj->CursorPos);
if (CursorOffset < strlen(pText)) {
int NumBytes;
pText += CursorOffset;
NumBytes = GUI_UC_GetCharSize(pText);
strcpy(pText, pText + NumBytes);
WM_NotifyParent(hObj, WM_NOTIFICATION_VALUE_CHANGED);
}
}
}
/*********************************************************************
*
* _InsertChar
*
* Create space at the current cursor position and inserts a character.
*/
static int _InsertChar(EDIT_Handle hObj, EDIT_Obj* pObj, U16 Char) {
if (_IsCharsAvailable(pObj, 1)) {
int BytesNeeded;
BytesNeeded = GUI_UC__CalcSizeOfChar(Char);
if (_IsSpaceInBuffer(pObj, BytesNeeded)) {
int CursorOffset;
char* pText;
pText = (char*) GUI_ALLOC_h2p(pObj->hpText);
CursorOffset = GUI_UC__NumChars2NumBytes(pText, pObj->CursorPos);
pText += CursorOffset;
memmove(pText + BytesNeeded, pText, strlen(pText) + 1);
GUI_UC_Encode(pText, Char);
WM_NotifyParent(hObj, WM_NOTIFICATION_VALUE_CHANGED);
return 1;
}
}
return 0;
}
/*********************************************************************
*
* _OnTouch
*/
static void _OnTouch(EDIT_Handle hObj, EDIT_Obj* pObj, WM_MESSAGE*pMsg) {
const GUI_PID_STATE* pState = (const GUI_PID_STATE*)pMsg->Data.p;
GUI_USE_PARA(pObj);
if (pMsg->Data.p) { /* Something happened in our area (pressed or released) */
if (pState->Pressed) {
GUI_DEBUG_LOG1("EDIT_Callback(WM_TOUCH, Pressed, Handle %d)\n",1);
EDIT_SetCursorAtPixel(hObj, pState->x);
WM_NotifyParent(hObj, WM_NOTIFICATION_CLICKED);
} else {
GUI_DEBUG_LOG1("EDIT_Callback(WM_TOUCH, Released, Handle %d)\n",1);
WM_NotifyParent(hObj, WM_NOTIFICATION_RELEASED);
}
} else {
GUI_DEBUG_LOG1("EDIT_Callback(WM_TOUCH, Moved out, Handle %d)\n",1);
WM_NotifyParent(hObj, WM_NOTIFICATION_MOVED_OUT);
}
}
/*********************************************************************
*
* _SetValue
*/
static void _SetValue(EDIT_Handle hObj, I32 Value, int Unsigned) {
EDIT_Obj* pObj;
if (hObj) {
WM_LOCK();
pObj = EDIT_H2P(hObj);
/* Put in min/max range */
if (Unsigned) {
if ((unsigned)Value < (unsigned)pObj->Min) {
Value = pObj->Min;
}
if ((unsigned)Value > (unsigned)pObj->Max) {
Value = pObj->Max;
}
} else {
if (Value < pObj->Min) {
Value = pObj->Min;
}
if (Value > pObj->Max) {
Value = pObj->Max;
}
}
if (pObj->CurrentValue != (U32)Value) {
pObj->CurrentValue = Value;
if (pObj->pfUpdateBuffer) {
pObj->pfUpdateBuffer(hObj);
}
WM_InvalidateWindow(hObj);
WM_NotifyParent(hObj, WM_NOTIFICATION_VALUE_CHANGED);
}
WM_UNLOCK();
}
}
/*********************************************************************
*
* Private routines:
*
**********************************************************************
*/
/*********************************************************************
*
* EDIT_h2p
*/
#if GUI_DEBUG_LEVEL >= GUI_DEBUG_LEVEL_CHECK_ALL
EDIT_Obj * EDIT_h2p(EDIT_Handle h) {
EDIT_Obj * p = (EDIT_Obj *)GUI_ALLOC_h2p(h);
if (p) {
if (p->DebugId != EDIT_ID) {
GUI_DEBUG_ERROROUT("EDIT.c: Wrong handle type or Object not init'ed");
return 0;
}
}
return p;
}
#endif
/*********************************************************************
*
* EDIT__GetCurrentChar
*/
U16 EDIT__GetCurrentChar(EDIT_Obj* pObj) {
U16 Char = 0;
if (pObj->hpText) {
const char* pText;
pText = (const char*) GUI_ALLOC_h2p(pObj->hpText);
pText += GUI_UC__NumChars2NumBytes(pText, pObj->CursorPos);
Char = GUI_UC_GetCharCode(pText);
}
return Char;
}
/*********************************************************************
*
* EDIT__SetCursorPos
*
* Sets a new cursor position.
*/
void EDIT__SetCursorPos(EDIT_Obj* pObj, int CursorPos) {
if (pObj->hpText) {
char* pText;
int NumChars, Offset;
pText = (char*) GUI_ALLOC_h2p(pObj->hpText);
NumChars = GUI__GetNumChars(pText);
Offset = (pObj->EditMode == GUI_EDIT_MODE_INSERT) ? 0 : 1;
if (CursorPos < 0) {
CursorPos = 0;
}
if (CursorPos > NumChars) {
CursorPos = NumChars;
}
if (CursorPos > (pObj->MaxLen - Offset)) {
CursorPos = pObj->MaxLen - Offset;
}
if (pObj->CursorPos != CursorPos) {
pObj->CursorPos = CursorPos;
}
pObj->SelSize = 0;
}
}
/*********************************************************************
*
* EDIT__SetValueUnsigned
*/
void EDIT__SetValueUnsigned(EDIT_Handle hObj, I32 Value) {
_SetValue(hObj, Value, 1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -