📄 cellview.c
字号:
#include "string.h"
/*#include "malloc.h"*/
#include "CellView.h"
#ifdef _WIN32
#include "Windows.h"
#else
#include <gendef.h>
#include "osg.h"
#include "main_task.h"
#endif
#define DEFAULT -1
#define IS_HZ(num) (num & 0x80) == 0x80
/*
建立单元视图区
*/
void DrawCell(void *data,int x,int y,int color);
/*static void DrawLine(int x0,int y0, int x1,int y1);*/
static void DrawRectangle(int x0,int y0,int x1,int y1,int bkcolor,int framecolor);
static void DrawText(int x,int y,void *data,int color);
static void DrawRectangle(int x0,int y0,int x1,int y1,int bkcolor,int framecolor)
{
/*绘画矩形*/
#ifdef _WIN32
CDC *dc = AfxGetApp()->GetMainWnd()->GetDC();
CBrush brush(bkcolor);
dc->SelectObject(brush);
dc->Rectangle(x0,y0,x1,y1);
AfxGetApp()->GetMainWnd()->ReleaseDC(dc);
#else
GFMDrawRectangle(x0,y0,x1,y1,bkcolor, 1);
#endif
}
static void DrawText(int x,int y,void *data,int color)
{
/*绘画字符*/
#ifdef _WIN32
CDC * dc;
char *p = (char *)data;
dc = AfxGetApp()->GetMainWnd()->GetDC();
if( IS_HZ(p[0]))
dc->TextOut(x,y,p,2);
else if(p[0])
c->TextOut(x,y,p,1);
AfxGetApp()->GetMainWnd()->ReleaseDC(dc);
#else
char *p = (char *)data;
char txt[4] = {0};
if( IS_HZ(p[0]))
{
txt[0] = p[0];
txt[1] = p[1];
FNTDrawText(color,1, x, y, txt );
}
else if(p[0])
{
txt[0] = p[0];
txt[1] = '\0';
FNTDrawText(color,1, x, y, txt );
}
#endif
}
void DrawCell(void *data,int x,int y,int color)
{
DrawRectangle(x,y,CELL_WIDTH,CELL_HIGH,color,0);
if(data)
{
}
}
void DrawCellFrame(pCELLVIEW pview)/*绘画单元表格*/
{
int i,j,k = 0;
int width,high;
int color = Colors_thkblue1;
if(!pview) return;
width = pview->matrixwidth * CELL_WIDTH;
high = pview->matrixhigh * CELL_HIGH;
pview->data_pointor =(unsigned short *) pview->data;
for( i = 0;i < pview->matrixhigh;i++)
for( j = 0;j < pview->matrixwidth;j++)
{
if(i == pview->cursory && j == pview->cursorx)
{
color = Colors_white;
if(pview->OwnerDrawCell)
(*pview->OwnerDrawCell)(NULL, j * CELL_WIDTH + pview->localx,
i * CELL_HIGH + pview->localy,COLOR_thkblue);/*光标*/
else
DrawCell(NULL,j * CELL_WIDTH + pview->localx,
i * CELL_HIGH + pview->localy,COLOR_thkblue);
}
else
{
if(pview->OwnerDrawCell)
(*pview->OwnerDrawCell)(NULL, j * CELL_WIDTH + pview->localx,
i * CELL_HIGH + pview->localy,Colors_weakpurple);
else
DrawCell(NULL, j * CELL_WIDTH + pview->localx,
i * CELL_HIGH + pview->localy,Colors_weakpurple);
color = Colors_thkblue1;
}
if(pview->data_pointor[k] > 0)
{
if(pview->OwnerDrawText)
(*pview->OwnerDrawText)(j * CELL_WIDTH + pview->localx,i * CELL_HIGH
+pview->localy ,&pview->data_pointor[ k ],color);
else
DrawText(j * CELL_WIDTH + pview->localx,i * CELL_HIGH
+pview->localy ,&pview->data_pointor[ k ],color);
}
k++;
}
pview->data_pointor = (unsigned short *) pview->data;
pview->data_pointor += pview->cursory*
pview->matrixwidth + pview->cursorx;
}
pCELLVIEW CreateSingleView(int x,int y,unsigned short *data,int unit)
{
/*
pCELLVIEW vp;
vp = (pCELLVIEW)malloc(sizeof(CELLVIEW));
memset(vp,0,sizeof(pCELLVIEW));
memset(data,0,unit * 2);
vp->data = data;
vp->data_pointor = data;
vp->viewend = -1;
vp->viewlines =1 ;
vp->matrixhigh = 1;
vp->matrixwidth = unit;
vp->localx = x;
vp->localy = y;
return vp;*/
return NULL;
}
void ReleaseSingleView( pCELLVIEW pview)
{
if(pview)
free(pview);
}
pCELLVIEW CreateMultiView(int x,int y,int width,int high)
{
pCELLVIEW vp;
int size;
size = (width / CELL_WIDTH)*(high / CELL_HIGH) *2 + 4;
vp = (pCELLVIEW)malloc(sizeof(CELLVIEW) + size);
if(vp)
{
vp->size = (width / CELL_WIDTH)*(high / CELL_HIGH) ;/*为容纳汉字,单元为2字节*/
memset(vp,0,sizeof(CELLVIEW) +size);
vp->localx = x;
vp->localy = y;
vp->data_pointor = (unsigned short *)vp->data;
/*vp->viewend = -1;/*限制为一叶*/
/* vp->viewlines = high / CELL_HIGH ;/*vp->viewend + 1; */
vp->matrixhigh = high / CELL_HIGH; /*设置矩阵的高宽*/
vp->matrixwidth = width / CELL_WIDTH;
return vp;
}
return NULL;
}
/*
释放内存
*/
void ReleaseMultiView(pCELLVIEW pview)
{
if(pview)
{
/*free(pview->data);*/
free(pview);
}
}
int MoveCursor(pCELLVIEW pview,int xstep,int ystep)
{
/*绘画普通CELL*/
int realx,realy;
realx = pview->cursorx * CELL_WIDTH + pview->localx;
realy = pview->cursory * CELL_HIGH + pview->localy;
DrawCell(NULL,realx,realy,Colors_weakpurple);
DrawText( realx,realy ,pview->data_pointor,Colors_thkblue1);
/*移动光标,并移动数据区指针,并且绘画*/
pview->cursorx += xstep;
pview->cursory += ystep;
if( pview->cursory < 0)
{
pview->cursory = pview->matrixhigh - 1;
if( pview->cursorx > 0)
pview->cursorx--;
else
pview->cursorx = pview->matrixwidth - 1;
}
if( pview->cursorx < 0 )
{
if( pview->cursory > 0)
pview->cursory--;
else
pview->cursory = pview->matrixhigh -1;
pview->cursorx = pview->matrixwidth - 1;
}
if( pview->cursorx >= pview->matrixwidth)
{
if(pview->cursory < (pview->matrixhigh - 1) )
{
pview->cursory++;
pview->cursorx = pview->cursorx %
(pview->matrixwidth - 1) -1;
}else
{
pview->cursorx = 0;
pview->cursory = 0;
}
}
if(pview->cursory >= pview->matrixhigh )
{
pview->cursory = 0;
if( pview->cursorx < pview->matrixwidth - 1)
pview->cursorx++;
else
pview->cursorx = 0;
}
pview->data_pointor = (unsigned short *) pview->data;
pview->data_pointor += pview->cursory*
pview->matrixwidth + pview->cursorx;
/*绘画的焦点CELL*/
realx = pview->cursorx * CELL_WIDTH + pview->localx;
realy = pview->cursory * CELL_HIGH + pview->localy;
DrawCell(NULL, realx,realy,COLOR_thkblue);
DrawText( realx ,realy , pview->data_pointor,Colors_white);
return 1;
}
/*
func:加入数据到矩阵,并且绘画出来,然后让焦点自动增一
return: 成功返回加入数据,失败返回 0
*/
int AddToCellView(pCELLVIEW pview,void *data)
{
/*把数据加入当前数据区指针指向位置,并绘画,移动指针到下个位置,判断越界*/
/*绘画普通cell*/
*pview->data_pointor = *(unsigned short *)data;
/*绘画焦点CELL*/
MoveCursor(pview,1,0);
return *(int*)data;
}
/*
func:矩阵删除数据,并且绘画出来,然后让焦点自动减一
*/
void DelFromCellView(pCELLVIEW pview)
{
if(*pview->data_pointor)/*光标位置有字*/
{
*pview->data_pointor = 0;
MoveCursor(pview,0,0);
}
else
{
* (--pview->data_pointor) = 0;
pview->data_pointor++;
MoveCursor(pview,-1,0);
}
/*绘画焦点CELL*/
}
int InsertToCELLVIEW(pCELLVIEW pview,void *data,int cellx,int celly)
{
/*MoveCursor()*/
/*AddToCELLVIEW()*/
return *(int *)data;
}
unsigned short GetCellViewData(pCELLVIEW pview)
{
return *pview->data_pointor;
}
int GetViewCharCount(pCELLVIEW vp)/*有效字符的统计区别汉字作为两个字符*/
{
int i,j,k = 0 ,num = 0;
if(!vp || !vp->data)return DEFAULT;
vp->data_pointor = (unsigned short *)vp->data;
for(i = 0;i < vp->matrixhigh;i++)
for(j = 0;j < vp->matrixwidth;j++)
{
if(vp->data_pointor[k] != 0)
num++;
k++;
}
return num;
}
int GetViewChars(pCELLVIEW vp,void *dbuf)/*得到有效字符,data_pointor中两个字节为存放一个字符的单元*/
{
int num = 0;
unsigned char *p1 ;
unsigned char *p2 =(unsigned char *)dbuf;
vp->data_pointor = (unsigned short *)vp->data;
p1 = (unsigned char *) vp->data_pointor;
for(;p1 <= (unsigned char *)&vp->data_pointor[vp->matrixwidth*vp->matrixhigh] ;)
{
if(*p1 != 0)
{
if(IS_HZ(*p1))/*是不是汉字字符*/
{
*p2 = *p1;/*是汉字的话两个字节存放*/
p2++;
p1++;
*p2 = *p1;
p1++;
p2++;
num+= 2;
}else
{
*p2 = *p1;
p2++;
p1++;
p1++;
num++;
}
}else
{
p1++;
p1++;
*p2 = ' ';/*单纯的0作为空格*/
p2++;
num++;
}
}
*p2 = '\0';
/* *(unsigned short *)(++p) = 0;/*保证正确结尾*/
return num;
}
void SetSingleViewBuf(pCELLVIEW vp,unsigned char *data,int size)
{
int i,j;
unsigned char *p1,*p2 = &data[size -1];
vp->data_pointor = (unsigned short *)vp->data;
p1 = (unsigned char *) vp->data_pointor;
while(data <= p2)
{
if(IS_HZ(*data))
{
*p1 = *data;
p1++;
data++;
*p1 = *data;
p1++;
data++;
}else
{
*p1 = *data;
p1+=2;
data++;
}
}
DrawCellFrame(vp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -