[monet/monet-xml] Add simple parser for width and height of rectangle elements



commit 67102dde79442efae863b175b3826a830d23a9ed
Author: Thomas Wood <thomas wood intel com>
Date:   Sun Jun 13 16:24:01 2010 +0100

    Add simple parser for width and height of rectangle elements

 monet-gtk/monet.xml |   10 ++++----
 monet/mn-config.c   |   20 ++++++++++++++--
 monet/mn-config.h   |    4 +-
 monet/mn-style.c    |   62 ++++++++++++++++++++++++++++++++------------------
 4 files changed, 64 insertions(+), 32 deletions(-)
---
diff --git a/monet-gtk/monet.xml b/monet-gtk/monet.xml
index 0e6caaf..5f23fdc 100644
--- a/monet-gtk/monet.xml
+++ b/monet-gtk/monet.xml
@@ -1,22 +1,22 @@
 <monet>
 <widget type="button">
-  <rect x="0" y="0" width="-1" height="-1" stroke-width="1" stroke="#988c7c" corner-radius="4">
+  <rect x="0" y="0" width="w" height="h" stroke-width="1" stroke="#988c7c" corner-radius="4">
     <gradient x1="0" y1="0" x2="0" y1="50">
       <stop color="#fcfbfa" position="1"/>
       <stop color="#e7e2da" position="0"/>
     </gradient>
   </rect>
-  <rect x="1" y="1" width="-3" height="-3" stroke-width="1" stroke="#fff" corner-radius="3"></rect>
+  <rect x="1" y="1" width="w-2" height="h-2" stroke-width="1" stroke="#fff" corner-radius="3"></rect>
 </widget>
 
 <widget type="button" state="active">
-  <rect x="0" y="0" width="-1" height="-1" stroke-width="1" stroke="#988c7c" corner-radius="4">
+  <rect x="0" y="0" width="w" height="h" stroke-width="1" stroke="#988c7c" corner-radius="4">
     <gradient x1="0" y1="0" x2="0" y1="50">
       <stop color="#fcfbfa" position="0"/>
       <stop color="#e7e2da" position="1"/>
     </gradient>
   </rect>
-  <rect x="1" y="1" width="-3" height="-3" stroke-width="1" stroke="#fff" corner-radius="3"></rect>
+  <rect x="1" y="1" width="w-2" height="h-2" stroke-width="1" stroke="#fff" corner-radius="3"></rect>
 </widget>
 
 <widget type="entry">
@@ -26,7 +26,7 @@
   <line x1="-1" y1="0" y2="-1" x1="-1" stroke="#fff" stroke-width="1"/>
   <line x1="-1" y1="-1" y2="-1" x1="0" stroke="#fff" stroke-width="1"/>
 
-  <rect x="1" y="1" width="-3" height="-3" stroke="#ccc" stroke-width="1"/>
+  <rect x="1" y="1" width="w-2" height="w-2" stroke="#ccc" stroke-width="1"/>
 </widget>
 
 <widget type="radio">
diff --git a/monet/mn-config.c b/monet/mn-config.c
index 5c5b18f..a4eb76d 100644
--- a/monet/mn-config.c
+++ b/monet/mn-config.c
@@ -69,13 +69,27 @@ mn_config_dispose (GObject *object)
 }
 
 static void
+mn_config_free_drawing_op (MnDrawingOp *op)
+{
+  if (op->type == MN_RECT)
+    {
+      MnRectangleOp *rect = (MnRectangleOp *) op;
+
+      g_free (rect->width);
+      g_free (rect->height);
+    }
+
+  g_free (op);
+}
+
+static void
 mn_config_free_ops (GSList **ops)
 {
   gint i;
 
   for (i = 0; i < 4; i++)
     {
-      g_slist_foreach (ops[i], (GFunc) g_free, NULL);
+      g_slist_foreach (ops[i], (GFunc) mn_config_free_drawing_op, NULL);
       g_slist_free (ops[i]);
       ops[i] = NULL;
     }
@@ -250,9 +264,9 @@ widget_start_element (GMarkupParseContext  *context,
           else if (!strcmp (*attr_n, "y"))
             op->y = g_ascii_strtod (attribute_values[i], NULL);
           else if (!strcmp (*attr_n, "width"))
-            op->width = g_ascii_strtod (attribute_values[i], NULL);
+            op->width = g_strdup (attribute_values[i]);
           else if (!strcmp (*attr_n, "height"))
-            op->height = g_ascii_strtod (attribute_values[i], NULL);
+            op->height = g_strdup (attribute_values[i]);
           else if (!strcmp (*attr_n, "stroke"))
             ((MnDrawingOp*) op)->stroke = cairo_pattern_from_paint (attribute_values[i]);
           else if (!strcmp (*attr_n, "stroke-width"))
diff --git a/monet/mn-config.h b/monet/mn-config.h
index 47089b8..ed2753b 100644
--- a/monet/mn-config.h
+++ b/monet/mn-config.h
@@ -102,8 +102,8 @@ typedef struct
 
   double x;
   double y;
-  double width;
-  double height;
+  gchar *width;
+  gchar *height;
 
   double radius;
 } MnRectangleOp;
diff --git a/monet/mn-style.c b/monet/mn-style.c
index 019a7cc..ee53143 100644
--- a/monet/mn-style.c
+++ b/monet/mn-style.c
@@ -132,6 +132,29 @@ mn_style_load_config (MnStyle      *style,
   return (style->priv->config != NULL);
 }
 
+static double
+parse_expression (gchar  *expression,
+                  double  w1,
+                  double  h1)
+{
+  gchar *e;
+  double result = 0;
+  double num;
+
+  for (e = expression; *e; e = e + 1)
+    {
+      if (e[0] == 'w')
+        num = w1;
+      else if (e[0] == 'h')
+        num = h1;
+      else
+        num = g_ascii_strtod (e, &e);
+      result = result + num;
+    }
+
+  return result;
+}
+
 static void
 mn_style_draw_ops (MnStyle   *style,
                    GSList   **ops,
@@ -162,7 +185,7 @@ mn_style_draw_ops (MnStyle   *style,
       MnRectangleOp *rect;
       MnCircleOp *circle;
       MnLineOp *line;
-      gdouble w, h;
+      gdouble ax, ay, w, h;
       gdouble x_1, y_1, x_2, y_2;
       gdouble offset;
 
@@ -174,20 +197,15 @@ mn_style_draw_ops (MnStyle   *style,
         case MN_RECT:
           rect = (MnRectangleOp *) o->data;
 
-          offset = op->stroke_width / 2;
-
-          if (rect->width <= 0)
-            w = width + rect->width;
-          else
-            w = rect->width;
+          ax = rect->x;
+          ay = rect->y;
+          w = parse_expression (rect->width, width, height);
+          h = parse_expression (rect->height, width, height);
 
-          if (rect->height <= 0)
-            h = height + rect->height;
-          else
-            h = rect->height;
+          offset = op->stroke_width / 2;
 
           if (rect->radius == 0)
-            cairo_rectangle (cr, rect->x + offset, rect->y + offset,
+            cairo_rectangle (cr, ax + offset, ay + offset,
                              w - offset * 2, h - offset * 2);
           else
             {
@@ -195,24 +213,24 @@ mn_style_draw_ops (MnStyle   *style,
               w--; h--;
 
               cairo_arc (cr,
-                         rect->x + rect->radius + offset,
-                         rect->y + rect->radius + offset,
+                         ax + rect->radius + offset,
+                         ay + rect->radius + offset,
                          rect->radius, M_PI, M_PI * 1.5);
               cairo_arc (cr,
-                         rect->x + w - rect->radius - offset,
-                         rect->y + rect->radius + offset,
+                         ax + w - rect->radius - offset,
+                         ay + rect->radius + offset,
                          rect->radius, M_PI * 1.5, 0);
               cairo_arc (cr,
-                         rect->x + w - rect->radius - offset,
-                         rect->y + h - rect->radius - offset,
+                         ax + w - rect->radius - offset,
+                         ay + h - rect->radius - offset,
                          rect->radius, 0, M_PI * 0.5);
               cairo_arc (cr,
-                         rect->x + rect->radius + offset,
-                         rect->y + h - rect->radius - offset,
+                         ax + rect->radius + offset,
+                         ay + h - rect->radius - offset,
                          rect->radius, M_PI * 0.5, M_PI);
               cairo_line_to (cr,
-                             rect->x + offset,
-                             rect->y + rect->radius + offset);
+                             ax + offset,
+                             ay + rect->radius + offset);
             }
           break;
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]