Debian Codegraph @

fontforgeexe/macobjective.m

raw
1 /* Copyright (C) 2000-2012 by George Williams */
2 /* Copyright (C) 2021 by Jeremy Tan */
3 /*
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13
14 * The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 // clang-format -i --style=Webkit fontforgeexe/macobjective.m
30
31 #include <fontforge-config.h>
32
33 #import <Cocoa/Cocoa.h>
34
35 #pragma push_macro("AnchorPoint")
36 #pragma push_macro("FontInfo")
37 #pragma push_macro("KernPair")
38 #define AnchorPoint FFAnchorPoint
39 #define FontInfo FFFontInfo
40 #define KernPair FFKernPair
41 #include "fontforgeui.h"
42 #pragma pop_macro("KernPair")
43 #pragma pop_macro("FontInfo")
44 #pragma pop_macro("AnchorPoint")
45
46 @interface FFApplication : NSApplication
47 - (void)registerEventHandlers;
48 - (bool)setReady;
49 - (void)handleOpenApplicationEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent;
50 - (void)handleOpenDocumentsEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent;
51 @end
52
53 @implementation FFApplication {
54 bool ready;
55 NSMutableArray* pendingFiles;
56 }
57
58 - (void)registerEventHandlers
59 {
60 // The old Carbon approach used to register a preferences handler
61 // with kAEShowPreferences. Under Cocoa, you would have to make that
62 // menu yourself. Oh well. See also:
63 // https://www.mail-archive.com/[email protected]/msg00280.html
64 NSAppleEventManager* eventManager = [NSAppleEventManager sharedAppleEventManager];
65 [eventManager setEventHandler:self
66 andSelector:@selector
67 (handleOpenApplicationEvent:
68 withReplyEvent:)
69 forEventClass:kCoreEventClass
70 andEventID:kAEOpenApplication];
71 [eventManager setEventHandler:self
72 andSelector:@selector
73 (handleOpenApplicationEvent:
74 withReplyEvent:)
75 forEventClass:kCoreEventClass
76 andEventID:kAEReopenApplication];
77 [eventManager setEventHandler:self
78 andSelector:@selector
79 (handleOpenDocumentsEvent:
80 withReplyEvent:)
81 forEventClass:kCoreEventClass
82 andEventID:kAEOpenDocuments];
83 [eventManager setEventHandler:self
84 andSelector:@selector
85 (handleQuitEvent:
86 withReplyEvent:)
87 forEventClass:kCoreEventClass
88 andEventID:kAEQuitApplication];
89 }
90
91 - (bool)setReady
92 {
93 self->ready = true;
94 if (self->pendingFiles) {
95 for (NSURL* fileUrl in self->pendingFiles) {
96 ViewPostScriptFont([fileUrl fileSystemRepresentation], 0);
97 }
98 [self->pendingFiles release];
99 self->pendingFiles = nil;
100 return true;
101 }
102 return false;
103 }
104
105 - (void)handleOpenApplicationEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
106 {
107 if (self->ready && fv_list == NULL) {
108 _FVMenuOpen(NULL);
109 }
110 }
111
112 - (void)handleOpenDocumentsEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
113 {
114 NSAppleEventDescriptor* files = [event paramDescriptorForKeyword:keyDirectObject];
115 if (!files) {
116 return;
117 }
118
119 for (NSInteger i = 1; i <= [files numberOfItems]; ++i) {
120 NSString* fileUrlStr = [[files descriptorAtIndex:i] stringValue];
121 if (!fileUrlStr) {
122 continue;
123 }
124
125 NSURL* fileUrl = [NSURL URLWithString:fileUrlStr];
126 if (!fileUrl) {
127 continue;
128 }
129
130 // GDK also pumps the event loop before we're ready, which means
131 // that this event may fire before we can actually process it.
132 // So store the files to open until we're ready.
133 if (!self->ready) {
134 if (!self->pendingFiles) {
135 self->pendingFiles = [[NSMutableArray alloc] init];
136 }
137 [self->pendingFiles addObject:fileUrl];
138 continue;
139 }
140
141 ViewPostScriptFont([fileUrl fileSystemRepresentation], 0);
142 }
143 }
144
145 - (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
146 {
147 if (self->ready) {
148 MenuExit(NULL, NULL, NULL);
149 }
150 }
151
152 @end
153
154 void setup_cocoa_app(void)
155 {
156 [[FFApplication sharedApplication] registerEventHandlers];
157
158 // launchd (pid=1) sets the cwd to the root folder, so change it to home
159 // https://stackoverflow.com/a/52688733
160 if (getppid() == 1 && getenv("HOME") != NULL) {
161 chdir(getenv("HOME"));
162 }
163 }
164
165 bool launch_cocoa_app(void)
166 {
167 return [[FFApplication sharedApplication] setReady];
168 }