Code

a11d77d1dc86d9eb5372c10e6dc50ddfad6c7426
[inkscape.git] / src / display / canvas-grid.h
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 "display/sp-canvas.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 class SPDocument;
31 namespace Inkscape {
34 enum GridType {
35     GRID_RECTANGULAR = 0,
36     GRID_AXONOMETRIC = 1
37 };
38 #define GRID_MAXTYPENR 1
40 #define INKSCAPE_TYPE_GRID_CANVASITEM            (Inkscape::grid_canvasitem_get_type ())
41 #define INKSCAPE_GRID_CANVASITEM(obj)            (GTK_CHECK_CAST ((obj), INKSCAPE_TYPE_GRID_CANVASITEM, GridCanvasItem))
42 #define INKSCAPE_GRID_CANVASITEM_CLASS(klass)    (GTK_CHECK_CLASS_CAST ((klass), INKSCAPE_TYPE_GRID_CANVASITEM, GridCanvasItem))
43 #define INKSCAPE_IS_GRID_CANVASITEM(obj)         (GTK_CHECK_TYPE ((obj), INKSCAPE_TYPE_GRID_CANVASITEM))
44 #define INKSCAPE_IS_GRID_CANVASITEM_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), INKSCAPE_TYPE_GRID_CANVASITEM))
46 class CanvasGrid;
48 /** \brief  All the variables that are tracked for a grid specific
49             canvas item. */
50 struct GridCanvasItem : public SPCanvasItem{
51     CanvasGrid *grid; // the owning grid object
52 };
54 struct GridCanvasItemClass {
55     SPCanvasItemClass parent_class;
56 };
58 /* Standard Gtk function */
59 GtkType grid_canvasitem_get_type (void);
63 class CanvasGrid {
64 public:
65     virtual ~CanvasGrid();
67     // TODO: see effect.h and effect.cpp from live_effects how to link enums to SVGname to typename properly. (johan)
68     const char * getName();
69     const char * getSVGName();
70     GridType     getGridType();
71     static const char * getName(GridType type);
72     static const char * getSVGName(GridType type);
73     static GridType     getGridTypeFromSVGName(const char * typestr);
74     static GridType     getGridTypeFromName(const char * typestr);
76     static CanvasGrid* NewGrid(SPNamedView * nv, Inkscape::XML::Node * repr, SPDocument *doc, GridType gridtype);
77     static void writeNewGridToRepr(Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype);
79     GridCanvasItem * createCanvasItem(SPDesktop * desktop);
81     virtual void Update (Geom::Matrix const &affine, unsigned int flags) = 0;
82     virtual void Render (SPCanvasBuf *buf) = 0;
84     virtual void readRepr() = 0;
85     virtual void onReprAttrChanged (Inkscape::XML::Node * /*repr*/, const gchar */*key*/, const gchar */*oldval*/, const gchar */*newval*/, bool /*is_interactive*/) = 0;
87     Gtk::Widget * newWidget();
89     Geom::Point origin;     /**< Origin of the grid */
90     guint32 color;        /**< Color for normal lines */
91     guint32 empcolor;     /**< Color for emphasis lines */
92     gint empspacing;      /**< Spacing between emphasis lines */
94     SPUnit const* gridunit;
96     Inkscape::XML::Node * repr;
97     SPDocument *doc;
99     Inkscape::Snapper* snapper;
101     static void on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data);
103     bool isVisible() { return (isEnabled() &&visible); };
104     bool isEnabled();
106 protected:
107     CanvasGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument *in_doc, GridType type);
109     virtual Gtk::Widget * newSpecificWidget() = 0;
111     GSList * canvasitems;  // list of created canvasitems
113     SPNamedView * namedview;
115     Inkscape::UI::Widget::Registry _wr;
116     bool visible;
118     GridType gridtype;
120 private:
121     CanvasGrid(const CanvasGrid&);
122     CanvasGrid& operator=(const CanvasGrid&);
123 };
126 class CanvasXYGrid : public CanvasGrid {
127 public:
128     CanvasXYGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc);
129     virtual ~CanvasXYGrid();
131     void Update (Geom::Matrix const &affine, unsigned int flags);
132     void Render (SPCanvasBuf *buf);
134     void readRepr();
135     void onReprAttrChanged (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive);
137     Geom::Point spacing; /**< Spacing between elements of the grid */
138     bool scaled[2];    /**< Whether the grid is in scaled mode, which can
139                             be different in the X or Y direction, hence two
140                             variables */
141     Geom::Point ow;      /**< Transformed origin by the affine for the zoom */
142     Geom::Point sw;      /**< Transformed spacing by the affine for the zoom */
144 protected:
145     virtual Gtk::Widget * newSpecificWidget();
147 private:
148     CanvasXYGrid(const CanvasXYGrid&);
149     CanvasXYGrid& operator=(const CanvasXYGrid&);
151     void updateWidgets();
153     bool render_dotted;
154 };
158 class CanvasXYGridSnapper : public LineSnapper
160 public:
161     CanvasXYGridSnapper(CanvasXYGrid *grid, SnapManager *sm, Geom::Coord const d);
162     bool ThisSnapperMightSnap() const;
164     Geom::Coord getSnapperTolerance() const; //returns the tolerance of the snapper in screen pixels (i.e. independent of zoom)
165     bool getSnapperAlwaysSnap() const; //if true, then the snapper will always snap, regardless of its tolerance
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,  SnapSourceType const &source, long source_num, Geom::Point const normal_to_line, const Geom::Point point_on_line) const;
170     void _addSnappedPoint(SnappedConstraints &sc, Geom::Point const snapped_point, Geom::Coord const snapped_distance, SnapSourceType const &source, long source_num, bool constrained_snap) const;
171     CanvasXYGrid *grid;
172 };
174 }; /* namespace Inkscape */
179 #endif