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;
119 box3d_context->_vpdrag = NULL;
121 new (&box3d_context->sel_changed_connection) sigc::connection();
122 }
124 static void sp_box3d_context_dispose(GObject *object)
125 {
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);
158 }
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)
173 {
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 }
207 }
209 /* create a default perspective in document defs if none is present
210 (can happen after 'vacuum defs' or when a pre-0.46 file is opened) */
211 static void sp_box3d_context_check_for_persp_in_defs(SPDocument *document) {
212 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
214 bool has_persp = false;
215 for (SPObject *child = sp_object_first_child(defs); child != NULL; child = SP_OBJECT_NEXT(child) ) {
216 if (SP_IS_PERSP3D(child)) {
217 has_persp = true;
218 break;
219 }
220 }
222 if (!has_persp) {
223 document->current_persp3d = persp3d_create_xml_element (document);
224 }
225 }
227 static void sp_box3d_context_setup(SPEventContext *ec)
228 {
229 Box3DContext *bc = SP_BOX3D_CONTEXT(ec);
231 if (((SPEventContextClass *) parent_class)->setup) {
232 ((SPEventContextClass *) parent_class)->setup(ec);
233 }
235 sp_box3d_context_check_for_persp_in_defs(sp_desktop_document (ec->desktop));
237 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
238 if (item) {
239 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
240 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
241 if (shape_repr) {
242 ec->shape_repr = shape_repr;
243 Inkscape::GC::anchor(shape_repr);
244 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
245 }
246 }
248 bc->sel_changed_connection.disconnect();
249 bc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
250 sigc::bind(sigc::ptr_fun(&sp_box3d_context_selection_changed), (gpointer)bc)
251 );
253 bc->_vpdrag = new Box3D::VPDrag(sp_desktop_document (ec->desktop));
255 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
256 ec->enableSelectionCue();
257 }
259 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
260 ec->enableGrDrag();
261 }
263 bc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
264 }
266 static gint sp_box3d_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
267 {
268 SPDesktop *desktop = event_context->desktop;
270 gint ret = FALSE;
272 switch (event->type) {
273 case GDK_BUTTON_PRESS:
274 if ( event->button.button == 1 && !event_context->space_panning) {
275 Inkscape::setup_for_drag_start(desktop, event_context, event);
276 ret = TRUE;
277 }
278 break;
279 // motion and release are always on root (why?)
280 default:
281 break;
282 }
284 if (((SPEventContextClass *) parent_class)->item_handler) {
285 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
286 }
288 return ret;
289 }
291 static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEvent *event)
292 {
293 static bool dragging;
295 SPDesktop *desktop = event_context->desktop;
296 Inkscape::Selection *selection = sp_desktop_selection (desktop);
297 int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
299 Box3DContext *bc = SP_BOX3D_CONTEXT(event_context);
300 g_assert (SP_ACTIVE_DOCUMENT->current_persp3d);
301 Persp3D *cur_persp = SP_ACTIVE_DOCUMENT->current_persp3d;
303 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
305 gint ret = FALSE;
306 switch (event->type) {
307 case GDK_BUTTON_PRESS:
308 if ( event->button.button == 1 && !event_context->space_panning) {
309 NR::Point const button_w(event->button.x,
310 event->button.y);
312 // save drag origin
313 event_context->xp = (gint) button_w[NR::X];
314 event_context->yp = (gint) button_w[NR::Y];
315 event_context->within_tolerance = true;
317 // remember clicked item, *not* disregarding groups (since a 3D box is a group), honoring Alt
318 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);
320 dragging = true;
322 /* */
323 NR::Point const button_dt(desktop->w2d(button_w));
324 bc->drag_origin = button_dt;
325 bc->drag_ptB = button_dt;
326 bc->drag_ptC = button_dt;
328 /* Projective preimages of clicked point under current perspective */
329 bc->drag_origin_proj = cur_persp->tmat.preimage (button_dt, 0, Proj::Z);
330 bc->drag_ptB_proj = bc->drag_origin_proj;
331 bc->drag_ptC_proj = bc->drag_origin_proj;
332 bc->drag_ptC_proj.normalize();
333 bc->drag_ptC_proj[Proj::Z] = 0.25;
335 /* Snap center */
336 SnapManager const &m = desktop->namedview->snap_manager;
337 bc->center = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE,
338 button_dt, bc->item).getPoint();
340 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
341 ( GDK_KEY_PRESS_MASK |
342 GDK_BUTTON_RELEASE_MASK |
343 GDK_POINTER_MOTION_MASK |
344 GDK_BUTTON_PRESS_MASK ),
345 NULL, event->button.time);
346 ret = TRUE;
347 }
348 break;
349 case GDK_MOTION_NOTIFY:
350 if ( dragging
351 && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning)
352 {
353 if ( event_context->within_tolerance
354 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
355 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
356 break; // do not drag if we're within tolerance from origin
357 }
358 // Once the user has moved farther than tolerance from the original location
359 // (indicating they intend to draw, not click), then always process the
360 // motion notify coordinates as given (no snapping back to origin)
361 event_context->within_tolerance = false;
363 NR::Point const motion_w(event->motion.x,
364 event->motion.y);
365 NR::Point motion_dt(desktop->w2d(motion_w));
367 SnapManager const &m = desktop->namedview->snap_manager;
368 motion_dt = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, motion_dt, bc->item).getPoint();
370 bc->ctrl_dragged = event->motion.state & GDK_CONTROL_MASK;
372 if (event->motion.state & GDK_SHIFT_MASK && !bc->extruded && bc->item) {
373 // once shift is pressed, set bc->extruded
374 bc->extruded = true;
375 }
377 if (!bc->extruded) {
378 bc->drag_ptB = motion_dt;
379 bc->drag_ptC = motion_dt;
381 bc->drag_ptB_proj = cur_persp->tmat.preimage (motion_dt, 0, Proj::Z);
382 bc->drag_ptC_proj = bc->drag_ptB_proj;
383 bc->drag_ptC_proj.normalize();
384 bc->drag_ptC_proj[Proj::Z] = 0.25;
385 } else {
386 // Without Ctrl, motion of the extruded corner is constrained to the
387 // perspective line from drag_ptB to vanishing point Y.
388 if (!bc->ctrl_dragged) {
389 /* snapping */
390 Box3D::PerspectiveLine pline (bc->drag_ptB, Proj::Z, SP_ACTIVE_DOCUMENT->current_persp3d);
391 bc->drag_ptC = pline.closest_to (motion_dt);
393 bc->drag_ptB_proj.normalize();
394 bc->drag_ptC_proj = cur_persp->tmat.preimage (bc->drag_ptC, bc->drag_ptB_proj[Proj::X], Proj::X);
395 } else {
396 bc->drag_ptC = motion_dt;
398 bc->drag_ptB_proj.normalize();
399 bc->drag_ptC_proj = cur_persp->tmat.preimage (motion_dt, bc->drag_ptB_proj[Proj::X], Proj::X);
400 }
401 bc->drag_ptC = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, bc->drag_ptC, bc->item).getPoint();
402 if (bc->ctrl_dragged) {
403 g_print ("TODO: What should happen here?\n");
404 // Update bc->drag_ptB in case we are ctrl-dragging
405 /***
406 Box3D::PerspectiveLine pl1 (NR::Point (event_context->xp, event_context->yp), Box3D::Y, bc->_vpdrag->document->current_perspective);
407 Box3D::PerspectiveLine pl2 (bc->drag_ptB, Box3D::X, bc->_vpdrag->document->current_perspective);
408 NR::Point corner1 = pl1.meet(pl2);
410 Box3D::PerspectiveLine pl3 (corner1, Box3D::X, bc->_vpdrag->document->current_perspective);
411 Box3D::PerspectiveLine pl4 (bc->drag_ptC, Box3D::Z, bc->_vpdrag->document->current_perspective);
412 bc->drag_ptB = pl3.meet(pl4);
413 ***/
414 }
415 }
417 sp_box3d_drag(*bc, event->motion.state);
419 ret = TRUE;
420 }
421 break;
422 case GDK_BUTTON_RELEASE:
423 event_context->xp = event_context->yp = 0;
424 if ( event->button.button == 1 && !event_context->space_panning) {
425 dragging = false;
427 if (!event_context->within_tolerance) {
428 // we've been dragging, finish the box
429 sp_box3d_finish(bc);
430 } else if (event_context->item_to_select) {
431 // no dragging, select clicked item if any
432 if (event->button.state & GDK_SHIFT_MASK) {
433 selection->toggle(event_context->item_to_select);
434 } else {
435 selection->set(event_context->item_to_select);
436 }
437 } else {
438 // click in an empty space
439 selection->clear();
440 }
442 event_context->item_to_select = NULL;
443 ret = TRUE;
444 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
445 event->button.time);
446 }
447 break;
448 case GDK_KEY_PRESS:
449 switch (get_group0_keyval (&event->key)) {
450 case GDK_Alt_L:
451 case GDK_Alt_R:
452 case GDK_Control_L:
453 case GDK_Control_R:
454 case GDK_Shift_L:
455 case GDK_Shift_R:
456 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
457 case GDK_Meta_R:
458 /***
459 if (!dragging){
460 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
461 _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
462 _("<b>Shift</b>: draw around the starting point"),
463 NULL);
464 }
465 ***/
466 break;
467 case GDK_Up:
468 case GDK_Down:
469 case GDK_KP_Up:
470 case GDK_KP_Down:
471 // prevent the zoom field from activation
472 if (!MOD__CTRL_ONLY)
473 ret = TRUE;
474 break;
476 case GDK_bracketright:
477 persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::X, -180/snaps, MOD__ALT);
478 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
479 _("Change perspective (angle of PLs)"));
480 ret = true;
481 break;
483 case GDK_bracketleft:
484 persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::X, 180/snaps, MOD__ALT);
485 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
486 _("Change perspective (angle of PLs)"));
487 ret = true;
488 break;
490 case GDK_parenright:
491 persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Y, -180/snaps, MOD__ALT);
492 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
493 _("Change perspective (angle of PLs)"));
494 ret = true;
495 break;
497 case GDK_parenleft:
498 persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Y, 180/snaps, MOD__ALT);
499 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
500 _("Change perspective (angle of PLs)"));
501 ret = true;
502 break;
504 case GDK_braceright:
505 persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Z, -180/snaps, MOD__ALT);
506 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
507 _("Change perspective (angle of PLs)"));
508 ret = true;
509 break;
511 case GDK_braceleft:
512 persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Z, 180/snaps, MOD__ALT);
513 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
514 _("Change perspective (angle of PLs)"));
515 ret = true;
516 break;
518 case GDK_O:
519 if (MOD__CTRL && MOD__SHIFT) {
520 Box3D::create_canvas_point(persp3d_get_VP(inkscape_active_document()->current_persp3d, Proj::W).affine(),
521 6, 0xff00ff00);
522 }
523 ret = true;
524 break;
526 case GDK_I:
527 if (MOD__ALT) {
528 persp3d_print_debugging_info_all (inkscape_active_document());
529 } else {
530 persp3d_print_debugging_info (inkscape_active_document()->current_persp3d);
531 }
532 ret = true;
533 break;
535 case GDK_P:
536 {
537 if (MOD__SHIFT && MOD__CTRL) break; // Don't catch Shift+Ctrl+P (Preferences dialog)
538 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(SP_ACTIVE_DOCUMENT);
539 g_print ("=== Persp3D Objects: ==============================\n");
540 for (SPObject *i = sp_object_first_child(SP_OBJECT(defs)); i != NULL; i = SP_OBJECT_NEXT(i) ) {
541 g_print ("Object encountered\n");
542 if (SP_IS_PERSP3D(i)) {
543 //g_print ("Encountered a Persp3D in defs\n");
544 SP_PERSP3D(i)->tmat.print();
545 g_print ("\n");
546 g_print ("Computing preimage of point (300, 400)\n");
547 SP_PERSP3D(i)->tmat.preimage (NR::Point (300, 400), 0, Proj::Z);
548 g_print ("Computing preimage of point (200, 500)\n");
549 SP_PERSP3D(i)->tmat.preimage (NR::Point (200, 500), 0, Proj::Z);
550 }
551 }
552 g_print ("===================================================\n");
554 ret = true;
555 break;
556 }
558 case GDK_V:
559 if (bc->_vpdrag) {
560 bc->_vpdrag->printDraggers();
561 ret = true;
562 } else {
563 g_print ("No VPDrag in Box3DContext.\n");
564 }
565 break;
567 case GDK_x:
568 if (MOD__ALT_ONLY) {
569 desktop->setToolboxFocusTo ("altx-box3d");
570 ret = TRUE;
571 }
572 break;
573 case GDK_X:
574 if (MOD__CTRL) break; // Don't catch Ctrl+X ('cut') and Ctrl+Shift+X ('open XML editor')
575 persp3d_toggle_VPs(persp3d_currently_selected_persps(event_context), Proj::X);
576 bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
577 ret = true;
578 break;
580 case GDK_Y:
581 {
582 if (MOD__CTRL) break; // Don't catch Ctrl+Y ("redo")
583 persp3d_toggle_VPs(persp3d_currently_selected_persps(event_context), Proj::Y);
584 bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
585 ret = true;
586 break;
587 }
589 case GDK_Z:
590 {
591 if (MOD__CTRL) break; // Don't catch Ctrl+Z ("undo")
592 persp3d_toggle_VPs(persp3d_currently_selected_persps(event_context), Proj::Z);
593 bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
594 ret = true;
595 break;
596 }
598 case GDK_Escape:
599 sp_desktop_selection(desktop)->clear();
600 //TODO: make dragging escapable by Esc
601 break;
603 case GDK_space:
604 if (dragging) {
605 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
606 event->button.time);
607 dragging = false;
608 if (!event_context->within_tolerance) {
609 // we've been dragging, finish the box
610 sp_box3d_finish(bc);
611 }
612 // do not return true, so that space would work switching to selector
613 }
614 break;
616 default:
617 break;
618 }
619 break;
620 case GDK_KEY_RELEASE:
621 switch (get_group0_keyval (&event->key)) {
622 case GDK_Alt_L:
623 case GDK_Alt_R:
624 case GDK_Control_L:
625 case GDK_Control_R:
626 case GDK_Shift_L:
627 case GDK_Shift_R:
628 case GDK_Meta_L: // Meta is when you press Shift+Alt
629 case GDK_Meta_R:
630 event_context->defaultMessageContext()->clear();
631 break;
632 default:
633 break;
634 }
635 break;
636 default:
637 break;
638 }
640 if (!ret) {
641 if (((SPEventContextClass *) parent_class)->root_handler) {
642 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
643 }
644 }
646 return ret;
647 }
649 static void sp_box3d_drag(Box3DContext &bc, guint /*state*/)
650 {
651 SPDesktop *desktop = SP_EVENT_CONTEXT(&bc)->desktop;
653 if (!bc.item) {
655 if (Inkscape::have_viable_layer(desktop, bc._message_context) == false) {
656 return;
657 }
659 /* Create object */
660 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&bc));
661 Inkscape::XML::Node *repr = xml_doc->createElement("svg:g");
662 repr->setAttribute("sodipodi:type", "inkscape:box3d");
664 /* Set style */
665 sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.3dbox", false);
667 bc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
668 Inkscape::GC::release(repr);
669 /**** bc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer()); ****/
670 Inkscape::XML::Node *repr_side;
671 // TODO: Incorporate this in box3d-side.cpp!
672 for (int i = 0; i < 6; ++i) {
673 repr_side = xml_doc->createElement("svg:path");
674 repr_side->setAttribute("sodipodi:type", "inkscape:box3dside");
675 repr->addChild(repr_side, NULL);
677 Box3DSide *side = SP_BOX3D_SIDE(inkscape_active_document()->getObjectByRepr (repr_side));
679 guint desc = Box3D::int_to_face(i);
681 Box3D::Axis plane = (Box3D::Axis) (desc & 0x7);
682 plane = (Box3D::is_plane(plane) ? plane : Box3D::orth_plane_or_axis(plane));
683 side->dir1 = Box3D::extract_first_axis_direction(plane);
684 side->dir2 = Box3D::extract_second_axis_direction(plane);
685 side->front_or_rear = (Box3D::FrontOrRear) (desc & 0x8);
687 /* Set style */
688 box3d_side_apply_style(side);
690 SP_OBJECT(side)->updateRepr(); // calls box3d_side_write() and updates, e.g., the axes string description
691 }
693 box3d_set_z_orders(SP_BOX3D(bc.item));
694 bc.item->updateRepr();
696 // TODO: It would be nice to show the VPs during dragging, but since there is no selection
697 // at this point (only after finishing the box), we must do this "manually"
698 /**** bc._vpdrag->updateDraggers(); ****/
700 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
701 }
703 g_assert(bc.item);
705 SPBox3D *box = SP_BOX3D(bc.item);
707 box->orig_corner0 = bc.drag_origin_proj;
708 box->orig_corner7 = bc.drag_ptC_proj;
710 box3d_check_for_swapped_coords(box);
712 /* we need to call this from here (instead of from box3d_position_set(), for example)
713 because z-order setting must not interfere with display updates during undo/redo */
714 box3d_set_z_orders (box);
716 box3d_position_set(box);
718 // status text
719 //GString *Ax = SP_PX_TO_METRIC_STRING(origin[NR::X], desktop->namedview->getDefaultMetric());
720 //GString *Ay = SP_PX_TO_METRIC_STRING(origin[NR::Y], desktop->namedview->getDefaultMetric());
721 bc._message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>3D Box</b>; with <b>Shift</b> to extrude along the Z axis"));
722 //g_string_free(Ax, FALSE);
723 //g_string_free(Ay, FALSE);
724 }
726 static void sp_box3d_finish(Box3DContext *bc)
727 {
728 bc->_message_context->clear();
729 g_assert (SP_ACTIVE_DOCUMENT->current_persp3d);
730 //Persp3D *cur_persp = SP_ACTIVE_DOCUMENT->current_persp3d;
732 if ( bc->item != NULL ) {
733 SPDesktop * desktop = SP_EVENT_CONTEXT_DESKTOP(bc);
735 SPBox3D *box = SP_BOX3D(bc->item);
737 box->orig_corner0 = bc->drag_origin_proj;
738 box->orig_corner7 = bc->drag_ptC_proj;
740 box->updateRepr();
742 box3d_relabel_corners(box);
744 sp_canvas_end_forced_full_redraws(desktop->canvas);
746 sp_desktop_selection(desktop)->set(bc->item);
747 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
748 _("Create 3D box"));
750 bc->item = NULL;
751 }
753 bc->ctrl_dragged = false;
754 bc->extruded = false;
755 }
757 /*
758 Local Variables:
759 mode:c++
760 c-file-style:"stroustrup"
761 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
762 indent-tabs-mode:nil
763 fill-column:99
764 End:
765 */
766 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :