GR32_Backends_LCL_Win.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. {$I GR32.inc}
  37. uses
  38. {$IFDEF LCLWin32} Windows, {$ENDIF} LCLIntf, LCLType, Types, Controls,
  39. SysUtils, Classes, Graphics, GR32, GR32_Backends, GR32_Backends_Generic,
  40. GR32_Containers, GR32_Image, GR32_Paths;
  41. type
  42. { TLCLBackend }
  43. { This backend uses the LCL to manage and provide the buffer and additional
  44. graphics sub system features. The backing buffer is kept in memory. }
  45. TLCLBackend = class(TCustomBackend, IPaintSupport, IBitmapContextSupport,
  46. IDeviceContextSupport, ITextSupport, IFontSupport, ITextToPathSupport,
  47. ICanvasSupport, IInteroperabilitySupport)
  48. private
  49. procedure FontChangedHandler(Sender: TObject);
  50. procedure CanvasChangedHandler(Sender: TObject);
  51. procedure CanvasChanged;
  52. procedure FontChanged;
  53. protected
  54. FBitmapInfo: TBitmapInfo;
  55. FBitmapHandle: HBITMAP;
  56. FHDC: HDC;
  57. FFont: TFont;
  58. FCanvas: TCanvas;
  59. FFontHandle: HFont;
  60. FMapHandle: THandle;
  61. FOnFontChange: TNotifyEvent;
  62. FOnCanvasChange: TNotifyEvent;
  63. procedure InitializeSurface(NewWidth, NewHeight: Integer; ClearBuffer: Boolean); override;
  64. procedure FinalizeSurface; override;
  65. procedure PrepareFileMapping(NewWidth, NewHeight: Integer); virtual;
  66. public
  67. constructor Create; override;
  68. destructor Destroy; override;
  69. procedure Changed; override;
  70. function Empty: Boolean; override;
  71. public
  72. { IPaintSupport }
  73. procedure ImageNeeded;
  74. procedure CheckPixmap;
  75. procedure DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList;
  76. ACanvas: TCanvas; APaintBox: TCustomPaintBox32);
  77. { IBitmapContextSupport }
  78. function GetBitmapInfo: TBitmapInfo;
  79. function GetBitmapHandle: THandle;
  80. property BitmapInfo: TBitmapInfo read GetBitmapInfo;
  81. property BitmapHandle: THandle read GetBitmapHandle;
  82. { IDeviceContextSupport }
  83. function GetHandle: HDC;
  84. procedure Draw(const DstRect, SrcRect: TRect; hSrc: HDC); overload;
  85. procedure DrawTo(hDst: HDC; DstX, DstY: Integer); overload;
  86. procedure DrawTo(hDst: HDC; const DstRect, SrcRect: TRect); overload;
  87. property Handle: HDC read GetHandle;
  88. { ITextSupport }
  89. procedure Textout(X, Y: Integer; const Text: string); overload;
  90. procedure Textout(X, Y: Integer; const ClipRect: TRect; const Text: string); overload;
  91. procedure Textout(var DstRect: TRect; const Flags: Cardinal; const Text: string); overload;
  92. function TextExtent(const Text: string): TSize;
  93. { IFontSupport }
  94. function GetOnFontChange: TNotifyEvent;
  95. procedure SetOnFontChange(Handler: TNotifyEvent);
  96. function GetFont: TFont;
  97. procedure SetFont(const Font: TFont);
  98. procedure UpdateFont;
  99. property Font: TFont read GetFont write SetFont;
  100. property OnFontChange: TNotifyEvent read FOnFontChange write FOnFontChange;
  101. { ITextToPathSupport }
  102. procedure TextToPath(Path: TCustomPath; const X, Y: TFloat; const Text: string); overload;
  103. procedure TextToPath(Path: TCustomPath; const DstRect: TFloatRect; const Text: string; Flags: Cardinal); overload;
  104. function MeasureText(const DstRect: TFloatRect; const Text: string; Flags: Cardinal): TFloatRect;
  105. { IInteroperabilitySupport }
  106. function CopyFrom(Graphic: TGraphic): Boolean; overload;
  107. { ICanvasSupport }
  108. function GetCanvasChange: TNotifyEvent;
  109. procedure SetCanvasChange(Handler: TNotifyEvent);
  110. function GetCanvas: TCanvas;
  111. procedure DeleteCanvas;
  112. function CanvasAllocated: Boolean;
  113. property Canvas: TCanvas read GetCanvas;
  114. property OnCanvasChange: TNotifyEvent read GetCanvasChange write SetCanvasChange;
  115. end;
  116. { TLCLGDIMMFBackend }
  117. { Same as TGDIBackend but relies on memory mapped files or mapped swap space
  118. for the backing buffer. }
  119. TLCLMMFBackend = class(TLCLBackend)
  120. private
  121. FMapFileHandle: THandle;
  122. FMapIsTemporary: Boolean;
  123. FMapFileName: string;
  124. protected
  125. procedure PrepareFileMapping(NewWidth, NewHeight: Integer); override;
  126. public
  127. constructor Create(Owner: TBitmap32; IsTemporary: Boolean = True; const MapFileName: string = ''); virtual;
  128. destructor Destroy; override;
  129. end;
  130. { TGDIMemoryBackend }
  131. { A backend that keeps the backing buffer entirely in memory and offers
  132. IPaintSupport without allocating a GDI handle }
  133. { TLCLMemoryBackend }
  134. TLCLMemoryBackend = class(TMemoryBackend, IPaintSupport, IDeviceContextSupport)
  135. private
  136. procedure DoPaintRect(ABuffer: TBitmap32; ARect: TRect; ACanvas: TCanvas);
  137. function GetHandle: HDC; // Dummy
  138. protected
  139. FBitmapInfo: TBitmapInfo;
  140. procedure InitializeSurface(NewWidth: Integer; NewHeight: Integer;
  141. ClearBuffer: Boolean); override;
  142. public
  143. constructor Create; override;
  144. { IPaintSupport }
  145. procedure ImageNeeded;
  146. procedure CheckPixmap;
  147. procedure DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList; ACanvas: TCanvas; APaintBox: TCustomPaintBox32);
  148. { IInteroperabilitySupport }
  149. function CopyFrom(Graphic: TGraphic): Boolean; overload;
  150. { IDeviceContextSupport }
  151. procedure Draw(const DstRect, SrcRect: TRect; hSrc: HDC); overload;
  152. procedure DrawTo(hDst: HDC; DstX, DstY: Integer); overload;
  153. procedure DrawTo(hDst: HDC; const DstRect, SrcRect: TRect); overload;
  154. end;
  155. implementation
  156. uses
  157. GR32_Text_LCL_Win;
  158. var
  159. StockFont: HFONT;
  160. { TLCLBackend }
  161. constructor TLCLBackend.Create;
  162. begin
  163. inherited;
  164. FillChar(FBitmapInfo, SizeOf(TBitmapInfo), 0);
  165. with FBitmapInfo.bmiHeader do
  166. begin
  167. biSize := SizeOf(TBitmapInfoHeader);
  168. biPlanes := 1;
  169. biBitCount := 32;
  170. biCompression := BI_RGB;
  171. end;
  172. FMapHandle := 0;
  173. FFont := TFont.Create;
  174. FFont.OnChange := FontChangedHandler;
  175. end;
  176. destructor TLCLBackend.Destroy;
  177. begin
  178. DeleteCanvas;
  179. FFont.Free;
  180. inherited;
  181. end;
  182. procedure TLCLBackend.InitializeSurface(NewWidth, NewHeight: Integer; ClearBuffer: Boolean);
  183. begin
  184. with FBitmapInfo.bmiHeader do
  185. begin
  186. biWidth := NewWidth;
  187. biHeight := -NewHeight;
  188. biSizeImage := NewWidth * NewHeight * 4;
  189. end;
  190. PrepareFileMapping(NewWidth, NewHeight);
  191. FBitmapHandle := LCLIntf.CreateDIBSection(0, FBitmapInfo, DIB_RGB_COLORS, Pointer(FBits), FMapHandle, 0);
  192. if FBits = nil then
  193. raise Exception.Create(RCStrCannotAllocateDIBHandle);
  194. FHDC := CreateCompatibleDC(0);
  195. if FHDC = 0 then
  196. begin
  197. DeleteObject(FBitmapHandle);
  198. FBitmapHandle := 0;
  199. FBits := nil;
  200. raise Exception.Create(RCStrCannotCreateCompatibleDC);
  201. end;
  202. if SelectObject(FHDC, FBitmapHandle) = 0 then
  203. begin
  204. DeleteDC(FHDC);
  205. DeleteObject(FBitmapHandle);
  206. FHDC := 0;
  207. FBitmapHandle := 0;
  208. FBits := nil;
  209. raise Exception.Create(RCStrCannotSelectAnObjectIntoDC);
  210. end;
  211. end;
  212. procedure TLCLBackend.FinalizeSurface;
  213. begin
  214. if FHDC <> 0 then DeleteDC(FHDC);
  215. FHDC := 0;
  216. if FBitmapHandle <> 0 then DeleteObject(FBitmapHandle);
  217. FBitmapHandle := 0;
  218. FBits := nil;
  219. end;
  220. procedure TLCLBackend.PrepareFileMapping(NewWidth, NewHeight: Integer);
  221. begin
  222. // to be implemented by descendants
  223. end;
  224. procedure TLCLBackend.Changed;
  225. begin
  226. if FCanvas <> nil then FCanvas.Handle := Self.Handle;
  227. inherited;
  228. end;
  229. function TLCLBackend.Empty: Boolean;
  230. begin
  231. Result := FBitmapHandle = 0;
  232. end;
  233. { IPaintSupport }
  234. procedure TLCLBackend.ImageNeeded;
  235. begin
  236. end;
  237. procedure TLCLBackend.CheckPixmap;
  238. begin
  239. end;
  240. procedure TLCLBackend.DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList;
  241. ACanvas: TCanvas; APaintBox: TCustomPaintBox32);
  242. var
  243. i: Integer;
  244. begin
  245. if AInvalidRects.Count > 0 then
  246. for i := 0 to AInvalidRects.Count - 1 do
  247. with AInvalidRects[i]^ do
  248. Windows.BitBlt(ACanvas.Handle, Left, Top, Right - Left, Bottom - Top, ABuffer.Handle, Left, Top, SRCCOPY)
  249. else
  250. with APaintBox.GetViewportRect do
  251. Windows.BitBlt(ACanvas.Handle, Left, Top, Right - Left, Bottom - Top, ABuffer.Handle, Left, Top, SRCCOPY);
  252. end;
  253. { IFontSupport }
  254. function TLCLBackend.GetOnFontChange: TNotifyEvent;
  255. begin
  256. Result := FOnFontChange;
  257. end;
  258. function TLCLBackend.GetFont: TFont;
  259. begin
  260. Result := FFont;
  261. end;
  262. procedure TLCLBackend.SetFont(const Font: TFont);
  263. begin
  264. FFont.Assign(Font);
  265. FontChanged;
  266. end;
  267. procedure TLCLBackend.SetOnFontChange(Handler: TNotifyEvent);
  268. begin
  269. FOnFontChange := Handler;
  270. end;
  271. procedure TLCLBackend.UpdateFont;
  272. begin
  273. if (FFontHandle = 0) and (Handle <> 0) then
  274. begin
  275. SelectObject(Handle, Font.Handle);
  276. SetTextColor(Handle, ColorToRGB(Font.Color));
  277. SetBkMode(Handle, TRANSPARENT);
  278. FFontHandle := Font.Handle;
  279. end
  280. else
  281. begin
  282. SelectObject(Handle, FFontHandle);
  283. SetTextColor(Handle, ColorToRGB(Font.Color));
  284. SetBkMode(Handle, TRANSPARENT);
  285. end;
  286. end;
  287. procedure TLCLBackend.FontChanged;
  288. begin
  289. if Assigned(FOnFontChange) then
  290. FOnFontChange(Self);
  291. end;
  292. procedure TLCLBackend.FontChangedHandler(Sender: TObject);
  293. begin
  294. if FFontHandle <> 0 then
  295. begin
  296. if Handle <> 0 then SelectObject(Handle, StockFont);
  297. FFontHandle := 0;
  298. end;
  299. FontChanged;
  300. end;
  301. { ITextSupport }
  302. procedure TLCLBackend.Textout(X, Y: Integer; const Text: string);
  303. var
  304. Extent: TSize;
  305. begin
  306. UpdateFont;
  307. if not FOwner.MeasuringMode then
  308. begin
  309. if FOwner.Clipping then
  310. ExtTextout(Handle, X, Y, ETO_CLIPPED, @FOwner.ClipRect, PChar(Text), Length(Text), nil)
  311. else
  312. ExtTextout(Handle, X, Y, 0, nil, PChar(Text), Length(Text), nil);
  313. end;
  314. Extent := TextExtent(Text);
  315. FOwner.Changed(MakeRect(X, Y, X + Extent.cx + 1, Y + Extent.cy + 1));
  316. end;
  317. procedure TLCLBackend.Textout(X, Y: Integer; const ClipRect: TRect; const Text: string);
  318. var
  319. Extent: TSize;
  320. begin
  321. UpdateFont;
  322. if not FOwner.MeasuringMode then
  323. ExtTextout(Handle, X, Y, ETO_CLIPPED, @ClipRect, PChar(Text), Length(Text), nil);
  324. Extent := TextExtent(Text);
  325. FOwner.Changed(MakeRect(X, Y, X + Extent.cx + 1, Y + Extent.cy + 1));
  326. end;
  327. procedure TLCLBackend.Textout(var DstRect: TRect; const Flags: Cardinal; const Text: string);
  328. begin
  329. UpdateFont;
  330. if not FOwner.MeasuringMode then
  331. DrawText(Handle, PChar(Text), Length(Text), DstRect, Flags);
  332. FOwner.Changed(DstRect);
  333. end;
  334. function TLCLBackend.TextExtent(const Text: string): TSize;
  335. var
  336. DC: HDC;
  337. OldFont: HGDIOBJ;
  338. begin
  339. UpdateFont;
  340. Result.cX := 0;
  341. Result.cY := 0;
  342. if Handle <> 0 then
  343. GetTextExtentPoint32(Handle, PChar(Text), Length(Text), Result)
  344. else
  345. begin
  346. StockBitmap.Canvas.Lock;
  347. try
  348. DC := StockBitmap.Canvas.Handle;
  349. OldFont := SelectObject(DC, Font.Handle);
  350. GetTextExtentPoint32(DC, PChar(Text), Length(Text), Result);
  351. SelectObject(DC, OldFont);
  352. finally
  353. StockBitmap.Canvas.Unlock;
  354. end;
  355. end;
  356. end;
  357. { ITextToPathSupport }
  358. procedure TLCLBackend.TextToPath(Path: TCustomPath; const X, Y: TFloat;
  359. const Text: string);
  360. var
  361. R: TFloatRect;
  362. begin
  363. R := FloatRect(X, Y, X, Y);
  364. GR32_Text_LCL_Win.TextToPath(Font.Handle, Path, R, Text, 0);
  365. end;
  366. procedure TLCLBackend.TextToPath(Path: TCustomPath; const DstRect: TFloatRect;
  367. const Text: string; Flags: Cardinal);
  368. begin
  369. GR32_Text_LCL_Win.TextToPath(Font.Handle, Path, DstRect, Text, Flags);
  370. end;
  371. function TLCLBackend.MeasureText(const DstRect: TFloatRect;
  372. const Text: string; Flags: Cardinal): TFloatRect;
  373. begin
  374. Result := GR32_Text_LCL_Win.MeasureText(Font.Handle, DstRect, Text, Flags);
  375. end;
  376. procedure TLCLBackend.DrawTo(hDst: HDC; DstX, DstY: Integer);
  377. begin
  378. Windows.BitBlt(hDst, DstX, DstY, FOwner.Width, FOwner.Height, Handle, 0, 0,
  379. SRCCOPY);
  380. (*
  381. StretchDIBits(
  382. hDst, DstX, DstY, FOwner.Width, FOwner.Height,
  383. 0, 0, FOwner.Width, FOwner.Height, Bits, FBitmapInfo, DIB_RGB_COLORS, SRCCOPY);
  384. *)
  385. end;
  386. procedure TLCLBackend.DrawTo(hDst: HDC; const DstRect, SrcRect: TRect);
  387. begin
  388. Windows.StretchBlt(hDst,
  389. DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left, DstRect.Bottom - DstRect.Top, Handle,
  390. SrcRect.Left, SrcRect.Top, SrcRect.Right - SrcRect.Left, SrcRect.Bottom - SrcRect.Top, SRCCOPY);
  391. end;
  392. function TLCLBackend.GetBitmapHandle: THandle;
  393. begin
  394. Result := FBitmapHandle;
  395. end;
  396. function TLCLBackend.GetBitmapInfo: TBitmapInfo;
  397. begin
  398. Result := FBitmapInfo;
  399. end;
  400. { IInteroperabilitySupport }
  401. type
  402. TGraphicAccess = class(TGraphic);
  403. function TLCLBackend.CopyFrom(Graphic: TGraphic): Boolean;
  404. begin
  405. TGraphicAccess(Graphic).Draw(Canvas, MakeRect(0, 0, Canvas.Width, Canvas.Height));
  406. end;
  407. { ICanvasSupport }
  408. procedure TLCLBackend.DeleteCanvas;
  409. begin
  410. if Assigned(FCanvas) then
  411. begin
  412. FCanvas.Handle := 0;
  413. FCanvas.Free;
  414. FCanvas := nil;
  415. end;
  416. end;
  417. function TLCLBackend.CanvasAllocated: Boolean;
  418. begin
  419. Result := Assigned(FCanvas);
  420. end;
  421. procedure TLCLBackend.CanvasChanged;
  422. begin
  423. if Assigned(FOnCanvasChange) then
  424. FOnCanvasChange(Self);
  425. end;
  426. function TLCLBackend.GetCanvas: TCanvas;
  427. begin
  428. if not Assigned(FCanvas) then
  429. begin
  430. FCanvas := TCanvas.Create;
  431. FCanvas.Handle := Handle;
  432. FCanvas.OnChange := CanvasChangedHandler;
  433. end;
  434. Result := FCanvas;
  435. end;
  436. function TLCLBackend.GetCanvasChange: TNotifyEvent;
  437. begin
  438. Result := FOnCanvasChange;
  439. end;
  440. procedure TLCLBackend.CanvasChangedHandler(Sender: TObject);
  441. begin
  442. CanvasChanged;
  443. end;
  444. { IDeviceContextSupport }
  445. function TLCLBackend.GetHandle: HDC;
  446. begin
  447. Result := FHDC;
  448. end;
  449. procedure TLCLBackend.SetCanvasChange(Handler: TNotifyEvent);
  450. begin
  451. FOnCanvasChange := Handler;
  452. end;
  453. procedure TLCLBackend.Draw(const DstRect, SrcRect: TRect; hSrc: HDC);
  454. begin
  455. if FOwner.Empty then Exit;
  456. if not FOwner.MeasuringMode then
  457. Windows.StretchBlt(Handle, DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left,
  458. DstRect.Bottom - DstRect.Top, hSrc, SrcRect.Left, SrcRect.Top,
  459. SrcRect.Right - SrcRect.Left, SrcRect.Bottom - SrcRect.Top, SRCCOPY);
  460. FOwner.Changed(DstRect);
  461. end;
  462. { TLCLMMFBackend }
  463. constructor TLCLMMFBackend.Create(Owner: TBitmap32; IsTemporary: Boolean = True; const MapFileName: string = '');
  464. begin
  465. FMapFileName := MapFileName;
  466. FMapIsTemporary := IsTemporary;
  467. TMMFBackend.InitializeFileMapping(FMapHandle, FMapFileHandle, FMapFileName);
  468. inherited Create(Owner);
  469. end;
  470. destructor TLCLMMFBackend.Destroy;
  471. begin
  472. TMMFBackend.DeinitializeFileMapping(FMapHandle, FMapFileHandle, FMapFileName);
  473. inherited;
  474. end;
  475. procedure TLCLMMFBackend.PrepareFileMapping(NewWidth, NewHeight: Integer);
  476. begin
  477. TMMFBackend.CreateFileMapping(FMapHandle, FMapFileHandle, FMapFileName, FMapIsTemporary, NewWidth, NewHeight);
  478. end;
  479. { TLCLMemoryBackend }
  480. constructor TLCLMemoryBackend.Create;
  481. begin
  482. inherited;
  483. FillChar(FBitmapInfo, SizeOf(TBitmapInfo), 0);
  484. with FBitmapInfo.bmiHeader do
  485. begin
  486. biSize := SizeOf(TBitmapInfoHeader);
  487. biPlanes := 1;
  488. biBitCount := 32;
  489. biCompression := BI_RGB;
  490. biXPelsPerMeter := 96;
  491. biYPelsPerMeter := 96;
  492. biClrUsed := 0;
  493. end;
  494. end;
  495. procedure TLCLMemoryBackend.InitializeSurface(NewWidth: Integer;
  496. NewHeight: Integer; ClearBuffer: Boolean);
  497. begin
  498. inherited;
  499. with FBitmapInfo.bmiHeader do
  500. begin
  501. biWidth := NewWidth;
  502. biHeight := -NewHeight;
  503. end;
  504. end;
  505. { IPaintSupport }
  506. procedure TLCLMemoryBackend.ImageNeeded;
  507. begin
  508. end;
  509. procedure TLCLMemoryBackend.CheckPixmap;
  510. begin
  511. end;
  512. procedure TLCLMemoryBackend.DoPaintRect(ABuffer: TBitmap32;
  513. ARect: TRect; ACanvas: TCanvas);
  514. var
  515. Bitmap : HBITMAP;
  516. DeviceContext : HDC;
  517. Buffer : Pointer;
  518. OldObject : HGDIOBJ;
  519. begin
  520. {$IFDEF LCLWin32}
  521. if SetDIBitsToDevice(ACanvas.Handle, ARect.Left, ARect.Top, ARect.Right -
  522. ARect.Left, ARect.Bottom - ARect.Top, ARect.Left, ARect.Top, 0,
  523. ARect.Bottom - ARect.Top, ABuffer.Bits, Windows.BITMAPINFO(FBitmapInfo), DIB_RGB_COLORS) = 0 then
  524. begin
  525. // create compatible device context
  526. DeviceContext := CreateCompatibleDC(ACanvas.Handle);
  527. if DeviceContext <> 0 then
  528. try
  529. Bitmap := CreateDIBSection(DeviceContext, FBitmapInfo, DIB_RGB_COLORS,
  530. Buffer, 0, 0);
  531. if Bitmap <> 0 then
  532. begin
  533. OldObject := SelectObject(DeviceContext, Bitmap);
  534. try
  535. Move(ABuffer.Bits^, Buffer^, FBitmapInfo.bmiHeader.biWidth *
  536. FBitmapInfo.bmiHeader.biHeight * SizeOf(Cardinal));
  537. Windows.BitBlt(ACanvas.Handle, ARect.Left, ARect.Top, ARect.Right -
  538. ARect.Left, ARect.Bottom - ARect.Top, DeviceContext, 0, 0, SRCCOPY);
  539. finally
  540. if OldObject <> 0 then
  541. SelectObject(DeviceContext, OldObject);
  542. DeleteObject(Bitmap);
  543. end;
  544. end else
  545. raise Exception.Create(RCStrCannotCreateCompatibleDC);
  546. finally
  547. DeleteDC(DeviceContext);
  548. end;
  549. end;
  550. {$ELSE}
  551. raise Exception.Create('"SetDIBitsToDevice" is only included in windows unit!')
  552. {$ENDIF}
  553. end;
  554. procedure TLCLMemoryBackend.DoPaint(ABuffer: TBitmap32;
  555. AInvalidRects: TRectList; ACanvas: TCanvas; APaintBox: TCustomPaintBox32);
  556. var
  557. i : Integer;
  558. begin
  559. if AInvalidRects.Count > 0 then
  560. for i := 0 to AInvalidRects.Count - 1 do
  561. DoPaintRect(ABuffer, AInvalidRects[i]^, ACanvas)
  562. else
  563. DoPaintRect(ABuffer, APaintBox.GetViewportRect, ACanvas);
  564. end;
  565. { IInteroperabilitySupport }
  566. function TLCLMemoryBackend.CopyFrom(Graphic: TGraphic): Boolean;
  567. begin
  568. // yet todo
  569. end;
  570. { IDeviceContextSupport }
  571. procedure TLCLMemoryBackend.Draw(const DstRect, SrcRect: TRect; hSrc: HDC);
  572. begin
  573. if FOwner.Empty then Exit;
  574. if not FOwner.MeasuringMode then
  575. raise Exception.Create('Not yet supported!');
  576. FOwner.Changed(DstRect);
  577. end;
  578. procedure TLCLMemoryBackend.DrawTo(hDst: HDC; DstX, DstY: Integer);
  579. var
  580. Bitmap: HBITMAP;
  581. DeviceContext: HDC;
  582. Buffer: Pointer;
  583. OldObject: HGDIOBJ;
  584. begin
  585. {$IFDEF LCLWin32}
  586. if SetDIBitsToDevice(hDst, DstX, DstY,
  587. FOwner.Width, FOwner.Height, 0, 0, 0, FOwner.Height, FBits,
  588. Windows.BITMAPINFO(FBitmapInfo), DIB_RGB_COLORS) = 0 then
  589. begin
  590. // create compatible device context
  591. DeviceContext := CreateCompatibleDC(hDst);
  592. if DeviceContext <> 0 then
  593. try
  594. Bitmap := CreateDIBSection(DeviceContext, FBitmapInfo, DIB_RGB_COLORS,
  595. Buffer, 0, 0);
  596. if Bitmap <> 0 then
  597. begin
  598. OldObject := SelectObject(DeviceContext, Bitmap);
  599. try
  600. Move(FBits^, Buffer^, FBitmapInfo.bmiHeader.biWidth *
  601. FBitmapInfo.bmiHeader.biHeight * SizeOf(Cardinal));
  602. Windows.BitBlt(hDst, DstX, DstY, FOwner.Width, FOwner.Height, DeviceContext,
  603. 0, 0, SRCCOPY);
  604. finally
  605. if OldObject <> 0 then
  606. SelectObject(DeviceContext, OldObject);
  607. DeleteObject(Bitmap);
  608. end;
  609. end else
  610. raise Exception.Create('Can''t create compatible DC''');
  611. finally
  612. DeleteDC(DeviceContext);
  613. end;
  614. end;
  615. {$ELSE}
  616. raise Exception.Create('"SetDIBitsToDevice" is only included in windows unit!')
  617. {$ENDIF}
  618. end;
  619. procedure TLCLMemoryBackend.DrawTo(hDst: HDC; const DstRect, SrcRect: TRect);
  620. var
  621. Bitmap: HBITMAP;
  622. DeviceContext: HDC;
  623. Buffer: Pointer;
  624. OldObject: HGDIOBJ;
  625. begin
  626. {$IFDEF LCLWin32}
  627. if SetDIBitsToDevice(hDst, DstRect.Left, DstRect.Top,
  628. DstRect.Right - DstRect.Left, DstRect.Bottom - DstRect.Top, SrcRect.Left,
  629. SrcRect.Top, 0, SrcRect.Bottom - SrcRect.Top, FBits,
  630. Windows.BITMAPINFO(FBitmapInfo), DIB_RGB_COLORS) = 0 then
  631. begin
  632. // create compatible device context
  633. DeviceContext := CreateCompatibleDC(hDst);
  634. if DeviceContext <> 0 then
  635. try
  636. Buffer := nil;
  637. Bitmap := CreateDIBSection(DeviceContext, FBitmapInfo, DIB_RGB_COLORS,
  638. Buffer, 0, 0);
  639. if Bitmap <> 0 then
  640. begin
  641. OldObject := SelectObject(DeviceContext, Bitmap);
  642. try
  643. Move(FBits^, Buffer^, FBitmapInfo.bmiHeader.biWidth *
  644. FBitmapInfo.bmiHeader.biHeight * SizeOf(Cardinal));
  645. Windows.BitBlt(hDst, DstRect.Left, DstRect.Top, DstRect.Right -
  646. DstRect.Left, DstRect.Bottom - DstRect.Top, DeviceContext, 0, 0, SRCCOPY);
  647. finally
  648. if OldObject <> 0 then
  649. SelectObject(DeviceContext, OldObject);
  650. DeleteObject(Bitmap);
  651. end;
  652. end else
  653. raise Exception.Create('Can''t create compatible DC''');
  654. finally
  655. DeleteDC(DeviceContext);
  656. end;
  657. end;
  658. {$ELSE}
  659. raise Exception.Create('"SetDIBitsToDevice" is only included in windows unit!')
  660. {$ENDIF}
  661. end;
  662. function TLCLMemoryBackend.GetHandle: HDC;
  663. begin
  664. Result := 0;
  665. end;
  666. initialization
  667. StockFont := GetStockObject(SYSTEM_FONT);
  668. finalization
  669. end.