Code

fix release mouse crash, add missed disconnects, remove unused callback
[inkscape.git] / src / knot-holder-entity.cpp
1 #define __KNOT_HOLDER_ENTITY_C__
3 /** \file 
4  * KnotHolderEntity definition. 
5  * 
6  * Authors:
7  *   Mitsuru Oka <oka326@parkcity.ne.jp>
8  *   Maximilian Albert <maximilian.albert@gmail.com>
9  *
10  * Copyright (C) 1999-2001 Lauris Kaplinski
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  * Copyright (C) 2001 Mitsuru Oka
13  * Copyright (C) 2004 Monash University
14  * Copyright (C) 2008 Maximilian Albert
15  *
16  * Released under GNU GPL
17  */
19 #include "knotholder.h"
20 #include "sp-item.h"
21 #include "style.h"
22 #include "prefs-utils.h"
23 #include "macros.h"
24 #include <libnr/nr-matrix-ops.h>
25 #include "sp-pattern.h"
28 int KnotHolderEntity::counter = 0;
30 void
31 KnotHolderEntity::create(SPDesktop *desktop, SPItem *item, KnotHolder *parent, const gchar *tip,
32                          SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
33 {
34     knot = sp_knot_new(desktop, tip);
36     this->parent_holder = parent;
37     this->item = item; // TODO: remove the item either from here or from knotholder.cpp
39     my_counter = KnotHolderEntity::counter++;
41     g_object_set(G_OBJECT (knot->item), "shape", shape, NULL);
42     g_object_set(G_OBJECT (knot->item), "mode", mode, NULL);
44     knot->fill [SP_KNOT_STATE_NORMAL] = color;
45     g_object_set (G_OBJECT (knot->item), "fill_color", color, NULL);
47     update_knot();
48     sp_knot_show(knot);
50     _moved_connection = knot->_moved_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_moved_handler));
51     _click_connection = knot->_click_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_clicked_handler));
52     _ungrabbed_connection = knot->_ungrabbed_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_ungrabbed_handler));
53 }
56 KnotHolderEntity::~KnotHolderEntity()
57 {
58     _moved_connection.disconnect();
59     _click_connection.disconnect();
60     _ungrabbed_connection.disconnect();
62     /* unref should call destroy */
63     if (knot) {
64         g_object_unref(knot);
65     } else {
66         // FIXME: This shouldn't occur. Perhaps it is caused by LPE PointParams being knotholder entities, too
67         //        If so, it will likely be fixed with upcoming refactoring efforts.
68         g_return_if_fail(knot);
69     }
70 }
72 void
73 KnotHolderEntity::update_knot()
74 {
75     NR::Matrix const i2d(from_2geom(sp_item_i2d_affine(item)));
77     NR::Point dp(knot_get() * i2d);
79     _moved_connection.block();
80     sp_knot_set_position(knot, &dp, SP_KNOT_STATE_NORMAL); 
81     _moved_connection.unblock();
82 }
84 /* Pattern manipulation */
86 static gdouble sp_pattern_extract_theta(SPPattern *pat, gdouble scale)
87 {
88     gdouble theta = asin(pat->patternTransform[1] / scale);
89     if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
90     return theta;
91 }
93 static gdouble sp_pattern_extract_scale(SPPattern *pat)
94 {
95     gdouble s = pat->patternTransform[1];
96     gdouble c = pat->patternTransform[0];
97     gdouble xscale = sqrt(c * c + s * s);
98     return xscale;
99 }
101 static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
103     return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
106 void
107 PatternKnotHolderEntityXY::knot_set(NR::Point const &p, NR::Point const &origin, guint state)
109     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
111     NR::Point p_snapped = p;
113     if ( state & GDK_CONTROL_MASK ) {
114         if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
115             p_snapped[NR::Y] = origin[NR::Y];
116         } else {
117             p_snapped[NR::X] = origin[NR::X];
118         }
119     }
121     if (state)  {
122         NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
123         sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
124     }
126     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
129 NR::Point
130 PatternKnotHolderEntityXY::knot_get()
132     SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
133     return sp_pattern_extract_trans(pat);
136 NR::Point
137 PatternKnotHolderEntityAngle::knot_get()
139     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
141     gdouble x = (pattern_width(pat)*0.5);
142     gdouble y = 0;
143     NR::Point delta = NR::Point(x,y);
144     gdouble scale = sp_pattern_extract_scale(pat);
145     gdouble theta = sp_pattern_extract_theta(pat, scale);
146     delta = delta * NR::Matrix(NR::rotate(theta))*NR::Matrix(NR::scale(scale,scale));
147     delta = delta + sp_pattern_extract_trans(pat);
148     return delta;
151 void
152 PatternKnotHolderEntityAngle::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint state)
154     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
156     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
158     // get the angle from pattern 0,0 to the cursor pos
159     NR::Point delta = p - sp_pattern_extract_trans(pat);
160     gdouble theta = atan2(delta);
162     if ( state & GDK_CONTROL_MASK ) {
163         theta = sp_round(theta, M_PI/snaps);
164     }
166     // get the scale from the current transform so we can keep it.
167     gdouble scl = sp_pattern_extract_scale(pat);
168     NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
169     NR::Point const t = sp_pattern_extract_trans(pat);
170     rot[4] = t[NR::X];
171     rot[5] = t[NR::Y];
172     sp_item_adjust_pattern(item, rot, true);
173     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
176 void
177 PatternKnotHolderEntityScale::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
179     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
181     // Get the scale from the position of the knotholder,
182     NR::Point d = p - sp_pattern_extract_trans(pat);
183     gdouble s = NR::L2(d);
184     gdouble pat_x = pattern_width(pat) * 0.5;
185     gdouble pat_y = pattern_height(pat) * 0.5;
186     gdouble pat_h = hypot(pat_x, pat_y);
187     gdouble scl = s / pat_h;
189     // get angle from current transform, (need get current scale first to calculate angle)
190     gdouble oldscale = sp_pattern_extract_scale(pat);
191     gdouble theta = sp_pattern_extract_theta(pat,oldscale);
193     NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
194     NR::Point const t = sp_pattern_extract_trans(pat);
195     rot[4] = t[NR::X];
196     rot[5] = t[NR::Y];
197     sp_item_adjust_pattern(item, rot, true);
198     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
202 NR::Point
203 PatternKnotHolderEntityScale::knot_get()
205     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
207     gdouble x = pattern_width(pat)*0.5;
208     gdouble y = pattern_height(pat)*0.5;
209     NR::Point delta = NR::Point(x,y);
210     NR::Matrix a = pat->patternTransform;
211     a[4] = 0;
212     a[5] = 0;
213     delta = delta * a;
214     delta = delta + sp_pattern_extract_trans(pat);
215     return delta;
218 /*
219   Local Variables:
220   mode:c++
221   c-file-style:"stroustrup"
222   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
223   indent-tabs-mode:nil
224   fill-column:99
225   End:
226 */
227 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :