summaryrefslogtreecommitdiff
path: root/src/sheet/grid.c
diff options
context:
space:
mode:
authorRuben Undheim <ruben.undheim@gmail.com>2018-07-13 06:17:58 +0000
committerRuben Undheim <ruben.undheim@gmail.com>2018-07-13 06:17:58 +0000
commit9e0bd16a997e55d6b9c2e80734ea8e61794c7602 (patch)
treed73377e0368b0c730d5a14019b1eee53897505bf /src/sheet/grid.c
parente1fffcb07ce0d8b0db9e0b4b5e1e0c1128197af5 (diff)
New upstream version 0.84.22
Diffstat (limited to 'src/sheet/grid.c')
-rw-r--r--src/sheet/grid.c156
1 files changed, 68 insertions, 88 deletions
diff --git a/src/sheet/grid.c b/src/sheet/grid.c
index 63cbd8c..f2db618 100644
--- a/src/sheet/grid.c
+++ b/src/sheet/grid.c
@@ -6,12 +6,16 @@
* Ricardo Markiewicz <rmarkie@fi.uba.ar>
* Andres de Barbara <adebarbara@fi.uba.ar>
* Marc Lorber <lorber.marc@wanadoo.fr>
+ * Bernhard Schuster <bernhard@ahoi.io>
+ * Guido Trentalancia <guido@trentalancia.com>
*
* Web page: https://github.com/marc-lorber/oregano
*
* Copyright (C) 1999-2001 Richard Hult
* Copyright (C) 2003,2006 Ricardo Markiewicz
* Copyright (C) 2009-2012 Marc Lorber
+ * Copyright (C) 2013-2014 Bernhard Schuster
+ * Copyright (C) 2017 Guido Trentalancia
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@@ -25,32 +29,27 @@
*
* 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.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
*/
#include <math.h>
#include "grid.h"
+#define ROUND(x) (floor ((x)+0.5))
+#include "debug.h"
-#define ROUND(x) (floor((x)+0.5))
-#define NG_DEBUG(s) if (0) g_print ("%s\n", s)
+enum { ARG_0, ARG_COLOR, ARG_SPACING, ARG_SNAP };
-enum {
- ARG_0,
- ARG_COLOR,
- ARG_SPACING,
- ARG_SNAP
-};
-
-struct _GridPriv{
- GooCanvasItem * canvas_grid;
- guint snap;
- GdkColor color;
- gdouble spacing;
- gdouble cached_zoom;
- cairo_t * cairo;
+struct _GridPriv
+{
+ GooCanvasItem *canvas_grid;
+ guint snap;
+ GdkRGBA color;
+ gdouble spacing;
+ gdouble cached_zoom;
+ cairo_t *cairo;
};
G_DEFINE_TYPE (Grid, grid, GOO_TYPE_CANVAS_GROUP)
@@ -58,14 +57,12 @@ G_DEFINE_TYPE (Grid, grid, GOO_TYPE_CANVAS_GROUP)
static void grid_class_init (GridClass *class);
static void grid_init (Grid *grid);
static void grid_finalize (GObject *object);
-static void grid_set_property (GObject *object, guint prop_id,
- const GValue *value, GParamSpec *spec);
-static void grid_get_property (GObject *object, guint prop_id, GValue *value,
- GParamSpec *spec);
+static void grid_set_property (GObject *object, guint prop_id, const GValue *value,
+ GParamSpec *spec);
+static void grid_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *spec);
static void grid_dispose (GObject *object);
-static void
-grid_class_init (GridClass *class)
+static void grid_class_init (GridClass *class)
{
GObjectClass *object_class;
@@ -73,27 +70,26 @@ grid_class_init (GridClass *class)
grid_parent_class = g_type_class_peek_parent (class);
object_class->dispose = grid_dispose;
- object_class->finalize = grid_finalize;
+ object_class->finalize = grid_finalize;
object_class->set_property = grid_set_property;
object_class->get_property = grid_get_property;
- g_object_class_install_property (object_class, ARG_COLOR,
- g_param_spec_string ("color", "Grid::color", "the color",
- "black", G_PARAM_WRITABLE));
-
+ g_object_class_install_property (
+ object_class, ARG_COLOR,
+ g_param_spec_string ("color", "Grid::color", "the color", "black", G_PARAM_WRITABLE));
+
g_object_class_install_property (object_class, ARG_SPACING,
- g_param_spec_double ("spacing", "Grid::spacing",
- "the grid spacing", 0.0f, 100.0f, 10.0f,
- G_PARAM_READWRITE));
-
- g_object_class_install_property (object_class, ARG_SNAP,
- g_param_spec_boolean ("snap", "Grid::snap", "snap to grid?",
- TRUE,G_PARAM_READWRITE));
+ g_param_spec_double ("spacing", "Grid::spacing",
+ "the grid spacing", 0.0f, 100.0f, 10.0f,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (
+ object_class, ARG_SNAP,
+ g_param_spec_boolean ("snap", "Grid::snap", "snap to grid?", TRUE, G_PARAM_READWRITE));
}
-static void
-grid_init (Grid *grid)
+static void grid_init (Grid *grid)
{
GridPriv *priv;
@@ -105,14 +101,9 @@ grid_init (Grid *grid)
priv->snap = TRUE;
}
-static void
-grid_dispose (GObject *object)
-{
- G_OBJECT_CLASS (grid_parent_class)->dispose (object);
-}
+static void grid_dispose (GObject *object) { G_OBJECT_CLASS (grid_parent_class)->dispose (object); }
-static void
-grid_finalize (GObject *object)
+static void grid_finalize (GObject *object)
{
Grid *grid;
@@ -122,9 +113,8 @@ grid_finalize (GObject *object)
G_OBJECT_CLASS (grid_parent_class)->finalize (object);
}
-static void
-grid_set_property (GObject *object, guint prop_id, const GValue *value,
- GParamSpec *spec)
+static void grid_set_property (GObject *object, guint prop_id, const GValue *value,
+ GParamSpec *spec)
{
Grid *grid;
GridPriv *priv;
@@ -146,9 +136,7 @@ grid_set_property (GObject *object, guint prop_id, const GValue *value,
}
}
-static void
-grid_get_property (GObject *object, guint prop_id, GValue *value,
- GParamSpec *spec)
+static void grid_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *spec)
{
Grid *grid;
GridPriv *priv;
@@ -169,39 +157,25 @@ grid_get_property (GObject *object, guint prop_id, GValue *value,
}
}
-Grid *
-grid_create (GooCanvasItem *root, gdouble width, gdouble height)
+Grid *grid_new (GooCanvasItem *root, gdouble width, gdouble height)
{
- Grid * grid = NULL;
+ Grid *grid = NULL;
grid = g_object_new (TYPE_GRID, NULL);
- g_object_set (G_OBJECT (grid),
- "parent", root,
- NULL);
-
- grid->priv->canvas_grid = goo_canvas_grid_new (GOO_CANVAS_ITEM (grid),
- 0.0,
- 0.0,
- width,
- height,
- 10.0,
- 10.0,
- 0.0,
- 0.0,
- "horz-grid-line-width", 0.05,
- "horz-grid-line-color", "dark gray",
- "vert-grid-line-width", 0.05,
- "vert-grid-line-color", "dark gray",
- NULL);
-
+ g_object_set (G_OBJECT (grid), "parent", root, NULL);
+
+ grid->priv->canvas_grid = goo_canvas_grid_new (
+ GOO_CANVAS_ITEM (grid), 0.0, 0.0, width, height, 10.0, 10.0, 0.0, 0.0,
+ "horz-grid-line-width", 0.05, "horz-grid-line-color", "dark gray", "vert-grid-line-width",
+ 0.05, "vert-grid-line-color", "dark gray", NULL);
+
grid_show (grid, TRUE);
grid_snap (grid, TRUE);
return grid;
}
-inline void
-snap_to_grid (Grid *grid, gdouble *x, gdouble *y)
+inline gboolean snap_to_grid (Grid *grid, gdouble *x, gdouble *y)
{
GridPriv *priv;
gdouble spacing;
@@ -209,27 +183,33 @@ snap_to_grid (Grid *grid, gdouble *x, gdouble *y)
priv = grid->priv;
spacing = priv->spacing;
- if (priv->snap) {
- if (x) *x = ROUND ((*x) / spacing) * spacing;
- if (y) *y = ROUND ((*y) / spacing) * spacing;
+ Coords old = {0., 0.};
+ gboolean moved = FALSE;
+
+ if (G_LIKELY (priv->snap)) {
+ if (G_LIKELY (x)) {
+ old.x = *x;
+ *x = ROUND ((*x) / spacing) * spacing;
+ moved = moved || (fabs ((*x) - old.x) > COORDS_DELTA);
+ }
+ if (G_LIKELY (y)) {
+ old.y = *y;
+ *y = ROUND ((*y) / spacing) * spacing;
+ moved = moved || (fabs ((*y) - old.y) > COORDS_DELTA);
+ }
}
+ return moved;
}
-void
-grid_show (Grid *grid, gboolean show)
+void grid_show (Grid *grid, gboolean show)
{
if (show)
- g_object_set (G_OBJECT (grid),
- "visibility", GOO_CANVAS_ITEM_VISIBLE,
- NULL);
+ g_object_set (G_OBJECT (grid), "visibility", GOO_CANVAS_ITEM_VISIBLE, NULL);
else
- g_object_set (G_OBJECT (grid),
- "visibility", GOO_CANVAS_ITEM_INVISIBLE,
- NULL);
+ g_object_set (G_OBJECT (grid), "visibility", GOO_CANVAS_ITEM_INVISIBLE, NULL);
}
-void
-grid_snap (Grid *grid, gboolean snap)
+void grid_snap (Grid *grid, gboolean snap)
{
GridPriv *priv;