mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 05:27:41 -05:00
impr: Added clip library to improve clipboard situation
This commit is contained in:
@@ -42,6 +42,7 @@ set(LIBIMHEX_SOURCES
|
||||
source/helpers/keys.cpp
|
||||
source/helpers/freetype.cpp
|
||||
source/helpers/udp_server.cpp
|
||||
source/helpers/clipboard.cpp
|
||||
|
||||
source/test/tests.cpp
|
||||
|
||||
@@ -160,8 +161,7 @@ if (NOT IMHEX_EXTERNAL_PLUGIN_BUILD)
|
||||
endif()
|
||||
|
||||
if (NOT EMSCRIPTEN)
|
||||
# curl is only used in non-emscripten builds
|
||||
target_link_libraries(libimhex ${LIBIMHEX_LIBRARY_TYPE} CURL::libcurl)
|
||||
target_link_libraries(libimhex ${LIBIMHEX_LIBRARY_TYPE} CURL::libcurl clip)
|
||||
endif()
|
||||
|
||||
target_include_directories(libimhex ${LIBIMHEX_LIBRARY_TYPE} ${MBEDTLS_INCLUDE_DIR} ${LIBBACKTRACE_INCLUDE_DIRS} ${MAGIC_INCLUDE_DIRS})
|
||||
|
||||
16
lib/libimhex/include/hex/helpers/clipboard.hpp
Normal file
16
lib/libimhex/include/hex/helpers/clipboard.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <hex.hpp>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace hex::clipboard {
|
||||
|
||||
void init();
|
||||
void setBinaryData(std::span<const u8> data);
|
||||
[[nodiscard]] std::vector<u8> getBinaryData();
|
||||
void setTextData(const std::string &string);
|
||||
[[nodiscard]] std::string getTextData();
|
||||
|
||||
}
|
||||
91
lib/libimhex/source/helpers/clipboard.cpp
Normal file
91
lib/libimhex/source/helpers/clipboard.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <hex/helpers/clipboard.hpp>
|
||||
|
||||
#if __has_include(<clip.h>)
|
||||
#define CLIP_LIBRARY
|
||||
#endif
|
||||
|
||||
#if defined(CLIP_LIBRARY)
|
||||
#include <clip.h>
|
||||
#else
|
||||
#include <imgui.h>
|
||||
#include <fmt/color.h>
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#endif
|
||||
|
||||
namespace hex::clipboard {
|
||||
|
||||
#if defined(CLIP_LIBRARY)
|
||||
|
||||
static clip::format s_binaryFormat;
|
||||
static clip::format s_textFormat;
|
||||
|
||||
void init() {
|
||||
s_binaryFormat = clip::register_format("net.werwolv.imhex.binary");
|
||||
s_textFormat = clip::text_format();
|
||||
}
|
||||
|
||||
void setBinaryData(std::span<const u8> data) {
|
||||
clip::lock l;
|
||||
l.set_data(s_binaryFormat, reinterpret_cast<const char*>(data.data()), data.size());
|
||||
}
|
||||
|
||||
std::vector<u8> getBinaryData() {
|
||||
clip::lock l;
|
||||
|
||||
const auto size = l.get_data_length(s_binaryFormat);
|
||||
std::vector<u8> data(size);
|
||||
l.get_data(s_binaryFormat, reinterpret_cast<char*>(data.data()), size);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void setTextData(const std::string &string) {
|
||||
clip::lock l;
|
||||
l.set_data(s_textFormat, string.data(), string.size());
|
||||
}
|
||||
|
||||
std::string getTextData() {
|
||||
clip::lock l;
|
||||
|
||||
const auto size = l.get_data_length(s_binaryFormat);
|
||||
std::string data(size, 0x00);
|
||||
l.get_data(s_textFormat, data.data(), size);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void init() {}
|
||||
|
||||
void setBinaryData(std::span<const u8> data) {
|
||||
constexpr static auto Format = "{0:02X} ";
|
||||
std::string result;
|
||||
result.reserve(fmt::format(Format, 0x00).size() * data.size_bytes());
|
||||
|
||||
for (const auto &byte : data)
|
||||
result += fmt::format(Format, byte);
|
||||
result.pop_back();
|
||||
|
||||
ImGui::SetClipboardText(result.c_str());
|
||||
}
|
||||
|
||||
std::vector<u8> getBinaryData() {
|
||||
auto clipboard = ImGui::GetClipboardText();
|
||||
if (clipboard == nullptr)
|
||||
return {};
|
||||
|
||||
return parseHexString(clipboard);
|
||||
}
|
||||
|
||||
void setTextData(const std::string &string) {
|
||||
ImGui::SetClipboardText(string.c_str());
|
||||
}
|
||||
|
||||
std::string getTextData() {
|
||||
return ImGui::GetClipboardText();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
1
lib/third_party/clip
vendored
Submodule
1
lib/third_party/clip
vendored
Submodule
Submodule lib/third_party/clip added at 7a60eabaad
Reference in New Issue
Block a user