Code

updated catalan.nsh from launchpad by Xavi Conde
[inkscape.git] / src / box3d-context.cpp
1 #define __SP_BOX3D_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-3dbox.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"
42 #include "inkscape.h"
43 #include "desktop-style.h"
44 #include "transf_mat_3x4.h"
45 #include "perspective-line.h"
46 #include "persp3d.h"
47 #include "box3d-side.h"
48 #include "document-private.h" // for debugging (see case GDK_P)
49 #include "line-geometry.h"
51 static void sp_box3d_context_class_init(Box3DContextClass *klass);
52 static void sp_box3d_context_init(Box3DContext *box3d_context);
53 static void sp_box3d_context_dispose(GObject *object);
55 static void sp_box3d_context_setup(SPEventContext *ec);
57 static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEvent *event);
58 static gint sp_box3d_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
60 static void sp_box3d_drag(Box3DContext &bc, guint state);
61 static void sp_box3d_finish(Box3DContext *bc);
63 static SPEventContextClass *parent_class;
65 GtkType sp_box3d_context_get_type()
66 {
67     static GType type = 0;
68     if (!type) {
69         GTypeInfo info = {
70             sizeof(Box3DContextClass),
71             NULL, NULL,
72             (GClassInitFunc) sp_box3d_context_class_init,
73             NULL, NULL,
74             sizeof(Box3DContext),
75             4,
76             (GInstanceInitFunc) sp_box3d_context_init,
77             NULL,    /* value_table */
78         };
79         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "Box3DContext", &info, (GTypeFlags) 0);
80     }
81     return type;
82 }
84 static void sp_box3d_context_class_init(Box3DContextClass *klass)
85 {
86     GObjectClass *object_class = (GObjectClass *) klass;
87     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
89     parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
91     object_class->dispose = sp_box3d_context_dispose;
93     event_context_class->setup = sp_box3d_context_setup;
94     event_context_class->root_handler  = sp_box3d_context_root_handler;
95     event_context_class->item_handler  = sp_box3d_context_item_handler;
96 }
98 static void sp_box3d_context_init(Box3DContext *box3d_context)
99 {
100     SPEventContext *event_context = SP_EVENT_CONTEXT(box3d_context);
102     event_context->cursor_shape = cursor_3dbox_xpm;
103     event_context->hot_x = 4;
104     event_context->hot_y = 4;
105     event_context->xp = 0;
106     event_context->yp = 0;
107     event_context->tolerance = 0;
108     event_context->within_tolerance = false;
109     event_context->item_to_select = NULL;
111     event_context->shape_repr = NULL;
112     event_context->shape_knot_holder = NULL;
114     box3d_context->item = NULL;
116     box3d_context->ctrl_dragged = false;
117     box3d_context->extruded = false;
118     
119     box3d_context->_vpdrag = NULL;
121     new (&box3d_context->sel_changed_connection) sigc::connection();
124 static void sp_box3d_context_dispose(GObject *object)
126     Box3DContext *bc = SP_BOX3D_CONTEXT(object);
127     SPEventContext *ec = SP_EVENT_CONTEXT(object);
129     ec->enableGrDrag(false);
131     delete (bc->_vpdrag);
132     bc->_vpdrag = NULL;
134     bc->sel_changed_connection.disconnect();
135     bc->sel_changed_connection.~connection();
137     /* fixme: This is necessary because we do not grab */
138     if (bc->item) {
139         sp_box3d_finish(bc);
140     }
142     if (ec->shape_knot_holder) {
143         sp_knot_holder_destroy(ec->shape_knot_holder);
144         ec->shape_knot_holder = NULL;
145     }
147     if (ec->shape_repr) { // remove old listener
148         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
149         Inkscape::GC::release(ec->shape_repr);
150         ec->shape_repr = 0;
151     }
153     if (bc->_message_context) {
154         delete bc->_message_context;
155     }
157     G_OBJECT_CLASS(parent_class)->dispose(object);
160 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
161     NULL, /* child_added */
162     NULL, /* child_removed */
163     ec_shape_event_attr_changed,
164     NULL, /* content_changed */
165     NULL  /* order_changed */
166 };
168 /**
169 \brief  Callback that processes the "changed" signal on the selection;
170 destroys old and creates new knotholder
171 */
172 static void sp_box3d_context_selection_changed(Inkscape::Selection *selection, gpointer data)
174     Box3DContext *bc = SP_BOX3D_CONTEXT(data);
175     SPEventContext *ec = SP_EVENT_CONTEXT(bc);
177     if (ec->shape_knot_holder) { // destroy knotholder
178         sp_knot_holder_destroy(ec->shape_knot_holder);
179         ec->shape_knot_holder = NULL;
180     }
182     if (ec->shape_repr) { // remove old listener
183         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
184         Inkscape::GC::release(ec->shape_repr);
185         ec->shape_repr = 0;
186     }
188     SPDocument *doc = sp_desktop_document(bc->desktop);
189     doc->persps_sel.clear();
190     doc->persps_sel = persp3d_currently_selected_persps(ec);
192     SPItem *item = selection->singleItem();
193     if (item) {
194         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
195         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
196         if (shape_repr) {
197             ec->shape_repr = shape_repr;
198             Inkscape::GC::anchor(shape_repr);
199             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
200         }
201     }
203     if (doc->persps_sel.size() == 1) {
204         // selecting a single box changes the current perspective
205         doc->current_persp3d = *(doc->persps_sel.begin());
206     }
209 static void sp_box3d_context_setup(SPEventContext *ec)
211     Box3DContext *bc = SP_BOX3D_CONTEXT(ec);
213     if (((SPEventContextClass *) parent_class)->setup) {
214         ((SPEventContextClass *) parent_class)->setup(ec);
215     }
217     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
218     if (item) {
219         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
220         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
221         if (shape_repr) {
222             ec->shape_repr = shape_repr;
223             Inkscape::GC::anchor(shape_repr);
224             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
225         }
226     }
228     bc->sel_changed_connection.disconnect();
229     bc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
230         sigc::bind(sigc::ptr_fun(&sp_box3d_context_selection_changed), (gpointer)bc)
231     );
233     bc->_vpdrag = new Box3D::VPDrag(sp_desktop_document (ec->desktop));
235     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
236         ec->enableSelectionCue();
237     }
239     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
240         ec->enableGrDrag();
241     }
243     bc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
246 static gint sp_box3d_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_box3d_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);
277     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
279     Box3DContext *bc = SP_BOX3D_CONTEXT(event_context);
280     g_assert (SP_ACTIVE_DOCUMENT->current_persp3d);
281     Persp3D *cur_persp = SP_ACTIVE_DOCUMENT->current_persp3d;
283     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
285     gint ret = FALSE;
286     switch (event->type) {
287     case GDK_BUTTON_PRESS:
288         if ( event->button.button == 1  && !event_context->space_panning) {
289             NR::Point const button_w(event->button.x,
290                                      event->button.y);
292             // save drag origin
293             event_context->xp = (gint) button_w[NR::X];
294             event_context->yp = (gint) button_w[NR::Y];
295             event_context->within_tolerance = true;
296             
297             // remember clicked item, *not* disregarding groups (since a 3D box is a group), honoring Alt
298             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);
300             dragging = true;
301             
302             /*  */
303             NR::Point const button_dt(desktop->w2d(button_w));
304             bc->drag_origin = button_dt;
305             bc->drag_ptB = button_dt;
306             bc->drag_ptC = button_dt;
308             /* Projective preimages of clicked point under current perspective */
309             bc->drag_origin_proj = cur_persp->tmat.preimage (button_dt, 0, Proj::Z);
310             bc->drag_ptB_proj = bc->drag_origin_proj;
311             bc->drag_ptC_proj = bc->drag_origin_proj;
312             bc->drag_ptC_proj.normalize();
313             bc->drag_ptC_proj[Proj::Z] = 0.25;
315             /* Snap center */
316             SnapManager const &m = desktop->namedview->snap_manager;
317             bc->center = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE,
318                                     button_dt, bc->item).getPoint();
320             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
321                                 ( GDK_KEY_PRESS_MASK |
322                                   GDK_BUTTON_RELEASE_MASK       |
323                                   GDK_POINTER_MOTION_MASK       |
324                                   GDK_BUTTON_PRESS_MASK ),
325                                 NULL, event->button.time);
326             ret = TRUE;
327         }
328         break;
329     case GDK_MOTION_NOTIFY:
330         if ( dragging
331              && ( event->motion.state & GDK_BUTTON1_MASK )  && !event_context->space_panning)
332         {
333             if ( event_context->within_tolerance
334                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
335                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
336                 break; // do not drag if we're within tolerance from origin
337             }
338             // Once the user has moved farther than tolerance from the original location
339             // (indicating they intend to draw, not click), then always process the
340             // motion notify coordinates as given (no snapping back to origin)
341             event_context->within_tolerance = false;
343             NR::Point const motion_w(event->motion.x,
344                                      event->motion.y);
345             NR::Point motion_dt(desktop->w2d(motion_w));
347             SnapManager const &m = desktop->namedview->snap_manager;
348             motion_dt = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, motion_dt, bc->item).getPoint();
350             bc->ctrl_dragged  = event->motion.state & GDK_CONTROL_MASK;
352             if (event->motion.state & GDK_SHIFT_MASK && !bc->extruded && bc->item) {
353                 // once shift is pressed, set bc->extruded
354                 bc->extruded = true;
355             }
357             if (!bc->extruded) {
358                 bc->drag_ptB = motion_dt;
359                 bc->drag_ptC = motion_dt;
361                 bc->drag_ptB_proj = cur_persp->tmat.preimage (motion_dt, 0, Proj::Z);
362                 bc->drag_ptC_proj = bc->drag_ptB_proj;
363                 bc->drag_ptC_proj.normalize();
364                 bc->drag_ptC_proj[Proj::Z] = 0.25;
365             } else {
366                 // Without Ctrl, motion of the extruded corner is constrained to the
367                 // perspective line from drag_ptB to vanishing point Y.
368                 if (!bc->ctrl_dragged) {
369                     /* snapping */
370                     Box3D::PerspectiveLine pline (bc->drag_ptB, Proj::Z, SP_ACTIVE_DOCUMENT->current_persp3d);
371                     bc->drag_ptC = pline.closest_to (motion_dt);
373                     bc->drag_ptB_proj.normalize();
374                     bc->drag_ptC_proj = cur_persp->tmat.preimage (bc->drag_ptC, bc->drag_ptB_proj[Proj::X], Proj::X);
375                 } else {
376                     bc->drag_ptC = motion_dt;
378                     bc->drag_ptB_proj.normalize();
379                     bc->drag_ptC_proj = cur_persp->tmat.preimage (motion_dt, bc->drag_ptB_proj[Proj::X], Proj::X);
380                 }
381                 bc->drag_ptC = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, bc->drag_ptC, bc->item).getPoint();
382                 if (bc->ctrl_dragged) {
383                     g_print ("TODO: What should happen here?\n");
384                     // Update bc->drag_ptB in case we are ctrl-dragging
385                     /***
386                     Box3D::PerspectiveLine pl1 (NR::Point (event_context->xp, event_context->yp), Box3D::Y, bc->_vpdrag->document->current_perspective);
387                     Box3D::PerspectiveLine pl2 (bc->drag_ptB, Box3D::X, bc->_vpdrag->document->current_perspective);
388                     NR::Point corner1 = pl1.meet(pl2);
389                         
390                     Box3D::PerspectiveLine pl3 (corner1, Box3D::X, bc->_vpdrag->document->current_perspective);
391                     Box3D::PerspectiveLine pl4 (bc->drag_ptC, Box3D::Z, bc->_vpdrag->document->current_perspective);
392                     bc->drag_ptB = pl3.meet(pl4);
393                     ***/
394                 }
395             }
397             sp_box3d_drag(*bc, event->motion.state);
399             ret = TRUE;
400         }
401         break;
402     case GDK_BUTTON_RELEASE:
403         event_context->xp = event_context->yp = 0;
404         if ( event->button.button == 1  && !event_context->space_panning) {
405             dragging = false;
407             if (!event_context->within_tolerance) {
408                 // we've been dragging, finish the box
409                 sp_box3d_finish(bc);
410             } else if (event_context->item_to_select) {
411                 // no dragging, select clicked item if any
412                 if (event->button.state & GDK_SHIFT_MASK) {
413                     selection->toggle(event_context->item_to_select);
414                 } else {
415                     selection->set(event_context->item_to_select);
416                 }
417             } else {
418                 // click in an empty space
419                 selection->clear();
420             }
422             event_context->item_to_select = NULL;
423             ret = TRUE;
424             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
425                                   event->button.time);
426         }
427         break;
428     case GDK_KEY_PRESS:
429         switch (get_group0_keyval (&event->key)) {
430         case GDK_Alt_L:
431         case GDK_Alt_R:
432         case GDK_Control_L:
433         case GDK_Control_R:
434         case GDK_Shift_L:
435         case GDK_Shift_R:
436         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
437         case GDK_Meta_R:
438             /***
439             if (!dragging){
440                 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
441                                             _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
442                                             _("<b>Shift</b>: draw around the starting point"),
443                                             NULL);
444             }
445             ***/
446             break;
447         case GDK_Up:
448         case GDK_Down:
449         case GDK_KP_Up:
450         case GDK_KP_Down:
451             // prevent the zoom field from activation
452             if (!MOD__CTRL_ONLY)
453                 ret = TRUE;
454             break;
456         case GDK_bracketright:
457             persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::X, -180/snaps, MOD__ALT);
458             sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
459                              _("Change perspective (angle of PLs)"));
460             ret = true;
461             break;
463         case GDK_bracketleft:
464             persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::X, 180/snaps, MOD__ALT);
465             sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
466                              _("Change perspective (angle of PLs)"));
467             ret = true;
468             break;
470         case GDK_parenright:
471             persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Y, -180/snaps, MOD__ALT);
472             sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
473                              _("Change perspective (angle of PLs)"));
474             ret = true;
475             break;
477         case GDK_parenleft:
478             persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Y, 180/snaps, MOD__ALT);
479             sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
480                              _("Change perspective (angle of PLs)"));
481             ret = true;
482             break;
484         case GDK_braceright:
485             persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Z, -180/snaps, MOD__ALT);
486             sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
487                              _("Change perspective (angle of PLs)"));
488             ret = true;
489             break;
491         case GDK_braceleft:
492             persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Z, 180/snaps, MOD__ALT);
493             sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
494                              _("Change perspective (angle of PLs)"));
495             ret = true;
496             break;
498         case GDK_O:
499             if (MOD__CTRL && MOD__SHIFT) {
500                 Box3D::create_canvas_point(persp3d_get_VP(inkscape_active_document()->current_persp3d, Proj::W).affine(),
501                                            6, 0xff00ff00);
502             }
503             ret = true;
504             break;
506         case GDK_I:
507             if (MOD__ALT) {
508                 persp3d_print_debugging_info_all (inkscape_active_document());
509             } else {
510                 persp3d_print_debugging_info (inkscape_active_document()->current_persp3d);
511             }
512             ret = true;
513             break;
515         case GDK_P:
516         {
517             if (MOD__SHIFT && MOD__CTRL) break; // Don't catch Shift+Ctrl+P (Preferences dialog)
518             SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(SP_ACTIVE_DOCUMENT);
519             g_print ("=== Persp3D Objects: ==============================\n");
520             for (SPObject *i = sp_object_first_child(SP_OBJECT(defs)); i != NULL; i = SP_OBJECT_NEXT(i) ) {
521                 g_print ("Object encountered\n");
522                 if (SP_IS_PERSP3D(i)) {
523                     //g_print ("Encountered a Persp3D in defs\n");
524                     SP_PERSP3D(i)->tmat.print();
525                     g_print ("\n");
526                     g_print ("Computing preimage of point (300, 400)\n");
527                     SP_PERSP3D(i)->tmat.preimage (NR::Point (300, 400), 0, Proj::Z);
528                     g_print ("Computing preimage of point (200, 500)\n");
529                     SP_PERSP3D(i)->tmat.preimage (NR::Point (200, 500), 0, Proj::Z);
530                 }
531             }
532             g_print ("===================================================\n");
534             ret = true;
535             break;
536         }
538         case GDK_V:
539             if (bc->_vpdrag) {
540                 bc->_vpdrag->printDraggers();
541                 ret = true;
542             } else {
543                 g_print ("No VPDrag in Box3DContext.\n");
544             }
545             break;
547         case GDK_x:
548             if (MOD__ALT_ONLY) {
549                 desktop->setToolboxFocusTo ("altx-box3d");
550                 ret = TRUE;
551             }
552             break;
553         case GDK_X:
554             if (MOD__CTRL) break; // Don't catch Ctrl+X ('cut') and Ctrl+Shift+X ('open XML editor')
555             persp3d_toggle_VPs(persp3d_currently_selected_persps(event_context), Proj::X);
556             bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
557             ret = true;
558             break;
560         case GDK_Y:
561         {
562             if (MOD__CTRL) break; // Don't catch Ctrl+Y ("redo")
563             persp3d_toggle_VPs(persp3d_currently_selected_persps(event_context), Proj::Y);
564             bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
565             ret = true;
566             break;
567         }
569         case GDK_Z:
570         {
571             if (MOD__CTRL) break; // Don't catch Ctrl+Z ("undo")
572             persp3d_toggle_VPs(persp3d_currently_selected_persps(event_context), Proj::Z);
573             bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
574             ret = true;
575             break;
576         }
578         case GDK_Escape:
579             sp_desktop_selection(desktop)->clear();
580             //TODO: make dragging escapable by Esc
581             break;
583         case GDK_space:
584             if (dragging) {
585                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
586                                       event->button.time);
587                 dragging = false;
588                 if (!event_context->within_tolerance) {
589                     // we've been dragging, finish the box
590                     sp_box3d_finish(bc);
591                 }
592                 // do not return true, so that space would work switching to selector
593             }
594             break;
596         default:
597             break;
598         }
599         break;
600     case GDK_KEY_RELEASE:
601         switch (get_group0_keyval (&event->key)) {
602         case GDK_Alt_L:
603         case GDK_Alt_R:
604         case GDK_Control_L:
605         case GDK_Control_R:
606         case GDK_Shift_L:
607         case GDK_Shift_R:
608         case GDK_Meta_L:  // Meta is when you press Shift+Alt
609         case GDK_Meta_R:
610             event_context->defaultMessageContext()->clear();
611             break;
612         default:
613             break;
614         }
615         break;
616     default:
617         break;
618     }
620     if (!ret) {
621         if (((SPEventContextClass *) parent_class)->root_handler) {
622             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
623         }
624     }
626     return ret;
629 static void sp_box3d_drag(Box3DContext &bc, guint /*state*/)
631     SPDesktop *desktop = SP_EVENT_CONTEXT(&bc)->desktop;
633     if (!bc.item) {
635         if (Inkscape::have_viable_layer(desktop, bc._message_context) == false) {
636             return;
637         }
639         /* Create object */
640         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&bc));
641         Inkscape::XML::Node *repr = xml_doc->createElement("svg:g");
642         repr->setAttribute("sodipodi:type", "inkscape:box3d");
644         /* Set style */
645         sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.3dbox", false);
647         bc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
648         Inkscape::GC::release(repr);
649         /**** bc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer()); ****/
650         Inkscape::XML::Node *repr_side;
651         // TODO: Incorporate this in box3d-side.cpp!
652         for (int i = 0; i < 6; ++i) {
653             repr_side = xml_doc->createElement("svg:path");
654             repr_side->setAttribute("sodipodi:type", "inkscape:box3dside");
655             repr->addChild(repr_side, NULL);
657             Box3DSide *side = SP_BOX3D_SIDE(inkscape_active_document()->getObjectByRepr (repr_side));
659             guint desc = Box3D::int_to_face(i);
661             Box3D::Axis plane = (Box3D::Axis) (desc & 0x7);
662             plane = (Box3D::is_plane(plane) ? plane : Box3D::orth_plane_or_axis(plane));
663             side->dir1 = Box3D::extract_first_axis_direction(plane);
664             side->dir2 = Box3D::extract_second_axis_direction(plane);
665             side->front_or_rear = (Box3D::FrontOrRear) (desc & 0x8);
667             /* Set style */
668             box3d_side_apply_style(side);
670             SP_OBJECT(side)->updateRepr(); // calls box3d_side_write() and updates, e.g., the axes string description
671         }
673         box3d_set_z_orders(SP_BOX3D(bc.item));
674         bc.item->updateRepr();
676         // TODO: It would be nice to show the VPs during dragging, but since there is no selection
677         //       at this point (only after finishing the box), we must do this "manually"
678         /**** bc._vpdrag->updateDraggers(); ****/
680         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
681     }
683     g_assert(bc.item);
685     SPBox3D *box = SP_BOX3D(bc.item);
687     box->orig_corner0 = bc.drag_origin_proj;
688     box->orig_corner7 = bc.drag_ptC_proj;
690     box3d_check_for_swapped_coords(box);
692     /* we need to call this from here (instead of from box3d_position_set(), for example)
693        because z-order setting must not interfere with display updates during undo/redo */
694     box3d_set_z_orders (box);
696     box3d_position_set(box);
698     // status text
699     //GString *Ax = SP_PX_TO_METRIC_STRING(origin[NR::X], desktop->namedview->getDefaultMetric());
700     //GString *Ay = SP_PX_TO_METRIC_STRING(origin[NR::Y], desktop->namedview->getDefaultMetric());
701     bc._message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>3D Box</b>; with <b>Shift</b> to extrude along the Z axis"));
702     //g_string_free(Ax, FALSE);
703     //g_string_free(Ay, FALSE);
706 static void sp_box3d_finish(Box3DContext *bc)
708     bc->_message_context->clear();
709     g_assert (SP_ACTIVE_DOCUMENT->current_persp3d);
710     //Persp3D *cur_persp = SP_ACTIVE_DOCUMENT->current_persp3d;
712     if ( bc->item != NULL ) {
713         SPDesktop * desktop = SP_EVENT_CONTEXT_DESKTOP(bc);
715         SPBox3D *box = SP_BOX3D(bc->item);
717         box->orig_corner0 = bc->drag_origin_proj;
718         box->orig_corner7 = bc->drag_ptC_proj;
720         box->updateRepr();
722         box3d_relabel_corners(box);
724         sp_canvas_end_forced_full_redraws(desktop->canvas);
726         sp_desktop_selection(desktop)->set(bc->item);
727         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
728                          _("Create 3D box"));
730         bc->item = NULL;
731     }
733     bc->ctrl_dragged = false;
734     bc->extruded = false;
737 /*
738   Local Variables:
739   mode:c++
740   c-file-style:"stroustrup"
741   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
742   indent-tabs-mode:nil
743   fill-column:99
744   End:
745 */
746 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :