GR32_Backends_LCL_Win.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. unit GR32_Backends_LCL_Win;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Backend Extension for Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Andre Beckedorf - metaException
  26. * [email protected]
  27. *
  28. * Portions created by the Initial Developer are Copyright (C) 2007-2012
  29. * the Initial Developer. All Rights Reserved.
  30. *
  31. * Contributor(s):
  32. * Christian Budde
  33. *
  34. * ***** END LICENSE BLOCK ***** *)
  35. interface
  36. {$include GR32.inc}
  37. uses
  38. {$IFDEF LCLWin32} Windows, {$ENDIF} LCLIntf, LCLType, Types, Controls,
  39. SysUtils, Classes, Graphics,
  40. GR32,
  41. GR32_Backends,
  42. GR32_Backends_Generic,
  43. GR32_Containers,
  44. GR32_Paths;
  45. type
  46. { TLCLBackend }
  47. { This backend uses the LCL to manage and provide the buffer and additional
  48. graphics sub system features. The backing buffer is kept in memory. }
  49. TLCLBackend = class(TCustomBackend,
  50. IPaintSupport,
  51. IBitmapContextSupport,
  52. IDeviceContextSupport,
  53. ITextSupport,
  54. IFontSupport,
  55. ITextToPathSupport,
  56. ICanvasSupport,
  57. IInteroperabilitySupport,
  58. IUpdateRectSupport
  59. )
  60. private
  61. procedure FontChangedHandler(Sender: TObject);
  62. procedure CanvasChangedHandler(Sender: TObject);
  63. procedure CanvasChanged;
  64. procedure FontChanged;
  65. protected
  66. FBitmapInfo: TBitmapInfo;
  67. FBitmapHandle: HBITMAP;
  68. FHDC: HDC;
  69. FFont: TFont;
  70. FCanvas: TCanvas;
  71. FFontHandle: HFont;
  72. FMapHandle: THandle;
  73. FOnFontChange: TNotifyEvent;
  74. FOnCanvasChange: TNotifyEvent;
  75. procedure InitializeSurface(NewWidth, NewHeight: Integer; ClearBuffer: Boolean); override;
  76. procedure FinalizeSurface; override;
  77. procedure PrepareFileMapping(NewWidth, NewHeight: Integer); virtual;
  78. public
  79. constructor Create; override;
  80. destructor Destroy; override;
  81. procedure Changed; override;
  82. function Empty: Boolean; override;
  83. public
  84. { IPaintSupport }
  85. procedure ImageNeeded;
  86. procedure CheckPixmap;
  87. procedure DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList; ACanvas: TCanvas); overload;
  88. procedure DoPaint(ABuffer: TBitmap32; const AInvalidRect: TRect; ACanvas: TCanvas); overload;
  89. { IBitmapContextSupport }
  90. function GetBitmapInfo: TBitmapInfo;
  91. function GetBitmapHandle: THandle;
  92. property BitmapInfo: TBitmapInfo read GetBitmapInfo;
  93. property BitmapHandle: THandle read GetBitmapHandle;
  94. { IDeviceContextSupport }
  95. function GetHandle: HDC;
  96. procedure Draw(const DstRect, SrcRect: TRect; hSrc: HDC); overload;
  97. procedure DrawTo(hDst: HDC; DstX, DstY: Integer); overload;
  98. procedure DrawTo(hDst: HDC; const DstRect, SrcRect: TRect); overload;
  99. property Handle: HDC read GetHandle;
  100. { ITextSupport }
  101. procedure Textout(X, Y: Integer; const Text: string); overload;
  102. procedure Textout(X, Y: Integer; const ClipRect: TRect; const Text: string); overload;
  103. procedure Textout(var DstRect: TRect; const Flags: Cardinal; const Text: string); overload;
  104. function TextExtent(const Text: string): TSize;
  105. { IFontSupport }
  106. function GetOnFontChange: TNotifyEvent;
  107. procedure SetOnFontChange(Handler: TNotifyEvent);
  108. function GetFont: TFont;
  109. procedure SetFont(const Font: TFont);
  110. procedure UpdateFont;
  111. property Font: TFont read GetFont write SetFont;
  112. property OnFontChange: TNotifyEvent read FOnFontChange write FOnFontChange;
  113. { ITextToPathSupport }
  114. procedure TextToPath(Path: TCustomPath; const X, Y: TFloat; const Text: string); overload;
  115. procedure TextToPath(Path: TCustomPath; const DstRect: TFloatRect; const Text: string; Flags: Cardinal); overload;
  116. function MeasureText(const DstRect: TFloatRect; const Text: string; Flags: Cardinal): TFloatRect;
  117. { IInteroperabilitySupport }
  118. function CopyFrom(Graphic: TGraphic): Boolean; overload;
  119. { ICanvasSupport }
  120. function GetCanvasChange: TNotifyEvent;
  121. procedure SetCanvasChange(Handler: TNotifyEvent);
  122. function GetCanvas: TCanvas;
  123. procedure DeleteCanvas;
  124. function CanvasAllocated: Boolean;
  125. property Canvas: TCanvas read GetCanvas;
  126. property OnCanvasChange: TNotifyEvent read GetCanvasChange write SetCanvasChange;
  127. { IUpdateRectSupport }
  128. procedure InvalidateRect(AControl: TWinControl; const ARect: TRect);
  129. procedure GetUpdateRects(AControl: TWinControl; AUpdateRects: TRectList; AReservedCapacity: integer; var AFullUpdate: boolean); overload;
  130. end;
  131. { TLCLGDIMMFBackend }
  132. { Same as TGDIBackend but relies on memory mapped files or mapped swap space
  133. for the backing buffer. }
  134. TLCLMMFBackend = class(TLCLBackend)
  135. private
  136. FMapFileHandle: THandle;
  137. FMapIsTemporary: Boolean;
  138. FMapFileName: string;
  139. protected
  140. procedure PrepareFileMapping(NewWidth, NewHeight: Integer); override;
  141. public
  142. constructor Create(Owner: TBitmap32; IsTemporary: Boolean = True; const MapFileName: string = ''); virtual;
  143. destructor Destroy; override;
  144. end;
  145. { TGDIMemoryBackend }
  146. { A backend that keeps the backing buffer entirely in memory and offers
  147. IPaintSupport without allocating a GDI handle }
  148. { TLCLMemoryBackend }
  149. TLCLMemoryBackend = class(TMemoryBackend, IPaintSupport, IDeviceContextSupport)
  150. private
  151. procedure DoPaintRect(ABuffer: TBitmap32; const ARect: TRect; ACanvas: TCanvas);
  152. function GetHandle: HDC; // Dummy
  153. protected
  154. FBitmapInfo: TBitmapInfo;
  155. procedure InitializeSurface(NewWidth: Integer; NewHeight: Integer;
  156. ClearBuffer: Boolean); override;
  157. public
  158. constructor Create; override;
  159. { IPaintSupport }
  160. procedure ImageNeeded;
  161. procedure CheckPixmap;
  162. procedure DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList; ACanvas: TCanvas); overload;
  163. procedure DoPaint(ABuffer: TBitmap32; const AInvalidRect: TRect; ACanvas: TCanvas); overload;
  164. { IInteroperabilitySupport }
  165. function CopyFrom(Graphic: TGraphic): Boolean; overload;
  166. { IDeviceContextSupport }
  167. procedure Draw(const DstRect, SrcRect: TRect; hSrc: HDC); overload;
  168. procedure DrawTo(hDst: HDC; DstX, DstY: Integer); overload;
  169. procedure DrawTo(hDst: HDC; const DstRect, SrcRect: TRect); overload;
  170. end;
  171. implementation
  172. uses
  173. GR32.Text.Win;
  174. var
  175. StockFont: HFONT;
  176. { TLCLBackend }
  177. constructor TLCLBackend.Create;
  178. begin
  179. inherited;
  180. FillChar(FBitmapInfo, SizeOf(TBitmapInfo), 0);
  181. with FBitmapInfo.bmiHeader do
  182. begin
  183. biSize := SizeOf(TBitmapInfoHeader);
  184. biPlanes := 1;
  185. biBitCount := 32;
  186. biCompression := BI_RGB;
  187. end;
  188. FMapHandle := 0;
  189. FFont := TFont.Create;
  190. FFont.OnChange := FontChangedHandler;
  191. end;
  192. destructor TLCLBackend.Destroy;
  193. begin
  194. DeleteCanvas;
  195. FFont.Free;
  196. inherited;
  197. end;
  198. procedure TLCLBackend.InitializeSurface(NewWidth, NewHeight: Integer; ClearBuffer: Boolean);
  199. begin
  200. with FBitmapInfo.bmiHeader do
  201. begin
  202. biWidth := NewWidth;
  203. biHeight := -NewHeight;
  204. biSizeImage := NewWidth * NewHeight * 4;
  205. end;
  206. PrepareFileMapping(NewWidth, NewHeight);
  207. FBitmapHandle := LCLIntf.CreateDIBSection(0, FBitmapInfo, DIB_RGB_COLORS, Pointer(FBits), FMapHandle, 0);
  208. if FBits = nil then
  209. raise Exception.Create(RCStrCannotAllocateDIBHandle);
  210. FHDC := CreateCompatibleDC(0);
  211. if FHDC = 0 then
  212. begin
  213. DeleteObject(FBitmapHandle);
  214. FBitmapHandle := 0;
  215. FBits := nil;
  216. raise Exception.Create(RCStrCannotCreateCompatibleDC);
  217. end;
  218. if SelectObject(FHDC, FBitmapHandle) = 0 then
  219. begin
  220. DeleteDC(FHDC);
  221. DeleteObject(FBitmapHandle);
  222. FHDC := 0;
  223. FBitmapHandle := 0;
  224. FBits := nil;
  225. raise Exception.Create(RCStrCannotSelectAnObjectIntoDC);
  226. end;
  227. end;
  228. procedure TLCLBackend.FinalizeSurface;
  229. begin
  230. if FHDC <> 0 then DeleteDC(FHDC);
  231. FHDC := 0;
  232. if FBitmapHandle <> 0 then DeleteObject(FBitmapHandle);
  233. FBitmapHandle := 0;
  234. FBits := nil;
  235. end;
  236. procedure TLCLBackend.PrepareFileMapping(NewWidth, NewHeight: Integer);
  237. begin
  238. // to be implemented by descendants
  239. end;
  240. procedure TLCLBackend.Changed;
  241. begin
  242. if FCanvas <> nil then FCanvas.Handle := Self.Handle;
  243. inherited;
  244. end;
  245. function TLCLBackend.Empty: Boolean;
  246. begin
  247. Result := FBitmapHandle = 0;
  248. end;
  249. { IPaintSupport }
  250. procedure TLCLBackend.ImageNeeded;
  251. begin
  252. end;
  253. procedure TLCLBackend.CheckPixmap;
  254. begin
  255. end;
  256. procedure TLCLBackend.DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList; ACanvas: TCanvas);
  257. var
  258. i: Integer;
  259. begin
  260. for i := 0 to AInvalidRects.Count - 1 do
  261. with AInvalidRects[i]^ do
  262. Windows.BitBlt(ACanvas.Handle, Left, Top, Right - Left, Bottom - Top, ABuffer.Handle, Left, Top, SRCCOPY);
  263. end;
  264. procedure TLCLBackend.DoPaint(ABuffer: TBitmap32; const AInvalidRect: TRect; ACanvas: TCanvas);
  265. begin
  266. Windows.BitBlt(ACanvas.Handle, AInvalidRect.Left, AInvalidRect.Top, AInvalidRect.Width, AInvalidRect.Height, ABuffer.Handle, AInvalidRect.Left, AInvalidRect.Top, SRCCOPY);
  267. end;
  268. { IFontSupport }
  269. function TLCLBackend.GetOnFontChange: TNotifyEvent;
  270. begin
  271. Result := FOnFontChange;
  272. end;
  273. procedure TLCLBackend.InvalidateRect(AControl: TWinControl; const ARect: TRect);
  274. begin
  275. if (AControl.HandleAllocated) then
  276. Windows.InvalidateRect(AControl.Handle, ARect, False);
  277. end;
  278. procedure TLCLBackend.GetUpdateRects(AControl: TWinControl; AUpdateRects: TRectList; AReservedCapacity: integer; var AFullUpdate: boolean);
  279. var
  280. DC: HDC;
  281. RegionType: integer;
  282. UpdateRegion: HRGN;
  283. RegionSize: integer;
  284. RegionData: PRgnData;
  285. Offset: TPoint;
  286. i: integer;
  287. begin
  288. UpdateRegion := CreateRectRgn(0,0,0,0);
  289. try
  290. DC := GetDC(AControl.Handle);
  291. try
  292. // On Lazarus the WM_PAINT handler is called from within the BeginPaint/EndPaint
  293. // block so we cannot use GetUpdateRgb (BeginPaint) clears it. Instead we use
  294. // GetRandomRgn which can be used within BeginPaint/EndPaint.
  295. RegionType := GetRandomRgn(DC, UpdateRegion, SYSRGN);
  296. finally
  297. ReleaseDC(AControl.Handle, DC);
  298. end;
  299. case RegionType of
  300. 1: // Complex region
  301. begin
  302. RegionSize := GetRegionData(UpdateRegion, 0, nil);
  303. if (RegionSize > 0) then
  304. begin
  305. GetMem(RegionData, RegionSize);
  306. try
  307. RegionSize := GetRegionData(UpdateRegion, RegionSize, RegionData);
  308. Assert(RegionSize <> 0);
  309. // GetRandomRgn returns coordinates relative to the screen.
  310. // Make them relative to the control.
  311. Offset := Point(0, 0);
  312. MapWindowPoints(0, AControl.Handle, Offset, 1);
  313. for i := 0 to RegionData.rdh.nCount-1 do
  314. OffsetRect(PPolyRects(@RegionData.Buffer)[i], Offset.X, Offset.Y);
  315. if (RegionData.rdh.nCount = 1) and (GR32.EqualRect(PPolyRects(@RegionData.Buffer)[0], AControl.ClientRect)) then
  316. AFullUpdate := True
  317. else
  318. begin
  319. // Final count is known so set capacity to avoid reallocation
  320. AUpdateRects.Capacity := Max(AUpdateRects.Capacity, AUpdateRects.Count + AReservedCapacity + integer(RegionData.rdh.nCount));
  321. for i := 0 to RegionData.rdh.nCount-1 do
  322. AUpdateRects.Add(PPolyRects(@RegionData.Buffer)[i]);
  323. end;
  324. finally
  325. FreeMem(RegionData);
  326. end;
  327. end;
  328. end;
  329. 0: // Null region
  330. AFullUpdate := True;
  331. else
  332. // Error - Ignore it
  333. AFullUpdate := True
  334. end;
  335. finally
  336. DeleteObject(UpdateRegion);
  337. end;
  338. end;
  339. function TLCLBackend.GetFont: TFont;
  340. begin
  341. Result := FFont;
  342. end;
  343. procedure TLCLBackend.SetFont(const Font: TFont);
  344. begin
  345. FFont.Assign(Font);
  346. FontChanged;
  347. end;
  348. procedure TLCLBackend.SetOnFontChange(Handler: TNotifyEvent);
  349. begin
  350. FOnFontChange := Handler;
  351. end;
  352. procedure TLCLBackend.UpdateFont;
  353. begin
  354. if (FFontHandle = 0) and (Handle <> 0) then
  355. begin
  356. SelectObject(Handle, Font.Handle);
  357. SetTextColor(Handle, ColorToRGB(Font.Color));
  358. SetBkMode(Handle, TRANSPARENT);
  359. FFontHandle := Font.Handle;
  360. end
  361. else
  362. begin
  363. SelectObject(Handle, FFontHandle);
  364. SetTextColor(Handle, ColorToRGB(Font.Color));
  365. SetBkMode(Handle, TRANSPARENT);
  366. end;
  367. end;
  368. procedure TLCLBackend.FontChanged;
  369. begin
  370. if Assigned(FOnFontChange) then
  371. FOnFontChange(Self);
  372. end;
  373. procedure TLCLBackend.FontChangedHandler(Sender: TObject);
  374. begin
  375. if FFontHandle <> 0 then
  376. begin
  377. if Handle <> 0 then SelectObject(Handle, StockFont);
  378. FFontHandle := 0;
  379. end;
  380. FontChanged;
  381. end;
  382. { ITextSupport }
  383. procedure TLCLBackend.Textout(X, Y: Integer; const Text: string);
  384. var
  385. Extent: TSize;
  386. begin
  387. UpdateFont;
  388. if not FOwner.MeasuringMode then
  389. begin
  390. if FOwner.Clipping then
  391. ExtTextout(Handle, X, Y, ETO_CLIPPED, @FOwner.ClipRect, PChar(Text), Length(Text), nil)
  392. else
  393. ExtTextout(Handle, X, Y, 0, nil, PChar(Text), Length(Text), nil);
  394. end;
  395. Extent := TextExtent(Text);
  396. FOwner.Changed(MakeRect(X, Y, X + Extent.cx + 1, Y + Extent.cy + 1));
  397. end;
  398. procedure TLCLBackend.Textout(X, Y: Integer; const ClipRect: TRect; const Text: string);
  399. var
  400. Extent: TSize;
  401. begin
  402. UpdateFont;
  403. if not FOwner.MeasuringMode then
  404. ExtTextout(Handle, X, Y, ETO_CLIPPED, @ClipRect, PChar(Text), Length(Text), nil);
  405. Extent := TextExtent(Text);
  406. FOwner.Changed(MakeRect(X, Y, X + Extent.cx + 1, Y + Extent.cy + 1));
  407. end;
  408. procedure TLCLBackend.Textout(var DstRect: TRect; const Flags: Cardinal; const Text: string);
  409. begin
  410. UpdateFont;
  411. if not FOwner.MeasuringMode then
  412. DrawText(Handle, PChar(Text), Length(Text), DstRect, Flags);
  413. FOwner.Changed(DstRect);
  414. end;
  415. function TLCLBackend.TextExtent(const Text: string): TSize;
  416. var
  417. DC: HDC;
  418. OldFont: HGDIOBJ;
  419. begin
  420. UpdateFont;
  421. Result.cX := 0;
  422. Result.cY := 0;
  423. if Handle <> 0 then
  424. GetTextExtentPoint32(Handle, PChar(Text), Length(Text), Result)
  425. else
  426. begin
  427. StockBitmap.Canvas.Lock;
  428. try
  429. DC := StockBitmap.Canvas.Handle;
  430. OldFont := SelectObject(DC, Font.Handle);
  431. GetTextExtentPoint32(DC, PChar(Text), Length(Text), Result);
  432. SelectObject(DC, OldFont);
  433. finally
  434. StockBitmap.Canvas.Unlock;
  435. end;
  436. end;
  437. end;
  438. { ITextToPathSupport }
  439. procedure TLCLBackend.TextToPath(Path: TCustomPath; const X, Y: TFloat;
  440. const Text: string);
  441. var
  442. R: TFloatRect;
  443. begin
  444. R := FloatRect(X, Y, X, Y);
  445. TextToolsWin.TextToPath(Font.Handle, Path, R, Text, 0);
  446. end;
  447. procedure TLCLBackend.TextToPath(Path: TCustomPath; const DstRect: TFloatRect;
  448. const Text: string; Flags: Cardinal);
  449. begin
  450. TextToolsWin.TextToPath(Font.Handle, Path, DstRect, Text, Flags);
  451. end;
  452. function TLCLBackend.MeasureText(const DstRect: TFloatRect;
  453. const Text: string; Flags: Cardinal): TFloatRect;
  454. begin
  455. Result := TextToolsWin.MeasureText(Font.Handle, DstRect, Text, Flags);
  456. end;
  457. procedure TLCLBackend.DrawTo(hDst: HDC; DstX, DstY: Integer);
  458. begin
  459. Windows.BitBlt(hDst, DstX, DstY, FOwner.Width, FOwner.Height, Handle, 0, 0, SRCCOPY);
  460. (*
  461. StretchDIBits(
  462. hDst, DstX, DstY, FOwner.Width, FOwner.Height,
  463. 0, 0, FOwner.Width, FOwner.Height, Bits, FBitmapInfo, DIB_RGB_COLORS, SRCCOPY);
  464. *)
  465. end;
  466. procedure TLCLBackend.DrawTo(hDst: HDC; const DstRect, SrcRect: TRect);
  467. begin
  468. Windows.StretchBlt(hDst,
  469. DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left, DstRect.Bottom - DstRect.Top, Handle,
  470. SrcRect.Left, SrcRect.Top, SrcRect.Right - SrcRect.Left, SrcRect.Bottom - SrcRect.Top, SRCCOPY);
  471. end;
  472. function TLCLBackend.GetBitmapHandle: THandle;
  473. begin
  474. Result := FBitmapHandle;
  475. end;
  476. function TLCLBackend.GetBitmapInfo: TBitmapInfo;
  477. begin
  478. Result := FBitmapInfo;
  479. end;
  480. { IInteroperabilitySupport }
  481. type
  482. TGraphicAccess = class(TGraphic);
  483. function TLCLBackend.CopyFrom(Graphic: TGraphic): Boolean;
  484. begin
  485. TGraphicAccess(Graphic).Draw(Canvas, MakeRect(0, 0, Canvas.Width, Canvas.Height));
  486. Result := True;
  487. end;
  488. { ICanvasSupport }
  489. procedure TLCLBackend.DeleteCanvas;
  490. begin
  491. if Assigned(FCanvas) then
  492. begin
  493. FCanvas.Handle := 0;
  494. FCanvas.Free;
  495. FCanvas := nil;
  496. end;
  497. end;
  498. function TLCLBackend.CanvasAllocated: Boolean;
  499. begin
  500. Result := Assigned(FCanvas);
  501. end;
  502. procedure TLCLBackend.CanvasChanged;
  503. begin
  504. if Assigned(FOnCanvasChange) then
  505. FOnCanvasChange(Self);
  506. end;
  507. function TLCLBackend.GetCanvas: TCanvas;
  508. begin
  509. if not Assigned(FCanvas) then
  510. begin
  511. FCanvas := TCanvas.Create;
  512. FCanvas.Handle := Handle;
  513. FCanvas.OnChange := CanvasChangedHandler;
  514. end;
  515. Result := FCanvas;
  516. end;
  517. function TLCLBackend.GetCanvasChange: TNotifyEvent;
  518. begin
  519. Result := FOnCanvasChange;
  520. end;
  521. procedure TLCLBackend.CanvasChangedHandler(Sender: TObject);
  522. begin
  523. CanvasChanged;
  524. end;
  525. { IDeviceContextSupport }
  526. function TLCLBackend.GetHandle: HDC;
  527. begin
  528. Result := FHDC;
  529. end;
  530. procedure TLCLBackend.SetCanvasChange(Handler: TNotifyEvent);
  531. begin
  532. FOnCanvasChange := Handler;
  533. end;
  534. procedure TLCLBackend.Draw(const DstRect, SrcRect: TRect; hSrc: HDC);
  535. begin
  536. if FOwner.Empty then Exit;
  537. if not FOwner.MeasuringMode then
  538. Windows.StretchBlt(Handle, DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left,
  539. DstRect.Bottom - DstRect.Top, hSrc, SrcRect.Left, SrcRect.Top,
  540. SrcRect.Right - SrcRect.Left, SrcRect.Bottom - SrcRect.Top, SRCCOPY);
  541. FOwner.Changed(DstRect);
  542. end;
  543. { TLCLMMFBackend }
  544. constructor TLCLMMFBackend.Create(Owner: TBitmap32; IsTemporary: Boolean = True; const MapFileName: string = '');
  545. begin
  546. FMapFileName := MapFileName;
  547. FMapIsTemporary := IsTemporary;
  548. TMMFBackend.InitializeFileMapping(FMapHandle, FMapFileHandle, FMapFileName);
  549. inherited Create(Owner);
  550. end;
  551. destructor TLCLMMFBackend.Destroy;
  552. begin
  553. TMMFBackend.DeinitializeFileMapping(FMapHandle, FMapFileHandle, FMapFileName);
  554. inherited;
  555. end;
  556. procedure TLCLMMFBackend.PrepareFileMapping(NewWidth, NewHeight: Integer);
  557. begin
  558. TMMFBackend.CreateFileMapping(FMapHandle, FMapFileHandle, FMapFileName, FMapIsTemporary, NewWidth, NewHeight);
  559. end;
  560. { TLCLMemoryBackend }
  561. constructor TLCLMemoryBackend.Create;
  562. begin
  563. inherited;
  564. FillChar(FBitmapInfo, SizeOf(TBitmapInfo), 0);
  565. with FBitmapInfo.bmiHeader do
  566. begin
  567. biSize := SizeOf(TBitmapInfoHeader);
  568. biPlanes := 1;
  569. biBitCount := 32;
  570. biCompression := BI_RGB;
  571. biXPelsPerMeter := 96;
  572. biYPelsPerMeter := 96;
  573. biClrUsed := 0;
  574. end;
  575. end;
  576. procedure TLCLMemoryBackend.InitializeSurface(NewWidth: Integer;
  577. NewHeight: Integer; ClearBuffer: Boolean);
  578. begin
  579. inherited;
  580. with FBitmapInfo.bmiHeader do
  581. begin
  582. biWidth := NewWidth;
  583. biHeight := -NewHeight;
  584. end;
  585. end;
  586. { IPaintSupport }
  587. procedure TLCLMemoryBackend.ImageNeeded;
  588. begin
  589. end;
  590. procedure TLCLMemoryBackend.CheckPixmap;
  591. begin
  592. end;
  593. procedure TLCLMemoryBackend.DoPaintRect(ABuffer: TBitmap32; const ARect: TRect; ACanvas: TCanvas);
  594. var
  595. Bitmap : HBITMAP;
  596. DeviceContext : HDC;
  597. Buffer : Pointer;
  598. OldObject : HGDIOBJ;
  599. begin
  600. {$IFDEF LCLWin32}
  601. if SetDIBitsToDevice(ACanvas.Handle, ARect.Left, ARect.Top, ARect.Right -
  602. ARect.Left, ARect.Bottom - ARect.Top, ARect.Left, ARect.Top, 0,
  603. ARect.Bottom - ARect.Top, ABuffer.Bits, Windows.BITMAPINFO(FBitmapInfo), DIB_RGB_COLORS) = 0 then
  604. begin
  605. // create compatible device context
  606. DeviceContext := CreateCompatibleDC(ACanvas.Handle);
  607. if DeviceContext <> 0 then
  608. try
  609. Bitmap := CreateDIBSection(DeviceContext, FBitmapInfo, DIB_RGB_COLORS,
  610. Buffer, 0, 0);
  611. if Bitmap <> 0 then
  612. begin
  613. OldObject := SelectObject(DeviceContext, Bitmap);
  614. try
  615. Move(ABuffer.Bits^, Buffer^, FBitmapInfo.bmiHeader.biWidth * FBitmapInfo.bmiHeader.biHeight * SizeOf(Cardinal));
  616. Windows.BitBlt(ACanvas.Handle, ARect.Left, ARect.Top, ARect.Right - ARect.Left, ARect.Bottom - ARect.Top, DeviceContext, 0, 0, SRCCOPY);
  617. finally
  618. if OldObject <> 0 then
  619. SelectObject(DeviceContext, OldObject);
  620. DeleteObject(Bitmap);
  621. end;
  622. end else
  623. raise Exception.Create(RCStrCannotCreateCompatibleDC);
  624. finally
  625. DeleteDC(DeviceContext);
  626. end;
  627. end;
  628. {$ELSE}
  629. raise Exception.Create('"SetDIBitsToDevice" is only included in windows unit!')
  630. {$ENDIF}
  631. end;
  632. procedure TLCLMemoryBackend.DoPaint(ABuffer: TBitmap32; const AInvalidRect: TRect; ACanvas: TCanvas);
  633. begin
  634. DoPaintRect(ABuffer, AInvalidRect, ACanvas)
  635. end;
  636. procedure TLCLMemoryBackend.DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList; ACanvas: TCanvas);
  637. var
  638. i : Integer;
  639. begin
  640. for i := 0 to AInvalidRects.Count - 1 do
  641. DoPaintRect(ABuffer, AInvalidRects[i]^, ACanvas);
  642. end;
  643. { IInteroperabilitySupport }
  644. function TLCLMemoryBackend.CopyFrom(Graphic: TGraphic): Boolean;
  645. begin
  646. // yet todo
  647. Result := False;
  648. end;
  649. { IDeviceContextSupport }
  650. procedure TLCLMemoryBackend.Draw(const DstRect, SrcRect: TRect; hSrc: HDC);
  651. begin
  652. if FOwner.Empty then Exit;
  653. if not FOwner.MeasuringMode then
  654. raise Exception.Create('Not yet supported!');
  655. FOwner.Changed(DstRect);
  656. end;
  657. procedure TLCLMemoryBackend.DrawTo(hDst: HDC; DstX, DstY: Integer);
  658. var
  659. Bitmap: HBITMAP;
  660. DeviceContext: HDC;
  661. Buffer: Pointer;
  662. OldObject: HGDIOBJ;
  663. begin
  664. {$IFDEF LCLWin32}
  665. if SetDIBitsToDevice(hDst, DstX, DstY,
  666. FOwner.Width, FOwner.Height, 0, 0, 0, FOwner.Height, FBits,
  667. Windows.BITMAPINFO(FBitmapInfo), DIB_RGB_COLORS) = 0 then
  668. begin
  669. // create compatible device context
  670. DeviceContext := CreateCompatibleDC(hDst);
  671. if DeviceContext <> 0 then
  672. try
  673. Bitmap := CreateDIBSection(DeviceContext, FBitmapInfo, DIB_RGB_COLORS, Buffer, 0, 0);
  674. if Bitmap <> 0 then
  675. begin
  676. OldObject := SelectObject(DeviceContext, Bitmap);
  677. try
  678. Move(FBits^, Buffer^, FBitmapInfo.bmiHeader.biWidth *
  679. FBitmapInfo.bmiHeader.biHeight * SizeOf(Cardinal));
  680. Windows.BitBlt(hDst, DstX, DstY, FOwner.Width, FOwner.Height, DeviceContext,
  681. 0, 0, SRCCOPY);
  682. finally
  683. if OldObject <> 0 then
  684. SelectObject(DeviceContext, OldObject);
  685. DeleteObject(Bitmap);
  686. end;
  687. end else
  688. raise Exception.Create('Can''t create compatible DC''');
  689. finally
  690. DeleteDC(DeviceContext);
  691. end;
  692. end;
  693. {$ELSE}
  694. raise Exception.Create('"SetDIBitsToDevice" is only included in windows unit!')
  695. {$ENDIF}
  696. end;
  697. procedure TLCLMemoryBackend.DrawTo(hDst: HDC; const DstRect, SrcRect: TRect);
  698. var
  699. Bitmap: HBITMAP;
  700. DeviceContext: HDC;
  701. Buffer: Pointer;
  702. OldObject: HGDIOBJ;
  703. begin
  704. {$IFDEF LCLWin32}
  705. if SetDIBitsToDevice(hDst, DstRect.Left, DstRect.Top,
  706. DstRect.Right - DstRect.Left, DstRect.Bottom - DstRect.Top, SrcRect.Left,
  707. SrcRect.Top, 0, SrcRect.Bottom - SrcRect.Top, FBits,
  708. Windows.BITMAPINFO(FBitmapInfo), DIB_RGB_COLORS) = 0 then
  709. begin
  710. // create compatible device context
  711. DeviceContext := CreateCompatibleDC(hDst);
  712. if DeviceContext <> 0 then
  713. try
  714. Buffer := nil;
  715. Bitmap := CreateDIBSection(DeviceContext, FBitmapInfo, DIB_RGB_COLORS,
  716. Buffer, 0, 0);
  717. if Bitmap <> 0 then
  718. begin
  719. OldObject := SelectObject(DeviceContext, Bitmap);
  720. try
  721. Move(FBits^, Buffer^, FBitmapInfo.bmiHeader.biWidth *
  722. FBitmapInfo.bmiHeader.biHeight * SizeOf(Cardinal));
  723. Windows.BitBlt(hDst, DstRect.Left, DstRect.Top, DstRect.Right -
  724. DstRect.Left, DstRect.Bottom - DstRect.Top, DeviceContext, 0, 0, SRCCOPY);
  725. finally
  726. if OldObject <> 0 then
  727. SelectObject(DeviceContext, OldObject);
  728. DeleteObject(Bitmap);
  729. end;
  730. end else
  731. raise Exception.Create('Can''t create compatible DC''');
  732. finally
  733. DeleteDC(DeviceContext);
  734. end;
  735. end;
  736. {$ELSE}
  737. raise Exception.Create('"SetDIBitsToDevice" is only included in windows unit!')
  738. {$ENDIF}
  739. end;
  740. function TLCLMemoryBackend.GetHandle: HDC;
  741. begin
  742. Result := 0;
  743. end;
  744. initialization
  745. StockFont := GetStockObject(SYSTEM_FONT);
  746. finalization
  747. end.