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-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-09 22:43:52 +01:00
|
|
|
std::vector<int> handlers;
|
|
|
|
|
HtmlElement(const std::string_view id): ptr(CppDOM::Bindings::GetElementById(id)) {
|
2025-02-12 23:06:56 +01:00
|
|
|
|
|
|
|
|
}
|
2025-11-09 22:43:52 +01:00
|
|
|
HtmlElement(const std::string_view id, const std::string_view html): ptr(CppDOM::Bindings::GetElementById(id)) {
|
|
|
|
|
CppDOM::Bindings::SetInnerHTML(ptr, html);
|
|
|
|
|
}
|
|
|
|
|
void SetInnerHTML(const std::string_view& html) {
|
|
|
|
|
CppDOM::Bindings::SetInnerHTML(ptr, html);
|
2025-02-12 23:06:56 +01:00
|
|
|
}
|
2025-11-09 22:43:52 +01:00
|
|
|
|
2025-11-09 22:56:29 +01:00
|
|
|
// Event handling methods
|
2025-11-09 22:43:52 +01:00
|
|
|
int AddClickListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::clickHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::clickHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddClickListener(ptr, id);
|
|
|
|
|
return id;
|
2025-02-12 23:06:56 +01:00
|
|
|
}
|
2025-11-09 22:43:52 +01:00
|
|
|
|
|
|
|
|
void RemoveClickListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::clickHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveClickListener(ptr, id);
|
2025-02-12 23:06:56 +01:00
|
|
|
}
|
2025-11-09 22:43:52 +01:00
|
|
|
|
2025-11-09 22:56:29 +01:00
|
|
|
int AddMouseOverListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::mouseOverHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::mouseOverHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddMouseOverListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveMouseOverListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::mouseOverHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveMouseOverListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddMouseOutListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::mouseOutHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::mouseOutHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddMouseOutListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveMouseOutListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::mouseOutHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveMouseOutListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddMouseMoveListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::mouseMoveHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::mouseMoveHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddMouseMoveListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveMouseMoveListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::mouseMoveHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveMouseMoveListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddFocusListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::focusHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::focusHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddFocusListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveFocusListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::focusHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveFocusListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddBlurListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::blurHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::blurHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddBlurListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveBlurListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::blurHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveBlurListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddKeyDownListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::keyDownHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::keyDownHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddKeyDownListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveKeyDownListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::keyDownHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveKeyDownListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddKeyUpListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::keyUpHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::keyUpHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddKeyUpListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveKeyUpListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::keyUpHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveKeyUpListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddKeyPressListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::keyPressHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::keyPressHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddKeyPressListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveKeyPressListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::keyPressHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveKeyPressListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddChangeListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::changeHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::changeHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddChangeListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveChangeListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::changeHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveChangeListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddSubmitListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::submitHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::submitHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddSubmitListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveSubmitListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::submitHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveSubmitListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddInputListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::inputHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::inputHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddInputListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveInputListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::inputHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveInputListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddLoadListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::loadHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::loadHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddLoadListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveLoadListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::loadHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveLoadListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddErrorListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::errorHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::errorHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddErrorListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveErrorListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::errorHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveErrorListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddResizeListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::resizeHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::resizeHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddResizeListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveResizeListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::resizeHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveResizeListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddScrollListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::scrollHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::scrollHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddScrollListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveScrollListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::scrollHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveScrollListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddContextMenuListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::contextMenuHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::contextMenuHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddContextMenuListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveContextMenuListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::contextMenuHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveContextMenuListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddDragStartListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::dragStartHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::dragStartHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddDragStartListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveDragStartListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::dragStartHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveDragStartListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddDragEndListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::dragEndHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::dragEndHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddDragEndListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveDragEndListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::dragEndHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveDragEndListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AddDropListener(std::function<void(void)> callback) {
|
|
|
|
|
int id = CppDOM::Bindings::dropHandlerMaxId++;
|
|
|
|
|
handlers.push_back(id);
|
|
|
|
|
CppDOM::Bindings::dropHandlers->insert({id, callback});
|
|
|
|
|
CppDOM::Bindings::AddDropListener(ptr, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveDropListener(int id) {
|
|
|
|
|
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end());
|
|
|
|
|
CppDOM::Bindings::dropHandlers->erase(id);
|
|
|
|
|
CppDOM::Bindings::RemoveDropListener(ptr, id);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-09 22:43:52 +01:00
|
|
|
~HtmlElement(){
|
|
|
|
|
for(int handler : handlers) {
|
2025-11-09 22:56:29 +01:00
|
|
|
// Clean up all handlers based on type - this is a simplified approach
|
|
|
|
|
// In practice, you'd want to track handler types to clean them properly
|
2025-11-09 22:43:52 +01:00
|
|
|
CppDOM::Bindings::clickHandlers->erase(handler);
|
|
|
|
|
CppDOM::Bindings::RemoveClickListener(ptr, handler);
|
|
|
|
|
}
|
|
|
|
|
CppDOM::Bindings::FreeJs(ptr);
|
2025-02-12 23:06:56 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|