📄 scrollshow.c
字号:
//
// 滚动显示窗口
// by liaonh 2008/1
//
#include "test.h"
#include "Def.h"
#define MAX_LINES 100
String gstr_text[MAX_LINES*50] = {0};
#define MAX_TEXTLEN (sizeof(gstr_text)-1)
U16 gptr_pos[MAX_LINES];
U16 gi_lines;
U16 UpdateFlag;
U32 UpdateTimeout;
#define BoxBorder 4
U16 BoxWidth = (SW*3/4);
U16 BoxHeight = 200;
U16 BoxX = SW - (SW*3/4)-2;
U16 BoxY = SH - 220;
U8 CurrentLine = 0;
U16 CanShowlines;
#define TEXTAREA_WIDTH (BoxWidth-2*BoxBorder)
#define TEXTAREA_HEIGHT (BoxHeight-2*BoxBorder - FONT_HEIGHT - 5)
#define TEXTAREA_LINES (TEXTAREA_HEIGHT/(FONT_HEIGHT+2))
#define TEXTAREA_PAGELINE (TEXTAREA_LINES-1)
// 解析文本需要使用的行数
// str: text to parse
// ptr_pos: line offset in the text
// max_lines: lines limit
// width: Lcd Width or width you want to assign to...
static U16 ParseLines(String const * str,U16* ptr_pos,U16 max_lines,U16 width)
{
U16 x,lines,bakpos;
String const * ptr;
if(str == NULL)
{
return(0);
}
x = 0;
lines = 0;
bakpos = 0;
ptr = str;
while(lines < (max_lines - 1))
{
if(*ptr == 0)
{
if(x)
{
ptr_pos[lines++] = bakpos;
}
bakpos = ptr - str;
break;
}
else if((*ptr == '\r') || (*ptr == '\n'))
{
if( ptr[0] == '\r' && ptr[1] == '\n' )
ptr++;
ptr++;
x = 0;
ptr_pos[lines++] = bakpos;
bakpos = ptr - str;
}
else if( ptr[0] >= 0xa1 )
{
if((x + FONT_WIDTH*2) > width)
{
x = 0;
ptr_pos[lines++] = bakpos;
bakpos = ptr - str;
}
x += FONT_WIDTH*2;
ptr += 2;
}
else
{
if((x + FONT_WIDTH) > width)
{
x = 0;
ptr_pos[lines++] = bakpos;
bakpos = ptr - str;
}
x += FONT_WIDTH;
ptr++;
}
}
ptr_pos[lines] = bakpos;
return(lines);
}
void ClearText(void)
{
memset(gstr_text,0,sizeof(gstr_text));
memset(gptr_pos,0,sizeof(gptr_pos));
gi_lines = 0;
}
void SetNeedUpdate(void)
{
UpdateFlag |= 1;
UpdateTimeout = RunTimeMs + 100;
}
void AppendText(const char* text)
{
U16 len = strlen((char*)gstr_text);
U16 nlen = strlen(text);
U16 leftlen,dellen;
nlen += len;
if( nlen > MAX_TEXTLEN)
{
gi_lines = ParseLines(gstr_text,gptr_pos,MAX_LINES,BoxWidth);
dellen = nlen - MAX_TEXTLEN;
nlen = 0;
while( gptr_pos[nlen] < dellen && nlen < gi_lines )
nlen++;
if( nlen < gi_lines )
{
if( len > gptr_pos[nlen] )
leftlen = len - gptr_pos[nlen];
else
leftlen = 0;
for( len = 0; len < leftlen; len++)
{
gstr_text[len] = gstr_text[len+gptr_pos[nlen]];
}
gstr_text[len] = 0;
}
else
{
gstr_text[0] = 0;
}
}
strcat((char*)gstr_text,text);
// 确保最后一行可见
gi_lines = ParseLines(gstr_text,gptr_pos,MAX_LINES,BoxWidth);
if( CurrentLine >= gi_lines )
CurrentLine = 0;
while( (CurrentLine+TEXTAREA_PAGELINE) <= gi_lines )
CurrentLine++;
SetNeedUpdate();
}
void ShowScrollText(void)
{
String bakch;
String* ptr;
int i;
int x,y;
gi_lines = ParseLines(gstr_text,gptr_pos,MAX_LINES,TEXTAREA_WIDTH);
CanShowlines = TEXTAREA_LINES;
if( CurrentLine >= gi_lines )
{
if( gi_lines < CanShowlines )
CurrentLine = 0;
else
CurrentLine = gi_lines - CanShowlines;
}
if( gi_lines <= CurrentLine )
return;
CanShowlines += CurrentLine;
if( CanShowlines > gi_lines )
CanShowlines = gi_lines;
ResetTextOut();
SetTextColor(COLOR_WHITE);
SetBkColor(COLOR_BLACK);
Rectangle(BoxX,BoxY,BoxWidth,BoxHeight,gi_CurrentTextColor);
Rectangle(BoxX+1,BoxY+1,BoxWidth-2,BoxHeight-2,gi_CurrentBkColor);
Rectangle(BoxX+2,BoxY+2,BoxWidth-4,BoxHeight-4,gi_CurrentTextColor);
FillRect(BoxX+3,BoxY+3+FONT_HEIGHT+2,BoxWidth-6,BoxHeight-2-6-FONT_HEIGHT,gi_CurrentBkColor);
y = BoxY+BoxBorder;
SetTextColor(COLOR_BLUE);
SetTextMode(TM_HALFTRANSPARENT);
TextOut(BoxX+BoxBorder,y,"点击显示区上部上翻,下部下翻");
SetTextMode(TM_NOTTRANSPARENT);
SetTextColor(COLOR_WHITE);
x = BoxX+BoxBorder;
//#define LINE_NUMBER
#ifdef LINE_NUMBER
x += 5*FONT_WIDTH;
#endif
y += FONT_HEIGHT + 5;
for( i = CurrentLine; i < CanShowlines; i++)
{
#ifdef LINE_NUMBER
sprintf((char*)buf,"%03d: ",i+1);
TextOut(BoxX+BoxBorder,y,(char*)buf);
#endif
ptr = &gstr_text[gptr_pos[i+1]];
bakch = *ptr;
*ptr = 0;
TextOut(x,y,(char*)&gstr_text[gptr_pos[i]]);
*ptr = bakch;
y += (FONT_HEIGHT+2);
}
ResetTextOut();
}
int CheckMouse(void)
{
int x,y,mouse,ch = 0;
mouse = GetMousePos(&x,&y);
if( ch == 0 )
{// 检测鼠标动作
mouse = MouseDown();
if( mouse == MS_PRESSING )
ch = MOUSE_DOWN;
else if( mouse == MS_RELEASING )
ch = MOUSE_RELEASE;
else if( mouse == MS_PRESSED)
ch = MOUSE_MOVE;
}
return ch;
}
int ScrollShow(int v)
{
int ch;
int x,y,sx,sy;
int todo = 0;
ch = v & 0xff;
switch(ch)
{
case MOUSE_RELEASE:
GetMouseLastPos(&x, &y);
sx = TX2SX(x);
sy = TY2SY(y);
if( IsPtInRect(sx,sy,BoxX,BoxY,BoxWidth,BoxHeight/2))
{
todo = 1; // up
}
else if( IsPtInRect(sx,sy,BoxX,BoxY+BoxHeight/2,BoxWidth,BoxHeight/2))
{
todo = 2;// down
}
break;
case KEY_PAGEDOWN: todo = 2; break;
case KEY_PAGEUP: todo = 1; break;
}
if ( todo == 1 )
{
if( CurrentLine >= TEXTAREA_PAGELINE )
{
CurrentLine -= TEXTAREA_PAGELINE;
SetNeedUpdate();
}
else if( CurrentLine != 0 )
{
CurrentLine = 0;
SetNeedUpdate();
}
}
else if ( todo == 2 )
{
if( gi_lines >= (CurrentLine+TEXTAREA_PAGELINE) )
{
CurrentLine += TEXTAREA_PAGELINE;
SetNeedUpdate();
}
// else
// {
// CurrentLine = 0;
// SetNeedUpdate();
// }
}
if( (UpdateFlag & 1) )
{
if ( UpdateTimeout < RunTimeMs )
{
DisableUpdate(BoxX,BoxY,BoxX+BoxWidth,BoxY+BoxHeight);
ShowScrollText();
EnableUpdate(BoxX,BoxY,BoxX+BoxWidth,BoxY+BoxHeight);
UpdateTimeout = 0;
UpdateFlag = 0;
}
}
}
void SetScrollBox(int sel)
{
if( sel == 0 )
{
BoxWidth = (SW*3/4);
BoxHeight = 200;
}
else if (sel == 1)
{
BoxWidth = (SW*1/4);
BoxHeight = 400;
}
else if (sel == 2)
{
BoxWidth = (SW*2/4);
BoxHeight = 400;
}
BoxX = SW - BoxWidth;
BoxY = SH - 20 - BoxHeight;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -