Debian Codegraph @

src/lcl/castledialogs.pas

raw
1 {
2 Copyright 2008-2018 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Dialog windows. }
17 unit CastleDialogs;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses Classes, Dialogs, ExtDlgs;
24
25 type
26 { General open dialog that uses URL.
27 The URL is a file: or castle-data: URL. }
28 TCastleOpenDialog = class(TOpenDialog)
29 private
30 FAdviceDataDirectory: Boolean;
31 FUseCastleDataProtocol: Boolean;
32 function GetUrl: String;
33 procedure SetUrl(AValue: String);
34 protected
35 function DoExecute: boolean; override;
36 public
37 constructor Create(AOwner: TComponent); override;
38 published
39 property Url: String read GetUrl write SetUrl stored false;
40 { Warn (but still allow) if user selects URL outside of data directory. }
41 property AdviceDataDirectory: Boolean read FAdviceDataDirectory write FAdviceDataDirectory default false;
42 { If the URL is detected inside data directory ( https://castle-engine.io/data ),
43 make sure it starts with protocol castle-data:/ . }
44 property UseCastleDataProtocol: Boolean read FUseCastleDataProtocol write FUseCastleDataProtocol default true;
45 end;
46
47 { General save dialog that uses URL.
48 The URL is a file: or castle-data: URL. }
49 TCastleSaveDialog = class(TSaveDialog)
50 private
51 FAdviceDataDirectory: Boolean;
52 FUseCastleDataProtocol: Boolean;
53 function GetUrl: String;
54 procedure SetUrl(AValue: String);
55 protected
56 function DoExecute: boolean; override;
57 public
58 constructor Create(AOwner: TComponent); override;
59 published
60 property Url: String read GetUrl write SetUrl stored false;
61 { Warn (but still allow) if user selects URL outside of data directory. }
62 property AdviceDataDirectory: Boolean read FAdviceDataDirectory write FAdviceDataDirectory default false;
63 { If the URL is detected inside data directory ( https://castle-engine.io/data ),
64 make sure it starts with protocol castle-data:/ . }
65 property UseCastleDataProtocol: Boolean read FUseCastleDataProtocol write FUseCastleDataProtocol default true;
66 end;
67
68 { Dialog to open scene (select a file that can be loaded using TCastleScene.Load).
69 It uses an URL, and additionally initializes the filters
70 to include all the scene types we can load (through
71 LoadNode, TCastleScene.Load, TCastleScene.Url and so on). }
72 TCastleOpenSceneDialog = class(TOpenDialog)
73 private
74 FAdviceDataDirectory: Boolean;
75 FUseCastleDataProtocol: Boolean;
76 InitialFilterIndex: Integer;
77 InitialFilter: String;
78 function GetUrl: String;
79 procedure SetUrl(AValue: String);
80 function StoreFilterAndFilterIndex: boolean;
81 protected
82 function DoExecute: boolean; override;
83 public
84 constructor Create(AOwner: TComponent); override;
85 published
86 property Url: String read GetUrl write SetUrl stored false;
87 { Warn (but still allow) if user selects URL outside of data directory. }
88 property AdviceDataDirectory: Boolean read FAdviceDataDirectory write FAdviceDataDirectory default false;
89 { If the URL is detected inside data directory ( https://castle-engine.io/data ),
90 make sure it starts with protocol castle-data:/ . }
91 property UseCastleDataProtocol: Boolean read FUseCastleDataProtocol write FUseCastleDataProtocol default true;
92 property Filter stored StoreFilterAndFilterIndex;
93 property FilterIndex stored StoreFilterAndFilterIndex;
94 end;
95
96 { Image open dialog. It uses an URL, and additionally initializes the filters
97 to include all the image types our engine can load through
98 @link(CastleImages.LoadImage). }
99 TCastleOpenImageDialog = class(TOpenPictureDialog)
100 private
101 FAdviceDataDirectory: Boolean;
102 FUseCastleDataProtocol: Boolean;
103 InitialFilterIndex: Integer;
104 InitialFilter: String;
105 function PrepareUrl(const AFileName: String): String;
106 function GetUrl: String;
107 function GetUrlWithIndex(const Index: Integer): String;
108 procedure SetUrl(AValue: String);
109 function StoreFilterAndFilterIndex: boolean;
110 protected
111 function DoExecute: boolean; override;
112 public
113 constructor Create(AOwner: TComponent); override;
114
115 { Number of selected images to open (useful when multi-selection is allowed). }
116 function UrlCount: Integer;
117
118 { Get Url of a selected image to open (useful when multi-selection is allowed). }
119 property Urls[Index: Integer]: String read GetUrlWithIndex;
120 published
121 property Url: String read GetUrl write SetUrl stored false;
122 { Warn (but still allow) if user selects URL outside of data directory. }
123 property AdviceDataDirectory: Boolean read FAdviceDataDirectory write FAdviceDataDirectory default false;
124 { If the URL is detected inside data directory ( https://castle-engine.io/data ),
125 make sure it starts with protocol castle-data:/ . }
126 property UseCastleDataProtocol: Boolean read FUseCastleDataProtocol write FUseCastleDataProtocol default true;
127 property Filter stored StoreFilterAndFilterIndex;
128 property FilterIndex stored StoreFilterAndFilterIndex;
129 end;
130
131 { Image save dialog. It uses an URL, and additionally initializes the filters
132 to include all the image types our engine can save through
133 @link(CastleImages.SaveImage). }
134 TCastleSaveImageDialog = class(TSavePictureDialog)
135 private
136 FAdviceDataDirectory: Boolean;
137 FUseCastleDataProtocol: Boolean;
138 InitialFilterIndex: Integer;
139 InitialFilter: String;
140 function GetUrl: String;
141 procedure SetUrl(AValue: String);
142 function StoreFilterAndFilterIndex: boolean;
143 protected
144 function DoExecute: boolean; override;
145 public
146 constructor Create(AOwner: TComponent); override;
147 published
148 property Url: String read GetUrl write SetUrl stored false;
149 { Warn (but still allow) if user selects URL outside of data directory. }
150 property AdviceDataDirectory: Boolean read FAdviceDataDirectory write FAdviceDataDirectory default false;
151 { If the URL is detected inside data directory ( https://castle-engine.io/data ),
152 make sure it starts with protocol castle-data:/ . }
153 property UseCastleDataProtocol: Boolean read FUseCastleDataProtocol write FUseCastleDataProtocol default true;
154 property Filter stored StoreFilterAndFilterIndex;
155 property FilterIndex stored StoreFilterAndFilterIndex;
156 end;
157
158 TCastleOpenPascalUnitDialog = class(TOpenDialog)
159 private
160 const
161 InitialFilterIndex = 0;
162 InitialFilter = 'Pascal unit (*.pas, *.pp)|*.pas;*.pp|All Files|*';
163 function StoreFilterAndFilterIndex: boolean;
164 protected
165 function DoExecute: boolean; override;
166 public
167 constructor Create(AOwner: TComponent); override;
168 published
169 property Filter stored StoreFilterAndFilterIndex;
170 property FilterIndex stored StoreFilterAndFilterIndex;
171 end;
172
173 procedure Register;
174
175 implementation
176
177 uses SysUtils,
178 CastleUriUtils, CastleLCLUtils, X3DLoad, CastleImages, CastleFilesUtils,
179 CastleStringUtils, CastleUtils;
180
181 procedure Register;
182 begin
183 RegisterComponents('Castle', [
184 TCastleOpenDialog,
185 TCastleSaveDialog,
186 TCastleOpenSceneDialog,
187 TCastleOpenImageDialog,
188 TCastleSaveImageDialog,
189 TCastleOpenPascalUnitDialog
190 ]);
191 end;
192
193 procedure WarningIfOutsideDataDirectory(const Url: String);
194 begin
195 if URIProtocol(Url) <> 'castle-data' then
196 MessageDlg('File outside data', 'You are saving or opening a file outside of the project''s "data" directory.' + NL +
197 NL +
198 'The file is: ' + Url + NL +
199 NL +
200 'The "data" directory is: ' + ResolveCastleDataUrl('castle-data:/') + NL +
201 NL +
202 'While it is allowed, it is not encouraged for cross-platform applications:' + NL +
203 '- You will not be able to open this file using castle-data:/ URL (or ApplicationData function).' + NL +
204 '- The file will not be packaged with your distributed application automatically.' + NL +
205 'Unless you really know what you''re doing, we advice to instead open or save inside the project "data" directory.',
206 mtWarning, [mbOK], 0);
207 end;
208
209 function CleanupFileName(const FileName: String): String;
210 begin
211 { Using GTK 2 file open dialog under Ubuntu Mate,
212 you can get filenames with // .
213 They are mostly harmless (opening such filenames with various
214 routines work) but they make MaybeUseDataProtocol less often possible
215 (since sometimes paths will not match, because one contains //),
216 and thus would cause unnecessary calls to WarningIfOutsideDataDirectory . }
217 Result :=
218 {$ifdef UNIX}
219 StringReplace(FileName, '//', '/', [rfReplaceAll]);
220 {$else}
221 FileName;
222 {$endif}
223 end;
224
225 { TCastleOpenSceneDialog ----------------------------------------------------- }
226
227 function TCastleOpenSceneDialog.GetUrl: String;
228 begin
229 Result := FilenameToUriSafeUTF8(CleanupFileName(FileName));
230 if UseCastleDataProtocol then
231 Result := MaybeUseDataProtocol(Result);
232 end;
233
234 procedure TCastleOpenSceneDialog.SetUrl(AValue: String);
235 begin
236 FileName := UriToFilenameSafeUTF8(AValue);
237 InitialDir := ExtractFileDir(FileName);
238 end;
239
240 function TCastleOpenSceneDialog.StoreFilterAndFilterIndex: boolean;
241 begin
242 Result := (Filter <> InitialFilter) or (FilterIndex <> InitialFilterIndex);
243 end;
244
245 function TCastleOpenSceneDialog.DoExecute: boolean;
246 begin
247 Result := inherited DoExecute;
248 if Result and AdviceDataDirectory then
249 WarningIfOutsideDataDirectory(Url);
250 end;
251
252 constructor TCastleOpenSceneDialog.Create(AOwner: TComponent);
253 begin
254 inherited Create(AOwner);
255 FUseCastleDataProtocol := true;
256 FileFiltersToDialog(LoadScene_FileFilters, Self);
257 InitialFilter := Filter;
258 InitialFilterIndex := FilterIndex;
259 end;
260
261 { TCastleSaveImageDialog ------------------------------------------------- }
262
263 function TCastleSaveImageDialog.GetUrl: String;
264 begin
265 Result := FilenameToUriSafeUTF8(CleanupFileName(FileName));
266 if UseCastleDataProtocol then
267 Result := MaybeUseDataProtocol(Result);
268 end;
269
270 procedure TCastleSaveImageDialog.SetUrl(AValue: String);
271 begin
272 FileName := UriToFilenameSafeUTF8(AValue);
273 InitialDir := ExtractFileDir(FileName);
274 end;
275
276 function TCastleSaveImageDialog.StoreFilterAndFilterIndex: boolean;
277 begin
278 Result := (Filter <> InitialFilter) or (FilterIndex <> InitialFilterIndex);
279 end;
280
281 function TCastleSaveImageDialog.DoExecute: boolean;
282 begin
283 Result := inherited DoExecute;
284 if Result and AdviceDataDirectory then
285 WarningIfOutsideDataDirectory(Url);
286 end;
287
288 constructor TCastleSaveImageDialog.Create(AOwner: TComponent);
289 begin
290 inherited Create(AOwner);
291 FUseCastleDataProtocol := true;
292 FileFiltersToDialog(SaveImage_FileFilters, Self);
293 InitialFilter := Filter;
294 InitialFilterIndex := FilterIndex;
295 end;
296
297 { TCastleOpenImageDialog --------------------------------------------------- }
298
299 function TCastleOpenImageDialog.PrepareUrl(const AFileName: String): String;
300 begin
301 Result := FilenameToUriSafeUTF8(CleanupFileName(AFileName));
302 if UseCastleDataProtocol then
303 Result := MaybeUseDataProtocol(Result);
304 end;
305
306 function TCastleOpenImageDialog.GetUrl: String;
307 begin
308 Result := PrepareUrl(FileName);
309 end;
310
311 function TCastleOpenImageDialog.GetUrlWithIndex(const Index: Integer): String;
312 begin
313 Result := PrepareUrl(Files[Index]);
314 end;
315
316 procedure TCastleOpenImageDialog.SetUrl(AValue: String);
317 begin
318 FileName := UriToFilenameSafeUTF8(AValue);
319 InitialDir := ExtractFileDir(FileName);
320 end;
321
322 function TCastleOpenImageDialog.StoreFilterAndFilterIndex: boolean;
323 begin
324 Result := (Filter <> InitialFilter) or (FilterIndex <> InitialFilterIndex);
325 end;
326
327 function TCastleOpenImageDialog.DoExecute: boolean;
328 begin
329 Result := inherited DoExecute;
330 if Result and AdviceDataDirectory then
331 WarningIfOutsideDataDirectory(Url);
332 end;
333
334 constructor TCastleOpenImageDialog.Create(AOwner: TComponent);
335 begin
336 inherited Create(AOwner);
337 FUseCastleDataProtocol := true;
338 FileFiltersToDialog(LoadImage_FileFilters, Self);
339 InitialFilter := Filter;
340 InitialFilterIndex := FilterIndex;
341 end;
342
343 function TCastleOpenImageDialog.UrlCount: Integer;
344 begin
345 Result := Files.Count;
346 end;
347
348 { TCastleSaveDialog -------------------------------------------------------- }
349
350 function TCastleSaveDialog.GetUrl: String;
351 begin
352 Result := FilenameToUriSafeUTF8(CleanupFileName(FileName));
353 if UseCastleDataProtocol then
354 Result := MaybeUseDataProtocol(Result);
355 end;
356
357 procedure TCastleSaveDialog.SetUrl(AValue: String);
358 begin
359 FileName := UriToFilenameSafeUTF8(AValue);
360 InitialDir := ExtractFileDir(FileName);
361 end;
362
363 function TCastleSaveDialog.DoExecute: boolean;
364 begin
365 Result := inherited DoExecute;
366 if Result and AdviceDataDirectory then
367 WarningIfOutsideDataDirectory(Url);
368 end;
369
370 constructor TCastleSaveDialog.Create(AOwner: TComponent);
371 begin
372 inherited;
373 FUseCastleDataProtocol := true;
374 end;
375
376 { TCastleOpenDialog ---------------------------------------------------------- }
377
378 function TCastleOpenDialog.GetUrl: String;
379 begin
380 Result := FilenameToUriSafeUTF8(CleanupFileName(FileName));
381 if UseCastleDataProtocol then
382 Result := MaybeUseDataProtocol(Result);
383 end;
384
385 procedure TCastleOpenDialog.SetUrl(AValue: String);
386 begin
387 FileName := UriToFilenameSafeUTF8(AValue);
388 InitialDir := ExtractFileDir(FileName);
389 end;
390
391 function TCastleOpenDialog.DoExecute: boolean;
392 begin
393 Result := inherited DoExecute;
394 if Result and AdviceDataDirectory then
395 WarningIfOutsideDataDirectory(Url);
396 end;
397
398 constructor TCastleOpenDialog.Create(AOwner: TComponent);
399 begin
400 inherited;
401 FUseCastleDataProtocol := true;
402 end;
403
404 { TCastleOpenPascalUnitDialog ----------------------------------------------------- }
405
406 function TCastleOpenPascalUnitDialog.StoreFilterAndFilterIndex: boolean;
407 begin
408 Result := (Filter <> InitialFilter) or (FilterIndex <> InitialFilterIndex);
409 end;
410
411 function TCastleOpenPascalUnitDialog.DoExecute: boolean;
412 begin
413 Result := inherited DoExecute;
414 end;
415
416 constructor TCastleOpenPascalUnitDialog.Create(AOwner: TComponent);
417 begin
418 inherited Create(AOwner);
419 Filter := InitialFilter;
420 FilterIndex := InitialFilterIndex;
421 end;
422
423 end.