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), 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 }
339 bool CanvasGrid::isSnapEnabled()
340 {
341 if (snapper == NULL) {
342 return false;
343 }
345 return snapper->getEnabled();
346 }
348 // ##########################################################
349 // CanvasXYGrid
352 /**
353 * "attach_all" function
354 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP TO QUICKLY GET RESULTS
355 *
356 * Helper function that attachs widgets in a 3xn table. The widgets come in an
357 * array that has two entries per table row. The two entries code for four
358 * possible cases: (0,0) means insert space in first column; (0, non-0) means
359 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
360 * (non-0, non-0) means two widgets in columns 2 and 3.
361 **/
362 #define SPACE_SIZE_X 15
363 #define SPACE_SIZE_Y 10
364 static inline void
365 attach_all(Gtk::Table &table, Gtk::Widget const *const arr[], unsigned size, int start = 0)
366 {
367 for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2) {
368 if (arr[i] && arr[i+1]) {
369 table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
370 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
371 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
372 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
373 } else {
374 if (arr[i+1]) {
375 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
376 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
377 } else if (arr[i]) {
378 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
379 label.set_alignment (0.0);
380 table.attach (label, 0, 3, r, r+1,
381 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
382 } else {
383 Gtk::HBox *space = manage (new Gtk::HBox);
384 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
385 table.attach (*space, 0, 1, r, r+1,
386 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
387 }
388 }
389 ++r;
390 }
391 }
393 CanvasXYGrid::CanvasXYGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
394 : CanvasGrid(nv, in_repr, in_doc, GRID_RECTANGULAR), table(1, 1)
395 {
396 origin[NR::X] = origin[NR::Y] = 0.0;
397 color = DEFAULTGRIDCOLOR;
398 empcolor = DEFAULTGRIDEMPCOLOR;
399 empspacing = 5;
400 spacing[NR::X] = spacing[NR::Y] = 1.0;
401 gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
402 render_dotted = false;
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 :