1 | unit 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 |
|
---|
35 | interface
|
---|
36 |
|
---|
37 | {$I GR32.inc}
|
---|
38 |
|
---|
39 | uses
|
---|
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 |
|
---|
47 | type
|
---|
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 |
|
---|
132 | implementation
|
---|
133 |
|
---|
134 | uses
|
---|
135 | GR32_LowLevel;
|
---|
136 |
|
---|
137 | { TLCLBackend }
|
---|
138 |
|
---|
139 | constructor TLCLBackend.Create;
|
---|
140 | begin
|
---|
141 | inherited;
|
---|
142 | FBitmap := TBitmap.Create;
|
---|
143 | FBitmap.Canvas.OnChange := CanvasChangedHandler;
|
---|
144 | FFont := TFont.Create;
|
---|
145 | end;
|
---|
146 |
|
---|
147 | destructor TLCLBackend.Destroy;
|
---|
148 | begin
|
---|
149 | inherited;
|
---|
150 | FFont.Free;
|
---|
151 | FBitmap.Free;
|
---|
152 | end;
|
---|
153 |
|
---|
154 | procedure TLCLBackend.CanvasChangedHandler(Sender: TObject);
|
---|
155 | begin
|
---|
156 | if Assigned(FOnCanvasChange) then
|
---|
157 | FOnCanvasChange(Sender);
|
---|
158 | end;
|
---|
159 |
|
---|
160 | function TLCLBackend.GetBits: PColor32Array;
|
---|
161 | begin
|
---|
162 | Result := FBits;
|
---|
163 | end;
|
---|
164 |
|
---|
165 | procedure TLCLBackend.InitializeSurface(NewWidth, NewHeight: Integer; ClearBuffer: Boolean);
|
---|
166 | var
|
---|
167 | CDBitmap: TCDBitmap;
|
---|
168 | LazImage: TLazIntfImage;
|
---|
169 | begin
|
---|
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;
|
---|
185 | end;
|
---|
186 |
|
---|
187 | procedure TLCLBackend.FinalizeSurface;
|
---|
188 | begin
|
---|
189 | if Assigned(FBits) then
|
---|
190 | begin
|
---|
191 | FRawImage.FreeData;
|
---|
192 | FBits := nil;
|
---|
193 |
|
---|
194 | FBitmap.Handle := HBITMAP(0);
|
---|
195 | end;
|
---|
196 | FBits := nil;
|
---|
197 | end;
|
---|
198 |
|
---|
199 | procedure TLCLBackend.Changed;
|
---|
200 | begin
|
---|
201 | inherited;
|
---|
202 | end;
|
---|
203 |
|
---|
204 | function TLCLBackend.Empty: Boolean;
|
---|
205 | begin
|
---|
206 | Result := FBits = nil;
|
---|
207 | end;
|
---|
208 |
|
---|
209 | { IPaintSupport }
|
---|
210 |
|
---|
211 | procedure TLCLBackend.ImageNeeded;
|
---|
212 | begin
|
---|
213 |
|
---|
214 | end;
|
---|
215 |
|
---|
216 | procedure TLCLBackend.CheckPixmap;
|
---|
217 | begin
|
---|
218 |
|
---|
219 | end;
|
---|
220 |
|
---|
221 | procedure TLCLBackend.DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList;
|
---|
222 | ACanvas: TCanvas; APaintBox: TCustomPaintBox32);
|
---|
223 | begin
|
---|
224 | ACanvas.Draw(0, 0, FBitmap);
|
---|
225 | end;
|
---|
226 |
|
---|
227 |
|
---|
228 | { IDeviceContextSupport }
|
---|
229 |
|
---|
230 | function TLCLBackend.GetHandle: HDC;
|
---|
231 | begin
|
---|
232 | Result := Canvas.Handle;
|
---|
233 | end;
|
---|
234 |
|
---|
235 | procedure TLCLBackend.Draw(const DstRect, SrcRect: TRect; hSrc: HDC);
|
---|
236 | begin
|
---|
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 | );
|
---|
253 | end;
|
---|
254 |
|
---|
255 | procedure TLCLBackend.DrawTo(hDst: HDC; DstX, DstY: Integer);
|
---|
256 | begin
|
---|
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 | );
|
---|
273 | end;
|
---|
274 |
|
---|
275 | procedure TLCLBackend.DrawTo(hDst: HDC; const DstRect, SrcRect: TRect);
|
---|
276 | begin
|
---|
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 | );
|
---|
293 | end;
|
---|
294 |
|
---|
295 | { ITextSupport }
|
---|
296 |
|
---|
297 | procedure TLCLBackend.Textout(X, Y: Integer; const Text: string);
|
---|
298 | begin
|
---|
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);
|
---|
307 | end;
|
---|
308 |
|
---|
309 | procedure TLCLBackend.Textout(X, Y: Integer; const ClipRect: TRect; const Text: string);
|
---|
310 | begin
|
---|
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);
|
---|
316 | end;
|
---|
317 |
|
---|
318 | procedure TLCLBackend.Textout(var DstRect: TRect; const Flags: Cardinal; const Text: string);
|
---|
319 | begin
|
---|
320 | UpdateFont;
|
---|
321 |
|
---|
322 | LCLIntf.DrawText(FCanvas.Handle, PChar(Text), Length(Text), DstRect, Flags);
|
---|
323 | end;
|
---|
324 |
|
---|
325 | function TLCLBackend.TextExtent(const Text: string): TSize;
|
---|
326 | begin
|
---|
327 | if not Assigned(FCanvas) then GetCanvas;
|
---|
328 |
|
---|
329 | UpdateFont;
|
---|
330 |
|
---|
331 | Result := FCanvas.TextExtent(Text);
|
---|
332 | end;
|
---|
333 |
|
---|
334 | procedure TLCLBackend.TextoutW(X, Y: Integer; const Text: Widestring);
|
---|
335 | begin
|
---|
336 | Canvas.TextOut(X, Y, Text);
|
---|
337 | end;
|
---|
338 |
|
---|
339 | procedure TLCLBackend.TextoutW(X, Y: Integer; const ClipRect: TRect; const Text: Widestring);
|
---|
340 | begin
|
---|
341 | Canvas.ClipRect := ClipRect;;
|
---|
342 | Canvas.TextOut(X, Y, Text);
|
---|
343 | end;
|
---|
344 |
|
---|
345 | procedure TLCLBackend.TextoutW(var DstRect: TRect; const Flags: Cardinal; const Text: Widestring);
|
---|
346 | begin
|
---|
347 | TextOut(DstRect, Flags, Text);
|
---|
348 | end;
|
---|
349 |
|
---|
350 | function TLCLBackend.TextExtentW(const Text: Widestring): TSize;
|
---|
351 | begin
|
---|
352 | Result := TextExtent(Text);
|
---|
353 | end;
|
---|
354 |
|
---|
355 | { IFontSupport }
|
---|
356 |
|
---|
357 | function TLCLBackend.GetOnFontChange: TNotifyEvent;
|
---|
358 | begin
|
---|
359 | Result := FFont.OnChange;
|
---|
360 | end;
|
---|
361 |
|
---|
362 | procedure TLCLBackend.SetOnFontChange(Handler: TNotifyEvent);
|
---|
363 | begin
|
---|
364 | FFont.OnChange := Handler;
|
---|
365 | end;
|
---|
366 |
|
---|
367 | function TLCLBackend.GetFont: TFont;
|
---|
368 | begin
|
---|
369 | Result := FFont;
|
---|
370 | end;
|
---|
371 |
|
---|
372 | procedure TLCLBackend.SetFont(const Font: TFont);
|
---|
373 | begin
|
---|
374 | FFont.Assign(Font);
|
---|
375 | end;
|
---|
376 |
|
---|
377 | procedure TLCLBackend.UpdateFont;
|
---|
378 | begin
|
---|
379 | FFont.OnChange := FOnFontChange;
|
---|
380 |
|
---|
381 | if Assigned(FCanvas) then FCanvas.Font := FFont;
|
---|
382 | end;
|
---|
383 |
|
---|
384 | { ICanvasSupport }
|
---|
385 |
|
---|
386 | function TLCLBackend.GetCanvasChange: TNotifyEvent;
|
---|
387 | begin
|
---|
388 | Result := FOnCanvasChange;
|
---|
389 | end;
|
---|
390 |
|
---|
391 | procedure TLCLBackend.SetCanvasChange(Handler: TNotifyEvent);
|
---|
392 | begin
|
---|
393 | FOnCanvasChange := Handler;
|
---|
394 | end;
|
---|
395 |
|
---|
396 | function TLCLBackend.GetCanvas: TCanvas;
|
---|
397 | begin
|
---|
398 | Result := FBitmap.Canvas;
|
---|
399 | end;
|
---|
400 |
|
---|
401 | procedure TLCLBackend.DeleteCanvas;
|
---|
402 | begin
|
---|
403 | end;
|
---|
404 |
|
---|
405 | function TLCLBackend.CanvasAllocated: Boolean;
|
---|
406 | begin
|
---|
407 | Result := (Canvas <> nil);
|
---|
408 | end;
|
---|
409 |
|
---|
410 | end.
|
---|