| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- /*
- * 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/Controls/ElementFormControlInput.h"
- #include "../../Include/RmlUi/Core/Event.h"
- #include "InputTypeButton.h"
- #include "InputTypeCheckbox.h"
- #include "InputTypeRadio.h"
- #include "InputTypeRange.h"
- #include "InputTypeSubmit.h"
- #include "InputTypeText.h"
- namespace Rml {
- namespace Controls {
- // Constructs a new ElementFormControlInput.
- ElementFormControlInput::ElementFormControlInput(const Rml::Core::String& tag) : ElementFormControl(tag)
- {
- // OnAttributeChange will be called right after this, possible with a non-default type. Thus,
- // creating the default InputTypeText here may result in it being destroyed in just a few moments.
- // Instead, we create the InputTypeText in OnAttributeChange in the case where the type attribute has not been set.
- type = nullptr;
- }
- ElementFormControlInput::~ElementFormControlInput()
- {
- delete type;
- }
- // Returns a string representation of the current value of the form control.
- Rml::Core::String ElementFormControlInput::GetValue() const
- {
- RMLUI_ASSERT(type);
- return type->GetValue();
- }
- // Sets the current value of the form control.
- void ElementFormControlInput::SetValue(const Rml::Core::String& value)
- {
- SetAttribute("value", value);
- }
- // Returns if this value should be submitted with the form.
- bool ElementFormControlInput::IsSubmitted()
- {
- RMLUI_ASSERT(type);
- return type->IsSubmitted();
- }
- // Updates the element's underlying type.
- void ElementFormControlInput::OnUpdate()
- {
- RMLUI_ASSERT(type);
- type->OnUpdate();
- }
- // Renders the element's underlying type.
- void ElementFormControlInput::OnRender()
- {
- RMLUI_ASSERT(type);
- type->OnRender();
- }
- void ElementFormControlInput::OnResize()
- {
- RMLUI_ASSERT(type);
- type->OnResize();
- }
- // Checks for necessary functional changes in the control as a result of changed attributes.
- void ElementFormControlInput::OnAttributeChange(const Core::ElementAttributes& changed_attributes)
- {
- ElementFormControl::OnAttributeChange(changed_attributes);
- Rml::Core::String new_type_name;
- auto it_type = changed_attributes.find("type");
- if (it_type != changed_attributes.end())
- {
- new_type_name = it_type->second.Get<Core::String>("text");
- }
- else if (!type)
- {
- // Ref. comment in constructor.
- new_type_name = "text";
- }
- if (!new_type_name.empty() && new_type_name != type_name)
- {
- InputType* new_type = nullptr;
- if (new_type_name == "password")
- new_type = new InputTypeText(this, Rml::Controls::InputTypeText::OBSCURED);
- else if (new_type_name == "radio")
- new_type = new InputTypeRadio(this);
- else if (new_type_name == "checkbox")
- new_type = new InputTypeCheckbox(this);
- else if (new_type_name == "range")
- new_type = new InputTypeRange(this);
- else if (new_type_name == "submit")
- new_type = new InputTypeSubmit(this);
- else if (new_type_name == "button")
- new_type = new InputTypeButton(this);
- else if (new_type_name == "text")
- new_type = new InputTypeText(this);
- if (new_type)
- {
- delete type;
- type = new_type;
- if(!type_name.empty())
- SetClass(type_name, false);
- SetClass(new_type_name, true);
- type_name = new_type_name;
- DirtyLayout();
- }
- }
- if (!type->OnAttributeChange(changed_attributes))
- DirtyLayout();
- }
- // Called when properties on the element are changed.
- void ElementFormControlInput::OnPropertyChange(const Core::PropertyNameList& changed_properties)
- {
- ElementFormControl::OnPropertyChange(changed_properties);
- if (type != nullptr)
- type->OnPropertyChange(changed_properties);
- }
- // If we are the added element, this will pass the call onto our type handler.
- void ElementFormControlInput::OnChildAdd(Rml::Core::Element* child)
- {
- if (child == this && type != nullptr)
- type->OnChildAdd();
- }
- // If we are the removed element, this will pass the call onto our type handler.
- void ElementFormControlInput::OnChildRemove(Rml::Core::Element* child)
- {
- if (child == this && type != nullptr)
- type->OnChildRemove();
- }
- // Handles the "click" event to toggle the control's checked status.
- void ElementFormControlInput::ProcessDefaultAction(Core::Event& event)
- {
- ElementFormControl::ProcessDefaultAction(event);
- if(type != nullptr)
- type->ProcessDefaultAction(event);
- }
- bool ElementFormControlInput::GetIntrinsicDimensions(Rml::Core::Vector2f& dimensions)
- {
- if (!type)
- return false;
- return type->GetIntrinsicDimensions(dimensions);
- }
- }
- }
|