libRocket
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Element.h
Go to the documentation of this file.
1 /*
2  * This source file is part of libRocket, the HTML/CSS Interface Middleware
3  *
4  * For the latest information, see http://www.librocket.com
5  *
6  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  *
26  */
27 
28 #ifndef ROCKETCOREELEMENT_H
29 #define ROCKETCOREELEMENT_H
30 
31 #include "ReferenceCountable.h"
33 #include "Header.h"
34 #include "Box.h"
35 #include "Event.h"
36 #include "Property.h"
37 #include "Types.h"
38 
39 namespace Rocket {
40 namespace Core {
41  class Dictionary;
42 }
43 }
44 
45 namespace Rocket {
46 namespace Core {
47 
48 class Context;
49 class Decorator;
50 class ElementInstancer;
51 class EventDispatcher;
53 class ElementBackground;
54 class ElementBorder;
55 class ElementDecoration;
56 class ElementDefinition;
57 class ElementDocument;
58 class ElementScroll;
59 class ElementStyle;
60 class FontFaceHandle;
61 class PropertyDictionary;
62 class RenderInterface;
63 class StyleSheet;
64 
72 {
73 public:
77  Element(const String& tag);
78  virtual ~Element();
79 
80  void Update();
81  void Render();
82 
84  Element* Clone() const;
85 
89  void SetClass(const String& class_name, bool activate);
96  bool IsClassSet(const String& class_name) const;
99  void SetClassNames(const String& class_names);
102  String GetClassNames() const;
104 
107  virtual StyleSheet* GetStyleSheet() const;
108 
111  const ElementDefinition* GetDefinition();
112 
116  String GetAddress(bool include_pseudo_classes = false) const;
117 
122  void SetOffset(const Vector2f& offset, Element* offset_parent, bool offset_fixed = false);
127  Vector2f GetRelativeOffset(Box::Area area = Box::CONTENT);
132  Vector2f GetAbsoluteOffset(Box::Area area = Box::CONTENT);
133 
136  void SetClientArea(Box::Area client_area);
139  Box::Area GetClientArea() const;
140 
145  void SetContentBox(const Vector2f& content_offset, const Vector2f& content_box);
148  void SetBox(const Box& box);
151  void AddBox(const Box& box);
155  const Box& GetBox(int index = 0);
158  int GetNumBoxes();
159 
162  virtual float GetBaseline() const;
167  virtual bool GetIntrinsicDimensions(Vector2f& dimensions);
168 
172  virtual bool IsPointWithinElement(const Vector2f& point);
173 
176  bool IsVisible() const;
179  float GetZIndex() const;
180 
183  FontFaceHandle* GetFontFaceHandle() const;
184 
188  bool SetProperty(const String& name, const String& value);
197  bool SetProperty(const String& name, const Property& property);
201  void RemoveProperty(const String& name);
206  const Property* GetProperty(const String& name);
210  template < typename T >
211  T GetProperty(const String& name);
216  const Property* GetLocalProperty(const String& name);
222  float ResolveProperty(const String& name, float base_value);
228  float ResolveProperty(const Property *property, float base_value);
229 
231  void GetBorderWidthProperties(const Property **border_top_width, const Property **border_bottom_width, const Property **border_left_width, const Property **border_right_width);
233  void GetMarginProperties(const Property **margin_top, const Property **margin_bottom, const Property **margin_left, const Property **margin_right);
235  void GetPaddingProperties(const Property **padding_top, const Property **padding_bottom, const Property **padding_left, const Property **padding_right);
237  void GetDimensionProperties(const Property **width, const Property **height);
240  void GetLocalDimensionProperties(const Property **width, const Property **height);
242  void GetOverflow(int *overflow_x, int *overflow_y);
244  int GetPosition();
246  int GetFloat();
248  int GetDisplay();
250  int GetWhitespace();
251 
253  const Property *GetLineHeightProperty();
255  int GetTextAlign();
257  int GetTextTransform();
259  const Property *GetVerticalAlignProperty();
260 
267  bool IterateProperties(int& index, PseudoClassList& pseudo_classes, String& name, const Property*& property) const;
269 
273  void SetPseudoClass(const String& pseudo_class, bool activate);
280  bool IsPseudoClassSet(const String& pseudo_class) const;
284  bool ArePseudoClassesSet(const PseudoClassList& pseudo_classes) const;
287  const PseudoClassList& GetActivePseudoClasses() const;
289 
293  template< typename T >
297  void SetAttribute(const String& name, const T& value);
301  Variant* GetAttribute(const String& name) const;
305  template< typename T >
306  T GetAttribute(const String& name, const T& default_value) const;
310  bool HasAttribute(const String& name);
313  void RemoveAttribute(const String& name);
316  void SetAttributes(const ElementAttributes* attributes);
322  template< typename T >
323  bool IterateAttributes(int& index, String& name, T& value) const;
326  int GetNumAttributes() const;
328 
332  bool IterateDecorators(int& index, PseudoClassList& pseudo_classes, String& name, Decorator*& decorator, DecoratorDataHandle& decorator_data);
342 
345  Element* GetFocusLeafNode();
346 
349  Context* GetContext();
350 
354 
357  const String& GetTagName() const;
358 
361  const String& GetId() const;
364  void SetId(const String& id);
365 
368  float GetAbsoluteLeft();
371  float GetAbsoluteTop();
372 
376  float GetClientLeft();
380  float GetClientTop();
384  float GetClientWidth();
388  float GetClientHeight();
389 
392  Element* GetOffsetParent();
395  float GetOffsetLeft();
398  float GetOffsetTop();
401  float GetOffsetWidth();
404  float GetOffsetHeight();
405 
408  float GetScrollLeft();
411  void SetScrollLeft(float scroll_left);
414  float GetScrollTop();
417  void SetScrollTop(float scroll_top);
420  float GetScrollWidth();
423  float GetScrollHeight();
424 
427  ElementStyle* GetStyle();
428 
431  virtual ElementDocument* GetOwnerDocument();
432 
435  Element* GetParentNode() const;
436 
439  Element* GetNextSibling() const;
442  Element* GetPreviousSibling() const;
443 
446  Element* GetFirstChild() const;
449  Element* GetLastChild() const;
453  Element* GetChild(int index) const;
457  int GetNumChildren(bool include_non_dom_elements = false) const;
458 
461  virtual void GetInnerRML(String& content) const;
464  String GetInnerRML() const;
467  void SetInnerRML(const String& rml);
468 
470 
474 
477  bool Focus();
479  void Blur();
481  void Click();
482 
487  void AddEventListener(const String& event, EventListener* listener, bool in_capture_phase = false);
492  void RemoveEventListener(const String& event, EventListener* listener, bool in_capture_phase = false);
498  bool DispatchEvent(const String& event, const Dictionary& parameters, bool interruptible = false);
499 
502  void ScrollIntoView(bool align_with_top = true);
503 
507  void AppendChild(Element* element, bool dom_element = true);
512  void InsertBefore(Element* element, Element* adjacent_element);
517  bool ReplaceChild(Element* inserted_element, Element* replaced_element);
521  bool RemoveChild(Element* element);
524  bool HasChildNodes() const;
525 
529  Element* GetElementById(const String& id);
533  void GetElementsByTagName(ElementList& elements, const String& tag);
537  void GetElementsByClassName(ElementList& elements, const String& class_name);
539 
544  EventDispatcher* GetEventDispatcher() const;
549  ElementBackground* GetElementBackground() const;
552  ElementBorder* GetElementBorder() const;
555  ElementDecoration* GetElementDecoration() const;
558  ElementScroll* GetElementScroll() const;
560 
562  int GetClippingIgnoreDepth();
564  bool IsClippingEnabled();
565 
569 
572  void SetInstancer(ElementInstancer* instancer);
573 
576  virtual void ProcessEvent(Event& event);
577 
579  void UpdateLayout();
580 
581 protected:
584  void ForceLocalStackingContext();
585 
587  virtual void OnUpdate();
589  virtual void OnRender();
590 
592  virtual void OnLayout();
593 
596  virtual void OnAttributeChange(const AttributeNameList& changed_attributes);
599  virtual void OnPropertyChange(const PropertyNameList& changed_properties);
600 
602  // @param[in] child The element that has been added. This may be this element.
603  virtual void OnChildAdd(Element* child);
605  // @param[in] child The element that has been removed. This may be this element.
606  virtual void OnChildRemove(Element* child);
607 
609  virtual void DirtyLayout();
610 
612  virtual bool IsLayoutDirty();
613 
615  virtual void LockLayout(bool lock);
616 
618  virtual void DirtyFont();
619 
622  virtual void GetRML(String& content);
623 
624  virtual void OnReferenceDeactivate();
625 
626 private:
627  void SetParent(Element* parent);
628 
629  void ReleaseDeletedElements();
630  void ReleaseElements(ElementList& elements);
631 
632  void DirtyOffset();
633  void UpdateOffset();
634 
635  void BuildLocalStackingContext();
636  void BuildStackingContext(ElementList* stacking_context);
637  void DirtyStackingContext();
638 
639  void DirtyStructure();
640 
641  // Original tag this element came from.
642  String tag;
643 
644  // The optional, unique ID of this object.
645  String id;
646 
647  // Instancer that created us, used for destruction.
648  ElementInstancer* instancer;
649 
650  // Parent element.
651  Element* parent;
652  // Currently focused child object
653  Element* focus;
654  // The owning document
655  ElementDocument* owner_document;
656 
657  // The event dispatcher for this element.
658  EventDispatcher* event_dispatcher;
659  // Style information for this element.
660  ElementStyle* style;
661  // Background functionality for this element.
662  ElementBackground* background;
663  // Border functionality for this element.
664  ElementBorder* border;
665  // Decorator information for this element.
666  ElementDecoration* decoration;
667  // Scrollbar information for this element.
668  ElementScroll* scroll;
669  // Attributes on this element.
670  ElementAttributes attributes;
671 
672  // The offset of the element, and the element it is offset from.
673  Element* offset_parent;
674  Vector2f relative_offset_base; // the base offset from the parent
675  Vector2f relative_offset_position; // the offset of a relatively positioned element
676  bool offset_fixed;
677 
678  mutable Vector2f absolute_offset;
679  mutable bool offset_dirty;
680 
681  // The offset this element adds to its logical children due to scrolling content.
682  Vector2f scroll_offset;
683 
684  // The size of the element.
685  typedef std::vector< Box > BoxList;
686  BoxList boxes;
687  // And of the element's internal content.
688  Vector2f content_offset;
689  Vector2f content_box;
690 
691  // Defines what box area represents the element's client area; this is usually padding, but may be content.
692  Box::Area client_area;
693 
694  // True if the element is visible and active.
695  bool visible;
696 
697  ElementList children;
698  int num_non_dom_children;
699 
700  ElementList active_children;
701  ElementList deleted_children;
702 
703  float z_index;
704  bool local_stacking_context;
705  bool local_stacking_context_forced;
706 
707  ElementList stacking_context;
708  bool stacking_context_dirty;
709 
710  // The element's font face; used to render text and resolve em / ex properties.
711  FontFaceHandle* font_face_handle;
712 
713  // Cached rendering information
714  int clipping_ignore_depth;
715  bool clipping_enabled;
716  bool clipping_state_dirty;
717 
718  friend class Context;
719  friend class ElementStyle;
720  friend class LayoutEngine;
721  friend class LayoutInlineBox;
722 };
723 
724 #include "Element.inl"
725 
726 }
727 }
728 
729 #endif
#define ROCKETCORE_API
Definition: Header.h:73
Definition: ElementDocument.h:55
ROCKETCORE_API RenderInterface * GetRenderInterface()
Definition: Event.h:48
Definition: ScriptInterface.h:43
std::set< String > PseudoClassList
Definition: Types.h:91
Definition: Box.h:43
uintptr_t DecoratorDataHandle
Definition: Types.h:87
Definition: Clipboard.h:33
Area
Definition: Box.h:46
Definition: Variant.h:48
Definition: ElementScroll.h:46
Definition: StyleSheet.h:50
Definition: Context.h:62
Definition: EventListener.h:46
Definition: PropertyDictionary.h:45
std::set< String > PropertyNameList
Definition: Types.h:92
ROCKETCORE_API Context * GetContext(const String &name)
ROCKETDEBUGGER_API bool IsVisible()
Definition: Dictionary.h:44
Definition: Box.h:51
Definition: Property.h:43
Definition: ElementInstancer.h:54
Definition: Decorator.h:53
Definition: RenderInterface.h:48
Definition: Element.h:71
std::vector< Element * > ElementList
Definition: Types.h:90
std::set< String > AttributeNameList
Definition: Types.h:93