2009-11-08 Chris Toshok <toshok@ximian.com>
[moon.git] / src / shape.h
bloba876abced687f8f730d5b99a0a799ce148c3f08c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * shape.h: This match the classes inside System.Windows.Shapes
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007-2008 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
14 #ifndef __SHAPE_H__
15 #define __SHAPE_H__
17 #include <glib.h>
18 #include <cairo.h>
20 #include "geometry.h"
21 #include "frameworkelement.h"
22 #include "moon-path.h"
24 class Brush;
27 // Helpers
30 G_BEGIN_DECLS
32 cairo_fill_rule_t convert_fill_rule (FillRule fill_rule);
33 void calc_line_bounds (double x1, double x2, double y1, double y2, double thickness, PenLineCap start_cap, PenLineCap end_cap, Rect* bounds);
35 G_END_DECLS
39 // Shape class
40 //
41 /* @Namespace=System.Windows.Shapes */
42 class Shape : public FrameworkElement {
43 protected:
44 virtual ~Shape ();
46 Brush *stroke, *fill;
47 cairo_surface_t *cached_surface;
48 gint64 cached_size;
49 bool needs_clip;
51 void DoDraw (cairo_t *cr, bool do_op);
53 void SetupLineCaps (cairo_t *cr);
54 void SetupLineJoinMiter (cairo_t *cr);
55 virtual bool SetupLine (cairo_t* cr);
56 bool SetupDashes (cairo_t *cr, double thickness);
57 bool SetupDashes (cairo_t *cr, double thickness, double offset);
58 bool Fill (cairo_t *cr, bool do_op);
59 void Clip (cairo_t *cr);
60 virtual bool DrawShape (cairo_t *cr, bool do_op) { g_warning ("%s does not implement DrawShape ().", GetTypeName ()); return false; }
61 // virtual bool DrawDegenerateShape (cairo_t *cr, bool do_op) = 0;
63 moon_path *path;
64 virtual void InvalidatePathCache (bool free = false);
65 void InvalidateSurfaceCache (void);
66 void InvalidateNaturalBounds ();
67 void InvalidateFillBounds ();
68 void InvalidateStrokeBounds ();
69 void InvalidateStretch ();
71 Rect GetStretchExtents ();
72 Rect GetNaturalBounds ();
74 bool IsCandidateForCaching (void);
76 virtual Rect ComputeShapeBounds (bool logical) { return ComputeShapeBounds (logical, NULL); }
77 virtual Rect ComputeShapeBounds (bool logical, cairo_matrix_t * matrix);
79 virtual void ShiftPosition (Point p);
80 virtual void TransformBounds (cairo_matrix_t *old, cairo_matrix_t *current);
82 virtual Rect ComputeStretchBounds ();
84 DoubleCollection *GetStrokeDashArray ();
85 Rect natural_bounds;
86 public:
87 cairo_matrix_t stretch_transform;
88 /* @PropertyType=Brush,GenerateAccessors */
89 const static int FillProperty;
90 /* @PropertyType=Stretch,AutoCreator=Shape::CreateDefaultStretch,GenerateAccessors */
91 const static int StretchProperty;
92 /* @PropertyType=Brush,GenerateAccessors */
93 const static int StrokeProperty;
94 /* @PropertyType=DoubleCollection,GenerateAccessors */
95 const static int StrokeDashArrayProperty;
96 /* @PropertyType=PenLineCap,DefaultValue=PenLineCapFlat,GenerateAccessors */
97 const static int StrokeDashCapProperty;
98 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors */
99 const static int StrokeDashOffsetProperty;
100 /* @PropertyType=PenLineCap,DefaultValue=PenLineCapFlat,GenerateAccessors */
101 const static int StrokeEndLineCapProperty;
102 /* @PropertyType=PenLineJoin,DefaultValue=PenLineJoinMiter,GenerateAccessors */
103 const static int StrokeLineJoinProperty;
104 /* @PropertyType=double,DefaultValue=10.0,GenerateAccessors */
105 const static int StrokeMiterLimitProperty;
106 /* @PropertyType=PenLineCap,DefaultValue=PenLineCapFlat,GenerateAccessors */
107 const static int StrokeStartLineCapProperty;
108 /* @PropertyType=double,DefaultValue=1.0,GenerateAccessors */
109 const static int StrokeThicknessProperty;
111 /* @GenerateCBinding,GeneratePInvoke,ManagedAccess=Protected */
112 Shape ();
114 /* @GenerateCBinding,GeneratePInvoke */
115 virtual Transform *GetGeometryTransform ();
118 // Overrides from UIElement.
120 virtual Size ComputeActualSize ();
121 virtual Size MeasureOverride (Size Availablesize);
122 virtual Size ArrangeOverride (Size finalSize);
123 virtual void Render (cairo_t *cr, Region *region, bool path_only = false);
124 virtual void GetSizeForBrush (cairo_t *cr, double *width, double *height);
125 virtual void ComputeBounds ();
126 virtual bool InsideObject (cairo_t *cr, double x, double y);
127 virtual Point GetOriginPoint () { return extents.GetTopLeft (); }
130 // new virtual methods for shapes
132 virtual bool IsStroked () { return stroke; }
133 virtual bool IsFilled () { return fill; }
134 virtual bool CanFill () { return false; }
135 virtual bool CanFindElement () { return IsFilled () || IsStroked (); }
136 virtual FillRule GetFillRule () { return FillRuleNonzero; }
139 // Draw: draws the Shape in the cairo context (affine transforms are set before this
140 // is called).
142 // This is called multiple times: one for fills, one for strokes
143 // if they are both set. It will also be called to compute the bounding box.
145 virtual void Draw (cairo_t *cr);
146 virtual void BuildPath () {};
147 void Stroke (cairo_t *cr, bool do_op);
148 bool NeedsClipping ();
150 virtual void CacheInvalidateHint (void);
151 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
152 virtual void OnSubPropertyChanged (DependencyProperty *prop, DependencyObject *obj, PropertyChangedEventArgs *subobj_args);
153 virtual Point GetTransformOrigin ();
155 // State helpers
156 bool IsEmpty () { return (flags & UIElement::SHAPE_EMPTY); };
157 bool IsNormal () { return (flags & UIElement::SHAPE_NORMAL); };
158 bool IsDegenerate () { return (flags & UIElement::SHAPE_DEGENERATE); };
159 bool HasRadii () { return (flags & UIElement::SHAPE_RADII); };
160 void SetShapeFlags (UIElementFlags sf) { flags &= ~UIElement::SHAPE_MASK; flags |= sf; };
161 void AddShapeFlags (UIElementFlags sf) { flags |= sf; };
164 // Property Accessors
166 void SetFill (Brush *fill);
167 Brush *GetFill ();
169 void SetStroke (Brush *stroke);
170 Brush *GetStroke ();
172 void SetStretch (Stretch stretch);
173 Stretch GetStretch ();
175 void SetStrokeDashArray (DoubleCollection *dashes);
177 void SetStrokeDashCap (PenLineCap cap);
178 PenLineCap GetStrokeDashCap ();
180 void SetStrokeDashOffset (double offset);
181 double GetStrokeDashOffset ();
183 void SetStrokeEndLineCap (PenLineCap cap);
184 PenLineCap GetStrokeEndLineCap ();
186 void SetStrokeLineJoin (PenLineJoin join);
187 PenLineJoin GetStrokeLineJoin ();
189 void SetStrokeMiterLimit (double limit);
190 double GetStrokeMiterLimit ();
192 void SetStrokeStartLineCap (PenLineCap cap);
193 PenLineCap GetStrokeStartLineCap ();
195 void SetStrokeThickness (double thickness);
196 double GetStrokeThickness ();
198 static Value* CreateDefaultStretch (DependencyObject *instance, DependencyProperty *property);
203 // Ellipse
205 /* @Namespace=System.Windows.Shapes */
206 class Ellipse : public Shape {
207 protected:
208 virtual ~Ellipse () {}
209 virtual bool DrawShape (cairo_t *cr, bool do_op);
210 virtual Rect ComputeShapeBounds (bool logical);
211 virtual Rect ComputeStretchBounds ();
213 public:
214 /* @GenerateCBinding,GeneratePInvoke */
215 Ellipse ();
217 virtual void BuildPath ();
218 virtual bool CanFill () { return true; }
220 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
225 // Rectangle class
227 /* @Namespace=System.Windows.Shapes */
228 class Rectangle : public Shape {
229 protected:
230 virtual ~Rectangle () {}
231 virtual bool DrawShape (cairo_t *cr, bool do_op);
232 virtual Rect ComputeShapeBounds (bool logical);
233 virtual Rect ComputeStretchBounds ();
235 public:
236 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors */
237 const static int RadiusXProperty;
238 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors */
239 const static int RadiusYProperty;
241 /* @GenerateCBinding,GeneratePInvoke */
242 Rectangle ();
244 virtual Rect GetCoverageBounds ();
245 virtual void BuildPath ();
246 virtual bool CanFill () { return true; }
248 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
251 // Property Accessors
253 void SetRadiusX (double radius);
254 double GetRadiusX ();
256 void SetRadiusY (double radius);
257 double GetRadiusY ();
262 // Line class
264 /* @Namespace=System.Windows.Shapes */
265 class Line : public Shape {
266 protected:
267 virtual ~Line () {}
268 virtual bool DrawShape (cairo_t *cr, bool do_op);
269 virtual Rect ComputeShapeBounds (bool logical);
271 public:
272 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors */
273 const static int X1Property;
274 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors */
275 const static int Y1Property;
276 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors */
277 const static int X2Property;
278 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors */
279 const static int Y2Property;
281 /* @GenerateCBinding,GeneratePInvoke */
282 Line () { SetObjectType (Type::LINE); }
284 virtual void BuildPath ();
286 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
288 // Line has no center to compute, it's always 0,0 because it provides it's own start and end
289 // virtual Point GetTransformOrigin ();
292 // Property Accessors
294 void SetX1 (double x1);
295 double GetX1 ();
297 void SetY1 (double y1);
298 double GetY1 ();
300 void SetX2 (double x2);
301 double GetX2 ();
303 void SetY2 (double y2);
304 double GetY2 ();
309 // Polygon
311 /* @Namespace=System.Windows.Shapes */
312 class Polygon : public Shape {
313 protected:
314 virtual ~Polygon () {}
315 virtual bool DrawShape (cairo_t *cr, bool do_op);
317 PointCollection *GetPoints ();
319 public:
320 /* @PropertyType=FillRule,DefaultValue=FillRuleEvenOdd,GenerateAccessors */
321 const static int FillRuleProperty;
322 /* @PropertyType=PointCollection,AutoCreateValue,GenerateAccessors */
323 const static int PointsProperty;
325 /* @GenerateCBinding,GeneratePInvoke */
326 Polygon ();
328 // Polygon has no center to compute, it's always 0,0 because it provides it's own start and end
329 // virtual Point GetTransformOrigin ();
331 virtual void BuildPath ();
333 virtual bool CanFill () { return true; }
335 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
336 virtual void OnCollectionItemChanged (Collection *col, DependencyObject *obj, PropertyChangedEventArgs *args);
337 virtual void OnCollectionChanged (Collection *col, CollectionChangedEventArgs *args);
340 // Property Accessors
342 void SetFillRule (FillRule rule);
343 virtual FillRule GetFillRule ();
345 void SetPoints (PointCollection *points);
350 // Polyline
352 /* @Namespace=System.Windows.Shapes */
353 class Polyline : public Shape {
354 protected:
355 virtual ~Polyline () {}
356 virtual bool DrawShape (cairo_t *cr, bool do_op);
358 PointCollection *GetPoints ();
360 public:
361 /* @PropertyType=FillRule,DefaultValue=FillRuleEvenOdd,GenerateAccessors */
362 const static int FillRuleProperty;
363 /* @PropertyType=PointCollection,AutoCreateValue,GenerateAccessors */
364 const static int PointsProperty;
366 /* @GenerateCBinding,GeneratePInvoke */
367 Polyline ();
369 // Polyline has no center to compute, it's always 0,0 because it provides it's own start and end
370 // virtual Point GetTransformOrigin ();
372 virtual void BuildPath ();
374 virtual bool CanFill () { return true; }
376 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
377 virtual void OnCollectionItemChanged (Collection *col, DependencyObject *obj, PropertyChangedEventArgs *args);
378 virtual void OnCollectionChanged (Collection *col, CollectionChangedEventArgs *args);
381 // Property Accessors
383 void SetFillRule (FillRule rule);
384 virtual FillRule GetFillRule ();
386 void SetPoints (PointCollection *points);
391 // Path
393 /* @Namespace=System.Windows.Shapes */
394 class Path : public Shape {
395 protected:
396 virtual ~Path () {}
397 virtual bool SetupLine (cairo_t *cr);
398 virtual bool DrawShape (cairo_t *cr, bool do_op);
399 virtual Rect ComputeShapeBounds (bool logical, cairo_matrix_t *matrix);
401 public:
402 /* @PropertyType=Geometry,GenerateAccessors */
403 const static int DataProperty;
405 /* @GenerateCBinding,GeneratePInvoke */
406 Path () { SetObjectType (Type::PATH); }
408 // Path has no center to compute, it's always 0,0 because it provides it's own start and end
409 // virtual Point GetTransformOrigin ();
411 virtual void Draw (cairo_t *cr);
413 virtual bool CanFill () { return true; }
414 virtual FillRule GetFillRule ();
416 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
417 virtual void OnSubPropertyChanged (DependencyProperty *prop, DependencyObject *obj, PropertyChangedEventArgs *subobj_args);
420 // Property Accessors
422 void SetData (Geometry *data);
423 Geometry *GetData ();
426 #endif /* __SHAPE_H__ */