Codebase list xapp / 091baee
Add xapp-gtk-utils.c/.h Michael Webster 6 years ago
3 changed file(s) with 127 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
1515
1616 introspection_sources = \
1717 xapp-monitor-blanker.c \
18 xapp-kbd-layout-controller.c
18 xapp-kbd-layout-controller.c \
19 xapp-gtk-utils.c
1920
2021 libxapp_la_SOURCES = \
2122 $(introspection_sources)
3738 libxappdir = $(includedir)/xapp/libxapp
3839 libxapp_HEADERS = \
3940 xapp-monitor-blanker.h \
40 xapp-kbd-layout-controller.h
41 xapp-kbd-layout-controller.h \
42 xapp-gtk-utils.h
4143
4244 -include $(INTROSPECTION_MAKEFILE)
4345 INTROSPECTION_GIRS =
0
1 #include <config.h>
2
3 #include <stdlib.h>
4 #include <string.h>
5 #include <math.h>
6 #include <X11/Xlib.h>
7
8 #include <gdk/gdk.h>
9 #include <gdk/gdkx.h>
10 #include "xapp-gtk-utils.h"
11
12 static void
13 set_icon_name (GtkWidget *widget,
14 const gchar *icon_name)
15 {
16 GdkDisplay *display;
17 GdkWindow *window;
18
19 window = gtk_widget_get_window (widget);
20
21 if (gdk_window_get_effective_toplevel (window) != window)
22 {
23 g_warning ("Window is not toplevel");
24 return;
25 }
26
27 display = gdk_window_get_display (window);
28
29 if (icon_name != NULL)
30 {
31 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
32 GDK_WINDOW_XID (window),
33 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_XAPP_ICON_NAME"),
34 gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
35 PropModeReplace, (guchar *)icon_name, strlen (icon_name));
36 }
37 else
38 {
39 XDeleteProperty (GDK_DISPLAY_XDISPLAY (display),
40 GDK_WINDOW_XID (window),
41 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_XAPP_ICON_NAME"));
42 }
43 }
44
45 static void
46 on_gtk_window_realized (GtkWidget *widget,
47 gpointer user_data)
48 {
49 gchar *int_string;
50
51 g_return_if_fail (GTK_IS_WIDGET (widget));
52
53 int_string = (gchar *) user_data;
54
55 g_signal_handlers_disconnect_by_func (widget, on_gtk_window_realized, int_string);
56 g_object_weak_unref (G_OBJECT (widget), (GWeakNotify) g_free, int_string);
57
58 set_icon_name (widget, int_string);
59
60 g_free (int_string);
61 }
62
63 /**
64 * xapp_gtk_window_set_icon_name:
65 * @window: The #GtkWindow to set the icon name for
66 * @icon_name: (nullable): The icon name or path to set, or %NULL to unset.
67 *
68 * Sets the icon name hint for a window manager (like muffin) to make
69 * available when applications want to change their icons during runtime
70 * without having to resort to the internal low-res pixbufs that GdkWindow
71 * sets on the client side. This string can be either an icon name
72 * corresponding to something in the icon theme, or an absolute path to an
73 * icon, or %NULL to unset.
74 */
75 void
76 xapp_gtk_window_set_icon_name (GtkWindow *window,
77 const gchar *icon_name)
78 {
79 g_return_if_fail (GTK_IS_WINDOW (window));
80
81 /* If the window is realized, set the icon name immediately */
82
83 if (gtk_widget_get_realized (GTK_WIDGET (window)))
84 {
85 set_icon_name (GTK_WIDGET (window), icon_name);
86 }
87 /* Otherwise, hang a callback on window's realize signal and do it then */
88 else
89 {
90 gchar *int_string;
91
92 int_string = g_strdup (icon_name);
93
94 g_signal_connect_after (GTK_WIDGET (window),
95 "realize",
96 G_CALLBACK (on_gtk_window_realized),
97 int_string);
98
99 /* Insurance, in case window gets destroyed without ever being realized */
100 g_object_weak_ref (G_OBJECT (window),
101 (GWeakNotify) g_free,
102 int_string);
103 }
104 }
105
106
0 #ifndef __XAPP_GTK_UTILS_H__
1 #define __XAPP_GTK_UTILS_H__
2
3 #include <stdio.h>
4
5 #include <glib-object.h>
6 #include <gtk/gtk.h>
7
8 G_BEGIN_DECLS
9
10 void xapp_gtk_window_set_icon_name (GtkWindow *window,
11 const gchar *icon_name);
12
13 G_END_DECLS
14
15 #endif /* __XAPP_GTK_UTILS_H__ */