{ ------------------------------------------------------------- }
{ Purpose : NextSuite 6                                         }
{ By      : Bojan Nikolic                                       }
{ For     : BergSoft                                            }
{ ------------------------------------------------------------- }
{ Copyright (C) BergSoft.net 2015. All Rights Reserved.         }
{ ------------------------------------------------------------- }
{$I NxSuite6.inc}
unit NxStyles6;

interface

uses
  { Standard }
  Classes, Types, Windows, Graphics, Controls, StdCtrls, ExtCtrls,
{$IFDEF STYLE_SERVICES}Themes,{$ENDIF}
{$IFDEF UI_TYPES}UITypes,{$ENDIF}
  { Next Standard 6 }
  NxTypes6, NxIntf6, NxClasses6, NxConsts6, NxGraphics6;

type
  TNxStyleOption = (soNativeStyles, soVCLStyles);
  TNxStyleOptions = set of TNxStyleOption;
  TNxStyleDetails = set of (sdSelected, sdFocused);

  { TNxControlStyle6 }

  TNxControlStyle6 = class
  public
  end;

  { TNxPanelStyle6 }

  TNxPanelStyle6 = class
  public
    class function GetFillColor(Control: TControl; DefaultColor: TColor): TColor;
  end;

  { TNxCheckBoxStyledView6 }

  TNxCheckBoxStyledView6 = class
  public
    class procedure Paint(Canvas: TCanvas; Handle: THandle; Dest: TRect;
      ButtonState: TNxButtonState; CheckBoxState: TCheckBoxState;
      Options: TNxStyleOptions);
  end;

  { TNxButtonStyle6 }

  TNxButtonStyle6 = class
  public
    class function GetTextColor(Control: TControl; Color: TColor;
      State: TNxButtonState): TColor;
    class procedure Paint(Canvas: TCanvas; Handle: THandle; Dest: TRect;
      State: TNxButtonState; Options: TNxStyleOptions; PaintParent: Boolean = True);
  end;

  { TNxComboBoxStyle6 }

  TNxComboBoxStyle6 = class
  public
  end;

  { TNxDropButtonStyledView6 }

  TNxDropButtonStyledView6 = class
  public
    class procedure Paint(Canvas: TCanvas; Handle: THandle; Dest: TRect;
      State: TNxButtonState; Options: TNxStyleOptions);
  end;

  { TNxToolButtonStyledView6 }

  TNxToolbarStyle6 = class
  public
    class function StyleClient(Control: TWinControl): Boolean;
    class procedure PaintToolbar(Canvas: TCanvas; Handle: THandle; Rect: TRect;
      Options: TNxStyleOptions);
    class procedure Paint(Canvas: TCanvas; Handle: THandle; Dest: TRect;
      State: TNxButtonState; Options: TNxStyleOptions; EraseBkgnd: Boolean);
  end;

  { TNxTrackBarStyle6 }

  TNxTrackBarStyle6 = class
  public
    class procedure PaintTrack(Canvas: TCanvas; Handle: THandle; TrackRect: TRect;
      Options: TNxStyleOptions);
    class procedure PaintThumb(Canvas: TCanvas; Handle: THandle; Dest: TRect;
      State: TNxButtonState; Options: TNxStyleOptions);
  end;

  { TNxProgressStyledView6 }

  TNxProgressStyledView6 = class
    class procedure Paint(Canvas: TCanvas; Handle: THandle; Rect: TRect; Options: TNxStyleOptions);
    class procedure PaintChunk(Canvas: TCanvas; Handle: THandle; Rect: TRect; Options: TNxStyleOptions);
  end;

  { TNxHeaderStyledView6 }

  TNxHeaderStyle6 = class
    class function GetTextColor(Control: TControl; Color: TColor;
      State: TNxButtonState): TColor;
    class procedure Paint(Canvas: TCanvas; Handle: THandle; Dest: TRect;
      State: TNxButtonState; Options: TNxStyleOptions);
    class procedure PaintInactive(Canvas: TCanvas; Handle: THandle; Dest: TRect;
      Options: TNxStyleOptions);
  end;

  { TNxTreeStyledView6 }

  TNxTreeStyledView6 = class
    class function GetFillColor(Control: TControl; DefaultColor: TColor): TColor;
    class function GetTextColor(Control: TControl; Details: TNxStyleDetails;
      DefaultColor: TColor): TColor;
    class procedure PaintToggle(Canvas: TCanvas; Handle: THandle; Dest: TRect;
      Expanded: Boolean; State: TNxButtonState; Options: TNxStyleOptions);
  end;

  { TNxPageControlStyle6 }

  TNxPageControlStyle6 = class
  public
    class procedure PaintBackground(Canvas: TCanvas; Handle: THandle; Dest: TRect;
      Options: TNxStyleOptions);
    class procedure PaintButton(Canvas: TCanvas; Handle: THandle; Dest: TRect;
      State: TNxButtonState; Options: TNxStyleOptions);
    class procedure PaintTab(Canvas: TCanvas; Handle: THandle; Dest: TRect;
      State: TNxButtonState; Options: TNxStyleOptions; TabsLocation: TNxTabsLocation);
  end;

  { TNxUserDefinedStyle6 }

  TNxUserDefinedStyle = class(TNxNotifyPersistent6)
  private
    FBorderColor: TColor;
    FColor: TColor;
    procedure SetBorderColor(const Value: TColor);
    procedure SetColor(const Value: TColor);
  public
    constructor Create;
    procedure DrawToCanvas(const Canvas: TCanvas; const Rect: TRect); virtual;
  published
    property BorderColor: TColor read FBorderColor write SetBorderColor;
    property Color: TColor read FColor write SetColor;
  end;

  { TNxButtonUserDefinedStyle6 }

  TNxButtonUserDefinedStyle6 = class(TNxNotifyPersistent6)
  private
    FCheckedStyle: TNxUserDefinedStyle;
    FFlat: Boolean;
    FHotStyle: TNxUserDefinedStyle;
    FPushedStyle: TNxUserDefinedStyle;
    procedure SetCheckedStyle(const Value: TNxUserDefinedStyle);
    procedure SetFlat(const Value: Boolean);
    procedure SetHotStyle(const Value: TNxUserDefinedStyle);
    procedure SetPushedStyle(const Value: TNxUserDefinedStyle);
  public
    constructor Create; virtual;
    destructor Destroy; override;
    { Methods }
    procedure DrawToCanvas(const Canvas: TCanvas; const Rect: TRect;
      const State: TNxButtonState); virtual;
  published
    property CheckedStyle: TNxUserDefinedStyle read FCheckedStyle write SetCheckedStyle;
    property Flat: Boolean read FFlat write SetFlat default True;
    property HotStyle: TNxUserDefinedStyle read FHotStyle write SetHotStyle;
    property PushedStyle: TNxUserDefinedStyle read FPushedStyle write SetPushedStyle;
  end;

  function SupportStyle(Options: TNxStyleOptions): Boolean;

implementation

uses
  Math,
{$IFDEF THEMES}
  UxTheme
{$ELSE}
  NxTheme6
{$ENDIF}
;

function StyleServicesEnabled: Boolean;
begin
{$IFDEF STYLE_SERVICES}
  Result := StyleServices.Enabled and StyleServices.Available;
{$ELSE}
  Result := False;
{$ENDIF}
end;

function SupportStyle(Options: TNxStyleOptions): Boolean;
begin
  Result := False;
  if (soVCLStyles in Options)
    and StyleServicesEnabled then
  begin
    Result := True;
    Exit;
  end;
  if (soNativeStyles in Options)
    and Themed then
  begin
    Result := True;
    Exit;
  end;
end;

{ TNxControlStyle6 }

{ TNxCheckBoxStyledView6 }

class procedure TNxCheckBoxStyledView6.Paint(Canvas: TCanvas; Handle: THandle; Dest: TRect;
  ButtonState: TNxButtonState; CheckBoxState: TCheckBoxState;
  Options: TNxStyleOptions);
var
  Index: Integer;
  Theme: HTHEME;
{$IFDEF STYLE_SERVICES}
  Detail: TThemedButton;
  Details: TThemedElementDetails;
{$ENDIF}
begin
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    Detail := tbCheckBoxUncheckedNormal;

    case CheckBoxState of
      cbUnchecked:
        begin
          Detail := tbCheckBoxUncheckedNormal;
          if bsHover in ButtonState then Detail := tbCheckBoxUncheckedHot;
          if bsPressed in ButtonState then Detail := tbCheckBoxUncheckedPressed;
          if bsDisabled in ButtonState then Detail := tbCheckBoxUncheckedDisabled;
        end;
      cbChecked:
        begin
          Detail := tbCheckBoxCheckedNormal;
          if bsHover in ButtonState then Detail := tbCheckBoxCheckedHot;
          if bsPressed in ButtonState then Detail := tbCheckBoxCheckedPressed;
          if bsDisabled in ButtonState then Detail := tbCheckBoxCheckedDisabled;
        end;
      cbGrayed:
        begin
          Detail := tbCheckBoxMixedNormal;
          if bsHover in ButtonState then Detail := tbCheckBoxMixedHot;
          if bsPressed in ButtonState then Detail := tbCheckBoxMixedPressed;
          if bsDisabled in ButtonState then Detail := tbCheckBoxMixedDisabled;
        end;
    end;

    Details := StyleServices.GetElementDetails(Detail);

    StyleServices.DrawElement(Canvas.Handle, Details, Dest);

    { Fix bug }
    Canvas.Pen.Color := clNone;
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    { Create Handle }
    Theme := OpenThemeData(Handle, teButton);

    Index := 0;

    case CheckBoxState of
      cbUnchecked:
        begin
          Index := tiRadioNormal;
          if bsHover in ButtonState then Index := tiRadioHover;
          if bsPressed in ButtonState then Index := tiRadioDown;
          if bsDisabled in ButtonState then Index := tiRadioDisabled;
        end;
      cbChecked:
        begin
          Index := tiRadioChecked;
          if bsHover in ButtonState then Index := tiRadioCheckedHover;
          if bsPressed in ButtonState then Index := tiRadioCheckedDown;
          if bsDisabled in ButtonState then Index := tiRadioCheckedDisabled;
        end;
      cbGrayed:
        begin
          Index := tiRadioGrayed;
          if bsHover in ButtonState then Index := tiRadioGrayedHover;
          if bsPressed in ButtonState then Index := tiRadioGrayedDown;
          if bsDisabled in ButtonState then Index := tiRadioGrayedDisabled;
        end;
    end;

    { Draw CheckBox }
    DrawThemeBackground(Theme, Canvas.Handle, tcCheckBox, Index, Dest, nil);

    { Destroy }
    CloseThemeData(Theme);
  end else
  begin
    { Classic Style }
    with Canvas, Dest.TopLeft do
    begin
      if bsPressed in ButtonState
        then Brush.Color := clBtnFace
        else Brush.Color := clWindow;

      Pen.Color := clBtnShadow;

      Rectangle(Dest);

      { Check }
      if CheckBoxState = cbChecked then
      begin
        Pen.Color := clWindowText;
        Polyline([Point(X + 2, Y + 4), Point(X + 4, Y + 6), Point(X + 9, Y + 1)]);
        Polyline([Point(X + 2, Y + 5), Point(X + 4, Y + 7), Point(X + 9, Y + 2)]);
        Polyline([Point(X + 2, Y + 6), Point(X + 4, Y + 8), Point(X + 9, Y + 3)]);
      end;
    end;
  end;
end;

{ TNxPushButtonStyledView6 }

class function TNxButtonStyle6.GetTextColor(Control: TControl;
  Color: TColor; State: TNxButtonState): TColor;
{$IFDEF STYLE_SERVICES}
var
  OutColor: TColor;
  Detail: TThemedButton;
{$ENDIF}
begin
  Result := Color;
  if SupportStyle([soVCLStyles]) then
  begin
{$IFDEF STYLE_SERVICES}
    if seFont in Control.StyleElements then
    begin
      with StyleServices do
      begin
        Detail := tbPushButtonNormal;
        if bsSelected in State then Detail := tbPushButtonDefaulted;
        if bsHover in State then Detail := tbPushButtonHot;
        if bsPressed in State then Detail := tbPushButtonPressed;
        if not Control.Enabled then Detail := tbPushButtonDisabled;

        if GetElementColor(GetElementDetails(Detail), ecTextColor, OutColor) then
        begin
          if Result <> clNone then Result := OutColor;
        end;
      end;
    end;
{$ENDIF}
  end;
end;

class procedure TNxButtonStyle6.Paint(Canvas: TCanvas; Handle: THandle;
  Dest: TRect; State: TNxButtonState; Options: TNxStyleOptions; PaintParent: Boolean);
var
  Index, Flags: Integer;
  Theme: HTHEME;
{$IFDEF STYLE_SERVICES}
  Detail: TThemedButton;
{$ENDIF}
begin
  { Note: Button doesn't fully fill ClientRectangle
          (both Delphi & Native Styles) }
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    Detail := tbPushButtonNormal;
    if bsSelected in State then Detail := tbPushButtonDefaulted;
    if bsHover in State then Detail := tbPushButtonHot;
    if bsPressed in State then Detail := tbPushButtonPressed;
    if bsChecked in State then Detail := tbPushButtonPressed;
    if bsDisabled in State then Detail := tbPushButtonDisabled;

    if PaintParent then
      with StyleServices do
      begin
        DrawParentBackground(Handle, Canvas.Handle, nil, True, Dest);
      end;

    StyleServices.DrawElement(Canvas.Handle,
      StyleServices.GetElementDetails(Detail), Dest);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    with Canvas do
    begin
      FillRect(Dest);
    end;
    InflateRect(Dest, 1, 1);

    { Create Handle }
    Theme := OpenThemeData(Handle, NxConsts6.teButton);

    Index := 0;
    if bsHover in State then Index := tcToolbarButtonHover;
    if bsPressed in State then Index := tcToolbarButtonDown;
    if bsChecked in State then Index := tcToolbarButtonDown;
    if bsDisabled in State then Index := tcToolbarButtonDown;

    { Draw }
    DrawThemeBackground(Theme, Canvas.Handle, tcButton, Index, Dest, nil);

    { Destroy }
    CloseThemeData(Theme);
  end else
  begin
    if (bsPressed in State)
      or (bsChecked in State)
    then
      with Canvas do
      begin
        Brush.Color := clBtnFace;
        Pen.Color := clBtnShadow;
        Rectangle(Dest);
      end
    else
      begin
        Flags := DFCS_ADJUSTRECT or DFCS_BUTTONPUSH;
        if bsDisabled in State then Flags := Flags or DFCS_INACTIVE;
        DrawFrameControl(Canvas.Handle, Dest, DFC_BUTTON, Flags)
      end;
  end;
end;

{ TNxDropButtonStyledView6 }

class procedure TNxDropButtonStyledView6.Paint(Canvas: TCanvas; Handle: THandle;
  Dest: TRect; State: TNxButtonState; Options: TNxStyleOptions);
var
  Index, Flags: Integer;
  Theme: HTHEME;
{$IFDEF STYLE_SERVICES}
  Detail: TThemedComboBox;
{$ENDIF}
begin
  { Note: Button doesn't fully fill ClientRectangle
          (both Delphi & Native Styles) }
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    Detail := tcDropDownButtonNormal;
    if bsHover in State then Detail := tcDropDownButtonHot;
    if bsPressed in State then Detail := tcDropDownButtonPressed;
    if bsChecked in State then Detail := tcDropDownButtonPressed;
    if bsDisabled in State then Detail := tcDropDownButtonDisabled;

    StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(Detail), Dest);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    { Create Handle }
    Theme := OpenThemeData(Handle, NxConsts6.teComboBox);

    Index := 0;
    if bsHover in State then Index := tcToolbarButtonHover;
    if bsPressed in State then Index := tcToolbarButtonDown;
    if bsChecked in State then Index := tcToolbarButtonDown;
    if bsDisabled in State then Index := tcToolbarButtonDown;

    { Draw }
    DrawThemeBackground(Theme, Canvas.Handle, tcButton, Index, Dest, nil);

    { Destroy }
    CloseThemeData(Theme);
  end else
  begin
    { Classic Drawing }
    Flags := DFCS_SCROLLCOMBOBOX or DFCS_ADJUSTRECT;
    with Canvas do
    begin
      if bsPressed in State then Flags := Flags or DFCS_PUSHED;
      if bsDisabled in State then Flags := Flags or DFCS_INACTIVE;
      DrawFrameControl(Canvas.Handle, Dest, DFC_SCROLL, Flags)
    end;
  end;
end;

{ TNxToolButtonStyledView6 }

class procedure TNxToolbarStyle6.Paint(Canvas: TCanvas; Handle: THandle;
  Dest: TRect; State: TNxButtonState; Options: TNxStyleOptions; EraseBkgnd: Boolean);
var
  Index: Integer;
  Theme: HTHEME;
{$IFDEF STYLE_SERVICES}
  Detail: TThemedToolBar;
{$ENDIF}
begin
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    if EraseBkgnd then Canvas.FillRect(Dest);

    Detail := ttbButtonNormal;
    if bsHover in State then Detail := ttbButtonHot;
    if bsPressed in State then Detail := ttbButtonPressed;
    if bsChecked in State then Detail := ttbButtonPressed;
    if bsDisabled in State then Detail := ttbButtonDisabled;

    StyleServices.DrawElement(Canvas.Handle,
      StyleServices.GetElementDetails(Detail), Dest);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    if EraseBkgnd then Canvas.FillRect(Dest);

    { Create Handle }
    Theme := OpenThemeData(Handle, NxConsts6.teToolbar);

    Index := 0;
    if bsHover in State then Index := tcToolbarButtonHover;
    if bsPressed in State then Index := tcToolbarButtonDown;
    if bsChecked in State then Index := tcToolbarButtonDown;
    if bsDisabled in State then Index := tcToolbarButtonDisabled;

    { Draw }
    DrawThemeBackground(Theme, Canvas.Handle, tcButton, Index, Dest, nil);

    { Destroy }
    CloseThemeData(Theme);
  end else
  begin
    if (bsHover in State)
      or (bsChecked in State) then
    begin
      if (bsPressed in State)
        or (bsChecked in State)
      then
        Frame3D(Canvas, Dest, clBtnShadow, clBtnHighlight, 1)
      else
        Frame3D(Canvas, Dest, clBtnHighlight, clBtnShadow, 1);
    end;
  end;
end;

class function TNxToolbarStyle6.StyleClient(Control: TWinControl): Boolean;
begin
{$IFDEF STYLE_SERVICES}
  Result := seClient in Control.StyleElements;
{$ELSE}
  Result := False;
{$ENDIF}
end;

class procedure TNxToolbarStyle6.PaintToolbar(Canvas: TCanvas; Handle: THandle;
  Rect: TRect;
  Options: TNxStyleOptions);
var
  Theme: HTHEME;
{$IFDEF STYLE_SERVICES}
  Detail: TThemedToolBar;
{$ENDIF}
begin
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    Detail := ttbToolBarRoot;
    StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(Detail), Rect);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    Theme := OpenThemeData(Handle, NxConsts6.teToolbar);
    DrawThemeBackground(Theme, Canvas.Handle, tcToolbarRoot, 0, Rect, nil);
    CloseThemeData(Theme);
  end else Canvas.FillRect(Rect);
end;

{ TNxTrackBarStyledView6 }

class procedure TNxTrackBarStyle6.PaintThumb(Canvas: TCanvas;
  Handle: THandle; Dest: TRect; State: TNxButtonState;
  Options: TNxStyleOptions);
var
  Theme: HTHEME;
  SliderState, w: Integer;
{$IFDEF STYLE_SERVICES}
  Detail: TThemedTrackBar;
{$ENDIF}
begin
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    Detail := ttbThumbNormal;
    if bsHover in State then Detail := ttbThumbHot;
    if bsPressed in State then Detail := ttbThumbPressed;
    if bsChecked in State then Detail := ttbThumbPressed;
    if bsDisabled in State then Detail := ttbThumbDisabled;
    StyleServices.DrawElement(Canvas.Handle,
      StyleServices.GetElementDetails(Detail), Dest);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    Theme := OpenThemeData(Handle, NxConsts6.teTrackBar);

    SliderState := tiTrackbarSliderNormal;
    if bsHover in State then SliderState := tiTrackbarSliderHot;
    if bsPressed in State then SliderState := tiTrackbarSliderPressed;
    if bsDisabled in State then SliderState := tiTrackbarSliderDisabled;

    { Draw Slider }
    DrawThemeBackground(Theme, Canvas.Handle, tcTrackbarSliderRectangular,
      SliderState, Dest, nil);

    CloseThemeData(Theme);
  end else
  begin
    with Canvas, Dest do
    begin
      w := Right - Left;
      Brush.Color := clBtnFace;
      Pen.Color := clBtnFace;
      Polygon([Point(Left, Top),
        Point(Right, Top), Point(Right, Bottom - w div 2),
        Point(Left + w div 2, Bottom), Point(Left, Bottom - w div 2),
        Point(Left, Top)]);
      Pen.Color := clBtnHighlight;
      MoveTo(Right - 1, Top);
      LineTo(Left, Top);
      LineTo(Left, Bottom - w div 2);
      LineTo(Left + w div 2, Bottom);
      Pen.Color := cl3DDkShadow;
      LineTo(Right, Bottom - w div 2);
      LineTo(Right, Top - 1);
      Pen.Color := cl3DLight;
      MoveTo(Right - 2, Top + 1);
      LineTo(Left + 1, Top + 1);
      LineTo(Left + 1, Bottom - w div 2);
      LineTo(Left + w div 2, Bottom - 1);
      Pen.Color := clBtnShadow;
      LineTo(Right - 1, Bottom - w div 2);
      LineTo(Right - 1, Top);
    end;
  end;

end;

class procedure TNxTrackBarStyle6.PaintTrack(Canvas: TCanvas; Handle: THandle; TrackRect: TRect;
  Options: TNxStyleOptions);
var
  Theme: HTHEME;
begin
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    StyleServices.DrawElement(Canvas.Handle,
      StyleServices.GetElementDetails(ttbTrack), TrackRect);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    Theme := OpenThemeData(Handle, NxConsts6.teTrackBar);
    DrawThemeBackground(Theme, Canvas.Handle, tcTrackbarRail,
      1, TrackRect, nil);
    CloseThemeData(Theme);
  end else
  begin
    with Canvas do
    begin
      Frame3D(Canvas, TrackRect, clBtnHighlight, clBtnShadow, 1);
      Brush.Color := clBtnFace;
      FillRect(TrackRect);
    end;
  end;

end;

{ TNxProgressStyledView6 }

class procedure TNxProgressStyledView6.Paint(Canvas: TCanvas; Handle: THandle;
  Rect: TRect; Options: TNxStyleOptions);
var
  Theme: HTHEME;
{$IFDEF STYLE_SERVICES}
  Detail: TThemedProgress;
{$ENDIF}
begin
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    Detail := tpBar;
    StyleServices.DrawElement(Canvas.Handle,
      StyleServices.GetElementDetails(Detail), Rect);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    Theme := OpenThemeData(Handle, teProgress);
    DrawThemeBackground(Theme, Canvas.Handle, tcBar, 1, Rect, nil);
    CloseThemeData(Theme);
  end else
  begin
    Frame3D(Canvas, Rect, clBtnShadow, clBtnHighlight, 1);
    Canvas.Brush.Color := clBtnFace;
    Canvas.FillRect(Rect);
  end;
end;

class procedure TNxProgressStyledView6.PaintChunk(Canvas: TCanvas; Handle: THandle; Rect: TRect;
  Options: TNxStyleOptions);
var
  Theme: HTHEME;
{$IFDEF STYLE_SERVICES}
  Detail: TThemedProgress;
{$ENDIF}
begin
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    Detail := tpChunk;
    StyleServices.DrawElement(Canvas.Handle,
      StyleServices.GetElementDetails(Detail), Rect);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    Theme := OpenThemeData(Handle, teProgress);
    DrawThemeBackground(Theme, Canvas.Handle, tcChunk, 1, Rect, nil);
    CloseThemeData(Theme);
  end else
  begin

  end;
end;

{ TNxHeaderStyledView6 }

class function TNxHeaderStyle6.GetTextColor(Control: TControl; Color: TColor; State: TNxButtonState): TColor;
{$IFDEF STYLE_SERVICES}
var
  OutColor: TColor;
  Detail: TThemedHeader;
{$ENDIF}
begin
  Result := Color;
  if SupportStyle([soVCLStyles]) then
  begin
{$IFDEF STYLE_SERVICES}
    if seFont in Control.StyleElements then
    begin
      with StyleServices do
      begin
        Detail := thHeaderItemNormal;
        if bsHover in State then Detail := thHeaderItemHot;
        if bsPressed in State then Detail := thHeaderItemPressed;
        if GetElementColor(GetElementDetails(Detail), ecTextColor, OutColor) then
        begin
          if Result <> clNone then Result := OutColor;
        end;
      end;
    end;
{$ENDIF}
  end;
end;

class procedure TNxHeaderStyle6.Paint(Canvas: TCanvas; Handle: THandle;
  Dest: TRect; State: TNxButtonState; Options: TNxStyleOptions);
var
{$IFDEF STYLE_SERVICES}
  Detail: TThemedHeader;
{$ENDIF}
  Index: Integer;
  Theme: HTHEME;
begin
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    Detail := thHeaderItemNormal;
    if bsHover in State then Detail := thHeaderItemHot;
    if bsPressed in State then Detail := thHeaderItemPressed;
    StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(Detail), Dest);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    Theme := OpenThemeData(Handle, teHeader);
    Index := tiHeaderNormal;
    if bsHover in State then Index := tiHeaderHover;
    if bsPressed in State then Index := tiHeaderDown;
    DrawThemeBackground(Theme, Canvas.Handle, tcHeader, Index, Dest, nil);
    CloseThemeData(Theme);
  end else
  begin
    Canvas.Brush.Color := clBtnFace;
    Canvas.FillRect(Dest);
  end;
end;

class procedure TNxHeaderStyle6.PaintInactive(Canvas: TCanvas; Handle: THandle; Dest: TRect; Options: TNxStyleOptions);
var
{$IFDEF STYLE_SERVICES}
  Detail: TThemedHeader;
{$ENDIF}
  Theme: HTHEME;
begin
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    Detail := thHeaderItemNormal;
    StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(Detail), Dest);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    Theme := OpenThemeData(Handle, teHeader);
    DrawThemeBackground(Theme, Canvas.Handle, tcHeader, tiHeaderPasive, Dest, nil);
    CloseThemeData(Theme);
  end else
  begin
    with Canvas do
    begin
      FillRect(Dest);
    end;
  end;
end;

{ TNxTreeStyledView6 }

class function TNxTreeStyledView6.GetFillColor(Control: TControl; DefaultColor: TColor): TColor;
{$IFDEF STYLE_SERVICES}
var
  OutColor: TColor;
  Detail: TThemedTreeview;
{$ENDIF}
begin
  Result := DefaultColor;
  if SupportStyle([soVCLStyles]) then
  begin
{$IFDEF STYLE_SERVICES}
    if seClient in Control.StyleElements then
    begin
      with StyleServices do
      begin
        Detail := ttTreeviewRoot;
        if GetElementColor(GetElementDetails(Detail), ecFillColor, OutColor) then
        begin
          if Result <> clNone then Result := OutColor;
        end;
      end;
    end;
{$ENDIF}
  end;
end;

class function TNxTreeStyledView6.GetTextColor(Control: TControl;
  Details: TNxStyleDetails; DefaultColor: TColor): TColor;
{$IFDEF STYLE_SERVICES}
var
  OutColor: TColor;
  Detail: TThemedTreeview;
{$ENDIF}
begin
  Result := DefaultColor;
  if SupportStyle([soVCLStyles]) then
  begin
{$IFDEF STYLE_SERVICES}
    if seClient in Control.StyleElements then
    begin
      with StyleServices do
      begin
        Detail := ttItemNormal;
        if sdSelected in Details then Detail := ttItemSelected;
        if GetElementColor(GetElementDetails(Detail), ecTextColor, OutColor) then
        begin
          if Result <> clNone then Result := OutColor;
        end;
      end;
    end;
{$ENDIF}
  end;
end;

class procedure TNxTreeStyledView6.PaintToggle(Canvas: TCanvas; Handle: THandle;
  Dest: TRect; Expanded: Boolean; State: TNxButtonState; Options: TNxStyleOptions);
var
{$IFDEF STYLE_SERVICES}
  Detail: TThemedTreeview;
{$ENDIF}
  Index, C: Integer;
  Theme: HTHEME;
begin
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    if Expanded then
      Detail := ttGlyphOpened
    else
      Detail := ttGlyphClosed;
    StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(Detail), Dest);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    Theme := OpenThemeData(Handle, teTreeView);
    if Expanded then Index := tiExpanded else Index := tiCollapsed;
    DrawThemeBackground(Theme, Canvas.Handle, tcExpandingButton, Index, Dest, nil);
    CloseThemeData(Theme);
  end else
  begin
    with Canvas do
    begin
      Pen.Color := clGray;
      Brush.Color := clWhite;
      Rectangle(Dest);
      C := Floor(RectWidth(Dest) / 2);
      Polyline([
        Point(Dest.Left + 2, Dest.Top + C),
        Point(Dest.Right - 2, Dest.Top + C)
      ]);
      if not Expanded then
        Polyline([
          Point(Dest.Left + C, Dest.Top + 2),
          Point(Dest.Left + C, Dest.Bottom - 2)
        ]);
    end;
  end;
end;

{ TNxPanelStyle6 }

class function TNxPanelStyle6.GetFillColor(Control: TControl; DefaultColor: TColor): TColor;
{$IFDEF STYLE_SERVICES}
var
  OutColor: TColor;
  Detail: TThemedPanel;
{$ENDIF}
begin
  Result := DefaultColor;
  if SupportStyle([soVCLStyles]) then
  begin
{$IFDEF STYLE_SERVICES}
    if seClient in Control.StyleElements then
    begin
      with StyleServices do
      begin
        Detail := tpPanelBackground;
        if GetElementColor(GetElementDetails(Detail), ecFillColor, OutColor) then
        begin
          if Result <> clNone then Result := OutColor;
        end;
      end;
    end;
{$ENDIF}
  end;
end;

{ TNxPageControlStyle6 }

class procedure TNxPageControlStyle6.PaintBackground(Canvas: TCanvas; Handle: THandle; Dest: TRect;
  Options: TNxStyleOptions);
{$IFDEF STYLE_SERVICES}
var
  Details: TThemedElementDetails;
{$ENDIF}
begin
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    Details := StyleServices.GetElementDetails(ttPane);
    StyleServices.DrawParentBackground(Handle, Canvas.Handle, Details, False);
{$ENDIF}
  end else
    with Canvas do
    begin
      FillRect(Dest);
    end;
end;

class procedure TNxPageControlStyle6.PaintButton(Canvas: TCanvas; Handle: THandle; Dest: TRect; State: TNxButtonState;
  Options: TNxStyleOptions);
var
  Index: Integer;
  Theme: HTHEME;
{$IFDEF STYLE_SERVICES}
  Detail: TThemedButton;
{$ENDIF}
begin
  { Note: Button doesn't fully fill ClientRectangle
          (both Delphi & Native Styles) }
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    Detail := tbPushButtonNormal;
    if bsSelected in State then Detail := tbPushButtonDefaulted;
    if bsHover in State then Detail := tbPushButtonHot;
    if bsPressed in State then Detail := tbPushButtonPressed;
    if bsChecked in State then Detail := tbPushButtonPressed;
    if bsDisabled in State then Detail := tbPushButtonDisabled;
    StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(Detail), Dest);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    with Canvas do
    begin
      FillRect(Dest);
    end;
    InflateRect(Dest, 1, 1);

    { Create Handle }
    Theme := OpenThemeData(Handle, NxConsts6.teButton);

    Index := 0;
    if bsHover in State then Index := tcToolbarButtonHover;
    if bsPressed in State then Index := tcToolbarButtonDown;
    if bsChecked in State then Index := tcToolbarButtonDown;
    if bsDisabled in State then Index := tcToolbarButtonDown;

    { Draw }
    DrawThemeBackground(Theme, Canvas.Handle, tcButton, Index, Dest, nil);

    { Destroy }
    CloseThemeData(Theme);
  end else
  begin
    if bsPressed in State then Frame3D(Canvas, Dest, clBtnShadow, clBtnHighlight, 1)
    else
    begin
      with Canvas do
      begin
        Brush.Color := clBtnFace;
        FillRect(Dest);
        Pen.Color := clBtnText;
        Brush.Color := clBtnText;
      end;
      Frame3D(Canvas, Dest, clBtnHighlight, clBtnShadow, 1);
    end;

  end;
end;

class procedure TNxPageControlStyle6.PaintTab(Canvas: TCanvas; Handle: THandle; Dest: TRect; State: TNxButtonState;
  Options: TNxStyleOptions; TabsLocation: TNxTabsLocation);
var
  Index, Flag: Integer;
  Theme: HTHEME;
{$IFDEF STYLE_SERVICES}
  Detail: TThemedTab;
{$ENDIF}
begin
  if (soVCLStyles in Options) and StyleServicesEnabled then
  begin
{$IFDEF STYLE_SERVICES}
    Detail := ttTabItemNormal;
    case TabsLocation of
      tbTop:  if bsDisabled in State then Detail := ttTabItemDisabled
              else if bsSelected in State then Detail := ttTabItemSelected
                else if bsHover in State then Detail := ttTabItemHot
                  else Detail := ttTabItemNormal;
      tbLeft:
        begin
          if bsDisabled in State then Detail := ttTabItemLeftEdgeDisabled
          else if bsSelected in State then Detail := ttTabItemLeftEdgeSelected
          else if bsHover in State then Detail := ttTabItemLeftEdgeHot
          else Detail := ttTabItemLeftEdgeNormal;
        end;
      tbBottom:
        begin
          if bsDisabled in State then Detail := ttTabItemBothEdgeDisabled
          else if bsSelected in State then Detail := ttTabItemBothEdgeSelected
          else if bsHover in State then Detail := ttTabItemBothEdgeHot
          else Detail := ttTabItemBothEdgeNormal;
        end;
      tbRight:
        begin
          if bsDisabled in State then Detail := ttTabItemRightEdgeDisabled
          else if bsSelected in State then Detail := ttTabItemRightEdgeSelected
          else if bsHover in State then Detail := ttTabItemRightEdgeHot
          else Detail := ttTabItemRightEdgeNormal;
        end;
    end;
    StyleServices.DrawElement(Canvas.Handle, StyleServices.GetElementDetails(Detail), Dest);
{$ENDIF}
  end else if (soNativeStyles in Options) and Themed then
  begin
    { Create Handle }
    Theme := OpenThemeData(Handle, teTab);
    Flag := 2;

    Index := 0;
    if bsSelected in State then Index := 3;
    if bsHover in State then Index := 2;
    if bsFocused in State then Index := 4;

    { Draw }
    DrawThemeBackground(Theme, Canvas.Handle, Flag, Index, Dest, nil);

    { Destroy }
    CloseThemeData(Theme);
  end else
  begin
    with Canvas do
    begin
      if Brush.Color <> clNone then FillRect(Dest);
      case TabsLocation of
        tbTop, tbLeft:
        begin
          Pen.Color := clBtnHighlight;
          Polyline([
            Point(Dest.Left, Dest.Bottom),
            Point(Dest.Left, Dest.Top),
            Point(Dest.Right, Dest.Top)
          ]);
        end;
        else
        begin
          Pen.Color := clBtnShadow;
          Polyline([
            Point(Dest.Left, Dest.Bottom - 1),
            Point(Dest.Right - 1, Dest.Bottom - 1),
            Point(Dest.Right - 1, Dest.Top - 1)
          ]);
        end;
      end;
      case TabsLocation of
        tbTop:
        begin
          Pen.Color := clBtnShadow;
          Polyline([
            Point(Dest.Right - 1, Dest.Top + 1),
            Point(Dest.Right - 1, Dest.Bottom)
          ]);
        end;
        tbLeft:
        begin
          Pen.Color := clBtnShadow;
          Polyline([
            Point(Dest.Left, Dest.Bottom - 1),
            Point(Dest.Right, Dest.Bottom - 1)
          ]);
        end;
        tbRight:
        begin
          Pen.Color := clBtnHighlight;
          Polyline([
            Point(Dest.Left, Dest.Top),
            Point(Dest.Right, Dest.Top)
          ]);

        end;
        else
        begin
          Pen.Color := clBtnHighlight;
          Polyline([
            Point(Dest.Left, Dest.Top),
            Point(Dest.Left, Dest.Bottom - 1)
          ]);
        end;
      end;
    end;

  end;
end;

{ TNxUserDefinedStyle }

constructor TNxUserDefinedStyle.Create;
begin
  FBorderColor := clBtnShadow;
  FColor := clBtnFace;
end;

procedure TNxUserDefinedStyle.DrawToCanvas(const Canvas: TCanvas; const Rect: TRect);
begin
  with Canvas do
  begin
    { 13.5.2018: There is a bug with HiDpi and Rectangle procedure.
      Requires investigation. }

    if FColor <> clNone then Brush.Color := FColor;
    FillRect(Rect);

    if FBorderColor <> clNone then Brush.Color := FBorderColor;
    FrameRect(Rect);
  end;
end;

procedure TNxUserDefinedStyle.SetBorderColor(const Value: TColor);
begin
  if Value <> FBorderColor then
  begin
    FBorderColor := Value;
    DoChange;
  end;
end;

procedure TNxUserDefinedStyle.SetColor(const Value: TColor);
begin
  if Value <> FColor then
  begin
    FColor := Value;
    DoChange;
  end;
end;

{ TNxButtonUserDefinedStyle6 }

constructor TNxButtonUserDefinedStyle6.Create;
begin
  FFlat := True;
  FCheckedStyle := TNxUserDefinedStyle.Create;
  FHotStyle := TNxUserDefinedStyle.Create;
  FPushedStyle := TNxUserDefinedStyle.Create;
end;

destructor TNxButtonUserDefinedStyle6.Destroy;
begin
  FCheckedStyle.Free;
  FHotStyle.Free;
  FPushedStyle.Free;
  inherited;
end;

procedure TNxButtonUserDefinedStyle6.DrawToCanvas(const Canvas: TCanvas; const Rect: TRect;
  const State: TNxButtonState);
begin
  if bsChecked in State then
  begin
    if bsPressed in State then FPushedStyle.DrawToCanvas(Canvas, Rect)
      else FCheckedStyle.DrawToCanvas(Canvas, Rect);
  end else
  begin
    if bsHover in State then
    begin
      if bsPressed in State then FPushedStyle.DrawToCanvas(Canvas, Rect)
      else FHotStyle.DrawToCanvas(Canvas, Rect);
    end else
    begin
      Canvas.FillRect(Rect);
    end;
  end;
end;

procedure TNxButtonUserDefinedStyle6.SetCheckedStyle(const Value: TNxUserDefinedStyle);
begin
  if Value <> FCheckedStyle then
  begin
    FCheckedStyle := Value;
    DoChange;
  end;
end;

procedure TNxButtonUserDefinedStyle6.SetFlat(const Value: Boolean);
begin
  if Value <> FFlat then
  begin
    FFlat := Value;
    DoChange;
  end;
end;

procedure TNxButtonUserDefinedStyle6.SetHotStyle(const Value: TNxUserDefinedStyle);
begin
  if FHotStyle <> FHotStyle then
  begin
    FHotStyle := Value;
    DoChange;
  end;
end;

procedure TNxButtonUserDefinedStyle6.SetPushedStyle(const Value: TNxUserDefinedStyle);
begin
  if Value <> FPushedStyle then
  begin
    FPushedStyle := Value;
    DoChange;
  end;
end;

end.
