Code

Workaround for some segfaults: Store perspectives globally instead of in each desktop...
[inkscape.git] / src / box3d-context.cpp
1 #define __SP_3DBOX_CONTEXT_C__
3 /*
4  * 3D box drawing context
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2007      Maximilian Albert <Anhalter42@gmx.de>
11  * Copyright (C) 2006      Johan Engelen <johan@shouraizou.nl>
12  * Copyright (C) 2000-2005 authors
13  * Copyright (C) 2000-2001 Ximian, Inc.
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #include "config.h"
20 #include <gdk/gdkkeysyms.h>
22 #include "macros.h"
23 #include "display/sp-canvas.h"
24 #include "document.h"
25 #include "sp-namedview.h"
26 #include "selection.h"
27 #include "desktop-handles.h"
28 #include "snap.h"
29 #include "display/curve.h"
30 #include "desktop.h"
31 #include "message-context.h"
32 #include "pixmaps/cursor-rect.xpm"
33 #include "box3d.h"
34 #include "box3d-context.h"
35 #include "sp-metrics.h"
36 #include <glibmm/i18n.h>
37 #include "object-edit.h"
38 #include "xml/repr.h"
39 #include "xml/node-event-vector.h"
40 #include "prefs-utils.h"
41 #include "context-fns.h"
43 static void sp_3dbox_context_class_init(SP3DBoxContextClass *klass);
44 static void sp_3dbox_context_init(SP3DBoxContext *box3d_context);
45 static void sp_3dbox_context_dispose(GObject *object);
47 static void sp_3dbox_context_setup(SPEventContext *ec);
48 static void sp_3dbox_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
50 static gint sp_3dbox_context_root_handler(SPEventContext *event_context, GdkEvent *event);
51 static gint sp_3dbox_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
53 static void sp_3dbox_drag(SP3DBoxContext &bc, guint state);
54 static void sp_3dbox_finish(SP3DBoxContext *bc);
56 static SPEventContextClass *parent_class;
59 GtkType sp_3dbox_context_get_type()
60 {
61     static GType type = 0;
62     if (!type) {
63         GTypeInfo info = {
64             sizeof(SP3DBoxContextClass),
65             NULL, NULL,
66             (GClassInitFunc) sp_3dbox_context_class_init,
67             NULL, NULL,
68             sizeof(SP3DBoxContext),
69             4,
70             (GInstanceInitFunc) sp_3dbox_context_init,
71             NULL,    /* value_table */
72         };
73         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SP3DBoxContext", &info, (GTypeFlags) 0);
74     }
75     return type;
76 }
78 static void sp_3dbox_context_class_init(SP3DBoxContextClass *klass)
79 {
80     GObjectClass *object_class = (GObjectClass *) klass;
81     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
83     parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
85     object_class->dispose = sp_3dbox_context_dispose;
87     event_context_class->setup = sp_3dbox_context_setup;
88     event_context_class->set = sp_3dbox_context_set;
89     event_context_class->root_handler  = sp_3dbox_context_root_handler;
90     event_context_class->item_handler  = sp_3dbox_context_item_handler;
91 }
93 guint SP3DBoxContext::number_of_handles = 3;
95 static void sp_3dbox_context_init(SP3DBoxContext *box3d_context)
96 {
97     SPEventContext *event_context = SP_EVENT_CONTEXT(box3d_context);
99     event_context->cursor_shape = cursor_rect_xpm;
100     event_context->hot_x = 4;
101     event_context->hot_y = 4;
102     event_context->xp = 0;
103     event_context->yp = 0;
104     event_context->tolerance = 0;
105     event_context->within_tolerance = false;
106     event_context->item_to_select = NULL;
108     event_context->shape_repr = NULL;
109     event_context->shape_knot_holder = NULL;
111     box3d_context->item = NULL;
113     box3d_context->ctrl_dragged = false;
114     box3d_context->extruded = false;
115     
116     new (&box3d_context->sel_changed_connection) sigc::connection();
119 static void sp_3dbox_context_dispose(GObject *object)
121     SP3DBoxContext *bc = SP_3DBOX_CONTEXT(object);
122     SPEventContext *ec = SP_EVENT_CONTEXT(object);
124     ec->enableGrDrag(false);
126     bc->sel_changed_connection.disconnect();
127     bc->sel_changed_connection.~connection();
129     /* fixme: This is necessary because we do not grab */
130     if (bc->item) {
131         sp_3dbox_finish(bc);
132     }
134     if (ec->shape_knot_holder) {
135         sp_knot_holder_destroy(ec->shape_knot_holder);
136         ec->shape_knot_holder = NULL;
137     }
139     if (ec->shape_repr) { // remove old listener
140         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
141         Inkscape::GC::release(ec->shape_repr);
142         ec->shape_repr = 0;
143     }
145     if (bc->_message_context) {
146         delete bc->_message_context;
147     }
149     G_OBJECT_CLASS(parent_class)->dispose(object);
152 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
153     NULL, /* child_added */
154     NULL, /* child_removed */
155     ec_shape_event_attr_changed,
156     NULL, /* content_changed */
157     NULL  /* order_changed */
158 };
160 /**
161 \brief  Callback that processes the "changed" signal on the selection;
162 destroys old and creates new knotholder
163 */
164 void sp_3dbox_context_selection_changed(Inkscape::Selection *selection, gpointer data)
166     SP3DBoxContext *bc = SP_3DBOX_CONTEXT(data);
167     SPEventContext *ec = SP_EVENT_CONTEXT(bc);
169     if (ec->shape_knot_holder) { // destroy knotholder
170         sp_knot_holder_destroy(ec->shape_knot_holder);
171         ec->shape_knot_holder = NULL;
172     }
174     if (ec->shape_repr) { // remove old listener
175         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
176         Inkscape::GC::release(ec->shape_repr);
177         ec->shape_repr = 0;
178     }
180     SPItem *item = selection->singleItem();
181     if (item) {
182         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
183         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
184         if (shape_repr) {
185             ec->shape_repr = shape_repr;
186             Inkscape::GC::anchor(shape_repr);
187             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
188         }
189     }
192 static void sp_3dbox_context_setup(SPEventContext *ec)
194     SP3DBoxContext *bc = SP_3DBOX_CONTEXT(ec);
196     if (((SPEventContextClass *) parent_class)->setup) {
197         ((SPEventContextClass *) parent_class)->setup(ec);
198     }
200     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
201     if (item) {
202         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
203         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
204         if (shape_repr) {
205             ec->shape_repr = shape_repr;
206             Inkscape::GC::anchor(shape_repr);
207             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
208         }
209     }
211     bc->sel_changed_connection.disconnect();
212     bc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
213         sigc::bind(sigc::ptr_fun(&sp_3dbox_context_selection_changed), (gpointer)bc)
214     );
216     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
217         ec->enableSelectionCue();
218     }
220     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
221         ec->enableGrDrag();
222     }
224     bc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
227 static void sp_3dbox_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
229     //SP3DBoxContext *bc = SP_3DBOX_CONTEXT(ec);
231     /* fixme: Proper error handling for non-numeric data.  Use a locale-independent function like
232      * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
233     /**
234     if ( strcmp(key, "rx") == 0 ) {
235         bc->rx = ( val
236                          ? g_ascii_strtod (val, NULL)
237                          : 0.0 );
238     } else if ( strcmp(key, "ry") == 0 ) {
239         bc->ry = ( val
240                          ? g_ascii_strtod (val, NULL)
241                          : 0.0 );
242     }
243     **/
246 static gint sp_3dbox_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
248     SPDesktop *desktop = event_context->desktop;
250     gint ret = FALSE;
252     switch (event->type) {
253     case GDK_BUTTON_PRESS:
254         if ( event->button.button == 1 && !event_context->space_panning) {
255             Inkscape::setup_for_drag_start(desktop, event_context, event);
256             ret = TRUE;
257         }
258         break;
259         // motion and release are always on root (why?)
260     default:
261         break;
262     }
264     if (((SPEventContextClass *) parent_class)->item_handler) {
265         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
266     }
268     return ret;
271 static gint sp_3dbox_context_root_handler(SPEventContext *event_context, GdkEvent *event)
273     static bool dragging;
275     SPDesktop *desktop = event_context->desktop;
276     Inkscape::Selection *selection = sp_desktop_selection (desktop);
278     SP3DBoxContext *bc = SP_3DBOX_CONTEXT(event_context);
280     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
282     gint ret = FALSE;
283     switch (event->type) {
284     case GDK_BUTTON_PRESS:
285         if ( event->button.button == 1  && !event_context->space_panning) {
286             NR::Point const button_w(event->button.x,
287                                      event->button.y);
289             // save drag origin
290             event_context->xp = (gint) button_w[NR::X];
291             event_context->yp = (gint) button_w[NR::Y];
292             event_context->within_tolerance = true;
293             
294             // remember clicked item, disregarding groups, honoring Alt
295             event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, event->button.state & GDK_CONTROL_MASK);
297             dragging = true;
298             
299             /* Position center */
300             NR::Point const button_dt(desktop->w2d(button_w));
301             bc->drag_origin = button_dt;
302             bc->drag_ptB = button_dt;
303             bc->drag_ptC = button_dt;
305             /* Snap center */
306             SnapManager const &m = desktop->namedview->snap_manager;
307             bc->center = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE | Inkscape::Snapper::SNAPPOINT_BBOX,
308                                     button_dt, bc->item).getPoint();
310             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
311                                 ( GDK_KEY_PRESS_MASK |
312                                   GDK_BUTTON_RELEASE_MASK       |
313                                   GDK_POINTER_MOTION_MASK       |
314                                   GDK_BUTTON_PRESS_MASK ),
315                                 NULL, event->button.time);
316             ret = TRUE;
317         }
318         break;
319     case GDK_MOTION_NOTIFY:
320         if ( dragging
321              && ( event->motion.state & GDK_BUTTON1_MASK )  && !event_context->space_panning)
322         {
323             if ( event_context->within_tolerance
324                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
325                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
326                 break; // do not drag if we're within tolerance from origin
327             }
328             // Once the user has moved farther than tolerance from the original location
329             // (indicating they intend to draw, not click), then always process the
330             // motion notify coordinates as given (no snapping back to origin)
331             event_context->within_tolerance = false;
333             NR::Point const motion_w(event->motion.x,
334                                      event->motion.y);
335             NR::Point motion_dt(desktop->w2d(motion_w));
337             SnapManager const &m = desktop->namedview->snap_manager;
338             motion_dt = m.freeSnap(Inkscape::Snapper::SNAPPOINT_BBOX | Inkscape::Snapper::SNAPPOINT_NODE, motion_dt, bc->item).getPoint();
340             bc->ctrl_dragged  = event->motion.state & GDK_CONTROL_MASK;
342             if (event->motion.state & GDK_SHIFT_MASK && !bc->extruded) {
343                 /* once shift is pressed, set bc->extruded (no need to create further faces;
344                    all of them are already created in sp_3dbox_init) */
345                 bc->extruded = true;
346             }
348             if (!bc->extruded) {
349                 bc->drag_ptB = motion_dt;
350                 bc->drag_ptC = motion_dt;
351             } else {
352                 // Without Ctrl, motion of the extruded corner is constrained to the
353                 // perspective line from drag_ptB to vanishing point Y.
354                 if (!bc->ctrl_dragged) {
355                         bc->drag_ptC = Box3D::perspective_line_snap (bc->drag_ptB, Box3D::Z, motion_dt, Box3D::Perspective3D::current_perspective);
356                 } else {
357                     bc->drag_ptC = motion_dt;
358                 }
359                 bc->drag_ptC = m.freeSnap(Inkscape::Snapper::SNAPPOINT_BBOX | Inkscape::Snapper::SNAPPOINT_NODE, bc->drag_ptC, bc->item).getPoint();
360                 if (bc->ctrl_dragged) {
361                         Box3D::PerspectiveLine pl1 (NR::Point (event_context->xp, event_context->yp), Box3D::Y, Box3D::Perspective3D::current_perspective);
362                         Box3D::PerspectiveLine pl2 (bc->drag_ptB, Box3D::X, Box3D::Perspective3D::current_perspective);
363                         NR::Point corner1 = pl1.meet(pl2);
364                         
365                         Box3D::PerspectiveLine pl3 (corner1, Box3D::X, Box3D::Perspective3D::current_perspective);
366                         Box3D::PerspectiveLine pl4 (bc->drag_ptC, Box3D::Z, Box3D::Perspective3D::current_perspective);
367                         bc->drag_ptB = pl3.meet(pl4);
368                 }
369             }
370             
371             
372             sp_3dbox_drag(*bc, event->motion.state);
373             
374             ret = TRUE;
375         }
376         break;
377     case GDK_BUTTON_RELEASE:
378         event_context->xp = event_context->yp = 0;
379         if ( event->button.button == 1  && !event_context->space_panning) {
380             dragging = false;
382             if (!event_context->within_tolerance) {
383                 // we've been dragging, finish the box
384                 sp_3dbox_finish(bc);
385             } else if (event_context->item_to_select) {
386                 // no dragging, select clicked item if any
387                 if (event->button.state & GDK_SHIFT_MASK) {
388                     selection->toggle(event_context->item_to_select);
389                 } else {
390                     selection->set(event_context->item_to_select);
391                 }
392             } else {
393                 // click in an empty space
394                 selection->clear();
395             }
397             event_context->item_to_select = NULL;
398             ret = TRUE;
399             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
400                                   event->button.time);
401         }
402         break;
403     case GDK_KEY_PRESS:
404         switch (get_group0_keyval (&event->key)) {
405         case GDK_Alt_L:
406         case GDK_Alt_R:
407         case GDK_Control_L:
408         case GDK_Control_R:
409         case GDK_Shift_L:
410         case GDK_Shift_R:
411         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
412         case GDK_Meta_R:
413             /***
414             if (!dragging){
415                 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
416                                             _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
417                                             _("<b>Shift</b>: draw around the starting point"),
418                                             NULL);
419             }
420             ***/
421             break;
422         case GDK_Up:
423         case GDK_Down:
424         case GDK_KP_Up:
425         case GDK_KP_Down:
426             // prevent the zoom field from activation
427             if (!MOD__CTRL_ONLY)
428                 ret = TRUE;
429             break;
431         case GDK_I:
432             Box3D::Perspective3D::print_debugging_info();
433             ret = true;
434             break;
436         case GDK_x:
437         case GDK_X:
438             if (MOD__ALT_ONLY) {
439                 // desktop->setToolboxFocusTo ("altx-rect");
440                 ret = TRUE;
441             }
442             break;
444         case GDK_Escape:
445             sp_desktop_selection(desktop)->clear();
446             //TODO: make dragging escapable by Esc
447             break;
449         case GDK_space:
450             if (dragging) {
451                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
452                                       event->button.time);
453                 dragging = false;
454                 if (!event_context->within_tolerance) {
455                     // we've been dragging, finish the box
456                     sp_3dbox_finish(bc);
457                 }
458                 // do not return true, so that space would work switching to selector
459             }
460             break;
462         default:
463             break;
464         }
465         break;
466     case GDK_KEY_RELEASE:
467         switch (get_group0_keyval (&event->key)) {
468         case GDK_Alt_L:
469         case GDK_Alt_R:
470         case GDK_Control_L:
471         case GDK_Control_R:
472         case GDK_Shift_L:
473         case GDK_Shift_R:
474         case GDK_Meta_L:  // Meta is when you press Shift+Alt
475         case GDK_Meta_R:
476             event_context->defaultMessageContext()->clear();
477             break;
478         default:
479             break;
480         }
481         break;
482     default:
483         break;
484     }
486     if (!ret) {
487         if (((SPEventContextClass *) parent_class)->root_handler) {
488             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
489         }
490     }
492     return ret;
495 static void sp_3dbox_drag(SP3DBoxContext &bc, guint state)
497     SPDesktop *desktop = SP_EVENT_CONTEXT(&bc)->desktop;
499     if (!bc.item) {
501         if (Inkscape::have_viable_layer(desktop, bc._message_context) == false) {
502             return;
503         }
505         /* Create object */
506         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&bc));
507         Inkscape::XML::Node *repr = xml_doc->createElement("svg:g");
508         repr->setAttribute("sodipodi:type", "inkscape:3dbox");
510         /* Set style */
511         sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.3dbox", false);
513         bc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
514         Inkscape::GC::release(repr);
515         bc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
517         /* Hook paths to the faces of the box */
518         for (int i = 0; i < 6; ++i) {
519             SP_3DBOX(bc.item)->faces[i]->hook_path_to_3dbox();
520         }
522         bc.item->updateRepr();
524         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
525     }
527     // FIXME: remove these extra points
528     NR::Point pt = bc.drag_ptB;
529     NR::Point shift_pt = bc.drag_ptC;
531     NR::Rect r;
532     if (!(state & GDK_SHIFT_MASK)) {
533         r = Inkscape::snap_rectangular_box(desktop, bc.item, pt, bc.center, state);
534     } else {
535         r = Inkscape::snap_rectangular_box(desktop, bc.item, shift_pt, bc.center, state);
536     }
538     SPEventContext *ec = SP_EVENT_CONTEXT(&bc);
539     NR::Point origin_w(ec->xp, ec->yp);
540     NR::Point origin(desktop->w2d(origin_w));
541     sp_3dbox_position_set(bc);
543     // status text
544     //GString *Ax = SP_PX_TO_METRIC_STRING(origin[NR::X], desktop->namedview->getDefaultMetric());
545     //GString *Ay = SP_PX_TO_METRIC_STRING(origin[NR::Y], desktop->namedview->getDefaultMetric());
546     bc._message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>3D Box</b>; with <b>Shift</b> to extrude along the Z axis"));
547     //g_string_free(Ax, FALSE);
548     //g_string_free(Ay, FALSE);
551 static void sp_3dbox_finish(SP3DBoxContext *bc)
553     bc->_message_context->clear();
555     if ( bc->item != NULL ) {
556         SPDesktop * desktop;
558         desktop = SP_EVENT_CONTEXT_DESKTOP(bc);
560         SP_OBJECT(bc->item)->updateRepr();
561         sp_3dbox_set_ratios(SP_3DBOX(bc->item));
563         sp_canvas_end_forced_full_redraws(desktop->canvas);
565         sp_desktop_selection(desktop)->set(bc->item);
566         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
567                          _("Create 3D box"));
569         bc->item = NULL;
570     }
572     bc->ctrl_dragged = false;
573     bc->extruded = false;
576 /*
577   Local Variables:
578   mode:c++
579   c-file-style:"stroustrup"
580   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
581   indent-tabs-mode:nil
582   fill-column:99
583   End:
584 */
585 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :