Debian Codegraph @

src/services/castleopendocument_macos.inc

raw
1 {%MainUnit castleopendocument.pas}
2 {
3 Copyright 2012-2024 Michalis Kamburelis and Lazarus developers.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9 Parts of this file are based on Lazarus LCL code, which has
10 exactly the same license as our "Castle Game Engine":
11 LGPL with static linking exception, see COPYING.txt for details.
12
13 "Castle Game Engine" is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17 ----------------------------------------------------------------------------
18 }
19
20 { OpenDocument and OpenUrl implementations for macOS. }
21
22 // Relies on RunCmdFromPath
23 {$I castleopendocument_process.inc}
24
25 {$ifdef read_uses}
26 MacOSAll,
27 {$endif}
28
29 {$ifdef read_implementation}
30
31 { Based on Lazarus LCL:
32 lcl/include/sysenvapis_mac.inc }
33
34 // Open a given URL with the default browser
35 function OpenUrl(AUrl: String): Boolean;
36 var
37 cf: CFStringRef;
38 url: CFUrlRef;
39 FileName: string;
40 begin
41 if AUrl = '' then
42 Exit(False);
43
44 { If this is a local filename, open it using OpenDocument. }
45 if not UrlHasAnchor(AUrl) then
46 begin
47 FileName := UriToFilenameSafe(AUrl);
48 if FileName <> '' then
49 Exit(OpenDocument(FileName));
50 end;
51
52 cf := CFStringCreateWithCString(kCFAllocatorDefault, @AUrl[1], kCFStringEncodingUTF8);
53 if not Assigned(cf) then
54 Exit(False);
55 url := CFUrlCreateWithString(nil, cf, nil);
56 Result := LSOpenCFUrlRef(url, nil) = 0;
57
58 CFRelease(url);
59 CFRelease(cf);
60 end;
61
62 // Open a document with the default application associated with it in the system
63 function OpenDocument(APath: String): Boolean;
64 begin
65 Result := True;
66 RunCmdFromPath('open',APath);
67 end;
68
69 {$endif read_implementation}