From 42b3866753840b2cd8cbdef64fcaf4b5206e66e3 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sun, 30 Jul 2023 15:10:26 +0200 Subject: [PATCH] start thinking about pasting files --- src/CMakeLists.txt | 2 ++ src/chat_gui4.cpp | 13 +++++++++++++ src/main.cpp | 6 +++++- src/main_screen.cpp | 4 ++++ src/main_screen.hpp | 2 ++ src/screen.hpp | 5 +++++ src/sdl_clipboard_utils.cpp | 23 +++++++++++++++++++++++ src/sdl_clipboard_utils.hpp | 6 ++++++ 8 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/sdl_clipboard_utils.cpp create mode 100644 src/sdl_clipboard_utils.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ca8f5226..a4a38639 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,6 +19,8 @@ add_executable(tomato texture_uploader.hpp ./sdlrenderer_texture_uploader.hpp ./sdlrenderer_texture_uploader.cpp + ./sdl_clipboard_utils.hpp + ./sdl_clipboard_utils.cpp ./file_selector.hpp ./file_selector.cpp diff --git a/src/chat_gui4.cpp b/src/chat_gui4.cpp index b9f2f314..de16df1c 100644 --- a/src/chat_gui4.cpp +++ b/src/chat_gui4.cpp @@ -11,6 +11,11 @@ #include #include +#include + +#include "./sdl_clipboard_utils.hpp" +#include "SDL_clipboard.h" + #include #include #include @@ -256,6 +261,14 @@ void ChatGui4::render(void) { } } ImGui::EndChild(); + + if (ImGui::IsKeyPressed(ImGuiKey_V) && ImGui::IsKeyPressed(ImGuiMod_Shortcut)) { + if (const auto* mime_type = clipboardHasImage(); mime_type != nullptr) { + size_t data_size = 0; + const auto* data = SDL_GetClipboardData(mime_type, &data_size); + // open file send preview.rawpixels + } + } } ImGui::EndChild(); } diff --git a/src/main.cpp b/src/main.cpp index 42a1a1d0..ec0f3ba1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,6 @@ #include #include "./theme.hpp" -#include "./sdlrenderer_texture_uploader.hpp" #include "./start_screen.hpp" @@ -63,6 +62,11 @@ int main(int argc, char** argv) { quit = true; break; } + + if (screen->handleEvent(event)) { + continue; + } + ImGui_ImplSDL3_ProcessEvent(&event); } if (quit) { diff --git a/src/main_screen.cpp b/src/main_screen.cpp index 5d4cfa43..6a2f5a36 100644 --- a/src/main_screen.cpp +++ b/src/main_screen.cpp @@ -55,6 +55,10 @@ MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path) : MainScreen::~MainScreen(void) { } +bool MainScreen::handleEvent(SDL_Event& e) { + return false; +} + Screen* MainScreen::poll(bool& quit) { auto new_time = std::chrono::high_resolution_clock::now(); const float time_delta {std::chrono::duration(new_time - last_time).count()}; diff --git a/src/main_screen.hpp b/src/main_screen.hpp index c8c71f5b..6f9de6a2 100644 --- a/src/main_screen.hpp +++ b/src/main_screen.hpp @@ -55,6 +55,8 @@ struct MainScreen final : public Screen { MainScreen(SDL_Renderer* renderer_, std::string save_path); ~MainScreen(void); + bool handleEvent(SDL_Event& e) override; + // return nullptr if not next // sets bool quit to true if exit Screen* poll(bool&) override; diff --git a/src/screen.hpp b/src/screen.hpp index c48d9d1f..238f014f 100644 --- a/src/screen.hpp +++ b/src/screen.hpp @@ -1,8 +1,13 @@ #pragma once +#include + struct Screen { virtual ~Screen(void) = default; + // return true if handled + virtual bool handleEvent(SDL_Event& e) { return false; } + // return nullptr if not next // sets bool quit to true if exit virtual Screen* poll(bool& quit) = 0; diff --git a/src/sdl_clipboard_utils.cpp b/src/sdl_clipboard_utils.cpp new file mode 100644 index 00000000..3968462d --- /dev/null +++ b/src/sdl_clipboard_utils.cpp @@ -0,0 +1,23 @@ +#include "./sdl_clipboard_utils.hpp" + +#include + +#include + +const char* clipboardHasImage(void) { + const static std::vector image_mime_types { + "image/webp", + "image/png", + "image/gif", + "image/jpeg", + "image/bmp", + }; + + for (const char* mime_type : image_mime_types) { + if (SDL_HasClipboardData(mime_type) == SDL_TRUE) { + return mime_type; + } + } + + return nullptr; +} diff --git a/src/sdl_clipboard_utils.hpp b/src/sdl_clipboard_utils.hpp new file mode 100644 index 00000000..3e1fd52e --- /dev/null +++ b/src/sdl_clipboard_utils.hpp @@ -0,0 +1,6 @@ +#pragma once + +// returns the mimetype (c-string) of the image in the clipboard +// or nullptr, if there is none +const char* clipboardHasImage(void); +