1 /** @file
2 * @brief Cartesian grid implementation
3 */
4 /* Copyright (C) Johan Engelen 2006-2007 <johan@shouraizou.nl>
5 * Copyright (C) Lauris Kaplinski 2000
6 */
8 /* As a general comment, I am not exactly proud of how things are done.
9 * (for example the 'enable' widget and readRepr things)
10 * It does seem to work however. I intend to clean up and sort things out later, but that can take forever...
11 * Don't be shy to correct things.
12 */
14 #define INKSCAPE_CANVAS_GRID_C
16 #include "sp-canvas-util.h"
17 #include "util/mathfns.h"
18 #include "display-forward.h"
19 #include <libnr/nr-pixops.h>
20 #include "desktop-handles.h"
21 #include "helper/units.h"
22 #include "svg/svg-color.h"
23 #include "xml/node-event-vector.h"
24 #include "sp-object.h"
26 #include "sp-namedview.h"
27 #include "inkscape.h"
28 #include "desktop.h"
30 #include "../document.h"
31 #include "preferences.h"
33 #include "canvas-grid.h"
34 #include "canvas-axonomgrid.h"
36 namespace Inkscape {
38 static gchar const *const grid_name[] = {
39 N_("Rectangular grid"),
40 N_("Axonometric grid")
41 };
42 static gchar const *const grid_svgname[] = {
43 "xygrid",
44 "axonomgrid"
45 };
48 // ##########################################################
49 // Grid CanvasItem
50 static void grid_canvasitem_class_init (GridCanvasItemClass *klass);
51 static void grid_canvasitem_init (GridCanvasItem *grid);
52 static void grid_canvasitem_destroy (GtkObject *object);
54 static void grid_canvasitem_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
55 static void grid_canvasitem_render (SPCanvasItem *item, SPCanvasBuf *buf);
57 static SPCanvasItemClass * parent_class;
59 GtkType
60 grid_canvasitem_get_type (void)
61 {
62 static GtkType grid_canvasitem_type = 0;
64 if (!grid_canvasitem_type) {
65 GtkTypeInfo grid_canvasitem_info = {
66 (gchar *)"GridCanvasItem",
67 sizeof (GridCanvasItem),
68 sizeof (GridCanvasItemClass),
69 (GtkClassInitFunc) grid_canvasitem_class_init,
70 (GtkObjectInitFunc) grid_canvasitem_init,
71 NULL, NULL,
72 (GtkClassInitFunc) NULL
73 };
74 grid_canvasitem_type = gtk_type_unique (sp_canvas_item_get_type (), &grid_canvasitem_info);
75 }
76 return grid_canvasitem_type;
77 }
79 static void
80 grid_canvasitem_class_init (GridCanvasItemClass *klass)
81 {
82 GtkObjectClass *object_class;
83 SPCanvasItemClass *item_class;
85 object_class = (GtkObjectClass *) klass;
86 item_class = (SPCanvasItemClass *) klass;
88 parent_class = (SPCanvasItemClass*)gtk_type_class (sp_canvas_item_get_type ());
90 object_class->destroy = grid_canvasitem_destroy;
92 item_class->update = grid_canvasitem_update;
93 item_class->render = grid_canvasitem_render;
94 }
96 static void
97 grid_canvasitem_init (GridCanvasItem *griditem)
98 {
99 griditem->grid = NULL;
100 }
102 static void
103 grid_canvasitem_destroy (GtkObject *object)
104 {
105 g_return_if_fail (object != NULL);
106 g_return_if_fail (INKSCAPE_IS_GRID_CANVASITEM (object));
108 if (GTK_OBJECT_CLASS (parent_class)->destroy)
109 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
110 }
112 /**
113 */
114 static void
115 grid_canvasitem_render (SPCanvasItem * item, SPCanvasBuf * buf)
116 {
117 GridCanvasItem *gridcanvasitem = INKSCAPE_GRID_CANVASITEM (item);
119 if ( gridcanvasitem->grid && gridcanvasitem->grid->isVisible() ) {
120 sp_canvas_prepare_buffer (buf);
121 gridcanvasitem->grid->Render(buf);
122 }
123 }
125 static void
126 grid_canvasitem_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
127 {
128 GridCanvasItem *gridcanvasitem = INKSCAPE_GRID_CANVASITEM (item);
130 if (parent_class->update)
131 (* parent_class->update) (item, affine, flags);
133 if (gridcanvasitem->grid) {
134 gridcanvasitem->grid->Update(affine, flags);
136 sp_canvas_request_redraw (item->canvas,
137 -1000000, -1000000,
138 1000000, 1000000);
140 item->x1 = item->y1 = -1000000;
141 item->x2 = item->y2 = 1000000;
142 }
143 }
147 // ##########################################################
148 // CanvasGrid
150 static Inkscape::XML::NodeEventVector const _repr_events = {
151 NULL, /* child_added */
152 NULL, /* child_removed */
153 CanvasGrid::on_repr_attr_changed,
154 NULL, /* content_changed */
155 NULL /* order_changed */
156 };
158 CanvasGrid::CanvasGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument *in_doc, GridType type)
159 : visible(true), gridtype(type)
160 {
161 repr = in_repr;
162 doc = in_doc;
163 if (repr) {
164 repr->addListener (&_repr_events, this);
165 }
167 namedview = nv;
168 canvasitems = NULL;
169 }
171 CanvasGrid::~CanvasGrid()
172 {
173 if (repr) {
174 repr->removeListenerByData (this);
175 }
177 while (canvasitems) {
178 gtk_object_destroy(GTK_OBJECT(canvasitems->data));
179 canvasitems = g_slist_remove(canvasitems, canvasitems->data);
180 }
181 }
183 const char *
184 CanvasGrid::getName()
185 {
186 return _(grid_name[gridtype]);
187 }
189 const char *
190 CanvasGrid::getSVGName()
191 {
192 return grid_svgname[gridtype];
193 }
195 GridType
196 CanvasGrid::getGridType()
197 {
198 return gridtype;
199 }
202 char const *
203 CanvasGrid::getName(GridType type)
204 {
205 return _(grid_name[type]);
206 }
208 char const *
209 CanvasGrid::getSVGName(GridType type)
210 {
211 return grid_svgname[type];
212 }
214 GridType
215 CanvasGrid::getGridTypeFromSVGName(char const *typestr)
216 {
217 if (!typestr) return GRID_RECTANGULAR;
219 gint t = 0;
220 for (t = GRID_MAXTYPENR; t >= 0; t--) { //this automatically defaults to grid0 which is rectangular grid
221 if (!strcmp(typestr, grid_svgname[t])) break;
222 }
223 return (GridType) t;
224 }
226 GridType
227 CanvasGrid::getGridTypeFromName(char const *typestr)
228 {
229 if (!typestr) return GRID_RECTANGULAR;
231 gint t = 0;
232 for (t = GRID_MAXTYPENR; t >= 0; t--) { //this automatically defaults to grid0 which is rectangular grid
233 if (!strcmp(typestr, _(grid_name[t]))) break;
234 }
235 return (GridType) t;
236 }
239 /*
240 * writes an <inkscape:grid> child to repr.
241 */
242 void
243 CanvasGrid::writeNewGridToRepr(Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype)
244 {
245 if (!repr) return;
246 if (gridtype > GRID_MAXTYPENR) return;
248 // first create the child xml node, then hook it to repr. This order is important, to not set off listeners to repr before the new node is complete.
250 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
251 Inkscape::XML::Node *newnode;
252 newnode = xml_doc->createElement("inkscape:grid");
253 newnode->setAttribute("type", getSVGName(gridtype));
255 repr->appendChild(newnode);
256 Inkscape::GC::release(newnode);
258 sp_document_done(doc, SP_VERB_DIALOG_NAMEDVIEW, _("Create new grid"));
259 }
261 /*
262 * Creates a new CanvasGrid object of type gridtype
263 */
264 CanvasGrid*
265 CanvasGrid::NewGrid(SPNamedView * nv, Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype)
266 {
267 if (!repr) return NULL;
268 if (!doc) {
269 g_error("CanvasGrid::NewGrid - doc==NULL");
270 return NULL;
271 }
273 switch (gridtype) {
274 case GRID_RECTANGULAR:
275 return (CanvasGrid*) new CanvasXYGrid(nv, repr, doc);
276 case GRID_AXONOMETRIC:
277 return (CanvasGrid*) new CanvasAxonomGrid(nv, repr, doc);
278 }
280 return NULL;
281 }
284 /**
285 * creates a new grid canvasitem for the SPDesktop given as parameter. Keeps a link to this canvasitem in the canvasitems list.
286 */
287 GridCanvasItem *
288 CanvasGrid::createCanvasItem(SPDesktop * desktop)
289 {
290 if (!desktop) return NULL;
291 // Johan: I think for multiple desktops it is best if each has their own canvasitem,
292 // but share the same CanvasGrid object; that is what this function is for.
294 // check if there is already a canvasitem on this desktop linking to this grid
295 for (GSList *l = canvasitems; l != NULL; l = l->next) {
296 if ( sp_desktop_gridgroup(desktop) == SP_CANVAS_GROUP(SP_CANVAS_ITEM(l->data)->parent) ) {
297 return NULL;
298 }
299 }
301 GridCanvasItem * item = INKSCAPE_GRID_CANVASITEM( sp_canvas_item_new(sp_desktop_gridgroup(desktop), INKSCAPE_TYPE_GRID_CANVASITEM, NULL) );
302 item->grid = this;
303 sp_canvas_item_show(SP_CANVAS_ITEM(item));
305 gtk_object_ref(GTK_OBJECT(item)); // since we're keeping a link to this item, we need to bump up the ref count
306 canvasitems = g_slist_prepend(canvasitems, item);
308 return item;
309 }
311 Gtk::Widget *
312 CanvasGrid::newWidget()
313 {
314 Gtk::VBox * vbox = Gtk::manage( new Gtk::VBox() );
315 Gtk::Label * namelabel = Gtk::manage(new Gtk::Label("", Gtk::ALIGN_CENTER) );
317 Glib::ustring str("<b>");
318 str += getName();
319 str += "</b>";
320 namelabel->set_markup(str);
321 vbox->pack_start(*namelabel, true, true);
323 Inkscape::UI::Widget::RegisteredCheckButton * _rcb_enabled = Gtk::manage(
324 new Inkscape::UI::Widget::RegisteredCheckButton( _("_Enabled"),
325 _("Determines whether to snap to this grid or not. Can be 'on' for invisible grids."),
326 "enabled", _wr, false, repr, doc) );
327 Inkscape::UI::Widget::RegisteredCheckButton * _rcb_visible = Gtk::manage(
328 new Inkscape::UI::Widget::RegisteredCheckButton( _("_Visible"),
329 _("Determines whether the grid is displayed or not. Objects are still snapped to invisible grids."),
330 "visible", _wr, true, repr, doc) );
332 vbox->pack_start(*_rcb_enabled, true, true);
333 vbox->pack_start(*_rcb_visible, true, true);
334 Gtk::Widget * gridwdg = newSpecificWidget();
335 vbox->pack_start(*gridwdg, true, true);
337 std::list<Gtk::Widget*> slaves;
338 slaves.push_back(_rcb_visible);
339 slaves.push_back(gridwdg);
340 _rcb_enabled->setSlaveWidgets(slaves);
342 // set widget values
343 _rcb_visible->setActive(visible);
344 if (snapper != NULL) {
345 _rcb_enabled->setActive(snapper->getEnabled());
346 }
348 return dynamic_cast<Gtk::Widget *> (vbox);
349 }
351 void
352 CanvasGrid::on_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, void *data)
353 {
354 if (!data)
355 return;
357 ((CanvasGrid*) data)->onReprAttrChanged(repr, key, oldval, newval, is_interactive);
358 }
360 bool CanvasGrid::isEnabled()
361 {
362 if (snapper == NULL) {
363 return false;
364 }
366 return snapper->getEnabled();
367 }
369 // ##########################################################
370 // CanvasXYGrid
373 /**
374 * "attach_all" function
375 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP TO QUICKLY GET RESULTS
376 *
377 * Helper function that attachs widgets in a 3xn table. The widgets come in an
378 * array that has two entries per table row. The two entries code for four
379 * possible cases: (0,0) means insert space in first column; (0, non-0) means
380 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
381 * (non-0, non-0) means two widgets in columns 2 and 3.
382 **/
383 #define SPACE_SIZE_X 15
384 #define SPACE_SIZE_Y 10
385 static inline void
386 attach_all(Gtk::Table &table, Gtk::Widget const *const arr[], unsigned size, int start = 0)
387 {
388 for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2) {
389 if (arr[i] && arr[i+1]) {
390 table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
391 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
392 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
393 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
394 } else {
395 if (arr[i+1]) {
396 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
397 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
398 } else if (arr[i]) {
399 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
400 label.set_alignment (0.0);
401 table.attach (label, 0, 3, r, r+1,
402 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
403 } else {
404 Gtk::HBox *space = manage (new Gtk::HBox);
405 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
406 table.attach (*space, 0, 1, r, r+1,
407 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
408 }
409 }
410 ++r;
411 }
412 }
414 CanvasXYGrid::CanvasXYGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
415 : CanvasGrid(nv, in_repr, in_doc, GRID_RECTANGULAR)
416 {
417 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
418 gridunit = sp_unit_get_by_abbreviation( prefs->getString("/options/grids/xy/units").data() );
419 if (!gridunit)
420 gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
421 origin[Geom::X] = sp_units_get_pixels(prefs->getDouble("/options/grids/xy/origin_x", 0.0), *gridunit);
422 origin[Geom::Y] = sp_units_get_pixels(prefs->getDouble("/options/grids/xy/origin_y", 0.0), *gridunit);
423 color = prefs->getInt("/options/grids/xy/color", 0x0000ff20);
424 empcolor = prefs->getInt("/options/grids/xy/empcolor", 0x0000ff40);
425 empspacing = prefs->getInt("/options/grids/xy/empspacing", 5);
426 spacing[Geom::X] = sp_units_get_pixels(prefs->getDouble("/options/grids/xy/spacing_x", 0.0), *gridunit);
427 spacing[Geom::Y] = sp_units_get_pixels(prefs->getDouble("/options/grids/xy/spacing_y", 0.0), *gridunit);
428 render_dotted = prefs->getBool("/options/grids/xy/dotted", false);
430 snapper = new CanvasXYGridSnapper(this, &namedview->snap_manager, 0);
432 if (repr) readRepr();
433 }
435 CanvasXYGrid::~CanvasXYGrid ()
436 {
437 if (snapper) delete snapper;
438 }
441 /* fixme: Collect all these length parsing methods and think common sane API */
443 static gboolean
444 sp_nv_read_length(gchar const *str, guint base, gdouble *val, SPUnit const **unit)
445 {
446 if (!str) {
447 return FALSE;
448 }
450 gchar *u;
451 gdouble v = g_ascii_strtod(str, &u);
452 if (!u) {
453 return FALSE;
454 }
455 while (isspace(*u)) {
456 u += 1;
457 }
459 if (!*u) {
460 /* No unit specified - keep default */
461 *val = v;
462 return TRUE;
463 }
465 if (base & SP_UNIT_DEVICE) {
466 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
467 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
468 *val = v;
469 return TRUE;
470 }
471 }
473 if (base & SP_UNIT_ABSOLUTE) {
474 if (!strncmp(u, "pt", 2)) {
475 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
476 } else if (!strncmp(u, "mm", 2)) {
477 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
478 } else if (!strncmp(u, "cm", 2)) {
479 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
480 } else if (!strncmp(u, "m", 1)) {
481 *unit = &sp_unit_get_by_id(SP_UNIT_M);
482 } else if (!strncmp(u, "in", 2)) {
483 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
484 } else {
485 return FALSE;
486 }
487 *val = v;
488 return TRUE;
489 }
491 return FALSE;
492 }
494 static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
495 {
496 if (!str) {
497 return FALSE;
498 }
500 gchar *u;
501 gdouble v = g_ascii_strtod(str, &u);
502 if (!u) {
503 return FALSE;
504 }
505 v = CLAMP(v, 0.0, 1.0);
507 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
509 return TRUE;
510 }
512 /** If the passed scalar is invalid (<=0), then set the widget and the scalar
513 to use the given old value.
515 @param oldVal Old value to use if the new one is invalid.
516 @param pTarget The scalar to validate.
517 @param widget Widget associated with the scalar.
518 */
519 static void validateScalar(double oldVal,
520 double* pTarget)
521 {
522 // Avoid nullness.
523 if ( pTarget == NULL )
524 return;
526 // Invalid new value?
527 if ( *pTarget <= 0 ) {
528 // If the old value is somehow invalid as well, then default to 1.
529 if ( oldVal <= 0 )
530 oldVal = 1;
532 // Reset the scalar and associated widget to the old value.
533 *pTarget = oldVal;
534 } //if
536 } //validateScalar
539 /** If the passed int is invalid (<=0), then set the widget and the int
540 to use the given old value.
542 @param oldVal Old value to use if the new one is invalid.
543 @param pTarget The int to validate.
544 @param widget Widget associated with the int.
545 */
546 static void validateInt(gint oldVal,
547 gint* pTarget)
548 {
549 // Avoid nullness.
550 if ( pTarget == NULL )
551 return;
553 // Invalid new value?
554 if ( *pTarget <= 0 ) {
555 // If the old value is somehow invalid as well, then default to 1.
556 if ( oldVal <= 0 )
557 oldVal = 1;
559 // Reset the int and associated widget to the old value.
560 *pTarget = oldVal;
561 } //if
563 } //validateInt
565 void
566 CanvasXYGrid::readRepr()
567 {
568 gchar const *value;
569 if ( (value = repr->attribute("originx")) ) {
570 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[Geom::X], &gridunit);
571 origin[Geom::X] = sp_units_get_pixels(origin[Geom::X], *(gridunit));
572 }
574 if ( (value = repr->attribute("originy")) ) {
575 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[Geom::Y], &gridunit);
576 origin[Geom::Y] = sp_units_get_pixels(origin[Geom::Y], *(gridunit));
577 }
579 if ( (value = repr->attribute("spacingx")) ) {
580 double oldVal = spacing[Geom::X];
581 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[Geom::X], &gridunit);
582 validateScalar( oldVal, &spacing[Geom::X]);
583 spacing[Geom::X] = sp_units_get_pixels(spacing[Geom::X], *(gridunit));
585 }
586 if ( (value = repr->attribute("spacingy")) ) {
587 double oldVal = spacing[Geom::Y];
588 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[Geom::Y], &gridunit);
589 validateScalar( oldVal, &spacing[Geom::Y]);
590 spacing[Geom::Y] = sp_units_get_pixels(spacing[Geom::Y], *(gridunit));
592 }
594 if ( (value = repr->attribute("color")) ) {
595 color = (color & 0xff) | sp_svg_read_color(value, color);
596 }
598 if ( (value = repr->attribute("empcolor")) ) {
599 empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
600 }
602 if ( (value = repr->attribute("opacity")) ) {
603 sp_nv_read_opacity(value, &color);
604 }
605 if ( (value = repr->attribute("empopacity")) ) {
606 sp_nv_read_opacity(value, &empcolor);
607 }
609 if ( (value = repr->attribute("empspacing")) ) {
610 gint oldVal = empspacing;
611 empspacing = atoi(value);
612 validateInt( oldVal, &empspacing);
613 }
615 if ( (value = repr->attribute("dotted")) ) {
616 render_dotted = (strcmp(value,"false") != 0 && strcmp(value, "0") != 0);
617 }
619 if ( (value = repr->attribute("visible")) ) {
620 visible = (strcmp(value,"false") != 0 && strcmp(value, "0") != 0);
621 }
623 if ( (value = repr->attribute("enabled")) ) {
624 g_assert(snapper != NULL);
625 snapper->setEnabled(strcmp(value,"false") != 0 && strcmp(value, "0") != 0);
626 }
628 for (GSList *l = canvasitems; l != NULL; l = l->next) {
629 sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
630 }
632 return;
633 }
635 /**
636 * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
637 */
638 void
639 CanvasXYGrid::onReprAttrChanged(Inkscape::XML::Node */*repr*/, gchar const */*key*/, gchar const */*oldval*/, gchar const */*newval*/, bool /*is_interactive*/)
640 {
641 readRepr();
643 if ( ! (_wr.isUpdating()) )
644 updateWidgets();
645 }
650 Gtk::Widget *
651 CanvasXYGrid::newSpecificWidget()
652 {
653 Gtk::Table * table = Gtk::manage( new Gtk::Table(1,1) );
655 Inkscape::UI::Widget::RegisteredUnitMenu *_rumg = Gtk::manage( new Inkscape::UI::Widget::RegisteredUnitMenu(
656 _("Grid _units:"), "units", _wr, repr, doc) );
657 Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_ox = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
658 _("_Origin X:"), _("X coordinate of grid origin"), "originx", *_rumg, _wr, repr, doc) );
659 Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_oy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
660 _("O_rigin Y:"), _("Y coordinate of grid origin"), "originy", *_rumg, _wr, repr, doc) );
661 Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_sx = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
662 _("Spacing _X:"), _("Distance between vertical grid lines"), "spacingx", *_rumg, _wr, repr, doc) );
663 Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_sy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
664 _("Spacing _Y:"), _("Distance between horizontal grid lines"), "spacingy", *_rumg, _wr, repr, doc) );
666 Inkscape::UI::Widget::RegisteredColorPicker *_rcp_gcol = Gtk::manage(
667 new Inkscape::UI::Widget::RegisteredColorPicker(
668 _("Grid line _color:"), _("Grid line color"), _("Color of grid lines"),
669 "color", "opacity", _wr, repr, doc));
671 Inkscape::UI::Widget::RegisteredColorPicker *_rcp_gmcol = Gtk::manage(
672 new Inkscape::UI::Widget::RegisteredColorPicker(
673 _("Ma_jor grid line color:"), _("Major grid line color"),
674 _("Color of the major (highlighted) grid lines"), "empcolor", "empopacity",
675 _wr, repr, doc));
677 Inkscape::UI::Widget::RegisteredSuffixedInteger *_rsi = Gtk::manage( new Inkscape::UI::Widget::RegisteredSuffixedInteger(
678 _("_Major grid line every:"), "", _("lines"), "empspacing", _wr, repr, doc) );
680 table->set_spacings(2);
682 _wr.setUpdating (true);
684 _rsu_ox->setDigits(4);
685 _rsu_ox->setIncrements(0.1, 1.0);
687 _rsu_oy->setDigits(4);
688 _rsu_oy->setIncrements(0.1, 1.0);
690 _rsu_sx->setDigits(4);
691 _rsu_sx->setIncrements(0.1, 1.0);
693 _rsu_sy->setDigits(4);
694 _rsu_sy->setIncrements(0.1, 1.0);
696 Inkscape::UI::Widget::RegisteredCheckButton * _rcb_dotted = Gtk::manage(
697 new Inkscape::UI::Widget::RegisteredCheckButton( _("_Show dots instead of lines"),
698 _("If set, displays dots at gridpoints instead of gridlines"),
699 "dotted", _wr, false, repr, doc) );
700 _wr.setUpdating (false);
702 Gtk::Widget const *const widget_array[] = {
703 0, _rumg,
704 0, _rsu_ox,
705 0, _rsu_oy,
706 0, _rsu_sx,
707 0, _rsu_sy,
708 _rcp_gcol->_label, _rcp_gcol,
709 0, 0,
710 _rcp_gmcol->_label, _rcp_gmcol,
711 0, _rsi,
712 0, _rcb_dotted,
713 };
715 attach_all (*table, widget_array, sizeof(widget_array));
717 // set widget values
718 _rumg->setUnit (gridunit);
720 gdouble val;
721 val = origin[Geom::X];
722 val = sp_pixels_get_units (val, *(gridunit));
723 _rsu_ox->setValue (val);
724 val = origin[Geom::Y];
725 val = sp_pixels_get_units (val, *(gridunit));
726 _rsu_oy->setValue (val);
727 val = spacing[Geom::X];
728 double gridx = sp_pixels_get_units (val, *(gridunit));
729 _rsu_sx->setValue (gridx);
730 val = spacing[Geom::Y];
731 double gridy = sp_pixels_get_units (val, *(gridunit));
732 _rsu_sy->setValue (gridy);
734 _rcp_gcol->setRgba32 (color);
735 _rcp_gmcol->setRgba32 (empcolor);
736 _rsi->setValue (empspacing);
738 _rcb_dotted->setActive(render_dotted);
740 return table;
741 }
744 /**
745 * Update dialog widgets from object's values.
746 */
747 void
748 CanvasXYGrid::updateWidgets()
749 {
750 /*
751 if (_wr.isUpdating()) return;
753 _wr.setUpdating (true);
755 _rcb_visible.setActive(visible);
756 if (snapper != NULL) {
757 _rcb_enabled.setActive(snapper->getEnabled());
758 }
760 _rumg.setUnit (gridunit);
762 gdouble val;
763 val = origin[Geom::X];
764 val = sp_pixels_get_units (val, *(gridunit));
765 _rsu_ox.setValue (val);
766 val = origin[Geom::Y];
767 val = sp_pixels_get_units (val, *(gridunit));
768 _rsu_oy.setValue (val);
769 val = spacing[Geom::X];
770 double gridx = sp_pixels_get_units (val, *(gridunit));
771 _rsu_sx.setValue (gridx);
772 val = spacing[Geom::Y];
773 double gridy = sp_pixels_get_units (val, *(gridunit));
774 _rsu_sy.setValue (gridy);
776 _rcp_gcol.setRgba32 (color);
777 _rcp_gmcol.setRgba32 (empcolor);
778 _rsi.setValue (empspacing);
780 _rcb_dotted.setActive(render_dotted);
782 _wr.setUpdating (false);
784 return;
785 */
786 }
790 void
791 CanvasXYGrid::Update (Geom::Matrix const &affine, unsigned int /*flags*/)
792 {
793 ow = origin * affine;
794 sw = spacing * affine;
795 sw -= Geom::Point(affine[4], affine[5]);
797 for(int dim = 0; dim < 2; dim++) {
798 gint scaling_factor = empspacing;
800 if (scaling_factor <= 1)
801 scaling_factor = 5;
803 scaled[dim] = FALSE;
804 sw[dim] = fabs (sw[dim]);
805 while (sw[dim] < 8.0) {
806 scaled[dim] = TRUE;
807 sw[dim] *= scaling_factor;
808 /* First pass, go up to the major line spacing, then
809 keep increasing by two. */
810 scaling_factor = 2;
811 }
812 }
813 }
816 static void
817 grid_hline (SPCanvasBuf *buf, gint y, gint xs, gint xe, guint32 rgba)
818 {
819 if ((y >= buf->rect.y0) && (y < buf->rect.y1)) {
820 guint r, g, b, a;
821 gint x0, x1, x;
822 guchar *p;
823 r = NR_RGBA32_R (rgba);
824 g = NR_RGBA32_G (rgba);
825 b = NR_RGBA32_B (rgba);
826 a = NR_RGBA32_A (rgba);
827 x0 = MAX (buf->rect.x0, xs);
828 x1 = MIN (buf->rect.x1, xe + 1);
829 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x0 - buf->rect.x0) * 4;
830 for (x = x0; x < x1; x++) {
831 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
832 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
833 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
834 p += 4;
835 }
836 }
837 }
839 static void
840 grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
841 {
842 if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
843 guint r, g, b, a;
844 gint y0, y1, y;
845 guchar *p;
846 r = NR_RGBA32_R(rgba);
847 g = NR_RGBA32_G (rgba);
848 b = NR_RGBA32_B (rgba);
849 a = NR_RGBA32_A (rgba);
850 y0 = MAX (buf->rect.y0, ys);
851 y1 = MIN (buf->rect.y1, ye + 1);
852 p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
853 for (y = y0; y < y1; y++) {
854 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
855 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
856 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
857 p += buf->buf_rowstride;
858 }
859 }
860 }
862 static void
863 grid_dot (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
864 {
865 if ( (y >= buf->rect.y0) && (y < buf->rect.y1)
866 && (x >= buf->rect.x0) && (x < buf->rect.x1) ) {
867 guint r, g, b, a;
868 guchar *p;
869 r = NR_RGBA32_R (rgba);
870 g = NR_RGBA32_G (rgba);
871 b = NR_RGBA32_B (rgba);
872 a = NR_RGBA32_A (rgba);
873 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
874 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
875 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
876 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
877 }
878 }
880 void
881 CanvasXYGrid::Render (SPCanvasBuf *buf)
882 {
883 gdouble const sxg = floor ((buf->rect.x0 - ow[Geom::X]) / sw[Geom::X]) * sw[Geom::X] + ow[Geom::X];
884 gint const xlinestart = (gint) Inkscape::round((sxg - ow[Geom::X]) / sw[Geom::X]);
885 gdouble const syg = floor ((buf->rect.y0 - ow[Geom::Y]) / sw[Geom::Y]) * sw[Geom::Y] + ow[Geom::Y];
886 gint const ylinestart = (gint) Inkscape::round((syg - ow[Geom::Y]) / sw[Geom::Y]);
888 //set correct coloring, depending preference (when zoomed out, always major coloring or minor coloring)
889 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
890 guint32 _empcolor;
891 bool no_emp_when_zoomed_out = prefs->getBool("/options/grids/no_emphasize_when_zoomedout", false);
892 if( (scaled[Geom::X] || scaled[Geom::Y]) && no_emp_when_zoomed_out ) {
893 _empcolor = color;
894 } else {
895 _empcolor = empcolor;
896 }
898 if (!render_dotted) {
899 gint ylinenum;
900 gdouble y;
901 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[Geom::Y], ylinenum++) {
902 gint const y0 = (gint) Inkscape::round(y);
903 if (!scaled[Geom::Y] && (ylinenum % empspacing) != 0) {
904 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, color);
905 } else {
906 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, _empcolor);
907 }
908 }
910 gint xlinenum;
911 gdouble x;
912 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[Geom::X], xlinenum++) {
913 gint const ix = (gint) Inkscape::round(x);
914 if (!scaled[Geom::X] && (xlinenum % empspacing) != 0) {
915 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, color);
916 } else {
917 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, _empcolor);
918 }
919 }
920 } else {
921 gint ylinenum;
922 gdouble y;
923 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[Geom::Y], ylinenum++) {
924 gint const iy = (gint) Inkscape::round(y);
926 gint xlinenum;
927 gdouble x;
928 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[Geom::X], xlinenum++) {
929 gint const ix = (gint) Inkscape::round(x);
930 if ( (!scaled[Geom::X] && (xlinenum % empspacing) != 0)
931 || (!scaled[Geom::Y] && (ylinenum % empspacing) != 0)
932 || ((scaled[Geom::X] || scaled[Geom::Y]) && no_emp_when_zoomed_out) )
933 {
934 grid_dot (buf, ix, iy, color | (guint32)0x000000FF); // put alpha to max value
935 } else {
936 gint const pitch = 1;
937 grid_dot (buf, ix-pitch, iy, _empcolor);
938 grid_dot (buf, ix+pitch, iy, _empcolor);
940 grid_dot (buf, ix, iy, _empcolor | (guint32)0x000000FF); // put alpha to max value
942 grid_dot (buf, ix, iy-pitch, _empcolor);
943 grid_dot (buf, ix, iy+pitch, _empcolor);
944 }
945 }
947 }
948 }
949 }
951 CanvasXYGridSnapper::CanvasXYGridSnapper(CanvasXYGrid *grid, SnapManager *sm, Geom::Coord const d) : LineSnapper(sm, d)
952 {
953 this->grid = grid;
954 }
956 LineSnapper::LineList
957 CanvasXYGridSnapper::_getSnapLines(Geom::Point const &p) const
958 {
959 LineList s;
961 if ( grid == NULL ) {
962 return s;
963 }
965 for (unsigned int i = 0; i < 2; ++i) {
967 /* This is to make sure we snap to only visible grid lines */
968 double scaled_spacing = grid->sw[i]; // this is spacing of visible lines in screen pixels
970 // convert screen pixels to px
971 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
972 if (SP_ACTIVE_DESKTOP) {
973 scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
974 }
976 Geom::Coord rounded;
977 Geom::Point point_on_line;
979 rounded = Inkscape::Util::round_to_upper_multiple_plus(p[i], scaled_spacing, grid->origin[i]);
980 point_on_line = i ? Geom::Point(0, rounded) : Geom::Point(rounded, 0);
981 s.push_back(std::make_pair(component_vectors[i], point_on_line));
983 rounded = Inkscape::Util::round_to_lower_multiple_plus(p[i], scaled_spacing, grid->origin[i]);
984 point_on_line = i ? Geom::Point(0, rounded) : Geom::Point(rounded, 0);
985 s.push_back(std::make_pair(component_vectors[i], point_on_line));
986 }
988 return s;
989 }
991 void CanvasXYGridSnapper::_addSnappedLine(SnappedConstraints &sc, Geom::Point const snapped_point, Geom::Coord const snapped_distance, Geom::Point const normal_to_line, Geom::Point const point_on_line) const
992 {
993 SnappedLine dummy = SnappedLine(snapped_point, snapped_distance, getSnapperTolerance(), getSnapperAlwaysSnap(), normal_to_line, point_on_line);
994 sc.grid_lines.push_back(dummy);
995 }
997 /**
998 * \return true if this Snapper will snap at least one kind of point.
999 */
1000 bool CanvasXYGridSnapper::ThisSnapperMightSnap() const
1001 {
1002 return _snapmanager->snapprefs.getSnapModeBBoxOrNodes();
1003 }
1005 } // namespace Inkscape
1008 /*
1009 Local Variables:
1010 mode:c++
1011 c-file-style:"stroustrup"
1012 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1013 indent-tabs-mode:nil
1014 fill-column:99
1015 End:
1016 */
1017 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :