- add TColorBox.CustomColors property (as delphi has)
  - populate TColorBox.CustomColors with standard + extended colors
  - add function to extract ColorIndex and ColorValue from CustomColors list

win32: 
  - split TWin32WSColorBox code between CreateHandle, FreeHandle and ShowModal
  - use TColorBox.CustomColors to populate win32 colorbox custom colors (todo for qt, gtk2, carbon?)

git-svn-id: trunk@17337 -
This commit is contained in:
paul 2008-11-11 10:39:22 +00:00
parent 8277d0556e
commit b36d20730f
4 changed files with 115 additions and 35 deletions

View File

@ -703,8 +703,6 @@ var
begin
if IdentToColor(s, AColor) then
begin
if AColor = clWhite then
AColor := AColor;
// check clDefault
if not (cbIncludeDefault in Style) and (AColor = clDefault) then
Exit;

View File

@ -224,19 +224,24 @@ type
constructor Create(AOwner: TComponent); override;
end;
{ TColorDialog }
TColorDialog = class(TCommonDialog)
private
FColor: TColor;
FCustomColors: TStrings;
procedure SetCustomColors(const AValue: TStrings);
procedure AddDefaultColor(const s: AnsiString);
protected
function DefaultTitle: string; override;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
published
property Title;
property Color: TColor read FColor write FColor;
// entry looks like ColorA = FFFF00 ... ColorX = C0C0C0
property CustomColors: TStrings read FCustomColors write SetCustomColors;
end;
@ -507,6 +512,9 @@ function SelectDirectory(const Caption, InitialDirectory: string;
function SelectDirectory(out Directory: string;
Options: TSelectDirOpts; HelpCtx: Longint): Boolean;
function ExtractColorIndexAndColor(const AColorList: TStrings; const AIndex: Integer;
out ColorIndex: Integer; out ColorValue: TColor): Boolean;
procedure Register;

View File

@ -16,6 +16,34 @@
* *
*****************************************************************************
}
function ExtractColorIndexAndColor(const AColorList: TStrings;
const AIndex: Integer; out ColorIndex: Integer; out ColorValue: TColor
): Boolean;
var
Name, Value: String;
Code: Integer;
begin
// extract Index and Color from string like: ColorA = 10FF30
Name := AColorList.Names[AIndex];
Value := AColorList.ValueFromIndex[AIndex];
Result := Pos('Color', Name) = 1;
if not Result then
Exit;
Delete(Name, 1, 5); // delete Color
Result := Length(Name) = 1;
if not Result then
Exit;
ColorIndex := Ord(Name[1]) - Ord('A');
Val('$' + Value, ColorValue, Code);
Result := Code = 0;
end;
{------------------------------------------------------------------------------
Method: TColorDialog.Create
Params: AOwner: the owner of the class
@ -26,12 +54,37 @@
constructor TColorDialog.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
FCustomColors := TStringList.Create;
// add default colors
GetColorValues(@AddDefaultColor);
fCompStyle := csColorDialog;
end;
destructor TColorDialog.Destroy;
begin
FCustomColors.Free;
inherited Destroy;
end;
procedure TColorDialog.SetCustomColors(const AValue: TStrings);
begin
FCustomColors.Assign(AValue);
end;
procedure TColorDialog.AddDefaultColor(const s: AnsiString);
var
AColor: TColor;
Index: Integer;
begin
if IdentToColor(s, AColor) and
ColorIndex(AColor, Index) and
(Index < StandardColorsCount + ExtendedColorCount) then
FCustomColors.Values['Color' + Chr(Ord('A') + Index)] := IntToHex(AColor, 6);
end;
function TColorDialog.DefaultTitle: string;
begin
Result:=rsSelectcolorTitle;
Result := rsSelectcolorTitle;
end;

View File

@ -119,6 +119,8 @@ type
protected
public
class function CreateHandle(const ACommonDialog: TCommonDialog): THandle; override;
class procedure ShowModal(const ACommonDialog: TCommonDialog); override;
class procedure DestroyHandle(const ACommonDialog: TCommonDialog); override;
end;
{ TWin32WSColorButton }
@ -294,45 +296,64 @@ end;
{ TWin32WSColorDialog }
class function TWin32WSColorDialog.CreateHandle(const ACommonDialog: TCommonDialog): THandle;
const
{ 16 basic RGB colors; names listed in comments for debugging }
CustomColors: array[1..16] of dword = (
0, //Black
$C0C0C0, //Silver
$808080, //Gray
$FFFFFF, //White
$000080, //Maroon
$0000FF, //Red
$800080, //Purple
$FF00FF, //Fuchsia
$008000, //Green
$00FF00, //Lime
$008080, //Olive
$00FFFF, //Yellow
$800000, //Navy
$FF0000, //Blue
$808000, //Teal
$FFFF00 //Aqua
);
var
CC: TChooseColor;
UserResult: WINBOOL;
CC: PChooseColor;
ColorDialog: TColorDialog absolute ACommonDialog;
procedure FillCustomColors;
var
i, AIndex: integer;
AColor: TColor;
begin
for i := 0 to ColorDialog.CustomColors.Count - 1 do
if ExtractColorIndexAndColor(ColorDialog.CustomColors, i, AIndex, AColor) then
begin
if AIndex < 16 then
CC^.lpCustColors[AIndex] := AColor;
end;
end;
begin
ZeroMemory(@CC, sizeof(TChooseColor));
with CC Do
CC := AllocMem(SizeOf(TChooseColor));
with CC^ Do
begin
LStructSize := sizeof(TChooseColor);
HWndOwner := GetOwnerHandle(ACommonDialog);
RGBResult := ColorToRGB(TColorDialog(ACommonDialog).Color);
LPCustColors := @CustomColors[1];
RGBResult := ColorToRGB(ColorDialog.Color);
LPCustColors := AllocMem(16 * SizeOf(DWord));
FillCustomColors;
Flags := CC_FULLOPEN or CC_RGBINIT;
end;
UserResult := ChooseColor(@CC);
SetDialogResult(ACommonDialog, UserResult);
if UserResult then
TColorDialog(ACommonDialog).Color := CC.RGBResult;
Result := THandle(CC);
end;
Result := 0;
class procedure TWin32WSColorDialog.ShowModal(const ACommonDialog: TCommonDialog);
var
CC: PChooseColor;
UserResult: WINBOOL;
begin
if ACommonDialog.Handle <> 0 then
begin
CC := PChooseColor(ACommonDialog.Handle);
UserResult := ChooseColor(CC);
SetDialogResult(ACommonDialog, UserResult);
if UserResult then
TColorDialog(ACommonDialog).Color := CC^.RGBResult;
end;
end;
class procedure TWin32WSColorDialog.DestroyHandle(
const ACommonDialog: TCommonDialog);
var
CC: PChooseColor;
begin
if ACommonDialog.Handle <> 0 then
begin
CC := PChooseColor(ACommonDialog.Handle);
FreeMem(CC^.lpCustColors);
FreeMem(CC);
end;
end;
procedure UpdateStorage(Wnd: HWND; OpenFile: LPOPENFILENAME);