1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** @file
3  * Cartesian grid item for the Inkscape canvas.
4  *//*
5  * Authors: see git history
6  *
7  * Copyright (C) 2018 Authors
8  * Copyright (C) Johan Engelen 2006-2007 <johan@shouraizou.nl>
9  * Copyright (C) Lauris Kaplinski 2000
10  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11  */
12 
13 #ifndef INKSCAPE_CANVAS_GRID_H
14 #define INKSCAPE_CANVAS_GRID_H
15 
16 #include "ui/widget/alignment-selector.h"
17 #include "ui/widget/registered-widget.h"
18 #include "ui/widget/registry.h"
19 #include "line-snapper.h"
20 
21 class  SPDesktop;
22 class SPNamedView;
23 class  SPDocument;
24 
25 namespace Gtk {
26   class Widget;
27 }
28 
29 namespace Inkscape {
30 
31 class Snapper;
32 class CanvasItemBuffer;
33 class CanvasItemGrid;
34 
35 namespace XML {
36 class Node;
37 }
38 
39 namespace Util {
40 class Unit;
41 }
42 
43 enum GridType {
44     GRID_RECTANGULAR = 0,
45     GRID_AXONOMETRIC = 1
46 };
47 
48 #define GRID_MAXTYPENR 1
49 
50 #define GRID_DEFAULT_COLOR 0x3F3FFF20
51 #define GRID_DEFAULT_EMPCOLOR 0x3F3FFF40
52 
53 class CanvasGrid {
54 public:
55     virtual ~CanvasGrid();
56 
57     // TODO: see effect.h and effect.cpp from live_effects how to link enums to SVGname to typename properly. (johan)
58     const char * getName() const;
59     const char * getSVGName() const;
60     GridType     getGridType() const;
61     static const char * getName(GridType type);
62     static const char * getSVGName(GridType type);
63     static GridType     getGridTypeFromSVGName(const char * typestr);
64     static GridType     getGridTypeFromName(const char * typestr);
65 
66     static CanvasGrid* NewGrid(SPNamedView * nv, Inkscape::XML::Node * repr, SPDocument *doc, GridType gridtype);
67     static void writeNewGridToRepr(Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype);
68 
69     Inkscape::CanvasItemGrid * createCanvasItem(SPDesktop * desktop);
70     void removeCanvasItem(Inkscape::CanvasItemGrid *item);
71 
72     virtual void Update (Geom::Affine const &affine, unsigned int flags) = 0;
73     virtual void Render (Inkscape::CanvasItemBuffer *buf) = 0;
74 
75     virtual void readRepr() = 0;
76     virtual void onReprAttrChanged (Inkscape::XML::Node * /*repr*/, char const */*key*/, char const */*oldval*/, char const */*newval*/, bool /*is_interactive*/) = 0;
77 
78     Gtk::Widget * newWidget();
79 
80     void setOrigin(Geom::Point const &origin_px); /**< writes new origin (specified in px units) to SVG */
81     Geom::Point origin;     /**< Origin of the grid */
82 
83     guint32 color;        /**< Color for normal lines */
84     guint32 empcolor;     /**< Color for emphasis lines */
85     gint empspacing;      /**< Spacing between emphasis lines */
86 
87     Inkscape::Util::Unit const* gridunit;  /**< points to Unit object in UnitTable (so don't delete it) */
88 
89     Inkscape::XML::Node * repr;
90     SPDocument *doc;
91 
92     Inkscape::Snapper* snapper;
93 
94     static void on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data);
95 
isLegacy()96     bool isLegacy() const { return legacy; }
isPixel()97     bool isPixel() const { return pixel; }
98 
isVisible()99     bool isVisible() const { return (isEnabled() &&visible); };
100     bool isEnabled() const;
101 
102     void align_clicked(int align);
103 
104 protected:
105     CanvasGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument *in_doc, GridType type);
106 
107     virtual Gtk::Widget * newSpecificWidget() = 0;
108 
109     std::vector<Inkscape::CanvasItemGrid *> canvas_item_grids;  // List of created CanvasGridItem's.
110 
111     SPNamedView * namedview;
112 
113     Inkscape::UI::Widget::Registry _wr;
114     bool visible;
115     bool render_dotted;
116 
117     GridType gridtype;
118 
119     // For dealing with old Inkscape SVG files (pre 0.92)
120     bool legacy;
121     bool pixel;
122 
123     Inkscape::UI::Widget::RegisteredCheckButton *_rcb_enabled = nullptr;
124     Inkscape::UI::Widget::RegisteredCheckButton *_rcb_snap_visible_only = nullptr;
125     Inkscape::UI::Widget::RegisteredCheckButton *_rcb_visible = nullptr;
126     Inkscape::UI::Widget::RegisteredCheckButton *_rcb_dotted = nullptr;
127     Inkscape::UI::Widget::AlignmentSelector     *_as_alignment = nullptr;
128 
129 private:
130     CanvasGrid(const CanvasGrid&) = delete;
131     CanvasGrid& operator=(const CanvasGrid&) = delete;
132 };
133 
134 
135 class CanvasXYGrid : public CanvasGrid {
136 public:
137     CanvasXYGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc);
138     ~CanvasXYGrid() override;
139 
140     virtual void Scale  (Geom::Scale const &scale);
141     void Update (Geom::Affine const &affine, unsigned int flags) override;
142     void Render (Inkscape::CanvasItemBuffer *buf) override;
143 
144     void readRepr() override;
145     void onReprAttrChanged (Inkscape::XML::Node * repr, char const *key, char const *oldval, char const *newval, bool is_interactive) override;
146 
147     Geom::Point spacing; /**< Spacing between elements of the grid */
148     bool scaled[2];    /**< Whether the grid is in scaled mode, which can
149                             be different in the X or Y direction, hence two
150                             variables */
151     Geom::Point ow;      /**< Transformed origin by the affine for the zoom */
152     Geom::Point sw[2];   /**< Transformed spacing by the affine for the zoom */
153 
154 protected:
155     Gtk::Widget * newSpecificWidget() override;
156 
157 private:
158     CanvasXYGrid(const CanvasXYGrid&) = delete;
159     CanvasXYGrid& operator=(const CanvasXYGrid&) = delete;
160 
161     void updateWidgets();
162 
163     Inkscape::UI::Widget::RegisteredUnitMenu *_rumg = nullptr;
164     Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_ox = nullptr;
165     Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_oy = nullptr;
166     Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_sx = nullptr;
167     Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_sy = nullptr;
168     Inkscape::UI::Widget::RegisteredColorPicker *_rcp_gcol = nullptr;
169     Inkscape::UI::Widget::RegisteredColorPicker *_rcp_gmcol = nullptr;
170     Inkscape::UI::Widget::RegisteredSuffixedInteger *_rsi = nullptr;
171 };
172 
173 
174 
175 class CanvasXYGridSnapper : public LineSnapper
176 {
177 public:
178     CanvasXYGridSnapper(CanvasXYGrid *grid, SnapManager *sm, Geom::Coord const d);
179     bool ThisSnapperMightSnap() const override;
180 
181     Geom::Coord getSnapperTolerance() const override; //returns the tolerance of the snapper in screen pixels (i.e. independent of zoom)
182     bool getSnapperAlwaysSnap() const override; //if true, then the snapper will always snap, regardless of its tolerance
183 
184 private:
185     LineList _getSnapLines(Geom::Point const &p) const override;
186     void _addSnappedLine(IntermSnapResults &isr, 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 override;
187     void _addSnappedPoint(IntermSnapResults &isr, Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, SnapSourceType const &source, long source_num, bool constrained_snap) const override;
188     void _addSnappedLinePerpendicularly(IntermSnapResults &isr, Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, SnapSourceType const &source, long source_num, bool constrained_snap) const override;
189     CanvasXYGrid *grid;
190 };
191 
192 }; /* namespace Inkscape */
193 
194 
195 
196 
197 #endif
198