rect-toolbar.cpp revision e918c2e4dcbcd27cf2a8259e53ba7b1196edb291
2N/A/**
2N/A * @file
2N/A * Rect aux toolbar
2N/A */
2N/A/* Authors:
2N/A * MenTaLguY <mental@rydia.net>
2N/A * Lauris Kaplinski <lauris@kaplinski.com>
2N/A * bulia byak <buliabyak@users.sf.net>
2N/A * Frank Felfe <innerspace@iname.com>
2N/A * John Cliff <simarilius@yahoo.com>
2N/A * David Turner <novalis@gnu.org>
2N/A * Josh Andler <scislac@scislac.com>
2N/A * Jon A. Cruz <jon@joncruz.org>
2N/A * Maximilian Albert <maximilian.albert@gmail.com>
2N/A * Tavmjong Bah <tavmjong@free.fr>
2N/A * Abhishek Sharma
2N/A * Kris De Gussem <Kris.DeGussem@gmail.com>
2N/A *
2N/A * Copyright (C) 2004 David Turner
2N/A * Copyright (C) 2003 MenTaLguY
2N/A * Copyright (C) 1999-2011 authors
2N/A * Copyright (C) 2001-2002 Ximian, Inc.
2N/A *
2N/A * Released under GNU GPL, read the file 'COPYING' for more information
2N/A */
2N/A
2N/A#ifdef HAVE_CONFIG_H
2N/A# include "config.h"
2N/A#endif
2N/A
2N/A#include "ui/widget/spinbutton.h"
2N/A#include <glibmm/i18n.h>
2N/A#include "toolbox.h"
2N/A#include "rect-toolbar.h"
2N/A
2N/A#include "../desktop.h"
2N/A#include "../desktop-handles.h"
2N/A#include "document-undo.h"
2N/A#include "../verbs.h"
2N/A#include "../inkscape.h"
2N/A#include "../selection-chemistry.h"
2N/A#include "../selection.h"
2N/A#include "../ege-adjustment-action.h"
2N/A#include "../ege-output-action.h"
2N/A#include "../ege-select-one-action.h"
2N/A#include "../ink-action.h"
2N/A#include "../ink-comboboxentry-action.h"
2N/A#include "../widgets/button.h"
2N/A#include "../widgets/spinbutton-events.h"
2N/A#include "../widgets/spw-utilities.h"
2N/A#include "../widgets/widget-sizes.h"
2N/A#include "../xml/node-event-vector.h"
2N/A#include "../xml/repr.h"
2N/A#include "ui/uxmanager.h"
2N/A#include "../ui/icon-names.h"
2N/A#include "../helper/unit-menu.h"
2N/A#include "../helper/units.h"
2N/A#include "../helper/unit-tracker.h"
2N/A#include "../pen-context.h"
2N/A#include "../sp-namedview.h"
2N/A#include "../sp-rect.h"
2N/A
2N/Ausing Inkscape::UnitTracker;
2N/Ausing Inkscape::UI::UXManager;
2N/Ausing Inkscape::DocumentUndo;
2N/Ausing Inkscape::UI::ToolboxFactory;
2N/Ausing Inkscape::UI::PrefPusher;
2N/A
2N/A
2N/A//########################
2N/A//## Rect ##
2N/A//########################
2N/A
2N/Astatic void sp_rtb_sensitivize( GObject *tbl )
2N/A{
2N/A GtkAdjustment *adj1 = GTK_ADJUSTMENT( g_object_get_data(tbl, "rx") );
2N/A GtkAdjustment *adj2 = GTK_ADJUSTMENT( g_object_get_data(tbl, "ry") );
2N/A GtkAction* not_rounded = GTK_ACTION( g_object_get_data(tbl, "not_rounded") );
2N/A
2N/A if (gtk_adjustment_get_value(adj1) == 0 && gtk_adjustment_get_value(adj2) == 0 && g_object_get_data(tbl, "single")) { // only for a single selected rect (for now)
2N/A gtk_action_set_sensitive( not_rounded, FALSE );
2N/A } else {
2N/A gtk_action_set_sensitive( not_rounded, TRUE );
2N/A }
2N/A}
2N/A
2N/A
2N/Astatic void sp_rtb_value_changed(GtkAdjustment *adj, GObject *tbl, gchar const *value_name,
2N/A void (SPRect::*setter)(gdouble))
2N/A{
2N/A SPDesktop *desktop = static_cast<SPDesktop *>(g_object_get_data( tbl, "desktop" ));
2N/A
2N/A UnitTracker* tracker = reinterpret_cast<UnitTracker*>(g_object_get_data( tbl, "tracker" ));
2N/A SPUnit const *unit = tracker->getActiveUnit();
2N/A
2N/A if (DocumentUndo::getUndoSensitive(sp_desktop_document(desktop))) {
2N/A Inkscape::Preferences *prefs = Inkscape::Preferences::get();
2N/A prefs->setDouble(Glib::ustring("/tools/shapes/rect/") + value_name,
2N/A sp_units_get_pixels(gtk_adjustment_get_value(adj), *unit));
2N/A }
2N/A
2N/A // quit if run by the attr_changed listener
2N/A if (g_object_get_data( tbl, "freeze" )) {
2N/A return;
2N/A }
2N/A
2N/A // in turn, prevent listener from responding
2N/A g_object_set_data( tbl, "freeze", GINT_TO_POINTER(TRUE));
2N/A
2N/A bool modmade = false;
2N/A Inkscape::Selection *selection = sp_desktop_selection(desktop);
2N/A for (GSList const *items = selection->itemList(); items != NULL; items = items->next) {
2N/A if (SP_IS_RECT(items->data)) {
2N/A if (gtk_adjustment_get_value(adj) != 0) {
2N/A (SP_RECT(items->data)->*setter)(sp_units_get_pixels(gtk_adjustment_get_value(adj), *unit));
2N/A } else {
2N/A SP_OBJECT(items->data)->getRepr()->setAttribute(value_name, NULL);
2N/A }
2N/A modmade = true;
2N/A }
2N/A }
2N/A
2N/A sp_rtb_sensitivize( tbl );
2N/A
2N/A if (modmade) {
2N/A DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_RECT,
2N/A _("Change rectangle"));
2N/A }
2N/A
2N/A g_object_set_data( tbl, "freeze", GINT_TO_POINTER(FALSE) );
2N/A}
2N/A
2N/Astatic void sp_rtb_rx_value_changed(GtkAdjustment *adj, GObject *tbl)
2N/A{
2N/A sp_rtb_value_changed(adj, tbl, "rx", &SPRect::setVisibleRx);
2N/A}
2N/A
2N/Astatic void sp_rtb_ry_value_changed(GtkAdjustment *adj, GObject *tbl)
2N/A{
2N/A sp_rtb_value_changed(adj, tbl, "ry", &SPRect::setVisibleRy);
2N/A}
2N/A
2N/Astatic void sp_rtb_width_value_changed(GtkAdjustment *adj, GObject *tbl)
2N/A{
2N/A sp_rtb_value_changed(adj, tbl, "width", &SPRect::setVisibleWidth);
2N/A}
2N/A
2N/Astatic void sp_rtb_height_value_changed(GtkAdjustment *adj, GObject *tbl)
2N/A{
2N/A sp_rtb_value_changed(adj, tbl, "height", &SPRect::setVisibleHeight);
2N/A}
2N/A
2N/A
2N/A
2N/Astatic void sp_rtb_defaults( GtkWidget * /*widget*/, GObject *obj)
2N/A{
2N/A GtkAdjustment *adj = 0;
2N/A
2N/A adj = GTK_ADJUSTMENT( g_object_get_data(obj, "rx") );
2N/A gtk_adjustment_set_value(adj, 0.0);
2N/A // this is necessary if the previous value was 0, but we still need to run the callback to change all selected objects
2N/A gtk_adjustment_value_changed(adj);
2N/A
2N/A adj = GTK_ADJUSTMENT( g_object_get_data(obj, "ry") );
2N/A gtk_adjustment_set_value(adj, 0.0);
2N/A gtk_adjustment_value_changed(adj);
2N/A
2N/A sp_rtb_sensitivize( obj );
2N/A}
2N/A
2N/Astatic void rect_tb_event_attr_changed(Inkscape::XML::Node * /*repr*/, gchar const * /*name*/,
2N/A gchar const * /*old_value*/, gchar const * /*new_value*/,
2N/A bool /*is_interactive*/, gpointer data)
2N/A{
2N/A GObject *tbl = G_OBJECT(data);
2N/A
2N/A // quit if run by the _changed callbacks
2N/A if (g_object_get_data( tbl, "freeze" )) {
2N/A return;
2N/A }
2N/A
2N/A // in turn, prevent callbacks from responding
2N/A g_object_set_data( tbl, "freeze", GINT_TO_POINTER(TRUE) );
2N/A
2N/A UnitTracker* tracker = reinterpret_cast<UnitTracker*>( g_object_get_data( tbl, "tracker" ) );
2N/A SPUnit const *unit = tracker->getActiveUnit();
2N/A
2N/A gpointer item = g_object_get_data( tbl, "item" );
2N/A if (item && SP_IS_RECT(item)) {
2N/A {
2N/A GtkAdjustment *adj = GTK_ADJUSTMENT( g_object_get_data( tbl, "rx" ) );
2N/A gdouble rx = SP_RECT(item)->getVisibleRx();
2N/A gtk_adjustment_set_value(adj, sp_pixels_get_units(rx, *unit));
2N/A }
2N/A
2N/A {
2N/A GtkAdjustment *adj = GTK_ADJUSTMENT( g_object_get_data( tbl, "ry" ) );
2N/A gdouble ry = SP_RECT(item)->getVisibleRy();
2N/A gtk_adjustment_set_value(adj, sp_pixels_get_units(ry, *unit));
2N/A }
2N/A
2N/A {
2N/A GtkAdjustment *adj = GTK_ADJUSTMENT( g_object_get_data( tbl, "width" ) );
2N/A gdouble width = SP_RECT(item)->getVisibleWidth();
2N/A gtk_adjustment_set_value(adj, sp_pixels_get_units(width, *unit));
2N/A }
2N/A
2N/A {
2N/A GtkAdjustment *adj = GTK_ADJUSTMENT( g_object_get_data( tbl, "height" ) );
2N/A gdouble height = SP_RECT(item)->getVisibleHeight();
2N/A gtk_adjustment_set_value(adj, sp_pixels_get_units(height, *unit));
2N/A }
2N/A }
2N/A
2N/A sp_rtb_sensitivize( tbl );
2N/A
2N/A g_object_set_data( tbl, "freeze", GINT_TO_POINTER(FALSE) );
2N/A}
2N/A
2N/A
2N/Astatic Inkscape::XML::NodeEventVector rect_tb_repr_events = {
2N/A NULL, /* child_added */
2N/A NULL, /* child_removed */
2N/A rect_tb_event_attr_changed,
2N/A NULL, /* content_changed */
2N/A NULL /* order_changed */
2N/A};
2N/A
2N/A/**
2N/A * \param selection should not be NULL.
2N/A */
2N/Astatic void sp_rect_toolbox_selection_changed(Inkscape::Selection *selection, GObject *tbl)
2N/A{
2N/A int n_selected = 0;
2N/A Inkscape::XML::Node *repr = NULL;
2N/A SPItem *item = NULL;
2N/A
2N/A if ( g_object_get_data( tbl, "repr" ) ) {
2N/A g_object_set_data( tbl, "item", NULL );
2N/A }
2N/A purge_repr_listener( tbl, tbl );
2N/A
2N/A for (GSList const *items = selection->itemList();
2N/A items != NULL;
2N/A items = items->next) {
2N/A if (SP_IS_RECT(reinterpret_cast<SPItem *>(items->data))) {
2N/A n_selected++;
2N/A item = reinterpret_cast<SPItem *>(items->data);
2N/A repr = item->getRepr();
2N/A }
2N/A }
2N/A
2N/A EgeOutputAction* act = EGE_OUTPUT_ACTION( g_object_get_data( tbl, "mode_action" ) );
2N/A
2N/A g_object_set_data( tbl, "single", GINT_TO_POINTER(FALSE) );
2N/A
2N/A if (n_selected == 0) {
2N/A g_object_set( G_OBJECT(act), "label", _("<b>New:</b>"), NULL );
2N/A
2N/A GtkAction* w = GTK_ACTION( g_object_get_data( tbl, "width_action" ) );
2N/A gtk_action_set_sensitive(w, FALSE);
2N/A GtkAction* h = GTK_ACTION( g_object_get_data( tbl, "height_action" ) );
2N/A gtk_action_set_sensitive(h, FALSE);
2N/A
2N/A } else if (n_selected == 1) {
2N/A g_object_set( G_OBJECT(act), "label", _("<b>Change:</b>"), NULL );
2N/A g_object_set_data( tbl, "single", GINT_TO_POINTER(TRUE) );
2N/A
2N/A GtkAction* w = GTK_ACTION( g_object_get_data( tbl, "width_action" ) );
2N/A gtk_action_set_sensitive(w, TRUE);
2N/A GtkAction* h = GTK_ACTION( g_object_get_data( tbl, "height_action" ) );
2N/A gtk_action_set_sensitive(h, TRUE);
2N/A
2N/A if (repr) {
2N/A g_object_set_data( tbl, "repr", repr );
2N/A g_object_set_data( tbl, "item", item );
2N/A Inkscape::GC::anchor(repr);
2N/A sp_repr_add_listener(repr, &rect_tb_repr_events, tbl);
2N/A sp_repr_synthesize_events(repr, &rect_tb_repr_events, tbl);
2N/A }
2N/A } else {
2N/A // FIXME: implement averaging of all parameters for multiple selected
2N/A //gtk_label_set_markup(GTK_LABEL(l), _("<b>Average:</b>"));
2N/A g_object_set( G_OBJECT(act), "label", _("<b>Change:</b>"), NULL );
2N/A sp_rtb_sensitivize( tbl );
2N/A }
2N/A}
2N/A
2N/A
void sp_rect_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObject* holder)
{
EgeAdjustmentAction* eact = 0;
Inkscape::IconSize secondarySize = ToolboxFactory::prefToSize("/toolbox/secondary", 1);
{
EgeOutputAction* act = ege_output_action_new( "RectStateAction", _("<b>New:</b>"), "", 0 );
ege_output_action_set_use_markup( act, TRUE );
gtk_action_group_add_action( mainActions, GTK_ACTION( act ) );
g_object_set_data( holder, "mode_action", act );
}
// rx/ry units menu: create
UnitTracker* tracker = new UnitTracker( SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE );
//tracker->addUnit( SP_UNIT_PERCENT, 0 );
// fixme: add % meaning per cent of the width/height
tracker->setActiveUnit( sp_desktop_namedview(desktop)->doc_units );
g_object_set_data( holder, "tracker", tracker );
/* W */
{
gchar const* labels[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gdouble values[] = {1, 2, 3, 5, 10, 20, 50, 100, 200, 500};
eact = create_adjustment_action( "RectWidthAction",
_("Width"), _("W:"), _("Width of rectangle"),
"/tools/shapes/rect/width", 0,
GTK_WIDGET(desktop->canvas), NULL/*us*/, holder, TRUE, "altx-rect",
0, 1e6, SPIN_STEP, SPIN_PAGE_STEP,
labels, values, G_N_ELEMENTS(labels),
sp_rtb_width_value_changed );
tracker->addAdjustment( ege_adjustment_action_get_adjustment(eact) );
g_object_set_data( holder, "width_action", eact );
gtk_action_set_sensitive( GTK_ACTION(eact), FALSE );
gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
}
/* H */
{
gchar const* labels[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gdouble values[] = {1, 2, 3, 5, 10, 20, 50, 100, 200, 500};
eact = create_adjustment_action( "RectHeightAction",
_("Height"), _("H:"), _("Height of rectangle"),
"/tools/shapes/rect/height", 0,
GTK_WIDGET(desktop->canvas), NULL/*us*/, holder, FALSE, NULL,
0, 1e6, SPIN_STEP, SPIN_PAGE_STEP,
labels, values, G_N_ELEMENTS(labels),
sp_rtb_height_value_changed );
tracker->addAdjustment( ege_adjustment_action_get_adjustment(eact) );
g_object_set_data( holder, "height_action", eact );
gtk_action_set_sensitive( GTK_ACTION(eact), FALSE );
gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
}
/* rx */
{
gchar const* labels[] = {_("not rounded"), 0, 0, 0, 0, 0, 0, 0, 0};
gdouble values[] = {0.5, 1, 2, 3, 5, 10, 20, 50, 100};
eact = create_adjustment_action( "RadiusXAction",
_("Horizontal radius"), _("Rx:"), _("Horizontal radius of rounded corners"),
"/tools/shapes/rect/rx", 0,
GTK_WIDGET(desktop->canvas), NULL/*us*/, holder, FALSE, NULL,
0, 1e6, SPIN_STEP, SPIN_PAGE_STEP,
labels, values, G_N_ELEMENTS(labels),
sp_rtb_rx_value_changed);
tracker->addAdjustment( ege_adjustment_action_get_adjustment(eact) );
gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
}
/* ry */
{
gchar const* labels[] = {_("not rounded"), 0, 0, 0, 0, 0, 0, 0, 0};
gdouble values[] = {0.5, 1, 2, 3, 5, 10, 20, 50, 100};
eact = create_adjustment_action( "RadiusYAction",
_("Vertical radius"), _("Ry:"), _("Vertical radius of rounded corners"),
"/tools/shapes/rect/ry", 0,
GTK_WIDGET(desktop->canvas), NULL/*us*/, holder, FALSE, NULL,
0, 1e6, SPIN_STEP, SPIN_PAGE_STEP,
labels, values, G_N_ELEMENTS(labels),
sp_rtb_ry_value_changed);
tracker->addAdjustment( ege_adjustment_action_get_adjustment(eact) );
gtk_action_group_add_action( mainActions, GTK_ACTION(eact) );
}
// add the units menu
{
GtkAction* act = tracker->createAction( "RectUnitsAction", _("Units"), ("") );
gtk_action_group_add_action( mainActions, act );
}
/* Reset */
{
InkAction* inky = ink_action_new( "RectResetAction",
_("Not rounded"),
_("Make corners sharp"),
INKSCAPE_ICON("rectangle-make-corners-sharp"),
secondarySize );
g_signal_connect_after( G_OBJECT(inky), "activate", G_CALLBACK(sp_rtb_defaults), holder );
gtk_action_group_add_action( mainActions, GTK_ACTION(inky) );
gtk_action_set_sensitive( GTK_ACTION(inky), TRUE );
g_object_set_data( holder, "not_rounded", inky );
}
g_object_set_data( holder, "single", GINT_TO_POINTER(TRUE) );
sp_rtb_sensitivize( holder );
sigc::connection *connection = new sigc::connection(
sp_desktop_selection(desktop)->connectChanged(sigc::bind(sigc::ptr_fun(sp_rect_toolbox_selection_changed), holder))
);
g_signal_connect( holder, "destroy", G_CALLBACK(delete_connection), connection );
g_signal_connect( holder, "destroy", G_CALLBACK(purge_repr_listener), holder );
}
/*
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:fileencoding=utf-8:textwidth=99 :