ColorPalette: New property SelectedIndex.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4281 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2015-08-21 14:46:15 +00:00
parent bec8c5ac86
commit 7d6e60fc65
3 changed files with 120 additions and 62 deletions

View File

@ -76,22 +76,26 @@ type
FOnSelectColor: TColorPaletteEvent;
FRows: Integer;
FColors: TList;
FPickedColor: TColor;
FSelectedColor: TColor; // same as PickedColor, but updated only if "IsCorrectShift"
FSelectedColor: TColor;
FSelectedIndex: Integer;
FPickMode: TPickMode;
FPickShift: TPickShift;
FMousePt: TPoint;
FMouseIndex: Integer;
FPrevMouseIndex: Integer;
FStoredShift: TShiftState;
FShowSelection: Boolean;
function GetColorCount: Integer;
function GetColors(Index: Integer): TColor;
function GetColors(AIndex: Integer): TColor;
function GetPickedColor: TColor;
procedure SetButtonHeight(const AValue: Integer);
procedure SetButtonWidth(const AValue: Integer);
procedure SetColors(Index: Integer; const AValue: TColor);
procedure SetColors(AIndex: Integer; const AValue: TColor);
procedure SetCols(AValue: Integer);
procedure SetSelectedIndex(AValue: Integer);
procedure SetShowSelection(AValue: Boolean);
protected
procedure ColorPick(AColor: TColor; Shift: TShiftState); dynamic;
procedure ColorPick(AIndex: Integer; Shift: TShiftState); dynamic;
procedure ColorMouseMove(AColor: TColor; Shift: TShiftState); dynamic;
procedure DoAddColor(AColor: TColor); virtual;
procedure DoColorPick(AColor: TColor; AShift: TShiftState); virtual;
@ -107,6 +111,8 @@ type
property ColumnCount: Integer read FCols write SetCols;
property PickMode: TPickMode read FPickMode write FPickMode default pmDefault;
property PickShift: TPickShift read FPickShift write FPickShift default [ssLeft];
property SelectedIndex: Integer read FSelectedIndex write SetSelectedIndex default 0;
property ShowSelection: Boolean read FShowSelection write SetShowSelection default false;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
@ -119,7 +125,7 @@ type
property Colors[Index: Integer]: TColor read GetColors write SetColors;
property ColorCount: Integer read GetColorCount;
property PickedColor: TColor read FSelectedColor; deprecated 'Use SelectedColor';
property PickedColor: TColor read GetPickedColor; deprecated 'Use SelectedColor';
property SelectedColor: TColor read FSelectedColor;
property OnSelectColor: TColorPaletteEvent read FOnSelectColor write FOnSelectColor;
@ -150,7 +156,9 @@ type
property PickMode;
property PickShift;
property PopupMenu;
property SelectedIndex;
property ShowHint;
property ShowSelection;
property Visible;
property OnChangeBounds;
@ -227,11 +235,14 @@ begin
Invalidate;
end;
procedure TCustomColorPalette.ColorPick(AColor: TColor; Shift: TShiftState);
procedure TCustomColorPalette.ColorPick(AIndex: Integer; Shift: TShiftState);
var
c: TColor;
begin
DoColorPick(AColor, Shift);
c := GetColors(AIndex);
DoColorPick(c, Shift);
if IsCorrectShift(Shift) then
DoSelectColor(AColor);
SelectedIndex := AIndex;
end;
procedure TCustomColorPalette.ColorMouseMove(AColor: TColor; Shift: TShiftState);
@ -260,12 +271,15 @@ end;
procedure TCustomColorPalette.DoDeleteColor(AIndex: Integer);
begin
if (AIndex < 0) or (AIndex >= FColors.Count) then
exit;
FColors.Delete(AIndex);
end;
procedure TCustomColorPalette.DoSelectColor(AColor: TColor);
begin
FSelectedColor := AColor;
Invalidate;
if Assigned(FOnSelectColor) then FOnSelectColor(self, AColor);
end;
@ -274,14 +288,20 @@ begin
Result := FColors.Count;
end;
function TCustomColorPalette.GetColors(Index: Integer): TColor;
function TCustomColorPalette.GetColors(AIndex: Integer): TColor;
begin
Result := TColor(PtrUInt(FColors.Items[Index]));
if (AIndex < 0) or (AIndex >= FColors.Count) then
Result := clNone
else
Result := TColor(PtrUInt(FColors.Items[AIndex]));
end;
function TCustomColorPalette.GetPickedColor: TColor;
begin
Result := GetColors(FMouseIndex);
end;
function TCustomColorPalette.IsCorrectShift(Shift: TShiftState): Boolean;
var
ss: TShiftState;
begin
Result := True;
if (ssLeft in FPickShift) and (Classes.ssLeft in Shift) then exit;
@ -376,7 +396,7 @@ begin
end;
UpdateSize;
Invalidate;
SelectedIndex := 0;
end;
procedure TCustomColorPalette.MouseDown(Button: TMouseButton;
@ -398,10 +418,9 @@ begin
if (FMouseIndex < FColors.Count) then
begin
FPickedColor := GetColors(FMouseIndex);
FStoredShift := Shift; // store for usage by pmDefault at MouseUp
if FPickMode <> pmDefault then
ColorPick(FPickedColor, Shift);
ColorPick(FMouseIndex, Shift);
end;
end;
@ -422,10 +441,8 @@ begin
if C <> clNone then
ColorMouseMove(C, Shift);
if FPickMode = pmContinuous then begin
FPickedColor := GetColors(FMouseIndex);
ColorPick(FPickedColor, Shift);
end;
if FPickMode = pmContinuous then
ColorPick(FMouseIndex, Shift);
end;
FPrevMouseIndex := FMouseIndex;
@ -437,7 +454,7 @@ begin
case FPickMode of
pmDefault:
if (FMousePt.X = X) and (FMousePt.Y = Y) then
ColorPick(FPickedColor, FStoredShift);
ColorPick(FMouseIndex, FStoredShift);
pmImmediate, pmContinuous:
begin
X := X div FButtonWidth;
@ -446,8 +463,7 @@ begin
if (FMouseIndex >= 0) and (FMouseIndex < FColors.Count) and
(FMouseIndex <> FPrevMouseIndex) then
begin
FPickedColor := GetColors(FMouseIndex);
ColorPick(FPickedColor, Shift);
ColorPick(FMouseIndex, Shift);
end;
end;
end;
@ -460,6 +476,7 @@ procedure TCustomColorPalette.Paint;
var
I, X, Y: Integer;
c: TColor;
R: TRect;
begin
Canvas.Pen.Color := clBlack;
for I := 0 to Pred(FColors.Count) do
@ -469,9 +486,19 @@ begin
c := GetColors(I);
if c <> clNone then
begin
R := Bounds(X * FButtonWidth, Y*FButtonHeight, FButtonWidth, FButtonHeight);
if FShowSelection and (FSelectedIndex = I) then
begin
if Red(c) + Green(c) + Blue(c) > 128*3 then
Canvas.Pen.Color := clBlack else
Canvas.Pen.Color := clWhite;
Canvas.Pen.Width := 3;
end else begin
Canvas.Pen.Color := clBlack;
Canvas.Pen.Width := 1;
end;
Canvas.Brush.Color := c;
Canvas.Rectangle(Bounds(X * FButtonWidth, Y * FButtonHeight, FButtonWidth,
FButtonHeight));
Canvas.Rectangle(R);
end;
end;
end;
@ -517,9 +544,9 @@ begin
UpdateSize;
end;
procedure TCustomColorPalette.SetColors(Index: Integer; const AValue: TColor);
procedure TCustomColorPalette.SetColors(AIndex: Integer; const AValue: TColor);
begin
FColors.Items[Index] := Pointer(AValue);
FColors.Items[AIndex] := Pointer(AValue);
Invalidate;
end;
@ -532,6 +559,26 @@ begin
Invalidate;
end;
procedure TCustomColorPalette.SetShowSelection(AValue: Boolean);
begin
if FShowSelection = AValue then exit;
FShowSelection := AValue;
Invalidate;
end;
procedure TCustomColorPalette.SetSelectedIndex(AValue: Integer);
begin
if FSelectedIndex = AValue then exit;
if AValue < 0 then
FSelectedIndex := 0
else
if AValue >= FColors.Count then
FSelectedIndex := FColors.Count-1
else
FSelectedIndex := AValue;
DoSelectColor(GetColors(FSelectedIndex));
end;
procedure TCustomColorPalette.UpdateSize;
begin
if (FCols = 0) or (FColors.Count = 0) then FRows := 0

View File

@ -40,10 +40,10 @@ object MainForm: TMainForm
Width = 63
end
object LblColorInfo: TLabel
Left = 12
Left = 9
Height = 65
Top = 45
Width = 135
Width = 146
AutoSize = False
Caption = 'LblColorInfo'
Font.Color = clGreen
@ -118,7 +118,7 @@ object MainForm: TMainForm
object EdColCount: TSpinEdit
Left = 11
Height = 23
Top = 432
Top = 463
Width = 66
MinValue = 1
OnChange = EdColCountChange
@ -128,7 +128,7 @@ object MainForm: TMainForm
object Label2: TLabel
Left = 11
Height = 15
Top = 411
Top = 442
Width = 80
Caption = 'Column count:'
ParentColor = False
@ -152,13 +152,22 @@ object MainForm: TMainForm
Text = 'default'
end
object LblPickMode: TLabel
Left = 11
Left = 10
Height = 15
Top = 355
Width = 56
Caption = 'Pick mode'
ParentColor = False
end
object CbShowSelection: TCheckBox
Left = 11
Height = 19
Top = 413
Width = 99
Caption = 'Show selection'
OnChange = CbShowSelectionChange
TabOrder = 9
end
end
object Bevel1: TBevel
Left = 160

View File

@ -21,6 +21,7 @@ type
BtnAddColor: TButton;
BtnLoadDefaultPal: TButton;
BtnEditColor: TButton;
CbShowSelection: TCheckBox;
ColorDialog: TColorDialog;
ColorPalette: TColorPalette;
CbPickMode: TComboBox;
@ -41,6 +42,7 @@ type
procedure BtnLoadDefaultPalClick(Sender: TObject);
procedure BtnLoadRndPaletteClick(Sender: TObject);
procedure CbPickModeSelect(Sender: TObject);
procedure CbShowSelectionChange(Sender: TObject);
procedure ColorPaletteDblClick(Sender: TObject);
procedure ColorPaletteMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
@ -51,7 +53,6 @@ type
procedure MnuEditPickedColorClick(Sender: TObject);
private
{ private declarations }
curIndex: integer;
procedure EditCurColor;
procedure SetColorInfo(ATitle: string; AColor: TColor);
procedure UpdateCaption;
@ -105,17 +106,14 @@ procedure TMainForm.BtnDeleteColorClick(Sender: TObject);
begin
with ColorPalette do
begin
if (curIndex < ColorCount) and (ColorCount > 0) then
begin
DeleteColor(curIndex);
if curIndex = ColorCount then dec(curIndex);
ColorSample.Brush.Color := Colors[curIndex] ;
if Colors[curIndex] = clNone then
ColorSample.Brush.Style := bsClear else
ColorSample.Brush.Style := bsSolid;
UpdateCaption;
SetColorInfo('Current', ColorPalette.Colors[curIndex]);
end;
DeleteColor(SelectedIndex);
if SelectedIndex = ColorCount then SelectedIndex := ColorCount-1;
ColorSample.Brush.Color := Colors[SelectedColor];
if Colors[SelectedColor] = clNone then
ColorSample.Brush.Style := bsClear else
ColorSample.Brush.Style := bsSolid;
UpdateCaption;
SetColorInfo('Current', Colors[SelectedIndex]);
end;
end;
@ -151,14 +149,19 @@ begin
ColorPalette.PickMode := TPickMode(CbPickMode.ItemIndex);
end;
procedure TMainForm.CbShowSelectionChange(Sender: TObject);
begin
ColorPalette.ShowSelection := CbShowSelection.Checked;
end;
procedure TMainForm.ColorPaletteDblClick(Sender: TObject);
begin
with ColorDialog do
begin
Color := ColorPalette.Colors[curIndex];
Color := ColorPalette.Colors[ColorPalette.SelectedIndex];
if Execute then
begin
ColorPalette.Colors[curIndex] := Color;
ColorPalette.Colors[ColorPalette.SelectedIndex] := Color;
ColorSample.Brush.Color := Color;
ColorSample.Brush.Style := bsSolid;
SetColorInfo('Current', Color);
@ -174,23 +177,23 @@ end;
procedure TMainForm.ColorPaletteMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
with ColorPalette do
begin
X := X div ButtonWidth;
Y := Y div ButtonHeight;
curIndex := X + Y * ColumnCount;
end;
BtnDeleteColor.caption := 'Delete color #' + IntToStr(curIndex);
exit;
BtnDeleteColor.caption := 'Delete color #' + IntToStr(ColorPalette.SelectedIndex);
UpdateCaption;
end;
procedure TMainForm.ColorPaletteSelectColor(Sender: TObject; AColor: TColor);
begin
ColorSample.Brush.Color := ColorPalette.SelectedColor;
if ColorPalette.Colors[curIndex] = clNone then
ColorSample.Brush.Color := AColor;
if AColor = clNone then
ColorSample.Brush.Style := bsClear else
ColorSample.Brush.Style := bsSolid;
SetColorInfo('SelectedColor', ColorPalette.SelectedColor);
SetColorInfo('SelectedColor', AColor);
BtnDeleteColor.Caption := 'Delete color #' + IntToStr(ColorPalette.SelectedIndex);
UpdateCaption;
end;
procedure TMainForm.EdColCountChange(Sender: TObject);
@ -218,9 +221,8 @@ end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
curIndex := 0;
ColorSample.Brush.Color := ColorPalette.Colors[0];
SetColorInfo('Current', ColorPalette.Colors[curIndex]);
ColorSample.Brush.Color := ColorPalette.SelectedColor;
SetColorInfo('Current', ColorPalette.SelectedColor);
UpdateCaption;
{ ColorPalette.PickShift must contain ssRight in order to be able to select
@ -257,14 +259,14 @@ end;
procedure TMainForm.UpdateCaption;
begin
Caption := Format('ColorPalette demo - CurIndex: %d (%d colors available)',
[curIndex, ColorPalette.ColorCount]
[ColorPalette.SelectedIndex, ColorPalette.ColorCount]
);
end;
procedure TMainForm.UpdatePalette;
begin
ColorPalette.Colors[curIndex] := ColorSample.Brush.Color;
SetColorInfo('Current', ColorPalette.Colors[curIndex]);
ColorPalette.Colors[ColorPalette.SelectedIndex] := ColorSample.Brush.Color;
SetColorInfo('Current', ColorSample.Brush.Color);
with BtnEditColor do
begin
Caption := 'Edit';