Debian Codegraph @

sys/GuiShell.cpp

raw symbols SCIP
1 /* GuiShell.cpp
2 *
3 * Copyright (C) 1993-2018,2020-2022,2024,2025 Paul Boersma, 2013 Tom Naughton
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "GuiP.h"
20 #include "../kar/UnicodeData.h"
21
22 Thing_implement (GuiShell, GuiForm, 0);
23
24 #if cocoa
25 @implementation GuiCocoaShell {
26 GuiShell d_userData;
27 }
28 - (void) dealloc { // override
29 if (Melder_debug == 55)
30 Melder_casual (U"\t\tGuiCocoaShell-", Melder_pointer (self), U" dealloc");
31 GuiShell me = d_userData;
32 if (me) {
33 my d_cocoaShell = nullptr; // this is already under destruction, so undangle
34 forget (me);
35 }
36 [super dealloc];
37 }
38 - (GuiThing) getUserData {
39 return d_userData;
40 }
41 - (void) setUserData: (GuiThing) userData {
42 Melder_assert (userData == nullptr || Thing_isa (userData, classGuiShell));
43 d_userData = static_cast <GuiShell> (userData);
44 }
45 - (void) keyDown: (NSEvent *) theEvent {
46 trace (U"GuiCocoaShell: key down");
47 [super keyDown: theEvent]; // for automatic behaviour in dialog boxes; do GuiWindows have to override this to do nothing?
48 }
49 - (BOOL) windowShouldClose: (id) sender {
50 GuiCocoaShell *widget = (GuiCocoaShell *) sender;
51 GuiShell me = (GuiShell) [widget getUserData];
52 Melder_assert (me);
53 if (my d_goAwayCallback) {
54 trace (U"calling goAwayCallback)");
55 my d_goAwayCallback (my d_goAwayBoss);
56 } else {
57 trace (U"hiding window or dialog");
58 [widget orderOut: nil];
59 }
60 return false;
61 }
62 - (void) cancelOperation: (id) sender {
63 trace (U"GuiCocoaShell: escape key pressed");
64 GuiCocoaShell *widget = (GuiCocoaShell *) sender;
65 GuiWindow me = (GuiWindow) [widget getUserData];
66 if (me && my classInfo == classGuiWindow && my d_escapeCallback) {
67 try {
68 structGuiMenuItemEvent event { nullptr, false, false, false };
69 my d_escapeCallback (my d_escapeBoss, & event);
70 } catch (MelderError) {
71 Melder_flushError (U"Cancelling in window not completely handled.");
72 }
73 } else {
74 trace (U"calling the global escape callback (1)");
75 if (theGuiEscapeMenuItemCallback) {
76 try {
77 trace (U"calling the global escape callback (2)");
78 structGuiMenuItemEvent event { nullptr, false, false, false };
79 theGuiEscapeMenuItemCallback (theGuiEscapeMenuItemBoss, & event);
80 } catch (MelderError) {
81 Melder_flushError (U"Cancelling not completely handled.");
82 }
83 }
84 }
85 }
86 - (void) setDistinctiveBackGround {
87 if (@available (macOS 10.15, *))
88 [self setBackgroundColor: [NSColor
89 colorWithName: @"dynamicWindowBackgroundColour"
90 dynamicProvider: ^ NSColor*_Nonnull (NSAppearance*_Nonnull appearance) {
91 if ([appearance bestMatchFromAppearancesWithNames: @[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]] == NSAppearanceNameDarkAqua)
92 return [NSColor colorWithCalibratedWhite: 0.15 alpha: 1.0]; // dark mode, changing on the fly
93 else
94 return [NSColor colorWithCalibratedWhite: 0.85 alpha: 1.0]; // light mode, changing on the fly
95 }
96 ]];
97 else
98 [self setBackgroundColor: [NSColor colorWithCalibratedWhite: 0.85 alpha: 1.0]]; // not dynamic
99 }
100 @end
101 @implementation GuiCocoaShellDelegate
102 - (void) windowWillEnterFullScreen: (NSNotification *) notification {
103 //TRACE
104 trace (1);
105 }
106 - (void) windowDidEnterFullScreen: (NSNotification *) notification {
107 //TRACE
108 trace (1);
109 }
110 - (void) windowWillExitFullScreen: (NSNotification *) notification {
111 //TRACE
112 trace (1);
113 }
114 - (void) windowDidExitFullScreen: (NSNotification *) notification {
115 //TRACE
116 trace (1);
117 }
118 @end
119 #endif
120
121 void structGuiShell :: v9_destroy () noexcept {
122 #if cocoa
123 if (Melder_debug == 55)
124 Melder_casual (U"\t", Thing_messageNameAndAddress (this), U" v9_destroy: cocoaShell ", Melder_pointer (our d_cocoaShell));
125 if (our d_cocoaShell) {
126 [our d_cocoaShell setUserData: nullptr]; // undangle reference to this
127 [our d_cocoaShell orderOut: nil];
128 [our d_cocoaShell close];
129 [our d_cocoaShell release];
130 our d_cocoaShell = nullptr; // undangle
131 }
132 #endif
133 GuiShell_Parent :: v9_destroy ();
134 }
135
136 int GuiShell_getShellWidth (GuiShell me) {
137 int width = 0;
138 #if gtk
139 GtkAllocation allocation;
140 gtk_widget_get_allocation (GTK_WIDGET (my d_gtkWindow), & allocation); // TODO: replace with gtk_window_get_size()
141 width = allocation.width;
142 #elif motif
143 width = my d_xmShell -> width;
144 #elif cocoa
145 width = [my d_cocoaShell frame].size.width;
146 #endif
147 return width;
148 }
149
150 int GuiShell_getShellHeight (GuiShell me) {
151 int height = 0;
152 #if gtk
153 GtkAllocation allocation;
154 gtk_widget_get_allocation (GTK_WIDGET (my d_gtkWindow), & allocation); // TODO: replace with gtk_window_get_size()
155 height = allocation.height;
156 #elif motif
157 height = my d_xmShell -> height;
158 #elif cocoa
159 height = [my d_cocoaShell frame].size.height;
160 #endif
161 return height;
162 }
163
164 void GuiShell_setTitle (GuiShell me, conststring32 title /* cattable */) {
165 #if gtk
166 gtk_window_set_title (my d_gtkWindow, Melder_peek32to8 (title));
167 #if defined (chrome)
168 if (my chrome_surrogateShellTitleLabelWidget) {
169 char *markup = g_markup_printf_escaped ("<span weight=\"ultrabold\" underline=\"low\">%s</span>", Melder_peek32to8 (title));
170 gtk_label_set_markup (GTK_LABEL (my chrome_surrogateShellTitleLabelWidget), markup);
171 g_free (markup);
172 }
173 #endif
174 #elif motif
175 SetWindowTextW (my d_xmShell -> window, Melder_peek32toW (title));
176 #elif cocoa
177 [my d_cocoaShell setTitle: (NSString *) Melder_peek32toCfstring (title)];
178 #endif
179 }
180
181 void GuiShell_drain (GuiShell me, bool waitUntilItHasHappened, bool allowOtherEvents) {
182 #if gtk
183 //gdk_window_process_all_updates ();
184 while (gtk_events_pending ())
185 gtk_main_iteration ();
186 #elif motif
187 UpdateWindow (my d_xmShell -> window);
188 #elif cocoa
189 Melder_assert (my d_cocoaShell);
190 Melder_assert ([NSThread isMainThread]);
191 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
192 [my d_cocoaShell makeKeyAndOrderFront: nil];
193 [my d_cocoaShell layoutIfNeeded];
194 [my d_cocoaShell displayIfNeeded];
195 //[CATransaction flush]; // in case we ever implement layers
196 if (waitUntilItHasHappened) {
197 constexpr double smallestExpectedScreenRefreshFrequency = 50.0; // hertz
198 constexpr double greatestExpectedScreenRefreshPeriod =
199 1.0 / smallestExpectedScreenRefreshFrequency; // e.g. 0.02 seconds
200 constexpr double safeWaitingDurationForAtLeastOneScreenRefresh =
201 1.5 * greatestExpectedScreenRefreshPeriod; // e.g. 0.03 seconds
202 const double timeByWhichAtLeastOneScreenRefreshHasOccurred =
203 Melder_clock() + safeWaitingDurationForAtLeastOneScreenRefresh;
204 while (Melder_clock() < timeByWhichAtLeastOneScreenRefreshHasOccurred) {
205 NSEvent *nsEvent;
206 while ((nsEvent = [NSApp
207 nextEventMatchingMask: NSEventMaskAny
208 untilDate: [NSDate distantPast]
209 inMode: NSDefaultRunLoopMode
210 dequeue: YES]) != nullptr)
211 {
212 if (allowOtherEvents) {
213 [NSApp sendEvent: nsEvent];
214 } else {
215 NSUInteger nsEventType = [nsEvent type];
216 if (nsEventType == NSEventTypeKeyDown)
217 NSBeep();
218 /*
219 Ignore all other events.
220 */
221 }
222 }
223 constexpr double moderatePollingFrequency = 1000.0; // hertz
224 constexpr double moderatePollingPeriod = 1.0 / moderatePollingFrequency; // e.g. 1 ms
225 [NSThread sleepForTimeInterval: moderatePollingPeriod];
226 }
227 } else {
228 NSEvent *nsEvent;
229 while ((nsEvent = [NSApp
230 nextEventMatchingMask: NSEventMaskAny
231 untilDate: [NSDate distantPast]
232 inMode: NSDefaultRunLoopMode
233 dequeue: YES]) != nullptr)
234 {
235 if (allowOtherEvents) {
236 [NSApp sendEvent: nsEvent];
237 } else {
238 NSUInteger nsEventType = [nsEvent type];
239 if (nsEventType == NSEventTypeKeyDown)
240 NSBeep();
241 /*
242 Ignore all other events.
243 */
244 }
245 }
246 }
247 [pool release];
248 #endif
249 }
250
251 /* End of file GuiShell.cpp */