{**************************
Dashboard Clock OnSetting script Ver1.0 by snowfox
**************************}
var
  i: Integer;
  fbgBitmapName:string;
  
  ts1,ts2:TTabSheet;
  //Tab1
  chkShowSecond:TCheckBox;
  lblHandleType:TLabel;
  cbbHandleType:TComboBox;

  lblImage:TLabel;
  pngImage:TPanel;
  img:TImage;
  //Center and Radius
  grpCenter:TGroupBox;
  lblCenterX,lblCenterY:TLabel;
  seCenterX,seCenterY:TSpinEdit;
  lblRadius:TLabel;
  seRadius:TSpinEdit;
  //Color
  grpColor:TGroupBox;
  lblColorHour,lblColorMin,lblColorSec:TLabel;
  shpHour,shpMin,shpSec:TShape;

  //Tab2
  lblTextRect,lblText,lblTimeZone:TLabel;
  edtTextRect,edtText:TEdit;
  cbbTimeZone:TComboBox;
  

procedure cbbTimeZoneChange(Sender: TObject);
var
	s:string;
	inifn:TIniFile;
	lst:TStrings;
	i:integer;
begin
  if not FileExists(SkinPath+'TimeZone.dat') then exit;
  inifn:=TIniFile.create(SkinPath+'TimeZone.dat');
  with inifn do
  try
    s:=ReadString('TimeZone',cbbTimeZone.Text,'');
    if s<>'' then edtText.text:=s+'(<#Month>月<#Day>日<#Hour>点)';
  finally
    inifn.free;
  end;
end;

//Init Skin form setting
procedure InitSetting;
var
	s:string;
	inifn:TIniFile;
	lst:TStrings;
	i:integer;
begin
  inifn:=TIniFile.create(SkinFileName);
	lst:=TStringList.create;
  with inifn do
  try
    //Tab1 时钟设置
    i:=ReadInteger('Item0','Interval',60);
    chkShowSecond.checked:=(i=1);
    cbbHandleType.Items.text:=GetFileList(g_ResPath+'ClockHand\','*.ini');
    cbbHandleType.Items.Insert(0,'<菱形指针>');
    cbbHandleType.Items.Insert(0,'<线形指针>');
    
    i:=ReadInteger('Item0','HandStyle',0);
    if i=2 then//菱形指针
      cbbHandleType.itemindex:=1
    else if i=1 then//图片指针
    begin
      s:=ReadString('Item0','HandSkinFile','');
      if s<>'' then
      begin
        i:=cbbHandleType.Items.IndexOf(s);
      	if i>=0 then
      	  cbbHandleType.itemindex:=i
      	else
        	cbbHandleType.text:=s;
      end
      else
      	cbbHandleType.itemindex:=0;
    end
    else//默认线形指针
      cbbHandleType.itemindex:=0;
    cbbHandleTypeChange(cbbHandleType);

    fbgBitmapName:=ReadString('system','BackgroundBitmapName','Dashboard.png');
		LoadPngImage(img,fbgBitmapName,i,i);
    //Center and Radius
    seCenterX.value:=ReadInteger('Item0','CenterX',0);
    seCenterY.value:=ReadInteger('Item0','CenterY',0);
    seRadius.value:=ReadInteger('Item0','Radius',0);
    //Color
    shpHour.brush.color:=StrToColor(ReadString('Item0','HourColor','#000000'));
    shpMin.brush.color:=StrToColor(ReadString('Item0','MinuteColor','#000000'));
    shpSec.brush.color:=StrToColor(ReadString('Item0','SecondColor','#000000'));


    //Tab2 区域设置
    edtTextRect.text:=ReadString('Item0','TextRect','0/140/172/20/1/1');
    edtText.text:=ReadString('Item0','Text','北京(<#Month>月<#Day>日<#Hour>点)');
    if FileExists(SkinPath+'TimeZone.dat') then
    begin
    	lst.loadfromfile(SkinPath+'TimeZone.dat');
    	if lst.count>1 then
    	  for i:=1 to lst.count-1 do
    	    cbbTimeZone.Items.add(lst.names[i]);
    	cbbTimeZone.ItemIndex:=ReadInteger('Item0','TimeZoneIndex',39);
      cbbTimeZone.OnChange:=@cbbTimeZoneChange;
    end;
  finally
    inifn.free;
    lst.free;
  end;
end;

//save Skin form setting
function SaveSetting:boolean;
var
	s:string;
	inifn:TIniFile;
	i:integer;
begin
  result:=true;
  inifn:=TIniFile.create(SkinFileName);
  with inifn do
  try
    //Tab1 时钟设置
    if chkShowSecond.checked then
      WriteInteger('Item0','Interval',1)
    else
      WriteInteger('Item0','Interval',60);
    case cbbHandleType.itemindex of
      0://Default Line
	      WriteInteger('Item0','HandStyle',0);
      1://菱形指针
	      WriteInteger('Item0','HandStyle',2);
	  else
	    begin
	      WriteInteger('Item0','HandStyle',1);
    	  WriteString('Item0','HandSkinFile',cbbHandleType.Text);
      end;
    end;
    WriteString('system','BackgroundBitmapName',fbgBitmapName);
    //Center and Radius
    WriteInteger('Item0','CenterX',seCenterX.value);
    WriteInteger('Item0','CenterY',seCenterY.value);
    WriteInteger('Item0','Radius',seRadius.value);
    //Color
    WriteString('Item0','HourColor',ColorToStr(shpHour.brush.color));
    WriteString('Item0','MinuteColor',ColorToStr(shpMin.brush.color));
    WriteString('Item0','SecondColor',ColorToStr(shpSec.brush.color));
    //Tab2 区域设置
    WriteString('Item0','TextRect',edtTextRect.text);
    WriteString('Item0','Text',edtText.text);
  	WriteInteger('Item0','TimeZoneIndex',cbbTimeZone.ItemIndex);
  	WriteString('Item0','TimeZone',copy(cbbTimeZone.text,6,6));
  finally
    inifn.free;
  end;
end;

//---时钟设置---
procedure CreateTab1;
begin
  ts1:=TTabSheet.Create(pgc);
  ts1.PageControl:=pgc;
  ts1.Caption:='时钟设置';
  chkShowSecond:=TCheckBox.create(self);
  with chkShowSecond do
  begin
    parent:=ts1;
    left:=16; top:=14; width:=81;
    caption:='显示秒针';
  end;
  
  //image preview
  lblImage:=TLabel.create(self);
  with lblImage do
  begin
    parent:=ts1;
    left:=166; top:=16; width:=60;
    caption:='背景预览：';
  end;
  pngImage:=TPanel.create(self);
  with pngImage do
  begin
    parent:=ts1;
    left:=166; top:=32;
    width:=200; height:=200;
    caption:='';
    BevelOuter:=bvLowered;
  end;
  img:=TImage.create(self);
  with img do
  begin
    cursor:=crHandPoint;
    parent:=pngImage;
    left:=1; top:=1;
    width:=198; height:=198;
    OnClick:=@imgClick;
  end;
  //Center and Radius
  grpCenter:=TGroupBox.create(self);
  with grpCenter do
  begin
    parent:=ts1;
    left:=16; top:=40; width:=137; height:=73;
    caption:='时钟中心点：';
  end;
  lblCenterX:=TLabel.create(self);
  with lblCenterX do
  begin
    parent:=grpCenter;
    left:=8; top:=20; width:=36;
    caption:='水平：';
  end;
  seCenterX:=TSpinEdit.create(self);
  with seCenterX do
  begin
    parent:=grpCenter;
    left:=48; top:=16; width:=73;
  end;
  lblCenterY:=TLabel.create(self);
  with lblCenterY do
  begin
    parent:=grpCenter;
    left:=8; top:=44; width:=36;
    caption:='水平：';
  end;
  seCenterY:=TSpinEdit.create(self);
  with seCenterY do
  begin
    parent:=grpCenter;
    left:=48; top:=40; width:=73;
  end;
  lblRadius:=TLabel.create(self);
  with lblRadius do
  begin
    parent:=ts1;
    left:=16; top:=124; width:=60;
    caption:='指针半径：';
  end;
  seRadius:=TSpinEdit.create(self);
  with seRadius do
  begin
    parent:=ts1;
    left:=80; top:=120; width:=73;
  end;
  //
  lblHandleType:=TLabel.create(self);
  with lblHandleType do
  begin
    parent:=ts1;
    left:=16; top:=152; width:=60;
    caption:='指针类型：';
  end;
  cbbHandleType:=TComboBox.create(self);
  with cbbHandleType do
  begin
    parent:=ts1;
    left:=16; top:=166; width:=137;
    DropDownCount:=15;
    OnChange:=@cbbHandleTypeChange;
    //Style:=csDropDownList;
  end;
  //Color
  grpColor:=TGroupBox.create(self);
  with grpColor do
  begin
    parent:=ts1;
    left:=16; top:=231; width:=297; height:=41;
    caption:='指针颜色：';
  end;
	lblColorHour:=TLabel.create(self);
	with lblColorHour do
	begin
	  parent:=grpColor;
	  left:=16; top:=18; width:=36;
	  caption:='时针：';
	end;
	shpHour:=TShape.create(self);
	with shpHour do
	begin
	  parent:=grpColor;
	  left:=56; top:=16; width:=33; height:=17;
	  cursor:=crHandPoint;
	  OnMouseDown:=@shpMouseDown;
	end;
	lblColorMin:=TLabel.create(self);
	with lblColorMin do
	begin
	  parent:=grpColor;
	  left:=104; top:=18; width:=36;
	  caption:='分针：';
	end;
	shpMin:=TShape.create(self);
	with shpMin do
	begin
	  parent:=grpColor;
	  left:=144; top:=16; width:=33; height:=17;
	  cursor:=crHandPoint;
	  OnMouseDown:=@shpMouseDown;
	end;
	lblColorSec:=TLabel.create(self);
	with lblColorSec do
	begin
	  parent:=grpColor;
	  left:=200; top:=18; width:=36;
	  caption:='秒针：';
	end;
	shpSec:=TShape.create(self);
	with shpSec do
	begin
	  parent:=grpColor;
	  left:=240; top:=16; width:=33; height:=17;
	  cursor:=crHandPoint;
	  OnMouseDown:=@shpMouseDown;
	end;
end;

//---区域设置---
procedure CreateTab2;
begin
  ts2:=TTabSheet.Create(pgc);
  ts2.PageControl:=pgc;
  ts2.Caption:='区域设置';
  //--- solar setting ---
  lblTextRect:=TLabel.create(self);
  with lblTextRect do
  begin
    parent:=ts2;
    left:=16; top:=16; width:=60;
    caption:='文字坐标：';
  end;
  edtTextRect:=TEdit.create(self);
  with edtTextRect do
  begin
    parent:=ts2;
    left:=80; top:=11; width:=169;
  end;
  lblText:=TLabel.create(self);
  with lblText do
  begin
    parent:=ts2;
    left:=16; top:=40; width:=60;
    caption:='文字内容：';
  end;
  edtText:=TEdit.create(self);
  with edtText do
  begin
    parent:=ts2;
    left:=80; top:=36; width:=265;
  end;
  lblTimeZone:=TLabel.create(self);
  with lblTimeZone do
  begin
    parent:=ts2;
    left:=16; top:=68; width:=60;
    caption:='时区选择：';
  end;
  cbbTimeZone:=TComboBox.create(self);
  with cbbTimeZone do
  begin
    parent:=ts2;
    left:=16; top:=88; width:=329;
    DropDownCount:=15;
    Style:=csDropDownList;
  end;
end;

procedure imgClick(Sender: TObject);
var
  dlgPic:TOpenPictureDialog;
  w,h:integer;
begin
  dlgPic:=TOpenPictureDialog.create(self);
  try
    dlgPic.FileName:=fbgBitmapName;
    if dlgPic.Execute then
    begin
      fbgBitmapName:=dlgPic.FileName;
      LoadPngImage(img,fbgBitmapName,w,h);
      seCenterX.value:=w div 2;
      seCenterY.value:=h div 2;
      seRadius.value:=round(w/3);
    end;
  finally
    dlgPic.free;
  end;
  
end;

procedure cbbHandleTypeChange(Sender: TObject);
var
	isShow:boolean;
begin
  if cbbHandleType.ItemIndex in [0..1] then isShow:=true
    else isShow:=false;
  grpColor.Enabled:=isShow;
  lblColorHour.Enabled:=isShow;
  lblColorMin.Enabled:=isShow;
  lblColorSec.Enabled:=isShow;
end;

procedure shpMouseDown(Sender: TObject; Button: TMouseButton; 
	Shift: TShiftState; X, Y: Integer);
var
  dlgColor:TColorDialog;
begin
  dlgColor:=TColorDialog.create(self);
  if dlgColor.Execute then
  try
    TShape(Sender).Brush.color:=dlgColor.color;
  finally
    dlgColor.free;  
  end;
end;

begin
  CreateTab1;
  CreateTab2;
  InitSetting;
  
end.