var
  tmr:TTimer;
  PicWidth,PicHeight:integer;
  PicX,PicY:integer;
  PicsPath:string;
  FileList:TStrings;
  PicIndex,PlayStyle:integer;
  bIncludeChildDir:boolean;
  bAutoChangeWallpaper:boolean;

//uses xsGraphics,xsSysUtils
procedure SetWallPaper(aFileName);
var
  fn:string;
begin
  fn:=GetWindowsDir+'\8jie Wallpaper.bmp';
  if ImageConvert(aFileName,fn) then
    SetDesktopWallpaper(fn,'stretch');
end;

procedure RefreshPic;
var
  i:integer;
  s:string;
begin
  xspfrm.BeginUpdate;
  imgbg.NeedRefresh:=true;
  if PicIndex<FileList.count then
    imgPic.FileName:=PicsPath+FileList.Strings[PicIndex];
  if PlayStyle=1 then 
  begin
  	FileList.Delete(PicIndex);
    if FileList.count=0 then
		  FileList.text:=GetFileList(PicsPath,'.jpg,.bmp,.png,.gif',bIncludeChildDir,false,false);
  end;
  if 1.336*imgPic.ActualHeight<imgPic.ActualWidth then
  begin//宽度超过比例
    imgPic.Width:=PicWidth;
    imgPic.Height:=(imgPic.ActualHeight*imgPic.Width)/imgPic.ActualWidth;//round(PicHeight*imgPic.Width/imgPic.ActualWidth);
    imgPic.Left:=PicX;
    imgPic.Top:=PicY+(PicHeight-imgPic.Height)/2;
  end
  else
  begin//高度超过比例
    imgPic.Height:=PicHeight;
    if imgPic.ActualHeight>0 then
      imgPic.Width:=(imgPic.ActualWidth*imgPic.Height)/imgPic.ActualHeight;
    //round(PicWidth*imgPic.Height/imgPic.ActualHeight);
    imgPic.Left:=PicX+(PicWidth-imgPic.Width)/2;
    imgPic.Top:=PicY;
  end;
  xspfrm.EndUpdate;
  if bAutoChangeWallpaper and (PicIndex<FileList.count) then
    SetWallPaper(PicsPath+FileList.Strings[PicIndex]);
end;

procedure reveal;
begin
  ShellExecute(0,'open',imgPic.FileName,'','',1);
end;

procedure Pause;
begin
  tmr.Enabled:=false;
  xspfrm.BeginUpdate;
  imgPause.visible:=false;
  imgPlay.visible:=not imgPause.visible;
  xspfrm.EndUpdate;
end;

procedure Play;
begin
	tmr.Enabled:=true;
  xspfrm.BeginUpdate;
  imgPause.visible:=true;
  imgPlay.visible:=not imgPause.visible;
  xspfrm.EndUpdate;
end;

//0:顺序播放; 1:随机无重复播放; 2:随机播放
function GetIndex(value:integer):boolean;
begin
  result:=false;
  if FileList.count<=0 then exit;
  case PlayStyle of
    0://顺序播放
      begin
  			PicIndex:=PicIndex+value;
  			if PicIndex<0 then PicIndex:=FileList.count-1
      end;
    1://随机无重复播放
      begin
        Randomize;
        PicIndex:=round(random * FileList.count);
      end;
    2://随机播放
      begin
        Randomize;
        PicIndex:=round(random * FileList.count);
      end;
    else
			PicIndex:=PicIndex+value;
  end;
	if PicIndex<0 then PicIndex:=FileList.count-1
  PicIndex:=PicIndex mod FileList.count;
  result:=true;
end;

procedure Next;
begin
  if GetIndex(1) then
  	RefreshPic;
end;

procedure Prev;
begin
  if GetIndex(-1) then
    RefreshPic;
end;

procedure OntmrTimer(sender)
begin
  Next;
end;

//--- Widget鼠标滚轮事件，当在Widget上进行滚轮操作时会触发此事件 ---
procedure OnWidgetMouseWheel(WheelDelta);
begin
  if WheelDelta>0 then
    Next
  else
    Prev;
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  BeginUpdate;
  Widget.Width:=136;
  Widget.Height:=100;
  imgbg.FileName:=WidgetPath+'images\glass_frame_dock.png';
  PicWidth:=121;
  PicHeight:=90;
  PicX:=4;
  PicY:=5;
  imgSidebar.Top:=74;
  imgPrev.Top:=imgSidebar.Top+3;
  imgPause.Top:=imgPrev.Top;
  imgPlay.Top:=imgPrev.Top;
  imgNext.Top:=imgPrev.Top;
  imgReveal.Top:=imgPrev.Top;
  imgSidebar.Left:=9;
  imgPrev.left:=imgSidebar.Left+8;
  imgPause.left:=imgPrev.left+22;
  imgPlay.left:=imgPause.left;
  imgNext.left:=imgPause.left+23;
  imgReveal.left:=imgNext.left+25;
  RefreshPic;
  EndUpdate;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  BeginUpdate;
  imgbg.FileName:=WidgetPath+'images\glass_frame.png';
  Widget.Width:=360;
  Widget.Height:=280;
  PicWidth:=318;
  PicHeight:=238;
  PicX:=18;
  PicY:=17;
  imgSidebar.Top:=232;
  imgPrev.Top:=imgSidebar.Top+3;
  imgPause.Top:=imgPrev.Top;
  imgPlay.Top:=imgPrev.Top;
  imgNext.Top:=imgPrev.Top;
  imgReveal.Top:=imgPrev.Top;
  imgSidebar.Left:=(Widget.Width-112) div 2;
  imgPrev.left:=imgSidebar.Left+8;
  imgPause.left:=imgPrev.left+22;
  imgPlay.left:=imgPause.left;
  imgNext.left:=imgPause.left+23;
  imgReveal.left:=imgNext.left+25;
  RefreshPic;
  EndUpdate;
  //xspfrm.Update(true);
end;

//读取配置
procedure InitSetting;
begin
  PlayStyle:=GetSettingInt('PlayStyle',0);
  PicsPath:=GetSetting('Path','');
  bIncludeChildDir:=GetSettingInt('IncludeChildDir',1)=1;
  tmr.Interval:=GetSettingInt('RefreshInterval',10)*1000;
  bAutoChangeWallpaper:=GetSettingInt('AutoChangeWallpaper',1);
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
const
  CSIDL_PERSONAL=5;
begin
  FileList:=TStringList.create;
  BeginUpdate;
  imgPic.ImageStyle.Quality:=xqHigh;
  tmr:=TTimer.Create(Widget);
  tmr.Interval:=10000;
  tmr.OnTimer:='OntmrTimer';
  tmr.Enabled:=true;

  PicIndex:=0;
  InitSetting;
  if trim(PicsPath)='' then
    ShowTrayHint('提示','请在小工具设置窗口中选择图片的路径!')
  else
  begin
    //PicsPath:=GetSpecialFolderDir(CSIDL_PERSONAL)+'\My Pictures\';
    FileList.text:=GetFileList(PicsPath,'.jpg,.bmp,.png,.gif',bIncludeChildDir,false,false);
    //FileList.savetofile('c:\list.txt');
  end;
  if Docking then //OnWidgetDock;
  begin
    OnWidgetDock;
  end
  else
  begin
    PicWidth:=318;
    PicHeight:=238;
    PicX:=18;
    PicY:=17;
  end;

  if FileList.count=0 then
  begin
    tmr.Enabled:=false;
		imgPic.FileName:=WidgetPath+'images\8jie.jpg';
  end;
  OntmrTimer(nil);
  EndUpdate;
  AutoAdjustMem:=false;
end;

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

//--- Widget事件，当打开鼠标移到Widget区域内时触发此事件 ---
procedure OnWidgetMouseEnter;
begin
  xspfrm.beginupdate;
  imgSidebar.visible:=true;
  imgPrev.visible:=imgSidebar.visible;
  if tmr.Enabled then
  	imgPause.visible:=imgSidebar.visible;
  else
  	imgPlay.visible:=imgSidebar.visible;
  imgNext.visible:=imgSidebar.visible;
  imgReveal.visible:=imgSidebar.visible;
  xspfrm.endupdate;
end;

//--- Widget事件，当打开鼠标移出Widget区域时触发此事件 ---
procedure OnWidgetMouseLeave;
begin
  xspfrm.beginupdate;
  imgSidebar.visible:=false;
  imgPrev.visible:=imgSidebar.visible;
  imgPause.visible:=imgSidebar.visible;
	imgPlay.visible:=imgSidebar.visible;
  imgNext.visible:=imgSidebar.visible;
  imgReveal.visible:=imgSidebar.visible;
  xspfrm.endupdate;
end;

