Context.h

00001 /*
00002  * This source file is part of libRocket, the HTML/CSS Interface Middleware
00003  *
00004  * For the latest information, see http://www.librocket.com
00005  *
00006  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  *
00026  */
00027 
00028 #ifndef ROCKETCORECONTEXT_H
00029 #define ROCKETCORECONTEXT_H
00030 
00031 #include <Rocket/Core/Header.h>
00032 #include <Rocket/Core/Types.h>
00033 #include <Rocket/Core/ReferenceCountable.h>
00034 #include <Rocket/Core/ElementReference.h>
00035 #include <Rocket/Core/Input.h>
00036 #include <Rocket/Core/String.h>
00037 #include <Rocket/Core/ScriptInterface.h>
00038 
00039 namespace Rocket {
00040 namespace Core {
00041 
00042 class Stream;
00043 class Dictionary;
00044 
00045 }
00046 }
00047 
00048 namespace Rocket {
00049 namespace Core {
00050 
00051 class ContextInstancer;
00052 class ElementDocument;
00053 class EventListener;
00054 class RenderInterface;
00055 
00062 class ROCKETCORE_API Context : public ScriptInterface
00063 {
00064 public:
00068         Context(const String& name);
00070         virtual ~Context();
00071 
00074         const String& GetName() const;
00075 
00078         void SetDimensions(const Vector2i& dimensions);
00081         const Vector2i& GetDimensions() const;
00082 
00084         bool Update();
00086         bool Render();
00087 
00091         ElementDocument* CreateDocument(const String& tag = "body");
00095         ElementDocument* LoadDocument(const String& document_path);
00099         ElementDocument* LoadDocument(Stream* document_stream);
00103         ElementDocument* LoadDocumentFromMemory(const String& string);
00106         void UnloadDocument(ElementDocument* document);
00108         void UnloadAllDocuments();
00109 
00113         void AddMouseCursor(ElementDocument* cursor_document);
00117         ElementDocument* LoadMouseCursor(const String& cursor_document_path);
00120         void UnloadMouseCursor(const String& cursor_name);
00122         void UnloadAllMouseCursors();
00126         bool SetMouseCursor(const String& cursor_name);
00129         void ShowMouseCursor(bool show);
00130 
00134         ElementDocument* GetDocument(const String& id);
00138         ElementDocument* GetDocument(int index);
00141         int GetNumDocuments() const;
00142 
00145         Element* GetHoverElement();
00146 
00149         Element* GetFocusElement();
00150 
00153         Element* GetRootElement();
00154 
00157         void PullDocumentToFront(ElementDocument* document);
00160         void PushDocumentToBack(ElementDocument* document);
00161 
00166         void AddEventListener(const String& event, EventListener* listener, bool in_capture_phase = false);
00171         void RemoveEventListener(const String& event, EventListener* listener, bool in_capture_phase = false);
00172 
00177         bool ProcessKeyDown(Input::KeyIdentifier key_identifier, int key_modifier_state);
00182         bool ProcessKeyUp(Input::KeyIdentifier key_identifier, int key_modifier_state);
00183 
00187         bool ProcessTextInput(word character);
00191         bool ProcessTextInput(const String& string);
00192 
00197         void ProcessMouseMove(int x, int y, int key_modifier_state);
00201         void ProcessMouseButtonDown(int button_index, int key_modifier_state);
00205         void ProcessMouseButtonUp(int button_index, int key_modifier_state);
00210         bool ProcessMouseWheel(int wheel_delta, int key_modifier_state);
00211 
00214         RenderInterface* GetRenderInterface() const;
00215 
00218         void SetInstancer(ContextInstancer* instancer);
00219 
00220 protected:
00221         virtual void OnReferenceDeactivate();
00222 
00223 private:
00224         String name;
00225         Vector2i dimensions;
00226 
00227         ContextInstancer* instancer;
00228 
00229         typedef std::set< ElementReference > ElementSet;
00230         typedef std::vector< ElementReference > ElementList;
00231         // Set of elements that are currently in hover state.
00232         ElementSet hover_chain;
00233         // List of elements that are currently in active state.
00234         ElementList active_chain;
00235         // History of windows that have had focus
00236         ElementList document_focus_history;
00237 
00238         // Documents that have been unloaded from the context but not yet released.
00239         ElementList unloaded_documents;
00240 
00241         // Root of the element tree.
00242         Element* root;
00243         // The element that current has input focus.
00244         ElementReference focus;
00245         // The top-most element being hovered over.
00246         ElementReference hover;
00247         // The element that was being hovered over when the primary mouse button was pressed most recently.
00248         ElementReference active;
00249 
00250         // The element that was clicked on last.
00251         Element* last_click_element;
00252         // The time the last click occured.
00253         float last_click_time;
00254 
00255         typedef std::map< String, ElementDocument* > CursorMap;
00256         CursorMap cursors;
00257         ElementReference default_cursor;
00258         ElementReference active_cursor;
00259         bool show_cursor;
00260 
00261         ElementDocument* cursor_proxy;
00262 
00263         // The element that is currently being dragged (or about to be dragged).
00264         ElementReference drag;
00265         // True if a drag has begun (ie, the ondragstart event has been fired for the drag element), false otherwise.
00266         bool drag_started;
00267         // True if the current drag is a verbose drag (ie, sends ondragover, ondragout, ondragdrop, etc, events).
00268         bool drag_verbose;
00269         // Used when dragging a cloned object.
00270         Element* drag_clone;
00271 
00272         // The element currently being dragged over; this is equivalent to hover, but only set while an element is being
00273         // dragged, and excludes the dragged element.
00274         ElementReference drag_hover;
00275         // Set of elements that are currently being dragged over; this differs from the hover state as the dragged element
00276         // itself can't be part of it.
00277         ElementSet drag_hover_chain;
00278 
00279         // Input state; stored from the most recent input events we receive from the application.
00280         Vector2i mouse_position;
00281 
00282         // The render interface this context renders through.
00283         RenderInterface* render_interface;
00284 
00285         // Internal callback for when an element is removed from the hierarchy.
00286         void OnElementRemove(Element* element);
00287         // Internal callback for when a new element gains focus.
00288         bool OnFocusChange(Element* element);
00289 
00290         // Generates an event for faking clicks on an element.
00291         void GenerateClickEvent(Element* element);
00292 
00293         // Updates the current hover elements, sending required events.
00294         void UpdateHoverChain(const Dictionary& parameters, const Dictionary& drag_parameters, const Vector2i& old_mouse_position);
00295         // Returns the youngest descendent of the given element which is under the given point in screen coordinates.
00296         // @param[in] point The point to test.
00297         // @param[in] ignore_element If set, this element and its descendents will be ignored.
00298         // @param[in] element Used internally.
00299         // @return The element under the point, or NULL if nothing is.
00300         Element* GetElementAtPoint(const Vector2f& point, const Element* ignore_element = NULL, Element* element = NULL);
00301 
00302         // Creates the drag clone from the given element. The old drag clone will be released if
00303         // necessary.
00304         // @param[in] element The element to clone.
00305         void CreateDragClone(Element* element);
00306         // Releases the drag clone, if one exists.
00307         void ReleaseDragClone();
00308 
00309         // Builds the parameters for a generic key event.
00310         void GenerateKeyEventParameters(Dictionary& parameters, Input::KeyIdentifier key_identifier);
00311         // Builds the parameters for a generic mouse event.
00312         void GenerateMouseEventParameters(Dictionary& parameters, int button_index = -1);
00313         // Builds the parameters for the key modifier state.
00314         void GenerateKeyModifierEventParameters(Dictionary& parameters, int key_modifier_state);
00315         // Builds the parameters for a drag event.
00316         void GenerateDragEventParameters(Dictionary& parameters);
00317 
00318         // Releases all unloaded documents pending destruction.
00319         void ReleaseUnloadedDocuments();
00320 
00321         // Sends the specified event to all elements in new_items that don't appear in old_items.
00322         static void SendEvents(const ElementSet& old_items, const ElementSet& new_items, const String& event, const Dictionary& parameters, bool interruptible);
00323 
00324         friend class Element;
00325         friend ROCKETCORE_API Context* CreateContext(const String&, const Vector2i&, RenderInterface*);
00326 };
00327 
00328 }
00329 }
00330 
00331 #endif