Code

Fix version check for wheel selector.
[inkscape.git] / src / widgets / sp-color-wheel-selector.cpp
index 174b071f972680d3a55be23dacd430500e3b1ecf..2e36a024e858a5f75c694ee42b58b3a78a21ef50 100644 (file)
@@ -10,7 +10,8 @@
 #include "../dialogs/dialog-events.h"
 #include "sp-color-wheel-selector.h"
 #include "sp-color-scales.h"
-
+#include "sp-color-icc-selector.h"
+#include "../svg/svg-icc-color.h"
 
 G_BEGIN_DECLS
 
@@ -107,6 +108,47 @@ void sp_color_wheel_selector_init (SPColorWheelSelector *cs)
     }
 }
 
+static void resizeHSVWheel( GtkHSV *hsv, GtkAllocation *allocation )
+{
+    gint diam = std::min(allocation->width, allocation->height);
+
+    // drop a little for resizing
+    diam -= 4;
+
+    GtkStyle *style = gtk_widget_get_style( GTK_WIDGET(hsv) );
+    if ( style ) {
+        gint thick = std::max(style->xthickness, style->ythickness);
+        if (thick > 0) {
+            diam -= thick * 2;
+        }
+    }
+    gint padding = -1;
+    gtk_widget_style_get( GTK_WIDGET(hsv), 
+                          "focus-padding", &padding,
+                          NULL );
+    if (padding > 0) {
+        diam -= padding * 2;
+    }
+     
+    diam = std::max(20, diam);
+    gint ring = static_cast<gint>( static_cast<gdouble>(diam) / (4.0 * 1.618) );
+    gtk_hsv_set_metrics( hsv, diam, ring );
+}
+
+#if GTK_CHECK_VERSION(2,18,0)
+static void handleWheelStyleSet(GtkHSV *hsv, GtkStyle* /*previous*/, gpointer /*userData*/)
+{
+    GtkAllocation allocation = {0, 0, 0, 0};
+    gtk_widget_get_allocation( GTK_WIDGET(hsv), &allocation );
+    resizeHSVWheel( hsv, &allocation );
+}
+#endif // GTK_CHECK_VERSION(2,18,0)
+
+static void handleWheelAllocation(GtkHSV *hsv, GtkAllocation *allocation, gpointer /*userData*/)
+{
+    resizeHSVWheel( hsv, allocation );
+}
+
 void ColorWheelSelector::init()
 {
     GtkWidget *t;
@@ -124,14 +166,15 @@ void ColorWheelSelector::init()
     /* Create components */
     row = 0;
 
-    _wheel = sp_color_wheel_new ();
-    gtk_widget_show (_wheel);
-    gtk_table_attach (GTK_TABLE (t), _wheel, 0, 3, row, row + 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), XPAD, YPAD);
+    _wheel = gtk_hsv_new();
+    gtk_hsv_set_metrics( GTK_HSV(_wheel), 48, 8 );
+    gtk_widget_show( _wheel );
+    gtk_table_attach( GTK_TABLE(t), _wheel, 0, 3, row, row + 1,  (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), XPAD, YPAD);
 
     row++;
 
     /* Label */
-    _label = gtk_label_new_with_mnemonic (_("_A"));
+    _label = gtk_label_new_with_mnemonic (_("_A:"));
     gtk_misc_set_alignment (GTK_MISC (_label), 1.0, 0.5);
     gtk_widget_show (_label);
     gtk_table_attach (GTK_TABLE (t), _label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, XPAD, YPAD);
@@ -170,8 +213,17 @@ void ColorWheelSelector::init()
     gtk_signal_connect (GTK_OBJECT (_slider), "changed",
                         GTK_SIGNAL_FUNC (_sliderChanged), _csel);
 
-    gtk_signal_connect (GTK_OBJECT(_wheel), "changed",
-                        GTK_SIGNAL_FUNC (_wheelChanged), _csel);
+    gtk_signal_connect( GTK_OBJECT(_wheel), "changed",
+                        GTK_SIGNAL_FUNC(_wheelChanged), _csel );
+
+
+    // GTK does not automatically scale the color wheel, so we have to add that in:
+    gtk_signal_connect( GTK_OBJECT(_wheel), "size-allocate",
+                        GTK_SIGNAL_FUNC(handleWheelAllocation), _csel );
+#if GTK_CHECK_VERSION(2,18,0)
+    gtk_signal_connect( GTK_OBJECT(_wheel), "style-set",
+                        GTK_SIGNAL_FUNC(handleWheelStyleSet), _csel );
+#endif // GTK_CHECK_VERSION(2,18,0)
 }
 
 static void
@@ -205,13 +257,24 @@ sp_color_wheel_selector_new (void)
 
 /* Helpers for setting color value */
 
+static void preserve_icc(SPColor *color, SPColorWheelSelector *cs){
+    ColorSelector* selector = (ColorSelector*)(SP_COLOR_SELECTOR(cs)->base);
+    color->icc = selector->getColor().icc ? new SVGICCColor(*selector->getColor().icc) : 0;
+}
+
 void ColorWheelSelector::_colorChanged()
 {
 #ifdef DUMP_CHANGE_INFO
     g_message("ColorWheelSelector::_colorChanged( this=%p, %f, %f, %f,   %f)", this, color.v.c[0], color.v.c[1], color.v.c[2], alpha );
 #endif
     _updating = TRUE;
-    sp_color_wheel_set_color( SP_COLOR_WHEEL( _wheel ), &_color );
+    {
+        gdouble h = 0;
+        gdouble s = 0;
+        gdouble v = 0;
+        gtk_rgb_to_hsv( _color.v.c[0], _color.v.c[1], _color.v.c[2], &h, &s, &v  );
+        gtk_hsv_set_color( GTK_HSV(_wheel), h, s, v );
+    }
 
     guint32 start = _color.toRGBA32( 0x00 );
     guint32 mid = _color.toRGBA32( 0x7f );
@@ -237,6 +300,7 @@ void ColorWheelSelector::_adjustmentChanged( GtkAdjustment *adjustment, SPColorW
 
     wheelSelector->_updating = TRUE;
 
+    preserve_icc(&wheelSelector->_color, cs);
     wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
 
     wheelSelector->_updating = FALSE;
@@ -249,6 +313,8 @@ void ColorWheelSelector::_sliderGrabbed( SPColorSlider *slider, SPColorWheelSele
     if (!wheelSelector->_dragging) {
         wheelSelector->_dragging = TRUE;
         wheelSelector->_grabbed();
+
+        preserve_icc(&wheelSelector->_color, cs);
         wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
     }
 }
@@ -260,6 +326,8 @@ void ColorWheelSelector::_sliderReleased( SPColorSlider *slider, SPColorWheelSel
     if (wheelSelector->_dragging) {
         wheelSelector->_dragging = FALSE;
         wheelSelector->_released();
+
+        preserve_icc(&wheelSelector->_color, cs);
         wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
     }
 }
@@ -269,15 +337,25 @@ void ColorWheelSelector::_sliderChanged( SPColorSlider *slider, SPColorWheelSele
     (void)slider;
     ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
 
+    preserve_icc(&wheelSelector->_color, cs);
     wheelSelector->_updateInternals( wheelSelector->_color, ColorScales::getScaled( wheelSelector->_adj ), wheelSelector->_dragging );
 }
 
-void ColorWheelSelector::_wheelChanged( SPColorWheel *wheel, SPColorWheelSelector *cs )
+void ColorWheelSelector::_wheelChanged( GtkHSV *hsv, SPColorWheelSelector *cs )
 {
-    ColorWheelSelector* wheelSelector = (ColorWheelSelector*)(SP_COLOR_SELECTOR(cs)->base);
-    SPColor color;
+    ColorWheelSelector* wheelSelector = static_cast<ColorWheelSelector*>(SP_COLOR_SELECTOR(cs)->base);
+
+    gdouble h = 0;
+    gdouble s = 0;
+    gdouble v = 0;
+    gtk_hsv_get_color( hsv, &h, &s, &v );
+    
+    gdouble r = 0;
+    gdouble g = 0;
+    gdouble b = 0;
+    gtk_hsv_to_rgb(h, s, v, &r, &g, &b);
 
-    sp_color_wheel_get_color( wheel, &color );
+    SPColor color(r, g, b);
 
     guint32 start = color.toRGBA32( 0x00 );
     guint32 mid = color.toRGBA32( 0x7f );
@@ -285,7 +363,8 @@ void ColorWheelSelector::_wheelChanged( SPColorWheel *wheel, SPColorWheelSelecto
 
     sp_color_slider_set_colors (SP_COLOR_SLIDER(wheelSelector->_slider), start, mid, end);
 
-    wheelSelector->_updateInternals( color, wheelSelector->_alpha, sp_color_wheel_is_adjusting( wheel ) );
+    preserve_icc(&color, cs);
+    wheelSelector->_updateInternals( color, wheelSelector->_alpha, gtk_hsv_is_adjusting( hsv ) );
 }
 
 
@@ -298,4 +377,4 @@ void ColorWheelSelector::_wheelChanged( SPColorWheel *wheel, SPColorWheelSelecto
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :