source: trunk/Packages/Graphics32/GR32_Backends_LCL_CustomDrawn.pas

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 9.7 KB
Line 
1unit GR32_Backends_LCL_CustomDrawn;
2
3(* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1 or LGPL 2.1 with linking exception
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Alternatively, the contents of this file may be used under the terms of the
17 * Free Pascal modified version of the GNU Lesser General Public License
18 * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
19 * of this license are applicable instead of those above.
20 * Please see the file LICENSE.txt for additional information concerning this
21 * license.
22 *
23 * The Original Code is Backend Extension for Graphics32
24 *
25 * The Initial Developer of the Original Code is
26 * Mattias Andersson <mattias@centaurix.com>
27 *
28 * Portions created by the Initial Developer are Copyright (C) 2007-2009
29 * the Initial Developer. All Rights Reserved.
30 *
31 * Contributor(s):
32 *
33 * ***** END LICENSE BLOCK ***** *)
34
35interface
36
37{$I GR32.inc}
38
39uses
40 { RTL and LCL }
41 LCLIntf, LCLType, types, Controls, SysUtils, Classes, Graphics,
42 { Graphics 32 }
43 GR32, GR32_Backends, GR32_Containers, GR32_Image,
44 { CustomDrawn bindings }
45 GraphType, FPImage, IntfGraphics, LCLProc, CustomDrawnProc;
46
47type
48
49 { TLCLBackend }
50
51 TLCLBackend = class(
52 TCustomBackend,
53 IPaintSupport,
54 ITextSupport,
55 IFontSupport,
56 IDeviceContextSupport,
57 ICanvasSupport
58 )
59 private
60 FFont: TFont;
61 FCanvas: TCanvas;
62 FCanvasHandle: HDC;
63 FOnFontChange: TNotifyEvent;
64 FOnCanvasChange: TNotifyEvent;
65
66 FWidth, FHeight: Cardinal;
67
68 FRawImage: TRawImage;
69 FBitmap: TBitmap;
70
71 procedure CanvasChangedHandler(Sender: TObject);
72 protected
73 { BITS_GETTER }
74 function GetBits: PColor32Array; override;
75
76 procedure InitializeSurface(NewWidth, NewHeight: Integer; ClearBuffer: Boolean); override;
77 procedure FinalizeSurface; override;
78
79 public
80 constructor Create; override;
81 destructor Destroy; override;
82
83 procedure Changed; override;
84
85 function Empty: Boolean; override;
86 public
87 { IPaintSupport }
88 procedure DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList; ACanvas: TCanvas; APaintBox: TCustomPaintBox32);
89 procedure ImageNeeded;
90 procedure CheckPixmap;
91
92 { IDeviceContextSupport }
93 function GetHandle: HDC;
94 procedure Draw(const DstRect, SrcRect: TRect; hSrc: HDC); overload;
95 procedure DrawTo(hDst: HDC; DstX, DstY: Integer); overload;
96 procedure DrawTo(hDst: HDC; const DstRect, SrcRect: TRect); overload;
97 property Handle: HDC read GetHandle;
98
99 { ITextSupport }
100 procedure Textout(X, Y: Integer; const Text: string); overload;
101 procedure Textout(X, Y: Integer; const ClipRect: TRect; const Text: string); overload;
102 procedure Textout(var DstRect: TRect; const Flags: Cardinal; const Text: string); overload;
103 function TextExtent(const Text: string): TSize;
104
105 procedure TextoutW(X, Y: Integer; const Text: Widestring); overload;
106 procedure TextoutW(X, Y: Integer; const ClipRect: TRect; const Text: Widestring); overload;
107 procedure TextoutW(var DstRect: TRect; const Flags: Cardinal; const Text: Widestring); overload;
108 function TextExtentW(const Text: Widestring): TSize;
109
110 { IFontSupport }
111 function GetOnFontChange: TNotifyEvent;
112 procedure SetOnFontChange(Handler: TNotifyEvent);
113 function GetFont: TFont;
114 procedure SetFont(const Font: TFont);
115
116 procedure UpdateFont;
117 property Font: TFont read GetFont write SetFont;
118 property OnFontChange: TNotifyEvent read FOnFontChange write FOnFontChange;
119
120 { ICanvasSupport }
121 function GetCanvasChange: TNotifyEvent;
122 procedure SetCanvasChange(Handler: TNotifyEvent);
123 function GetCanvas: TCanvas;
124
125 procedure DeleteCanvas;
126 function CanvasAllocated: Boolean;
127
128 property Canvas: TCanvas read GetCanvas;
129 property OnCanvasChange: TNotifyEvent read GetCanvasChange write SetCanvasChange;
130 end;
131
132implementation
133
134uses
135 GR32_LowLevel;
136
137{ TLCLBackend }
138
139constructor TLCLBackend.Create;
140begin
141 inherited;
142 FBitmap := TBitmap.Create;
143 FBitmap.Canvas.OnChange := CanvasChangedHandler;
144 FFont := TFont.Create;
145end;
146
147destructor TLCLBackend.Destroy;
148begin
149 inherited;
150 FFont.Free;
151 FBitmap.Free;
152end;
153
154procedure TLCLBackend.CanvasChangedHandler(Sender: TObject);
155begin
156 if Assigned(FOnCanvasChange) then
157 FOnCanvasChange(Sender);
158end;
159
160function TLCLBackend.GetBits: PColor32Array;
161begin
162 Result := FBits;
163end;
164
165procedure TLCLBackend.InitializeSurface(NewWidth, NewHeight: Integer; ClearBuffer: Boolean);
166var
167 CDBitmap: TCDBitmap;
168 LazImage: TLazIntfImage;
169begin
170 { We allocate our own memory for the image }
171
172 FRawImage.Description.Init_BPP32_B8G8R8A8_BIO_TTB(NewWidth, NewHeight);
173 FRawImage.CreateData(ClearBuffer);
174 FBits := PColor32Array(FRawImage.Data);
175 if FBits = nil then
176 raise Exception.Create('[TLCLBackend.InitializeSurface] ERROR FBits = nil');
177
178 LazImage := TLazIntfImage.Create(FRawImage, False);
179 CDBitmap := TCDBitmap.Create;
180 CDBitmap.Image := LazImage;
181 FBitmap.Handle := HBITMAP(CDBitmap);
182
183 FWidth := NewWidth;
184 FHeight := NewHeight;
185end;
186
187procedure TLCLBackend.FinalizeSurface;
188begin
189 if Assigned(FBits) then
190 begin
191 FRawImage.FreeData;
192 FBits := nil;
193
194 FBitmap.Handle := HBITMAP(0);
195 end;
196 FBits := nil;
197end;
198
199procedure TLCLBackend.Changed;
200begin
201 inherited;
202end;
203
204function TLCLBackend.Empty: Boolean;
205begin
206 Result := FBits = nil;
207end;
208
209{ IPaintSupport }
210
211procedure TLCLBackend.ImageNeeded;
212begin
213
214end;
215
216procedure TLCLBackend.CheckPixmap;
217begin
218
219end;
220
221procedure TLCLBackend.DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList;
222 ACanvas: TCanvas; APaintBox: TCustomPaintBox32);
223begin
224 ACanvas.Draw(0, 0, FBitmap);
225end;
226
227
228{ IDeviceContextSupport }
229
230function TLCLBackend.GetHandle: HDC;
231begin
232 Result := Canvas.Handle;
233end;
234
235procedure TLCLBackend.Draw(const DstRect, SrcRect: TRect; hSrc: HDC);
236begin
237 StretchMaskBlt(
238 Canvas.Handle,
239 DstRect.Left,
240 DstRect.Top,
241 DstRect.Right - DstRect.Left,
242 DstRect.Bottom - DstRect.Top,
243 hSrc,
244 SrcRect.Left,
245 SrcRect.Top,
246 SrcRect.Right - SrcRect.Left,
247 SrcRect.Bottom - SrcRect.Top,
248 0,
249 0,
250 0,
251 Canvas.CopyMode
252 );
253end;
254
255procedure TLCLBackend.DrawTo(hDst: HDC; DstX, DstY: Integer);
256begin
257 StretchMaskBlt(
258 hDst,
259 DstX,
260 DstY,
261 FWidth,
262 FHeight,
263 Canvas.Handle,
264 0,
265 0,
266 FWidth,
267 FHeight,
268 0,
269 0,
270 0,
271 Canvas.CopyMode
272 );
273end;
274
275procedure TLCLBackend.DrawTo(hDst: HDC; const DstRect, SrcRect: TRect);
276begin
277 StretchMaskBlt(
278 hDst,
279 DstRect.Left,
280 DstRect.Top,
281 DstRect.Right - DstRect.Left,
282 DstRect.Bottom - DstRect.Top,
283 Canvas.Handle,
284 SrcRect.Left,
285 SrcRect.Top,
286 SrcRect.Right - SrcRect.Left,
287 SrcRect.Bottom - SrcRect.Top,
288 0,
289 0,
290 0,
291 Canvas.CopyMode
292 );
293end;
294
295{ ITextSupport }
296
297procedure TLCLBackend.Textout(X, Y: Integer; const Text: string);
298begin
299 if not Assigned(FCanvas) then GetCanvas;
300
301 UpdateFont;
302
303 if not FOwner.MeasuringMode then
304 FCanvas.TextOut(X, Y, Text);
305
306// FOwner.Changed(DstRect);
307end;
308
309procedure TLCLBackend.Textout(X, Y: Integer; const ClipRect: TRect; const Text: string);
310begin
311 if not Assigned(FCanvas) then GetCanvas;
312
313 UpdateFont;
314
315 LCLIntf.ExtTextOut(FCanvas.Handle, X, Y, ETO_CLIPPED, @ClipRect, PChar(Text), Length(Text), nil);
316end;
317
318procedure TLCLBackend.Textout(var DstRect: TRect; const Flags: Cardinal; const Text: string);
319begin
320 UpdateFont;
321
322 LCLIntf.DrawText(FCanvas.Handle, PChar(Text), Length(Text), DstRect, Flags);
323end;
324
325function TLCLBackend.TextExtent(const Text: string): TSize;
326begin
327 if not Assigned(FCanvas) then GetCanvas;
328
329 UpdateFont;
330
331 Result := FCanvas.TextExtent(Text);
332end;
333
334procedure TLCLBackend.TextoutW(X, Y: Integer; const Text: Widestring);
335begin
336 Canvas.TextOut(X, Y, Text);
337end;
338
339procedure TLCLBackend.TextoutW(X, Y: Integer; const ClipRect: TRect; const Text: Widestring);
340begin
341 Canvas.ClipRect := ClipRect;;
342 Canvas.TextOut(X, Y, Text);
343end;
344
345procedure TLCLBackend.TextoutW(var DstRect: TRect; const Flags: Cardinal; const Text: Widestring);
346begin
347 TextOut(DstRect, Flags, Text);
348end;
349
350function TLCLBackend.TextExtentW(const Text: Widestring): TSize;
351begin
352 Result := TextExtent(Text);
353end;
354
355{ IFontSupport }
356
357function TLCLBackend.GetOnFontChange: TNotifyEvent;
358begin
359 Result := FFont.OnChange;
360end;
361
362procedure TLCLBackend.SetOnFontChange(Handler: TNotifyEvent);
363begin
364 FFont.OnChange := Handler;
365end;
366
367function TLCLBackend.GetFont: TFont;
368begin
369 Result := FFont;
370end;
371
372procedure TLCLBackend.SetFont(const Font: TFont);
373begin
374 FFont.Assign(Font);
375end;
376
377procedure TLCLBackend.UpdateFont;
378begin
379 FFont.OnChange := FOnFontChange;
380
381 if Assigned(FCanvas) then FCanvas.Font := FFont;
382end;
383
384{ ICanvasSupport }
385
386function TLCLBackend.GetCanvasChange: TNotifyEvent;
387begin
388 Result := FOnCanvasChange;
389end;
390
391procedure TLCLBackend.SetCanvasChange(Handler: TNotifyEvent);
392begin
393 FOnCanvasChange := Handler;
394end;
395
396function TLCLBackend.GetCanvas: TCanvas;
397begin
398 Result := FBitmap.Canvas;
399end;
400
401procedure TLCLBackend.DeleteCanvas;
402begin
403end;
404
405function TLCLBackend.CanvasAllocated: Boolean;
406begin
407 Result := (Canvas <> nil);
408end;
409
410end.
Note: See TracBrowser for help on using the repository browser.