123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910 |
- unit GR32_Backends_LCL_Win;
- (* ***** BEGIN LICENSE BLOCK *****
- * Version: MPL 1.1 or LGPL 2.1 with linking exception
- *
- * The contents of this file are subject to the Mozilla Public License Version
- * 1.1 (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS" basis,
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- * for the specific language governing rights and limitations under the
- * License.
- *
- * Alternatively, the contents of this file may be used under the terms of the
- * Free Pascal modified version of the GNU Lesser General Public License
- * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
- * of this license are applicable instead of those above.
- * Please see the file LICENSE.txt for additional information concerning this
- * license.
- *
- * The Original Code is Backend Extension for Graphics32
- *
- * The Initial Developer of the Original Code is
- * Andre Beckedorf - metaException
- * [email protected]
- *
- * Portions created by the Initial Developer are Copyright (C) 2007-2012
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s):
- * Christian Budde
- *
- * ***** END LICENSE BLOCK ***** *)
- interface
- {$include GR32.inc}
- uses
- {$IFDEF LCLWin32} Windows, {$ENDIF} LCLIntf, LCLType, Types, Controls,
- SysUtils, Classes, Graphics,
- GR32,
- GR32_Backends,
- GR32_Backends_Generic,
- GR32_Containers,
- GR32_Paths;
- type
- { TLCLBackend }
- { This backend uses the LCL to manage and provide the buffer and additional
- graphics sub system features. The backing buffer is kept in memory. }
- TLCLBackend = class(TCustomBackend,
- IPaintSupport,
- IBitmapContextSupport,
- IDeviceContextSupport,
- ITextSupport,
- IFontSupport,
- ITextToPathSupport,
- ICanvasSupport,
- IInteroperabilitySupport,
- IUpdateRectSupport
- )
- private
- procedure FontChangedHandler(Sender: TObject);
- procedure CanvasChangedHandler(Sender: TObject);
- procedure CanvasChanged;
- procedure FontChanged;
- protected
- FBitmapInfo: TBitmapInfo;
- FBitmapHandle: HBITMAP;
- FHDC: HDC;
- FFont: TFont;
- FCanvas: TCanvas;
- FFontHandle: HFont;
- FMapHandle: THandle;
- FOnFontChange: TNotifyEvent;
- FOnCanvasChange: TNotifyEvent;
- procedure InitializeSurface(NewWidth, NewHeight: Integer; ClearBuffer: Boolean); override;
- procedure FinalizeSurface; override;
- procedure PrepareFileMapping(NewWidth, NewHeight: Integer); virtual;
- public
- constructor Create; override;
- destructor Destroy; override;
- procedure Changed; override;
- function Empty: Boolean; override;
- public
- { IPaintSupport }
- procedure ImageNeeded;
- procedure CheckPixmap;
- procedure DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList; ACanvas: TCanvas); overload;
- procedure DoPaint(ABuffer: TBitmap32; const AInvalidRect: TRect; ACanvas: TCanvas); overload;
- { IBitmapContextSupport }
- function GetBitmapInfo: TBitmapInfo;
- function GetBitmapHandle: THandle;
- property BitmapInfo: TBitmapInfo read GetBitmapInfo;
- property BitmapHandle: THandle read GetBitmapHandle;
- { IDeviceContextSupport }
- function GetHandle: HDC;
- procedure Draw(const DstRect, SrcRect: TRect; hSrc: HDC); overload;
- procedure DrawTo(hDst: HDC; DstX, DstY: Integer); overload;
- procedure DrawTo(hDst: HDC; const DstRect, SrcRect: TRect); overload;
- property Handle: HDC read GetHandle;
- { ITextSupport }
- procedure Textout(X, Y: Integer; const Text: string); overload;
- procedure Textout(X, Y: Integer; const ClipRect: TRect; const Text: string); overload;
- procedure Textout(var DstRect: TRect; const Flags: Cardinal; const Text: string); overload;
- function TextExtent(const Text: string): TSize;
- { IFontSupport }
- function GetOnFontChange: TNotifyEvent;
- procedure SetOnFontChange(Handler: TNotifyEvent);
- function GetFont: TFont;
- procedure SetFont(const Font: TFont);
- procedure UpdateFont;
- property Font: TFont read GetFont write SetFont;
- property OnFontChange: TNotifyEvent read FOnFontChange write FOnFontChange;
- { ITextToPathSupport }
- procedure TextToPath(Path: TCustomPath; const X, Y: TFloat; const Text: string); overload;
- procedure TextToPath(Path: TCustomPath; const DstRect: TFloatRect; const Text: string; Flags: Cardinal); overload;
- function MeasureText(const DstRect: TFloatRect; const Text: string; Flags: Cardinal): TFloatRect;
- { IInteroperabilitySupport }
- function CopyFrom(Graphic: TGraphic): Boolean; overload;
- { ICanvasSupport }
- function GetCanvasChange: TNotifyEvent;
- procedure SetCanvasChange(Handler: TNotifyEvent);
- function GetCanvas: TCanvas;
- procedure DeleteCanvas;
- function CanvasAllocated: Boolean;
- property Canvas: TCanvas read GetCanvas;
- property OnCanvasChange: TNotifyEvent read GetCanvasChange write SetCanvasChange;
- { IUpdateRectSupport }
- procedure InvalidateRect(AControl: TWinControl; const ARect: TRect);
- procedure GetUpdateRects(AControl: TWinControl; AUpdateRects: TRectList; AReservedCapacity: integer; var AFullUpdate: boolean); overload;
- end;
- { TLCLGDIMMFBackend }
- { Same as TGDIBackend but relies on memory mapped files or mapped swap space
- for the backing buffer. }
- TLCLMMFBackend = class(TLCLBackend)
- private
- FMapFileHandle: THandle;
- FMapIsTemporary: Boolean;
- FMapFileName: string;
- protected
- procedure PrepareFileMapping(NewWidth, NewHeight: Integer); override;
- public
- constructor Create(Owner: TBitmap32; IsTemporary: Boolean = True; const MapFileName: string = ''); virtual;
- destructor Destroy; override;
- end;
- { TGDIMemoryBackend }
- { A backend that keeps the backing buffer entirely in memory and offers
- IPaintSupport without allocating a GDI handle }
- { TLCLMemoryBackend }
- TLCLMemoryBackend = class(TMemoryBackend, IPaintSupport, IDeviceContextSupport)
- private
- procedure DoPaintRect(ABuffer: TBitmap32; const ARect: TRect; ACanvas: TCanvas);
- function GetHandle: HDC; // Dummy
- protected
- FBitmapInfo: TBitmapInfo;
- procedure InitializeSurface(NewWidth: Integer; NewHeight: Integer;
- ClearBuffer: Boolean); override;
- public
- constructor Create; override;
- { IPaintSupport }
- procedure ImageNeeded;
- procedure CheckPixmap;
- procedure DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList; ACanvas: TCanvas); overload;
- procedure DoPaint(ABuffer: TBitmap32; const AInvalidRect: TRect; ACanvas: TCanvas); overload;
- { IInteroperabilitySupport }
- function CopyFrom(Graphic: TGraphic): Boolean; overload;
- { IDeviceContextSupport }
- procedure Draw(const DstRect, SrcRect: TRect; hSrc: HDC); overload;
- procedure DrawTo(hDst: HDC; DstX, DstY: Integer); overload;
- procedure DrawTo(hDst: HDC; const DstRect, SrcRect: TRect); overload;
- end;
- implementation
- uses
- GR32.Text.Win;
- var
- StockFont: HFONT;
- { TLCLBackend }
- constructor TLCLBackend.Create;
- begin
- inherited;
- FillChar(FBitmapInfo, SizeOf(TBitmapInfo), 0);
- with FBitmapInfo.bmiHeader do
- begin
- biSize := SizeOf(TBitmapInfoHeader);
- biPlanes := 1;
- biBitCount := 32;
- biCompression := BI_RGB;
- end;
- FMapHandle := 0;
- FFont := TFont.Create;
- FFont.OnChange := FontChangedHandler;
- end;
- destructor TLCLBackend.Destroy;
- begin
- DeleteCanvas;
- FFont.Free;
- inherited;
- end;
- procedure TLCLBackend.InitializeSurface(NewWidth, NewHeight: Integer; ClearBuffer: Boolean);
- begin
- with FBitmapInfo.bmiHeader do
- begin
- biWidth := NewWidth;
- biHeight := -NewHeight;
- biSizeImage := NewWidth * NewHeight * 4;
- end;
- PrepareFileMapping(NewWidth, NewHeight);
- FBitmapHandle := LCLIntf.CreateDIBSection(0, FBitmapInfo, DIB_RGB_COLORS, Pointer(FBits), FMapHandle, 0);
- if FBits = nil then
- raise Exception.Create(RCStrCannotAllocateDIBHandle);
- FHDC := CreateCompatibleDC(0);
- if FHDC = 0 then
- begin
- DeleteObject(FBitmapHandle);
- FBitmapHandle := 0;
- FBits := nil;
- raise Exception.Create(RCStrCannotCreateCompatibleDC);
- end;
- if SelectObject(FHDC, FBitmapHandle) = 0 then
- begin
- DeleteDC(FHDC);
- DeleteObject(FBitmapHandle);
- FHDC := 0;
- FBitmapHandle := 0;
- FBits := nil;
- raise Exception.Create(RCStrCannotSelectAnObjectIntoDC);
- end;
- end;
- procedure TLCLBackend.FinalizeSurface;
- begin
- if FHDC <> 0 then DeleteDC(FHDC);
- FHDC := 0;
- if FBitmapHandle <> 0 then DeleteObject(FBitmapHandle);
- FBitmapHandle := 0;
- FBits := nil;
- end;
- procedure TLCLBackend.PrepareFileMapping(NewWidth, NewHeight: Integer);
- begin
- // to be implemented by descendants
- end;
- procedure TLCLBackend.Changed;
- begin
- if FCanvas <> nil then FCanvas.Handle := Self.Handle;
- inherited;
- end;
- function TLCLBackend.Empty: Boolean;
- begin
- Result := FBitmapHandle = 0;
- end;
- { IPaintSupport }
- procedure TLCLBackend.ImageNeeded;
- begin
- end;
- procedure TLCLBackend.CheckPixmap;
- begin
- end;
- procedure TLCLBackend.DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList; ACanvas: TCanvas);
- var
- i: Integer;
- begin
- for i := 0 to AInvalidRects.Count - 1 do
- with AInvalidRects[i]^ do
- Windows.BitBlt(ACanvas.Handle, Left, Top, Right - Left, Bottom - Top, ABuffer.Handle, Left, Top, SRCCOPY);
- end;
- procedure TLCLBackend.DoPaint(ABuffer: TBitmap32; const AInvalidRect: TRect; ACanvas: TCanvas);
- begin
- Windows.BitBlt(ACanvas.Handle, AInvalidRect.Left, AInvalidRect.Top, AInvalidRect.Width, AInvalidRect.Height, ABuffer.Handle, AInvalidRect.Left, AInvalidRect.Top, SRCCOPY);
- end;
- { IFontSupport }
- function TLCLBackend.GetOnFontChange: TNotifyEvent;
- begin
- Result := FOnFontChange;
- end;
- procedure TLCLBackend.InvalidateRect(AControl: TWinControl; const ARect: TRect);
- begin
- if (AControl.HandleAllocated) then
- Windows.InvalidateRect(AControl.Handle, ARect, False);
- end;
- procedure TLCLBackend.GetUpdateRects(AControl: TWinControl; AUpdateRects: TRectList; AReservedCapacity: integer; var AFullUpdate: boolean);
- var
- DC: HDC;
- RegionType: integer;
- UpdateRegion: HRGN;
- RegionSize: integer;
- RegionData: PRgnData;
- Offset: TPoint;
- i: integer;
- begin
- UpdateRegion := CreateRectRgn(0,0,0,0);
- try
- DC := GetDC(AControl.Handle);
- try
- // On Lazarus the WM_PAINT handler is called from within the BeginPaint/EndPaint
- // block so we cannot use GetUpdateRgb (BeginPaint) clears it. Instead we use
- // GetRandomRgn which can be used within BeginPaint/EndPaint.
- RegionType := GetRandomRgn(DC, UpdateRegion, SYSRGN);
- finally
- ReleaseDC(AControl.Handle, DC);
- end;
- case RegionType of
- 1: // Complex region
- begin
- RegionSize := GetRegionData(UpdateRegion, 0, nil);
- if (RegionSize > 0) then
- begin
- GetMem(RegionData, RegionSize);
- try
- RegionSize := GetRegionData(UpdateRegion, RegionSize, RegionData);
- Assert(RegionSize <> 0);
- // GetRandomRgn returns coordinates relative to the screen.
- // Make them relative to the control.
- Offset := Point(0, 0);
- MapWindowPoints(0, AControl.Handle, Offset, 1);
- for i := 0 to RegionData.rdh.nCount-1 do
- OffsetRect(PPolyRects(@RegionData.Buffer)[i], Offset.X, Offset.Y);
- if (RegionData.rdh.nCount = 1) and (GR32.EqualRect(PPolyRects(@RegionData.Buffer)[0], AControl.ClientRect)) then
- AFullUpdate := True
- else
- begin
- // Final count is known so set capacity to avoid reallocation
- AUpdateRects.Capacity := Max(AUpdateRects.Capacity, AUpdateRects.Count + AReservedCapacity + integer(RegionData.rdh.nCount));
- for i := 0 to RegionData.rdh.nCount-1 do
- AUpdateRects.Add(PPolyRects(@RegionData.Buffer)[i]);
- end;
- finally
- FreeMem(RegionData);
- end;
- end;
- end;
- 0: // Null region
- AFullUpdate := True;
- else
- // Error - Ignore it
- AFullUpdate := True
- end;
- finally
- DeleteObject(UpdateRegion);
- end;
- end;
- function TLCLBackend.GetFont: TFont;
- begin
- Result := FFont;
- end;
- procedure TLCLBackend.SetFont(const Font: TFont);
- begin
- FFont.Assign(Font);
- FontChanged;
- end;
- procedure TLCLBackend.SetOnFontChange(Handler: TNotifyEvent);
- begin
- FOnFontChange := Handler;
- end;
- procedure TLCLBackend.UpdateFont;
- begin
- if (FFontHandle = 0) and (Handle <> 0) then
- begin
- SelectObject(Handle, Font.Handle);
- SetTextColor(Handle, ColorToRGB(Font.Color));
- SetBkMode(Handle, TRANSPARENT);
- FFontHandle := Font.Handle;
- end
- else
- begin
- SelectObject(Handle, FFontHandle);
- SetTextColor(Handle, ColorToRGB(Font.Color));
- SetBkMode(Handle, TRANSPARENT);
- end;
- end;
- procedure TLCLBackend.FontChanged;
- begin
- if Assigned(FOnFontChange) then
- FOnFontChange(Self);
- end;
- procedure TLCLBackend.FontChangedHandler(Sender: TObject);
- begin
- if FFontHandle <> 0 then
- begin
- if Handle <> 0 then SelectObject(Handle, StockFont);
- FFontHandle := 0;
- end;
- FontChanged;
- end;
- { ITextSupport }
- procedure TLCLBackend.Textout(X, Y: Integer; const Text: string);
- var
- Extent: TSize;
- begin
- UpdateFont;
- if not FOwner.MeasuringMode then
- begin
- if FOwner.Clipping then
- ExtTextout(Handle, X, Y, ETO_CLIPPED, @FOwner.ClipRect, PChar(Text), Length(Text), nil)
- else
- ExtTextout(Handle, X, Y, 0, nil, PChar(Text), Length(Text), nil);
- end;
- Extent := TextExtent(Text);
- FOwner.Changed(MakeRect(X, Y, X + Extent.cx + 1, Y + Extent.cy + 1));
- end;
- procedure TLCLBackend.Textout(X, Y: Integer; const ClipRect: TRect; const Text: string);
- var
- Extent: TSize;
- begin
- UpdateFont;
- if not FOwner.MeasuringMode then
- ExtTextout(Handle, X, Y, ETO_CLIPPED, @ClipRect, PChar(Text), Length(Text), nil);
- Extent := TextExtent(Text);
- FOwner.Changed(MakeRect(X, Y, X + Extent.cx + 1, Y + Extent.cy + 1));
- end;
- procedure TLCLBackend.Textout(var DstRect: TRect; const Flags: Cardinal; const Text: string);
- begin
- UpdateFont;
- if not FOwner.MeasuringMode then
- DrawText(Handle, PChar(Text), Length(Text), DstRect, Flags);
- FOwner.Changed(DstRect);
- end;
- function TLCLBackend.TextExtent(const Text: string): TSize;
- var
- DC: HDC;
- OldFont: HGDIOBJ;
- begin
- UpdateFont;
- Result.cX := 0;
- Result.cY := 0;
- if Handle <> 0 then
- GetTextExtentPoint32(Handle, PChar(Text), Length(Text), Result)
- else
- begin
- StockBitmap.Canvas.Lock;
- try
- DC := StockBitmap.Canvas.Handle;
- OldFont := SelectObject(DC, Font.Handle);
- GetTextExtentPoint32(DC, PChar(Text), Length(Text), Result);
- SelectObject(DC, OldFont);
- finally
- StockBitmap.Canvas.Unlock;
- end;
- end;
- end;
- { ITextToPathSupport }
- procedure TLCLBackend.TextToPath(Path: TCustomPath; const X, Y: TFloat;
- const Text: string);
- var
- R: TFloatRect;
- begin
- R := FloatRect(X, Y, X, Y);
- TextToolsWin.TextToPath(Font.Handle, Path, R, Text, 0);
- end;
- procedure TLCLBackend.TextToPath(Path: TCustomPath; const DstRect: TFloatRect;
- const Text: string; Flags: Cardinal);
- begin
- TextToolsWin.TextToPath(Font.Handle, Path, DstRect, Text, Flags);
- end;
- function TLCLBackend.MeasureText(const DstRect: TFloatRect;
- const Text: string; Flags: Cardinal): TFloatRect;
- begin
- Result := TextToolsWin.MeasureText(Font.Handle, DstRect, Text, Flags);
- end;
- procedure TLCLBackend.DrawTo(hDst: HDC; DstX, DstY: Integer);
- begin
- Windows.BitBlt(hDst, DstX, DstY, FOwner.Width, FOwner.Height, Handle, 0, 0, SRCCOPY);
- (*
- StretchDIBits(
- hDst, DstX, DstY, FOwner.Width, FOwner.Height,
- 0, 0, FOwner.Width, FOwner.Height, Bits, FBitmapInfo, DIB_RGB_COLORS, SRCCOPY);
- *)
- end;
- procedure TLCLBackend.DrawTo(hDst: HDC; const DstRect, SrcRect: TRect);
- begin
- Windows.StretchBlt(hDst,
- DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left, DstRect.Bottom - DstRect.Top, Handle,
- SrcRect.Left, SrcRect.Top, SrcRect.Right - SrcRect.Left, SrcRect.Bottom - SrcRect.Top, SRCCOPY);
- end;
- function TLCLBackend.GetBitmapHandle: THandle;
- begin
- Result := FBitmapHandle;
- end;
- function TLCLBackend.GetBitmapInfo: TBitmapInfo;
- begin
- Result := FBitmapInfo;
- end;
- { IInteroperabilitySupport }
- type
- TGraphicAccess = class(TGraphic);
- function TLCLBackend.CopyFrom(Graphic: TGraphic): Boolean;
- begin
- TGraphicAccess(Graphic).Draw(Canvas, MakeRect(0, 0, Canvas.Width, Canvas.Height));
- Result := True;
- end;
- { ICanvasSupport }
- procedure TLCLBackend.DeleteCanvas;
- begin
- if Assigned(FCanvas) then
- begin
- FCanvas.Handle := 0;
- FCanvas.Free;
- FCanvas := nil;
- end;
- end;
- function TLCLBackend.CanvasAllocated: Boolean;
- begin
- Result := Assigned(FCanvas);
- end;
- procedure TLCLBackend.CanvasChanged;
- begin
- if Assigned(FOnCanvasChange) then
- FOnCanvasChange(Self);
- end;
- function TLCLBackend.GetCanvas: TCanvas;
- begin
- if not Assigned(FCanvas) then
- begin
- FCanvas := TCanvas.Create;
- FCanvas.Handle := Handle;
- FCanvas.OnChange := CanvasChangedHandler;
- end;
- Result := FCanvas;
- end;
- function TLCLBackend.GetCanvasChange: TNotifyEvent;
- begin
- Result := FOnCanvasChange;
- end;
- procedure TLCLBackend.CanvasChangedHandler(Sender: TObject);
- begin
- CanvasChanged;
- end;
- { IDeviceContextSupport }
- function TLCLBackend.GetHandle: HDC;
- begin
- Result := FHDC;
- end;
- procedure TLCLBackend.SetCanvasChange(Handler: TNotifyEvent);
- begin
- FOnCanvasChange := Handler;
- end;
- procedure TLCLBackend.Draw(const DstRect, SrcRect: TRect; hSrc: HDC);
- begin
- if FOwner.Empty then Exit;
- if not FOwner.MeasuringMode then
- Windows.StretchBlt(Handle, DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left,
- DstRect.Bottom - DstRect.Top, hSrc, SrcRect.Left, SrcRect.Top,
- SrcRect.Right - SrcRect.Left, SrcRect.Bottom - SrcRect.Top, SRCCOPY);
- FOwner.Changed(DstRect);
- end;
- { TLCLMMFBackend }
- constructor TLCLMMFBackend.Create(Owner: TBitmap32; IsTemporary: Boolean = True; const MapFileName: string = '');
- begin
- FMapFileName := MapFileName;
- FMapIsTemporary := IsTemporary;
- TMMFBackend.InitializeFileMapping(FMapHandle, FMapFileHandle, FMapFileName);
- inherited Create(Owner);
- end;
- destructor TLCLMMFBackend.Destroy;
- begin
- TMMFBackend.DeinitializeFileMapping(FMapHandle, FMapFileHandle, FMapFileName);
- inherited;
- end;
- procedure TLCLMMFBackend.PrepareFileMapping(NewWidth, NewHeight: Integer);
- begin
- TMMFBackend.CreateFileMapping(FMapHandle, FMapFileHandle, FMapFileName, FMapIsTemporary, NewWidth, NewHeight);
- end;
- { TLCLMemoryBackend }
- constructor TLCLMemoryBackend.Create;
- begin
- inherited;
- FillChar(FBitmapInfo, SizeOf(TBitmapInfo), 0);
- with FBitmapInfo.bmiHeader do
- begin
- biSize := SizeOf(TBitmapInfoHeader);
- biPlanes := 1;
- biBitCount := 32;
- biCompression := BI_RGB;
- biXPelsPerMeter := 96;
- biYPelsPerMeter := 96;
- biClrUsed := 0;
- end;
- end;
- procedure TLCLMemoryBackend.InitializeSurface(NewWidth: Integer;
- NewHeight: Integer; ClearBuffer: Boolean);
- begin
- inherited;
- with FBitmapInfo.bmiHeader do
- begin
- biWidth := NewWidth;
- biHeight := -NewHeight;
- end;
- end;
- { IPaintSupport }
- procedure TLCLMemoryBackend.ImageNeeded;
- begin
- end;
- procedure TLCLMemoryBackend.CheckPixmap;
- begin
- end;
- procedure TLCLMemoryBackend.DoPaintRect(ABuffer: TBitmap32; const ARect: TRect; ACanvas: TCanvas);
- var
- Bitmap : HBITMAP;
- DeviceContext : HDC;
- Buffer : Pointer;
- OldObject : HGDIOBJ;
- begin
- {$IFDEF LCLWin32}
- if SetDIBitsToDevice(ACanvas.Handle, ARect.Left, ARect.Top, ARect.Right -
- ARect.Left, ARect.Bottom - ARect.Top, ARect.Left, ARect.Top, 0,
- ARect.Bottom - ARect.Top, ABuffer.Bits, Windows.BITMAPINFO(FBitmapInfo), DIB_RGB_COLORS) = 0 then
- begin
- // create compatible device context
- DeviceContext := CreateCompatibleDC(ACanvas.Handle);
- if DeviceContext <> 0 then
- try
- Bitmap := CreateDIBSection(DeviceContext, FBitmapInfo, DIB_RGB_COLORS,
- Buffer, 0, 0);
- if Bitmap <> 0 then
- begin
- OldObject := SelectObject(DeviceContext, Bitmap);
- try
- Move(ABuffer.Bits^, Buffer^, FBitmapInfo.bmiHeader.biWidth * FBitmapInfo.bmiHeader.biHeight * SizeOf(Cardinal));
- Windows.BitBlt(ACanvas.Handle, ARect.Left, ARect.Top, ARect.Right - ARect.Left, ARect.Bottom - ARect.Top, DeviceContext, 0, 0, SRCCOPY);
- finally
- if OldObject <> 0 then
- SelectObject(DeviceContext, OldObject);
- DeleteObject(Bitmap);
- end;
- end else
- raise Exception.Create(RCStrCannotCreateCompatibleDC);
- finally
- DeleteDC(DeviceContext);
- end;
- end;
- {$ELSE}
- raise Exception.Create('"SetDIBitsToDevice" is only included in windows unit!')
- {$ENDIF}
- end;
- procedure TLCLMemoryBackend.DoPaint(ABuffer: TBitmap32; const AInvalidRect: TRect; ACanvas: TCanvas);
- begin
- DoPaintRect(ABuffer, AInvalidRect, ACanvas)
- end;
- procedure TLCLMemoryBackend.DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList; ACanvas: TCanvas);
- var
- i : Integer;
- begin
- for i := 0 to AInvalidRects.Count - 1 do
- DoPaintRect(ABuffer, AInvalidRects[i]^, ACanvas);
- end;
- { IInteroperabilitySupport }
- function TLCLMemoryBackend.CopyFrom(Graphic: TGraphic): Boolean;
- begin
- // yet todo
- Result := False;
- end;
- { IDeviceContextSupport }
- procedure TLCLMemoryBackend.Draw(const DstRect, SrcRect: TRect; hSrc: HDC);
- begin
- if FOwner.Empty then Exit;
- if not FOwner.MeasuringMode then
- raise Exception.Create('Not yet supported!');
- FOwner.Changed(DstRect);
- end;
- procedure TLCLMemoryBackend.DrawTo(hDst: HDC; DstX, DstY: Integer);
- var
- Bitmap: HBITMAP;
- DeviceContext: HDC;
- Buffer: Pointer;
- OldObject: HGDIOBJ;
- begin
- {$IFDEF LCLWin32}
- if SetDIBitsToDevice(hDst, DstX, DstY,
- FOwner.Width, FOwner.Height, 0, 0, 0, FOwner.Height, FBits,
- Windows.BITMAPINFO(FBitmapInfo), DIB_RGB_COLORS) = 0 then
- begin
- // create compatible device context
- DeviceContext := CreateCompatibleDC(hDst);
- if DeviceContext <> 0 then
- try
- Bitmap := CreateDIBSection(DeviceContext, FBitmapInfo, DIB_RGB_COLORS, Buffer, 0, 0);
- if Bitmap <> 0 then
- begin
- OldObject := SelectObject(DeviceContext, Bitmap);
- try
- Move(FBits^, Buffer^, FBitmapInfo.bmiHeader.biWidth *
- FBitmapInfo.bmiHeader.biHeight * SizeOf(Cardinal));
- Windows.BitBlt(hDst, DstX, DstY, FOwner.Width, FOwner.Height, DeviceContext,
- 0, 0, SRCCOPY);
- finally
- if OldObject <> 0 then
- SelectObject(DeviceContext, OldObject);
- DeleteObject(Bitmap);
- end;
- end else
- raise Exception.Create('Can''t create compatible DC''');
- finally
- DeleteDC(DeviceContext);
- end;
- end;
- {$ELSE}
- raise Exception.Create('"SetDIBitsToDevice" is only included in windows unit!')
- {$ENDIF}
- end;
- procedure TLCLMemoryBackend.DrawTo(hDst: HDC; const DstRect, SrcRect: TRect);
- var
- Bitmap: HBITMAP;
- DeviceContext: HDC;
- Buffer: Pointer;
- OldObject: HGDIOBJ;
- begin
- {$IFDEF LCLWin32}
- if SetDIBitsToDevice(hDst, DstRect.Left, DstRect.Top,
- DstRect.Right - DstRect.Left, DstRect.Bottom - DstRect.Top, SrcRect.Left,
- SrcRect.Top, 0, SrcRect.Bottom - SrcRect.Top, FBits,
- Windows.BITMAPINFO(FBitmapInfo), DIB_RGB_COLORS) = 0 then
- begin
- // create compatible device context
- DeviceContext := CreateCompatibleDC(hDst);
- if DeviceContext <> 0 then
- try
- Buffer := nil;
- Bitmap := CreateDIBSection(DeviceContext, FBitmapInfo, DIB_RGB_COLORS,
- Buffer, 0, 0);
- if Bitmap <> 0 then
- begin
- OldObject := SelectObject(DeviceContext, Bitmap);
- try
- Move(FBits^, Buffer^, FBitmapInfo.bmiHeader.biWidth *
- FBitmapInfo.bmiHeader.biHeight * SizeOf(Cardinal));
- Windows.BitBlt(hDst, DstRect.Left, DstRect.Top, DstRect.Right -
- DstRect.Left, DstRect.Bottom - DstRect.Top, DeviceContext, 0, 0, SRCCOPY);
- finally
- if OldObject <> 0 then
- SelectObject(DeviceContext, OldObject);
- DeleteObject(Bitmap);
- end;
- end else
- raise Exception.Create('Can''t create compatible DC''');
- finally
- DeleteDC(DeviceContext);
- end;
- end;
- {$ELSE}
- raise Exception.Create('"SetDIBitsToDevice" is only included in windows unit!')
- {$ENDIF}
- end;
- function TLCLMemoryBackend.GetHandle: HDC;
- begin
- Result := 0;
- end;
- initialization
- StockFont := GetStockObject(SYSTEM_FONT);
- finalization
- end.
|