1 #ifndef INKSCAPE_CANVAS_GRID_H
2 #define INKSCAPE_CANVAS_GRID_H
4 /*
5 * Inkscape::CXYGrid
6 *
7 * Generic (and quite unintelligent) grid item for gnome canvas
8 *
9 * Copyright (C) Johan Engelen 2006-2007 <johan@shouraizou.nl>
10 * Copyright (C) Lauris Kaplinski 2000
11 *
12 */
14 #include <cstring>
15 #include <string>
17 #include <gtkmm/box.h>
18 #include <gtkmm.h>
20 #include "display/sp-canvas.h"
21 #include "xml/repr.h"
22 #include "ui/widget/color-picker.h"
23 #include "ui/widget/scalar-unit.h"
24 #include "ui/widget/registered-widget.h"
25 #include "ui/widget/registry.h"
26 #include "xml/node-event-vector.h"
27 #include "snapper.h"
28 #include "line-snapper.h"
30 struct SPDesktop;
31 struct SPNamedView;
32 class SPDocument;
34 namespace Inkscape {
37 enum GridType {
38 GRID_RECTANGULAR = 0,
39 GRID_AXONOMETRIC = 1
40 };
41 #define GRID_MAXTYPENR 1
43 #define INKSCAPE_TYPE_GRID_CANVASITEM (Inkscape::grid_canvasitem_get_type ())
44 #define INKSCAPE_GRID_CANVASITEM(obj) (GTK_CHECK_CAST ((obj), INKSCAPE_TYPE_GRID_CANVASITEM, GridCanvasItem))
45 #define INKSCAPE_GRID_CANVASITEM_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), INKSCAPE_TYPE_GRID_CANVASITEM, GridCanvasItem))
46 #define INKSCAPE_IS_GRID_CANVASITEM(obj) (GTK_CHECK_TYPE ((obj), INKSCAPE_TYPE_GRID_CANVASITEM))
47 #define INKSCAPE_IS_GRID_CANVASITEM_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), INKSCAPE_TYPE_GRID_CANVASITEM))
49 class CanvasGrid;
51 /** \brief All the variables that are tracked for a grid specific
52 canvas item. */
53 struct GridCanvasItem : public SPCanvasItem{
54 CanvasGrid *grid; // the owning grid object
55 };
57 struct GridCanvasItemClass {
58 SPCanvasItemClass parent_class;
59 };
61 /* Standard Gtk function */
62 GtkType grid_canvasitem_get_type (void);
66 class CanvasGrid {
67 public:
68 virtual ~CanvasGrid();
70 // TODO: see effect.h and effect.cpp from live_effects how to link enums to SVGname to typename properly. (johan)
71 const char * getName();
72 const char * getSVGName();
73 GridType getGridType();
74 static const char * getName(GridType type);
75 static const char * getSVGName(GridType type);
76 static GridType getGridTypeFromSVGName(const char * typestr);
77 static GridType getGridTypeFromName(const char * typestr);
79 static CanvasGrid* NewGrid(SPNamedView * nv, Inkscape::XML::Node * repr, SPDocument *doc, GridType gridtype);
80 static void writeNewGridToRepr(Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype);
82 GridCanvasItem * createCanvasItem(SPDesktop * desktop);
84 virtual void Update (NR::Matrix const &affine, unsigned int flags) = 0;
85 virtual void Render (SPCanvasBuf *buf) = 0;
87 virtual void readRepr() = 0;
88 virtual void onReprAttrChanged (Inkscape::XML::Node * /*repr*/, const gchar */*key*/, const gchar */*oldval*/, const gchar */*newval*/, bool /*is_interactive*/) = 0;
90 Gtk::Widget * newWidget();
92 Inkscape::XML::Node * repr;
93 SPDocument *doc;
95 Inkscape::Snapper* snapper;
97 static void on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data);
99 bool isVisible() { return (isEnabled() &&visible); };
100 bool isEnabled();
102 protected:
103 CanvasGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument *in_doc, GridType type);
105 virtual Gtk::Widget * newSpecificWidget() = 0;
107 GSList * canvasitems; // list of created canvasitems
109 SPNamedView * namedview;
111 Inkscape::UI::Widget::Registry _wr;
112 bool visible;
114 GridType gridtype;
116 private:
117 CanvasGrid(const CanvasGrid&);
118 CanvasGrid& operator=(const CanvasGrid&);
119 };
122 class CanvasXYGrid : public CanvasGrid {
123 public:
124 CanvasXYGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc);
125 virtual ~CanvasXYGrid();
127 void Update (NR::Matrix const &affine, unsigned int flags);
128 void Render (SPCanvasBuf *buf);
130 void readRepr();
131 void onReprAttrChanged (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive);
133 NR::Point origin;
134 guint32 color;
135 guint32 empcolor;
136 gint empspacing;
137 SPUnit const* gridunit;
139 NR::Point spacing; /**< Spacing between elements of the grid */
140 bool scaled[2]; /**< Whether the grid is in scaled mode, which can
141 be different in the X or Y direction, hense two
142 variables */
143 NR::Point ow; /**< Transformed origin by the affine for the zoom */
144 NR::Point sw; /**< Transformed spacing by the affine for the zoom */
146 protected:
147 virtual Gtk::Widget * newSpecificWidget();
149 private:
150 CanvasXYGrid(const CanvasXYGrid&);
151 CanvasXYGrid& operator=(const CanvasXYGrid&);
153 void updateWidgets();
155 bool render_dotted;
156 };
160 class CanvasXYGridSnapper : public LineSnapper
161 {
162 public:
163 CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, NR::Coord const d);
164 bool ThisSnapperMightSnap() const;
166 private:
167 LineList _getSnapLines(NR::Point const &p) const;
168 void _addSnappedLine(SnappedConstraints &sc, NR::Point const snapped_point, NR::Coord const snapped_distance, NR::Point const normal_to_line, const NR::Point point_on_line) const;
169 CanvasXYGrid *grid;
170 };
172 }; /* namespace Inkscape */
177 #endif