1 #define __SP_GRADIENT_CONTEXT_C__
3 /*
4 * Gradient drawing and editing tool
5 *
6 * Authors:
7 * bulia byak <buliabyak@users.sf.net>
8 *
9 * Copyright (C) 2005 Authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
19 #include <gdk/gdkkeysyms.h>
21 #include "macros.h"
22 #include "document.h"
23 #include "selection.h"
24 #include "desktop.h"
25 #include "desktop-handles.h"
26 #include "message-context.h"
27 #include "message-stack.h"
28 #include "pixmaps/cursor-gradient.xpm"
29 #include "gradient-context.h"
30 #include <glibmm/i18n.h>
31 #include "prefs-utils.h"
32 #include "gradient-drag.h"
33 #include "gradient-chemistry.h"
34 #include "xml/repr.h"
35 #include "sp-item.h"
37 static void sp_gradient_context_class_init(SPGradientContextClass *klass);
38 static void sp_gradient_context_init(SPGradientContext *gr_context);
39 static void sp_gradient_context_dispose(GObject *object);
41 static void sp_gradient_context_setup(SPEventContext *ec);
43 static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event);
45 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime);
47 static SPEventContextClass *parent_class;
50 GtkType sp_gradient_context_get_type()
51 {
52 static GType type = 0;
53 if (!type) {
54 GTypeInfo info = {
55 sizeof(SPGradientContextClass),
56 NULL, NULL,
57 (GClassInitFunc) sp_gradient_context_class_init,
58 NULL, NULL,
59 sizeof(SPGradientContext),
60 4,
61 (GInstanceInitFunc) sp_gradient_context_init,
62 NULL, /* value_table */
63 };
64 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPGradientContext", &info, (GTypeFlags) 0);
65 }
66 return type;
67 }
69 static void sp_gradient_context_class_init(SPGradientContextClass *klass)
70 {
71 GObjectClass *object_class = (GObjectClass *) klass;
72 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
74 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
76 object_class->dispose = sp_gradient_context_dispose;
78 event_context_class->setup = sp_gradient_context_setup;
79 event_context_class->root_handler = sp_gradient_context_root_handler;
80 }
82 static void sp_gradient_context_init(SPGradientContext *gr_context)
83 {
84 SPEventContext *event_context = SP_EVENT_CONTEXT(gr_context);
86 event_context->cursor_shape = cursor_gradient_xpm;
87 event_context->hot_x = 4;
88 event_context->hot_y = 4;
89 event_context->xp = 0;
90 event_context->yp = 0;
91 event_context->tolerance = 0;
92 event_context->within_tolerance = false;
93 event_context->item_to_select = NULL;
94 }
96 static void sp_gradient_context_dispose(GObject *object)
97 {
98 SPGradientContext *rc = SP_GRADIENT_CONTEXT(object);
99 SPEventContext *ec = SP_EVENT_CONTEXT(object);
101 ec->enableGrDrag(false);
103 if (rc->_message_context) {
104 delete rc->_message_context;
105 }
107 G_OBJECT_CLASS(parent_class)->dispose(object);
108 }
110 static void sp_gradient_context_setup(SPEventContext *ec)
111 {
112 SPGradientContext *rc = SP_GRADIENT_CONTEXT(ec);
114 if (((SPEventContextClass *) parent_class)->setup) {
115 ((SPEventContextClass *) parent_class)->setup(ec);
116 }
118 if (prefs_get_int_attribute("tools.gradient", "selcue", 1) != 0) {
119 ec->enableSelectionCue();
120 }
122 ec->enableGrDrag();
124 rc->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(ec->desktop));
125 }
127 void
128 sp_gradient_context_select_next (SPEventContext *event_context)
129 {
130 GrDrag *drag = event_context->_grdrag;
131 g_assert (drag);
133 drag->select_next();
134 }
136 void
137 sp_gradient_context_select_prev (SPEventContext *event_context)
138 {
139 GrDrag *drag = event_context->_grdrag;
140 g_assert (drag);
142 drag->select_prev();
143 }
145 static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
146 {
147 static bool dragging;
149 SPDesktop *desktop = event_context->desktop;
150 Inkscape::Selection *selection = sp_desktop_selection (desktop);
152 SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
154 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
155 double const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
157 GrDrag *drag = event_context->_grdrag;
158 g_assert (drag);
160 gint ret = FALSE;
161 switch (event->type) {
162 case GDK_2BUTTON_PRESS:
163 if ( event->button.button == 1 ) {
164 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
165 SPItem *item = SP_ITEM(i->data);
166 SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR);
167 guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
169 SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, SP_OBJECT (item), new_fill);
171 SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
172 sp_gradient_reset_to_userspace(priv, item);
173 }
175 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
176 _("Create default gradient"));
178 ret = TRUE;
179 }
180 break;
181 case GDK_BUTTON_PRESS:
182 if ( event->button.button == 1 ) {
183 NR::Point const button_w(event->button.x, event->button.y);
185 // save drag origin
186 event_context->xp = (gint) button_w[NR::X];
187 event_context->yp = (gint) button_w[NR::Y];
188 event_context->within_tolerance = true;
190 // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
191 // enable Ctrl+doubleclick of exactly the selected item(s)
192 if (!(event->button.state & GDK_CONTROL_MASK))
193 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
195 dragging = true;
196 /* Position center */
197 NR::Point const button_dt = desktop->w2d(button_w);
198 /* Snap center to nearest magnetic point */
200 rc->origin = button_dt;
202 ret = TRUE;
203 }
204 break;
205 case GDK_MOTION_NOTIFY:
206 if ( dragging
207 && ( event->motion.state & GDK_BUTTON1_MASK ) )
208 {
209 if ( event_context->within_tolerance
210 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
211 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
212 break; // do not drag if we're within tolerance from origin
213 }
214 // Once the user has moved farther than tolerance from the original location
215 // (indicating they intend to draw, not click), then always process the
216 // motion notify coordinates as given (no snapping back to origin)
217 event_context->within_tolerance = false;
219 NR::Point const motion_w(event->motion.x,
220 event->motion.y);
221 NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
223 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
225 ret = TRUE;
226 }
227 break;
228 case GDK_BUTTON_RELEASE:
229 event_context->xp = event_context->yp = 0;
230 if ( event->button.button == 1 ) {
231 dragging = false;
233 // unless clicked with Ctrl (to enable Ctrl+doubleclick)
234 if (event->button.state & GDK_CONTROL_MASK) {
235 ret = TRUE;
236 break;
237 }
239 if (!event_context->within_tolerance) {
240 // we've been dragging, do nothing (grdrag handles that)
241 } else if (event_context->item_to_select) {
242 // no dragging, select clicked item if any
243 if (event->button.state & GDK_SHIFT_MASK) {
244 selection->toggle(event_context->item_to_select);
245 } else {
246 selection->set(event_context->item_to_select);
247 }
248 } else {
249 // click in an empty space; do the same as Esc
250 if (drag->selected) {
251 drag->setSelected (NULL);
252 } else {
253 selection->clear();
254 }
255 }
257 event_context->item_to_select = NULL;
258 ret = TRUE;
259 }
260 break;
261 case GDK_KEY_PRESS:
262 switch (get_group0_keyval (&event->key)) {
263 case GDK_Alt_L:
264 case GDK_Alt_R:
265 case GDK_Control_L:
266 case GDK_Control_R:
267 case GDK_Shift_L:
268 case GDK_Shift_R:
269 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
270 case GDK_Meta_R:
271 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
272 _("<b>Ctrl</b>: snap gradient angle"),
273 _("<b>Shift</b>: draw gradient around the starting point"),
274 NULL);
275 break;
277 case GDK_x:
278 case GDK_X:
279 if (MOD__ALT_ONLY) {
280 desktop->setToolboxFocusTo ("altx-grad");
281 ret = TRUE;
282 }
283 break;
285 case GDK_Escape:
286 if (drag->selected) {
287 drag->setSelected (NULL);
288 } else {
289 selection->clear();
290 }
291 ret = TRUE;
292 //TODO: make dragging escapable by Esc
293 break;
295 case GDK_Left: // move handle left
296 case GDK_KP_Left:
297 case GDK_KP_4:
298 if (!MOD__CTRL) { // not ctrl
299 if (MOD__ALT) { // alt
300 if (MOD__SHIFT) drag->selected_move_screen(-10, 0); // shift
301 else drag->selected_move_screen(-1, 0); // no shift
302 }
303 else { // no alt
304 if (MOD__SHIFT) drag->selected_move(-10*nudge, 0); // shift
305 else drag->selected_move(-nudge, 0); // no shift
306 }
307 ret = TRUE;
308 }
309 break;
310 case GDK_Up: // move handle up
311 case GDK_KP_Up:
312 case GDK_KP_8:
313 if (!MOD__CTRL) { // not ctrl
314 if (MOD__ALT) { // alt
315 if (MOD__SHIFT) drag->selected_move_screen(0, 10); // shift
316 else drag->selected_move_screen(0, 1); // no shift
317 }
318 else { // no alt
319 if (MOD__SHIFT) drag->selected_move(0, 10*nudge); // shift
320 else drag->selected_move(0, nudge); // no shift
321 }
322 ret = TRUE;
323 }
324 break;
325 case GDK_Right: // move handle right
326 case GDK_KP_Right:
327 case GDK_KP_6:
328 if (!MOD__CTRL) { // not ctrl
329 if (MOD__ALT) { // alt
330 if (MOD__SHIFT) drag->selected_move_screen(10, 0); // shift
331 else drag->selected_move_screen(1, 0); // no shift
332 }
333 else { // no alt
334 if (MOD__SHIFT) drag->selected_move(10*nudge, 0); // shift
335 else drag->selected_move(nudge, 0); // no shift
336 }
337 ret = TRUE;
338 }
339 break;
340 case GDK_Down: // move handle down
341 case GDK_KP_Down:
342 case GDK_KP_2:
343 if (!MOD__CTRL) { // not ctrl
344 if (MOD__ALT) { // alt
345 if (MOD__SHIFT) drag->selected_move_screen(0, -10); // shift
346 else drag->selected_move_screen(0, -1); // no shift
347 }
348 else { // no alt
349 if (MOD__SHIFT) drag->selected_move(0, -10*nudge); // shift
350 else drag->selected_move(0, -nudge); // no shift
351 }
352 ret = TRUE;
353 }
354 break;
355 case GDK_r:
356 case GDK_R:
357 if (MOD__SHIFT_ONLY) {
358 // First try selected dragger
359 if (drag && drag->selected) {
360 drag->selected_reverse_vector();
361 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
362 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
363 sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
364 sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
365 }
366 }
367 // we did an undoable action
368 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
369 _("Invert gradient"));
370 ret = TRUE;
371 }
372 break;
373 default:
374 break;
375 }
376 break;
377 case GDK_KEY_RELEASE:
378 switch (get_group0_keyval (&event->key)) {
379 case GDK_Alt_L:
380 case GDK_Alt_R:
381 case GDK_Control_L:
382 case GDK_Control_R:
383 case GDK_Shift_L:
384 case GDK_Shift_R:
385 case GDK_Meta_L: // Meta is when you press Shift+Alt
386 case GDK_Meta_R:
387 event_context->defaultMessageContext()->clear();
388 break;
389 default:
390 break;
391 }
392 break;
393 default:
394 break;
395 }
397 if (!ret) {
398 if (((SPEventContextClass *) parent_class)->root_handler) {
399 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
400 }
401 }
403 return ret;
404 }
406 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime)
407 {
408 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
409 Inkscape::Selection *selection = sp_desktop_selection(desktop);
410 SPDocument *document = sp_desktop_document(desktop);
411 SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
413 if (!selection->isEmpty()) {
414 int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
415 int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
417 SPGradient *vector;
418 if (ec->item_to_select) {
419 vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
420 } else {
421 vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(selection->itemList()->data), fill_or_stroke);
422 }
424 // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
425 SPCSSAttr *css = sp_repr_css_attr_new();
426 sp_repr_css_set_property(css, "fill-opacity", "1.0");
428 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
430 //FIXME: see above
431 sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
433 sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
435 if (type == SP_GRADIENT_TYPE_LINEAR) {
436 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_P1, rc.origin, fill_or_stroke, true, false);
437 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_P2, pt, fill_or_stroke, true, false);
438 } else if (type == SP_GRADIENT_TYPE_RADIAL) {
439 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, rc.origin, fill_or_stroke, true, false);
440 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, pt, fill_or_stroke, true, false);
441 }
442 SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
443 }
444 if (ec->_grdrag) {
445 ec->_grdrag->updateDraggers();
446 // prevent regenerating draggers by selection modified signal, which sometimes
447 // comes too late and thus destroys the knot which we will now grab:
448 ec->_grdrag->local_change = true;
449 // give the grab out-of-bounds values of xp/yp because we're already dragging
450 // and therefore are already out of tolerance
451 ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
452 type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_P2 : POINT_RG_R1,
453 fill_or_stroke, 99999, 99999, etime);
454 }
455 // We did an undoable action, but sp_document_done will be called by the knot when released
457 // status text; we do not track coords because this branch is run once, not all the time
458 // during drag
459 int n_objects = g_slist_length((GSList *) selection->itemList());
460 rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
461 ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
462 "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
463 n_objects);
464 } else {
465 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
466 }
467 }
470 /*
471 Local Variables:
472 mode:c++
473 c-file-style:"stroustrup"
474 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
475 indent-tabs-mode:nil
476 fill-column:99
477 End:
478 */
479 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :