Code

Implement singleton behaviour for undo history dialog. Make sure event
[inkscape.git] / src / desktop.cpp
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  *
13  * Copyright (C) 2006 John Bintz
14  * Copyright (C) 2004 MenTaLguY
15  * Copyright (C) 1999-2002 Lauris Kaplinski
16  * Copyright (C) 2000-2001 Ximian, Inc.
17  *
18  * Released under GNU GPL, read the file 'COPYING' for more information
19  */
21 /** \class SPDesktop
22  * SPDesktop is a subclass of View, implementing an editable document
23  * canvas.  It is extensively used by many UI controls that need certain
24  * visual representations of their own.
25  *
26  * SPDesktop provides a certain set of SPCanvasItems, serving as GUI
27  * layers of different control objects. The one containing the whole
28  * document is the drawing layer. In addition to it, there are grid,
29  * guide, sketch and control layers. The sketch layer is used for
30  * temporary drawing objects, before the real objects in document are
31  * created. The control layer contains editing knots, rubberband and
32  * similar non-document UI objects.
33  *
34  * Each SPDesktop is associated with a SPNamedView node of the document
35  * tree.  Currently, all desktops are created from a single main named
36  * view, but in the future there may be support for different ones.
37  * SPNamedView serves as an in-document container for desktop-related
38  * data, like grid and guideline placement, snapping options and so on.
39  *
40  * Associated with each SPDesktop are the two most important editing
41  * related objects - SPSelection and SPEventContext.
42  *
43  * Sodipodi keeps track of the active desktop and invokes notification
44  * signals whenever it changes. UI elements can use these to update their
45  * display to the selection of the currently active editing window.
46  * (Lauris Kaplinski)
47  */
49 #ifdef HAVE_CONFIG_H
50 # include "config.h"
51 #endif
53 #include <glibmm/i18n.h>
54 #include <sigc++/functors/mem_fun.h>
56 #include "macros.h"
57 #include "inkscape-private.h"
58 #include "desktop.h"
59 #include "desktop-events.h"
60 #include "desktop-handles.h"
61 #include "document.h"
62 #include "message-stack.h"
63 #include "selection.h"
64 #include "select-context.h"
65 #include "sp-namedview.h"
66 #include "color.h"
67 #include "sp-item-group.h"
68 #include "prefs-utils.h"
69 #include "object-hierarchy.h"
70 #include "helper/units.h"
71 #include "display/canvas-arena.h"
72 #include "display/nr-arena.h"
73 #include "display/gnome-canvas-acetate.h"
74 #include "display/sodipodi-ctrlrect.h"
75 #include "display/sp-canvas-util.h"
76 #include "libnr/nr-matrix-div.h"
77 #include "libnr/nr-rect-ops.h"
78 #include "ui/dialog/dialog-manager.h"
79 #include "xml/repr.h"
80 #include "message-context.h"
81 #include "layer-manager.h"
82 #include "event-log.h"
84 namespace Inkscape { namespace XML { class Node; }}
86 // Callback declarations
87 static void _onSelectionChanged (Inkscape::Selection *selection, SPDesktop *desktop);
88 static gint _arena_handler (SPCanvasArena *arena, NRArenaItem *ai, GdkEvent *event, SPDesktop *desktop);
89 static void _layer_activated(SPObject *layer, SPDesktop *desktop);
90 static void _layer_deactivated(SPObject *layer, SPDesktop *desktop);
91 static void _layer_hierarchy_changed(SPObject *top, SPObject *bottom, SPDesktop *desktop);
92 static void _reconstruction_start(SPDesktop * desktop);
93 static void _reconstruction_finish(SPDesktop * desktop);
94 static void _namedview_modified (SPObject *obj, guint flags, SPDesktop *desktop);
95 static void _update_snap_distances (SPDesktop *desktop);
97 /**
98  * Return new desktop object.
99  * \pre namedview != NULL.
100  * \pre canvas != NULL.
101  */
102 SPDesktop::SPDesktop()
104     _dlg_mgr = NULL;
105     _widget = 0;
106     namedview = NULL;
107     selection = NULL;
108     acetate = NULL;
109     main = NULL;
110     grid = NULL;
111     guides = NULL;
112     drawing = NULL;
113     sketch = NULL;
114     controls = NULL;
115     event_context = 0;
116     layer_manager = 0;
118     _d2w.set_identity();
119     _w2d.set_identity();
120     _doc2dt = NR::Matrix(NR::scale(1, -1));
122     guides_active = false;
124     zooms_past = NULL;
125     zooms_future = NULL;
127     is_fullscreen = false;
129     gr_item = NULL;
130     gr_point_num = 0;
131     gr_fill_or_stroke = true;
133     _layer_hierarchy = NULL;
134     _active = false;
136     selection = Inkscape::GC::release (new Inkscape::Selection (this));
139 void
140 SPDesktop::init (SPNamedView *nv, SPCanvas *aCanvas)
143     _guides_message_context = new Inkscape::MessageContext(const_cast<Inkscape::MessageStack*>(messageStack()));
145     current = sp_repr_css_attr_inherited (inkscape_get_repr (INKSCAPE, "desktop"), "style");
147     namedview = nv;
148     canvas = aCanvas;
150     SPDocument *document = SP_OBJECT_DOCUMENT (namedview);
151     /* Kill flicker */
152     sp_document_ensure_up_to_date (document);
154     /* Setup Dialog Manager */
155     _dlg_mgr = new Inkscape::UI::Dialog::DialogManager();
157     dkey = sp_item_display_key_new (1);
159     /* Connect document */
160     setDocument (document);
162     number = namedview->getViewCount();
165     /* Setup Canvas */
166     g_object_set_data (G_OBJECT (canvas), "SPDesktop", this);
168     SPCanvasGroup *root = sp_canvas_root (canvas);
170     /* Setup adminstrative layers */
171     acetate = sp_canvas_item_new (root, GNOME_TYPE_CANVAS_ACETATE, NULL);
172     g_signal_connect (G_OBJECT (acetate), "event", G_CALLBACK (sp_desktop_root_handler), this);
173     main = (SPCanvasGroup *) sp_canvas_item_new (root, SP_TYPE_CANVAS_GROUP, NULL);
174     g_signal_connect (G_OBJECT (main), "event", G_CALLBACK (sp_desktop_root_handler), this);
176     table = sp_canvas_item_new (main, SP_TYPE_CTRLRECT, NULL);
177     SP_CTRLRECT(table)->setRectangle(NR::Rect(NR::Point(-80000, -80000), NR::Point(80000, 80000)));
178     SP_CTRLRECT(table)->setColor(0x00000000, true, 0x00000000);
179     sp_canvas_item_move_to_z (table, 0);
181     page = sp_canvas_item_new (main, SP_TYPE_CTRLRECT, NULL);
182     ((CtrlRect *) page)->setColor(0x00000000, FALSE, 0x00000000);
183     page_border = sp_canvas_item_new (main, SP_TYPE_CTRLRECT, NULL);
185     drawing = sp_canvas_item_new (main, SP_TYPE_CANVAS_ARENA, NULL);
186     g_signal_connect (G_OBJECT (drawing), "arena_event", G_CALLBACK (_arena_handler), this);
188     SP_CANVAS_ARENA (drawing)->arena->delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0); // default is 1 px
190     // Start always in normal mode
191     SP_CANVAS_ARENA (drawing)->arena->rendermode = RENDERMODE_NORMAL;
192     canvas->rendermode = RENDERMODE_NORMAL; // canvas needs that for choosing the best buffer size
194     grid = (SPCanvasGroup *) sp_canvas_item_new (main, SP_TYPE_CANVAS_GROUP, NULL);
195     guides = (SPCanvasGroup *) sp_canvas_item_new (main, SP_TYPE_CANVAS_GROUP, NULL);
196     sketch = (SPCanvasGroup *) sp_canvas_item_new (main, SP_TYPE_CANVAS_GROUP, NULL);
197     controls = (SPCanvasGroup *) sp_canvas_item_new (main, SP_TYPE_CANVAS_GROUP, NULL);
199     /* Push select tool to the bottom of stack */
200     /** \todo
201      * FIXME: this is the only call to this.  Everything else seems to just
202      * call "set" instead of "push".  Can we assume that there is only one
203      * context ever?
204      */
205     push_event_context (SP_TYPE_SELECT_CONTEXT, "tools.select", SP_EVENT_CONTEXT_STATIC);
207     // display rect and zoom are now handled in sp_desktop_widget_realize()
209     NR::Rect const d(NR::Point(0.0, 0.0),
210                      NR::Point(sp_document_width(document), sp_document_height(document)));
212     SP_CTRLRECT(page)->setRectangle(d);
213     SP_CTRLRECT(page_border)->setRectangle(d);
215     /* the following sets the page shadow on the canvas
216        It was originally set to 5, which is really cheesy!
217        It now is an attribute in the document's namedview. If a value of
218        0 is used, then the constructor for a shadow is not initialized.
219     */
221     if ( namedview->pageshadow != 0 && namedview->showpageshadow ) {
222         SP_CTRLRECT(page_border)->setShadow(namedview->pageshadow, 0x3f3f3fff);
223     }
226     /* Connect event for page resize */
227     _doc2dt[5] = sp_document_height (document);
228     sp_canvas_item_affine_absolute (SP_CANVAS_ITEM (drawing), _doc2dt);
230     _modified_connection = namedview->connectModified(sigc::bind<2>(sigc::ptr_fun(&_namedview_modified), this));
232     NRArenaItem *ai = sp_item_invoke_show (SP_ITEM (sp_document_root (document)),
233             SP_CANVAS_ARENA (drawing)->arena,
234             dkey,
235             SP_ITEM_SHOW_DISPLAY);
236     if (ai) {
237         nr_arena_item_add_child (SP_CANVAS_ARENA (drawing)->root, ai, NULL);
238         nr_arena_item_unref (ai);
239     }
241     namedview->show(this);
242     /* Ugly hack */
243     activate_guides (true);
244     /* Ugly hack */
245     _namedview_modified (namedview, SP_OBJECT_MODIFIED_FLAG, this);
247 /* Set up notification of rebuilding the document, this allows
248        for saving object related settings in the document. */
249     _reconstruction_start_connection =
250         document->connectReconstructionStart(sigc::bind(sigc::ptr_fun(_reconstruction_start), this));
251     _reconstruction_finish_connection =
252         document->connectReconstructionFinish(sigc::bind(sigc::ptr_fun(_reconstruction_finish), this));
253     _reconstruction_old_layer_id = NULL;
254     
255     _commit_connection = document->connectCommit(sigc::mem_fun(*this, &SPDesktop::updateNow));
256     
257     // ?
258     // sp_active_desktop_set (desktop);
259     _inkscape = INKSCAPE;
261     _activate_connection = _activate_signal.connect(
262         sigc::bind(
263             sigc::ptr_fun(_onActivate),
264             this
265         )
266     );
267      _deactivate_connection = _deactivate_signal.connect(
268         sigc::bind(
269             sigc::ptr_fun(_onDeactivate),
270             this
271         )
272     );
274     _sel_modified_connection = selection->connectModified(
275         sigc::bind(
276             sigc::ptr_fun(&_onSelectionModified),
277             this
278         )
279     );
280     _sel_changed_connection = selection->connectChanged(
281         sigc::bind(
282             sigc::ptr_fun(&_onSelectionChanged),
283             this
284         )
285     );
288     /* setup LayerManager */
289     //   (Setting up after the connections are all in place, as it may use some of them)
290     layer_manager = new Inkscape::LayerManager( this );
294 void SPDesktop::destroy()
296     _activate_connection.disconnect();
297     _deactivate_connection.disconnect();
298     _sel_modified_connection.disconnect();
299     _sel_changed_connection.disconnect();
300     _modified_connection.disconnect();
301     _commit_connection.disconnect();
302     _reconstruction_start_connection.disconnect();
303     _reconstruction_finish_connection.disconnect();
305     g_signal_handlers_disconnect_by_func(G_OBJECT (acetate), (gpointer) G_CALLBACK(sp_desktop_root_handler), this);
306     g_signal_handlers_disconnect_by_func(G_OBJECT (main), (gpointer) G_CALLBACK(sp_desktop_root_handler), this);
307     g_signal_handlers_disconnect_by_func(G_OBJECT (drawing), (gpointer) G_CALLBACK(_arena_handler), this);
309     while (event_context) {
310         SPEventContext *ec = event_context;
311         event_context = ec->next;
312         sp_event_context_finish (ec);
313         g_object_unref (G_OBJECT (ec));
314     }
316     if (_layer_hierarchy) {
317         delete _layer_hierarchy;
318     }
320     if (_inkscape) {
321         _inkscape = NULL;
322     }
324     if (drawing) {
325         sp_item_invoke_hide (SP_ITEM (sp_document_root (doc())), dkey);
326         drawing = NULL;
327     }
329     delete _guides_message_context;
330     _guides_message_context = NULL;
332     g_list_free (zooms_past);
333     g_list_free (zooms_future);
336 SPDesktop::~SPDesktop() {}
338 //--------------------------------------------------------------------
339 /* Public methods */
341 void SPDesktop::setDisplayModeNormal()
343     SP_CANVAS_ARENA (drawing)->arena->rendermode = RENDERMODE_NORMAL;
344     canvas->rendermode = RENDERMODE_NORMAL; // canvas needs that for choosing the best buffer size
345     sp_canvas_item_affine_absolute (SP_CANVAS_ITEM (main), _d2w); // redraw
348 void SPDesktop::setDisplayModeOutline()
350     SP_CANVAS_ARENA (drawing)->arena->rendermode = RENDERMODE_OUTLINE;
351     canvas->rendermode = RENDERMODE_OUTLINE; // canvas needs that for choosing the best buffer size
352     sp_canvas_item_affine_absolute (SP_CANVAS_ITEM (main), _d2w); // redraw
355 /**
356  * Returns current root (=bottom) layer.
357  */
358 SPObject *SPDesktop::currentRoot() const
360     return _layer_hierarchy ? _layer_hierarchy->top() : NULL;
363 /**
364  * Returns current top layer.
365  */
366 SPObject *SPDesktop::currentLayer() const
368     return _layer_hierarchy ? _layer_hierarchy->bottom() : NULL;
371 /**
372  * Sets the current layer of the desktop.
373  * 
374  * Make \a object the top layer.
375  */
376 void SPDesktop::setCurrentLayer(SPObject *object) {
377     g_return_if_fail(SP_IS_GROUP(object));
378     g_return_if_fail( currentRoot() == object || (currentRoot() && currentRoot()->isAncestorOf(object)) );
379     // printf("Set Layer to ID: %s\n", SP_OBJECT_ID(object));
380     _layer_hierarchy->setBottom(object);
383 /**
384  * Return layer that contains \a object.
385  */
386 SPObject *SPDesktop::layerForObject(SPObject *object) {
387     g_return_val_if_fail(object != NULL, NULL);
389     SPObject *root=currentRoot();
390     object = SP_OBJECT_PARENT(object);
391     while ( object && object != root && !isLayer(object) ) {
392         object = SP_OBJECT_PARENT(object);
393     }
394     return object;
397 /**
398  * True if object is a layer.
399  */
400 bool SPDesktop::isLayer(SPObject *object) const {
401     return ( SP_IS_GROUP(object)
402              && ( SP_GROUP(object)->effectiveLayerMode(this->dkey)
403                   == SPGroup::LAYER ) );
406 /**
407  * True if desktop viewport fully contains \a item's bbox.
408  */
409 bool SPDesktop::isWithinViewport (SPItem *item) const
411     NR::Rect const viewport = get_display_area();
412     NR::Rect const bbox = sp_item_bbox_desktop(item);
413     return viewport.contains(bbox);
416 ///
417 bool SPDesktop::itemIsHidden(SPItem const *item) const {
418     return item->isHidden(this->dkey);
421 /**
422  * Set activate property of desktop; emit signal if changed.
423  */
424 void
425 SPDesktop::set_active (bool new_active)
427     if (new_active != _active) {
428         _active = new_active;
429         if (new_active) {
430             _activate_signal.emit();
431         } else {
432             _deactivate_signal.emit();
433         }
434     }
437 /**
438  * Set activate status of current desktop's named view.
439  */
440 void
441 SPDesktop::activate_guides(bool activate)
443     guides_active = activate;
444     namedview->activateGuides(this, activate);
447 /**
448  * Make desktop switch documents.
449  */
450 void
451 SPDesktop::change_document (SPDocument *theDocument)
453     g_return_if_fail (theDocument != NULL);
455     /* unselect everything before switching documents */
456     selection->clear();
458     setDocument (theDocument);
459     _namedview_modified (namedview, SP_OBJECT_MODIFIED_FLAG, this);
460     _document_replaced_signal.emit (this, theDocument);
463 /**
464  * Make desktop switch event contexts.
465  */
466 void
467 SPDesktop::set_event_context (GtkType type, const gchar *config)
469     SPEventContext *ec;
470     while (event_context) {
471         ec = event_context;
472         sp_event_context_deactivate (ec);
473         event_context = ec->next;
474         sp_event_context_finish (ec);
475         g_object_unref (G_OBJECT (ec));
476     }
478     Inkscape::XML::Node *repr = (config) ? inkscape_get_repr (_inkscape, config) : NULL;
479     ec = sp_event_context_new (type, this, repr, SP_EVENT_CONTEXT_STATIC);
480     ec->next = event_context;
481     event_context = ec;
482     sp_event_context_activate (ec);
483     _event_context_changed_signal.emit (this, ec);
486 /**
487  * Push event context onto desktop's context stack.
488  */
489 void
490 SPDesktop::push_event_context (GtkType type, const gchar *config, unsigned int key)
492     SPEventContext *ref, *ec;
493     Inkscape::XML::Node *repr;
495     if (event_context && event_context->key == key) return;
496     ref = event_context;
497     while (ref && ref->next && ref->next->key != key) ref = ref->next;
498     if (ref && ref->next) {
499         ec = ref->next;
500         ref->next = ec->next;
501         sp_event_context_finish (ec);
502         g_object_unref (G_OBJECT (ec));
503     }
505     if (event_context) sp_event_context_deactivate (event_context);
506     repr = (config) ? inkscape_get_repr (INKSCAPE, config) : NULL;
507     ec = sp_event_context_new (type, this, repr, key);
508     ec->next = event_context;
509     event_context = ec;
510     sp_event_context_activate (ec);
511     _event_context_changed_signal.emit (this, ec);
514 /**
515  * Sets the coordinate status to a given point
516  */
517 void
518 SPDesktop::set_coordinate_status (NR::Point p) {
519     _widget->setCoordinateStatus(p);
522 /**
523  * \see sp_document_item_from_list_at_point_bottom()
524  */
525 SPItem *
526 SPDesktop::item_from_list_at_point_bottom (const GSList *list, NR::Point const p) const
528     g_return_val_if_fail (doc() != NULL, NULL);
529     return sp_document_item_from_list_at_point_bottom (dkey, SP_GROUP (doc()->root), list, p);
532 /**
533  * \see sp_document_item_at_point()
534  */
535 SPItem *
536 SPDesktop::item_at_point (NR::Point const p, bool into_groups, SPItem *upto) const
538     g_return_val_if_fail (doc() != NULL, NULL);
539     return sp_document_item_at_point (doc(), dkey, p, into_groups, upto);
542 /**
543  * \see sp_document_group_at_point()
544  */
545 SPItem *
546 SPDesktop::group_at_point (NR::Point const p) const
548     g_return_val_if_fail (doc() != NULL, NULL);
549     return sp_document_group_at_point (doc(), dkey, p);
552 /**
553  * \brief  Returns the mouse point in document coordinates; if mouse is
554  * outside the canvas, returns the center of canvas viewpoint
555  */
556 NR::Point
557 SPDesktop::point() const
559     NR::Point p = _widget->getPointer();
560     NR::Point pw = sp_canvas_window_to_world (canvas, p);
561     p = w2d(pw);
563     NR::Rect const r = canvas->getViewbox();
565     NR::Point r0 = w2d(r.min());
566     NR::Point r1 = w2d(r.max());
568     if (p[NR::X] >= r0[NR::X] &&
569         p[NR::X] <= r1[NR::X] &&
570         p[NR::Y] >= r1[NR::Y] &&
571         p[NR::Y] <= r0[NR::Y])
572     {
573         return p;
574     } else {
575         return (r0 + r1) / 2;
576     }
579 /**
580  * Put current zoom data in history list.
581  */
582 void
583 SPDesktop::push_current_zoom (GList **history)
585     NR::Rect const area = get_display_area();
587     NRRect *old_zoom = g_new(NRRect, 1);
588     old_zoom->x0 = area.min()[NR::X];
589     old_zoom->x1 = area.max()[NR::X];
590     old_zoom->y0 = area.min()[NR::Y];
591     old_zoom->y1 = area.max()[NR::Y];
592     if ( *history == NULL
593          || !( ( ((NRRect *) ((*history)->data))->x0 == old_zoom->x0 ) &&
594                ( ((NRRect *) ((*history)->data))->x1 == old_zoom->x1 ) &&
595                ( ((NRRect *) ((*history)->data))->y0 == old_zoom->y0 ) &&
596                ( ((NRRect *) ((*history)->data))->y1 == old_zoom->y1 ) ) )
597     {
598         *history = g_list_prepend (*history, old_zoom);
599     }
602 /**
603  * Set viewbox.
604  */
605 void
606 SPDesktop::set_display_area (double x0, double y0, double x1, double y1, double border, bool log)
608     g_assert(_widget);
610     // save the zoom
611     if (log) {
612         push_current_zoom(&zooms_past);
613         // if we do a logged zoom, our zoom-forward list is invalidated, so delete it
614         g_list_free (zooms_future);
615         zooms_future = NULL;
616     }
618     double const cx = 0.5 * (x0 + x1);
619     double const cy = 0.5 * (y0 + y1);
621     NR::Rect const viewbox = NR::expand(canvas->getViewbox(), border);
623     double scale = expansion(_d2w);
624     double newscale;
625     if (((x1 - x0) * viewbox.dimensions()[NR::Y]) > ((y1 - y0) * viewbox.dimensions()[NR::X])) {
626         newscale = viewbox.dimensions()[NR::X] / (x1 - x0);
627     } else {
628         newscale = viewbox.dimensions()[NR::Y] / (y1 - y0);
629     }
631     newscale = CLAMP(newscale, SP_DESKTOP_ZOOM_MIN, SP_DESKTOP_ZOOM_MAX);
633     int clear = FALSE;
634     if (!NR_DF_TEST_CLOSE (newscale, scale, 1e-4 * scale)) {
635         /* Set zoom factors */
636         _d2w = NR::Matrix(NR::scale(newscale, -newscale));
637         _w2d = NR::Matrix(NR::scale(1/newscale, 1/-newscale));
638         sp_canvas_item_affine_absolute(SP_CANVAS_ITEM(main), _d2w);
639         clear = TRUE;
640     }
642     /* Calculate top left corner */
643     x0 = cx - 0.5 * viewbox.dimensions()[NR::X] / newscale;
644     y1 = cy + 0.5 * viewbox.dimensions()[NR::Y] / newscale;
646     /* Scroll */
647     sp_canvas_scroll_to (canvas, x0 * newscale - border, y1 * -newscale - border, clear);
649     _widget->updateRulers();
650     _widget->updateScrollbars(expansion(_d2w));
651     _widget->updateZoom();
654 void SPDesktop::set_display_area(NR::Rect const &a, NR::Coord b, bool log)
656     set_display_area(a.min()[NR::X], a.min()[NR::Y], a.max()[NR::X], a.max()[NR::Y], b, log);
659 /**
660  * Return viewbox dimensions.
661  */
662 NR::Rect SPDesktop::get_display_area() const
664     NR::Rect const viewbox = canvas->getViewbox();
666     double const scale = _d2w[0];
668     return NR::Rect(NR::Point(viewbox.min()[NR::X] / scale, viewbox.max()[NR::Y] / -scale),
669                     NR::Point(viewbox.max()[NR::X] / scale, viewbox.min()[NR::Y] / -scale));
672 /**
673  * Revert back to previous zoom if possible.
674  */
675 void
676 SPDesktop::prev_zoom()
678     if (zooms_past == NULL) {
679         messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No previous zoom."));
680         return;
681     }
683     // push current zoom into forward zooms list
684     push_current_zoom (&zooms_future);
686     // restore previous zoom
687     set_display_area (((NRRect *) zooms_past->data)->x0,
688             ((NRRect *) zooms_past->data)->y0,
689             ((NRRect *) zooms_past->data)->x1,
690             ((NRRect *) zooms_past->data)->y1,
691             0, false);
693     // remove the just-added zoom from the past zooms list
694     zooms_past = g_list_remove (zooms_past, ((NRRect *) zooms_past->data));
697 /**
698  * Set zoom to next in list.
699  */
700 void
701 SPDesktop::next_zoom()
703     if (zooms_future == NULL) {
704         this->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No next zoom."));
705         return;
706     }
708     // push current zoom into past zooms list
709     push_current_zoom (&zooms_past);
711     // restore next zoom
712     set_display_area (((NRRect *) zooms_future->data)->x0,
713             ((NRRect *) zooms_future->data)->y0,
714             ((NRRect *) zooms_future->data)->x1,
715             ((NRRect *) zooms_future->data)->y1,
716             0, false);
718     // remove the just-used zoom from the zooms_future list
719     zooms_future = g_list_remove (zooms_future, ((NRRect *) zooms_future->data));
722 /**
723  * Zoom to point with absolute zoom factor.
724  */
725 void
726 SPDesktop::zoom_absolute_keep_point (double cx, double cy, double px, double py, double zoom)
728     zoom = CLAMP (zoom, SP_DESKTOP_ZOOM_MIN, SP_DESKTOP_ZOOM_MAX);
730     // maximum or minimum zoom reached, but there's no exact equality because of rounding errors;
731     // this check prevents "sliding" when trying to zoom in at maximum zoom;
732     /// \todo someone please fix calculations properly and remove this hack
733     if (fabs(expansion(_d2w) - zoom) < 0.0001*zoom && (fabs(SP_DESKTOP_ZOOM_MAX - zoom) < 0.01 || fabs(SP_DESKTOP_ZOOM_MIN - zoom) < 0.000001))
734         return;
736     NR::Rect const viewbox = canvas->getViewbox();
738     double const width2 = viewbox.dimensions()[NR::X] / zoom;
739     double const height2 = viewbox.dimensions()[NR::Y] / zoom;
741     set_display_area(cx - px * width2,
742                      cy - py * height2,
743                      cx + (1 - px) * width2,
744                      cy + (1 - py) * height2,
745                      0.0);
748 /**
749  * Zoom to center with absolute zoom factor.
750  */
751 void
752 SPDesktop::zoom_absolute (double cx, double cy, double zoom)
754     zoom_absolute_keep_point (cx, cy, 0.5, 0.5, zoom);
757 /**
758  * Zoom to point with relative zoom factor.
759  */
760 void
761 SPDesktop::zoom_relative_keep_point (double cx, double cy, double zoom)
763     NR::Rect const area = get_display_area();
765     if (cx < area.min()[NR::X]) {
766         cx = area.min()[NR::X];
767     }
768     if (cx > area.max()[NR::X]) {
769         cx = area.max()[NR::X];
770     }
771     if (cy < area.min()[NR::Y]) {
772         cy = area.min()[NR::Y];
773     }
774     if (cy > area.max()[NR::Y]) {
775         cy = area.max()[NR::Y];
776     }
778     gdouble const scale = expansion(_d2w) * zoom;
779     double const px = (cx - area.min()[NR::X]) / area.dimensions()[NR::X];
780     double const py = (cy - area.min()[NR::Y]) / area.dimensions()[NR::Y];
782     zoom_absolute_keep_point(cx, cy, px, py, scale);
785 /**
786  * Zoom to center with relative zoom factor.
787  */
788 void
789 SPDesktop::zoom_relative (double cx, double cy, double zoom)
791     gdouble scale = expansion(_d2w) * zoom;
792     zoom_absolute (cx, cy, scale);
795 /**
796  * Set display area to origin and current document dimensions.
797  */
798 void
799 SPDesktop::zoom_page()
801     NR::Rect d(NR::Point(0, 0),
802                NR::Point(sp_document_width(doc()), sp_document_height(doc())));
804     if (d.dimensions()[NR::X] < 1.0 || d.dimensions()[NR::Y] < 1.0) {
805         return;
806     }
808     set_display_area(d, 10);
811 /**
812  * Set display area to current document width.
813  */
814 void
815 SPDesktop::zoom_page_width()
817     NR::Rect const a = get_display_area();
819     if (sp_document_width(doc()) < 1.0) {
820         return;
821     }
823     NR::Rect d(NR::Point(0, a.midpoint()[NR::Y]),
824                NR::Point(sp_document_width(doc()), a.midpoint()[NR::Y]));
826     set_display_area(d, 10);
829 /**
830  * Zoom to selection.
831  */
832 void
833 SPDesktop::zoom_selection()
835     NR::Rect const d = selection->bounds();
837     if (d.dimensions()[NR::X] < 0.1 || d.dimensions()[NR::Y] < 0.1) {
838         return;
839     }
841     set_display_area(d, 10);
844 /**
845  * Tell widget to let zoom widget grab keyboard focus.
846  */
847 void
848 SPDesktop::zoom_grab_focus()
850     _widget->letZoomGrabFocus();
853 /**
854  * Zoom to whole drawing.
855  */
856 void
857 SPDesktop::zoom_drawing()
859     g_return_if_fail (doc() != NULL);
860     SPItem *docitem = SP_ITEM (sp_document_root (doc()));
861     g_return_if_fail (docitem != NULL);
863     NR::Rect d = sp_item_bbox_desktop(docitem);
865     /* Note that the second condition here indicates that
866     ** there are no items in the drawing.
867     */
868     if ( d.dimensions()[NR::X] < 1.0 || d.dimensions()[NR::Y] < 1.0 ) {
869         return;
870     }
872     set_display_area(d, 10);
875 /**
876  * Scroll canvas by specific coordinate amount.
877  */
878 void
879 SPDesktop::scroll_world (double dx, double dy)
881     g_assert(_widget);
883     NR::Rect const viewbox = canvas->getViewbox();
885     sp_canvas_scroll_to(canvas, viewbox.min()[NR::X] - dx, viewbox.min()[NR::Y] - dy, FALSE);
887     _widget->updateRulers();
888     _widget->updateScrollbars(expansion(_d2w));
891 bool
892 SPDesktop::scroll_to_point (NR::Point const *p, gdouble autoscrollspeed)
894     gdouble autoscrolldistance = (gdouble) prefs_get_int_attribute_limited ("options.autoscrolldistance", "value", 0, -1000, 10000);
896     // autoscrolldistance is in screen pixels, but the display area is in document units
897     autoscrolldistance /= expansion(_d2w);
898     NR::Rect const dbox = NR::expand(get_display_area(), -autoscrolldistance);
900     if (!((*p)[NR::X] > dbox.min()[NR::X] && (*p)[NR::X] < dbox.max()[NR::X]) ||
901         !((*p)[NR::Y] > dbox.min()[NR::Y] && (*p)[NR::Y] < dbox.max()[NR::Y])   ) {
903         NR::Point const s_w( (*p) * _d2w );
905         gdouble x_to;
906         if ((*p)[NR::X] < dbox.min()[NR::X])
907             x_to = dbox.min()[NR::X];
908         else if ((*p)[NR::X] > dbox.max()[NR::X])
909             x_to = dbox.max()[NR::X];
910         else
911             x_to = (*p)[NR::X];
913         gdouble y_to;
914         if ((*p)[NR::Y] < dbox.min()[NR::Y])
915             y_to = dbox.min()[NR::Y];
916         else if ((*p)[NR::Y] > dbox.max()[NR::Y])
917             y_to = dbox.max()[NR::Y];
918         else
919             y_to = (*p)[NR::Y];
921         NR::Point const d_dt(x_to, y_to);
922         NR::Point const d_w( d_dt * _d2w );
923         NR::Point const moved_w( d_w - s_w );
925         if (autoscrollspeed == 0)
926             autoscrollspeed = prefs_get_double_attribute_limited ("options.autoscrollspeed", "value", 1, 0, 10);
928         if (autoscrollspeed != 0)
929             scroll_world (autoscrollspeed * moved_w);
931         return true;
932     }
933     return false;
936 void
937 SPDesktop::fullscreen()
939     _widget->setFullscreen();
942 void
943 SPDesktop::getWindowGeometry (gint &x, gint &y, gint &w, gint &h)
945     _widget->getGeometry (x, y, w, h);
948 void
949 SPDesktop::setWindowPosition (NR::Point p)
951     _widget->setPosition (p);
954 void
955 SPDesktop::setWindowSize (gint w, gint h)
957     _widget->setSize (w, h);
960 void
961 SPDesktop::setWindowTransient (void *p, int transient_policy)
963     _widget->setTransient (p, transient_policy);
966 void
967 SPDesktop::presentWindow()
969     _widget->present();
972 bool
973 SPDesktop::warnDialog (gchar *text)
975     return _widget->warnDialog (text);
978 void
979 SPDesktop::toggleRulers()
981     _widget->toggleRulers();
984 void
985 SPDesktop::toggleScrollbars()
987     _widget->toggleScrollbars();
990 void
991 SPDesktop::layoutWidget()
993     _widget->layout();
996 void
997 SPDesktop::destroyWidget()
999     _widget->destroy();
1002 bool
1003 SPDesktop::shutdown()
1005     return _widget->shutdown();
1008 void
1009 SPDesktop::setToolboxFocusTo (gchar const *label)
1011     _widget->setToolboxFocusTo (label);
1014 void
1015 SPDesktop::setToolboxAdjustmentValue (gchar const* id, double val)
1017     _widget->setToolboxAdjustmentValue (id, val);
1020 bool
1021 SPDesktop::isToolboxButtonActive (gchar const *id)
1023     return _widget->isToolboxButtonActive (id);
1026 void
1027 SPDesktop::emitToolSubselectionChanged(gpointer data)
1029         _tool_subselection_changed.emit(data);
1030         inkscape_subselection_changed (this);
1033 void
1034 SPDesktop::updateNow()
1036   sp_canvas_update_now(canvas);
1039 void
1040 SPDesktop::enableInteraction()
1042   _widget->enableInteraction();
1045 void SPDesktop::disableInteraction()
1047   _widget->disableInteraction();
1050 //----------------------------------------------------------------------
1051 // Callback implementations. The virtual ones are connected by the view.
1053 void
1054 SPDesktop::onPositionSet (double x, double y)
1056     _widget->viewSetPosition (NR::Point(x,y));
1059 void
1060 SPDesktop::onResized (double x, double y)
1062    // Nothing called here
1065 /**
1066  * Redraw callback; queues Gtk redraw; connected by View.
1067  */
1068 void
1069 SPDesktop::onRedrawRequested ()
1071     if (main) {
1072         _widget->requestCanvasUpdate();
1073     }
1076 void
1077 SPDesktop::updateCanvasNow()
1079   _widget->requestCanvasUpdateAndWait();
1082 /**
1083  * Associate document with desktop.
1084  */
1085 /// \todo fixme: refactor SPDesktop::init to use setDocument
1086 void
1087 SPDesktop::setDocument (SPDocument *doc)
1089     if (this->doc() && doc) {
1090         namedview->hide(this);
1091         sp_item_invoke_hide (SP_ITEM (sp_document_root (this->doc())), dkey);
1092     }
1094     if (_layer_hierarchy) {
1095         _layer_hierarchy->clear();
1096         delete _layer_hierarchy;
1097     }
1098     _layer_hierarchy = new Inkscape::ObjectHierarchy(NULL);
1099     _layer_hierarchy->connectAdded(sigc::bind(sigc::ptr_fun(_layer_activated), this));
1100     _layer_hierarchy->connectRemoved(sigc::bind(sigc::ptr_fun(_layer_deactivated), this));
1101     _layer_hierarchy->connectChanged(sigc::bind(sigc::ptr_fun(_layer_hierarchy_changed), this));
1102     _layer_hierarchy->setTop(SP_DOCUMENT_ROOT(doc));
1104     /* setup EventLog */
1105     event_log = new Inkscape::EventLog(doc);
1106     doc->addUndoObserver(*event_log);
1108     _commit_connection.disconnect();
1109     _commit_connection = doc->connectCommit(sigc::mem_fun(*this, &SPDesktop::updateNow));
1111     /// \todo fixme: This condition exists to make sure the code
1112     /// inside is called only once on initialization. But there
1113     /// are surely more safe methods to accomplish this.
1114     if (drawing) {
1115         NRArenaItem *ai;
1117         namedview = sp_document_namedview (doc, NULL);
1118         _modified_connection = namedview->connectModified(sigc::bind<2>(sigc::ptr_fun(&_namedview_modified), this));
1119         number = namedview->getViewCount();
1121         ai = sp_item_invoke_show (SP_ITEM (sp_document_root (doc)),
1122                 SP_CANVAS_ARENA (drawing)->arena,
1123                 dkey,
1124                 SP_ITEM_SHOW_DISPLAY);
1125         if (ai) {
1126             nr_arena_item_add_child (SP_CANVAS_ARENA (drawing)->root, ai, NULL);
1127             nr_arena_item_unref (ai);
1128         }
1129         namedview->show(this);
1130         /* Ugly hack */
1131         activate_guides (true);
1132         /* Ugly hack */
1133         _namedview_modified (namedview, SP_OBJECT_MODIFIED_FLAG, this);
1134     }
1136     _document_replaced_signal.emit (this, doc);
1138     View::setDocument (doc);
1141 void
1142 SPDesktop::onStatusMessage
1143 (Inkscape::MessageType type, gchar const *message)
1145     if (_widget) {
1146         _widget->setMessage(type, message);
1147     }
1150 void
1151 SPDesktop::onDocumentURISet (gchar const* uri)
1153     _widget->setTitle(uri);
1156 /**
1157  * Resized callback.
1158  */
1159 void
1160 SPDesktop::onDocumentResized (gdouble width, gdouble height)
1162     _doc2dt[5] = height;
1163     sp_canvas_item_affine_absolute (SP_CANVAS_ITEM (drawing), _doc2dt);
1164     NR::Rect const a(NR::Point(0, 0), NR::Point(width, height));
1165     SP_CTRLRECT(page)->setRectangle(a);
1166     SP_CTRLRECT(page_border)->setRectangle(a);
1170 void
1171 SPDesktop::_onActivate (SPDesktop* dt)
1173     if (!dt->_widget) return;
1174     dt->_widget->activateDesktop();
1177 void
1178 SPDesktop::_onDeactivate (SPDesktop* dt)
1180     if (!dt->_widget) return;
1181     dt->_widget->deactivateDesktop();
1184 void
1185 SPDesktop::_onSelectionModified
1186 (Inkscape::Selection *selection, guint flags, SPDesktop *dt)
1188     if (!dt->_widget) return;
1189     dt->_widget->updateScrollbars (expansion(dt->_d2w));
1192 static void
1193 _onSelectionChanged
1194 (Inkscape::Selection *selection, SPDesktop *desktop)
1196     /** \todo
1197      * only change the layer for single selections, or what?
1198      * This seems reasonable -- for multiple selections there can be many
1199      * different layers involved.
1200      */
1201     SPItem *item=selection->singleItem();
1202     if (item) {
1203         SPObject *layer=desktop->layerForObject(item);
1204         if ( layer && layer != desktop->currentLayer() ) {
1205             desktop->setCurrentLayer(layer);
1206         }
1207     }
1210 /**
1211  * Calls event handler of current event context.
1212  * \param arena Unused
1213  * \todo fixme
1214  */
1215 static gint
1216 _arena_handler (SPCanvasArena *arena, NRArenaItem *ai, GdkEvent *event, SPDesktop *desktop)
1218     if (ai) {
1219         SPItem *spi = (SPItem*)NR_ARENA_ITEM_GET_DATA (ai);
1220         return sp_event_context_item_handler (desktop->event_context, spi, event);
1221     } else {
1222         return sp_event_context_root_handler (desktop->event_context, event);
1223     }
1226 static void
1227 _layer_activated(SPObject *layer, SPDesktop *desktop) {
1228     g_return_if_fail(SP_IS_GROUP(layer));
1229     SP_GROUP(layer)->setLayerDisplayMode(desktop->dkey, SPGroup::LAYER);
1232 /// Callback
1233 static void
1234 _layer_deactivated(SPObject *layer, SPDesktop *desktop) {
1235     g_return_if_fail(SP_IS_GROUP(layer));
1236     SP_GROUP(layer)->setLayerDisplayMode(desktop->dkey, SPGroup::GROUP);
1239 /// Callback
1240 static void
1241 _layer_hierarchy_changed(SPObject *top, SPObject *bottom,
1242                                          SPDesktop *desktop)
1244     desktop->_layer_changed_signal.emit (bottom);
1247 /// Called when document is starting to be rebuilt.
1248 static void
1249 _reconstruction_start (SPDesktop * desktop)
1251     // printf("Desktop, starting reconstruction\n");
1252     desktop->_reconstruction_old_layer_id = g_strdup(SP_OBJECT_ID(desktop->currentLayer()));
1253     desktop->_layer_hierarchy->setBottom(desktop->currentRoot());
1255     /*
1256     GSList const * selection_objs = desktop->selection->list();
1257     for (; selection_objs != NULL; selection_objs = selection_objs->next) {
1259     }
1260     */
1261     desktop->selection->clear();
1263     // printf("Desktop, starting reconstruction end\n");
1266 /// Called when document rebuild is finished.
1267 static void
1268 _reconstruction_finish (SPDesktop * desktop)
1270     // printf("Desktop, finishing reconstruction\n");
1271     if (desktop->_reconstruction_old_layer_id == NULL)
1272         return;
1274     SPObject * newLayer = SP_OBJECT_DOCUMENT(desktop->namedview)->getObjectById(desktop->_reconstruction_old_layer_id);
1275     if (newLayer != NULL)
1276         desktop->setCurrentLayer(newLayer);
1278     g_free(desktop->_reconstruction_old_layer_id);
1279     desktop->_reconstruction_old_layer_id = NULL;
1280     // printf("Desktop, finishing reconstruction end\n");
1281     return;
1284 /**
1285  * Namedview_modified callback.
1286  */
1287 static void
1288 _namedview_modified (SPObject *obj, guint flags, SPDesktop *desktop)
1290     SPNamedView *nv=SP_NAMEDVIEW(obj);
1292     if (flags & SP_OBJECT_MODIFIED_FLAG) {
1294         /* Recalculate snap distances */
1295         /* FIXME: why is the desktop getting involved in setting up something
1296         ** that is entirely to do with the namedview?
1297         */
1298         _update_snap_distances (desktop);
1300         /* Show/hide page background */
1301         if (nv->pagecolor & 0xff) {
1302             sp_canvas_item_show (desktop->table);
1303             ((CtrlRect *) desktop->table)->setColor(0x00000000, true, nv->pagecolor);
1304             sp_canvas_item_move_to_z (desktop->table, 0);
1305         } else {
1306             sp_canvas_item_hide (desktop->table);
1307         }
1309         /* Show/hide page border */
1310         if (nv->showborder) {
1311             // show
1312             sp_canvas_item_show (desktop->page_border);
1313             // set color and shadow
1314             ((CtrlRect *) desktop->page_border)->setColor(nv->bordercolor, false, 0x00000000);
1315             if (nv->pageshadow) {
1316                 ((CtrlRect *) desktop->page_border)->setShadow(nv->pageshadow, nv->bordercolor);
1317             }
1318             // place in the z-order stack
1319             if (nv->borderlayer == SP_BORDER_LAYER_BOTTOM) {
1320                  sp_canvas_item_move_to_z (desktop->page_border, 2);
1321             } else {
1322                 int order = sp_canvas_item_order (desktop->page_border);
1323                 int morder = sp_canvas_item_order (desktop->drawing);
1324                 if (morder > order) sp_canvas_item_raise (desktop->page_border,
1325                                     morder - order);
1326             }
1327         } else {
1328                 sp_canvas_item_hide (desktop->page_border);
1329                 if (nv->pageshadow) {
1330                     ((CtrlRect *) desktop->page)->setShadow(0, 0x00000000);
1331                 }
1332         }
1333         
1334         /* Show/hide page shadow */
1335         if (nv->showpageshadow && nv->pageshadow) {
1336             ((CtrlRect *) desktop->page_border)->setShadow(nv->pageshadow, nv->bordercolor);
1337         } else {
1338             ((CtrlRect *) desktop->page_border)->setShadow(0, 0x00000000);
1339         }
1341         if (SP_RGBA32_A_U(nv->pagecolor) < 128 ||
1342             (SP_RGBA32_R_U(nv->pagecolor) +
1343              SP_RGBA32_G_U(nv->pagecolor) +
1344              SP_RGBA32_B_U(nv->pagecolor)) >= 384) {
1345             // the background color is light or transparent, use black outline
1346             SP_CANVAS_ARENA (desktop->drawing)->arena->outlinecolor = 0xff;
1347         } else { // use white outline
1348             SP_CANVAS_ARENA (desktop->drawing)->arena->outlinecolor = 0xffffffff;
1349         }
1350     }
1353 /**
1354  * Callback to reset snapper's distances.
1355  */
1356 static void
1357 _update_snap_distances (SPDesktop *desktop)
1359     SPUnit const &px = sp_unit_get_by_id(SP_UNIT_PX);
1361     SPNamedView &nv = *desktop->namedview;
1363     nv.snap_manager.grid.setDistance(sp_convert_distance_full(nv.gridtolerance,
1364                                                                       *nv.gridtoleranceunit,
1365                                                                       px));
1366     nv.snap_manager.guide.setDistance(sp_convert_distance_full(nv.guidetolerance,
1367                                                                        *nv.guidetoleranceunit,
1368                                                                        px));
1369     nv.snap_manager.object.setDistance(sp_convert_distance_full(nv.objecttolerance,
1370                                                                         *nv.objecttoleranceunit,
1371                                                                         px));
1375 NR::Matrix SPDesktop::w2d() const
1377     return _w2d;
1380 NR::Point SPDesktop::w2d(NR::Point const &p) const
1382     return p * _w2d;
1385 NR::Point SPDesktop::d2w(NR::Point const &p) const
1387     return p * _d2w;
1390 NR::Matrix SPDesktop::doc2dt() const
1392     return _doc2dt;
1395 NR::Point SPDesktop::doc2dt(NR::Point const &p) const
1397     return p * _doc2dt;
1400 NR::Point SPDesktop::dt2doc(NR::Point const &p) const
1402     return p / _doc2dt;
1406 /**
1407  * Pop event context from desktop's context stack. Never used.
1408  */
1409 // void
1410 // SPDesktop::pop_event_context (unsigned int key)
1411 // {
1412 //    SPEventContext *ec = NULL;
1413 //
1414 //    if (event_context && event_context->key == key) {
1415 //        g_return_if_fail (event_context);
1416 //        g_return_if_fail (event_context->next);
1417 //        ec = event_context;
1418 //        sp_event_context_deactivate (ec);
1419 //        event_context = ec->next;
1420 //        sp_event_context_activate (event_context);
1421 //        _event_context_changed_signal.emit (this, ec);
1422 //    }
1423 //
1424 //    SPEventContext *ref = event_context;
1425 //    while (ref && ref->next && ref->next->key != key)
1426 //        ref = ref->next;
1427 //
1428 //    if (ref && ref->next) {
1429 //        ec = ref->next;
1430 //        ref->next = ec->next;
1431 //    }
1432 //
1433 //    if (ec) {
1434 //        sp_event_context_finish (ec);
1435 //        g_object_unref (G_OBJECT (ec));
1436 //    }
1437 // }
1439 /*
1440   Local Variables:
1441   mode:c++
1442   c-file-style:"stroustrup"
1443   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1444   indent-tabs-mode:nil
1445   fill-column:99
1446   End:
1447 */
1448 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :