unit IWTMSPCKDsnColor;

interface

//{$I IWCompilerDefines.inc}

uses
  DesignIntf, DesignEditors, Types, VCLEditors,
  Forms, Windows, Graphics, Controls,
  SysUtils, Classes, IWColor, IWBaseControl, IWTMSPCKDsnColorDialog;

type
  TIWTMSPCKColorEditor = class( TColorProperty, ICustomPropertyDrawing, ICustomPropertyListDrawing)
  public
    procedure GetValues(Proc: TGetStrProc); override;
    function GetValue: string; override;
    procedure SetValue(const Value: string); override;

    procedure ListDrawValue(const Value: string; ACanvas: TCanvas;
      const ARect: TRect; ASelected: Boolean);

    procedure Edit; override;
    procedure PropDrawName(ACanvas: TCanvas; const ARect: TRect;
      ASelected: Boolean);
    procedure PropDrawValue(ACanvas: TCanvas; const ARect: TRect;
      ASelected: Boolean);
  end;

implementation

var
  FCustomColors : TStringList;

{ TIWTMSPCKColorEditor }

procedure TIWTMSPCKColorEditor.GetValues(Proc: TGetStrProc);
begin
  inherited GetValues( Proc );
  GetIWColorValues(Proc);
end;

function TIWTMSPCKColorEditor.GetValue: string;
begin
  Result := IWColorToString(TIWColor(GetOrdValue));
end;

procedure TIWTMSPCKColorEditor.SetValue(const Value: string);
var
  NewValue: Longint;
begin
  if IdentToIWColor(Value, NewValue) then begin
    SetOrdValue(NewValue)
  end  else begin
    inherited SetValue(Value);
  end;
end;

procedure TIWTMSPCKColorEditor.PropDrawValue(ACanvas: TCanvas; const ARect: TRect; ASelected: Boolean);
begin
  if GetVisualValue <> '' then
    ListDrawValue(GetVisualValue, ACanvas, ARect, True{ASelected})
  else
    DefaultPropertyDrawValue(Self, ACanvas, ARect);
end;

procedure TIWTMSPCKColorEditor.PropDrawName(ACanvas: TCanvas; const ARect: TRect; ASelected: Boolean);
begin
  inherited PropDrawName(ACanvas, ARect, ASelected );
end;

procedure TIWTMSPCKColorEditor.ListDrawValue(const Value: string; ACanvas: TCanvas; const ARect: TRect; ASelected: Boolean);
  function ColorToBorderColor(AColor: TColor): TColor;
  type
    TColorQuad = record
      Red,
      Green,
      Blue,
      Alpha: Byte;
    end;
  begin

    if ((AColor and $FF) > 192) or
       (((AColor shr 8)and $FF) > 192) or
       (((AColor shr 16)and $FF) > 192) then
      Result := clBlack
    else if ASelected then
      Result := clWhite
    else
      Result := AColor;
  end;
var
  Right: Integer;
  OldPenColor, OldBrushColor: TColor;
begin
  Right := (ARect.Bottom - ARect.Top) {* 2} + ARect.Left;
  with ACanvas do
  begin
    // save off things
    OldPenColor := Pen.Color;
    OldBrushColor := Brush.Color;

    // frame things
    Pen.Color := Brush.Color;
    Rectangle(ARect.Left, ARect.Top, Right, ARect.Bottom);

    // set things up and do the work

    Brush.Color := StringToIWColor(Value); // the difference
    Pen.Color := ColorToBorderColor(ColorToRGB(Brush.Color));
    Rectangle(ARect.Left + 1, ARect.Top + 1, Right - 1, ARect.Bottom - 1);

    // restore the things we twiddled with
    Brush.Color := OldBrushColor;
    Pen.Color := OldPenColor;
    DefaultPropertyListDrawValue(Value, ACanvas, Rect(Right, ARect.Top, ARect.Right,
      ARect.Bottom), ASelected);
  end;
end;

procedure TIWTMSPCKColorEditor.Edit;
var
 Result : Integer;
 Ident : string;
begin
  with TIWTMSPCKColorForm.Create( Application, FCustomColors ) do try
  begin
    SelectedIdentifier := GetValue;
    Result := ShowModal();
    if Result = mrOK then begin
      Ident := SelectedIdentifier;
      SetValue( Ident );
    end;
  end;
  finally
    Free;
  end;

end;

initialization
  FCustomColors := TStringList.Create;
finalization
  FreeAndNil(FCustomColors);
end.
