/* Metacity Theme Rendering */
/*
* Copyright (C) 2001 Havoc Pennington
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <config.h>
#include "theme.h"
#include "theme-parser.h"
#include "util.h"
#include "gradient.h"
#include <gtk/gtkwidget.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define GDK_COLOR_RGBA(color) \
((guint32) (0xff | \
(((color).red / 256) << 24) | \
(((color).green / 256) << 16) | \
(((color).blue / 256) << 8)))
#define GDK_COLOR_RGB(color) \
((guint32) ((((color).red / 256) << 16) | \
(((color).green / 256) << 8) | \
(((color).blue / 256))))
#define ALPHA_TO_UCHAR(d) ((unsigned char) ((d) * 255))
#define DEBUG_FILL_STRUCT(s) memset ((s), 0xef, sizeof (*(s)))
#define CLAMP_UCHAR(v) ((guchar) (CLAMP (((int)v), (int)0, (int)255)))
#define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
static void gtk_style_shade (GdkColor *a,
GdkColor *b,
gdouble k);
static void rgb_to_hls (gdouble *r,
gdouble *g,
gdouble *b);
static void hls_to_rgb (gdouble *h,
gdouble *l,
gdouble *s);
static GdkPixbuf *
colorize_pixbuf (GdkPixbuf *orig,
GdkColor *new_color)
{
GdkPixbuf *pixbuf;
double intensity;
int x, y;
const guchar *src;
guchar *dest;
int orig_rowstride;
int dest_rowstride;
int width, height;
gboolean has_alpha;
const guchar *src_pixels;
guchar *dest_pixels;
pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (orig), gdk_pixbuf_get_has_alpha (orig),
gdk_pixbuf_get_bits_per_sample (orig),
gdk_pixbuf_get_width (orig), gdk_pixbuf_get_height (orig));
if (pixbuf == NULL)
return NULL;
orig_rowstride = gdk_pixbuf_get_rowstride (orig);
dest_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
has_alpha = gdk_pixbuf_get_has_alpha (orig);
src_pixels = gdk_pixbuf_get_pixels (orig);
dest_pixels = gdk_pixbuf_get_pixels (pixbuf);
for (y = 0; y < height; y++)
{
src = src_pixels + y * orig_rowstride;
dest = dest_pixels + y * dest_rowstride;
for (x = 0; x < width; x++)
{
double dr, dg, db;
intensity = INTENSITY (src[0], src[1], src[2]) / 255.0;
if (intensity <= 0.5)
{
/* Go from black at intensity = 0.0 to new_color at intensity = 0.5 */
dr = (new_color->red * intensity * 2.0) / 65535.0;
dg = (new_color->green * intensity * 2.0) / 65535.0;
db = (new_color->blue * intensity * 2.0) / 65535.0;
}
else
{
/* Go from new_color at intensity = 0.5 to white at intensity = 1.0 */
dr = (new_color->red + (65535 - new_color->red) * (intensity - 0.5) * 2.0) / 65535.0;
dg = (new_color->green + (65535 - new_color->green) * (intensity - 0.5) * 2.0) / 65535.0;
db = (new_color->blue + (65535 - new_color->blue) * (intensity - 0.5) * 2.0) / 65535.0;
}
dest[0] = CLAMP_UCHAR (255 * dr);
dest[1] = CLAMP_UCHAR (255 * dg);
dest[2] = CLAMP_UCHAR (255 * db);
if (has_alpha)
{
dest[3] = src[3];
src += 4;
dest += 4;
}
else
{
src += 3;
dest += 3;
}
}
}
return pixbuf;
}
static void
color_composite (const GdkColor *bg,
const GdkColor *fg,
double alpha_d,
GdkColor *color)
{
guint16 alpha;
*color = *bg;
alpha = alpha_d * 0xffff;
color->red = color->red + (((fg->red - color->red) * alpha + 0x8000) >> 16);
color->green = color->green + (((fg->green - color->green) * alpha + 0x8000) >> 16);
color->blue = color->blue + (((fg->blue - color->blue) * alpha + 0x8000) >> 16);
}
static void
init_border (GtkBorder *border)
{
border->top = -1;
border->bottom = -1;
border->left = -1;
border->right = -1;
}
MetaFrameLayout*
meta_frame_layout_new (void)
{
MetaFrameLayout *layout;
layout = g_new0 (MetaFrameLayout, 1);
layout->refcount = 1;
/* Fill with -1 values to detect invalid themes */
layout->left_width = -1;
layout->right_width = -1;
layout->bottom_height = -1;
init_border (&layout->title_border);
layout->title_vertical_pad = -1;
layout->right_titlebar_edge = -1;
layout->left_titlebar_edge = -1;
layout->button_sizing = META_BUTTON_SIZING_LAST;
layout->button_aspect = 1.0;
layout->button_width = -1;
layout->button_height = -1;
layout->has_title = TRUE;
layout->title_scale = 1.0;
init_border (&layout->button_border);
return layout;
}
static gboolean
validate_border (const GtkBorder *border,
const char **bad)
{
*bad = NULL;
if (border->top < 0)
*bad = _("top");
else if (border->bottom < 0)
*bad = _("bottom");
else if (border->left < 0)
*bad = _("left");
else if (border->right < 0)
*bad = _("right");
return *bad == NULL;
}
static gboolean
validate_geometry_value (int val,
const char *name
|