diff --git a/include/event.hpp b/include/event.hpp index 8dd2ff8fd..e9888f69c 100644 --- a/include/event.hpp +++ b/include/event.hpp @@ -7,25 +7,26 @@ namespace hex { enum class Events { DataChanged, - PatternChanged + PatternChanged, + FileDropped }; struct EventHandler { void *sender; Events eventType; - std::function callback; + std::function callback; }; class EventManager { public: - void post(Events eventType, void *userData) { + void post(Events eventType, const void *userData) { for (auto &handler : this->m_eventHandlers) if (eventType == handler.eventType) handler.callback(userData); } - void subscribe(Events eventType, void *sender, std::function callback) { + void subscribe(Events eventType, void *sender, std::function callback) { for (auto &handler : this->m_eventHandlers) if (eventType == handler.eventType && sender == handler.sender) return; diff --git a/include/views/view.hpp b/include/views/view.hpp index 7037782c5..789e2dcee 100644 --- a/include/views/view.hpp +++ b/include/views/view.hpp @@ -25,8 +25,12 @@ namespace hex { return View::s_deferedCalls; } + static void postEvent(Events eventType, const void *userData = nullptr) { + View::s_eventManager.post(eventType, userData); + } + protected: - void subscribeEvent(Events eventType, std::function callback) { + void subscribeEvent(Events eventType, std::function callback) { View::s_eventManager.subscribe(eventType, this, callback); } @@ -34,10 +38,6 @@ namespace hex { View::s_eventManager.unsubscribe(eventType, this); } - void postEvent(Events eventType, void *userData = nullptr) { - View::s_eventManager.post(eventType, userData); - } - void doLater(std::function &&function) { View::s_deferedCalls.push_back(function); } diff --git a/include/views/view_hexeditor.hpp b/include/views/view_hexeditor.hpp index aaa07b7fd..14f5f91e8 100644 --- a/include/views/view_hexeditor.hpp +++ b/include/views/view_hexeditor.hpp @@ -51,6 +51,7 @@ namespace hex { void drawSearchPopup(); void drawGotoPopup(); + void openFile(std::string path); void copyBytes(); void copyString(); diff --git a/source/views/view_hashes.cpp b/source/views/view_hashes.cpp index e0b7b64c8..a7134d595 100644 --- a/source/views/view_hashes.cpp +++ b/source/views/view_hashes.cpp @@ -9,7 +9,7 @@ namespace hex { ViewHashes::ViewHashes(prv::Provider* &dataProvider) : View(), m_dataProvider(dataProvider) { - View::subscribeEvent(Events::DataChanged, [this](void*){ + View::subscribeEvent(Events::DataChanged, [this](const void*){ this->m_shouldInvalidate = true; }); } diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index e98b6f4bb..09f9972df 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -49,6 +49,13 @@ namespace hex { _this->m_memoryEditor.HighlightColor = 0x50C08080; return false; }; + + View::subscribeEvent(Events::FileDropped, [this](const void *userData) { + auto filePath = static_cast(userData); + + if (filePath != nullptr) + this->openFile(filePath); + }); } ViewHexEditor::~ViewHexEditor() { @@ -73,16 +80,20 @@ namespace hex { this->m_fileBrowser.Display(); if (this->m_fileBrowser.HasSelected()) { - if (this->m_dataProvider != nullptr) - delete this->m_dataProvider; - - this->m_dataProvider = new prv::FileProvider(this->m_fileBrowser.GetSelected().string()); - View::postEvent(Events::DataChanged); + this->openFile(this->m_fileBrowser.GetSelected().string()); this->m_fileBrowser.ClearSelected(); } } + void ViewHexEditor::openFile(std::string path) { + if (this->m_dataProvider != nullptr) + delete this->m_dataProvider; + + this->m_dataProvider = new prv::FileProvider(path); + View::postEvent(Events::DataChanged); + } + void ViewHexEditor::copyBytes() { size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); diff --git a/source/views/view_information.cpp b/source/views/view_information.cpp index e1bf16c81..3dc596115 100644 --- a/source/views/view_information.cpp +++ b/source/views/view_information.cpp @@ -22,7 +22,7 @@ namespace hex { ViewInformation::ViewInformation(prv::Provider* &dataProvider) : View(), m_dataProvider(dataProvider) { - View::subscribeEvent(Events::DataChanged, [this](void*) { + View::subscribeEvent(Events::DataChanged, [this](const void*) { this->m_shouldInvalidate = true; }); } diff --git a/source/views/view_strings.cpp b/source/views/view_strings.cpp index 0189323a0..e5169df08 100644 --- a/source/views/view_strings.cpp +++ b/source/views/view_strings.cpp @@ -7,7 +7,7 @@ namespace hex { ViewStrings::ViewStrings(prv::Provider* &dataProvider) : View(), m_dataProvider(dataProvider) { - View::subscribeEvent(Events::DataChanged, [this](void*){ + View::subscribeEvent(Events::DataChanged, [this](const void*){ this->m_shouldInvalidate = true; }); diff --git a/source/window.cpp b/source/window.cpp index 127f405d1..fb52a9803 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -137,6 +137,7 @@ namespace hex { void Window::frameEnd() { ImGui::Render(); + int display_w, display_h; glfwGetFramebufferSize(this->m_window, &display_w, &display_h); glViewport(0, 0, display_w, display_h); @@ -176,6 +177,13 @@ namespace hex { Window::s_currShortcut = { key, mods }; }); + glfwSetDropCallback(this->m_window, [](GLFWwindow *window, int count, const char **paths) { + if (count != 1) + return; + + View::postEvent(Events::FileDropped, paths[0]); + }); + glfwSetWindowSizeLimits(this->m_window, 720, 480, GLFW_DONT_CARE, GLFW_DONT_CARE); if (gladLoadGL() == 0)