uses
  Forms, stdctrls, controls, //sysutils,
  ComCtrls;

var
  mode:integer
  section:string;

procedure ShowNote;//用于打开备忘录
begin
  ShowEditNote(cal.date);
end;

procedure GetHuangLiStr(var y:string; var j:string; var c:string; dt:TDate);
var
  lst:TStrings;
begin
  lst:=TStringList.create;
  try
    lst.text:=GetHuangLi(AppPath+'res\Others\HuangLi\',dt);
    y:=lst.Values['y'];
    j:=lst.Values['j'];
    c:=lst.Values['c'];
  finally
    lst.free;
  end;
end;

procedure ModeChange;//集中刷新代码，刷新所有组件并判断是否显示,保存当前模式
var
  y,m,d:word;
  sy,sj,sc:string;
begin
  BeginUpdate;
  imgJan.visible:=(mode=2) or (mode=7) or (mode=8);
  lblJan.visible:=imgJan.visible;
  imgFeb.visible:=imgJan.visible;
  lblFeb.visible:=imgJan.visible;
  imgMar.visible:=imgJan.visible;
  lblMar.visible:=imgJan.visible;
  imgApr.visible:=imgJan.visible;
  lblApr.visible:=imgJan.visible;
  imgMay.visible:=imgJan.visible;
  lblMay.visible:=imgJan.visible;
  imgJun.visible:=imgJan.visible;
  lblJun.visible:=imgJan.visible;
  imgJul.visible:=imgJan.visible;
  lblJul.visible:=imgJan.visible;
  imgAug.visible:=imgJan.visible;
  lblAug.visible:=imgJan.visible;
  imgSep.visible:=imgJan.visible;
  lblSep.visible:=imgJan.visible;
  imgOct.visible:=imgJan.visible;
  lblOct.visible:=imgJan.visible;
  imgNov.visible:=imgJan.visible;
  lblNov.visible:=imgJan.visible;
  imgDec.visible:=imgJan.visible;
  lblDec.visible:=imgJan.visible;
  cal.visible:=(mode=1) or (mode=5) or (mode=6);
  lblYear.visible:=(mode<>3) and (mode<>4);
  lblDay.visible:=(mode<>1) and (mode<>2);
  lblYearMonth.visible:=(mode<>1) and (mode<>2);
  lblWeek.visible:=(mode=4) or (mode=5) or (mode=7);
  lblLunar.visible:=(mode<>1) and (mode<>2);
  imgGotoToday.visible:=(mode=3) or (mode=6) or (mode=8); 
  lblSolarTerm.visible:=(mode=4) or (mode=5) or (mode=7);
  case mode of
  1: 
     begin
       imgbg.FileName:=WidgetPath+'images\'+section+'\calendar_single.png';
	   imgPrev.hint:='上一月';
       imgNext.hint:='下一月';
	   SetSetting('mode',1);	   	   
	 end;
  2: 
     begin
       imgbg.FileName:=WidgetPath+'images\'+section+'\calendar_single.png';
	   imgPrev.hint:='上一年';
       imgNext.hint:='下一年';
	   SetSetting('mode',4);	   
	 end;
  3: 
     begin
       imgbg.FileName:=WidgetPath+'images\'+section+'\calendar_single.png';
	   lblYearMonth.FontStyle.Font.Color:=$000000;
	   imgPrev.hint:='上一天';
       imgNext.hint:='下一天';
	   lblLunar.top:=lblYearMonth.top+82;
	   lblLunar.caption:=DateToDateCnStr(cal.date,'yyyyy(a)年'#13#10'mmmm月ddd t');
	   SetSetting('mode',4);  
	 end;  
  4: 
     begin
       imgbg.FileName:=WidgetPath+'images\'+section+'\calendar_single_orange.png';
       lblYearMonth.FontStyle.Font.Color:=$FFFFFF;
	   imgPrev.hint:='上一天';
       imgNext.hint:='下一天';
	   lblLunar.top:=lblYearMonth.top+97;
	   lblLunar.caption:=DateToDateCnStr(cal.date,'yyyyy(a)年mmmm月ddd');
	   SetSetting('mode',4);	   	   
     end;
  5: 
     begin
       imgbg.FileName:=WidgetPath+'images\'+section+'\calendar_double_orange.png';
       lblYearMonth.FontStyle.Font.Color:=$FFFFFF;
	   imgPrev.hint:='上一月';
       imgNext.hint:='下一月';
	   lblLunar.top:=lblYearMonth.top+97;
	   lblLunar.caption:=DateToDateCnStr(cal.date,'yyyyy(a)年mmmm月ddd');
	   SetSetting('mode',5);	   	   	   	   
     end;
  6: 
     begin
       imgbg.FileName:=WidgetPath+'images\calendar_double.png';
       lblYearMonth.FontStyle.Font.Color:=$000000;	   
	   imgPrev.hint:='上一月';
       imgNext.hint:='下一月';
	   lblLunar.top:=lblYearMonth.top+82;
	   lblLunar.caption:=DateToDateCnStr(cal.date,'yyyyy(a)年'#13#10'mmmm月ddd t');
	   SetSetting('mode',5);  	   
     end;
  7: 
     begin
       imgbg.FileName:=WidgetPath+'images\'+section+'\calendar_double_orange.png';
       lblYearMonth.FontStyle.Font.Color:=$FFFFFF;
	   imgPrev.hint:='上一年';
       imgNext.hint:='下一年';
	   lblLunar.top:=lblYearMonth.top+97;
	   lblLunar.caption:=DateToDateCnStr(cal.date,'yyyyy(a)年mmmm月ddd');
	   SetSetting('mode',5);	   
     end;
  8: 
     begin
       imgbg.FileName:=WidgetPath+'images\calendar_double.png';
       lblYearMonth.FontStyle.Font.Color:=$000000;	   
	   imgPrev.hint:='上一年';
       imgNext.hint:='下一年';
	   lblLunar.top:=lblYearMonth.top+82;
	   lblLunar.caption:=DateToDateCnStr(cal.date,'yyyyy(a)年'#13#10'mmmm月ddd t');
	   SetSetting('mode',5);  	   	   
     end;
  end;
  lblDay.FontStyle.Font.Color:=lblYearMonth.FontStyle.Font.Color;
  lblWeek.FontStyle.Font.Color:=lblYearMonth.FontStyle.Font.Color;
  lblSolarTerm.FontStyle.Font.Color:=lblYearMonth.FontStyle.Font.Color;
  lblLunar.FontStyle.Font.Color:=lblYearMonth.FontStyle.Font.Color;
  DecodeDate(Cal.Date,y,m,d);
  lblYearMonth.Caption:=FormatDateTime('yyyy mmm',cal.date);
  lblWeek.caption:=FormatDateTime('dddd',cal.date);
  lblSolarTerm.caption:=DateToDateCnStr(cal.date,'t'); 
  lblYear.caption:=y; 
  lblDay.caption:=inttostr(d);
  lblDay.Hint:=FormatDateTime('阳历：yyyy年m月d日 dddd'#13#10,cal.date)+
    DateToDateCnStr(cal.date,'农历：yyyyy(a)年 mmmm月ddd t'#13#10'天干地支：yyyyy(a)年 mmmmm月 ddddd日 t');
  sy:=''; sj:=''; sc:='';
  GetHuangLiStr(sy,sj,sc,cal.date);
  lblDay.Hint:=lblDay.Hint+#13#10'--- 老黄历 ---'#13#10'宜：'+sy+#13#10'忌：'+sj;
  if sc<>'' then lblDay.Hint:=lblDay.Hint+#13#10'冲：'+sc;
  EndUpdate;
end;

procedure IncDateMonth(value:integer);//用于控制上下按钮的功能
var
  y,m,d:word;
begin
  BeginUpdate;
  if (mode=2) or (mode=7) or (mode=8) then
	begin
	  DecodeDate(Cal.Date,y,m,d);
      Cal.date:=EncodeDate(y+value,m,d);
	  lblYear.caption:=y+value;
	end
  else
  begin
    if (mode=3) or (mode=4) then
    begin
      cal.date:=cal.date+value;
    end
    else
    begin
      cal.date:=incMonth(cal.date,value);
    end;
  end;
  if mode=5 then mode:=6;
  if mode=7 then mode:=8;
  if mode=4 then mode:=3;
  ModeChange;
  EndUpdate;
end;

procedure Prev;//上一个
begin
  IncDateMonth(-1);
end;

procedure Next;//下一个
begin
  IncDateMonth(1);
end;

procedure GotoToday;//回到今天
var
  y,m,d:word;
begin
  cal.date:=now;
  DecodeDate(cal.date,y,m,d);
  lblYear.caption:=y;
  if not Docking then mode:=5;
    else mode:=4;
  ModeChange;
end;


procedure AppointMonth(value:integer);//将日期跳转至指定月份
var
  y,m,d:word;
begin
  DecodeDate(Cal.Date,y,m,d);
  Cal.date:=EncodeDate(y,value,d);
  if mode=7 then mode:=6;
  if mode=8 then mode:=6;
  if mode=2 then mode:=1;
  ModeChange; 
end;

procedure MonthClick(sender:TObject);
begin
  AppointMonth(TControl(Sender).tag);
end;

procedure CalDateChange;//日期变更
begin
  if mode=5 then mode:=6;
  if mode=1 then mode:=3;
  ModeChange;
end;

procedure SelectMonth;//切换至选择月份界面
begin
  if mode=5 then mode:=7;
  if mode=6 then mode:=8;
  if mode=1 then mode:=2;
  ModeChange;  
end;

procedure ShowSingleMonth(sender);//侧边栏时切换至月历显示
begin
  mode:=1;
  ModeChange;
end;

//读取当前Widget的INI设置
procedure InitIni;
begin
  bgIndex:=GetSettingInt('bgIndex',1);
  case bgIndex of 
    1: section:='blue';  
    2: section:='grey';  
    3: section:='orange';  
  end;
  imgbg.FileName:=WidgetPath+'images\'+section+'\calendar_double_orange.png';
  imgGotoToday.FileName:=WidgetPath+'images\'+section+'\btn-page.png';
  mode:=GetSettingInt('mode',5);
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  BeginUpdate;
  Widget.Width:=136;
  Widget.Height:=141;
  cal.top:=20;
  lblYearMonth.OnClick:='ShowSingleMonth';
  lblYearMonth.top:=20;
  lblDay.top:=lblYearMonth.top+7;
  lblWeek.top:=lblYearMonth.top+82;
  imgGotoToday.top:=lblYearMonth.top+89;
  imgPrev.top:=20;
  imgNext.top:=imgPrev.top;
  lblYear.top:=20;
  imgJan.top:=38;
  lblJan.top:=48;
  imgFeb.top:=38;
  lblFeb.top:=48;
  imgMar.top:=38;
  lblMar.top:=48;
  imgApr.top:=38;
  lblApr.top:=48;
  imgMay.top:=66;
  lblMay.top:=76;
  imgJun.top:=66;
  lblJun.top:=76;
  imgJul.top:=66;
  lblJul.top:=76;
  imgAug.top:=66;
  lblAug.top:=76;
  imgSep.top:=94;
  lblSep.top:=104;
  imgOct.top:=94;
  lblOct.top:=104;
  imgNov.top:=94;
  lblNov.top:=104;
  imgDec.top:=94;
  lblDec.top:=104;
  mode:=4;
  GotoToday;
  ModeChange;
  EndUpdate;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  BeginUpdate;
  Widget.Width:=145;
  Widget.Height:=264;
  cal.top:=8;
  lblYearMonth.top:=143;
  lblYearMonth.OnClick:='';
  lblDay.top:=lblYearMonth.top+7;
  lblWeek.top:=lblYearMonth.top+82;
  imgGotoToday.top:=lblYearMonth.top+89;
  imgPrev.top:=5;
  imgNext.top:=imgPrev.top;
  imgJan.top:=33;
  lblJan.top:=43;
  imgFeb.top:=33;
  lblFeb.top:=43;
  imgMar.top:=33;
  lblMar.top:=43;
  imgApr.top:=33;
  lblApr.top:=43;
  imgMay.top:=61;
  lblMay.top:=71;
  imgJun.top:=61;
  lblJun.top:=71;
  imgJul.top:=61;
  lblJul.top:=71;
  imgAug.top:=61;
  lblAug.top:=71;
  imgSep.top:=89;
  lblSep.top:=99;
  imgOct.top:=89;
  lblOct.top:=99;
  imgNov.top:=89;
  lblNov.top:=99;
  imgDec.top:=89;
  lblDec.top:=99;
  lblYear.top:=5;
  GotoToday;
  mode:=5;
  ModeChange;  
  EndUpdate;
end;

procedure SelectDate(Sender: TObject);//弹出日期选择对话框
begin
  frm:=TForm.create(nil);
  frm.width:=230;
  frm.height:=130;
  frm.Position:=poScreenCenter;
  frm.font.name:='宋体';
  frm.font.size:=9;
  frm.BorderStyle:=bsDialog;
  try
    lblNo:=TLabel.create(frm);
    lblNo.parent:=frm;
    lblNo.Caption:='请输入或选择日期：';
    lblNo.left:=16; lblNo.top:=16;
    
    dtp:=TDateTimePicker.Create(frm);
    dtp.parent:=frm;
    dtp.left:=16; dtp.top:=32;

    btnOK:=TButton.create(frm);
    btnOK.caption:='确  定';
    btnOK.left:=50;
    btnOK.top:=67;
    btnOK.width:=70;
    btnOK.parent:=frm;
    btnOK.ModalResult:=1;
    //btnOk.OnClick:='btnOkClick';
    btnOk.default:=true;
    
    btnCancel:=TButton.create(frm);
    btnCancel.caption:='取  消';
    btnCancel.left:=130;
    btnCancel.top:=67;
    btnCancel.width:=70;
    btnCancel.parent:=frm;
    btnCancel.ModalResult:=2;
    btnCancel.cancel:=true;
    
    if frm.ShowModal=1 then
    begin
      //showmessage('您选择的日期是：'+datetostr(dtp.date));
    	Cal.date:=dtp.date;
	    if dtp.date<>now then
    	begin
	      if Docking then mode:=3
	        else mode:=6;
	    end
    	else
	    begin
	      if Docking then mode:=4
	        else mode:=5;
      end;	
	    ModeChange;
    end;
  finally
    frm.free;
  end;
end;


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

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
end;

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