diff --git a/src/app/modules/gui.cpp b/src/app/modules/gui.cpp index 29d7f517e..38a8b4f72 100644 --- a/src/app/modules/gui.cpp +++ b/src/app/modules/gui.cpp @@ -47,6 +47,7 @@ #include "base/shared_ptr.h" #include "base/unique_ptr.h" #include "raster/sprite.h" +#include "she/clipboard.h" #include "she/display.h" #include "she/surface.h" #include "she/system.h" @@ -61,6 +62,7 @@ #ifdef ALLEGRO_WINDOWS #include + #endif #define SPRITEDITOR_ACTION_COPYSELECTION "CopySelection" @@ -134,6 +136,7 @@ protected: }; static she::Display* main_display = NULL; +static she::Clipboard* main_clipboard = NULL; static CustomizedGuiManager* manager = NULL; static Theme* ase_theme = NULL; @@ -191,9 +194,12 @@ int init_module_gui() return -1; } + main_clipboard = she::Instance()->createClipboard(); + // Create the default-manager manager = new CustomizedGuiManager(); manager->setDisplay(main_display); + manager->setClipboard(main_clipboard); // Setup the GUI theme for all widgets CurrentTheme::set(ase_theme = new SkinTheme()); @@ -236,6 +242,7 @@ void exit_module_gui() remove_keyboard(); remove_mouse(); + main_clipboard->dispose(); main_display->dispose(); } diff --git a/src/she/clipboard.h b/src/she/clipboard.h new file mode 100644 index 000000000..c91224689 --- /dev/null +++ b/src/she/clipboard.h @@ -0,0 +1,25 @@ +// SHE library +// Copyright (C) 2012-2014 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifndef SHE_CLIPBOARD_H_INCLUDED +#define SHE_CLIPBOARD_H_INCLUDED +#pragma once + +#include + +namespace she { + + class Clipboard { + public: + virtual ~Clipboard() { } + virtual void dispose() = 0; + virtual std::string getText() = 0; + virtual void setText(const std::string& text) = 0; + }; + +} // namespace she + +#endif diff --git a/src/she/clipboard_simple.h b/src/she/clipboard_simple.h new file mode 100644 index 000000000..d997b5ea2 --- /dev/null +++ b/src/she/clipboard_simple.h @@ -0,0 +1,38 @@ +// SHE library +// Copyright (C) 2012-2014 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifndef SHE_CLIPBOARD_IMPL_H_INCLUDED +#define SHE_CLIPBOARD_IMPL_H_INCLUDED +#pragma once + +#include "she/clipboard.h" + +namespace she { + + class ClipboardImpl : public Clipboard { + public: + ~ClipboardImpl() { + } + + void dispose() OVERRIDE { + delete this; + } + + std::string getText() OVERRIDE { + return m_text; + } + + void setText(const std::string& text) { + m_text = text; + } + + private: + std::string m_text; + }; + +} // namespace she + +#endif diff --git a/src/she/clipboard_win.h b/src/she/clipboard_win.h new file mode 100644 index 000000000..d96ddfa2f --- /dev/null +++ b/src/she/clipboard_win.h @@ -0,0 +1,76 @@ +// SHE library +// Copyright (C) 2012-2014 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifndef SHE_CLIPBOARD_IMPL_H_INCLUDED +#define SHE_CLIPBOARD_IMPL_H_INCLUDED +#pragma once + +#include "base/string.h" +#include "she/clipboard.h" + +#pragma warning(disable:4996) // To void MSVC warning about std::copy() with unsafe arguments + +namespace she { + + class ClipboardImpl : public Clipboard { + public: + ~ClipboardImpl() { + } + + void dispose() OVERRIDE { + delete this; + } + + std::string getText() OVERRIDE { + std::string text; + + if (IsClipboardFormatAvailable(CF_UNICODETEXT)) { + if (OpenClipboard(win_get_window())) { + HGLOBAL hglobal = GetClipboardData(CF_UNICODETEXT); + if (hglobal != NULL) { + LPWSTR lpstr = static_cast(GlobalLock(hglobal)); + if (lpstr != NULL) { + text = base::to_utf8(lpstr).c_str(); + GlobalUnlock(hglobal); + } + } + CloseClipboard(); + } + } + + return text; + } + + void setText(const std::string& text) { + if (IsClipboardFormatAvailable(CF_UNICODETEXT)) { + if (OpenClipboard(win_get_window())) { + EmptyClipboard(); + + if (!text.empty()) { + std::wstring wstr = base::from_utf8(text); + int len = wstr.size(); + + HGLOBAL hglobal = GlobalAlloc(GMEM_MOVEABLE | + GMEM_ZEROINIT, sizeof(WCHAR)*(len+1)); + + LPWSTR lpstr = static_cast(GlobalLock(hglobal)); + std::copy(wstr.begin(), wstr.end(), lpstr); + GlobalUnlock(hglobal); + + SetClipboardData(CF_UNICODETEXT, hglobal); + } + CloseClipboard(); + } + } + } + + private: + std::string m_text; + }; + +} // namespace she + +#endif diff --git a/src/she/she.h b/src/she/she.h index 7a8f086cc..f5bfa4916 100644 --- a/src/she/she.h +++ b/src/she/she.h @@ -1,5 +1,5 @@ // SHE library -// Copyright (C) 2012-2013 David Capello +// Copyright (C) 2012-2014 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -8,6 +8,7 @@ #define SHE_H_INCLUDED #pragma once +#include "she/clipboard.h" #include "she/display.h" #include "she/event.h" #include "she/event_queue.h" diff --git a/src/she/she_alleg4.cpp b/src/she/she_alleg4.cpp index a5c4447f2..4aecba4e2 100644 --- a/src/she/she_alleg4.cpp +++ b/src/she/she_alleg4.cpp @@ -1,5 +1,5 @@ // SHE library -// Copyright (C) 2012-2013 David Capello +// Copyright (C) 2012-2014 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -26,6 +26,12 @@ #endif #endif +#ifdef WIN32 + #include "she/clipboard_win.h" +#else + #include "she/clipboard_simple.h" +#endif + #include "loadpng.h" #include @@ -413,6 +419,10 @@ public: Alleg4Surface::AutoDestroy); } + Clipboard* createClipboard() { + return new ClipboardImpl(); + } + }; static System* g_instance; diff --git a/src/she/system.h b/src/she/system.h index 17ea839de..3a7c605cc 100644 --- a/src/she/system.h +++ b/src/she/system.h @@ -13,9 +13,10 @@ namespace she { - class Surface; + class Clipboard; class Display; class EventLoop; + class Surface; class DisplayCreationException : std::runtime_error { public: @@ -31,6 +32,7 @@ namespace she { virtual Display* createDisplay(int width, int height, int scale) = 0; virtual Surface* createSurface(int width, int height) = 0; virtual Surface* createSurfaceFromNativeHandle(void* nativeHandle) = 0; + virtual Clipboard* createClipboard() = 0; }; System* CreateSystem(); diff --git a/src/ui/clipboard.cpp b/src/ui/clipboard.cpp index 546341e78..0316233ce 100644 --- a/src/ui/clipboard.cpp +++ b/src/ui/clipboard.cpp @@ -10,22 +10,27 @@ #include "ui/clipboard.h" -#ifdef WIN32 -#include "ui/clipboard_win.h" -#else -#include "ui/clipboard_none.h" -#endif +#include "she/clipboard.h" +#include "ui/manager.h" + +#include static std::string clipboard_text; +namespace ui { +namespace clipboard { + const char* ui::clipboard::get_text() { - get_system_clipboard_text(clipboard_text); + clipboard_text = Manager::getDefault()->getClipboard()->getText(); return clipboard_text.c_str(); } void ui::clipboard::set_text(const char* text) { clipboard_text = text ? text: ""; - set_system_clipboard_text(clipboard_text); + Manager::getDefault()->getClipboard()->setText(clipboard_text); } + +} // namespace clipboard +} // namespace ui diff --git a/src/ui/clipboard_none.h b/src/ui/clipboard_none.h deleted file mode 100644 index 202ae7a40..000000000 --- a/src/ui/clipboard_none.h +++ /dev/null @@ -1,21 +0,0 @@ -// Aseprite UI Library -// Copyright (C) 2001-2014 David Capello -// -// This file is released under the terms of the MIT license. -// Read LICENSE.txt for more information. - -#include - -namespace { - -void get_system_clipboard_text(std::string& text) -{ - // Do nothing -} - -void set_system_clipboard_text(const std::string& text) -{ - // Do nothing -} - -} diff --git a/src/ui/manager.cpp b/src/ui/manager.cpp index 2e62a808c..2fecda7c7 100644 --- a/src/ui/manager.cpp +++ b/src/ui/manager.cpp @@ -110,6 +110,7 @@ static void allegro_window_close_hook() Manager::Manager() : Widget(kManagerWidget) , m_display(NULL) + , m_clipboard(NULL) , m_eventQueue(NULL) { if (!m_defaultManager) { @@ -184,6 +185,11 @@ void Manager::setDisplay(she::Display* display) m_eventQueue = m_display->getEventQueue(); } +void Manager::setClipboard(she::Clipboard* clipboard) +{ + m_clipboard = clipboard; +} + void Manager::run() { MessageLoop loop(this); diff --git a/src/ui/manager.h b/src/ui/manager.h index 31b5fcd4a..b58e1d2fc 100644 --- a/src/ui/manager.h +++ b/src/ui/manager.h @@ -14,6 +14,7 @@ #include "ui/widget.h" namespace she { + class Clipboard; class Display; class EventQueue; } @@ -35,7 +36,10 @@ namespace ui { ~Manager(); she::Display* getDisplay() { return m_display; } + she::Clipboard* getClipboard() { return m_clipboard; } + void setDisplay(she::Display* display); + void setClipboard(she::Clipboard* clipboard); void run(); @@ -103,6 +107,7 @@ namespace ui { WidgetsList m_garbage; she::Display* m_display; + she::Clipboard* m_clipboard; she::EventQueue* m_eventQueue; }; diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index a98477cff..b8ac032c1 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -9,7 +9,6 @@ #endif #include "ui/base.h" -#include "ui/clipboard.h" #include "ui/overlay_manager.h" #include "ui/theme.h" @@ -39,9 +38,6 @@ GuiSystem::~GuiSystem() // finish theme CurrentTheme::set(NULL); - // destroy clipboard - clipboard::set_text(NULL); - // shutdown system _ji_widgets_exit(); _ji_font_exit();