Debian Codegraph @

lib/gtk/font-manager-application-window.c

raw
1 /* font-manager-application-window.c
2 *
3 * Copyright (C) 2022-2024 Jerry Casiano
4 *
5 * This program 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
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * 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 program.
17 *
18 * If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
19 */
20
21 #include "font-manager-application-window.h"
22
23 /**
24 * SECTION: font-manager-application-window
25 * @short_description: Base class for application windows
26 * @title: Application Window
27 * @include: font-manager-application-window.h
28 *
29 * Base class for application windows. Sets up common keyboard shortcuts and
30 * provides for saving and restoring state.
31 */
32
33 typedef struct
34 {
35 GSettings *settings;
36 }
37 FontManagerApplicationWindowPrivate;
38
39 G_DEFINE_TYPE_WITH_PRIVATE(FontManagerApplicationWindow,
40 font_manager_application_window,
41 GTK_TYPE_APPLICATION_WINDOW)
42
43 enum
44 {
45 PROP_RESERVED,
46 PROP_SETTINGS,
47 NUM_PROPS
48 };
49
50 static void
51 font_manager_application_window_dispose (GObject *gobject)
52 {
53 FontManagerApplicationWindow *self = FONT_MANAGER_APPLICATION_WINDOW(gobject);
54 g_return_if_fail(self != NULL);
55 FontManagerApplicationWindowPrivate *priv;
56 priv = font_manager_application_window_get_instance_private(self);
57 g_clear_object(&priv->settings);
58 font_manager_widget_dispose(GTK_WIDGET(gobject));
59 G_OBJECT_CLASS(font_manager_application_window_parent_class)->dispose(gobject);
60 return;
61 }
62
63 static void
64 font_manager_application_window_about (GtkWidget *widget,
65 G_GNUC_UNUSED const char *action_name,
66 G_GNUC_UNUSED GVariant *parameter)
67 {
68 g_return_if_fail(widget != NULL);
69 font_manager_application_window_show_about(FONT_MANAGER_APPLICATION_WINDOW(widget));
70 return;
71 }
72
73 static void
74 font_manager_application_window_help (GtkWidget *widget,
75 G_GNUC_UNUSED const char *action_name,
76 G_GNUC_UNUSED GVariant *parameter)
77 {
78 g_return_if_fail(widget != NULL);
79 font_manager_application_window_show_help(FONT_MANAGER_APPLICATION_WINDOW(widget));
80 return;
81 }
82
83 static gboolean
84 font_manager_application_window_on_close_request (GtkWindow *window)
85 {
86 FontManagerApplicationWindow *self = FONT_MANAGER_APPLICATION_WINDOW(window);
87 FontManagerApplicationWindowPrivate *priv;
88 priv = font_manager_application_window_get_instance_private(self);
89 GdkSurface *surface = gtk_native_get_surface(GTK_NATIVE(window));
90 GdkToplevelState state = gdk_toplevel_get_state(GDK_TOPLEVEL(surface));
91 gboolean tiled = (state & GDK_TOPLEVEL_STATE_TILED);
92 if (tiled) {
93 g_debug("State not saved, tiled window detected");
94 } else if (priv->settings && !tiled) {
95 gint width, height;
96 g_object_get(window,
97 "default-width", &width,
98 "default-height", &height,
99 NULL);
100 g_debug("Saving state : Window size : %i x %i", width, height);
101 g_settings_set(priv->settings, "window-size", "(ii)", width, height);
102 g_clear_object(&priv->settings);
103 } else {
104 g_debug("Settings instance unavailable, failed to save state");
105 }
106 return GTK_WINDOW_CLASS(font_manager_application_window_parent_class)->close_request(window);
107 }
108
109 static void
110 font_manager_application_window_quit (GtkWidget *widget,
111 G_GNUC_UNUSED const char *action_name,
112 G_GNUC_UNUSED GVariant *parameter)
113 {
114 g_return_if_fail(widget != NULL);
115 gtk_window_close(GTK_WINDOW(widget));
116 return;
117 }
118
119 static void
120 font_manager_application_window_get_property (GObject *gobject,
121 guint property_id,
122 GValue *value,
123 GParamSpec *pspec)
124 {
125 g_return_if_fail(gobject != NULL);
126 FontManagerApplicationWindow *self = FONT_MANAGER_APPLICATION_WINDOW(gobject);
127 FontManagerApplicationWindowPrivate *priv;
128 priv = font_manager_application_window_get_instance_private(self);
129 switch (property_id) {
130 case PROP_SETTINGS:
131 g_value_set_object(value, priv->settings);
132 break;
133 default:
134 G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
135 }
136 return;
137 }
138
139 static void
140 font_manager_application_window_set_property (GObject *gobject,
141 guint property_id,
142 const GValue *value,
143 GParamSpec *pspec)
144 {
145 g_return_if_fail(gobject != NULL);
146 FontManagerApplicationWindow *self = FONT_MANAGER_APPLICATION_WINDOW(gobject);
147 FontManagerApplicationWindowPrivate *priv;
148 priv = font_manager_application_window_get_instance_private(self);
149 switch (property_id) {
150 case PROP_SETTINGS:
151 g_set_object(&priv->settings, G_SETTINGS(g_value_get_object(value)));
152 break;
153 default:
154 G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
155 }
156 return;
157 }
158
159 static void
160 font_manager_application_window_class_init (FontManagerApplicationWindowClass *klass)
161 {
162 GObjectClass *object_class = G_OBJECT_CLASS(klass);
163 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
164 GtkWindowClass *window_class = GTK_WINDOW_CLASS(klass);
165 window_class->close_request = font_manager_application_window_on_close_request;
166 object_class->dispose = font_manager_application_window_dispose;
167 object_class->get_property = font_manager_application_window_get_property;
168 object_class->set_property = font_manager_application_window_set_property;
169
170 gtk_widget_class_install_action(widget_class,
171 "about",
172 NULL,
173 font_manager_application_window_about);
174
175 gtk_widget_class_install_action(widget_class,
176 "help",
177 NULL,
178 font_manager_application_window_help);
179
180 gtk_widget_class_install_action(widget_class,
181 "quit",
182 NULL,
183 font_manager_application_window_quit);
184
185 gtk_widget_class_add_binding_action(widget_class,
186 GDK_KEY_F1,
187 0,
188 "help", NULL);
189
190 gtk_widget_class_add_binding_action(widget_class,
191 GDK_KEY_q,
192 GDK_CONTROL_MASK,
193 "quit", NULL);
194
195 gtk_widget_class_add_binding_action(widget_class,
196 GDK_KEY_w,
197 GDK_CONTROL_MASK,
198 "quit", NULL);
199
200 /**
201 * FontManagerApplicationWindow:settings:
202 *
203 * The following keys MUST be present in @settings:
204 *
205 * - window-size : (int, int)
206 * - is-maximized : boolean
207 */
208 g_object_class_install_property(object_class,
209 PROP_SETTINGS,
210 g_param_spec_object("settings",
211 NULL,
212 "#GSettings instance to use",
213 G_TYPE_SETTINGS,
214 G_PARAM_STATIC_STRINGS |
215 G_PARAM_READWRITE));
216
217 return;
218 }
219
220 static void
221 font_manager_application_window_init (FontManagerApplicationWindow *self)
222 {
223 font_manager_set_application_style();
224 return;
225 }
226
227 /**
228 * font_manager_application_window_restore_state:
229 * @self: #FontManagerApplicationWindow
230 */
231 void
232 font_manager_application_window_restore_state (FontManagerApplicationWindow *self)
233 {
234 FontManagerApplicationWindowPrivate *priv;
235 priv = font_manager_application_window_get_instance_private(self);
236 gint width, height;
237 if (priv->settings) {
238 g_settings_bind (priv->settings, "is-maximized", self, "maximized", G_SETTINGS_BIND_DEFAULT);
239 g_settings_get(priv->settings, "window-size", "(ii)", &width, &height);
240 g_debug("Restoring state : Window size : %i x %i", width, height);
241 gtk_window_set_default_size(GTK_WINDOW(self), width, height);
242 } else {
243 g_debug("Settings instance unavailable, failed to restore state");
244 }
245 return;
246 }
247
248 #define PACKAGE_LICENSE \
249 " This program is free software: you can redistribute it and/or modify\n" \
250 " it under the terms of the GNU General Public License as published by\n" \
251 " the Free Software Foundation, either version 3 of the License, or\n" \
252 " (at your option) any later version.\n" \
253 "\n" \
254 " This program is distributed in the hope that it will be useful,\n" \
255 " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \
256 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \
257 " GNU General Public License for more details.\n" \
258 "\n" \
259 " You should have received a copy of the GNU General Public License\n" \
260 " along with this program.\n" \
261 "\n" \
262 " If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.\n"
263
264 /**
265 * font_manager_application_window_show_about:
266 * @self: #FontManagerApplicationWindow
267 *
268 * Show "About" dialog
269 */
270 void
271 font_manager_application_window_show_about (FontManagerApplicationWindow *self)
272 {
273 g_return_if_fail(self);
274
275 const char *authors[] = {
276 "Jerry Casiano <[email protected]>",
277 NULL
278 };
279
280 gtk_show_about_dialog(GTK_WINDOW(self),
281 "program-name", _("Font Manager"),
282 "logo-icon-name", "com.github.FontManager.FontManager-symbolic",
283 "version", PACKAGE_VERSION,
284 "copyright", "Copyright © 2009-2024 Jerry Casiano",
285 "comments", _("Simple font management for GTK+ desktop environments"),
286 "website", PACKAGE_URL,
287 "authors", authors,
288 "documenters", authors,
289 "license", PACKAGE_LICENSE,
290 "translator-credits", _("translator-credits"),
291 NULL
292 );
293 return;
294 }
295
296 /**
297 * font_manager_application_window_show_help:
298 * @self: #FontManagerApplicationWindow
299 *
300 * Attempts to launch default help application (Yelp)
301 */
302 void
303 font_manager_application_window_show_help (FontManagerApplicationWindow *self)
304 {
305 g_return_if_fail(self);
306 g_autofree gchar *uri = g_strdup_printf("help:%s", PACKAGE_NAME);
307 g_autoptr(GtkUriLauncher) launcher = gtk_uri_launcher_new(uri);
308 gtk_uri_launcher_launch(launcher, GTK_WINDOW(self), NULL, NULL, NULL);
309 return;
310 }
311
312 /**
313 * font_manager_application_window_new:
314 * @settings: #Gsettings instance to use or %NULL
315 *
316 * The following keys MUST be present in @settings:
317 *
318 * - window-size : (int, int)
319 * - is-maximized : boolean
320 *
321 * Returns: (transfer full): A newly created #FontManagerApplicationWindow
322 * Free the returned object using #g_object_unref().
323 */
324 GtkWidget *
325 font_manager_application_window_new (GSettings *settings)
326 {
327 return g_object_new(FONT_MANAGER_TYPE_APPLICATION_WINDOW, "settings", settings, NULL);
328 }
329
330