| 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 |
{ File filters, for TCastleWindow.FileDialog and Lazarus file dialogs. } |
| 17 |
unit CastleFileFilters; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses SysUtils, Classes, Generics.Collections; |
| 24 |
|
| 25 |
type |
| 26 |
TFileFilter = class |
| 27 |
private |
| 28 |
FName: string; |
| 29 |
FPatterns: TStringList; |
| 30 |
public |
| 31 |
constructor Create; |
| 32 |
destructor Destroy; override; |
| 33 |
|
| 34 |
property Name: string read FName write FName; |
| 35 |
property Patterns: TStringList read FPatterns; |
| 36 |
end; |
| 37 |
|
| 38 |
TFileFilterList = class({$ifdef FPC}specialize{$endif} TObjectList<TFileFilter>) |
| 39 |
private |
| 40 |
FDefaultFilter: Integer; |
| 41 |
public |
| 42 |
constructor Create(AFreeObjects: Boolean); |
| 43 |
|
| 44 |
{ Add one file filter, selectable by user. |
| 45 |
@param(Name Is a name displayed for for user.) |
| 46 |
@param(Patterns Each string in Patterns is a pattern |
| 47 |
using * and ? matching characters.) } |
| 48 |
procedure AddFilter(const Name: string; const Patterns: array of string); |
| 49 |
|
| 50 |
{ Index of default filter, selected by default for user when using |
| 51 |
this filters list. } |
| 52 |
property DefaultFilter: Integer |
| 53 |
read FDefaultFilter write FDefaultFilter default 0; |
| 54 |
|
| 55 |
{ An easy way to add multiple filters encoded in a single string. |
| 56 |
Filters are separated by '|' (bar character). |
| 57 |
Each filter has a name, separated from patterns list also by '|'. |
| 58 |
If filter name starts with '*', it's set as a default filter. |
| 59 |
Patterns are separated by ';' (semicolon character). |
| 60 |
|
| 61 |
As you can see, this prevents some special characters from appearing |
| 62 |
in names and patterns. For maximum flexibility, don't use this |
| 63 |
function, use AddFilter. |
| 64 |
|
| 65 |
For example @code(All files (*)|*|All images (*.png;*.jpg)|*.png;*.jpg|PNG images (*.png)|*.png) |
| 66 |
|
| 67 |
Not finished pairs of name + pattern at the end of the string are ignored. |
| 68 |
In particular, empty string is unfinished (actually, it contains |
| 69 |
an empty Name, and is unfinished because there is no |, so no matching |
| 70 |
Pattern) so empty string causes no filters to be added. } |
| 71 |
procedure AddFiltersFromString(const FiltersStr: string); |
| 72 |
|
| 73 |
{ One of the filtes (excluding "catch all" filters) matches given URL. |
| 74 |
This excludes masks like "*" and "*.*" (the latter should not really |
| 75 |
be used, because it will not match all files on Unix). } |
| 76 |
function Matches(const Url: String): Boolean; overload; |
| 77 |
|
| 78 |
{ Whether the filters described in FiltersStr (like for |
| 79 |
@link(AddFiltersFromString)) match the given URL. } |
| 80 |
class function Matches(const FiltersStr, Url: String): Boolean; overload; |
| 81 |
|
| 82 |
{ Writes all recognized extensions (without * and *.*) separated |
| 83 |
by semicolon. } |
| 84 |
function AllExtensions: String; |
| 85 |
|
| 86 |
{ Convert file filters into LCL or FMX Dialog.Filter, Dialog.FilterIndex. |
| 87 |
When AllFields is false, then filters starting with "All " in the name, |
| 88 |
like "All files", "All images", are not included in the output. } |
| 89 |
procedure LclFmxFilters( |
| 90 |
out OutFilter: string; out OutFilterIndex: Integer; const AllFields: Boolean = true); |
| 91 |
|
| 92 |
{ Convert file filters into LCL or FMX Dialog.Filter, Dialog.FilterIndex. |
| 93 |
The filters are provided here just like for AddFiltersFromString. |
| 94 |
Effectively we convert one String encoding of filters |
| 95 |
(as for CGE AddFiltersFromString) |
| 96 |
into very similar encoding of filters suitable for LCL or FMX. } |
| 97 |
class procedure LclFmxFiltersFromString(const FileFilters: string; |
| 98 |
out OutFilter: string; out OutFilterIndex: Integer; const AllFields: Boolean = true); |
| 99 |
end; |
| 100 |
|
| 101 |
implementation |
| 102 |
|
| 103 |
uses StrUtils, CastleStringUtils, CastleUriUtils, CastleUtils; |
| 104 |
|
| 105 |
{ TFileFilter ---------------------------------------------------------------- } |
| 106 |
|
| 107 |
constructor TFileFilter.Create; |
| 108 |
begin |
| 109 |
inherited; |
| 110 |
FPatterns := TStringList.Create; |
| 111 |
end; |
| 112 |
|
| 113 |
destructor TFileFilter.Destroy; |
| 114 |
begin |
| 115 |
FreeAndNil(FPatterns); |
| 116 |
inherited; |
| 117 |
end; |
| 118 |
|
| 119 |
{ TFileFilterList ----------------------------------------------------------- } |
| 120 |
|
| 121 |
constructor TFileFilterList.Create(AFreeObjects: Boolean); |
| 122 |
begin |
| 123 |
inherited; |
| 124 |
FDefaultFilter := 0; |
| 125 |
end; |
| 126 |
|
| 127 |
procedure TFileFilterList.AddFilter(const Name: string; |
| 128 |
const Patterns: array of string); |
| 129 |
var |
| 130 |
Filter: TFileFilter; |
| 131 |
I: Integer; |
| 132 |
begin |
| 133 |
Filter := TFileFilter.Create; |
| 134 |
Add(Filter); |
| 135 |
Filter.FName := Name; |
| 136 |
for I := 0 to System.High(Patterns) do |
| 137 |
Filter.FPatterns.Append(Patterns[I]); |
| 138 |
end; |
| 139 |
|
| 140 |
procedure TFileFilterList.AddFiltersFromString(const FiltersStr: string); |
| 141 |
|
| 142 |
procedure AddFilterFromPair(Name: string; const Patterns: string); |
| 143 |
var |
| 144 |
Filter: TFileFilter; |
| 145 |
LastSeparator, NextSeparator: Integer; |
| 146 |
Part: string; |
| 147 |
begin |
| 148 |
if SCharIs(Name, 1, '*') then |
| 149 |
begin |
| 150 |
DefaultFilter := Count; |
| 151 |
System.Delete(Name, 1, 1); |
| 152 |
end; |
| 153 |
|
| 154 |
Filter := TFileFilter.Create; |
| 155 |
Add(Filter); |
| 156 |
|
| 157 |
Filter.Name := Name; |
| 158 |
|
| 159 |
{ tests: Writeln('new name: "', Filter.FName, '"'); } |
| 160 |
|
| 161 |
LastSeparator := 0; |
| 162 |
repeat |
| 163 |
{ calcualate NextSeparator and set Part } |
| 164 |
NextSeparator := PosEx(';', Patterns, LastSeparator + 1); |
| 165 |
if NextSeparator = 0 then |
| 166 |
Part := SEnding(Patterns, LastSeparator + 1) else |
| 167 |
Part := CopyPos(Patterns, LastSeparator + 1, NextSeparator - 1); |
| 168 |
|
| 169 |
{ new Part is a next pattern, add it } |
| 170 |
Filter.Patterns.Append(Part); |
| 171 |
{ tests: Writeln('new pattern: "', Part, '"'); } |
| 172 |
|
| 173 |
{ advance LastSeparator for next loop roll } |
| 174 |
LastSeparator := NextSeparator; |
| 175 |
until LastSeparator = 0; |
| 176 |
end; |
| 177 |
|
| 178 |
var |
| 179 |
LastSeparator, NextSeparator: Integer; |
| 180 |
NamePart: Boolean; |
| 181 |
Part, Name, Patterns: string; |
| 182 |
begin |
| 183 |
NamePart := true; |
| 184 |
LastSeparator := 0; |
| 185 |
repeat |
| 186 |
{ calcualate NextSeparator and set Name or Patterns } |
| 187 |
NextSeparator := PosEx('|', FiltersStr, LastSeparator + 1); |
| 188 |
if NextSeparator = 0 then |
| 189 |
Part := SEnding(FiltersStr, LastSeparator + 1) else |
| 190 |
Part := CopyPos(FiltersStr, LastSeparator + 1, NextSeparator - 1); |
| 191 |
if NamePart then |
| 192 |
Name := Part else |
| 193 |
Patterns := Part; |
| 194 |
|
| 195 |
{ actually make use of Name + Patterns to create new filter } |
| 196 |
if not NamePart then |
| 197 |
AddFilterFromPair(Name, Patterns); |
| 198 |
|
| 199 |
{ advance LastSeparator and NamePart for next loop roll } |
| 200 |
LastSeparator := NextSeparator; |
| 201 |
NamePart := not NamePart; |
| 202 |
until LastSeparator = 0; |
| 203 |
end; |
| 204 |
|
| 205 |
function TFileFilterList.Matches(const Url: String): Boolean; |
| 206 |
var |
| 207 |
UrlName, Pattern: String; |
| 208 |
Filter: TFileFilter; |
| 209 |
begin |
| 210 |
UrlName := ExtractURIName(Url); |
| 211 |
for Filter in Self do |
| 212 |
for Pattern in Filter.Patterns do |
| 213 |
if (Pattern <> '*') and |
| 214 |
(Pattern <> '*.*') and |
| 215 |
IsWild(UrlName, Pattern, { IgnoreCase} not FileNameCaseSensitive) then |
| 216 |
Exit(true); |
| 217 |
Result := false; |
| 218 |
end; |
| 219 |
|
| 220 |
class function TFileFilterList.Matches(const FiltersStr, Url: String): Boolean; |
| 221 |
var |
| 222 |
Filters: TFileFilterList; |
| 223 |
begin |
| 224 |
Filters := TFileFilterList.Create(true); |
| 225 |
try |
| 226 |
Filters.AddFiltersFromString(FiltersStr); |
| 227 |
Result := Filters.Matches(Url); |
| 228 |
finally FreeAndNil(Filters) end; |
| 229 |
end; |
| 230 |
|
| 231 |
function TFileFilterList.AllExtensions: String; |
| 232 |
var |
| 233 |
Pattern: String; |
| 234 |
Filter: TFileFilter; |
| 235 |
begin |
| 236 |
Result := ''; |
| 237 |
for Filter in Self do |
| 238 |
for Pattern in Filter.Patterns do |
| 239 |
begin |
| 240 |
if (Pattern = '*') or (Pattern = '*.*') then |
| 241 |
continue; |
| 242 |
Result := SAppendPart(Result, ';', Pattern); |
| 243 |
end; |
| 244 |
end; |
| 245 |
|
| 246 |
procedure TFileFilterList.LclFmxFilters( |
| 247 |
out OutFilter: string; out OutFilterIndex: Integer; const AllFields: Boolean); |
| 248 |
var |
| 249 |
Filter: TFileFilter; |
| 250 |
I, J: Integer; |
| 251 |
begin |
| 252 |
OutFilter := ''; |
| 253 |
|
| 254 |
{ initialize OutFilterIndex. |
| 255 |
Will be corrected for AllFields=false case, and will be incremented |
| 256 |
(because LCL/FMX FilterIndex counts from 1) later. } |
| 257 |
|
| 258 |
OutFilterIndex := DefaultFilter; |
| 259 |
|
| 260 |
for I := 0 to Count - 1 do |
| 261 |
begin |
| 262 |
Filter := Items[I]; |
| 263 |
if (not AllFields) and IsPrefix('All ', Filter.Name) then |
| 264 |
begin |
| 265 |
{ then we don't want to add this to OutFilter. |
| 266 |
We also need to fix OutFilterIndex, to shift it. } |
| 267 |
if I = DefaultFilter then |
| 268 |
OutFilterIndex := 0 else |
| 269 |
if I < DefaultFilter then |
| 270 |
Dec(OutFilterIndex); |
| 271 |
Continue; |
| 272 |
end; |
| 273 |
|
| 274 |
OutFilter := OutFilter + Filter.Name + '|'; |
| 275 |
|
| 276 |
for J := 0 to Filter.Patterns.Count - 1 do |
| 277 |
begin |
| 278 |
if J <> 0 then OutFilter := OutFilter + ';'; |
| 279 |
{ TODO: We're considering this, to improve display of rows with very long |
| 280 |
filters list, like "All Scenes" or "Images" when opening scenes, |
| 281 |
e.g. in CastleFmxPlayAnimation. |
| 282 |
|
| 283 |
if J > 5 then |
| 284 |
begin |
| 285 |
OutFilter := OutFilter + '...'; |
| 286 |
Break; |
| 287 |
end; |
| 288 |
} |
| 289 |
OutFilter := OutFilter + Filter.Patterns[J]; |
| 290 |
end; |
| 291 |
|
| 292 |
OutFilter := OutFilter + '|'; |
| 293 |
end; |
| 294 |
|
| 295 |
{ LCL/FMX FilterIndex counts from 1. |
| 296 |
|
| 297 |
Except Delphi/Linux (FMXLinux) that seems (maybe a bug?) to count from 0. |
| 298 |
Testcase; CastleFmxPlayAnimation, press "Load scene" button, |
| 299 |
default filter should be "All Scenes". } |
| 300 |
{$if not (defined(DELPHI) and defined(LINUX))} |
| 301 |
Inc(OutFilterIndex); |
| 302 |
{$endif} |
| 303 |
end; |
| 304 |
|
| 305 |
class procedure TFileFilterList.LclFmxFiltersFromString(const FileFilters: string; |
| 306 |
out OutFilter: string; out OutFilterIndex: Integer; const AllFields: Boolean); |
| 307 |
var |
| 308 |
FFList: TFileFilterList; |
| 309 |
begin |
| 310 |
FFList := TFileFilterList.Create(true); |
| 311 |
try |
| 312 |
FFList.AddFiltersFromString(FileFilters); |
| 313 |
FFList.LclFmxFilters(OutFilter, OutFilterIndex, AllFields); |
| 314 |
finally FreeAndNil(FFList) end; |
| 315 |
end; |
| 316 |
|
| 317 |
end. |