lpe-tool-context.cpp revision 78969d2654d9551abe3e7a5142875516809e4254
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev/*
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev * LPEToolContext: a context for a generic tool composed of subtools that are given by LPEs
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev *
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev * Authors:
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev * Maximilian Albert <maximilian.albert@gmail.com>
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev * Lauris Kaplinski <lauris@kaplinski.com>
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev *
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev * Copyright (C) 1998 The Free Software Foundation
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev * Copyright (C) 1999-2005 authors
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev * Copyright (C) 2001-2002 Ximian, Inc.
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev * Copyright (C) 2008 Maximilian Albert
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev *
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev * Released under GNU GPL, read the file 'COPYING' for more information
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev */
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#ifdef HAVE_CONFIG_H
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "config.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#endif
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "forward.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "pixmaps/cursor-node.xpm"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "pixmaps/cursor-crosshairs.xpm"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include <gtk/gtk.h>
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "desktop.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "message-context.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "prefs-utils.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "shape-editor.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "selection.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "desktop-handles.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "document.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "display/curve.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "display/canvas-bpath.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "message-stack.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev#include "lpe-tool-context.h"
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic void sp_lpetool_context_class_init(SPLPEToolContextClass *klass);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic void sp_lpetool_context_init(SPLPEToolContext *erc);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic void sp_lpetool_context_dispose(GObject *object);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic void sp_lpetool_context_setup(SPEventContext *ec);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic void sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic gint sp_lpetool_context_root_handler(SPEventContext *ec, GdkEvent *event);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevvoid sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevconst int num_subtools = 7;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita KitaevInkscape::LivePathEffect::EffectType lpesubtools[] = {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev Inkscape::LivePathEffect::INVALID_LPE, // this must be here to account for the "all inactive" action
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev Inkscape::LivePathEffect::LINE_SEGMENT,
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev Inkscape::LivePathEffect::CIRCLE_3PTS,
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev Inkscape::LivePathEffect::CIRCLE_WITH_RADIUS,
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev Inkscape::LivePathEffect::PARALLEL,
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev Inkscape::LivePathEffect::PERP_BISECTOR,
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev Inkscape::LivePathEffect::ANGLE_BISECTOR,
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev};
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic SPPenContextClass *lpetool_parent_class = 0;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita KitaevGType sp_lpetool_context_get_type(void)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev{
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev static GType type = 0;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev if (!type) {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev GTypeInfo info = {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev sizeof(SPLPEToolContextClass),
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev 0, // base_init
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev 0, // base_finalize
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev (GClassInitFunc)sp_lpetool_context_class_init,
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev 0, // class_finalize
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev 0, // class_data
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev sizeof(SPLPEToolContext),
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev 0, // n_preallocs
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev (GInstanceInitFunc)sp_lpetool_context_init,
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev 0 // value_table
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev };
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev type = g_type_register_static(SP_TYPE_PEN_CONTEXT, "SPLPEToolContext", &info, static_cast<GTypeFlags>(0));
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev }
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev return type;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev}
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic void
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevsp_lpetool_context_class_init(SPLPEToolContextClass *klass)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev{
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev GObjectClass *object_class = (GObjectClass *) klass;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lpetool_parent_class = (SPPenContextClass*)g_type_class_peek_parent(klass);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev object_class->dispose = sp_lpetool_context_dispose;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev event_context_class->setup = sp_lpetool_context_setup;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev event_context_class->set = sp_lpetool_context_set;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev event_context_class->root_handler = sp_lpetool_context_root_handler;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev}
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic void
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevsp_lpetool_context_init(SPLPEToolContext *lc)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev{
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->cursor_shape = cursor_crosshairs_xpm;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->hot_x = 7;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->hot_y = 7;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->canvas_bbox = NULL;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev new (&lc->sel_changed_connection) sigc::connection();
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev}
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic void
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevsp_lpetool_context_dispose(GObject *object)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev{
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(object);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev delete lc->shape_editor;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev if (lc->canvas_bbox) {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->canvas_bbox = NULL;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev }
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->sel_changed_connection.disconnect();
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->sel_changed_connection.~connection();
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev}
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic void
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevsp_lpetool_context_setup(SPEventContext *ec)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev{
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(ec);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev if (((SPEventContextClass *) lpetool_parent_class)->setup)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPItem *item = selection->singleItem();
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->sel_changed_connection.disconnect();
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->sel_changed_connection =
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_lpetool_context_selection_changed), (gpointer)lc));
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor = new ShapeEditor(ec->desktop);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lpetool_context_switch_mode(lc, Inkscape::LivePathEffect::INVALID_LPE);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lpetool_context_reset_limiting_bbox(lc);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev// TODO temp force:
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev ec->enableSelectionCue();
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev if (item) {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->set_item(item, SH_NODEPATH);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->set_item(item, SH_KNOTHOLDER);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev }
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev ec->enableSelectionCue();
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev }
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->update_statusbar();
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev}
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev/**
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev\brief Callback that processes the "changed" signal on the selection;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevdestroys old and creates new nodepath and reassigns listeners to the new selected item's repr
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev*/
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevvoid
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevsp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev{
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(data);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev // TODO: update ShapeEditorsCollective instead
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->unset_item(SH_NODEPATH);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->unset_item(SH_KNOTHOLDER);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPItem *item = selection->singleItem();
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->set_item(item, SH_NODEPATH);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->set_item(item, SH_KNOTHOLDER);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->update_statusbar();
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev}
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic void
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevsp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev{
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev // FIXME: how to set this correcly? the value from preferences-skeleton.h doesn't seem to get
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev // read (it wants to set drag = 1)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lpetool_parent_class->set(ec, key, "drag");
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev /**
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev //pass on up to parent class to handle common attributes.
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev if ( lpetool_parent_class->set ) {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lpetool_parent_class->set(ec, key, val);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev }
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev **/
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev}
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevgint
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevsp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev{
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPDesktop *desktop = event_context->desktop;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev Inkscape::Selection *selection = sp_desktop_selection (desktop);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev bool ret = false;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev if (sp_pen_context_has_waiting_LPE(lc)) {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev // quit when we are waiting for a LPE to be applied
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev return ret;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev }
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev switch (event->type) {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev case GDK_BUTTON_PRESS:
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev if (lc->mode == Inkscape::LivePathEffect::INVALID_LPE) {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev // don't do anything for now if we are inactive
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Choose a construction tool from the toolbar."));
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev ret = true;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev break;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev }
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev if (event->button.button == 1 && !event_context->space_panning) {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev // save drag origin
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev event_context->xp = (gint) event->button.x;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev event_context->yp = (gint) event->button.y;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev event_context->within_tolerance = true;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->cancel_hit();
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev using namespace Inkscape::LivePathEffect;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev EffectType type = lpesubtools[mode];
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev // save drag origin
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev bool over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->button.x, event->button.y), true);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Inkscape::LivePathEffect::Effect::acceptsNumClicks(type));
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev // we pass the mouse click on to pen tool as the first click which it should collect
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev }
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev break;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev case GDK_MOTION_NOTIFY:
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev if (!lc->shape_editor->has_nodepath() || selection->singleItem() == NULL) {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev break;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev }
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev
bool over_stroke = false;
over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->motion.x, event->motion.y), false);
if (over_stroke) {
event_context->cursor_shape = cursor_node_xpm;
event_context->hot_x = 1;
event_context->hot_y = 1;
sp_event_context_update_cursor(event_context);
} else {
lc->cursor_shape = cursor_crosshairs_xpm;
lc->hot_x = 7;
lc->hot_y = 7;
sp_event_context_update_cursor(event_context);
}
}
break;
case GDK_BUTTON_RELEASE:
{
/**
break;
**/
}
case GDK_KEY_PRESS:
/**
switch (get_group0_keyval (&event->key)) {
}
break;
**/
case GDK_KEY_RELEASE:
/**
switch (get_group0_keyval(&event->key)) {
case GDK_Control_L:
case GDK_Control_R:
dc->_message_context->clear();
break;
default:
break;
}
**/
default:
break;
}
if (!ret) {
if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
}
}
return ret;
}
/*
* Finds the index in the list of geometric subtools corresponding to the given LPE type.
* Returns -1 if no subtool is found.
*/
int
lpetool_mode_to_index(Inkscape::LivePathEffect::EffectType const type) {
for (int i = 0; i < num_subtools; ++i) {
if (lpesubtools[i] == type) {
return i;
}
}
return -1;
}
/*
* Checks whether an item has a construction applied as LPE and if so returns the index in
* lpesubtools of this construction
*/
int lpetool_item_has_construction(SPLPEToolContext *lc, SPItem *item)
{
if (!SP_IS_LPE_ITEM(item)) {
return -1;
}
Inkscape::LivePathEffect::Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
if (!lpe) {
return -1;
}
return lpetool_mode_to_index(lpe->effectType());
}
/*
* Attempts to perform the construction of the given type (i.e., to apply the corresponding LPE) to
* a single selected item. Returns whether we succeeded.
*/
bool
lpetool_try_construction(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
{
Inkscape::Selection *selection = sp_desktop_selection(lc->desktop);
SPItem *item = selection->singleItem();
// TODO: should we check whether type represents a valid geometric construction?
if (item && SP_IS_LPE_ITEM(item) && Inkscape::LivePathEffect::Effect::acceptsNumClicks(type) == 0) {
Inkscape::LivePathEffect::Effect::createAndApply(type, sp_desktop_document(lc->desktop), item);
return true;
}
return false;
}
void
lpetool_context_switch_mode(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
{
int index = lpetool_mode_to_index(type);
if (index != -1) {
lc->mode = type;
lc->desktop->setToolboxSelectOneValue ("lpetool_mode_action", index);
} else {
g_warning ("Invalid mode selected: %d", type);
return;
}
}
void
lpetool_get_limiting_bbox_corners(SPDocument *document, Geom::Point &A, Geom::Point &B) {
Geom::Coord w = sp_document_width(document);
Geom::Coord h = sp_document_height(document);
double ulx = prefs_get_double_attribute ("tools.lpetool", "bbox_upperleftx", 0);
double uly = prefs_get_double_attribute ("tools.lpetool", "bbox_upperlefty", 0);
double lrx = prefs_get_double_attribute ("tools.lpetool", "bbox_lowerrightx", w);
double lry = prefs_get_double_attribute ("tools.lpetool", "bbox_lowerrighty", h);
A = Geom::Point(ulx, uly);
B = Geom::Point(lrx, lry);
}
/*
* Reads the limiting bounding box from preferences and draws it on the screen
*/
// TODO: Note that currently the bbox is not user-settable; we simply use the page borders
void
lpetool_context_reset_limiting_bbox(SPLPEToolContext *lc)
{
if (lc->canvas_bbox) {
gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
lc->canvas_bbox = NULL;
}
if (prefs_get_int_attribute("tools.lpetool", "show_bbox", 1) == 0)
return;
SPDocument *document = sp_desktop_document(lc->desktop);
Geom::Point A, B;
lpetool_get_limiting_bbox_corners(document, A, B);
NR::Matrix doc2dt(lc->desktop->doc2dt());
A *= doc2dt;
B *= doc2dt;
Geom::Rect rect(A, B);
SPCurve *curve = SPCurve::new_from_rect(rect);
lc->canvas_bbox = sp_canvas_bpath_new (sp_desktop_controls(lc->desktop), curve);
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(lc->canvas_bbox), 0x0000ffff, 0.8, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 5);
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :