]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcolorscale.c
filechooserbutton: Update the button's state only on programmatic changes to the...
[~andy/gtk] / gtk / gtkcolorscale.c
index c1cc6605ac0fc4bc500506c12ea23c425412b1f6..88e4459b59441b8886d0cb51f86ec27670d5d674 100644 (file)
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "config.h"
 
-#include "gtkcolorscale.h"
+#include "gtkcolorscaleprivate.h"
 
+#include "gtkcolorchooserprivate.h"
 #include "gtkcolorutils.h"
 #include "gtkorientable.h"
 #include "gtkstylecontext.h"
 #include "gtkaccessible.h"
+#include "gtkpressandholdprivate.h"
 #include "gtkprivate.h"
 #include "gtkintl.h"
 
+#include <math.h>
+
 struct _GtkColorScalePrivate
 {
   cairo_surface_t *surface;
   gint width, height;
   GdkRGBA color;
   GtkColorScaleType type;
+
+  GtkPressAndHold *press_and_hold;
 };
 
 enum
@@ -44,25 +48,55 @@ enum
 
 G_DEFINE_TYPE (GtkColorScale, gtk_color_scale, GTK_TYPE_SCALE)
 
-static cairo_pattern_t *
-get_checkered_pattern (void)
+static void
+gtk_color_scale_get_trough_size (GtkColorScale *scale,
+                                 gint *x_offset_out,
+                                 gint *y_offset_out,
+                                 gint *width_out,
+                                 gint *height_out)
 {
-  /* need to respect pixman's stride being a multiple of 4 */
-  static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
-                                   0x00, 0xFF, 0x00, 0x00 };
-  static cairo_surface_t *checkered = NULL;
-  cairo_pattern_t *pattern;
+  GtkWidget *widget = GTK_WIDGET (scale);
+  gint width, height, focus_line_width, focus_padding;
+  gint x_offset, y_offset;
+  gint slider_width, slider_height;
+
+  gtk_widget_style_get (widget,
+                        "focus-line-width", &focus_line_width,
+                        "focus-padding", &focus_padding,
+                        "slider-width", &slider_width,
+                        "slider-length", &slider_height,
+                        NULL);
+
+  width = gtk_widget_get_allocated_width (widget) - 2 * (focus_line_width + focus_padding);
+  height = gtk_widget_get_allocated_height (widget) - 2 * (focus_line_width + focus_padding);
 
-  if (checkered == NULL)
-    checkered = cairo_image_surface_create_for_data (data,
-                                                     CAIRO_FORMAT_A8,
-                                                     2, 2, 4);
+  x_offset = focus_line_width + focus_padding;
+  y_offset = focus_line_width + focus_padding;
 
-  pattern = cairo_pattern_create_for_surface (checkered);
-  cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
-  cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
+  /* if the slider has a vertical shape, draw the trough asymmetric */
+  if (slider_width > slider_height)
+    {
+      if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
+        {
+          if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
+            x_offset += (gint) floor (slider_width / 2.0);
+
+          width = (gint) floor (slider_width / 2.0);
+        }
+      else
+        {
+          height = (gint) floor (slider_width / 2.0);
+        }
+    }
 
-  return pattern;
+  if (width_out)
+    *width_out = width;
+  if (height_out)
+    *height_out = height;
+  if (x_offset_out)
+    *x_offset_out = x_offset;
+  if (y_offset_out)
+    *y_offset_out = y_offset;
 }
 
 static void
@@ -75,8 +109,9 @@ create_surface (GtkColorScale *scale)
   if (!gtk_widget_get_realized (widget))
     return;
 
-  width = gtk_widget_get_allocated_width (widget);
-  height = gtk_widget_get_allocated_height (widget);
+  gtk_color_scale_get_trough_size (scale,
+                                   NULL, NULL,
+                                   &width, &height);
 
   if (!scale->priv->surface ||
       width != scale->priv->width ||
@@ -152,7 +187,7 @@ create_surface (GtkColorScale *scale)
       cairo_paint (cr);
       cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
 
-      pattern = get_checkered_pattern ();
+      pattern = _gtk_color_chooser_get_checkered_pattern ();
       cairo_matrix_init_scale (&matrix, 0.125, 0.125);
       cairo_pattern_set_matrix (pattern, &matrix);
       cairo_mask (cr, pattern);
@@ -171,55 +206,37 @@ create_surface (GtkColorScale *scale)
     }
 }
 
-static gboolean
-scale_has_asymmetric_thumb (GtkWidget *widget)
-{
-  gchar *theme;
-  gboolean res;
-
-  g_object_get (gtk_widget_get_settings (widget),
-                "gtk-theme-name", &theme,
-                NULL);
-  res = strcmp ("Adwaita", theme) == 0;
-  g_free (theme);
-
-  return res;
-}
-
 static gboolean
 scale_draw (GtkWidget *widget,
             cairo_t   *cr)
 {
   GtkColorScale *scale = GTK_COLOR_SCALE (widget);
-  gint width, height;
-
-  width = gtk_widget_get_allocated_width (widget);
-  height = gtk_widget_get_allocated_height (widget);
+  gint width, height, x_offset, y_offset;
+  cairo_pattern_t *pattern;
 
   create_surface (scale);
+  gtk_color_scale_get_trough_size (scale,
+                                   &x_offset, &y_offset,
+                                   &width, &height);
 
   cairo_save (cr);
+  cairo_translate (cr, x_offset, y_offset);
+  cairo_rectangle (cr, 0, 0, width, height);
 
-  if (scale_has_asymmetric_thumb (widget))
+  pattern = cairo_pattern_create_for_surface (scale->priv->surface);
+  if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL &&
+      gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
     {
-      if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
-        {
-          if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
-            cairo_rectangle (cr, 1, 1, width / 2 - 1, height - 2);
-          else
-            cairo_rectangle (cr, width / 2, 1, width / 2 - 1, height - 2);
-        }
-      else
-        {
-          cairo_rectangle (cr, 1, 1, width - 2, height / 2);
-        }
+      cairo_matrix_t matrix;
+
+      cairo_matrix_init_scale (&matrix, -1, 1);
+      cairo_matrix_translate (&matrix, -width, 0);
+      cairo_pattern_set_matrix (pattern, &matrix);
     }
-  else
-    cairo_rectangle (cr, 1, 1, width - 2, height - 2);
+  cairo_set_source (cr, pattern);
+  cairo_fill (cr);
 
-  cairo_clip (cr);
-  cairo_set_source_surface (cr, scale->priv->surface, 0, 0);
-  cairo_paint (cr);
+  cairo_pattern_destroy (pattern);
 
   cairo_restore (cr);
 
@@ -234,6 +251,7 @@ gtk_color_scale_init (GtkColorScale *scale)
   scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
                                              GTK_TYPE_COLOR_SCALE,
                                              GtkColorScalePrivate);
+  gtk_widget_add_events (GTK_WIDGET (scale), GDK_TOUCH_MASK);
 }
 
 static void
@@ -244,6 +262,8 @@ scale_finalize (GObject *object)
   if (scale->priv->surface)
     cairo_surface_destroy (scale->priv->surface);
 
+  g_clear_object (&scale->priv->press_and_hold);
+
   G_OBJECT_CLASS (gtk_color_scale_parent_class)->finalize (object);
 }
 
@@ -304,6 +324,48 @@ scale_set_property (GObject      *object,
     }
 }
 
+static void
+hold_action (GtkPressAndHold *pah,
+             gint             x,
+             gint             y,
+             GtkColorScale   *scale)
+{
+  gboolean handled;
+
+  g_signal_emit_by_name (scale, "popup-menu", &handled);
+}
+
+static gboolean
+scale_touch (GtkWidget     *widget,
+             GdkEventTouch *event)
+{
+  GtkColorScale *scale = GTK_COLOR_SCALE (widget);
+
+  if (!scale->priv->press_and_hold)
+    {
+      gint drag_threshold;
+
+      g_object_get (gtk_widget_get_settings (widget),
+                    "gtk-dnd-drag-threshold", &drag_threshold,
+                    NULL);
+
+      scale->priv->press_and_hold = gtk_press_and_hold_new ();
+
+      g_object_set (scale->priv->press_and_hold,
+                    "drag-threshold", drag_threshold,
+                    "hold-time", 1000,
+                    NULL);
+
+      g_signal_connect (scale->priv->press_and_hold, "hold",
+                        G_CALLBACK (hold_action), scale);
+    }
+
+  gtk_press_and_hold_process_event (scale->priv->press_and_hold, (GdkEvent *)event);
+
+  return GTK_WIDGET_CLASS (gtk_color_scale_parent_class)->touch_event (widget, event);
+}
+
+
 static void
 gtk_color_scale_class_init (GtkColorScaleClass *class)
 {
@@ -315,6 +377,7 @@ gtk_color_scale_class_init (GtkColorScaleClass *class)
   object_class->set_property = scale_set_property;
 
   widget_class->draw = scale_draw;
+  widget_class->touch_event = scale_touch;
 
   g_object_class_install_property (object_class, PROP_SCALE_TYPE,
       g_param_spec_int ("scale-type", P_("Scale type"), P_("Scale type"),