{-----------------------------------------------------------------
更新历史：
[2007-04-08]: V1.0.0.1
  修改了关闭报时功能后，每分钟会报时一次的问题
-----------------------------------------------------------------}
var
  tmr:TTimer;
  TimeZoneIndex:integer;
  TimeZoneTime:TDateTime;
  miPlayVoiceTime,miInterval:TMenuItem;
  isFlicker,is24Hour,
  isFullHourPlay,isHalfHourPlay,
  isCustomColor:boolean;
  

procedure PlayVoiceTime;
begin
  VoiceTime(AppPath+'Res\Sounds\VoiceTime\',is24Hour,now+TimeZoneTime);
end;

procedure DrawClock(ForceRefresh);
var
  h,m,s,ms:word;
begin
  DecodeTime(now+TimeZoneTime,h,m,s,ms);
  imgbg.hint:=formatdatetime('ddddd'+#13#10+'hh:mm:ss'+#13#10+'dddd',now+TimeZoneTime);//format('%2d:%2d:%2d',[h,m,s]);
  imghighlights.hint:=imgbg.hint;
  //if imgs.visible or (s=0) or ForceRefresh then
  begin
	  xspfrm.BeginUpdate;
	  if is24Hour or (h<=12) then
	  begin
  		imgh0.FileName:=WidgetPath+'images\'+inttostr(h div 10)+'.png';
  		imgh1.FileName:=WidgetPath+'images\'+inttostr(h mod 10)+'.png';
  	end
  	else
  	begin
  		imgh0.FileName:=WidgetPath+'images\'+inttostr((h-12) div 10)+'.png';
  		imgh1.FileName:=WidgetPath+'images\'+inttostr((h-12) mod 10)+'.png';
  	end;
  	imgm0.FileName:=WidgetPath+'images\'+inttostr(m div 10)+'.png';
  	imgm1.FileName:=WidgetPath+'images\'+inttostr(m mod 10)+'.png';
  	if h<12 then
  		imgampm.FileName:=WidgetPath+'images\am.png'
  	else
  		imgampm.FileName:=WidgetPath+'images\pm.png';
  	imgWeek.FileName:=WidgetPath+'images\week\'+IntToStr(DayOfTheWeek(now+TimeZoneTime) mod 7)+'.png';
  	if isFlicker then
  	  imgColon.visible:=not imgColon.visible;
 	  xspfrm.EndUpdate;
  end;
  if (s=0) and ((isFullHourPlay and (m=0)) or (isHalfHourPlay and (m=30))) then
    PlayVoiceTime;
end;

procedure miPlayVoiceTimeClick(sender);
begin
  PlayVoiceTime;
end;

procedure OntmrTimer(sender)
begin
  DrawClock(false);
end;
procedure InitMenu;
begin
  miPlayVoiceTime:=TMenuItem.Create(Widget);
  miPlayVoiceTime.Caption:='语音报时';
  miPlayVoiceTime.onclick:='miPlayVoiceTimeClick';
  pm.Items.Insert(0,miPlayVoiceTime);

  miInterval:=TMenuItem.Create(Widget);
  miInterval.Caption:='-';
  pm.Items.Insert(1,miInterval);
end;

//读取当前Widget的INI设置
procedure InitIni;
var
  s:string;
  i,j:integer;
begin
 	TimeZoneTime:=0
 	lblCaption.visible:=GetSettingInt('ShowCaption',0)=1;
 	lblCaption.Caption:=GetSetting('Caption','中  国');
 	if not lblCaption.visible then
 	  Widget.Height:=imgbg.Height;
 	TimeZoneIndex:=GetSettingInt('TimeZoneIndex',0);
 	isFlicker:=GetSettingInt('isFlicker',1)=1;
 	is24Hour:=GetSettingInt('is24Hour',1)=1;
 	isFullHourPlay:=GetSettingInt('isFullHourPlay',1)=1;
 	isHalfHourPlay:=GetSettingInt('isHalfHourPlay',1)=1;
  s:=GetSetting('TimeZone','');
  if s<>'' then
  try
    i:=strtoint(copy(s,1,3));
		TimeZoneTime:=i/24+GetTimeZone;
    j:=strtoint(copy(s,5,2));
		TimeZoneTime:=((TimeZoneTime*1440)+j)/1440;
  except
  end;
  isCustomColor:=GetSettingInt('CustomColor',0)=1;
  if isCustomColor then
  begin
	  imgbg.ImageStyle.RotateColor:=StrToColor(GetSetting('Color','$C08080'),$C08080);
    imgbg.ImageStyle.ColorMatrixStyle:=xcmColorRotate;
    imgh0.ImageStyle.ColorMatrixStyle:=xcmColorRotate;
    imgh0.ImageStyle.RotateColor:=imgbg.ImageStyle.RotateColor;
    imgh1.ImageStyle.ColorMatrixStyle:=xcmColorRotate;
    imgh1.ImageStyle.RotateColor:=imgbg.ImageStyle.RotateColor;
    imgm0.ImageStyle.ColorMatrixStyle:=xcmColorRotate;
    imgm0.ImageStyle.RotateColor:=imgbg.ImageStyle.RotateColor;
    imgm1.ImageStyle.ColorMatrixStyle:=xcmColorRotate;
    imgm1.ImageStyle.RotateColor:=imgbg.ImageStyle.RotateColor;
    imgColon.ImageStyle.ColorMatrixStyle:=xcmColorRotate;
    imgColon.ImageStyle.RotateColor:=imgbg.ImageStyle.RotateColor;
    imgWeek.ImageStyle.ColorMatrixStyle:=xcmColorRotate;
    imgWeek.ImageStyle.RotateColor:=imgbg.ImageStyle.RotateColor;
    imgampm.ImageStyle.ColorMatrixStyle:=xcmColorRotate;
    imgampm.ImageStyle.RotateColor:=imgbg.ImageStyle.RotateColor;
  end;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  xspfrm.beginupdate;
  InitIni;
  InitMenu;
  AutoAdjustMem:=0;
  DrawClock(true);
  if Docking then 
  	Widget.Width:=136;
  xspfrm.endupdate;
  tmr:=TTimer.Create(Widget);
  tmr.OnTimer:='OntmrTimer';
end;

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

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  xspfrm.beginupdate;
  Widget.Width:=136;
  xspfrm.endupdateall;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  xspfrm.beginupdate;
  Widget.Width:=147;
  xspfrm.endupdate;
end;
