style.c 10.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*
 * Style.c: Style resource management
 *
 * Author:
 *  Miguel de Icaza (miguel@gnu.org)
 *  (C) 1998 Miguel de Icaza
 */
#include <config.h>
#include <gnome.h>
10
11
#include "gnumeric.h"
#include "format.h"
12
#include "color.h"
13

14
15
16
#define DEFAULT_FONT "-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*"
#define DEFAULT_SIZE 14

17
18
19
static GHashTable *style_format_hash;
static GHashTable *style_font_hash;
static GHashTable *style_border_hash;
Arturo Espinosa's avatar
Arturo Espinosa committed
20
static GHashTable *style_color_hash;
21
22
23
24
25
26
27
28
29
30
31
32
33

StyleFormat *
style_format_new (char *name)
{
	StyleFormat *format;

	g_return_val_if_fail (name != NULL, NULL);
	
	format = (StyleFormat *) g_hash_table_lookup (style_format_hash, name);
	
	if (!format){
		format = g_new0 (StyleFormat, 1);
		format->format = g_strdup (name);
34
		format_compile (format);
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
		g_hash_table_insert (style_format_hash, name, format);
	}
	format->ref_count++;

	return format;
}

void
style_format_ref (StyleFormat *sf)
{
	g_return_if_fail (sf != NULL);

	sf->ref_count++;
}

void
style_format_unref (StyleFormat *sf)
{
	g_return_if_fail (sf->ref_count > 0);

	sf->ref_count--;
	if (sf->ref_count != 0)
		return;
58

59
	g_hash_table_remove (style_format_hash, sf->format);
60
61

	format_destroy (sf);
62
63
64
65
66
	g_free (sf->format);
	g_free (sf);
}

StyleFont *
67
style_font_new_simple (char *font_name, int units)
68
69
70
71
72
73
{
	StyleFont *font;
	StyleFont key;

	g_return_val_if_fail (font_name != NULL, NULL);
	g_return_val_if_fail (units != 0, NULL);
74

75
76
77
78
79
	key.font_name = font_name;
	key.units    = units;
	
	font = (StyleFont *) g_hash_table_lookup (style_font_hash, &key);
	if (!font){
80
81
82
83
		GdkFont *gdk_font;

		gdk_font = gdk_font_load (font_name);
		
84
85
86
87
88
89
90
91
		if (!gdk_font)
			return NULL;
		
		font = g_new0 (StyleFont, 1);
		font->font_name = g_strdup (font_name);
		font->units    = units;
		font->font     = gdk_font_load (font_name);
		g_hash_table_insert (style_font_hash, font, font);
92
93
94
95
96
97
	}
	font->ref_count++;

	return font;
}

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
StyleFont *
style_font_new (char *font_name, int units)
{
	StyleFont *font;
	StyleFont key;

	g_return_val_if_fail (font_name != NULL, NULL);
	g_return_val_if_fail (units != 0, NULL);

	font = style_font_new_simple (font_name, units);
	if (!font)
		font = style_font_new_simple (DEFAULT_FONT, DEFAULT_SIZE);

	if (!font)
		font = style_font_new_simple ("fixed", DEFAULT_SIZE);

	if (!font)
		g_error ("Can not load fixed font\n");

	return font;
}

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
void
style_font_ref (StyleFont *sf)
{
	g_return_if_fail (sf != NULL);

	sf->ref_count++;
}

void
style_font_unref (StyleFont *sf)
{
	g_return_if_fail (sf != NULL);
	g_return_if_fail (sf->ref_count > 0);

	sf->ref_count--;
	if (sf->ref_count != 0)
		return;

	g_hash_table_remove (style_font_hash, sf);
	g_free (sf->font_name);
	g_free (sf);
}

StyleBorder *
144
145
146
147
style_border_new (StyleBorderType left,    StyleBorderType right,
		  StyleBorderType top,     StyleBorderType bottom,
		  StyleColor *left_color,  StyleColor *right_color,
		  StyleColor *top_color,   StyleColor *bottom_color)
148
149
150
151
152
153
154
155
{
	StyleBorder key, *border;

	key.left    	 = left;
	key.right   	 = right;
	key.top     	 = top;
	key.bottom  	 = bottom;
	if (left_color)
156
		key.left_color   = left_color;
157
	if (right_color)
158
		key.right_color  = right_color;
159
	if (top_color)
160
		key.top_color    = top_color;
161
	if (bottom_color)
162
		key.bottom_color = bottom_color;
163
164
165
166
167
168

	border = (StyleBorder *) g_hash_table_lookup (style_border_hash, &key);
	if (!border){
		border = g_new0 (StyleBorder, 1);
		*border = key;
		g_hash_table_insert (style_border_hash, border, border);
169
		border->ref_count = 0;
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
	}
	border->ref_count++;

	return border;
}

void
style_border_ref (StyleBorder *sb)
{
	g_return_if_fail (sb != NULL);

	sb->ref_count++;
}

void
style_border_unref (StyleBorder *sb)
{
	g_return_if_fail (sb != NULL);
	g_return_if_fail (sb->ref_count > 0);

	sb->ref_count--;
	if (sb->ref_count != 0)
		return;

	g_hash_table_remove (style_border_hash, sb);
	g_free (sb);
}

StyleBorder *
style_border_new_plain (void)
{
	return style_border_new (BORDER_NONE, BORDER_NONE,
				 BORDER_NONE, BORDER_NONE,
				 NULL, NULL, NULL, NULL);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
206
StyleColor *
207
style_color_new (gushort red, gushort green, gushort blue)
Arturo Espinosa's avatar
Arturo Espinosa committed
208
{
209
210
211
212
213
214
215
216
217
218
219
	StyleColor *sc;
	GdkColor key;
	
	key.red   = red;
	key.green = green;
	key.blue  = blue;

	sc = g_hash_table_lookup (style_color_hash, &key);
	if (!sc){
		sc = g_new (StyleColor, 1);
		sc->color = key;
220
221
		sc->color.pixel = color_alloc (red, green, blue);

222
223
224
225
226
227
		g_hash_table_insert (style_color_hash, sc, sc);
		sc->ref_count = 0;
	}
	sc->ref_count++;
	
	return sc;
Arturo Espinosa's avatar
Arturo Espinosa committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
}

void
style_color_ref (StyleColor *sc)
{
	g_return_if_fail (sc != NULL);

	sc->ref_count++;
}

void
style_color_unref (StyleColor *sc)
{
	g_return_if_fail (sc != NULL);
	g_return_if_fail (sc->ref_count > 0);

	sc->ref_count--;
	if (sc->ref_count != 0)
		return;

248
249
250
251
	/*
	 * There is no need to deallocate colors, as they come from
	 * the GDK Color Context
	 */
Arturo Espinosa's avatar
Arturo Espinosa committed
252
253
254
255
	g_hash_table_remove (style_color_hash, sc);
	g_free (sc);
}

256
257
258
259
260
261
262
Style *
style_new (void)
{
	Style *style;

	style = g_new0 (Style, 1);

Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
263
264
	style->valid_flags = STYLE_ALL;
	
265
	style->format      = style_format_new ("General");
266
	style->font        = style_font_new (DEFAULT_FONT, DEFAULT_SIZE);
267
	style->border      = style_border_new_plain ();
268
	style->fore_color  = style_color_new (0, 0, 0);
269
	style->back_color  = style_color_new (0xffff, 0xffff, 0xffff);
270
271
	style->halign      = HALIGN_GENERAL;
	style->valign      = VALIGN_CENTER;
272
	style->orientation = ORIENT_HORIZ;
273

274
275
276
	return style;
}

Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
277
278
279
280
281
282
283
284
285
286
287
288
Style *
style_new_empty (void)
{
	Style *style;

	style = g_new0 (Style, 1);

	style->valid_flags = 0;
	
	return style;
}

289
290
291
292
void
style_destroy (Style *style)
{
	g_return_if_fail (style != NULL);
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
293
294
295
296
297

	if (style->valid_flags & STYLE_FORMAT)
		style_format_unref (style->format);

	if (style->valid_flags & STYLE_FONT)
298
		style_font_unref (style->font);
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
299
300
301

	if (style->valid_flags & STYLE_BORDER)
		style_border_unref (style->border);
Arturo Espinosa's avatar
Arturo Espinosa committed
302

303
304
305
306
307
308
309
	if (style->valid_flags & STYLE_FORE_COLOR)
		if (style->fore_color)
			style_color_unref (style->fore_color);

	if (style->valid_flags & STYLE_BACK_COLOR)
		if (style->back_color)
			style_color_unref (style->back_color);
310

311
312
313
314
	g_free (style);
}


315
316
317
318
319
320
321
Style *
style_duplicate (Style *original)
{
	Style *style;

	style = g_new0 (Style, 1);

322
	/* Bit copy */
323
	*style = *original;
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339

	/* Do the rest of the copy */
	if (style->valid_flags & STYLE_FORMAT)
		style_format_ref (style->format);
	else
		style->format = NULL;

	if (style->valid_flags & STYLE_FONT)
		style_font_ref (style->font);
	else
		style->font = NULL;

	if (style->valid_flags & STYLE_BORDER)
		style_border_ref (style->border);
	else
		style->border = NULL;
340

341
	if (style->valid_flags & STYLE_FORE_COLOR)
342
		style_color_ref (style->fore_color);
343
344
345
346
	else
		style->fore_color = NULL;

	if (style->valid_flags & STYLE_BACK_COLOR)
347
		style_color_ref (style->back_color);
Arturo Espinosa's avatar
Arturo Espinosa committed
348
	else
349
		style->back_color = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
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
	return style;
}

/*
 * The routines used to hash and compare the different styles
 */
static gint
font_equal (gconstpointer v, gconstpointer v2)
{
	StyleFont *k1 = (StyleFont *) v;
	StyleFont *k2 = (StyleFont *) v2;

	if (k1->units != k2->units)
		return 0;
	
	return !strcmp (k1->font_name, k2->font_name);
}

static guint
font_hash (gconstpointer v)
{
	StyleFont *k = (StyleFont *) v;

	return k->units + g_str_hash (k->font_name);
}

static gint
border_equal (gconstpointer v, gconstpointer v2)
{
	StyleBorder *k1 = (StyleBorder *) v;
	StyleBorder *k2 = (StyleBorder *) v2;

	if (k1->left != k2->left)
		return 0;
	if (k1->right != k2->right)
		return 0;
	if (k1->top != k2->top)
		return 0;
	if (k1->bottom != k2->bottom)
		return 0;
	if (k1->left != BORDER_NONE)
392
		if (k1->left_color != k2->left_color)
393
394
			return 0;
	if (k1->right != BORDER_NONE)
395
		if (k1->right_color != k2->right_color)
396
397
			return 0;
	if (k1->top != BORDER_NONE)
398
		if (k1->top_color != k2->top_color)
399
400
			return 0;
	if (k1->bottom != BORDER_NONE)
401
		if (k1->bottom_color != k2->bottom_color)
402
403
404
405
406
407
408
409
410
411
412
413
			return 0;
	return 1;
}

static guint
border_hash (gconstpointer v)
{
	StyleBorder *k = (StyleBorder *) v;

	return (k->left << 12) | (k->right << 8) | (k->top << 4) | (k->bottom);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
414
415
416
417
418
419
static gint
color_equal (gconstpointer v, gconstpointer v2)
{
	StyleColor *k1 = (StyleColor *) v;
	StyleColor *k2 = (StyleColor *) v2;

420
421
422
	if (k1->color.red   == k2->color.red &&
	    k1->color.green == k2->color.green &&
	    k1->color.blue  == k2->color.blue)
Arturo Espinosa's avatar
Arturo Espinosa committed
423
424
425
426
427
428
429
430
431
432
		return 1;
	
	return 0;
}

static guint
color_hash (gconstpointer v)
{
	StyleColor *k = (StyleColor *)v;

433
	return (k->color.red << 16) | (k->color.green << 8) | (k->color.blue);
Arturo Espinosa's avatar
Arturo Espinosa committed
434
435
}

436
437
438
439
440
441
void
style_init (void)
{
	style_format_hash = g_hash_table_new (g_str_hash, g_str_equal);
	style_font_hash   = g_hash_table_new (font_hash, font_equal);
	style_border_hash = g_hash_table_new (border_hash, border_equal);
Arturo Espinosa's avatar
Arturo Espinosa committed
442
	style_color_hash  = g_hash_table_new (color_hash, color_equal);
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
void
style_merge_to (Style *target, Style *source)
{
	if (!(target->valid_flags & STYLE_FORMAT))
		if (source->valid_flags & STYLE_FORMAT){
			target->valid_flags |= STYLE_FORMAT;
			target->format = source->format;
			style_format_ref (target->format);
		}

	if (!(target->valid_flags & STYLE_FONT))
		if (source->valid_flags & STYLE_FONT){
			target->valid_flags |= STYLE_FONT;
			target->font = source->font;
			style_font_ref (target->font);
		}

	if (!(target->valid_flags & STYLE_BORDER))
		if (source->valid_flags & STYLE_BORDER){
			target->valid_flags |= STYLE_BORDER;
			target->border = source->border;
			style_border_ref (target->border);
		}

	if (!(target->valid_flags & STYLE_ALIGN))
		if (source->valid_flags & STYLE_ALIGN){
			target->valid_flags |= STYLE_ALIGN;
			target->halign      = source->halign;
			target->valign      = source->valign;
			target->orientation = source->orientation;
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
476

477
478
479
480
481
482
483
484
485
486
487
488
489
490
	if (!(target->valid_flags & STYLE_FORE_COLOR))
		if (source->valid_flags & STYLE_FORE_COLOR){
			target->valid_flags |= STYLE_FORE_COLOR;
			target->fore_color = source->fore_color;
			if (target->fore_color)
				style_color_ref (target->fore_color);
		}

	if (!(target->valid_flags & STYLE_BACK_COLOR))
		if (source->valid_flags & STYLE_BACK_COLOR){
			target->valid_flags |= STYLE_BACK_COLOR;
			target->back_color = source->back_color;
			if (target->back_color)
				style_color_ref (target->back_color);
Arturo Espinosa's avatar
Arturo Espinosa committed
491
		}
492
493
494
495
496
497

	if (!(target->valid_flags & STYLE_PATTERN))
		if (source->valid_flags & STYLE_PATTERN){
			target->valid_flags |= STYLE_PATTERN;
			target->pattern = source->pattern;
		}
498
}