///////////////////////////////////////////
////默认脚本SDK模板  object pascal 语言编写
////您可以随意修改脚本,并署上您的名字发布. 如果是基于其他作者脚本修改的,建议最好能注明一下:)
////
////作者: Holy&醉拥孤衾
////创建时间: 2006- -
////说明:
///////////////////////////////////////////
////更新历史:
////
///////////////////////////////////////////

//当widget保存配置信息的时候触发此事件,并执行此函数内代码.
procedure OnSaveIni;
begin
  //如果自定义了某些变量,请在这里将自定义的配置信息保存
  //SaveIni(name,value);
    SaveIni('todolistFileName',xStr[500]);
    SaveIni('labelFontname',xStr[501]);
    SaveIni('labelFontSize',xStr[502]);
end;

procedure OnSaveDefIni;
begin
    SaveDefIni('todolistFileName',xStr[500]);
    SaveDefIni('labelFontname',xStr[501]);
    SaveDefIni('labelFontSize',xStr[502]);
end;

//弹出的确认窗口
function myShowMsgBox(caption,msg):boolean;
begin
  fm := TForm.CreateForm;
  fm.Caption := caption;
  fm.Width := 340;
  fm.Height := 110;
  fm.BorderStyle := bsToolWindow;
  fm.Position := poDesktopCenter;
  fm.font.name := 'Tahoma';
  fm.font.size := 9;
  fm.FormStyle := 3;    //窗口顶端显示

  //------文本 Label0
  Label0 :=  TLabel.Create( fm );
  Label0.Parent := fm;
  Label0.Left := 16;
  Label0.top := 24;
  Label0.width := 54;
  Label0.height := 12;
  Label0.AutoSize := -1;
  Label0.WordWrap := 0;
  Label0.Caption := msg;

  //------按钮 BitBtn0
  BitBtn0 :=  TBitBtn.Create( fm );
  BitBtn0.Parent := fm;
  BitBtn0.Left := 90;
  BitBtn0.top := 56;
  BitBtn0.width := 75;
  BitBtn0.height := 25;
  BitBtn0.Caption := '确定';
  BitBtn0.ModalResult := 1;
  BitBtn0.onclick := 'BitBtn0Click';

  //------按钮 BitBtn1
  BitBtn1 :=  TBitBtn.Create( fm );
  BitBtn1.Parent := fm;
  BitBtn1.Left := 170;
  BitBtn1.top := 56;
  BitBtn1.width := 75;
  BitBtn1.height := 25;
  BitBtn1.Caption := '取消';
  BitBtn1.ModalResult := 2;


  //----显示配置窗口----
  if fm.ShowModal = mrOk then
  begin
    result := true;
  end
  else
    result := false;
  fm.free;
end;

procedure LoadItems(pri,itemCount,ItemHeight,ItemWidth,nodel);
var
  i,id,curItemIndex,dt,isHideOverdueTodo,str;
begin
  labelFontname := xStr[501];
  labelFontSize := xStr[502];
    ///////是否隐藏过期任务
    isHideOverdueTodo := xBool[0];

    for i := 0 to ItemCount-1 do
    begin
      if lowercase(nodel.Item(i).selectSingleNode('priority').text)<>lowercase(pri) then
        continue;

      if isHideOverdueTodo then
      begin
        str := nodel.Item(i).selectSingleNode('date').text;
        if trim(str)='' then continue;

        dt := StrToDateTimeDef(str,0);
        if dt=0 then continue;

        if trunc(dt)<date then
          continue;
      end;


      xInt[1100] := xInt[1100] + 1;
      curItemIndex :=  xInt[1100] - 1;

      try
        xStr[curItemIndex] := nodel.Item(i).selectSingleNode('id').text ;
        xStr[50+curItemIndex] := nodel.Item(i).selectSingleNode('title').text ;
        xStr[100+curItemIndex] := nodel.Item(i).selectSingleNode('priority').text ;
        xStr[150+curItemIndex] := nodel.Item(i).selectSingleNode('date').text ;
        xStr[200+curItemIndex] := nodel.Item(i).selectSingleNode('memo').text ;
      except
      end;
      id := nodel.Item(i).selectSingleNode('id').text ;

      AddLabel(curItemIndex,'');
      SetLabelPro(curItemIndex,'text',xStr[50+curItemIndex]);
      SetLabelPro(curItemIndex,'left',20);
      SetLabelPro(curItemIndex,'top', curItemIndex*ItemHeight + 3);
      SetLabelPro(curItemIndex,'AutoSize',false);
      SetLabelPro(curItemIndex,'width',140);
    //  SetLabelPro(curItemIndex,'height',13);
      SetLabelPro(curItemIndex,'visible','true');

      dt := StrToDateTimeDef(xStr[150+curItemIndex],now + 1);

      if trunc(dt)>=date then
      begin
        SetLabelPro(curItemIndex,'fontstr','255,255,255/-12/'+ labelFontname +'/'+ labelFontSize +'/1000')
        SetLabelPro(curItemIndex,'alpha','255');
        SetLabelPro(curItemIndex,'shadowalpha','255');
      end;
      else
      begin
        SetLabelPro(curItemIndex,'fontstr','255,255,255/-12/'+ labelFontname +'/'+ labelFontSize +'/1001');
        SetLabelPro(curItemIndex,'alpha','200');
        SetLabelPro(curItemIndex,'shadowalpha','200');
      end;

      //设定末尾省略号显示
      SetLabelPro(curItemIndex,'Trimming','4');
      //设定单行显示
      SetLabelPro(curItemIndex,'formatflags','4096');

      SetLabelPro(curItemIndex,'EnableShadow','true');
    //  SetLabelPro(curItemIndex,'shadowalpha','255');
      SetLabelPro(curItemIndex,'shadowcolor','clBlack');
      SetLabelPro(curItemIndex,'shadowleft','0');
      SetLabelPro(curItemIndex,'shadowtop','1');
      SetLabelPro(curItemIndex,'shadowBlur','1');


      AddButton(2*curItemIndex + 1,false,widgetPath + 'edit.png');
      AddButton(2*curItemIndex + 2,false,widgetPath + 'delete.png');

      SetButtonPro(2*curItemIndex + 1,'left', ItemWidth - 33 );
      SetButtonPro(2*curItemIndex + 1,'top', ItemHeight*curItemIndex + 4 );
      SetButtonPro(2*curItemIndex + 1,'visible',false);
      SetButtonPro(2*curItemIndex + 1,'tagstr',id);
      SetButtonPro(2*curItemIndex + 1,'EnableHotSize','true');
      SetButtonPro(2*curItemIndex + 1,'HotSizeOffset', 1 );
      SetButtonPro(2*curItemIndex + 1,'hotdirection','1');
      SetButtonPro(2*curItemIndex + 1,'quality','2');
//      SetButtonPro(2*curItemIndex + 1,'hint','编辑');

      SetButtonPro(2*curItemIndex + 2,'left', ItemWidth - 20 );
      SetButtonPro(2*curItemIndex + 2,'top', ItemHeight*curItemIndex + 4 );
      SetButtonPro(2*curItemIndex + 2,'visible',false);
      SetButtonPro(2*curItemIndex + 2,'tagstr',id);
      SetButtonPro(2*curItemIndex + 2,'EnableHotSize','true');
      SetButtonPro(2*curItemIndex + 2,'HotSizeOffset', 1 );
      SetButtonPro(2*curItemIndex + 2,'hotdirection','1');
      SetButtonPro(2*curItemIndex + 2,'quality','2');
//      SetButtonPro(2*curItemIndex + 2,'hint','删除');
    end;

end;

procedure ShowHideButtons(flag);
var
  itemCount,ItemHeight,ItemWidth;
begin
  ItemCount := xInt[1000];
  ItemHeight := xInt[1001];
  ItemWidth := xInt[1002];

  SetButtonPro(0,'visible',flag);

  for i:=0 to ItemCount-1 do
  begin
    SetButtonPro(2*i + 1,'visible',flag);
    SetButtonPro(2*i + 2,'visible',flag);
    if flag then
      SetLabelPro(i,'width',115)
    else
      SetLabelPro(i,'width',140);
  end;
  RefreshWidget;

end;

procedure RefreshTodoList;
var
  itemCount,ItemHeight,ItemWidth,todolistFileName,sl;
begin
  ItemHeight := xInt[1001];
  ItemWidth := xInt[1002];

  todolistFileName:= xStr[500];
  labelFontname := xStr[501];
  labelFontSize := xStr[502];
  ///////////
  xInt[1100] := 0; //临时全局变量,用于存放当前加载了多少个item
  LoadedItemCount := xInt[1100];

  if not fileexists(XDeskShowPath + 'UserSave\' + todolistFileName) then
  begin
    sl := TStringList.create;
    sl.Add('<?xml version="1.0" encoding="gb2312"?>');
    sl.add('<todolist>');
    sl.add('</todolist>');
    sl.savetofile(XDeskShowPath + 'UserSave\' + todolistFileName);

    sl.free;
  end;

  /////////////////////
  xmld := CreateOleObject('microsoft.xmldom');
  if xmld.Load(XDeskShowPath + 'UserSave\' + todolistFileName) then
  begin
    try
      nodel := xmld.DocumentElement.getElementsByTagName('item');
      ItemCount := nodel.Length;
    except
      itemcount := 0;
    end;

    xInt[1000] := ItemCount;

    i := 49 ;
    while i>=0 do
    begin
      SetLabelPro(i,'top',-1000);
      SetLabelPro(i,'visible','false');

      if i>0 then
      begin
        SetButtonPro(i,'left',-1000);
        SetButtonPro(i,'visible',false);
       // DeleteButton(i);
      end;
      i := i - 1;
    end;

    xInt[1100] := 0;

    if ItemCount<>0 then
    begin
      LoadItems('0',itemCount,ItemHeight,ItemWidth,nodel);
      LoadItems('1',itemCount,ItemHeight,ItemWidth,nodel);
      LoadItems('2',itemCount,ItemHeight,ItemWidth,nodel);
      LoadItems('3',itemCount,ItemHeight,ItemWidth,nodel);
      LoadItems('4',itemCount,ItemHeight,ItemWidth,nodel);
    end;

    ItemCount := xInt[1100];
    xInt[1000] := ItemCount;


    if ItemCount=0 then
    begin
      AddLabel(0,'');
      SetLabelPro(0,'text','休息,休息休息~~');
      SetLabelPro(0,'left',10);
      SetLabelPro(0,'top', 3);
      SetLabelPro(0,'AutoSize',false);
      SetLabelPro(0,'width',145);
      SetLabelPro(0,'height',15);
      SetLabelPro(0,'visible','true');
      SetLabelPro(0,'fontstr','255,255,255/-12/'+ labelFontname +'/'+ labelFontSize +'/1000');
      SetLabelPro(0,'Trimming','4');

      SetLabelPro(0,'EnableShadow','true');
      SetLabelPro(0,'shadowalpha','255');
      SetLabelPro(0,'shadowcolor','clBlack');
      SetLabelPro(0,'shadowleft','0');
      SetLabelPro(0,'shadowtop','1');
      SetLabelPro(0,'shadowBlur','2');

    end;


    SetButtonPro(0,'left', ItemWidth -  GetButtonPro(0,'width') );
    if ItemCount<>0 then
      SetButtonPro(0,'top', ItemHeight*ItemCount )
    else
      SetButtonPro(0,'top', ItemHeight );

    SetButtonPro(0,'visible',false);
  end;
  xmld := unassigned;

  //修改widet的宽度和高度为 序号0的图片控件的宽度和高度
  widgetWidth := ItemWidth;
  if ItemCount<>0 then
    widgetHeight := ItemHeight * itemCount + GetButtonPro(0,'height')
  else
    widgetHeight := ItemHeight  + GetButtonPro(0,'height');

  ShowHideButtons(true);
end;


//修改任务的窗口
Procedure AddEditItemForm(flag,ItemId,ItemIndex);
var
  ItemCount,todolistFileName;
begin
  todolistFileName:= xStr[500];

  fm := TForm.CreateForm;
  if flag='add' then
    fm.Caption := '新建任务'
  else if flag='edit' then
    fm.Caption := '编辑任务';
  fm.Width := 390;
  fm.Height := 285;                         
  fm.BorderStyle := bsToolWindow;
  fm.Position := poDesktopCenter;
  fm.font.name := '宋体';
  fm.font.size := 9;


  //------文本 Label0
  Label0 :=  TLabel.Create( fm );
  Label0.Parent := fm;
  Label0.Left := 16;
  Label0.top := 18;
  Label0.width := 54;
  Label0.height := 12;
  Label0.AutoSize := -1;
  Label0.WordWrap := 0;
  Label0.Caption := '任务名称:';

  //------编辑框 XDSEdit0
  XDSEdit0 :=  TXDSEdit.Create( fm );
  XDSEdit0.Parent := fm;
  XDSEdit0.Left := 82;
  XDSEdit0.top := 14;
  XDSEdit0.width := 190;
  XDSEdit0.height := 20;
  if flag='add' then
    XDSEdit0.Text := '新任务'
  else if flag='edit' then
    XDSEdit0.Text :=  xStr[50 + ItemIndex];
  XDSEdit0.LinkStyle := lsNormal;

  //------文本 Label1
  Label1 :=  TLabel.Create( fm );
  Label1.Parent := fm;
  Label1.Left := 16;
  Label1.top := 52;
  Label1.width := 54;
  Label1.height := 12;
  Label1.AutoSize := -1;
  Label1.WordWrap := 0;
  Label1.Caption := '完成日期:';

  //------时间框 DateTimePicker0
  DateTimePicker0 :=  TDateTimePicker.Create( fm );
  DateTimePicker0.Parent := fm;
  DateTimePicker0.Left := 80;
  DateTimePicker0.top := 48;
  DateTimePicker0.width := 194;
  DateTimePicker0.height := 22;
  DateTimePicker0.Kind := 0;
  DateTimePicker0.DateMode := 0;
  DateTimePicker0.Format := 'yyyy 年 MM 月 dd 日 dddd';
  if flag='add' then
    DateTimePicker0.Date := now;
  else if flag='edit' then
    DateTimePicker0.Date := StrToDateTimeDef( xStr[150 + ItemIndex] ,now);

  //------文本 Label2
  Label2 :=  TLabel.Create( fm );
  Label2.Parent := fm;
  Label2.Left := 16;
  Label2.top := 85;
  Label2.width := 54;
  Label2.height := 12;
  Label2.AutoSize := -1;
  Label2.WordWrap := 0;
  Label2.Caption := '优先级别:';

  //------下拉框 ComboBox0
  ComboBox0 :=  TComboBox.Create( fm );
  ComboBox0.Parent := fm;
  ComboBox0.Left := 80;
  ComboBox0.top := 80;
  ComboBox0.width := 194;
  ComboBox0.height := 20;
  ComboBox0.Style := 2;
  ComboBox0.DropDownCount := 8;
  ComboBox0.ItemIndex := 2;
  ComboBox0.items.add('最高');
  ComboBox0.items.add('较高');
  ComboBox0.items.add('一般');
  ComboBox0.items.add('较低');
  ComboBox0.items.add('最低');
  ComboBox0.Text := '一般';
  if flag='add' then
    ComboBox0.ItemIndex := 2
  else if flag='edit' then
    ComboBox0.ItemIndex  :=  StrToIntDef( xStr[100 + ItemIndex] ,2);

  //------文本 Label3
  Label3 :=  TLabel.Create( fm );
  Label3.Parent := fm;
  Label3.Left := 16;
  Label3.top := 115;
  Label3.width := 54;
  Label3.height := 12;
  Label3.AutoSize := -1;
  Label3.WordWrap := 0;
  Label3.Caption := '详细信息:';

  //------文本框 Memo0
  Memo0 :=  TMemo.Create( fm );
  Memo0.Parent := fm;
  Memo0.Left := 16;
  Memo0.top := 130;
  Memo0.width := 356;
  Memo0.height := 121;
  Memo0.ScrollBars := 0;
  if flag='add' then
    Memo0.Lines.Text := ''
  else if flag='edit' then
    Memo0.Lines.Text := xStr[200 + ItemIndex];
  print(xStr[200 + ItemIndex]);

  //------按钮 BitBtn0
  BitBtn0 :=  TBitBtn.Create( fm );
  BitBtn0.Parent := fm;
  BitBtn0.Left := 296;
  BitBtn0.top := 13;
  BitBtn0.width := 75;
  BitBtn0.height := 25;
  BitBtn0.Caption := '确定';
  BitBtn0.ModalResult := 1;
  BitBtn0.onclick := 'BitBtn0Click';

  //------按钮 BitBtn1
  BitBtn1 :=  TBitBtn.Create( fm );
  BitBtn1.Parent := fm;
  BitBtn1.Left := 296;
  BitBtn1.top := 45;
  BitBtn1.width := 75;
  BitBtn1.height := 25;
  BitBtn1.Caption := '取消';
  BitBtn1.ModalResult := 2;


  //----显示配置窗口----
  if fm.ShowModal = mrOk then
  begin
    xmld := CreateOleObject('microsoft.xmldom');
    if xmld.Load(XDeskShowPath + 'UserSave\' + todolistFileName) then
    begin
      if trim(XDSEdit0.Text)='' then XDSEdit0.Text := '[空]';
    
      if flag='add' then
      begin
        node :=  xmld.createNode(1,'item','');
        nodeid :=  xmld.createNode(1,'id','');
        nodeTitle :=  xmld.createNode(1,'title','');
        nodePriority :=  xmld.createNode(1,'priority','');
        nodeDate :=  xmld.createNode(1,'date','');
        nodeMemo :=  xmld.createNode(1,'memo','');

        nodeid.text := GetGUID;
        nodeTitle.text := '';
        nodeTitle.appendChild( xmld.createCDATASection( XDSEdit0.Text ) );
        nodePriority.text := inttostr(ComboBox0.ItemIndex);
        nodeDate.text :=  FormatDateTime('yyyy-mm-dd hh:nn:ss',DateTimePicker0.date);
        nodeMemo.text := '';
        nodeMemo.appendChild( xmld.createCDATASection( Memo0.Lines.Text) );

        node.AppendChild(nodeid);
        node.AppendChild(nodeTitle);
        node.AppendChild(nodePriority);
        node.AppendChild(nodeDate);
        node.AppendChild(nodeMemo);

        xmld.DocumentElement.AppendChild(node);

      end
      else if flag='edit' then
      begin
        nodel := xmld.DocumentElement.getElementsByTagName('item');
        ItemCount := nodel.Length;
        for i := 0 to ItemCount-1 do
        begin
          if Lowercase(ItemId) = Lowercase(nodel.Item(i).selectSingleNode('id').text) then
          begin
            nodel.Item(i).selectSingleNode('title').text := '';
            nodel.Item(i).selectSingleNode('title').appendChild( xmld.createCDATASection(  XDSEdit0.Text  ) );
            nodel.Item(i).selectSingleNode('date').text := FormatDateTime('yyyy-mm-dd hh:nn:ss',DateTimePicker0.date);
            nodel.Item(i).selectSingleNode('priority').text := inttostr(ComboBox0.ItemIndex);
            nodel.Item(i).selectSingleNode('memo').text := '';
            nodel.Item(i).selectSingleNode('memo').appendChild( xmld.createCDATASection(  Memo0.Lines.Text  ) );// '<![CDATA[ 1111111111 ]]>';

            break;
          end;
        end;
      end;


    end;
    xmld.save(XDeskShowPath + 'UserSave\' + todolistFileName);
    xmld := unassigned;

    RefreshTodoList;
  end;
  fm.free;

end;

procedure DeleteItem(itemId);
var
  todolistFileName;
begin
  todolistFileName := xStr[500];

  xmld := CreateOleObject('microsoft.xmldom');
  if xmld.Load(XDeskShowPath + 'UserSave\' + todolistFileName) then
  begin
    nodel := xmld.DocumentElement.getElementsByTagName('item');
    ItemCount := nodel.Length;

    for i := 0 to ItemCount-1 do
    begin
      if Lowercase(ItemId) = Lowercase(nodel.Item(i).selectSingleNode('id').text) then
      begin
        print('delete:' + itemid);
        xmld.DocumentElement.removeChild(nodel.Item(i));
        break;
      end;
    end;
    xmld.save(XDeskShowPath + 'UserSave\' + todolistFileName);
  end;

  xmld := unassigned;
  RefreshTodoList;

end;




//插件初始化函数   用于资源和图片的载入等初始化
procedure Init;
var
  ItemCount,ItemHeight,ItemWidth,LoadedItemCount,todolistFileName,isHideOverdueTodo;
begin
  ItemCount := 0;


  todolistFileName := LoadIni('todolistFileName','todolist.xdsTodo');
  labelFontname := LoadIni('labelFontname','黑体');
  labelFontSize := LoadIni('labelFontSize','10');

  xStr[500] := todolistFileName ;//XDeskShowPath + 'UserSave\todolist.xdsTodo';

  xStr[501] := labelFontname;
  xStr[502] := labelFontSize;

  //设置整个widget插件的宽度和高度
  widgetWidth := 100;
  widgetHeight := 100;

  //设置背景图片的坐标
  BackgroundLeft := 0;
  BackgroundTop := 0;

  //载入图片控件,文字控件,按钮控件,图片资源等等
  LoadPicResource(0,widgetPath + 'Item3.png');
  LoadPicResource(2,widgetPath + 'ball/'+'0.png');
  LoadPicResource(3,widgetPath + 'ball/'+'1.png');
  LoadPicResource(4,widgetPath + 'ball/'+'2.png');
  LoadPicResource(5,widgetPath + 'ball/'+'3.png');
  LoadPicResource(6,widgetPath + 'ball/'+'4.png');

  ItemHeight := GetResPicHeight(0);
  ItemWidth :=  GetResPicWidth(0);

  ///////是否隐藏过期任务
  xBool[0] := LoadIni('isHideOverdueTodo','0');
  isHideOverdueTodo := xBool[0];

  ///////////
  xInt[1100] := 0; //临时全局变量,用于存放当前加载了多少个item
  LoadedItemCount := xInt[1100];

  if lowercase(ExtractFileExt(todolistFileName))<>'.xdstodo' then
  begin
    todolistFileName := todolistFileName + '.xdstodo';
    xStr[500] := todolistFileName ;
    SaveIni('todolistFileName',xStr[500]);
  end;

  sl := TStringList.create;

  if not fileexists(XDeskShowPath + 'UserSave\' + todolistFileName) then
  begin
    sl.clear;
    sl.Add('<?xml version="1.0" encoding="gb2312"?>');
    sl.add('<todolist>');
    sl.add('</todolist>');
    sl.savetofile(XDeskShowPath + 'UserSave\' + todolistFileName);
  end;

  sl.clear;
  sl.loadFromFile(XDeskShowPath + 'UserSave\' + todolistFileName);

  if Pos('<todolist>',sl.Text)<0 then
  begin
    print('createtodolistfile');
    sl.clear;
    sl.Add('<?xml version="1.0" encoding="gb2312"?>');
    sl.add('<todolist>');
    sl.add('</todolist>');
    sl.savetofile(XDeskShowPath + 'UserSave\' + todolistFileName);
  end;

  sl.free;

  ///////////////////////////////
  xmld := CreateOleObject('microsoft.xmldom');
  if xmld.Load(XDeskShowPath + 'UserSave\' + todolistFileName) then
  begin
    try
      nodel := xmld.DocumentElement.getElementsByTagName('item');
      ItemCount := nodel.Length;
    except
      itemcount := 0;
    end;

    AddButton(0,false,widgetPath + 'add3.png');
    SetButtonPro(0,'visible',false);

    i := 49 ;
    while i>=0 do
    begin
      SetLabelPro(i,'top',-1000);
      SetLabelPro(i,'visible','false');

      if i>0 then
      begin
        SetButtonPro(i,'left',-1000);
        SetButtonPro(i,'visible',false);
       // DeleteButton(i);
      end;
      i := i - 1;
    end;

    xInt[1100] := 0;


    if ItemCount<>0 then
    begin
      LoadItems('0',itemCount,ItemHeight,ItemWidth,nodel);
      LoadItems('1',itemCount,ItemHeight,ItemWidth,nodel);
      LoadItems('2',itemCount,ItemHeight,ItemWidth,nodel);
      LoadItems('3',itemCount,ItemHeight,ItemWidth,nodel);
      LoadItems('4',itemCount,ItemHeight,ItemWidth,nodel);
    end;


    ItemCount := xInt[1100];
    xInt[1000] := ItemCount;


    if ItemCount=0 then
    begin
      AddLabel(0,'');
      SetLabelPro(0,'text','休息,休息休息~~');
      SetLabelPro(0,'left',10);
      SetLabelPro(0,'top', 3);
      SetLabelPro(0,'AutoSize',false);
      SetLabelPro(0,'width',145);
      SetLabelPro(0,'height',15);
      SetLabelPro(0,'visible','true');
      SetLabelPro(0,'fontstr','255,255,255/-12/'+ labelFontname +'/'+ labelFontSize +'/1000');
      SetLabelPro(0,'Trimming','4');

      SetLabelPro(0,'EnableShadow','true');
      SetLabelPro(0,'shadowalpha','255');
      SetLabelPro(0,'shadowcolor','clBlack');
      SetLabelPro(0,'shadowleft','0');
      SetLabelPro(0,'shadowtop','1');
      SetLabelPro(0,'shadowBlur','2');

    end;

    SetButtonPro(0,'left', ItemWidth -  GetButtonPro(0,'width') );
    if ItemCount<>0 then
      SetButtonPro(0,'top', ItemHeight*ItemCount )
    else
      SetButtonPro(0,'top', ItemHeight );


    xmld := unassigned;

  end;




  //修改widet的宽度和高度为 序号0的图片控件的宽度和高度
  widgetWidth := GetResPicWidth(0);
  if ItemCount<>0 then
    widgetHeight := ItemHeight * itemCount + GetButtonPro(0,'height')
  else
    widgetHeight := ItemHeight  + GetButtonPro(0,'height');



  xInt[1001] := ItemHeight;
  xInt[1002] := ItemWidth;


  nCustMenu0.Visible := true;
  nCustMenu0.caption := '新建任务...';
  nCustMenu1.Visible := true;
  nCustMenu1.caption := '-';

  nCustMenu2.Visible := true;
  if not isHideOverdueTodo then
    nCustMenu2.caption := '隐藏过期任务'
  else
    nCustMenu2.caption := '显示所有任务';

  nCustMenu3.Visible := true;
  nCustMenu3.caption := '-';

  nCustMenu4.Visible := true;
  nCustMenu4.caption := '设置任务数据存储文件名...';

  nCustMenu5.Visible := true;
  nCustMenu5.caption := '更改字体...';

  nCustMenu6.Visible := true;
  nCustMenu6.caption := '-';
end;

//画widget整个外观的函数
procedure OnDrawWidget;
var
  itemCount,ItemHeight,ItemWidth;
begin
  ItemCount := xInt[1000];
  ItemHeight := xInt[1001];
  ItemWidth := xInt[1002];


  //可以在这里添加任何对widget外观的绘制代码.
  for i:=0 to ItemCount-1 do
  begin
    DrawImage(0,0, i*ItemHeight );

  end;

  if ItemCount=0 then
    DrawImage(0,0,0 );


  //系统自绘所有widget控件的函数. 如果不调用此函数将不会显示所有图片/文字/按钮控件
  InheritDrawWidget;

  //可以在这里添加任何对widget外观的绘制代码
  for i:=0 to ItemCount-1 do
  begin
    DrawImage(2 + strtointdef(xStr[100+i],2) ,5, i*(ItemHeight) +2);
    DrawImage(1,0, i*ItemHeight );
  end;

  if ItemCount=0 then
  begin

    DrawImage(1,0, 0 );
  end;

end;

//启动插件事件
procedure StartWidget;
begin
  //可以在这里添加在启动widget时需要做的一些事情的代码.例如启动定时器
  {......}
  //示例:将序号为0的定时器启动,间隔时间1000毫秒
  //SetTimer(0,1000,true);

end;

//定时器每到预定时间时执行此函数代码,index为定时器序号
procedure OnTimer(index);
begin
  Case index of
    0:
    begin
      //当序号为 0 的定时器到预定时间时,这里添加的代码将被执行
      {......}

    end;
    1:
      begin
      //当序号为 1 的定时器到预定时间时,这里添加的代码将被执行
      //以下依次类推,可以自行添加序号.最多可以有11个定时器,序号最多到10
      {......}

      end;
    end;

end;

//当按钮控件被单击(鼠标在按钮上按下并抬起后)时,执行此函数代码. index为被点击的按钮的序号
procedure OnButtonClick(index);
var
  itemindex;
begin
  if index=0 then
  begin
    AddEditItemForm('add',0,0);
//    AddItem;
  end
  else
  begin
    if index mod 2 then
    begin
      AddEditItemForm('edit',GetButtonPro(index,'tagstr'),(index + 1)/2-1);
      //EditItem(GetButtonPro(index,'tagstr'));


    end
    else
    begin
      if myShowMsgBox('删除任务确认','确认要删除任务 "' + xStr[50+ index/2-1] + '" ?') then
        DeleteItem(GetButtonPro(index,'tagstr'));
    end;

  end;


end;

//当按钮控件被鼠标悬停在上面时,执行此函数代码. index为被悬停的按钮的序号
procedure OnButtonHot(index);
begin
  RefreshWidget;
  Case index of
    0:
      begin
      //当序号为 0 的按钮被悬停时,这里添加的代码将被执行
      //以下依次类推,可以自行添加对应按钮的序号
      {......}

      end;
    1:
      begin

      end;
    end;
end;

//当按钮控件被鼠标按下时,执行此函数代码. index为被被按下的按钮的序号
procedure OnButtonDown(index);
begin
  RefreshWidget;
  Case index of
    0:
      begin
      //当序号为 0 的按钮被鼠标按下时,这里添加的代码将被执行
      //以下依次类推,可以自行添加对应按钮的序号
      {......}

      end;
    1:
      begin

      end;
    end;
end;

//当鼠标单击widget时触发此事件,执行此函数内代码
procedure OnClickWidget;
begin

  //当widget被鼠标单击时,这里添加的代码将被执行
  {......}

end;


//当鼠标双击widget时触发此事件,执行此函数内代码
procedure OnDBClickWidget;
begin
  //当widget被双击时,这里添加的代码将被执行
  {......}
  AddEditItemForm('add',0,0);
end;

//当鼠标进入widget上方时,触发此事件,将执行此函数代码.
procedure OnMouseEnterWidget;
begin

end;

//当鼠标离开widget上方时,触发此事件,将执行此函数代码.
procedure OnMouseLeaveWidget;
begin

end;

//当鼠标在widget上移动的时候,调用此函数.
procedure OnMoveOnWidget;
begin

end;

//当有文件拖放到widget上时,触发此事件,将执行此函数代码.
procedure OnDropFile(filename);
begin
  // filename为被拖放到widget上的文件的完整路径和文件名.在这里写处理此事件的代码
  {...}
end;

//当xdeskshow的widget获得焦点时,触发此事件,并执行此函数内代码.
procedure OnActiveWidget;
begin


end;


//当xdeskshow的widget失去焦点时,触发此事件,并执行此函数内代码.
procedure OnDeActiveWidget;
begin
  ShowHideButtons(false);

  while  GetWidgetTop > GetScreenHeight - widgetHeight + GetButtonPro(0,'height') do
  begin
    SetWidgetTop(GetWidgetTop - 1);
  end;


end;


//当在widget上有按键时触发此事件,并执行此函数内代码. key为按下的键的代码. ctl=true表示ctrl键按下,alt=true表示Alt键按下,shf=true表示Shift键按下. 否则都为0.
procedure OnKeyDownWidget(key,ctl,alt,shf);
begin
  //print('按下的键的代码是: '+inttostr(key) );
  //if ctl then print('Ctrl键被按下');


end;

//当鼠标滚轮在widget中向上滚动时触发此事件,执行此函数内代码
//MousePosX,MousePosY为鼠标当前坐标
procedure OnWidgetMouseWheelUp(MousePosX,MousePosY);
begin
  //鼠标滚轮在widget中向上滚动时,这里添加的代码将被执行
  {......}
end;

//当鼠标滚轮在widget中向下滚动时触发此事件,执行此函数内代码
//MousePosX,MousePosY为鼠标当前坐标
procedure OnWidgetMouseWheelDown(MousePosX,MousePosY);
begin
  //鼠标滚轮在widget中向下滚动时,这里添加的代码将被执行
  {......}
end;



//当有新驱动器添加时触发此事件,执行此函数内代码
//例如:USB盘插入时
procedure OnDriveAdd(DriveType,Path);
begin

end;

//当有新驱动器添加时触发此事件,执行此函数内代码
//例如:USB盘拔出时
procedure OnDriveRemoved(DriveType,Path);
begin

end;


//当邮件检测完毕时,这里添加的代码将被执行
//index为检测开始时赋予的序号，count为检测获得的邮件数量
procedure CheckEmailCountCompleted(index,count);
begin
  Case index of
    0:
      begin
      //当序号为 0 的邮件检测完毕时,这里添加的代码将被执行
      //以下依次类推,可以自行添加对应的序号

      end;
    1:
      begin

      end;
    end;
end;




//当获取网页完毕时,这里添加的代码将被执行
//index为获取网页html开始时赋予的序号，str为获得的指定网页的html字符串,filename为网页文件存放的文件名,用完后建议删除filename指向的文件
procedure GetHtmlStrCompleted(index,str,filename);
begin
  Case index of
    0:
      begin
      //当序号为 0 的网页获取完毕时,这里添加的代码将被执行
      //以下依次类推,可以自行添加对应的序号

      end;
    1:
      begin

      end;
    end;
  DeleteFile(filename);
end;

Procedure MyTodoFileConfigForm;
var
  ss,sl;
begin
  todolistFileName:= xStr[500];

  fm := TForm.CreateForm;
  fm.Caption := '设置任务数据存储文件名';
  fm.Width := 270;
  fm.Height := 135;
  fm.BorderStyle := bsToolWindow;
  fm.Position := poDesktopCenter;
  fm.font.name := '宋体';
  fm.font.size := 9;
  //------文本 Label0
  Label0 :=  TLabel.Create( fm );
  Label0.Parent := fm;
  Label0.Left := 16;
  Label0.top := 10;
  Label0.width := 48;
  Label0.height := 12;
  Label0.AutoSize := -1;
  Label0.WordWrap := 0;
  Label0.Caption := '请选择任务数据存储文件名:'+ #13 +'[也可以直接输入新文件名来新建数据文件]';

  //------下拉框 ComboBox0
  ComboBox0 :=  TComboBox.Create( fm );
  ComboBox0.Parent := fm;
  ComboBox0.Left := 16;
  ComboBox0.top := 40;
  ComboBox0.width := 229;
  ComboBox0.height := 20;
  ComboBox0.Style := 0;
  ComboBox0.DropDownCount := 28;

  sl := TStringlist.create;
  sl.text := GetExtFileNameList(XDeskShowPath + 'UserSave\' ,'xdsTodo');
  for i:=0 to sl.count-1 do
  begin
    ComboBox0.items.add( sl.strings[i] );
  end;
  if sl.count>0 then
  begin
    ComboBox0.ItemIndex := ComboBox0.Items.IndexOf(todolistFileName);
  end;
  sl.free;
                                                 
//  ComboBox0.Text := '下拉框0';

  //------线条 Bevel0
  Bevel0 :=  TBevel.Create( fm );
  Bevel0.Parent := fm;
  Bevel0.Left := 0;
  Bevel0.top := 72;
  Bevel0.width := 361;
  Bevel0.height := 10;
  Bevel0.Style := 0;
  Bevel0.Shape := 2;

{  //------按钮 BitBtn0
  BitBtn15 :=  TBitBtn.Create( fm );
  BitBtn15.Parent := fm;
  BitBtn15.Left := 16;
  BitBtn15.top := 83;
  BitBtn15.width := 89;
  BitBtn15.height := 25;
  BitBtn15.Caption := '新建任务文件';
  BitBtn15.ModalResult := 0;
  //BitBtn15.onclick := 'BitBtnSaveThemeClick';
    }
  //------按钮 BitBtn1
  BitBtn0 :=  TBitBtn.Create( fm );
  BitBtn0.Parent := fm;
  BitBtn0.Left := 130;
  BitBtn0.top := 83;
  BitBtn0.width := 57;
  BitBtn0.height := 25;
  BitBtn0.Caption := '确定';
  BitBtn0.ModalResult := 1;
  BitBtn0.onclick := 'BitBtn0Click';

  //------按钮 BitBtn2
  BitBtn2 :=  TBitBtn.Create( fm );
  BitBtn2.Parent := fm;
  BitBtn2.Left := 187;
  BitBtn2.top := 83;
  BitBtn2.width := 57;
  BitBtn2.height := 25;
  BitBtn2.Caption := '取消';
  BitBtn2.ModalResult := 2;


  //----显示配置窗口----
  if fm.ShowModal = mrOk then
  begin
    ss := ComboBox0.text;
    if trim(ss)='' then
      ss := '未命名.xdsTodo';

    todolistFileName := ss;
    xStr[500] := todolistFileName ;
    SaveIni('todolistFileName',xStr[500]);
    SaveDefIni('todolistFileName',xStr[500]);

    if lowercase(ExtractFileExt(todolistFileName))<>'.xdstodo' then
    begin
      todolistFileName := todolistFileName + '.xdstodo';
      xStr[500] := todolistFileName ;
      SaveIni('todolistFileName',xStr[500]);
      SaveDefIni('todolistFileName',xStr[500]);
    end;

    //RefreshTodoList;
    ReLoadWidget;



  end;
  fm.free;
end;


//当自定义菜单被点击时,这里添加的代码将被执行
//index为自定义菜单的序号（0－8）
procedure OnCustomMenuClick(index);
var
  isHideOverdueTodo;
begin
  ///////是否隐藏过期任务
  isHideOverdueTodo := xBool[0];

  Case index of
    0:
      begin
      //当序号为 0 的自定义菜单被点击时,这里添加的代码将被执行
      //以下依次类推,0至8号,可以自行添加对应序号的响应代码
      //  showmessage('第一个自定义菜单被点击哦！');
         AddEditItemForm('add',0,0);
      end;
    1:
      begin

      end;
    2:
      begin
        isHideOverdueTodo := not isHideOverdueTodo;
        xBool[0] := isHideOverdueTodo;

        RefreshTodoList;

        if xBool[0] then
        begin
          SaveIni('isHideOverdueTodo','1');
          SaveDefIni('isHideOverdueTodo','1');
        end
        else
        begin
          SaveIni('isHideOverdueTodo','0');
          SaveDefIni('isHideOverdueTodo','0');           
        end;

        if not isHideOverdueTodo then
          nCustMenu2.caption := '隐藏过期任务'
        else
          nCustMenu2.caption := '显示所有任务';

      end;
    4:
      begin
        MyTodoFileConfigForm;
      end;
    5:
      begin
        fd := TFontDialog.create(nil);
        fd.font.name := xStr[501];
        fd.font.size := xStr[502];
        fd.execute;

        xStr[501] := fd.font.name;
        xStr[502] := fd.font.size;

        SaveIni('labelFontname',xStr[501]);
        SaveIni('labelFontSize',xStr[502]);

        SaveDefIni('labelFontname',xStr[501]);
        SaveDefIni('labelFontSize',xStr[502]);

        fd.free;

        RefreshTodoList;

      end;
  end;
end;

//当关闭widget时,触发此事件,并执行此函数内代码.
procedure OnCloseWidget;
begin


end;

//当退出XDeskShow时,触发此事件,并执行此函数内代码.
procedure OnExitXDS;
begin


end;


//当克隆本插件时,触发此事件,并执行此函数内代码.
procedure OnCloneWidget;
begin


end;

//当widget切换脚本的时候,触发此事件,并执行此函数内代码.
procedure OnBeforeChangeScript;
begin


end;

//当在Init初始化的时候CreateFolderMon指定的分区/文件夹被改变的时候触发此事件
//filename为被改变/读写的文件名
procedure OnFolderChange(filename);
begin


end;

//当widget的右键菜单弹出的时候触发此事件
procedure OnPopupMenu;
begin

end;


procedure OnWidgetMouseDown(x,y,ctl,alt,shf);
begin
  ShowHideButtons(true);
end;






Procedure BitBtn0Click(sender);
begin
  Sender.ModalResult := mrOk;
end;








