Debian Codegraph @

gtk/gtkwindowbuttonsquartz.c

raw symbols SCIP
1 /* GTK - The GIMP Toolkit
2 * Copyright (c) 2024 Arjan Molenaar <[email protected]>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * SPDX-License-Identifier: LGPL-2.1-or-later
18 */
19
20 #include "config.h"
21
22 #import <AppKit/AppKit.h>
23 #include <math.h>
24 #include <gdk/macos/GdkMacosWindow.h>
25 #include "gtkprivate.h"
26 #include "gtknative.h"
27 #include "gtkwindowbuttonsquartzprivate.h"
28
29 @interface NSWindow()
30 /* Expose the private titlebarHeight property, so we can set
31 * the titlebar height to match the height of a GTK header bar.
32 */
33 @property CGFloat titlebarHeight;
34
35 @end
36
37 enum
38 {
39 PROP_0,
40 PROP_DECORATION_LAYOUT,
41 NUM_PROPERTIES
42 };
43
44 static GParamSpec *props[NUM_PROPERTIES] = { NULL, };
45
46 /**
47 * GtkWindowButtonsQuartz:
48 *
49 * This class provides macOS native window buttons for close/minimize/maximize.
50 *
51 * The buttons can be set by adding "native" to the `decoration-layout` of
52 * GtkWindowControls or GtkHeader.
53 *
54 * ## Accessibility
55 *
56 * `GtkWindowButtonsQuartz` uses the `GTK_ACCESSIBLE_ROLE_IMG` role.
57 */
58
59 typedef struct _GtkWindowButtonsQuartzClass GtkWindowButtonsQuartzClass;
60
61 struct _GtkWindowButtonsQuartz
62 {
63 GtkWidget parent_instance;
64
65 gboolean close;
66 gboolean minimize;
67 gboolean maximize;
68
69 char *decoration_layout;
70
71 GBinding *fullscreen_binding;
72 };
73
74 struct _GtkWindowButtonsQuartzClass
75 {
76 GtkWidgetClass parent_class;
77 };
78
79 G_DEFINE_TYPE (GtkWindowButtonsQuartz, gtk_window_buttons_quartz, GTK_TYPE_WIDGET)
80
81 static void
82 set_window_controls_height (GdkMacosWindow *window,
83 int height)
84 {
85 g_return_if_fail (GDK_IS_MACOS_WINDOW (window));
86
87 if ([window respondsToSelector:@selector(setTitlebarHeight:)])
88 [window setTitlebarHeight:height];
89 [[window contentView] setNeedsLayout:YES];
90 }
91
92 static void
93 window_controls_bounds (NSWindow *window, NSRect *out_bounds)
94 {
95 NSRect bounds = NSZeroRect;
96 NSButton* button;
97
98 button = [window standardWindowButton:NSWindowCloseButton];
99 bounds = NSUnionRect(bounds, [button frame]);
100
101 button = [window standardWindowButton:NSWindowZoomButton];
102 bounds = NSUnionRect(bounds, [button frame]);
103
104 *out_bounds = bounds;
105 }
106
107 static GdkMacosWindow*
108 native_window (GtkWidget *widget)
109 {
110 GtkNative *native = GTK_NATIVE (gtk_widget_get_root (widget));
111
112 if (native != NULL)
113 {
114 GdkSurface *surface = gtk_native_get_surface (native);
115
116 if (GDK_IS_MACOS_SURFACE (surface))
117 return (GdkMacosWindow*) gdk_macos_surface_get_native_window (GDK_MACOS_SURFACE (surface));
118 }
119 return NULL;
120 }
121
122 static void
123 enable_window_controls (GtkWindowButtonsQuartz *self,
124 gboolean enabled)
125 {
126 gboolean is_sovereign_window;
127 gboolean resizable;
128 gboolean deletable;
129 GtkRoot *root;
130 GtkWindow *window;
131 GdkMacosWindow *nswindow;
132
133 root = gtk_widget_get_root (GTK_WIDGET (self));
134 if (!root || !GTK_IS_WINDOW (root))
135 return;
136
137 nswindow = native_window (GTK_WIDGET (self));
138 if (!GDK_IS_MACOS_WINDOW (nswindow))
139 return;
140
141 window = GTK_WINDOW (root);
142 is_sovereign_window = !gtk_window_get_modal (window) &&
143 gtk_window_get_transient_for (window) == NULL;
144 resizable = gtk_window_get_resizable (window);
145 deletable = gtk_window_get_deletable (window);
146
147 [[nswindow standardWindowButton:NSWindowCloseButton] setEnabled:enabled && self->close && deletable];
148 [[nswindow standardWindowButton:NSWindowMiniaturizeButton] setEnabled:enabled && self->minimize && is_sovereign_window];
149 [[nswindow standardWindowButton:NSWindowZoomButton] setEnabled:enabled && self->maximize && resizable && is_sovereign_window];
150 }
151
152 static void
153 update_window_controls_from_decoration_layout (GtkWindowButtonsQuartz *self)
154 {
155 char **tokens;
156
157 if (self->decoration_layout)
158 tokens = g_strsplit_set (self->decoration_layout, ",:", -1);
159 else
160 {
161 char *layout_desc;
162 g_object_get (gtk_widget_get_settings (GTK_WIDGET (self)),
163 "gtk-decoration-layout", &layout_desc,
164 NULL);
165 tokens = g_strsplit_set (layout_desc, ",:", -1);
166
167 g_free (layout_desc);
168 }
169
170 self->close = g_strv_contains ((const char * const *) tokens, "close");
171 self->minimize = g_strv_contains ((const char * const *) tokens, "minimize");
172 self->maximize = g_strv_contains ((const char * const *) tokens, "maximize");
173
174 g_strfreev (tokens);
175
176 enable_window_controls (self, TRUE);
177 }
178
179 static void
180 gtk_window_buttons_quartz_finalize (GObject *object)
181 {
182 GtkWindowButtonsQuartz *self = GTK_WINDOW_BUTTONS_QUARTZ (object);
183
184 g_clear_pointer (&self->decoration_layout, g_free);
185
186 G_OBJECT_CLASS (gtk_window_buttons_quartz_parent_class)->finalize (object);
187 }
188
189 static void
190 gtk_window_buttons_quartz_get_property (GObject *object,
191 guint prop_id,
192 GValue *value,
193 GParamSpec *pspec)
194 {
195 GtkWindowButtonsQuartz *self = GTK_WINDOW_BUTTONS_QUARTZ (object);
196
197 switch (prop_id)
198 {
199 case PROP_DECORATION_LAYOUT:
200 g_value_set_string (value, self->decoration_layout);
201 break;
202 default:
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
204 break;
205 }
206 }
207
208 static void
209 gtk_window_buttons_quartz_set_property (GObject *object,
210 guint prop_id,
211 const GValue *value,
212 GParamSpec *pspec)
213 {
214 GtkWindowButtonsQuartz *self = GTK_WINDOW_BUTTONS_QUARTZ (object);
215
216 switch (prop_id)
217 {
218 case PROP_DECORATION_LAYOUT:
219 g_free (self->decoration_layout);
220 self->decoration_layout = g_strdup (g_value_get_string (value));
221
222 update_window_controls_from_decoration_layout (self);
223 break;
224 default:
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 break;
227 }
228 }
229
230 static void
231 gtk_window_buttons_quartz_root (GtkWidget *widget)
232 {
233 GtkWindowButtonsQuartz *self = GTK_WINDOW_BUTTONS_QUARTZ (widget);
234
235 GTK_WIDGET_CLASS (gtk_window_buttons_quartz_parent_class)->root (widget);
236
237 if (self->fullscreen_binding)
238 g_object_unref (self->fullscreen_binding);
239
240 self->fullscreen_binding = g_object_bind_property (gtk_widget_get_root (widget),
241 "fullscreened",
242 widget,
243 "visible",
244 G_BINDING_SYNC_CREATE | G_BINDING_INVERT_BOOLEAN);
245 }
246
247 static void
248 gtk_window_buttons_quartz_unroot (GtkWidget *widget)
249 {
250 GtkWindowButtonsQuartz *self = GTK_WINDOW_BUTTONS_QUARTZ (widget);
251
252 if (self->fullscreen_binding)
253 {
254 g_object_unref (self->fullscreen_binding);
255 self->fullscreen_binding = NULL;
256 }
257
258 GTK_WIDGET_CLASS (gtk_window_buttons_quartz_parent_class)->unroot (widget);
259 }
260
261 static void
262 gtk_window_buttons_quartz_realize (GtkWidget *widget)
263 {
264 GtkWindowButtonsQuartz *self = GTK_WINDOW_BUTTONS_QUARTZ (widget);
265 GdkMacosWindow *window;
266 NSRect bounds;
267
268 GTK_WIDGET_CLASS (gtk_window_buttons_quartz_parent_class)->realize (widget);
269
270 window = native_window (widget);
271
272 if (!GDK_IS_MACOS_WINDOW (window))
273 {
274 g_critical ("Cannot show GtkWindowButtonsQuartz on a non-macos window");
275 return;
276 }
277
278 [window setShowStandardWindowButtons:YES];
279
280 enable_window_controls (self, TRUE);
281
282 window_controls_bounds (window, &bounds);
283 gtk_widget_set_size_request (widget, bounds.origin.x + bounds.size.width, bounds.size.height);
284 }
285
286 static void
287 gtk_window_buttons_quartz_unrealize (GtkWidget *widget)
288 {
289 GdkMacosWindow *window = native_window (widget);
290
291 if (GDK_IS_MACOS_WINDOW (window))
292 [window setShowStandardWindowButtons:NO];
293
294 GTK_WIDGET_CLASS (gtk_window_buttons_quartz_parent_class)->unrealize (widget);
295 }
296
297 static void
298 gtk_window_buttons_quartz_measure (GtkWidget *widget,
299 GtkOrientation orientation,
300 int for_size,
301 int *minimum,
302 int *natural,
303 int *minimum_baseline,
304 int *natural_baseline)
305 {
306 GdkMacosWindow *window = native_window (widget);
307
308 if (GDK_IS_MACOS_WINDOW (window))
309 {
310 NSRect bounds;
311
312 window_controls_bounds (window, &bounds);
313
314 if (orientation == GTK_ORIENTATION_VERTICAL)
315 *minimum = *natural = ceil(bounds.size.height);
316 else if (orientation == GTK_ORIENTATION_HORIZONTAL)
317 *minimum = *natural = ceil(bounds.origin.x + bounds.size.width);
318 }
319 }
320
321 static void
322 gtk_window_buttons_quartz_size_allocate (GtkWidget *widget,
323 int width,
324 int height,
325 int baseline)
326 {
327 GdkMacosWindow *window = native_window (widget);
328 graphene_rect_t bounds = { 0 };
329
330 GTK_WIDGET_CLASS (gtk_window_buttons_quartz_parent_class)->size_allocate (widget, width, height, baseline);
331
332 if (!gtk_widget_compute_bounds (widget, (GtkWidget *) gtk_widget_get_root (widget), &bounds))
333 g_warning ("Could not calculate widget bounds");
334
335 set_window_controls_height (window, bounds.origin.y * 2 + height);
336 }
337
338 static void
339 gtk_window_buttons_quartz_class_init (GtkWindowButtonsQuartzClass *class)
340 {
341 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
342 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
343
344 gobject_class->finalize = gtk_window_buttons_quartz_finalize;
345 gobject_class->get_property = gtk_window_buttons_quartz_get_property;
346 gobject_class->set_property = gtk_window_buttons_quartz_set_property;
347
348 widget_class->measure = gtk_window_buttons_quartz_measure;
349 widget_class->size_allocate = gtk_window_buttons_quartz_size_allocate;
350 widget_class->root = gtk_window_buttons_quartz_root;
351 widget_class->unroot = gtk_window_buttons_quartz_unroot;
352 widget_class->realize = gtk_window_buttons_quartz_realize;
353 widget_class->unrealize = gtk_window_buttons_quartz_unrealize;
354
355 /**
356 * GtkWindowButtonsQuartz:decoration-layout:
357 *
358 * The decoration layout for window buttons.
359 *
360 * If this property is not set, the
361 * [[email protected]:gtk-decoration-layout] setting is used.
362 */
363 props[PROP_DECORATION_LAYOUT] =
364 g_param_spec_string ("decoration-layout", NULL, NULL,
365 NULL,
366 GTK_PARAM_READWRITE);
367
368 g_object_class_install_properties (gobject_class, NUM_PROPERTIES, props);
369
370 gtk_widget_class_set_css_name (widget_class, I_("windowbuttonsquartz"));
371
372 gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_IMG);
373 }
374
375 static void
376 gtk_window_buttons_quartz_init (GtkWindowButtonsQuartz *self)
377 {
378 self->close = TRUE;
379 self->minimize = TRUE;
380 self->maximize = TRUE;
381 self->decoration_layout = NULL;
382 self->fullscreen_binding = NULL;
383 }