| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663 |
- /*
- * 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 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/ElementDocument.h"
- #include "../../Include/RmlUi/Core/Context.h"
- #include "../../Include/RmlUi/Core/ElementText.h"
- #include "../../Include/RmlUi/Core/Factory.h"
- #include "../../Include/RmlUi/Core/Profiling.h"
- #include "../../Include/RmlUi/Core/StreamMemory.h"
- #include "../../Include/RmlUi/Core/StyleSheet.h"
- #include "../../Include/RmlUi/Core/StyleSheetContainer.h"
- #include "DocumentHeader.h"
- #include "ElementStyle.h"
- #include "EventDispatcher.h"
- #include "LayoutEngine.h"
- #include "StreamFile.h"
- #include "StyleSheetFactory.h"
- #include "Template.h"
- #include "TemplateCache.h"
- #include "XMLParseTools.h"
- namespace Rml {
- ElementDocument::ElementDocument(const String& tag) : Element(tag)
- {
- context = nullptr;
- modal = false;
- layout_dirty = true;
- position_dirty = false;
- ForceLocalStackingContext();
- SetOwnerDocument(this);
- SetProperty(PropertyId::Position, Property(Style::Position::Absolute));
- }
- ElementDocument::~ElementDocument()
- {
- }
- void ElementDocument::ProcessHeader(const DocumentHeader* document_header)
- {
- RMLUI_ZoneScoped;
- // Store the source address that we came from
- source_url = document_header->source;
- // Construct a new header and copy the template details across
- DocumentHeader header;
- header.MergePaths(header.template_resources, document_header->template_resources, document_header->source);
- // Merge in any templates, note a merge may cause more templates to merge
- for (size_t i = 0; i < header.template_resources.size(); i++)
- {
- Template* merge_template = TemplateCache::LoadTemplate(URL(header.template_resources[i]).GetURL());
- if (merge_template)
- header.MergeHeader(*merge_template->GetHeader());
- else
- Log::Message(Log::LT_WARNING, "Template %s not found", header.template_resources[i].c_str());
- }
- // Merge the document's header last, as it is the most overriding.
- header.MergeHeader(*document_header);
- // Set the title to the document title.
- title = document_header->title;
- // If a style-sheet (or sheets) has been specified for this element, then we load them and set the combined sheet
- // on the element; all of its children will inherit it by default.
- SharedPtr<StyleSheetContainer> new_style_sheet;
- // Combine any inline sheets.
- for (const DocumentHeader::Resource& rcss : header.rcss)
- {
- if (rcss.is_inline)
- {
- UniquePtr<StyleSheetContainer> inline_sheet = MakeUnique<StyleSheetContainer>();
- auto stream = MakeUnique<StreamMemory>((const byte*)rcss.content.c_str(), rcss.content.size());
- stream->SetSourceURL(rcss.path);
- if (inline_sheet->LoadStyleSheetContainer(stream.get(), rcss.line))
- {
- if (new_style_sheet)
- {
- SharedPtr<StyleSheetContainer> combined_sheet = new_style_sheet->CombineStyleSheetContainer(*inline_sheet);
- new_style_sheet = combined_sheet;
- }
- else
- new_style_sheet = std::move(inline_sheet);
- }
- stream.reset();
- }
- else
- {
- SharedPtr<StyleSheetContainer> sub_sheet = StyleSheetFactory::GetStyleSheetContainer(rcss.path);
- if (sub_sheet)
- {
- if (new_style_sheet)
- {
- SharedPtr<StyleSheetContainer> combined_sheet = new_style_sheet->CombineStyleSheetContainer(*sub_sheet);
- new_style_sheet = std::move(combined_sheet);
- }
- else
- new_style_sheet = sub_sheet;
- }
- else
- Log::Message(Log::LT_ERROR, "Failed to load style sheet %s.", rcss.path.c_str());
- }
- }
- // If a style sheet is available, set it on the document and release it.
- if (new_style_sheet)
- SetStyleSheetContainer(std::move(new_style_sheet));
- // Load scripts.
- for (const DocumentHeader::Resource& script : header.scripts)
- {
- if (script.is_inline)
- {
- LoadInlineScript(script.content, script.path, script.line);
- }
- else
- {
- LoadExternalScript(script.path);
- }
- }
- // Hide this document.
- SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
- const float dp_ratio = (context ? context->GetDensityIndependentPixelRatio() : 1.0f);
- const Vector2f vp_dimensions = (context ? Vector2f(context->GetDimensions()) : Vector2f(1.0f));
- // Update properties so that e.g. visibility status can be queried properly immediately.
- UpdateProperties(dp_ratio, vp_dimensions);
- }
- // Returns the document's context.
- Context* ElementDocument::GetContext()
- {
- return context;
- }
- // Sets the document's title.
- void ElementDocument::SetTitle(const String& _title)
- {
- title = _title;
- }
- const String& ElementDocument::GetTitle() const
- {
- return title;
- }
- const String& ElementDocument::GetSourceURL() const
- {
- return source_url;
- }
- // Sets the style sheet this document, and all of its children, uses.
- void ElementDocument::SetStyleSheetContainer(SharedPtr<StyleSheetContainer> _style_sheet_container)
- {
- RMLUI_ZoneScoped;
- if (style_sheet_container == _style_sheet_container)
- return;
- style_sheet_container = std::move(_style_sheet_container);
-
- GetStyle()->DirtyDefinition();
- }
- // Returns the document's style sheet.
- const StyleSheet* ElementDocument::GetStyleSheet() const
- {
- if(context && style_sheet_container)
- return style_sheet_container->GetCompiledStyleSheet(context->GetDensityIndependentPixelRatio(), Vector2f(context->GetDimensions()));
- return nullptr;
- }
- // Returns the document's style sheet container.
- const SharedPtr<StyleSheetContainer>& ElementDocument::GetStyleSheetContainer() const
- {
- return style_sheet_container;
- }
- // Reload the document's style sheet from source files.
- void ElementDocument::ReloadStyleSheet()
- {
- if (!context)
- return;
- auto stream = MakeUnique<StreamFile>();
- if (!stream->Open(source_url))
- {
- Log::Message(Log::LT_WARNING, "Failed to open file to reload style sheet in document: %s", source_url.c_str());
- return;
- }
- Factory::ClearStyleSheetCache();
- Factory::ClearTemplateCache();
- ElementPtr temp_doc = Factory::InstanceDocumentStream(nullptr, stream.get(), context->GetDocumentsBaseTag());
- if (!temp_doc) {
- Log::Message(Log::LT_WARNING, "Failed to reload style sheet, could not instance document: %s", source_url.c_str());
- return;
- }
- SetStyleSheetContainer(static_cast<ElementDocument*>(temp_doc.get())->GetStyleSheetContainer());
- }
- void ElementDocument::DirtyMediaQueries()
- {
- GetStyle()->DirtyDefinition();
- }
- // Brings the document to the front of the document stack.
- void ElementDocument::PullToFront()
- {
- if (context != nullptr)
- context->PullDocumentToFront(this);
- }
- // Sends the document to the back of the document stack.
- void ElementDocument::PushToBack()
- {
- if (context != nullptr)
- context->PushDocumentToBack(this);
- }
- void ElementDocument::Show(ModalFlag modal_flag, FocusFlag focus_flag)
- {
- switch (modal_flag)
- {
- case ModalFlag::None: modal = false; break;
- case ModalFlag::Modal: modal = true; break;
- case ModalFlag::Keep: break;
- }
- bool focus = false;
- bool autofocus = false;
- bool focus_previous = false;
- switch (focus_flag)
- {
- case FocusFlag::None:
- break;
- case FocusFlag::Document:
- focus = true;
- break;
- case FocusFlag::Keep:
- focus = true;
- focus_previous = true;
- break;
- case FocusFlag::Auto:
- focus = true;
- autofocus = true;
- break;
- }
- // Set to visible and switch focus if necessary
- SetProperty(PropertyId::Visibility, Property(Style::Visibility::Visible));
-
- // We should update the document now, otherwise the focusing methods below do not think we are visible
- // If this turns out to be slow, the more performant approach is just to compute the new visibility property
- UpdateDocument();
- if (focus)
- {
- Element* focus_element = this;
- if (autofocus)
- {
- Element* first_element = nullptr;
- Element* element = FindNextTabElement(this, true);
- while (element && element != first_element)
- {
- if (!first_element)
- first_element = element;
- if (element->HasAttribute("autofocus"))
- {
- focus_element = element;
- break;
- }
- element = FindNextTabElement(element, true);
- }
- }
- else if (focus_previous)
- {
- focus_element = GetFocusLeafNode();
- }
- // Focus the window or element
- bool focused = focus_element->Focus();
- if (focused && focus_element != this)
- focus_element->ScrollIntoView(false);
- }
- DispatchEvent(EventId::Show, Dictionary());
- }
- void ElementDocument::Hide()
- {
- SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
- // We should update the document now, so that the (un)focusing will get the correct visibility
- UpdateDocument();
- DispatchEvent(EventId::Hide, Dictionary());
-
- if (context)
- {
- context->UnfocusDocument(this);
- }
- }
- // Close this document
- void ElementDocument::Close()
- {
- if (context != nullptr)
- context->UnloadDocument(this);
- }
- ElementPtr ElementDocument::CreateElement(const String& name)
- {
- return Factory::InstanceElement(nullptr, name, name, XMLAttributes());
- }
- // Create a text element.
- ElementPtr ElementDocument::CreateTextNode(const String& text)
- {
- // Create the element.
- ElementPtr element = CreateElement("#text");
- if (!element)
- {
- Log::Message(Log::LT_ERROR, "Failed to create text element, instancer returned nullptr.");
- return nullptr;
- }
- // Cast up
- ElementText* element_text = rmlui_dynamic_cast< ElementText* >(element.get());
- if (!element_text)
- {
- Log::Message(Log::LT_ERROR, "Failed to create text element, instancer didn't return a derivative of ElementText.");
- return nullptr;
- }
-
- // Set the text
- element_text->SetText(text);
- return element;
- }
- // Is the current document modal
- bool ElementDocument::IsModal() const
- {
- return modal && IsVisible();
- }
- // Default load inline script implementation
- void ElementDocument::LoadInlineScript(const String& RMLUI_UNUSED_PARAMETER(content), const String& RMLUI_UNUSED_PARAMETER(source_path), int RMLUI_UNUSED_PARAMETER(line))
- {
- RMLUI_UNUSED(content);
- RMLUI_UNUSED(source_path);
- RMLUI_UNUSED(line);
- }
- // Default load external script implementation
- void ElementDocument::LoadExternalScript(const String& RMLUI_UNUSED_PARAMETER(source_path))
- {
- RMLUI_UNUSED(source_path);
- }
- // Updates the document, including its layout
- void ElementDocument::UpdateDocument()
- {
- const float dp_ratio = (context ? context->GetDensityIndependentPixelRatio() : 1.0f);
- const Vector2f vp_dimensions = (context ? Vector2f(context->GetDimensions()) : Vector2f(1.0f));
- Update(dp_ratio, vp_dimensions);
- UpdateLayout();
- UpdatePosition();
- }
- // Updates the layout if necessary.
- void ElementDocument::UpdateLayout()
- {
- // Note: Carefully consider when to call this function for performance reasons.
- // Ideally, only called once per update loop.
- if(layout_dirty)
- {
- RMLUI_ZoneScoped;
- RMLUI_ZoneText(source_url.c_str(), source_url.size());
- Vector2f containing_block(0, 0);
- if (GetParentNode() != nullptr)
- containing_block = GetParentNode()->GetBox().GetSize();
- LayoutEngine::FormatElement(this, containing_block);
- // Ignore dirtied layout during document formatting. Layouting must not require re-iteration.
- // In particular, scrollbars being enabled may set the dirty flag, but this case is already handled within the layout engine.
- layout_dirty = false;
- }
- }
- // Updates the position of the document based on the style properties.
- void ElementDocument::UpdatePosition()
- {
- if(position_dirty)
- {
- RMLUI_ZoneScoped;
- position_dirty = false;
- Element* root = GetParentNode();
- // We only position ourselves if we are a child of our context's root element. That is, we don't want to proceed if we are unparented or an iframe document.
- if (!root || !context || (root != context->GetRootElement()))
- return;
- Vector2f position;
- // Work out our containing block; relative offsets are calculated against it.
- Vector2f containing_block = root->GetBox().GetSize(Box::CONTENT);
- auto& computed = GetComputedValues();
- if (computed.left.type != Style::Left::Auto)
- position.x = ResolveValue(computed.left, containing_block.x);
- else if (computed.right.type != Style::Right::Auto)
- position.x = (containing_block.x - GetBox().GetSize(Box::MARGIN).x) - ResolveValue(computed.right, containing_block.x);
- else
- position.x = GetBox().GetEdge(Box::MARGIN, Box::LEFT);
- if (computed.top.type != Style::Top::Auto)
- position.y = ResolveValue(computed.top, containing_block.y);
- else if (computed.bottom.type != Style::Bottom::Auto)
- position.y = (containing_block.y - GetBox().GetSize(Box::MARGIN).y) - ResolveValue(computed.bottom, containing_block.y);
- else
- position.y = GetBox().GetEdge(Box::MARGIN, Box::TOP);
- SetOffset(position, nullptr);
- }
- }
- void ElementDocument::DirtyPosition()
- {
- position_dirty = true;
- }
- void ElementDocument::DirtyLayout()
- {
- layout_dirty = true;
- }
- bool ElementDocument::IsLayoutDirty()
- {
- return layout_dirty;
- }
- void ElementDocument::DirtyDpProperties()
- {
- GetStyle()->DirtyPropertiesWithUnitsRecursive(Property::DP);
- }
- void ElementDocument::DirtyVwAndVhProperties()
- {
- GetStyle()->DirtyPropertiesWithUnitsRecursive(Property::VW | Property::VH);
- }
- // Repositions the document if necessary.
- void ElementDocument::OnPropertyChange(const PropertyIdSet& changed_properties)
- {
- Element::OnPropertyChange(changed_properties);
- // If the document's font-size has been changed, we need to dirty all rem properties.
- if (changed_properties.Contains(PropertyId::FontSize))
- GetStyle()->DirtyPropertiesWithUnitsRecursive(Property::REM);
- if (changed_properties.Contains(PropertyId::Top) ||
- changed_properties.Contains(PropertyId::Right) ||
- changed_properties.Contains(PropertyId::Bottom) ||
- changed_properties.Contains(PropertyId::Left))
- DirtyPosition();
- }
- // Processes the 'onpropertychange' event, checking for a change in position or size.
- void ElementDocument::ProcessDefaultAction(Event& event)
- {
- Element::ProcessDefaultAction(event);
- // Process generic keyboard events for this window in bubble phase
- if (event == EventId::Keydown)
- {
- int key_identifier = event.GetParameter<int>("key_identifier", Input::KI_UNKNOWN);
- // Process TAB
- if (key_identifier == Input::KI_TAB)
- {
- if (Element* element = FindNextTabElement(event.GetTargetElement(), !event.GetParameter<bool>("shift_key", false)))
- {
- if(element->Focus())
- {
- element->ScrollIntoView(false);
- event.StopPropagation();
- }
- }
- }
- // Process ENTER being pressed on a focusable object (emulate click)
- else if (key_identifier == Input::KI_RETURN ||
- key_identifier == Input::KI_NUMPADENTER)
- {
- Element* focus_node = GetFocusLeafNode();
- if (focus_node && focus_node->GetComputedValues().tab_index == Style::TabIndex::Auto)
- {
- focus_node->Click();
- event.StopPropagation();
- }
- }
- }
- }
- void ElementDocument::OnResize()
- {
- DirtyPosition();
- }
- enum class CanFocus { Yes, No, NoAndNoChildren };
- static CanFocus CanFocusElement(Element* element)
- {
- if (!element->IsVisible())
- return CanFocus::NoAndNoChildren;
- const ComputedValues& computed = element->GetComputedValues();
- if (computed.focus == Style::Focus::None)
- return CanFocus::NoAndNoChildren;
- if (computed.tab_index == Style::TabIndex::Auto)
- return CanFocus::Yes;
- return CanFocus::No;
- }
- // Find the next element to focus, starting at the current element
- //
- // This algorithm is quite sneaky, I originally thought a depth first search would
- // work, but it appears not. What is required is to cut the tree in half along the nodes
- // from current_element up the root and then either traverse the tree in a clockwise or
- // anticlock wise direction depending if you're searching forward or backward respectively
- Element* ElementDocument::FindNextTabElement(Element* current_element, bool forward)
- {
- // If we're searching forward, check the immediate children of this node first off.
- if (forward)
- {
- for (int i = 0; i < current_element->GetNumChildren(); i++)
- if (Element* result = SearchFocusSubtree(current_element->GetChild(i), forward))
- return result;
- }
- // Now walk up the tree, testing either the bottom or top
- // of the tree, depending on whether we're going forward
- // or backward respectively.
- bool search_enabled = false;
- Element* document = current_element->GetOwnerDocument();
- Element* child = current_element;
- Element* parent = current_element->GetParentNode();
- while (child != document)
- {
- const int num_children = parent->GetNumChildren();
- for (int i = 0; i < num_children; i++)
- {
- // Calculate index into children
- const int child_index = forward ? i : (num_children - i - 1);
- Element* search_child = parent->GetChild(child_index);
- // Do a search if its enabled
- if (search_enabled)
- if(Element* result = SearchFocusSubtree(search_child, forward))
- return result;
- // Enable searching when we reach the child.
- if (search_child == child)
- search_enabled = true;
- }
- // Advance up the tree
- child = parent;
- parent = parent->GetParentNode();
- search_enabled = false;
- }
- // We could not find anything to focus along this direction.
- // If we can focus the document, then focus that now.
- if (current_element != document && CanFocusElement(document) == CanFocus::Yes)
- return document;
- // Otherwise, search the entire document tree. This way we will wrap around.
- const int num_children = document->GetNumChildren();
- for (int i = 0; i < num_children; i++)
- {
- const int child_index = forward ? i : (num_children - i - 1);
- if (Element* result = SearchFocusSubtree(document->GetChild(child_index), forward))
- return result;
- }
- return nullptr;
- }
- Element* ElementDocument::SearchFocusSubtree(Element* element, bool forward)
- {
- CanFocus can_focus = CanFocusElement(element);
- if (can_focus == CanFocus::Yes)
- return element;
- else if (can_focus == CanFocus::NoAndNoChildren)
- return nullptr;
- // Check all children
- for (int i = 0; i < element->GetNumChildren(); i++)
- {
- int child_index = i;
- if (!forward)
- child_index = element->GetNumChildren() - i - 1;
- if (Element * result = SearchFocusSubtree(element->GetChild(child_index), forward))
- return result;
- }
- return nullptr;
- }
- } // namespace Rml
|