summaryrefslogtreecommitdiffstats
path: root/src/live_effects/parameter/text.cpp
blob: ea0171dc2ed5b938e66c2570be864b55ae554ef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) Maximilian Albert 2008 <maximilian.albert@gmail.com>
 *
 * Authors:
 *   Maximilian Albert
 *   Johan Engelen
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "text.h"

#include <glibmm/i18n.h>
#include <gtkmm/alignment.h>

#include <2geom/sbasis-geometric.h>

#include "inkscape.h"

#include "display/control/canvas-item-text.h"

#include "live_effects/effect.h"

#include "svg/stringstream.h"
#include "svg/svg.h"

#include "ui/icon-names.h"
#include "ui/widget/registered-widget.h"


namespace Inkscape {

namespace LivePathEffect {

TextParam::TextParam( const Glib::ustring& label, const Glib::ustring& tip,
                      const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
                      Effect* effect, const Glib::ustring default_value )
    : Parameter(label, tip, key, wr, effect)
    , value(default_value)
    , defvalue(default_value)
{
    if (SPDesktop *desktop = SP_ACTIVE_DESKTOP) { // FIXME: we shouldn't use this!
        canvas_text = new Inkscape::CanvasItemText(desktop->getCanvasTemp(), Geom::Point(0, 0), default_value);
    }
}

TextParam::~TextParam()
{
    if (canvas_text) {
        delete canvas_text;
    }
}

void
TextParam::param_set_default()
{
    param_setValue(defvalue);
}

void
TextParam::param_update_default(const gchar * default_value)
{
    defvalue = (Glib::ustring)default_value;
}

// This is a bit silly, we should have an option in the constructor to not create the canvas_text object.
void
TextParam::param_hide_canvas_text()
{
    if (canvas_text) {
        delete canvas_text;
        canvas_text = nullptr;
    }
}

void
TextParam::setPos(Geom::Point pos)
{
    if (canvas_text) {
        canvas_text->set_coord(pos);
    }
}

void
TextParam::setPosAndAnchor(const Geom::Piecewise<Geom::D2<Geom::SBasis> > &pwd2,
                           const double t, const double length, bool /*use_curvature*/)
{
    using namespace Geom;

    Piecewise<D2<SBasis> > pwd2_reparam = arc_length_parametrization(pwd2, 2 , 0.1);
    double t_reparam = pwd2_reparam.cuts.back() * t;
    Point pos = pwd2_reparam.valueAt(t_reparam);
    Point dir = unit_vector(derivative(pwd2_reparam).valueAt(t_reparam));
    Point n = -rot90(dir);
    double angle = Geom::angle_between(dir, Point(1,0));
    if (canvas_text) {
        canvas_text->set_coord(pos + n * length);
        canvas_text->set_anchor(Geom::Point(std::sin(angle), -std::cos(angle)));
    }
}

void
TextParam::setAnchor(double x_value, double y_value)
{
    anchor_x = x_value;
    anchor_y = y_value;
    if (canvas_text) {
        canvas_text->set_anchor(Geom::Point(anchor_x, anchor_y));
    }
}

bool
TextParam::param_readSVGValue(const gchar * strvalue)
{
    param_setValue(strvalue);
    return true;
}

Glib::ustring
TextParam::param_getSVGValue() const
{
    return value;
}

Glib::ustring
TextParam::param_getDefaultSVGValue() const
{
    return defvalue;
}

void 
TextParam::setTextParam(Inkscape::UI::Widget::RegisteredText *rsu) 
{
    Glib::ustring str(rsu->getText());
    param_setValue(str);
    write_to_SVG();
}

Gtk::Widget *
TextParam::param_newWidget()
{
    Inkscape::UI::Widget::RegisteredText *rsu = Gtk::manage(new Inkscape::UI::Widget::RegisteredText(
        param_label, param_tooltip, param_key, *param_wr, param_effect->getRepr(), param_effect->getSPDoc()));
    rsu->setText(value);
    rsu->setProgrammatically = false;
    rsu->set_undo_parameters(_("Change text parameter"), INKSCAPE_ICON("dialog-path-effects"));
    Gtk::Box *text_container = Gtk::manage(new Gtk::Box());
    Gtk::Button *set =  Gtk::manage(new Gtk::Button(Glib::ustring("✔")));
    set->signal_clicked()
    .connect(sigc::bind<Inkscape::UI::Widget::RegisteredText *>(sigc::mem_fun(*this, &TextParam::setTextParam),rsu));
    text_container->pack_start(*rsu, false, false, 2);
    text_container->pack_start(*set, false, false, 2);
    Gtk::Widget *return_widg = dynamic_cast<Gtk::Widget *> (text_container);
    return_widg->set_halign(Gtk::ALIGN_END);
    return return_widg;
}

void
TextParam::param_setValue(const Glib::ustring newvalue)
{
    if (value != newvalue) {
        param_effect->refresh_widgets = true;
    }
    value = newvalue;
    if (canvas_text) {
        canvas_text->set_text(newvalue);
    }
}

} /* namespace LivePathEffect */

} /* namespace Inkscape */

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