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_visible.init ( _("_Visible"),
178 _("Determines whether the grid is displayed or not. Objects are still snapped to invisible grids."),
179 "visible", _wr, false, repr, doc);
180 vbox.pack_start(*dynamic_cast<Gtk::Widget*>(_rcb_visible._button), true, true);
182 _rcb_snap_enabled.init ( _("_Snapping enabled"),
183 _("Determines whether to snap to this grid or not. Can be 'on' for invisible grids."),
184 "snap_enabled", _wr, false, repr, doc);
185 vbox.pack_start(*dynamic_cast<Gtk::Widget*>(_rcb_snap_enabled._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(repr); FIX THIS. THIS SHOULD BE HERE!!!
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::isSnapEnabled()
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 origin[NR::X] = prefs_get_double_attribute ("options.grids.xy", "origin_x", 0.0);
395 origin[NR::Y] = prefs_get_double_attribute ("options.grids.xy", "origin_y", 0.0);
396 color = prefs_get_int_attribute("options.grids.xy", "color", 0x0000ff20);
397 empcolor = prefs_get_int_attribute("options.grids.xy", "empcolor", 0x0000ff40);
398 empspacing = prefs_get_int_attribute("options.grids.xy", "empspacing", 5);
399 spacing[NR::X] = prefs_get_double_attribute ("options.grids.xy", "spacing_x", 0.0);
400 spacing[NR::Y] = prefs_get_double_attribute ("options.grids.xy", "spacing_y", 0.0);
401 gridunit = sp_unit_get_by_abbreviation( prefs_get_string_attribute("options.grids.xy", "units") );
402 render_dotted = prefs_get_int_attribute ("options.grids.xy", "dotted", 0) == 1;
404 snapper = new CanvasXYGridSnapper(this, namedview, 0);
406 // initialize widgets:
407 vbox.set_border_width(2);
408 table.set_spacings(2);
409 vbox.pack_start(table, false, false, 0);
411 Inkscape::UI::Widget::ScalarUnit * sutemp;
412 _rumg.init (_("Grid _units:"), "units", _wr, repr, doc);
413 _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"),
414 "originx", _rumg, _wr, repr, doc);
415 sutemp = _rsu_ox.getSU();
416 sutemp->setDigits(4);
417 sutemp->setIncrements(0.1, 1.0);
418 _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"),
419 "originy", _rumg, _wr, repr, doc);
420 sutemp = _rsu_oy.getSU();
421 sutemp->setDigits(4);
422 sutemp->setIncrements(0.1, 1.0);
423 _rsu_sx.init (_("Spacing _X:"), _("Distance between vertical grid lines"),
424 "spacingx", _rumg, _wr, repr, doc);
425 sutemp = _rsu_sx.getSU();
426 sutemp->setDigits(4);
427 sutemp->setIncrements(0.1, 1.0);
428 _rsu_sy.init (_("Spacing _Y:"), _("Distance between horizontal grid lines"),
429 "spacingy", _rumg, _wr, repr, doc);
430 sutemp = _rsu_sy.getSU();
431 sutemp->setDigits(4);
432 sutemp->setIncrements(0.1, 1.0);
433 _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"),
434 _("Color of grid lines"), "color", "opacity", _wr, repr, doc);
435 _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"),
436 _("Color of the major (highlighted) grid lines"),
437 "empcolor", "empopacity", _wr, repr, doc);
438 _rsi.init (_("_Major grid line every:"), _("lines"), "empspacing", _wr, repr, doc);
439 _rcb_dotted.init ( _("_Show dots instead of lines"),
440 _("If set, displays dots at gridpoints instead of gridlines"),
441 "dotted", _wr, false, repr, doc);
443 Gtk::Widget const *const widget_array[] = {
444 _rumg._label, _rumg._sel,
445 0, _rsu_ox.getSU(),
446 0, _rsu_oy.getSU(),
447 0, _rsu_sx.getSU(),
448 0, _rsu_sy.getSU(),
449 _rcp_gcol._label, _rcp_gcol._cp,
450 0, 0,
451 _rcp_gmcol._label, _rcp_gmcol._cp,
452 _rsi._label, &_rsi._hbox,
453 0, _rcb_dotted._button,
454 };
456 attach_all (table, widget_array, sizeof(widget_array));
458 vbox.show();
460 if (repr) readRepr();
461 updateWidgets();
462 }
464 CanvasXYGrid::~CanvasXYGrid ()
465 {
466 if (snapper) delete snapper;
467 }
470 /* fixme: Collect all these length parsing methods and think common sane API */
472 static gboolean
473 sp_nv_read_length(gchar const *str, guint base, gdouble *val, SPUnit const **unit)
474 {
475 if (!str) {
476 return FALSE;
477 }
479 gchar *u;
480 gdouble v = g_ascii_strtod(str, &u);
481 if (!u) {
482 return FALSE;
483 }
484 while (isspace(*u)) {
485 u += 1;
486 }
488 if (!*u) {
489 /* No unit specified - keep default */
490 *val = v;
491 return TRUE;
492 }
494 if (base & SP_UNIT_DEVICE) {
495 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
496 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
497 *val = v;
498 return TRUE;
499 }
500 }
502 if (base & SP_UNIT_ABSOLUTE) {
503 if (!strncmp(u, "pt", 2)) {
504 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
505 } else if (!strncmp(u, "mm", 2)) {
506 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
507 } else if (!strncmp(u, "cm", 2)) {
508 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
509 } else if (!strncmp(u, "m", 1)) {
510 *unit = &sp_unit_get_by_id(SP_UNIT_M);
511 } else if (!strncmp(u, "in", 2)) {
512 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
513 } else {
514 return FALSE;
515 }
516 *val = v;
517 return TRUE;
518 }
520 return FALSE;
521 }
523 static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
524 {
525 if (!str) {
526 return FALSE;
527 }
529 gchar *u;
530 gdouble v = g_ascii_strtod(str, &u);
531 if (!u) {
532 return FALSE;
533 }
534 v = CLAMP(v, 0.0, 1.0);
536 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
538 return TRUE;
539 }
541 /** If the passed scalar is invalid (<=0), then set the widget and the scalar
542 to use the given old value.
544 @param oldVal Old value to use if the new one is invalid.
545 @param pTarget The scalar to validate.
546 @param widget Widget associated with the scalar.
547 */
548 static void validateScalar(double oldVal,
549 double* pTarget,
550 Inkscape::UI::Widget::RegisteredScalarUnit& widget)
551 {
552 // Avoid nullness.
553 if ( pTarget == NULL )
554 return;
556 // Invalid new value?
557 if ( *pTarget <= 0 ) {
558 // If the old value is somehow invalid as well, then default to 1.
559 if ( oldVal <= 0 )
560 oldVal = 1;
562 // Reset the scalar and associated widget to the old value.
563 *pTarget = oldVal;
564 widget.setValue( *pTarget);
565 } //if
567 } //validateScalar
570 /** If the passed int is invalid (<=0), then set the widget and the int
571 to use the given old value.
573 @param oldVal Old value to use if the new one is invalid.
574 @param pTarget The int to validate.
575 @param widget Widget associated with the int.
576 */
577 static void validateInt(gint oldVal,
578 gint* pTarget,
579 Inkscape::UI::Widget::RegisteredSuffixedInteger& widget)
580 {
581 // Avoid nullness.
582 if ( pTarget == NULL )
583 return;
585 // Invalid new value?
586 if ( *pTarget <= 0 ) {
587 // If the old value is somehow invalid as well, then default to 1.
588 if ( oldVal <= 0 )
589 oldVal = 1;
591 // Reset the int and associated widget to the old value.
592 *pTarget = oldVal;
593 widget.setValue( *pTarget);
594 } //if
596 } //validateInt
598 void
599 CanvasXYGrid::readRepr()
600 {
601 gchar const *value;
602 if ( (value = repr->attribute("originx")) ) {
603 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::X], &gridunit);
604 origin[NR::X] = sp_units_get_pixels(origin[NR::X], *(gridunit));
605 }
607 if ( (value = repr->attribute("originy")) ) {
608 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::Y], &gridunit);
609 origin[NR::Y] = sp_units_get_pixels(origin[NR::Y], *(gridunit));
610 }
612 if ( (value = repr->attribute("spacingx")) ) {
613 double oldVal = spacing[NR::X];
614 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::X], &gridunit);
615 validateScalar( oldVal, &spacing[NR::X], _rsu_sx );
616 spacing[NR::X] = sp_units_get_pixels(spacing[NR::X], *(gridunit));
618 }
619 if ( (value = repr->attribute("spacingy")) ) {
620 double oldVal = spacing[NR::Y];
621 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &spacing[NR::Y], &gridunit);
622 validateScalar( oldVal, &spacing[NR::Y], _rsu_sy );
623 spacing[NR::Y] = sp_units_get_pixels(spacing[NR::Y], *(gridunit));
625 }
627 if ( (value = repr->attribute("color")) ) {
628 color = (color & 0xff) | sp_svg_read_color(value, color);
629 }
631 if ( (value = repr->attribute("empcolor")) ) {
632 empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
633 }
635 if ( (value = repr->attribute("opacity")) ) {
636 sp_nv_read_opacity(value, &color);
637 }
638 if ( (value = repr->attribute("empopacity")) ) {
639 sp_nv_read_opacity(value, &empcolor);
640 }
642 if ( (value = repr->attribute("empspacing")) ) {
643 gint oldVal = empspacing;
644 empspacing = atoi(value);
645 validateInt( oldVal, &empspacing, _rsi );
646 }
648 if ( (value = repr->attribute("dotted")) ) {
649 render_dotted = (strcmp(value,"true") == 0);
650 }
652 if ( (value = repr->attribute("visible")) ) {
653 visible = (strcmp(value,"true") == 0);
654 }
656 if ( (value = repr->attribute("snap_enabled")) ) {
657 g_assert(snapper != NULL);
658 snapper->setEnabled(strcmp(value,"true") == 0);
659 }
661 for (GSList *l = canvasitems; l != NULL; l = l->next) {
662 sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
663 }
665 return;
666 }
668 /**
669 * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
670 */
671 void
672 CanvasXYGrid::onReprAttrChanged(Inkscape::XML::Node */*repr*/, gchar const */*key*/, gchar const */*oldval*/, gchar const */*newval*/, bool /*is_interactive*/)
673 {
674 readRepr();
676 if ( ! (_wr.isUpdating()) )
677 updateWidgets();
678 }
683 Gtk::Widget &
684 CanvasXYGrid::getWidget()
685 {
686 return vbox;
687 }
690 /**
691 * Update dialog widgets from object's values.
692 */
693 void
694 CanvasXYGrid::updateWidgets()
695 {
696 if (_wr.isUpdating()) return;
698 _wr.setUpdating (true);
700 _rcb_visible.setActive(visible);
701 if (snapper != NULL) {
702 _rcb_snap_enabled.setActive(snapper->getEnabled());
703 }
705 _rumg.setUnit (gridunit);
707 gdouble val;
708 val = origin[NR::X];
709 val = sp_pixels_get_units (val, *(gridunit));
710 _rsu_ox.setValue (val);
711 val = origin[NR::Y];
712 val = sp_pixels_get_units (val, *(gridunit));
713 _rsu_oy.setValue (val);
714 val = spacing[NR::X];
715 double gridx = sp_pixels_get_units (val, *(gridunit));
716 _rsu_sx.setValue (gridx);
717 val = spacing[NR::Y];
718 double gridy = sp_pixels_get_units (val, *(gridunit));
719 _rsu_sy.setValue (gridy);
721 _rcp_gcol.setRgba32 (color);
722 _rcp_gmcol.setRgba32 (empcolor);
723 _rsi.setValue (empspacing);
725 _rcb_dotted.setActive(render_dotted);
727 _wr.setUpdating (false);
729 return;
730 }
734 void
735 CanvasXYGrid::Update (NR::Matrix const &affine, unsigned int /*flags*/)
736 {
737 ow = origin * affine;
738 sw = spacing * affine;
739 sw -= NR::Point(affine[4], affine[5]);
741 for(int dim = 0; dim < 2; dim++) {
742 gint scaling_factor = empspacing;
744 if (scaling_factor <= 1)
745 scaling_factor = 5;
747 scaled[dim] = FALSE;
748 sw[dim] = fabs (sw[dim]);
749 while (sw[dim] < 8.0) {
750 scaled[dim] = TRUE;
751 sw[dim] *= scaling_factor;
752 /* First pass, go up to the major line spacing, then
753 keep increasing by two. */
754 scaling_factor = 2;
755 }
756 }
757 }
760 static void
761 grid_hline (SPCanvasBuf *buf, gint y, gint xs, gint xe, guint32 rgba)
762 {
763 if ((y >= buf->rect.y0) && (y < buf->rect.y1)) {
764 guint r, g, b, a;
765 gint x0, x1, x;
766 guchar *p;
767 r = NR_RGBA32_R (rgba);
768 g = NR_RGBA32_G (rgba);
769 b = NR_RGBA32_B (rgba);
770 a = NR_RGBA32_A (rgba);
771 x0 = MAX (buf->rect.x0, xs);
772 x1 = MIN (buf->rect.x1, xe + 1);
773 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x0 - buf->rect.x0) * 3;
774 for (x = x0; x < x1; x++) {
775 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
776 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
777 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
778 p += 3;
779 }
780 }
781 }
783 static void
784 grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
785 {
786 if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
787 guint r, g, b, a;
788 gint y0, y1, y;
789 guchar *p;
790 r = NR_RGBA32_R(rgba);
791 g = NR_RGBA32_G (rgba);
792 b = NR_RGBA32_B (rgba);
793 a = NR_RGBA32_A (rgba);
794 y0 = MAX (buf->rect.y0, ys);
795 y1 = MIN (buf->rect.y1, ye + 1);
796 p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
797 for (y = y0; y < y1; y++) {
798 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
799 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
800 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
801 p += buf->buf_rowstride;
802 }
803 }
804 }
806 static void
807 grid_dot (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
808 {
809 if ( (y >= buf->rect.y0) && (y < buf->rect.y1)
810 && (x >= buf->rect.x0) && (x < buf->rect.x1) ) {
811 guint r, g, b, a;
812 guchar *p;
813 r = NR_RGBA32_R (rgba);
814 g = NR_RGBA32_G (rgba);
815 b = NR_RGBA32_B (rgba);
816 a = NR_RGBA32_A (rgba);
817 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
818 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
819 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
820 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
821 }
822 }
824 void
825 CanvasXYGrid::Render (SPCanvasBuf *buf)
826 {
827 gdouble const sxg = floor ((buf->rect.x0 - ow[NR::X]) / sw[NR::X]) * sw[NR::X] + ow[NR::X];
828 gint const xlinestart = (gint) Inkscape::round((sxg - ow[NR::X]) / sw[NR::X]);
829 gdouble const syg = floor ((buf->rect.y0 - ow[NR::Y]) / sw[NR::Y]) * sw[NR::Y] + ow[NR::Y];
830 gint const ylinestart = (gint) Inkscape::round((syg - ow[NR::Y]) / sw[NR::Y]);
832 if (!render_dotted) {
833 gint ylinenum;
834 gdouble y;
835 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
836 gint const y0 = (gint) Inkscape::round(y);
838 if (!scaled[NR::Y] && (ylinenum % empspacing) == 0) {
839 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, empcolor);
840 } else {
841 grid_hline (buf, y0, buf->rect.x0, buf->rect.x1 - 1, color);
842 }
843 }
845 gint xlinenum;
846 gdouble x;
847 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
848 gint const ix = (gint) Inkscape::round(x);
849 if (!scaled[NR::X] && (xlinenum % empspacing) == 0) {
850 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, empcolor);
851 } else {
852 grid_vline (buf, ix, buf->rect.y0, buf->rect.y1, color);
853 }
854 }
855 } else {
856 gint ylinenum;
857 gdouble y;
858 for (y = syg, ylinenum = ylinestart; y < buf->rect.y1; y += sw[NR::Y], ylinenum++) {
859 gint const iy = (gint) Inkscape::round(y);
861 gint xlinenum;
862 gdouble x;
863 for (x = sxg, xlinenum = xlinestart; x < buf->rect.x1; x += sw[NR::X], xlinenum++) {
864 gint const ix = (gint) Inkscape::round(x);
865 if ( (!scaled[NR::X] && (xlinenum % empspacing) == 0)
866 || (!scaled[NR::Y] && (ylinenum % empspacing) == 0) )
867 {
868 grid_dot (buf, ix, iy, empcolor | (guint32)0x000000FF); // put alpha to max value
869 } else {
870 grid_dot (buf, ix, iy, color | (guint32)0x000000FF); // put alpha to max value
871 }
872 }
874 }
875 }
876 }
878 CanvasXYGridSnapper::CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
879 {
880 this->grid = grid;
881 }
883 LineSnapper::LineList
884 CanvasXYGridSnapper::_getSnapLines(NR::Point const &p) const
885 {
886 LineList s;
888 if ( grid == NULL ) {
889 return s;
890 }
892 for (unsigned int i = 0; i < 2; ++i) {
894 /* This is to make sure we snap to only visible grid lines */
895 double scaled_spacing = grid->sw[i]; // this is spacing of visible lines if screen pixels
897 // convert screen pixels to px
898 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
899 if (SP_ACTIVE_DESKTOP) {
900 scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
901 }
903 NR::Coord rounded;
904 NR::Point point_on_line;
906 rounded = Inkscape::Util::round_to_upper_multiple_plus(p[i], scaled_spacing, grid->origin[i]);
907 point_on_line = i ? NR::Point(0, rounded) : NR::Point(rounded, 0);
908 s.push_back(std::make_pair(component_vectors[i], point_on_line));
910 rounded = Inkscape::Util::round_to_lower_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));
913 }
915 return s;
916 }
918 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
919 {
920 SnappedLine dummy = SnappedLine(snapped_point, snapped_distance, normal_to_line, point_on_line);
921 sc.grid_lines.push_back(dummy);
922 }
924 /**
925 * \return true if this Snapper will snap at least one kind of point.
926 */
927 bool CanvasXYGridSnapper::ThisSnapperMightSnap() const
928 {
929 return _named_view == NULL ? false : (_snap_enabled && _snap_from != 0);
930 }
936 }; /* namespace Inkscape */
939 /*
940 Local Variables:
941 mode:c++
942 c-file-style:"stroustrup"
943 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
944 indent-tabs-mode:nil
945 fill-column:99
946 End:
947 */
948 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :