asix_ed.c

来自「一个操作系统源代码 用于嵌入式设备 在Vc++环境下仿真 成功移植到多款处理器上」· C语言 代码 · 共 1,876 行 · 第 1/4 页

C
1,876
字号
/**********************************************************
*
*Copyright ? 2001-2003 National ASIC Center,	All right Reserved
*
* FILE NAME:			asix_ed.c
* PROGRAMMER:			longn_qi
* Date of Creation:		2001/12/20
* 
* DESCRIPTION:
*		
* NOTE:
*
* FUNCTIONS LIST:
* -- PUBLIC FUNCTIONS --
* STATUS ed_create( char *caption, U32 style, U16 x, U16 y, U16 width, U16 height, U32 wndid, U32 menu, void **ctrl_str, void *exdata )
*
* -- LOCAL FUNCTIONS --
* STATUS OrgnizeMatrix( struct ed_ctrl *edctrl, U16 startLine, U16 startColumn )
* void UpdateInputArea( struct ed_ctrl *edctrl, U16 startLine, U16 startColumn )
* STATUS FillVector( struct ed_ctrl *edctrl, S8 *str, U16 line, U16 *strLen )
* S8 *InsertChar( S8 *str, S8 *pos, U16 v, U32 *strLen, U32 bufSize )
* void LocateCursorPos( struct ed_ctrl *edctrl )
* STATUS SingleEdMsgProc( U16 asix_msg, struct ed_ctrl *edctrl, void *data, U16 wparam, void *reserved )
* STATUS MultiEdMsgProc( U16 asix_msg, struct ed_ctrl *edctrl, void *data, U16 wparam, void *reserved )
* STATUS MoveLeft( struct ed_ctrl *edctrl, U16 num )
* STATUS MoveLeft( struct ed_ctrl *edctrl, U16 num )
* STATUS JumpDown( struct ed_ctrl *edctrl, U16 num )
* STATUS JumpUp( struct ed_ctrl *edctrl, U16 num )
*
***********************************************************/

#include <string.h>
#include <ctype.h>
#include <asixwin.h>

#include <asixwin\asix_ed.h>

extern Word2Bytes( unsigned char *s, unsigned short v );

STATIC STATUS InitialMatrix( struct ed_ctrl *edctrl );
STATIC STATUS OrgnizeMatrix( struct ed_ctrl *edctrl, U16 startLine, U16 startColumn );
STATIC void UpdateInputArea( struct ed_ctrl *edctrl, U16 startLine, U16 startColumn );
STATIC STATUS FillVector( struct ed_ctrl *edctrl, S8 *str, U16 line, U16 *strLen );
STATIC S8 *InsertChar( S8 *str, S8 *pos, U16 v, U32 *strLen, U32 bufSize );
STATIC void LocateCursorPos( struct ed_ctrl *edctrl );
STATIC STATUS SingleEdMsgProc( U16 asix_msg, struct ed_ctrl *edctrl, void *data, U16 wparam, void *reserved );
STATIC STATUS MultiEdMsgProc( U16 asix_msg, struct ed_ctrl *edctrl, void *data, U16 wparam, void *reserved );
STATIC STATUS MoveLeft( struct ed_ctrl *edctrl, U16 num );
STATIC STATUS MoveRight( struct ed_ctrl *edctrl, U16 num );
STATIC STATUS JumpDown( struct ed_ctrl *edctrl, U16 num );
STATIC STATUS JumpUp( struct ed_ctrl *edctrl, U16 num );

STATUS ed_create(	char *caption, 
					U32 style, 
					U16 x, U16 y, 
					U16 width, U16 height,
					U32 wndid, 
					U32 menu, 
					void **ctrl_str, 
					void *exdata )
{
	struct ed_ctrl *edctrl;
	U16		lcdh,lcdw;			//logical screen size
	U32		hGC;
//	U32		borderColor;
//	U16		i;

	hGC = GetGC();

	lcdh = ASIX_LCD_H;
	lcdw = ASIX_LCD_W;

	// 参数检查
	// check control window size
	if( (x+width) > lcdw || (y+height) > lcdh ) 
		return ASIX_ERROR;
	if( width < EDITOR_MIN_WIDTH || height < EDITOR_MIN_HEIGHT )
		return ASIX_ERROR;
	// check style
	if( (style & ES_GROUP1) != ES_SINGLELINE && (style & ES_GROUP1) != ES_MULTILINE )
		return ASIX_ERROR;
	if( (style & ES_GROUP2) != ES_LEFT && (style & ES_GROUP2) != ES_CENTER && (style & ES_GROUP2) != ES_RIGHT )
		return ASIX_ERROR;

	asix_ed_memdbgprintf( "### Crearte Editor ###" );
	edctrl = (struct ed_ctrl *)Lcalloc( sizeof(struct ed_ctrl) );
	if (edctrl == NULL )
	{
		asix_ed_memdbgprintf( "### Crearte Editor Error ###" );
		return ASIX_NO_MEM;
	}
	
	// longn_qi 2001/12/25 deleted (1)
	// because "Lcalloc" has offered clearing, following "memset" call is redundant.
	//memset( edctrl, 0, sizeof(struct ed_ctrl) );

	asix_ed_memdbgprintf( "asix_ed appeal mem for itself" );

	edctrl->classid = (U32) WNDCLASS_EDITOR;
	edctrl->windowid = wndid;
	edctrl->style = style;
	
	edctrl->x = x;
	edctrl->y = y;
	if( edctrl->style & ES_MULTILINE )
		width -= EDITOR_SCROLLBAR_WIDTH;
	edctrl->width = width;
	edctrl->height = height;

	// 设置行高、页列数、页行数、字符缓冲区大小、最大页号和最大行数
	edctrl->pageColumn = ( width - 2 ) / ENGLISH_CHAR_WIDTH;
	edctrl->maxPage = 1;
	if( style & ES_MULTILINE )
	{
		edctrl->lineHeight = ENGLISH_CHAR_HEIGHT + 2;
		edctrl->pageLine = height / ( ENGLISH_CHAR_HEIGHT + 2 );	// 页行数必须不小于1
		
		// 确定内部字符缓冲区
		if( menu != 0 )
			edctrl->bufSize = menu;
		else
		{
			if( caption == NULL || *caption == '\0' )
				edctrl->bufSize = edctrl->pageLine * edctrl->pageColumn +1;
			else
				edctrl->bufSize = strlen( caption ) +1;
		}
		edctrl->maxLine = edctrl->bufSize -1;	// 都是换行符时,行数达到最多
	}
	else
	{
		edctrl->lineHeight = height - 2;
		edctrl->pageLine = 1;

		// 确定内部字符缓冲区
		if( menu != 0 )
			edctrl->bufSize = menu;
		else
		{
			if( caption == NULL || *caption == '\0' )
				edctrl->bufSize = edctrl->pageColumn +1;
			else
				edctrl->bufSize = strlen( caption ) +1;
		}
		edctrl->maxLine = 1;
	}
	
	edctrl->pbuf = (S8 *)Lcalloc( edctrl->bufSize );
	if (edctrl == NULL )
		goto ErrHandle;

	asix_ed_memdbgprintf( "asix_ed appeal mem for its buffer" );

	// 字符缓冲区初始化,若初始字符串长度超过缓冲区大小,截断
	if( caption != NULL )
	{
		edctrl->strLen = strlen( caption );
		if( edctrl->strLen >= edctrl->bufSize )
		{
			if( caption[ edctrl->bufSize -1 ] & 0x80 )
				edctrl->strLen = edctrl->bufSize - 2;
			else
				edctrl->strLen = edctrl->bufSize - 1;
		}
		strncpy( edctrl->pbuf, caption, edctrl->strLen );
		edctrl->pbuf[ edctrl->strLen ] = '\0';
	}
	else
	{
		edctrl->strLen = 0;
		edctrl->pbuf[0] = '\0';
	}

	//edctrl->curLineHead = edctrl->pbuf;
	// 由于edctrl申请内存时已初始化为0,所以下面三个设置冗余
	//edctrl->curLine = 0;
	//edctrl->curColumn = 0;	
	//edctrl->pmatrix = NULL;

	// 组织文本
	if( InitialMatrix( edctrl ) == ASIX_ERROR )
		goto ErrHandle;

	// 绘制显示区,若有边框,边框宽度为1
	if( style & ES_NOBORDER )
	{
		ClearRec( hGC, EDITOR_BACKGROUND_COLOR, x, y, width, height, GPC_REPLACE_STYLE );
	}
	else
	{
		ClearRec( hGC, EDITOR_BACKGROUND_COLOR, x+1, y+1, width-2, height-2, GPC_REPLACE_STYLE );
		DrawRec( hGC, EDITOR_FRAME_COLOR, x, y, x+width-1, y+height-1, GPC_SOLID_LINE, GPC_REPLACE_STYLE );
	}

	// 刷新显示初始字符串
	if( caption != NULL && caption[0] != '\0' )
		UpdateInputArea( edctrl, 0, 0 );

	// 激活输入区
	if( ActiveAreaEnable(	(P_U32) &(edctrl->editorid), 
							INPUT_AREA, 
							CONTINUOUS_MODE,
							edctrl->x,
							edctrl->y,
							edctrl->x + edctrl->width -1,
							edctrl->y + edctrl->height -1,
							edctrl->windowid )
							!= PPSM_OK )
		goto ErrHandle;
	else
	{
		*ctrl_str = edctrl;
	}

	if( style & ES_MULTILINE )
	{
		edctrl->sbid = CreateWindow(	WNDCLASS_SCROLL, 
										"EditorScroll", 
										WS_CHILD | SBS_VERT, 
										(U16)(x + edctrl->width), y,
										(U16)(EDITOR_SCROLLBAR_WIDTH), height,
										edctrl->windowid,
										MAKELONG(0,edctrl->maxPage), 
										NULL);
		if( edctrl->sbid == 0 )
			goto ErrHandle;
	}

	asix_ed_memdbgprintf( "### Crearte Editor Successfully ###" );
	return ASIX_OK;


ErrHandle:	
	if( edctrl->sbid != 0 )
		DestroyWindow( edctrl->sbid );

	if( edctrl->editorid != 0 )
		ActiveAreaDisable( edctrl->editorid );

	if( edctrl->pmatrix != NULL )
	{
		Lfree( edctrl->vector );
		asix_ed_memdbgprintf( "asix_ed free mem of its vector" );
		
		Lfree( edctrl->pmatrix );		
		asix_ed_memdbgprintf( "asix_ed free mem of its matrix" );
	}

	if( edctrl->pbuf != NULL )
	{
		Lfree( edctrl->pbuf );		
		asix_ed_memdbgprintf( "asix_ed free mem of its buffer" );
	}

	Lfree( edctrl );
	asix_ed_memdbgprintf( "asix_ed free mem of itself" );

	asix_ed_memdbgprintf( "### Crearte Editor Error ###" );
	return ASIX_ERROR;
}

STATUS ed_destroy(void *ctrl_str)
{
	struct ed_ctrl *edctrl;
	U32		hGC;

	edctrl = (struct ed_ctrl*) ctrl_str;

	if( edctrl == NULL )
		return ASIX_ERROR;

	asix_ed_memdbgprintf( "### Destroy Editor ###" );
	if( GetFocus() == edctrl->windowid )
	{
		hGC = GetGC();
		FreeCursor( hGC );
	}

	ActiveAreaDisable( edctrl->editorid );
	
	Lfree( edctrl->pbuf );	
	asix_ed_memdbgprintf( "asix_ed free mem of its buffer" );
	
	if( edctrl->sbid != 0 )
		DestroyWindow( edctrl->sbid );
	
	Lfree( edctrl->vector );	
	asix_ed_memdbgprintf( "asix_ed free mem of its vector" );

	Lfree( edctrl->pmatrix );
	asix_ed_memdbgprintf( "asix_ed free mem of its matrix" );
	
	Lfree( edctrl );
	asix_ed_memdbgprintf( "asix_ed free mem of itself" );

	asix_ed_memdbgprintf( "### Destroy Editor Successfully ###" );
	return ASIX_OK;
}

STATUS ed_msgproc( U32 win_id, U16 asix_msg, U32 lparam, void *data, U16 wparam, void *reserved)
{
	ASIX_WINDOW		*pwnd = (ASIX_WINDOW *)win_id;
	struct ed_ctrl	*edctrl;

	if( pwnd == NULL )	// not a window
		return ASIX_ERROR;
	
	if( pwnd->wndclass == NULL || pwnd->wndclass->wndclass_id != WNDCLASS_EDITOR )	// is a editor
			return ASIX_ERROR;
	
	edctrl = (struct ed_ctrl *)pwnd->ctrl_str;

	if( edctrl == NULL ) // no description for editor
		return ASIX_ERROR;
	//if( (U32)pwnd != edctrl->windowid ) // not for me
	//	return ASIX_ERROR;
	
	if( edctrl->style & ES_MULTILINE )
		return MultiEdMsgProc( asix_msg, edctrl, data, wparam, reserved );
	else
		return SingleEdMsgProc( asix_msg, edctrl, data, wparam, reserved );
}

STATUS ed_msgtrans(void *ctrl_str, U16 msg_type, U32 areaId, P_U16 data, U32 size, PMSG trans_msg)
{
	struct ed_ctrl *edctrl;

	edctrl = (struct ed_ctrl *)ctrl_str;

	if( edctrl == NULL || trans_msg==NULL ) // no description for editor
		return ASIX_NO_MSG;
	if( msg_type < WM_BEGINMSG && areaId != edctrl->editorid )	// not for me
		return ASIX_NO_MSG;
	if( msg_type > WM_BEGINMSG && areaId != edctrl->windowid )	// not for me
		return ASIX_NO_MSG;

	memset( (void *)trans_msg, 0x0, sizeof (MSG) );
	trans_msg->messageType = ASIX_MESSAGE;
	
	switch( msg_type )
	{
		case ASIX_INPUT_STATUS:

			trans_msg->lparam = edctrl->windowid;
			trans_msg->data = (void *)edctrl;
			
			switch( size )
			{
				case PPSM_INPUT_TOUCH:
					trans_msg->message = WM_PENDOWN;
					GetPosData(data, &edctrl->penx, &edctrl->peny);
					break;
				case PPSM_INPUT_PEN_UP:
					trans_msg->message = WM_PENUP;
					break;
				default:
					break;
			}

			break;
		case ASIX_PEN:
			
			GetPosData(data, &edctrl->penx, &edctrl->peny);
			
			trans_msg->lparam = edctrl->windowid;
			trans_msg->data = (void *)edctrl;
			trans_msg->message = WM_PENXY;
			
			break;
		default:
			break;
	}
	return ASIX_OK;
}

STATUS ed_repaint(void *ctrl_str, U32 lparam)
{
	struct ed_ctrl	*edctrl;
	ASIX_WINDOW		*wndptr;
	U32		hGC;
	U16		x, y, width, height;

	edctrl = (struct ed_ctrl*) ctrl_str;

	if( edctrl == NULL )
		return ASIX_ERROR;

	wndptr = (ASIX_WINDOW *)edctrl->windowid;

	x = edctrl->x;
	y = edctrl->y;
	width = edctrl->width;
	height = edctrl->height;

	hGC = GetGC();

	if( wndptr->status & WST_ENABLE )
	{
		ClearRec( hGC, EDITOR_BACKGROUND_COLOR, x, y, width, height, GPC_REPLACE_STYLE );
		if( !( edctrl->style & ES_NOBORDER ) )
			DrawRec( hGC, EDITOR_FRAME_COLOR, x, y, x+width-1, y+height-1, GPC_SOLID_LINE, GPC_REPLACE_STYLE );
		
		UpdateInputArea( edctrl, edctrl->curPage, 0 );

	}
	else
	{
		ClearRec( hGC, EDITOR_DISABLE_COLOR, x, y, width, height, GPC_XOR_STYLE );
		if( ! ( edctrl->style & ES_NOBORDER ) )
			DrawRec( hGC, EDITOR_FRAME_COLOR, x, y, x+width-1, y+height-1, GPC_SOLID_LINE, GPC_REPLACE_STYLE );
	}

	if( GetFocus() == edctrl->windowid )
	{
	}

	return ASIX_OK;
}

STATUS ed_caption(void *ctrl_str, char *caption, void *exdata)
{
	struct ed_ctrl *edctrl;
	ASIX_WINDOW		*wndptr;
	U32		hGC;
	
	edctrl = (struct ed_ctrl*) ctrl_str;

	if( edctrl == NULL )
		return ASIX_ERROR;

	wndptr = (ASIX_WINDOW *)edctrl->windowid;

	if( caption != NULL )
	{
		edctrl->strLen = strlen( caption );
		if( edctrl->strLen >= edctrl->bufSize )
		{
			if( caption[ edctrl->bufSize -1 ] & 0x80 )
				edctrl->strLen = edctrl->bufSize - 2;
			else
				edctrl->strLen = edctrl->bufSize - 1;
		}
		strncpy( edctrl->pbuf, caption, edctrl->strLen );
		edctrl->pbuf[ edctrl->strLen ] = '\0';
	}
	else
	{
		edctrl->strLen = 0;
		edctrl->pbuf[0] = '\0';
	}

	InitialMatrix( edctrl );

	if( edctrl->sbid != 0 )
		SetScrollRange( edctrl->sbid, 0, edctrl->maxPage );

//	if( caption != NULL && caption[0] != '\0' )
	UpdateInputArea( edctrl, 0, 0 );

	hGC = GetGC();
	if( !( wndptr->status & WST_ENABLE ) )
	{
		ClearRec( hGC, EDITOR_DISABLE_COLOR, edctrl->x, edctrl->y, edctrl->width, edctrl->height, GPC_XOR_STYLE );
		if( ! ( edctrl->style & ES_NOBORDER ) )
			DrawRec( hGC, EDITOR_FRAME_COLOR, edctrl->x, edctrl->y, edctrl->x+edctrl->width-1, edctrl->y+edctrl->height-1, GPC_SOLID_LINE, GPC_REPLACE_STYLE );
	}

	return ASIX_OK;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?