[cairo-commit] cairo-gtk-engine/src caligula-draw.c,1.1,1.2

Kristian Hogsberg commit at pdx.freedesktop.org
Tue Feb 8 20:20:03 PST 2005


Committed by: krh

Update of /cvs/cairo/cairo-gtk-engine/src
In directory gabe:/tmp/cvs-serv8392/src

Modified Files:
	caligula-draw.c 
Log Message:
2005-02-08  Kristian Høgsberg  <krh at redhat.com>

        * src/caligula-draw.c (caligula_draw_box): Add highly crackful
        button drawing code.



Index: caligula-draw.c
===================================================================
RCS file: /cvs/cairo/cairo-gtk-engine/src/caligula-draw.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- caligula-draw.c	9 Feb 2005 03:08:48 -0000	1.1
+++ caligula-draw.c	9 Feb 2005 04:20:01 -0000	1.2
@@ -19,6 +19,8 @@
  * USA
  */
 
+#include <stdlib.h>
+
 #include <cairo.h>
 #include <cairo-xlib.h>
 #include <gdk/gdkx.h>
@@ -57,6 +59,260 @@
     cairo_destroy (cr);
 }
 
+static int
+get_random (int low, int high)
+{
+    return low + random () / (RAND_MAX / (high - low));
+}
+
+#if 0 /* Swirly button */
+
+enum { LEFT, RIGHT };
+#define STRENGTH 10
+
+static void
+draw_swoosh (cairo_t *cr, int side, int x, int y, int width, int height,
+	     int top_x, int top_y, int left_x, int left_y,
+	     int right_x, int right_y, int bottom_x, int bottom_y)
+{
+    cairo_pattern_t *pattern;
+
+    cairo_save (cr);
+
+    cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
+    if (side == LEFT) {
+	cairo_new_path (cr);
+	cairo_move_to (cr, top_x, y);
+	cairo_line_to (cr, right_x + 1, y);
+	cairo_line_to (cr, right_x + 1, top_y - 1);
+	cairo_line_to (cr, left_x - 1, top_y - 1);
+	cairo_line_to (cr, left_x - 1, bottom_y + 1);
+	cairo_line_to (cr, x, bottom_y + 1);
+	cairo_line_to (cr, x, left_y);
+	cairo_close_path (cr);
+	cairo_clip (cr);
+    }
+    else {
+	cairo_new_path (cr);
+	cairo_move_to (cr, bottom_x, y + height);
+	cairo_line_to (cr, x + width, right_y);
+	cairo_line_to (cr, x + width, top_y - 1);
+	cairo_line_to (cr, right_x + 1, top_y - 1);
+	cairo_line_to (cr, right_x + 1, bottom_y + 1);
+	cairo_line_to (cr, left_x - 1, bottom_y + 1);
+	cairo_line_to (cr, left_x - 1, y + height);
+	cairo_close_path (cr);
+	cairo_clip (cr);
+    }
+
+    cairo_new_path (cr);
+    cairo_move_to (cr, top_x, top_y);
+    cairo_curve_to (cr,
+		    top_x + STRENGTH, top_y,
+		    right_x, right_y - STRENGTH,
+		    right_x, right_y);
+
+    cairo_curve_to (cr,
+		    right_x, right_y + STRENGTH,
+		    bottom_x + STRENGTH, bottom_y,
+		    bottom_x, bottom_y);
+
+    cairo_curve_to (cr,
+		    bottom_x - STRENGTH, bottom_y,
+		    left_x, left_y + STRENGTH,
+		    left_x, left_y);
+
+    cairo_curve_to (cr,
+		    left_x, left_y - STRENGTH,
+		    top_x - STRENGTH, top_y,
+		    top_x, top_y);
+    cairo_close_path (cr);
+
+    top_y += 1;
+    left_x += 4;
+    right_x -= 4;
+    bottom_y -= 1;
+
+    cairo_move_to (cr, top_x, top_y);
+    cairo_curve_to (cr,
+		    top_x + STRENGTH, top_y,
+		    right_x, right_y - STRENGTH,
+		    right_x, right_y);
+
+    cairo_curve_to (cr,
+		    right_x, right_y + STRENGTH,
+		    bottom_x + STRENGTH, bottom_y,
+		    bottom_x, bottom_y);
+
+    cairo_curve_to (cr,
+		    bottom_x - STRENGTH, bottom_y,
+		    left_x, left_y + STRENGTH,
+		    left_x, left_y);
+
+    cairo_curve_to (cr,
+		    left_x, left_y - STRENGTH,
+		    top_x - STRENGTH, top_y,
+		    top_x, top_y);
+
+    if (side == LEFT)
+	pattern = cairo_pattern_create_linear (left_x, bottom_y, top_x, top_y);
+    else
+	pattern = cairo_pattern_create_linear (right_x, top_y,
+					       bottom_x, bottom_y);
+
+    cairo_pattern_add_color_stop (pattern, 0, 0, 0, 0.5, 0.5);
+    cairo_pattern_add_color_stop (pattern, 1, 0, 0, 0.2, 1);
+    cairo_set_pattern (cr, pattern);
+    cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
+    cairo_fill (cr);
+    cairo_pattern_destroy (pattern);
+
+    cairo_restore (cr);
+}	
+
+static void
+swoosh (cairo_t *cr, int x, int y, int width, int height)
+{
+    int top_x, top_y, left_x, left_y, right_x, right_y, bottom_x, bottom_y;
+
+    top_x = x + get_random (15, 40);
+    top_y = y - 2;
+    left_x = x - 4;
+    left_y = get_random (y + height / 2 - 5, y + height / 2 + 5);
+    right_x = left_x + get_random (50, 60);
+    right_y = get_random (y + height / 2 - 5, y + height / 2 + 5);
+    bottom_x = x + get_random (10, 20);
+    bottom_y = y + height - get_random (0, 4);
+
+    draw_swoosh (cr, LEFT, x, y, width, height,
+		 top_x, top_y, left_x, left_y,
+		 right_x, right_y, bottom_x, bottom_y);
+
+    top_x = x + width - get_random (10, 20);
+    top_y = y + get_random (0, 4);
+    right_x = x + width + 4;
+    right_y = y + get_random (height / 2 - 5, height / 2 + 5);
+    left_x = right_x - get_random (50, 60);
+    left_y = y + get_random (height / 2 - 5, height / 2 + 5);
+    bottom_x = x + width - get_random (15, 40);
+    bottom_y = y + height + 2;
+
+    draw_swoosh (cr, RIGHT, x, y, width, height,
+		 top_x, top_y, left_x, left_y,
+		 right_x, right_y, bottom_x, bottom_y);
+}	
+
+static void
+draw_button (cairo_t *cr, int x, int y, int width, int height)
+{
+    cairo_text_extents_t extents;
+    cairo_pattern_t *pattern;
+    int text_x, text_y;
+    const double gray = 0.92;
+    const double outline_gray = 0.50;
+
+    /* Gradient fill */
+    pattern = cairo_pattern_create_linear (x, y, x, y + height);
+    cairo_pattern_add_color_stop (pattern, 0, 0, 0, 0, 1);
+    cairo_pattern_add_color_stop (pattern, 0.035, gray, gray, gray, 1);
+    cairo_pattern_add_color_stop (pattern, 0.25, 1, 1, 1, 1);
+    cairo_pattern_add_color_stop (pattern, 0.45, gray, gray, gray, 1);
+    cairo_pattern_add_color_stop (pattern, 0.91, 1, 1, 1, 1);
+    cairo_pattern_add_color_stop (pattern, 1, 0, 0, 0, 0);
+    cairo_set_pattern (cr, pattern);
+
+    cairo_rectangle (cr, x, y, width, height);
+    cairo_fill (cr);
+
+    /* Outline */
+    cairo_set_alpha (cr, 1);
+    cairo_set_rgb_color (cr, outline_gray, outline_gray, outline_gray);
+    cairo_set_line_width (cr, 1);
+    cairo_rectangle (cr, x + 0.5, y + 0.5, width - 1, height - 1);
+    cairo_stroke (cr);
+
+    swoosh (cr, x, y, width, height);
+}
+
+#else  /* Grass like button */
+
+static void
+swoosh (cairo_t *cr, int x, int y, int width, int height)
+{
+    const double swoosh_alpha = 0.10;
+    double top_x, top_y, right_x, right_y;
+
+    top_x = x + get_random (width / 2, width);
+    top_y = y - get_random (0, height * 2);
+    right_x = x + get_random (0, width);
+    right_y = y + height + get_random (0, height);
+
+    cairo_new_path (cr);
+    cairo_move_to (cr, top_x, top_y);
+    cairo_curve_to (cr,
+		    top_x + 5, top_y - 5,
+		    right_x, right_y - 5,
+		    right_x, right_y);
+
+    cairo_curve_to (cr,
+		    right_x, right_y - 5,
+		    top_x - 5, top_y,
+		    top_x, top_y);
+
+    cairo_close_path (cr);
+    cairo_set_rgb_color (cr, 0, 0, 0);
+    cairo_set_alpha (cr, swoosh_alpha);
+    cairo_fill (cr);
+}	
+
+static void
+draw_button (cairo_t *cr, int x, int y, int width, int height)
+{
+    cairo_text_extents_t extents;
+    cairo_pattern_t *pattern;
+    int text_x, text_y;
+    const double gray = 0.92;
+    const double outline_gray = 0.50;
+
+    cairo_save (cr);
+
+    cairo_rectangle (cr, x, y, width, height);
+#if 1
+    cairo_clip (cr);
+#endif
+
+    cairo_set_rgb_color (cr, 1, 1, 1);
+    cairo_fill (cr);
+
+    /* Gradient fill */
+    pattern = cairo_pattern_create_linear (x, y, x, y + height);
+    cairo_pattern_add_color_stop (pattern, 0, 0, 0, 0, 1);
+    cairo_pattern_add_color_stop (pattern, 0.035, gray, gray, gray, 1);
+    cairo_pattern_add_color_stop (pattern, 0.25, 1, 1, 1, 1);
+    cairo_pattern_add_color_stop (pattern, 0.45, gray, gray, gray, 1);
+    cairo_pattern_add_color_stop (pattern, 0.91, 1, 1, 1, 1);
+    cairo_pattern_add_color_stop (pattern, 1, 0, 0, 0, 0);
+    cairo_set_pattern (cr, pattern);
+
+    cairo_rectangle (cr, x, y, width, height);
+    cairo_fill (cr);
+
+    swoosh (cr, x, y, width, height);
+    swoosh (cr, x, y, width, height);
+    swoosh (cr, x, y, width, height);
+
+    /* Outline */
+    cairo_set_alpha (cr, 1);
+    cairo_set_rgb_color (cr, outline_gray, outline_gray, outline_gray);
+    cairo_set_line_width (cr, 1);
+    cairo_rectangle (cr, x + 0.5, y + 0.5, width - 1, height - 1);
+    cairo_stroke (cr);
+
+    cairo_restore (cr);
+}
+
+#endif /* end of buttons */
+
 void
 caligula_draw_box (GtkStyle * style,
 		   GdkWindow * window,
@@ -72,9 +328,7 @@
 
     cr = caligula_begin_paint (window, &x_offset, &y_offset);
   
-    cairo_set_rgb_color (cr, 0.5, 0.5, 1.0);
-    cairo_rectangle (cr, x, y, width, height);
-    cairo_fill (cr);
+    draw_button (cr, x, y, width, height);
 
     caligula_end_paint (cr);
 }




More information about the cairo-commit mailing list