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"
32 #include "prefs-utils.h"
34 #include "canvas-grid.h"
35 #include "canvas-axonomgrid.h"
37 namespace Inkscape {
39 static gchar const *const grid_name[] = {
40 N_("Rectangular grid"),
41 N_("Axonometric grid")
42 };
43 static gchar const *const grid_svgname[] = {
44 "xygrid",
45 "axonomgrid"
46 };
49 // ##########################################################
50 // Grid CanvasItem
51 static void grid_canvasitem_class_init (GridCanvasItemClass *klass);
52 static void grid_canvasitem_init (GridCanvasItem *grid);
53 static void grid_canvasitem_destroy (GtkObject *object);
55 static void grid_canvasitem_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
56 static void grid_canvasitem_render (SPCanvasItem *item, SPCanvasBuf *buf);
58 static SPCanvasItemClass * parent_class;
60 GtkType
61 grid_canvasitem_get_type (void)
62 {
63 static GtkType grid_canvasitem_type = 0;
65 if (!grid_canvasitem_type) {
66 GtkTypeInfo grid_canvasitem_info = {
67 "GridCanvasItem",
68 sizeof (GridCanvasItem),
69 sizeof (GridCanvasItemClass),
70 (GtkClassInitFunc) grid_canvasitem_class_init,
71 (GtkObjectInitFunc) grid_canvasitem_init,
72 NULL, NULL,
73 (GtkClassInitFunc) NULL
74 };
75 grid_canvasitem_type = gtk_type_unique (sp_canvas_item_get_type (), &grid_canvasitem_info);
76 }
77 return grid_canvasitem_type;
78 }
80 static void
81 grid_canvasitem_class_init (GridCanvasItemClass *klass)
82 {
83 GtkObjectClass *object_class;
84 SPCanvasItemClass *item_class;
86 object_class = (GtkObjectClass *) klass;
87 item_class = (SPCanvasItemClass *) klass;
89 parent_class = (SPCanvasItemClass*)gtk_type_class (sp_canvas_item_get_type ());
91 object_class->destroy = grid_canvasitem_destroy;
93 item_class->update = grid_canvasitem_update;
94 item_class->render = grid_canvasitem_render;
95 }
97 static void
98 grid_canvasitem_init (GridCanvasItem *griditem)
99 {
100 griditem->grid = NULL;
101 }
103 static void
104 grid_canvasitem_destroy (GtkObject *object)
105 {
106 g_return_if_fail (object != NULL);
107 g_return_if_fail (INKSCAPE_IS_GRID_CANVASITEM (object));
109 if (GTK_OBJECT_CLASS (parent_class)->destroy)
110 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
111 }
113 /**
114 */
115 static void
116 grid_canvasitem_render (SPCanvasItem * item, SPCanvasBuf * buf)
117 {
118 GridCanvasItem *gridcanvasitem = INKSCAPE_GRID_CANVASITEM (item);
120 if ( gridcanvasitem->grid && gridcanvasitem->grid->isVisible() ) {
121 sp_canvas_prepare_buffer (buf);
122 gridcanvasitem->grid->Render(buf);
123 }
124 }
126 static void
127 grid_canvasitem_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
128 {
129 GridCanvasItem *gridcanvasitem = INKSCAPE_GRID_CANVASITEM (item);
131 if (parent_class->update)
132 (* parent_class->update) (item, affine, flags);
134 if (gridcanvasitem->grid) {
135 gridcanvasitem->grid->Update(affine, flags);
137 sp_canvas_request_redraw (item->canvas,
138 -1000000, -1000000,
139 1000000, 1000000);
141 item->x1 = item->y1 = -1000000;
142 item->x2 = item->y2 = 1000000;
143 }
144 }
148 // ##########################################################
149 // CanvasGrid
151 static Inkscape::XML::NodeEventVector const _repr_events = {
152 NULL, /* child_added */
153 NULL, /* child_removed */
154 CanvasGrid::on_repr_attr_changed,
155 NULL, /* content_changed */
156 NULL /* order_changed */
157 };
159 CanvasGrid::CanvasGrid(SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument *in_doc, GridType type)
160 : namelabel("", Gtk::ALIGN_CENTER), visible(true), gridtype(type)
161 {
162 repr = in_repr;
163 doc = in_doc;
164 if (repr) {
165 repr->addListener (&_repr_events, this);
166 }
168 namedview = nv;
169 canvasitems = NULL;
171 Glib::ustring str("<b>");
172 str += getName();
173 str += "</b>";
174 namelabel.set_markup(str);
175 vbox.pack_start(namelabel, true, true);
177 _rcb_enabled.init ( _("_Enabled"),
178 _("Determines whether to snap to this grid or not. Can be 'on' for invisible grids."),
179 "enabled", _wr, false, repr, doc);
180 vbox.pack_start(*dynamic_cast<Gtk::Widget*>(_rcb_enabled._button), true, true);
182 _rcb_visible.init ( _("_Visible"),
183 _("Determines whether the grid is displayed or not. Objects are still snapped to invisible grids."),
184 "visible", _wr, true, repr, doc);
185 vbox.pack_start(*dynamic_cast<Gtk::Widget*>(_rcb_visible._button), true, true);
186 }
188 CanvasGrid::~CanvasGrid()
189 {
190 if (repr) {
191 repr->removeListenerByData (this);
192 }
194 while (canvasitems) {
195 gtk_object_destroy(GTK_OBJECT(canvasitems->data));
196 canvasitems = g_slist_remove(canvasitems, canvasitems->data);
197 }
198 }
200 const char *
201 CanvasGrid::getName()
202 {
203 return _(grid_name[gridtype]);
204 }
206 const char *
207 CanvasGrid::getSVGName()
208 {
209 return grid_svgname[gridtype];
210 }
212 GridType
213 CanvasGrid::getGridType()
214 {
215 return gridtype;
216 }
219 char const *
220 CanvasGrid::getName(GridType type)
221 {
222 return _(grid_name[type]);
223 }
225 char const *
226 CanvasGrid::getSVGName(GridType type)
227 {
228 return grid_svgname[type];
229 }
231 GridType
232 CanvasGrid::getGridTypeFromSVGName(char const *typestr)
233 {
234 if (!typestr) return GRID_RECTANGULAR;
236 gint t = 0;
237 for (t = GRID_MAXTYPENR; t >= 0; t--) { //this automatically defaults to grid0 which is rectangular grid
238 if (!strcmp(typestr, grid_svgname[t])) break;
239 }
240 return (GridType) t;
241 }
243 GridType
244 CanvasGrid::getGridTypeFromName(char const *typestr)
245 {
246 if (!typestr) return GRID_RECTANGULAR;
248 gint t = 0;
249 for (t = GRID_MAXTYPENR; t >= 0; t--) { //this automatically defaults to grid0 which is rectangular grid
250 if (!strcmp(typestr, _(grid_name[t]))) break;
251 }
252 return (GridType) t;
253 }
256 /*
257 * writes an <inkscape:grid> child to repr.
258 */
259 void
260 CanvasGrid::writeNewGridToRepr(Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype)
261 {
262 if (!repr) return;
263 if (gridtype > GRID_MAXTYPENR) return;
265 // 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.
267 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
268 Inkscape::XML::Node *newnode;
269 newnode = xml_doc->createElement("inkscape:grid");
270 newnode->setAttribute("type", getSVGName(gridtype));
272 repr->appendChild(newnode);
273 Inkscape::GC::release(newnode);
275 sp_document_done(doc, SP_VERB_DIALOG_NAMEDVIEW, _("Create new grid"));
276 }
278 /*
279 * Creates a new CanvasGrid object of type gridtype
280 */
281 CanvasGrid*
282 CanvasGrid::NewGrid(SPNamedView * nv, Inkscape::XML::Node * repr, SPDocument * doc, GridType gridtype)
283 {
284 if (!repr) return NULL;
285 if (!doc) {
286 g_error("CanvasGrid::NewGrid - doc==NULL");
287 return NULL;
288 }
290 switch (gridtype) {
291 case GRID_RECTANGULAR:
292 return (CanvasGrid*) new CanvasXYGrid(nv, repr, doc);
293 case GRID_AXONOMETRIC:
294 return (CanvasGrid*) new CanvasAxonomGrid(nv, repr, doc);
295 }
297 return NULL;
298 }
301 /**
302 * creates a new grid canvasitem for the SPDesktop given as parameter. Keeps a link to this canvasitem in the canvasitems list.
303 */
304 GridCanvasItem *
305 CanvasGrid::createCanvasItem(SPDesktop * desktop)
306 {
307 if (!desktop) return NULL;
308 // Johan: I think for multiple desktops it is best if each has their own canvasitem,
309 // but share the same CanvasGrid object; that is what this function is for.
311 // check if there is already a canvasitem on this desktop linking to this grid
312 for (GSList *l = canvasitems; l != NULL; l = l->next) {
313 if ( sp_desktop_gridgroup(desktop) == SP_CANVAS_GROUP(SP_CANVAS_ITEM(l->data)->parent) ) {
314 return NULL;
315 }
316 }
318 GridCanvasItem * item = INKSCAPE_GRID_CANVASITEM( sp_canvas_item_new(sp_desktop_gridgroup(desktop), INKSCAPE_TYPE_GRID_CANVASITEM, NULL) );
319 item->grid = this;
320 sp_canvas_item_show(SP_CANVAS_ITEM(item));
322 gtk_object_ref(GTK_OBJECT(item)); // since we're keeping a link to this item, we need to bump up the ref count
323 canvasitems = g_slist_prepend(canvasitems, item);
325 return item;
326 }
328 void
329 CanvasGrid::on_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, void *data)
330 {
331 if (!data)
332 return;
334 ((CanvasGrid*) data)->onReprAttrChanged(repr, key, oldval, newval, is_interactive);
335 }
337 bool CanvasGrid::isEnabled()
338 {
339 if (snapper == NULL) {
340 return false;
341 }
343 return snapper->getEnabled();
344 }
346 // ##########################################################
347 // CanvasXYGrid
350 /**
351 * "attach_all" function
352 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP TO QUICKLY GET RESULTS
353 *
354 * Helper function that attachs widgets in a 3xn table. The widgets come in an
355 * array that has two entries per table row. The two entries code for four
356 * possible cases: (0,0) means insert space in first column; (0, non-0) means
357 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
358 * (non-0, non-0) means two widgets in columns 2 and 3.
359 **/
360 #define SPACE_SIZE_X 15
361 #define SPACE_SIZE_Y 10
362 static inline void
363 attach_all(Gtk::Table &table, Gtk::Widget const *const arr[], unsigned size, int start = 0)
364 {
365 for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2) {
366 if (arr[i] && arr[i+1]) {
367 table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
368 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
369 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
370 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
371 } else {
372 if (arr[i+1]) {
373 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
374 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
375 } else if (arr[i]) {
376 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
377 label.set_alignment (0.0);
378 table.attach (label, 0, 3, r, r+1,
379 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
380 } else {
381 Gtk::HBox *space = manage (new Gtk::HBox);
382 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
383 table.attach (*space, 0, 1, r, r+1,
384 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
385 }
386 }
387 ++r;
388 }
389 }
391 CanvasXYGrid::CanvasXYGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
392 : CanvasGrid(nv, in_repr, in_doc, GRID_RECTANGULAR), table(1, 1)
393 {
394 gridunit = sp_unit_get_by_abbreviation( prefs_get_string_attribute("options.grids.xy", "units") );
395 if (!gridunit)
396 gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
397 origin[NR::X] = sp_units_get_pixels( prefs_get_double_attribute ("options.grids.xy", "origin_x", 0.0), *(gridunit) );
398 origin[NR::Y] = sp_units_get_pixels( prefs_get_double_attribute ("options.grids.xy", "origin_y", 0.0), *(gridunit) );
399 color = prefs_get_int_attribute("options.grids.xy", "color", 0x0000ff20);
400 empcolor = prefs_get_int_attribute("options.grids.xy", "empcolor", 0x0000ff40);
401 empspacing = prefs_get_int_attribute("options.grids.xy", "empspacing", 5);
402 spacing[NR::X] = sp_units_get_pixels( prefs_get_double_attribute ("options.grids.xy", "spacing_x", 0.0), *(gridunit) );
403 spacing[NR::Y] = sp_units_get_pixels( prefs_get_double_attribute ("options.grids.xy", "spacing_y", 0.0), *(gridunit) );
404 render_dotted = prefs_get_int_attribute ("options.grids.xy", "dotted", 0) == 1;
406 snapper = new CanvasXYGridSnapper(this, namedview, 0);
408 // initialize widgets:
409 vbox.set_border_width(2);
410 table.set_spacings(2);
411 vbox.pack_start(table, false, false, 0);
413 _wr.setUpdating (true);
414 Inkscape::UI::Widget::ScalarUnit * sutemp;
415 _rumg.init (_("Grid _units:"), "units", _wr, repr, doc);
416 _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"),
417 "originx", _rumg, _wr, repr, doc);
418 sutemp = _rsu_ox.getSU();
419 sutemp->setDigits(4);
420 sutemp->setIncrements(0.1, 1.0);
421 _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"),
422 "originy", _rumg, _wr, repr, doc);
423 sutemp = _rsu_oy.getSU();
424 sutemp->setDigits(4);
425 sutemp->setIncrements(0.1, 1.0);
426 _rsu_sx.init (_("Spacing _X:"), _("Distance between vertical grid lines"),
427 "spacingx", _rumg, _wr, repr, doc);
428 sutemp = _rsu_sx.getSU();
429 sutemp->setDigits(4);
430 sutemp->setIncrements(0.1, 1.0);
431 _rsu_sy.init (_("Spacing _Y:"), _("Distance between horizontal grid lines"),
432 "spacingy", _rumg, _wr, repr, doc);
433 sutemp = _rsu_sy.getSU();
434 sutemp->setDigits(4);
435 sutemp->setIncrements(0.1, 1.0);
436 _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"),
437 _("Color of grid lines"), "color", "opacity", _wr, repr, doc);
438 _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"),
439 _("Color of the major (highlighted) grid lines"),
440 "empcolor", "empopacity", _wr, repr, doc);
441 _rsi.init (_("_Major grid line every:"), _("lines"), "empspacing", _wr, repr, doc);
442 _rcb_dotted.init ( _("_Show dots instead of lines"),
443 _("If set, displays dots at gridpoints instead of gridlines"),
444 "dotted", _wr, false, repr, doc);
445 _wr.setUpdating (false);
447 Gtk::Widget const *const widget_array[] = {
448 _rumg._label, _rumg._sel,
449 0, _rsu_ox.getSU(),
450 0, _rsu_oy.getSU(),
451 0, _rsu_sx.getSU(),
452 0, _rsu_sy.getSU(),
453 _rcp_gcol._label, _rcp_gcol._cp,
454 0, 0,
455 _rcp_gmcol._label, _rcp_gmcol._cp,
456 _rsi._label, &_rsi._hbox,
457 0, _rcb_dotted._button,
458 };
460 attach_all (table, widget_array, sizeof(widget_array));
462 vbox.show();
464 if (repr) readRepr();
465 updateWidgets();
466 }
468 CanvasXYGrid::~CanvasXYGrid ()
469 {
470 if (snapper) delete snapper;
471 }
474 /* fixme: Collect all these length parsing methods and think common sane API */
476 static gboolean
477 sp_nv_read_length(gchar const *str, guint base, gdouble *val, SPUnit const **unit)
478 {
479 if (!str) {
480 return FALSE;
481 }
483 gchar *u;
484 gdouble v = g_ascii_strtod(str, &u);
485 if (!u) {
486 return FALSE;
487 }
488 while (isspace(*u)) {
489 u += 1;
490 }
492 if (!*u) {
493 /* No unit specified - keep default */
494 *val = v;
495 return TRUE;
496 }
498 if (base & SP_UNIT_DEVICE) {
499 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
500 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
501 *val = v;
502 return TRUE;
503 }
504 }
506 if (base & SP_UNIT_ABSOLUTE) {
507 if (!strncmp(u, "pt", 2)) {
508 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
509 } else if (!strncmp(u, "mm", 2)) {
510 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
511 } else if (!strncmp(u, "cm", 2)) {
512 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
513 } else if (!strncmp(u, "m", 1)) {
514 *unit = &sp_unit_get_by_id(SP_UNIT_M);
515 } else if (!strncmp(u, "in", 2)) {
516 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
517 } else {
518 return FALSE;
519 }
520 *val = v;
521 return TRUE;
522 }
524 return FALSE;
525 }
527 static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
528 {
529 if (!str) {
530 return FALSE;
531 }
533 gchar *u;
534 gdouble v = g_ascii_strtod(str, &u);
535 if (!u) {
536 return FALSE;
537 }
538 v = CLAMP(v, 0.0, 1.0);
540 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
542 return TRUE;
543 }
545 /** If the passed scalar is invalid (<=0), then set the widget and the scalar
546 to use the given old value.
548 @param oldVal Old value to use if the new one is invalid.
549 @param pTarget The scalar to validate.
550 @param widget Widget associated with the scalar.
551 */
552 static void validateScalar(double oldVal,
553 double* pTarget,
554 Inkscape::UI::Widget::RegisteredScalarUnit& widget)
555 {
556 // Avoid nullness.
557 if ( pTarget == NULL )
558 return;
560 // Invalid new value?
561 if ( *pTarget <= 0 ) {
562 // If the old value is somehow invalid as well, then default to 1.
563 if ( oldVal <= 0 )
564 oldVal = 1;
566 // Reset the scalar and associated widget to the old value.
567 *pTarget = oldVal;
568 widget.setValue( *pTarget);
569 } //if
571 } //validateScalar
574 /** If the passed int is invalid (<=0), then set the widget and the int
575 to use the given old value.
577 @param oldVal Old value to use if the new one is invalid.
578 @param pTarget The int to validate.
579 @param widget Widget associated with the int.
580 */
581 static void validateInt(gint oldVal,
582 gint* pTarget,
583 Inkscape::UI::Widget::RegisteredSuffixedInteger& widget)
584 {
585 // Avoid nullness.
586 if ( pTarget == NULL )
587 return;
589 // Invalid new value?
590 if ( *pTarget <= 0 ) {
591 // If the old value is somehow invalid as well, then default to 1.
592 if ( oldVal <= 0 )
593 oldVal = 1;
595 // Reset the int and associated widget to the old value.
596 *pTarget = oldVal;
597 widget.setValue( *pTarget);
598 } //if
600 } //validateInt
602 void
603 CanvasXYGrid::readRepr()
604 {
605 gchar const *value;
606 if ( (value = repr->attribute("originx")) ) {
607 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::X], &gridunit);
608 origin[NR::X] = sp_units_get_pixels(origin[NR::X], *(gridunit));
609 }
611 if ( (value = repr->attribute("originy")) ) {
612 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::Y], &gridunit);
613 origin[NR::Y] = sp_units_get_pixels(origin[NR::Y], *(gridunit));
614 }
616 if ( (value = repr->attribute("spacingx")) ) {
617 double oldVal = spacing[NR::X];
618 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::X], &gridunit);
619 validateScalar( oldVal, &spacing[NR::X], _rsu_sx );
620 spacing[NR::X] = sp_units_get_pixels(spacing[NR::X], *(gridunit));
622 }
623 if ( (value = repr->attribute("spacingy")) ) {
624 double oldVal = spacing[NR::Y];
625 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::Y], &gridunit);
626 validateScalar( oldVal, &spacing[NR::Y], _rsu_sy );
627 spacing[NR::Y] = sp_units_get_pixels(spacing[NR::Y], *(gridunit));
629 }
631 if ( (value = repr->attribute("color")) ) {
632 color = (color & 0xff) | sp_svg_read_color(value, color);
633 }
635 if ( (value = repr->attribute("empcolor")) ) {
636 empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
637 }
639 if ( (value = repr->attribute("opacity")) ) {
640 sp_nv_read_opacity(value, &color);
641 }
642 if ( (value = repr->attribute("empopacity")) ) {
643 sp_nv_read_opacity(value, &empcolor);
644 }
646 if ( (value = repr->attribute("empspacing")) ) {
647 gint oldVal = empspacing;
648 empspacing = atoi(value);
649 validateInt( oldVal, &empspacing, _rsi );
650 }
652 if ( (value = repr->attribute("dotted")) ) {
653 render_dotted = (strcmp(value,"true") == 0);
654 }
656 if ( (value = repr->attribute("visible")) ) {
657 visible = (strcmp(value,"true") == 0);
658 }
660 if ( (value = repr->attribute("enabled")) ) {
661 g_assert(snapper != NULL);
662 snapper->setEnabled(strcmp(value,"true") == 0);
663 }
665 for (GSList *l = canvasitems; l != NULL; l = l->next) {
666 sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
667 }
669 return;
670 }
672 /**
673 * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
674 */
675 void
676 CanvasXYGrid::onReprAttrChanged(Inkscape::XML::Node */*repr*/, gchar const */*key*/, gchar const */*oldval*/, gchar const */*newval*/, bool /*is_interactive*/)
677 {
678 readRepr();
680 if ( ! (_wr.isUpdating()) )
681 updateWidgets();
682 }
687 Gtk::Widget &
688 CanvasXYGrid::getWidget()
689 {
690 return vbox;
691 }
694 /**
695 * Update dialog widgets from object's values.
696 */
697 void
698 CanvasXYGrid::updateWidgets()
699 {
700 if (_wr.isUpdating()) return;
702 _wr.setUpdating (true);
704 _rcb_visible.setActive(visible);
705 if (snapper != NULL) {
706 _rcb_enabled.setActive(snapper->getEnabled());
707 }
709 _rumg.setUnit (gridunit);
711 gdouble val;
712 val = origin[NR::X];
713 val = sp_pixels_get_units (val, *(gridunit));
714 _rsu_ox.setValue (val);
715 val = origin[NR::Y];
716 val = sp_pixels_get_units (val, *(gridunit));
717 _rsu_oy.setValue (val);
718 val = spacing[NR::X];
719 double gridx = sp_pixels_get_units (val, *(gridunit));
720 _rsu_sx.setValue (gridx);
721 val = spacing[NR::Y];
722 double gridy = sp_pixels_get_units (val, *(gridunit));
723 _rsu_sy.setValue (gridy);
725 _rcp_gcol.setRgba32 (color);
726 _rcp_gmcol.setRgba32 (empcolor);
727 _rsi.setValue (empspacing);
729 _rcb_dotted.setActive(render_dotted);
731 _wr.setUpdating (false);
733 return;
734 }
738 void
739 CanvasXYGrid::Update (NR::Matrix const &affine, unsigned int /*flags*/)
740 {
741 ow = origin * affine;
742 sw = spacing * affine;
743 sw -= NR::Point(affine[4], affine[5]);
745 for(int dim = 0; dim < 2; dim++) {
746 gint scaling_factor = empspacing;
748 if (scaling_factor <= 1)
749 scaling_factor = 5;
751 scaled[dim] = FALSE;
752 sw[dim] = fabs (sw[dim]);
753 while (sw[dim] < 8.0) {
754 scaled[dim] = TRUE;
755 sw[dim] *= scaling_factor;
756 /* First pass, go up to the major line spacing, then
757 keep increasing by two. */
758 scaling_factor = 2;
759 }
760 }
761 }
764 static void
765 grid_hline (SPCanvasBuf *buf, gint y, gint xs, gint xe, guint32 rgba)
766 {
767 if ((y >= buf->rect.y0) && (y < buf->rect.y1)) {
768 guint r, g, b, a;
769 gint x0, x1, x;
770 guchar *p;
771 r = NR_RGBA32_R (rgba);
772 g = NR_RGBA32_G (rgba);
773 b = NR_RGBA32_B (rgba);
774 a = NR_RGBA32_A (rgba);
775 x0 = MAX (buf->rect.x0, xs);
776 x1 = MIN (buf->rect.x1, xe + 1);
777 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x0 - buf->rect.x0) * 3;
778 for (x = x0; x < x1; x++) {
779 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
780 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
781 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
782 p += 3;
783 }
784 }
785 }
787 static void
788 grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
789 {
790 if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
791 guint r, g, b, a;
792 gint y0, y1, y;
793 guchar *p;
794 r = NR_RGBA32_R(rgba);
795 g = NR_RGBA32_G (rgba);
796 b = NR_RGBA32_B (rgba);
797 a = NR_RGBA32_A (rgba);
798 y0 = MAX (buf->rect.y0, ys);
799 y1 = MIN (buf->rect.y1, ye + 1);
800 p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
801 for (y = y0; y < y1; y++) {
802 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
803 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
804 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
805 p += buf->buf_rowstride;
806 }
807 }
808 }
810 static void
811 grid_dot (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
812 {
813 if ( (y >= buf->rect.y0) && (y < buf->rect.y1)
814 && (x >= buf->rect.x0) && (x < buf->rect.x1) ) {
815 guint r, g, b, a;
816 guchar *p;
817 r = NR_RGBA32_R (rgba);
818 g = NR_RGBA32_G (rgba);
819 b = NR_RGBA32_B (rgba);
820 a = NR_RGBA32_A (rgba);
821 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
822 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
823 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
824 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
825 }
826 }
828 void
829 CanvasXYGrid::Render (SPCanvasBuf *buf)
830 {
831 gdouble const sxg = floor ((buf->rect.x0 - ow[NR::X]) / sw[NR::X]) * sw[NR::X] + ow[NR::X];
832 gint const xlinestart = (gint) Inkscape::round((sxg - ow[NR::X]) / sw[NR::X]);
833 gdouble const syg = floor ((buf->rect.y0 - ow[NR::Y]) / sw[NR::Y]) * sw[NR::Y] + ow[NR::Y];
834 gint const ylinestart = (gint) Inkscape::round((syg - ow[NR::Y]) / sw[NR::Y]);
836 if (!render_dotted) {
837 gint ylinenum;
838 gdouble y;
839 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
840 gint const y0 = (gint) Inkscape::round(y);
842 if (!scaled[NR::Y] && (ylinenum % empspacing) == 0) {
843 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, empcolor);
844 } else {
845 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, color);
846 }
847 }
849 gint xlinenum;
850 gdouble x;
851 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
852 gint const ix = (gint) Inkscape::round(x);
853 if (!scaled[NR::X] && (xlinenum % empspacing) == 0) {
854 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, empcolor);
855 } else {
856 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, color);
857 }
858 }
859 } else {
860 gint ylinenum;
861 gdouble y;
862 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
863 gint const iy = (gint) Inkscape::round(y);
865 gint xlinenum;
866 gdouble x;
867 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
868 gint const ix = (gint) Inkscape::round(x);
869 if ( (!scaled[NR::X] && (xlinenum % empspacing) == 0)
870 || (!scaled[NR::Y] && (ylinenum % empspacing) == 0) )
871 {
872 grid_dot (buf, ix, iy, empcolor | (guint32)0x000000FF); // put alpha to max value
873 } else {
874 grid_dot (buf, ix, iy, color | (guint32)0x000000FF); // put alpha to max value
875 }
876 }
878 }
879 }
880 }
882 CanvasXYGridSnapper::CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
883 {
884 this->grid = grid;
885 }
887 LineSnapper::LineList
888 CanvasXYGridSnapper::_getSnapLines(NR::Point const &p) const
889 {
890 LineList s;
892 if ( grid == NULL ) {
893 return s;
894 }
896 for (unsigned int i = 0; i < 2; ++i) {
898 /* This is to make sure we snap to only visible grid lines */
899 double scaled_spacing = grid->sw[i]; // this is spacing of visible lines if screen pixels
901 // convert screen pixels to px
902 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
903 if (SP_ACTIVE_DESKTOP) {
904 scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
905 }
907 NR::Coord rounded;
908 NR::Point point_on_line;
910 rounded = Inkscape::Util::round_to_upper_multiple_plus(p[i], scaled_spacing, grid->origin[i]);
911 point_on_line = i ? NR::Point(0, rounded) : NR::Point(rounded, 0);
912 s.push_back(std::make_pair(component_vectors[i], point_on_line));
914 rounded = Inkscape::Util::round_to_lower_multiple_plus(p[i], scaled_spacing, grid->origin[i]);
915 point_on_line = i ? NR::Point(0, rounded) : NR::Point(rounded, 0);
916 s.push_back(std::make_pair(component_vectors[i], point_on_line));
917 }
919 return s;
920 }
922 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
923 {
924 SnappedLine dummy = SnappedLine(snapped_point, snapped_distance, getSnapperTolerance(), getSnapperAlwaysSnap(), normal_to_line, point_on_line);
925 sc.grid_lines.push_back(dummy);
926 }
928 /**
929 * \return true if this Snapper will snap at least one kind of point.
930 */
931 bool CanvasXYGridSnapper::ThisSnapperMightSnap() const
932 {
933 return _named_view == NULL ? false : (_snap_enabled && _snap_from != 0);
934 }
940 }; /* namespace Inkscape */
943 /*
944 Local Variables:
945 mode:c++
946 c-file-style:"stroustrup"
947 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
948 indent-tabs-mode:nil
949 fill-column:99
950 End:
951 */
952 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :