Logo Search packages:      
Sourcecode: inkscape version File versions

grid.cpp

Go to the documentation of this file.
/**
    \file grid.cpp
 
    A plug-in to add a grid creation effect into Inkscape.
*/
/*
 * Authors:
 *   Ted Gould <ted@gould.cx>
 *
 * Copyright (C) 2004-2005 Authors
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include <glibmm/ustring.h>
#include <gtkmm/enums.h>
#include <gtkmm/plug.h>
#include <gtkmm/label.h>
#include <gtkmm/box.h>
#include <gtkmm/adjustment.h>
#include <gtkmm/spinbutton.h>

#include <view.h>
#include <document.h>
#include <desktop.h>
#include <selection.h>
#include <xml/repr.h>
#include <svg/ftos.h>

#include <extension/implementation/implementation.h>
#include <extension/extension.h>
#include <extension/effect.h>

#include <glibmm/i18n.h>

namespace Inkscape {
namespace Extension {
namespace Plugin {

/** \brief  Implementation class of the GIMP gradient plugin.  This mostly
            just creates a namespace for the GIMP gradient plugin today.
*/
00043 class Grid : public Inkscape::Extension::Implementation::Implementation {

public:
    bool load(Inkscape::Extension::Extension *module);
    void effect(Inkscape::Extension::Effect *module, SPView *document);
    Gdk::NativeWindow prefs_effect(Inkscape::Extension::Effect *module, SPView * view);
};

/**
    \brief  A function to allocated anything -- just an example here
    \param  module  Unused
    \return Whether the load was sucessful
*/
bool
00057 Grid::load (Inkscape::Extension::Extension *module)
{
    // std::cout << "Hey, I'm Grid, I'm loading!" << std::endl;
    return TRUE;
}

/**
    \brief  This actually draws the grid.
    \param  module   The effect that was called (unused)
    \param  document What should be edited.
*/
void
00069 Grid::effect (Inkscape::Extension::Effect *module, SPView *document)
{
    SPSelection * selection     = ((SPDesktop *)document)->selection;

    NR::Rect bounding_area = NR::Rect(NR::Point(0,0), NR::Point(100,100));
    if (selection->isEmpty()) {
        /* get page size */
        SPDocument * doc = document->doc;
        bounding_area = NR::Rect(NR::Point(0,0),
                                 NR::Point(sp_document_width(doc),
                                           sp_document_height(doc)));
    } else {
        bounding_area = selection->bounds();
    }


    float xspacing = module->get_param_float("xspacing");
    float yspacing = module->get_param_float("yspacing");
    float line_width = module->get_param_float("lineWidth");
    float xoffset = module->get_param_float("xoffset");
    float yoffset = module->get_param_float("yoffset");

    // std::cout << "Spacing: " << spacing;
    // std::cout << " Line Width: " << line_width;
    // std::cout << " Offset: " << offset << std::endl;

    Glib::ustring path_data;

    for (NR::Point start_point = bounding_area.min();
            start_point[NR::X] + xoffset <= (bounding_area.max())[NR::X];
            start_point[NR::X] += xspacing) {
        NR::Point end_point = start_point;
        end_point[NR::Y] = (bounding_area.max())[NR::Y];
        gchar floatstring[64];

        path_data += "M ";
        sprintf(floatstring, "%f", start_point[NR::X] + xoffset);
        path_data += floatstring;
        path_data += " ";
        sprintf(floatstring, "%f", start_point[NR::Y]);
        path_data += floatstring;
        path_data += " L ";
        sprintf(floatstring, "%f", end_point[NR::X] + xoffset);
        path_data += floatstring;
        path_data += " ";
        sprintf(floatstring, "%f", end_point[NR::Y]);
        path_data += floatstring;
        path_data += " ";
    }

    for (NR::Point start_point = bounding_area.min();
            start_point[NR::Y] + yoffset <= (bounding_area.max())[NR::Y];
            start_point[NR::Y] += yspacing) {
        NR::Point end_point = start_point;
        end_point[NR::X] = (bounding_area.max())[NR::X];
        gchar floatstring[64];

        path_data += "M ";
        sprintf(floatstring, "%f", start_point[NR::X]);
        path_data += floatstring;
        path_data += " ";
        sprintf(floatstring, "%f", start_point[NR::Y] + yoffset);
        path_data += floatstring;
        path_data += " L ";
        sprintf(floatstring, "%f", end_point[NR::X]);
        path_data += floatstring;
        path_data += " ";
        sprintf(floatstring, "%f", end_point[NR::Y] + yoffset);
        path_data += floatstring;
        path_data += " ";
    }

    // std::cout << "Path Data: " << path_data << std::endl;

    SPRepr * current_layer = SP_OBJECT_REPR(((SPDesktop *)document)->currentLayer());
    SPRepr * path = sp_repr_new("svg:path");

    sp_repr_set_attr(path, "d", path_data.c_str());

    Glib::ustring style("fill:none;fill-opacity:0.75000000;fill-rule:evenodd;stroke:#000000;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000");
    style += ";stroke-width:";
    gchar floatstring[64];
    sprintf(floatstring, "%f", line_width);
    style += floatstring;
    style += "pt";
    sp_repr_set_attr(path, "style", style.c_str());

    // Glib::ustring transform("scale(1.25 1.25)");
    // sp_repr_set_attr(path, "transform", transform.c_str());

    sp_repr_append_child(current_layer, path);

    return;
}

/** \brief  A class to make an adjustment that uses Extension params */
00165 class PrefAdjustment : public Gtk::Adjustment {
    /** Extension that this relates to */
00167     Inkscape::Extension::Extension * _ext;
    /** The string which represents the parameter */
00169     char * _pref;
public:
    /** \brief  Make the adjustment using an extension and the string
                describing the parameter. */
00173     PrefAdjustment(Inkscape::Extension::Extension * ext, char * pref) :
            Gtk::Adjustment(0.0, 0.0, 10.0, 0.1), _ext(ext), _pref(pref) {
        this->set_value(_ext->get_param_float(_pref)); 
        this->signal_value_changed().connect(sigc::mem_fun(this, &PrefAdjustment::val_changed));
        return;
    };

    void val_changed (void);
}; /* class PrefAdjustment */

/** \brief  A function to respond to the value_changed signal from the
            adjustment.

    This function just grabs the value from the adjustment and writes
    it to the parameter.  Very simple, but yet beautiful.
*/
void
00190 PrefAdjustment::val_changed (void)
{
    // std::cout << "Value Changed to: " << this->get_value() << std::endl;
    _ext->set_param_float(_pref, this->get_value());
    return;
}

/** \brief  A function to get the prefences for the grid
    \param  moudule  Module which holds the params
    \param  view     Unused today - may get style information in the future.

    This function builds a VBox, and puts it into a Gtk::Plug.  This way
    the native window pointer can be pulled out and returned up to be
    stuck in a Gtk::Socket further up the call stack.  In the Vbox there
    are several Hboxes, each one being a spin button to adjust a particular
    parameter.  The names of the parameters and the labels are all
    stored in the arrays in the middle of the function.  This makes
    the code very generic.  This will probably have to change if someone
    wants to make this dialog look nicer.
*/
Gdk::NativeWindow
00211 Grid::prefs_effect(Inkscape::Extension::Effect *module, SPView * view)
{
    Gtk::Plug * plug;

    plug = new Gtk::Plug((unsigned int)0);

    Gtk::VBox * vbox;
    vbox = new Gtk::VBox();

#define NUM_PREFERENCES  5
    char * labels[NUM_PREFERENCES] = {N_("Line Width"),
                       N_("Horizontal Spacing"),
                       N_("Vertical Spacing"),
                       N_("Horizontal Offset"),
                       N_("Vertical Offset")};
    char * prefs[NUM_PREFERENCES] =  {"lineWidth",
                       "xspacing",
                       "yspacing",
                       "xoffset",
                       "yoffset"};

    for (int i = 0; i < NUM_PREFERENCES; i++) {
        Gtk::HBox * hbox = new Gtk::HBox();

        Gtk::Label * label = new Gtk::Label(_(labels[i]), Gtk::ALIGN_LEFT);
        label->show();
        hbox->pack_start(*label, true, true);

        PrefAdjustment * pref = new PrefAdjustment(module, prefs[i]);

        Gtk::SpinButton * spin = new Gtk::SpinButton(*pref, 0.1, 1);
        spin->show();
        hbox->pack_start(*spin, false, false);

        hbox->show();

        vbox->pack_start(*hbox, true, true);
    }
#undef NUM_PREFERENCES

    vbox->show();
    plug->add(*vbox);
    plug->show();

    return plug->get_id();
}


}; /* namespace Plugin */
}; /* namespace Extension */
00261 }; /* namespace Inkscape */

#include <extension/implementation/plugin-link.h>

/**
    \brief  This is the actual implementation here.  This is done as
            the more generic Implemetnation object so that this code
            can be stolen by someone else more easily.
*/
Inkscape::Extension::Implementation::Implementation * myplug;

/**
    \brief  A function with a C prototype to link back into Inkscape.  This
            function allocated a \c GimpGrad and then calls it's load.
*/
int
00277 load (inkscape_extension * in_ext)
{
    myplug = new Inkscape::Extension::Plugin::Grid();

    return myplug->load(reinterpret_cast<Inkscape::Extension::Extension *>(in_ext));
}

/**
    \brief  A function with a C prototype to link back to Inkscape.  This
            function called the \c GimpGrad unload function and then deletes
            the object.
*/
void
00290 unload (inkscape_extension * in_ext)
{
    myplug->unload(reinterpret_cast<Inkscape::Extension::Extension *>(in_ext));
    delete myplug;
    return;
}

void
effect (inkscape_extension * in_ext, SPView * view)
{
    return myplug->effect(reinterpret_cast<Inkscape::Extension::Effect *>(in_ext), view);
}

unsigned int
prefs_effect (inkscape_extension * in_ext, SPView * view)
{
    return (unsigned int)myplug->prefs_effect(reinterpret_cast<Inkscape::Extension::Effect *>(in_ext), view);
}

/**
    \brief  A structure holding all the functions that Inkscape uses to
            communicate with this plugin.  These are all C-prototype based
            for easy compliation.
*/
00314 inkscape_plugin_function_table INKSCAPE_PLUGIN_NAME = {
    INKSCAPE_PLUGIN_VERSION,
    load,
    unload,
    NULL,
    NULL,
    effect,
    prefs_effect
};

/*
  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 :

Generated by  Doxygen 1.6.0   Back to index