ColorPalette: New properties BorderColor and BorderWidth.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4283 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2015-08-21 22:32:56 +00:00
parent 895bad7c04
commit 3980f70b8b
3 changed files with 218 additions and 77 deletions

View File

@ -87,15 +87,18 @@ type
FShowColorHint: Boolean; FShowColorHint: Boolean;
FShowSelection: Boolean; FShowSelection: Boolean;
FSavedHint: String; FSavedHint: String;
FBorderColor: TColor;
FBorderWidth: Integer;
function GetColorCount: Integer; function GetColorCount: Integer;
function GetColors(AIndex: Integer): TColor; function GetColors(AIndex: Integer): TColor;
function GetPickedColor: TColor; function GetPickedColor: TColor;
procedure SetBorderColor(const AValue: TColor);
procedure SetBorderWidth(const AValue: Integer);
procedure SetButtonHeight(const AValue: Integer); procedure SetButtonHeight(const AValue: Integer);
procedure SetButtonWidth(const AValue: Integer); procedure SetButtonWidth(const AValue: Integer);
procedure SetColors(AIndex: Integer; const AValue: TColor); procedure SetColors(AIndex: Integer; const AValue: TColor);
procedure SetCols(AValue: Integer); procedure SetCols(AValue: Integer);
procedure SetSelectedIndex(AValue: Integer); procedure SetSelectedIndex(AValue: Integer);
// procedure SetShowColorHint(AValue: Boolean);
procedure SetShowSelection(AValue: Boolean); procedure SetShowSelection(AValue: Boolean);
protected protected
procedure ColorPick(AIndex: Integer; Shift: TShiftState); dynamic; procedure ColorPick(AIndex: Integer; Shift: TShiftState); dynamic;
@ -112,6 +115,8 @@ type
procedure MouseMove(Shift:TShiftState; X, Y:Integer); override; procedure MouseMove(Shift:TShiftState; X, Y:Integer); override;
procedure MouseUp(Button: TMouseButton; Shift:TShiftState; X, Y:Integer); override; procedure MouseUp(Button: TMouseButton; Shift:TShiftState; X, Y:Integer); override;
procedure UpdateSize; virtual; procedure UpdateSize; virtual;
property BorderColor: TColor read FBorderColor write SetBorderColor default clBlack;
property BorderWidth: Integer read FBorderWidth write SetBorderWidth default 1;
property ButtonWidth: Integer read FButtonWidth write SetButtonWidth; property ButtonWidth: Integer read FButtonWidth write SetButtonWidth;
property ButtonHeight: Integer read FButtonHeight write SetButtonHeight; property ButtonHeight: Integer read FButtonHeight write SetButtonHeight;
property ColumnCount: Integer read FCols write SetCols; property ColumnCount: Integer read FCols write SetCols;
@ -120,11 +125,12 @@ type
property SelectedIndex: Integer read FSelectedIndex write SetSelectedIndex default 0; property SelectedIndex: Integer read FSelectedIndex write SetSelectedIndex default 0;
property ShowColorHint: Boolean read FShowColorHint write FShowColorHint default true; property ShowColorHint: Boolean read FShowColorHint write FShowColorHint default true;
property ShowSelection: Boolean read FShowSelection write SetShowSelection default false; property ShowSelection: Boolean read FShowSelection write SetShowSelection default false;
public public
constructor Create(TheOwner: TComponent); override; constructor Create(TheOwner: TComponent); override;
destructor Destroy; override; destructor Destroy; override;
procedure Paint; override; procedure Paint; override;
public
procedure AddColor(AColor: TColor); procedure AddColor(AColor: TColor);
procedure DeleteColor(AIndex: Integer); procedure DeleteColor(AIndex: Integer);
procedure LoadPalette(const FileName: String); procedure LoadPalette(const FileName: String);
@ -145,13 +151,27 @@ type
TColorPalette = class(TCustomColorPalette) TColorPalette = class(TCustomColorPalette)
published published
// inherited from TCustomColorPalette
property BorderColor;
property BorderWidth;
property ButtonWidth;
property ButtonHeight;
property ColumnCount;
property PickMode;
property PickShift;
property SelectedIndex;
property ShowColorHint;
property ShowSelection;
property OnColorMouseMove;
property OnColorPick;
property OnSelectColor;
// inherited from TCustomColorPalette's ancestors
property Align; property Align;
property Anchors; property Anchors;
property BorderSpacing; property BorderSpacing;
property ButtonWidth;
property ButtonHeight;
property Color; property Color;
property ColumnCount;
property Constraints; property Constraints;
property DragCursor; property DragCursor;
property DragKind; property DragKind;
@ -160,19 +180,12 @@ type
property Hint; property Hint;
property ParentColor; property ParentColor;
property ParentShowHint; property ParentShowHint;
property PickMode;
property PickShift;
property PopupMenu; property PopupMenu;
property SelectedIndex;
property ShowColorHint;
property ShowHint; property ShowHint;
property ShowSelection;
property Visible; property Visible;
property OnChangeBounds; property OnChangeBounds;
property OnClick; property OnClick;
property OnColorMouseMove;
property OnColorPick;
property OnDblClick; property OnDblClick;
property OnMouseDown; property OnMouseDown;
property OnMouseMove; property OnMouseMove;
@ -180,7 +193,6 @@ type
property OnMouseEnter; property OnMouseEnter;
property OnMouseLeave; property OnMouseLeave;
property OnResize; property OnResize;
property OnSelectColor;
end; end;
procedure Register; procedure Register;
@ -188,6 +200,9 @@ type
implementation implementation
uses
LCLIntf;
procedure Register; procedure Register;
begin begin
RegisterComponents('Misc', [TColorPalette]); RegisterComponents('Misc', [TColorPalette]);
@ -202,6 +217,8 @@ begin
ControlStyle := ControlStyle + [csFixedWidth, csFixedHeight]; ControlStyle := ControlStyle + [csFixedWidth, csFixedHeight];
FColors := TList.Create; FColors := TList.Create;
FBorderColor := clBlack;
FBorderWidth := 1;
FButtonWidth := 12; FButtonWidth := 12;
FButtonHeight := 12; FButtonHeight := 12;
FPrevMouseIndex := -1; FPrevMouseIndex := -1;
@ -520,34 +537,74 @@ begin
end; end;
procedure TCustomColorPalette.Paint; procedure TCustomColorPalette.Paint;
var
I, X, Y: Integer; procedure PaintBox(x1, y1, x2, y2: Integer; c: TColor);
c: TColor;
R: TRect;
begin
Canvas.Pen.Color := clBlack;
for I := 0 to Pred(FColors.Count) do
begin begin
Y := I div FCols; if c = clNone then
X := I mod FCols; exit;
c := GetColors(I);
if c <> clNone then // Fill interior
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.Brush.Color := c;
Canvas.Rectangle(R); Canvas.FillRect(x1, y1, x2, y2);
// Paint border
if (FBorderColor <> clNone) and (FBorderWidth > 0) then
begin
x1 := x1 - FBorderWidth div 2 - FBorderWidth mod 2;
y1 := y1 - FBorderWidth div 2 - FBorderWidth mod 2;
x2 := x1 + FButtonWidth;
y2 := y1 + FButtonHeight;
Canvas.Pen.Color := FBorderColor;
Canvas.Pen.Width := FBorderWidth;
Canvas.MoveTo(x1, y1);
Canvas.LineTo(x2, y1);
Canvas.LineTo(x2, y2);
Canvas.LineTo(x1, y2);
Canvas.LineTo(x1, y1);
end; end;
end; end;
var
I, X, Y, W, H, d: Integer;
c: TColor;
Rsel: TRect;
begin
Canvas.Pen.Endcap := pecSquare;
// Paint color boxes
X := FBorderWidth;
Y := FBorderWidth;
W := FButtonWidth - FBorderWidth;
H := FButtonHeight - FBorderWidth;
for I := 0 to pred(FColors.Count) do
begin
if I = FSelectedIndex then // Selected rect of box with selected color
Rsel := Bounds(X, Y, W, H);
c := GetColors(I);
PaintBox(X, Y, X+W, Y+H, c);
inc(X, FButtonWidth);
if X >= Width then
begin
inc(Y, FButtonHeight);
X := FBorderWidth;
end;
end;
// Paint selection
if FShowSelection then
begin
d := FBorderWidth div 2;
if d = 0 then
c := GetColors(FSelectedIndex) else
c := ColorToRgb(FBorderColor);
Canvas.Pen.Color := InvertColor(c);
Canvas.Pen.Width := 1;
Canvas.Pen.Style := psSolid;
Canvas.Brush.Style := bsSolid;
Canvas.Brush.Style := bsClear;
InflateRect(Rsel, d, d);
Canvas.Rectangle(Rsel);
end;
end; end;
procedure TCustomColorPalette.SavePalette(const Filename: String); procedure TCustomColorPalette.SavePalette(const Filename: String);
@ -575,12 +632,19 @@ begin
end; end;
end; end;
procedure TCustomColorPalette.SetButtonWidth(const AValue: Integer); procedure TCustomColorPalette.SetBorderColor(const AValue: TColor);
begin begin
if FButtonWidth = AValue then Exit; if FBorderColor = AValue then exit;
FButtonWidth := AValue; FBorderColor := AValue;
if FButtonWidth < 1 then FButtonWidth := 1; Invalidate;
end;
procedure TCustomColorPalette.SetBorderWidth(const AValue: Integer);
begin
if FBorderWidth = AValue then exit;
FBorderWidth := AValue;
UpdateSize; UpdateSize;
Invalidate;
end; end;
procedure TCustomColorPalette.SetButtonHeight(const AValue: Integer); procedure TCustomColorPalette.SetButtonHeight(const AValue: Integer);
@ -591,6 +655,14 @@ begin
UpdateSize; UpdateSize;
end; end;
procedure TCustomColorPalette.SetButtonWidth(const AValue: Integer);
begin
if FButtonWidth = AValue then Exit;
FButtonWidth := AValue;
if FButtonWidth < 1 then FButtonWidth := 1;
UpdateSize;
end;
procedure TCustomColorPalette.SetColors(AIndex: Integer; const AValue: TColor); procedure TCustomColorPalette.SetColors(AIndex: Integer; const AValue: TColor);
begin begin
FColors.Items[AIndex] := Pointer(AValue); FColors.Items[AIndex] := Pointer(AValue);
@ -642,7 +714,7 @@ begin
else else
FRows := Ceil(FColors.Count / FCols); FRows := Ceil(FColors.Count / FCols);
SetBounds(Left, Top, FCols * FButtonWidth + 1, FRows * FButtonHeight + 1); SetBounds(Left, Top, FCols * FButtonWidth + FBorderWidth, FRows * FButtonHeight + FBorderWidth);
end; end;

View File

@ -1,10 +1,10 @@
object MainForm: TMainForm object MainForm: TMainForm
Left = 358 Left = 358
Height = 502 Height = 622
Top = 179 Top = 179
Width = 455 Width = 455
Caption = 'MainForm' Caption = 'MainForm'
ClientHeight = 502 ClientHeight = 622
ClientWidth = 455 ClientWidth = 455
OnCreate = FormCreate OnCreate = FormCreate
ShowHint = True ShowHint = True
@ -19,19 +19,18 @@ object MainForm: TMainForm
ButtonHeight = 16 ButtonHeight = 16
ColumnCount = 8 ColumnCount = 8
PickShift = [ssLeft, ssMiddle] PickShift = [ssLeft, ssMiddle]
OnSelectColor = ColorPaletteSelectColor
PopupMenu = PalettePopupMenu PopupMenu = PalettePopupMenu
OnDblClick = ColorPaletteDblClick OnDblClick = ColorPaletteDblClick
OnMouseDown = ColorPaletteMouseDown
OnSelectColor = ColorPaletteSelectColor
end end
object Panel1: TPanel object Panel1: TPanel
Left = 0 Left = 0
Height = 502 Height = 622
Top = 0 Top = 0
Width = 160 Width = 160
Align = alLeft Align = alLeft
BevelOuter = bvNone BevelOuter = bvNone
ClientHeight = 502 ClientHeight = 622
ClientWidth = 160 ClientWidth = 160
TabOrder = 0 TabOrder = 0
object ColorSample: TShape object ColorSample: TShape
@ -117,29 +116,30 @@ object MainForm: TMainForm
TabOrder = 6 TabOrder = 6
end end
object EdColCount: TSpinEdit object EdColCount: TSpinEdit
Left = 11 Left = 9
Height = 23 Height = 23
Top = 463 Top = 581
Width = 66 Width = 59
Alignment = taRightJustify
MinValue = 1 MinValue = 1
OnChange = EdColCountChange OnChange = EdColCountChange
TabOrder = 7 TabOrder = 7
Value = 8 Value = 8
end end
object Label2: TLabel object Label2: TLabel
Left = 11 Left = 9
Height = 15 Height = 15
Top = 442 Top = 561
Width = 80 Width = 48
Caption = 'Column count:' Caption = 'Columns'
ParentColor = False ParentColor = False
end end
object CbPickMode: TComboBox object CbPickMode: TComboBox
Left = 11 Left = 10
Height = 23 Height = 23
Hint = 'Defines when the picked color is determined and when the OnPickColor event is generated:'#13#10#13#10'pmDefault: '#13#10' Color selection at mouse-down, OnPickColor event at mouse-up if at same location'#13#10#13#10'pmImmediate:'#13#10' Color selection and OnPickColor event at mouse-down'#13#10#13#10'pmContinuous:'#13#10' Color selection and OnPickColor event while mouse is down' Hint = 'Defines when the picked color is determined and when the OnPickColor event is generated:'#13#10#13#10'pmDefault: '#13#10' Color selection at mouse-down, OnPickColor event at mouse-up if at same location'#13#10#13#10'pmImmediate:'#13#10' Color selection and OnPickColor event at mouse-down'#13#10#13#10'pmContinuous:'#13#10' Color selection and OnPickColor event while mouse is down'
Top = 358 Top = 355
Width = 136 Width = 137
ItemHeight = 15 ItemHeight = 15
ItemIndex = 0 ItemIndex = 0
Items.Strings = ( Items.Strings = (
@ -155,13 +155,13 @@ object MainForm: TMainForm
object LblPickMode: TLabel object LblPickMode: TLabel
Left = 10 Left = 10
Height = 15 Height = 15
Top = 337 Top = 335
Width = 56 Width = 56
Caption = 'Pick mode' Caption = 'Pick mode'
ParentColor = False ParentColor = False
end end
object CbShowSelection: TCheckBox object CbShowSelection: TCheckBox
Left = 11 Left = 10
Height = 19 Height = 19
Top = 395 Top = 395
Width = 99 Width = 99
@ -170,7 +170,7 @@ object MainForm: TMainForm
TabOrder = 9 TabOrder = 9
end end
object CbShowColorHints: TCheckBox object CbShowColorHints: TCheckBox
Left = 11 Left = 10
Height = 19 Height = 19
Top = 416 Top = 416
Width = 108 Width = 108
@ -180,10 +180,67 @@ object MainForm: TMainForm
State = cbChecked State = cbChecked
TabOrder = 10 TabOrder = 10
end end
object LblPickMode1: TLabel
Left = 10
Height = 15
Top = 451
Width = 68
Caption = 'Border color:'
ParentColor = False
end
object CbBorderColor: TColorBox
Left = 10
Height = 22
Top = 471
Width = 137
NoneColorColor = clWindow
Style = [cbStandardColors, cbExtendedColors, cbSystemColors, cbIncludeNone]
ItemHeight = 16
OnSelect = CbBorderColorSelect
TabOrder = 11
end
object Label3: TLabel
Left = 10
Height = 15
Top = 505
Width = 68
Caption = 'Border width'
ParentColor = False
end
object EdBorderWidth: TSpinEdit
Left = 10
Height = 23
Top = 525
Width = 58
Alignment = taRightJustify
MaxValue = 16
OnChange = EdBorderWidthChange
TabOrder = 12
Value = 1
end
object Label4: TLabel
Left = 93
Height = 15
Top = 561
Width = 40
Caption = 'Btn size'
ParentColor = False
end
object EdBoxSize: TSpinEdit
Left = 93
Height = 23
Top = 581
Width = 54
Alignment = taRightJustify
MinValue = 1
OnChange = EdBoxSizeChange
TabOrder = 13
Value = 16
end
end end
object Bevel1: TBevel object Bevel1: TBevel
Left = 160 Left = 160
Height = 486 Height = 606
Top = 8 Top = 8
Width = 3 Width = 3
Align = alLeft Align = alLeft

View File

@ -6,7 +6,7 @@ interface
uses uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
Spin, ExtCtrls, Menus, ColorPalette; Spin, ExtCtrls, Menus, ColorBox, ColorPalette;
type type
@ -23,13 +23,19 @@ type
BtnEditColor: TButton; BtnEditColor: TButton;
CbShowSelection: TCheckBox; CbShowSelection: TCheckBox;
CbShowColorHints: TCheckBox; CbShowColorHints: TCheckBox;
CbBorderColor: TColorBox;
ColorDialog: TColorDialog; ColorDialog: TColorDialog;
ColorPalette: TColorPalette; ColorPalette: TColorPalette;
CbPickMode: TComboBox; CbPickMode: TComboBox;
EdBorderWidth: TSpinEdit;
EdBoxSize: TSpinEdit;
Label3: TLabel;
Label4: TLabel;
LblPickMode: TLabel; LblPickMode: TLabel;
EdColCount: TSpinEdit; EdColCount: TSpinEdit;
Label2: TLabel; Label2: TLabel;
LblColorInfo: TLabel; LblColorInfo: TLabel;
LblPickMode1: TLabel;
MnuEditPickedColor: TMenuItem; MnuEditPickedColor: TMenuItem;
MnuDeletePickedColor: TMenuItem; MnuDeletePickedColor: TMenuItem;
PalettePopupMenu: TPopupMenu; PalettePopupMenu: TPopupMenu;
@ -45,10 +51,11 @@ type
procedure CbPickModeSelect(Sender: TObject); procedure CbPickModeSelect(Sender: TObject);
procedure CbShowColorHintsChange(Sender: TObject); procedure CbShowColorHintsChange(Sender: TObject);
procedure CbShowSelectionChange(Sender: TObject); procedure CbShowSelectionChange(Sender: TObject);
procedure CbBorderColorSelect(Sender: TObject);
procedure ColorPaletteDblClick(Sender: TObject); procedure ColorPaletteDblClick(Sender: TObject);
procedure ColorPaletteMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ColorPaletteSelectColor(Sender: TObject; AColor: TColor); procedure ColorPaletteSelectColor(Sender: TObject; AColor: TColor);
procedure EdBorderWidthChange(Sender: TObject);
procedure EdBoxSizeChange(Sender: TObject);
procedure EdColCountChange(Sender: TObject); procedure EdColCountChange(Sender: TObject);
procedure FormCreate(Sender: TObject); procedure FormCreate(Sender: TObject);
procedure MnuDeletePickedColorClick(Sender: TObject); procedure MnuDeletePickedColorClick(Sender: TObject);
@ -70,8 +77,8 @@ implementation
{$R *.lfm} {$R *.lfm}
{ TMainForm }
{ TMainForm }
procedure TMainForm.BtnAddColorClick(Sender: TObject); procedure TMainForm.BtnAddColorClick(Sender: TObject);
begin begin
@ -146,6 +153,11 @@ begin
UpdatePalette; UpdatePalette;
end; end;
procedure TMainForm.CbBorderColorSelect(Sender: TObject);
begin
ColorPalette.BorderColor := CbBorderColor.Selected;
end;
procedure TMainForm.CbPickModeSelect(Sender: TObject); procedure TMainForm.CbPickModeSelect(Sender: TObject);
begin begin
ColorPalette.PickMode := TPickMode(CbPickMode.ItemIndex); ColorPalette.PickMode := TPickMode(CbPickMode.ItemIndex);
@ -181,17 +193,6 @@ begin
end; end;
end; end;
procedure TMainForm.ColorPaletteMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
exit;
BtnDeleteColor.caption := 'Delete color #' + IntToStr(ColorPalette.SelectedIndex);
UpdateCaption;
end;
procedure TMainForm.ColorPaletteSelectColor(Sender: TObject; AColor: TColor); procedure TMainForm.ColorPaletteSelectColor(Sender: TObject; AColor: TColor);
begin begin
ColorSample.Brush.Color := AColor; ColorSample.Brush.Color := AColor;
@ -203,6 +204,17 @@ begin
UpdateCaption; UpdateCaption;
end; end;
procedure TMainForm.EdBorderWidthChange(Sender: TObject);
begin
ColorPalette.BorderWidth := EdBorderWidth.Value;
end;
procedure TMainForm.EdBoxSizeChange(Sender: TObject);
begin
ColorPalette.ButtonWidth := EdBoxSize.Value;
ColorPalette.ButtonHeight := EdBoxSize.Value;
end;
procedure TMainForm.EdColCountChange(Sender: TObject); procedure TMainForm.EdColCountChange(Sender: TObject);
begin begin
ColorPalette.ColumnCount := EdColCount.Value; ColorPalette.ColumnCount := EdColCount.Value;