Move copy/paste of clipboard text from "ui" to "she" library

This commit is contained in:
David Capello 2014-03-29 22:12:01 -03:00
parent c1e26d0ac0
commit 896fb27fd5
12 changed files with 185 additions and 35 deletions

View File

@ -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 <winalleg.h>
#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();
}

25
src/she/clipboard.h Normal file
View File

@ -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 <string>
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

View File

@ -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

76
src/she/clipboard_win.h Normal file
View File

@ -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<LPWSTR>(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<LPWSTR>(GlobalLock(hglobal));
std::copy(wstr.begin(), wstr.end(), lpstr);
GlobalUnlock(hglobal);
SetClipboardData(CF_UNICODETEXT, hglobal);
}
CloseClipboard();
}
}
}
private:
std::string m_text;
};
} // namespace she
#endif

View File

@ -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"

View File

@ -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 <cassert>
@ -413,6 +419,10 @@ public:
Alleg4Surface::AutoDestroy);
}
Clipboard* createClipboard() {
return new ClipboardImpl();
}
};
static System* g_instance;

View File

@ -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();

View File

@ -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 <string>
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

View File

@ -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 <string>
namespace {
void get_system_clipboard_text(std::string& text)
{
// Do nothing
}
void set_system_clipboard_text(const std::string& text)
{
// Do nothing
}
}

View File

@ -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);

View File

@ -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;
};

View File

@ -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();