Code

Cosmetic
[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 Box3D::Perspective3D * Box3D::Perspective3D::current_perspective = NULL;
94 guint SP3DBoxContext::number_of_handles = 3;
96 static void sp_3dbox_context_init(SP3DBoxContext *box3d_context)
97 {
98     SPEventContext *event_context = SP_EVENT_CONTEXT(box3d_context);
100     event_context->cursor_shape = cursor_rect_xpm;
101     event_context->hot_x = 4;
102     event_context->hot_y = 4;
103     event_context->xp = 0;
104     event_context->yp = 0;
105     event_context->tolerance = 0;
106     event_context->within_tolerance = false;
107     event_context->item_to_select = NULL;
109     event_context->shape_repr = NULL;
110     event_context->shape_knot_holder = NULL;
112     box3d_context->item = NULL;
114     box3d_context->ctrl_dragged = false;
115     box3d_context->extruded = false;
116     
117     new (&box3d_context->sel_changed_connection) sigc::connection();
120 static void sp_3dbox_context_dispose(GObject *object)
122     SP3DBoxContext *bc = SP_3DBOX_CONTEXT(object);
123     SPEventContext *ec = SP_EVENT_CONTEXT(object);
125     ec->enableGrDrag(false);
127     bc->sel_changed_connection.disconnect();
128     bc->sel_changed_connection.~connection();
130     /* fixme: This is necessary because we do not grab */
131     if (bc->item) {
132         sp_3dbox_finish(bc);
133     }
135     if (ec->shape_knot_holder) {
136         sp_knot_holder_destroy(ec->shape_knot_holder);
137         ec->shape_knot_holder = NULL;
138     }
140     if (ec->shape_repr) { // remove old listener
141         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
142         Inkscape::GC::release(ec->shape_repr);
143         ec->shape_repr = 0;
144     }
146     if (bc->_message_context) {
147         delete bc->_message_context;
148     }
150     G_OBJECT_CLASS(parent_class)->dispose(object);
153 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
154     NULL, /* child_added */
155     NULL, /* child_removed */
156     ec_shape_event_attr_changed,
157     NULL, /* content_changed */
158     NULL  /* order_changed */
159 };
161 /**
162 \brief  Callback that processes the "changed" signal on the selection;
163 destroys old and creates new knotholder
164 */
165 void sp_3dbox_context_selection_changed(Inkscape::Selection *selection, gpointer data)
167     SP3DBoxContext *bc = SP_3DBOX_CONTEXT(data);
168     SPEventContext *ec = SP_EVENT_CONTEXT(bc);
170     if (ec->shape_knot_holder) { // destroy knotholder
171         sp_knot_holder_destroy(ec->shape_knot_holder);
172         ec->shape_knot_holder = NULL;
173     }
175     if (ec->shape_repr) { // remove old listener
176         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
177         Inkscape::GC::release(ec->shape_repr);
178         ec->shape_repr = 0;
179     }
181     SPItem *item = selection->singleItem();
182     if (item) {
183         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
184         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
185         if (shape_repr) {
186             ec->shape_repr = shape_repr;
187             Inkscape::GC::anchor(shape_repr);
188             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
189         }
190     }
193 static void sp_3dbox_context_setup(SPEventContext *ec)
195     SP3DBoxContext *bc = SP_3DBOX_CONTEXT(ec);
197     if (((SPEventContextClass *) parent_class)->setup) {
198         ((SPEventContextClass *) parent_class)->setup(ec);
199     }
201     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
202     if (item) {
203         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
204         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
205         if (shape_repr) {
206             ec->shape_repr = shape_repr;
207             Inkscape::GC::anchor(shape_repr);
208             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
209         }
210     }
212     bc->sel_changed_connection.disconnect();
213     bc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
214         sigc::bind(sigc::ptr_fun(&sp_3dbox_context_selection_changed), (gpointer)bc)
215     );
217     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
218         ec->enableSelectionCue();
219     }
221     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
222         ec->enableGrDrag();
223     }
225     bc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
228 static void sp_3dbox_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
230     //SP3DBoxContext *bc = SP_3DBOX_CONTEXT(ec);
232     /* fixme: Proper error handling for non-numeric data.  Use a locale-independent function like
233      * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
234     /**
235     if ( strcmp(key, "rx") == 0 ) {
236         bc->rx = ( val
237                          ? g_ascii_strtod (val, NULL)
238                          : 0.0 );
239     } else if ( strcmp(key, "ry") == 0 ) {
240         bc->ry = ( val
241                          ? g_ascii_strtod (val, NULL)
242                          : 0.0 );
243     }
244     **/
247 static gint sp_3dbox_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
249     SPDesktop *desktop = event_context->desktop;
251     gint ret = FALSE;
253     switch (event->type) {
254     case GDK_BUTTON_PRESS:
255         if ( event->button.button == 1 && !event_context->space_panning) {
256             Inkscape::setup_for_drag_start(desktop, event_context, event);
257             ret = TRUE;
258         }
259         break;
260         // motion and release are always on root (why?)
261     default:
262         break;
263     }
265     if (((SPEventContextClass *) parent_class)->item_handler) {
266         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
267     }
269     return ret;
272 static gint sp_3dbox_context_root_handler(SPEventContext *event_context, GdkEvent *event)
274     static bool dragging;
276     SPDesktop *desktop = event_context->desktop;
277     Inkscape::Selection *selection = sp_desktop_selection (desktop);
279     SP3DBoxContext *bc = SP_3DBOX_CONTEXT(event_context);
281     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
283     gint ret = FALSE;
284     switch (event->type) {
285     case GDK_BUTTON_PRESS:
286         if ( event->button.button == 1  && !event_context->space_panning) {
287             NR::Point const button_w(event->button.x,
288                                      event->button.y);
290             // save drag origin
291             event_context->xp = (gint) button_w[NR::X];
292             event_context->yp = (gint) button_w[NR::Y];
293             event_context->within_tolerance = true;
294             
295             // remember clicked item, disregarding groups, honoring Alt
296             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);
298             dragging = true;
299             
300             /* Position center */
301             NR::Point const button_dt(desktop->w2d(button_w));
302             bc->drag_origin = button_dt;
303             bc->drag_ptB = button_dt;
304             bc->drag_ptC = button_dt;
306             /* Snap center */
307             SnapManager const &m = desktop->namedview->snap_manager;
308             bc->center = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE | Inkscape::Snapper::SNAPPOINT_BBOX,
309                                     button_dt, bc->item).getPoint();
311             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
312                                 ( GDK_KEY_PRESS_MASK |
313                                   GDK_BUTTON_RELEASE_MASK       |
314                                   GDK_POINTER_MOTION_MASK       |
315                                   GDK_BUTTON_PRESS_MASK ),
316                                 NULL, event->button.time);
317             ret = TRUE;
318         }
319         break;
320     case GDK_MOTION_NOTIFY:
321         if ( dragging
322              && ( event->motion.state & GDK_BUTTON1_MASK )  && !event_context->space_panning)
323         {
324             if ( event_context->within_tolerance
325                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
326                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
327                 break; // do not drag if we're within tolerance from origin
328             }
329             // Once the user has moved farther than tolerance from the original location
330             // (indicating they intend to draw, not click), then always process the
331             // motion notify coordinates as given (no snapping back to origin)
332             event_context->within_tolerance = false;
334             NR::Point const motion_w(event->motion.x,
335                                      event->motion.y);
336             NR::Point motion_dt(desktop->w2d(motion_w));
338             SnapManager const &m = desktop->namedview->snap_manager;
339             motion_dt = m.freeSnap(Inkscape::Snapper::SNAPPOINT_BBOX | Inkscape::Snapper::SNAPPOINT_NODE, motion_dt, bc->item).getPoint();
341             bc->ctrl_dragged  = event->motion.state & GDK_CONTROL_MASK;
343             if (event->motion.state & GDK_SHIFT_MASK && !bc->extruded) {
344                 /* once shift is pressed, set bc->extruded (no need to create further faces;
345                    all of them are already created in sp_3dbox_init) */
346                 bc->extruded = true;
347             }
349             if (!bc->extruded) {
350                 bc->drag_ptB = motion_dt;
351                 bc->drag_ptC = motion_dt;
352             } else {
353                 // Without Ctrl, motion of the extruded corner is constrained to the
354                 // perspective line from drag_ptB to vanishing point Y.
355                 if (!bc->ctrl_dragged) {
356                         bc->drag_ptC = Box3D::perspective_line_snap (bc->drag_ptB, Box3D::Z, motion_dt, Box3D::Perspective3D::current_perspective);
357                 } else {
358                     bc->drag_ptC = motion_dt;
359                 }
360                 bc->drag_ptC = m.freeSnap(Inkscape::Snapper::SNAPPOINT_BBOX | Inkscape::Snapper::SNAPPOINT_NODE, bc->drag_ptC, bc->item).getPoint();
361                 if (bc->ctrl_dragged) {
362                         Box3D::PerspectiveLine pl1 (NR::Point (event_context->xp, event_context->yp), Box3D::Y, Box3D::Perspective3D::current_perspective);
363                         Box3D::PerspectiveLine pl2 (bc->drag_ptB, Box3D::X, Box3D::Perspective3D::current_perspective);
364                         NR::Point corner1 = pl1.meet(pl2);
365                         
366                         Box3D::PerspectiveLine pl3 (corner1, Box3D::X, Box3D::Perspective3D::current_perspective);
367                         Box3D::PerspectiveLine pl4 (bc->drag_ptC, Box3D::Z, Box3D::Perspective3D::current_perspective);
368                         bc->drag_ptB = pl3.meet(pl4);
369                 }
370             }
371             
372             
373             sp_3dbox_drag(*bc, event->motion.state);
374             
375             ret = TRUE;
376         }
377         break;
378     case GDK_BUTTON_RELEASE:
379         event_context->xp = event_context->yp = 0;
380         if ( event->button.button == 1  && !event_context->space_panning) {
381             dragging = false;
383             if (!event_context->within_tolerance) {
384                 // we've been dragging, finish the box
385                 sp_3dbox_finish(bc);
386             } else if (event_context->item_to_select) {
387                 // no dragging, select clicked item if any
388                 if (event->button.state & GDK_SHIFT_MASK) {
389                     selection->toggle(event_context->item_to_select);
390                 } else {
391                     selection->set(event_context->item_to_select);
392                 }
393             } else {
394                 // click in an empty space
395                 selection->clear();
396             }
398             event_context->item_to_select = NULL;
399             ret = TRUE;
400             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
401                                   event->button.time);
402         }
403         break;
404     case GDK_KEY_PRESS:
405         switch (get_group0_keyval (&event->key)) {
406         case GDK_Alt_L:
407         case GDK_Alt_R:
408         case GDK_Control_L:
409         case GDK_Control_R:
410         case GDK_Shift_L:
411         case GDK_Shift_R:
412         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
413         case GDK_Meta_R:
414             /***
415             if (!dragging){
416                 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
417                                             _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
418                                             _("<b>Shift</b>: draw around the starting point"),
419                                             NULL);
420             }
421             ***/
422             break;
423         case GDK_Up:
424         case GDK_Down:
425         case GDK_KP_Up:
426         case GDK_KP_Down:
427             // prevent the zoom field from activation
428             if (!MOD__CTRL_ONLY)
429                 ret = TRUE;
430             break;
432         case GDK_x:
433         case GDK_X:
434             if (MOD__ALT_ONLY) {
435                 // desktop->setToolboxFocusTo ("altx-rect");
436                 ret = TRUE;
437             }
438             break;
440         case GDK_Escape:
441             sp_desktop_selection(desktop)->clear();
442             //TODO: make dragging escapable by Esc
443             break;
445         case GDK_space:
446             if (dragging) {
447                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
448                                       event->button.time);
449                 dragging = false;
450                 if (!event_context->within_tolerance) {
451                     // we've been dragging, finish the box
452                     sp_3dbox_finish(bc);
453                 }
454                 // do not return true, so that space would work switching to selector
455             }
456             break;
458         default:
459             break;
460         }
461         break;
462     case GDK_KEY_RELEASE:
463         switch (get_group0_keyval (&event->key)) {
464         case GDK_Alt_L:
465         case GDK_Alt_R:
466         case GDK_Control_L:
467         case GDK_Control_R:
468         case GDK_Shift_L:
469         case GDK_Shift_R:
470         case GDK_Meta_L:  // Meta is when you press Shift+Alt
471         case GDK_Meta_R:
472             event_context->defaultMessageContext()->clear();
473             break;
474         default:
475             break;
476         }
477         break;
478     default:
479         break;
480     }
482     if (!ret) {
483         if (((SPEventContextClass *) parent_class)->root_handler) {
484             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
485         }
486     }
488     return ret;
491 static void sp_3dbox_drag(SP3DBoxContext &bc, guint state)
493     SPDesktop *desktop = SP_EVENT_CONTEXT(&bc)->desktop;
495     if (!bc.item) {
497         if (Inkscape::have_viable_layer(desktop, bc._message_context) == false) {
498             return;
499         }
501         /* Create object */
502         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&bc));
503         Inkscape::XML::Node *repr = xml_doc->createElement("svg:g");
504         repr->setAttribute("sodipodi:type", "inkscape:3dbox");
506         /* Set style */
507         sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.3dbox", false);
509         bc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
510         Inkscape::GC::release(repr);
511         bc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
513         /* Hook paths to the faces of the box */
514         for (int i = 0; i < 6; ++i) {
515             SP_3DBOX(bc.item)->faces[i]->hook_path_to_3dbox();
516         }
518         bc.item->updateRepr();
520         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
521     }
523     // FIXME: remove these extra points
524     NR::Point pt = bc.drag_ptB;
525     NR::Point shift_pt = bc.drag_ptC;
527     NR::Rect r;
528     if (!(state & GDK_SHIFT_MASK)) {
529         r = Inkscape::snap_rectangular_box(desktop, bc.item, pt, bc.center, state);
530     } else {
531         r = Inkscape::snap_rectangular_box(desktop, bc.item, shift_pt, bc.center, state);
532     }
534     SPEventContext *ec = SP_EVENT_CONTEXT(&bc);
535     NR::Point origin_w(ec->xp, ec->yp);
536     NR::Point origin(desktop->w2d(origin_w));
537     sp_3dbox_position_set(bc);
539     // status text
540     //GString *Ax = SP_PX_TO_METRIC_STRING(origin[NR::X], desktop->namedview->getDefaultMetric());
541     //GString *Ay = SP_PX_TO_METRIC_STRING(origin[NR::Y], desktop->namedview->getDefaultMetric());
542     bc._message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>3D Box</b>; with <b>Shift</b> to extrude along the Z axis"));
543     //g_string_free(Ax, FALSE);
544     //g_string_free(Ay, FALSE);
547 static void sp_3dbox_finish(SP3DBoxContext *bc)
549     bc->_message_context->clear();
551     if ( bc->item != NULL ) {
552         SPDesktop * desktop;
554         desktop = SP_EVENT_CONTEXT_DESKTOP(bc);
556         SP_OBJECT(bc->item)->updateRepr();
558         sp_canvas_end_forced_full_redraws(desktop->canvas);
560         sp_desktop_selection(desktop)->set(bc->item);
561         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
562                          _("Create 3D box"));
564         bc->item = NULL;
565     }
567     bc->ctrl_dragged = false;
568     bc->extruded = false;
571 /*
572   Local Variables:
573   mode:c++
574   c-file-style:"stroustrup"
575   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
576   indent-tabs-mode:nil
577   fill-column:99
578   End:
579 */
580 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :