2003-09-17 07:44:49 +00:00
|
|
|
/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
|
2003-09-17 07:32:52 +00:00
|
|
|
|
|
|
|
image.c for the Openbox window manager
|
2006-08-22 16:44:18 +00:00
|
|
|
Copyright (c) 2006 Mikael Magnusson
|
2007-04-23 17:56:35 +00:00
|
|
|
Copyright (c) 2003-2007 Dana Jansens
|
2003-09-17 07:32:52 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
See the COPYING file for a copy of the GNU General Public License.
|
|
|
|
*/
|
|
|
|
|
2003-07-10 04:31:34 +00:00
|
|
|
#include "geom.h"
|
2003-03-26 05:38:13 +00:00
|
|
|
#include "image.h"
|
2003-06-20 07:58:51 +00:00
|
|
|
#include "color.h"
|
2008-02-14 09:47:49 +00:00
|
|
|
#include "imagecache.h"
|
2010-01-08 22:48:07 +00:00
|
|
|
#ifdef USE_IMLIB2
|
|
|
|
#include <Imlib2.h>
|
|
|
|
#endif
|
2003-06-20 07:58:51 +00:00
|
|
|
|
|
|
|
#include <glib.h>
|
2003-03-26 05:38:13 +00:00
|
|
|
|
2006-03-16 23:53:11 +00:00
|
|
|
#define FRACTION 12
|
|
|
|
#define FLOOR(i) ((i) & (~0UL << FRACTION))
|
|
|
|
#define AVERAGE(a, b) (((((a) ^ (b)) & 0xfefefefeL) >> 1) + ((a) & (b)))
|
2003-09-04 06:23:27 +00:00
|
|
|
|
2010-01-08 22:48:07 +00:00
|
|
|
void RrImagePicInit(RrImagePic *pic, const gchar *name,
|
|
|
|
gint w, gint h, RrPixel32 *data)
|
2008-02-13 14:14:58 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
pic->width = w;
|
|
|
|
pic->height = h;
|
|
|
|
pic->data = data;
|
|
|
|
pic->sum = 0;
|
|
|
|
for (i = w*h; i > 0; --i)
|
|
|
|
pic->sum += *(data++);
|
2010-01-08 22:48:07 +00:00
|
|
|
pic->name = g_strdup(name);
|
2008-02-13 14:14:58 +00:00
|
|
|
}
|
|
|
|
|
2008-02-14 02:30:18 +00:00
|
|
|
static void RrImagePicFree(RrImagePic *pic)
|
|
|
|
{
|
|
|
|
if (pic) {
|
|
|
|
g_free(pic->data);
|
2010-01-08 22:48:07 +00:00
|
|
|
g_free(pic->name);
|
2008-02-14 02:30:18 +00:00
|
|
|
g_free(pic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-13 13:43:57 +00:00
|
|
|
/*! Add a picture to an Image, that is, add another copy of the image at
|
|
|
|
another size. This may add it to the "originals" list or to the
|
|
|
|
"resized" list. */
|
2008-02-14 09:47:49 +00:00
|
|
|
static void AddPicture(RrImage *self, RrImagePic ***list, gint *len,
|
|
|
|
RrImagePic *pic)
|
2003-09-04 06:23:27 +00:00
|
|
|
{
|
2008-02-14 09:47:49 +00:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_assert(pic->width > 0 && pic->height > 0);
|
|
|
|
|
2010-01-08 22:48:07 +00:00
|
|
|
if (pic->name)
|
|
|
|
g_assert(!g_hash_table_lookup(self->cache->name_table, pic->name));
|
|
|
|
else
|
|
|
|
g_assert(!g_hash_table_lookup(self->cache->pic_table, pic));
|
2008-02-14 09:47:49 +00:00
|
|
|
|
|
|
|
/* grow the list */
|
|
|
|
*list = g_renew(RrImagePic*, *list, ++*len);
|
|
|
|
|
|
|
|
/* move everything else down one */
|
|
|
|
for (i = *len-1; i > 0; --i)
|
|
|
|
(*list)[i] = (*list)[i-1];
|
|
|
|
|
|
|
|
/* set the new picture up at the front of the list */
|
|
|
|
(*list)[0] = pic;
|
|
|
|
|
2010-01-08 22:48:07 +00:00
|
|
|
/* add the name or the picture as a key to point to this image in the
|
|
|
|
cache */
|
|
|
|
if (pic->name)
|
|
|
|
g_hash_table_insert(self->cache->name_table, pic->name, self);
|
|
|
|
else
|
|
|
|
g_hash_table_insert(self->cache->pic_table, (*list)[0], self);
|
2008-02-14 09:47:49 +00:00
|
|
|
|
2009-12-15 21:03:35 +00:00
|
|
|
/*
|
2008-02-14 09:47:49 +00:00
|
|
|
#ifdef DEBUG
|
2008-03-02 20:17:31 +00:00
|
|
|
g_debug("Adding %s picture to the cache:\n "
|
2009-11-21 19:29:45 +00:00
|
|
|
"Image 0x%lx, w %d h %d Hash %u",
|
2008-03-02 20:17:31 +00:00
|
|
|
(*list == self->original ? "ORIGINAL" : "RESIZED"),
|
2009-11-21 19:29:45 +00:00
|
|
|
(gulong)self, pic->width, pic->height, RrImagePicHash(pic));
|
2008-02-14 09:47:49 +00:00
|
|
|
#endif
|
2009-12-15 21:03:35 +00:00
|
|
|
*/
|
2008-02-14 09:47:49 +00:00
|
|
|
}
|
|
|
|
|
2008-02-13 13:43:57 +00:00
|
|
|
/*! Remove a picture from an Image. This may remove it from the "originals"
|
|
|
|
list or the "resized" list. */
|
2008-02-14 09:47:49 +00:00
|
|
|
static void RemovePicture(RrImage *self, RrImagePic ***list,
|
|
|
|
gint i, gint *len)
|
|
|
|
{
|
|
|
|
gint j;
|
|
|
|
|
2009-12-15 21:03:35 +00:00
|
|
|
/*
|
2008-02-14 09:47:49 +00:00
|
|
|
#ifdef DEBUG
|
2008-03-02 20:17:31 +00:00
|
|
|
g_debug("Removing %s picture from the cache:\n "
|
2009-11-21 19:29:45 +00:00
|
|
|
"Image 0x%lx, w %d h %d Hash %u",
|
2008-03-02 20:17:31 +00:00
|
|
|
(*list == self->original ? "ORIGINAL" : "RESIZED"),
|
2009-11-21 19:29:45 +00:00
|
|
|
(gulong)self, (*list)[i]->width, (*list)[i]->height,
|
2008-03-02 20:17:31 +00:00
|
|
|
RrImagePicHash((*list)[i]));
|
2008-02-14 09:47:49 +00:00
|
|
|
#endif
|
2009-12-15 21:03:35 +00:00
|
|
|
*/
|
2008-02-14 09:47:49 +00:00
|
|
|
|
2010-01-08 22:48:07 +00:00
|
|
|
/* remove the name or picture as a key in the cache */
|
|
|
|
if ((*list)[i]->name)
|
|
|
|
g_hash_table_remove(self->cache->name_table, (*list)[i]->name);
|
|
|
|
else
|
|
|
|
g_hash_table_remove(self->cache->pic_table, (*list)[i]);
|
2008-02-14 09:47:49 +00:00
|
|
|
|
2008-02-14 02:30:18 +00:00
|
|
|
/* free the picture */
|
|
|
|
RrImagePicFree((*list)[i]);
|
2008-02-14 09:47:49 +00:00
|
|
|
/* shift everything down one */
|
|
|
|
for (j = i; j < *len-1; ++j)
|
|
|
|
(*list)[j] = (*list)[j+1];
|
|
|
|
/* shrink the list */
|
|
|
|
*list = g_renew(RrImagePic*, *list, --*len);
|
|
|
|
}
|
|
|
|
|
2008-02-13 13:43:57 +00:00
|
|
|
/*! Given a picture in RGBA format, of a specified size, resize it to the new
|
|
|
|
requested size (but keep its aspect ratio). If the image does not need to
|
|
|
|
be resized (it is already the right size) then this returns NULL. Otherwise
|
|
|
|
it returns a newly allocated RrImagePic with the resized picture inside it
|
|
|
|
*/
|
2008-02-14 09:47:49 +00:00
|
|
|
static RrImagePic* ResizeImage(RrPixel32 *src,
|
2008-02-13 13:43:57 +00:00
|
|
|
gulong srcW, gulong srcH,
|
|
|
|
gulong dstW, gulong dstH)
|
2008-02-14 09:47:49 +00:00
|
|
|
{
|
2008-02-13 14:14:58 +00:00
|
|
|
RrPixel32 *dst, *dststart;
|
2008-02-14 09:47:49 +00:00
|
|
|
RrImagePic *pic;
|
2006-03-16 23:53:11 +00:00
|
|
|
gulong dstX, dstY, srcX, srcY;
|
|
|
|
gulong srcX1, srcX2, srcY1, srcY2;
|
|
|
|
gulong ratioX, ratioY;
|
2008-02-14 09:47:49 +00:00
|
|
|
gulong aspectW, aspectH;
|
|
|
|
|
2008-03-28 20:01:46 +00:00
|
|
|
/* XXX should these variables be ensured to not be zero in the callers? */
|
|
|
|
srcW = srcW ? srcW : 1;
|
|
|
|
srcH = srcH ? srcH : 1;
|
|
|
|
dstW = dstW ? dstW : 1;
|
|
|
|
dstH = dstH ? dstH : 1;
|
|
|
|
|
2008-02-14 09:47:49 +00:00
|
|
|
/* keep the aspect ratio */
|
|
|
|
aspectW = dstW;
|
|
|
|
aspectH = (gint)(dstW * ((gdouble)srcH / srcW));
|
|
|
|
if (aspectH > dstH) {
|
|
|
|
aspectH = dstH;
|
|
|
|
aspectW = (gint)(dstH * ((gdouble)srcW / srcH));
|
|
|
|
}
|
2008-03-28 20:01:46 +00:00
|
|
|
dstW = aspectW ? aspectW : 1;
|
|
|
|
dstH = aspectH ? aspectH : 1;
|
2008-02-14 09:47:49 +00:00
|
|
|
|
|
|
|
if (srcW == dstW && srcH == dstH)
|
2008-03-28 20:01:46 +00:00
|
|
|
return NULL; /* no scaling needed! */
|
2008-02-14 09:47:49 +00:00
|
|
|
|
2008-02-13 14:14:58 +00:00
|
|
|
dststart = dst = g_new(RrPixel32, dstW * dstH);
|
2007-07-17 00:48:16 +00:00
|
|
|
|
2006-03-16 23:53:11 +00:00
|
|
|
ratioX = (srcW << FRACTION) / dstW;
|
|
|
|
ratioY = (srcH << FRACTION) / dstH;
|
2007-07-17 00:48:16 +00:00
|
|
|
|
2006-03-16 23:53:11 +00:00
|
|
|
srcY2 = 0;
|
|
|
|
for (dstY = 0; dstY < dstH; dstY++) {
|
|
|
|
srcY1 = srcY2;
|
|
|
|
srcY2 += ratioY;
|
2007-07-17 00:48:16 +00:00
|
|
|
|
2006-03-16 23:53:11 +00:00
|
|
|
srcX2 = 0;
|
|
|
|
for (dstX = 0; dstX < dstW; dstX++) {
|
|
|
|
gulong red = 0, green = 0, blue = 0, alpha = 0;
|
|
|
|
gulong portionX, portionY, portionXY, sumXY = 0;
|
|
|
|
RrPixel32 pixel;
|
2007-07-17 00:48:16 +00:00
|
|
|
|
2006-03-16 23:53:11 +00:00
|
|
|
srcX1 = srcX2;
|
|
|
|
srcX2 += ratioX;
|
2007-07-17 00:48:16 +00:00
|
|
|
|
2006-03-16 23:53:11 +00:00
|
|
|
for (srcY = srcY1; srcY < srcY2; srcY += (1UL << FRACTION)) {
|
|
|
|
if (srcY == srcY1) {
|
|
|
|
srcY = FLOOR(srcY);
|
|
|
|
portionY = (1UL << FRACTION) - (srcY1 - srcY);
|
|
|
|
if (portionY > srcY2 - srcY1)
|
|
|
|
portionY = srcY2 - srcY1;
|
|
|
|
}
|
|
|
|
else if (srcY == FLOOR(srcY2))
|
|
|
|
portionY = srcY2 - srcY;
|
|
|
|
else
|
|
|
|
portionY = (1UL << FRACTION);
|
2007-07-17 00:48:16 +00:00
|
|
|
|
2006-03-16 23:53:11 +00:00
|
|
|
for (srcX = srcX1; srcX < srcX2; srcX += (1UL << FRACTION)) {
|
|
|
|
if (srcX == srcX1) {
|
|
|
|
srcX = FLOOR(srcX);
|
|
|
|
portionX = (1UL << FRACTION) - (srcX1 - srcX);
|
|
|
|
if (portionX > srcX2 - srcX1)
|
|
|
|
portionX = srcX2 - srcX1;
|
|
|
|
}
|
|
|
|
else if (srcX == FLOOR(srcX2))
|
|
|
|
portionX = srcX2 - srcX;
|
|
|
|
else
|
|
|
|
portionX = (1UL << FRACTION);
|
2007-07-17 00:48:16 +00:00
|
|
|
|
2006-03-16 23:53:11 +00:00
|
|
|
portionXY = (portionX * portionY) >> FRACTION;
|
|
|
|
sumXY += portionXY;
|
2007-07-17 00:48:16 +00:00
|
|
|
|
2006-11-15 21:12:06 +00:00
|
|
|
pixel = *(src + (srcY >> FRACTION) * srcW
|
|
|
|
+ (srcX >> FRACTION));
|
|
|
|
red += ((pixel >> RrDefaultRedOffset) & 0xFF)
|
|
|
|
* portionXY;
|
|
|
|
green += ((pixel >> RrDefaultGreenOffset) & 0xFF)
|
|
|
|
* portionXY;
|
|
|
|
blue += ((pixel >> RrDefaultBlueOffset) & 0xFF)
|
|
|
|
* portionXY;
|
|
|
|
alpha += ((pixel >> RrDefaultAlphaOffset) & 0xFF)
|
|
|
|
* portionXY;
|
2006-03-16 23:53:11 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-17 00:48:16 +00:00
|
|
|
|
2006-03-16 23:53:11 +00:00
|
|
|
g_assert(sumXY != 0);
|
|
|
|
red /= sumXY;
|
|
|
|
green /= sumXY;
|
|
|
|
blue /= sumXY;
|
|
|
|
alpha /= sumXY;
|
2007-07-17 00:48:16 +00:00
|
|
|
|
2006-03-16 23:53:11 +00:00
|
|
|
*dst++ = (red << RrDefaultRedOffset) |
|
|
|
|
(green << RrDefaultGreenOffset) |
|
|
|
|
(blue << RrDefaultBlueOffset) |
|
|
|
|
(alpha << RrDefaultAlphaOffset);
|
2003-09-04 06:23:27 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-14 09:47:49 +00:00
|
|
|
|
2008-02-13 14:14:58 +00:00
|
|
|
pic = g_new(RrImagePic, 1);
|
2010-01-08 22:48:07 +00:00
|
|
|
RrImagePicInit(pic, NULL, dstW, dstH, dststart);
|
2008-02-13 14:14:58 +00:00
|
|
|
|
2008-02-14 09:47:49 +00:00
|
|
|
return pic;
|
2003-09-04 06:23:27 +00:00
|
|
|
}
|
|
|
|
|
2009-12-09 19:59:18 +00:00
|
|
|
/*! This draws an RGBA picture into the target, within the rectangle specified
|
2008-02-14 09:47:49 +00:00
|
|
|
by the area parameter. If the area's size differs from the source's then it
|
|
|
|
will be centered within the rectangle */
|
|
|
|
void DrawRGBA(RrPixel32 *target, gint target_w, gint target_h,
|
|
|
|
RrPixel32 *source, gint source_w, gint source_h,
|
|
|
|
gint alpha, RrRect *area)
|
2003-09-04 06:23:27 +00:00
|
|
|
{
|
|
|
|
RrPixel32 *dest;
|
|
|
|
gint col, num_pixels;
|
2008-02-14 09:47:49 +00:00
|
|
|
gint dw, dh;
|
2003-09-04 06:23:27 +00:00
|
|
|
|
2008-02-14 09:47:49 +00:00
|
|
|
g_assert(source_w <= area->width && source_h <= area->height);
|
2008-02-13 13:43:57 +00:00
|
|
|
g_assert(area->x + area->width <= target_w);
|
|
|
|
g_assert(area->y + area->height <= target_h);
|
2003-09-04 06:23:27 +00:00
|
|
|
|
2008-02-14 09:47:49 +00:00
|
|
|
/* keep the aspect ratio */
|
2003-09-04 06:23:27 +00:00
|
|
|
dw = area->width;
|
2008-02-14 09:47:49 +00:00
|
|
|
dh = (gint)(dw * ((gdouble)source_h / source_w));
|
2003-09-04 06:23:27 +00:00
|
|
|
if (dh > area->height) {
|
|
|
|
dh = area->height;
|
2008-02-14 09:47:49 +00:00
|
|
|
dw = (gint)(dh * ((gdouble)source_w / source_h));
|
2003-09-04 06:23:27 +00:00
|
|
|
}
|
2003-05-09 16:57:17 +00:00
|
|
|
|
2007-05-26 17:35:17 +00:00
|
|
|
/* copy source -> dest, and apply the alpha channel.
|
|
|
|
center the image if it is smaller than the area */
|
2003-09-04 06:23:27 +00:00
|
|
|
col = 0;
|
|
|
|
num_pixels = dw * dh;
|
2007-05-26 17:35:17 +00:00
|
|
|
dest = target + area->x + (area->width - dw) / 2 +
|
|
|
|
(target_w * (area->y + (area->height - dh) / 2));
|
2003-09-04 06:23:27 +00:00
|
|
|
while (num_pixels-- > 0) {
|
2008-02-14 09:47:49 +00:00
|
|
|
guchar a, r, g, b, bgr, bgg, bgb;
|
2003-03-26 05:38:13 +00:00
|
|
|
|
2007-05-29 03:31:25 +00:00
|
|
|
/* apply the rgba's opacity as well */
|
2008-02-14 09:47:49 +00:00
|
|
|
a = ((*source >> RrDefaultAlphaOffset) * alpha) >> 8;
|
2003-09-04 06:23:27 +00:00
|
|
|
r = *source >> RrDefaultRedOffset;
|
|
|
|
g = *source >> RrDefaultGreenOffset;
|
|
|
|
b = *source >> RrDefaultBlueOffset;
|
2006-03-16 23:53:11 +00:00
|
|
|
|
2003-09-04 06:23:27 +00:00
|
|
|
/* background color */
|
|
|
|
bgr = *dest >> RrDefaultRedOffset;
|
|
|
|
bgg = *dest >> RrDefaultGreenOffset;
|
|
|
|
bgb = *dest >> RrDefaultBlueOffset;
|
2003-03-26 05:38:13 +00:00
|
|
|
|
2008-02-14 09:47:49 +00:00
|
|
|
r = bgr + (((r - bgr) * a) >> 8);
|
|
|
|
g = bgg + (((g - bgg) * a) >> 8);
|
|
|
|
b = bgb + (((b - bgb) * a) >> 8);
|
2003-03-26 05:38:13 +00:00
|
|
|
|
2003-09-04 06:23:27 +00:00
|
|
|
*dest = ((r << RrDefaultRedOffset) |
|
|
|
|
(g << RrDefaultGreenOffset) |
|
|
|
|
(b << RrDefaultBlueOffset));
|
2003-03-26 05:38:13 +00:00
|
|
|
|
2003-09-04 06:23:27 +00:00
|
|
|
dest++;
|
|
|
|
source++;
|
2003-03-26 05:38:13 +00:00
|
|
|
|
2006-03-05 23:23:55 +00:00
|
|
|
if (++col >= dw) {
|
2003-09-04 06:23:27 +00:00
|
|
|
col = 0;
|
|
|
|
dest += target_w - dw;
|
2003-05-09 16:57:17 +00:00
|
|
|
}
|
2003-03-26 05:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-14 09:47:49 +00:00
|
|
|
|
2008-02-13 13:43:57 +00:00
|
|
|
/*! Draw an RGBA texture into a target pixel buffer. */
|
2008-02-14 09:47:49 +00:00
|
|
|
void RrImageDrawRGBA(RrPixel32 *target, RrTextureRGBA *rgba,
|
|
|
|
gint target_w, gint target_h,
|
|
|
|
RrRect *area)
|
|
|
|
{
|
|
|
|
RrImagePic *scaled;
|
|
|
|
|
|
|
|
scaled = ResizeImage(rgba->data, rgba->width, rgba->height,
|
|
|
|
area->width, area->height);
|
|
|
|
|
|
|
|
if (scaled) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
g_warning("Scaling an RGBA! You should avoid this and just make "
|
|
|
|
"it the right size yourself!");
|
|
|
|
#endif
|
|
|
|
DrawRGBA(target, target_w, target_h,
|
|
|
|
scaled->data, scaled->width, scaled->height,
|
|
|
|
rgba->alpha, area);
|
2008-03-05 19:12:45 +00:00
|
|
|
RrImagePicFree(scaled);
|
2008-02-14 09:47:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
DrawRGBA(target, target_w, target_h,
|
|
|
|
rgba->data, rgba->width, rgba->height,
|
|
|
|
rgba->alpha, area);
|
|
|
|
}
|
|
|
|
|
2008-02-13 13:43:57 +00:00
|
|
|
/*! Create a new RrImage, which is linked to an image cache */
|
2008-02-14 09:47:49 +00:00
|
|
|
RrImage* RrImageNew(RrImageCache *cache)
|
|
|
|
{
|
|
|
|
RrImage *self;
|
|
|
|
|
2008-02-13 13:43:57 +00:00
|
|
|
g_assert(cache != NULL);
|
|
|
|
|
2008-02-14 09:47:49 +00:00
|
|
|
self = g_new0(RrImage, 1);
|
|
|
|
self->ref = 1;
|
|
|
|
self->cache = cache;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
This patch implements support for icons in user-defined menus into Openbox
Image loading is done using the Imlib2 library.
I chose Imlib2 because it's pretty fast, it's easy to use, supports many file
formats (tested xpm, gif, jpeg, png) and doesn't introduce too much bloat (it
depends :)).
I ported the patch to 3.4.7-pre3 and added some enhancements. Caching is much
better now, and icons can be disabled at compile time using --disable-imlib2
option.
What's new?
Syntax of configuration files (namely rc.xml and menu.xml) has been changed
slightly to allow users to associate icons to menu entries. This is done by
specifying path to icon file in the new "icon" attribute in "<item>" element,
e.g:
<item label="Vim" icon="/usr/share/pixmaps/vim-32.xpm">
<action name="Execute"><execute>x-terminal-emulator -T Vim -e
vim</execute></action>
</item>
If user doesn't want to display any icons in his user-defined menus, he/she can
disable icons in rc.xml, inside "<menu>" section:
<menu>
...
<showIcons>no</showIcons>
...
</menu>
Default value is "yes".
(New boolean variable "config_menu_user_show_icons" has been added to source
code.)
An icon is loaded (using menu_item_attach_icon()) when a new entry of menu is
created. Fortunately, I haven't notice any performance problems because of this
:).
2008-03-25 20:58:12 +00:00
|
|
|
/*! Set function that will be called just before RrImage is destroyed. */
|
2010-01-08 22:48:07 +00:00
|
|
|
void RrImageSetDestroyFunc(RrImage *image, RrImageDestroyFunc func,
|
|
|
|
gpointer data)
|
This patch implements support for icons in user-defined menus into Openbox
Image loading is done using the Imlib2 library.
I chose Imlib2 because it's pretty fast, it's easy to use, supports many file
formats (tested xpm, gif, jpeg, png) and doesn't introduce too much bloat (it
depends :)).
I ported the patch to 3.4.7-pre3 and added some enhancements. Caching is much
better now, and icons can be disabled at compile time using --disable-imlib2
option.
What's new?
Syntax of configuration files (namely rc.xml and menu.xml) has been changed
slightly to allow users to associate icons to menu entries. This is done by
specifying path to icon file in the new "icon" attribute in "<item>" element,
e.g:
<item label="Vim" icon="/usr/share/pixmaps/vim-32.xpm">
<action name="Execute"><execute>x-terminal-emulator -T Vim -e
vim</execute></action>
</item>
If user doesn't want to display any icons in his user-defined menus, he/she can
disable icons in rc.xml, inside "<menu>" section:
<menu>
...
<showIcons>no</showIcons>
...
</menu>
Default value is "yes".
(New boolean variable "config_menu_user_show_icons" has been added to source
code.)
An icon is loaded (using menu_item_attach_icon()) when a new entry of menu is
created. Fortunately, I haven't notice any performance problems because of this
:).
2008-03-25 20:58:12 +00:00
|
|
|
{
|
|
|
|
image->destroy_func = func;
|
2010-01-08 22:48:07 +00:00
|
|
|
image->destroy_data = data;
|
This patch implements support for icons in user-defined menus into Openbox
Image loading is done using the Imlib2 library.
I chose Imlib2 because it's pretty fast, it's easy to use, supports many file
formats (tested xpm, gif, jpeg, png) and doesn't introduce too much bloat (it
depends :)).
I ported the patch to 3.4.7-pre3 and added some enhancements. Caching is much
better now, and icons can be disabled at compile time using --disable-imlib2
option.
What's new?
Syntax of configuration files (namely rc.xml and menu.xml) has been changed
slightly to allow users to associate icons to menu entries. This is done by
specifying path to icon file in the new "icon" attribute in "<item>" element,
e.g:
<item label="Vim" icon="/usr/share/pixmaps/vim-32.xpm">
<action name="Execute"><execute>x-terminal-emulator -T Vim -e
vim</execute></action>
</item>
If user doesn't want to display any icons in his user-defined menus, he/she can
disable icons in rc.xml, inside "<menu>" section:
<menu>
...
<showIcons>no</showIcons>
...
</menu>
Default value is "yes".
(New boolean variable "config_menu_user_show_icons" has been added to source
code.)
An icon is loaded (using menu_item_attach_icon()) when a new entry of menu is
created. Fortunately, I haven't notice any performance problems because of this
:).
2008-03-25 20:58:12 +00:00
|
|
|
}
|
|
|
|
|
2008-02-14 09:47:49 +00:00
|
|
|
void RrImageRef(RrImage *self)
|
|
|
|
{
|
|
|
|
++self->ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RrImageUnref(RrImage *self)
|
|
|
|
{
|
|
|
|
if (self && --self->ref == 0) {
|
2009-12-15 21:03:35 +00:00
|
|
|
/*
|
2008-02-14 09:47:49 +00:00
|
|
|
#ifdef DEBUG
|
2008-03-02 20:17:31 +00:00
|
|
|
g_debug("Refcount to 0, removing ALL pictures from the cache:\n "
|
2009-11-21 19:29:45 +00:00
|
|
|
"Image 0x%lx", (gulong)self);
|
2008-02-14 09:47:49 +00:00
|
|
|
#endif
|
2009-12-15 21:03:35 +00:00
|
|
|
*/
|
This patch implements support for icons in user-defined menus into Openbox
Image loading is done using the Imlib2 library.
I chose Imlib2 because it's pretty fast, it's easy to use, supports many file
formats (tested xpm, gif, jpeg, png) and doesn't introduce too much bloat (it
depends :)).
I ported the patch to 3.4.7-pre3 and added some enhancements. Caching is much
better now, and icons can be disabled at compile time using --disable-imlib2
option.
What's new?
Syntax of configuration files (namely rc.xml and menu.xml) has been changed
slightly to allow users to associate icons to menu entries. This is done by
specifying path to icon file in the new "icon" attribute in "<item>" element,
e.g:
<item label="Vim" icon="/usr/share/pixmaps/vim-32.xpm">
<action name="Execute"><execute>x-terminal-emulator -T Vim -e
vim</execute></action>
</item>
If user doesn't want to display any icons in his user-defined menus, he/she can
disable icons in rc.xml, inside "<menu>" section:
<menu>
...
<showIcons>no</showIcons>
...
</menu>
Default value is "yes".
(New boolean variable "config_menu_user_show_icons" has been added to source
code.)
An icon is loaded (using menu_item_attach_icon()) when a new entry of menu is
created. Fortunately, I haven't notice any performance problems because of this
:).
2008-03-25 20:58:12 +00:00
|
|
|
if (self->destroy_func)
|
2010-01-08 22:48:07 +00:00
|
|
|
self->destroy_func(self, self->destroy_data);
|
2008-02-14 09:47:49 +00:00
|
|
|
while (self->n_original > 0)
|
|
|
|
RemovePicture(self, &self->original, 0, &self->n_original);
|
|
|
|
while (self->n_resized > 0)
|
|
|
|
RemovePicture(self, &self->resized, 0, &self->n_resized);
|
|
|
|
g_free(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-08 22:48:07 +00:00
|
|
|
static void AddPictureFromData(RrImage *self, const char *name,
|
|
|
|
const RrPixel32 *data, gint w, gint h)
|
2008-02-14 09:47:49 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
RrImagePic *pic;
|
|
|
|
|
|
|
|
/* make sure we don't already have this size.. */
|
|
|
|
for (i = 0; i < self->n_original; ++i)
|
|
|
|
if (self->original[i]->width == w && self->original[i]->height == h) {
|
2009-12-15 21:03:35 +00:00
|
|
|
/*
|
2008-02-14 09:47:49 +00:00
|
|
|
#ifdef DEBUG
|
2008-03-02 20:17:31 +00:00
|
|
|
g_debug("Found duplicate ORIGINAL image:\n "
|
2009-11-21 19:29:45 +00:00
|
|
|
"Image 0x%lx, w %d h %d", (gulong)self, w, h);
|
2008-02-14 09:47:49 +00:00
|
|
|
#endif
|
2009-12-15 21:03:35 +00:00
|
|
|
*/
|
2008-02-14 09:47:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove any resized pictures of this same size */
|
|
|
|
for (i = 0; i < self->n_resized; ++i)
|
|
|
|
if (self->resized[i]->width == w || self->resized[i]->height == h) {
|
|
|
|
RemovePicture(self, &self->resized, i, &self->n_resized);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add the new picture */
|
|
|
|
pic = g_new(RrImagePic, 1);
|
2010-01-08 22:48:07 +00:00
|
|
|
RrImagePicInit(pic, name, w, h, g_memdup(data, w*h*sizeof(RrPixel32)));
|
2008-02-14 09:47:49 +00:00
|
|
|
AddPicture(self, &self->original, &self->n_original, pic);
|
|
|
|
}
|
|
|
|
|
2010-01-08 22:48:07 +00:00
|
|
|
gboolean RrImageAddPictureName(RrImage *self, const gchar *name)
|
|
|
|
{
|
|
|
|
#ifdef USE_IMLIB2
|
|
|
|
Imlib_Image img;
|
|
|
|
gint w, h;
|
|
|
|
RrPixel32 *data;
|
|
|
|
gchar *path;
|
|
|
|
|
|
|
|
/* XXX find the path via freedesktop icon spec (use obt) ! */
|
|
|
|
path = g_strdup(name);
|
|
|
|
|
|
|
|
if (!(img = imlib_load_image(path)))
|
|
|
|
g_message("Cannot load image \"%s\" from file \"%s\"", name, path);
|
|
|
|
g_free(path);
|
|
|
|
|
|
|
|
if (!img)
|
|
|
|
return FALSE; /* failed to load it */
|
|
|
|
|
|
|
|
/* Get data and dimensions of the image.
|
|
|
|
|
|
|
|
WARNING: This stuff is NOT threadsafe !!
|
|
|
|
*/
|
|
|
|
imlib_context_set_image(img);
|
|
|
|
data = imlib_image_get_data_for_reading_only();
|
|
|
|
w = imlib_image_get_width();
|
|
|
|
h = imlib_image_get_height();
|
|
|
|
|
|
|
|
/* add it to the RrImage, and set its name */
|
|
|
|
AddPictureFromData(self, name, data, w, h);
|
|
|
|
|
|
|
|
imlib_free_image();
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! Add a new picture with the given RGBA pixel data and dimensions into the
|
|
|
|
RrImage. This adds an "original" picture to the image.
|
|
|
|
*/
|
|
|
|
void RrImageAddPicture(RrImage *self, const RrPixel32 *data, gint w, gint h)
|
|
|
|
{
|
|
|
|
AddPictureFromData(self, NULL, data, w, h);
|
|
|
|
}
|
|
|
|
|
2008-02-13 13:43:57 +00:00
|
|
|
/*! Remove the picture from the RrImage which has the given dimensions. This
|
|
|
|
removes an "original" picture from the image.
|
|
|
|
*/
|
2008-02-14 09:47:49 +00:00
|
|
|
void RrImageRemovePicture(RrImage *self, gint w, gint h)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
/* remove any resized pictures of this same size */
|
|
|
|
for (i = 0; i < self->n_original; ++i)
|
|
|
|
if (self->original[i]->width == w && self->original[i]->height == h) {
|
|
|
|
RemovePicture(self, &self->original, i, &self->n_original);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-13 13:43:57 +00:00
|
|
|
/*! Draw an RrImage texture into a target pixel buffer. If the RrImage does
|
|
|
|
not contain a picture of the appropriate size, then one of its "original"
|
|
|
|
pictures will be resized and used (and stored in the RrImage as a "resized"
|
|
|
|
picture).
|
|
|
|
*/
|
2008-02-14 09:47:49 +00:00
|
|
|
void RrImageDrawImage(RrPixel32 *target, RrTextureImage *img,
|
|
|
|
gint target_w, gint target_h,
|
|
|
|
RrRect *area)
|
|
|
|
{
|
2008-02-13 03:29:23 +00:00
|
|
|
gint i, min_diff, min_i, min_aspect_diff, min_aspect_i;
|
2008-02-14 09:47:49 +00:00
|
|
|
RrImage *self;
|
|
|
|
RrImagePic *pic;
|
2008-02-14 02:30:18 +00:00
|
|
|
gboolean free_pic;
|
2008-02-14 09:47:49 +00:00
|
|
|
|
|
|
|
self = img->image;
|
|
|
|
pic = NULL;
|
2008-02-14 02:30:18 +00:00
|
|
|
free_pic = FALSE;
|
2008-02-14 09:47:49 +00:00
|
|
|
|
2009-12-09 19:59:18 +00:00
|
|
|
/* is there an original of this size? (only the larger of
|
|
|
|
w or h has to be right cuz we maintain aspect ratios) */
|
2008-02-14 09:47:49 +00:00
|
|
|
for (i = 0; i < self->n_original; ++i)
|
2009-12-09 19:59:18 +00:00
|
|
|
if ((self->original[i]->width >= self->original[i]->height &&
|
|
|
|
self->original[i]->width == area->width) ||
|
|
|
|
(self->original[i]->width <= self->original[i]->height &&
|
|
|
|
self->original[i]->height == area->height))
|
2008-02-14 09:47:49 +00:00
|
|
|
{
|
|
|
|
pic = self->original[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* is there a resize of this size? */
|
|
|
|
for (i = 0; i < self->n_resized; ++i)
|
2009-12-09 19:59:18 +00:00
|
|
|
if ((self->resized[i]->width >= self->resized[i]->height &&
|
|
|
|
self->resized[i]->width == area->width) ||
|
|
|
|
(self->resized[i]->width <= self->resized[i]->height &&
|
|
|
|
self->resized[i]->height == area->height))
|
2008-02-14 09:47:49 +00:00
|
|
|
{
|
|
|
|
gint j;
|
|
|
|
RrImagePic *saved;
|
|
|
|
|
|
|
|
/* save the selected one */
|
|
|
|
saved = self->resized[i];
|
|
|
|
|
|
|
|
/* shift all the others down */
|
|
|
|
for (j = i; j > 0; --j)
|
|
|
|
self->resized[j] = self->resized[j-1];
|
|
|
|
|
|
|
|
/* and move the selected one to the top of the list */
|
|
|
|
self->resized[0] = saved;
|
|
|
|
|
|
|
|
pic = self->resized[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pic) {
|
2008-02-13 03:29:23 +00:00
|
|
|
gdouble aspect;
|
|
|
|
|
2008-02-14 09:47:49 +00:00
|
|
|
/* find an original with a close size */
|
2008-02-13 03:29:23 +00:00
|
|
|
min_diff = min_aspect_diff = -1;
|
|
|
|
min_i = min_aspect_i = 0;
|
|
|
|
aspect = ((gdouble)area->width) / area->height;
|
2008-02-14 09:47:49 +00:00
|
|
|
for (i = 0; i < self->n_original; ++i) {
|
|
|
|
gint diff;
|
|
|
|
gint wdiff, hdiff;
|
2008-02-13 03:29:23 +00:00
|
|
|
gdouble myasp;
|
2008-02-14 09:47:49 +00:00
|
|
|
|
|
|
|
/* our size difference metric.. */
|
|
|
|
wdiff = self->original[i]->width - area->width;
|
2010-01-14 20:03:06 +00:00
|
|
|
if (wdiff < 0) wdiff *= 2; /* prefer scaling down than up */
|
2008-02-14 09:47:49 +00:00
|
|
|
hdiff = self->original[i]->height - area->height;
|
2010-01-14 20:03:06 +00:00
|
|
|
if (hdiff < 0) hdiff *= 2; /* prefer scaling down than up */
|
2008-02-14 09:47:49 +00:00
|
|
|
diff = (wdiff * wdiff) + (hdiff * hdiff);
|
|
|
|
|
2008-02-13 03:29:23 +00:00
|
|
|
/* find the smallest difference */
|
2008-02-14 09:47:49 +00:00
|
|
|
if (min_diff < 0 || diff < min_diff) {
|
|
|
|
min_diff = diff;
|
|
|
|
min_i = i;
|
|
|
|
}
|
2008-02-13 03:29:23 +00:00
|
|
|
/* and also find the smallest difference with the same aspect
|
|
|
|
ratio (and prefer this one) */
|
|
|
|
myasp = ((gdouble)self->original[i]->width) /
|
|
|
|
self->original[i]->height;
|
|
|
|
if (ABS(aspect - myasp) < 0.0000001 &&
|
|
|
|
(min_aspect_diff < 0 || diff < min_aspect_diff))
|
|
|
|
{
|
|
|
|
min_aspect_diff = diff;
|
|
|
|
min_aspect_i = i;
|
|
|
|
}
|
2008-02-14 09:47:49 +00:00
|
|
|
}
|
|
|
|
|
2008-02-13 03:29:23 +00:00
|
|
|
/* use the aspect ratio correct source if there is one */
|
|
|
|
if (min_aspect_i >= 0)
|
|
|
|
min_i = min_aspect_i;
|
|
|
|
|
2008-02-14 09:47:49 +00:00
|
|
|
/* resize the original to the given area */
|
|
|
|
pic = ResizeImage(self->original[min_i]->data,
|
|
|
|
self->original[min_i]->width,
|
|
|
|
self->original[min_i]->height,
|
|
|
|
area->width, area->height);
|
|
|
|
|
|
|
|
/* add the resized image to the image, as the first in the resized
|
|
|
|
list */
|
2008-02-13 13:43:57 +00:00
|
|
|
if (self->n_resized >= self->cache->max_resized_saved)
|
2008-02-14 09:47:49 +00:00
|
|
|
/* remove the last one (last used one) */
|
|
|
|
RemovePicture(self, &self->resized, self->n_resized - 1,
|
|
|
|
&self->n_resized);
|
2008-02-13 13:43:57 +00:00
|
|
|
if (self->cache->max_resized_saved)
|
|
|
|
/* add it to the top of the resized list */
|
|
|
|
AddPicture(self, &self->resized, &self->n_resized, pic);
|
2008-02-14 02:30:18 +00:00
|
|
|
else
|
|
|
|
free_pic = TRUE; /* don't leak mem! */
|
2008-02-14 09:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_assert(pic != NULL);
|
|
|
|
|
|
|
|
DrawRGBA(target, target_w, target_h,
|
|
|
|
pic->data, pic->width, pic->height,
|
|
|
|
img->alpha, area);
|
2008-02-14 02:30:18 +00:00
|
|
|
if (free_pic)
|
|
|
|
RrImagePicFree(pic);
|
2008-02-14 09:47:49 +00:00
|
|
|
}
|