Code

Move all of the snapper code to 2geom
[inkscape.git] / src / display / canvas-grid.h
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     NR::Point origin;     /**< Origin of the grid */
93     guint32 color;        /**< Color for normal lines */
94     guint32 empcolor;     /**< Color for emphasis lines */
95     gint empspacing;      /**< Spacing between emphasis lines */
96     
97     SPUnit const* gridunit;
98     
99     Inkscape::XML::Node * repr;
100     SPDocument *doc;
102     Inkscape::Snapper* snapper;
104     static void on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data);
106     bool isVisible() { return (isEnabled() &&visible); };
107     bool isEnabled();
109 protected:
110     CanvasGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument *in_doc, GridType type);
112     virtual Gtk::Widget * newSpecificWidget() = 0;
114     GSList * canvasitems;  // list of created canvasitems
116     SPNamedView * namedview;
118     Inkscape::UI::Widget::Registry _wr;
119     bool visible;
121     GridType gridtype;
123 private:
124     CanvasGrid(const CanvasGrid&);
125     CanvasGrid& operator=(const CanvasGrid&);
126 };
129 class CanvasXYGrid : public CanvasGrid {
130 public:
131     CanvasXYGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc);
132     virtual ~CanvasXYGrid();
134     void Update (NR::Matrix const &affine, unsigned int flags);
135     void Render (SPCanvasBuf *buf);
137     void readRepr();
138     void onReprAttrChanged (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive);
140     NR::Point spacing; /**< Spacing between elements of the grid */
141     bool scaled[2];    /**< Whether the grid is in scaled mode, which can
142                             be different in the X or Y direction, hense two
143                             variables */
144     NR::Point ow;      /**< Transformed origin by the affine for the zoom */
145     NR::Point sw;      /**< Transformed spacing by the affine for the zoom */
147 protected:
148     virtual Gtk::Widget * newSpecificWidget();
150 private:
151     CanvasXYGrid(const CanvasXYGrid&);
152     CanvasXYGrid& operator=(const CanvasXYGrid&);
154     void updateWidgets();
156     bool render_dotted;
157 };
161 class CanvasXYGridSnapper : public LineSnapper
163 public:
164     CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, Geom::Coord const d);
165     bool ThisSnapperMightSnap() const;
167 private:
168     LineList _getSnapLines(Geom::Point const &p) const;
169     void _addSnappedLine(SnappedConstraints &sc, Geom::Point const snapped_point, Geom::Coord const snapped_distance, Geom::Point const normal_to_line, const Geom::Point point_on_line) const;
170     CanvasXYGrid *grid;
171 };
173 }; /* namespace Inkscape */
178 #endif