var
  SingleView,SingleShowMonth:boolean;
  MonthChange:boolean;
  aFontColor:TColor;

procedure ShowNote;
begin
  ShowEditNote(cal.date);
end;
  
procedure IncDateMonth(value:integer);
begin
  if Docking and not SingleShowMonth then
  begin
    if SingleShowMonth then
    begin
      SingleShowMonth:=false;
      SetSetting('SingleShowMonth','0');
    end;
    cal.date:=cal.date+value;
  end
  else
  begin
    MonthChange:=true;
    cal.date:=incMonth(cal.date,value);
  end;
end;

procedure GotoToday;
begin
  cal.date:=now;
end;

procedure ShowDateChange
var
  y,m,d:word;
begin
  BeginUpdate;
  if trunc(cal.date)<>trunc(now) then
  begin
    imgGotoToday.visible:=not SingleView or not SingleShowMonth;
    //lblWeek.visible:=false;
    lblLunar.caption:=DateToDateCnStr(cal.date,'yyyyy(a)年mmmm月ddd');
  end
  else
  begin
    imgGotoToday.visible:=false;
    lblWeek.visible:=not SingleShowMonth;
    lblLunar.caption:=DateToDateCnStr(cal.date,'yyyyy(a)年mmmm月ddd');
  end;
  lblYearMonth.FontStyle.Font.Color:=aFontColor;
  lblDay.FontStyle.Font.Color:=aFontColor;
  lblWeek.FontStyle.Font.Color:=aFontColor;
  lblLunar.FontStyle.Font.Color:=aFontColor;
  DecodeDate(Cal.Date,y,m,d);
  lblYearMonth.Caption:=FormatDateTime('yyyy mmm',cal.date);
  lblDay.caption:=inttostr(d);
  lblWeek.caption:=FormatDateTime('dddd',cal.date);//+GetWeatherInfo(0,'City')
  EndUpdate;
end;

//单/双日历显示切换
procedure SetViewStyle(single:boolean);
const
  LeftSpace=120;
begin
  SingleView:=single;
  BeginUpdate;
  if single then
  begin
    Widget.Width:=136;
    lblYearMonth.cursor:=-21;//crHandPoint;
    imgRings.FileName:=WidgetPath+'images\Rings dock.png';
    imgbg.width:=130;
    imgPrev.hint:='上一天';
    imgNext.hint:='下一天';
    cal.visible:=SingleShowMonth;
    lblYearMonth.visible:=not SingleShowMonth;
    if SingleShowMonth then
    begin//在Dock上显示月历
      //imgbg.FileName:=WidgetPath+'images\calendar_single.png'
      imgGotoToday.visible:=false;
    end
    else
    begin//在桌面上显示日历
      lblYearMonth.OnClick:='ShowSingleMonth';
    end;
    lblYearMonth.left:=0;
    lblDay.left:=0;
    lblWeek.left:=0;
    lblLunar.left:=0;
    lblDay.visible:=lblYearMonth.visible;
    lblWeek.visible:=lblYearMonth.visible;
    lblLunar.visible:=lblYearMonth.visible;
  end
  else
  begin
    Widget.Width:=265;
    SingleShowMonth:=false;
    SetSetting('SingleShowMonth','0');
    lblYearMonth.cursor:=0;//crDefault;
    imgRings.FileName:=WidgetPath+'images\Rings.png';
    imgbg.width:=252;
    imgPrev.hint:='上一月';
    imgNext.hint:='下一月';
    cal.visible:=true;
    lblYearMonth.visible:=true;
    lblDay.visible:=lblYearMonth.visible;
    lblWeek.visible:=lblYearMonth.visible;
    lblLunar.visible:=lblYearMonth.visible;
    lblYearMonth.OnClick:='';
    lblYearMonth.left:=LeftSpace;
    lblDay.left:=LeftSpace;
    lblWeek.left:=LeftSpace;
    lblLunar.left:=LeftSpace;
  end;
  ShowDateChange;
  EndUpdate;
end;

procedure CalDateChange;
begin
  if SingleShowMonth then
  begin
    if MonthChange then
    begin
      MonthChange:=false;
      ShowDateChange;
    end
    else
    begin
      SingleShowMonth:=false;
      SetSetting('SingleShowMonth','0');
      SetViewStyle(true);
    end;
  end
  else
    ShowDateChange;
end;

procedure ShowSingleMonth(sender);
begin
  SingleShowMonth:=true;
  SetSetting('SingleShowMonth','1');
  SetViewStyle(true);
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  SetViewStyle(true);
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  SetViewStyle(false);
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  SingleShowMonth:=false;
  MonthChange:=false;
  BeginUpdate;
  cal.showlunar:=false;
  cal.ShowMonth:=true;
  cal.ShowYear:=true;
  cal.ShortWeekCaption:=true;

  aFontColor:=$FFFFFF;
  SingleView:=false;
  SingleShowMonth:=GetSetting('SingleShowMonth','0')='1';

  if GetSettingInt('CustomColor',0)=1 then
  begin
    imgbg.ImageStyle.ColorMatrixStyle:=xcmColorRotate;
    imgbg.ImageStyle.RotateColor:=StrToColor(GetSetting('Color','$BF9306'),$BF9306);
    case imgbg.ImageStyle.RotateColor of
      $36312D: nbgColor1.checked:=true;
      $823C46: nbgColor2.checked:=true;
      $00B06E: nbgColor3.checked:=true;
      $BF9306: nbgColor4.checked:=true;
      $4B00BB: nbgColor5.checked:=true;
      $4C5589: nbgColor6.checked:=true;
//    else
//      nbgColor0.checked:=true;
    end;
  end
  else
    nbgColorDef.checked:=true;
  cal.calcolors.DayColor:=aFontColor;
  cal.calcolors.SaturdayColor:=aFontColor;
  cal.calcolors.SundayColor:=aFontColor;
  ShowDateChange;
  if Docking then 
  	OnWidgetDock;
  EndUpdate;
  BeginUpdate;
  cal.date:=now;
  cal.NeedRefresh:=true;
  EndUpdateAll;
end;

//--- 系统日期改变事件，当系统日期改变时会触发此事件 ---
procedure OnSystemDateChanged;
begin
  GotoToday;
end;

procedure SetbgColor(value:TColor);
begin
  if value=-1 then
  begin
    imgbg.ImageStyle.ColorMatrixStyle:=xcmNone;
    SetSettingInt('CustomColor',0);
  end
  else
  begin
    imgbg.ImageStyle.RotateColor:=value;
    imgbg.ImageStyle.ColorMatrixStyle:=xcmColorRotate;
    SetSettingInt('CustomColor',1);
    SetSetting('Color',ColorToHtmlStr(value));
  end;
end;
//  		  <nbgColor0 caption="自定义..." onclick="ShowWidgetSetting(0)" AutoCheck="1" RadioItem="1" />
