const
  sInterval=#1;
  
var
  PageIndex:integer;
  NoteFileName:string;
  Notes:TStrings;
  NoteNames, NoteColor;
  NoteIndex:integer;

  imgSizeX,imgSizeY:integer;
  SizeChange:boolean;

procedure SetPos(isDocking:boolean);
begin
  if isDocking then
  begin
    mmo.left:=12;
    mmo.width:=101;
    mmo.height:=Widget.Height-47;
    imgBar.Left:=26;
    imgBar.top:=Widget.Height-34;
    lblPage.left:=imgBar.left+13;
    lblPage.top:=imgBar.top;
    imgPrev.left:=imgBar.left+2;
    imgPrev.top:=imgBar.top+2;
    imgNext.left:=imgBar.left+58;
    imgNext.top:=imgBar.top+2;
    imgDel.top:=imgBar.top+3;
    imgAdd.top:=imgDel.top;
    imgAdd.left:=105;
    //imgSize.visible:=false;
    imgSize.left:=Widget.width-imgSize.width-4;
    imgSize.top:=Widget.height-imgSize.height-10;
    imgSize.Cursor:=crSizeNS;
  end
  else
  begin
    imgbg.width:=Widget.Width-5;
    imgbg.height:=Widget.Height;
    mmo.width:=imgbg.width-34;
    mmo.height:=imgbg.height-54;
    imgBar.top:=imgbg.height-37;
    imgBar.Left:=(imgbg.width div 2)-38;
    lblPage.left:=imgBar.left+13;
    lblPage.top:=imgBar.top;
    imgPrev.left:=imgBar.left+2;
    imgPrev.top:=imgBar.top+2;
    imgNext.left:=imgBar.left+58;
    imgNext.top:=imgBar.top+2;
    imgDel.top:=imgBar.top+3;
    imgAdd.top:=imgDel.top;
    imgAdd.left:=imgbg.width-32;
    //imgSize.visible:=true;
    imgSize.left:=Widget.width-imgSize.width-15;
    imgSize.top:=Widget.height-imgSize.height-15;
    imgSize.Cursor:=crSizeNWSE;
  end;
end;

procedure RefreshStaus;
begin
  imgbar.visible:=Notes.Count>1;
  lblPage.visible:=imgBar.visible;
  imgPrev.visible:=imgBar.visible;
  imgNext.visible:=imgBar.visible;
  imgDel.Enabled:=(PageIndex>=0) and (PageIndex<Notes.Count);
  lblPage.caption:=format('%d / %d',[PageIndex+1,Notes.Count]);
end;

procedure SaveNote;
begin
  //StringsToStr(mmo.text,sInterval);
  if (PageIndex>=0) and (PageIndex<Notes.count) then
    Notes.strings[PageIndex]:=StringsToStr(mmo.text,sInterval);
//  else
//    Notes.text:='';
  Notes.savetofile(NoteFileName);
  //showtrayhint('',Notes.strings[PageIndex]);
end;

procedure ShowNote(index:integer);
begin
//  showtrayhint('note',notes.text);
  if (index>=0) and (index<Notes.count) then
  begin
    BeginUpdate;
    mmo.text:=StrToStrings(Notes.strings[index],sInterval);
    RefreshStaus;
    EndUpdate;
  end;
end;

procedure PrevNote;
begin
  if (PageIndex>=0) and (PageIndex<Notes.Count) then
    Notes.strings[PageIndex]:=StringsToStr(mmo.text,sInterval);
  dec(PageIndex);
  if PageIndex<0 then PageIndex:=Notes.count-1;
  ShowNote(PageIndex);
end;

procedure NextNote;
begin
  if (PageIndex>=0) and (PageIndex<Notes.Count) then
    Notes.strings[PageIndex]:=StringsToStr(mmo.text,sInterval);
  inc(PageIndex)
  if PageIndex>=Notes.Count then 
    PageIndex:=0;
  ShowNote(PageIndex);
end;

procedure AddNote;
begin
//showtrayhint('PageIndex',inttostr(pageindex));
  if (PageIndex>=0) and (PageIndex<Notes.Count) then
  begin
    Notes.strings[PageIndex]:=StringsToStr(mmo.text,sInterval);
    Notes.add('');
  end
  else
  begin
    if mmo.text<>'' then
      Notes.add(StringsToStr(mmo.text,sInterval));
    Notes.add('');
  end;
  PageIndex:=Notes.Count-1;
  SetSetting('PageIndex',PageIndex);
  ShowNote(PageIndex);
end;

procedure DelNote;
begin
  if (PageIndex>=0) and (PageIndex<Notes.count) then
  begin
    Notes.Delete(PageIndex);
    if PageIndex>=Notes.count then dec(PageIndex);
  end
  else
  begin
    Notes.clear;
    mmo.text:='';
  end;
  if Notes.count<=0 then
  begin
    PageIndex:=-1;
    mmo.text:='';
  end;
  ShowNote(PageIndex);
  SaveNote;
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  BeginUpdate;
  Widget.Width:=137;
  Widget.height:=GetSettingInt('NoteHeight',121);
  //Widget.Height:=121;
  imgbg.FileName:=WidgetPath+'images\sticky_'+NoteNames[NoteIndex]+'_docked.png';
  imgbg.width:=Widget.Width; //136;
  imgbg.height:=Widget.height; //121;
  SetPos(true);
  EndUpdate;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  BeginUpdate;
  Widget.width:=GetSettingInt('NoteWidth',202);
  Widget.height:=GetSettingInt('NoteHeight',177);
  imgbg.FileName:=WidgetPath+'images\sticky_'+NoteNames[NoteIndex]+'.png';
  SetPos(false);
  EndUpdate;
end;

//读取当前Widget的INI设置
procedure InitIni;
begin
  NoteFileName:=AppPath+'Data\'+WidgetName+'.note';
  if FileExists(NoteFileName) then
    Notes.LoadFromFile(NoteFileName);
  PageIndex:=GetSettingInt('PageIndex',0);
  NoteIndex:=GetSetting('NoteIndex',0);
  s:=NoteNames[NoteIndex]
  if Docking then
    imgbg.FileName:=WidgetPath+'images\sticky_'+s+'_docked.png'
  else
    imgbg.FileName:=WidgetPath+'images\sticky_'+s+'.png';
  mmo.FontStyle.Font.Name:=GetSetting('FontName','宋体');
  mmo.FontStyle.Font.size:=GetSettingInt('FontSize',9);
  mmo.FontStyle.Font.color:=GetSettingInt('Color',$000000);
  mmo.TransparentColor:=NoteColor[NoteIndex]; 
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  NoteNames:=['yellow', 'blue', 'green', 'pink', 'purple', 'white'];
  NoteColor:=[$BCFFFF, $FDF4C9, $BEF5C3, $FEC2FD, $FED3CC, $F8F9F9];
  Notes:=TStringList.Create;
  BeginUpdate;
  InitIni;
  Widget.width:=GetSettingInt('NoteWidth',202);
  Widget.height:=GetSettingInt('NoteHeight',177);
  SetPos(Docking);
  if Notes.Count<=0 then
    AddNote
  else
    ShowNote(PageIndex);
  EndUpdate;
  imgSize.OnMouseDown:='imgSizeMouseDown';
  imgSize.OnMouseUp:='imgSizeMouseUp';
  imgSize.OnMouseMove:='imgSizeMouseMove';
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
  if (Notes.Count>0) and (PageIndex>=0) and (PageIndex<Notes.Count) then
    Notes.strings[PageIndex]:=StringsToStr(mmo.text,sInterval);
  if PageIndex>=0 then
    SetSetting('PageIndex',PageIndex);
  SaveNote;
  Notes.free;
end;

//--- Widget事件，当打开鼠标移到Widget区域内时触发此事件 ---
procedure OnWidgetMouseEnter;
begin
  BeginUpdate;
  imgbar.visible:=Notes.Count>1;
  lblPage.visible:=imgBar.visible;
  imgPrev.visible:=imgBar.visible;
  imgNext.visible:=imgBar.visible;
  imgDel.visible:=true;
  imgAdd.visible:=true;
  EndUpdate;
end;

//--- Widget事件，当打开鼠标移出Widget区域时触发此事件 ---
procedure OnWidgetMouseLeave;
begin
  BeginUpdate;
  imgbar.visible:=Notes.Count>1;
  lblPage.visible:=imgBar.visible;
  imgPrev.visible:=imgBar.visible;
  imgNext.visible:=imgBar.visible;
  imgDel.visible:=false;
  imgAdd.visible:=false;
  EndUpdate;

  if (PageIndex>=0) and (PageIndex<Notes.count) then
    if (Notes.strings[PageIndex]<>StringsToStr(mmo.text,sInterval)) then
    SaveNote;
end;

procedure imgSizeMouseDown(sender; Shift: Button; TShiftState; X, Y: Integer);
begin
  imgSize.visible:=false;
  imgSizeX:=X;
  imgSizeY:=Y;
  SizeChange:=true;
  BeginUpdate;
  mmo.visible:=false;
  imgBar.visible:=mmo.visible;
  lblPage.visible:=imgBar.visible;
  imgPrev.visible:=imgBar.visible;
  imgNext.visible:=imgBar.visible;
  imgDel.visible:=imgBar.visible;
  imgAdd.visible:=imgBar.visible;
  EndUpdate;
end;

procedure imgSizeMouseUp(sender; Shift: Button; TShiftState; X, Y: Integer);
begin
  SizeChange:=false;
  BeginUpdate;
  imgSize.visible:=true;
  mmo.visible:=true;
  SetPos(Docking);
  imgBar.visible:=mmo.visible;
  lblPage.visible:=imgBar.visible;
  imgPrev.visible:=imgBar.visible;
  imgNext.visible:=imgBar.visible;
  imgDel.visible:=imgBar.visible;
  imgAdd.visible:=imgBar.visible;
  EndUpdate;
  if not Docking then
    SetSettingInt('NoteWidth',Widget.width);
  SetSettingInt('NoteHeight',Widget.height);
end;

procedure imgSizeMouseMove(sender; Shift: TShiftState; X, Y: Integer);
var
  i:integer;
begin
  if SizeChange then
  begin
    BeginUpdate;
    if not Docking then
    begin
      i:=Widget.Width+X-imgSizeX;
      if (i>160) and (i<800) then
      begin
        Widget.Width:=i;
        imgbg.Width:=Widget.Width-5;
        imgSizeX:=X;
      end;
    end;
    
    i:=Widget.Height+Y-imgSizeY;
    if (i>100) and (i<500) then
    begin
      Widget.Height:=i;
      imgbg.Height:=Widget.Height;
      imgSizeY:=Y;
    end;
    if not Docking then
      imgbg.width:=Widget.Width-5;
    imgbg.height:=Widget.Height;
    
    EndUpdate;
    if Docking then
      RefreshSidebarOrder;
  end;
end;

