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