Bug 421977 - nsGNOMEShellService::GetDesktopBackgroundColor should support GConf's actual format. r=gavin

This commit is contained in:
Christopher A. Aillon
2008-09-08 12:06:32 +02:00
parent a7c6d07217
commit b1d5664983
3 changed files with 145 additions and 63 deletions

View File

@@ -63,6 +63,8 @@
#include <glib.h>
#include <glib-object.h>
#include <gtk/gtkversion.h>
#include <gdk/gdk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <limits.h>
#include <stdlib.h>
@@ -446,58 +448,8 @@ nsGNOMEShellService::SetDesktopBackground(nsIDOMElement* aElement,
return rv;
}
// In: pointer to two characters CC
// Out: parsed color number
static PRUint8
HexToNum(char ch)
{
if ('0' <= ch && '9' >= ch)
return ch - '0';
if ('A' <= ch && 'F' >= ch)
return ch - 'A';
if ('a' <= ch && 'f' >= ch)
return ch - 'a';
return 0;
}
// In: 3 or 6-character RRGGBB hex string
// Out: component colors
static PRBool
HexToRGB(const nsCString& aColorSpec,
PRUint8 &aRed,
PRUint8 &aGreen,
PRUint8 &aBlue)
{
const char *buf = aColorSpec.get();
if (aColorSpec.Length() == 6) {
aRed = HexToNum(buf[0]) >> 4 |
HexToNum(buf[1]);
aGreen = HexToNum(buf[2]) >> 4 |
HexToNum(buf[3]);
aBlue = HexToNum(buf[4]) >> 4 |
HexToNum(buf[5]);
return PR_TRUE;
}
if (aColorSpec.Length() == 3) {
aRed = HexToNum(buf[0]);
aGreen = HexToNum(buf[1]);
aBlue = HexToNum(buf[2]);
aRed |= aRed >> 4;
aGreen |= aGreen >> 4;
aBlue |= aBlue >> 4;
return PR_TRUE;
}
return PR_FALSE;
}
#define COLOR_16_TO_8_BIT(_c) ((_c) >> 8)
#define COLOR_8_TO_16_BIT(_c) ((_c) << 8)
NS_IMETHODIMP
nsGNOMEShellService::GetDesktopBackgroundColor(PRUint32 *aColor)
@@ -512,22 +464,34 @@ nsGNOMEShellService::GetDesktopBackgroundColor(PRUint32 *aColor)
return NS_OK;
}
// Chop off the leading '#' character
background.Cut(0, 1);
GdkColor color;
gboolean success = gdk_color_parse(background.get(), &color);
PRUint8 red, green, blue;
if (!HexToRGB(background, red, green, blue))
return NS_ERROR_FAILURE;
NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
// The result must be in RGB order with the high 8 bits zero.
*aColor = (red << 16 | green << 8 | blue);
*aColor = COLOR_16_TO_8_BIT(color.red) << 16 |
COLOR_16_TO_8_BIT(color.green) << 8 |
COLOR_16_TO_8_BIT(color.blue);
return NS_OK;
}
static void
ColorToHex(PRUint32 aColor, nsCString& aResult)
ColorToCString(PRUint32 aColor, nsCString& aResult)
{
char *buf = aResult.BeginWriting(7);
#if GTK_CHECK_VERSION(2,12,0)
GdkColor color;
color.red = COLOR_8_TO_16_BIT(aColor >> 16);
color.green = COLOR_8_TO_16_BIT((aColor >> 8) & 0xff);
color.blue = COLOR_8_TO_16_BIT(aColor & 0xff);
gchar *colorString = gdk_color_to_string(&color);
aResult.Assign(colorString);
g_free(colorString);
#else // GTK 2.12.0
// The #rrrrggggbbbb format is used to match gdk_color_to_string()
char *buf = aResult.BeginWriting(13);
if (!buf)
return;
@@ -535,7 +499,8 @@ ColorToHex(PRUint32 aColor, nsCString& aResult)
PRUint8 green = (aColor >> 8) & 0xff;
PRUint8 blue = aColor & 0xff;
PR_snprintf(buf, 8, "#%02x%02x%02x", red, green, blue);
PR_snprintf(buf, 14, "#%02x00%02x00%02x00", red, green, blue);
#endif // GTK 2.12.0
}
NS_IMETHODIMP
@@ -544,7 +509,7 @@ nsGNOMEShellService::SetDesktopBackgroundColor(PRUint32 aColor)
nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
nsCString colorString;
ColorToHex(aColor, colorString);
ColorToCString(aColor, colorString);
gconf->SetString(NS_LITERAL_CSTRING(kDesktopColorKey), colorString);