summaryrefslogtreecommitdiff
path: root/libs/clearlooks-newer/clearlooks_rc_style.c
blob: fdce13af1c0e14eb87e9e561471b23ef11f0133c (plain)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* Clearlooks theme engine
 * Copyright (C) 2005 Richard Stellingwerff.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Written by Owen Taylor <otaylor@redhat.com>
 * and by Alexander Larsson <alexl@redhat.com>
 * Modified by Richard Stellingwerff <remenic@gmail.com>
 * Modified by Kulyk Nazar <schamane@myeburg.net>
 */

#include "clearlooks_style.h"
#include "clearlooks_rc_style.h"

#include "animation.h"

static void      clearlooks_rc_style_init         (ClearlooksRcStyle      *style);
#ifdef HAVE_ANIMATION
static void      clearlooks_rc_style_finalize     (GObject                *object);
#endif
static void      clearlooks_rc_style_class_init   (ClearlooksRcStyleClass *klass);
static GtkStyle *clearlooks_rc_style_create_style (GtkRcStyle             *rc_style);
static guint     clearlooks_rc_style_parse        (GtkRcStyle             *rc_style,
						   GtkSettings            *settings,
						   GScanner               *scanner);
static void      clearlooks_rc_style_merge        (GtkRcStyle             *dest,
						   GtkRcStyle             *src);


static GtkRcStyleClass *clearlooks_parent_rc_class;

GType clearlooks_type_rc_style = 0;

enum
{
	TOKEN_SCROLLBARCOLOR = G_TOKEN_LAST + 1,
	TOKEN_COLORIZESCROLLBAR,
	TOKEN_CONTRAST,
	TOKEN_SUNKENMENU,
	TOKEN_PROGRESSBARSTYLE,
	TOKEN_MENUBARSTYLE,
	TOKEN_TOOLBARSTYLE,
	TOKEN_MENUITEMSTYLE,
	TOKEN_LISTVIEWITEMSTYLE,
	TOKEN_ANIMATION,
	TOKEN_STYLE,
	TOKEN_RADIUS,

	TOKEN_CLASSIC,
	TOKEN_GLOSSY,
	TOKEN_INVERTED,
	TOKEN_GUMMY,

	TOKEN_TRUE,
	TOKEN_FALSE
};

static struct
{
	const gchar        *name;
	guint               token;
}
clearlooks_gtk2_rc_symbols[] =
{
	{ "scrollbar_color",    TOKEN_SCROLLBARCOLOR  },
	{ "colorize_scrollbar", TOKEN_COLORIZESCROLLBAR },
	{ "contrast",           TOKEN_CONTRAST  },
	{ "sunkenmenubar",      TOKEN_SUNKENMENU },
	{ "progressbarstyle",   TOKEN_PROGRESSBARSTYLE },
	{ "menubarstyle",       TOKEN_MENUBARSTYLE },
	{ "toolbarstyle",       TOKEN_TOOLBARSTYLE },
	{ "menuitemstyle",      TOKEN_MENUITEMSTYLE },
	{ "listviewitemstyle",  TOKEN_LISTVIEWITEMSTYLE },
	{ "animation",          TOKEN_ANIMATION },
	{ "style",              TOKEN_STYLE },
	{ "radius",             TOKEN_RADIUS },

	{ "CLASSIC",            TOKEN_CLASSIC },
	{ "GLOSSY",             TOKEN_GLOSSY },
	{ "INVERTED",           TOKEN_INVERTED },
	{ "GUMMY",              TOKEN_GUMMY },

	{ "TRUE",               TOKEN_TRUE },
	{ "FALSE",              TOKEN_FALSE }
};


void
clearlooks_rc_style_register_type (GTypeModule *module)
{
	static const GTypeInfo object_info =
	{
		sizeof (ClearlooksRcStyleClass),
		(GBaseInitFunc) NULL,
		(GBaseFinalizeFunc) NULL,
		(GClassInitFunc) clearlooks_rc_style_class_init,
		NULL,           /* class_finalize */
		NULL,           /* class_data */
		sizeof (ClearlooksRcStyle),
		0,              /* n_preallocs */
		(GInstanceInitFunc) clearlooks_rc_style_init,
		NULL
	};

	clearlooks_type_rc_style = g_type_module_register_type (module,
						     GTK_TYPE_RC_STYLE,
						     "ClearlooksRcStyle",
						     &object_info, 0);
}

static void
clearlooks_rc_style_init (ClearlooksRcStyle *clearlooks_rc)
{
	clearlooks_rc->style = CL_STYLE_CLASSIC;

	clearlooks_rc->flags = 0;

	clearlooks_rc->contrast = 1.0;
	clearlooks_rc->menubarstyle = 0;
	clearlooks_rc->toolbarstyle = 0;
	clearlooks_rc->animation = FALSE;
	clearlooks_rc->colorize_scrollbar = FALSE;
	clearlooks_rc->radius = 3.0;
}

#ifdef HAVE_ANIMATION
static void
clearlooks_rc_style_finalize (GObject *object)
{
	/* cleanup all the animation stuff */
	clearlooks_animation_cleanup ();

	if (G_OBJECT_CLASS (clearlooks_parent_rc_class)->finalize != NULL)
		G_OBJECT_CLASS (clearlooks_parent_rc_class)->finalize(object);
}
#endif


static void
clearlooks_rc_style_class_init (ClearlooksRcStyleClass *klass)
{
	GtkRcStyleClass *rc_style_class = GTK_RC_STYLE_CLASS (klass);
#ifdef HAVE_ANIMATION
	GObjectClass    *g_object_class = G_OBJECT_CLASS (klass);
#endif

	clearlooks_parent_rc_class = g_type_class_peek_parent (klass);

	rc_style_class->parse = clearlooks_rc_style_parse;
	rc_style_class->create_style = clearlooks_rc_style_create_style;
	rc_style_class->merge = clearlooks_rc_style_merge;

#ifdef HAVE_ANIMATION
	g_object_class->finalize = clearlooks_rc_style_finalize;
#endif
}

static guint
clearlooks_gtk2_rc_parse_boolean (GtkSettings *settings,
                     GScanner     *scanner,
                     gboolean *retval)
{
	guint token;

	(void) settings;
	token = g_scanner_get_next_token(scanner);

	token = g_scanner_get_next_token(scanner);
	if (token != G_TOKEN_EQUAL_SIGN)
	   return G_TOKEN_EQUAL_SIGN;

	token = g_scanner_get_next_token(scanner);
	if (token == TOKEN_TRUE)
	   *retval = TRUE;
	else if (token == TOKEN_FALSE)
	   *retval = FALSE;
	else
	   return TOKEN_TRUE;

	return G_TOKEN_NONE;
}

static guint
clearlooks_gtk2_rc_parse_color(GtkSettings  *settings,
		  GScanner     *scanner,
		  GdkColor     *color)
{
	guint token;

	(void) settings;

	/* Skip 'blah_color' */
	token = g_scanner_get_next_token(scanner);

	token = g_scanner_get_next_token(scanner);
	if (token != G_TOKEN_EQUAL_SIGN)
	   return G_TOKEN_EQUAL_SIGN;

	return gtk_rc_parse_color (scanner, color);
}

static guint
clearlooks_gtk2_rc_parse_double (GtkSettings  *settings,
                                 GScanner     *scanner,
                                 gdouble      *val)
{
	guint token;

	(void) settings;

	/* Skip 'blah' */
	token = g_scanner_get_next_token(scanner);

	token = g_scanner_get_next_token(scanner);
	if (token != G_TOKEN_EQUAL_SIGN)
	   return G_TOKEN_EQUAL_SIGN;

	token = g_scanner_get_next_token(scanner);
	if (token != G_TOKEN_FLOAT)
	   return G_TOKEN_FLOAT;

	*val = scanner->value.v_float;

	return G_TOKEN_NONE;
}

static guint
clearlooks_gtk2_rc_parse_int (GtkSettings  *settings,
		         GScanner     *scanner,
		         guint8       *progressbarstyle)
{
	guint token;

	(void) settings;

	/* Skip 'sunkenmenubar' */
	token = g_scanner_get_next_token(scanner);

	token = g_scanner_get_next_token(scanner);
	if (token != G_TOKEN_EQUAL_SIGN)
	   return G_TOKEN_EQUAL_SIGN;

	token = g_scanner_get_next_token(scanner);
	if (token != G_TOKEN_INT)
	   return G_TOKEN_INT;

	*progressbarstyle = scanner->value.v_int;

	return G_TOKEN_NONE;
}

static guint
clearlooks_gtk2_rc_parse_style (GtkSettings      *settings,
                                GScanner         *scanner,
                                ClearlooksStyles *style)
{
	guint token;

	(void) settings;

	g_assert (CL_NUM_STYLES == CL_STYLE_GUMMY + 1); /* so that people don't forget ;-) */

	/* Skip 'style' */
	token = g_scanner_get_next_token (scanner);

	token = g_scanner_get_next_token (scanner);
	if (token != G_TOKEN_EQUAL_SIGN)
	   return G_TOKEN_EQUAL_SIGN;

	token = g_scanner_get_next_token (scanner);

	switch (token)
	{
		case TOKEN_CLASSIC:
		   *style = CL_STYLE_CLASSIC;
		   break;
		case TOKEN_GLOSSY:
		   *style = CL_STYLE_GLOSSY;
		   break;
		case TOKEN_INVERTED:
		   *style = CL_STYLE_INVERTED;
		   break;
		case TOKEN_GUMMY:
		   *style = CL_STYLE_GUMMY;
		   break;
		default:
		   return TOKEN_CLASSIC;
	}

	return G_TOKEN_NONE;
}

static guint
clearlooks_gtk2_rc_parse_dummy (GtkSettings      *settings,
                                GScanner         *scanner,
                                const gchar      *name)
{
	guint token;

	(void) settings;

	/* Skip option */
	token = g_scanner_get_next_token (scanner);

	/* print a warning. Isn't there a way to get the string from the scanner? */
	g_scanner_warn (scanner, "Clearlooks configuration option \"%s\" is not supported and will be ignored.", name);

	/* equal sign */
	token = g_scanner_get_next_token (scanner);
	if (token != G_TOKEN_EQUAL_SIGN)
	   return G_TOKEN_EQUAL_SIGN;

	/* eat whatever comes next */
	token = g_scanner_get_next_token (scanner);

	return G_TOKEN_NONE;
}

static guint
clearlooks_rc_style_parse (GtkRcStyle *rc_style,
			   GtkSettings  *settings,
			   GScanner   *scanner)

{
	static GQuark scope_id = 0;
	ClearlooksRcStyle *clearlooks_style = CLEARLOOKS_RC_STYLE (rc_style);

	guint old_scope;
	guint token;
	guint i;

	/* Set up a new scope in this scanner. */

	if (!scope_id)
	   scope_id = g_quark_from_string("clearlooks_theme_engine");

	/* If we bail out due to errors, we *don't* reset the scope, so the
	* error messaging code can make sense of our tokens.
	*/
	old_scope = g_scanner_set_scope(scanner, scope_id);

	/* Now check if we already added our symbols to this scope
	* (in some previous call to clearlooks_rc_style_parse for the
	* same scanner.
	*/

	if (!g_scanner_lookup_symbol(scanner, clearlooks_gtk2_rc_symbols[0].name))
	{
		for (i = 0; i < G_N_ELEMENTS (clearlooks_gtk2_rc_symbols); i++)
			g_scanner_scope_add_symbol(scanner, scope_id,
				   	clearlooks_gtk2_rc_symbols[i].name,
				   	GINT_TO_POINTER(clearlooks_gtk2_rc_symbols[i].token));
	}

	/* We're ready to go, now parse the top level */

	token = g_scanner_peek_next_token(scanner);
	while (token != G_TOKEN_RIGHT_CURLY)
	{
		switch (token)
		{
			case TOKEN_SCROLLBARCOLOR:
				token = clearlooks_gtk2_rc_parse_color (settings, scanner, &clearlooks_style->scrollbar_color);
				clearlooks_style->flags |= CL_FLAG_SCROLLBAR_COLOR;
				break;
			case TOKEN_COLORIZESCROLLBAR:
				token = clearlooks_gtk2_rc_parse_boolean (settings, scanner, &clearlooks_style->colorize_scrollbar);
				clearlooks_style->flags |= CL_FLAG_COLORIZE_SCROLLBAR;
				break;
			case TOKEN_CONTRAST:
				token = clearlooks_gtk2_rc_parse_double (settings, scanner, &clearlooks_style->contrast);
				clearlooks_style->flags |= CL_FLAG_CONTRAST;
				break;
			case TOKEN_MENUBARSTYLE:
				token = clearlooks_gtk2_rc_parse_int (settings, scanner, &clearlooks_style->menubarstyle);
				clearlooks_style->flags |= CL_FLAG_MENUBARSTYLE;
				break;
			case TOKEN_TOOLBARSTYLE:
				token = clearlooks_gtk2_rc_parse_int (settings, scanner, &clearlooks_style->toolbarstyle);
				clearlooks_style->flags |= CL_FLAG_TOOLBARSTYLE;
				break;
			case TOKEN_ANIMATION:
				token = clearlooks_gtk2_rc_parse_boolean (settings, scanner, &clearlooks_style->animation);
				clearlooks_style->flags |= CL_FLAG_ANIMATION;
				break;
			case TOKEN_STYLE:
				token = clearlooks_gtk2_rc_parse_style (settings, scanner, &clearlooks_style->style);
				clearlooks_style->flags |= CL_FLAG_STYLE;
				break;
			case TOKEN_RADIUS:
				token = clearlooks_gtk2_rc_parse_double (settings, scanner, &clearlooks_style->radius);
				clearlooks_style->flags |= CL_FLAG_RADIUS;
				break;

			/* stuff to ignore */
			case TOKEN_SUNKENMENU:
				token = clearlooks_gtk2_rc_parse_dummy (settings, scanner, "sunkenmenu");
				break;
			case TOKEN_PROGRESSBARSTYLE:
				token = clearlooks_gtk2_rc_parse_dummy (settings, scanner, "progressbarstyle");
				break;
			case TOKEN_MENUITEMSTYLE:
				token = clearlooks_gtk2_rc_parse_dummy (settings, scanner, "menuitemstyle");
				break;
			case TOKEN_LISTVIEWITEMSTYLE:
				token = clearlooks_gtk2_rc_parse_dummy (settings, scanner, "listviewitemstyle");
				break;

			default:
				g_scanner_get_next_token(scanner);
				token = G_TOKEN_RIGHT_CURLY;
				break;
		}

		if (token != G_TOKEN_NONE)
			return token;

		token = g_scanner_peek_next_token(scanner);
	}

	g_scanner_get_next_token(scanner);

	g_scanner_set_scope(scanner, old_scope);

	return G_TOKEN_NONE;
}

static void
clearlooks_rc_style_merge (GtkRcStyle *dest,
			   GtkRcStyle *src)
{
	ClearlooksRcStyle *dest_w, *src_w;
	ClearlooksRcFlags flags;

	clearlooks_parent_rc_class->merge (dest, src);

	if (!CLEARLOOKS_IS_RC_STYLE (src))
		return;

	src_w = CLEARLOOKS_RC_STYLE (src);
	dest_w = CLEARLOOKS_RC_STYLE (dest);

	flags = (~dest_w->flags) & src_w->flags;

	if (flags & CL_FLAG_STYLE)
		dest_w->style = src_w->style;
	if (flags & CL_FLAG_CONTRAST)
		dest_w->contrast = src_w->contrast;
	if (flags & CL_FLAG_MENUBARSTYLE)
		dest_w->menubarstyle = src_w->menubarstyle;
	if (flags & CL_FLAG_TOOLBARSTYLE)
		dest_w->toolbarstyle = src_w->toolbarstyle;
	if (flags & CL_FLAG_SCROLLBAR_COLOR)
		dest_w->scrollbar_color = src_w->scrollbar_color;
	if (flags & CL_FLAG_COLORIZE_SCROLLBAR)
		dest_w->colorize_scrollbar = src_w->colorize_scrollbar;
	if (flags & CL_FLAG_ANIMATION)
		dest_w->animation = src_w->animation;
	if (flags & CL_FLAG_RADIUS)
		dest_w->radius = src_w->radius;

	dest_w->flags |= src_w->flags;
}


/* Create an empty style suitable to this RC style
 */
static GtkStyle *
clearlooks_rc_style_create_style (GtkRcStyle *rc_style)
{
	(void) rc_style;

	return GTK_STYLE (g_object_new (CLEARLOOKS_TYPE_STYLE, NULL));
}