1 /** @file
2 * @brief Cartesian grid item for the Inkscape canvas
3 */
4 /* Copyright (C) Johan Engelen 2006-2007 <johan@shouraizou.nl>
5 * Copyright (C) Lauris Kaplinski 2000
6 */
8 #ifndef INKSCAPE_CANVAS_GRID_H
9 #define INKSCAPE_CANVAS_GRID_H
11 #include <cstring>
12 #include <string>
14 #include <gtkmm/box.h>
15 #include <gtkmm.h>
17 #include "sp-canvas-item.h"
18 #include "xml/repr.h"
19 #include "ui/widget/color-picker.h"
20 #include "ui/widget/scalar-unit.h"
21 #include "ui/widget/registered-widget.h"
22 #include "ui/widget/registry.h"
23 #include "xml/node-event-vector.h"
24 #include "snapper.h"
25 #include "line-snapper.h"
27 struct SPDesktop;
28 struct SPNamedView;
29 struct SPCanvasBuf;
30 class SPDocument;
32 namespace Inkscape {
35 enum GridType {
36 GRID_RECTANGULAR = 0,
37 GRID_AXONOMETRIC = 1
38 };
39 #define GRID_MAXTYPENR 1
41 #define INKSCAPE_TYPE_GRID_CANVASITEM (Inkscape::grid_canvasitem_get_type ())
42 #define INKSCAPE_GRID_CANVASITEM(obj) (GTK_CHECK_CAST ((obj), INKSCAPE_TYPE_GRID_CANVASITEM, GridCanvasItem))
43 #define INKSCAPE_GRID_CANVASITEM_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), INKSCAPE_TYPE_GRID_CANVASITEM, GridCanvasItem))
44 #define INKSCAPE_IS_GRID_CANVASITEM(obj) (GTK_CHECK_TYPE ((obj), INKSCAPE_TYPE_GRID_CANVASITEM))
45 #define INKSCAPE_IS_GRID_CANVASITEM_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), INKSCAPE_TYPE_GRID_CANVASITEM))
47 class CanvasGrid;
49 /** \brief All the variables that are tracked for a grid specific
50 canvas item. */
51 struct GridCanvasItem : public SPCanvasItem{
52 CanvasGrid *grid; // the owning grid object
53 };
55 struct GridCanvasItemClass {
56 SPCanvasItemClass parent_class;
57 };
59 /* Standard Gtk function */
60 GtkType grid_canvasitem_get_type (void);
64 class CanvasGrid {
65 public:
66 virtual ~CanvasGrid();
68 // TODO: see effect.h and effect.cpp from live_effects how to link enums to SVGname to typename properly. (johan)
69 const char * getName();
70 const char * getSVGName();
71 GridType getGridType();
72 static const char * getName(GridType type);
73 static const char * getSVGName(GridType type);
74 static GridType getGridTypeFromSVGName(const char * typestr);
75 static GridType getGridTypeFromName(const char * typestr);
77 static CanvasGrid* NewGrid(SPNamedView * nv, Inkscape::XML::Node * repr, SPDocument *doc, GridType gridtype);
78 static void writeNewGridToRepr(Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype);
80 GridCanvasItem * createCanvasItem(SPDesktop * desktop);
82 virtual void Update (Geom::Matrix const &affine, unsigned int flags) = 0;
83 virtual void Render (SPCanvasBuf *buf) = 0;
85 virtual void readRepr() = 0;
86 virtual void onReprAttrChanged (Inkscape::XML::Node * /*repr*/, const gchar */*key*/, const gchar */*oldval*/, const gchar */*newval*/, bool /*is_interactive*/) = 0;
88 Gtk::Widget * newWidget();
90 Geom::Point origin; /**< Origin of the grid */
91 guint32 color; /**< Color for normal lines */
92 guint32 empcolor; /**< Color for emphasis lines */
93 gint empspacing; /**< Spacing between emphasis lines */
95 SPUnit const* gridunit;
97 Inkscape::XML::Node * repr;
98 SPDocument *doc;
100 Inkscape::Snapper* snapper;
102 static void on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data);
104 bool isVisible() { return (isEnabled() &&visible); };
105 bool isEnabled();
107 protected:
108 CanvasGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument *in_doc, GridType type);
110 virtual Gtk::Widget * newSpecificWidget() = 0;
112 GSList * canvasitems; // list of created canvasitems
114 SPNamedView * namedview;
116 Inkscape::UI::Widget::Registry _wr;
117 bool visible;
119 GridType gridtype;
121 private:
122 CanvasGrid(const CanvasGrid&);
123 CanvasGrid& operator=(const CanvasGrid&);
124 };
127 class CanvasXYGrid : public CanvasGrid {
128 public:
129 CanvasXYGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc);
130 virtual ~CanvasXYGrid();
132 void Update (Geom::Matrix const &affine, unsigned int flags);
133 void Render (SPCanvasBuf *buf);
135 void readRepr();
136 void onReprAttrChanged (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive);
138 Geom::Point spacing; /**< Spacing between elements of the grid */
139 bool scaled[2]; /**< Whether the grid is in scaled mode, which can
140 be different in the X or Y direction, hence two
141 variables */
142 Geom::Point ow; /**< Transformed origin by the affine for the zoom */
143 Geom::Point sw; /**< Transformed spacing by the affine for the zoom */
145 protected:
146 virtual Gtk::Widget * newSpecificWidget();
148 private:
149 CanvasXYGrid(const CanvasXYGrid&);
150 CanvasXYGrid& operator=(const CanvasXYGrid&);
152 void updateWidgets();
154 bool render_dotted;
155 };
159 class CanvasXYGridSnapper : public LineSnapper
160 {
161 public:
162 CanvasXYGridSnapper(CanvasXYGrid *grid, SnapManager *sm, Geom::Coord const d);
163 bool ThisSnapperMightSnap() const;
165 Geom::Coord getSnapperTolerance() const; //returns the tolerance of the snapper in screen pixels (i.e. independent of zoom)
166 bool getSnapperAlwaysSnap() const; //if true, then the snapper will always snap, regardless of its tolerance
168 private:
169 LineList _getSnapLines(Geom::Point const &p) const;
170 void _addSnappedLine(SnappedConstraints &sc, Geom::Point const snapped_point, Geom::Coord const snapped_distance, SnapSourceType const &source, long source_num, Geom::Point const normal_to_line, const Geom::Point point_on_line) const;
171 void _addSnappedPoint(SnappedConstraints &sc, Geom::Point const snapped_point, Geom::Coord const snapped_distance, SnapSourceType const &source, long source_num, bool constrained_snap) const;
172 CanvasXYGrid *grid;
173 };
175 }; /* namespace Inkscape */
180 #endif