|
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-window.h"
|
|
11 |
|
|
12 |
struct _XAppGtkWindowPrivate
|
|
13 |
{
|
|
14 |
gchar *icon_name;
|
|
15 |
gchar *icon_path;
|
|
16 |
gboolean need_set_at_realize;
|
|
17 |
};
|
|
18 |
|
|
19 |
G_DEFINE_TYPE (XAppGtkWindow, xapp_gtk_window, GTK_TYPE_WINDOW);
|
|
20 |
|
|
21 |
static void
|
|
22 |
clear_strings (XAppGtkWindow *window)
|
|
23 |
{
|
|
24 |
XAppGtkWindowPrivate *priv = window->priv;
|
|
25 |
|
|
26 |
g_clear_pointer (&priv->icon_name, g_free);
|
|
27 |
g_clear_pointer (&priv->icon_path, g_free);
|
|
28 |
}
|
|
29 |
|
|
30 |
static void
|
|
31 |
set_window_hint (GtkWidget *widget,
|
|
32 |
const gchar *str)
|
|
33 |
{
|
|
34 |
GdkDisplay *display;
|
|
35 |
GdkWindow *window;
|
|
36 |
|
|
37 |
window = gtk_widget_get_window (widget);
|
|
38 |
|
|
39 |
if (gdk_window_get_effective_toplevel (window) != window)
|
|
40 |
{
|
|
41 |
g_warning ("Window is not toplevel");
|
|
42 |
return;
|
|
43 |
}
|
|
44 |
|
|
45 |
display = gdk_window_get_display (window);
|
|
46 |
|
|
47 |
if (str != NULL)
|
|
48 |
{
|
|
49 |
XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
|
|
50 |
GDK_WINDOW_XID (window),
|
|
51 |
gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_XAPP_ICON_NAME"),
|
|
52 |
gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
|
|
53 |
PropModeReplace, (guchar *) str, strlen (str));
|
|
54 |
}
|
|
55 |
else
|
|
56 |
{
|
|
57 |
XDeleteProperty (GDK_DISPLAY_XDISPLAY (display),
|
|
58 |
GDK_WINDOW_XID (window),
|
|
59 |
gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_XAPP_ICON_NAME"));
|
|
60 |
}
|
|
61 |
}
|
|
62 |
|
|
63 |
static void
|
|
64 |
update_window (XAppGtkWindow *window)
|
|
65 |
{
|
|
66 |
XAppGtkWindowPrivate *priv = window->priv;
|
|
67 |
|
|
68 |
if (priv->icon_name != NULL)
|
|
69 |
{
|
|
70 |
set_window_hint (GTK_WIDGET (window), priv->icon_name);
|
|
71 |
}
|
|
72 |
else if (priv->icon_path != NULL)
|
|
73 |
{
|
|
74 |
set_window_hint (GTK_WIDGET (window), priv->icon_path);
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
static void
|
|
79 |
xapp_gtk_window_realize (GtkWidget *widget)
|
|
80 |
{
|
|
81 |
XAppGtkWindow *window = XAPP_GTK_WINDOW (widget);
|
|
82 |
XAppGtkWindowPrivate *priv = window->priv;
|
|
83 |
|
|
84 |
GTK_WIDGET_CLASS (xapp_gtk_window_parent_class)->realize (widget);
|
|
85 |
|
|
86 |
if (priv->need_set_at_realize)
|
|
87 |
{
|
|
88 |
update_window (window);
|
|
89 |
priv->need_set_at_realize = FALSE;
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
static void
|
|
94 |
xapp_gtk_window_unrealize (GtkWidget *widget)
|
|
95 |
{
|
|
96 |
XAppGtkWindow *window = XAPP_GTK_WINDOW (widget);
|
|
97 |
XAppGtkWindowPrivate *priv = window->priv;
|
|
98 |
|
|
99 |
GTK_WIDGET_CLASS (xapp_gtk_window_parent_class)->unrealize (widget);
|
|
100 |
|
|
101 |
if (priv->icon_name != NULL || priv->icon_path != NULL)
|
|
102 |
{
|
|
103 |
priv->need_set_at_realize = TRUE;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
static void
|
|
108 |
xapp_gtk_window_finalize (GObject *object)
|
|
109 |
{
|
|
110 |
XAppGtkWindow *window = XAPP_GTK_WINDOW (object);
|
|
111 |
XAppGtkWindowPrivate *priv = window->priv;
|
|
112 |
|
|
113 |
g_clear_pointer (&priv->icon_name, g_free);
|
|
114 |
g_clear_pointer (&priv->icon_path, g_free);
|
|
115 |
|
|
116 |
G_OBJECT_CLASS (xapp_gtk_window_parent_class)->finalize (object);
|
|
117 |
}
|
|
118 |
|
|
119 |
static void
|
|
120 |
xapp_gtk_window_init (XAppGtkWindow *window)
|
|
121 |
{
|
|
122 |
XAppGtkWindowPrivate *priv;
|
|
123 |
|
|
124 |
window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, XAPP_TYPE_GTK_WINDOW, XAppGtkWindowPrivate);
|
|
125 |
|
|
126 |
priv = window->priv;
|
|
127 |
|
|
128 |
priv->icon_name = NULL;
|
|
129 |
priv->icon_path = NULL;
|
|
130 |
priv->need_set_at_realize = FALSE;
|
|
131 |
}
|
|
132 |
|
|
133 |
static void
|
|
134 |
xapp_gtk_window_class_init (XAppGtkWindowClass *klass)
|
|
135 |
{
|
|
136 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
137 |
GtkWidgetClass *wclass = GTK_WIDGET_CLASS (klass);
|
|
138 |
|
|
139 |
gobject_class->finalize = xapp_gtk_window_finalize;
|
|
140 |
wclass->realize = xapp_gtk_window_realize;
|
|
141 |
wclass->unrealize = xapp_gtk_window_unrealize;
|
|
142 |
|
|
143 |
g_type_class_add_private (gobject_class, sizeof (XAppGtkWindowPrivate));
|
|
144 |
}
|
|
145 |
|
|
146 |
XAppGtkWindow *
|
|
147 |
xapp_gtk_window_new (void)
|
|
148 |
{
|
|
149 |
return g_object_new (XAPP_TYPE_GTK_WINDOW, NULL);
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* xapp_gtk_window_set_icon_name:
|
|
154 |
* @window: The #GtkWindow to set the icon name for
|
|
155 |
* @icon_name: (nullable): The icon name or path to set, or %NULL to unset.
|
|
156 |
*
|
|
157 |
* Sets the icon name hint for a window manager (like muffin) to make
|
|
158 |
* available when applications want to change their icons during runtime
|
|
159 |
* without having to resort to the internal low-res pixbufs that GdkWindow
|
|
160 |
* sets on the client side. This also chains up and calls GtkWindow.set_icon_name
|
|
161 |
* for convenience and compatibility. Set to %NULL to unset.
|
|
162 |
*/
|
|
163 |
void
|
|
164 |
xapp_gtk_window_set_icon_name (XAppGtkWindow *window,
|
|
165 |
const gchar *icon_name)
|
|
166 |
{
|
|
167 |
g_return_if_fail (XAPP_IS_GTK_WINDOW (window));
|
|
168 |
|
|
169 |
clear_strings (window);
|
|
170 |
|
|
171 |
if (icon_name != NULL)
|
|
172 |
{
|
|
173 |
window->priv->icon_name = g_strdup (icon_name);
|
|
174 |
}
|
|
175 |
|
|
176 |
/* If the window is realized, set the icon name immediately */
|
|
177 |
if (gtk_widget_get_realized (GTK_WIDGET (window)))
|
|
178 |
{
|
|
179 |
update_window (window);
|
|
180 |
}
|
|
181 |
/* Otherwise, remind ourselves to do it in our realize vfunc */
|
|
182 |
else
|
|
183 |
{
|
|
184 |
window->priv->need_set_at_realize = TRUE;
|
|
185 |
}
|
|
186 |
|
|
187 |
/* Call the GtkWindow method for compatibility */
|
|
188 |
gtk_window_set_icon_name (GTK_WINDOW (window), icon_name);
|
|
189 |
}
|
|
190 |
|
|
191 |
|
|
192 |
/**
|
|
193 |
* xapp_gtk_window_set_icon_from_file:
|
|
194 |
* @window: The #XAppGtkWindow to set the icon name for
|
|
195 |
* @file_name: (nullable): The icon path to set, or %NULL to unset.
|
|
196 |
* @error: (nullable): An error to set if something goes wrong.
|
|
197 |
*
|
|
198 |
* Sets the icon name hint for a window manager (like muffin) to make
|
|
199 |
* available when applications want to change their icons during runtime
|
|
200 |
* without having to resort to the internal low-res pixbufs that GdkWindow
|
|
201 |
* sets on the client side. This also chains up and calls GtkWindow.set_icon_from_file
|
|
202 |
* for convenience and compatibility. Set to %NULL to unset.
|
|
203 |
*/
|
|
204 |
void
|
|
205 |
xapp_gtk_window_set_icon_from_file (XAppGtkWindow *window,
|
|
206 |
const gchar *file_name,
|
|
207 |
GError **error)
|
|
208 |
{
|
|
209 |
g_return_if_fail (XAPP_IS_GTK_WINDOW (window));
|
|
210 |
|
|
211 |
clear_strings (window);
|
|
212 |
|
|
213 |
if (file_name != NULL)
|
|
214 |
{
|
|
215 |
window->priv->icon_path = g_strdup (file_name);
|
|
216 |
}
|
|
217 |
|
|
218 |
/* If the window is realized, set the icon path immediately */
|
|
219 |
if (gtk_widget_get_realized (GTK_WIDGET (window)))
|
|
220 |
{
|
|
221 |
update_window (window);
|
|
222 |
}
|
|
223 |
/* Otherwise, remind ourselves to do it later */
|
|
224 |
else
|
|
225 |
{
|
|
226 |
window->priv->need_set_at_realize = TRUE;
|
|
227 |
}
|
|
228 |
|
|
229 |
gtk_window_set_icon_from_file (GTK_WINDOW (window), file_name, error);
|
|
230 |
}
|
|
231 |
|