836ad33976cb284358636fa65a722587a175ac3f
1 /*
2 * This is where the implementation of the DBus based document API lives.
3 * All the methods in here (except in the helper section) are
4 * designed to be called remotly via DBus. application-interface.cpp
5 * has the methods used to connect to the bus and get a document instance.
6 *
7 * Documentation for these methods is in document-interface.xml
8 * which is the "gold standard" as to how the interface should work.
9 *
10 * Authors:
11 * Soren Berg <Glimmer07@gmail.com>
12 *
13 * Copyright (C) 2009 Soren Berg
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 #include "document-interface.h"
19 #include <string.h>
21 #include "verbs.h"
22 #include "helper/action.h" //sp_action_perform
24 #include "inkscape.h" //inkscape_find_desktop_by_dkey, activate desktops
26 #include "desktop-handles.h" //sp_desktop_document()
27 #include "xml/repr.h" //sp_repr_document_new
29 #include "sp-object.h"
31 #include "document.h" // getReprDoc()
33 #include "desktop-style.h" //sp_desktop_get_style
35 #include "selection.h" //selection struct
36 #include "selection-chemistry.h"// lots of selection functions
38 #include "sp-ellipse.h"
40 #include "layer-fns.h" //LPOS_BELOW
42 #include "style.h" //style_write
44 #include "file.h" //IO
46 #include "extension/system.h" //IO
48 #include "extension/output.h" //IO
50 #include "print.h" //IO
52 #include "live_effects/parameter/text.h" //text
53 #include "display/canvas-text.h" //text
55 //#include "2geom/svg-path-parser.h" //get_node_coordinates
57 /****************************************************************************
58 HELPER / SHORTCUT FUNCTIONS
59 ****************************************************************************/
61 /*
62 * This function or the one below it translates the user input for an object
63 * into Inkscapes internal representation. It is called by almost every
64 * method so it should be as fast as possible.
65 *
66 * (eg turns "rect2234" to an SPObject or Inkscape::XML::Node)
67 *
68 * If the internal representation changes (No more 'id' attributes) this is the
69 * place to adjust things.
70 */
71 Inkscape::XML::Node *
72 get_repr_by_name (SPDesktop *desk, gchar *name, GError **error)
73 {
74 /* ALTERNATIVE (is this faster if only repr is needed?)
75 Inkscape::XML::Node *node = sp_repr_lookup_name((doc->root)->repr, name);
76 */
77 Inkscape::XML::Node * node = sp_desktop_document(desk)->getObjectById(name)->repr;
78 if (!node)
79 {
80 g_set_error(error, INKSCAPE_ERROR, INKSCAPE_ERROR_OBJECT, "Object '%s' not found in document.", name);
81 return NULL;
82 }
83 return node;
84 }
86 /*
87 * See comment for get_repr_by_name, above.
88 */
89 SPObject *
90 get_object_by_name (SPDesktop *desk, gchar *name, GError **error)
91 {
92 SPObject * obj = sp_desktop_document(desk)->getObjectById(name);
93 if (!obj)
94 {
95 g_set_error(error, INKSCAPE_ERROR, INKSCAPE_ERROR_OBJECT, "Object '%s' not found in document.", name);
96 return NULL;
97 }
98 return obj;
99 }
101 /*
102 * Tests for NULL strings and throws an appropriate error.
103 * Every method that takes a string parameter (other than the
104 * name of an object, that's tested seperatly) should call this.
105 */
106 gboolean
107 dbus_check_string (gchar *string, GError ** error, const gchar * errorstr)
108 {
109 if (string == NULL)
110 {
111 g_set_error(error, INKSCAPE_ERROR, INKSCAPE_ERROR_OTHER, "%s", errorstr);
112 return FALSE;
113 }
114 return TRUE;
115 }
117 /*
118 * This is used to return object values to the user
119 */
120 const gchar *
121 get_name_from_object (SPObject * obj)
122 {
123 return obj->repr->attribute("id");
124 }
126 /*
127 * Some verbs (cut, paste) only work on the active layer.
128 * This makes sure that the document that is about to recive a command is active.
129 */
130 void
131 desktop_ensure_active (SPDesktop* desk) {
132 if (desk != SP_ACTIVE_DESKTOP)
133 inkscape_activate_desktop (desk);
134 return;
135 }
137 gdouble
138 selection_get_center_x (Inkscape::Selection *sel){
139 NRRect *box = g_new(NRRect, 1);;
140 box = sel->boundsInDocument(box);
141 return box->x0 + ((box->x1 - box->x0)/2);
142 }
144 gdouble
145 selection_get_center_y (Inkscape::Selection *sel){
146 NRRect *box = g_new(NRRect, 1);;
147 box = sel->boundsInDocument(box);
148 return box->y0 + ((box->y1 - box->y0)/2);
149 }
151 /*
152 * This function is used along with selection_restore to
153 * take advantage of functionality provided by a selection
154 * for a single object.
155 *
156 * It saves the current selection and sets the selection to
157 * the object specified. Any selection verb can be used on the
158 * object and then selection_restore is called, restoring the
159 * original selection.
160 *
161 * This should be mostly transparent to the user who need never
162 * know we never bothered to implement it seperatly. Although
163 * they might see the selection box flicker if used in a loop.
164 */
165 const GSList *
166 selection_swap(SPDesktop *desk, gchar *name, GError **error)
167 {
168 Inkscape::Selection *sel = sp_desktop_selection(desk);
169 const GSList *oldsel = g_slist_copy((GSList *)sel->list());
171 sel->set(get_object_by_name(desk, name, error));
172 return oldsel;
173 }
175 /*
176 * See selection_swap, above
177 */
178 void
179 selection_restore(SPDesktop *desk, const GSList * oldsel)
180 {
181 Inkscape::Selection *sel = sp_desktop_selection(desk);
182 sel->setList(oldsel);
183 }
185 /*
186 * Shortcut for creating a Node.
187 */
188 Inkscape::XML::Node *
189 dbus_create_node (SPDesktop *desk, const gchar *type)
190 {
191 SPDocument * doc = sp_desktop_document (desk);
192 Inkscape::XML::Document *xml_doc = doc->getReprDoc();
194 return xml_doc->createElement(type);
195 }
197 /*
198 * Called by the shape creation functions. Gets the default style for the doc
199 * or sets it arbitrarily if none.
200 *
201 * There is probably a better way to do this (use the shape tools default styles)
202 * but I'm not sure how.
203 */
204 gchar *
205 finish_create_shape (DocumentInterface *object, GError **error, Inkscape::XML::Node *newNode, gchar *desc)
206 {
208 SPCSSAttr *style = sp_desktop_get_style(object->desk, TRUE);
210 if (style) {
211 newNode->setAttribute("style", sp_repr_css_write_string(style), TRUE);
212 }
213 else {
214 newNode->setAttribute("style", "fill:#0000ff;fill-opacity:1;stroke:#c900b9;stroke-width:0;stroke-miterlimit:0;stroke-opacity:1;stroke-dasharray:none", TRUE);
215 }
217 object->desk->currentLayer()->appendChildRepr(newNode);
218 object->desk->currentLayer()->updateRepr();
220 if (object->updates)
221 sp_document_done(sp_desktop_document(object->desk), 0, (gchar *)desc);
222 //else
223 //document_interface_pause_updates(object, error);
225 return strdup(newNode->attribute("id"));
226 }
228 /*
229 * This is the code used internally to call all the verbs.
230 *
231 * It handles error reporting and update pausing (which needs some work.)
232 * This is a good place to improve efficiency as it is called a lot.
233 *
234 * document_interface_call_verb is similar but is called by the user.
235 */
236 gboolean
237 dbus_call_verb (DocumentInterface *object, int verbid, GError **error)
238 {
239 SPDesktop *desk2 = object->desk;
240 desktop_ensure_active (desk2);
242 if ( desk2 ) {
243 Inkscape::Verb *verb = Inkscape::Verb::get( verbid );
244 if ( verb ) {
245 SPAction *action = verb->get_action(desk2);
246 if ( action ) {
247 //if (!object->updates)
248 //document_interface_pause_updates (object, error);
249 sp_action_perform( action, NULL );
250 if (object->updates)
251 sp_document_done(sp_desktop_document(desk2), verb->get_code(), g_strdup(verb->get_tip()));
252 //if (!object->updates)
253 //document_interface_pause_updates (object, error);
254 return TRUE;
255 }
256 }
257 }
258 g_set_error(error, INKSCAPE_ERROR, INKSCAPE_ERROR_VERB, "Verb failed to execute");
259 return FALSE;
260 }
262 /****************************************************************************
263 DOCUMENT INTERFACE CLASS STUFF
264 ****************************************************************************/
266 G_DEFINE_TYPE(DocumentInterface, document_interface, G_TYPE_OBJECT)
268 static void
269 document_interface_finalize (GObject *object)
270 {
271 G_OBJECT_CLASS (document_interface_parent_class)->finalize (object);
272 }
275 static void
276 document_interface_class_init (DocumentInterfaceClass *klass)
277 {
278 GObjectClass *object_class;
279 object_class = G_OBJECT_CLASS (klass);
280 object_class->finalize = document_interface_finalize;
281 }
283 static void
284 document_interface_init (DocumentInterface *object)
285 {
286 object->desk = NULL;
287 }
290 DocumentInterface *
291 document_interface_new (void)
292 {
293 return (DocumentInterface*)g_object_new (TYPE_DOCUMENT_INTERFACE, NULL);
294 }
296 /*
297 * Error stuff...
298 *
299 * To add a new error type, edit here and in the .h InkscapeError enum.
300 */
301 GQuark
302 inkscape_error_quark (void)
303 {
304 static GQuark quark = 0;
305 if (!quark)
306 quark = g_quark_from_static_string ("inkscape_error");
308 return quark;
309 }
311 #define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
313 GType
314 inkscape_error_get_type (void)
315 {
316 static GType etype = 0;
318 if (etype == 0)
319 {
320 static const GEnumValue values[] =
321 {
323 ENUM_ENTRY (INKSCAPE_ERROR_SELECTION, "Incompatible_Selection"),
324 ENUM_ENTRY (INKSCAPE_ERROR_OBJECT, "Incompatible_Object"),
325 ENUM_ENTRY (INKSCAPE_ERROR_VERB, "Failed_Verb"),
326 ENUM_ENTRY (INKSCAPE_ERROR_OTHER, "Generic_Error"),
327 { 0, 0, 0 }
328 };
330 etype = g_enum_register_static ("InkscapeError", values);
331 }
333 return etype;
334 }
336 /****************************************************************************
337 MISC FUNCTIONS
338 ****************************************************************************/
340 gboolean
341 document_interface_delete_all (DocumentInterface *object, GError **error)
342 {
343 sp_edit_clear_all (object->desk);
344 return TRUE;
345 }
347 gboolean
348 document_interface_call_verb (DocumentInterface *object, gchar *verbid, GError **error)
349 {
350 SPDesktop *desk2 = object->desk;
351 desktop_ensure_active (object->desk);
352 if ( desk2 ) {
353 Inkscape::Verb *verb = Inkscape::Verb::getbyid( verbid );
354 if ( verb ) {
355 SPAction *action = verb->get_action(desk2);
356 if ( action ) {
357 sp_action_perform( action, NULL );
358 if (object->updates) {
359 sp_document_done(sp_desktop_document(desk2), verb->get_code(), g_strdup(verb->get_tip()));
360 }
361 }
362 }
363 }
364 g_set_error(error, INKSCAPE_ERROR, INKSCAPE_ERROR_VERB, "Verb '%s' failed to execute or was not found.", verbid);
365 return FALSE;
366 }
369 /****************************************************************************
370 CREATION FUNCTIONS
371 ****************************************************************************/
373 gchar*
374 document_interface_rectangle (DocumentInterface *object, int x, int y,
375 int width, int height, GError **error)
376 {
379 Inkscape::XML::Node *newNode = dbus_create_node(object->desk, "svg:rect");
380 sp_repr_set_int(newNode, "x", x); //could also use newNode->setAttribute()
381 sp_repr_set_int(newNode, "y", y);
382 sp_repr_set_int(newNode, "width", width);
383 sp_repr_set_int(newNode, "height", height);
384 return finish_create_shape (object, error, newNode, (gchar *)"create rectangle");
385 }
387 gchar*
388 document_interface_ellipse_center (DocumentInterface *object, int cx, int cy,
389 int rx, int ry, GError **error)
390 {
391 Inkscape::XML::Node *newNode = dbus_create_node(object->desk, "svg:path");
392 newNode->setAttribute("sodipodi:type", "arc");
393 sp_repr_set_int(newNode, "sodipodi:cx", cx);
394 sp_repr_set_int(newNode, "sodipodi:cy", cy);
395 sp_repr_set_int(newNode, "sodipodi:rx", rx);
396 sp_repr_set_int(newNode, "sodipodi:ry", ry);
397 return finish_create_shape (object, error, newNode, (gchar *)"create circle");
398 }
400 gchar*
401 document_interface_polygon (DocumentInterface *object, int cx, int cy,
402 int radius, int rotation, int sides,
403 GError **error)
404 {
405 gdouble rot = ((rotation / 180.0) * 3.14159265) - ( 3.14159265 / 2.0);
406 Inkscape::XML::Node *newNode = dbus_create_node(object->desk, "svg:path");
407 newNode->setAttribute("inkscape:flatsided", "true");
408 newNode->setAttribute("sodipodi:type", "star");
409 sp_repr_set_int(newNode, "sodipodi:cx", cx);
410 sp_repr_set_int(newNode, "sodipodi:cy", cy);
411 sp_repr_set_int(newNode, "sodipodi:r1", radius);
412 sp_repr_set_int(newNode, "sodipodi:r2", radius);
413 sp_repr_set_int(newNode, "sodipodi:sides", sides);
414 sp_repr_set_int(newNode, "inkscape:randomized", 0);
415 sp_repr_set_svg_double(newNode, "sodipodi:arg1", rot);
416 sp_repr_set_svg_double(newNode, "sodipodi:arg2", rot);
417 sp_repr_set_svg_double(newNode, "inkscape:rounded", 0);
419 return finish_create_shape (object, error, newNode, (gchar *)"create polygon");
420 }
422 gchar*
423 document_interface_star (DocumentInterface *object, int cx, int cy,
424 int r1, int r2, int sides, gdouble rounded,
425 gdouble arg1, gdouble arg2, GError **error)
426 {
427 Inkscape::XML::Node *newNode = dbus_create_node(object->desk, "svg:path");
428 newNode->setAttribute("inkscape:flatsided", "false");
429 newNode->setAttribute("sodipodi:type", "star");
430 sp_repr_set_int(newNode, "sodipodi:cx", cx);
431 sp_repr_set_int(newNode, "sodipodi:cy", cy);
432 sp_repr_set_int(newNode, "sodipodi:r1", r1);
433 sp_repr_set_int(newNode, "sodipodi:r2", r2);
434 sp_repr_set_int(newNode, "sodipodi:sides", sides);
435 sp_repr_set_int(newNode, "inkscape:randomized", 0);
436 sp_repr_set_svg_double(newNode, "sodipodi:arg1", arg1);
437 sp_repr_set_svg_double(newNode, "sodipodi:arg2", arg2);
438 sp_repr_set_svg_double(newNode, "inkscape:rounded", rounded);
440 return finish_create_shape (object, error, newNode, (gchar *)"create star");
441 }
443 gchar*
444 document_interface_ellipse (DocumentInterface *object, int x, int y,
445 int width, int height, GError **error)
446 {
447 int rx = width/2;
448 int ry = height/2;
449 return document_interface_ellipse_center (object, x+rx, y+ry, rx, ry, error);
450 }
452 gchar*
453 document_interface_line (DocumentInterface *object, int x, int y,
454 int x2, int y2, GError **error)
455 {
456 Inkscape::XML::Node *newNode = dbus_create_node(object->desk, "svg:path");
457 std::stringstream out;
458 // Not sure why this works.
459 out << "m " << x << "," << y << " " << x2 - x << "," << y2 - y;
460 newNode->setAttribute("d", out.str().c_str());
461 return finish_create_shape (object, error, newNode, (gchar *)"create line");
462 }
464 gchar*
465 document_interface_spiral (DocumentInterface *object, int cx, int cy,
466 int r, int revolutions, GError **error)
467 {
468 Inkscape::XML::Node *newNode = dbus_create_node(object->desk, "svg:path");
469 newNode->setAttribute("sodipodi:type", "spiral");
470 sp_repr_set_int(newNode, "sodipodi:cx", cx);
471 sp_repr_set_int(newNode, "sodipodi:cy", cy);
472 sp_repr_set_int(newNode, "sodipodi:radius", r);
473 sp_repr_set_int(newNode, "sodipodi:revolution", revolutions);
474 sp_repr_set_int(newNode, "sodipodi:t0", 0);
475 sp_repr_set_int(newNode, "sodipodi:argument", 0);
476 sp_repr_set_int(newNode, "sodipodi:expansion", 1);
477 gchar * retval = finish_create_shape (object, error, newNode, (gchar *)"create spiral");
478 //Makes sure there is no fill for spirals by default.
479 gchar* newString = g_strconcat(newNode->attribute("style"), ";fill:none", NULL);
480 newNode->setAttribute("style", newString);
481 g_free(newString);
482 return retval;
483 }
485 gboolean
486 document_interface_text (DocumentInterface *object, int x, int y, gchar *text, GError **error)
487 {
488 //FIXME: Not selectable (aka broken). Needs to be rewritten completely.
490 SPDesktop *desktop = object->desk;
491 SPCanvasText * canvas_text = (SPCanvasText *) sp_canvastext_new(sp_desktop_tempgroup(desktop), desktop, Geom::Point(0,0), "");
492 sp_canvastext_set_text (canvas_text, text);
493 sp_canvastext_set_coords (canvas_text, x, y);
495 return TRUE;
496 }
498 gchar *
499 document_interface_image (DocumentInterface *object, int x, int y, gchar *filename, GError **error)
500 {
501 gchar * uri = g_filename_to_uri (filename, FALSE, error);
502 if (!uri)
503 return FALSE;
505 Inkscape::XML::Node *newNode = dbus_create_node(object->desk, "svg:image");
506 sp_repr_set_int(newNode, "x", x);
507 sp_repr_set_int(newNode, "y", y);
508 newNode->setAttribute("xlink:href", uri);
510 object->desk->currentLayer()->appendChildRepr(newNode);
511 object->desk->currentLayer()->updateRepr();
513 if (object->updates)
514 sp_document_done(sp_desktop_document(object->desk), 0, "Imported bitmap.");
516 //g_free(uri);
517 return strdup(newNode->attribute("id"));
518 }
520 gchar *document_interface_node (DocumentInterface *object, gchar *type, GError **error)
521 {
522 SPDocument * doc = sp_desktop_document (object->desk);
523 Inkscape::XML::Document *xml_doc = doc->getReprDoc();
525 Inkscape::XML::Node *newNode = xml_doc->createElement(type);
527 object->desk->currentLayer()->appendChildRepr(newNode);
528 object->desk->currentLayer()->updateRepr();
530 if (object->updates)
531 sp_document_done(sp_desktop_document(object->desk), 0, (gchar *)"created empty node");
532 //else
533 //document_interface_pause_updates(object, error);
535 return strdup(newNode->attribute("id"));
536 }
538 /****************************************************************************
539 ENVIORNMENT FUNCTIONS
540 ****************************************************************************/
541 gdouble
542 document_interface_document_get_width (DocumentInterface *object)
543 {
544 return sp_document_width(sp_desktop_document(object->desk));
545 }
547 gdouble
548 document_interface_document_get_height (DocumentInterface *object)
549 {
550 return sp_document_height(sp_desktop_document(object->desk));
551 }
553 gchar *
554 document_interface_document_get_css (DocumentInterface *object, GError **error)
555 {
556 SPCSSAttr *current = (object->desk)->current;
557 return sp_repr_css_write_string(current);
558 }
560 gboolean
561 document_interface_document_merge_css (DocumentInterface *object,
562 gchar *stylestring, GError **error)
563 {
564 SPCSSAttr * style = sp_repr_css_attr_new();
565 sp_repr_css_attr_add_from_string (style, stylestring);
566 sp_desktop_set_style (object->desk, style);
567 return TRUE;
568 }
570 gboolean
571 document_interface_document_set_css (DocumentInterface *object,
572 gchar *stylestring, GError **error)
573 {
574 SPCSSAttr * style = sp_repr_css_attr_new();
575 sp_repr_css_attr_add_from_string (style, stylestring);
576 //Memory leak?
577 object->desk->current = style;
578 return TRUE;
579 }
581 gboolean
582 document_interface_document_resize_to_fit_selection (DocumentInterface *object,
583 GError **error)
584 {
585 return dbus_call_verb (object, SP_VERB_FIT_CANVAS_TO_SELECTION, error);
586 return TRUE;
587 }
589 /****************************************************************************
590 OBJECT FUNCTIONS
591 ****************************************************************************/
593 gboolean
594 document_interface_set_attribute (DocumentInterface *object, char *shape,
595 char *attribute, char *newval, GError **error)
596 {
597 Inkscape::XML::Node *newNode = get_repr_by_name(object->desk, shape, error);
599 /* ALTERNATIVE (is this faster?)
600 Inkscape::XML::Node *newnode = sp_repr_lookup_name((doc->root)->repr, name);
601 */
602 if (!dbus_check_string(newval, error, "New value string was empty."))
603 return FALSE;
605 if (!newNode)
606 return FALSE;
608 newNode->setAttribute(attribute, newval, TRUE);
609 return TRUE;
610 }
612 gboolean
613 document_interface_set_int_attribute (DocumentInterface *object,
614 char *shape, char *attribute,
615 int newval, GError **error)
616 {
617 Inkscape::XML::Node *newNode = get_repr_by_name (object->desk, shape, error);
618 if (!newNode)
619 return FALSE;
621 sp_repr_set_int (newNode, attribute, newval);
622 return TRUE;
623 }
626 gboolean
627 document_interface_set_double_attribute (DocumentInterface *object,
628 char *shape, char *attribute,
629 double newval, GError **error)
630 {
631 Inkscape::XML::Node *newNode = get_repr_by_name (object->desk, shape, error);
633 if (!dbus_check_string (attribute, error, "New value string was empty."))
634 return FALSE;
635 if (!newNode)
636 return FALSE;
638 sp_repr_set_svg_double (newNode, attribute, newval);
639 return TRUE;
640 }
642 gchar *
643 document_interface_get_attribute (DocumentInterface *object, char *shape,
644 char *attribute, GError **error)
645 {
646 Inkscape::XML::Node *newNode = get_repr_by_name(object->desk, shape, error);
648 if (!dbus_check_string (attribute, error, "Attribute name empty."))
649 return NULL;
650 if (!newNode)
651 return NULL;
653 return g_strdup(newNode->attribute(attribute));
654 }
656 gboolean
657 document_interface_move (DocumentInterface *object, gchar *name, gdouble x,
658 gdouble y, GError **error)
659 {
660 const GSList *oldsel = selection_swap(object->desk, name, error);
661 if (!oldsel)
662 return FALSE;
663 sp_selection_move (object->desk, x, 0 - y);
664 selection_restore(object->desk, oldsel);
665 return TRUE;
666 }
668 gboolean
669 document_interface_move_to (DocumentInterface *object, gchar *name, gdouble x,
670 gdouble y, GError **error)
671 {
672 const GSList *oldsel = selection_swap(object->desk, name, error);
673 if (!oldsel)
674 return FALSE;
675 Inkscape::Selection * sel = sp_desktop_selection(object->desk);
676 sp_selection_move (object->desk, x - selection_get_center_x(sel),
677 0 - (y - selection_get_center_y(sel)));
678 selection_restore(object->desk, oldsel);
679 return TRUE;
680 }
682 gboolean
683 document_interface_object_to_path (DocumentInterface *object,
684 char *shape, GError **error)
685 {
686 const GSList *oldsel = selection_swap(object->desk, shape, error);
687 if (!oldsel)
688 return FALSE;
689 dbus_call_verb (object, SP_VERB_OBJECT_TO_CURVE, error);
690 selection_restore(object->desk, oldsel);
691 return TRUE;
692 }
694 gchar *
695 document_interface_get_path (DocumentInterface *object, char *pathname, GError **error)
696 {
697 Inkscape::XML::Node *node = get_repr_by_name(object->desk, pathname, error);
699 if (!node)
700 return NULL;
702 if (node->attribute("d") == NULL)
703 {
704 g_set_error(error, INKSCAPE_ERROR, INKSCAPE_ERROR_OBJECT, "Object is not a path.");
705 return NULL;
706 }
707 return strdup(node->attribute("d"));
708 }
710 gboolean
711 document_interface_transform (DocumentInterface *object, gchar *shape,
712 gchar *transformstr, GError **error)
713 {
714 //FIXME: This should merge transformations.
715 gchar trans[] = "transform";
716 document_interface_set_attribute (object, shape, trans, transformstr, error);
717 return TRUE;
718 }
720 gchar *
721 document_interface_get_css (DocumentInterface *object, gchar *shape,
722 GError **error)
723 {
724 gchar style[] = "style";
725 return document_interface_get_attribute (object, shape, style, error);
726 }
728 gboolean
729 document_interface_modify_css (DocumentInterface *object, gchar *shape,
730 gchar *cssattrb, gchar *newval, GError **error)
731 {
732 // Doesn't like non-variable strings for some reason.
733 gchar style[] = "style";
734 Inkscape::XML::Node *node = get_repr_by_name(object->desk, shape, error);
736 if (!dbus_check_string (cssattrb, error, "Attribute string empty."))
737 return FALSE;
738 if (!node)
739 return FALSE;
741 SPCSSAttr * oldstyle = sp_repr_css_attr (node, style);
742 sp_repr_css_set_property(oldstyle, cssattrb, newval);
743 node->setAttribute (style, sp_repr_css_write_string (oldstyle), TRUE);
744 return TRUE;
745 }
747 gboolean
748 document_interface_merge_css (DocumentInterface *object, gchar *shape,
749 gchar *stylestring, GError **error)
750 {
751 gchar style[] = "style";
753 Inkscape::XML::Node *node = get_repr_by_name(object->desk, shape, error);
755 if (!dbus_check_string (stylestring, error, "Style string empty."))
756 return FALSE;
757 if (!node)
758 return FALSE;
760 SPCSSAttr * newstyle = sp_repr_css_attr_new();
761 sp_repr_css_attr_add_from_string (newstyle, stylestring);
763 SPCSSAttr * oldstyle = sp_repr_css_attr (node, style);
765 sp_repr_css_merge(oldstyle, newstyle);
766 node->setAttribute (style, sp_repr_css_write_string (oldstyle), TRUE);
767 return TRUE;
768 }
770 gboolean
771 document_interface_set_color (DocumentInterface *object, gchar *shape,
772 int r, int g, int b, gboolean fill, GError **error)
773 {
774 gchar style[15];
775 if (r<0 || r>255 || g<0 || g>255 || b<0 || b>255)
776 {
777 g_set_error(error, INKSCAPE_ERROR, INKSCAPE_ERROR_OTHER, "Given (%d,%d,%d). All values must be between 0-255 inclusive.", r, g, b);
778 return FALSE;
779 }
781 if (fill)
782 snprintf(style, 15, "fill:#%.2x%.2x%.2x", r, g, b);
783 else
784 snprintf(style, 15, "stroke:#%.2x%.2x%.2x", r, g, b);
786 if (strcmp(shape, "document") == 0)
787 return document_interface_document_merge_css (object, style, error);
789 return document_interface_merge_css (object, shape, style, error);
790 }
792 gboolean
793 document_interface_move_to_layer (DocumentInterface *object, gchar *shape,
794 gchar *layerstr, GError **error)
795 {
796 const GSList *oldsel = selection_swap(object->desk, shape, error);
797 if (!oldsel)
798 return FALSE;
800 document_interface_selection_move_to_layer(object, layerstr, error);
801 selection_restore(object->desk, oldsel);
802 return TRUE;
803 }
805 GArray *
806 document_interface_get_node_coordinates (DocumentInterface *object, gchar *shape)
807 {
808 //FIXME: Needs lot's of work.
809 /*
810 Inkscape::XML::Node *shapenode = get_repr_by_name (object->desk, shape, error);
811 if (shapenode == NULL || shapenode->attribute("d") == NULL) {
812 return FALSE;
813 }
814 char * path = strdup(shapenode->attribute("d"));
815 printf("PATH: %s\n", path);
817 Geom::parse_svg_path (path);
818 return NULL;
819 */
820 return NULL;
821 }
824 /****************************************************************************
825 FILE I/O FUNCTIONS
826 ****************************************************************************/
828 gboolean
829 document_interface_save (DocumentInterface *object, GError **error)
830 {
831 SPDocument * doc = sp_desktop_document(object->desk);
832 printf("1: %s\n2: %s\n3: %s\n", doc->uri, doc->base, doc->name);
833 if (doc->uri)
834 return document_interface_save_as (object, doc->uri, error);
835 return FALSE;
836 }
838 gboolean
839 document_interface_load (DocumentInterface *object,
840 gchar *filename, GError **error)
841 {
842 desktop_ensure_active (object->desk);
843 const Glib::ustring file(filename);
844 sp_file_open(file, NULL, TRUE, TRUE);
845 if (object->updates)
846 sp_document_done(sp_desktop_document(object->desk), SP_VERB_FILE_OPEN, "Opened File");
847 return TRUE;
848 }
850 gboolean
851 document_interface_save_as (DocumentInterface *object,
852 gchar *filename, GError **error)
853 {
854 SPDocument * doc = sp_desktop_document(object->desk);
855 #ifdef WITH_GNOME_VFS
856 const Glib::ustring file(filename);
857 return file_save_remote(doc, file, NULL, TRUE, TRUE);
858 #endif
859 if (!doc || strlen(filename)<1) //Safety check
860 return false;
862 try {
863 Inkscape::Extension::save(NULL, doc, filename,
864 false, false, true, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
865 } catch (...) {
866 //SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
867 return false;
868 }
870 //SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
871 //SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::NORMAL_MESSAGE, "Document saved.");
872 return true;
873 }
875 gboolean
876 document_interface_mark_as_unmodified (DocumentInterface *object, GError **error)
877 {
878 SPDocument * doc = sp_desktop_document(object->desk);
879 if (doc)
880 doc->modified_since_save = FALSE;
881 return TRUE;
882 }
884 /*
885 gboolean
886 document_interface_print_to_file (DocumentInterface *object, GError **error)
887 {
888 SPDocument * doc = sp_desktop_document(object->desk);
889 sp_print_document_to_file (doc, g_strdup("/home/soren/test.pdf"));
891 return TRUE;
892 }
893 */
894 /****************************************************************************
895 PROGRAM CONTROL FUNCTIONS
896 ****************************************************************************/
898 gboolean
899 document_interface_close (DocumentInterface *object, GError **error)
900 {
901 return dbus_call_verb (object, SP_VERB_FILE_CLOSE_VIEW, error);
902 }
904 gboolean
905 document_interface_exit (DocumentInterface *object, GError **error)
906 {
907 return dbus_call_verb (object, SP_VERB_FILE_QUIT, error);
908 }
910 gboolean
911 document_interface_undo (DocumentInterface *object, GError **error)
912 {
913 return dbus_call_verb (object, SP_VERB_EDIT_UNDO, error);
914 }
916 gboolean
917 document_interface_redo (DocumentInterface *object, GError **error)
918 {
919 return dbus_call_verb (object, SP_VERB_EDIT_REDO, error);
920 }
924 /****************************************************************************
925 UPDATE FUNCTIONS
926 FIXME: This would work better by adding a flag to SPDesktop to prevent
927 updating but that would be very intrusive so for now there is a workaround.
928 Need to make sure it plays well with verbs because they are used so much.
929 ****************************************************************************/
931 void
932 document_interface_pause_updates (DocumentInterface *object, GError **error)
933 {
934 object->updates = FALSE;
935 object->desk->canvas->drawing_disabled = 1;
936 //object->desk->canvas->need_redraw = 0;
937 //object->desk->canvas->need_repick = 0;
938 //sp_desktop_document(object->desk)->root->uflags = FALSE;
939 //sp_desktop_document(object->desk)->root->mflags = FALSE;
940 }
942 void
943 document_interface_resume_updates (DocumentInterface *object, GError **error)
944 {
945 object->updates = TRUE;
946 object->desk->canvas->drawing_disabled = 0;
947 //object->desk->canvas->need_redraw = 1;
948 //object->desk->canvas->need_repick = 1;
949 //sp_desktop_document(object->desk)->root->uflags = TRUE;
950 //sp_desktop_document(object->desk)->root->mflags = TRUE;
951 //sp_desktop_document(object->desk)->_updateDocument();
952 //FIXME: use better verb than rect.
953 sp_document_done(sp_desktop_document(object->desk), SP_VERB_CONTEXT_RECT, "Multiple actions");
954 }
956 void
957 document_interface_update (DocumentInterface *object, GError **error)
958 {
959 sp_desktop_document(object->desk)->root->uflags = TRUE;
960 sp_desktop_document(object->desk)->root->mflags = TRUE;
961 object->desk->enableInteraction();
962 sp_desktop_document(object->desk)->_updateDocument();
963 object->desk->disableInteraction();
964 sp_desktop_document(object->desk)->root->uflags = FALSE;
965 sp_desktop_document(object->desk)->root->mflags = FALSE;
966 //sp_document_done(sp_desktop_document(object->desk), SP_VERB_CONTEXT_RECT, "Multiple actions");
967 }
969 /****************************************************************************
970 SELECTION FUNCTIONS FIXME: use call_verb where appropriate (once update system is tested.)
971 ****************************************************************************/
973 gboolean
974 document_interface_selection_get (DocumentInterface *object, char ***out, GError **error)
975 {
976 Inkscape::Selection * sel = sp_desktop_selection(object->desk);
977 GSList const *oldsel = sel->list();
979 int size = g_slist_length((GSList *) oldsel);
981 *out = g_new0 (char *, size + 1);
983 int i = 0;
984 for (GSList const *iter = oldsel; iter != NULL; iter = iter->next) {
985 (*out)[i] = g_strdup(SP_OBJECT(iter->data)->repr->attribute("id"));
986 i++;
987 }
988 (*out)[i] = NULL;
990 return TRUE;
991 }
993 gboolean
994 document_interface_selection_add (DocumentInterface *object, char *name, GError **error)
995 {
996 SPObject * obj = get_object_by_name(object->desk, name, error);
997 if (!obj)
998 return FALSE;
1000 Inkscape::Selection *selection = sp_desktop_selection(object->desk);
1002 selection->add(obj);
1003 return TRUE;
1004 }
1006 gboolean
1007 document_interface_selection_add_list (DocumentInterface *object,
1008 char **names, GError **error)
1009 {
1010 int i;
1011 for (i=0;names[i] != NULL;i++) {
1012 document_interface_selection_add(object, names[i], error);
1013 }
1014 return TRUE;
1015 }
1017 gboolean
1018 document_interface_selection_set (DocumentInterface *object, char *name, GError **error)
1019 {
1020 SPDocument * doc = sp_desktop_document (object->desk);
1021 Inkscape::Selection *selection = sp_desktop_selection(object->desk);
1022 selection->set(doc->getObjectById(name));
1023 return TRUE;
1024 }
1026 gboolean
1027 document_interface_selection_set_list (DocumentInterface *object,
1028 gchar **names, GError **error)
1029 {
1030 sp_desktop_selection(object->desk)->clear();
1031 int i;
1032 for (i=0;names[i] != NULL;i++) {
1033 document_interface_selection_add(object, names[i], error);
1034 }
1035 return TRUE;
1036 }
1038 gboolean
1039 document_interface_selection_rotate (DocumentInterface *object, int angle, GError **error)
1040 {
1041 Inkscape::Selection *selection = sp_desktop_selection(object->desk);
1042 sp_selection_rotate(selection, angle);
1043 return TRUE;
1044 }
1046 gboolean
1047 document_interface_selection_delete (DocumentInterface *object, GError **error)
1048 {
1049 //sp_selection_delete (object->desk);
1050 return dbus_call_verb (object, SP_VERB_EDIT_DELETE, error);
1051 }
1053 gboolean
1054 document_interface_selection_clear (DocumentInterface *object, GError **error)
1055 {
1056 sp_desktop_selection(object->desk)->clear();
1057 return TRUE;
1058 }
1060 gboolean
1061 document_interface_select_all (DocumentInterface *object, GError **error)
1062 {
1063 //sp_edit_select_all (object->desk);
1064 return dbus_call_verb (object, SP_VERB_EDIT_SELECT_ALL, error);
1065 }
1067 gboolean
1068 document_interface_select_all_in_all_layers(DocumentInterface *object,
1069 GError **error)
1070 {
1071 //sp_edit_select_all_in_all_layers (object->desk);
1072 return dbus_call_verb (object, SP_VERB_EDIT_SELECT_ALL_IN_ALL_LAYERS, error);
1073 }
1075 gboolean
1076 document_interface_selection_box (DocumentInterface *object, int x, int y,
1077 int x2, int y2, gboolean replace,
1078 GError **error)
1079 {
1080 //FIXME: implement.
1081 return FALSE;
1082 }
1084 gboolean
1085 document_interface_selection_invert (DocumentInterface *object, GError **error)
1086 {
1087 //sp_edit_invert (object->desk);
1088 return dbus_call_verb (object, SP_VERB_EDIT_INVERT, error);
1089 }
1091 gboolean
1092 document_interface_selection_group (DocumentInterface *object, GError **error)
1093 {
1094 //sp_selection_group (object->desk);
1095 return dbus_call_verb (object, SP_VERB_SELECTION_GROUP, error);
1096 }
1097 gboolean
1098 document_interface_selection_ungroup (DocumentInterface *object, GError **error)
1099 {
1100 //sp_selection_ungroup (object->desk);
1101 return dbus_call_verb (object, SP_VERB_SELECTION_UNGROUP, error);
1102 }
1104 gboolean
1105 document_interface_selection_cut (DocumentInterface *object, GError **error)
1106 {
1107 //desktop_ensure_active (object->desk);
1108 //sp_selection_cut (object->desk);
1109 return dbus_call_verb (object, SP_VERB_EDIT_CUT, error);
1110 }
1112 gboolean
1113 document_interface_selection_copy (DocumentInterface *object, GError **error)
1114 {
1115 //desktop_ensure_active (object->desk);
1116 //sp_selection_copy ();
1117 return dbus_call_verb (object, SP_VERB_EDIT_COPY, error);
1118 }
1119 /*
1120 gboolean
1121 document_interface_selection_paste (DocumentInterface *object, GError **error)
1122 {
1123 desktop_ensure_active (object->desk);
1124 if (!object->updates)
1125 document_interface_pause_updates (object, error);
1126 sp_selection_paste (object->desk, TRUE);
1127 if (!object->updates)
1128 document_interface_pause_updates (object, error);
1129 return TRUE;
1130 //return dbus_call_verb (object, SP_VERB_EDIT_PASTE, error);
1131 }
1132 */
1133 gboolean
1134 document_interface_selection_paste (DocumentInterface *object, GError **error)
1135 {
1136 return dbus_call_verb (object, SP_VERB_EDIT_PASTE, error);
1137 }
1139 gboolean
1140 document_interface_selection_scale (DocumentInterface *object, gdouble grow, GError **error)
1141 {
1142 Inkscape::Selection *selection = sp_desktop_selection(object->desk);
1143 if (!selection)
1144 {
1145 return FALSE;
1146 }
1147 sp_selection_scale (selection, grow);
1148 return TRUE;
1149 }
1151 gboolean
1152 document_interface_selection_move (DocumentInterface *object, gdouble x, gdouble y, GError **error)
1153 {
1154 sp_selection_move (object->desk, x, 0 - y); //switching coordinate systems.
1155 return TRUE;
1156 }
1158 gboolean
1159 document_interface_selection_move_to (DocumentInterface *object, gdouble x, gdouble y, GError **error)
1160 {
1161 Inkscape::Selection * sel = sp_desktop_selection(object->desk);
1163 Geom::OptRect sel_bbox = sel->bounds();
1164 if (sel_bbox) {
1165 Geom::Point m( x - selection_get_center_x(sel) , 0 - (y - selection_get_center_y(sel)) );
1166 sp_selection_move_relative(sel, m, true);
1167 }
1168 return TRUE;
1169 }
1171 //FIXME: does not paste in new layer.
1172 // This needs to use lower level cut_impl and paste_impl (messy)
1173 // See the built-in sp_selection_to_next_layer and duplicate.
1174 gboolean
1175 document_interface_selection_move_to_layer (DocumentInterface *object,
1176 gchar *layerstr, GError **error)
1177 {
1178 SPDesktop * dt = object->desk;
1180 Inkscape::Selection *selection = sp_desktop_selection(dt);
1182 // check if something is selected
1183 if (selection->isEmpty())
1184 return FALSE;
1186 SPObject *next = get_object_by_name(object->desk, layerstr, error);
1188 if (!next)
1189 return FALSE;
1191 if (strcmp("layer", (next->repr)->attribute("inkscape:groupmode")) == 0) {
1193 sp_selection_cut(dt);
1195 dt->setCurrentLayer(next);
1197 sp_selection_paste(dt, TRUE);
1198 }
1199 return TRUE;
1200 }
1202 GArray *
1203 document_interface_selection_get_center (DocumentInterface *object)
1204 {
1205 Inkscape::Selection * sel = sp_desktop_selection(object->desk);
1207 if (sel)
1208 {
1209 gdouble x = selection_get_center_x(sel);
1210 gdouble y = selection_get_center_y(sel);
1211 GArray * intArr = g_array_new (TRUE, TRUE, sizeof(double));
1213 g_array_append_val (intArr, x);
1214 g_array_append_val (intArr, y);
1215 return intArr;
1216 }
1218 return NULL;
1219 }
1221 gboolean
1222 document_interface_selection_to_path (DocumentInterface *object, GError **error)
1223 {
1224 return dbus_call_verb (object, SP_VERB_OBJECT_TO_CURVE, error);
1225 }
1228 gchar *
1229 document_interface_selection_combine (DocumentInterface *object, gchar *cmd,
1230 GError **error)
1231 {
1232 if (strcmp(cmd, "union") == 0)
1233 dbus_call_verb (object, SP_VERB_SELECTION_UNION, error);
1234 else if (strcmp(cmd, "intersection") == 0)
1235 dbus_call_verb (object, SP_VERB_SELECTION_INTERSECT, error);
1236 else if (strcmp(cmd, "difference") == 0)
1237 dbus_call_verb (object, SP_VERB_SELECTION_DIFF, error);
1238 else if (strcmp(cmd, "exclusion") == 0)
1239 dbus_call_verb (object, SP_VERB_SELECTION_SYMDIFF, error);
1240 else
1241 return NULL;
1243 if (sp_desktop_selection(object->desk)->singleRepr() != NULL)
1244 return g_strdup((sp_desktop_selection(object->desk)->singleRepr())->attribute("id"));
1245 return NULL;
1246 }
1248 gboolean
1249 document_interface_selection_divide (DocumentInterface *object, char ***out, GError **error)
1250 {
1251 dbus_call_verb (object, SP_VERB_SELECTION_CUT, error);
1253 return document_interface_selection_get (object, out, error);
1254 }
1256 gboolean
1257 document_interface_selection_change_level (DocumentInterface *object, gchar *cmd,
1258 GError **error)
1259 {
1260 if (strcmp(cmd, "raise") == 0)
1261 return dbus_call_verb (object, SP_VERB_SELECTION_RAISE, error);
1262 if (strcmp(cmd, "lower") == 0)
1263 return dbus_call_verb (object, SP_VERB_SELECTION_LOWER, error);
1264 if ((strcmp(cmd, "to_top") == 0) || (strcmp(cmd, "to_front") == 0))
1265 return dbus_call_verb (object, SP_VERB_SELECTION_TO_FRONT, error);
1266 if ((strcmp(cmd, "to_bottom") == 0) || (strcmp(cmd, "to_back") == 0))
1267 return dbus_call_verb (object, SP_VERB_SELECTION_TO_BACK, error);
1268 return TRUE;
1269 }
1271 /****************************************************************************
1272 LAYER FUNCTIONS
1273 ****************************************************************************/
1275 gchar *
1276 document_interface_layer_new (DocumentInterface *object, GError **error)
1277 {
1278 SPDesktop * dt = object->desk;
1279 SPObject *new_layer = Inkscape::create_layer(dt->currentRoot(), dt->currentLayer(), Inkscape::LPOS_BELOW);
1280 dt->setCurrentLayer(new_layer);
1281 return g_strdup(get_name_from_object (new_layer));
1282 }
1284 gboolean
1285 document_interface_layer_set (DocumentInterface *object,
1286 gchar *layerstr, GError **error)
1287 {
1288 SPObject * obj = get_object_by_name (object->desk, layerstr, error);
1290 if (!obj)
1291 return FALSE;
1293 object->desk->setCurrentLayer (obj);
1294 return TRUE;
1295 }
1297 gchar **
1298 document_interface_layer_get_all (DocumentInterface *object)
1299 {
1300 //FIXME: implement.
1301 return NULL;
1302 }
1304 gboolean
1305 document_interface_layer_change_level (DocumentInterface *object,
1306 gchar *cmd, GError **error)
1307 {
1308 if (strcmp(cmd, "raise") == 0)
1309 return dbus_call_verb (object, SP_VERB_LAYER_RAISE, error);
1310 if (strcmp(cmd, "lower") == 0)
1311 return dbus_call_verb (object, SP_VERB_LAYER_LOWER, error);
1312 if ((strcmp(cmd, "to_top") == 0) || (strcmp(cmd, "to_front") == 0))
1313 return dbus_call_verb (object, SP_VERB_LAYER_TO_TOP, error);
1314 if ((strcmp(cmd, "to_bottom") == 0) || (strcmp(cmd, "to_back") == 0))
1315 return dbus_call_verb (object, SP_VERB_LAYER_TO_BOTTOM, error);
1316 return TRUE;
1317 }
1319 gboolean
1320 document_interface_layer_next (DocumentInterface *object, GError **error)
1321 {
1322 return dbus_call_verb (object, SP_VERB_LAYER_NEXT, error);
1323 }
1325 gboolean
1326 document_interface_layer_previous (DocumentInterface *object, GError **error)
1327 {
1328 return dbus_call_verb (object, SP_VERB_LAYER_PREV, error);
1329 }