/* * Copyright (C) 2008, Red Hat, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #include "sugar-grid.h" enum { PROP_0, PROP_WIDTH, PROP_HEIGHT, }; struct _SugarGrid { GObject base_instance; SugarGridPrivate *priv; }; struct _SugarGridClass { GObjectClass base_class; }; struct _SugarGridPrivate { guint width; guint height; guchar *weights; }; G_DEFINE_TYPE(SugarGrid, sugar_grid, G_TYPE_OBJECT) void sugar_grid_setup(SugarGrid *grid, gint width, gint height) { g_free(grid->priv->weights); grid->priv->weights = g_new0(guchar, width * height); grid->priv->width = width; grid->priv->height = height; } static gboolean check_bounds(SugarGrid *grid, GdkRectangle *rect) { return (grid->priv->weights != NULL && grid->priv->width >= rect->x + rect->width && grid->priv->height >= rect->y + rect->height); } void sugar_grid_add_weight(SugarGrid *grid, GdkRectangle *rect) { int i, k; if (!check_bounds(grid, rect)) { g_warning("Trying to add weight outside the grid bounds."); return; } for (k = rect->y; k < rect->y + rect->height; k++) { for (i = rect->x; i < rect->x + rect->width; i++) { grid->priv->weights[i + k * grid->priv->width] += 1; } } } void sugar_grid_remove_weight(SugarGrid *grid, GdkRectangle *rect) { int i, k; if (!check_bounds(grid, rect)) { g_warning("Trying to remove weight outside the grid bounds."); return; } for (k = rect->y; k < rect->y + rect->height; k++) { for (i = rect->x; i < rect->x + rect->width; i++) { grid->priv->weights[i + k * grid->priv->width] -= 1; } } } guint sugar_grid_compute_weight(SugarGrid *grid, GdkRectangle *rect) { int i, k, sum = 0; if (!check_bounds(grid, rect)) { g_warning("Trying to compute weight outside the grid bounds."); return 0; } for (k = rect->y; k < rect->y + rect->height; k++) { for (i = rect->x; i < rect->x + rect->width; i++) { sum += grid->priv->weights[i + k * grid->priv->width]; } } return sum; } static void sugar_grid_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { SugarGrid *self = SUGAR_GRID (object); switch (property_id) { case PROP_WIDTH: g_value_set_uint ((GValue *)value, self->priv->width); break; case PROP_HEIGHT: g_value_set_uint ((GValue *)value, self->priv->height); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; } } static void sugar_grid_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { SugarGrid *self = SUGAR_GRID (object); switch (property_id) { case PROP_WIDTH: g_value_set_uint (value, self->priv->width); break; case PROP_HEIGHT: g_value_set_uint (value, self->priv->height); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; } } static void sugar_grid_finalize(GObject *object) { SugarGrid *grid = SUGAR_GRID(object); g_free(grid->priv->weights); } static void sugar_grid_class_init(SugarGridClass *grid_class) { GObjectClass *gobject_class; GParamSpec *pspec; gobject_class = G_OBJECT_CLASS(grid_class); gobject_class->finalize = sugar_grid_finalize; gobject_class->set_property = sugar_grid_set_property; gobject_class->get_property = sugar_grid_get_property; g_type_class_add_private (grid_class, sizeof (SugarGridPrivate)); pspec = g_param_spec_uint ("width", NULL, "Width of the Grid", 0, G_MAXUINT, 0, G_PARAM_READWRITE); g_object_class_install_property (gobject_class, PROP_WIDTH, pspec); pspec = g_param_spec_uint ("height", NULL, "Height of the Grid", 0, G_MAXUINT, 0, G_PARAM_READWRITE); g_object_class_install_property (gobject_class, PROP_HEIGHT, pspec); } static void sugar_grid_init(SugarGrid *grid) { grid->priv = G_TYPE_INSTANCE_GET_PRIVATE (grid, SUGAR_TYPE_GRID, SugarGridPrivate); grid->priv->width = 0; grid->priv->height = 0; grid->priv->weights = NULL; }