smplebrowser.c
来自「好记星的控件,包括button,list,对文件操作」· C语言 代码 · 共 371 行
C
371 行
/**************************************************************************
Copyright (C) 2005 SHENZHEN MEIJIN CO.LTD
FILE NAME: ApSmpBrowser.C
MODULE NAME: ApSmpBrowser
DESCRIPTION: 简单浏览器(只支持纯文本方式)
对外函数声明:
SmpBrowserHandleEvent 简单浏览器消息处理函数
SmpBrwGetLine 获取一行数据
**************************************************************************
DTAE AUTHOR VERSION REMARKS
=========== ========== ========= ======================================
2006-4-14 gaolinhui V1.0 create
***************************************************************************/
#include "kernel.h"
/****************************************************************************
Function:
SmpBrowserHandleEvent
Description:
简单浏览器消息处理函数
input:
output:
0 - 消息未处理
!0 - 消息已经处理
note:
****************************************************************************
高林辉 2006-4-14 创建
****************************************************************************/
INT SmpBrowserHandleEvent(APGUI_STRUCT_MODEL *pGui, UINT uEvent, UINT uParam)
{
PMSmpBrowser handle;
INT nRet;
UINT32 dwTopLine;
UINT32 dwTotal;
UINT16 wFontLib;
UINT16 wLineSpace;
UINT16 wPageLine;
BOOL bInit;
handle = (PMSmpBrowser)pGui;
dwTopLine = SMPBRW_TOPLINE;
dwTotal = SMPBRW_TOTAL;
wFontLib = SYSTLIB;
wPageLine = SMPBRW_PAGELINE;
wLineSpace = SMPBRW_LINEHI;
bInit = SMPBRW_INIT;
nRet = 0;
switch(uEvent)
{
case EVENT_COMMAND:
{
nRet = 1;
switch(uParam)
{
case CM_SMPBRW_UP:
if(dwTopLine)
{
SMPBRW_TOPLINE --;
MsgPost(NULL, EVENT_DRAW, 0);
}
break;
case CM_SMPBRW_DWN:
if(dwTopLine+wPageLine < dwTotal)
{
SMPBRW_TOPLINE ++;
MsgPost(NULL, EVENT_DRAW, 0);
}
break;
case CM_SMPBRW_PGUP:
if(dwTopLine >= wPageLine)
{
SMPBRW_TOPLINE -= wPageLine;
MsgPost(NULL, EVENT_DRAW, 0);
}
else if(dwTopLine > 0)
{
SMPBRW_TOPLINE = 0;
MsgPost(NULL, EVENT_DRAW, 0);
}
break;
case CM_SMPBRW_PGDWN:
if(dwTopLine+wPageLine < dwTotal)
{
SMPBRW_TOPLINE += wPageLine;
MsgPost(NULL, EVENT_DRAW, 0);
}
break;
case CM_SMPBRW_SCANTOTAL:
{
UINT8* pBuf = handle->pData;
UINT16 wid;
UINT8 byRet;
UINT32 totals;
wid = handle->uWidth;
totals = 0;
if(!pBuf)
{
nRet = -1;
break;
}
while(*pBuf)
{
pBuf += SmpBrwGetLine(pBuf, wid, wFontLib, &byRet);
if(byRet == 0xff)
{
nRet = -1;
break;
}
totals ++;
if(byRet == 1)
{
nRet = totals;
break;
}
}
}
break;
default:
nRet = 0;
break;
}
}
break;
case EVENT_KEY:
case EVENT_KEYREPEAT:
{
UINT uKey;
uKey = READKB_VALUE(uParam);
if(bInit != SMPBRW_INITCODE)
{
break;
}
if(uKey == MVK_UP)
{
MsgPost(NULL,EVENT_COMMAND, CM_SMPBRW_UP);
nRet = 1;
}
else if(uKey == MVK_DOWN)
{
MsgPost(NULL,EVENT_COMMAND, CM_SMPBRW_DWN);
nRet = 1;
}
else if(uKey == MVK_PGUP)
{
MsgPost(NULL,EVENT_COMMAND, CM_SMPBRW_PGUP);
nRet = 1;
}
else if(uKey == MVK_PGDN)
{
MsgPost(NULL,EVENT_COMMAND, CM_SMPBRW_PGDWN);
nRet = 1;
}
}
break;
case EVENT_DRAW:
{
UINT32 dwline = 0;
UINT8* pData;
UINT8* pBuf;
UINT8 byRet,byLineLen;
INT16 x,y;
INT i;
//检查初始化是否成功
if(bInit != SMPBRW_INITCODE)
break;
// 申请内存用于临时使用
pBuf = MemAlloc(150);
if(!pBuf) return 1;
/*搜索到当前行*/
pData = handle->pData;
while(dwline < dwTopLine)
{
pData += SmpBrwGetLine(pData, handle->uWidth, wFontLib, &byRet);
if(byRet<0) return 1;
dwline ++;
}
//画当前页
GraphDisableRefresh();
//清屏幕
GraphClearRect(handle->uX,handle->uY,
handle->uX+handle->uWidth-1,
handle->uY+handle->uHeight-1);
//画每一行数据
x = handle->uX;
y = handle->uY;
for(i=0; i<wPageLine; i++)
{
//获取一行的长度
byLineLen = (UINT8)SmpBrwGetLine(pData, handle->uWidth, wFontLib, &byRet);
if(byRet < 0) return 1;
//拷贝数据
memcpy(pBuf, pData, byLineLen);
*(pBuf+byLineLen) = 0x00;
//画数据
GraphDrawText(x,y,0,0, pBuf);
//更新指针
pData += byLineLen;
//更新变量
y += wLineSpace;
}
GraphEnableRefresh();
if(pBuf)
{
MemFree(pBuf);
pBuf = NULL;
}
}
break;
case EVENT_INITIALIZE:
{
INT16 x,y;
UINT16 wid,hei;
UINT16 wFonthi;
INT nData;
/*初始化状态为失败*/
SMPBRW_INIT = 0;
x = handle->uX;
y = handle->uY;
wid = handle->uWidth;
hei = handle->uHeight;
//验证参数合法性
if(handle->pData == NULL)
break;
if(x > LCD_SCREEN_WIDTH || y > LCD_SCREEN_HEIGHT)
break;
if(x+wid > LCD_SCREEN_WIDTH)
wid = LCD_SCREEN_WIDTH-x;
if(y+hei > LCD_SCREEN_HEIGHT)
hei = LCD_SCREEN_HEIGHT-y;
//初始化变量
handle->uWidth = wid;
handle->uHeight = hei;
//计算行高
wFonthi = FontGetHeight(SYSTLIB);
wFonthi += SMPBRW_LINESPACE;
SMPBRW_LINEHI = (UINT8)wFonthi;
//计算一页的行数
if(hei < wFonthi) //如果不足于放一行,返回失败
break;
SMPBRW_PAGELINE = (UINT8)(hei/wFonthi);
//设置初始顶行
SMPBRW_TOPLINE = 0;
//搜索总行数
nData = MsgSend2((APGUI_STRUCT_MODEL*)handle,
EVENT_COMMAND, CM_SMPBRW_SCANTOTAL);
if(nData == -1)
break;
SMPBRW_TOTAL = (UINT32)nData;
//初始化完毕
SMPBRW_INIT = SMPBRW_INITCODE;
nRet = 1;
}
break;
case EVENT_DESTROY:
break;
}
return nRet;
}
/****************************************************************************
Function:
SmpBrwGetLine
Description:
简单浏览器搜索一行数据
input:
pData - 搜索行起始的位置
wLineWidth - 一行的宽度
fontlib - 字库
output:
*pRet - 0 成功找到一行
1 找到最后一行
0xff 错误失败
return
- 一行数据的长度
note:
****************************************************************************
高林辉 2006-4-14 创建
****************************************************************************/
UINT16 SmpBrwGetLine(UINT8* pData, UINT16 wLineWidth, UINT16 fontlib,UINT8* pRet)
{
UINT16 wCount = 0;
UINT16 word;
FontInfo fontinfo;
UINT8* p;
p = pData;
*pRet = 0;
if(!p)
{
*pRet = 0xff;
return 0;
}
while(*p != 0)
{
if(*p&0x80)
{
word = MAKEWORD(*(p+1),*p);
if(FontGetCharInfo(fontlib, word, &fontinfo, 0))
{
word = fontinfo.wFontWidth;
}
else
{
word = 2*FontGetAsciiWidth(fontlib,' ');
}
if(word+wCount > wLineWidth)
break;
wCount += word;
p += 2;
}
else
{
//如果是回车,返回行结束
if(*p == 0x0d)
{
p++;
if(*p == 0x0a)
p++;
break;
}
if(*p == 0x0A)
{
p++;
if(*p == 0xd)
p++;
break;
}
word = *p;
if(FontGetCharInfo(fontlib, word, &fontinfo, 0))
{
word = fontinfo.wFontWidth;
}
else
{
word = FontGetAsciiWidth(fontlib,' ');
}
if(word+wCount > wLineWidth)
break;
wCount += word;
p ++;
}
}
if(*p == 0)
{
*pRet = 1;
}
return (UINT16)(p - pData);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?