const
  //DefURL='http://8-jie.cn/backend.asp?Action=Widget';
  DefURL='http://bbs.8-jie.cn/rss.php?auth=9151Akd%2FT2nNwATPA4ZcSEcN72fBdDFjpXSxsJo%2F4IN4watclKtOIQ';
  FontColor_Def=$846759;
  HotFontStyle_Def=',,#60d0f9';
  FontColor_Trans=$FFFFFF;
  HotFontStyle_Trans=',,#AAAAAA';
  FontColor_Vista=$FFFFFF;
  HotFontStyle_Vista=',,clRed';

var
  EveryPageCount,
  RowHeight:integer;
  
  xshttp:TxsHTTP;
  rss:TxsRss;
  rssItem:TxsRssItem;
  RSSFileName,RSSListFileName:string;
  aurl:string;
  iBase,iPageIndex, iPageCount:integer;
  tmr:TTimer;
  FirstShowHint:boolean; //让提示仅在第一次打开时或者修改设置时才显示一次，否则老是提示也挺烦人的
  imgLines,lblDescs,lblTitles,lblDates;
  HotFontStyleStr:string;
  BlurFont:boolean;

  SizeX, SizeY :integer;
  ChangeSize:boolean;
  ShowTitleHint:boolean;
  SkinName:string;
  isDragRSSRow:boolean;

procedure FreeMenuItems(mi:TMenuItem);
begin
  if mi=nil then exit;
  if mi.count>0 then
    for i:=mi.count-1 downto 0 do
    begin
      FreeMenuItems(mi.Items[i]);
      mi.Delete(i);
    end;
  mi.free;
  mi:=nil;
end;

procedure FreeMenu;
begin
 // FreeMenuItems(nRSS);
end;

//显示RSS项目
procedure ShowItems(aPageIndex:integer);
var
  i:integer;
begin
  if {(iPageIndex<>aPageIndex) and} rss.Active then
  begin
    BeginUpdate; 
    if (aPageIndex>=0) and (aPageIndex<iPageCount) then
    begin
      iPageIndex:=aPageIndex;
      lblPage.caption:=format('%d / %d',[aPageIndex+1,iPageCount]);
      iBase:=aPageIndex*EveryPageCount;
    end;
    for i:=0 to EveryPageCount-1 do
    begin
      if iBase+i<rss.count then
      begin
        rssItem:=rss.GetItem(iBase+i);
        lblTitles[i].caption:=rssItem.title;
        lblDescs[i].caption:=rssItem.author;//+'  '+rssItem.link;
        lblDates[i].caption:=rssItem.pubDate;
        if ShowTitleHint then
          lblTitles[i].hint:=rssItem.title+#13#10#13#10+rssItem.description
        else
          lblTitles[i].hint:='';
      end
      else
      begin
        lblTitles[i].caption:='';     lblTitles[i].hint:='';
        lblDescs[i].caption:='';      lblDates[i].caption:='';
      end;
    end;
    EndUpdate;
  end;
end;

//读取RSS成功
procedure HttpDone(url,result,isString);
var
  lst:TStrings;
begin
  if aurl=url then
  begin
  	lst:=TStringList.create;
  	try
  		lst.text:=result;
  		lst.savetofile(RSSFileName);
      rss.LoadFromFile(RSSFileName);
      if rss.Active then
      begin
        iPageCount:=rss.Count div EveryPageCount;
        if (rss.count mod EveryPageCount)>0 then inc(iPageCount);
        iPageIndex:=0;
        ShowItems(iPageIndex);
      end
      else iPageCount:=0;
      if FirstShowHint then
      begin
        FirstShowHint:=false;
        //showtrayhint('提示','读取RSS信息成功！');
      end;
    finally
      lst.free;
    end;
  end;
  if xshttp<>nil then
  begin
  	xshttp.free;
  	xshttp:=nil;
  end;
  ScriptFinished:=true;
  lblPage.caption:=format('%d / %d',[iPageIndex+1,iPageCount]);
end;

//读取RSS失败
procedure HttpGetError(url, isString);
begin
  if (aurl=url) and FirstShowHint then
  begin
    FirstShowHint:=false;
    showtrayhint('提示','读取RSS信息失败！');
  end;
  if xshttp<>nil then
  begin
  	xshttp.free;
  	xshttp:=nil;
  end;
  ScriptFinished:=true;
  lblPage.caption:=format('%d / %d',[iPageIndex+1,iPageCount]);
end;

procedure GetRSSFeeds;
begin
  xshttp:=TxsHTTP.Create(nil);
  xshttp.OnHTTPGetFailed:='HttpGetError';
  xshttp.OnHttpGetDone:='HttpDone';
  xshttp.GetHTTP(aurl,true,'AllowRedirects=1;');
  lblPage.caption:='读取...';
end;

procedure ChangeRSS(sender:TObject);
var
  mi:TMenuItem;
begin
  if sender is TMenuItem then
  begin
    mi:=TMenuItem(Sender).parent;
    if (mi<>nil) then
    begin
      inifn:=TIniFile.Create(RSSListFileName);
      try
        aurl:=inifn.ReadString(mi.caption,TMenuItem(Sender).caption,DefURL);
      finally
        inifn.free;
      end;
      SetSetting('Url',aurl);
      GetRSSFeeds;
    end;
  end;
end;

//初始化RSS分类菜单
procedure InitRSSMenu;
var
  i,j:integer;
  inifn:TIniFile;
  s:string;
  lstTypes,lst:TStrings;
  mi,mi1:TMenuItem;
begin
  lstTypes:=TStringList.create;
  lst:=TStringList.create;
  inifn:=TIniFile.Create(RSSListFileName);
  try
    inifn.ReadSections(lstTypes);
    if lstTypes.count>0 then
      for i:=0 to lstTypes.count-1 do
      begin
        mi:=TMenuItem.create(Widget);
        mi.caption:=lstTypes.strings[i];
        nRSS.Insert(i,mi);
        inifn.ReadSection(lstTypes.strings[i],lst);
        if lst.count>0 then
          for j:=0 to lst.count-1 do
          begin
            mi1:=TMenuItem.create(Widget);
            mi1.caption:=lst.strings[j];
            mi1.OnClick:='ChangeRSS';
            mi.Insert(j,mi1);
          end;
      end;
  finally
    lstTypes.free;
    lst.free;
    inifn.free;
  end;
end;

procedure ShowPreview(value:boolean);
var
  w,h:integer;
begin
  if value=imgPreview.visible then exit;
  if value then w:=300
    else w:=-300;
  BeginUpdate;
  imgPreview.visible:=value;
  imgFlyoutTop.visible:=value;
  imgFlyoutBottom.visible:=value;
  lblTitle.visible:=value;

  if Docking then
    h:=45+EveryPageCount*RowHeight
  else
    h:=59+EveryPageCount*RowHeight;
  if value then
  begin
    if h<imgPreview.height then h:=imgPreview.height;
    if h<imgFlyoutBottom.top+imgFlyoutBottom.height then h:=imgFlyoutBottom.top+imgFlyoutBottom.height;
    //h:=h+64;
    //if h<237 then h:=237;
    Widget.Height:=h;
  end
  else
  begin
    Widget.Height:=h;
  end;
  EndUpdate;
  if value then
  begin
    htmlwin.left:=htmlwin.x+Widget.left;
    htmlwin.top:=htmlwin.y+Widget.top;
  end;
  htmlwin.isVisible:=value;
  if not value then
    RefreshSidebarOrder;
end;

procedure RefreshPos;
var
  pos:integer;
  h:integer;
begin
  BeginUpdate;
  imgPreview.NeedRefresh:=true;
  if Docking then
  begin
    Pos:=308;
    RowHeight:=27;
    imgbg.left:=308;
    Widget.Width:=136+Pos;
    imgbg.width:=130;
    imgbg.height:=40+EveryPageCount*RowHeight;
    
    for i:=0 to EveryPageCount-1 do
    if imgLines[i]<>nil then
    begin
      imgLines[i].FileName:=WidgetPath+'images\'+SkinName+'\line_dock.png';
      imgLines[i].left:=5+Pos;
      imgLines[i].top:=RowHeight*(i+1)+6;
      lblTitles[i].width:=120;
      lblTitles[i].top:=12+RowHeight*i;
      lblTitles[i].left:=6+Pos;
      //lblTitles[i].FontStyle.Blur:=not nSkinDefault.checked;
      if lblTitles[i].FontStyle.Font.Name='黑体' then
      begin
        lblTitles[i].FontStyle.Font.size:=10;
        lblDates[i].FontStyle.Font.size:=7;
        lblDescs[i].FontStyle.Font.size:=7;
      end
      else
      begin
        lblTitles[i].FontStyle.Font.size:=9;
        lblDates[i].FontStyle.Font.size:=6;
        lblDescs[i].FontStyle.Font.size:=6;
        //lblTitles[i].HotFontStyle:=',9,#60d0f9';
      end;
      lblDescs[i].visible:=false;
      lblDates[i].visible:=false;
      //lblDates[i].FontStyle.blur:=lblTitles[i].FontStyle.Blur;
    end;
  
    imgnav.left:=16+Pos;
    imgnav.top :=imgbg.height-24; //148;
    lblPage.left:=imgnav.left+15;
    lblPage.top:=imgnav.top;
    btnUp.left:=imgnav.left+2;
    btnUp.top:=imgnav.top+2;
    btnDown.left:=imgnav.left+60;
    btnDown.top:=imgnav.top+2;
    btnRefresh.left:=405;
    btnRefresh.NeedRefresh:=true;
  end
  else
  begin
    Pos:=308;
    RowHeight:=34;
    Widget.Width:=620;
    imgbg.left:=316;
    imgbg.width:=284;
    imgbg.height:=45+EveryPageCount*RowHeight;
    
    for i:=0 to EveryPageCount-1 do
    if imgLines[i]<>nil then
    begin
      imgLines[i].FileName:=WidgetPath+'images\'+SkinName+'\line.png';
      imgLines[i].left:=323;
      imgLines[i].top:=RowHeight*(i+1)+15;
      lblTitles[i].Width:=258;
      lblTitles[i].top:=18+RowHeight*i;
      lblTitles[i].left:=326;
      lblDescs[i].top:=lblTitles[i].top+18;
      lblDates[i].top:=lblDescs[i].top;
      //lblTitles[i].FontStyle.Blur:=not nSkinDefault.checked;
      if lblTitles[i].FontStyle.Font.Name='黑体' then
      begin
        lblTitles[i].FontStyle.Font.size:=11;
        lblDates[i].FontStyle.Font.size:=8;
        lblDescs[i].FontStyle.Font.size:=8;
      end
      else
      begin
        lblTitles[i].FontStyle.Font.size:=10;
        lblDates[i].FontStyle.Font.size:=7;
        lblDescs[i].FontStyle.Font.size:=7;
      end;
      lblDescs[i].visible:=true;
      lblDates[i].visible:=true;
      //lblDates[i].FontStyle.blur:=lblTitles[i].FontStyle.Blur;
    end;

    imgnav.left:=414;
    imgnav.top :=imgbg.height-24; //148;
    lblPage.left:=imgnav.left+15;
    lblPage.top:=imgnav.top;
    btnUp.left:=imgnav.left+4;
    btnDown.left:=imgnav.left+60;
    btnUp.top:=imgnav.top+2;
    btnDown.top:=imgnav.top+2;
    btnRefresh.left:=571;
  end;
  btnRefresh.top:=imgbg.top+imgbg.height-28;
  imgSize.top:=imgbg.top+imgbg.height-imgSize.height;
  imgPreviewSize.top:=imgPreview.top+imgPreview.height-13;
  
  //计算背景透明图片的位置和大小
  if Docking then
  begin
    imgFlyoutTop.width:=138;
    imgFlyoutTop.left:=303;
  end
  else
  begin
    imgFlyoutTop.width:=296;
    imgFlyoutTop.left:=308;
  end;
  imgPreview.width:=imgFlyoutTop.left;
  imgFlyoutBottom.width:=imgFlyoutTop.width;
  imgFlyoutBottom.left:=imgFlyoutTop.left;
  
  if imgbg.height<imgPreview.height-10 then
  begin
    imgFlyoutBottom.FileName:=WidgetPath+'images\'+SkinName+'\flyoutBgRightBottom1.png';
    imgFlyoutTop.Height:=imgbg.height-5;
    imgFlyoutBottom.top:=imgFlyoutTop.top+imgFlyoutTop.height;
    imgFlyoutBottom.height:=imgPreview.height-imgFlyoutBottom.top;
  end
  else
  begin
    imgFlyoutBottom.FileName:=WidgetPath+'images\'+SkinName+'\flyoutBgRightBottom2.png';
    imgFlyoutTop.Height:=imgPreview.height-2; // 236;
    imgFlyoutBottom.top:=imgFlyoutTop.top+imgFlyoutTop.height;
    imgFlyoutBottom.height:=imgbg.height+5-imgFlyoutTop.Height+6;
    if imgbg.height<=230 then
    begin
      imgFlyoutBottom.height:=imgFlyoutBottom.height+10;
    end;
  end;
  if Docking then
  begin
    if htmlwin.isVisible then
    begin
      h:=50+EveryPageCount*RowHeight;
      if h<imgPreview.height then h:=imgPreview.height;
      if h<(imgFlyoutBottom.top+imgFlyoutBottom.height) then h:=imgFlyoutBottom.top+imgFlyoutBottom.height;
      Widget.height:=h;
    end
    else
      Widget.height:=45+EveryPageCount*RowHeight;
  end
  else
  begin
    if htmlwin.isVisible then
    begin
      h:=55+EveryPageCount*RowHeight;
      if h<imgPreview.height then h:=imgPreview.height;
      Widget.height:=h;
    end
    else
      Widget.height:=50+EveryPageCount*RowHeight;
  end;    
  EndUpdate;
  //当拖拽右边的RSS列表时才刷新边栏上的位置
  if isDragRSSRow and Docking then
    RefreshSidebarOrder;
end;

procedure ChangeSkin(aSkinName:string);
var
  i:integer;
  fn:string;
  fColor:TColor;
  bBlur:boolean;
begin
  SkinName:=LowerCase(aSkinName);
  BeginUpdate;
  
  fn:=WidgetPath+'images\'+SkinName+'\';
  imgbg.FileName:=fn+'bg.png';
  imgFlyoutTop.FileName:=fn+'flyoutBgRightTop.png';
  if imgbg.height<imgPreview.height-10 then
    imgFlyoutBottom.FileName:=fn+'flyoutBgRightBottom1.png'
  else
    imgFlyoutBottom.FileName:=fn+'flyoutBgRightBottom2.png';
  imgPreview.FileName:=fn+'flyoutBgLeft.png';
  imgnav.FileName:=fn+'navBack.png';
  btnUp.FileName:=fn+'btn_up.png';
  btnDown.FileName:=fn+'btn_down.png';
  btnRefresh.FileName:=fn+'btnrefresh.png';
  
  RefreshPos;
  
  SetSetting('SkinName',SkinName);
  
  case SkinName of
    'whitetrans' :
      begin
        nSkinWhiteTrans.checked:=true;
        fColor:=FontColor_Trans;
        //bBlur:=false;
        BlurFont:=true;
        HotFontStyleStr:=HotFontStyle_Trans;
      end;
    'vista':
      begin
        nSkinVista.checked:=true;
        fColor:=FontColor_Vista;
        BlurFont:=false;
        //bBlur:=false;
        HotFontStyleStr:=HotFontStyle_Vista;
      end;
  else
    begin
      nSkinDefault.checked:=true;
      fColor:=FontColor_Def;
      BlurFont:=false;
      //bBlur:=false;
      HotFontStyleStr:=HotFontStyle_Def;
    end;
  end;
  for i:=0 to EveryPageCount-1 do
  begin
    lblTitles[i].FontStyle.Font.Color:=fColor;
    lblTitles[i].FontStyle.Blur:=BlurFont; //bBlur;
    lblTitles[i].HotFontStyle:=HotFontStyleStr;
    lblDescs[i].FontStyle.Font.Color:=fColor;
    lblDescs[i].FontStyle.Blur:=BlurFont; //bBlur;
    lblDates[i].FontStyle.Font.Color:=fColor;
    lblDates[i].FontStyle.Blur:=BlurFont; //bBlur;
  end;

  
  EndUpdateAll;
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  RefreshPos;
  ShowPreview(false);
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  RefreshPos;
  ShowPreview(false);
end;

procedure OnTitleClick(sender);
var
  i:integer;
  s:string;
begin
  if (sender is TxspLabel) and (sender.caption<>'') then
  begin
    i:=sender.tag;
    rssItem:=rss.GetItem(iBase+i);
    s:=rssItem.Link;
    lblTitle.caption:=rssItem.title;
    lblTitle.hint:=s;
    //showtrayhint(inttostr(i),'Base:'+inttostr(iBase));
    if s<>'' then
    begin
      //htmlwin.url:=s;
      //不显示滚动条：style="overflow-y:hidden" 
      htmlwin.text:='<html><body borderstyle="none" style="font-family: 微软雅黑,宋体,Arial; font-size: 10pt">'
        +rssItem.description+'<br> <div align=right> <a  href="'+rssItem.Link
        +'"  target="_blank">   <strong>[全文阅读]</strong></right>  </a></div></body></html>';
      ShowPreview(true);
   	end;
//  htmlwin.x:=12;
//  htmlwin.y:=43;
  end;
end;

procedure OnTitleDblClick(sender);
var
  i:integer;
  s:string;
begin
  if (sender is TxspLabel) and (sender.caption<>'') then
  begin
    i:=sender.tag;
    rssItem:=rss.GetItem(iBase+i);
    s:=rssItem.Link;
    //ShellExecute(0,'open','explorer '+s,'',nil,1);
    ShellExecute(0,'open',s,'','',1);
  end;
end;

procedure NextPage;
begin
  if iPageIndex<iPageCount-1 then
    showItems(iPageIndex+1)
  else
    showItems(0);
end;

procedure PrevPage;
begin
  if iPageIndex>0 then
    showItems(iPageIndex-1)
  else
    showItems(iPageCount-1);
end;

//创建新的一行所需的组件
procedure NewItem(index:integer);
var
  s:string;
  iSize:integer;
  fColor:TColor;
  fBlur:boolean;
begin
  BeginUpdate;
  if screen.fonts.indexof('微软雅黑')>=0 then
  begin
    s:='微软雅黑'
    BlurFont:=false;
  end
  else
  begin
    s:='黑体';
    lblTitle.FontStyle.Font.Size:=11;
    BlurFont:=true;
  end;
  lblTitle.FontStyle.Font.Name:=s;  
  if (index<10) and (imgLines[index]=nil) then
  begin
    imgLines[index]:=TxspImage.create(Widget);
    imgLines[index].parent:=Widget;
    imgLines[index].AutoSize:=true;
  
    case SkinName of
      'whitetrans' :
        begin
          fColor:=FontColor_Trans;
          //bBlur:=true;
          HotFontStyleStr:=HotFontStyle_Trans;
        end;
      'vista' :
        begin
          fColor:=FontColor_Vista;
          //bBlur:=false;
          HotFontStyleStr:=HotFontStyle_Vista;
        end;
    else
      begin
        fColor:=FontColor_Def;
        //bBlur:=false;
        HotFontStyleStr:=HotFontStyle_Def;
      end;
    end;

    lblDescs[index]:=TxspLabel.create(Widget);
    lblDescs[index].caption:='';
    lblDescs[index].parent:=Widget;
    lblDescs[index].Height:=18;
    lblDescs[index].left:=326;
    lblDescs[index].FontStyle.Font.Color:=fColor;
    lblDescs[index].FontStyle.Blur:=BlurFont; //bBlur;

    lblTitles[index]:=TxspLabel.create(Widget);
    lblTitles[index].caption:='';
    lblTitles[index].parent:=Widget;
    lblTitles[index].FontStyle.Font.Name:=s;
    //lblTitles[index].FontStyle.Font.size:=9
    //lblTitles[index].FontStyle.Font.color:=$C19D82; // $DDDDDD; //FFFFFF; 829dc1
    //if s='黑体' then
      lblTitles[index].FontStyle.blur:=BlurFont; //true;
    lblTitles[index].OnClick:='OnTitleClick';
    lblTitles[index].OnDblClick:='OnTitleDblClick';
    lblTitles[index].Tag:=index;
    lblTitles[index].Height:=18;
    lblTitles[index].FontStyle.Font.Color:=fColor;
    lblTitles[index].FontStyle.Blur:=BlurFont; //bBlur;
    lblTitles[index].HotFontStyle:=HotFontStyleStr;
  
    lblDates[index]:=TxspLabel.create(Widget);
    lblDates[index].caption:='';
    lblDates[index].parent:=Widget;
    lblDates[index].FontStyle.Align:=xaTopRight;
    lblDates[index].left:=412;
    lblDates[index].width:=170;
    lblDates[index].Height:=15;
    lblDates[index].FontStyle.Font.Color:=fColor;
    lblDates[index].FontStyle.Blur:=BlurFont; //bBlur;

  end;
  EndUpdate;
  //ChangeSkin(SkinName);
end;

//删除指定行数中的组件
procedure DelItem(ItemCount:integer);
var
  i:integer;
begin
  for i:=EveryPageCount-ItemCount to EveryPageCount-1 do
  begin
    if imgLines[i]<>nil then
    begin
      TxspImage(imgLines[i]).free;
      imgLines[i]:=nil;
    end;
    if lblDescs[i]<>nil then
    begin
      TxspLabel(lblDescs[i]).free;
      lblDescs[i]:=nil;
    end;
    if lblTitles[i]<>nil then
    begin
      TxspLabel(lblTitles[i]).free;
      lblTitles[i]:=nil;
    end;
    if lblDates[i]<>nil then
    begin
      TxspLabel(lblDates[i]).free;
      lblDates[i]:=nil;
    end;
  end;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
var
  i:integer;
begin
  isDragRSSRow:=true;
  EveryPageCount:=5;
  FirstShowHint:=true;
  ChangeSize:=false;
  imgSize.OnMouseDown:='imgSizeMouseDown';
  imgSize.OnMouseUp:='imgSizeMouseUp';
  imgSize.OnMouseMove:='imgSizeMouseMove';
  imgPreviewSize.OnMouseDown:='imgSizeMouseDown';
  imgPreviewSize.OnMouseUp:='imgSizeMouseUp';
  imgPreviewSize.OnMouseMove:='imgSizeMouseMove';
  imgLines:=[nil,nil,nil,nil,nil,nil,nil,nil,nil,nil];
  lblDescs:=[nil,nil,nil,nil,nil,nil,nil,nil,nil,nil];
  lblTitles:=[nil,nil,nil,nil,nil,nil,nil,nil,nil,nil];
  lblDates:=[nil,nil,nil,nil,nil,nil,nil,nil,nil,nil];
  //htmlwin.width:=287;
  iBase:=0;
  iPageIndex:=0;
  aUrl:=GetSetting('Url',DefURL);
  RSSFileName:=AppPath+'Data\'+WidgetName+'_RSS.xml';
  RSSListFileName:=AppPath+'Data\rsslist.dat';

  EveryPageCount:=GetSettingInt('EveryPageCount',5);
  if EveryPageCount>10 then EveryPageCount:=10;
  ShowTitleHint:=GetSettingInt('ShowTitleHint',0)=1;
  SkinName:=GetSetting('SkinName','default');
  //if SkinName<>'default' then ChangeSkin(SkinName);

  BeginUpdate;
  imgPreview.height:=GetSettingInt('PreviewHeight',237);
  htmlwin.height:=imgPreview.height-56;
  
  if EveryPageCount>0 then
  begin
    for i:=0 to EveryPageCount-1 do 
    begin
      NewItem(i);
    end;
    RefreshPos;  
  end;
  rss:=TxsRss.create;
  if FileExists(RSSFilename) then
    rss.LoadFromFile(RSSFileName);
  if rss.Active then
  begin
    iPageCount:=rss.Count div EveryPageCount;
    if (rss.count mod EveryPageCount)>0 then inc(iPageCount);
    ShowItems(0);
    //showtrayhint('active',inttostr(rss.count));
  end
  else iPageCount:=0;
    
  EndUpdateAll;
  GetRSSFeeds;
  i:=GetSetting('RefreshInterval',10);
  if i<=0 then
    tmr:=nil
  else
  begin
    tmr:=TTimer.create(Widget);
    tmr.Interval:=i*60000;
    tmr.OnTimer:='OntmrTimer';
  end;
  ChangeSkin(SkinName);
  {if Docking then
    OnWidgetDock
  else
    OnWidgetUnDock;}
  InitRSSMenu;  
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
  FreeMenu;
  if tmr<>nil then tmr.free;
  rss.free;
  rss:=nil;
end;

procedure OntmrTimer(sender);
begin
  GetRSSFeeds;
end;

//--- 应用程序失去焦点事件，当应用失去当前焦点时触发此事件 ---
procedure OnApplicationDeactivate;
begin
  ShowPreview(false);
end;

procedure OnWidgetMouseLeave(sender);
begin
  ShowPreview(false);
end;

procedure BrowseUrl;
begin
  ShellExecute(0,'open',lblTitle.hint,'','',1);
  //ShellExecute(0,'open','iexplore.exe',lblTitle.hint,nil,1);
end;

//--- 调整窗口大小的代码 ---
procedure imgSizeMouseDown(sender; Shift: Button; TShiftState; X, Y: Integer);
begin
  SizeX:=X;
  SizeY:=Y;
  ChangeSize:=true;
  isDragRSSRow:=(sender=imgSize);
end;

procedure imgSizeMouseUp(sender; Shift: Button; TShiftState; X, Y: Integer);
begin
  ChangeSize:=false;
  if sender=imgPreviewSize then
  begin

  end;

  //imgSize.top:=Widget.height-imgSize.height;
end;

procedure imgSizeMouseMove(sender; Shift: TShiftState; X, Y: Integer);
var
  i,j:integer;
begin
  if ChangeSize then
  begin //拖拽右边的RSS列表
    if sender=imgSize then
    begin
      if Y>SizeY then
        i:=(Y-SizeY) div RowHeight
      else
        i:=-((SizeY-Y) div RowHeight);
      if i<>0 then
      begin
        if i>0 then
        begin
          for j:=0 to i-1 do
          begin
            if (EveryPageCount+j)>=10 then exit;
            NewItem(EveryPageCount+j);
          end;
        end
        else if (EveryPageCount+i>2) then
        begin
          DelItem(-i);
        end
        else exit;
        
        EveryPageCount:=EveryPageCount+i;
        if EveryPageCount>10 then EveryPageCount:=10;
        SetSettingInt('EveryPageCount',EveryPageCount);
        //SizeY:=y;
        iPageCount:=rss.Count div EveryPageCount;
        if (rss.Count mod EveryPageCount)>0 then iPageCount:=iPageCount+1;
        if iPageIndex>=iPageCount then
          iPageIndex:=iPageCount-1;
        RefreshPos;
        ShowItems(iPageIndex);
        //showtrayhint(Widget.height,y); //exit;
      end;
    end
    else if (imgPreview.height+Y)>=200 then //拖拽左边的预览窗口高度
    begin
      BeginUpdate;
      imgPreview.height:=imgPreview.height+Y;
      RefreshPos;
      EndUpdate;
      htmlwin.height:=imgPreview.height-56;
      SetSettingInt('PreviewHeight',imgPreview.height);
    end;
  end;
end;

//--- Widget鼠标滚轮事件，当在Widget上进行滚轮操作时会触发此事件 ---
procedure OnWidgetMouseWheel(WheelDelta);
begin
  if WheelDelta>0 then
  begin
    if iBase>0 then 
    begin
      dec(iBase);
      ShowItems(-1);
    end;
  end
  else
  begin
    if iBase<rss.count-EveryPageCount then
    begin
      inc(iBase);
      ShowItems(-1);
    end;
  end;
  if (iBase mod EveryPageCount)=0 then
    iPageIndex:=iBase div EveryPageCount
  else
    iPageIndex:=(iBase div EveryPageCount)+1;
  lblPage.caption:=format('%d / %d',[iPageIndex+1,iPageCount]);
end;

