Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 69cc8f48526eab99e3daefa6abd5000c259ff382 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*******************************************************************************
 * Copyright (c) 2019 Syntevo and others. All rights reserved.
 * The contents of this file are made available under the terms
 * of the GNU Lesser General Public License (LGPL) Version 2.1 that
 * accompanies this distribution (lgpl-v21.txt).  The LGPL is also
 * available at http://www.gnu.org/licenses/lgpl.html.  If the version
 * of the LGPL at http://www.gnu.org is different to the version of
 * the LGPL accompanying this distribution and there is any conflict
 * between the two license versions, the terms of the LGPL accompanying
 * this distribution shall govern.
 *
 * Contributors:
 *     Syntevo - initial implementation
 *******************************************************************************/
#include <gtk/gtk.h>
#include <cstring>
#include <string>
#include <string.h>

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

std::string format_GdkRGBA(GdkRGBA a_Color)
{
	char buffer[64];

	sprintf
	(
		buffer,
		"{%3d,%3d,%3d,%3d}",
		(unsigned char)(255 * a_Color.red),
		(unsigned char)(255 * a_Color.green),
		(unsigned char)(255 * a_Color.blue),
		(unsigned char)(255 * a_Color.alpha)
	);

	return buffer;
}

void testColors()
{
	for (int testBits = 0; testBits < (1 << 4); testBits++)
	{
		GdkRGBA colorFore;
		{
			GtkWidgetPath* widgetPath = gtk_widget_path_new();
			gint pathPos = gtk_widget_path_append_type(widgetPath, gtk_tooltip_get_type());

			#if GTK_CHECK_VERSION(3,20,0)
			if (testBits & 1)
				gtk_widget_path_iter_set_object_name(widgetPath, pathPos, "tooltip");
			#else
			// Silence the warning
			(void)pathPos;
			#endif

			if (testBits & 2)
				gtk_widget_path_append_type(widgetPath, gtk_label_get_type());

			GtkStyleContext* styleContext = gtk_style_context_new();
			gtk_style_context_set_path(styleContext, widgetPath);
			gtk_widget_path_free(widgetPath);

			if (testBits & 4)
				gtk_style_context_add_class(styleContext, GTK_STYLE_CLASS_BACKGROUND);

			if (testBits & 8)
				gtk_style_context_add_class(styleContext, GTK_STYLE_CLASS_TOOLTIP);

			gtk_style_context_get_color(styleContext, GTK_STATE_FLAG_NORMAL, &colorFore);

			g_object_unref(styleContext);
		}

		GdkRGBA colorBack;
		{
			GtkWidgetPath* widgetPath = gtk_widget_path_new();
			gint pathPos = gtk_widget_path_append_type(widgetPath, gtk_tooltip_get_type());

			#if GTK_CHECK_VERSION(3,20,0)
			if (testBits & 1)
				gtk_widget_path_iter_set_object_name(widgetPath, pathPos, "tooltip");
			#else
			// Silence the warning
			(void)pathPos;
			#endif

			if (testBits & 2)
				gtk_widget_path_append_type(widgetPath, gtk_label_get_type());

			GtkStyleContext* styleContext = gtk_style_context_new();
			gtk_style_context_set_path(styleContext, widgetPath);
			gtk_widget_path_free(widgetPath);

			if (testBits & 4)
				gtk_style_context_add_class(styleContext, GTK_STYLE_CLASS_BACKGROUND);

			if (testBits & 8)
				gtk_style_context_add_class(styleContext, GTK_STYLE_CLASS_TOOLTIP);

			gtk_style_context_get_background_color(styleContext, GTK_STATE_FLAG_NORMAL, &colorBack);

			g_object_unref(styleContext);
		}

		printf
		(
			"%c%c%c%c fore=%s back=%s\n",
			(testBits & 1) ? '1' : '-',
			(testBits & 2) ? '2' : '-',
			(testBits & 4) ? '3' : '-',
			(testBits & 8) ? '4' : '-',
			format_GdkRGBA(colorFore).c_str(),
			format_GdkRGBA(colorBack).c_str()
		);
	}
}

void styleContext_getForeColor(GtkStyleContext* a_StyleContext, GdkRGBA* a_ForeColor)
{
	// gtk_style_context_save(a_StyleContext);
	// gtk_style_context_set_state(a_StyleContext, GTK_STATE_FLAG_NORMAL);
	gtk_style_context_get_color(a_StyleContext, GTK_STATE_FLAG_NORMAL, a_ForeColor);
	// gtk_style_context_restore(a_StyleContext);
}

bool styleContext_hasBackgroundImage(GtkStyleContext* a_StyleContext)
{
	GValue valueBackgroundImage = G_VALUE_INIT;
	gtk_style_context_get_property(a_StyleContext, GTK_STYLE_PROPERTY_BACKGROUND_IMAGE, GTK_STATE_FLAG_NORMAL, &valueBackgroundImage);

	const bool result = (NULL != g_value_get_boxed(&valueBackgroundImage));

	g_value_unset(&valueBackgroundImage);

	return result;
}

int inversePremultipliedColor(int color, int alpha) {
	if (alpha == 0) return 0;
	return (255*color + alpha-1) / alpha;
}

void renderAllBackgrounds(GtkStyleContext* a_StyleContext, cairo_t* a_Cairo)
{
	GtkStyleContext* parentStyleContext = gtk_style_context_get_parent(a_StyleContext);
	if (parentStyleContext)
		renderAllBackgrounds(parentStyleContext, a_Cairo);

	gtk_render_background(a_StyleContext, a_Cairo, -50, -50, 100, 100);
}

void styleContext_estimateBackColor(GtkStyleContext* a_StyleContext, GdkRGBA* a_BackColor)
{
	GtkStateFlags state = GTK_STATE_FLAG_NORMAL;

	unsigned char imageBytes[4];
	{
		gtk_style_context_save(a_StyleContext);
		gtk_style_context_set_state(a_StyleContext, state);
		cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
		cairo_t* cairo = cairo_create(surface);

		renderAllBackgrounds(a_StyleContext, cairo);
		cairo_surface_flush(surface);
		memcpy(imageBytes, cairo_image_surface_get_data(surface), sizeof(imageBytes));

		cairo_surface_destroy(surface);
		cairo_destroy(cairo);
		gtk_style_context_restore(a_StyleContext);
	}

	const unsigned char a = imageBytes[3];
	const unsigned char r = imageBytes[2];
	const unsigned char g = imageBytes[1];
	const unsigned char b = imageBytes[0];

	a_BackColor->alpha = a / 255.0;
	a_BackColor->red   = inversePremultipliedColor(r, a) / 255.0;
	a_BackColor->green = inversePremultipliedColor(g, a) / 255.0;
	a_BackColor->blue  = inversePremultipliedColor(b, a) / 255.0;
}

void styleContext_getBackColor(GtkStyleContext* a_StyleContext, GdkRGBA* a_BackColor)
{
	gtk_style_context_get_background_color(a_StyleContext, GTK_STATE_FLAG_NORMAL, a_BackColor);
}

std::string dumpStyleContext(GtkStyleContext* a_StyleContext)
{
	// char* contextDump = gtk_style_context_to_string(a_StyleContext, GTK_STYLE_CONTEXT_PRINT_RECURSE);

	const GtkWidgetPath* widgetPath = gtk_style_context_get_path(a_StyleContext);
	char* contextDump = gtk_widget_path_to_string(widgetPath);
	std::string result = contextDump;
	g_free(contextDump);

	return result;
}

void printTooltipColors(GtkStyleContext* a_StyleContextLabel, GtkStyleContext* a_StyleContextTooltip, const char* a_Caption)
{
	const std::string dumpFore = dumpStyleContext(a_StyleContextLabel);
	const std::string dumpBack = dumpStyleContext(a_StyleContextTooltip);

	GdkRGBA colorFore;
	styleContext_getForeColor(a_StyleContextLabel, &colorFore);

	const bool hasBackgroundImage = styleContext_hasBackgroundImage(a_StyleContextTooltip);

	GdkRGBA colorBackEstim;
	styleContext_estimateBackColor(a_StyleContextLabel, &colorBackEstim);

	GdkRGBA colorBackValue;
	styleContext_getBackColor(a_StyleContextTooltip, &colorBackValue);

	printf
	(
		"With %s:\n"
		"\tlabel   = %s\n"
		"\ttooltip = %s\n"
		"\t%s = Fore color (via gtk_style_context_get_color(label))\n"
		"\t%s = Back color (via gtk_render_background(label))\n"
		"\t%s = Back color (via gtk_style_context_get_background_color(tooltip))\n"
		"\tbackground-image=%c\n",
		a_Caption,
		dumpFore.c_str(),
		dumpBack.c_str(),
		format_GdkRGBA(colorFore).c_str(),
		format_GdkRGBA(colorBackEstim).c_str(),
		format_GdkRGBA(colorBackValue).c_str(),
		hasBackgroundImage ? 'Y' : 'N'
	);
}

void getTooltipColors_GtkWidgetPath()
{
#if GTK_CHECK_VERSION(3,20,0)
	if (0 != gtk_check_version(3, 20, 0))
	{
		// 'gtk_widget_path_iter_set_object_name' was introduced in 3.20.0
		printf("With GtkWidgetPath:    Requires 3.20.0\n");
		return;
	}

	// Foreground color is taken from 'tooltip label' css node
	GtkStyleContext* styleContextLabel = gtk_style_context_new();
	{
		GtkWidgetPath* widgetPath = gtk_widget_path_new();
		gtk_widget_path_append_type(widgetPath, 0);
		gtk_widget_path_iter_set_object_name(widgetPath, -1, "tooltip");
		gtk_widget_path_iter_add_class(widgetPath, -1, GTK_STYLE_CLASS_BACKGROUND);
		gtk_widget_path_append_type(widgetPath, GTK_TYPE_LABEL);
		gtk_style_context_set_path(styleContextLabel, widgetPath);
		gtk_widget_path_free(widgetPath);
	}

	// Background color is taken from 'tooltip.background' css node
	GtkStyleContext* styleContextTooltip = gtk_style_context_new();
	{
		GtkWidgetPath* widgetPath = gtk_widget_path_new();
		gtk_widget_path_append_type(widgetPath, 0);
		gtk_widget_path_iter_set_object_name(widgetPath, -1, "tooltip");
		gtk_widget_path_iter_add_class(widgetPath, -1, GTK_STYLE_CLASS_BACKGROUND);
		gtk_style_context_set_path(styleContextTooltip, widgetPath);
		gtk_widget_path_free(widgetPath);
	}

	// Print
	printTooltipColors(styleContextLabel, styleContextTooltip, "GtkWidgetPath");

	// Destroy temporary style contexts
	g_object_unref(styleContextLabel);
	g_object_unref(styleContextTooltip);
#endif // GTK_CHECK_VERSION(3,20,0)
}

void getTooltipColors_GtkTooltipWindow()
{
#if GTK_CHECK_VERSION(3,19,2)
	if (0 != gtk_check_version(3, 19, 2))
	{
		// 'GtkTooltipWindow' was introduced in 3.19.2
		printf("With GtkTooltipWindow: Requires 3.19.2\n");
		return;
	}

	// Force 'GtkTooltip' to register 'GtkTooltipWindow' class
	{
		GType typeTooltip = gtk_tooltip_get_type();
		GObject* tooltip = (GObject*)g_object_new(typeTooltip, NULL);
		g_object_unref(tooltip);
	}

	// Obtain GType of non-exported type 'GtkTooltipWindow'
	GType typeTooltipWindow = g_type_from_name("GtkTooltipWindow");
	if (0 == typeTooltipWindow)
	{
		printf("With GtkTooltipWindow: g_type_from_name() failed\n");
		return;
	}

	// Create temporary tooltip
	GtkWidget* tooltip = (GtkWidget*)g_object_new(typeTooltipWindow, NULL);

	// Create temporary label in tooltip
	GtkWidget* tooltipLabel = (GtkWidget*)g_object_new(GTK_TYPE_LABEL, NULL);
	gtk_widget_set_parent(tooltipLabel, tooltip);

	// Obtain style contexts
	GtkStyleContext* styleContextLabel   = gtk_widget_get_style_context(tooltipLabel);
	GtkStyleContext* styleContextTooltip = gtk_widget_get_style_context(tooltip);

	// Print
	printTooltipColors(styleContextLabel, styleContextTooltip, "GtkTooltipWindow");

	// Also destroys child label
	gtk_widget_destroy(tooltip);
#endif // GTK_CHECK_VERSION(3,19,2)
}

void getTooltipColors_TooltipSetCustom()
{
	// Create a temporary tooltip
	GType typeTooltip = gtk_tooltip_get_type();
	GtkTooltip* tooltip = (GtkTooltip*)g_object_new(typeTooltip, NULL);

	// Add temporary label as custom control into tooltip
	GtkWidget* customLabel = (GtkWidget*)g_object_new(GTK_TYPE_LABEL, NULL);
	gtk_tooltip_set_custom(tooltip, customLabel);

	// Find tooltip window, using custom widget as entry point
	GtkWidget* tooltipBox    = gtk_widget_get_parent(customLabel);
	GtkWidget* tooltipWindow = gtk_widget_get_parent(tooltipBox);

	// Obtain style contexts
	// For label, just use temporary label; this is easier then finding the original label
	GtkStyleContext* styleContextLabel   = gtk_widget_get_style_context(customLabel);
	GtkStyleContext* styleContextTooltip = gtk_widget_get_style_context(tooltipWindow);

	// Print
	printTooltipColors(styleContextLabel, styleContextTooltip, "TooltipSetCustom");

	// Cleanup
	// customLabel is owned by tooltip and will be destroyed automatically
	g_object_unref(tooltip);
}

bool isThemeAvailable()
{
	const char* themeName = g_getenv("GTK_THEME");

	if (!themeName)
		return true;

	if (0 == strcasecmp(themeName, "Adwaita"))
		return true;

	// When GTK doesn't find a theme, it simply loads Adwaita.
	GtkCssProvider* selectedTheme = gtk_css_provider_get_named(themeName, NULL);
	GtkCssProvider* adwaitaTheme = gtk_css_provider_get_named("Adwaita", NULL);

	char* selectedThemeString = gtk_css_provider_to_string(selectedTheme);
	char* adwaitaThemeString = gtk_css_provider_to_string(adwaitaTheme);
	const bool isAdwaita =(0 == strcmp(selectedThemeString, adwaitaThemeString));
	g_free(selectedThemeString);
	g_free(adwaitaThemeString);

	if (isAdwaita)
	{
		printf("Error: Theme %s not found\n", themeName);
		return false;
	}

	return true;
}

int main(int argc, char* argv[])
{
	gtk_init(&argc, &argv);

	if (!isThemeAvailable())
		return 0;

	printf
	(
		"GTK version: %d.%d.%d\n",
		gtk_get_major_version(),
		gtk_get_minor_version(),
		gtk_get_micro_version()
	);

	// testColors();
	getTooltipColors_GtkWidgetPath();
	getTooltipColors_GtkTooltipWindow();
	getTooltipColors_TooltipSetCustom();

	return 0;
}

Back to the top