forked from dim-s/soulengine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MIMAGE.PAS
372 lines (347 loc) · 9 KB
/
MIMAGE.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
{
TMImage - delivered from TImage to create backgrounds in mosaic style
Created by RSC <rsctm@freemail.hu>
Addtional property:
Mosaic: set it true if you wanna use the mosaic style
The new component will be created into the Samples group!!
}
{$I 'sDef.inc'}
unit MImage;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls, Graphics,
GIFImage2, svgimage, vcl.imaging.jpeg,
{$IFDEF ADD_SKINS}
acPNG
{$ELSE}
vcl.imaging.pngimage
{$ENDIF};
{$M+}
{$TypeInfo On}
type
TMImage = class(TGraphicControl)
private
FPicture: TPicture;
FOnProgress: TProgressEvent;
FStretch: boolean;
FCenter: boolean;
FIncrementalDisplay: boolean;
FTransparent: boolean;
FDrawing: boolean;
FProportional: boolean;
fMosaic: boolean;
function GetImageCanvas: TCanvas;
function GetCanvas: TCanvas;
procedure PictureChanged(Sender: TObject);
procedure SetCenter(Value: boolean);
procedure SetPicture(Value: TPicture);
procedure SetStretch(Value: boolean);
procedure SetTransparent(Value: boolean);
procedure SetProportional(Value: boolean);
protected
function CanAutoSize(var NewWidth, NewHeight: integer): boolean; override;
function DestRect: TRect;
function DoPaletteChange: boolean;
function GetPalette: HPALETTE; override;
procedure Paint; override;
procedure Progress(Sender: TObject; Stage: TProgressStage;
PercentDone: byte; RedrawNow: boolean; const R: TRect; const Msg: string); dynamic;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Align;
property ImageCanvas: TCanvas read GetImageCanvas;
property Canvas: TCanvas read GetCanvas;
property Anchors;
property AutoSize;
property Center: boolean read FCenter write SetCenter default False;
property Constraints;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property IncrementalDisplay: boolean read FIncrementalDisplay
write FIncrementalDisplay default False;
property Mosaic: boolean read fMosaic write fMosaic default False;
property ParentShowHint;
property Picture: TPicture read FPicture write SetPicture;
property PopupMenu;
property Proportional: boolean
read FProportional write SetProportional default False;
property ShowHint;
property Stretch: boolean read FStretch write SetStretch default False;
property Transparent: boolean read FTransparent write SetTransparent default False;
property Visible;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnProgress: TProgressEvent read FOnProgress write FOnProgress;
property OnStartDock;
property OnStartDrag;
end;
implementation
uses Forms, Consts;
constructor TMImage.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
FPicture := TPicture.Create;
FPicture.OnChange := PictureChanged;
FPicture.OnProgress := Progress;
Height := 105;
Width := 105;
end;
destructor TMImage.Destroy;
begin
FPicture.Free;
inherited Destroy;
end;
function TMImage.GetPalette: HPALETTE;
begin
Result := 0;
if FPicture.Graphic <> nil then
Result := FPicture.Graphic.Palette;
end;
function TMImage.DestRect: TRect;
var
w, h, cw, ch: integer;
xyaspect: double;
begin
w := Picture.Width;
h := Picture.Height;
cw := ClientWidth;
ch := ClientHeight;
if Stretch or (Proportional and ((w > cw) or (h > ch))) then
begin
if Proportional and (w > 0) and (h > 0) then
begin
xyaspect := w / h;
if w > h then
begin
w := cw;
h := Trunc(cw / xyaspect);
if h > ch then // woops, too big
begin
h := ch;
w := Trunc(ch * xyaspect);
end;
end
else
begin
h := ch;
w := Trunc(ch * xyaspect);
if w > cw then // woops, too big
begin
w := cw;
h := Trunc(cw / xyaspect);
end;
end;
end
else
begin
w := cw;
h := ch;
end;
end;
with Result do
begin
Left := 0;
Top := 0;
Right := w;
Bottom := h;
end;
if Center then
OffsetRect(Result, (cw - w) div 2, (ch - h) div 2);
end;
procedure TMImage.Paint;
var
Save: boolean;
x, y: integer;
begin
if csDesigning in ComponentState then
with inherited Canvas do
begin
Pen.Style := psDash;
Brush.Style := bsClear;
Rectangle(0, 0, Width, Height);
end;
Save := FDrawing;
FDrawing := True;
try
if (fMosaic) and (Picture.Width > 0) and (Picture.Height > 0) then
begin
y := 0;
while y < Height do
begin
x := 0;
while x < Width do
begin
inherited Canvas.Draw(x, y, Picture.Graphic);
x := x + Picture.Width;
end;
y := y + Picture.Height;
end;
end
else
begin
with inherited Canvas do
StretchDraw(DestRect, Picture.Graphic);
end;
finally
FDrawing := Save;
end;
end;
function TMImage.DoPaletteChange: boolean;
var
ParentForm: TCustomForm;
Tmp: TGraphic;
begin
Result := False;
Tmp := Picture.Graphic;
if Visible and (not (csLoading in ComponentState)) and (Tmp <> nil) and
(Tmp.PaletteModified) then
begin
if (Tmp.Palette = 0) then
Tmp.PaletteModified := False
else
begin
ParentForm := GetParentForm(Self);
if Assigned(ParentForm) and ParentForm.Active and Parentform.HandleAllocated then
begin
if FDrawing then
ParentForm.Perform(wm_QueryNewPalette, 0, 0)
else
PostMessage(ParentForm.Handle, wm_QueryNewPalette, 0, 0);
Result := True;
Tmp.PaletteModified := False;
end;
end;
end;
end;
procedure TMImage.Progress(Sender: TObject; Stage: TProgressStage;
PercentDone: byte; RedrawNow: boolean; const R: TRect; const Msg: string);
begin
if FIncrementalDisplay and RedrawNow then
begin
if DoPaletteChange then
Update
else
Paint;
end;
if Assigned(FOnProgress) then
FOnProgress(Sender, Stage, PercentDone, RedrawNow, R, Msg);
end;
function TMImage.GetCanvas: TCanvas;
begin
Result := inherited Canvas;
end;
function TMImage.GetImageCanvas: TCanvas;
var
Bitmap: TBitmap;
begin
if Picture.Graphic = nil then
begin
Bitmap := TBitmap.Create;
try
Bitmap.Width := Width;
Bitmap.Height := Height;
Picture.Graphic := Bitmap;
finally
Bitmap.Free;
end;
end;
if Picture.Graphic is TBitmap then
Result := TBitmap(Picture.Graphic).Canvas
else if Picture.Graphic is TSVGGraphic then
Result := TSVGGraphic(Picture.Graphic).Canvas
else if Picture.Graphic is TJPEGImage then
Result := TJPEGImage(Picture.Graphic).Canvas
else if Picture.Graphic is TPngImage then
Result := TPngImage(Picture.Graphic).Canvas
else if Picture.Graphic is GIFImage2.TGIFImage then
Result := GIFImage2.TGIFImage(Picture.Graphic).Bitmap.Canvas
else
raise EInvalidOperation.Create(SImageCanvasNeedsBitmap);
end;
procedure TMImage.SetCenter(Value: boolean);
begin
if FCenter <> Value then
begin
FCenter := Value;
PictureChanged(Self);
end;
end;
procedure TMImage.SetPicture(Value: TPicture);
begin
FPicture.Assign(Value);
end;
procedure TMImage.SetStretch(Value: boolean);
begin
if Value <> FStretch then
begin
FStretch := Value;
PictureChanged(Self);
end;
end;
procedure TMImage.SetTransparent(Value: boolean);
begin
if Value <> FTransparent then
begin
FTransparent := Value;
PictureChanged(Self);
end;
end;
procedure TMImage.SetProportional(Value: boolean);
begin
if FProportional <> Value then
begin
FProportional := Value;
PictureChanged(Self);
end;
end;
procedure TMImage.PictureChanged(Sender: TObject);
var
G: TGraphic;
begin
if AutoSize and (Picture.Width > 0) and (Picture.Height > 0) then
SetBounds(Left, Top, Picture.Width, Picture.Height);
G := Picture.Graphic;
if G <> nil then
begin
if not ((G is TMetaFile) or (G is TIcon)) then
G.Transparent := FTransparent;
if (not G.Transparent) and Stretch and not Proportional then
ControlStyle := ControlStyle + [csOpaque]
else // picture might not cover entire clientrect
ControlStyle := ControlStyle - [csOpaque];
if DoPaletteChange and FDrawing then
Update;
end
else
ControlStyle := ControlStyle - [csOpaque];
if not FDrawing then
Invalidate;
end;
function TMImage.CanAutoSize(var NewWidth, NewHeight: integer): boolean;
begin
Result := True;
if not (csDesigning in ComponentState) or (Picture.Width > 0) and
(Picture.Height > 0) then
begin
if Align in [alNone, alLeft, alRight] then
NewWidth := Picture.Width;
if Align in [alNone, alTop, alBottom] then
NewHeight := Picture.Height;
end;
end;
end.