~valavanisalex/ubuntu/precise/inkscape/fix-943984

« back to all changes in this revision

Viewing changes to inkscape-0.47pre1/src/box3d-context.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2009-07-02 17:09:45 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20090702170945-nn6d6zswovbwju1t
Tags: 0.47~pre1-0ubuntu1
* New upstream release.
  - Don't constrain maximization on small resolution devices (pre0)
    (LP: #348842)
  - Fixes segfault on startup (pre0)
    (LP: #391149)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define __SP_BOX3D_CONTEXT_C__
 
2
 
 
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
 */
 
17
 
 
18
#include "config.h"
 
19
 
 
20
#include <gdk/gdkkeysyms.h>
 
21
 
 
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 "selection-chemistry.h"
 
28
#include "desktop-handles.h"
 
29
#include "snap.h"
 
30
#include "display/curve.h"
 
31
#include "desktop.h"
 
32
#include "message-context.h"
 
33
#include "pixmaps/cursor-3dbox.xpm"
 
34
#include "box3d.h"
 
35
#include "box3d-context.h"
 
36
#include "sp-metrics.h"
 
37
#include <glibmm/i18n.h>
 
38
#include "object-edit.h"
 
39
#include "xml/repr.h"
 
40
#include "xml/node-event-vector.h"
 
41
#include "preferences.h"
 
42
#include "context-fns.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"
 
49
#include "line-geometry.h"
 
50
#include "shape-editor.h"
 
51
 
 
52
static void sp_box3d_context_class_init(Box3DContextClass *klass);
 
53
static void sp_box3d_context_init(Box3DContext *box3d_context);
 
54
static void sp_box3d_context_dispose(GObject *object);
 
55
 
 
56
static void sp_box3d_context_setup(SPEventContext *ec);
 
57
 
 
58
static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEvent *event);
 
59
static gint sp_box3d_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
 
60
 
 
61
static void sp_box3d_drag(Box3DContext &bc, guint state);
 
62
static void sp_box3d_finish(Box3DContext *bc);
 
63
 
 
64
static SPEventContextClass *parent_class;
 
65
 
 
66
GtkType sp_box3d_context_get_type()
 
67
{
 
68
    static GType type = 0;
 
69
    if (!type) {
 
70
        GTypeInfo info = {
 
71
            sizeof(Box3DContextClass),
 
72
            NULL, NULL,
 
73
            (GClassInitFunc) sp_box3d_context_class_init,
 
74
            NULL, NULL,
 
75
            sizeof(Box3DContext),
 
76
            4,
 
77
            (GInstanceInitFunc) sp_box3d_context_init,
 
78
            NULL,    /* value_table */
 
79
        };
 
80
        type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "Box3DContext", &info, (GTypeFlags) 0);
 
81
    }
 
82
    return type;
 
83
}
 
84
 
 
85
static void sp_box3d_context_class_init(Box3DContextClass *klass)
 
86
{
 
87
    GObjectClass *object_class = (GObjectClass *) klass;
 
88
    SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
 
89
 
 
90
    parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
 
91
 
 
92
    object_class->dispose = sp_box3d_context_dispose;
 
93
 
 
94
    event_context_class->setup = sp_box3d_context_setup;
 
95
    event_context_class->root_handler  = sp_box3d_context_root_handler;
 
96
    event_context_class->item_handler  = sp_box3d_context_item_handler;
 
97
}
 
98
 
 
99
static void sp_box3d_context_init(Box3DContext *box3d_context)
 
100
{
 
101
    SPEventContext *event_context = SP_EVENT_CONTEXT(box3d_context);
 
102
 
 
103
    event_context->cursor_shape = cursor_3dbox_xpm;
 
104
    event_context->hot_x = 4;
 
105
    event_context->hot_y = 4;
 
106
    event_context->xp = 0;
 
107
    event_context->yp = 0;
 
108
    event_context->tolerance = 0;
 
109
    event_context->within_tolerance = false;
 
110
    event_context->item_to_select = NULL;
 
111
 
 
112
    box3d_context->item = NULL;
 
113
 
 
114
    box3d_context->ctrl_dragged = false;
 
115
    box3d_context->extruded = false;
 
116
 
 
117
    box3d_context->_vpdrag = NULL;
 
118
 
 
119
    new (&box3d_context->sel_changed_connection) sigc::connection();
 
120
}
 
121
 
 
122
static void sp_box3d_context_dispose(GObject *object)
 
123
{
 
124
    Box3DContext *bc = SP_BOX3D_CONTEXT(object);
 
125
    SPEventContext *ec = SP_EVENT_CONTEXT(object);
 
126
 
 
127
    ec->enableGrDrag(false);
 
128
 
 
129
    delete (bc->_vpdrag);
 
130
    bc->_vpdrag = NULL;
 
131
 
 
132
    bc->sel_changed_connection.disconnect();
 
133
    bc->sel_changed_connection.~connection();
 
134
 
 
135
    delete ec->shape_editor;
 
136
    ec->shape_editor = NULL;
 
137
 
 
138
    /* fixme: This is necessary because we do not grab */
 
139
    if (bc->item) {
 
140
        sp_box3d_finish(bc);
 
141
    }
 
142
 
 
143
    if (bc->_message_context) {
 
144
        delete bc->_message_context;
 
145
    }
 
146
 
 
147
    G_OBJECT_CLASS(parent_class)->dispose(object);
 
148
}
 
149
 
 
150
/**
 
151
\brief  Callback that processes the "changed" signal on the selection;
 
152
destroys old and creates new knotholder
 
153
*/
 
154
static void sp_box3d_context_selection_changed(Inkscape::Selection *selection, gpointer data)
 
155
{
 
156
    Box3DContext *bc = SP_BOX3D_CONTEXT(data);
 
157
    SPEventContext *ec = SP_EVENT_CONTEXT(bc);
 
158
 
 
159
    ec->shape_editor->unset_item(SH_KNOTHOLDER);
 
160
    SPItem *item = selection->singleItem();
 
161
    ec->shape_editor->set_item(item, SH_KNOTHOLDER);
 
162
 
 
163
    if (selection->perspList().size() == 1) {
 
164
        // selecting a single box changes the current perspective
 
165
        ec->desktop->doc()->current_persp3d = selection->perspList().front();
 
166
    }
 
167
}
 
168
 
 
169
/* create a default perspective in document defs if none is present
 
170
   (can happen after 'vacuum defs' or when a pre-0.46 file is opened) */
 
171
static void sp_box3d_context_check_for_persp_in_defs(SPDocument *document) {
 
172
    SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
 
173
 
 
174
    bool has_persp = false;
 
175
    for (SPObject *child = sp_object_first_child(defs); child != NULL; child = SP_OBJECT_NEXT(child) ) {
 
176
        if (SP_IS_PERSP3D(child)) {
 
177
            has_persp = true;
 
178
            break;
 
179
        }
 
180
    }
 
181
 
 
182
    if (!has_persp) {
 
183
        document->current_persp3d = persp3d_create_xml_element (document);
 
184
    }
 
185
}
 
186
 
 
187
static void sp_box3d_context_setup(SPEventContext *ec)
 
188
{
 
189
    Box3DContext *bc = SP_BOX3D_CONTEXT(ec);
 
190
 
 
191
    if (((SPEventContextClass *) parent_class)->setup) {
 
192
        ((SPEventContextClass *) parent_class)->setup(ec);
 
193
    }
 
194
 
 
195
    sp_box3d_context_check_for_persp_in_defs(sp_desktop_document (ec->desktop));
 
196
 
 
197
    ec->shape_editor = new ShapeEditor(ec->desktop);
 
198
 
 
199
    SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
 
200
    if (item) {
 
201
        ec->shape_editor->set_item(item, SH_KNOTHOLDER);
 
202
    }
 
203
 
 
204
    bc->sel_changed_connection.disconnect();
 
205
    bc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
 
206
        sigc::bind(sigc::ptr_fun(&sp_box3d_context_selection_changed), (gpointer)bc)
 
207
    );
 
208
 
 
209
    bc->_vpdrag = new Box3D::VPDrag(sp_desktop_document (ec->desktop));
 
210
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
211
 
 
212
    if (prefs->getBool("/tools/shapes/selcue")) {
 
213
        ec->enableSelectionCue();
 
214
    }
 
215
 
 
216
    if (prefs->getBool("/tools/shapes/gradientdrag")) {
 
217
        ec->enableGrDrag();
 
218
    }
 
219
 
 
220
    bc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
 
221
}
 
222
 
 
223
static gint sp_box3d_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
 
224
{
 
225
    SPDesktop *desktop = event_context->desktop;
 
226
 
 
227
    gint ret = FALSE;
 
228
 
 
229
    switch (event->type) {
 
230
    case GDK_BUTTON_PRESS:
 
231
        if ( event->button.button == 1 && !event_context->space_panning) {
 
232
            Inkscape::setup_for_drag_start(desktop, event_context, event);
 
233
            ret = TRUE;
 
234
        }
 
235
        break;
 
236
        // motion and release are always on root (why?)
 
237
    default:
 
238
        break;
 
239
    }
 
240
 
 
241
    if (((SPEventContextClass *) parent_class)->item_handler) {
 
242
        ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
 
243
    }
 
244
 
 
245
    return ret;
 
246
}
 
247
 
 
248
static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEvent *event)
 
249
{
 
250
    static bool dragging;
 
251
 
 
252
    SPDesktop *desktop = event_context->desktop;
 
253
    Inkscape::Selection *selection = sp_desktop_selection (desktop);
 
254
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
255
    int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
 
256
 
 
257
    Box3DContext *bc = SP_BOX3D_CONTEXT(event_context);
 
258
    g_assert (SP_ACTIVE_DOCUMENT->current_persp3d);
 
259
    Persp3D *cur_persp = SP_ACTIVE_DOCUMENT->current_persp3d;
 
260
 
 
261
    event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
 
262
 
 
263
    gint ret = FALSE;
 
264
    switch (event->type) {
 
265
    case GDK_BUTTON_PRESS:
 
266
        if ( event->button.button == 1  && !event_context->space_panning) {
 
267
            Geom::Point const button_w(event->button.x,
 
268
                                       event->button.y);
 
269
 
 
270
            // save drag origin
 
271
            event_context->xp = (gint) button_w[Geom::X];
 
272
            event_context->yp = (gint) button_w[Geom::Y];
 
273
            event_context->within_tolerance = true;
 
274
 
 
275
            // remember clicked item, *not* disregarding groups (since a 3D box is a group), honoring Alt
 
276
            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);
 
277
 
 
278
            dragging = true;
 
279
            sp_event_context_snap_window_open(event_context);
 
280
 
 
281
            /*  */
 
282
            Geom::Point button_dt(desktop->w2d(button_w));
 
283
            bc->drag_origin = from_2geom(button_dt);
 
284
            bc->drag_ptB = from_2geom(button_dt);
 
285
            bc->drag_ptC = from_2geom(button_dt);
 
286
 
 
287
            /* Projective preimages of clicked point under current perspective */
 
288
            bc->drag_origin_proj = cur_persp->tmat.preimage (from_2geom(button_dt), 0, Proj::Z);
 
289
            bc->drag_ptB_proj = bc->drag_origin_proj;
 
290
            bc->drag_ptC_proj = bc->drag_origin_proj;
 
291
            bc->drag_ptC_proj.normalize();
 
292
            bc->drag_ptC_proj[Proj::Z] = 0.25;
 
293
 
 
294
            /* Snap center */
 
295
            SnapManager &m = desktop->namedview->snap_manager;
 
296
            m.setup(desktop, true, bc->item);
 
297
            m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, button_dt, Inkscape::SNAPSOURCE_HANDLE);
 
298
            bc->center = from_2geom(button_dt);
 
299
 
 
300
            sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
 
301
                                ( GDK_KEY_PRESS_MASK |
 
302
                                  GDK_BUTTON_RELEASE_MASK       |
 
303
                                  GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK       |
 
304
                                  GDK_BUTTON_PRESS_MASK ),
 
305
                                NULL, event->button.time);
 
306
            ret = TRUE;
 
307
        }
 
308
        break;
 
309
    case GDK_MOTION_NOTIFY:
 
310
        if ( dragging
 
311
             && ( event->motion.state & GDK_BUTTON1_MASK )  && !event_context->space_panning)
 
312
        {
 
313
            if ( event_context->within_tolerance
 
314
                 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
 
315
                 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
 
316
                break; // do not drag if we're within tolerance from origin
 
317
            }
 
318
            // Once the user has moved farther than tolerance from the original location
 
319
            // (indicating they intend to draw, not click), then always process the
 
320
            // motion notify coordinates as given (no snapping back to origin)
 
321
            event_context->within_tolerance = false;
 
322
 
 
323
            Geom::Point const motion_w(event->motion.x,
 
324
                                       event->motion.y);
 
325
            Geom::Point motion_dt(desktop->w2d(motion_w));
 
326
 
 
327
            SnapManager &m = desktop->namedview->snap_manager;
 
328
            m.setup(desktop, true, bc->item);
 
329
            m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, motion_dt, Inkscape::SNAPSOURCE_HANDLE);
 
330
 
 
331
            bc->ctrl_dragged  = event->motion.state & GDK_CONTROL_MASK;
 
332
 
 
333
            if (event->motion.state & GDK_SHIFT_MASK && !bc->extruded && bc->item) {
 
334
                // once shift is pressed, set bc->extruded
 
335
                bc->extruded = true;
 
336
            }
 
337
 
 
338
            if (!bc->extruded) {
 
339
                bc->drag_ptB = from_2geom(motion_dt);
 
340
                bc->drag_ptC = from_2geom(motion_dt);
 
341
 
 
342
                bc->drag_ptB_proj = cur_persp->tmat.preimage (from_2geom(motion_dt), 0, Proj::Z);
 
343
                bc->drag_ptC_proj = bc->drag_ptB_proj;
 
344
                bc->drag_ptC_proj.normalize();
 
345
                bc->drag_ptC_proj[Proj::Z] = 0.25;
 
346
            } else {
 
347
                // Without Ctrl, motion of the extruded corner is constrained to the
 
348
                // perspective line from drag_ptB to vanishing point Y.
 
349
                if (!bc->ctrl_dragged) {
 
350
                    /* snapping */
 
351
                    Box3D::PerspectiveLine pline (bc->drag_ptB, Proj::Z, SP_ACTIVE_DOCUMENT->current_persp3d);
 
352
                    bc->drag_ptC = pline.closest_to (from_2geom(motion_dt));
 
353
 
 
354
                    bc->drag_ptB_proj.normalize();
 
355
                    bc->drag_ptC_proj = cur_persp->tmat.preimage (bc->drag_ptC, bc->drag_ptB_proj[Proj::X], Proj::X);
 
356
                } else {
 
357
                    bc->drag_ptC = from_2geom(motion_dt);
 
358
 
 
359
                    bc->drag_ptB_proj.normalize();
 
360
                    bc->drag_ptC_proj = cur_persp->tmat.preimage (from_2geom(motion_dt), bc->drag_ptB_proj[Proj::X], Proj::X);
 
361
                }
 
362
                Geom::Point pt2g = to_2geom(bc->drag_ptC);
 
363
                m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
 
364
                bc->drag_ptC = from_2geom(pt2g);
 
365
            }
 
366
 
 
367
            sp_box3d_drag(*bc, event->motion.state);
 
368
 
 
369
            ret = TRUE;
 
370
        }
 
371
        break;
 
372
    case GDK_BUTTON_RELEASE:
 
373
        event_context->xp = event_context->yp = 0;
 
374
        if ( event->button.button == 1  && !event_context->space_panning) {
 
375
            dragging = false;
 
376
            sp_event_context_snap_window_closed(event_context, false); //button release will also occur on a double-click; in that case suppress warnings
 
377
 
 
378
            if (!event_context->within_tolerance) {
 
379
                // we've been dragging, finish the box
 
380
                sp_box3d_finish(bc);
 
381
            } else if (event_context->item_to_select) {
 
382
                // no dragging, select clicked item if any
 
383
                if (event->button.state & GDK_SHIFT_MASK) {
 
384
                    selection->toggle(event_context->item_to_select);
 
385
                } else {
 
386
                    selection->set(event_context->item_to_select);
 
387
                }
 
388
            } else {
 
389
                // click in an empty space
 
390
                selection->clear();
 
391
            }
 
392
 
 
393
            event_context->item_to_select = NULL;
 
394
            ret = TRUE;
 
395
            sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
 
396
                                  event->button.time);
 
397
        }
 
398
        break;
 
399
    case GDK_KEY_PRESS:
 
400
        switch (get_group0_keyval (&event->key)) {
 
401
        case GDK_Up:
 
402
        case GDK_Down:
 
403
        case GDK_KP_Up:
 
404
        case GDK_KP_Down:
 
405
            // prevent the zoom field from activation
 
406
            if (!MOD__CTRL_ONLY)
 
407
                ret = TRUE;
 
408
            break;
 
409
 
 
410
        case GDK_bracketright:
 
411
            persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::X, -180/snaps, MOD__ALT);
 
412
            sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
 
413
                             _("Change perspective (angle of PLs)"));
 
414
            ret = true;
 
415
            break;
 
416
 
 
417
        case GDK_bracketleft:
 
418
            persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::X, 180/snaps, MOD__ALT);
 
419
            sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
 
420
                             _("Change perspective (angle of PLs)"));
 
421
            ret = true;
 
422
            break;
 
423
 
 
424
        case GDK_parenright:
 
425
            persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Y, -180/snaps, MOD__ALT);
 
426
            sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
 
427
                             _("Change perspective (angle of PLs)"));
 
428
            ret = true;
 
429
            break;
 
430
 
 
431
        case GDK_parenleft:
 
432
            persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Y, 180/snaps, MOD__ALT);
 
433
            sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
 
434
                             _("Change perspective (angle of PLs)"));
 
435
            ret = true;
 
436
            break;
 
437
 
 
438
        case GDK_braceright:
 
439
            persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Z, -180/snaps, MOD__ALT);
 
440
            sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
 
441
                             _("Change perspective (angle of PLs)"));
 
442
            ret = true;
 
443
            break;
 
444
 
 
445
        case GDK_braceleft:
 
446
            persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Z, 180/snaps, MOD__ALT);
 
447
            sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
 
448
                             _("Change perspective (angle of PLs)"));
 
449
            ret = true;
 
450
            break;
 
451
 
 
452
        /* TODO: what is this???
 
453
        case GDK_O:
 
454
            if (MOD__CTRL && MOD__SHIFT) {
 
455
                Box3D::create_canvas_point(persp3d_get_VP(inkscape_active_document()->current_persp3d, Proj::W).affine(),
 
456
                                           6, 0xff00ff00);
 
457
            }
 
458
            ret = true;
 
459
            break;
 
460
        */
 
461
 
 
462
        case GDK_g:
 
463
        case GDK_G:
 
464
            if (MOD__SHIFT_ONLY) {
 
465
                sp_selection_to_guides(desktop);
 
466
                ret = true;
 
467
            }
 
468
            break;
 
469
 
 
470
        case GDK_x:
 
471
        case GDK_X:
 
472
            if (MOD__ALT_ONLY) {
 
473
                desktop->setToolboxFocusTo ("altx-box3d");
 
474
                ret = TRUE;
 
475
            }
 
476
            if (MOD__SHIFT_ONLY) {
 
477
                persp3d_toggle_VPs(selection->perspList(), Proj::X);
 
478
                bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
 
479
                ret = true;
 
480
            }
 
481
            break;
 
482
 
 
483
        case GDK_y:
 
484
        case GDK_Y:
 
485
            if (MOD__SHIFT_ONLY) {
 
486
                persp3d_toggle_VPs(selection->perspList(), Proj::Y);
 
487
                bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
 
488
                ret = true;
 
489
            }
 
490
            break;
 
491
 
 
492
        case GDK_z:
 
493
        case GDK_Z:
 
494
            if (MOD__SHIFT_ONLY) {
 
495
                persp3d_toggle_VPs(selection->perspList(), Proj::Z);
 
496
                bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
 
497
                ret = true;
 
498
            }
 
499
            break;
 
500
 
 
501
        case GDK_Escape:
 
502
            sp_desktop_selection(desktop)->clear();
 
503
            //TODO: make dragging escapable by Esc
 
504
            break;
 
505
 
 
506
        case GDK_space:
 
507
            if (dragging) {
 
508
                sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
 
509
                                      event->button.time);
 
510
                dragging = false;
 
511
                sp_event_context_snap_window_closed(event_context);
 
512
                if (!event_context->within_tolerance) {
 
513
                    // we've been dragging, finish the box
 
514
                    sp_box3d_finish(bc);
 
515
                }
 
516
                // do not return true, so that space would work switching to selector
 
517
            }
 
518
            break;
 
519
 
 
520
        default:
 
521
            break;
 
522
        }
 
523
        break;
 
524
    default:
 
525
        break;
 
526
    }
 
527
 
 
528
    if (!ret) {
 
529
        if (((SPEventContextClass *) parent_class)->root_handler) {
 
530
            ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
 
531
        }
 
532
    }
 
533
 
 
534
    return ret;
 
535
}
 
536
 
 
537
static void sp_box3d_drag(Box3DContext &bc, guint /*state*/)
 
538
{
 
539
    SPDesktop *desktop = SP_EVENT_CONTEXT(&bc)->desktop;
 
540
 
 
541
    if (!bc.item) {
 
542
 
 
543
        if (Inkscape::have_viable_layer(desktop, bc._message_context) == false) {
 
544
            return;
 
545
        }
 
546
 
 
547
        /* Create object */
 
548
        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&bc));
 
549
        Inkscape::XML::Node *repr = xml_doc->createElement("svg:g");
 
550
        repr->setAttribute("sodipodi:type", "inkscape:box3d");
 
551
 
 
552
        /* Set style */
 
553
        sp_desktop_apply_style_tool (desktop, repr, "/tools/shapes/3dbox", false);
 
554
 
 
555
        bc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
 
556
        Inkscape::GC::release(repr);
 
557
        Inkscape::XML::Node *repr_side;
 
558
        // TODO: Incorporate this in box3d-side.cpp!
 
559
        for (int i = 0; i < 6; ++i) {
 
560
            repr_side = xml_doc->createElement("svg:path");
 
561
            repr_side->setAttribute("sodipodi:type", "inkscape:box3dside");
 
562
            repr->addChild(repr_side, NULL);
 
563
 
 
564
            Box3DSide *side = SP_BOX3D_SIDE(inkscape_active_document()->getObjectByRepr (repr_side));
 
565
 
 
566
            guint desc = Box3D::int_to_face(i);
 
567
 
 
568
            Box3D::Axis plane = (Box3D::Axis) (desc & 0x7);
 
569
            plane = (Box3D::is_plane(plane) ? plane : Box3D::orth_plane_or_axis(plane));
 
570
            side->dir1 = Box3D::extract_first_axis_direction(plane);
 
571
            side->dir2 = Box3D::extract_second_axis_direction(plane);
 
572
            side->front_or_rear = (Box3D::FrontOrRear) (desc & 0x8);
 
573
 
 
574
            /* Set style */
 
575
            box3d_side_apply_style(side);
 
576
 
 
577
            SP_OBJECT(side)->updateRepr(); // calls box3d_side_write() and updates, e.g., the axes string description
 
578
        }
 
579
 
 
580
        box3d_set_z_orders(SP_BOX3D(bc.item));
 
581
        bc.item->updateRepr();
 
582
 
 
583
        // TODO: It would be nice to show the VPs during dragging, but since there is no selection
 
584
        //       at this point (only after finishing the box), we must do this "manually"
 
585
        /**** bc._vpdrag->updateDraggers(); ****/
 
586
 
 
587
        sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
 
588
    }
 
589
 
 
590
    g_assert(bc.item);
 
591
 
 
592
    SPBox3D *box = SP_BOX3D(bc.item);
 
593
 
 
594
    box->orig_corner0 = bc.drag_origin_proj;
 
595
    box->orig_corner7 = bc.drag_ptC_proj;
 
596
 
 
597
    box3d_check_for_swapped_coords(box);
 
598
 
 
599
    /* we need to call this from here (instead of from box3d_position_set(), for example)
 
600
       because z-order setting must not interfere with display updates during undo/redo */
 
601
    box3d_set_z_orders (box);
 
602
 
 
603
    box3d_position_set(box);
 
604
 
 
605
    // status text
 
606
    bc._message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>3D Box</b>; with <b>Shift</b> to extrude along the Z axis"));
 
607
}
 
608
 
 
609
static void sp_box3d_finish(Box3DContext *bc)
 
610
{
 
611
    bc->_message_context->clear();
 
612
    g_assert (SP_ACTIVE_DOCUMENT->current_persp3d);
 
613
 
 
614
    if ( bc->item != NULL ) {
 
615
        SPDesktop * desktop = SP_EVENT_CONTEXT_DESKTOP(bc);
 
616
 
 
617
        SPBox3D *box = SP_BOX3D(bc->item);
 
618
 
 
619
        box->orig_corner0 = bc->drag_origin_proj;
 
620
        box->orig_corner7 = bc->drag_ptC_proj;
 
621
 
 
622
        box->updateRepr();
 
623
 
 
624
        box3d_relabel_corners(box);
 
625
 
 
626
        sp_canvas_end_forced_full_redraws(desktop->canvas);
 
627
 
 
628
        sp_desktop_selection(desktop)->set(bc->item);
 
629
        sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
 
630
                         _("Create 3D box"));
 
631
 
 
632
        bc->item = NULL;
 
633
    }
 
634
 
 
635
    bc->ctrl_dragged = false;
 
636
    bc->extruded = false;
 
637
}
 
638
 
 
639
void sp_box3d_context_update_lines(SPEventContext *ec) {
 
640
    /*  update perspective lines if we are in the 3D box tool (so that infinite ones are shown correctly) */
 
641
    if (SP_IS_BOX3D_CONTEXT (ec)) {
 
642
        Box3DContext *bc = SP_BOX3D_CONTEXT (ec);
 
643
        bc->_vpdrag->updateLines();
 
644
    }
 
645
}
 
646
 
 
647
/*
 
648
  Local Variables:
 
649
  mode:c++
 
650
  c-file-style:"stroustrup"
 
651
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
652
  indent-tabs-mode:nil
 
653
  fill-column:99
 
654
  End:
 
655
*/
 
656
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :