1 #define __SP_DESKTOP_C__
3 /** \file
4 * Editable view implementation
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * MenTaLguY <mental@rydia.net>
9 * bulia byak <buliabyak@users.sf.net>
10 * Ralf Stephan <ralf@ark.in-berlin.de>
11 * John Bintz <jcoswell@coswellproductions.org>
12 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
13 *
14 * Copyright (C) 2007 Jon A. Cruz
15 * Copyright (C) 2006-2008 Johan Engelen
16 * Copyright (C) 2006 John Bintz
17 * Copyright (C) 2004 MenTaLguY
18 * Copyright (C) 1999-2002 Lauris Kaplinski
19 * Copyright (C) 2000-2001 Ximian, Inc.
20 *
21 * Released under GNU GPL, read the file 'COPYING' for more information
22 */
24 /** \class SPDesktop
25 * SPDesktop is a subclass of View, implementing an editable document
26 * canvas. It is extensively used by many UI controls that need certain
27 * visual representations of their own.
28 *
29 * SPDesktop provides a certain set of SPCanvasItems, serving as GUI
30 * layers of different control objects. The one containing the whole
31 * document is the drawing layer. In addition to it, there are grid,
32 * guide, sketch and control layers. The sketch layer is used for
33 * temporary drawing objects, before the real objects in document are
34 * created. The control layer contains editing knots, rubberband and
35 * similar non-document UI objects.
36 *
37 * Each SPDesktop is associated with a SPNamedView node of the document
38 * tree. Currently, all desktops are created from a single main named
39 * view, but in the future there may be support for different ones.
40 * SPNamedView serves as an in-document container for desktop-related
41 * data, like grid and guideline placement, snapping options and so on.
42 *
43 * Associated with each SPDesktop are the two most important editing
44 * related objects - SPSelection and SPEventContext.
45 *
46 * Sodipodi keeps track of the active desktop and invokes notification
47 * signals whenever it changes. UI elements can use these to update their
48 * display to the selection of the currently active editing window.
49 * (Lauris Kaplinski)
50 */
52 #ifdef HAVE_CONFIG_H
53 # include "config.h"
54 #endif
56 #include <glibmm/i18n.h>
57 #include <sigc++/functors/mem_fun.h>
58 #include <gtkmm.h>
60 #include "macros.h"
61 #include "inkscape-private.h"
62 #include "desktop.h"
63 #include "desktop-events.h"
64 #include "desktop-handles.h"
65 #include "document.h"
66 #include "message-stack.h"
67 #include "selection.h"
68 #include "select-context.h"
69 #include "sp-namedview.h"
70 #include "color.h"
71 #include "sp-item-group.h"
72 #include "prefs-utils.h"
73 #include "object-hierarchy.h"
74 #include "helper/units.h"
75 #include "display/canvas-arena.h"
76 #include "display/nr-arena.h"
77 #include "display/gnome-canvas-acetate.h"
78 #include "display/sodipodi-ctrlrect.h"
79 #include "display/sp-canvas-util.h"
80 #include "display/canvas-temporary-item-list.h"
81 #include "display/snap-indicator.h"
82 #include "libnr/nr-rect-ops.h"
83 #include "ui/dialog/dialog-manager.h"
84 #include "xml/repr.h"
85 #include "message-context.h"
86 #include "device-manager.h"
87 #include "layer-fns.h"
88 #include "layer-manager.h"
89 #include "event-log.h"
90 #include "display/canvas-grid.h"
91 #include "widgets/desktop-widget.h"
92 #include "box3d-context.h"
94 #include "display/sp-canvas.h"
96 namespace Inkscape { namespace XML { class Node; }}
98 // Callback declarations
99 static void _onSelectionChanged (Inkscape::Selection *selection, SPDesktop *desktop);
100 static gint _arena_handler (SPCanvasArena *arena, NRArenaItem *ai, GdkEvent *event, SPDesktop *desktop);
101 static void _layer_activated(SPObject *layer, SPDesktop *desktop);
102 static void _layer_deactivated(SPObject *layer, SPDesktop *desktop);
103 static void _layer_hierarchy_changed(SPObject *top, SPObject *bottom, SPDesktop *desktop);
104 static void _reconstruction_start(SPDesktop * desktop);
105 static void _reconstruction_finish(SPDesktop * desktop);
106 static void _namedview_modified (SPObject *obj, guint flags, SPDesktop *desktop);
107 static void _update_snap_distances (SPDesktop *desktop);
109 /**
110 * Return new desktop object.
111 * \pre namedview != NULL.
112 * \pre canvas != NULL.
113 */
114 SPDesktop::SPDesktop() :
115 _dlg_mgr( 0 ),
116 namedview( 0 ),
117 canvas( 0 ),
118 selection( 0 ),
119 event_context( 0 ),
120 layer_manager( 0 ),
121 event_log( 0 ),
122 temporary_item_list( 0 ),
123 snapindicator( 0 ),
124 acetate( 0 ),
125 main( 0 ),
126 gridgroup( 0 ),
127 guides( 0 ),
128 drawing( 0 ),
129 sketch( 0 ),
130 controls( 0 ),
131 tempgroup ( 0 ),
132 table( 0 ),
133 page( 0 ),
134 page_border( 0 ),
135 current( 0 ),
136 zooms_past( 0 ),
137 zooms_future( 0 ),
138 dkey( 0 ),
139 number( 0 ),
140 window_state(0),
141 interaction_disabled_counter( 0 ),
142 waiting_cursor( false ),
143 guides_active( false ),
144 gr_item( 0 ),
145 gr_point_type( 0 ),
146 gr_point_i( 0 ),
147 gr_fill_or_stroke( true ),
148 _layer_hierarchy( 0 ),
149 _reconstruction_old_layer_id( 0 ),
150 _display_mode(Inkscape::RENDERMODE_NORMAL),
151 _saved_display_mode(Inkscape::RENDERMODE_NORMAL),
152 _widget( 0 ),
153 _inkscape( 0 ),
154 _guides_message_context( 0 ),
155 _active( false ),
156 _w2d(),
157 _d2w(),
158 _doc2dt( NR::Matrix(NR::scale(1, -1)) ),
159 grids_visible( false )
160 {
161 _d2w.set_identity();
162 _w2d.set_identity();
164 selection = Inkscape::GC::release( new Inkscape::Selection(this) );
165 }
167 void
168 SPDesktop::init (SPNamedView *nv, SPCanvas *aCanvas)
169 {
170 // Temporary workaround for link order issues:
171 Inkscape::DeviceManager::getManager().getDevices();
173 _guides_message_context = new Inkscape::MessageContext(const_cast<Inkscape::MessageStack*>(messageStack()));
175 current = sp_repr_css_attr_inherited (inkscape_get_repr (INKSCAPE, "desktop"), "style");
177 namedview = nv;
178 canvas = aCanvas;
180 SPDocument *document = SP_OBJECT_DOCUMENT (namedview);
181 /* Kill flicker */
182 sp_document_ensure_up_to_date (document);
184 /* Setup Dialog Manager */
185 _dlg_mgr = &Inkscape::UI::Dialog::DialogManager::getInstance();
187 dkey = sp_item_display_key_new (1);
189 /* Connect document */
190 setDocument (document);
192 number = namedview->getViewCount();
195 /* Setup Canvas */
196 g_object_set_data (G_OBJECT (canvas), "SPDesktop", this);
198 SPCanvasGroup *root = sp_canvas_root (canvas);
200 /* Setup adminstrative layers */
201 acetate = sp_canvas_item_new (root, GNOME_TYPE_CANVAS_ACETATE, NULL);
202 g_signal_connect (G_OBJECT (acetate), "event", G_CALLBACK (sp_desktop_root_handler), this);
203 main = (SPCanvasGroup *) sp_canvas_item_new (root, SP_TYPE_CANVAS_GROUP, NULL);
204 g_signal_connect (G_OBJECT (main), "event", G_CALLBACK (sp_desktop_root_handler), this);
206 table = sp_canvas_item_new (main, SP_TYPE_CTRLRECT, NULL);
207 SP_CTRLRECT(table)->setRectangle(NR::Rect(NR::Point(-80000, -80000), NR::Point(80000, 80000)));
208 SP_CTRLRECT(table)->setColor(0x00000000, true, 0x00000000);
209 sp_canvas_item_move_to_z (table, 0);
211 page = sp_canvas_item_new (main, SP_TYPE_CTRLRECT, NULL);
212 ((CtrlRect *) page)->setColor(0x00000000, FALSE, 0x00000000);
213 page_border = sp_canvas_item_new (main, SP_TYPE_CTRLRECT, NULL);
215 drawing = sp_canvas_item_new (main, SP_TYPE_CANVAS_ARENA, NULL);
216 g_signal_connect (G_OBJECT (drawing), "arena_event", G_CALLBACK (_arena_handler), this);
218 SP_CANVAS_ARENA (drawing)->arena->delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0); // default is 1 px
220 if (prefs_get_int_attribute("options.startmode", "outline", 0)) {
221 // Start in outline mode
222 setDisplayModeOutline();
223 } else {
224 // Start in normal mode, default
225 setDisplayModeNormal();
226 }
228 gridgroup = (SPCanvasGroup *) sp_canvas_item_new (main, SP_TYPE_CANVAS_GROUP, NULL);
229 guides = (SPCanvasGroup *) sp_canvas_item_new (main, SP_TYPE_CANVAS_GROUP, NULL);
230 sketch = (SPCanvasGroup *) sp_canvas_item_new (main, SP_TYPE_CANVAS_GROUP, NULL);
231 controls = (SPCanvasGroup *) sp_canvas_item_new (main, SP_TYPE_CANVAS_GROUP, NULL);
232 tempgroup = (SPCanvasGroup *) sp_canvas_item_new (main, SP_TYPE_CANVAS_GROUP, NULL);
234 /* Push select tool to the bottom of stack */
235 /** \todo
236 * FIXME: this is the only call to this. Everything else seems to just
237 * call "set" instead of "push". Can we assume that there is only one
238 * context ever?
239 */
240 push_event_context (SP_TYPE_SELECT_CONTEXT, "tools.select", SP_EVENT_CONTEXT_STATIC);
242 // display rect and zoom are now handled in sp_desktop_widget_realize()
244 NR::Rect const d(NR::Point(0.0, 0.0),
245 NR::Point(sp_document_width(document), sp_document_height(document)));
247 SP_CTRLRECT(page)->setRectangle(d);
248 SP_CTRLRECT(page_border)->setRectangle(d);
250 /* the following sets the page shadow on the canvas
251 It was originally set to 5, which is really cheesy!
252 It now is an attribute in the document's namedview. If a value of
253 0 is used, then the constructor for a shadow is not initialized.
254 */
256 if ( namedview->pageshadow != 0 && namedview->showpageshadow ) {
257 SP_CTRLRECT(page_border)->setShadow(namedview->pageshadow, 0x3f3f3fff);
258 }
261 /* Connect event for page resize */
262 _doc2dt[5] = sp_document_height (document);
263 sp_canvas_item_affine_absolute (SP_CANVAS_ITEM (drawing), _doc2dt);
265 _modified_connection = namedview->connectModified(sigc::bind<2>(sigc::ptr_fun(&_namedview_modified), this));
267 NRArenaItem *ai = sp_item_invoke_show (SP_ITEM (sp_document_root (document)),
268 SP_CANVAS_ARENA (drawing)->arena,
269 dkey,
270 SP_ITEM_SHOW_DISPLAY);
271 if (ai) {
272 nr_arena_item_add_child (SP_CANVAS_ARENA (drawing)->root, ai, NULL);
273 }
275 namedview->show(this);
276 /* Ugly hack */
277 activate_guides (true);
278 /* Ugly hack */
279 _namedview_modified (namedview, SP_OBJECT_MODIFIED_FLAG, this);
281 /* Set up notification of rebuilding the document, this allows
282 for saving object related settings in the document. */
283 _reconstruction_start_connection =
284 document->connectReconstructionStart(sigc::bind(sigc::ptr_fun(_reconstruction_start), this));
285 _reconstruction_finish_connection =
286 document->connectReconstructionFinish(sigc::bind(sigc::ptr_fun(_reconstruction_finish), this));
287 _reconstruction_old_layer_id = NULL;
289 // ?
290 // sp_active_desktop_set (desktop);
291 _inkscape = INKSCAPE;
293 _activate_connection = _activate_signal.connect(
294 sigc::bind(
295 sigc::ptr_fun(_onActivate),
296 this
297 )
298 );
299 _deactivate_connection = _deactivate_signal.connect(
300 sigc::bind(
301 sigc::ptr_fun(_onDeactivate),
302 this
303 )
304 );
306 _sel_modified_connection = selection->connectModified(
307 sigc::bind(
308 sigc::ptr_fun(&_onSelectionModified),
309 this
310 )
311 );
312 _sel_changed_connection = selection->connectChanged(
313 sigc::bind(
314 sigc::ptr_fun(&_onSelectionChanged),
315 this
316 )
317 );
320 /* setup LayerManager */
321 // (Setting up after the connections are all in place, as it may use some of them)
322 layer_manager = new Inkscape::LayerManager( this );
324 showGrids(namedview->grids_visible, false);
326 temporary_item_list = new Inkscape::Display::TemporaryItemList( this );
327 snapindicator = new Inkscape::Display::SnapIndicator ( this );
328 }
331 void SPDesktop::destroy()
332 {
333 if (snapindicator) {
334 delete snapindicator;
335 snapindicator = NULL;
336 }
337 if (temporary_item_list) {
338 delete temporary_item_list;
339 temporary_item_list = NULL;
340 }
342 namedview->hide(this);
344 _activate_connection.disconnect();
345 _deactivate_connection.disconnect();
346 _sel_modified_connection.disconnect();
347 _sel_changed_connection.disconnect();
348 _modified_connection.disconnect();
349 _commit_connection.disconnect();
350 _reconstruction_start_connection.disconnect();
351 _reconstruction_finish_connection.disconnect();
353 g_signal_handlers_disconnect_by_func(G_OBJECT (acetate), (gpointer) G_CALLBACK(sp_desktop_root_handler), this);
354 g_signal_handlers_disconnect_by_func(G_OBJECT (main), (gpointer) G_CALLBACK(sp_desktop_root_handler), this);
355 g_signal_handlers_disconnect_by_func(G_OBJECT (drawing), (gpointer) G_CALLBACK(_arena_handler), this);
357 while (event_context) {
358 SPEventContext *ec = event_context;
359 event_context = ec->next;
360 sp_event_context_finish (ec);
361 g_object_unref (G_OBJECT (ec));
362 }
364 if (_layer_hierarchy) {
365 delete _layer_hierarchy;
366 // _layer_hierarchy = NULL; //this should be here, but commented to find other bug somewhere else.
367 }
369 if (layer_manager) {
370 delete layer_manager;
371 layer_manager = NULL;
372 }
374 if (_inkscape) {
375 _inkscape = NULL;
376 }
378 if (drawing) {
379 sp_item_invoke_hide (SP_ITEM (sp_document_root (doc())), dkey);
380 drawing = NULL;
381 }
383 delete _guides_message_context;
384 _guides_message_context = NULL;
386 g_list_free (zooms_past);
387 g_list_free (zooms_future);
388 }
390 SPDesktop::~SPDesktop() {}
392 //--------------------------------------------------------------------
393 /* Public methods */
396 /* These methods help for temporarily showing things on-canvas.
397 * The *only* valid use of the TemporaryItem* that you get from add_temporary_canvasitem
398 * is when you want to prematurely remove the item from the canvas, by calling
399 * desktop->remove_temporary_canvasitem(tempitem).
400 */
401 /** Note that lifetime is measured in milliseconds
402 * One should *not* keep a reference to the SPCanvasItem, the temporary item code will
403 * delete the object for you and the reference will become invalid without you knowing it.
404 * It is perfectly safe to ignore the returned pointer: the object is deleted by itself, so don't delete it elsewhere!
405 * The *only* valid use of the returned TemporaryItem* is as argument for SPDesktop::remove_temporary_canvasitem,
406 * because the object might be deleted already without you knowing it.
407 * move_to_bottom = true by default so the item does not interfere with handling of other items on the canvas like nodes.
408 */
409 Inkscape::Display::TemporaryItem *
410 SPDesktop::add_temporary_canvasitem (SPCanvasItem *item, guint lifetime, bool move_to_bottom)
411 {
412 if (move_to_bottom) {
413 sp_canvas_item_move_to_z(item, 0);
414 }
416 return temporary_item_list->add_item(item, lifetime);
417 }
419 /** It is perfectly safe to call this function while the object has already been deleted due to a timeout.
420 */
421 void
422 SPDesktop::remove_temporary_canvasitem (Inkscape::Display::TemporaryItem * tempitem)
423 {
424 // check for non-null temporary_item_list, because during destruction of desktop, some destructor might try to access this list!
425 if (tempitem && temporary_item_list) {
426 temporary_item_list->delete_item(tempitem);
427 }
428 }
430 void SPDesktop::_setDisplayMode(Inkscape::RenderMode mode) {
431 SP_CANVAS_ARENA (drawing)->arena->rendermode = mode;
432 canvas->rendermode = mode;
433 _display_mode = mode;
434 if (mode != Inkscape::RENDERMODE_OUTLINE) {
435 _saved_display_mode = _display_mode;
436 }
437 sp_canvas_item_affine_absolute (SP_CANVAS_ITEM (main), _d2w); // redraw
438 _widget->setTitle(SP_DOCUMENT_NAME(sp_desktop_document(this)));
439 }
441 void SPDesktop::displayModeToggle() {
442 if (_display_mode == Inkscape::RENDERMODE_OUTLINE) {
443 _setDisplayMode(_saved_display_mode);
444 } else {
445 _setDisplayMode(Inkscape::RENDERMODE_OUTLINE);
446 }
447 }
449 /**
450 * Returns current root (=bottom) layer.
451 */
452 SPObject *SPDesktop::currentRoot() const
453 {
454 return _layer_hierarchy ? _layer_hierarchy->top() : NULL;
455 }
457 /**
458 * Returns current top layer.
459 */
460 SPObject *SPDesktop::currentLayer() const
461 {
462 return _layer_hierarchy ? _layer_hierarchy->bottom() : NULL;
463 }
465 /**
466 * Sets the current layer of the desktop.
467 *
468 * Make \a object the top layer.
469 */
470 void SPDesktop::setCurrentLayer(SPObject *object) {
471 g_return_if_fail(SP_IS_GROUP(object));
472 g_return_if_fail( currentRoot() == object || (currentRoot() && currentRoot()->isAncestorOf(object)) );
473 // printf("Set Layer to ID: %s\n", SP_OBJECT_ID(object));
474 _layer_hierarchy->setBottom(object);
475 }
477 void SPDesktop::toggleLayerSolo(SPObject *object) {
478 g_return_if_fail(SP_IS_GROUP(object));
479 g_return_if_fail( currentRoot() == object || (currentRoot() && currentRoot()->isAncestorOf(object)) );
481 bool othersShowing = false;
482 std::vector<SPObject*> layers;
483 for ( SPObject* obj = Inkscape::next_layer(currentRoot(), object); obj; obj = Inkscape::next_layer(currentRoot(), obj) ) {
484 layers.push_back(obj);
485 othersShowing |= !SP_ITEM(obj)->isHidden();
486 }
487 for ( SPObject* obj = Inkscape::previous_layer(currentRoot(), object); obj; obj = Inkscape::previous_layer(currentRoot(), obj) ) {
488 layers.push_back(obj);
489 othersShowing |= !SP_ITEM(obj)->isHidden();
490 }
493 if ( SP_ITEM(object)->isHidden() ) {
494 SP_ITEM(object)->setHidden(false);
495 }
497 for ( std::vector<SPObject*>::iterator it = layers.begin(); it != layers.end(); ++it ) {
498 SP_ITEM(*it)->setHidden(othersShowing);
499 }
500 }
502 /**
503 * Return layer that contains \a object.
504 */
505 SPObject *SPDesktop::layerForObject(SPObject *object) {
506 g_return_val_if_fail(object != NULL, NULL);
508 SPObject *root=currentRoot();
509 object = SP_OBJECT_PARENT(object);
510 while ( object && object != root && !isLayer(object) ) {
511 object = SP_OBJECT_PARENT(object);
512 }
513 return object;
514 }
516 /**
517 * True if object is a layer.
518 */
519 bool SPDesktop::isLayer(SPObject *object) const {
520 return ( SP_IS_GROUP(object)
521 && ( SP_GROUP(object)->effectiveLayerMode(this->dkey)
522 == SPGroup::LAYER ) );
523 }
525 /**
526 * True if desktop viewport fully contains \a item's bbox.
527 */
528 bool SPDesktop::isWithinViewport (SPItem *item) const
529 {
530 NR::Rect const viewport = get_display_area();
531 boost::optional<NR::Rect> const bbox = sp_item_bbox_desktop(item);
532 if (bbox) {
533 return viewport.contains(*bbox);
534 } else {
535 return true;
536 }
537 }
539 ///
540 bool SPDesktop::itemIsHidden(SPItem const *item) const {
541 return item->isHidden(this->dkey);
542 }
544 /**
545 * Set activate property of desktop; emit signal if changed.
546 */
547 void
548 SPDesktop::set_active (bool new_active)
549 {
550 if (new_active != _active) {
551 _active = new_active;
552 if (new_active) {
553 _activate_signal.emit();
554 } else {
555 _deactivate_signal.emit();
556 }
557 }
558 }
560 /**
561 * Set activate status of current desktop's named view.
562 */
563 void
564 SPDesktop::activate_guides(bool activate)
565 {
566 guides_active = activate;
567 namedview->activateGuides(this, activate);
568 }
570 /**
571 * Make desktop switch documents.
572 */
573 void
574 SPDesktop::change_document (SPDocument *theDocument)
575 {
576 g_return_if_fail (theDocument != NULL);
578 /* unselect everything before switching documents */
579 selection->clear();
581 setDocument (theDocument);
583 /* update the rulers, connect the desktop widget's signal to the new namedview etc.
584 (this can probably be done in a better way) */
585 Gtk::Window *parent = this->getToplevel();
586 g_assert(parent != NULL);
587 SPDesktopWidget *dtw = (SPDesktopWidget *) parent->get_data("desktopwidget");
588 if (dtw) dtw->desktop = this;
589 sp_desktop_widget_update_namedview(dtw);
591 _namedview_modified (namedview, SP_OBJECT_MODIFIED_FLAG, this);
592 _document_replaced_signal.emit (this, theDocument);
593 }
595 /**
596 * Make desktop switch event contexts.
597 */
598 void
599 SPDesktop::set_event_context (GtkType type, const gchar *config)
600 {
601 SPEventContext *ec;
602 while (event_context) {
603 ec = event_context;
604 sp_event_context_deactivate (ec);
605 event_context = ec->next;
606 sp_event_context_finish (ec);
607 g_object_unref (G_OBJECT (ec));
608 }
610 Inkscape::XML::Node *repr = (config) ? inkscape_get_repr (_inkscape, config) : NULL;
611 ec = sp_event_context_new (type, this, repr, SP_EVENT_CONTEXT_STATIC);
612 ec->next = event_context;
613 event_context = ec;
614 sp_event_context_activate (ec);
615 _event_context_changed_signal.emit (this, ec);
616 }
618 /**
619 * Push event context onto desktop's context stack.
620 */
621 void
622 SPDesktop::push_event_context (GtkType type, const gchar *config, unsigned int key)
623 {
624 SPEventContext *ref, *ec;
625 Inkscape::XML::Node *repr;
627 if (event_context && event_context->key == key) return;
628 ref = event_context;
629 while (ref && ref->next && ref->next->key != key) ref = ref->next;
630 if (ref && ref->next) {
631 ec = ref->next;
632 ref->next = ec->next;
633 sp_event_context_finish (ec);
634 g_object_unref (G_OBJECT (ec));
635 }
637 if (event_context) sp_event_context_deactivate (event_context);
638 repr = (config) ? inkscape_get_repr (INKSCAPE, config) : NULL;
639 ec = sp_event_context_new (type, this, repr, key);
640 ec->next = event_context;
641 event_context = ec;
642 sp_event_context_activate (ec);
643 _event_context_changed_signal.emit (this, ec);
644 }
646 /**
647 * Sets the coordinate status to a given point
648 */
649 void
650 SPDesktop::set_coordinate_status (NR::Point p) {
651 _widget->setCoordinateStatus(p);
652 }
654 /**
655 * \see sp_document_item_from_list_at_point_bottom()
656 */
657 SPItem *
658 SPDesktop::item_from_list_at_point_bottom (const GSList *list, NR::Point const p) const
659 {
660 g_return_val_if_fail (doc() != NULL, NULL);
661 return sp_document_item_from_list_at_point_bottom (dkey, SP_GROUP (doc()->root), list, p);
662 }
664 /**
665 * \see sp_document_item_at_point()
666 */
667 SPItem *
668 SPDesktop::item_at_point (NR::Point const p, bool into_groups, SPItem *upto) const
669 {
670 g_return_val_if_fail (doc() != NULL, NULL);
671 return sp_document_item_at_point (doc(), dkey, p, into_groups, upto);
672 }
674 /**
675 * \see sp_document_group_at_point()
676 */
677 SPItem *
678 SPDesktop::group_at_point (NR::Point const p) const
679 {
680 g_return_val_if_fail (doc() != NULL, NULL);
681 return sp_document_group_at_point (doc(), dkey, p);
682 }
684 /**
685 * \brief Returns the mouse point in document coordinates; if mouse is
686 * outside the canvas, returns the center of canvas viewpoint
687 */
688 NR::Point
689 SPDesktop::point() const
690 {
691 NR::Point p = _widget->getPointer();
692 NR::Point pw = sp_canvas_window_to_world (canvas, p);
693 p = w2d(pw);
695 NR::Rect const r = canvas->getViewbox();
697 NR::Point r0 = w2d(r.min());
698 NR::Point r1 = w2d(r.max());
700 if (p[NR::X] >= r0[NR::X] &&
701 p[NR::X] <= r1[NR::X] &&
702 p[NR::Y] >= r1[NR::Y] &&
703 p[NR::Y] <= r0[NR::Y])
704 {
705 return p;
706 } else {
707 return (r0 + r1) / 2;
708 }
709 }
711 /**
712 * Put current zoom data in history list.
713 */
714 void
715 SPDesktop::push_current_zoom (GList **history)
716 {
717 NR::Rect const area = get_display_area();
719 NRRect *old_zoom = g_new(NRRect, 1);
720 old_zoom->x0 = area.min()[NR::X];
721 old_zoom->x1 = area.max()[NR::X];
722 old_zoom->y0 = area.min()[NR::Y];
723 old_zoom->y1 = area.max()[NR::Y];
724 if ( *history == NULL
725 || !( ( ((NRRect *) ((*history)->data))->x0 == old_zoom->x0 ) &&
726 ( ((NRRect *) ((*history)->data))->x1 == old_zoom->x1 ) &&
727 ( ((NRRect *) ((*history)->data))->y0 == old_zoom->y0 ) &&
728 ( ((NRRect *) ((*history)->data))->y1 == old_zoom->y1 ) ) )
729 {
730 *history = g_list_prepend (*history, old_zoom);
731 }
732 }
734 /**
735 * Set viewbox (x0, x1, y0 and y1 are in document pixels. Border is in screen pixels).
736 */
737 void
738 SPDesktop::set_display_area (double x0, double y0, double x1, double y1, double border, bool log)
739 {
740 g_assert(_widget);
742 // save the zoom
743 if (log) {
744 push_current_zoom(&zooms_past);
745 // if we do a logged zoom, our zoom-forward list is invalidated, so delete it
746 g_list_free (zooms_future);
747 zooms_future = NULL;
748 }
750 double const cx = 0.5 * (x0 + x1);
751 double const cy = 0.5 * (y0 + y1);
753 NR::Rect const viewbox = NR::expand(canvas->getViewbox(), border);
755 double scale = expansion(_d2w);
756 double newscale;
757 if (((x1 - x0) * viewbox.dimensions()[NR::Y]) > ((y1 - y0) * viewbox.dimensions()[NR::X])) {
758 newscale = viewbox.dimensions()[NR::X] / (x1 - x0);
759 } else {
760 newscale = viewbox.dimensions()[NR::Y] / (y1 - y0);
761 }
763 newscale = CLAMP(newscale, SP_DESKTOP_ZOOM_MIN, SP_DESKTOP_ZOOM_MAX); // unit: 'screen pixels' per 'document pixels'
765 int clear = FALSE;
766 if (!NR_DF_TEST_CLOSE (newscale, scale, 1e-4 * scale)) {
767 /* Set zoom factors */
768 _d2w = NR::Matrix(NR::scale(newscale, -newscale));
769 _w2d = NR::Matrix(NR::scale(1/newscale, 1/-newscale));
770 sp_canvas_item_affine_absolute(SP_CANVAS_ITEM(main), _d2w);
771 clear = TRUE;
772 }
774 /* Calculate top left corner (in document pixels) */
775 x0 = cx - 0.5 * viewbox.dimensions()[NR::X] / newscale;
776 y1 = cy + 0.5 * viewbox.dimensions()[NR::Y] / newscale;
778 /* Scroll */
779 sp_canvas_scroll_to (canvas, x0 * newscale - border, y1 * -newscale - border, clear);
781 /* update perspective lines if we are in the 3D box tool (so that infinite ones are shown correctly) */
782 sp_box3d_context_update_lines(event_context);
784 _widget->updateRulers();
785 _widget->updateScrollbars(expansion(_d2w));
786 _widget->updateZoom();
787 }
789 void SPDesktop::set_display_area(NR::Rect const &a, NR::Coord b, bool log)
790 {
791 set_display_area(a.min()[NR::X], a.min()[NR::Y], a.max()[NR::X], a.max()[NR::Y], b, log);
792 }
794 /**
795 * Return viewbox dimensions.
796 */
797 NR::Rect SPDesktop::get_display_area() const
798 {
799 NR::Rect const viewbox = canvas->getViewbox();
801 double const scale = _d2w[0];
803 return NR::Rect(NR::Point(viewbox.min()[NR::X] / scale, viewbox.max()[NR::Y] / -scale),
804 NR::Point(viewbox.max()[NR::X] / scale, viewbox.min()[NR::Y] / -scale));
805 }
807 /**
808 * Revert back to previous zoom if possible.
809 */
810 void
811 SPDesktop::prev_zoom()
812 {
813 if (zooms_past == NULL) {
814 messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No previous zoom."));
815 return;
816 }
818 // push current zoom into forward zooms list
819 push_current_zoom (&zooms_future);
821 // restore previous zoom
822 set_display_area (((NRRect *) zooms_past->data)->x0,
823 ((NRRect *) zooms_past->data)->y0,
824 ((NRRect *) zooms_past->data)->x1,
825 ((NRRect *) zooms_past->data)->y1,
826 0, false);
828 // remove the just-added zoom from the past zooms list
829 zooms_past = g_list_remove (zooms_past, ((NRRect *) zooms_past->data));
830 }
832 /**
833 * Set zoom to next in list.
834 */
835 void
836 SPDesktop::next_zoom()
837 {
838 if (zooms_future == NULL) {
839 this->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No next zoom."));
840 return;
841 }
843 // push current zoom into past zooms list
844 push_current_zoom (&zooms_past);
846 // restore next zoom
847 set_display_area (((NRRect *) zooms_future->data)->x0,
848 ((NRRect *) zooms_future->data)->y0,
849 ((NRRect *) zooms_future->data)->x1,
850 ((NRRect *) zooms_future->data)->y1,
851 0, false);
853 // remove the just-used zoom from the zooms_future list
854 zooms_future = g_list_remove (zooms_future, ((NRRect *) zooms_future->data));
855 }
857 /**
858 * Zoom to point with absolute zoom factor.
859 */
860 void
861 SPDesktop::zoom_absolute_keep_point (double cx, double cy, double px, double py, double zoom)
862 {
863 zoom = CLAMP (zoom, SP_DESKTOP_ZOOM_MIN, SP_DESKTOP_ZOOM_MAX);
865 // maximum or minimum zoom reached, but there's no exact equality because of rounding errors;
866 // this check prevents "sliding" when trying to zoom in at maximum zoom;
867 /// \todo someone please fix calculations properly and remove this hack
868 if (fabs(expansion(_d2w) - zoom) < 0.0001*zoom && (fabs(SP_DESKTOP_ZOOM_MAX - zoom) < 0.01 || fabs(SP_DESKTOP_ZOOM_MIN - zoom) < 0.000001))
869 return;
871 NR::Rect const viewbox = canvas->getViewbox();
873 double const width2 = viewbox.dimensions()[NR::X] / zoom;
874 double const height2 = viewbox.dimensions()[NR::Y] / zoom;
876 set_display_area(cx - px * width2,
877 cy - py * height2,
878 cx + (1 - px) * width2,
879 cy + (1 - py) * height2,
880 0.0);
881 }
883 /**
884 * Zoom to center with absolute zoom factor.
885 */
886 void
887 SPDesktop::zoom_absolute (double cx, double cy, double zoom)
888 {
889 zoom_absolute_keep_point (cx, cy, 0.5, 0.5, zoom);
890 }
892 /**
893 * Zoom to point with relative zoom factor.
894 */
895 void
896 SPDesktop::zoom_relative_keep_point (double cx, double cy, double zoom)
897 {
898 NR::Rect const area = get_display_area();
900 if (cx < area.min()[NR::X]) {
901 cx = area.min()[NR::X];
902 }
903 if (cx > area.max()[NR::X]) {
904 cx = area.max()[NR::X];
905 }
906 if (cy < area.min()[NR::Y]) {
907 cy = area.min()[NR::Y];
908 }
909 if (cy > area.max()[NR::Y]) {
910 cy = area.max()[NR::Y];
911 }
913 gdouble const scale = expansion(_d2w) * zoom;
914 double const px = (cx - area.min()[NR::X]) / area.dimensions()[NR::X];
915 double const py = (cy - area.min()[NR::Y]) / area.dimensions()[NR::Y];
917 zoom_absolute_keep_point(cx, cy, px, py, scale);
918 }
920 /**
921 * Zoom to center with relative zoom factor.
922 */
923 void
924 SPDesktop::zoom_relative (double cx, double cy, double zoom)
925 {
926 gdouble scale = expansion(_d2w) * zoom;
927 zoom_absolute (cx, cy, scale);
928 }
930 /**
931 * Set display area to origin and current document dimensions.
932 */
933 void
934 SPDesktop::zoom_page()
935 {
936 NR::Rect d(NR::Point(0, 0),
937 NR::Point(sp_document_width(doc()), sp_document_height(doc())));
939 if (d.isEmpty(1.0)) {
940 return;
941 }
943 set_display_area(d, 10);
944 }
946 /**
947 * Set display area to current document width.
948 */
949 void
950 SPDesktop::zoom_page_width()
951 {
952 NR::Rect const a = get_display_area();
954 if (sp_document_width(doc()) < 1.0) {
955 return;
956 }
958 NR::Rect d(NR::Point(0, a.midpoint()[NR::Y]),
959 NR::Point(sp_document_width(doc()), a.midpoint()[NR::Y]));
961 set_display_area(d, 10);
962 }
964 /**
965 * Zoom to selection.
966 */
967 void
968 SPDesktop::zoom_selection()
969 {
970 boost::optional<NR::Rect> const d = selection->bounds();
972 if ( !d || d->isEmpty(0.1) ) {
973 return;
974 }
976 set_display_area(*d, 10);
977 }
979 /**
980 * Tell widget to let zoom widget grab keyboard focus.
981 */
982 void
983 SPDesktop::zoom_grab_focus()
984 {
985 _widget->letZoomGrabFocus();
986 }
988 /**
989 * Zoom to whole drawing.
990 */
991 void
992 SPDesktop::zoom_drawing()
993 {
994 g_return_if_fail (doc() != NULL);
995 SPItem *docitem = SP_ITEM (sp_document_root (doc()));
996 g_return_if_fail (docitem != NULL);
998 boost::optional<NR::Rect> d = sp_item_bbox_desktop(docitem);
1000 /* Note that the second condition here indicates that
1001 ** there are no items in the drawing.
1002 */
1003 if ( !d || d->isEmpty(1.0) ) {
1004 return;
1005 }
1007 set_display_area(*d, 10);
1008 }
1010 /**
1011 * Scroll canvas by specific coordinate amount in svg coordinates.
1012 */
1013 void
1014 SPDesktop::scroll_world_in_svg_coords (double dx, double dy, bool is_scrolling)
1015 {
1016 double scale = expansion(_d2w);
1017 scroll_world(dx*scale, dy*scale, is_scrolling);
1018 }
1020 /**
1021 * Scroll canvas by specific coordinate amount.
1022 */
1023 void
1024 SPDesktop::scroll_world (double dx, double dy, bool is_scrolling)
1025 {
1026 g_assert(_widget);
1028 NR::Rect const viewbox = canvas->getViewbox();
1030 sp_canvas_scroll_to(canvas, viewbox.min()[NR::X] - dx, viewbox.min()[NR::Y] - dy, FALSE, is_scrolling);
1032 /* update perspective lines if we are in the 3D box tool (so that infinite ones are shown correctly) */
1033 sp_box3d_context_update_lines(event_context);
1035 _widget->updateRulers();
1036 _widget->updateScrollbars(expansion(_d2w));
1037 }
1039 bool
1040 SPDesktop::scroll_to_point (Geom::Point const &p, gdouble autoscrollspeed)
1041 {
1042 gdouble autoscrolldistance = (gdouble) prefs_get_int_attribute_limited ("options.autoscrolldistance", "value", 0, -1000, 10000);
1044 // autoscrolldistance is in screen pixels, but the display area is in document units
1045 autoscrolldistance /= expansion(_d2w);
1046 NR::Rect const dbox = NR::expand(get_display_area(), -autoscrolldistance);
1048 if (!(p[NR::X] > dbox.min()[NR::X] && p[NR::X] < dbox.max()[NR::X]) ||
1049 !(p[NR::Y] > dbox.min()[NR::Y] && p[NR::Y] < dbox.max()[NR::Y]) ) {
1051 NR::Point const s_w( p * (Geom::Matrix)_d2w );
1053 gdouble x_to;
1054 if (p[NR::X] < dbox.min()[NR::X])
1055 x_to = dbox.min()[NR::X];
1056 else if (p[NR::X] > dbox.max()[NR::X])
1057 x_to = dbox.max()[NR::X];
1058 else
1059 x_to = p[NR::X];
1061 gdouble y_to;
1062 if (p[NR::Y] < dbox.min()[NR::Y])
1063 y_to = dbox.min()[NR::Y];
1064 else if (p[NR::Y] > dbox.max()[NR::Y])
1065 y_to = dbox.max()[NR::Y];
1066 else
1067 y_to = p[NR::Y];
1069 NR::Point const d_dt(x_to, y_to);
1070 NR::Point const d_w( d_dt * _d2w );
1071 NR::Point const moved_w( d_w - s_w );
1073 if (autoscrollspeed == 0)
1074 autoscrollspeed = prefs_get_double_attribute_limited ("options.autoscrollspeed", "value", 1, 0, 10);
1076 if (autoscrollspeed != 0)
1077 scroll_world (autoscrollspeed * moved_w);
1079 return true;
1080 }
1081 return false;
1082 }
1084 bool
1085 SPDesktop::is_iconified()
1086 {
1087 return 0!=(window_state & GDK_WINDOW_STATE_ICONIFIED);
1088 }
1090 void
1091 SPDesktop::iconify()
1092 {
1093 _widget->setIconified();
1094 }
1096 bool
1097 SPDesktop::is_maximized()
1098 {
1099 return 0!=(window_state & GDK_WINDOW_STATE_MAXIMIZED);
1100 }
1102 void
1103 SPDesktop::maximize()
1104 {
1105 _widget->setMaximized();
1106 }
1108 bool
1109 SPDesktop::is_fullscreen()
1110 {
1111 return 0!=(window_state & GDK_WINDOW_STATE_FULLSCREEN);
1112 }
1114 void
1115 SPDesktop::fullscreen()
1116 {
1117 _widget->setFullscreen();
1118 }
1120 void
1121 SPDesktop::getWindowGeometry (gint &x, gint &y, gint &w, gint &h)
1122 {
1123 _widget->getGeometry (x, y, w, h);
1124 }
1126 void
1127 SPDesktop::setWindowPosition (NR::Point p)
1128 {
1129 _widget->setPosition (p);
1130 }
1132 void
1133 SPDesktop::setWindowSize (gint w, gint h)
1134 {
1135 _widget->setSize (w, h);
1136 }
1138 void
1139 SPDesktop::setWindowTransient (void *p, int transient_policy)
1140 {
1141 _widget->setTransient (p, transient_policy);
1142 }
1144 Gtk::Window*
1145 SPDesktop::getToplevel( )
1146 {
1147 return _widget->getWindow();
1148 }
1150 void
1151 SPDesktop::presentWindow()
1152 {
1153 _widget->present();
1154 }
1156 bool
1157 SPDesktop::warnDialog (gchar *text)
1158 {
1159 return _widget->warnDialog (text);
1160 }
1162 void
1163 SPDesktop::toggleRulers()
1164 {
1165 _widget->toggleRulers();
1166 }
1168 void
1169 SPDesktop::toggleScrollbars()
1170 {
1171 _widget->toggleScrollbars();
1172 }
1174 void
1175 SPDesktop::layoutWidget()
1176 {
1177 _widget->layout();
1178 }
1180 void
1181 SPDesktop::destroyWidget()
1182 {
1183 _widget->destroy();
1184 }
1186 bool
1187 SPDesktop::shutdown()
1188 {
1189 return _widget->shutdown();
1190 }
1192 bool SPDesktop::onDeleteUI (GdkEventAny*)
1193 {
1194 if(shutdown())
1195 return true;
1197 destroyWidget();
1198 return false;
1199 }
1201 /**
1202 * onWindowStateEvent
1203 *
1204 * Called when the window changes its maximize/fullscreen/iconify/pinned state.
1205 * Since GTK doesn't have a way to query this state information directly, we
1206 * record it for the desktop here, and also possibly trigger a layout.
1207 */
1208 bool
1209 SPDesktop::onWindowStateEvent (GdkEventWindowState* event)
1210 {
1211 // Record the desktop window's state
1212 window_state = event->new_window_state;
1214 // Layout may differ depending on full-screen mode or not
1215 GdkWindowState changed = event->changed_mask;
1216 if (changed & (GDK_WINDOW_STATE_FULLSCREEN|GDK_WINDOW_STATE_MAXIMIZED)) {
1217 layoutWidget();
1218 }
1220 return false;
1221 }
1223 void
1224 SPDesktop::setToolboxFocusTo (gchar const *label)
1225 {
1226 _widget->setToolboxFocusTo (label);
1227 }
1229 void
1230 SPDesktop::setToolboxAdjustmentValue (gchar const* id, double val)
1231 {
1232 _widget->setToolboxAdjustmentValue (id, val);
1233 }
1235 void
1236 SPDesktop::setToolboxSelectOneValue (gchar const* id, gint val)
1237 {
1238 _widget->setToolboxSelectOneValue (id, val);
1239 }
1241 bool
1242 SPDesktop::isToolboxButtonActive (gchar const *id)
1243 {
1244 return _widget->isToolboxButtonActive (id);
1245 }
1247 void
1248 SPDesktop::emitToolSubselectionChanged(gpointer data)
1249 {
1250 _tool_subselection_changed.emit(data);
1251 inkscape_subselection_changed (this);
1252 }
1254 void
1255 SPDesktop::updateNow()
1256 {
1257 sp_canvas_update_now(canvas);
1258 }
1260 void
1261 SPDesktop::enableInteraction()
1262 {
1263 _widget->enableInteraction();
1264 }
1266 void SPDesktop::disableInteraction()
1267 {
1268 _widget->disableInteraction();
1269 }
1271 void SPDesktop::setWaitingCursor()
1272 {
1273 GdkCursor *waiting = gdk_cursor_new(GDK_WATCH);
1274 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(this))->window, waiting);
1275 gdk_cursor_unref(waiting);
1276 // GDK needs the flush for the cursor change to take effect
1277 gdk_flush();
1278 waiting_cursor = true;
1279 }
1281 void SPDesktop::clearWaitingCursor()
1282 {
1283 if (waiting_cursor)
1284 sp_event_context_update_cursor(sp_desktop_event_context(this));
1285 }
1287 void SPDesktop::toggleColorProfAdjust()
1288 {
1289 _widget->toggleColorProfAdjust();
1290 }
1292 void SPDesktop::toggleGrids()
1293 {
1294 if (namedview->grids) {
1295 if(gridgroup) {
1296 showGrids(!grids_visible);
1297 }
1298 } else {
1299 //there is no grid present at the moment. add a rectangular grid and make it visible
1300 Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
1301 Inkscape::CanvasGrid::writeNewGridToRepr(repr, sp_desktop_document(this), Inkscape::GRID_RECTANGULAR);
1302 showGrids(true);
1303 }
1304 }
1306 void SPDesktop::showGrids(bool show, bool dirty_document)
1307 {
1308 grids_visible = show;
1309 sp_namedview_show_grids(namedview, grids_visible, dirty_document);
1310 if (show) {
1311 sp_canvas_item_show(SP_CANVAS_ITEM(gridgroup));
1312 } else {
1313 sp_canvas_item_hide(SP_CANVAS_ITEM(gridgroup));
1314 }
1315 }
1317 void SPDesktop::toggleSnapping()
1318 {
1319 bool v = namedview->snap_manager.getSnapEnabledGlobally();
1320 Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
1321 sp_repr_set_boolean(repr, "inkscape:snap-global", !v);
1322 }
1324 //----------------------------------------------------------------------
1325 // Callback implementations. The virtual ones are connected by the view.
1327 void
1328 SPDesktop::onPositionSet (double x, double y)
1329 {
1330 _widget->viewSetPosition (NR::Point(x,y));
1331 }
1333 void
1334 SPDesktop::onResized (double /*x*/, double /*y*/)
1335 {
1336 // Nothing called here
1337 }
1339 /**
1340 * Redraw callback; queues Gtk redraw; connected by View.
1341 */
1342 void
1343 SPDesktop::onRedrawRequested ()
1344 {
1345 if (main) {
1346 _widget->requestCanvasUpdate();
1347 }
1348 }
1350 void
1351 SPDesktop::updateCanvasNow()
1352 {
1353 _widget->requestCanvasUpdateAndWait();
1354 }
1356 /**
1357 * Associate document with desktop.
1358 */
1359 void
1360 SPDesktop::setDocument (SPDocument *doc)
1361 {
1362 if (this->doc() && doc) {
1363 namedview->hide(this);
1364 sp_item_invoke_hide (SP_ITEM (sp_document_root (this->doc())), dkey);
1365 }
1367 if (_layer_hierarchy) {
1368 _layer_hierarchy->clear();
1369 delete _layer_hierarchy;
1370 }
1371 _layer_hierarchy = new Inkscape::ObjectHierarchy(NULL);
1372 _layer_hierarchy->connectAdded(sigc::bind(sigc::ptr_fun(_layer_activated), this));
1373 _layer_hierarchy->connectRemoved(sigc::bind(sigc::ptr_fun(_layer_deactivated), this));
1374 _layer_hierarchy->connectChanged(sigc::bind(sigc::ptr_fun(_layer_hierarchy_changed), this));
1375 _layer_hierarchy->setTop(SP_DOCUMENT_ROOT(doc));
1377 /* setup EventLog */
1378 event_log = new Inkscape::EventLog(doc);
1379 doc->addUndoObserver(*event_log);
1381 _commit_connection.disconnect();
1382 _commit_connection = doc->connectCommit(sigc::mem_fun(*this, &SPDesktop::updateNow));
1384 /// \todo fixme: This condition exists to make sure the code
1385 /// inside is NOT called on initialization, only on replacement. But there
1386 /// are surely more safe methods to accomplish this.
1387 // TODO since the comment had reversed logic, check the intent of this block of code:
1388 if (drawing) {
1389 NRArenaItem *ai = 0;
1391 namedview = sp_document_namedview (doc, NULL);
1392 _modified_connection = namedview->connectModified(sigc::bind<2>(sigc::ptr_fun(&_namedview_modified), this));
1393 number = namedview->getViewCount();
1395 ai = sp_item_invoke_show (SP_ITEM (sp_document_root (doc)),
1396 SP_CANVAS_ARENA (drawing)->arena,
1397 dkey,
1398 SP_ITEM_SHOW_DISPLAY);
1399 if (ai) {
1400 nr_arena_item_add_child (SP_CANVAS_ARENA (drawing)->root, ai, NULL);
1401 }
1402 namedview->show(this);
1403 /* Ugly hack */
1404 activate_guides (true);
1405 /* Ugly hack */
1406 _namedview_modified (namedview, SP_OBJECT_MODIFIED_FLAG, this);
1407 }
1409 _document_replaced_signal.emit (this, doc);
1411 View::setDocument (doc);
1412 }
1414 void
1415 SPDesktop::onStatusMessage
1416 (Inkscape::MessageType type, gchar const *message)
1417 {
1418 if (_widget) {
1419 _widget->setMessage(type, message);
1420 }
1421 }
1423 void
1424 SPDesktop::onDocumentURISet (gchar const* uri)
1425 {
1426 _widget->setTitle(uri);
1427 }
1429 /**
1430 * Resized callback.
1431 */
1432 void
1433 SPDesktop::onDocumentResized (gdouble width, gdouble height)
1434 {
1435 _doc2dt[5] = height;
1436 sp_canvas_item_affine_absolute (SP_CANVAS_ITEM (drawing), _doc2dt);
1437 NR::Rect const a(NR::Point(0, 0), NR::Point(width, height));
1438 SP_CTRLRECT(page)->setRectangle(a);
1439 SP_CTRLRECT(page_border)->setRectangle(a);
1440 }
1443 void
1444 SPDesktop::_onActivate (SPDesktop* dt)
1445 {
1446 if (!dt->_widget) return;
1447 dt->_widget->activateDesktop();
1448 }
1450 void
1451 SPDesktop::_onDeactivate (SPDesktop* dt)
1452 {
1453 if (!dt->_widget) return;
1454 dt->_widget->deactivateDesktop();
1455 }
1457 void
1458 SPDesktop::_onSelectionModified
1459 (Inkscape::Selection */*selection*/, guint /*flags*/, SPDesktop *dt)
1460 {
1461 if (!dt->_widget) return;
1462 dt->_widget->updateScrollbars (expansion(dt->_d2w));
1463 }
1465 static void
1466 _onSelectionChanged
1467 (Inkscape::Selection *selection, SPDesktop *desktop)
1468 {
1469 /** \todo
1470 * only change the layer for single selections, or what?
1471 * This seems reasonable -- for multiple selections there can be many
1472 * different layers involved.
1473 */
1474 SPItem *item=selection->singleItem();
1475 if (item) {
1476 SPObject *layer=desktop->layerForObject(item);
1477 if ( layer && layer != desktop->currentLayer() ) {
1478 desktop->setCurrentLayer(layer);
1479 }
1480 }
1481 }
1483 /**
1484 * Calls event handler of current event context.
1485 * \param arena Unused
1486 * \todo fixme
1487 */
1488 static gint
1489 _arena_handler (SPCanvasArena */*arena*/, NRArenaItem *ai, GdkEvent *event, SPDesktop *desktop)
1490 {
1491 if (ai) {
1492 SPItem *spi = (SPItem*)NR_ARENA_ITEM_GET_DATA (ai);
1493 return sp_event_context_item_handler (desktop->event_context, spi, event);
1494 } else {
1495 return sp_event_context_root_handler (desktop->event_context, event);
1496 }
1497 }
1499 static void
1500 _layer_activated(SPObject *layer, SPDesktop *desktop) {
1501 g_return_if_fail(SP_IS_GROUP(layer));
1502 SP_GROUP(layer)->setLayerDisplayMode(desktop->dkey, SPGroup::LAYER);
1503 }
1505 /// Callback
1506 static void
1507 _layer_deactivated(SPObject *layer, SPDesktop *desktop) {
1508 g_return_if_fail(SP_IS_GROUP(layer));
1509 SP_GROUP(layer)->setLayerDisplayMode(desktop->dkey, SPGroup::GROUP);
1510 }
1512 /// Callback
1513 static void
1514 _layer_hierarchy_changed(SPObject */*top*/, SPObject *bottom,
1515 SPDesktop *desktop)
1516 {
1517 desktop->_layer_changed_signal.emit (bottom);
1518 }
1520 /// Called when document is starting to be rebuilt.
1521 static void
1522 _reconstruction_start (SPDesktop * desktop)
1523 {
1524 // printf("Desktop, starting reconstruction\n");
1525 desktop->_reconstruction_old_layer_id = g_strdup(SP_OBJECT_ID(desktop->currentLayer()));
1526 desktop->_layer_hierarchy->setBottom(desktop->currentRoot());
1528 /*
1529 GSList const * selection_objs = desktop->selection->list();
1530 for (; selection_objs != NULL; selection_objs = selection_objs->next) {
1532 }
1533 */
1534 desktop->selection->clear();
1536 // printf("Desktop, starting reconstruction end\n");
1537 }
1539 /// Called when document rebuild is finished.
1540 static void
1541 _reconstruction_finish (SPDesktop * desktop)
1542 {
1543 // printf("Desktop, finishing reconstruction\n");
1544 if (desktop->_reconstruction_old_layer_id == NULL)
1545 return;
1547 SPObject * newLayer = SP_OBJECT_DOCUMENT(desktop->namedview)->getObjectById(desktop->_reconstruction_old_layer_id);
1548 if (newLayer != NULL)
1549 desktop->setCurrentLayer(newLayer);
1551 g_free(desktop->_reconstruction_old_layer_id);
1552 desktop->_reconstruction_old_layer_id = NULL;
1553 // printf("Desktop, finishing reconstruction end\n");
1554 return;
1555 }
1557 /**
1558 * Namedview_modified callback.
1559 */
1560 static void
1561 _namedview_modified (SPObject *obj, guint flags, SPDesktop *desktop)
1562 {
1563 SPNamedView *nv=SP_NAMEDVIEW(obj);
1565 if (flags & SP_OBJECT_MODIFIED_FLAG) {
1567 /* Recalculate snap distances */
1568 /* FIXME: why is the desktop getting involved in setting up something
1569 ** that is entirely to do with the namedview?
1570 */
1571 _update_snap_distances (desktop);
1573 /* Show/hide page background */
1574 if (nv->pagecolor & 0xff) {
1575 sp_canvas_item_show (desktop->table);
1576 ((CtrlRect *) desktop->table)->setColor(0x00000000, true, nv->pagecolor);
1577 sp_canvas_item_move_to_z (desktop->table, 0);
1578 } else {
1579 sp_canvas_item_hide (desktop->table);
1580 }
1582 /* Show/hide page border */
1583 if (nv->showborder) {
1584 // show
1585 sp_canvas_item_show (desktop->page_border);
1586 // set color and shadow
1587 ((CtrlRect *) desktop->page_border)->setColor(nv->bordercolor, false, 0x00000000);
1588 if (nv->pageshadow) {
1589 ((CtrlRect *) desktop->page_border)->setShadow(nv->pageshadow, nv->bordercolor);
1590 }
1591 // place in the z-order stack
1592 if (nv->borderlayer == SP_BORDER_LAYER_BOTTOM) {
1593 sp_canvas_item_move_to_z (desktop->page_border, 2);
1594 } else {
1595 int order = sp_canvas_item_order (desktop->page_border);
1596 int morder = sp_canvas_item_order (desktop->drawing);
1597 if (morder > order) sp_canvas_item_raise (desktop->page_border,
1598 morder - order);
1599 }
1600 } else {
1601 sp_canvas_item_hide (desktop->page_border);
1602 if (nv->pageshadow) {
1603 ((CtrlRect *) desktop->page)->setShadow(0, 0x00000000);
1604 }
1605 }
1607 /* Show/hide page shadow */
1608 if (nv->showpageshadow && nv->pageshadow) {
1609 ((CtrlRect *) desktop->page_border)->setShadow(nv->pageshadow, nv->bordercolor);
1610 } else {
1611 ((CtrlRect *) desktop->page_border)->setShadow(0, 0x00000000);
1612 }
1614 if (SP_RGBA32_A_U(nv->pagecolor) < 128 ||
1615 (SP_RGBA32_R_U(nv->pagecolor) +
1616 SP_RGBA32_G_U(nv->pagecolor) +
1617 SP_RGBA32_B_U(nv->pagecolor)) >= 384) {
1618 // the background color is light or transparent, use black outline
1619 SP_CANVAS_ARENA (desktop->drawing)->arena->outlinecolor = prefs_get_int_attribute("options.wireframecolors", "onlight", 0xff);
1620 } else { // use white outline
1621 SP_CANVAS_ARENA (desktop->drawing)->arena->outlinecolor = prefs_get_int_attribute("options.wireframecolors", "ondark", 0xffffffff);
1622 }
1623 }
1624 }
1626 /**
1627 * Callback to reset snapper's distances.
1628 */
1629 static void
1630 _update_snap_distances (SPDesktop *desktop)
1631 {
1632 SPUnit const &px = sp_unit_get_by_id(SP_UNIT_PX);
1634 SPNamedView &nv = *desktop->namedview;
1636 //tell all grid snappers
1637 for ( GSList const *l = nv.grids; l != NULL; l = l->next) {
1638 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
1639 grid->snapper->setSnapperTolerance(sp_convert_distance_full(nv.gridtolerance,
1640 *nv.gridtoleranceunit,
1641 px));
1642 }
1644 nv.snap_manager.guide.setSnapperTolerance(sp_convert_distance_full(nv.guidetolerance,
1645 *nv.guidetoleranceunit,
1646 px));
1647 nv.snap_manager.object.setSnapperTolerance(sp_convert_distance_full(nv.objecttolerance,
1648 *nv.objecttoleranceunit,
1649 px));
1650 }
1653 NR::Matrix SPDesktop::w2d() const
1654 {
1655 return _w2d;
1656 }
1658 NR::Point SPDesktop::w2d(NR::Point const &p) const
1659 {
1660 return p * _w2d;
1661 }
1663 NR::Point SPDesktop::d2w(NR::Point const &p) const
1664 {
1665 return p * _d2w;
1666 }
1668 NR::Matrix SPDesktop::doc2dt() const
1669 {
1670 return _doc2dt;
1671 }
1673 NR::Point SPDesktop::doc2dt(NR::Point const &p) const
1674 {
1675 return p * _doc2dt;
1676 }
1678 NR::Point SPDesktop::dt2doc(NR::Point const &p) const
1679 {
1680 return p * _doc2dt.inverse();
1681 }
1684 /**
1685 * Pop event context from desktop's context stack. Never used.
1686 */
1687 // void
1688 // SPDesktop::pop_event_context (unsigned int key)
1689 // {
1690 // SPEventContext *ec = NULL;
1691 //
1692 // if (event_context && event_context->key == key) {
1693 // g_return_if_fail (event_context);
1694 // g_return_if_fail (event_context->next);
1695 // ec = event_context;
1696 // sp_event_context_deactivate (ec);
1697 // event_context = ec->next;
1698 // sp_event_context_activate (event_context);
1699 // _event_context_changed_signal.emit (this, ec);
1700 // }
1701 //
1702 // SPEventContext *ref = event_context;
1703 // while (ref && ref->next && ref->next->key != key)
1704 // ref = ref->next;
1705 //
1706 // if (ref && ref->next) {
1707 // ec = ref->next;
1708 // ref->next = ec->next;
1709 // }
1710 //
1711 // if (ec) {
1712 // sp_event_context_finish (ec);
1713 // g_object_unref (G_OBJECT (ec));
1714 // }
1715 // }
1717 /*
1718 Local Variables:
1719 mode:c++
1720 c-file-style:"stroustrup"
1721 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1722 indent-tabs-mode:nil
1723 fill-column:99
1724 End:
1725 */
1726 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :