1
/* $Id: balou-theme.c 4618 2004-07-14 20:17:58Z benny $ */
3
* Copyright (c) 2004 Benedikt Meurer <benny@xfce.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2, or (at your option)
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26
#ifdef HAVE_SYS_STAT_H
44
#include <libxfce4util/libxfce4util.h>
46
#include <engines/balou/balou-theme.h>
49
static void load_color_pair (const XfceRc *rc,
51
GdkColor *color1_return,
52
GdkColor *color2_return,
53
const gchar *color_default);
54
static GdkPixbuf *load_pixbuf (const gchar *path,
56
gint available_height);
57
static time_t mtime (const gchar *path);
58
static GdkPixbuf *load_cached_preview (const BalouTheme *theme);
59
static void store_cached_preview (const BalouTheme *theme,
76
#define DEFAULT_BGCOLOR "White"
77
#define DEFAULT_FGCOLOR "Black"
78
#define DEFAULT_FONT "Sans Bold 12"
82
balou_theme_load (const gchar *name)
85
const gchar *image_file;
91
theme = g_new0 (BalouTheme, 1);
93
resource = g_strdup_printf ("%s/balou/themerc", name);
94
file = xfce_resource_lookup (XFCE_RESOURCE_THEMES, resource);
99
rc = xfce_rc_simple_open (file, TRUE);
106
theme->theme_file = g_strdup (file);
108
xfce_rc_set_group (rc, "Info");
109
theme->name = g_strdup (xfce_rc_read_entry (rc, "Name", name));
110
theme->description = g_strdup (xfce_rc_read_entry (rc, "Description", _("No description given")));
112
xfce_rc_set_group (rc, "Splash Screen");
113
load_color_pair (rc, "bgcolor", &theme->bgcolor1, &theme->bgcolor2,
116
spec = xfce_rc_read_entry (rc, "fgcolor", DEFAULT_FGCOLOR);
117
if (!gdk_color_parse (spec, &theme->fgcolor))
118
gdk_color_parse (DEFAULT_FGCOLOR, &theme->fgcolor);
120
spec = xfce_rc_read_entry (rc, "font", DEFAULT_FONT);
121
theme->font = g_strdup (spec);
123
image_file = xfce_rc_read_entry (rc, "logo", NULL);
124
if (image_file != NULL)
126
resource = g_path_get_dirname (file);
127
theme->logo_file = g_build_filename (resource, image_file, NULL);
132
theme->logo_file = NULL;
142
gdk_color_parse (DEFAULT_BGCOLOR, &theme->bgcolor1);
143
gdk_color_parse (DEFAULT_BGCOLOR, &theme->bgcolor2);
144
gdk_color_parse (DEFAULT_FGCOLOR, &theme->fgcolor);
145
theme->font = g_strdup (DEFAULT_FONT);
146
theme->logo_file = NULL;
153
balou_theme_get_name (const BalouTheme *theme)
160
balou_theme_get_description (const BalouTheme *theme)
162
return theme->description;
167
balou_theme_get_font (const BalouTheme *theme)
174
balou_theme_get_bgcolor (const BalouTheme *theme,
175
GdkColor *color_return)
177
*color_return = theme->bgcolor1;
182
balou_theme_get_fgcolor (const BalouTheme *theme,
183
GdkColor *color_return)
185
*color_return = theme->fgcolor;
190
balou_theme_get_logo (const BalouTheme *theme,
191
gint available_width,
192
gint available_height)
194
return load_pixbuf (theme->logo_file,
201
balou_theme_draw_gradient (const BalouTheme *theme,
202
GdkDrawable *drawable,
204
GdkRectangle logobox,
205
GdkRectangle textbox)
213
if (gdk_color_equal (&theme->bgcolor1, &theme->bgcolor2))
215
gdk_gc_set_rgb_fg_color (gc, &theme->bgcolor1);
216
gdk_draw_rectangle (drawable, gc, TRUE, logobox.x, logobox.y,
217
logobox.width, logobox.height);
218
gdk_draw_rectangle (drawable, gc, TRUE, textbox.x, textbox.y,
219
textbox.width, textbox.height);
223
/* calculate differences */
224
dred = theme->bgcolor1.red - theme->bgcolor2.red;
225
dgreen = theme->bgcolor1.green - theme->bgcolor2.green;
226
dblue = theme->bgcolor1.blue - theme->bgcolor2.blue;
228
for (i = 0; i < logobox.height; ++i)
230
color.red = theme->bgcolor2.red + (i * dred / logobox.height);
231
color.green = theme->bgcolor2.green + (i * dgreen / logobox.height);
232
color.blue = theme->bgcolor2.blue + (i * dblue / logobox.height);
234
gdk_gc_set_rgb_fg_color (gc, &color);
235
gdk_draw_line (drawable, gc, logobox.x, logobox.y + i,
236
logobox.x + logobox.width, logobox.y + i);
239
if (textbox.width != 0 && textbox.height != 0)
241
gdk_gc_set_rgb_fg_color (gc, &theme->bgcolor1);
242
gdk_draw_rectangle (drawable, gc, TRUE, textbox.x, textbox.y,
243
textbox.width, textbox.height);
250
balou_theme_generate_preview (const BalouTheme *theme,
257
GdkRectangle logobox;
258
GdkRectangle textbox;
266
/* check for a cached preview first */
267
pixbuf = load_cached_preview (theme);
270
pw = gdk_pixbuf_get_width (pixbuf);
271
ph = gdk_pixbuf_get_height (pixbuf);
273
if (pw == width && ph == height)
277
else if (pw >= width && ph >= height)
279
scaled = gdk_pixbuf_scale_simple (pixbuf, width, height,
280
GDK_INTERP_BILINEAR);
281
g_object_unref (pixbuf);
285
g_object_unref (pixbuf);
288
root = gdk_screen_get_root_window (gdk_screen_get_default ());
289
pixmap = gdk_pixmap_new (GDK_DRAWABLE (root), WIDTH, HEIGHT, -1);
290
gc = gdk_gc_new (pixmap);
291
gdk_gc_set_function (gc, GDK_COPY);
295
logobox.width = WIDTH;
296
logobox.height = HEIGHT;
299
balou_theme_draw_gradient (theme, GDK_DRAWABLE (pixmap),
300
gc, logobox, textbox);
302
pixbuf = balou_theme_get_logo (theme, WIDTH, HEIGHT);
305
pw = gdk_pixbuf_get_width (pixbuf);
306
ph = gdk_pixbuf_get_height (pixbuf);
308
gdk_draw_pixbuf (GDK_DRAWABLE (pixmap), gc, pixbuf, 0, 0,
309
(WIDTH - pw) / 2, (HEIGHT - ph) / 2,
310
pw, ph, GDK_RGB_DITHER_NONE, 0, 0);
312
g_object_unref (G_OBJECT (pixbuf));
315
pixbuf = gdk_pixbuf_get_from_drawable (NULL, GDK_DRAWABLE (pixmap),
316
NULL, 0, 0, 0, 0, WIDTH, HEIGHT);
317
scaled = gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_BILINEAR);
319
g_object_unref (pixbuf);
320
g_object_unref (pixmap);
324
store_cached_preview (theme, scaled);
334
balou_theme_destroy (BalouTheme *theme)
336
if (theme->name != NULL)
337
g_free (theme->name);
338
if (theme->description != NULL)
339
g_free (theme->description);
340
if (theme->theme_file != NULL)
341
g_free (theme->theme_file);
342
if (theme->logo_file != NULL)
343
g_free (theme->logo_file);
350
load_color_pair (const XfceRc *rc,
352
GdkColor *color1_return,
353
GdkColor *color2_return,
354
const gchar *color_default)
359
spec = xfce_rc_read_entry (rc, name, color_default);
362
gdk_color_parse (color_default, color1_return);
363
gdk_color_parse (color_default, color2_return);
367
s = g_strsplit (spec, ":", 2);
371
gdk_color_parse (color_default, color1_return);
372
gdk_color_parse (color_default, color2_return);
374
else if (s[1] == NULL)
376
if (!gdk_color_parse (s[0], color1_return))
377
gdk_color_parse (color_default, color1_return);
378
*color2_return = *color1_return;
382
if (!gdk_color_parse (s[0], color2_return))
383
gdk_color_parse (color_default, color2_return);
384
if (!gdk_color_parse (s[1], color1_return))
385
*color1_return = *color2_return;
394
load_pixbuf (const gchar *path,
395
gint available_width,
396
gint available_height)
398
static char *suffixes[] = { "svg", "png", "jpeg", "jpg", "xpm", NULL };
400
GdkPixbuf *pb = NULL;
408
if (G_UNLIKELY (path == NULL))
411
pb = gdk_pixbuf_new_from_file (path, NULL);
412
if (G_UNLIKELY (pb == NULL))
414
for (n = 0; pb == NULL && suffixes[n] != NULL; ++n)
416
file = g_strdup_printf ("%s.%s", path, suffixes[n]);
417
pb = gdk_pixbuf_new_from_file (file, NULL);
422
if (G_UNLIKELY (pb == NULL))
425
pb_width = gdk_pixbuf_get_width (pb);
426
pb_height = gdk_pixbuf_get_height (pb);
428
if (pb_width > available_width || pb_height > available_height)
430
wratio = (gdouble) pb_width / (gdouble) available_width;
431
hratio = (gdouble) pb_height / (gdouble) available_height;
435
pb_width = rint (pb_width / hratio);
436
pb_height = available_height;
440
pb_width = available_width;
441
pb_height = rint (pb_height / wratio);
444
scaled = gdk_pixbuf_scale_simple (pb,
447
GDK_INTERP_BILINEAR);
457
mtime (const gchar *path)
461
if (path == NULL || stat (path, &sb) < 0)
469
load_cached_preview (const BalouTheme *theme)
475
resource = g_strconcat ("splash-theme-preview-", theme->name, ".png", NULL);
476
preview = xfce_resource_lookup (XFCE_RESOURCE_CACHE, resource);
482
if ((mtime (preview) < mtime (theme->theme_file))
483
|| (theme->logo_file != NULL
484
&& (mtime (preview) < mtime (theme->logo_file))))
486
/* preview is outdated, need to regenerate preview */
493
pixbuf = gdk_pixbuf_new_from_file (preview, NULL);
501
store_cached_preview (const BalouTheme *theme,
507
resource = g_strconcat ("splash-theme-preview-", theme->name, ".png", NULL);
508
preview = xfce_resource_save_location (XFCE_RESOURCE_CACHE, resource, TRUE);
513
gdk_pixbuf_save (pixbuf, preview, "png", NULL, NULL);