add the 2.1-bootstrap dir to MONO_PATH when running smcs
[moon.git] / src / uielement.h
blob5fb7778c310ac006ce55b037958864b7bbbb8c4d
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * uielement.h
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
14 #ifndef __MOON_UIELEMENT_H__
15 #define __MOON_UIELEMENT_H__
17 #include <glib.h>
19 #include "dependencyobject.h"
20 #include "resources.h"
21 #include "point.h"
22 #include "rect.h"
23 #include "region.h"
24 #include "list.h"
25 #include "size.h"
26 #include "layoutinformation.h"
28 #define QUANTUM_ALPHA 1
30 #if QUANTUM_ALPHA
31 #define IS_TRANSLUCENT(x) (x * 255 < 254.5)
32 #define IS_INVISIBLE(x) (x * 255 < .5)
33 #else
34 #define IS_TRANSLUCENT(x) (x < 1.0)
35 #define IS_INVISIBLE(x) (x <= 0.0)
36 #endif
38 class Surface;
40 /* @Namespace=System.Windows */
41 class UIElement : public DependencyObject {
42 public:
43 UIElement ();
44 virtual void Dispose ();
46 int dirty_flags;
47 List::Node *up_dirty_node;
48 List::Node *down_dirty_node;
50 bool force_invalidate_of_new_bounds;
51 bool emitting_loaded;
53 Region *dirty_region;
55 int DumpHierarchy (UIElement *obj);
57 enum UIElementFlags {
58 IS_LOADED = 0x01,
60 // these two flags correspond to the 2 states of VisibilityProperty
61 RENDER_VISIBLE = 0x02,
63 // the HitTestVisible property
64 HIT_TEST_VISIBLE = 0x04,
66 TOTAL_RENDER_VISIBLE = 0x08,
67 TOTAL_HIT_TEST_VISIBLE = 0x10,
69 SHAPE_EMPTY = 0x20, // there's is nothing to draw, the cached path may be NULL
70 SHAPE_NORMAL = 0x40, // normal drawing
71 SHAPE_DEGENERATE = 0x80, // degenerate drawing, use the Stroke brush for filling
72 SHAPE_RADII = 0x100,
73 SHAPE_MASK = (SHAPE_EMPTY | SHAPE_NORMAL | SHAPE_DEGENERATE | SHAPE_RADII),
75 // this means the element will be emitting OnLoaded
76 // shortly, and any child added to the element while
77 // it is in this state should post Loaded as well.
78 PENDING_LOADED = 0x200
81 virtual TimeManager *GetTimeManager ();
83 virtual bool PermitsMultipleParents () { return false; }
85 virtual void SetVisualParent (UIElement *visual_parent);
86 /* @GenerateCBinding,GeneratePInvoke */
87 UIElement *GetVisualParent () { return visual_parent; }
89 int GetVisualLevel () { return visual_level; }
90 void SetVisualLevel (int level) { visual_level = level; }
92 /* @GenerateCBinding,GeneratePInvoke,ManagedAccess=Internal */
93 virtual void SetSubtreeObject (DependencyObject *value);
95 /* @GenerateCBinding,GeneratePInvoke,ManagedAccess=Internal */
96 virtual DependencyObject *GetSubtreeObject () { return subtree_object; }
98 /* @GenerateCBinding,GeneratePInvoke */
99 virtual void ElementAdded (UIElement *obj);
100 /* @GenerateCBinding,GeneratePInvoke */
101 virtual void ElementRemoved (UIElement *obj);
103 virtual bool EnableAntiAlias() { return true; }
105 virtual void SetSurface (Surface *s);
107 // UpdateTotalRenderVisibility:
108 // Updates the opacity and render visibility on this item based on
109 // its parent's opacity and visibility as well as the value of its
110 // OpacityProperty and RenderVisibilityProperty.
112 void UpdateTotalRenderVisibility ();
113 void ComputeTotalRenderVisibility ();
114 bool GetActualTotalRenderVisibility ();
117 // GetRenderVisible:
118 // Returns true if the Visibility property of this item is "Visible", and false otherwise
120 bool GetRenderVisible () { return (flags & UIElement::TOTAL_RENDER_VISIBLE) != 0; }
123 // UpdateTotalHitTestVisibility:
124 // Updates the hit testability of the item based on the you know..
125 // The hit test flag.
126 void UpdateTotalHitTestVisibility ();
127 void ComputeTotalHitTestVisibility ();
128 bool GetActualTotalHitTestVisibility ();
131 // GetHitTestVisible:
132 // Returns true if the IsHitTestVisible property of this item true, and false otherwise
134 bool GetHitTestVisible () { return (flags & UIElement::TOTAL_HIT_TEST_VISIBLE) != 0; }
137 // UpdateTransform:
138 // Updates the absolute_xform for this item
140 void UpdateTransform ();
141 void ComputeLocalTransform ();
142 void ComputeTransform ();
143 virtual void TransformBounds (cairo_matrix_t *old, cairo_matrix_t *current);
146 // IsLoaded:
147 // Returns true if the element has been attached to a
148 // surface and is part of the visual hierarchy.
150 bool IsLoaded () { return (flags & UIElement::IS_LOADED) != 0; }
151 void ClearLoaded ();
154 // Render:
155 // Renders the given @item on the @surface. the area that is
156 // exposed is delimited by x, y, width, height
158 virtual void Render (cairo_t *cr, Region *region, bool path_only = false);
161 // Paint:
162 // Do an optimized render pass on the this element and it's
163 // subtree.
165 void Paint (cairo_t *cr, Region *region, cairo_matrix_t *matrix);
167 // a non virtual method for use when we want to wrap render
168 // with debugging and/or timing info
169 void DoRender (cairo_t *cr, Region *region);
170 bool UseBackToFront ();
173 // GetSizeForBrush:
174 // Gets the size of the area to be painted by a Brush (needed for image/video scaling)
175 virtual void GetSizeForBrush (cairo_t *cr, double *width, double *height);
178 // GetOriginPoint:
179 // Gets real origin, required e.g. for lineargradientbrushes on Path
180 virtual Point GetOriginPoint ()
182 return Point (0.0, 0.0);
186 // ShiftPosition:
188 // This method should actually set the x/y of the bounds
189 // rectangle to the new position. It is virtual to allow
190 // subclasses with specialized bounds (Panel, InkPresenter)
191 // the opportunity to set those bounds' positions as well.
193 virtual void ShiftPosition (Point p);
196 // UpdateBounds:
197 // Recomputes the bounds of this element, and if they're
198 // different chains up to its parent telling it to update
199 // its bounds.
201 void UpdateBounds (bool force_redraw_of_new_bounds = false);
203 // ComputeBounds:
204 // Updates the bounding box for the given item, this uses the parent
205 // chain to compute the composite affine.
207 // Output:
208 // item->bounds is updated
210 virtual void ComputeBounds ();
213 // GetBounds:
214 // returns the current bounding box for the given item in surface
215 // coordinates.
217 Rect GetBounds () { return bounds; }
220 // GetSubtreeBounds:
221 // returns the bounding box including all sub-uielements.
222 // implemented by containers in surface coordinates.
224 virtual Rect GetSubtreeBounds () { return bounds; }
227 // GetRenderBounds:
228 // returns the bounding box to be rendered, which
229 // unfortunately isn't necessarily the same as either our
230 // bounds or subtree bounds (in the case of inkpresenter)
231 virtual Rect GetRenderBounds () { return bounds; }
234 // GetCoverageBounds:
235 // returns the bounding box in global coordinates that opaquely covered by this object
237 virtual Rect GetCoverageBounds () { return Rect (); }
241 // IsLayoutContainer:
242 // returns true if the container has children that require a measure
243 // pass.
244 virtual bool IsLayoutContainer () { return GetSubtreeObject () != NULL; }
245 virtual bool IsContainer () { return IsLayoutContainer (); }
247 // HitTest
249 // Accumulate a list of all elements that will contain the
250 // point (or intersect the rectangle). The first node in the
251 // list is the most deeply nested node, the last node is the
252 // root.
253 virtual void HitTest (cairo_t *cr, Point p, List *uielement_list);
254 virtual void HitTest (cairo_t *cr, Rect r, List *uielement_list);
256 /* @GenerateCBinding,GeneratePInvoke */
257 void FindElementsInHostCoordinates_p (Point p, HitTestCollection *uielement_list);
258 /* @GenerateCBinding,GeneratePInvoke */
259 void FindElementsInHostCoordinates_r (Rect p, HitTestCollection *uielement_list);
261 virtual bool CanFindElement () { return false; }
262 virtual void FindElementsInHostCoordinates (cairo_t *cr, Point P, List *uielement_list);
263 virtual void FindElementsInHostCoordinates (cairo_t *cr, Rect r, List *uielement_list);
266 // Recomputes the bounding box, requests redraws,
267 // the parameter determines if we should also update the transformation
269 void FullInvalidate (bool render_xform);
272 // InsideObject:
273 // Returns whether the position x, y is inside the object
275 virtual bool InsideObject (cairo_t *cr, double x, double y);
278 // Checks if the point is inside the Clip region.
279 // Returns true if no Clip region is defined
280 // (which is actually an infinitely big Clip region).
282 bool InsideClip (cairo_t *cr, double x, double y);
285 // Invalidates a subrectangle of this element
287 void Invalidate (Rect r);
290 // Invalidates a subregion of this element
291 void Invalidate (Region *region);
294 // Invalidates the entire bounding rectangle of this element
296 void Invalidate ();
299 // Invalidates the paint region of the element and its subtree
301 void InvalidateSubtreePaint ();
302 void InvalidateMask ();
303 void InvalidateClip ();
304 void InvalidateVisibility ();
307 // GetTransformOrigin:
308 // Returns the transformation origin based on of the item and the
309 // xform_origin
310 virtual Point GetTransformOrigin () {
311 return Point (0, 0);
315 // EmitKeyDown:
317 bool EmitKeyDown (GdkEventKey *key);
320 // EmitKeyUp:
322 bool EmitKeyUp (GdkEventKey *key);
325 // EmitGotFocus:
326 // Invoked when the mouse focuses the given object
328 bool EmitGotFocus ();
331 // EmitLostFocus:
332 // Invoked when the given object loses mouse focus
334 bool EmitLostFocus ();
337 // EmitLostMouseCapture:
338 // Invoked when the given object loses mouse capture
340 bool EmitLostMouseCapture ();
343 // CaptureMouse:
345 // Attempts to capture the mouse. If successful, all mouse
346 // events will be transmitted directly to this element.
347 // Leave/Enter events will no longer be sent.
349 /* @GenerateCBinding,GeneratePInvoke,GenerateJSBinding */
350 bool CaptureMouse ();
351 virtual bool CanCaptureMouse () { return true; }
353 /* @GenerateCBinding,GeneratePInvoke */
354 virtual bool Focus (bool recurse = true);
357 // ReleaseMouseCapture:
359 // Attempts to release the mouse. If successful, any
360 // applicable Leave/Enter events for the current mouse
361 // position will be sent.
363 /* @GenerateCBinding,GeneratePInvoke,GenerateJSBinding */
364 void ReleaseMouseCapture ();
366 List* WalkTreeForLoaded (bool *delay);
368 void PostSubtreeLoad (List *load_list);
369 static void EmitSubtreeLoad (List *load_list);
370 static void emit_delayed_loaded (EventObject *data);
372 virtual void OnLoaded ();
374 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
375 virtual void OnSubPropertyChanged (DependencyProperty *prop, DependencyObject *obj, PropertyChangedEventArgs *subobj_args);
376 virtual void OnCollectionChanged (Collection *col, CollectionChangedEventArgs *args);
379 // CacheInvalidateHint:
380 // Give a hint to this UIElement that it should free any possible
381 // cached (mem-intensive) data it has. This ie. can happen when the
382 // element is removed from a collection, becomes invisible, etc.
384 virtual void CacheInvalidateHint ();
387 // 2.0 methods
390 // Layout foo
392 void DoMeasure ();
393 void DoArrange ();
395 /* @GenerateCBinding,GeneratePInvoke */
396 virtual void Measure (Size availableSize) = 0;
397 /* @GenerateCBinding,GeneratePInvoke */
398 virtual void Arrange (Rect finalRect) = 0;
399 /* @GenerateCBinding,GeneratePInvoke */
400 void InvalidateMeasure ();
401 /* @GenerateCBinding,GeneratePInvoke */
402 void InvalidateArrange ();
403 /* @GenerateCBinding,GeneratePInvoke,GenerateJSBinding */
404 virtual void UpdateLayout () = 0;
406 /* @GenerateCBinding,GeneratePInvoke */
407 Size GetDesiredSize () { return desired_size; }
409 /* @GenerateCBinding,GeneratePInvoke */
410 Size GetRenderSize () { return render_size; }
412 /* @GenerateCBinding,GeneratePInvoke,GenerateJSBinding=TransformToVisual,Version=2.0 */
413 GeneralTransform *GetTransformToUIElementWithError (UIElement *to_element, MoonError *error);
416 // TransformPoint:
418 // Maps the point to the coordinate space of this item
420 void TransformPoint (double *x, double *y);
422 /* @PropertyType=Geometry,GenerateAccessors */
423 const static int ClipProperty;
424 /* @PropertyType=bool,DefaultValue=true,GenerateAccessors */
425 const static int IsHitTestVisibleProperty;
426 /* @PropertyType=Brush,GenerateAccessors */
427 const static int OpacityMaskProperty;
428 /* @PropertyType=double,DefaultValue=1.0,GenerateAccessors */
429 const static int OpacityProperty;
430 /* @PropertyType=Point,DefaultValue=Point (0\,0),GenerateAccessors */
431 const static int RenderTransformOriginProperty;
432 /* @PropertyType=Transform,GenerateAccessors,GenerateManagedAccessors=false */
433 const static int RenderTransformProperty;
434 /* @PropertyType=Visibility,DefaultValue=VisibilityVisible,GenerateAccessors */
435 const static int VisibilityProperty;
436 /* @PropertyType=bool,DefaultValue=true,GenerateAccessors */
437 const static int UseLayoutRoundingProperty;
439 // in 2.0 these properties are actually in FrameworkElement
440 /* @PropertyType=MouseCursor,DefaultValue=MouseCursorDefault,ManagedDeclaringType=FrameworkElement,ManagedPropertyType=Cursor,ManagedFieldAccess=Internal,GenerateAccessors,Validator=CursorValidator */
441 const static int CursorProperty;
442 /* @PropertyType=ResourceDictionary,ManagedDeclaringType=FrameworkElement,AutoCreateValue,ManagedFieldAccess=Internal,ManagedSetterAccess=Internal,GenerateAccessors */
443 const static int ResourcesProperty;
444 /* @PropertyType=object,ManagedDeclaringType=FrameworkElement,ManagedPropertyType=object,IsCustom=true */
445 const static int TagProperty;
446 /* @PropertyType=TriggerCollection,ManagedDeclaringType=FrameworkElement,AutoCreateValue,ManagedFieldAccess=Internal,ManagedSetterAccess=Internal,GenerateAccessors */
447 const static int TriggersProperty;
450 // Property Accessors
453 void SetClip (Geometry *clip);
454 Geometry *GetClip ();
456 MouseCursor GetCursor ();
457 void SetCursor (MouseCursor value);
459 void SetIsHitTestVisible (bool visible);
460 bool GetIsHitTestVisible ();
462 void SetOpacityMask (Brush *mask);
463 Brush *GetOpacityMask ();
465 void SetOpacity (double opacity);
466 double GetOpacity ();
468 void SetRenderTransform (Transform *transform);
469 Transform *GetRenderTransform ();
471 void SetRenderTransformOrigin (Point *origin);
472 Point *GetRenderTransformOrigin ();
474 ResourceDictionary* GetResources();
475 void SetResources (ResourceDictionary *value);
477 TriggerCollection *GetTriggers ();
478 void SetTriggers (TriggerCollection* value);
480 Visibility GetVisibility ();
481 void SetVisibility (Visibility value);
483 bool GetUseLayoutRounding ();
484 void SetUseLayoutRounding (bool value);
486 // Events you can AddHandler to
487 const static int LoadedEvent;
488 const static int UnloadedEvent;
489 const static int MouseMoveEvent;
490 const static int MouseLeftButtonDownEvent;
491 const static int MouseLeftButtonUpEvent;
492 const static int KeyDownEvent;
493 const static int KeyUpEvent;
494 const static int MouseEnterEvent;
495 const static int MouseLeaveEvent;
496 const static int InvalidatedEvent;
497 const static int GotFocusEvent;
498 const static int LostFocusEvent;
499 const static int LostMouseCaptureEvent;
501 const static int MouseLeftButtonMultiClickEvent;
502 const static int MouseRightButtonDownEvent;
503 const static int MouseRightButtonUpEvent;
504 const static int MouseWheelEvent;
506 // Helper method which checks recursively checks this element and its visual
507 // parents to see if any are loaded.
508 static bool IsSubtreeLoaded (UIElement *element);
510 protected:
511 virtual ~UIElement ();
512 Rect IntersectBoundsWithClipPath (Rect bounds, bool transform);
513 void RenderClipPath (cairo_t *cr, bool path_only = false);
515 void SetDesiredSize (Size s) { desired_size = s; }
516 void SetRenderSize (Size s) { render_size = s; }
518 // The computed bounding box
519 Rect bounds;
520 Rect extents;
522 int flags;
524 // Absolute affine transform, precomputed with all of its data
525 cairo_matrix_t absolute_xform;
526 cairo_matrix_t layout_xform;
528 void FrontToBack (Region *surface_region, List *render_list);
529 virtual void PreRender (cairo_t *cr, Region *region, bool front_to_back);
530 virtual void PostRender (cairo_t *cr, Region *region, bool front_to_back);
532 static void CallPreRender (cairo_t *cr, UIElement *element, Region *region, bool front_to_back);
533 static void CallPostRender (cairo_t *cr, UIElement *element, Region *region, bool front_to_back);
535 private:
536 int visual_level;
537 UIElement *visual_parent;
538 DependencyObject *subtree_object;
539 double total_opacity;
540 Brush *opacityMask;
541 Size desired_size;
542 Size render_size;
544 // The local render transform including tranform origin
545 cairo_matrix_t local_xform;
548 #endif /* __MOON_UIELEMENT_H__ */