uses
  sysutils, menus, IniFiles;

var
  ShowType:integer;
  ImagePath,DaySkin:string;

procedure nGotoTodayClick(Sender:TObject);
begin
  cal.date:=now;
end;

procedure IncDate(value:integer);
begin
  //cal.date:=cal.date+value;
  cal.date:=incMonth(cal.date,value);
end;

procedure PrevMonth(Sender:TObject);
begin
  IncDate(-1);
end;

procedure NextMonth(Sender:TObject);
begin
  IncDate(1);
end;

procedure CalDateChange;
var
  y,m,d:word;
begin
  xspfrm.beginupdate;
  DecodeDate(Cal.Date,y,m,d);
  //lblYear.Caption:=IntToStr(y);
  //lblMonth.caption:=FormatDateTime('mmmm  ',cal.date);
  //lblLunar.caption:=DateToDateCnStr(cal.date,'yyyyy(a)年mmmm月ddd');
  imgMonth.ImageStyle.ImageIndex:=m-1;
  xspfrm.endupdate;
end;

procedure InitCal;
var
  i:integer;
  sPath:string;
  ini:TIniFile;
begin
  ShowType:=GetSettingInt('ShowType',0);
  DaySkin:=GetSetting('DaySkin','Comic');

  BeginUpdate;
  case ShowType of
    1: cal.layout:=xclhorz;
    2: cal.layout:=xclVert;
  else
    cal.layout:=xclGrid;
  end;
  //cal.layout:=xclHorz;
  
  cal.ShowYear:=false;
  cal.ShowMonth:=false;
  nShowWeek.checked:=GetSettingBool('ShowWeek',true);
  cal.ShowWeek:=nShowWeek.checked;
  cal.ShowLunar:=false;
  cal.FontStyle.Align:=xaCenter;
  //cal.DayWidth:=22;
  //cal.showSolar:=false;
  sPath:=ImagePath+'Days\'+DaySkin+'\';
  if FileExists(sPath+'setting.ini') then
  begin
    ini:=TIniFile.Create(sPath+'setting.ini');
    with ini do
    try
      cal.SolarBitmapName.DayNums:=ReadInteger('setting','DayNums',10);
      cal.SolarBitmapName.DaySep:=ReadInteger('setting','DaySep',-3);
    finally
      ini.free;
    end;
  end
  else
  begin
    cal.SolarBitmapName.DayNums:=10;
    cal.SolarBitmapName.DaySep:=-3;
  end;
  cal.SolarBitmapName.Day    :=sPath+GetSetting('NormalFile','白色')+'.png';
  cal.SolarBitmapName.Holiday:=sPath+GetSetting('HolidayFile','蓝色')+'.png';
  cal.SolarBitmapName.Event  :=sPath+GetSetting('EventFile','红色')+'.png';
  cal.SolarBitmapName.Weekend:=sPath+GetSetting('WeekendFile','粉红')+'.png';
  cal.SolarBitmapName.TodayNums:=1;
  cal.SolarBitmapName.Today  :=ImagePath+'Today\'+GetSetting('TodayFile','Red')+'.png';
  cal.SolarBitmapName.Week   :=ImagePath+'Weekdays\'+GetSetting('WeekdaysFile','黑体')+'.png';
  imgMonth.FileName:=ImagePath+'Months\'+GetSetting('MonthFile','超粗黑')+'.png';

  imgMonth.ImageStyle.Align:=xaCenter;
  case cal.layout of
    xclVert:
      begin
        with imgMonth do
        begin
          left:=-25; 
          top:=0;
          ImageStyle.Align:=xaCenter;
          ImageStyle.Angle:=90;
          width:=80; 
          height:=180;
        end;
        with imgPrevMonth do
        begin
          left:=5; top:=8;
          ImageStyle.Angle:=90;
        end;
        with imgNextMonth do
        begin
          left:=5; top:=162;
          ImageStyle.Angle:=90;
        end;
        with cal do
        begin
          left:=30; top:=0;
          DayHeight:=26;
          DayWidth:=28;
          if cal.ShowWeek then
            width:=DayWidth*2+12;
          else
            width:=DayWidth+6;
          height:=DayHeight*31;
          Widget.width:=left+width;
          Widget.height:=height;
        end;
      end;
    xclHorz:
      begin
        with imgMonth do
        begin
          left:=0; 
          top:=0;
          width:=180; 
          height:=25;
          ImageStyle.Angle:=0;
        end;
        with imgPrevMonth do
        begin
          left:=8; top:=5;
          ImageStyle.Angle:=0;
        end;
        with imgNextMonth do
        begin
          left:=162; top:=5;
          ImageStyle.Angle:=0;
        end;
        with cal do
        begin
          top:=30; left:=0;
          DayHeight:=28;
          if cal.ShowWeek then
            height:=DayHeight*2+12;
          else
            height:=DayHeight+10;
          DayWidth:=26;
          width:=DayWidth*31;
          Widget.width:=width;
          Widget.height:=height+imgMonth.height;
        end;
      end;
  else //Grid
    begin  
      with imgMonth do
      begin
        left:=0; 
        top:=0;
        width:=180; 
        height:=25;
        ImageStyle.Angle:=0;
      end;
      with imgPrevMonth do
      begin
        left:=8; top:=5;
        ImageStyle.Angle:=0;
      end;
      with imgNextMonth do
      begin
        left:=162; top:=5;
        ImageStyle.Angle:=0;
      end;
      with cal do
      begin
        top:=30; left:=0;
        DayWidth:=29;
        width:=DayWidth*7;
        if cal.ShowWeek then
        begin
          DayHeight:=22;
          height:=DayHeight*7+12;
        end
        else
        begin
          DayHeight:=26;
          height:=DayHeight*6+6;
        end
        Widget.width:=width+10;
        Widget.height:=height+imgMonth.height;
      end;
    end;
  end;
  EndUpdate;
end;

procedure nShowTypeGridClick(Sender:TObject);
begin
  SetSettingInt('ShowType',TMenuItem(Sender).tag);
  InitCal;
end;

procedure nShowWeekClick(Sender:TObject);
begin
  SetSettingBool('ShowWeek',nShowWeek.checked);
  InitCal;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  ImagePath:=WidgetPath+'images\';
  BeginUpdate;
  cal.date:=now;
  InitCal;

  
  CalDateChange;
 
  EndUpdate;
end;

//--- 系统日期改变事件，当系统日期改变时会触发此事件 ---
procedure OnSystemDateChanged;
begin
  Cal.Date:=now;
end;
