From aa8e772fa4e7f2196259bebc61da1b09aed76f04 Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Thu, 9 Sep 2010 00:33:59 -0700 Subject: [PATCH] Minor cleanup. --- src/sp-cursor.cpp | 119 ++++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 51 deletions(-) diff --git a/src/sp-cursor.cpp b/src/sp-cursor.cpp index 5ec80c9f6..cc52f3c97 100644 --- a/src/sp-cursor.cpp +++ b/src/sp-cursor.cpp @@ -1,13 +1,15 @@ -#define __SP_CURSOR_C__ - /* * Some convenience stuff * * Authors: * Lauris Kaplinski + * Jasper van de Gronde + * Jon A. Cruz * * Copyright (C) 1999-2002 authors * Copyright (C) 2001-2002 Ximian, Inc. + * Copyright (C) 2010 Jasper van de Gronde + * Copyright (C) 2010 Jon A. Cruz * * Released under GNU GPL, read the file 'COPYING' for more information */ @@ -20,24 +22,23 @@ #include "color.h" #include "sp-cursor.h" -void -sp_cursor_bitmap_and_mask_from_xpm(GdkBitmap **bitmap, GdkBitmap **mask, gchar const *const *xpm) +void sp_cursor_bitmap_and_mask_from_xpm(GdkBitmap **bitmap, GdkBitmap **mask, gchar const *const *xpm) { - int height; - int width; - int colors; - int pix; + int height = 0; + int width = 0; + int colors = 0; + int pix = 0; sscanf(xpm[0], "%d %d %d %d", &height, &width, &colors, &pix); - g_return_if_fail (height == 32); - g_return_if_fail (width == 32); - g_return_if_fail (colors >= 3); + g_return_if_fail(height == 32); + g_return_if_fail(width == 32); + g_return_if_fail(colors >= 3); int transparent_color = ' '; std::string black_colors; - char pixmap_buffer[(32 * 32)/8]; - char mask_buffer[(32 * 32)/8]; + char pixmap_buffer[(32 * 32) / 8] = {0}; + char mask_buffer[(32 * 32) / 8] = {0}; for (int i = 0; i < colors; i++) { @@ -68,22 +69,21 @@ sp_cursor_bitmap_and_mask_from_xpm(GdkBitmap **bitmap, GdkBitmap **mask, gchar c for (int y = 0; y < 32; y++) { for (int x = 0; x < 32; ) { - char value = 0; char maskv = 0; for (int pix = 0; pix < 8; pix++, x++){ - if (xpm[1+colors+y][x] != transparent_color) { + if (xpm[1 + colors + y][x] != transparent_color) { maskv |= 1 << pix; - if (black_colors.find(xpm[1+colors+y][x]) != std::string::npos) { + if (black_colors.find(xpm[1 + colors + y][x]) != std::string::npos) { value |= 1 << pix; } } } - pixmap_buffer[(y * 4 + x/8)-1] = value; - mask_buffer[(y * 4 + x/8)-1] = maskv; + pixmap_buffer[(y * 4 + x / 8) - 1] = value; + mask_buffer[(y * 4 + x / 8) - 1] = maskv; } } @@ -97,25 +97,43 @@ static void free_cursor_data(guchar *pixels, gpointer data) { struct RGBA { guchar v[4]; - RGBA() { v[0] = 0; v[1] = 0; v[2] = 0; v[3] = 0; } - RGBA(guchar r, guchar g, guchar b, guchar a) { v[0] = r; v[1] = g; v[2] = b; v[3] = a; } - operator guint32 const () const { return *reinterpret_cast(v); } + + RGBA() { + v[0] = 0; + v[1] = 0; + v[2] = 0; + v[3] = 0; + } + + RGBA(guchar r, guchar g, guchar b, guchar a) { + v[0] = r; + v[1] = g; + v[2] = b; + v[3] = a; + } + + operator guint32() const { + guint32 result = (static_cast(v[0]) << 0) + | (static_cast(v[1]) << 8) + | (static_cast(v[2]) << 16) + | (static_cast(v[3]) << 24); + return result; + } }; -GdkPixbuf* -sp_cursor_pixbuf_from_xpm(gchar const *const *xpm, GdkColor const& black, GdkColor const& white, guint32 fill, guint32 stroke) +GdkPixbuf *sp_cursor_pixbuf_from_xpm(gchar const *const *xpm, GdkColor const& black, GdkColor const& white, guint32 fill, guint32 stroke) { - int height; - int width; - int colors; - int pix; + int height = 0; + int width = 0; + int colors = 0; + int pix = 0; sscanf(xpm[0], "%d %d %d %d", &height, &width, &colors, &pix); //g_return_if_fail (height == 32); //g_return_if_fail (width == 32); //g_return_if_fail (colors >= 3); - std::map colorMap; + std::map colorMap; for (int i = 0; i < colors; i++) { @@ -132,17 +150,17 @@ sp_cursor_pixbuf_from_xpm(gchar const *const *xpm, GdkColor const& black, GdkCol } if (strcmp(p, "None") == 0) { - colorMap.insert(std::make_pair(ccode, RGBA())); + colorMap[ccode] = RGBA(); } else if (strcmp(p, "Fill") == 0) { - colorMap.insert(std::make_pair(ccode, RGBA(SP_RGBA32_R_U(fill),SP_RGBA32_G_U(fill),SP_RGBA32_B_U(fill),SP_RGBA32_A_U(fill)))); + colorMap[ccode] = RGBA(SP_RGBA32_R_U(fill), SP_RGBA32_G_U(fill), SP_RGBA32_B_U(fill), SP_RGBA32_A_U(fill)); } else if (strcmp(p, "Stroke") == 0) { - colorMap.insert(std::make_pair(ccode, RGBA(SP_RGBA32_R_U(stroke),SP_RGBA32_G_U(stroke),SP_RGBA32_B_U(stroke),SP_RGBA32_A_U(stroke)))); + colorMap[ccode] = RGBA(SP_RGBA32_R_U(stroke), SP_RGBA32_G_U(stroke), SP_RGBA32_B_U(stroke), SP_RGBA32_A_U(stroke)); } else if (strcmp(p, "#000000") == 0) { - colorMap.insert(std::make_pair(ccode, RGBA(black.red,black.green,black.blue,255))); + colorMap[ccode] = RGBA(black.red, black.green, black.blue, 255); } else if (strcmp(p, "#FFFFFF") == 0) { - colorMap.insert(std::make_pair(ccode, RGBA(white.red,white.green,white.blue,255))); + colorMap[ccode] = RGBA(white.red, white.green, white.blue, 255); } else { - colorMap.insert(std::make_pair(ccode, RGBA())); + colorMap[ccode] = RGBA(); } } @@ -150,34 +168,33 @@ sp_cursor_pixbuf_from_xpm(gchar const *const *xpm, GdkColor const& black, GdkCol for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { - std::map::const_iterator it = colorMap.find(xpm[1+colors+y][x]); - pixmap_buffer[y * width + x] = it==colorMap.end() ? 0u : it->second; + std::map::const_iterator it = colorMap.find(xpm[1 + colors + y][x]); + pixmap_buffer[y * width + x] = (it == colorMap.end()) ? 0u : it->second; } } - return gdk_pixbuf_new_from_data(reinterpret_cast(pixmap_buffer), GDK_COLORSPACE_RGB, TRUE, 8, width, height, width*sizeof(guint32), free_cursor_data, NULL); + return gdk_pixbuf_new_from_data(reinterpret_cast(pixmap_buffer), GDK_COLORSPACE_RGB, TRUE, 8, width, height, width * sizeof(guint32), free_cursor_data, NULL); } -GdkCursor * -sp_cursor_new_from_xpm(gchar const *const *xpm, gint hot_x, gint hot_y) +GdkCursor *sp_cursor_new_from_xpm(gchar const *const *xpm, gint hot_x, gint hot_y) { + GdkCursor *cursor = 0; GdkColor const fg = { 0, 0, 0, 0 }; GdkColor const bg = { 0, 65535, 65535, 65535 }; - GdkBitmap *bitmap = NULL; - GdkBitmap *mask = NULL; - - sp_cursor_bitmap_and_mask_from_xpm (&bitmap, &mask, xpm); - if ( bitmap != NULL && mask != NULL ) { - GdkCursor *new_cursor = gdk_cursor_new_from_pixmap (bitmap, mask, - &fg, &bg, - hot_x, hot_y); - g_object_unref (bitmap); - g_object_unref (mask); - return new_cursor; + GdkBitmap *bitmap = 0; + GdkBitmap *mask = 0; + + sp_cursor_bitmap_and_mask_from_xpm(&bitmap, &mask, xpm); + if ( bitmap && mask ) { + cursor = gdk_cursor_new_from_pixmap(bitmap, mask, + &fg, &bg, + hot_x, hot_y); + g_object_unref(bitmap); + g_object_unref(mask); } - return NULL; + return cursor; } /* -- 2.30.2