1 #define __SP_GRADIENT_CONTEXT_C__
3 /*
4 * Gradient drawing and editing tool
5 *
6 * Authors:
7 * bulia byak <buliabyak@users.sf.net>
8 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9 *
10 * Copyright (C) 2007 Johan Engelen
11 * Copyright (C) 2005 Authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
21 #include <gdk/gdkkeysyms.h>
23 #include "macros.h"
24 #include "document.h"
25 #include "selection.h"
26 #include "desktop.h"
27 #include "desktop-handles.h"
28 #include "message-context.h"
29 #include "message-stack.h"
30 #include "pixmaps/cursor-gradient.xpm"
31 #include "pixmaps/cursor-gradient-add.xpm"
32 #include "pixmaps/cursor-gradient-delete.xpm"
33 #include "gradient-context.h"
34 #include "gradient-chemistry.h"
35 #include <glibmm/i18n.h>
36 #include "prefs-utils.h"
37 #include "gradient-drag.h"
38 #include "gradient-chemistry.h"
39 #include "xml/repr.h"
40 #include "sp-item.h"
41 #include "display/sp-ctrlline.h"
42 #include "sp-linear-gradient.h"
43 #include "sp-radial-gradient.h"
44 #include "sp-stop.h"
45 #include "svg/css-ostringstream.h"
46 #include "svg/svg-color.h"
47 #include "snap.h"
48 #include "sp-namedview.h"
52 static void sp_gradient_context_class_init(SPGradientContextClass *klass);
53 static void sp_gradient_context_init(SPGradientContext *gr_context);
54 static void sp_gradient_context_dispose(GObject *object);
56 static void sp_gradient_context_setup(SPEventContext *ec);
58 static gint sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event);
60 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime);
62 static SPEventContextClass *parent_class;
65 GtkType sp_gradient_context_get_type()
66 {
67 static GType type = 0;
68 if (!type) {
69 GTypeInfo info = {
70 sizeof(SPGradientContextClass),
71 NULL, NULL,
72 (GClassInitFunc) sp_gradient_context_class_init,
73 NULL, NULL,
74 sizeof(SPGradientContext),
75 4,
76 (GInstanceInitFunc) sp_gradient_context_init,
77 NULL, /* value_table */
78 };
79 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPGradientContext", &info, (GTypeFlags) 0);
80 }
81 return type;
82 }
84 static void sp_gradient_context_class_init(SPGradientContextClass *klass)
85 {
86 GObjectClass *object_class = (GObjectClass *) klass;
87 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
89 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
91 object_class->dispose = sp_gradient_context_dispose;
93 event_context_class->setup = sp_gradient_context_setup;
94 event_context_class->root_handler = sp_gradient_context_root_handler;
95 }
97 static void sp_gradient_context_init(SPGradientContext *gr_context)
98 {
99 SPEventContext *event_context = SP_EVENT_CONTEXT(gr_context);
101 gr_context->cursor_addnode = false;
102 event_context->cursor_shape = cursor_gradient_xpm;
103 event_context->hot_x = 4;
104 event_context->hot_y = 4;
105 event_context->xp = 0;
106 event_context->yp = 0;
107 event_context->tolerance = 6;
108 event_context->within_tolerance = false;
109 event_context->item_to_select = NULL;
110 }
112 static void sp_gradient_context_dispose(GObject *object)
113 {
114 SPGradientContext *rc = SP_GRADIENT_CONTEXT(object);
115 SPEventContext *ec = SP_EVENT_CONTEXT(object);
117 ec->enableGrDrag(false);
119 if (rc->_message_context) {
120 delete rc->_message_context;
121 }
123 G_OBJECT_CLASS(parent_class)->dispose(object);
124 }
126 static void sp_gradient_context_setup(SPEventContext *ec)
127 {
128 SPGradientContext *rc = SP_GRADIENT_CONTEXT(ec);
130 if (((SPEventContextClass *) parent_class)->setup) {
131 ((SPEventContextClass *) parent_class)->setup(ec);
132 }
134 if (prefs_get_int_attribute("tools.gradient", "selcue", 1) != 0) {
135 ec->enableSelectionCue();
136 }
138 ec->enableGrDrag();
140 rc->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(ec->desktop));
141 }
143 void
144 sp_gradient_context_select_next (SPEventContext *event_context)
145 {
146 GrDrag *drag = event_context->_grdrag;
147 g_assert (drag);
149 drag->select_next();
150 }
152 void
153 sp_gradient_context_select_prev (SPEventContext *event_context)
154 {
155 GrDrag *drag = event_context->_grdrag;
156 g_assert (drag);
158 drag->select_prev();
159 }
161 // FIXME: make global function in libnr or somewhere.
162 static NR::Point
163 snap_vector_midpoint (NR::Point p, NR::Point begin, NR::Point end)
164 {
165 double length = NR::L2(end - begin);
166 NR::Point be = (end - begin) / length;
167 double r = NR::dot(p - begin, be);
169 if (r < 0.0) return begin;
170 if (r > length) return end;
172 return (begin + r * be);
173 }
175 static bool
176 sp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, NR::Point event_p)
177 {
178 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
180 //Translate mouse point into proper coord system
181 rc->mousepoint_doc = desktop->w2d(event_p);
183 SPCtrlLine* line = SP_CTRLLINE(item);
185 NR::Point nearest = snap_vector_midpoint (rc->mousepoint_doc, line->s, line->e);
186 double dist_screen = NR::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
188 double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
190 bool close = (dist_screen < tolerance);
192 return close;
193 }
195 // Fixme : must be able to put this in a general file.
196 static guint32
197 average_color (guint32 c1, guint32 c2, gdouble p = 0.5)
198 {
199 guint32 r = (guint32) (SP_RGBA32_R_U (c1) * (1 - p) + SP_RGBA32_R_U (c2) * p);
200 guint32 g = (guint32) (SP_RGBA32_G_U (c1) * (1 - p) + SP_RGBA32_G_U (c2) * p);
201 guint32 b = (guint32) (SP_RGBA32_B_U (c1) * (1 - p) + SP_RGBA32_B_U (c2) * p);
202 guint32 a = (guint32) (SP_RGBA32_A_U (c1) * (1 - p) + SP_RGBA32_A_U (c2) * p);
204 return SP_RGBA32_U_COMPOSE (r, g, b, a);
205 }
207 static double
208 get_offset_between_points (NR::Point p, NR::Point begin, NR::Point end)
209 {
210 double length = NR::L2(end - begin);
211 NR::Point be = (end - begin) / length;
212 double r = NR::dot(p - begin, be);
214 if (r < 0.0) return 0.0;
215 if (r > length) return 1.0;
217 return (r / length);
218 }
220 static void
221 sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item, NR::Point mouse_p, guint32 etime)
222 {
223 // item is the selected item. mouse_p the location in doc coordinates of where to add the stop
225 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
226 SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
228 double tolerance = (double) ec->tolerance;
230 gfloat offset; // type of SPStop.offset = gfloat
231 SPGradient *gradient;
232 bool fill_or_stroke = true;
233 bool r1_knot = false;
235 bool addknot = false;
236 do {
237 gradient = sp_item_gradient (item, fill_or_stroke);
238 if (SP_IS_LINEARGRADIENT(gradient)) {
239 NR::Point begin = sp_item_gradient_get_coords(item, POINT_LG_BEGIN, 0, fill_or_stroke);
240 NR::Point end = sp_item_gradient_get_coords(item, POINT_LG_END, 0, fill_or_stroke);
242 NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end);
243 double dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
244 if ( dist_screen < tolerance ) {
245 // add the knot
246 offset = get_offset_between_points(nearest, begin, end);
247 addknot = true;
248 break; // break out of the while loop: add only one knot
249 }
250 } else if (SP_IS_RADIALGRADIENT(gradient)) {
251 NR::Point begin = sp_item_gradient_get_coords(item, POINT_RG_CENTER, 0, fill_or_stroke);
252 NR::Point end = sp_item_gradient_get_coords(item, POINT_RG_R1, 0, fill_or_stroke);
253 NR::Point nearest = snap_vector_midpoint (mouse_p, begin, end);
254 double dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
255 if ( dist_screen < tolerance ) {
256 offset = get_offset_between_points(nearest, begin, end);
257 addknot = true;
258 r1_knot = true;
259 break; // break out of the while loop: add only one knot
260 }
262 end = sp_item_gradient_get_coords(item, POINT_RG_R2, 0, fill_or_stroke);
263 nearest = snap_vector_midpoint (mouse_p, begin, end);
264 dist_screen = NR::L2 (mouse_p - nearest) * desktop->current_zoom();
265 if ( dist_screen < tolerance ) {
266 offset = get_offset_between_points(nearest, begin, end);
267 addknot = true;
268 r1_knot = false;
269 break; // break out of the while loop: add only one knot
270 }
271 }
272 fill_or_stroke = !fill_or_stroke;
273 } while (!fill_or_stroke && !addknot) ;
275 if (addknot) {
276 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
277 SPStop* prev_stop = sp_first_stop(vector);
278 SPStop* next_stop = sp_next_stop(prev_stop);
279 while ( (next_stop) && (next_stop->offset < offset) ) {
280 prev_stop = next_stop;
281 next_stop = sp_next_stop(next_stop);
282 }
283 if (!next_stop) {
284 // logical error: the endstop should have offset 1 and should always be more than this offset here
285 return;
286 }
288 Inkscape::XML::Node *new_stop_repr = NULL;
289 new_stop_repr = SP_OBJECT_REPR(prev_stop)->duplicate(SP_OBJECT_REPR(vector)->document());
290 SP_OBJECT_REPR(vector)->addChild(new_stop_repr, SP_OBJECT_REPR(prev_stop));
292 SPStop *newstop = (SPStop *) SP_OBJECT_DOCUMENT(vector)->getObjectByRepr(new_stop_repr);
293 newstop->offset = offset;
294 sp_repr_set_css_double( SP_OBJECT_REPR(newstop), "offset", (double)offset);
295 guint32 const c1 = sp_stop_get_rgba32(prev_stop);
296 guint32 const c2 = sp_stop_get_rgba32(next_stop);
297 guint32 cnew = average_color (c1, c2, (offset - prev_stop->offset) / (next_stop->offset - prev_stop->offset));
298 Inkscape::CSSOStringStream os;
299 gchar c[64];
300 sp_svg_write_color (c, 64, cnew);
301 gdouble opacity = (gdouble) SP_RGBA32_A_F (cnew);
302 os << "stop-color:" << c << ";stop-opacity:" << opacity <<";";
303 SP_OBJECT_REPR (newstop)->setAttribute("style", os.str().c_str());
306 Inkscape::GC::release(new_stop_repr);
307 sp_document_done (SP_OBJECT_DOCUMENT (vector), SP_VERB_CONTEXT_GRADIENT,
308 _("Add gradient stop"));
310 ec->_grdrag->updateDraggers();
311 sp_gradient_ensure_vector (gradient);
313 if (vector->has_stops) {
314 int i = 0;
315 for ( SPObject *ochild = sp_object_first_child (SP_OBJECT(vector)) ; ochild != NULL ; ochild = SP_OBJECT_NEXT(ochild) ) {
316 if (SP_IS_STOP (ochild)) {
317 if ( SP_STOP(ochild) == newstop ) {
318 break;
319 } else {
320 i++;
321 }
322 }
323 }
325 gradient = sp_item_gradient (item, fill_or_stroke);
326 GrPointType pointtype = POINT_G_INVALID;
327 if (SP_IS_LINEARGRADIENT(gradient)) {
328 pointtype = POINT_LG_MID;
329 } else if (SP_IS_RADIALGRADIENT(gradient)) {
330 pointtype = r1_knot ? POINT_RG_MID1 : POINT_RG_MID2;
331 }
332 GrDragger *dragger = SP_EVENT_CONTEXT(rc)->_grdrag->getDraggerFor (item, pointtype, i, fill_or_stroke);
333 if (dragger && (etime == 0) ) {
334 ec->_grdrag->setSelected (dragger);
335 } else {
336 ec->_grdrag->grabKnot (item,
337 pointtype,
338 i,
339 fill_or_stroke, 99999, 99999, etime);
340 }
341 ec->_grdrag->local_change = true;
343 }
344 }
345 }
348 static gint
349 sp_gradient_context_root_handler(SPEventContext *event_context, GdkEvent *event)
350 {
351 static bool dragging;
353 SPDesktop *desktop = event_context->desktop;
354 Inkscape::Selection *selection = sp_desktop_selection (desktop);
356 SPGradientContext *rc = SP_GRADIENT_CONTEXT(event_context);
358 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
359 double const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
361 GrDrag *drag = event_context->_grdrag;
362 g_assert (drag);
364 gint ret = FALSE;
365 switch (event->type) {
366 case GDK_2BUTTON_PRESS:
367 if ( event->button.button == 1 ) {
368 bool over_line = false;
369 SPCtrlLine *line = NULL;
370 if (drag->lines) {
371 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
372 line = (SPCtrlLine*) l->data;
373 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
374 }
375 }
376 if (over_line) {
377 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
378 } else {
379 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
380 SPItem *item = SP_ITEM(i->data);
381 SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR);
382 guint new_fill = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
384 SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, SP_OBJECT (item), new_fill);
386 SPGradient *priv = sp_item_set_gradient(item, vector, new_type, new_fill);
387 sp_gradient_reset_to_userspace(priv, item);
388 }
390 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
391 _("Create default gradient"));
392 }
393 ret = TRUE;
394 }
395 break;
396 case GDK_BUTTON_PRESS:
397 if ( event->button.button == 1 && !event_context->space_panning ) {
398 NR::Point const button_w(event->button.x, event->button.y);
400 // save drag origin
401 event_context->xp = (gint) button_w[NR::X];
402 event_context->yp = (gint) button_w[NR::Y];
403 event_context->within_tolerance = true;
405 // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
406 // enable Ctrl+doubleclick of exactly the selected item(s)
407 if (!(event->button.state & GDK_CONTROL_MASK))
408 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
410 dragging = true;
411 /* Position center */
412 NR::Point const button_dt = desktop->w2d(button_w);
413 /* Snap center to nearest magnetic point */
415 SnapManager const &m = desktop->namedview->snap_manager;
416 rc->origin = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, button_dt, NULL).getPoint();
418 ret = TRUE;
419 }
420 break;
421 case GDK_MOTION_NOTIFY:
422 if ( dragging
423 && ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning )
424 {
425 if ( event_context->within_tolerance
426 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
427 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
428 break; // do not drag if we're within tolerance from origin
429 }
430 // Once the user has moved farther than tolerance from the original location
431 // (indicating they intend to draw, not click), then always process the
432 // motion notify coordinates as given (no snapping back to origin)
433 event_context->within_tolerance = false;
435 NR::Point const motion_w(event->motion.x,
436 event->motion.y);
437 NR::Point const motion_dt = event_context->desktop->w2d(motion_w);
439 sp_gradient_drag(*rc, motion_dt, event->motion.state, event->motion.time);
441 ret = TRUE;
442 } else {
443 bool over_line = false;
444 if (drag->lines) {
445 for (GSList *l = drag->lines; l != NULL; l = l->next) {
446 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
447 }
448 }
450 if (rc->cursor_addnode && !over_line) {
451 event_context->cursor_shape = cursor_gradient_xpm;
452 sp_event_context_update_cursor(event_context);
453 rc->cursor_addnode = false;
454 } else if (!rc->cursor_addnode && over_line) {
455 event_context->cursor_shape = cursor_gradient_add_xpm;
456 sp_event_context_update_cursor(event_context);
457 rc->cursor_addnode = true;
458 }
459 }
460 break;
461 case GDK_BUTTON_RELEASE:
462 event_context->xp = event_context->yp = 0;
463 if ( event->button.button == 1 && !event_context->space_panning ) {
464 if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
465 bool over_line = false;
466 SPCtrlLine *line = NULL;
467 if (drag->lines) {
468 for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
469 line = (SPCtrlLine*) l->data;
470 over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
471 }
472 }
473 if (over_line) {
474 sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, 0);
475 ret = TRUE;
476 }
477 } else {
478 dragging = false;
480 // unless clicked with Ctrl (to enable Ctrl+doubleclick). (don't what this is for (johan))
481 if (event->button.state & GDK_CONTROL_MASK) {
482 ret = TRUE;
483 break;
484 }
486 if (!event_context->within_tolerance) {
487 // we've been dragging, do nothing (grdrag handles that)
488 } else if (event_context->item_to_select) {
489 // no dragging, select clicked item if any
490 if (event->button.state & GDK_SHIFT_MASK) {
491 selection->toggle(event_context->item_to_select);
492 } else {
493 selection->set(event_context->item_to_select);
494 }
495 } else {
496 // click in an empty space; do the same as Esc
497 if (drag->selected) {
498 drag->deselectAll();
499 } else {
500 selection->clear();
501 }
502 }
504 event_context->item_to_select = NULL;
505 ret = TRUE;
506 }
507 }
508 break;
509 case GDK_KEY_PRESS:
510 switch (get_group0_keyval (&event->key)) {
511 case GDK_Alt_L:
512 case GDK_Alt_R:
513 case GDK_Control_L:
514 case GDK_Control_R:
515 case GDK_Shift_L:
516 case GDK_Shift_R:
517 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
518 case GDK_Meta_R:
519 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
520 _("<b>Ctrl</b>: snap gradient angle"),
521 _("<b>Shift</b>: draw gradient around the starting point"),
522 NULL);
523 break;
525 case GDK_x:
526 case GDK_X:
527 if (MOD__ALT_ONLY) {
528 desktop->setToolboxFocusTo ("altx-grad");
529 ret = TRUE;
530 }
531 break;
533 case GDK_Escape:
534 if (drag->selected) {
535 drag->deselectAll();
536 } else {
537 selection->clear();
538 }
539 ret = TRUE;
540 //TODO: make dragging escapable by Esc
541 break;
543 case GDK_Left: // move handle left
544 case GDK_KP_Left:
545 case GDK_KP_4:
546 if (!MOD__CTRL) { // not ctrl
547 if (MOD__ALT) { // alt
548 if (MOD__SHIFT) drag->selected_move_screen(-10, 0); // shift
549 else drag->selected_move_screen(-1, 0); // no shift
550 }
551 else { // no alt
552 if (MOD__SHIFT) drag->selected_move(-10*nudge, 0); // shift
553 else drag->selected_move(-nudge, 0); // no shift
554 }
555 ret = TRUE;
556 }
557 break;
558 case GDK_Up: // move handle up
559 case GDK_KP_Up:
560 case GDK_KP_8:
561 if (!MOD__CTRL) { // not ctrl
562 if (MOD__ALT) { // alt
563 if (MOD__SHIFT) drag->selected_move_screen(0, 10); // shift
564 else drag->selected_move_screen(0, 1); // no shift
565 }
566 else { // no alt
567 if (MOD__SHIFT) drag->selected_move(0, 10*nudge); // shift
568 else drag->selected_move(0, nudge); // no shift
569 }
570 ret = TRUE;
571 }
572 break;
573 case GDK_Right: // move handle right
574 case GDK_KP_Right:
575 case GDK_KP_6:
576 if (!MOD__CTRL) { // not ctrl
577 if (MOD__ALT) { // alt
578 if (MOD__SHIFT) drag->selected_move_screen(10, 0); // shift
579 else drag->selected_move_screen(1, 0); // no shift
580 }
581 else { // no alt
582 if (MOD__SHIFT) drag->selected_move(10*nudge, 0); // shift
583 else drag->selected_move(nudge, 0); // no shift
584 }
585 ret = TRUE;
586 }
587 break;
588 case GDK_Down: // move handle down
589 case GDK_KP_Down:
590 case GDK_KP_2:
591 if (!MOD__CTRL) { // not ctrl
592 if (MOD__ALT) { // alt
593 if (MOD__SHIFT) drag->selected_move_screen(0, -10); // shift
594 else drag->selected_move_screen(0, -1); // no shift
595 }
596 else { // no alt
597 if (MOD__SHIFT) drag->selected_move(0, -10*nudge); // shift
598 else drag->selected_move(0, -nudge); // no shift
599 }
600 ret = TRUE;
601 }
602 break;
603 case GDK_r:
604 case GDK_R:
605 if (MOD__SHIFT_ONLY) {
606 // First try selected dragger
607 if (drag && drag->selected) {
608 drag->selected_reverse_vector();
609 } else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
610 for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
611 sp_item_gradient_reverse_vector (SP_ITEM(i->data), true);
612 sp_item_gradient_reverse_vector (SP_ITEM(i->data), false);
613 }
614 }
615 // we did an undoable action
616 sp_document_done (sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
617 _("Invert gradient"));
618 ret = TRUE;
619 }
620 break;
621 /*
622 case GDK_Insert:
623 case GDK_KP_Insert:
624 // with any modifiers:
625 // insert mid-stops between selected stops in gradient, or between all stops if none or only one selected
626 ret = TRUE;
627 break;
628 */
629 case GDK_Delete:
630 case GDK_KP_Delete:
631 case GDK_BackSpace:
632 if ( drag->selected ) {
633 drag->deleteSelected(MOD__CTRL_ONLY);
634 ret = TRUE;
635 }
636 break;
637 default:
638 break;
639 }
640 break;
641 case GDK_KEY_RELEASE:
642 switch (get_group0_keyval (&event->key)) {
643 case GDK_Alt_L:
644 case GDK_Alt_R:
645 case GDK_Control_L:
646 case GDK_Control_R:
647 case GDK_Shift_L:
648 case GDK_Shift_R:
649 case GDK_Meta_L: // Meta is when you press Shift+Alt
650 case GDK_Meta_R:
651 event_context->defaultMessageContext()->clear();
652 break;
653 default:
654 break;
655 }
656 break;
657 default:
658 break;
659 }
661 if (!ret) {
662 if (((SPEventContextClass *) parent_class)->root_handler) {
663 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
664 }
665 }
667 return ret;
668 }
670 static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime)
671 {
672 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
673 Inkscape::Selection *selection = sp_desktop_selection(desktop);
674 SPDocument *document = sp_desktop_document(desktop);
675 SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
677 if (!selection->isEmpty()) {
678 int type = prefs_get_int_attribute ("tools.gradient", "newgradient", 1);
679 int fill_or_stroke = prefs_get_int_attribute ("tools.gradient", "newfillorstroke", 1);
681 SPGradient *vector;
682 if (ec->item_to_select) {
683 vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
684 } else {
685 vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(selection->itemList()->data), fill_or_stroke);
686 }
688 // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
689 SPCSSAttr *css = sp_repr_css_attr_new();
690 sp_repr_css_set_property(css, "fill-opacity", "1.0");
692 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
694 //FIXME: see above
695 sp_repr_css_change_recursive(SP_OBJECT_REPR(i->data), css, "style");
697 sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
699 if (type == SP_GRADIENT_TYPE_LINEAR) {
700 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
701 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
702 } else if (type == SP_GRADIENT_TYPE_RADIAL) {
703 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
704 sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
705 }
706 SP_OBJECT (i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
707 }
708 if (ec->_grdrag) {
709 ec->_grdrag->updateDraggers();
710 // prevent regenerating draggers by selection modified signal, which sometimes
711 // comes too late and thus destroys the knot which we will now grab:
712 ec->_grdrag->local_change = true;
713 // give the grab out-of-bounds values of xp/yp because we're already dragging
714 // and therefore are already out of tolerance
715 ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
716 type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
717 0, //point_i
718 fill_or_stroke, 99999, 99999, etime);
719 }
720 // We did an undoable action, but sp_document_done will be called by the knot when released
722 // status text; we do not track coords because this branch is run once, not all the time
723 // during drag
724 int n_objects = g_slist_length((GSList *) selection->itemList());
725 rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
726 ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
727 "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
728 n_objects);
729 } else {
730 sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
731 }
732 }
735 /*
736 Local Variables:
737 mode:c++
738 c-file-style:"stroustrup"
739 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
740 indent-tabs-mode:nil
741 fill-column:99
742 End:
743 */
744 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :