2009-10-20 Chris Toshok <toshok@ximian.com>
[moon.git] / src / rect.cpp
bloba88a4128f573ae8fd9f909a93447ef15eaacfb40
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * rect.cpp
5 * Copyright 2007 Novell, Inc. (http://www.novell.com)
7 * See the LICENSE file included with the distribution for details.
8 *
9 */
11 #include <config.h>
12 #include <stdlib.h>
14 #include "rect.h"
15 #include "utils.h"
16 #include "moon-path.h"
18 bool
19 Rect::FromStr (const char *s, Rect *r)
21 GArray *values = double_garray_from_str (s, 4);
23 if (!values)
24 return false;
26 *r = Rect (g_array_index (values, double, 0),
27 g_array_index (values, double, 1),
28 g_array_index (values, double, 2),
29 g_array_index (values, double, 3));
31 g_array_free (values, true);
32 return true;
35 Rect
36 Rect::Transform (cairo_matrix_t *transform)
38 Rect rect = *this;
40 if (!transform)
41 return rect;
43 double p1_x = rect.x; double p1_y = rect.y;
44 double p2_x = rect.x+rect.width; double p2_y = rect.y;
45 double p3_x = rect.x+rect.width; double p3_y = rect.y+rect.height;
46 double p4_x = rect.x; double p4_y = rect.y+rect.height;
48 cairo_matrix_transform_point (transform, &p1_x, &p1_y);
49 cairo_matrix_transform_point (transform, &p2_x, &p2_y);
50 cairo_matrix_transform_point (transform, &p3_x, &p3_y);
51 cairo_matrix_transform_point (transform, &p4_x, &p4_y);
53 #define MIN2(v1,v2) ((v1)>(v2)?(v2):(v1))
54 #define MIN4(v1,v2,v3,v4) (MIN2(MIN2(MIN2(v1,v2),v3),v4))
56 #define MAX2(v1,v2) ((v1)>(v2)?(v1):(v2))
57 #define MAX4(v1,v2,v3,v4) (MAX2(MAX2(MAX2(v1,v2),v3),v4))
59 double l = MIN4(p1_x,p2_x,p3_x,p4_x);
60 double t = MIN4(p1_y,p2_y,p3_y,p4_y);
61 double r = MAX4(p1_x,p2_x,p3_x,p4_x);
62 double b = MAX4(p1_y,p2_y,p3_y,p4_y);
64 return Rect (l, t, r-l, b-t);
68 void Rect::Draw (cairo_t *cr, CornerRadius *round) const
70 if (round) {
71 Rect paint = *this;
73 double top_adj = MAX (round->topLeft + round->topRight - paint.width, 0) / 2;
74 double bottom_adj = MAX (round->bottomLeft + round->bottomRight - paint.width, 0) / 2;
75 double left_adj = MAX (round->topLeft + round->bottomLeft - paint.height, 0) / 2;
76 double right_adj = MAX (round->topRight + round->bottomRight - paint.height, 0) / 2;
78 double tlt = round->topLeft - top_adj;
79 cairo_move_to (cr, paint.x + tlt, paint.y);
81 double trt = round->topRight - top_adj;
82 double trr = round->topRight - right_adj;
83 cairo_line_to (cr, paint.x + paint.width - trt, paint.y);
84 cairo_curve_to (cr,
85 paint.x + paint.width - trt + trt * ARC_TO_BEZIER, paint.y,
86 paint.x + paint.width, paint.y + trr * ARC_TO_BEZIER,
87 paint.x + paint.width, paint.y + trr);
89 double brr = round->bottomRight - right_adj;
90 double brb = round->bottomRight - bottom_adj;
91 cairo_line_to (cr, paint.x + paint.width, paint.y + paint.height - brr);
92 cairo_curve_to (cr,
93 paint.x + paint.width, paint.y + paint.height - brr + brr * ARC_TO_BEZIER,
94 paint.x + paint.width + brb * ARC_TO_BEZIER - brb, paint.y + paint.height,
95 paint.x + paint.width - brb, paint.y + paint.height);
97 double blb = round->bottomLeft - bottom_adj;
98 double bll = round->bottomLeft - left_adj;
99 cairo_line_to (cr, paint.x + blb, paint.y + paint.height);
100 cairo_curve_to (cr,
101 paint.x + blb - blb * ARC_TO_BEZIER, paint.y + paint.height,
102 paint.x, paint.y + paint.height - bll * ARC_TO_BEZIER,
103 paint.x, paint.y + paint.height - bll);
105 double tll = round->topLeft - left_adj;
106 cairo_line_to (cr, paint.x, paint.y + tll);
107 cairo_curve_to (cr,
108 paint.x, paint.y + tll - tll * ARC_TO_BEZIER,
109 paint.x + tlt - tlt * ARC_TO_BEZIER, paint.y,
110 paint.x + tlt, paint.y);
111 } else {
112 Draw (cr);