var
  ClockList, HandList, HighlightList:TStrings;
  ClockPath, HandPath, HighlightPath,
  ClockName, HandName, HighlightName:string;

procedure chkUseSystemTimeOnClick;
begin
  lblTimeZone.Enabled:=chkUseSystemTime.checked;
  cbbTimeZone.Enabled:=chkUseSystemTime.checked;
end;

procedure lblClockPropertiesOnClick;
begin
  ShellExecute(0,'open','RunDLL32.exe','Shell32.dll,Control_RunDLL TimeDate.cpl,,0','',1);
end;


procedure InitTimeZone;
var
  fn:string;
  inifn:TIniFile;
begin
  fn:=WidgetPath+'TimeZone.dat';
  if FileExists(fn) then
  begin
  	inifn:=TIniFile.Create(fn);
	  try
	    inifn.ReadSection('TimeZone',cbbTimeZone.Items);
    finally
  	  inifn.free;
  	end;
  end;
end;

//重画预览图
procedure RefreshClock; //(aClockName,aHandName,aHighlightName:string; aColor:TColor);
begin
  BeginUpdate;
  imgbg.FileName:=ClockPath+ClockName+'.png';
  imgh.FileName:=HandPath+HandName+'_h.png';
  imgm.FileName:=HandPath+HandName+'_m.png';
  imgs.FileName:=HandPath+HandName+'_s.png';
  imgdot.FileName:=HandPath+HandName+'_dot.png';
  if FileExists(HighlightPath+HighlightName+'.png') then
    imghighlights.FileName:=HighlightPath+HighlightName+'.png'
  else
    imghighlights.FileName:='';
  
  lblText.fontstyle.font.color:=shpFontColor.Color;
  
  EndUpdate;
end;

//初始化时钟默认配置
procedure InitClockDefault(aClock:string);
var
  i:integer;
  inifn:TIniFile;
begin
  i:=cbbClockList.items.indexOf(aClock);
  if (i<0) or (aClock=ClockName) then exit;
  lblClockIndex.caption:=format('%d/%d',[i+1,cbbClockList.items.count]);
  ClockName:=aClock;
  if FileExists(HandPath+Clockname+'_h.png') then
    HandName:=ClockName;
  if FileExists(HighlightPath+Clockname+'.png') then
    HighlightName:=ClockName
  else
    HighlightName:='';
  
  if FileExists(ClockPath+ClockName+'.ini') then
  begin
    inifn:=TIniFile.Create(ClockPath+ClockName+'.ini');
    try
      HandName:=inifn.ReadString('setting','hand',HandName);
      HighlightName:=inifn.ReadString('setting','highlights',HighlightName);
      shpFontColor.Color:=StrToColor(inifn.ReadString('setting','TextColor',''),$000000);
      shpFontColor.ColorTo:=shpFontColor.Color;
      lblText.left:=inifn.ReadInteger('setting','Text_X',0)+40;
      lblText.top :=inifn.ReadInteger('setting','Text_Y',78)+80;
      lblText.width:=inifn.ReadInteger('setting','Text_Width',130);
      lblText.height:=inifn.ReadInteger('setting','Text_Height',16);
      
    finally
      inifn.free;
    end;
  end
  else
  begin
    lblText.left:=40;
    lblText.top :=158;
    lblText.width:=130;
    lblText.height:=16;
  end;

  cbbHandList.text:=HandName;
  cbbHighlightList.text:=HighlightName;
  cbbClockList.text:=ClockName;
  edtRectX.text:=IntToStr(lblText.left-40);
  edtRectY.text:=IntToStr(lblText.top-80);
  edtRectW.text:=IntToStr(lblText.width);
  edtRectH.text:=IntToStr(lblText.height);
  
  RefreshClock;
end;

procedure SaveClockDefault;
var
  i:integer;
  inifn:TIniFile;
begin
   inifn:=TIniFile.Create(ClockPath+ClockName+'.ini');
   try
     inifn.WriteString('setting','hand',HandName);
     inifn.WriteString('setting','highlights',HighlightName);
     inifn.WriteString('setting','TextColor',ColorToHtmlStr(shpFontColor.Color));
     inifn.WriteInteger('setting','Text_X',lblText.left-40);
     inifn.WriteInteger('setting','Text_Y',lblText.top-80);
     inifn.WriteInteger('setting','Text_Width',lblText.width);
     inifn.WriteInteger('setting','Text_Height',lblText.height);
   finally
     inifn.free;
   end;
end;

procedure InitIni;
var
  s:string;
begin
  edtCaption.text:=GetSetting('ClockText','北京');
  cbbTimeZone.ItemIndex:=GetSettingInt('TimeZoneIndex',39);
  chkShowSec.checked:=GetSettingInt('ShowSec',1)=1;
  chkSecSpringStyle.checked:=GetSettingInt('SecSpringStyle',1)=1;
  chkisFullHourPlay.checked:=GetSettingInt('isFullHourPlay',1)=1;
  chkisHalfHourPlay.checked:=GetSettingInt('isHalfHourPlay',1)=1;
  chkUseSystemTime.checked:=GetSettingInt('UseSystemTime',0)=1;
  
  ClockList:=TStringList.Create;
  HandList:=TStringList.Create;
  HighlightList:=TStringList.Create;
  ClockPath:=WidgetPath+'images\bg\';
  HandPath:=WidgetPath+'images\hand\';
  HighlightPath:=WidgetPath+'images\highlights\';
  HandName:=GetSetting('HandName','trad');
  HighlightName:=GetSetting('HighlightsName','');
  shpFontColor.Color:=GetSettingInt('ClockColor',$000000);
  shpFontColor.ColorTo:=shpFontColor.Color;
  ClockList.text:=GetFileList(ClockPath,'*.png',false,false,false);
  HandList.text:=GetFileList(HandPath,'*.png',false,false,false);
  HighlightList.text:=GetFileList(HighlightPath,'*.png',false,false,false);

  if ClockList.count>0 then
    for i:=0 to ClockList.count-1 do
      ClockList.strings[i]:=copy(ClockList.strings[i],1,length(ClockList.strings[i])-4);
  cbbClockList.items.text:=ClockList.text;
  cbbClockList.ItemIndex:=cbbClockList.Items.IndexOf(HandName);
  
  if HandList.count>0 then
    for i:=HandList.count-1 downto 0 do
    begin
      j:=pos('_h.png',HandList.strings[i]);
      if j>0 then
        HandList.strings[i]:=copy(HandList.strings[i],1,j-1)
      else
        HandList.Delete(i);
    end;
  cbbHandList.items.text:=HandList.text;

  if HighlightList.count>0 then
    for i:=0 to HighlightList.count-1 do
      HighlightList.strings[i]:=copy(HighlightList.strings[i],1,length(HighlightList.strings[i])-4);
  cbbHighlightList.items.text:='（无）'#13#10+HighlightList.text;

  s:=GetSetting('ClockName','trad');
  InitClockDefault(s);
  if ClockName<>'trad' then
    RefreshClock;
  cbbHandList.text:=HandName;
  cbbHighlightList.text:=HighlightName;
  cbbClockList.text:=ClockName;
  lblClockIndex.caption:=format('%d/%d',[ClockList.IndexOf(ClockName)+1,ClockList.count]);
end;

procedure SaveIni;
begin
  SetSetting('ClockText',edtCaption.text);
  SetSettingInt('TimeZoneIndex',cbbTimeZone.ItemIndex);
  SetSetting('TimeZone',copy(cbbTimeZone.Text,6,6));
  if chkShowSec.checked then SetSetting('ShowSec',1)
    else SetSetting('ShowSec',0);
  if chkSecSpringStyle.checked then SetSetting('SecSpringStyle',1)
    else SetSetting('SecSpringStyle',0);
  if chkisFullHourPlay.checked then SetSetting('isFullHourPlay',1)
    else SetSetting('isFullHourPlay',0);
  if chkisHalfHourPlay.checked then SetSetting('isHalfHourPlay',1)
    else SetSetting('isHalfHourPlay',0);
  if chkUseSystemTime.checked then SetSetting('UseSystemTime',1)
    else SetSetting('UseSystemTime',0);

  SetSetting('ClockName',ClockName);
  SetSetting('HandName',HandName);
  SetSetting('HighlightsName',HighlightName);
  SetSetting('ClockColor',ColorToHtmlStr(shpFontColor.Color));
  SaveClockDefault;
end;

procedure OnSettingInit;
begin
  BeginUpdate;
  InitTimeZone;
  InitIni;
  shpFontColor.OnClick:='OnshpColorClick';
  EndUpdate;
  cbbTimeZone.OnSelect:='OnSelected';
  cbbClockList.OnSelect:='OnClockSelected';
  cbbHandList.OnSelect:='OnHandSelected';
  cbbHighlightList.OnSelect:='OnHighlightSelected';
  edtRectX.OnChange:='edtRectOnChange';
  edtRectY.OnChange:='edtRectOnChange';
  edtRectW.OnChange:='edtRectOnChange';
  edtRectH.OnChange:='edtRectOnChange';
end;

//--- 按下设置窗口的确定按钮时，触发此事件 ---
//通过设置返回值来改变是否允许确认,当result为False时不关闭设置窗口
function OnSettingClose(iResult);
begin
  HighlightList.free;
	ClockList.free;
	HandList.free;
  result:=true;
  SaveIni;
end;

procedure OnSelected(sender);
var
  fn:string;
  inifn:TIniFile;
begin
  fn:=WidgetPath+'TimeZone.dat';
  if FileExists(fn) then
  begin
  	inifn:=TIniFile.Create(fn);
	  try
	    edtCaption.text:=inifn.ReadString('TimeZone',cbbTimeZone.Text,'');
    finally
  	  inifn.free;
  	end;
  end;
end;

procedure OnClockSelected(Sender:TObject);
begin
  InitClockDefault(cbbClockList.text);
end;

procedure OnHandSelected(Sender:TObject);
begin
  BeginUpdate;
  HandName:=cbbHandList.text;
  imgh.FileName:=HandPath+HandName+'_h.png';
  imgm.FileName:=HandPath+HandName+'_m.png';
  imgs.FileName:=HandPath+HandName+'_s.png';
  imgdot.FileName:=HandPath+HandName+'_dot.png';
  EndUpdate;
end;

procedure OnHighlightSelected(Sender:TObject);
begin
  HighlightName:=cbbHighlightList.text;
  if FileExists(HighlightPath+HighlightName+'.png') then
    imghighlights.FileName:=HighlightPath+HighlightName+'.png'
  else
    imghighlights.FileName:='';
end;

procedure edtRectOnChange(Sender:TObject);
begin
  BeginUpdate;
  try
    lblText.left:=StrToInt(edtRectX.text)+40;
  except end;
  try
    lblText.top:=StrToInt(edtRectY.text)+80;
  except end;
  try
    lblText.width:=StrToInt(edtRectW.text);
  except end;
  try
    lblText.height:=StrToInt(edtRectH.text);
  except end;
  imghighlights.NeedRefresh:=true;
  EndUpdate;
end;

procedure OnshpColorClick(Sender:TObject);
var
  dlgColor:TColorDialog;
begin
  dlgColor:=TColorDialog.create(WidgetSetting);
  try
    dlgColor.Color:=shpFontColor.Color;
    if dlgColor.Execute then
    begin
      BeginUpdate;
      shpFontColor.Color:=dlgColor.Color;
  		shpFontColor.ColorTo:=dlgColor.Color;
  		EndUpdate;
    end;
  finally
    dlgColor.free;
  end;
end;

procedure ChangeClock(value:integer);
begin
  BeginUpdate;
  if value>0 then
  begin
    if cbbClockList.ItemIndex+Value<cbbClockList.items.count then
      cbbClockList.ItemIndex:=cbbClockList.ItemIndex+Value
    else
      cbbClockList.ItemIndex:=cbbClockList.ItemIndex+Value-cbbClockList.items.count;
  end
  else
  begin
    if cbbClockList.ItemIndex+Value>=0 then
      cbbClockList.ItemIndex:=cbbClockList.ItemIndex+Value
    else
      cbbClockList.ItemIndex:=cbbClockList.ItemIndex+Value+cbbClockList.items.count;
  end;
  cbbClockList.Text:=cbbClockList.Items.strings[cbbClockList.ItemIndex];
  OnClockSelected(cbbClockList);
  EndUpdate;
end;