-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathbgradialogs.pas
393 lines (321 loc) · 10.2 KB
/
bgradialogs.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// SPDX-License-Identifier: LGPL-3.0-linking-exception
{
Additional dialogs to take advantage of our controls
2025-01 Massimo Magnano
}
unit BGRADialogs;
{$mode objfpc}{$H+}
{$ifdef WINDOWS}
//{$define Show_PreviewControl} //THIS IS JUST FOR TESTING, It is not recommended for now under Windows
{$endif}
interface
uses
{$ifdef Show_PreviewControl}
Windows, Graphics,
{$endif}
Classes, SysUtils, ExtDlgs, Controls, StdCtrls, ExtCtrls,
BGRABitmapTypes, BCRoundedImage;
resourcestring
rsSelectAPreviewFile = 'Select the File to preview';
type
{ TBGRAOpenPictureDialog }
TBGRAOpenPictureDialog = class(TPreviewFileDialog)
private
FDefaultFilter: string;
FImageCtrl: TBCRoundedImage;
FPicturePanel: TPanel;
FPictureDetails: TLabel;
FPreviewFilename: string;
protected
{$ifdef Show_PreviewControl}
DialogWnd,
pParentWnd, pBrotherWnd : HWnd;
{$endif}
class procedure WSRegisterClass; override;
function IsFilterStored: Boolean; virtual;
procedure InitPreviewControl; override;
procedure ClearPreview; virtual;
procedure UpdatePreview; virtual;
{$ifdef Show_PreviewControl}
procedure GetDialogWnd;
procedure ResizePreviewControl;
{$endif}
property ImageCtrl: TBCRoundedImage read FImageCtrl;
property PicturePanel: TPanel read FPicturePanel;
property PictureDetails: TLabel read FPictureDetails;
public
constructor Create(TheOwner: TComponent); override;
procedure DoClose; override;
procedure DoSelectionChange; override;
procedure DoShow; override;
function GetFilterExt: String;
property DefaultFilter: string read FDefaultFilter;
published
property Filter stored IsFilterStored;
end;
{ TSavePictureDialog }
TBGRASavePictureDialog = class(TBGRAOpenPictureDialog)
protected
class procedure WSRegisterClass; override;
function DefaultTitle: string; override;
public
constructor Create(TheOwner: TComponent); override;
end;
//Functions to Get Filters String useful in Dialogs
function GetBGRAFormatFilter(AFormat: TBGRAImageFormat): String;
procedure BuildBGRAFilterStrings(AUseReaders: Boolean; var Descriptions, Filters: String);
function BuildBGRAImageReaderFilter: String;
function BuildBGRAImageWriterFilter: String;
procedure Register;
implementation
uses
WSExtDlgs, Masks, FileUtil, LazFileUtils, LCLStrConsts, LCLType;
function GetBGRAFormatFilter(AFormat: TBGRAImageFormat): String;
begin
Result := StringReplace('*.' + BGRAImageFormat[AFormat].Extensions, ';', ';*.', [rfReplaceAll]);
end;
procedure BuildBGRAFilterStrings(AUseReaders: Boolean; var Descriptions, Filters: String);
var
iFormat: TBGRAImageFormat;
Filter: String;
addExt: Boolean;
begin
Descriptions := '';
Filters := '';
for iFormat:=Low(TBGRAImageFormat) to High(TBGRAImageFormat) do
begin
if AUseReaders
then addExt:= (iFormat<>ifUnknown) and (DefaultBGRAImageReader[iFormat] <> nil)
else addExt:= (iFormat<>ifUnknown) and (DefaultBGRAImageWriter[iFormat] <> nil);
if addExt then
begin
if (iFormat>ifJpeg) then
begin
Descriptions := Descriptions + '|';
Filters := Filters + ';';
end;
Filter := GetBGRAFormatFilter(iFormat);
FmtStr(Descriptions, '%s%s (%s)|%s',
[Descriptions, BGRAImageFormat[iFormat].TypeName, Filter, Filter]);
FmtStr(Filters, '%s%s', [Filters, Filter]);
end;
end;
FmtStr(Descriptions, '%s (%s)|%1:s|%s', [rsGraphic, Filters, Descriptions]);
end;
function BuildBGRAImageReaderFilter: String;
var
Filters: string;
begin
Result := '';
BuildBGRAFilterStrings(True, Result, Filters);
end;
function BuildBGRAImageWriterFilter: String;
var
Filters: string;
begin
Result := '';
BuildBGRAFilterStrings(False, Result, Filters);
end;
{ TBGRAOpenPictureDialog }
class procedure TBGRAOpenPictureDialog.WSRegisterClass;
begin
inherited WSRegisterClass;
RegisterOpenPictureDialog;
end;
function TBGRAOpenPictureDialog.IsFilterStored: Boolean;
begin
Result := (Filter<>FDefaultFilter);
end;
procedure TBGRAOpenPictureDialog.DoClose;
begin
inherited DoClose;
// PreviewFileControl.ParentWindow:=0;
end;
procedure TBGRAOpenPictureDialog.DoSelectionChange;
begin
UpdatePreview;
inherited DoSelectionChange;
end;
procedure TBGRAOpenPictureDialog.DoShow;
begin
ClearPreview;
inherited DoShow;
end;
procedure TBGRAOpenPictureDialog.InitPreviewControl;
begin
inherited InitPreviewControl;
PreviewFileControl.Width:=300;
PreviewFileControl.Height:=300;
FPicturePanel.Parent:=PreviewFileControl;
FPicturePanel.Align:=alClient;
{ #note -oMaxM : We create it here because the LCL assumes there is a groupbox
with only an image inside and crashes if it find it before this point }
FPictureDetails:=TLabel.Create(Self);
with FPictureDetails do begin
Name:='FPictureDetails';
Parent:= FPicturePanel;
Top:=PreviewFileControl.Height-20;
Height:=20;
Width:=PreviewFileControl.Width;
Align:=alBottom;
Caption:='';
end;
FImageCtrl.Align:=alClient;
end;
procedure TBGRAOpenPictureDialog.ClearPreview;
begin
FPicturePanel.VerticalAlignment:=taVerticalCenter;
FPicturePanel.Caption:= rsSelectAPreviewFile;
FImageCtrl.Bitmap:=nil;
FImageCtrl.Visible:= False;
FPictureDetails.Caption:='';
end;
procedure TBGRAOpenPictureDialog.UpdatePreview;
var
CurFilename: String;
FileIsValid: boolean;
begin
{$ifdef Show_PreviewControl}
if (DialogWnd = 0) then GetDialogWnd;
ResizePreviewControl;
{$endif}
FPicturePanel.Caption:= '';
FPictureDetails.Caption:='';
CurFilename := FileName;
if CurFilename = FPreviewFilename then exit;
FPreviewFilename := CurFilename;
FileIsValid := FileExistsUTF8(FPreviewFilename)
and (not DirPathExists(FPreviewFilename))
and FileIsReadable(FPreviewFilename);
if FileIsValid then
try
FImageCtrl.Bitmap.LoadFromFile(FPreviewFilename);
FImageCtrl.Visible:= True;
FImageCtrl.Invalidate; { #todo -oMaxM : an event in TBGRBitmap might be useful }
FPictureDetails.Caption:= Format('%d x %d x %d dpi', [FImageCtrl.Bitmap.Width, FImageCtrl.Bitmap.Height, Trunc(FImageCtrl.Bitmap.ResolutionX)]);
except
FileIsValid := False;
end;
if not FileIsValid then ClearPreview;
end;
{$ifdef Show_PreviewControl}
procedure TBGRAOpenPictureDialog.GetDialogWnd;
var
pHandle: HWND;
thID, prID, appID:DWord;
begin
pBrotherWnd:= 0;
pParentWnd:= 0;
//LCL doesn't pass us the Dialog Handle, so we have to look for it the old fashioned way
appID:= GetProcessId;
repeat
DialogWnd:= FindWindowEx(0, DialogWnd, PChar('#32770'), nil);
thID:= GetWindowThreadProcessId(DialogWnd, prID);
until (DialogWnd=0) or (prID = appID);
//Get Parent and Brother Control
// this depends on the OS and needs to be tested as much as possible (for now it works with Windows 10)
if (DialogWnd<>0) then
begin
pHandle:= FindWindowEx(DialogWnd, 0, PChar('DUIViewWndClassName'), nil);
if (pHandle<>0) then //Windows 10
begin
pParentWnd:= FindWindowEx(pHandle, 0, PChar('DirectUIHWND'), nil);
if (pParentWnd<>0) then
begin
repeat
pBrotherWnd:= FindWindowEx(pParentWnd, pBrotherWnd, PChar('CtrlNotifySink'), nil);
pHandle:= FindWindowEx(pBrotherWnd, 0, PChar('SHELLDLL_DefView'), nil);
until (pBrotherWnd=0) or (pHandle<>0);
if (pBrotherWnd<>0) and (pHandle<>0) then PreviewFileControl.ParentWindow:=pParentWnd;
end;
end;
end;
end;
procedure TBGRAOpenPictureDialog.ResizePreviewControl;
var
rectParent, rectBrother: TRect;
begin
if (DialogWnd<>0) and (pParentWnd<>0) and (pBrotherWnd<>0) then
begin
if GetClientRect(pParentWnd, rectParent) and GetWindowRect(pBrotherWnd, rectBrother) then
begin
ScreenToClient(pParentWnd, rectBrother.TopLeft);
ScreenToClient(pParentWnd, rectBrother.BottomRight);
PreviewFileControl.SetBounds(rectBrother.Left+4+rectBrother.Width, rectBrother.Top+4,
rectParent.Right-rectBrother.Right-8,
rectParent.Bottom-rectBrother.Top-8);
end;
end;
end;
{$endif}
constructor TBGRAOpenPictureDialog.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
FDefaultFilter := BuildBGRAImageReaderFilter+'|'+
Format(rsAllFiles,[GetAllFilesMask, GetAllFilesMask,'']);
Filter:=FDefaultFilter;
{$ifdef Show_PreviewControl}
DialogWnd:= 0;
pBrotherWnd:= 0;
pParentWnd:= 0;
{$endif}
FPicturePanel:=TPanel.Create(Self);
with FPicturePanel do begin
Name:='FPicturePanel';
BorderStyle:=bsNone;
BevelOuter:=bvNone;
VerticalAlignment:=taVerticalCenter;
end;
FImageCtrl:=TBCRoundedImage.Create(Self);
with FImageCtrl do begin
Name:='FImageCtrl';
Parent:=FPicturePanel;
Style:=isSquare;
Proportional:=true;
end;
end;
function TBGRAOpenPictureDialog.GetFilterExt: String;
var
ParsedFilter: TParseStringList;
begin
Result := '';
ParsedFilter := TParseStringList.Create(Filter, '|');
try
if (FilterIndex > 0) and (FilterIndex * 2 <= ParsedFilter.Count) then
begin
Result := AnsiLowerCase(ParsedFilter[FilterIndex * 2 - 1]);
// remove *.*
if (Result <> '') and (Result[1] = '*') then Delete(Result, 1, 1);
if (Result <> '') and (Result[1] = '.') then Delete(Result, 1, 1);
if (Result <> '') and (Result[1] = '*') then Delete(Result, 1, 1);
// remove all after ;
if Pos(';', Result) > 0 then Delete(Result, Pos(';', Result), MaxInt);
end;
if Result = '' then Result := DefaultExt;
finally
ParsedFilter.Free;
end;
end;
{ TSavePictureDialog }
class procedure TBGRASavePictureDialog.WSRegisterClass;
begin
inherited WSRegisterClass;
RegisterSavePictureDialog;
end;
function TBGRASavePictureDialog.DefaultTitle: string;
begin
Result := rsfdFileSaveAs;
end;
constructor TBGRASavePictureDialog.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
FDefaultFilter := BuildBGRAImageWriterFilter+'|'+
Format(rsAllFiles,[GetAllFilesMask, GetAllFilesMask,'']);
Filter:=FDefaultFilter;
fCompStyle:=csSaveFileDialog;
end;
procedure Register;
begin
RegisterComponents('BGRA Dialogs',[TBGRAOpenPictureDialog, TBGRASavePictureDialog]);
end;
end.