Code

Refactoring SPColor to C++ and removing legacy CMYK implementation
[inkscape.git] / src / widgets / paint-selector.cpp
1 #define __SP_PAINT_SELECTOR_C__
3 /** \file
4  * SPPaintSelector: Generic paint selector widget.
5  */
7 /*
8  * Copyright (C) Lauris Kaplinski 2002
9  *   bulia byak <buliabyak@users.sf.net>
10 */
12 #define noSP_PS_VERBOSE
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
20 #include <gtk/gtkhbox.h>
21 #include <gtk/gtkradiobutton.h>
22 #include <gtk/gtkframe.h>
23 #include <gtk/gtklabel.h>
24 #include <gtk/gtkoptionmenu.h>
25 #include <gtk/gtktooltips.h>
26 #include <gtk/gtkmenuitem.h>
28 #include "../sp-pattern.h"
29 #include <glibmm/i18n.h>
30 #include "../widgets/icon.h"
31 #include "../inkscape-stock.h"
32 #include "widgets/widget-sizes.h"
33 #include "xml/repr.h"
35 #include "sp-color-notebook.h"
36 #include "sp-linear-gradient-fns.h"
37 #include "sp-radial-gradient-fns.h"
38 /* fixme: Move it from dialogs to here */
39 #include "gradient-selector.h"
40 #include <inkscape.h>
41 #include <document-private.h>
42 #include <desktop-style.h>
43 #include <style.h>
44 #include "svg/svg-color.h"
45 #include "svg/css-ostringstream.h"
47 #include "paint-selector.h"
49 enum {
50     MODE_CHANGED,
51     GRABBED,
52     DRAGGED,
53     RELEASED,
54     CHANGED,
55     FILLRULE_CHANGED,
56     LAST_SIGNAL
57 };
59 static void sp_paint_selector_class_init(SPPaintSelectorClass *klass);
60 static void sp_paint_selector_init(SPPaintSelector *slider);
61 static void sp_paint_selector_destroy(GtkObject *object);
63 static GtkWidget *sp_paint_selector_style_button_add(SPPaintSelector *psel, gchar const *px, SPPaintSelectorMode mode, GtkTooltips *tt, gchar const *tip);
64 static void sp_paint_selector_style_button_toggled(GtkToggleButton *tb, SPPaintSelector *psel);
65 static void sp_paint_selector_fillrule_toggled(GtkToggleButton *tb, SPPaintSelector *psel);
67 static void sp_paint_selector_set_mode_empty(SPPaintSelector *psel);
68 static void sp_paint_selector_set_mode_multiple(SPPaintSelector *psel);
69 static void sp_paint_selector_set_mode_none(SPPaintSelector *psel);
70 static void sp_paint_selector_set_mode_color(SPPaintSelector *psel, SPPaintSelectorMode mode);
71 static void sp_paint_selector_set_mode_gradient(SPPaintSelector *psel, SPPaintSelectorMode mode);
72 static void sp_paint_selector_set_mode_pattern(SPPaintSelector *psel, SPPaintSelectorMode mode);
73 static void sp_paint_selector_set_mode_unset(SPPaintSelector *psel);
76 static void sp_paint_selector_set_style_buttons(SPPaintSelector *psel, GtkWidget *active);
78 static GtkVBoxClass *parent_class;
79 static guint psel_signals[LAST_SIGNAL] = {0};
81 GtkType
82 sp_paint_selector_get_type(void)
83 {
84     static GtkType type = 0;
85     if (!type) {
86         GtkTypeInfo info = {
87             "SPPaintSelector",
88             sizeof(SPPaintSelector),
89             sizeof(SPPaintSelectorClass),
90             (GtkClassInitFunc) sp_paint_selector_class_init,
91             (GtkObjectInitFunc) sp_paint_selector_init,
92             NULL, NULL, NULL
93         };
94         type = gtk_type_unique(GTK_TYPE_VBOX, &info);
95     }
96     return type;
97 }
99 static void
100 sp_paint_selector_class_init(SPPaintSelectorClass *klass)
102     GtkObjectClass *object_class;
103     GtkWidgetClass *widget_class;
105     object_class = (GtkObjectClass *) klass;
106     widget_class = (GtkWidgetClass *) klass;
108     parent_class = (GtkVBoxClass*)gtk_type_class(GTK_TYPE_VBOX);
110     psel_signals[MODE_CHANGED] = gtk_signal_new("mode_changed",
111                                                 (GtkSignalRunType)(GTK_RUN_FIRST | GTK_RUN_NO_RECURSE),
112                                                 GTK_CLASS_TYPE(object_class),
113                                                 GTK_SIGNAL_OFFSET(SPPaintSelectorClass, mode_changed),
114                                                 gtk_marshal_NONE__UINT,
115                                                 GTK_TYPE_NONE, 1, GTK_TYPE_UINT);
116     psel_signals[GRABBED] =  gtk_signal_new("grabbed",
117                                             (GtkSignalRunType)(GTK_RUN_FIRST | GTK_RUN_NO_RECURSE),
118                                             GTK_CLASS_TYPE(object_class),
119                                             GTK_SIGNAL_OFFSET(SPPaintSelectorClass, grabbed),
120                                             gtk_marshal_NONE__NONE,
121                                             GTK_TYPE_NONE, 0);
122     psel_signals[DRAGGED] =  gtk_signal_new("dragged",
123                                             (GtkSignalRunType)(GTK_RUN_FIRST | GTK_RUN_NO_RECURSE),
124                                             GTK_CLASS_TYPE(object_class),
125                                             GTK_SIGNAL_OFFSET(SPPaintSelectorClass, dragged),
126                                             gtk_marshal_NONE__NONE,
127                                             GTK_TYPE_NONE, 0);
128     psel_signals[RELEASED] = gtk_signal_new("released",
129                                             (GtkSignalRunType)(GTK_RUN_FIRST | GTK_RUN_NO_RECURSE),
130                                             GTK_CLASS_TYPE(object_class),
131                                             GTK_SIGNAL_OFFSET(SPPaintSelectorClass, released),
132                                             gtk_marshal_NONE__NONE,
133                                             GTK_TYPE_NONE, 0);
134     psel_signals[CHANGED] =  gtk_signal_new("changed",
135                                             (GtkSignalRunType)(GTK_RUN_FIRST | GTK_RUN_NO_RECURSE),
136                                             GTK_CLASS_TYPE(object_class),
137                                             GTK_SIGNAL_OFFSET(SPPaintSelectorClass, changed),
138                                             gtk_marshal_NONE__NONE,
139                                             GTK_TYPE_NONE, 0);
140     psel_signals[FILLRULE_CHANGED] = gtk_signal_new("fillrule_changed",
141                                                     (GtkSignalRunType)(GTK_RUN_FIRST | GTK_RUN_NO_RECURSE),
142                                                     GTK_CLASS_TYPE(object_class),
143                                                     GTK_SIGNAL_OFFSET(SPPaintSelectorClass, fillrule_changed),
144                                                     gtk_marshal_NONE__UINT,
145                                                     GTK_TYPE_NONE, 1, GTK_TYPE_UINT);
147     object_class->destroy = sp_paint_selector_destroy;
150 #define XPAD 4
151 #define YPAD 1
153 static void
154 sp_paint_selector_init(SPPaintSelector *psel)
156     GtkTooltips *tt = gtk_tooltips_new();
158     psel->mode = (SPPaintSelectorMode)-1; // huh?  do you mean 0xff?  --  I think this means "not in the enum"
160     /* Paint style button box */
161     psel->style = gtk_hbox_new(FALSE, 0);
162     gtk_widget_show(psel->style);
163     gtk_container_set_border_width(GTK_CONTAINER(psel->style), 4);
164     gtk_box_pack_start(GTK_BOX(psel), psel->style, FALSE, FALSE, 0);
166     /* Buttons */
167     psel->none = sp_paint_selector_style_button_add(psel, INKSCAPE_STOCK_FILL_NONE,
168                                                     SP_PAINT_SELECTOR_MODE_NONE, tt, _("No paint"));
169     psel->solid = sp_paint_selector_style_button_add(psel, INKSCAPE_STOCK_FILL_SOLID,
170                                                      SP_PAINT_SELECTOR_MODE_COLOR_RGB, tt, _("Flat color"));
171     psel->gradient = sp_paint_selector_style_button_add(psel, INKSCAPE_STOCK_FILL_GRADIENT,
172                                                         SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR, tt, _("Linear gradient"));
173     psel->radial = sp_paint_selector_style_button_add(psel, INKSCAPE_STOCK_FILL_RADIAL,
174                                                       SP_PAINT_SELECTOR_MODE_GRADIENT_RADIAL, tt, _("Radial gradient"));
175     psel->pattern = sp_paint_selector_style_button_add(psel, INKSCAPE_STOCK_FILL_PATTERN,
176                                                        SP_PAINT_SELECTOR_MODE_PATTERN, tt, _("Pattern"));
177     psel->unset = sp_paint_selector_style_button_add(psel, INKSCAPE_STOCK_FILL_UNSET,
178                                                      SP_PAINT_SELECTOR_MODE_UNSET, tt, _("Unset paint (make it undefined so it can be inherited)"));
180     /* Fillrule */
181     {
182         psel->fillrulebox = gtk_hbox_new(FALSE, 0);
183         gtk_box_pack_end(GTK_BOX(psel->style), psel->fillrulebox, FALSE, FALSE, 0);
185         GtkWidget *w;
186         psel->evenodd = gtk_radio_button_new(NULL);
187         gtk_button_set_relief(GTK_BUTTON(psel->evenodd), GTK_RELIEF_NONE);
188         gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(psel->evenodd), FALSE);
189         // TRANSLATORS: for info, see http://www.w3.org/TR/2000/CR-SVG-20000802/painting.html#FillRuleProperty
190         gtk_tooltips_set_tip(tt, psel->evenodd, _("Any path self-intersections or subpaths create holes in the fill (fill-rule: evenodd)"), NULL);
191         gtk_object_set_data(GTK_OBJECT(psel->evenodd), "mode", GUINT_TO_POINTER(SP_PAINT_SELECTOR_FILLRULE_EVENODD));
192         w = sp_icon_new(Inkscape::ICON_SIZE_DECORATION, "fillrule_evenodd");
193         gtk_container_add(GTK_CONTAINER(psel->evenodd), w);
194         gtk_box_pack_start(GTK_BOX(psel->fillrulebox), psel->evenodd, FALSE, FALSE, 0);
195         gtk_signal_connect(GTK_OBJECT(psel->evenodd), "toggled", GTK_SIGNAL_FUNC(sp_paint_selector_fillrule_toggled), psel);
197         psel->nonzero = gtk_radio_button_new(gtk_radio_button_group(GTK_RADIO_BUTTON(psel->evenodd)));
198         gtk_button_set_relief(GTK_BUTTON(psel->nonzero), GTK_RELIEF_NONE);
199         gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(psel->nonzero), FALSE);
200         // TRANSLATORS: for info, see http://www.w3.org/TR/2000/CR-SVG-20000802/painting.html#FillRuleProperty
201         gtk_tooltips_set_tip(tt, psel->nonzero, _("Fill is solid unless a subpath is counterdirectional (fill-rule: nonzero)"), NULL);
202         gtk_object_set_data(GTK_OBJECT(psel->nonzero), "mode", GUINT_TO_POINTER(SP_PAINT_SELECTOR_FILLRULE_NONZERO));
203         w = sp_icon_new(Inkscape::ICON_SIZE_DECORATION, "fillrule_nonzero");
204         gtk_container_add(GTK_CONTAINER(psel->nonzero), w);
205         gtk_box_pack_start(GTK_BOX(psel->fillrulebox), psel->nonzero, FALSE, FALSE, 0);
206         gtk_signal_connect(GTK_OBJECT(psel->nonzero), "toggled", GTK_SIGNAL_FUNC(sp_paint_selector_fillrule_toggled), psel);
207     }
209     /* Frame */
210     psel->frame = gtk_frame_new("");
211     gtk_widget_show(psel->frame);
212     gtk_container_set_border_width(GTK_CONTAINER(psel->frame), 0);
213     gtk_box_pack_start(GTK_BOX(psel), psel->frame, TRUE, TRUE, 0);
215     /* Last used color */
216     psel->color.set( 0.0, 0.0, 0.0 );
217     psel->alpha = 1.0;
220 static void
221 sp_paint_selector_destroy(GtkObject *object)
223     SPPaintSelector *psel = SP_PAINT_SELECTOR(object);
225     // clean up our long-living pattern menu
226     g_object_set_data(G_OBJECT(psel),"patternmenu",NULL);
228     if (((GtkObjectClass *) parent_class)->destroy)
229         (* ((GtkObjectClass *) parent_class)->destroy)(object);
232 static GtkWidget *
233 sp_paint_selector_style_button_add(SPPaintSelector *psel,
234                                    gchar const *pixmap, SPPaintSelectorMode mode,
235                                    GtkTooltips *tt, gchar const *tip)
237     GtkWidget *b, *w;
239     b = gtk_toggle_button_new();
240     gtk_tooltips_set_tip(tt, b, tip, NULL);
241     gtk_widget_show(b);
243     gtk_container_set_border_width(GTK_CONTAINER(b), 0);
245     gtk_button_set_relief(GTK_BUTTON(b), GTK_RELIEF_NONE);
247     gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(b), FALSE);
248     gtk_object_set_data(GTK_OBJECT(b), "mode", GUINT_TO_POINTER(mode));
250     w = sp_icon_new(Inkscape::ICON_SIZE_BUTTON, pixmap);
251     gtk_widget_show(w);
252     gtk_container_add(GTK_CONTAINER(b), w);
254     gtk_box_pack_start(GTK_BOX(psel->style), b, FALSE, FALSE, 0);
255     gtk_signal_connect(GTK_OBJECT(b), "toggled", GTK_SIGNAL_FUNC(sp_paint_selector_style_button_toggled), psel);
257     return b;
260 static void
261 sp_paint_selector_style_button_toggled(GtkToggleButton *tb, SPPaintSelector *psel)
263     if (!psel->update && gtk_toggle_button_get_active(tb)) {
264         sp_paint_selector_set_mode(psel, (SPPaintSelectorMode)GPOINTER_TO_UINT(gtk_object_get_data(GTK_OBJECT(tb), "mode")));
265     }
268 static void
269 sp_paint_selector_fillrule_toggled(GtkToggleButton *tb, SPPaintSelector *psel)
271     if (!psel->update && gtk_toggle_button_get_active(tb)) {
272         SPPaintSelectorFillRule fr = (SPPaintSelectorFillRule)GPOINTER_TO_UINT(gtk_object_get_data(GTK_OBJECT(tb), "mode"));
273         gtk_signal_emit(GTK_OBJECT(psel), psel_signals[FILLRULE_CHANGED], fr);
274     }
277 void
278 sp_paint_selector_show_fillrule(SPPaintSelector *psel, bool is_fill)
280     if (psel->fillrulebox) {
281         if (is_fill) {
282             gtk_widget_show_all(psel->fillrulebox);
283         } else {
284             gtk_widget_destroy(psel->fillrulebox);
285             psel->fillrulebox = NULL;
286         }
287     }
291 GtkWidget *
292 sp_paint_selector_new(bool is_fill)
294     SPPaintSelector *psel;
296     psel = (SPPaintSelector*)gtk_type_new(SP_TYPE_PAINT_SELECTOR);
298     sp_paint_selector_set_mode(psel, SP_PAINT_SELECTOR_MODE_MULTIPLE);
300     // This silliness is here because I don't know how to pass a parameter to the
301     // GtkObject "constructor" (sp_paint_selector_init). Remove it when paint_selector
302     // becomes a normal class.
303     sp_paint_selector_show_fillrule(psel, is_fill);
305     return GTK_WIDGET(psel);
308 void
309 sp_paint_selector_set_mode(SPPaintSelector *psel, SPPaintSelectorMode mode)
311     if (psel->mode != mode) {
312         psel->update = TRUE;
313 #ifdef SP_PS_VERBOSE
314         g_print("Mode change %d -> %d\n", psel->mode, mode);
315 #endif
316         switch (mode) {
317             case SP_PAINT_SELECTOR_MODE_EMPTY:
318                 sp_paint_selector_set_mode_empty(psel);
319                 break;
320             case SP_PAINT_SELECTOR_MODE_MULTIPLE:
321                 sp_paint_selector_set_mode_multiple(psel);
322                 break;
323             case SP_PAINT_SELECTOR_MODE_NONE:
324                 sp_paint_selector_set_mode_none(psel);
325                 break;
326             case SP_PAINT_SELECTOR_MODE_COLOR_RGB:
327             case SP_PAINT_SELECTOR_MODE_COLOR_CMYK:
328                 sp_paint_selector_set_mode_color(psel, mode);
329                 break;
330             case SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR:
331             case SP_PAINT_SELECTOR_MODE_GRADIENT_RADIAL:
332                 sp_paint_selector_set_mode_gradient(psel, mode);
333                 break;
334             case SP_PAINT_SELECTOR_MODE_PATTERN:
335                 sp_paint_selector_set_mode_pattern(psel, mode);
336                 break;
337             case SP_PAINT_SELECTOR_MODE_UNSET:
338                 sp_paint_selector_set_mode_unset(psel);
339                 break;
340             default:
341                 g_warning("file %s: line %d: Unknown paint mode %d", __FILE__, __LINE__, mode);
342                 break;
343         }
344         psel->mode = mode;
345         gtk_signal_emit(GTK_OBJECT(psel), psel_signals[MODE_CHANGED], psel->mode);
346         psel->update = FALSE;
347     }
350 void
351 sp_paint_selector_set_fillrule(SPPaintSelector *psel, SPPaintSelectorFillRule fillrule)
353     if (psel->fillrulebox) {
354         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(psel->evenodd), (fillrule == SP_PAINT_SELECTOR_FILLRULE_EVENODD));
355         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(psel->nonzero), (fillrule == SP_PAINT_SELECTOR_FILLRULE_NONZERO));
356     }
359 void
360 sp_paint_selector_set_color_alpha(SPPaintSelector *psel, SPColor const *color, float alpha)
362     g_return_if_fail( ( 0.0 <= alpha ) && ( alpha <= 1.0 ) );
363     SPColorSelector *csel;
364     guint32 rgba;
366 /*
367     if ( sp_color_get_colorspace_type(color) == SP_COLORSPACE_TYPE_CMYK )
368     {
369 #ifdef SP_PS_VERBOSE
370         g_print("PaintSelector set CMYKA\n");
371 #endif
372         sp_paint_selector_set_mode(psel, SP_PAINT_SELECTOR_MODE_COLOR_CMYK);
373     }
374     else
375 */
376     {
377 #ifdef SP_PS_VERBOSE
378         g_print("PaintSelector set RGBA\n");
379 #endif
380         sp_paint_selector_set_mode(psel, SP_PAINT_SELECTOR_MODE_COLOR_RGB);
381     }
383     csel = (SPColorSelector*)gtk_object_get_data(GTK_OBJECT(psel->selector), "color-selector");
384     rgba = color->toRGBA32( alpha );
385     csel->base->setColorAlpha( *color, alpha );
388 void
389 sp_paint_selector_set_gradient_linear(SPPaintSelector *psel, SPGradient *vector)
391     SPGradientSelector *gsel;
392 #ifdef SP_PS_VERBOSE
393     g_print("PaintSelector set GRADIENT LINEAR\n");
394 #endif
395     sp_paint_selector_set_mode(psel, SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR);
397     gsel = (SPGradientSelector*)gtk_object_get_data(GTK_OBJECT(psel->selector), "gradient-selector");
399     sp_gradient_selector_set_mode(gsel, SP_GRADIENT_SELECTOR_MODE_LINEAR);
400     sp_gradient_selector_set_vector(gsel, (vector) ? SP_OBJECT_DOCUMENT(vector) : NULL, vector);
403 void
404 sp_paint_selector_set_gradient_radial(SPPaintSelector *psel, SPGradient *vector)
406     SPGradientSelector *gsel;
407 #ifdef SP_PS_VERBOSE
408     g_print("PaintSelector set GRADIENT RADIAL\n");
409 #endif
410     sp_paint_selector_set_mode(psel, SP_PAINT_SELECTOR_MODE_GRADIENT_RADIAL);
412     gsel = (SPGradientSelector*)gtk_object_get_data(GTK_OBJECT(psel->selector), "gradient-selector");
414     sp_gradient_selector_set_mode(gsel, SP_GRADIENT_SELECTOR_MODE_RADIAL);
415     sp_gradient_selector_set_vector(gsel, (vector) ? SP_OBJECT_DOCUMENT(vector) : NULL, vector);
418 void
419 sp_paint_selector_set_gradient_properties(SPPaintSelector *psel, SPGradientUnits units, SPGradientSpread spread)
421     SPGradientSelector *gsel;
422     g_return_if_fail(SP_IS_PAINT_SELECTOR(psel));
423     g_return_if_fail((psel->mode == SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR) ||
424                      (psel->mode == SP_PAINT_SELECTOR_MODE_GRADIENT_RADIAL));
425     gsel = (SPGradientSelector*)gtk_object_get_data(GTK_OBJECT(psel->selector), "gradient-selector");
426     sp_gradient_selector_set_units(gsel, units);
427     sp_gradient_selector_set_spread(gsel, spread);
430 void
431 sp_paint_selector_get_gradient_properties(SPPaintSelector *psel, SPGradientUnits *units, SPGradientSpread *spread)
433     SPGradientSelector *gsel;
434     g_return_if_fail(SP_IS_PAINT_SELECTOR(psel));
435     g_return_if_fail((psel->mode == SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR) ||
436                      (psel->mode == SP_PAINT_SELECTOR_MODE_GRADIENT_RADIAL));
437     gsel = (SPGradientSelector*)gtk_object_get_data(GTK_OBJECT(psel->selector), "gradient-selector");
438     if (units) *units = sp_gradient_selector_get_units(gsel);
439     if (spread) *spread = sp_gradient_selector_get_spread(gsel);
442 /**
443  * \post (alpha == NULL) || (*alpha in [0.0, 1.0]).
444  */
445 void
446 sp_paint_selector_get_color_alpha(SPPaintSelector *psel, SPColor *color, gfloat *alpha)
448     SPColorSelector *csel;
450     csel = (SPColorSelector*)gtk_object_get_data(GTK_OBJECT(psel->selector), "color-selector");
452     csel->base->getColorAlpha( *color, alpha );
454     g_assert( !alpha
455               || ( ( 0.0 <= *alpha )
456                    && ( *alpha <= 1.0 ) ) );
459 SPGradient *
460 sp_paint_selector_get_gradient_vector(SPPaintSelector *psel)
462     SPGradientSelector *gsel;
464     g_return_val_if_fail((psel->mode == SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR) ||
465                          (psel->mode == SP_PAINT_SELECTOR_MODE_GRADIENT_RADIAL), NULL);
467     gsel = (SPGradientSelector*)gtk_object_get_data(GTK_OBJECT(psel->selector), "gradient-selector");
469     return sp_gradient_selector_get_vector(gsel);
472 void
473 sp_gradient_selector_attrs_to_gradient(SPGradient *gr, SPPaintSelector *psel)
475     SPGradientUnits units;
476     SPGradientSpread spread;
477     sp_paint_selector_get_gradient_properties(psel, &units, &spread);
478     sp_gradient_set_units(gr, units);
479     sp_gradient_set_spread(gr, spread);
480     SP_OBJECT(gr)->updateRepr();
483 static void
484 sp_paint_selector_clear_frame(SPPaintSelector *psel)
486     g_return_if_fail( psel != NULL);
488     if (psel->selector) {
490         /* before we destroy the frame contents, we must detach
491          * the patternmenu so that Gtk doesn't gtk_widget_destroy
492          * all the children of the menu.  (We also have a g_object_ref
493          * count set on it too so that the gtk_container_remove doesn't
494          * end up destroying it.
495          */
496         GtkWidget *patterns = (GtkWidget *)g_object_get_data(G_OBJECT(psel), "patternmenu");
497         if (patterns != NULL) {
498             GtkWidget * parent = gtk_widget_get_parent( GTK_WIDGET(patterns));
499             if ( parent != NULL ) {
500                 g_assert( GTK_IS_CONTAINER(parent) );
501                 gtk_container_remove( GTK_CONTAINER(parent), patterns );
502             }
503         }
505         gtk_widget_destroy(psel->selector);
506         psel->selector = NULL;
507     }
510 static void
511 sp_paint_selector_set_mode_empty(SPPaintSelector *psel)
513     sp_paint_selector_set_style_buttons(psel, NULL);
514     gtk_widget_set_sensitive(psel->style, FALSE);
516     sp_paint_selector_clear_frame(psel);
518     gtk_frame_set_label(GTK_FRAME(psel->frame), _("No objects"));
521 static void
522 sp_paint_selector_set_mode_multiple(SPPaintSelector *psel)
524     sp_paint_selector_set_style_buttons(psel, NULL);
525     gtk_widget_set_sensitive(psel->style, TRUE);
527     sp_paint_selector_clear_frame(psel);
529     gtk_frame_set_label(GTK_FRAME(psel->frame), _("Multiple styles"));
532 static void
533 sp_paint_selector_set_mode_unset(SPPaintSelector *psel)
535     sp_paint_selector_set_style_buttons(psel, psel->unset);
536     gtk_widget_set_sensitive(psel->style, TRUE);
538     sp_paint_selector_clear_frame(psel);
540     gtk_frame_set_label(GTK_FRAME(psel->frame), _("Paint is undefined"));
543 static void
544 sp_paint_selector_set_mode_none(SPPaintSelector *psel)
546     sp_paint_selector_set_style_buttons(psel, psel->none);
547     gtk_widget_set_sensitive(psel->style, TRUE);
549     sp_paint_selector_clear_frame(psel);
551     gtk_frame_set_label(GTK_FRAME(psel->frame), _("No paint"));
554 /* Color paint */
556 static void
557 sp_paint_selector_color_grabbed(SPColorSelector *csel, SPPaintSelector *psel)
559     gtk_signal_emit(GTK_OBJECT(psel), psel_signals[GRABBED]);
562 static void
563 sp_paint_selector_color_dragged(SPColorSelector *csel, SPPaintSelector *psel)
565     gtk_signal_emit(GTK_OBJECT(psel), psel_signals[DRAGGED]);
568 static void
569 sp_paint_selector_color_released(SPColorSelector *csel, SPPaintSelector *psel)
571     gtk_signal_emit(GTK_OBJECT(psel), psel_signals[RELEASED]);
574 static void
575 sp_paint_selector_color_changed(SPColorSelector *csel, SPPaintSelector *psel)
577     csel->base->getColorAlpha( psel->color, &psel->alpha );
579     gtk_signal_emit(GTK_OBJECT(psel), psel_signals[CHANGED]);
582 static void
583 sp_paint_selector_set_mode_color(SPPaintSelector *psel, SPPaintSelectorMode mode)
585     GtkWidget *csel;
587     sp_paint_selector_set_style_buttons(psel, psel->solid);
588     gtk_widget_set_sensitive(psel->style, TRUE);
590     if ((psel->mode == SP_PAINT_SELECTOR_MODE_COLOR_RGB) || (psel->mode == SP_PAINT_SELECTOR_MODE_COLOR_CMYK)) {
591         /* Already have color selector */
592         csel = (GtkWidget*)gtk_object_get_data(GTK_OBJECT(psel->selector), "color-selector");
593     } else {
595         sp_paint_selector_clear_frame(psel);
596         /* Create new color selector */
597         /* Create vbox */
598         GtkWidget *vb = gtk_vbox_new(FALSE, 4);
599         gtk_widget_show(vb);
601         /* Color selector */
602         csel = sp_color_selector_new( SP_TYPE_COLOR_NOTEBOOK );
603         gtk_widget_show(csel);
604         gtk_object_set_data(GTK_OBJECT(vb), "color-selector", csel);
605         gtk_box_pack_start(GTK_BOX(vb), csel, TRUE, TRUE, 0);
606         gtk_signal_connect(GTK_OBJECT(csel), "grabbed", GTK_SIGNAL_FUNC(sp_paint_selector_color_grabbed), psel);
607         gtk_signal_connect(GTK_OBJECT(csel), "dragged", GTK_SIGNAL_FUNC(sp_paint_selector_color_dragged), psel);
608         gtk_signal_connect(GTK_OBJECT(csel), "released", GTK_SIGNAL_FUNC(sp_paint_selector_color_released), psel);
609         gtk_signal_connect(GTK_OBJECT(csel), "changed", GTK_SIGNAL_FUNC(sp_paint_selector_color_changed), psel);
610         /* Pack everything to frame */
611         gtk_container_add(GTK_CONTAINER(psel->frame), vb);
612         psel->selector = vb;
614         /* Set color */
615         SP_COLOR_SELECTOR( csel )->base->setColorAlpha( psel->color, psel->alpha );
617     }
619     gtk_frame_set_label(GTK_FRAME(psel->frame), _("Flat color"));
620 #ifdef SP_PS_VERBOSE
621     g_print("Color req\n");
622 #endif
625 /* Gradient */
627 static void
628 sp_paint_selector_gradient_grabbed(SPColorSelector *csel, SPPaintSelector *psel)
630     gtk_signal_emit(GTK_OBJECT(psel), psel_signals[GRABBED]);
633 static void
634 sp_paint_selector_gradient_dragged(SPColorSelector *csel, SPPaintSelector *psel)
636     gtk_signal_emit(GTK_OBJECT(psel), psel_signals[DRAGGED]);
639 static void
640 sp_paint_selector_gradient_released(SPColorSelector *csel, SPPaintSelector *psel)
642     gtk_signal_emit(GTK_OBJECT(psel), psel_signals[RELEASED]);
645 static void
646 sp_paint_selector_gradient_changed(SPColorSelector *csel, SPPaintSelector *psel)
648     gtk_signal_emit(GTK_OBJECT(psel), psel_signals[CHANGED]);
651 static void
652 sp_paint_selector_set_mode_gradient(SPPaintSelector *psel, SPPaintSelectorMode mode)
654     GtkWidget *gsel;
656     /* fixme: We do not need function-wide gsel at all */
658     if (mode == SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR) {
659         sp_paint_selector_set_style_buttons(psel, psel->gradient);
660     } else {
661         sp_paint_selector_set_style_buttons(psel, psel->radial);
662     }
663     gtk_widget_set_sensitive(psel->style, TRUE);
665     if ((psel->mode == SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR) || (psel->mode == SP_PAINT_SELECTOR_MODE_GRADIENT_RADIAL)) {
666         /* Already have gradient selector */
667         gsel = (GtkWidget*)gtk_object_get_data(GTK_OBJECT(psel->selector), "gradient-selector");
668     } else {
669         sp_paint_selector_clear_frame(psel);
670         /* Create new gradient selector */
671         gsel = sp_gradient_selector_new();
672         gtk_widget_show(gsel);
673         gtk_signal_connect(GTK_OBJECT(gsel), "grabbed", GTK_SIGNAL_FUNC(sp_paint_selector_gradient_grabbed), psel);
674         gtk_signal_connect(GTK_OBJECT(gsel), "dragged", GTK_SIGNAL_FUNC(sp_paint_selector_gradient_dragged), psel);
675         gtk_signal_connect(GTK_OBJECT(gsel), "released", GTK_SIGNAL_FUNC(sp_paint_selector_gradient_released), psel);
676         gtk_signal_connect(GTK_OBJECT(gsel), "changed", GTK_SIGNAL_FUNC(sp_paint_selector_gradient_changed), psel);
677         /* Pack everything to frame */
678         gtk_container_add(GTK_CONTAINER(psel->frame), gsel);
679         psel->selector = gsel;
680         gtk_object_set_data(GTK_OBJECT(psel->selector), "gradient-selector", gsel);
681     }
683     /* Actually we have to set optiomenu history here */
684     if (mode == SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR) {
685         sp_gradient_selector_set_mode(SP_GRADIENT_SELECTOR(gsel), SP_GRADIENT_SELECTOR_MODE_LINEAR);
686         gtk_frame_set_label(GTK_FRAME(psel->frame), _("Linear gradient"));
687     } else {
688         sp_gradient_selector_set_mode(SP_GRADIENT_SELECTOR(gsel), SP_GRADIENT_SELECTOR_MODE_RADIAL);
689         gtk_frame_set_label(GTK_FRAME(psel->frame), _("Radial gradient"));
690     }
691 #ifdef SP_PS_VERBOSE
692     g_print("Gradient req\n");
693 #endif
696 static void
697 sp_paint_selector_set_style_buttons(SPPaintSelector *psel, GtkWidget *active)
699     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(psel->none), (active == psel->none));
700     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(psel->solid), (active == psel->solid));
701     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(psel->gradient), (active == psel->gradient));
702     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(psel->radial), (active == psel->radial));
703     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(psel->pattern), (active == psel->pattern));
704     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(psel->unset), (active == psel->unset));
707 static void
708 sp_psel_pattern_destroy(GtkWidget *widget,  SPPaintSelector *psel)
710     // drop our reference to the pattern menu widget
711     g_object_unref( G_OBJECT(widget) );
714 static void
715 sp_psel_pattern_change(GtkWidget *widget,  SPPaintSelector *psel)
717     gtk_signal_emit(GTK_OBJECT(psel), psel_signals[CHANGED]);
720 static GtkWidget*
721 ink_pattern_menu(GtkWidget *mnu)
723     /* Create new menu widget */
724     GtkWidget *m = gtk_menu_new();
725     gtk_widget_show(m);
727     /* Pick up all patterns  */
728     SPDocument *doc = SP_ACTIVE_DOCUMENT;
729     GSList *pl = NULL;
730     GSList const *patterns = sp_document_get_resource_list(doc, "pattern");
731     for (GSList *l = (GSList *) patterns; l != NULL; l = l->next) {
732         if (SP_PATTERN(l->data) == pattern_getroot(SP_PATTERN(l->data))) {  // only if this is a root pattern
733             pl = g_slist_prepend(pl, l->data);
734         }
735     }
737     pl = g_slist_reverse(pl);
739     if (!doc) {
740         GtkWidget *i;
741         i = gtk_menu_item_new_with_label(_("No document selected"));
742         gtk_widget_show(i);
743         gtk_menu_append(GTK_MENU(m), i);
744         gtk_widget_set_sensitive(mnu, FALSE);
745     } else if (!pl) {
746         GtkWidget *i;
747         i = gtk_menu_item_new_with_label(_("No patterns in document"));
748         gtk_widget_show(i);
749         gtk_menu_append(GTK_MENU(m), i);
750         gtk_widget_set_sensitive(mnu, FALSE);
751     } else {
752         for (; pl != NULL; pl = pl->next){
753             if (SP_IS_PATTERN(pl->data)){
754                 SPPattern *pat = SP_PATTERN(pl->data);
755                 GtkWidget *i = gtk_menu_item_new();
756                 gtk_widget_show(i);
757                 g_object_set_data(G_OBJECT(i), "pattern", pat);
758                 GtkWidget *hb = gtk_hbox_new(FALSE, 4);
759                 gtk_widget_show(hb);
760                 Inkscape::XML::Node *repr = SP_OBJECT_REPR((SPItem *) pl->data);
761                 GtkWidget *l = gtk_label_new(repr->attribute("id"));
762                 gtk_widget_show(l);
763                 gtk_misc_set_alignment(GTK_MISC(l), 1.0, 0.5);
764                 gtk_box_pack_start(GTK_BOX(hb), l, TRUE, TRUE, 0);
765                 gtk_widget_show(hb);
766                 gtk_container_add(GTK_CONTAINER(i), hb);
767                 gtk_menu_append(GTK_MENU(m), i);
768             }
769         }
771         gtk_widget_set_sensitive(mnu, TRUE);
772     }
773     gtk_option_menu_set_menu(GTK_OPTION_MENU(mnu), m);
775     /* Set history */
776     //gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
778     g_slist_free(pl);
779     return mnu;
783 /*update pattern list*/
784 void
785 sp_update_pattern_list( SPPaintSelector *psel,  SPPattern *pattern)
787     if (psel->update) return;
788     GtkWidget *mnu = (GtkWidget *)g_object_get_data(G_OBJECT(psel), "patternmenu");
789     g_assert( mnu != NULL );
791     /* Clear existing menu if any */
792     gtk_option_menu_remove_menu(GTK_OPTION_MENU(mnu));
794     ink_pattern_menu(mnu);
796     /* Set history */
798     if (pattern && !gtk_object_get_data(GTK_OBJECT(mnu), "update")) {
800         gtk_object_set_data(GTK_OBJECT(mnu), "update", GINT_TO_POINTER(TRUE));
802         gchar *patname = (gchar *) SP_OBJECT_REPR(pattern)->attribute("id");
804         GtkMenu *m = GTK_MENU(gtk_option_menu_get_menu(GTK_OPTION_MENU(mnu)));
805         GList *kids = GTK_MENU_SHELL(m)->children;
807         int patpos = 0;
808         int i = 0;
810         for (; kids != NULL; kids = kids->next) {
811             gchar *men_pat = (gchar *) SP_OBJECT_REPR(g_object_get_data(G_OBJECT(kids->data), "pattern"))->attribute("id");
812             if ( strcmp(men_pat, patname) == 0 ) {
813                 patpos = i;
814             }
815             i++;
816         }
818         gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), patpos);
819         gtk_object_set_data(GTK_OBJECT(mnu), "update", GINT_TO_POINTER(FALSE));
820     }
821     //gtk_option_menu_set_history(GTK_OPTION_MENU(mnu), 0);
824 static void
825 sp_paint_selector_set_mode_pattern(SPPaintSelector *psel, SPPaintSelectorMode mode)
827     if (mode == SP_PAINT_SELECTOR_MODE_PATTERN)
828         sp_paint_selector_set_style_buttons(psel, psel->pattern);
830     gtk_widget_set_sensitive(psel->style, TRUE);
832     GtkWidget *tbl = NULL;
834     if (psel->mode == SP_PAINT_SELECTOR_MODE_PATTERN){
835         /* Already have pattern menu */
836         tbl = (GtkWidget*)gtk_object_get_data(GTK_OBJECT(psel->selector), "pattern-selector");
837     } else {
838         sp_paint_selector_clear_frame(psel);
840         /* Create vbox */
841         tbl = gtk_vbox_new(FALSE, 4);
842         gtk_widget_show(tbl);
844         {
845             GtkWidget *hb = gtk_hbox_new(FALSE, 1);
847             GtkWidget *mnu = gtk_option_menu_new();
848             ink_pattern_menu(mnu);
849             gtk_signal_connect(GTK_OBJECT(mnu), "changed", GTK_SIGNAL_FUNC(sp_psel_pattern_change), psel);
850             gtk_signal_connect(GTK_OBJECT(mnu), "destroy", GTK_SIGNAL_FUNC(sp_psel_pattern_destroy), psel);
851             gtk_object_set_data(GTK_OBJECT(psel), "patternmenu", mnu);
852             g_object_ref( G_OBJECT(mnu));
854             gtk_container_add(GTK_CONTAINER(hb), mnu);
855             gtk_box_pack_start(GTK_BOX(tbl), hb, FALSE, FALSE, AUX_BETWEEN_BUTTON_GROUPS);
856         }
858         {
859             GtkWidget *hb = gtk_hbox_new(FALSE, 0);
860             GtkWidget *l = gtk_label_new(NULL);
861             gtk_label_set_markup(GTK_LABEL(l), _("Use <b>Object &gt; Pattern &gt; Objects to Pattern</b> to create a new pattern from selection."));
862             gtk_label_set_line_wrap(GTK_LABEL(l), true);
863             gtk_widget_set_size_request(l, 180, -1);
864             gtk_box_pack_start(GTK_BOX(hb), l, TRUE, TRUE, AUX_BETWEEN_BUTTON_GROUPS);
865             gtk_box_pack_start(GTK_BOX(tbl), hb, FALSE, FALSE, AUX_BETWEEN_BUTTON_GROUPS);
866         }
868         gtk_widget_show_all(tbl);
870         gtk_container_add(GTK_CONTAINER(psel->frame), tbl);
871         psel->selector = tbl;
872         gtk_object_set_data(GTK_OBJECT(psel->selector), "pattern-selector", tbl);
874         gtk_frame_set_label(GTK_FRAME(psel->frame), _("Pattern fill"));
875     }
876 #ifdef SP_PS_VERBOSE
877     g_print("Pattern req\n");
878 #endif
881 SPPattern *
882 sp_paint_selector_get_pattern(SPPaintSelector *psel)
884     SPPattern *pat;
886     g_return_val_if_fail((psel->mode == SP_PAINT_SELECTOR_MODE_PATTERN) , NULL);
888     GtkWidget *patmnu = (GtkWidget *) g_object_get_data(G_OBJECT(psel), "patternmenu");
889     /* no pattern menu if we were just selected */
890     if ( patmnu == NULL ) return NULL;
892     GtkMenu *m = GTK_MENU(gtk_option_menu_get_menu(GTK_OPTION_MENU(patmnu)));
894     pat = pattern_getroot(SP_PATTERN(g_object_get_data(G_OBJECT(gtk_menu_get_active(m)), "pattern")));
896     return pat;
899 void
900 sp_paint_selector_set_flat_color(SPPaintSelector *psel, SPDesktop *desktop, gchar const *color_property, gchar const *opacity_property)
902     SPCSSAttr *css = sp_repr_css_attr_new();
904     SPColor color;
905     gfloat alpha;
906     sp_paint_selector_get_color_alpha(psel, &color, &alpha);
907     guint32 rgba = color.toRGBA32( alpha );
909     gchar b[64];
910     sp_svg_write_color(b, 64, rgba);
912     sp_repr_css_set_property(css, color_property, b);
913     Inkscape::CSSOStringStream osalpha;
914     osalpha << alpha;
915     sp_repr_css_set_property(css, opacity_property, osalpha.str().c_str());
917     sp_desktop_set_style(desktop, css);
919     sp_repr_css_attr_unref(css);
922 SPPaintSelectorMode
923 sp_style_determine_paint_selector_mode(SPStyle *style, bool isfill)
925     SPPaintSelectorMode mode = SP_PAINT_SELECTOR_MODE_UNSET;
926     SPIPaint& target = isfill ? style->fill : style->stroke;
928     if ( !target.set ) {
929         SPPaintSelectorMode mode = SP_PAINT_SELECTOR_MODE_UNSET;
930     } else if ( target.isPaintserver() ) {
931         SPPaintServer *server = isfill? SP_STYLE_FILL_SERVER(style) : SP_STYLE_STROKE_SERVER(style);
933         if (SP_IS_LINEARGRADIENT(server)) {
934             mode = SP_PAINT_SELECTOR_MODE_GRADIENT_LINEAR;
935         } else if (SP_IS_RADIALGRADIENT(server)) {
936             mode = SP_PAINT_SELECTOR_MODE_GRADIENT_RADIAL;
937         } else if (SP_IS_PATTERN(server)) {
938             mode = SP_PAINT_SELECTOR_MODE_PATTERN;
939         } else {
940             g_warning( "file %s: line %d: Unknown paintserver", __FILE__, __LINE__ );
941             mode = SP_PAINT_SELECTOR_MODE_NONE;
942         }
943     } else if ( target.isColor() ) {
944         mode = SP_PAINT_SELECTOR_MODE_COLOR_RGB; // so far only rgb can be read from svg
945     } else if ( target.isNone() ) {
946         mode = SP_PAINT_SELECTOR_MODE_NONE;
947     } else {
948         g_warning( "file %s: line %d: Unknown paint type", __FILE__, __LINE__ );
949         mode = SP_PAINT_SELECTOR_MODE_NONE;
950     }
952     return mode;
955 /*
956   Local Variables:
957   mode:c++
958   c-file-style:"stroustrup"
959   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
960   indent-tabs-mode:nil
961   fill-column:99
962   End:
963 */
964 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :