lazarus/components/customdrawn/customdrawnextras.pas
sekelsenmat a95c92eeaf Adds the code to render the image
git-svn-id: trunk@28071 -
2010-11-04 11:04:46 +00:00

640 lines
15 KiB
ObjectPascal

{
Copyright (C) 2010 Felipe Monteiro de Carvalho
License: The same modifying LGPL with static linking exception as the LCL
This unit should be a repository for various custom drawn components,
such as a custom drawn version of TButton, of TEdit, of TPageControl, etc,
eventually forming a full set of custom drawn components.
}
unit customdrawnextras;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Graphics, Controls, LCLType, LCLIntf, IntfGraphics,
Math,
// fpimage
fpcanvas, fpimgcanv, fpimage;
type
// commented items are not yet supported
TBitmappedButtonOption = (bboUseImageForSelection
{bboUseImageForMouseOver, bboDrawFocusRectangle,}
(*bboCheckable,*));
TBitmappedButtonOptions = set of TBitmappedButtonOption;
// commented items are not yet supported
TBitmappedButtonState = (bbsNormal, bbsDown, bbsMouseOver, bbsFocused
(* bbsChecked, bbsCheckedSelected, bbsCheckedDown { is going to be unchecked }*));
{ TCustomBitmappedButton }
TCustomBitmappedButton = class(TCustomControl)
private
FOnChange: TNotifyEvent;
BmpBuffer: TBitmap;
StrBuffer: TStringList;
protected
FImageBtn: TPicture;
FImageBtnDown: TPicture;
FImageBtnMouseOver: TPicture;
FImageBtnFocused: TPicture;
FImageBtnChecked: TPicture;
FOptions: TBitmappedButtonOptions;
FState: TBitmappedButtonState;
// keyboard
procedure DoEnter; override;
procedure DoExit; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyUp(var Key: Word; Shift: TShiftState); override;
// mouse
procedure MouseDown(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override;
procedure MouseUp(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override;
procedure MouseEnter; override;
procedure MouseLeave; override;
// button state change
procedure DoButtonDown();
procedure DoButtonUp();
// inherited
procedure RealSetText(const Value: TCaption); override;
protected
// Properties
property ImageBtn: TPicture read FImageBtn;
property ImageBtnDown: TPicture read FImageBtnDown;
property ImageBtnFocused: TPicture read FImageBtnFocused;
property Options: TBitmappedButtonOptions read FOptions write FOptions;
// Events
property OnChange: TNotifyEvent read FOnChange write FOnChange;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure EraseBackground(DC: HDC); override;
procedure Paint; override;
function GetStateBitmap(): TBitmap;
end;
{@@
TBitmappedButton is a simple custom drawn button which bases it's drawing
on provided raster images. Currently the following states are supported:
normal, down and focused.
The Caption of this button may have multiple lines of text, separated by any
line separator. The text is drawn centralized in the button.
Some work was done trying to achieve alpha blending for the button, but this
wasn't successfull. It would work like this: The button may be drawn flat
or alpha blended using a separate image for the Alpha channel. While pixels
in the alpha channel will result in the button pixel being fully drawn,
while black pixels represent pixels which aren't drawn. grey pixels are
alpha blended.
}
TBitmappedButton = class(TCustomBitmappedButton)
published
// LCL properties and events
property Action;
property Anchors;
property AnchorSide;
// property BidiMode;
// property BorderSpacing;
// property Cancel;
property Caption;
property Constraints;
// property Default;
// property DragCursor;
// property DragKind;
// property DragMode;
property Enabled;
property Font;
// property ParentBidiMode;
// property ModalResult;
property OnChangeBounds;
property OnClick;
property OnContextPopup;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDrag;
property OnUTF8KeyPress;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
// Specific properties
property ImageBtn;
property ImageBtnDown;
property ImageBtnFocused;
property Options;
end;
TCDDrawStyle = (dsWinCE, dsCustom);
{@@
TCDGroupBox is a custom-drawn group box control
}
TCDGroupBoxDrawer = class;
TCDGroupBoxDrawerWinCE = class;
{ TCDGroupBox }
TCDGroupBox = class(TCustomControl)
private
FDrawStyle: TCDDrawStyle;
FCurrentDrawer: TCDGroupBoxDrawer;
FDrawerWinCE: TCDGroupBoxDrawerWinCE;
procedure PrepareCurrentDrawer();
procedure SetDrawStyle(const AValue: TCDDrawStyle);
public
CustomDrawer: TCDGroupBoxDrawer; // Fill the field to use the dsCustom draw mode
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure EraseBackground(DC: HDC); override;
procedure Paint; override;
published
property DrawStyle: TCDDrawStyle read FDrawStyle write SetDrawStyle;
property Caption;
property TabStop default False;
end;
{ TCDGroupBoxDrawer }
TCDGroupBoxDrawer = class
public
procedure SetClientRectPos(CDGroupBox: TCDGroupBox); virtual; abstract;
procedure DrawToIntfImage(ADest: TFPImageCanvas; CDGroupBox: TCDGroupBox); virtual; abstract;
end;
{ TCDGroupBoxDrawerWinCE }
TCDGroupBoxDrawerWinCE = class(TCDGroupBoxDrawer)
public
procedure SetClientRectPos(CDGroupBox: TCDGroupBox); override;
procedure DrawToIntfImage(ADest: TFPImageCanvas; CDGroupBox: TCDGroupBox); override;
end;
{@@
TCDTrackBar is a custom-drawn trackbar control
}
// TCDTrackBarDrawer = class;
{ TCDTrackBar }
{ TCDTrackBar = class(TCustomControl)
private
FMin: Integer;
FMax: Integer;
FPosition: Integer;
FOnChange: TNotifyEvent;
procedure SetMax(Value: Integer);
procedure SetMin(Value: Integer);
procedure SetPosition(Value: Integer);
protected
procedure Changed; virtual;
// keyboard
procedure DoEnter; override;
procedure DoExit; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyUp(var Key: Word; Shift: TShiftState); override;
// mouse
procedure MouseDown(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override;
procedure MouseUp(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override;
procedure MouseEnter; override;
procedure MouseLeave; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure EraseBackground(DC: HDC); override;
procedure Paint; override;
published
property Max: Integer read FMax write SetMax default 10;
property Min: Integer read FMin write SetMin default 0;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property Position: Integer read FPosition write SetPosition;
property TabStop default True;
end;}
{ TCDTrackBarDrawer }
{ TCDTrackBarDrawer = class
public
procedure DrawToIntfImage(ADest: TLazIntfImage; CDTrackBar: TCDTrackBar); virtual; abstract;
end;}
procedure Register;
implementation
const
INT_BitmappedButton_LineSpacing = 2;
procedure Register;
begin
RegisterComponents('Common Controls', [TBitmappedButton]);
end;
{ TCustomBitmappedButton }
procedure TCustomBitmappedButton.DoEnter;
begin
DoButtonUp();
inherited DoEnter;
end;
procedure TCustomBitmappedButton.DoExit;
begin
DoButtonUp();
inherited DoExit;
end;
procedure TCustomBitmappedButton.KeyDown(var Key: Word; Shift: TShiftState);
begin
inherited KeyDown(Key, Shift);
if Key = VK_SPACE then DoButtonDown();
end;
procedure TCustomBitmappedButton.KeyUp(var Key: Word; Shift: TShiftState);
begin
DoButtonUp();
inherited KeyUp(Key, Shift);
end;
procedure TCustomBitmappedButton.MouseDown(Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
DoButtonDown();
inherited MouseDown(Button, Shift, X, Y);
end;
procedure TCustomBitmappedButton.MouseUp(Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
DoButtonUp();
inherited MouseUp(Button, Shift, X, Y);
end;
procedure TCustomBitmappedButton.MouseEnter;
begin
inherited MouseEnter;
end;
procedure TCustomBitmappedButton.MouseLeave;
begin
inherited MouseLeave;
end;
procedure TCustomBitmappedButton.DoButtonDown();
var
NewState: TBitmappedButtonState;
begin
NewState := bbsDown;
case FState of
bbsNormal, bbsFocused: NewState := bbsDown;
// bbsChecked, bbsCheckedSelected: NewState := bbsCheckedDown;
end;
if NewState <> FState then
begin
FState := NewState;
Invalidate;
end;
end;
procedure TCustomBitmappedButton.DoButtonUp();
var
NewState: TBitmappedButtonState;
begin
if Focused then NewState := bbsFocused
else NewState := bbsNormal;
{ case FState of
bbsCheckedDown:
begin
if Focused then NewState := bbsCheckedSelected
else NewState := bbsChecked;
end;
end;}
if NewState <> FState then
begin
FState := NewState;
Invalidate;
end;
end;
procedure TCustomBitmappedButton.RealSetText(const Value: TCaption);
begin
inherited RealSetText(Value);
// Makes sure that caption changes are drawn
Invalidate;
end;
constructor TCustomBitmappedButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
BmpBuffer := TBitmap.Create;
StrBuffer := TStringList.Create;
FImageBtn := TPicture.Create;
FImageBtnDown := TPicture.Create;
FImageBtnMouseOver := TPicture.Create;
FImageBtnFocused := TPicture.Create;
FImageBtnChecked := TPicture.Create;
FOptions := [{bboDrawSelectionRectangle}];
TabStop := True;
end;
destructor TCustomBitmappedButton.Destroy;
begin
if Assigned(FImageBtn) then FImageBtn.Free;
if Assigned(FImageBtnDown) then FImageBtnDown.Free;
if Assigned(FImageBtnMouseOver) then FImageBtnMouseOver.Free;
if Assigned(FImageBtnFocused) then FImageBtnFocused.Free;
if Assigned(FImageBtnChecked) then FImageBtnChecked.Free;
BmpBuffer.Free;
StrBuffer.Free;
inherited Destroy;
end;
procedure TCustomBitmappedButton.EraseBackground(DC: HDC);
begin
// The correct implementation is doing nothing
end;
procedure TCustomBitmappedButton.Paint;
var
lTextX, lTextY, lTextCX, lTextCY, lTmp, lTextHeightPlusLineSpacing: integer;
i: Integer;
begin
// First draw the button image
BmpBuffer.Width := Width;
BmpBuffer.Height := Height;
BmpBuffer.Canvas.Draw(0, 0, GetStateBitmap());
// Now measure the text position
BmpBuffer.Canvas.Font.Assign(Self.Font);
BmpBuffer.Canvas.Brush.Style := bsClear;
StrBuffer.Text := Caption;
lTextCX := 0;
for i := 0 to StrBuffer.Count - 1 do
begin
lTmp := BmpBuffer.Canvas.TextWidth(StrBuffer.Strings[i]);
lTextCX := Max(lTextCX, lTmp);
end;
lTextHeightPlusLineSpacing := BmpBuffer.Canvas.TextHeight(Caption) + INT_BitmappedButton_LineSpacing;
lTextCY := BmpBuffer.Canvas.TextHeight(Caption) * StrBuffer.Count
+ INT_BitmappedButton_LineSpacing * (StrBuffer.Count - 1);
lTextX := Width div 2 - lTextCX div 2;
lTextY := Height div 2 - lTextCY div 2;
// Draw the text
for i := 0 to StrBuffer.Count - 1 do
begin
BmpBuffer.Canvas.TextOut(lTextX, lTextY + lTextHeightPlusLineSpacing * i, StrBuffer.Strings[i]);
end;
// And flush the buffer to the screen
Canvas.Draw(0, 0, BmpBuffer);
end;
function TCustomBitmappedButton.GetStateBitmap(): TBitmap;
begin
case FState of
bbsDown: Result := FImageBtnDown.Bitmap;
// bbsMouseOver: Result := FImageBtnMouseOver;
bbsFocused:
begin
if bboUseImageForSelection in Options then
Result := FImageBtnFocused.Bitmap
else Result := FImageBtn.Bitmap;
end;
// bbsChecked: Result := FImageBtnChecked;
else
Result := FImageBtn.Bitmap;
end;
end;
{ TCDGroupBox }
procedure TCDGroupBox.PrepareCurrentDrawer();
begin
case DrawStyle of
dsWince: FCurrentDrawer := FDrawerWinCE;
dsCustom: FCurrentDrawer := CustomDrawer;
end;
end;
procedure TCDGroupBox.SetDrawStyle(const AValue: TCDDrawStyle);
begin
if FDrawStyle=AValue then exit;
FDrawStyle:=AValue;
Invalidate;
PrepareCurrentDrawer();
FCurrentDrawer.SetClientRectPos(Self);
end;
constructor TCDGroupBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
TabStop := False;
FDrawerWinCE := TCDGroupBoxDrawerWinCE.Create;
end;
destructor TCDGroupBox.Destroy;
begin
inherited Destroy;
end;
procedure TCDGroupBox.EraseBackground(DC: HDC);
begin
end;
procedure TCDGroupBox.Paint;
var
AImage: TLazIntfImage = nil;
ABmp: TBitmap = nil;
lCanvas: TFPImageCanvas = nil;
begin
inherited Paint;
PrepareCurrentDrawer();
ABmp := TBitmap.Create;
try
ABmp.Width := Width;
ABmp.Height := Height;
AImage := ABmp.CreateIntfImage;
lCanvas := TFPImageCanvas.create(AImage);
FCurrentDrawer.DrawToIntfImage(lCanvas, Self);
ABmp.LoadFromIntfImage(AImage);
Canvas.Draw(0, 0, ABmp);
finally
if lCanvas <> nil then lCanvas.Free;
if AImage <> nil then AImage.Free;
ABmp.Free;
end;
end;
{ TCDGroupBoxDrawerWinCE }
procedure TCDGroupBoxDrawerWinCE.SetClientRectPos(CDGroupBox: TCDGroupBox);
var
lRect: TRect;
lCaptionHeight: Integer;
begin
lCaptionHeight := 10;
lRect := Rect(1, lCaptionHeight, CDGroupBox.Width - 1, CDGroupBox.Height - 1);
CDGroupBox.AdjustClientRect(lRect);
end;
procedure TCDGroupBoxDrawerWinCE.DrawToIntfImage(ADest: TFPImageCanvas;
CDGroupBox: TCDGroupBox);
begin
ADest.Brush.FPColor := colRed;
ADest.Rectangle(0, 0, CDGroupBox.Width, CDGroupBox.height);
end;
{ TCDTrackBar }
{procedure TCDTrackBar.SetMax(Value: Integer);
begin
if Value = FMax then Exit;
FMax := Value;
Invalidate;
end;
procedure TCDTrackBar.SetMin(Value: Integer);
begin
if Value = FMin then Exit;
FMin := Value;
Invalidate;
end;
procedure TCDTrackBar.SetPosition(Value: Integer);
begin
if Value = FPosition then Exit;
FPosition := Value;
Invalidate;
end;
procedure TCDTrackBar.Changed;
begin
end;
procedure TCDTrackBar.DoEnter;
begin
inherited DoEnter;
end;
procedure TCDTrackBar.DoExit;
begin
inherited DoExit;
end;
procedure TCDTrackBar.KeyDown(var Key: Word; Shift: TShiftState);
begin
inherited KeyDown(Key, Shift);
end;
procedure TCDTrackBar.KeyUp(var Key: Word; Shift: TShiftState);
begin
inherited KeyUp(Key, Shift);
end;
procedure TCDTrackBar.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited MouseDown(Button, Shift, X, Y);
end;
procedure TCDTrackBar.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited MouseUp(Button, Shift, X, Y);
end;
procedure TCDTrackBar.MouseEnter;
begin
inherited MouseEnter;
end;
procedure TCDTrackBar.MouseLeave;
begin
inherited MouseLeave;
end;
constructor TCDTrackBar.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;
destructor TCDTrackBar.Destroy;
begin
inherited Destroy;
end;
procedure TCDTrackBar.EraseBackground(DC: HDC);
begin
inherited EraseBackground(DC);
end;
procedure TCDTrackBar.Paint;
begin
inherited Paint;
end;}
{ TCDTrackBarDrawer }
//procedure TCDTrackBarDrawer.DrawToIntfImage(ADest: TLazIntfImage; CDTrackBar: TCDTrackBar);
//begin
// inherited DrawToIntfImage(ADest);
//end;
end.