var
  isHorz:boolean;
  aColor:string;
  GrowBox:boolean;
  GrowBoxX,GrowBoxY:integer;
  UnitIndex:integer;

//横向/纵向切换
procedure RefreshRuler;
begin
  BeginUpdate;
  if isHorz then
  begin
    imgbg.FileName:=WidgetPath+'images\horz.png';
    imgcur.FileName:=WidgetPath+'images\curh.png';
    imgSwitch.FileName:=WidgetPath+'images\rotate.png';
    imgSwitch.left:=8; imgSwitch.top:=48;
    imgColor.left:=2; imgColor.top:=30;
    imgcur.top:=0;
    shp.top:=30;
    lblcur.top:=shp.top+1;
    imgbg.Width:=300;
    imgbg.Height:=74;
    imggrowbox.FileName:=WidgetPath+'images\growbox.png';
    imggrowbox.left:=283;
    imggrowbox.top:=55;
    imgcur.left:=19;
    imgcur.top:=0;
    imggrowboxNull.cursor:=crSizeWE;
  end
  else//vert
  begin
    imgbg.FileName:=WidgetPath+'images\vert.png';
    imgcur.FileName:=WidgetPath+'images\curv.png';
    imgSwitch.FileName:=WidgetPath+'images\rotate_v.png';
    imgSwitch.left:=10; imgSwitch.top:=8;
    imgColor.left:=28; imgColor.top:=2;
    imgcur.left:=47;
    shp.left:=27;
    lblcur.left:=shp.left-1;
    imgbg.Width:=74;
    imgbg.Height:=300;
    imggrowbox.FileName:=WidgetPath+'images\growbox_v.png';
    imggrowbox.left:=8;
    imggrowbox.top:=283;
    imggrowboxNull.cursor:=crSizeNS;
  end;
  Widget.width:=imgbg.width;
  Widget.height:=imgbg.height;
  imggrowboxNull.left:=imggrowbox.left;
  imggrowboxNull.top:=imggrowbox.top;
  case aColor of
    'b': shp.Color:=$0071B5;
    'r': shp.Color:=$2222B2;
    'g': shp.Color:=$32aa32;
  else
    shp.Color:=$0071B5;
  end;
  shp.ColorTo:=shp.Color;
  EndUpdate;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  AutoScreenSnap:=false;
  AutoAdjustMem:=0;
  GrowBox:=false;
  imgbg.OnMouseMove:='MouseMove';
  lblcur.OnMouseMove:='MouseMove';
  imggrowboxNull.OnMouseDown:='GrowBoxMouseDown';
  imggrowboxNull.OnMouseUp:='GrowBoxMouseUp';
  imggrowboxNull.OnMouseMove:='GrowBoxMouseMove';
  AllowDock:=false;
  ShowToolbar:=false;
  isHorz:=GetSetting('isHorz','1')='1';
  aColor:=GetSetting('Color','b');
  UnitIndex:=GetSettingInt('UnitIndex',0);
  RefreshRuler;
end;

procedure SwitchHorz;
begin
  isHorz:=not isHorz;
  RefreshRuler;
end;

procedure SwitchColor;
begin
  case aColor of
    'b': aColor:='r';
    'r': aColor:='g';
    'g': aColor:='b';
  end;
  RefreshRuler;
end;

//刻度的移动
procedure MouseMove(sender; Shift: TShiftState; X, Y: Integer);
var
  px,py:integer;
begin
  px:=sender.left+x;
  py:=sender.top+y;
  BeginUpdate;
  if not imgcur.visible then
  begin
    imgcur.visible:=true;
    shp.visible:=imgcur.visible;
    lblcur.visible:=imgcur.visible;
  end;
  if isHorz and (px>13) and (px<=imgbg.Width-14) then
  begin
    imgcur.left:=px;
    case UnitIndex of
      1: //inch 英寸
        lblcur.caption:=format('%.2f',[(px-14)/screen.PixelsPerInch]);
      2: //毫米
        lblcur.caption:=format('%.1f',[(px-14)/screen.PixelsPerInch*25.4]);
    else
      lblcur.caption:=IntToStr(px-14);
    end;
    //screen.PixelsPerInch:屏幕每英寸的像素数量
    //lblcur.caption:=format('%d,%.2f',[px-14,(px-14)/screen.PixelsPerInch]);
    //1 inch 英寸=25.4 millimetres 毫米
    //lblcur.caption:=format('%d,%.1f',[px-14,(px-14)/screen.PixelsPerInch*25.4]);
    shp.width:=length(lblcur.caption)*9;
    if shp.width=9 then shp.width:=13;
    shp.left:=round(imgcur.left-shp.width/2);
    lblcur.left:=shp.left;
    lblcur.width:=shp.width;
  end
  else if not isHorz and (py>13) and (py<=imgbg.Height-14) then
  begin
    imgcur.top:=py;
    //lblcur.caption:=inttostr(py-14);
    case UnitIndex of
      1: //inch 英寸
        lblcur.caption:=format('%.2f',[(py-14)/screen.PixelsPerInch]);
      2: //毫米
        lblcur.caption:=format('%.1f',[(py-14)/screen.PixelsPerInch*25.4]);
    else
      lblcur.caption:=IntToStr(py-14);
    end;
    shp.width:=length(lblcur.caption)*9;
    if shp.width=9 then shp.width:=13;
    shp.top:=round(imgcur.top-shp.height/2);
    lblcur.top:=shp.top;
    lblcur.width:=shp.width;
  end;
    
  imgbg.NeedRefresh:=true;
  EndUpdate;
end;

procedure GrowBoxMouseDown(sender; Shift: Button; TShiftState; X, Y: Integer);
begin
  GrowBoxX:=X;
  GrowBoxY:=Y;
  GrowBox:=true;
  imgcur.visible:=false;
  shp.visible:=imgcur.visible;
  lblcur.visible:=imgcur.visible;
end;

procedure GrowBoxMouseUp(sender; Shift: Button; TShiftState; X, Y: Integer);
begin
  GrowBox:=false;
  imggrowboxNull.left:=imggrowbox.left;
  imggrowboxNull.top:=imggrowbox.top;
end;

procedure GrowBoxMouseMove(sender; Shift: TShiftState; X, Y: Integer);
var
  i:integer;
begin
  if GrowBox then
  begin
    if isHorz then
    begin
      i:=Widget.Width+X-GrowBoxX;
      if i>78 then
      begin
        Widget.Width:=i;
        imgbg.Width:=Widget.Width;
        GrowBoxX:=X;
        imggrowbox.left:=imgbg.Width-17;
      end
    end
    else
    begin
      i:=Widget.Height+Y-GrowBoxY;
      if i>78 then
      begin
        Widget.Height:=i;
        imgbg.Height:=Widget.Height;
        GrowBoxY:=Y;
        imggrowbox.top:=imgbg.height-17;
      end;
    end;
  end;
end;

