| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645 |
- /*
- * This source file is part of RmlUi, the HTML/CSS Interface Middleware
- *
- * For the latest information, see http://github.com/mikke89/RmlUi
- *
- * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
- * Copyright (c) 2019-2023 The RmlUi Team, and contributors
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
- #include "../../Include/RmlUi/Core/Context.h"
- #include "../../Include/RmlUi/Core/ComputedValues.h"
- #include "../../Include/RmlUi/Core/ContextInstancer.h"
- #include "../../Include/RmlUi/Core/Core.h"
- #include "../../Include/RmlUi/Core/DataModelHandle.h"
- #include "../../Include/RmlUi/Core/Debug.h"
- #include "../../Include/RmlUi/Core/ElementDocument.h"
- #include "../../Include/RmlUi/Core/ElementUtilities.h"
- #include "../../Include/RmlUi/Core/Factory.h"
- #include "../../Include/RmlUi/Core/Profiling.h"
- #include "../../Include/RmlUi/Core/RenderManager.h"
- #include "../../Include/RmlUi/Core/StreamMemory.h"
- #include "../../Include/RmlUi/Core/SystemInterface.h"
- #include "DataModel.h"
- #include "EventDispatcher.h"
- #include "PluginRegistry.h"
- #include "ScrollController.h"
- #include "StreamFile.h"
- #include <algorithm>
- #include <clocale>
- #include <iterator>
- #include <limits>
- namespace Rml {
- static constexpr float DOUBLE_CLICK_TIME = 0.5f; // [s]
- static constexpr float DOUBLE_CLICK_MAX_DIST = 3.f; // [dp]
- static constexpr float UNIT_SCROLL_LENGTH = 80.f; // [dp]
- // If the user stops scrolling for this amount of time in seconds before touch/click release, don't apply inertia.
- static constexpr float SCROLL_INERTIA_DELAY = 0.1f;
- static constexpr float TOUCH_MOVEMENT_DECAY_RATE = 5.0f;
- static void DebugVerifyLocaleSetting()
- {
- #ifdef RMLUI_DEBUG
- constexpr float expected_value = 1000.5f;
- const Rml::String expected_string = "1000.5";
- const Rml::String formatted_string = Rml::ToString(expected_value);
- const float parsed_value = Rml::FromString<float>(expected_string);
- const char* description = "RmlUi expects the global locale to be set to the default minimal \"C\" locale, please see `std::setlocale`.";
- if (formatted_string != expected_string)
- {
- Rml::Log::Message(Rml::Log::LT_ERROR,
- "Incompatible locale setting detected while formatting %f. Formatted: \"%s\". Expected: \"%s\". Current locale: %s. %s", expected_value,
- formatted_string.c_str(), expected_string.c_str(), std::setlocale(LC_ALL, nullptr), description);
- }
- if (parsed_value != expected_value)
- {
- Rml::Log::Message(Rml::Log::LT_ERROR,
- "Incompatible locale setting detected while parsing \"%s\". Parsed: %f. Expected: %f. Current locale: %s. %s", expected_string.c_str(),
- parsed_value, expected_value, std::setlocale(LC_ALL, nullptr), description);
- }
- #endif
- }
- Context::Context(const String& name, RenderManager* render_manager, TextInputHandler* text_input_handler) :
- name(name), render_manager(render_manager), text_input_handler(text_input_handler)
- {
- instancer = nullptr;
- root = As<ElementPtr>(Factory::InstanceNode("*", "#root"));
- root->SetId(name);
- root->SetOffset(Vector2f(0, 0), nullptr);
- root->SetProperty(PropertyId::ZIndex, Property(0, Unit::NUMBER));
- cursor_proxy = As<ElementPtr>(Factory::InstanceNode(documents_base_tag, documents_base_tag));
- ElementDocument* cursor_proxy_document = rmlui_dynamic_cast<ElementDocument*>(cursor_proxy.get());
- RMLUI_ASSERT(cursor_proxy_document);
- cursor_proxy_document->context = this;
- // The cursor proxy takes the style from its cloned element's document. The latter may define style rules for `<body>` which we don't want on the
- // proxy. Thus, we override some properties here that we in particular don't want to inherit from the client document, especially those that
- // result in decoration of the body element.
- cursor_proxy_document->SetProperty(PropertyId::BackgroundColor, Property(Colourb(255, 255, 255, 0), Unit::COLOUR));
- cursor_proxy_document->SetProperty(PropertyId::BorderTopWidth, Property(0, Unit::PX));
- cursor_proxy_document->SetProperty(PropertyId::BorderRightWidth, Property(0, Unit::PX));
- cursor_proxy_document->SetProperty(PropertyId::BorderBottomWidth, Property(0, Unit::PX));
- cursor_proxy_document->SetProperty(PropertyId::BorderLeftWidth, Property(0, Unit::PX));
- cursor_proxy_document->SetProperty(PropertyId::Decorator, Property());
- cursor_proxy_document->SetProperty(PropertyId::OverflowX, Property(Style::Overflow::Visible));
- cursor_proxy_document->SetProperty(PropertyId::OverflowY, Property(Style::Overflow::Visible));
- document_focus_history.push_back(root.get());
- focus = root.get();
- hover = nullptr;
- active = nullptr;
- drag = nullptr;
- drag_started = false;
- drag_verbose = false;
- drag_clone = nullptr;
- drag_hover = nullptr;
- last_click_element = nullptr;
- last_click_time = 0;
- mouse_active = false;
- enable_cursor = true;
- scroll_controller = MakeUnique<ScrollController>();
- }
- Context::~Context()
- {
- PluginRegistry::NotifyContextDestroy(this);
- UnloadAllDocuments();
- ReleaseUnloadedDocuments();
- root.reset();
- cursor_proxy.reset();
- instancer = nullptr;
- }
- const String& Context::GetName() const
- {
- return name;
- }
- void Context::SetDimensions(const Vector2i _dimensions)
- {
- if (dimensions != _dimensions)
- {
- dimensions = _dimensions;
- render_manager->SetViewport(dimensions);
- root->SetBox(Box(Vector2f(dimensions)));
- root->DirtyLayout();
- for (int i = 0; i < root->GetNumChildren(); ++i)
- {
- ElementDocument* document = root->GetChild(i)->GetOwnerDocument();
- if (document != nullptr)
- {
- document->DirtyMediaQueries();
- document->DirtyVwAndVhProperties();
- document->DirtyLayout();
- document->DirtyPosition();
- document->DispatchEvent(EventId::Resize, Dictionary());
- }
- }
- }
- }
- Vector2i Context::GetDimensions() const
- {
- return dimensions;
- }
- void Context::SetDensityIndependentPixelRatio(float dp_ratio)
- {
- if (density_independent_pixel_ratio != dp_ratio)
- {
- density_independent_pixel_ratio = dp_ratio;
- for (int i = 0; i < root->GetNumChildren(true); ++i)
- {
- ElementDocument* document = root->GetChild(i)->GetOwnerDocument();
- if (document)
- {
- document->DirtyMediaQueries();
- document->OnDpRatioChangeRecursive();
- }
- }
- }
- }
- float Context::GetDensityIndependentPixelRatio() const
- {
- return density_independent_pixel_ratio;
- }
- bool Context::Update()
- {
- RMLUI_ZoneScoped;
- DebugVerifyLocaleSetting();
- next_update_timeout = std::numeric_limits<double>::infinity();
- if (scroll_controller->Update(mouse_position, density_independent_pixel_ratio))
- RequestNextUpdate(0);
- // Update the hover chain to detect any new or moved elements under the mouse.
- if (mouse_active)
- UpdateHoverChain(mouse_position);
- // Update all the data models before updating properties and layout.
- for (auto& data_model : data_models)
- data_model.second->Update(true);
- // The style definition of each document should be independent of each other. By manually resetting these flags we avoid unnecessary definition
- // lookups in unrelated documents, such as when adding a new document. Adding an element dirties the parent definition, which in this case is the
- // root. By extension the definition of all the other documents are also dirtied, unnecessarily.
- root->dirty_definition = false;
- root->dirty_child_definitions = false;
- root->Update(density_independent_pixel_ratio, Vector2f(dimensions));
- for (int i = 0; i < root->GetNumChildren(); ++i)
- {
- if (auto doc = root->GetChild(i)->GetOwnerDocument())
- {
- doc->UpdateLayout();
- doc->UpdatePosition();
- }
- }
- // Release any documents that were unloaded during the update.
- ReleaseUnloadedDocuments();
- return true;
- }
- bool Context::Render()
- {
- RMLUI_ZoneScoped;
- render_manager->PrepareRender(dimensions);
- root->Render();
- // Render the cursor proxy so that any attached drag clone will be rendered below the cursor.
- if (drag_clone)
- {
- static_cast<ElementDocument&>(*cursor_proxy).UpdateDocument();
- cursor_proxy->SetOffset(
- Vector2f((float)Math::Clamp(mouse_position.x, 0, dimensions.x), (float)Math::Clamp(mouse_position.y, 0, dimensions.y)), nullptr);
- cursor_proxy->Render();
- }
- render_manager->ResetState();
- return true;
- }
- ElementDocument* Context::CreateDocument(const String& instancer_name)
- {
- NodePtr node = Factory::InstanceNode(instancer_name, documents_base_tag);
- if (!node)
- {
- Log::Message(Log::LT_ERROR, "Failed to instance document on instancer_name '%s', instancer returned nullptr.", instancer_name.c_str());
- return nullptr;
- }
- ElementDocument* document = rmlui_dynamic_cast<ElementDocument*>(node.get());
- if (!document)
- {
- Log::Message(Log::LT_ERROR,
- "Failed to instance document on instancer_name '%s', Found type '%s', was expecting derivative of ElementDocument.",
- instancer_name.c_str(), rmlui_type_name(*node));
- return nullptr;
- }
- document->context = this;
- root->AppendChild(std::move(node));
- PluginRegistry::NotifyDocumentLoad(document);
- return document;
- }
- ElementDocument* Context::LoadDocument(const String& document_path)
- {
- auto stream = MakeUnique<StreamFile>();
- if (!stream->Open(document_path))
- return nullptr;
- ElementDocument* document = LoadDocument(stream.get());
- return document;
- }
- ElementDocument* Context::LoadDocument(Stream* stream)
- {
- DebugVerifyLocaleSetting();
- PluginRegistry::NotifyDocumentOpen(this, stream->GetSourceURL().GetURL());
- NodePtr node = Factory::InstanceDocumentStream(this, stream, GetDocumentsBaseTag());
- if (!node)
- return nullptr;
- ElementDocument* document = rmlui_static_cast<ElementDocument*>(node.get());
- root->AppendChild(std::move(node));
- // The 'load' event is fired before updating the document, because the user might
- // need to initialize things before running an update. The drawback is that computed
- // values and layouting are not performed yet, resulting in default values when
- // querying such information in the event handler.
- PluginRegistry::NotifyDocumentLoad(document);
- document->DispatchEvent(EventId::Load, Dictionary());
- // Data models are updated after the 'load' event so that the user has a chance to change
- // any data variables first. We do not clear dirty variables here, since users may need to
- // retrieve whether or not eg. a data variable has changed in a controller.
- for (auto& data_model : data_models)
- data_model.second->Update(false);
- document->UpdateDocument();
- return document;
- }
- ElementDocument* Context::LoadDocumentFromMemory(const String& string, const String& source_url)
- {
- // Open the stream based on the string contents.
- auto stream = MakeUnique<StreamMemory>(reinterpret_cast<const byte*>(string.c_str()), string.size());
- stream->SetSourceURL(source_url);
- // Load the document from the stream.
- ElementDocument* document = LoadDocument(stream.get());
- return document;
- }
- void Context::UnloadDocument(ElementDocument* _document)
- {
- // Has this document already been unloaded?
- for (size_t i = 0; i < unloaded_documents.size(); ++i)
- {
- if (unloaded_documents[i].get() == _document)
- return;
- }
- ElementDocument* document = _document;
- if (document->GetParentNode() == root.get())
- {
- // Dispatch the unload notifications.
- document->DispatchEvent(EventId::Unload, Dictionary());
- PluginRegistry::NotifyDocumentUnload(document);
- // Remove from root and place document in a temporary location to be released later.
- unloaded_documents.push_back(document->Remove());
- }
- // Remove the item from the focus history.
- ElementList::iterator itr = std::find(document_focus_history.begin(), document_focus_history.end(), document);
- if (itr != document_focus_history.end())
- document_focus_history.erase(itr);
- // Focus to the previous document if the old document is the current focus.
- if (focus && focus->GetOwnerDocument() == document)
- {
- focus = nullptr;
- document_focus_history.back()->GetFocusLeafNode()->Focus();
- }
- // Clear the active element if the old document is the active element.
- if (active && active->GetOwnerDocument() == document)
- {
- active = nullptr;
- }
- // Clear other pointers to elements whose owner was deleted
- if (drag && drag->GetOwnerDocument() == document)
- {
- drag = nullptr;
- ReleaseDragClone();
- }
- if (drag_hover && drag_hover->GetOwnerDocument() == document)
- {
- drag_hover = nullptr;
- }
- // Rebuild the hover state.
- UpdateHoverChain(mouse_position);
- }
- void Context::UnloadAllDocuments()
- {
- // Unload all children.
- while (root->GetNumChildren(true) > 0)
- UnloadDocument(root->GetChild(0)->GetOwnerDocument());
- // The element lists may point to elements that are getting removed.
- active_chain.clear();
- hover_chain.clear();
- drag_hover_chain.clear();
- }
- void Context::EnableMouseCursor(bool enable)
- {
- // The cursor is set to an invalid name so that it is forced to update in the next update loop.
- cursor_name = ":reset:";
- enable_cursor = enable;
- }
- void Context::ActivateTheme(const String& theme_name, bool activate)
- {
- bool theme_changed = false;
- if (activate)
- theme_changed = active_themes.insert(theme_name).second;
- else
- theme_changed = (active_themes.erase(theme_name) > 0);
- if (theme_changed)
- {
- for (int i = 0; i < root->GetNumChildren(true); ++i)
- {
- if (ElementDocument* document = root->GetChild(i)->GetOwnerDocument())
- document->DirtyMediaQueries();
- }
- }
- }
- bool Context::IsThemeActive(const String& theme_name) const
- {
- return active_themes.count(theme_name);
- }
- ElementDocument* Context::GetDocument(const String& id)
- {
- for (int i = 0; i < root->GetNumChildren(); i++)
- {
- ElementDocument* document = root->GetChild(i)->GetOwnerDocument();
- if (document == nullptr)
- continue;
- if (document->GetId() == id)
- return document;
- }
- return nullptr;
- }
- ElementDocument* Context::GetDocument(int index)
- {
- Element* element = root->GetChild(index);
- if (element == nullptr)
- return nullptr;
- return element->GetOwnerDocument();
- }
- int Context::GetNumDocuments() const
- {
- return root->GetNumChildren();
- }
- Element* Context::GetHoverElement()
- {
- return hover;
- }
- Element* Context::GetFocusElement()
- {
- return focus;
- }
- Element* Context::GetRootElement()
- {
- return root.get();
- }
- void Context::PullDocumentToFront(ElementDocument* document)
- {
- if (document != root->GetLastChild())
- {
- // Calling RemoveChild() / AppendChild() would be cleaner, but that dirties the document's layout
- // unnecessarily, so we'll go under the hood here.
- for (int i = 0; i < root->GetNumChildren(); ++i)
- {
- if (root->GetChild(i) == document)
- {
- NodePtr node = std::move(root->children[i]);
- root->children.erase(root->children.begin() + i);
- root->children.insert(root->children.begin() + root->GetNumChildren(), std::move(node));
- root->DirtyStackingContext();
- }
- }
- }
- }
- void Context::PushDocumentToBack(ElementDocument* document)
- {
- if (document != root->GetFirstChild())
- {
- // See PullDocumentToFront().
- for (int i = 0; i < root->GetNumChildren(); ++i)
- {
- if (root->GetChild(i) == document)
- {
- NodePtr node = std::move(root->children[i]);
- root->children.erase(root->children.begin() + i);
- root->children.insert(root->children.begin(), std::move(node));
- root->DirtyStackingContext();
- }
- }
- }
- }
- void Context::UnfocusDocument(ElementDocument* document)
- {
- auto it = std::find(document_focus_history.begin(), document_focus_history.end(), document);
- if (it != document_focus_history.end())
- document_focus_history.erase(it);
- if (!document_focus_history.empty())
- document_focus_history.back()->GetFocusLeafNode()->Focus();
- }
- void Context::AddEventListener(const String& event, EventListener* listener, bool in_capture_phase)
- {
- root->AddEventListener(event, listener, in_capture_phase);
- }
- void Context::RemoveEventListener(const String& event, EventListener* listener, bool in_capture_phase)
- {
- root->RemoveEventListener(event, listener, in_capture_phase);
- }
- bool Context::ProcessKeyDown(Input::KeyIdentifier key_identifier, int key_modifier_state)
- {
- // Generate the parameters for the key event.
- Dictionary parameters;
- GenerateKeyEventParameters(parameters, key_identifier);
- GenerateKeyModifierEventParameters(parameters, key_modifier_state);
- if (focus)
- return focus->DispatchEvent(EventId::Keydown, parameters);
- else
- return root->DispatchEvent(EventId::Keydown, parameters);
- }
- bool Context::ProcessKeyUp(Input::KeyIdentifier key_identifier, int key_modifier_state)
- {
- // Generate the parameters for the key event.
- Dictionary parameters;
- GenerateKeyEventParameters(parameters, key_identifier);
- GenerateKeyModifierEventParameters(parameters, key_modifier_state);
- if (focus)
- return focus->DispatchEvent(EventId::Keyup, parameters);
- else
- return root->DispatchEvent(EventId::Keyup, parameters);
- }
- bool Context::ProcessTextInput(char character)
- {
- // Only the standard ASCII character set is a valid subset of UTF-8.
- if (static_cast<unsigned char>(character) > 127)
- return false;
- return ProcessTextInput(static_cast<Character>(character));
- }
- bool Context::ProcessTextInput(Character character)
- {
- // Generate the parameters for the key event.
- String text = StringUtilities::ToUTF8(character);
- return ProcessTextInput(text);
- }
- bool Context::ProcessTextInput(const String& string)
- {
- Element* target = (focus ? focus : root.get());
- Dictionary parameters;
- parameters["text"] = string;
- bool consumed = target->DispatchEvent(EventId::Textinput, parameters);
- return consumed;
- }
- bool Context::ProcessMouseMove(int x, int y, int key_modifier_state)
- {
- // Check whether the mouse moved since the last event came through.
- Vector2i old_mouse_position = mouse_position;
- mouse_position = {x, y};
- const bool mouse_moved = (mouse_position != old_mouse_position || !mouse_active);
- mouse_active = true;
- // Update the current hover chain. This will send all necessary 'onmouseout', 'onmouseover', 'ondragout' and 'ondragover' messages.
- Dictionary parameters, drag_parameters;
- UpdateHoverChain(old_mouse_position, key_modifier_state, ¶meters, &drag_parameters);
- // Dispatch any 'onmousemove' events.
- if (mouse_moved)
- {
- if (hover)
- {
- hover->DispatchEvent(EventId::Mousemove, parameters);
- if (drag_hover && drag_verbose)
- drag_hover->DispatchEvent(EventId::Dragmove, drag_parameters);
- }
- }
- return !IsMouseInteracting();
- }
- static Element* FindFocusElement(Element* element)
- {
- ElementDocument* owner_document = element->GetOwnerDocument();
- if (!owner_document || owner_document->GetComputedValues().focus() == Style::Focus::None)
- return nullptr;
- while (element && element->GetComputedValues().focus() == Style::Focus::None)
- {
- element = element->GetParentElement();
- }
- return element;
- }
- bool Context::ProcessMouseButtonDown(int button_index, int key_modifier_state)
- {
- Dictionary parameters;
- GenerateMouseEventParameters(parameters, button_index);
- GenerateKeyModifierEventParameters(parameters, key_modifier_state);
- bool propagate = true;
- if (button_index == 0)
- {
- Element* new_focus = hover;
- // Set the currently hovered element to focus if it isn't already the focus.
- if (hover)
- {
- new_focus = FindFocusElement(hover);
- if (new_focus && new_focus != focus)
- {
- if (!new_focus->Focus())
- return !IsMouseInteracting();
- }
- }
- // Save the just-pressed-on element as the pressed element.
- active = new_focus;
- // Call 'onmousedown' on every item in the hover chain, and copy the hover chain to the active chain.
- if (hover)
- propagate = hover->DispatchEvent(EventId::Mousedown, parameters);
- if (propagate)
- {
- // Check for a double-click on an element; if one has occured, we send the 'dblclick' event to the hover
- // element. If not, we'll start a timer to catch the next one.
- float mouse_distance_squared = float((mouse_position - last_click_mouse_position).SquaredMagnitude());
- float max_mouse_distance = DOUBLE_CLICK_MAX_DIST * density_independent_pixel_ratio;
- double click_time = GetSystemInterface()->GetElapsedTime();
- if (active == last_click_element && float(click_time - last_click_time) < DOUBLE_CLICK_TIME &&
- mouse_distance_squared < max_mouse_distance * max_mouse_distance)
- {
- if (hover)
- propagate = hover->DispatchEvent(EventId::Dblclick, parameters);
- last_click_element = nullptr;
- last_click_time = 0;
- }
- else
- {
- last_click_element = active;
- last_click_time = click_time;
- }
- }
- last_click_mouse_position = mouse_position;
- active_chain.insert(active_chain.end(), hover_chain.begin(), hover_chain.end());
- if (propagate)
- {
- // Traverse down the hierarchy of the newly focused element (if any), and see if we can begin dragging it.
- drag_started = false;
- drag = hover;
- while (drag)
- {
- Style::Drag drag_style = drag->GetComputedValues().drag();
- switch (drag_style)
- {
- case Style::Drag::None: drag = drag->GetParentElement(); continue;
- case Style::Drag::Block: drag = nullptr; continue;
- default: drag_verbose = (drag_style == Style::Drag::DragDrop || drag_style == Style::Drag::Clone);
- }
- break;
- }
- }
- }
- else
- {
- // Not the primary mouse button, so we're not doing any special processing.
- if (hover)
- propagate = hover->DispatchEvent(EventId::Mousedown, parameters);
- }
- if (scroll_controller->GetMode() == ScrollController::Mode::Autoscroll)
- {
- scroll_controller->Reset();
- }
- else if (button_index == 2 && hover && propagate)
- {
- Dictionary scroll_parameters;
- GenerateMouseEventParameters(scroll_parameters);
- GenerateKeyModifierEventParameters(scroll_parameters, key_modifier_state);
- scroll_parameters["autoscroll"] = true;
- // Dispatch a mouse scroll event, this gives elements an opportunity to block autoscroll from being initialized.
- if (hover->DispatchEvent(EventId::Mousescroll, scroll_parameters))
- scroll_controller->ActivateAutoscroll(hover->GetClosestScrollableContainer(), mouse_position);
- }
- return !IsMouseInteracting();
- }
- bool Context::ProcessMouseButtonUp(int button_index, int key_modifier_state)
- {
- Dictionary parameters;
- GenerateMouseEventParameters(parameters, button_index);
- GenerateKeyModifierEventParameters(parameters, key_modifier_state);
- // We want to return the interaction state before handling the mouse up events, so that any active element that is released is considered to
- // capture the event.
- const bool result = !IsMouseInteracting();
- // Process primary click.
- if (button_index == 0)
- {
- // The elements in the new hover chain have the 'onmouseup' event called on them.
- if (hover)
- hover->DispatchEvent(EventId::Mouseup, parameters);
- // If the active element (the one that was being hovered over when the mouse button was pressed) is still being
- // hovered over, we click it.
- if (hover && active && active == FindFocusElement(hover))
- {
- active->DispatchEvent(EventId::Click, parameters);
- }
- // Unset the 'active' pseudo-class on all the elements in the active chain; because they may not necessarily
- // have had 'onmouseup' called on them, we can't guarantee this has happened already.
- for (Element* element : active_chain)
- element->SetPseudoClass("active", false);
- active_chain.clear();
- active = nullptr;
- if (drag)
- {
- if (drag_started)
- {
- Dictionary drag_parameters;
- GenerateMouseEventParameters(drag_parameters);
- GenerateDragEventParameters(drag_parameters);
- GenerateKeyModifierEventParameters(drag_parameters, key_modifier_state);
- if (drag_hover)
- {
- if (drag_verbose)
- {
- drag_hover->DispatchEvent(EventId::Dragdrop, drag_parameters);
- // User may have removed the element, do an extra check.
- if (drag_hover)
- drag_hover->DispatchEvent(EventId::Dragout, drag_parameters);
- }
- }
- if (drag)
- drag->DispatchEvent(EventId::Dragend, drag_parameters);
- ReleaseDragClone();
- }
- drag = nullptr;
- drag_hover = nullptr;
- drag_hover_chain.clear();
- // We may have changes under our mouse, this ensures that the hover chain is properly updated
- ProcessMouseMove(mouse_position.x, mouse_position.y, key_modifier_state);
- }
- }
- else
- {
- // Not the left mouse button, so we're not doing any special processing.
- if (hover)
- hover->DispatchEvent(EventId::Mouseup, parameters);
- }
- // If we have autoscrolled while holding the middle mouse button, release the autoscroll mode now.
- if (scroll_controller->HasAutoscrollMoved())
- scroll_controller->Reset();
- return result;
- }
- bool Context::ProcessMouseWheel(float wheel_delta, int key_modifier_state)
- {
- return ProcessMouseWheel(Vector2f{0.f, wheel_delta}, key_modifier_state);
- }
- bool Context::ProcessMouseWheel(Vector2f wheel_delta, int key_modifier_state)
- {
- if (scroll_controller->GetMode() == ScrollController::Mode::Autoscroll)
- {
- scroll_controller->Reset();
- return false;
- }
- else if (!hover)
- {
- scroll_controller->Reset();
- return true;
- }
- Dictionary scroll_parameters;
- GenerateMouseEventParameters(scroll_parameters);
- GenerateKeyModifierEventParameters(scroll_parameters, key_modifier_state);
- scroll_parameters["wheel_delta_x"] = wheel_delta.x;
- scroll_parameters["wheel_delta_y"] = wheel_delta.y;
- // Dispatch a mouse scroll event, this gives elements an opportunity to block scrolling from being performed.
- if (!hover->DispatchEvent(EventId::Mousescroll, scroll_parameters))
- return false;
- const float unit_scroll_length = UNIT_SCROLL_LENGTH * density_independent_pixel_ratio;
- const Vector2f scroll_length = wheel_delta * unit_scroll_length;
- Element* target = hover->GetClosestScrollableContainer();
- if (scroll_controller->GetMode() == ScrollController::Mode::Smoothscroll && scroll_controller->GetTarget() == target)
- scroll_controller->IncrementSmoothscrollTarget(scroll_length);
- else
- scroll_controller->ActivateSmoothscroll(target, scroll_length, ScrollBehavior::Auto);
- return target == nullptr;
- }
- bool Context::ProcessMouseLeave()
- {
- mouse_active = false;
- // Update the hover chain. Now that 'mouse_active' is disabled this will remove the hover state from all elements.
- UpdateHoverChain(mouse_position);
- return !IsMouseInteracting();
- }
- bool Context::IsMouseInteracting() const
- {
- return (hover && hover != root.get()) || (active && active != root.get()) || scroll_controller->GetMode() == ScrollController::Mode::Autoscroll;
- }
- Context::TouchState* Context::LookupTouch(TouchId identifier)
- {
- auto touch_it = touch_states.find(identifier);
- return touch_it != touch_states.end() ? &touch_it->second : nullptr;
- }
- bool Context::ProcessTouchStart(const TouchList& touches, int key_modifier_state)
- {
- bool result = true;
- for (const auto& touch : touches)
- result &= ProcessTouchStart(touch, key_modifier_state);
- return result;
- }
- bool Context::ProcessTouchMove(const TouchList& touches, int key_modifier_state)
- {
- bool result = true;
- for (const auto& touch : touches)
- result &= ProcessTouchMove(touch, key_modifier_state);
- return result;
- }
- bool Context::ProcessTouchEnd(const TouchList& touches, int key_modifier_state)
- {
- bool result = true;
- for (const auto& touch : touches)
- result &= ProcessTouchEnd(touch, key_modifier_state);
- return result;
- }
- bool Context::ProcessTouchCancel(const TouchList& touches)
- {
- bool result = true;
- for (const auto& touch : touches)
- result &= ProcessTouchCancel(touch);
- return result;
- }
- bool Context::ProcessTouchStart(const Touch& touch, int key_modifier_state)
- {
- TouchState* state = LookupTouch(touch.identifier);
- RMLUI_ASSERTMSG(state == nullptr, "Receiving touch start event for an already started touch.");
- if (!state)
- {
- auto it_inserted = touch_states.emplace(touch.identifier, TouchState()).first;
- state = &it_inserted->second;
- }
- state->start_position = state->last_position = touch.position;
- state->scrolling_start_time_x = state->scrolling_start_time_y = 0;
- state->scrolling_last_time = GetSystemInterface()->GetElapsedTime();
- Element* touch_element = GetElementAtPoint(touch.position);
- state->scroll_container = touch_element ? touch_element->GetClosestScrollableContainer() : nullptr;
- // reset any scrolling when we touch the element
- if (state->scroll_container && scroll_controller->GetTarget() == state->scroll_container)
- scroll_controller->Reset();
- ProcessMouseMove(static_cast<int>(touch.position.x), static_cast<int>(touch.position.y), key_modifier_state);
- // always assume touch press/release events are handled as left mouse button
- return ProcessMouseButtonDown(0, key_modifier_state);
- }
- bool Context::ProcessTouchMove(const Touch& touch, int key_modifier_state)
- {
- TouchState* state = LookupTouch(touch.identifier);
- if (!state)
- return true;
- state->scrolling_last_time = GetSystemInterface()->GetElapsedTime();
- if (state->scroll_container)
- {
- if (drag)
- {
- // Don't scroll and reset scrolling state when dragging any element (scrollbars and others)
- state->start_position = state->last_position = touch.position;
- state->scrolling_start_time_x = state->scrolling_start_time_y = 0;
- }
- else
- {
- Vector2f delta = touch.position - state->last_position;
- if (delta.x != 0 || delta.y != 0)
- {
- // use instant scrolling when touch is pressed even when default scroll behavior is smooth
- scroll_controller->InstantScrollOnTarget(state->scroll_container, -delta);
- double current_time = GetSystemInterface()->GetElapsedTime();
- // If the user changes direction, reset the start time and position.
- bool going_right = (delta.x > 0);
- if (delta.x != 0 &&
- (going_right != state->scrolling_right ||
- state->scrolling_start_time_x == 0)) // time set to 0 means no touch move events happened before and direction is unclear
- {
- state->start_position.x = touch.position.x;
- state->scrolling_right = going_right;
- state->scrolling_start_time_x = state->scrolling_last_time;
- }
- else
- {
- // move starting position towards end position with a weight of e^-kt to better capture
- // and calculate velocity of the very last touch movements before touch release
- float elapsed_time_x = static_cast<float>(current_time - state->scrolling_start_time_x);
- float weight = Math::Exp(-elapsed_time_x * TOUCH_MOVEMENT_DECAY_RATE);
- state->start_position.x = touch.position.x - (touch.position.x - state->start_position.x) * weight;
- state->scrolling_start_time_x = current_time - (current_time - state->scrolling_start_time_x) * weight;
- }
- bool going_down = (delta.y > 0);
- if (delta.y != 0 &&
- (going_down != state->scrolling_down ||
- state->scrolling_start_time_y == 0)) // time set to 0 means no touch move events happened before and direction is unclear
- {
- state->start_position.y = touch.position.y;
- state->scrolling_down = going_down;
- state->scrolling_start_time_y = state->scrolling_last_time;
- }
- else
- {
- // move starting position towards end position with a weight of e^-kt to better capture
- // and calculate velocity of the very last touch movements before touch release
- float elapsed_time_y = static_cast<float>(current_time - state->scrolling_start_time_y);
- float weight = Math::Exp(-elapsed_time_y * TOUCH_MOVEMENT_DECAY_RATE);
- state->start_position.y = touch.position.y - (touch.position.y - state->start_position.y) * weight;
- state->scrolling_start_time_y = current_time - (current_time - state->scrolling_start_time_y) * weight;
- }
- }
- }
- }
- state->last_position = touch.position;
- return ProcessMouseMove(static_cast<int>(touch.position.x), static_cast<int>(touch.position.y), key_modifier_state);
- }
- bool Context::ProcessTouchEnd(const Touch& touch, int key_modifier_state)
- {
- TouchState* state = LookupTouch(touch.identifier);
- if (!state)
- return true;
- if (state->scroll_container)
- {
- double current_time = GetSystemInterface()->GetElapsedTime();
- double time_since_last_move = current_time - state->scrolling_last_time;
- if (time_since_last_move < SCROLL_INERTIA_DELAY)
- {
- // apply scrolling inertia
- Vector2f delta = touch.position - state->start_position;
- Vector2f velocity(-delta);
- float elapsed_time_x = static_cast<float>(current_time - state->scrolling_start_time_x);
- float elapsed_time_y = static_cast<float>(current_time - state->scrolling_start_time_y);
- if (elapsed_time_x > 0)
- velocity.x /= elapsed_time_x;
- if (elapsed_time_y > 0)
- velocity.y /= elapsed_time_y;
- scroll_controller->ApplyScrollInertia(state->scroll_container, velocity);
- }
- }
- touch_states.erase(touch.identifier);
- ProcessMouseMove(static_cast<int>(touch.position.x), static_cast<int>(touch.position.y), key_modifier_state);
- // always assume touch press/release events are handled as left mouse button
- return ProcessMouseButtonUp(0, key_modifier_state);
- }
- bool Context::ProcessTouchCancel(const Touch& touch)
- {
- TouchState* state = LookupTouch(touch.identifier);
- if (!state)
- return false;
- touch_states.erase(touch.identifier);
- return ProcessMouseButtonUp(0, 0);
- }
- void Context::SetDefaultScrollBehavior(ScrollBehavior scroll_behavior, float speed_factor)
- {
- scroll_controller->SetDefaultScrollBehavior(scroll_behavior, speed_factor);
- }
- RenderManager& Context::GetRenderManager()
- {
- return *render_manager;
- }
- TextInputHandler* Context::GetTextInputHandler() const
- {
- return text_input_handler;
- }
- void Context::SetInstancer(ContextInstancer* _instancer)
- {
- RMLUI_ASSERT(instancer == nullptr);
- instancer = _instancer;
- }
- DataModelConstructor Context::CreateDataModel(const String& name, DataTypeRegister* data_type_register)
- {
- if (!data_type_register)
- {
- if (!default_data_type_register)
- default_data_type_register = MakeUnique<DataTypeRegister>();
- data_type_register = default_data_type_register.get();
- }
- auto result = data_models.emplace(name, MakeUnique<DataModel>(data_type_register));
- bool inserted = result.second;
- if (inserted)
- return DataModelConstructor(result.first->second.get());
- Log::Message(Log::LT_ERROR, "Data model name '%s' already exists.", name.c_str());
- return DataModelConstructor();
- }
- DataModelConstructor Context::GetDataModel(const String& name)
- {
- if (DataModel* model = GetDataModelPtr(name))
- return DataModelConstructor(model);
- Log::Message(Log::LT_ERROR, "Data model name '%s' could not be found.", name.c_str());
- return DataModelConstructor();
- }
- bool Context::RemoveDataModel(const String& name)
- {
- auto it = data_models.find(name);
- if (it == data_models.end())
- return false;
- DataModel* model = it->second.get();
- ElementList elements = model->GetAttachedModelRootElements();
- for (Element* element : elements)
- element->SetDataModel(nullptr);
- data_models.erase(it);
- return true;
- }
- void Context::OnElementDetach(Element* element)
- {
- auto it_hover = hover_chain.find(element);
- if (it_hover != hover_chain.end())
- {
- Dictionary parameters;
- GenerateMouseEventParameters(parameters, -1);
- element->DispatchEvent(EventId::Mouseout, parameters);
- hover_chain.erase(it_hover);
- if (hover == element)
- hover = nullptr;
- }
- auto it_active = std::find(active_chain.begin(), active_chain.end(), element);
- if (it_active != active_chain.end())
- {
- active_chain.erase(it_active);
- if (active == element)
- active = nullptr;
- }
- if (drag)
- {
- auto it = drag_hover_chain.find(element);
- if (it != drag_hover_chain.end())
- {
- drag_hover_chain.erase(it);
- if (drag_hover == element)
- drag_hover = nullptr;
- }
- if (drag == element)
- {
- // The dragged element is being removed, silently cancel the drag operation
- if (drag_started)
- ReleaseDragClone();
- drag = nullptr;
- drag_hover = nullptr;
- drag_hover_chain.clear();
- }
- }
- // Focus normally cleared and set by parent during Element::RemoveChild.
- // However, there are some exceptions, such as when an there are multiple
- // ElementDocuments in the hierarchy above the current element.
- if (element == focus)
- focus = nullptr;
- // If the element is a document lower down in the hierarchy, we may need to remove it from the focus history.
- if (element->GetOwnerDocument() == element)
- {
- auto it = std::find(document_focus_history.begin(), document_focus_history.end(), element);
- if (it != document_focus_history.end())
- document_focus_history.erase(it);
- }
- if (scroll_controller->GetTarget() == element)
- scroll_controller->Reset();
- // Clear TouchState if we're touching element
- for (auto touch_it = touch_states.begin(); touch_it != touch_states.end();)
- {
- if (touch_it->second.scroll_container == element)
- touch_it = touch_states.erase(touch_it);
- else
- ++touch_it;
- }
- }
- bool Context::OnFocusChange(Element* new_focus, bool focus_visible)
- {
- RMLUI_ASSERT(new_focus);
- ElementSet old_chain;
- ElementSet new_chain;
- Element* old_focus = focus;
- ElementDocument* old_document = old_focus ? old_focus->GetOwnerDocument() : nullptr;
- ElementDocument* new_document = new_focus->GetOwnerDocument();
- // If the current focus is modal and the new focus is cannot receive focus from modal, deny the request.
- if (old_document && old_document->IsModal() && (!new_document || !(new_document->IsModal() || new_document->IsFocusableFromModal())))
- return false;
- // If the document of the new focus has been closed, deny the request.
- if (std::find_if(unloaded_documents.begin(), unloaded_documents.end(),
- [&](const auto& unloaded_document) { return unloaded_document.get() == new_document; }) != unloaded_documents.end())
- {
- return false;
- }
- // Build the old chains
- Element* element = old_focus;
- while (element)
- {
- old_chain.insert(element);
- element = element->GetParentElement();
- }
- // Build the new chain
- element = new_focus;
- while (element)
- {
- new_chain.insert(element);
- element = element->GetParentElement();
- }
- // Send out blur/focus events.
- Dictionary parameters;
- SendEvents(old_chain, new_chain, EventId::Blur, parameters);
- if (focus_visible)
- parameters["focus_visible"] = true;
- SendEvents(new_chain, old_chain, EventId::Focus, parameters);
- focus = new_focus;
- // Raise the element's document to the front, if desired.
- ElementDocument* document = focus->GetOwnerDocument();
- if (document != nullptr)
- {
- Style::ZIndex z_index_property = document->GetComputedValues().z_index();
- if (z_index_property.type == Style::ZIndex::Auto)
- document->PullToFront();
- }
- // Update the focus history
- if (old_document != new_document)
- {
- // If documents have changed, add the new document to the end of the history
- ElementList::iterator itr = std::find(document_focus_history.begin(), document_focus_history.end(), new_document);
- if (itr != document_focus_history.end())
- document_focus_history.erase(itr);
- if (new_document != nullptr)
- document_focus_history.push_back(new_document);
- }
- return true;
- }
- void Context::GenerateClickEvent(Element* element)
- {
- Dictionary parameters;
- GenerateMouseEventParameters(parameters, 0);
- element->DispatchEvent(EventId::Click, parameters);
- }
- void Context::UpdateHoverChain(Vector2i old_mouse_position, int key_modifier_state, Dictionary* out_parameters, Dictionary* out_drag_parameters)
- {
- const Vector2f position(mouse_position);
- Dictionary local_parameters, local_drag_parameters;
- Dictionary& parameters = out_parameters ? *out_parameters : local_parameters;
- Dictionary& drag_parameters = out_drag_parameters ? *out_drag_parameters : local_drag_parameters;
- // Generate the parameters for the mouse events (there could be a few!).
- GenerateMouseEventParameters(parameters);
- GenerateKeyModifierEventParameters(parameters, key_modifier_state);
- GenerateMouseEventParameters(drag_parameters);
- GenerateDragEventParameters(drag_parameters);
- GenerateKeyModifierEventParameters(drag_parameters, key_modifier_state);
- // Send out drag events.
- if (drag)
- {
- if (mouse_position != old_mouse_position)
- {
- if (!drag_started)
- {
- Dictionary drag_start_parameters = drag_parameters;
- drag_start_parameters["mouse_x"] = old_mouse_position.x;
- drag_start_parameters["mouse_y"] = old_mouse_position.y;
- drag->DispatchEvent(EventId::Dragstart, drag_start_parameters);
- drag_started = true;
- if (drag->GetComputedValues().drag() == Style::Drag::Clone)
- {
- // Clone the element and attach it to the mouse cursor.
- CreateDragClone(drag);
- }
- }
- drag->DispatchEvent(EventId::Drag, drag_parameters);
- }
- }
- hover = mouse_active ? GetElementAtPoint(position) : nullptr;
- if (enable_cursor)
- {
- String new_cursor_name;
- if (scroll_controller->GetMode() == ScrollController::Mode::Autoscroll)
- new_cursor_name = scroll_controller->GetAutoscrollCursor(mouse_position, density_independent_pixel_ratio);
- else if (drag)
- new_cursor_name = drag->GetComputedValues().cursor();
- else if (hover)
- new_cursor_name = hover->GetComputedValues().cursor();
- if (new_cursor_name != cursor_name)
- {
- GetSystemInterface()->SetMouseCursor(new_cursor_name);
- cursor_name = new_cursor_name;
- }
- }
- // Build the new hover chain.
- ElementSet new_hover_chain;
- Element* element = hover;
- while (element != nullptr)
- {
- new_hover_chain.insert(element);
- element = element->GetParentElement();
- }
- // Send mouseout / mouseover events.
- SendEvents(hover_chain, new_hover_chain, EventId::Mouseout, parameters);
- SendEvents(new_hover_chain, hover_chain, EventId::Mouseover, parameters);
- // Send out drag events.
- if (drag && mouse_active)
- {
- drag_hover = GetElementAtPoint(position, drag);
- ElementSet new_drag_hover_chain;
- element = drag_hover;
- while (element != nullptr)
- {
- new_drag_hover_chain.insert(element);
- element = element->GetParentElement();
- }
- if (drag_started && drag_verbose)
- {
- // Send out ondragover and ondragout events as appropriate.
- SendEvents(drag_hover_chain, new_drag_hover_chain, EventId::Dragout, drag_parameters);
- SendEvents(new_drag_hover_chain, drag_hover_chain, EventId::Dragover, drag_parameters);
- }
- drag_hover_chain.swap(new_drag_hover_chain);
- }
- // Swap the new chain in.
- hover_chain.swap(new_hover_chain);
- }
- Element* Context::GetElementAtPoint(Vector2f point, const Element* ignore_element, Element* element) const
- {
- if (!element)
- {
- if (ignore_element == root.get())
- return nullptr;
- element = root.get();
- }
- bool is_modal = false;
- ElementDocument* focus_document = nullptr;
- // If we have modal focus, only check down documents that can receive focus from modals.
- if (element == root.get() && focus)
- {
- focus_document = focus->GetOwnerDocument();
- if (focus_document && focus_document->IsModal())
- is_modal = true;
- }
- // Check any elements within our stacking context. We want to return the lowest-down element
- // that is under the cursor.
- if (element->local_stacking_context)
- {
- if (element->stacking_context_dirty)
- element->BuildLocalStackingContext();
- for (int i = (int)element->stacking_context.size() - 1; i >= 0; --i)
- {
- Element* stacking_child = element->stacking_context[i];
- if (ignore_element)
- {
- // Check if the element is a descendant of the element we're ignoring.
- Element* element_hierarchy = stacking_child;
- while (element_hierarchy)
- {
- if (element_hierarchy == ignore_element)
- break;
- element_hierarchy = element_hierarchy->GetParentElement();
- }
- if (element_hierarchy)
- continue;
- }
- if (is_modal)
- {
- ElementDocument* child_document = stacking_child->GetOwnerDocument();
- if (!child_document || !(child_document == focus_document || child_document->IsFocusableFromModal()))
- continue;
- }
- Element* child_element = GetElementAtPoint(point, ignore_element, stacking_child);
- if (child_element)
- return child_element;
- }
- }
- // Ignore elements whose pointer events are disabled.
- if (element->GetComputedValues().pointer_events() == Style::PointerEvents::None)
- return nullptr;
- // Projection may fail if we have a singular transformation matrix.
- bool projection_result = element->Project(point);
- // Check if the point is actually within this element.
- bool within_element = (projection_result && element->IsPointWithinElement(point));
- if (within_element)
- {
- // The element may have been clipped out of view if it overflows an ancestor, so check its clipping region.
- Rectanglei clip_region;
- if (ElementUtilities::GetClippingRegion(element, clip_region))
- within_element = clip_region.Contains(Vector2i(point));
- }
- if (within_element)
- return element;
- return nullptr;
- }
- void Context::CreateDragClone(Element* element)
- {
- RMLUI_ASSERTMSG(cursor_proxy, "Unable to create drag clone, no cursor proxy document.");
- ReleaseDragClone();
- // Instance the drag clone.
- ElementPtr element_drag_clone = element->Clone();
- if (!element_drag_clone)
- {
- Log::Message(Log::LT_ERROR, "Unable to duplicate drag clone.");
- return;
- }
- // Set the style sheet on the cursor proxy.
- if (ElementDocument* document = element->GetOwnerDocument())
- {
- // Borrow the target document's style sheet. Sharing style sheet containers should be used with care, and
- // only within the same context.
- static_cast<ElementDocument&>(*cursor_proxy).SetStyleSheetContainer(document->style_sheet_container);
- }
- drag_clone = element_drag_clone.get();
- // Append the clone to the cursor proxy element.
- cursor_proxy->AppendChild(As<NodePtr>(std::move(element_drag_clone)));
- // Position the clone. Use projected mouse coordinates to handle any ancestor transforms.
- const Vector2f absolute_pos = element->GetAbsoluteOffset(BoxArea::Border);
- Vector2f projected_mouse_position = Vector2f(mouse_position);
- if (Element* parent = element->GetParentElement())
- parent->Project(projected_mouse_position);
- drag_clone->SetProperty(PropertyId::Position, Property(Style::Position::Absolute));
- drag_clone->SetProperty(PropertyId::Left, Property(absolute_pos.x - projected_mouse_position.x, Unit::PX));
- drag_clone->SetProperty(PropertyId::Top, Property(absolute_pos.y - projected_mouse_position.y, Unit::PX));
- // We remove margins so that percentage- and auto-margins are evaluated correctly.
- drag_clone->SetProperty(PropertyId::MarginLeft, Property(0.f, Unit::PX));
- drag_clone->SetProperty(PropertyId::MarginTop, Property(0.f, Unit::PX));
- drag_clone->SetPseudoClass("drag", true);
- }
- void Context::ReleaseDragClone()
- {
- if (drag_clone)
- {
- cursor_proxy->RemoveChild(drag_clone);
- drag_clone = nullptr;
- static_cast<ElementDocument&>(*cursor_proxy).SetStyleSheetContainer(nullptr);
- }
- }
- void Context::PerformSmoothscrollOnTarget(Element* target, Vector2f delta_offset, ScrollBehavior scroll_behavior)
- {
- scroll_controller->ActivateSmoothscroll(target, delta_offset, scroll_behavior);
- }
- DataModel* Context::GetDataModelPtr(const String& name) const
- {
- auto it = data_models.find(name);
- if (it != data_models.end())
- return it->second.get();
- return nullptr;
- }
- void Context::GenerateKeyEventParameters(Dictionary& parameters, Input::KeyIdentifier key_identifier)
- {
- parameters["key_identifier"] = (int)key_identifier;
- }
- void Context::GenerateMouseEventParameters(Dictionary& parameters, int button_index)
- {
- parameters.reserve(3);
- parameters["mouse_x"] = mouse_position.x;
- parameters["mouse_y"] = mouse_position.y;
- if (button_index >= 0)
- parameters["button"] = button_index;
- }
- void Context::GenerateKeyModifierEventParameters(Dictionary& parameters, int key_modifier_state)
- {
- static const String property_names[] = {"ctrl_key", "shift_key", "alt_key", "meta_key", "caps_lock_key", "num_lock_key", "scroll_lock_key"};
- for (int i = 0; i < 7; i++)
- parameters[property_names[i]] = (int)((key_modifier_state & (1 << i)) > 0);
- }
- void Context::GenerateDragEventParameters(Dictionary& parameters)
- {
- parameters["drag_element"] = (void*)drag;
- }
- void Context::ReleaseUnloadedDocuments()
- {
- if (!unloaded_documents.empty())
- {
- OwnedElementList documents = std::move(unloaded_documents);
- unloaded_documents.clear();
- // Clear the deleted list.
- for (size_t i = 0; i < documents.size(); ++i)
- documents[i]->GetEventDispatcher()->DetachAllEvents();
- documents.clear();
- }
- }
- using ElementObserverList = Vector<ObserverPtr<Element>>;
- class ElementObserverListBackInserter {
- public:
- using iterator_category = std::output_iterator_tag;
- using value_type = void;
- using difference_type = void;
- using pointer = void;
- using reference = void;
- using container_type = ElementObserverList;
- ElementObserverListBackInserter(ElementObserverList& elements) : elements(&elements) {}
- ElementObserverListBackInserter& operator=(Element* element)
- {
- elements->push_back(element->GetObserverPtr());
- return *this;
- }
- ElementObserverListBackInserter& operator*() { return *this; }
- ElementObserverListBackInserter& operator++() { return *this; }
- ElementObserverListBackInserter& operator++(int) { return *this; }
- private:
- ElementObserverList* elements;
- };
- void Context::SendEvents(const ElementSet& old_items, const ElementSet& new_items, EventId id, const Dictionary& parameters)
- {
- // We put our elements in observer pointers in case some of them are deleted during dispatch.
- ElementObserverList elements;
- std::set_difference(old_items.begin(), old_items.end(), new_items.begin(), new_items.end(), ElementObserverListBackInserter(elements));
- for (auto& element : elements)
- {
- if (element)
- element->DispatchEvent(id, parameters);
- }
- }
- void Context::Release()
- {
- if (instancer)
- {
- instancer->ReleaseContext(this);
- }
- }
- void Context::SetDocumentsBaseTag(const String& tag)
- {
- documents_base_tag = tag;
- }
- const String& Context::GetDocumentsBaseTag()
- {
- return documents_base_tag;
- }
- void Context::RequestNextUpdate(double delay)
- {
- RMLUI_ASSERT(delay >= 0.0);
- next_update_timeout = Math::Min(next_update_timeout, delay);
- }
- double Context::GetNextUpdateDelay() const
- {
- return next_update_timeout;
- }
- } // namespace Rml
|