lpe-tool-context.cpp revision 78969d2654d9551abe3e7a5142875516809e4254
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev * LPEToolContext: a context for a generic tool composed of subtools that are given by LPEs
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev * Maximilian Albert <maximilian.albert@gmail.com>
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev * Lauris Kaplinski <lauris@kaplinski.com>
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 * Released under GNU GPL, read the file 'COPYING' for more information
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 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 Kitaevvoid sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita KitaevInkscape::LivePathEffect::EffectType lpesubtools[] = {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev Inkscape::LivePathEffect::INVALID_LPE, // this must be here to account for the "all inactive" action
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevstatic SPPenContextClass *lpetool_parent_class = 0;
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 0, // n_preallocs
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev 0 // value_table
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev type = g_type_register_static(SP_TYPE_PEN_CONTEXT, "SPLPEToolContext", &info, static_cast<GTypeFlags>(0));
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevsp_lpetool_context_class_init(SPLPEToolContextClass *klass)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev GObjectClass *object_class = (GObjectClass *) klass;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lpetool_parent_class = (SPPenContextClass*)g_type_class_peek_parent(klass);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev object_class->dispose = sp_lpetool_context_dispose;
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 new (&lc->sel_changed_connection) sigc::connection();
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(object);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(ec);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev if (((SPEventContextClass *) lpetool_parent_class)->setup)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_lpetool_context_selection_changed), (gpointer)lc));
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor = new ShapeEditor(ec->desktop);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lpetool_context_switch_mode(lc, Inkscape::LivePathEffect::INVALID_LPE);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev// TODO temp force:
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->set_item(item, SH_NODEPATH);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->set_item(item, SH_KNOTHOLDER);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
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 Kitaevsp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(data);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev // TODO: update ShapeEditorsCollective instead
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->set_item(item, SH_NODEPATH);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev lc->shape_editor->set_item(item, SH_KNOTHOLDER);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaevsp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
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 //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 Kitaevsp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev Inkscape::Selection *selection = sp_desktop_selection (desktop);
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev bool ret = false;
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 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 if (event->button.button == 1 && !event_context->space_panning) {
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev // save drag origin
8fe37a993fbb0c0a84a7ab4a77cd83c87162c2daNikita Kitaev int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
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 sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Inkscape::LivePathEffect::Effect::acceptsNumClicks(type));
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 if (!lc->shape_editor->has_nodepath() || selection->singleItem() == NULL) {
bool over_stroke = false;
if (over_stroke) {
case GDK_BUTTON_RELEASE:
case GDK_KEY_PRESS:
case GDK_KEY_RELEASE:
if (!ret) {
return ret;
for (int i = 0; i < num_subtools; ++i) {
if (!lpe) {
if (item && SP_IS_LPE_ITEM(item) && Inkscape::LivePathEffect::Effect::acceptsNumClicks(type) == 0) {
A *= doc2dt;
B *= doc2dt;
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(lc->canvas_bbox), 0x0000ffff, 0.8, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 5);