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 <display/sp-canvas.h>
15 #include "xml/repr.h"
16 #include <gtkmm/box.h>
19 #include <gtkmm.h>
20 #include "ui/widget/color-picker.h"
21 #include "ui/widget/scalar-unit.h"
23 #include "ui/widget/registered-widget.h"
24 #include "ui/widget/registry.h"
25 #include "ui/widget/tolerance-slider.h"
27 #include "xml/node-event-vector.h"
29 #include "snapper.h"
30 #include "line-snapper.h"
32 struct SPDesktop;
33 struct SPNamedView;
34 class SPDocument;
36 namespace Inkscape {
39 enum GridType {
40 GRID_RECTANGULAR = 0,
41 GRID_AXONOMETRIC = 1
42 };
43 #define GRID_MAXTYPENR 1
45 #define INKSCAPE_TYPE_GRID_CANVASITEM (Inkscape::grid_canvasitem_get_type ())
46 #define INKSCAPE_GRID_CANVASITEM(obj) (GTK_CHECK_CAST ((obj), INKSCAPE_TYPE_GRID_CANVASITEM, GridCanvasItem))
47 #define INKSCAPE_GRID_CANVASITEM_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), INKSCAPE_TYPE_GRID_CANVASITEM, GridCanvasItem))
48 #define INKSCAPE_IS_GRID_CANVASITEM(obj) (GTK_CHECK_TYPE ((obj), INKSCAPE_TYPE_GRID_CANVASITEM))
49 #define INKSCAPE_IS_GRID_CANVASITEM_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), INKSCAPE_TYPE_GRID_CANVASITEM))
51 class CanvasGrid;
53 /** \brief All the variables that are tracked for a grid specific
54 canvas item. */
55 struct GridCanvasItem : public SPCanvasItem{
56 CanvasGrid *grid; // the owning grid object
57 };
59 struct GridCanvasItemClass {
60 SPCanvasItemClass parent_class;
61 };
63 /* Standard Gtk function */
64 GtkType grid_canvasitem_get_type (void);
68 class CanvasGrid {
69 public:
70 virtual ~CanvasGrid();
72 // TODO: see effect.h and effect.cpp from live_effects how to link enums to SVGname to typename properly. (johan)
73 const char * getName();
74 const char * getSVGName();
75 GridType getGridType();
76 static const char * getName(GridType type);
77 static const char * getSVGName(GridType type);
78 static GridType getGridTypeFromSVGName(const char * typestr);
79 static GridType getGridTypeFromName(const char * typestr);
81 static CanvasGrid* NewGrid(SPNamedView * nv, Inkscape::XML::Node * repr, SPDocument *doc, GridType gridtype);
82 static void writeNewGridToRepr(Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype);
84 GridCanvasItem * createCanvasItem(SPDesktop * desktop);
86 virtual void Update (NR::Matrix const &affine, unsigned int flags) = 0;
87 virtual void Render (SPCanvasBuf *buf) = 0;
89 virtual void readRepr() = 0;
90 virtual void onReprAttrChanged (Inkscape::XML::Node * /*repr*/, const gchar */*key*/, const gchar */*oldval*/, const gchar */*newval*/, bool /*is_interactive*/) = 0;
92 Gtk::Widget * newWidget();
94 Inkscape::XML::Node * repr;
95 SPDocument *doc;
97 Inkscape::Snapper* snapper;
99 static void on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data);
101 bool isVisible() { return (isEnabled() &&visible); };
102 bool isEnabled();
104 protected:
105 CanvasGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument *in_doc, GridType type);
107 virtual Gtk::Widget * newSpecificWidget() = 0;
109 GSList * canvasitems; // list of created canvasitems
111 SPNamedView * namedview;
113 Inkscape::UI::Widget::Registry _wr;
114 bool visible;
116 GridType gridtype;
118 private:
119 CanvasGrid(const CanvasGrid&);
120 CanvasGrid& operator=(const CanvasGrid&);
121 };
124 class CanvasXYGrid : public CanvasGrid {
125 public:
126 CanvasXYGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc);
127 virtual ~CanvasXYGrid();
129 void Update (NR::Matrix const &affine, unsigned int flags);
130 void Render (SPCanvasBuf *buf);
132 void readRepr();
133 void onReprAttrChanged (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive);
135 NR::Point origin;
136 guint32 color;
137 guint32 empcolor;
138 gint empspacing;
139 SPUnit const* gridunit;
141 NR::Point spacing; /**< Spacing between elements of the grid */
142 bool scaled[2]; /**< Whether the grid is in scaled mode, which can
143 be different in the X or Y direction, hense two
144 variables */
145 NR::Point ow; /**< Transformed origin by the affine for the zoom */
146 NR::Point sw; /**< Transformed spacing by the affine for the zoom */
148 protected:
149 virtual Gtk::Widget * newSpecificWidget();
151 private:
152 CanvasXYGrid(const CanvasXYGrid&);
153 CanvasXYGrid& operator=(const CanvasXYGrid&);
155 void updateWidgets();
157 bool render_dotted;
158 };
162 class CanvasXYGridSnapper : public LineSnapper
163 {
164 public:
165 CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, NR::Coord const d);
166 bool ThisSnapperMightSnap() const;
168 private:
169 LineList _getSnapLines(NR::Point const &p) const;
170 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;
171 CanvasXYGrid *grid;
172 };
174 }; /* namespace Inkscape */
179 #endif