const
  bgCount=6;

var
  bgIndex:integer;
  
procedure RefreshPreview;
begin
  Beginupdate;
  bgIndex:=bgIndex mod bgCount;
  
  imgbg.FileName:=WidgetPath+'images\bg'+inttostr(bgIndex)+'.png';
  lblIndex.caption:=inttostr(bgIndex+1)+'/'+inttostr(bgCount);
  Endupdate;
end;

//读取配置
procedure InitIni;
var
  inifn:TIniFile;
begin
  BeginUpdate;
  bgIndex:=GetSettingInt('bgIndex',2);
  lblIndex.caption:=format('%d/%d',[bgIndex+1,bgCount]);
  imgbg.FileName:=WidgetPath+'images\bg'+inttostr(bgIndex)+'.png';
  edtSpeed.text:=GetSetting('Speed','5');
  EndUpdate;
end;

//保存配置
procedure SaveIni;
begin
  SetSetting('bgIndex',bgIndex);
  try
    SetSetting('Speed',StrToInt(edtSpeed.text));
  except
  
  end;
end;

//--- Widget设置窗口初始化事件，当打开Widget设置窗口时会触发此事件 ---
procedure OnSettingInit;
begin
  Beginupdate;
  InitIni;
  Endupdate;
end;

//--- 按下设置窗口的确定按钮时，触发此事件 ---
//通过设置返回值来改变是否允许确认,当result为False时不关闭设置窗口
function OnSettingClose(iResult);
var
  i:integer;
begin
  result:=false;
  try
    i:=StrToInt(edtSpeed.text);
    if i<3 then
    begin
      showmessage('单词滚动速度不能低于3秒！');
      exit;
    end;
  except
    showmessage('无效的单词滚动速度！');
    exit;
  end;
  

  result:=true;
  if Result then
    SaveIni;
end;
