libRocket
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Context.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 ROCKETCORECONTEXT_H
29 #define ROCKETCORECONTEXT_H
30 
31 #include "Header.h"
32 #include "Types.h"
33 #include "ReferenceCountable.h"
34 #include "ElementReference.h"
35 #include "Input.h"
36 #include "String.h"
37 #include "ScriptInterface.h"
38 
39 namespace Rocket {
40 namespace Core {
41 
42 class Stream;
43 class Dictionary;
44 
45 }
46 }
47 
48 namespace Rocket {
49 namespace Core {
50 
51 class ContextInstancer;
52 class ElementDocument;
53 class EventListener;
54 class RenderInterface;
55 
63 {
64 public:
68  Context(const String& name);
70  virtual ~Context();
71 
74  const String& GetName() const;
75 
78  void SetDimensions(const Vector2i& dimensions);
81  const Vector2i& GetDimensions() const;
82 
84  bool Update();
86  bool Render();
87 
91  ElementDocument* CreateDocument(const String& tag = "body");
95  ElementDocument* LoadDocument(const String& document_path);
99  ElementDocument* LoadDocument(Stream* document_stream);
103  ElementDocument* LoadDocumentFromMemory(const String& string);
106  void UnloadDocument(ElementDocument* document);
108  void UnloadAllDocuments();
109 
113  void AddMouseCursor(ElementDocument* cursor_document);
117  ElementDocument* LoadMouseCursor(const String& cursor_document_path);
120  void UnloadMouseCursor(const String& cursor_name);
122  void UnloadAllMouseCursors();
126  bool SetMouseCursor(const String& cursor_name);
129  void ShowMouseCursor(bool show);
130 
134  ElementDocument* GetDocument(const String& id);
138  ElementDocument* GetDocument(int index);
141  int GetNumDocuments() const;
142 
145  Element* GetHoverElement();
146 
149  Element* GetFocusElement();
150 
153  Element* GetRootElement();
154 
157  void PullDocumentToFront(ElementDocument* document);
160  void PushDocumentToBack(ElementDocument* document);
161 
166  void AddEventListener(const String& event, EventListener* listener, bool in_capture_phase = false);
171  void RemoveEventListener(const String& event, EventListener* listener, bool in_capture_phase = false);
172 
177  bool ProcessKeyDown(Input::KeyIdentifier key_identifier, int key_modifier_state);
182  bool ProcessKeyUp(Input::KeyIdentifier key_identifier, int key_modifier_state);
183 
187  bool ProcessTextInput(word character);
191  bool ProcessTextInput(const String& string);
192 
197  void ProcessMouseMove(int x, int y, int key_modifier_state);
201  void ProcessMouseButtonDown(int button_index, int key_modifier_state);
205  void ProcessMouseButtonUp(int button_index, int key_modifier_state);
210  bool ProcessMouseWheel(int wheel_delta, int key_modifier_state);
211 
218  bool GetActiveClipRegion(Vector2i& origin, Vector2i& dimensions) const;
222  void SetActiveClipRegion(const Vector2i& origin, const Vector2i& dimensions);
223 
226  void SetInstancer(ContextInstancer* instancer);
227 
228 protected:
229  virtual void OnReferenceDeactivate();
230 
231 private:
232  String name;
233  Vector2i dimensions;
234 
235  ContextInstancer* instancer;
236 
237  typedef std::set< ElementReference > ElementSet;
238  typedef std::vector< ElementReference > ElementList;
239  // Set of elements that are currently in hover state.
240  ElementSet hover_chain;
241  // List of elements that are currently in active state.
242  ElementList active_chain;
243  // History of windows that have had focus
244  ElementList document_focus_history;
245 
246  // Documents that have been unloaded from the context but not yet released.
247  ElementList unloaded_documents;
248 
249  // Root of the element tree.
250  Element* root;
251  // The element that current has input focus.
252  ElementReference focus;
253  // The top-most element being hovered over.
254  ElementReference hover;
255  // The element that was being hovered over when the primary mouse button was pressed most recently.
256  ElementReference active;
257 
258  // The element that was clicked on last.
259  Element* last_click_element;
260  // The time the last click occured.
261  float last_click_time;
262 
263  typedef std::map< String, ElementDocument* > CursorMap;
264  CursorMap cursors;
265  ElementReference default_cursor;
266  ElementReference active_cursor;
267  bool show_cursor;
268 
269  ElementDocument* cursor_proxy;
270 
271  // The element that is currently being dragged (or about to be dragged).
272  ElementReference drag;
273  // True if a drag has begun (ie, the ondragstart event has been fired for the drag element), false otherwise.
274  bool drag_started;
275  // True if the current drag is a verbose drag (ie, sends ondragover, ondragout, ondragdrop, etc, events).
276  bool drag_verbose;
277  // Used when dragging a cloned object.
278  Element* drag_clone;
279 
280  // The element currently being dragged over; this is equivalent to hover, but only set while an element is being
281  // dragged, and excludes the dragged element.
282  ElementReference drag_hover;
283  // Set of elements that are currently being dragged over; this differs from the hover state as the dragged element
284  // itself can't be part of it.
285  ElementSet drag_hover_chain;
286 
287  // Input state; stored from the most recent input events we receive from the application.
288  Vector2i mouse_position;
289 
290  // The render interface this context renders through.
291  RenderInterface* render_interface;
292  Vector2i clip_origin;
293  Vector2i clip_dimensions;
294 
295  // Internal callback for when an element is removed from the hierarchy.
296  void OnElementRemove(Element* element);
297  // Internal callback for when a new element gains focus.
298  bool OnFocusChange(Element* element);
299 
300  // Generates an event for faking clicks on an element.
301  void GenerateClickEvent(Element* element);
302 
303  // Updates the current hover elements, sending required events.
304  void UpdateHoverChain(const Dictionary& parameters, const Dictionary& drag_parameters, const Vector2i& old_mouse_position);
305  // Returns the youngest descendent of the given element which is under the given point in screen coordinates.
306  // @param[in] point The point to test.
307  // @param[in] ignore_element If set, this element and its descendents will be ignored.
308  // @param[in] element Used internally.
309  // @return The element under the point, or NULL if nothing is.
310  Element* GetElementAtPoint(const Vector2f& point, const Element* ignore_element = NULL, Element* element = NULL);
311 
312  // Creates the drag clone from the given element. The old drag clone will be released if
313  // necessary.
314  // @param[in] element The element to clone.
315  void CreateDragClone(Element* element);
316  // Releases the drag clone, if one exists.
317  void ReleaseDragClone();
318 
319  // Builds the parameters for a generic key event.
320  void GenerateKeyEventParameters(Dictionary& parameters, Input::KeyIdentifier key_identifier);
321  // Builds the parameters for a generic mouse event.
322  void GenerateMouseEventParameters(Dictionary& parameters, int button_index = -1);
323  // Builds the parameters for the key modifier state.
324  void GenerateKeyModifierEventParameters(Dictionary& parameters, int key_modifier_state);
325  // Builds the parameters for a drag event.
326  void GenerateDragEventParameters(Dictionary& parameters);
327 
328  // Releases all unloaded documents pending destruction.
329  void ReleaseUnloadedDocuments();
330 
331  // Sends the specified event to all elements in new_items that don't appear in old_items.
332  static void SendEvents(const ElementSet& old_items, const ElementSet& new_items, const String& event, const Dictionary& parameters, bool interruptible);
333 
334  friend class Element;
336 };
337 
338 }
339 }
340 
341 #endif
#define NULL
Definition: Types.h:33
KeyIdentifier
Definition: Input.h:42
#define ROCKETCORE_API
Definition: Header.h:73
Definition: ElementDocument.h:55
ROCKETCORE_API RenderInterface * GetRenderInterface()
Definition: ScriptInterface.h:43
Definition: Clipboard.h:33
Definition: Context.h:62
Definition: EventListener.h:46
Definition: Dictionary.h:44
Definition: Stream.h:47
unsigned short word
Definition: Types.h:51
ROCKETCORE_API Context * CreateContext(const String &name, const Vector2i &dimensions, RenderInterface *render_interface=NULL)
Definition: ElementReference.h:44
Definition: RenderInterface.h:48
Definition: Element.h:71
std::vector< Element * > ElementList
Definition: Types.h:90
Definition: ContextInstancer.h:46