📄 ppcalendar.cpp
字号:
#include "GUIpp.h"
#include "ppCalendar.h"
#include "DateTime.h"
#if _USE_GUI == 1
#define CAL_FRAM_WIDTH 2
#define CAL_BLOCKFRAM_WIDTH 1
#define CAL_FRAM_TO_TEXT_MARGIN 3
const char * strWeekName[7] = {
"日","一","二","三","四","五","六"
};
const char * strMonthDay[31] = {
"1","2","3","4","5","6","7","8","9",
"10","11","12","13","14","15","16","17",
"18","19","20","21","22","23","24", "25",
"26", "27","28","29","30","31"
};
void NANACalendar::SetGrid(void)
{
int week;
int i;
int column,line;
week = WeekDay(w_nYear,w_nMonth,1);
w_nMonthTotalDay = week & 0x0000001f;
week = week >> 5;
for(line = 0;line < 5;line ++)
{
for(column = 0;column<7;column++)
{
w_naGridDay[line][column] = -1;
}
}
for(i=0;i<w_nMonthTotalDay;i++)
{
w_naGridDay[((week + i) / 7) % 5][(week + i) % 7] = i;
}
}
NANARect NANACalendar::GetGridRc(int line,int column,NANARect rc)
{
rc.w_nX1= rc.w_nX1 + CAL_FRAM_WIDTH + w_CALENDAR_MARGIN_LEFT +
w_CALENDAR_BLOCK_WIDTH * column;
rc.w_nX2 = rc.w_nX1+ w_CALENDAR_BLOCK_WIDTH ;
rc.w_nY1 = rc.w_nY1 + CAL_FRAM_WIDTH +w_CALENDAR_MARGIN_TOP +
w_CALENDAR_BLOCK_HEIGHT * line;
rc.w_nY2 = rc.w_nY1 + w_CALENDAR_BLOCK_HEIGHT ;
return rc;
}
void NANACalendar::DrawGirdCell(NANARect rcBlock,int nDay,
NANA_FONT font,int day_style)
{
int nTextWidth;
int nTextHeight;
nTextHeight = font->GBFontSize;
nTextWidth = GetTextWidth(strMonthDay[nDay],font,-1);
if(day_style == CAL_DAY_STYLE_WHITE)
{
TextPrint(strMonthDay[nDay],
rcBlock.w_nX1+ ( w_CALENDAR_BLOCK_WIDTH - nTextWidth) /2,
rcBlock.w_nY1 + ( w_CALENDAR_BLOCK_HEIGHT - nTextHeight) /2,
font,0);
}else if(day_style == CAL_DAY_STYLE_WHITE_FRAME)
{
RectLine(rcBlock.w_nX1 + w_CALENDAR_BLOCK_MARGIN_LEFT,
rcBlock.w_nY1+ w_CALENDAR_BLOCK_MARGIN_TOP,
rcBlock.w_nX1 + w_CALENDAR_BLOCK_MARGIN_LEFT+w_CALENDAR_BLOCK_WIDTH - w_CALENDAR_BLOCK_MARGIN_LEFT * 2,
rcBlock.w_nY1+ w_CALENDAR_BLOCK_MARGIN_TOP+w_CALENDAR_BLOCK_HEIGHT - w_CALENDAR_BLOCK_MARGIN_TOP * 2,
COLOR_BORDER);
TextPrint(strMonthDay[nDay],
rcBlock.w_nX1+ ( w_CALENDAR_BLOCK_WIDTH - nTextWidth) /2,
rcBlock.w_nY1 + ( w_CALENDAR_BLOCK_HEIGHT - nTextHeight) /2,
font,0);
}else if ( day_style == CAL_DAY_STYLE_NEG)
{
Rect(rcBlock.w_nX1 + w_CALENDAR_BLOCK_MARGIN_LEFT,
rcBlock.w_nY1 + w_CALENDAR_BLOCK_MARGIN_TOP,
w_CALENDAR_BLOCK_WIDTH - w_CALENDAR_BLOCK_MARGIN_LEFT * 2,
w_CALENDAR_BLOCK_HEIGHT - w_CALENDAR_BLOCK_MARGIN_TOP * 2,
COLOR_BORDER,
COLOR_BKGROUND);
TextPrint(strMonthDay[nDay],
rcBlock.w_nX1 + ( w_CALENDAR_BLOCK_WIDTH - nTextWidth) /2,
rcBlock.w_nY1 + ( w_CALENDAR_BLOCK_HEIGHT - nTextHeight) /2,
font,1);
}else
if( day_style == CAL_DAY_STYLE_DOUBLE_FRAME)
{
RectanglLine(rcBlock.w_nX1 + w_CALENDAR_BLOCK_MARGIN_LEFT - 1,
rcBlock.w_nY1 + w_CALENDAR_BLOCK_MARGIN_TOP - 1,
w_CALENDAR_BLOCK_WIDTH - w_CALENDAR_BLOCK_MARGIN_LEFT * 2 + 2,
w_CALENDAR_BLOCK_HEIGHT - w_CALENDAR_BLOCK_MARGIN_TOP * 2 + 2);
RectanglLine(rcBlock.w_nX1 + w_CALENDAR_BLOCK_MARGIN_LEFT + 1,
rcBlock.w_nY1 + w_CALENDAR_BLOCK_MARGIN_TOP + 1,
w_CALENDAR_BLOCK_WIDTH - w_CALENDAR_BLOCK_MARGIN_LEFT * 2 - 2,
w_CALENDAR_BLOCK_HEIGHT - w_CALENDAR_BLOCK_MARGIN_TOP * 2 - 2);
TextPrint(strMonthDay[nDay],
rcBlock.w_nX1 + ( w_CALENDAR_BLOCK_WIDTH - nTextWidth) /2,
rcBlock.w_nY1 + ( w_CALENDAR_BLOCK_HEIGHT - nTextHeight) /2,
font,0);
}else
{// CAL_DAY_STYLE_NEG_FRAME
RectanglLine(rcBlock.w_nX1 + w_CALENDAR_BLOCK_MARGIN_LEFT - 1,
rcBlock.w_nY1 + w_CALENDAR_BLOCK_MARGIN_TOP - 1,
w_CALENDAR_BLOCK_WIDTH - w_CALENDAR_BLOCK_MARGIN_LEFT * 2 + 2,
w_CALENDAR_BLOCK_HEIGHT - w_CALENDAR_BLOCK_MARGIN_TOP * 2 + 2);
RectangleBlock(rcBlock.w_nX1 + w_CALENDAR_BLOCK_MARGIN_LEFT + 1,
rcBlock.w_nY1 + w_CALENDAR_BLOCK_MARGIN_TOP + 1,
w_CALENDAR_BLOCK_WIDTH - w_CALENDAR_BLOCK_MARGIN_LEFT * 2 - 2,
w_CALENDAR_BLOCK_HEIGHT - w_CALENDAR_BLOCK_MARGIN_TOP * 2 - 2,0);
TextPrint(strMonthDay[nDay],
rcBlock.w_nX1 + ( w_CALENDAR_BLOCK_WIDTH - nTextWidth) /2,
rcBlock.w_nY1 + ( w_CALENDAR_BLOCK_HEIGHT - nTextHeight) /2,
font,1);
}
}
int NANACalendar::Modify(int delta_Year,int delta_Month)
{
int nYear;
int nMonth;
nMonth = w_nMonth + delta_Month;
if(nMonth > 12){
delta_Year += nMonth / 12;
nMonth = nMonth % 12;
}
if(nMonth < 1)
{
delta_Year -= (((-nMonth)/12) + 1);
nMonth = 12 - ((-nMonth)%12);
}
nYear = w_nYear + delta_Year;
return Set(nYear,nMonth,w_day_selected_date + 1);
}
void NANACalendar::MoveSelectedDay(int delta_select_day)
{
w_day_selected_date += delta_select_day;
if(w_day_selected_date < 0)
{
w_day_selected_date = w_nMonthTotalDay - 1;
}
if((w_day_selected_date + 1) > w_nMonthTotalDay)
{
w_day_selected_date = 0;
}
SetUIDirty(1);
}
int NANACalendar::Set(int nYear,int nMonth,int nSelDay)
{
if(nYear <= 9999 && nYear >= 0 &&
nMonth >=1 && nMonth<=12 &&
nSelDay>=1 && nSelDay<=31)
{
w_nYear = nYear;
w_nMonth = nMonth;
w_day_selected_date = nSelDay - 1;
SetGrid();
if((w_day_selected_date + 1) > w_nMonthTotalDay)
{
w_day_selected_date = w_nMonthTotalDay - 1;
}
SetUIDirty(1);
return 1;
}else{
return 0;
}
}
void NANACalendar::SetDayStyle(int nDay,int nDayStyle)
{
w_day_style[nDay] = nDayStyle;
}
void NANACalendar::OnPaint(void)
{
int line;
int column;
int nTextWidth;
int nTextHeight;
char caTemp[10];
char caYearMonth[20];
NANARect rc;
NANARect rcBlock;
NANA_FONT font;
int nDay;
font = w_Font;
nTextWidth = GetTextWidth("日",font,-1);
nTextHeight = font->GBFontSize;
GetWindowRect(&rc);
if(! w_bPaintSelected )
{
//绘制边框
RectFill(rc.w_nX1,rc.w_nY1,rc.w_nX2,rc.w_nY2,COLOR_BKGROUND);
if (w_style & CAL_STYLE_THIN_FRAME)
{
RectanglLine(rc.w_nX1, rc.w_nY1,rc.w_nX2-rc.w_nX1,rc.w_nY2-rc.w_nY1);
}else
if (w_style & CAL_STYLE_THICK_FRAME)
{
RectanglLine(rc.w_nX1, rc.w_nY1,rc.w_nX2-rc.w_nX1,rc.w_nY2-rc.w_nY1);
RectanglLine(rc.w_nX1+1,rc.w_nY1+1,rc.w_nX2-rc.w_nX1-2,rc.w_nY2-rc.w_nY1-2);
}else
if (w_style & CAL_STYLE_ROUND_FRAME)
{
RoundRectangle_little(rc.w_nX1,rc.w_nY1,rc.w_nX2,rc.w_nY2);
}
//绘制标题
line = 0;
for(column = 0 ; column < 7 ; column ++)
{
rcBlock = GetGridRc(line, column, rc);
RectangleBlock(rcBlock.w_nX1,rcBlock.w_nY1,rcBlock.w_nX2-rcBlock.w_nX1,
rcBlock.w_nY2-rcBlock.w_nY1,0);
TextPrint(strWeekName[column],
rcBlock.w_nX1 + ( w_CALENDAR_BLOCK_WIDTH - nTextWidth) /2,
rcBlock.w_nY1 + ( w_CALENDAR_BLOCK_HEIGHT - nTextHeight) /2,
font,1);
}
//绘制年月日状态栏
strcpy(caYearMonth,"");
sprintf(caTemp,"%d",w_nYear);
strcat(caYearMonth,caTemp);
strcat(caYearMonth,"年");
sprintf(caTemp,"%d",w_nMonth);
strcat(caYearMonth,caTemp);
strcat(caYearMonth,"月");
TextPrint(caYearMonth,
rc.w_nX1 + (rc.w_nX2-rc.w_nX1-GetTextWidth(caYearMonth,font,-1))/2,
rc.w_nY2 - nTextHeight - ( w_CALENDAR_BLOCK_HEIGHT - nTextHeight) /2,
font,0);
}
for(line = 1;line < 6;line ++)
{
for(column = 0;column < 7 ;column ++)
{
nDay = w_naGridDay[line-1][column];
if(nDay >= 0 && nDay <= 31)
{
rcBlock = GetGridRc(line, column, rc);
if( (w_bPaintSelected) &&
(nDay == w_day_selected_date) &&
(!w_bSelectedShow) )
{
RectangleBlock(rcBlock.w_nX1,rcBlock.w_nY1,
rcBlock.w_nX2-rcBlock.w_nX1,
rcBlock.w_nY2-rcBlock.w_nY1,1);
}
if( ( !w_bPaintSelected )||
(w_bPaintSelected &&
nDay == w_day_selected_date &&
w_bSelectedShow)
)
{
if(nDay == w_day_selected_date)
{
DrawGirdCell(rcBlock,nDay,font,w_selday_style);
}
else
{
DrawGirdCell(rcBlock,nDay,font,w_day_style[nDay]);
}
}
}
}
}
}
void NANACalendar::OnTimer(NANATimer *pTimer)
{
if(IsWindowVisible() && (w_style & CAL_STYLE_SEL_BLINK))
{
w_bPaintSelected = 1;
w_bSelectedShow = !(w_bSelectedShow);
SetUIDirty(1);
//注意这里我们强制重绘,目的是和老的代码兼容
//也就是以w_bPaintSelected==1状态重绘,然后
//立即恢复w_bPaintSelected为0
NANADeskTopWnd::Instance()->UpdateUI();
w_bPaintSelected = 0;
}
}
void NANACalendar::CreateWnd(NANAWnd * pParentWnd,
int nX1,int nY1,
int nWidth,int nHeight,
int nStyle,
int nExtendedStyle,
NANA_FONT font)
{
int nTextHeight,nTextWidth;
w_selday_style = CAL_DAY_STYLE_WHITE_FRAME;
//创建窗体
NANAWnd::CreateWnd(pParentWnd,nX1, nY1, nWidth,nHeight,nStyle);
nTextWidth = GetTextWidth("00",font,-1);
nTextHeight = font->GBFontSize;
w_style = nExtendedStyle;
w_nWidth = nWidth;
w_nHeight = nHeight;
nWidth = nWidth - CAL_FRAM_WIDTH * 2;
nHeight= nHeight - CAL_FRAM_WIDTH * 2;
w_Font = font;
w_CALENDAR_BLOCK_WIDTH = nWidth / 7;
w_CALENDAR_BLOCK_HEIGHT = nHeight / 7;
w_CALENDAR_MARGIN_LEFT = (nWidth%7) - (nWidth%7)/2;
w_CALENDAR_MARGIN_TOP = (nHeight%7)/2;
if(w_CALENDAR_BLOCK_WIDTH - CAL_FRAM_TO_TEXT_MARGIN * 2- CAL_BLOCKFRAM_WIDTH *2 < nTextWidth ||
w_CALENDAR_BLOCK_HEIGHT - CAL_FRAM_TO_TEXT_MARGIN * 2 - CAL_BLOCKFRAM_WIDTH *2 < nTextHeight)
{
//not big enough ,fail to create calendar
DEBUG_Assert(0);
return;
}
w_CALENDAR_BLOCK_MARGIN_LEFT =
( w_CALENDAR_BLOCK_WIDTH - CAL_FRAM_TO_TEXT_MARGIN-
nTextWidth - CAL_BLOCKFRAM_WIDTH * 2 ) / 2;
w_CALENDAR_BLOCK_MARGIN_TOP =
( w_CALENDAR_BLOCK_HEIGHT - CAL_FRAM_TO_TEXT_MARGIN-
nTextHeight - CAL_BLOCKFRAM_WIDTH * 2 ) / 2;
w_CALENDAR_TITLE_MARGIN_LEFT =
(nWidth - GetTextWidth("0000年00月",font,-1)) / 2;
w_bPaintSelected = 0;
SetGrid();
w_btnPrevYear.CreateWnd(this,nX1+3,nY1+nHeight - font->GBFontSize-4,
3+font->GBFontSize,
5+font->GBFontSize);
w_btnPrevYear.SetCaption("<");
w_btnNextYear.CreateWnd(this,nX1+nWidth - font->GBFontSize-2 ,nY1+nHeight - font->GBFontSize-4,
3+font->GBFontSize,
5+font->GBFontSize);
w_btnNextYear.SetCaption(">");
w_btnPrevMonth.CreateWnd(this,nX1+2+font->GBFontSize+4,nY1+nHeight - font->GBFontSize-4,
2*font->GBFontSize,
5+font->GBFontSize);
w_btnPrevMonth.SetCaption("<-");
w_btnNextMonth.CreateWnd(this,nX1+nWidth - font->GBFontSize*3 -2 ,nY1+nHeight - font->GBFontSize-4,
2*font->GBFontSize,
5+font->GBFontSize);
w_btnNextMonth.SetCaption("->");
w_CalTimer.Start(4,4);
}
void NANACalendar::OnNotify(NANAWnd *pSrcWnd,int nMessageCode,int nParam1,int nParam2)
{
if(nMessageCode == UGNC_CLICK)
{
if(pSrcWnd == &w_btnPrevMonth)
{
Modify(0,-1);
}else
if(pSrcWnd == &w_btnNextMonth)
{
Modify(0,1);
}else
if(pSrcWnd == &w_btnPrevYear)
{
Modify(-1,0);
}else
if(pSrcWnd == &w_btnNextYear)
{
Modify(1,0);
}
}
}
void NANACalendar::OnMouseDown(int x,int y)
{
}
void NANACalendar::OnMouseUp(int x,int y)
{
int line,column,nDay;
NANARect rcBlock,rc;
GetWindowRect(&rc);
for(line = 1;line < 6;line ++)
{
for(column = 0;column < 7 ;column ++)
{
nDay = w_naGridDay[line-1][column];
if(nDay >= 0)
{
rcBlock = GetGridRc( line, column, rc);
if(rcBlock.IsPointInside(x,y))
{
w_day_selected_date = nDay;
SetUIDirty(1);
}
}
}
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -