{******************************************************************}
{ DtlsDlg.pas                                                     }
{                                                                  }
{ Author    : A.Nasir Senturk                                      }
{ Home Page : http://www.shenturk.com                              }
{ Email     : shenturk@gmail.com                                   }
{                                                                  }
{ Date      : 21.01.2007                                           }
{ Update    : 24.03.2007                                           }
{                                                                  }
{ Sizden iki şey rica edicem:                                      }
{ 1. Lutfen bu baslik kismini kaldirmayiniz.                       }
{ 2. Mumkunse bagis yapiniz.                                       }
{ *****************************************************************}

unit DtlsDlg;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, GdipApi, GdipObj, DirectDraw, TextUtil, ExtCtrls, ConstDef,
  ActnList, Menus;

type
  TDetailsForm = class(TForm)
    BackgrndLbl: TLabel;
    CloseBtn: TButton;
    HeaderTextLbl: TLabel;
    ControlLabel: TLabel;
    HeaderLabel: TLabel;
    TrafficActionList: TActionList;
    AlwaysTopAction: TAction;
    TrafficPopup: TPopupMenu;
    AlwaysTopMenu: TMenuItem;
    MouseTimer: TTimer;
    HibernateAction: TAction;
    HibernateMenu: TMenuItem;
    HideAction: TAction;
    HideMenu: TMenuItem;
    UpLabel1: TLabel;
    DownLabel1: TLabel;
    PeriodLabel1: TLabel;
    DataLabel1: TLabel;
    DataLabel2: TLabel;
    DataLabel3: TLabel;
    PeriodLabel2: TLabel;
    UpLabel2: TLabel;
    DownLabel2: TLabel;
    UpLabel3: TLabel;
    DownLabel3: TLabel;
    UpLabel4: TLabel;
    DownLabel4: TLabel;
    DataLabel4: TLabel;
    PeriodLabel3: TLabel;
    PeriodLabel4: TLabel;
    DataLabel5: TLabel;
    PeriodLabel5: TLabel;
    UpLabel5: TLabel;
    DownLabel5: TLabel;
    DataLabel6: TLabel;
    PeriodLabel6: TLabel;
    UpLabel6: TLabel;
    DownLabel6: TLabel;
    PeriodHead: TLabel;
    UpHead: TLabel;
    DownHead: TLabel;
    DescLbl: TLabel;
    RatioLabel: TLabel;
    Ratio1: TLabel;
    Ratio2: TLabel;
    Ratio3: TLabel;
    Ratio4: TLabel;
    Ratio5: TLabel;
    Ratio6: TLabel;
    ServiceNoTitleLbl: TLabel;
    ServiceNoLbl: TLabel;
    RecordTitleLbl: TLabel;
    RecordLbl: TLabel;
    QuotaTitleLbl: TLabel;
    QuotaLbl: TLabel;
    DisplayTitleLbl: TLabel;
    DisplayNameLbl: TLabel;
    CountLabel: TLabel;
    N1: TMenuItem;
    OptionsMenu: TMenuItem;
    MoveWithAct: TAction;
    MoveWithMenu: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure CloseBtnClick(Sender: TObject);
    procedure AlwaysTopActionExecute(Sender: TObject);
    procedure MouseTimerTimer(Sender: TObject);
    procedure HibernateActionExecute(Sender: TObject);
    procedure HideActionExecute(Sender: TObject);
    procedure MoveWithActExecute(Sender: TObject);
    procedure TrafficPopupPopup(Sender: TObject);
  private
    { Private declarations }
    OpacityMin,
    OpacityMax,
    Opacity: Byte;
    MainBuffer: TGPBitmap;
    DrawCanvas: TGPGraphics;
    PrevFormStyle: Cardinal;
    ColorizedImage,
    OverlayImage,
    CloseImage,
    CancelImage,
    CancelImageEnter,
    CancelImageLeave: TGPBitmap;

    Updating: Boolean;

    InGridPen,
    InLinePen: TGPPen;
    InBackBrush: TGPSolidBrush;

    OutGridPen,
    OutLinePen: TGPPen;
    OutBackBrush: TGPSolidBrush;

    BarBackBrush,
    BarFillBrush,
    InBarBrush,
    OutBarBrush: TGPSolidBrush;
    {
    ControlBuffer: TGPBitmap;
    ControlCanvas: TGPGraphics;
    }
    TableItems: array[0..MAX_ITEM - 1] of TTableItem;
    procedure AllocateDrawItems;
    procedure ReleaseDrawItems;
    procedure AllocateHandle;
    procedure ReleaseHandle;
    procedure SetFormStyleEx;
  protected
    InGridColor,
    InLineColor,
    InBackColor,
    OutGridColor,
    OutLineColor,
    OutBackColor: Cardinal;
  public
    { Public declarations }
    BackColor: Cardinal;
    BackOpacity: Byte;
    ScaleWidth,
    ScaleHeight: Single;
    TextColorNormal,
    TextColorExcess,
    TextColorHeader: TGPColor;
    procedure UpdateLayered;
    procedure UpdateMainWindow;
    procedure PaintBackground;
    procedure PaintButtons;
    procedure PaintHeaderText;
    procedure HideForm;
    procedure ShowForm;
    procedure HideFormEffect(const Min: Integer);
    procedure ShowFormEffect(const Max: Integer);
    procedure Hibernate;
    procedure Wakeup;
    procedure FadeInEffect(const Step, Wait, Max: Integer);
    procedure FadeOutEffect(const Step, Wait, Min: Integer);
    procedure InitDetailsForm;
    procedure DoneDetailsForm;
    procedure LoadOptions;
    procedure SaveOptions;
    procedure SetWorkArea;
    procedure SetColorsDrawItems;
    procedure LoadDetails;
    procedure PaintControlLabel;
    procedure PaintHeaderLabel;
    procedure PaintDataLabels;
    procedure PaintDataLabel1;
    procedure PaintDataLabel2;
    procedure PaintDataLabel3;
    procedure PaintDataLabel4;
    procedure PaintDataLabel5;
    procedure PaintDataLabel6;
    procedure PaintDescription;
    procedure PaintServiceNo;
    procedure PaintDisplayName;
    procedure PaintRecordDateTime;
    procedure PaintQuota;
    procedure PaintDataCount;
    function GetServiceNoText: string;
    function GetDisplayNameText: WideString;
    function GetRecordDateTimeText: string;
    function GetQuotaText: string;
    function GetPeriodText(const Index: Integer): WideString;
    function GetDownloadText(const Index: Integer): string;
    function GetUploadText(const Index: Integer): string;
    function GetDataCountText: WideString;
    function GetHasData(const Index: Integer): Boolean;
    function GetDownloadAsByte(const Index: Integer): Double;
    function GetUploadAsByte(const Index: Integer): Double;
    function GetColor(const Index: Integer): TGPColor;
    procedure UpdateDetails;
    procedure PaintRatio(RatioLabel: TLabel; const DataIndex: Integer);
    procedure UpdateIniFile;
  end;

implementation

uses Main, OptnsDlg, Math;

{$R *.dfm}

{ TDetailsForm }

procedure TDetailsForm.AllocateHandle;
begin
  MainBuffer := TGPBitmap.Create(Width, Height, PixelFormat32bppARGB);
  DrawCanvas := TGPGraphics.Create(MainBuffer);
end;

procedure TDetailsForm.FadeInEffect(const Step, Wait, Max: Integer);
begin
  if not EyMainForm.EnableFadeEffect then Exit;
  while Opacity < Max do
  begin
    Application.ProcessMessages;
    if Opacity + Step >= Max then
    begin
      Opacity := Max;
      UpdateMainWindow;
      Break;
    end;
    Opacity := Opacity + Step;
    UpdateMainWindow;
    Sleep(Wait);
  end;
end;

procedure TDetailsForm.FadeOutEffect(const Step, Wait, Min: Integer);
begin
  if not EyMainForm.EnableFadeEffect then Exit;
  while Opacity > Min do
  begin
    Application.ProcessMessages;
    if Opacity - Step <= Min then
    begin
      Opacity := Min;
      UpdateMainWindow;
      Break;
    end;
    Opacity := Opacity - Step;
    UpdateMainWindow;
    Sleep(Wait);
  end;
end;

procedure TDetailsForm.HideForm;
begin
  HideFormEffect(OpacityMin);
  Self.Hide;
end;

procedure TDetailsForm.PaintBackground;

  procedure PaintColorizedOverlay;
  begin
    DrawCanvas.DrawImage(OverlayImage,
      BackgrndLbl.Left + (7 * ScaleWidth) - ScaleWidth - 0.18,
      BackgrndLbl.Top + (3 * ScaleHeight),
      OverlayImage.GetWidth * ScaleWidth,
      OverlayImage.GetHeight * ScaleHeight);
  end;

  procedure PaintColorized(Color: Cardinal; Alpha: Byte);
  const
    CMatrix: ColorMatrix = (
      (1.0, 0.0, 0.0, 0.0, 1.0),
      (0.0, 1.0, 0.0, 0.0, 0.0),
      (0.0, 0.0, 1.0, 0.0, 0.0),
      (0.0, 0.0, 0.0, 1.0, 0.0),
      (0.0, 0.0, 0.0, 0.0, 1.0)
    );
  var
    Attr: TGPImageAttributes;
    Matrix: ColorMatrix;
  begin

    Matrix := CMatrix;

    Matrix[3, 3] := (Alpha / 255);
    Matrix[0, 0] := 2 * (GetRValue(Color) / 255);
    Matrix[1, 1] := 2 * (GetGValue(Color) / 255);
    Matrix[2, 2] := 2 * (GetBValue(Color) / 255);

    Attr := TGPImageAttributes.Create;
    try
      Attr.SetColorMatrix(Matrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap);

      DrawCanvas.DrawImage(ColorizedImage,
        MakeRect(BackgrndLbl.Left, BackgrndLbl.Top,
        ColorizedImage.GetWidth * ScaleWidth,
        ColorizedImage.GetHeight * ScaleHeight),  // dest rect
        0, 0, ColorizedImage.GetWidth, ColorizedImage.GetHeight,       // source rect
        UnitPixel,
        Attr);

      PaintColorizedOverlay;

    finally
      Attr.Free;
    end;

  end;

begin
  if BackgrndLbl.Visible then
    PaintColorized(BackColor, BackOpacity);
end;

procedure TDetailsForm.PaintHeaderText;
var
  WideText: WideString;
begin
  if HeaderTextLbl.Visible then
  begin
    WideText := 'Detaylar...';
    PaintLabelTo(DrawCanvas, HeaderTextLbl, WideText, StringAlignmentNear, $FFFFFFFF);
  end;
end;

procedure TDetailsForm.ReleaseHandle;
begin
  if Assigned(MainBuffer) then FreeAndNil(MainBuffer);
  if Assigned(DrawCanvas) then FreeAndNil(DrawCanvas);
end;

procedure TDetailsForm.ShowForm;
begin
  LoadDetails;
  UpdateLayered;
  Self.Show;
  ShowFormEffect(OpacityMax);
end;

procedure TDetailsForm.UpdateLayered;
begin

  Updating := True;
  try
    ReleaseHandle;
    AllocateHandle;
    PaintBackground;
    PaintButtons;
    PaintHeaderText;
    PaintControlLabel;
    PaintHeaderLabel;
    PaintDataLabels;
    PaintDataCount;
    PaintDescription;
    UpdateMainWindow;
  finally
    Updating := False;
  end;
end;

procedure TDetailsForm.UpdateMainWindow;
var
  ScrDC, MemDC: HDC;
  BitmapHandle, PrevBitmap: HBITMAP;
  BlendFunc: _BLENDFUNCTION;
  Size: TSize;
  P, S: TPoint;
begin

  ScrDC := CreateCompatibleDC(0);
  MemDC := CreateCompatibleDC(ScrDC);

  MainBuffer.GetHBITMAP(0, BitmapHandle);
  PrevBitmap := SelectObject(MemDC, BitmapHandle);
  Size.cx := Width;
  Size.cy := Height;
  P := Point(Left, Top);
  S := Point(0, 0);

  with BlendFunc do
  begin
    BlendOp := AC_SRC_OVER;
    BlendFlags := 0;
    SourceConstantAlpha := Opacity;
    AlphaFormat := AC_SRC_ALPHA;
  end;

  UpdateLayeredWindow(Handle, ScrDC, @P, @Size, MemDC, @S, 0,
    @BlendFunc, ULW_ALPHA);

  SelectObject(MemDC, PrevBitmap);
  DeleteObject(BitmapHandle);

  DeleteDC(MemDC);
  DeleteDC(ScrDC);

end;

procedure TDetailsForm.FormCreate(Sender: TObject);
begin

  HibernateMenu.Caption := strHibernateMenu;

  ScaleWidth := 1.18;
  ScaleHeight := 1.24;

  SetFormStyleEx;

  CloseImage := TGPBitmap.Create(UIPath + CloseLeaveImage);
  CancelImageEnter := TGPBitmap.Create(UIPath + CancelEnterImage);
  CancelImageLeave := TGPBitmap.Create(UIPath + CancelLeaveImage);
  CancelImage := CancelImageLeave;

  ColorizedImage := TGPBitmap.Create(UIPath + Background6Image);
  OverlayImage := TGPBitmap.Create(UIPath + Overlay6Image);

  Self.Left := IniFile.ReadInteger(sDetails, sLeft, 167);
  Self.Top := IniFile.ReadInteger(sDetails, sTop, 176);
  Self.Width := Round(ColorizedImage.GetWidth * ScaleWidth);
  Self.Height := Round(ColorizedImage.GetHeight * ScaleHeight);

  LoadOptions;

  OpacityMin := DefOpacityMin;
  OpacityMax := DefOpacityMax;

  if EyMainForm.EnableFadeEffect then Opacity := OpacityMin
  else Opacity := OpacityMax;

  UpdateLayered;

end;

procedure TDetailsForm.FormDestroy(Sender: TObject);
begin
  MouseTimer.OnTimer := nil;
  SaveOptions;
  ReleaseDrawItems;
  CancelImageEnter.Free;
  CancelImageLeave.Free;
  CloseImage.Free;
  OverlayImage.Free;
  ColorizedImage.Free;
  ReleaseHandle;
end;

procedure TDetailsForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  R: TRect;
begin

  if (Button = mbLeft) and (ssShift in Shift) then
  begin
    HibernateActionExecute(Self);
    Exit;
  end;

  if Button = mbLeft then
  begin
    ReleaseCapture;
    SendMessage( Handle, WM_SYSCOMMAND, SC_DRAGMOVE, 0 );
    GetWindowRect(Handle, R);
    Left := R.Left;
    Top := R.Top;
    UpdateLayered;
  end;
end;

procedure TDetailsForm.IniTDetailsForm;
begin
  LoadOptions;
end;

procedure TDetailsForm.DoneDetailsForm;
begin
  SaveOptions;
end;

procedure TDetailsForm.HideFormEffect(const Min: Integer);
begin
  if EyMainForm.EnableFadeEffect then
    FadeOutEffect(DefOpacityStep, DefOpacityWait, Min)
  else begin
    Opacity := Min;
    UpdateMainWindow;
  end;
end;

procedure TDetailsForm.ShowFormEffect(const Max: Integer);
begin
  if EyMainForm.EnableFadeEffect then
    FadeInEffect(DefOpacityStep, DefOpacityWait, Max)
  else begin
    Opacity := Max;
    UpdateMainWindow;
  end;
end;

procedure TDetailsForm.Hibernate;
begin
  OpacityMax := DefOpacityMax div 2;
  HideFormEffect(OpacityMax);
end;

procedure TDetailsForm.Wakeup;
begin
  OpacityMax := DefOpacityMax;
  ShowFormEffect(OpacityMax);
end;

procedure TDetailsForm.PaintButtons;
begin
  if CloseBtn.Visible then
  begin
    DrawImageTo(DrawCanvas, CloseBtn.Left, CloseBtn.Top,
      CloseImage.GetWidth, CloseImage.GetHeight, CloseImage);
  end;
end;

procedure TDetailsForm.LoadOptions;
begin

  SetWorkArea;

  ReleaseDrawItems;

  InGridColor := IniFile.ReadInteger(sDetails, sInGridColor, Integer($8F808080));
  InLineColor := IniFile.ReadInteger(sDetails, sInLineColor, Integer($FF00FF00));
  InBackColor := IniFile.ReadInteger(sDetails, sInBackColor, Integer($70000000));

  OutGridColor := IniFile.ReadInteger(sDetails, sOutGridColor, Integer($8F808080));
  OutLineColor := IniFile.ReadInteger(sDetails, sOutLineColor, Integer($FFFF8000));
  OutBackColor := IniFile.ReadInteger(sDetails, sOutBackColor, Integer($55000000));

  BackColor := IniFile.ReadInteger(sDetails, sBackColor, Integer($00C08000));
  BackOpacity := IniFile.ReadInteger(sDetails, sBackOpacity, Integer($E0));

  TextColorNormal := IniFile.ReadInteger(sDetails, sTextColorNormal, Integer($00FFFFFF));
  TextColorExcess := IniFile.ReadInteger(sDetails, sTextColorExcess, Integer($00FFFFC1));
  TextColorHeader := IniFile.ReadInteger(sDetails, sTextColorHeader, Integer($0000FFFF));
  AllocateDrawItems;
  
end;

procedure TDetailsForm.SaveOptions;
begin
  IniFile.WriteInteger(sDetails, sLeft, Self.Left);
  IniFile.WriteInteger(sDetails, sTop, Self.Top);
end;

procedure TDetailsForm.CloseBtnClick(Sender: TObject);
begin
  EyMainForm.ShowDetailsAct.Checked := False;
  UpdateLayered;
  HideForm;
end;

procedure TDetailsForm.SetFormStyleEx;
var
  StyleEx: Cardinal;
begin
  StyleEx := GetWindowLong(Handle, GWL_EXSTYLE);
  if StyleEx and WS_EX_LAYERED = 0 then
    SetWindowLong(Handle, GWL_EXSTYLE, StyleEx or WS_EX_LAYERED);
end;

procedure TDetailsForm.AllocateDrawItems;
begin
  InGridPen := TGPPen.Create(InGridColor);
  InLinePen := TGPPen.Create(InLineColor);
  InBackBrush := TGPSolidBrush.Create(InBackColor);
  OutGridPen := TGPPen.Create(OutGridColor);
  OutLinePen := TGPPen.Create(OutLineColor);
  OutBackBrush := TGPSolidBrush.Create(OutBackColor);
  BarBackBrush := TGPSolidBrush.Create($DC000000);
  BarFillBrush := TGPSolidBrush.Create($40FFFFFF);
  InBarBrush := TGPSolidBrush.Create($DC00FF00);
  OutBarBrush := TGPSolidBrush.Create($DC0000FF);
end;

procedure TDetailsForm.ReleaseDrawItems;
begin
  if Assigned(InBarBrush) then FreeAndNil(InBarBrush);
  if Assigned(OutBarBrush) then FreeAndNil(OutBarBrush);
  if Assigned(BarBackBrush) then FreeAndNil(BarBackBrush);
  if Assigned(BarFillBrush) then FreeAndNil(BarFillBrush);
  if Assigned(InGridPen) then FreeAndNil(InGridPen);
  if Assigned(InLinePen) then FreeAndNil(InLinePen);
  if Assigned(InBackBrush) then FreeAndNil(InBackBrush);
  if Assigned(OutGridPen) then FreeAndNil(OutGridPen);
  if Assigned(OutLinePen) then FreeAndNil(OutLinePen);
  if Assigned(OutBackBrush) then FreeAndNil(OutBackBrush);
end;

procedure TDetailsForm.AlwaysTopActionExecute(Sender: TObject);
var
  Style: Cardinal;
begin

  AlwaysTopAction.Checked := not AlwaysTopAction.Checked;
  {
  Style := GetWindowLong(Self.Handle, GWL_EXSTYLE);
  if AlwaysTopAction.Checked then
    Self.FormStyle := fsStayOnTop
  else Self.FormStyle := fsNormal;
  SetWindowLong(Self.Handle, GWL_EXSTYLE, Style);
  }
  if AlwaysTopAction.Checked then Style := HWND_TOPMOST //HWND_TOP or 
  else Style := HWND_NOTOPMOST;
  {
  SetWindowPos(Application.Handle, Style, 0, 0, 0, 0, SWP_NOMOVE or
      SWP_NOSIZE or SWP_FRAMECHANGED);
  }
  SetWindowPos(Self.Handle, Style, 0, 0, 0, 0, SWP_NOMOVE or
    SWP_NOSIZE or SWP_FRAMECHANGED);

end;

procedure TDetailsForm.SetColorsDrawItems;
begin
  InGridPen.SetColor(InGridColor);
  InLinePen.SetColor(InLineColor);
  InBackBrush.SetColor(InBackColor);
  OutGridPen.SetColor(OutGridColor);
  OutLinePen.SetColor(OutLineColor);
  OutBackBrush.SetColor(OutBackColor);
end;

procedure TDetailsForm.MouseTimerTimer(Sender: TObject);
var
  P: TPoint;
begin
  GetCursorPos(P);
  if PtInRect(Self.BoundsRect, P) then
  begin
    if (GetKeyState(VK_CONTROL) and $8000) <> 0 then
      HibernateActionExecute(Sender);
  end;
end;

procedure TDetailsForm.HibernateActionExecute(Sender: TObject);
begin
  if EyMainForm.AlertShow then Exit;
  HibernateAction.Checked := not HibernateAction.Checked;
  if HibernateAction.Checked then
  begin
    PrevFormStyle := GetWindowLong(Self.Handle, GWL_EXSTYLE);
    if PrevFormStyle and WS_EX_TRANSPARENT = 0 then
    begin
      EyMainForm.CheckHibernateAlert;
      Hibernate;
      SetWindowLong(Self.Handle, GWL_EXSTYLE, PrevFormStyle or WS_EX_TRANSPARENT);
      MouseTimer.Enabled := True;
    end;
  end
  else if PrevFormStyle <> 0 then begin
    MouseTimer.Enabled := False;
    Wakeup;
    SetWindowLong(Self.Handle, GWL_EXSTYLE, PrevFormStyle);
  end;
end;

procedure TDetailsForm.HideActionExecute(Sender: TObject);
begin
  EyMainForm.ShowDetailsActExecute(Sender);
end;

procedure TDetailsForm.PaintControlLabel;
begin
  if ControlLabel.Visible then
  begin
    DrawCanvas.FillRectangle(InBackBrush, MakeRect(ControlLabel.BoundsRect));
    DrawCanvas.DrawRectangle(InGridPen, MakeRect(ControlLabel.BoundsRect));
    {
    PaintLabelTo(DrawCanvas, ControlLabel, ControlLabel.Caption,
      StringAlignmentNear, $FFFFFFFF, True, TextRenderingHintAntiAliasGridFit);
    }
    PaintDisplayName;
    PaintServiceNo;
    PaintRecordDateTime;
    PaintQuota;
  end;
end;

procedure TDetailsForm.LoadDetails;
var
  Index: Integer;
  Period: string;

  function GetPeriodName(I: Integer): string;
  begin
    Result := Format(sPeriodFormat, [I]);
  end;

begin
  for Index := 0 to MAX_ITEM - 1 do
  begin
    Period := GetPeriodName(Index + 1);
    with TableItems[Index] do begin
      Found := IniFile.ReadBool(Period, sFound, False);
      Month := IniFile.ReadString(Period, sMonth, '');
      Year := IniFile.ReadString(Period, sYear, '');
      Download := IniFile.ReadFloat(Period, sDownload, 0.00);
      Upload := IniFile.ReadFloat(Period, sUpload, 0.00);
    end;
  end;
end;

procedure TDetailsForm.PaintHeaderLabel;
var
  AnyText: string;
begin
  if HeaderLabel.Visible then
  begin
    DrawCanvas.FillRectangle(InBackBrush, MakeRect(HeaderLabel.BoundsRect));
    DrawCanvas.DrawRectangle(InGridPen, MakeRect(HeaderLabel.BoundsRect));
    AnyText := 'Dönem';
    PaintLabelTo(DrawCanvas, PeriodHead, AnyText,
      StringAlignmentNear, ColorRefToARGB(TextColorHeader), True,
      TextRenderingHintAntiAliasGridFit);
    AnyText := 'Upload';
    PaintLabelTo(DrawCanvas, UpHead, AnyText,
      StringAlignmentFar, ColorRefToARGB(TextColorHeader), True,
      TextRenderingHintAntiAliasGridFit);
    AnyText := 'Download';
    PaintLabelTo(DrawCanvas, DownHead, AnyText,
      StringAlignmentFar, ColorRefToARGB(TextColorHeader), True,
      TextRenderingHintAntiAliasGridFit);
    AnyText := 'Oran';
    PaintLabelTo(DrawCanvas, RatioLabel, AnyText,
      StringAlignmentFar, ColorRefToARGB(TextColorHeader), True,
      TextRenderingHintAntiAliasGridFit);
  end;
end;

procedure TDetailsForm.PaintDataLabel1;
var
  AnyText: string;
  AnyColor: TGPColor;
begin
  if not GetHasData(0) then
  begin
    Ratio1.ShowHint := False;
    Exit;
  end;
  if DataLabel1.Visible then
  begin
    DrawCanvas.FillRectangle(InBackBrush, MakeRect(DataLabel1.BoundsRect));
    DrawCanvas.DrawRectangle(InGridPen, MakeRect(DataLabel1.BoundsRect));
    AnyColor := GetColor(0);
    AnyText := GetPeriodText(0);
    PaintLabelTo(DrawCanvas, PeriodLabel1, AnyText,
      StringAlignmentNear, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    AnyText := GetUploadText(0);
    PaintLabelTo(DrawCanvas, UpLabel1, AnyText,
      StringAlignmentFar, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    AnyText := GetDownloadText(0);
    PaintLabelTo(DrawCanvas, DownLabel1, AnyText,
      StringAlignmentFar, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    PaintRatio(Ratio1, 0);
  end;
end;

procedure TDetailsForm.PaintDataLabel2;
var
  AnyText: string;
  AnyColor: TGPColor;
begin
  if not GetHasData(1) then
  begin
    Ratio2.ShowHint := False;
    Exit;
  end;
  if DataLabel2.Visible then
  begin
    DrawCanvas.FillRectangle(OutBackBrush, MakeRect(DataLabel2.BoundsRect));
    DrawCanvas.DrawRectangle(OutGridPen, MakeRect(DataLabel2.BoundsRect));
    AnyText := GetPeriodText(1);
    AnyColor := GetColor(1);
    PaintLabelTo(DrawCanvas, PeriodLabel2, AnyText,
      StringAlignmentNear, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    AnyText := GetUploadText(1);
    PaintLabelTo(DrawCanvas, UpLabel2, AnyText,
      StringAlignmentFar, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    AnyText := GetDownloadText(1);
    PaintLabelTo(DrawCanvas, DownLabel2, AnyText,
      StringAlignmentFar, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    PaintRatio(Ratio2, 1);
  end;
end;

procedure TDetailsForm.PaintDataLabel3;
var
  AnyText: string;
  AnyColor: TGPColor;
begin
  if not GetHasData(2) then
  begin
    Ratio3.ShowHint := False;
    Exit;
  end;
  if DataLabel3.Visible then
  begin
    DrawCanvas.FillRectangle(InBackBrush, MakeRect(DataLabel3.BoundsRect));
    DrawCanvas.DrawRectangle(InGridPen, MakeRect(DataLabel3.BoundsRect));
    AnyText := GetPeriodText(2);
    AnyColor := GetColor(2);
    PaintLabelTo(DrawCanvas, PeriodLabel3, AnyText,
      StringAlignmentNear, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    AnyText := GetUploadText(2);
    PaintLabelTo(DrawCanvas, UpLabel3, AnyText,
      StringAlignmentFar, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    AnyText := GetDownloadText(2);
    PaintLabelTo(DrawCanvas, DownLabel3, AnyText,
      StringAlignmentFar, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    PaintRatio(Ratio3, 2);
  end;
end;

procedure TDetailsForm.PaintDataLabel4;
var
  AnyText: string;
  AnyColor: TGPColor;
begin
  if not GetHasData(3) then
  begin
    Ratio4.ShowHint := False;
    Exit;
  end;
  if DataLabel4.Visible then
  begin
    DrawCanvas.FillRectangle(OutBackBrush, MakeRect(DataLabel4.BoundsRect));
    DrawCanvas.DrawRectangle(OutGridPen, MakeRect(DataLabel4.BoundsRect));
    AnyText := GetPeriodText(3);
    AnyColor := GetColor(3);
    PaintLabelTo(DrawCanvas, PeriodLabel4, AnyText,
      StringAlignmentNear, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    AnyText := GetUploadText(3);
    PaintLabelTo(DrawCanvas, UpLabel4, AnyText,
      StringAlignmentFar, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    AnyText := GetDownloadText(3);
    PaintLabelTo(DrawCanvas, DownLabel4, AnyText,
      StringAlignmentFar, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    PaintRatio(Ratio4, 3);
  end;
end;

procedure TDetailsForm.PaintDataLabel5;
var
  AnyText: string;
  AnyColor: TGPColor;
begin
  if not GetHasData(4) then
  begin
    Ratio5.ShowHint := False;
    Exit;
  end;
  if DataLabel5.Visible then
  begin
    DrawCanvas.FillRectangle(InBackBrush, MakeRect(DataLabel5.BoundsRect));
    DrawCanvas.DrawRectangle(InGridPen, MakeRect(DataLabel5.BoundsRect));
    AnyText := GetPeriodText(4);
    AnyColor := GetColor(4);
    PaintLabelTo(DrawCanvas, PeriodLabel5, AnyText,
      StringAlignmentNear, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    AnyText := GetUploadText(4);
    PaintLabelTo(DrawCanvas, UpLabel5, AnyText,
      StringAlignmentFar, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    AnyText := GetDownloadText(4);
    PaintLabelTo(DrawCanvas, DownLabel5, AnyText,
      StringAlignmentFar, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    PaintRatio(Ratio5, 4);
  end;
end;

procedure TDetailsForm.PaintDataLabel6;
var
  AnyText: string;
  AnyColor: TGPColor;
begin
  if not GetHasData(5) then
  begin
    Ratio6.ShowHint := False;
    Exit;
  end;
  if DataLabel6.Visible then
  begin
    DrawCanvas.FillRectangle(OutBackBrush, MakeRect(DataLabel6.BoundsRect));
    DrawCanvas.DrawRectangle(OutGridPen, MakeRect(DataLabel6.BoundsRect));
    AnyText := GetPeriodText(5);
    AnyColor := GetColor(5);
    PaintLabelTo(DrawCanvas, PeriodLabel6, AnyText,
      StringAlignmentNear, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    AnyText := GetUploadText(5);
    PaintLabelTo(DrawCanvas, UpLabel6, AnyText,
      StringAlignmentFar, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    AnyText := GetDownloadText(5);
    PaintLabelTo(DrawCanvas, DownLabel6, AnyText,
      StringAlignmentFar, AnyColor, True, TextRenderingHintAntiAliasGridFit);
    PaintRatio(Ratio6, 5);
  end;
end;

procedure TDetailsForm.PaintDescription;
begin
  if DescLbl.Visible then
  begin
    DescLbl.Caption := '* Download/Kota oranıdır.';
    PaintLabelTo(DrawCanvas, DescLbl, DescLbl.Caption,
      StringAlignmentFar, ColorRefToARGB(TextColorNormal), True,
      TextRenderingHintAntiAliasGridFit);
  end;
end;

function TDetailsForm.GetServiceNoText: string;
var
  AnyText: string;
begin
  if EyMainForm.ServiceNoStr <> '' then
    AnyText := EyMainForm.ServiceNoStr
  else AnyText := 'Bilinmiyor';
  Result := Format(' : %s', [AnyText])
end;

function TDetailsForm.GetPeriodText(const Index: Integer): WideString;
begin
  Result := '-Veri Yok-';
  if (Index < 0) or (Index > MAX_ITEM-1) then Exit;
  if TableItems[Index].Found then
  begin
    with TableItems[Index] do
      Result := Year + ', ' + Month;
  end;
end;

function TDetailsForm.GetDownloadText(const Index: Integer): string;
begin
  Result := '--';
  if (Index < 0) or (Index > MAX_ITEM-1) then Exit;
  if TableItems[Index].Found then
  begin
    with TableItems[Index] do
      Result := BytesToGigaBytes(Download);
  end;
end;

function TDetailsForm.GetUploadText(const Index: Integer): string;
begin
  Result := '--';
  if (Index < 0) or (Index > MAX_ITEM-1) then Exit;
  if TableItems[Index].Found then
  begin
    with TableItems[Index] do
      Result := BytesToGigaBytes(Upload);
  end;
end;

function TDetailsForm.GetHasData(const Index: Integer): Boolean;
begin
  Result := False;
  if (Index < 0) or (Index > MAX_ITEM-1) then Exit;
  Result := TableItems[Index].Found;
end;

procedure TDetailsForm.UpdateDetails;
begin
  LoadOptions;
  LoadDetails;
  UpdateLayered;
end;

function TDetailsForm.GetDownloadAsByte(const Index: Integer): Double;
begin
  Result := 0.0;
  if (Index < 0) or (Index > MAX_ITEM-1) then Exit;
  if TableItems[Index].Found then
  begin
    with TableItems[Index] do
      Result := Download;
  end;
end;

function TDetailsForm.GetUploadAsByte(const Index: Integer): Double;
begin
  Result := 0.0;
  if (Index < 0) or (Index > MAX_ITEM-1) then Exit;
  if TableItems[Index].Found then
  begin
    with TableItems[Index] do
      Result := Upload;
  end;
end;

procedure TDetailsForm.PaintServiceNo;
begin
  if ServiceNoTitleLbl.Visible then
  begin
    ServiceNoTitleLbl.Caption := 'Hizmet No';
    PaintLabelTo(DrawCanvas, ServiceNoTitleLbl, ServiceNoTitleLbl.Caption,
      StringAlignmentFar, ColorRefToARGB(TextColorNormal), True,
      TextRenderingHintAntiAliasGridFit);
    ServiceNoLbl.Caption := GetServiceNoText();
    PaintLabelTo(DrawCanvas, ServiceNoLbl, ServiceNoLbl.Caption,
      StringAlignmentNear, ColorRefToARGB(TextColorNormal), True,
      TextRenderingHintAntiAliasGridFit);
  end;
end;

procedure TDetailsForm.PaintRecordDateTime;
begin
  if RecordTitleLbl.Visible then
  begin
    PaintLabelTo(DrawCanvas, RecordTitleLbl, RecordTitleLbl.Caption,
      StringAlignmentFar, ColorRefToARGB(TextColorNormal), True,
      TextRenderingHintAntiAliasGridFit);
    RecordLbl.Caption := GetRecordDateTimeText();
    PaintLabelTo(DrawCanvas, RecordLbl, RecordLbl.Caption,
      StringAlignmentNear, ColorRefToARGB(TextColorNormal), True,
      TextRenderingHintAntiAliasGridFit);
  end;
end;

function TDetailsForm.GetRecordDateTimeText: string;
var
  AnyText: string;
begin
  if EyMainForm.RecordTimeStr <> '' then
    AnyText := EyMainForm.RecordTimeStr
  else AnyText := 'Bilinmiyor';
  Result := Format(' : %s', [AnyText])
end;

procedure TDetailsForm.PaintQuota;
begin
  if QuotaTitleLbl.Visible then
  begin
    //QuotaTitleLbl.Caption := 'Kota';
    PaintLabelTo(DrawCanvas, QuotaTitleLbl, QuotaTitleLbl.Caption,
      StringAlignmentFar, ColorRefToARGB(TextColorNormal), True,
      TextRenderingHintAntiAliasGridFit);
    QuotaLbl.Caption := GetQuotaText();
    PaintLabelTo(DrawCanvas, QuotaLbl, QuotaLbl.Caption,
      StringAlignmentNear, ColorRefToARGB(TextColorNormal), True,
      TextRenderingHintAntiAliasGridFit);
  end;
end;

function TDetailsForm.GetQuotaText: string;
var
  AnyText: string;
begin
  if EyMainForm.QuotaAsByte <> 0.00 then
    AnyText := FormatFloat('#,0 GB', EyMainForm.QuotaAsByte / GigaByte) //BytesToGigaBytes(EyMainForm.QuotaAsByte)
  else AnyText := 'Bilinmiyor';
  Result := Format(' : %s', [AnyText])
end;

function TDetailsForm.GetColor(const Index: Integer): TGPColor;
begin
  Result := ColorRefToARGB(TextColorNormal);
  if (Index < 0) or (Index > MAX_ITEM-1) then Exit;
  if TableItems[Index].Found then
  begin
    with TableItems[Index] do
      if Download > EyMainForm.QuotaAsByte then
        Result := ColorRefToARGB(TextColorExcess);
  end;
end;

procedure TDetailsForm.PaintRatio(RatioLabel: TLabel; const DataIndex: Integer);
const
  BarCount = 12;
var
  X, Y, W, H: Integer;
  Percent, Index: Integer;
  Colorize: Cardinal;
  RealPercent: Double;
begin
  if not RatioLabel.ShowHint then RatioLabel.ShowHint := True;
  W := 2;
  H := 8;
  X := RatioLabel.Left + 1;
  Y := RatioLabel.Top + (RatioLabel.Height - H) div 2 + 1;
  Index := 0;
  if EyMainForm.QuotaAsByte <> 0 then
  begin
    RealPercent := GetDownloadAsByte(DataIndex) / EyMainForm.QuotaAsByte;
    Percent := Round(BarCount * (RealPercent));
  end
  else begin
    RealPercent := 0.00;
    Percent := 0;
  end;
  Percent := Math.Min(Percent, BarCount);
  RatioLabel.Hint := FormatFloat('%#,##0.00', RealPercent * 100);
  while Index < BarCount do
  begin
    if Index >= Percent then
      DrawCanvas.FillRectangle(BarFillBrush, MakeRect(X, Y, W, H))
    else begin
      Colorize := MakeColor($FF, $FF, $FF - (($FF div BarCount) * Index), 0);
      BarBackBrush.SetColor(Colorize);
      DrawCanvas.FillRectangle(BarBackBrush, MakeRect(X, Y, W, H));
    end;
    Inc(X, W + 1);
    Inc(Index);
  end;

end;

function TDetailsForm.GetDisplayNameText: WideString;
var
  AnyText: WideString;
begin
  if EyMainForm.DisplayName <> '' then
    AnyText := EyMainForm.DisplayName
  else AnyText := 'Bilinmiyor';
  Result := WideFormat(' : %s', [AnyText])
end;

procedure TDetailsForm.PaintDisplayName;
begin
  if DisplayTitleLbl.Visible then
  begin
    PaintLabelTo(DrawCanvas, DisplayTitleLbl, DisplayTitleLbl.Caption,
      StringAlignmentFar, ColorRefToARGB(TextColorNormal), True,
      TextRenderingHintAntiAliasGridFit);
    DisplayNameLbl.Caption := GetDisplayNameText();
    PaintLabelTo(DrawCanvas, DisplayNameLbl, DisplayNameLbl.Caption,
      StringAlignmentNear, ColorRefToARGB(TextColorNormal), True,
      TextRenderingHintAntiAliasGridFit);
  end;
end;

procedure TDetailsForm.PaintDataCount;
begin
  if CountLabel.Visible then
  begin
    CountLabel.Caption := GetDataCountText();
    PaintLabelTo(DrawCanvas, CountLabel, CountLabel.Caption,
      StringAlignmentNear, ColorRefToARGB(TextColorNormal), True,
      TextRenderingHintAntiAliasGridFit);
  end;
end;

function TDetailsForm.GetDataCountText: WideString;
begin
  if EyMainForm.DataCount <> 0 then
    Result := WideFormat('Toplam %d kayıt.', [EyMainForm.DataCount])
  else Result := 'Kayıt yok!';
end;

procedure TDetailsForm.SetWorkArea;
var
  R: TRect;
begin
  IntersectRect(R, Screen.WorkAreaRect, Self.BoundsRect);
  if IsRectEmpty(R) then
  begin
    Self.Left := Screen.WorkAreaWidth - Self.Width;
    Self.Top := Screen.WorkAreaHeight - Self.Height;
  end;
end;

procedure TDetailsForm.PaintDataLabels;
begin
  PaintDataLabel1;
  PaintDataLabel2;
  PaintDataLabel3;
  PaintDataLabel4;
  PaintDataLabel5;
  PaintDataLabel6;
end;

procedure TDetailsForm.MoveWithActExecute(Sender: TObject);
begin
  MoveWithAct.Checked := not MoveWithAct.Checked;
  EyMainForm.DetailsWith := MoveWithAct.Checked;
  UpdateIniFile;
end;

procedure TDetailsForm.UpdateIniFile;
begin
  IniFile.WriteInteger(sDetails, sLeft, Left);
  IniFile.WriteInteger(sDetails, sTop, Top);
  IniFile.WriteBool(sDetails, sMoveWith, MoveWithAct.Checked);
  IniFile.UpdateFile;
end;

procedure TDetailsForm.TrafficPopupPopup(Sender: TObject);
begin
  MoveWithAct.Checked := EyMainForm.DetailsWith;
end;

end.

