1 #define INKSCAPE_CANVAS_GRID_C
3 /*
4 *
5 * Copyright (C) Johan Engelen 2006-2007 <johan@shouraizou.nl>
6 * Copyright (C) Lauris Kaplinski 2000
7 *
8 */
10 /* As a general comment, I am not exactly proud of how things are done.
11 * (for example the 'enable' widget and readRepr things)
12 * It does seem to work however. I intend to clean up and sort things out later, but that can take forever...
13 * Don't be shy to correct things.
14 */
17 #include "sp-canvas-util.h"
18 #include "util/mathfns.h"
19 #include "display-forward.h"
20 #include <libnr/nr-pixops.h>
21 #include "desktop-handles.h"
22 #include "helper/units.h"
23 #include "svg/svg-color.h"
24 #include "xml/node-event-vector.h"
25 #include "sp-object.h"
27 #include "sp-namedview.h"
28 #include "inkscape.h"
29 #include "desktop.h"
31 #include "../document.h"
33 #include "canvas-grid.h"
34 #include "canvas-axonomgrid.h"
36 namespace Inkscape {
38 #define DEFAULTGRIDCOLOR 0x0000FF20
39 #define DEFAULTGRIDEMPCOLOR 0x0000FF40
41 static gchar const *const grid_name[] = {
42 N_("Rectangular grid"),
43 N_("Axonometric grid")
44 };
45 static gchar const *const grid_svgname[] = {
46 "xygrid",
47 "axonomgrid"
48 };
51 // ##########################################################
52 // Grid CanvasItem
53 static void grid_canvasitem_class_init (GridCanvasItemClass *klass);
54 static void grid_canvasitem_init (GridCanvasItem *grid);
55 static void grid_canvasitem_destroy (GtkObject *object);
57 static void grid_canvasitem_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
58 static void grid_canvasitem_render (SPCanvasItem *item, SPCanvasBuf *buf);
60 static SPCanvasItemClass * parent_class;
62 GtkType
63 grid_canvasitem_get_type (void)
64 {
65 static GtkType grid_canvasitem_type = 0;
67 if (!grid_canvasitem_type) {
68 GtkTypeInfo grid_canvasitem_info = {
69 "GridCanvasItem",
70 sizeof (GridCanvasItem),
71 sizeof (GridCanvasItemClass),
72 (GtkClassInitFunc) grid_canvasitem_class_init,
73 (GtkObjectInitFunc) grid_canvasitem_init,
74 NULL, NULL,
75 (GtkClassInitFunc) NULL
76 };
77 grid_canvasitem_type = gtk_type_unique (sp_canvas_item_get_type (), &grid_canvasitem_info);
78 }
79 return grid_canvasitem_type;
80 }
82 static void
83 grid_canvasitem_class_init (GridCanvasItemClass *klass)
84 {
85 GtkObjectClass *object_class;
86 SPCanvasItemClass *item_class;
88 object_class = (GtkObjectClass *) klass;
89 item_class = (SPCanvasItemClass *) klass;
91 parent_class = (SPCanvasItemClass*)gtk_type_class (sp_canvas_item_get_type ());
93 object_class->destroy = grid_canvasitem_destroy;
95 item_class->update = grid_canvasitem_update;
96 item_class->render = grid_canvasitem_render;
97 }
99 static void
100 grid_canvasitem_init (GridCanvasItem *griditem)
101 {
102 griditem->grid = NULL;
103 }
105 static void
106 grid_canvasitem_destroy (GtkObject *object)
107 {
108 g_return_if_fail (object != NULL);
109 g_return_if_fail (INKSCAPE_IS_GRID_CANVASITEM (object));
111 if (GTK_OBJECT_CLASS (parent_class)->destroy)
112 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
113 }
115 /**
116 */
117 static void
118 grid_canvasitem_render (SPCanvasItem * item, SPCanvasBuf * buf)
119 {
120 GridCanvasItem *gridcanvasitem = INKSCAPE_GRID_CANVASITEM (item);
122 if ( gridcanvasitem->grid && gridcanvasitem->grid->isVisible() ) {
123 sp_canvas_prepare_buffer (buf);
124 gridcanvasitem->grid->Render(buf);
125 }
126 }
128 static void
129 grid_canvasitem_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
130 {
131 GridCanvasItem *gridcanvasitem = INKSCAPE_GRID_CANVASITEM (item);
133 if (parent_class->update)
134 (* parent_class->update) (item, affine, flags);
136 if (gridcanvasitem->grid) {
137 gridcanvasitem->grid->Update(affine, flags);
139 sp_canvas_request_redraw (item->canvas,
140 -1000000, -1000000,
141 1000000, 1000000);
143 item->x1 = item->y1 = -1000000;
144 item->x2 = item->y2 = 1000000;
145 }
146 }
150 // ##########################################################
151 // CanvasGrid
153 static Inkscape::XML::NodeEventVector const _repr_events = {
154 NULL, /* child_added */
155 NULL, /* child_removed */
156 CanvasGrid::on_repr_attr_changed,
157 NULL, /* content_changed */
158 NULL /* order_changed */
159 };
161 CanvasGrid::CanvasGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument *in_doc, GridType type)
162 : namelabel("", Gtk::ALIGN_CENTER), visible(true), snap_enabled(true), gridtype(type)
163 {
164 repr = in_repr;
165 doc = in_doc;
166 if (repr) {
167 repr->addListener (&_repr_events, this);
168 }
170 namedview = nv;
171 canvasitems = NULL;
173 Glib::ustring str("<b>");
174 str += getName();
175 str += "</b>";
176 namelabel.set_markup(str);
177 vbox.pack_start(namelabel, true, true);
179 _rcb_visible.init ( _("_Visible"),
180 _("Determines whether the grid is displayed or not. Objects are still snapped to invisible grids."),
181 "visible", _wr, false, repr, doc);
182 vbox.pack_start(*dynamic_cast<Gtk::Widget*>(_rcb_visible._button), true, true);
184 _rcb_snap_enabled.init ( _("_Snapping enabled"),
185 _("Determines whether to snap to this grid or not. Can be 'on' for invisible grids."),
186 "snap_enabled", _wr, false, repr, doc);
187 vbox.pack_start(*dynamic_cast<Gtk::Widget*>(_rcb_snap_enabled._button), true, true);
188 }
190 CanvasGrid::~CanvasGrid()
191 {
192 if (repr) {
193 repr->removeListenerByData (this);
194 }
196 while (canvasitems) {
197 gtk_object_destroy(GTK_OBJECT(canvasitems->data));
198 canvasitems = g_slist_remove(canvasitems, canvasitems->data);
199 }
200 }
202 const char *
203 CanvasGrid::getName()
204 {
205 return _(grid_name[gridtype]);
206 }
208 const char *
209 CanvasGrid::getSVGName()
210 {
211 return grid_svgname[gridtype];
212 }
214 GridType
215 CanvasGrid::getGridType()
216 {
217 return gridtype;
218 }
221 char const *
222 CanvasGrid::getName(GridType type)
223 {
224 return _(grid_name[type]);
225 }
227 char const *
228 CanvasGrid::getSVGName(GridType type)
229 {
230 return grid_svgname[type];
231 }
233 GridType
234 CanvasGrid::getGridTypeFromSVGName(char const *typestr)
235 {
236 if (!typestr) return GRID_RECTANGULAR;
238 gint t = 0;
239 for (t = GRID_MAXTYPENR; t >= 0; t--) { //this automatically defaults to grid0 which is rectangular grid
240 if (!strcmp(typestr, grid_svgname[t])) break;
241 }
242 return (GridType) t;
243 }
245 GridType
246 CanvasGrid::getGridTypeFromName(char const *typestr)
247 {
248 if (!typestr) return GRID_RECTANGULAR;
250 gint t = 0;
251 for (t = GRID_MAXTYPENR; t >= 0; t--) { //this automatically defaults to grid0 which is rectangular grid
252 if (!strcmp(typestr, _(grid_name[t]))) break;
253 }
254 return (GridType) t;
255 }
258 /*
259 * writes an <inkscape:grid> child to repr.
260 */
261 void
262 CanvasGrid::writeNewGridToRepr(Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype)
263 {
264 if (!repr) return;
265 if (gridtype > GRID_MAXTYPENR) return;
267 // 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.
269 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
270 Inkscape::XML::Node *newnode;
271 newnode = xml_doc->createElement("inkscape:grid");
272 newnode->setAttribute("type", getSVGName(gridtype));
274 repr->appendChild(newnode);
275 // Inkscape::GC::release(repr); FIX THIS. THIS SHOULD BE HERE!!!
277 sp_document_done(doc, SP_VERB_DIALOG_NAMEDVIEW, _("Create new grid"));
278 }
280 /*
281 * Creates a new CanvasGrid object of type gridtype
282 */
283 CanvasGrid*
284 CanvasGrid::NewGrid(SPNamedView * nv, Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype)
285 {
286 if (!repr) return NULL;
287 if (!doc) {
288 g_error("CanvasGrid::NewGrid - doc==NULL");
289 return NULL;
290 }
292 switch (gridtype) {
293 case GRID_RECTANGULAR:
294 return (CanvasGrid*) new CanvasXYGrid(nv, repr, doc);
295 case GRID_AXONOMETRIC:
296 return (CanvasGrid*) new CanvasAxonomGrid(nv, repr, doc);
297 }
299 return NULL;
300 }
303 /**
304 * creates a new grid canvasitem for the SPDesktop given as parameter. Keeps a link to this canvasitem in the canvasitems list.
305 */
306 GridCanvasItem *
307 CanvasGrid::createCanvasItem(SPDesktop * desktop)
308 {
309 if (!desktop) return NULL;
310 // Johan: I think for multiple desktops it is best if each has their own canvasitem,
311 // but share the same CanvasGrid object; that is what this function is for.
313 // check if there is already a canvasitem on this desktop linking to this grid
314 for (GSList *l = canvasitems; l != NULL; l = l->next) {
315 if ( sp_desktop_gridgroup(desktop) == SP_CANVAS_GROUP(SP_CANVAS_ITEM(l->data)->parent) ) {
316 return NULL;
317 }
318 }
320 GridCanvasItem * item = INKSCAPE_GRID_CANVASITEM( sp_canvas_item_new(sp_desktop_gridgroup(desktop), INKSCAPE_TYPE_GRID_CANVASITEM, NULL) );
321 item->grid = this;
322 sp_canvas_item_show(SP_CANVAS_ITEM(item));
324 gtk_object_ref(GTK_OBJECT(item)); // since we're keeping a link to this item, we need to bump up the ref count
325 canvasitems = g_slist_prepend(canvasitems, item);
327 return item;
328 }
330 void
331 CanvasGrid::on_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, void *data)
332 {
333 if (!data)
334 return;
336 ((CanvasGrid*) data)->onReprAttrChanged(repr, key, oldval, newval, is_interactive);
337 }
340 // ##########################################################
341 // CanvasXYGrid
344 /**
345 * "attach_all" function
346 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP TO QUICKLY GET RESULTS
347 *
348 * Helper function that attachs widgets in a 3xn table. The widgets come in an
349 * array that has two entries per table row. The two entries code for four
350 * possible cases: (0,0) means insert space in first column; (0, non-0) means
351 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
352 * (non-0, non-0) means two widgets in columns 2 and 3.
353 **/
354 #define SPACE_SIZE_X 15
355 #define SPACE_SIZE_Y 10
356 static inline void
357 attach_all(Gtk::Table &table, Gtk::Widget const *const arr[], unsigned size, int start = 0)
358 {
359 for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2) {
360 if (arr[i] && arr[i+1]) {
361 table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
362 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
363 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
364 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
365 } else {
366 if (arr[i+1]) {
367 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
368 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
369 } else if (arr[i]) {
370 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
371 label.set_alignment (0.0);
372 table.attach (label, 0, 3, r, r+1,
373 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
374 } else {
375 Gtk::HBox *space = manage (new Gtk::HBox);
376 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
377 table.attach (*space, 0, 1, r, r+1,
378 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
379 }
380 }
381 ++r;
382 }
383 }
385 CanvasXYGrid::CanvasXYGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
386 : CanvasGrid(nv, in_repr, in_doc, GRID_RECTANGULAR), table(1, 1)
387 {
388 origin[NR::X] = origin[NR::Y] = 0.0;
389 color = DEFAULTGRIDCOLOR;
390 empcolor = DEFAULTGRIDEMPCOLOR;
391 empspacing = 5;
392 spacing[NR::X] = spacing[NR::Y] = 1.0;
393 gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
394 render_dotted = false;
396 snapper = new CanvasXYGridSnapper(this, namedview, 0);
398 // initialize widgets:
399 vbox.set_border_width(2);
400 table.set_spacings(2);
401 vbox.pack_start(table, false, false, 0);
403 Inkscape::UI::Widget::ScalarUnit * sutemp;
404 _rumg.init (_("Grid _units:"), "units", _wr, repr, doc);
405 _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"),
406 "originx", _rumg, _wr, repr, doc);
407 sutemp = _rsu_ox.getSU();
408 sutemp->setDigits(4);
409 sutemp->setIncrements(0.1, 1.0);
410 _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"),
411 "originy", _rumg, _wr, repr, doc);
412 sutemp = _rsu_oy.getSU();
413 sutemp->setDigits(4);
414 sutemp->setIncrements(0.1, 1.0);
415 _rsu_sx.init (_("Spacing _X:"), _("Distance between vertical grid lines"),
416 "spacingx", _rumg, _wr, repr, doc);
417 sutemp = _rsu_sx.getSU();
418 sutemp->setDigits(4);
419 sutemp->setIncrements(0.1, 1.0);
420 _rsu_sy.init (_("Spacing _Y:"), _("Distance between horizontal grid lines"),
421 "spacingy", _rumg, _wr, repr, doc);
422 sutemp = _rsu_sy.getSU();
423 sutemp->setDigits(4);
424 sutemp->setIncrements(0.1, 1.0);
425 _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"),
426 _("Color of grid lines"), "color", "opacity", _wr, repr, doc);
427 _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"),
428 _("Color of the major (highlighted) grid lines"),
429 "empcolor", "empopacity", _wr, repr, doc);
430 _rsi.init (_("_Major grid line every:"), _("lines"), "empspacing", _wr, repr, doc);
431 _rcb_dotted.init ( _("_Show dots instead of lines"),
432 _("If set, displays dots at gridpoints instead of gridlines"),
433 "dotted", _wr, false, repr, doc);
435 Gtk::Widget const *const widget_array[] = {
436 _rumg._label, _rumg._sel,
437 0, _rsu_ox.getSU(),
438 0, _rsu_oy.getSU(),
439 0, _rsu_sx.getSU(),
440 0, _rsu_sy.getSU(),
441 _rcp_gcol._label, _rcp_gcol._cp,
442 0, 0,
443 _rcp_gmcol._label, _rcp_gmcol._cp,
444 _rsi._label, &_rsi._hbox,
445 0, _rcb_dotted._button,
446 };
448 attach_all (table, widget_array, sizeof(widget_array));
450 vbox.show();
452 if (repr) readRepr();
453 updateWidgets();
454 }
456 CanvasXYGrid::~CanvasXYGrid ()
457 {
458 if (snapper) delete snapper;
459 }
462 /* fixme: Collect all these length parsing methods and think common sane API */
464 static gboolean
465 sp_nv_read_length(gchar const *str, guint base, gdouble *val, SPUnit const **unit)
466 {
467 if (!str) {
468 return FALSE;
469 }
471 gchar *u;
472 gdouble v = g_ascii_strtod(str, &u);
473 if (!u) {
474 return FALSE;
475 }
476 while (isspace(*u)) {
477 u += 1;
478 }
480 if (!*u) {
481 /* No unit specified - keep default */
482 *val = v;
483 return TRUE;
484 }
486 if (base & SP_UNIT_DEVICE) {
487 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
488 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
489 *val = v;
490 return TRUE;
491 }
492 }
494 if (base & SP_UNIT_ABSOLUTE) {
495 if (!strncmp(u, "pt", 2)) {
496 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
497 } else if (!strncmp(u, "mm", 2)) {
498 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
499 } else if (!strncmp(u, "cm", 2)) {
500 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
501 } else if (!strncmp(u, "m", 1)) {
502 *unit = &sp_unit_get_by_id(SP_UNIT_M);
503 } else if (!strncmp(u, "in", 2)) {
504 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
505 } else {
506 return FALSE;
507 }
508 *val = v;
509 return TRUE;
510 }
512 return FALSE;
513 }
515 static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
516 {
517 if (!str) {
518 return FALSE;
519 }
521 gchar *u;
522 gdouble v = g_ascii_strtod(str, &u);
523 if (!u) {
524 return FALSE;
525 }
526 v = CLAMP(v, 0.0, 1.0);
528 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
530 return TRUE;
531 }
533 /** If the passed scalar is invalid (<=0), then set the widget and the scalar
534 to use the given old value.
536 @param oldVal Old value to use if the new one is invalid.
537 @param pTarget The scalar to validate.
538 @param widget Widget associated with the scalar.
539 */
540 static void validateScalar(double oldVal,
541 double* pTarget,
542 Inkscape::UI::Widget::RegisteredScalarUnit& widget)
543 {
544 // Avoid nullness.
545 if ( pTarget == NULL )
546 return;
548 // Invalid new value?
549 if ( *pTarget <= 0 ) {
550 // If the old value is somehow invalid as well, then default to 1.
551 if ( oldVal <= 0 )
552 oldVal = 1;
554 // Reset the scalar and associated widget to the old value.
555 *pTarget = oldVal;
556 widget.setValue( *pTarget);
557 } //if
559 } //validateScalar
562 /** If the passed int is invalid (<=0), then set the widget and the int
563 to use the given old value.
565 @param oldVal Old value to use if the new one is invalid.
566 @param pTarget The int to validate.
567 @param widget Widget associated with the int.
568 */
569 static void validateInt(gint oldVal,
570 gint* pTarget,
571 Inkscape::UI::Widget::RegisteredSuffixedInteger& widget)
572 {
573 // Avoid nullness.
574 if ( pTarget == NULL )
575 return;
577 // Invalid new value?
578 if ( *pTarget <= 0 ) {
579 // If the old value is somehow invalid as well, then default to 1.
580 if ( oldVal <= 0 )
581 oldVal = 1;
583 // Reset the int and associated widget to the old value.
584 *pTarget = oldVal;
585 widget.setValue( *pTarget);
586 } //if
588 } //validateInt
590 void
591 CanvasXYGrid::readRepr()
592 {
593 gchar const *value;
594 if ( (value = repr->attribute("originx")) ) {
595 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::X], &gridunit);
596 origin[NR::X] = sp_units_get_pixels(origin[NR::X], *(gridunit));
597 }
599 if ( (value = repr->attribute("originy")) ) {
600 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::Y], &gridunit);
601 origin[NR::Y] = sp_units_get_pixels(origin[NR::Y], *(gridunit));
602 }
604 if ( (value = repr->attribute("spacingx")) ) {
605 double oldVal = spacing[NR::X];
606 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::X], &gridunit);
607 validateScalar( oldVal, &spacing[NR::X], _rsu_sx );
608 spacing[NR::X] = sp_units_get_pixels(spacing[NR::X], *(gridunit));
610 }
611 if ( (value = repr->attribute("spacingy")) ) {
612 double oldVal = spacing[NR::Y];
613 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::Y], &gridunit);
614 validateScalar( oldVal, &spacing[NR::Y], _rsu_sy );
615 spacing[NR::Y] = sp_units_get_pixels(spacing[NR::Y], *(gridunit));
617 }
619 if ( (value = repr->attribute("color")) ) {
620 color = (color & 0xff) | sp_svg_read_color(value, color);
621 }
623 if ( (value = repr->attribute("empcolor")) ) {
624 empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
625 }
627 if ( (value = repr->attribute("opacity")) ) {
628 sp_nv_read_opacity(value, &color);
629 }
630 if ( (value = repr->attribute("empopacity")) ) {
631 sp_nv_read_opacity(value, &empcolor);
632 }
634 if ( (value = repr->attribute("empspacing")) ) {
635 gint oldVal = empspacing;
636 empspacing = atoi(value);
637 validateInt( oldVal, &empspacing, _rsi );
638 }
640 if ( (value = repr->attribute("dotted")) ) {
641 render_dotted = (strcmp(value,"true") == 0);
642 }
644 if ( (value = repr->attribute("visible")) ) {
645 visible = (strcmp(value,"true") == 0);
646 }
648 for (GSList *l = canvasitems; l != NULL; l = l->next) {
649 sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
650 }
652 return;
653 }
655 /**
656 * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
657 */
658 void
659 CanvasXYGrid::onReprAttrChanged(Inkscape::XML::Node */*repr*/, gchar const */*key*/, gchar const */*oldval*/, gchar const */*newval*/, bool /*is_interactive*/)
660 {
661 readRepr();
663 if ( ! (_wr.isUpdating()) )
664 updateWidgets();
665 }
670 Gtk::Widget &
671 CanvasXYGrid::getWidget()
672 {
673 return vbox;
674 }
677 /**
678 * Update dialog widgets from object's values.
679 */
680 void
681 CanvasXYGrid::updateWidgets()
682 {
683 if (_wr.isUpdating()) return;
685 _wr.setUpdating (true);
687 _rcb_visible.setActive(visible);
688 _rcb_snap_enabled.setActive(snap_enabled);
690 _rumg.setUnit (gridunit);
692 gdouble val;
693 val = origin[NR::X];
694 val = sp_pixels_get_units (val, *(gridunit));
695 _rsu_ox.setValue (val);
696 val = origin[NR::Y];
697 val = sp_pixels_get_units (val, *(gridunit));
698 _rsu_oy.setValue (val);
699 val = spacing[NR::X];
700 double gridx = sp_pixels_get_units (val, *(gridunit));
701 _rsu_sx.setValue (gridx);
702 val = spacing[NR::Y];
703 double gridy = sp_pixels_get_units (val, *(gridunit));
704 _rsu_sy.setValue (gridy);
706 _rcp_gcol.setRgba32 (color);
707 _rcp_gmcol.setRgba32 (empcolor);
708 _rsi.setValue (empspacing);
710 _rcb_dotted.setActive(render_dotted);
712 _wr.setUpdating (false);
714 return;
715 }
719 void
720 CanvasXYGrid::Update (NR::Matrix const &affine, unsigned int /*flags*/)
721 {
722 ow = origin * affine;
723 sw = spacing * affine;
724 sw -= NR::Point(affine[4], affine[5]);
726 for(int dim = 0; dim < 2; dim++) {
727 gint scaling_factor = empspacing;
729 if (scaling_factor <= 1)
730 scaling_factor = 5;
732 scaled[dim] = FALSE;
733 sw[dim] = fabs (sw[dim]);
734 while (sw[dim] < 8.0) {
735 scaled[dim] = TRUE;
736 sw[dim] *= scaling_factor;
737 /* First pass, go up to the major line spacing, then
738 keep increasing by two. */
739 scaling_factor = 2;
740 }
741 }
742 }
745 static void
746 grid_hline (SPCanvasBuf *buf, gint y, gint xs, gint xe, guint32 rgba)
747 {
748 if ((y >= buf->rect.y0) && (y < buf->rect.y1)) {
749 guint r, g, b, a;
750 gint x0, x1, x;
751 guchar *p;
752 r = NR_RGBA32_R (rgba);
753 g = NR_RGBA32_G (rgba);
754 b = NR_RGBA32_B (rgba);
755 a = NR_RGBA32_A (rgba);
756 x0 = MAX (buf->rect.x0, xs);
757 x1 = MIN (buf->rect.x1, xe + 1);
758 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x0 - buf->rect.x0) * 3;
759 for (x = x0; x < x1; x++) {
760 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
761 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
762 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
763 p += 3;
764 }
765 }
766 }
768 static void
769 grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
770 {
771 if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
772 guint r, g, b, a;
773 gint y0, y1, y;
774 guchar *p;
775 r = NR_RGBA32_R(rgba);
776 g = NR_RGBA32_G (rgba);
777 b = NR_RGBA32_B (rgba);
778 a = NR_RGBA32_A (rgba);
779 y0 = MAX (buf->rect.y0, ys);
780 y1 = MIN (buf->rect.y1, ye + 1);
781 p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
782 for (y = y0; y < y1; y++) {
783 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
784 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
785 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
786 p += buf->buf_rowstride;
787 }
788 }
789 }
791 static void
792 grid_dot (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
793 {
794 if ( (y >= buf->rect.y0) && (y < buf->rect.y1)
795 && (x >= buf->rect.x0) && (x < buf->rect.x1) ) {
796 guint r, g, b, a;
797 guchar *p;
798 r = NR_RGBA32_R (rgba);
799 g = NR_RGBA32_G (rgba);
800 b = NR_RGBA32_B (rgba);
801 a = NR_RGBA32_A (rgba);
802 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
803 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
804 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
805 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
806 }
807 }
809 void
810 CanvasXYGrid::Render (SPCanvasBuf *buf)
811 {
812 gdouble const sxg = floor ((buf->rect.x0 - ow[NR::X]) / sw[NR::X]) * sw[NR::X] + ow[NR::X];
813 gint const xlinestart = (gint) Inkscape::round((sxg - ow[NR::X]) / sw[NR::X]);
814 gdouble const syg = floor ((buf->rect.y0 - ow[NR::Y]) / sw[NR::Y]) * sw[NR::Y] + ow[NR::Y];
815 gint const ylinestart = (gint) Inkscape::round((syg - ow[NR::Y]) / sw[NR::Y]);
817 if (!render_dotted) {
818 gint ylinenum;
819 gdouble y;
820 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
821 gint const y0 = (gint) Inkscape::round(y);
823 if (!scaled[NR::Y] && (ylinenum % empspacing) == 0) {
824 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, empcolor);
825 } else {
826 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, color);
827 }
828 }
830 gint xlinenum;
831 gdouble x;
832 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
833 gint const ix = (gint) Inkscape::round(x);
834 if (!scaled[NR::X] && (xlinenum % empspacing) == 0) {
835 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, empcolor);
836 } else {
837 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, color);
838 }
839 }
840 } else {
841 gint ylinenum;
842 gdouble y;
843 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
844 gint const iy = (gint) Inkscape::round(y);
846 gint xlinenum;
847 gdouble x;
848 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
849 gint const ix = (gint) Inkscape::round(x);
850 if ( (!scaled[NR::X] && (xlinenum % empspacing) == 0)
851 || (!scaled[NR::Y] && (ylinenum % empspacing) == 0) )
852 {
853 grid_dot (buf, ix, iy, empcolor | (guint32)0x000000FF); // put alpha to max value
854 } else {
855 grid_dot (buf, ix, iy, color | (guint32)0x000000FF); // put alpha to max value
856 }
857 }
859 }
860 }
861 }
863 CanvasXYGridSnapper::CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
864 {
865 this->grid = grid;
866 }
868 LineSnapper::LineList
869 CanvasXYGridSnapper::_getSnapLines(NR::Point const &p) const
870 {
871 LineList s;
873 if ( grid == NULL ) {
874 return s;
875 }
877 for (unsigned int i = 0; i < 2; ++i) {
879 /* This is to make sure we snap to only visible grid lines */
880 double scaled_spacing = grid->sw[i]; // this is spacing of visible lines if screen pixels
882 // convert screen pixels to px
883 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
884 if (SP_ACTIVE_DESKTOP) {
885 scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
886 }
888 NR::Coord rounded;
889 NR::Point point_on_line;
891 rounded = Inkscape::Util::round_to_upper_multiple_plus(p[i], scaled_spacing, grid->origin[i]);
892 point_on_line = i ? NR::Point(0, rounded) : NR::Point(rounded, 0);
893 s.push_back(std::make_pair(component_vectors[i], point_on_line));
895 rounded = Inkscape::Util::round_to_lower_multiple_plus(p[i], scaled_spacing, grid->origin[i]);
896 point_on_line = i ? NR::Point(0, rounded) : NR::Point(rounded, 0);
897 s.push_back(std::make_pair(component_vectors[i], point_on_line));
898 }
900 return s;
901 }
903 void CanvasXYGridSnapper::_addSnappedLine(SnappedConstraints &sc, NR::Point const snapped_point, NR::Coord const snapped_distance, NR::Point const normal_to_line, NR::Point const point_on_line) const
904 {
905 SnappedLine dummy = SnappedLine(snapped_point, snapped_distance, normal_to_line, point_on_line);
906 sc.grid_lines.push_back(dummy);
907 }
912 }; /* namespace Inkscape */
915 /*
916 Local Variables:
917 mode:c++
918 c-file-style:"stroustrup"
919 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
920 indent-tabs-mode:nil
921 fill-column:99
922 End:
923 */
924 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :