Crafter.CppDOM/interfaces/Crafter.CppDOM-HtmlElement.cppm

395 lines
17 KiB
Text
Raw Normal View History

2025-02-12 23:12:24 +01:00
/*
2025-02-12 23:13:02 +01:00
Crafter.CppDOM
Copyright (C) 2025 Catcrafts
2025-02-12 23:12:24 +01:00
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
2025-02-12 23:06:56 +01:00
export module Crafter.CppDOM:HtmlElement;
2025-11-09 20:11:22 +01:00
import std;
2025-02-12 23:06:56 +01:00
import :Bindings;
2025-11-10 20:02:11 +01:00
import :EventTypes;
2025-02-12 23:06:56 +01:00
2025-11-09 22:43:52 +01:00
namespace Crafter {
2025-02-12 23:06:56 +01:00
export class HtmlElement {
public:
void* const ptr;
2025-11-10 20:02:11 +01:00
std::vector<std::int32_t> handlers;
HtmlElement(const std::string_view id): ptr(CppDOMBindings::GetElementById(id)) {
2025-02-12 23:06:56 +01:00
}
2025-11-10 20:02:11 +01:00
HtmlElement(const std::string_view id, const std::string_view html): ptr(CppDOMBindings::GetElementById(id)) {
CppDOMBindings::SetInnerHTML(ptr, html);
2025-11-09 22:43:52 +01:00
}
2025-11-10 20:02:11 +01:00
void SetInnerHTML(const std::string_view html) {
CppDOMBindings::SetInnerHTML(ptr, html);
2025-02-12 23:06:56 +01:00
}
2025-11-09 22:43:52 +01:00
2025-11-10 20:02:11 +01:00
// Event handling methods with event data
// Mouse Events
std::int32_t AddClickListener(std::function<void(void)> callback) {
std::int32_t id = CppDOMBindings::clickHandlerMaxId++;
handlers.push_back(id);
CppDOMBindings::clickHandlers->insert({id, callback});
CppDOMBindings::AddClickListener(ptr, id);
return id;
}
void RemoveClickListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
CppDOMBindings::clickHandlers->erase(id);
CppDOMBindings::RemoveClickListener(ptr, id);
}
std::int32_t AddMouseOverListener(std::function<void(void)> callback) {
std::int32_t id = CppDOMBindings::mouseOverHandlerMaxId++;
handlers.push_back(id);
CppDOMBindings::mouseOverHandlers->insert({id, callback});
CppDOMBindings::AddMouseOverListener(ptr, id);
return id;
}
void RemoveMouseOverListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
CppDOMBindings::mouseOverHandlers->erase(id);
CppDOMBindings::RemoveMouseOverListener(ptr, id);
}
std::int32_t AddMouseOutListener(std::function<void(void)> callback) {
std::int32_t id = CppDOMBindings::mouseOutHandlerMaxId++;
handlers.push_back(id);
CppDOMBindings::mouseOutHandlers->insert({id, callback});
CppDOMBindings::AddMouseOutListener(ptr, id);
return id;
}
void RemoveMouseOutListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
CppDOMBindings::mouseOutHandlers->erase(id);
CppDOMBindings::RemoveMouseOutListener(ptr, id);
}
std::int32_t AddMouseMoveListener(std::function<void(void)> callback) {
std::int32_t id = CppDOMBindings::mouseMoveHandlerMaxId++;
handlers.push_back(id);
CppDOMBindings::mouseMoveHandlers->insert({id, callback});
CppDOMBindings::AddMouseMoveListener(ptr, id);
return id;
}
void RemoveMouseMoveListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
CppDOMBindings::mouseMoveHandlers->erase(id);
CppDOMBindings::RemoveMouseMoveListener(ptr, id);
}
std::int32_t AddMouseDownListener(std::function<void(void)> callback) {
std::int32_t id = CppDOMBindings::mouseDownHandlerMaxId++;
2025-11-09 22:43:52 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::mouseDownHandlers->insert({id, callback});
CppDOMBindings::AddMouseDownListener(ptr, id);
2025-11-09 22:43:52 +01:00
return id;
2025-02-12 23:06:56 +01:00
}
2025-11-09 22:43:52 +01:00
2025-11-10 20:02:11 +01:00
void RemoveMouseDownListener(std::int32_t id) {
2025-11-09 22:43:52 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::mouseDownHandlers->erase(id);
CppDOMBindings::RemoveMouseDownListener(ptr, id);
2025-02-12 23:06:56 +01:00
}
2025-11-09 22:43:52 +01:00
2025-11-10 20:02:11 +01:00
std::int32_t AddMouseUpListener(std::function<void(void)> callback) {
std::int32_t id = CppDOMBindings::mouseUpHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::mouseUpHandlers->insert({id, callback});
CppDOMBindings::AddMouseUpListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveMouseUpListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::mouseUpHandlers->erase(id);
CppDOMBindings::RemoveMouseUpListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
// Focus Events
std::int32_t AddFocusListener(std::function<void(void)> callback) {
std::int32_t id = CppDOMBindings::focusHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::focusHandlers->insert({id, callback});
CppDOMBindings::AddFocusListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveFocusListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::focusHandlers->erase(id);
CppDOMBindings::RemoveFocusListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
std::int32_t AddBlurListener(std::function<void(void)> callback) {
std::int32_t id = CppDOMBindings::blurHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::blurHandlers->insert({id, callback});
CppDOMBindings::AddBlurListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveBlurListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::blurHandlers->erase(id);
CppDOMBindings::RemoveBlurListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
// Keyboard Events
std::int32_t AddKeyDownListener(std::function<void(KeyboardEvent)> callback) {
std::int32_t id = CppDOMBindings::keyDownHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::keyDownHandlers->insert({id, callback});
CppDOMBindings::AddKeyDownListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveKeyDownListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::keyDownHandlers->erase(id);
CppDOMBindings::RemoveKeyDownListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
std::int32_t AddKeyUpListener(std::function<void(KeyboardEvent)> callback) {
std::int32_t id = CppDOMBindings::keyUpHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::keyUpHandlers->insert({id, callback});
CppDOMBindings::AddKeyUpListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveKeyUpListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::keyUpHandlers->erase(id);
CppDOMBindings::RemoveKeyUpListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
std::int32_t AddKeyPressListener(std::function<void(KeyboardEvent)> callback) {
std::int32_t id = CppDOMBindings::keyPressHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::keyPressHandlers->insert({id, callback});
CppDOMBindings::AddKeyPressListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveKeyPressListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::keyPressHandlers->erase(id);
CppDOMBindings::RemoveKeyPressListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
// Form Events
std::int32_t AddChangeListener(std::function<void(ChangeEvent)> callback) {
std::int32_t id = CppDOMBindings::changeHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::changeHandlers->insert({id, callback});
CppDOMBindings::AddChangeListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveChangeListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::changeHandlers->erase(id);
CppDOMBindings::RemoveChangeListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
std::int32_t AddSubmitListener(std::function<void(void)> callback) {
std::int32_t id = CppDOMBindings::submitHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::submitHandlers->insert({id, callback});
CppDOMBindings::AddSubmitListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveSubmitListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::submitHandlers->erase(id);
CppDOMBindings::RemoveSubmitListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
std::int32_t AddInputListener(std::function<void(InputEvent)> callback) {
std::int32_t id = CppDOMBindings::inputHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::inputHandlers->insert({id, callback});
CppDOMBindings::AddInputListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveInputListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::inputHandlers->erase(id);
CppDOMBindings::RemoveInputListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
std::int32_t AddResizeListener(std::function<void(ResizeEvent)> callback) {
std::int32_t id = CppDOMBindings::resizeHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::resizeHandlers->insert({id, callback});
CppDOMBindings::AddResizeListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveResizeListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::resizeHandlers->erase(id);
CppDOMBindings::RemoveResizeListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
std::int32_t AddScrollListener(std::function<void(ScrollEvent)> callback) {
std::int32_t id = CppDOMBindings::scrollHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::scrollHandlers->insert({id, callback});
CppDOMBindings::AddScrollListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveScrollListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::scrollHandlers->erase(id);
CppDOMBindings::RemoveScrollListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
// Context Menu Events
std::int32_t AddContextMenuListener(std::function<void(MouseEvent)> callback) {
std::int32_t id = CppDOMBindings::contextMenuHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::contextMenuHandlers->insert({id, callback});
CppDOMBindings::AddContextMenuListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveContextMenuListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::contextMenuHandlers->erase(id);
CppDOMBindings::RemoveContextMenuListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
// Drag and Drop Events
std::int32_t AddDragStartListener(std::function<void(MouseEvent)> callback) {
std::int32_t id = CppDOMBindings::dragStartHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::dragStartHandlers->insert({id, callback});
CppDOMBindings::AddDragStartListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveDragStartListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::dragStartHandlers->erase(id);
CppDOMBindings::RemoveDragStartListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
std::int32_t AddDragEndListener(std::function<void(MouseEvent)> callback) {
std::int32_t id = CppDOMBindings::dragEndHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::dragEndHandlers->insert({id, callback});
CppDOMBindings::AddDragEndListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveDragEndListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::dragEndHandlers->erase(id);
CppDOMBindings::RemoveDragEndListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
std::int32_t AddDropListener(std::function<void(MouseEvent)> callback) {
std::int32_t id = CppDOMBindings::dropHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::dropHandlers->insert({id, callback});
CppDOMBindings::AddDropListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveDropListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::dropHandlers->erase(id);
CppDOMBindings::RemoveDropListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
// Additional Drag Events
std::int32_t AddDragOverListener(std::function<void(MouseEvent)> callback) {
std::int32_t id = CppDOMBindings::dragOverHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::dragOverHandlers->insert({id, callback});
CppDOMBindings::AddDragOverListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveDragOverListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::dragOverHandlers->erase(id);
CppDOMBindings::RemoveDragOverListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
std::int32_t AddDragEnterListener(std::function<void(MouseEvent)> callback) {
std::int32_t id = CppDOMBindings::dragEnterHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::dragEnterHandlers->insert({id, callback});
CppDOMBindings::AddDragEnterListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveDragEnterListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::dragEnterHandlers->erase(id);
CppDOMBindings::RemoveDragEnterListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
std::int32_t AddDragLeaveListener(std::function<void(MouseEvent)> callback) {
std::int32_t id = CppDOMBindings::dragLeaveHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::dragLeaveHandlers->insert({id, callback});
CppDOMBindings::AddDragLeaveListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveDragLeaveListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::dragLeaveHandlers->erase(id);
CppDOMBindings::RemoveDragLeaveListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-10 20:02:11 +01:00
// Wheel Event
std::int32_t AddWheelListener(std::function<void(WheelEvent)> callback) {
std::int32_t id = CppDOMBindings::wheelHandlerMaxId++;
2025-11-09 22:56:29 +01:00
handlers.push_back(id);
2025-11-10 20:02:11 +01:00
CppDOMBindings::wheelHandlers->insert({id, callback});
CppDOMBindings::AddWheelListener(ptr, id);
2025-11-09 22:56:29 +01:00
return id;
}
2025-11-10 20:02:11 +01:00
void RemoveWheelListener(std::int32_t id) {
2025-11-09 22:56:29 +01:00
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
2025-11-10 20:02:11 +01:00
CppDOMBindings::wheelHandlers->erase(id);
CppDOMBindings::RemoveWheelListener(ptr, id);
2025-11-09 22:56:29 +01:00
}
2025-11-09 22:43:52 +01:00
~HtmlElement(){
2025-11-10 20:02:11 +01:00
for(std::int32_t handler : handlers) {
CppDOMBindings::clickHandlers->erase(handler);
CppDOMBindings::RemoveClickListener(ptr, handler);
2025-11-09 22:43:52 +01:00
}
2025-11-10 20:02:11 +01:00
CppDOMBindings::FreeJs(ptr);
2025-02-12 23:06:56 +01:00
}
};
}