Added File/Text Drag and Drop from the OS to the widgets
This commit is contained in:
parent
d4b4df1e51
commit
c98dd97670
9 changed files with 52 additions and 25 deletions
1
.clangd
1
.clangd
|
|
@ -1,2 +1,3 @@
|
|||
CompileFlags:
|
||||
CompilationDatabase: bin
|
||||
Add: ["--target=x86_64-w64-mingw32"]
|
||||
|
|
@ -1 +1 @@
|
|||
2050
|
||||
2052
|
||||
|
|
|
|||
|
|
@ -306,12 +306,13 @@ namespace ogfx
|
|||
if (!widget->isMouseInside()) return;
|
||||
if (Widget::s_dragAndDropData != nullptr)
|
||||
{
|
||||
widget->__onDragAndDrop(event);
|
||||
auto& const_cast_event = const_cast<Event&>(event);
|
||||
const_cast_event.drop.textOrFilePath = "";
|
||||
const_cast_event.drop.dropType = DropEventData::eDropType::InApp;
|
||||
const_cast_event.drop.userObject = Widget::s_dragAndDropData;
|
||||
widget->__onDragAndDrop(const_cast_event);
|
||||
Widget::clearDragAndDropData();
|
||||
}
|
||||
|
||||
auto& const_cast_event = const_cast<Event&>(event);
|
||||
// if (event.__original_signal_id == ostd::BuiltinSignals::TextDragAndDropped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -137,6 +137,8 @@ namespace ogfx
|
|||
connectSignal(ostd::BuiltinSignals::WindowResized);
|
||||
connectSignal(ostd::BuiltinSignals::WindowFocused);
|
||||
connectSignal(ostd::BuiltinSignals::WindowLostFocus);
|
||||
connectSignal(ostd::BuiltinSignals::FileDragAndDropped);
|
||||
connectSignal(ostd::BuiltinSignals::TextDragAndDropped);
|
||||
|
||||
__on_window_init(width, height, title);
|
||||
setSize(m_windowWidth, m_windowHeight);
|
||||
|
|
@ -528,21 +530,21 @@ namespace ogfx
|
|||
}
|
||||
else if (signal.ID == ostd::BuiltinSignals::FileDragAndDropped)
|
||||
{
|
||||
auto& ud = (ogfx::DropEventData&)signal.userData;
|
||||
evt.drop.dropType = ud.dropType;
|
||||
evt.drop.userObject = ud.userObject;
|
||||
evt.drop.textOrFilePath = ud.textOrFilePath;
|
||||
evt.__original_signal_id = ostd::BuiltinSignals::FileDragAndDropped;
|
||||
m_rootWidget.__onDragAndDrop(evt);
|
||||
// auto& ud = (ogfx::DropEventData&)signal.userData;
|
||||
// evt.drop.dropType = ud.dropType;
|
||||
// evt.drop.userObject = ud.userObject;
|
||||
// evt.drop.textOrFilePath = ud.textOrFilePath;
|
||||
// evt.__original_signal_id = ostd::BuiltinSignals::FileDragAndDropped;
|
||||
// m_rootWidget.__onMouseReleased(evt);
|
||||
}
|
||||
else if (signal.ID == ostd::BuiltinSignals::TextDragAndDropped)
|
||||
{
|
||||
auto& ud = (ogfx::DropEventData&)signal.userData;
|
||||
evt.drop.dropType = ud.dropType;
|
||||
evt.drop.userObject = ud.userObject;
|
||||
evt.drop.textOrFilePath = ud.textOrFilePath;
|
||||
evt.__original_signal_id = ostd::BuiltinSignals::TextDragAndDropped;
|
||||
m_rootWidget.__onDragAndDrop(evt);
|
||||
// auto& ud = (ogfx::DropEventData&)signal.userData;
|
||||
// evt.drop.dropType = ud.dropType;
|
||||
// evt.drop.userObject = ud.userObject;
|
||||
// evt.drop.textOrFilePath = ud.textOrFilePath;
|
||||
// evt.__original_signal_id = ostd::BuiltinSignals::TextDragAndDropped;
|
||||
// m_rootWidget.__onMouseReleased(evt);
|
||||
}
|
||||
else if (signal.ID == ostd::BuiltinSignals::WindowResized)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -28,6 +28,10 @@ namespace ogfx
|
|||
{
|
||||
namespace gui
|
||||
{
|
||||
|
||||
ostd::BaseObject* Widget::s_dragAndDropData { nullptr };
|
||||
bool Widget::s_hasDragAndDropData { false };
|
||||
|
||||
Widget::Widget(const ostd::Rectangle& bounds, WindowCore& window) : Rectangle(bounds), m_widgets(window, *this)
|
||||
{
|
||||
m_window = &window;
|
||||
|
|
@ -128,6 +132,7 @@ namespace ogfx
|
|||
{
|
||||
if (name == qualifier)
|
||||
{
|
||||
if (state == value) return;
|
||||
state = value;
|
||||
reloadTheme();
|
||||
return;
|
||||
|
|
@ -207,8 +212,8 @@ namespace ogfx
|
|||
|
||||
void Widget::__onDragAndDrop(const Event& event)
|
||||
{
|
||||
// if (hasChildren())
|
||||
// m_widgets.onMouseReleased(event);
|
||||
if (hasChildren())
|
||||
m_widgets.onMouseReleased(event);
|
||||
if (!event.isHandled())
|
||||
{
|
||||
if (callback_onDragAndDrop)
|
||||
|
|
|
|||
|
|
@ -143,7 +143,7 @@ namespace ogfx
|
|||
return theme.get<T>(key, fallback, getThemeIDList(), getThemeQualifierList());
|
||||
}
|
||||
|
||||
inline static void setDragAndDropData(ostd::BaseObject& data) { s_dragAndDropData = &data; }
|
||||
inline static void setDragAndDropData(ostd::BaseObject& data) { s_dragAndDropData = &data; s_hasDragAndDropData = true; }
|
||||
inline static void clearDragAndDropData(void) { s_dragAndDropData = nullptr; }
|
||||
inline static ostd::BaseObject* getDragAndDropData(void) { return s_dragAndDropData; }
|
||||
|
||||
|
|
@ -208,7 +208,9 @@ namespace ogfx
|
|||
|
||||
ostd::Rectangle m_padding { 0, 0, 0, 0 };
|
||||
|
||||
inline static ostd::BaseObject* s_dragAndDropData { nullptr };
|
||||
public:
|
||||
static ostd::BaseObject* s_dragAndDropData;
|
||||
static bool s_hasDragAndDropData;
|
||||
|
||||
friend class WidgetManager;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -49,6 +49,10 @@ namespace ostd
|
|||
void BaseObject::__handle_signal(Signal& signal)
|
||||
{
|
||||
if (m_signalsEnabled)
|
||||
{
|
||||
handleSignal(signal);
|
||||
if (callback_signal)
|
||||
callback_signal(signal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
#include <cstdint>
|
||||
#include <ostd/string/String.hpp>
|
||||
#include <functional>
|
||||
|
||||
namespace ostd
|
||||
{
|
||||
|
|
@ -29,10 +30,12 @@ namespace ostd
|
|||
struct Signal;
|
||||
class BaseObject : public __i_stringeable
|
||||
{
|
||||
public: using SignalCallback = std::function<void(Signal&)>;
|
||||
public:
|
||||
BaseObject(const BaseObject& copy);
|
||||
inline virtual ~BaseObject(void) = default;
|
||||
virtual BaseObject& operator=(const BaseObject& copy);
|
||||
inline void setSignalCallback(SignalCallback callback) { callback_signal = callback; }
|
||||
|
||||
virtual inline uint64_t getID(void) const { return m_uid; }
|
||||
virtual inline void setID(uint64_t id) { m_uid = id; }
|
||||
|
|
@ -74,6 +77,7 @@ namespace ostd
|
|||
bool m_valid;
|
||||
String m_typeName;
|
||||
bool m_signalsEnabled { true };
|
||||
SignalCallback callback_signal { nullptr };
|
||||
|
||||
inline static uint64_t s_next_oid { 1024 };
|
||||
static BaseObject s_invalid_obj;
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
along with OmniaFramework. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "gui/Events.hpp"
|
||||
#include <ogfx/utils/Keycodes.hpp>
|
||||
#include <ogfx/ogfx.hpp>
|
||||
|
||||
|
|
@ -62,14 +63,21 @@ class Window : public ogfx::gui::Window
|
|||
|
||||
m_label2.setPosition(0, 0);
|
||||
m_label2.setText("Ciccia Bella!");
|
||||
m_label2.connectSignal(ostd::BuiltinSignals::FileDragAndDropped);
|
||||
m_label2.addThemeID("testLabel");
|
||||
m_label2.addThemeID("testLabel2");
|
||||
m_label2.setSignalCallback([&](ostd::Signal& signal){
|
||||
if (signal.ID == ostd::BuiltinSignals::FileDragAndDropped)
|
||||
{
|
||||
auto& data = (ogfx::DropEventData&)signal.userData;
|
||||
std::cout << data.textOrFilePath << "\n";
|
||||
}
|
||||
});
|
||||
m_label2.enableDragAndDrop();
|
||||
m_label2.setDragAndDropCallback([&](const ogfx::gui::Event& event) -> void {
|
||||
std::cout << "DROP\n";
|
||||
if (auto data = ogfx::gui::Widget::getDragAndDropData())
|
||||
if (event.drop.userObject->getTypeName() == "ogfx::gui::widgets::Label")
|
||||
{
|
||||
std::cout << static_cast<ogfx::gui::widgets::Label&>(*data).getText() << "!\n";
|
||||
std::cout << static_cast<ogfx::gui::widgets::Label&>(*event.drop.userObject).getText() << "!\n";
|
||||
}
|
||||
});
|
||||
m_panel1.addChild(m_label2);
|
||||
|
|
|
|||
Loading…
Reference in a new issue