Use clip library to copy/paste text

In this way we can remove all the code related to handle clipboard text
from she and ui libraries.
This commit is contained in:
David Capello 2016-04-28 23:26:20 -03:00
parent 174d31a12a
commit 924c2ac86a
23 changed files with 11 additions and 415 deletions

3
.gitmodules vendored
View File

@ -27,3 +27,6 @@
[submodule "third_party/libpng"] [submodule "third_party/libpng"]
path = third_party/libpng path = third_party/libpng
url = https://github.com/aseprite/libpng.git url = https://github.com/aseprite/libpng.git
[submodule "src/clip"]
path = src/clip
url = https://github.com/aseprite/clip.git

View File

@ -86,6 +86,7 @@ add_subdirectory(base)
include_directories(${BASE_INCLUDE_DIR}) include_directories(${BASE_INCLUDE_DIR})
add_subdirectory(cfg) add_subdirectory(cfg)
add_subdirectory(clip)
add_subdirectory(css) add_subdirectory(css)
add_subdirectory(doc) add_subdirectory(doc)
add_subdirectory(filters) add_subdirectory(filters)

View File

@ -439,6 +439,7 @@ add_library(app-lib
target_link_libraries(app-lib target_link_libraries(app-lib
base-lib base-lib
cfg-lib cfg-lib
clip
css-lib css-lib
doc-lib doc-lib
filters-lib filters-lib

View File

@ -37,7 +37,6 @@
#include "base/shared_ptr.h" #include "base/shared_ptr.h"
#include "base/unique_ptr.h" #include "base/unique_ptr.h"
#include "doc/sprite.h" #include "doc/sprite.h"
#include "she/clipboard.h"
#include "she/display.h" #include "she/display.h"
#include "she/error.h" #include "she/error.h"
#include "she/surface.h" #include "she/surface.h"
@ -82,7 +81,6 @@ protected:
}; };
static she::Display* main_display = NULL; static she::Display* main_display = NULL;
static she::Clipboard* main_clipboard = NULL;
static CustomizedGuiManager* manager = NULL; static CustomizedGuiManager* manager = NULL;
static Theme* gui_theme = NULL; static Theme* gui_theme = NULL;
@ -176,12 +174,9 @@ int init_module_gui()
return -1; return -1;
} }
main_clipboard = she::instance()->createClipboard();
// Create the default-manager // Create the default-manager
manager = new CustomizedGuiManager(); manager = new CustomizedGuiManager();
manager->setDisplay(main_display); manager->setDisplay(main_display);
manager->setClipboard(main_clipboard);
// Setup the GUI theme for all widgets // Setup the GUI theme for all widgets
gui_theme = new SkinTheme(); gui_theme = new SkinTheme();
@ -208,7 +203,6 @@ void exit_module_gui()
CurrentTheme::set(NULL); CurrentTheme::set(NULL);
delete gui_theme; delete gui_theme;
main_clipboard->dispose();
main_display->dispose(); main_display->dispose();
} }

1
src/clip Submodule

@ -0,0 +1 @@
Subproject commit e57130a56659d001d7bc33081c7cb3abe0621be2

View File

@ -211,13 +211,11 @@ endif()
if(WIN32) if(WIN32)
list(APPEND SHE_SOURCES list(APPEND SHE_SOURCES
win/clipboard.cpp
win/native_dialogs.cpp) win/native_dialogs.cpp)
endif() endif()
if(APPLE) if(APPLE)
list(APPEND SHE_SOURCES list(APPEND SHE_SOURCES
osx/clipboard.mm
osx/logger.mm osx/logger.mm
osx/native_dialogs.mm) osx/native_dialogs.mm)
endif() endif()

View File

@ -1,27 +0,0 @@
// SHE library
// Copyright (C) 2012-2015 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 "she/display_handle.h"
#include <string>
namespace she {
class Clipboard {
public:
virtual ~Clipboard() { }
virtual void dispose() = 0;
virtual std::string getText(DisplayHandle hwnd) = 0;
virtual void setText(DisplayHandle hwnd, const std::string& text) = 0;
};
} // namespace she
#endif

View File

@ -1,38 +0,0 @@
// SHE library
// Copyright (C) 2012-2015 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(DisplayHandle) override {
return m_text;
}
void setText(DisplayHandle, const std::string& text) override {
m_text = text;
}
private:
std::string m_text;
};
} // namespace she
#endif

View File

@ -9,16 +9,12 @@
#pragma once #pragma once
#ifdef _WIN32 #ifdef _WIN32
#include "she/win/clipboard.h"
#include "she/win/native_dialogs.h" #include "she/win/native_dialogs.h"
#elif defined(__APPLE__) #elif defined(__APPLE__)
#include "she/osx/clipboard.h"
#include "she/osx/native_dialogs.h" #include "she/osx/native_dialogs.h"
#elif defined(ASEPRITE_WITH_GTK_FILE_DIALOG_SUPPORT) && defined(__linux__) #elif defined(ASEPRITE_WITH_GTK_FILE_DIALOG_SUPPORT) && defined(__linux__)
#include "she/clipboard_simple.h"
#include "she/gtk/native_dialogs.h" #include "she/gtk/native_dialogs.h"
#else #else
#include "she/clipboard_simple.h"
#include "she/native_dialogs.h" #include "she/native_dialogs.h"
#endif #endif
@ -68,16 +64,6 @@ public:
return m_nativeDialogs; return m_nativeDialogs;
} }
Clipboard* createClipboard() override {
#ifdef _WIN32
return new ClipboardWin32();
#elif defined(__APPLE__)
return new ClipboardOSX();
#else
return new ClipboardImpl();
#endif
}
Font* loadSpriteSheetFont(const char* filename, int scale) override { Font* loadSpriteSheetFont(const char* filename, int scale) override {
Surface* sheet = loadRgbaSurface(filename); Surface* sheet = loadRgbaSurface(filename);
Font* font = nullptr; Font* font = nullptr;

View File

@ -1,28 +0,0 @@
// SHE library
// Copyright (C) 2012-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef SHE_OSX_CLIPBOARD_H_INCLUDED
#define SHE_OSX_CLIPBOARD_H_INCLUDED
#pragma once
#include "base/string.h"
#include "she/clipboard.h"
namespace she {
class ClipboardOSX : public Clipboard {
public:
void dispose() override;
std::string getText(DisplayHandle display) override;
void setText(DisplayHandle display, const std::string& text) override;
private:
std::string m_text;
};
} // namespace she
#endif

View File

@ -1,36 +0,0 @@
// SHE library
// Copyright (C) 2012-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include <Cocoa/Cocoa.h>
#include "she/osx/clipboard.h"
namespace she {
void ClipboardOSX::dispose()
{
delete this;
}
std::string ClipboardOSX::getText(DisplayHandle display)
{
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
NSString* string = [pasteboard stringForType:NSStringPboardType];
if (string)
return std::string([string UTF8String]);
else
return std::string();
}
void ClipboardOSX::setText(DisplayHandle display, const std::string& text)
{
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard setString:[NSString stringWithUTF8String:text.c_str()]
forType:NSStringPboardType];
}
} // namespace she

View File

@ -8,7 +8,6 @@
#define SHE_H_INCLUDED #define SHE_H_INCLUDED
#pragma once #pragma once
#include "she/clipboard.h"
#include "she/display.h" #include "she/display.h"
#include "she/error.h" #include "she/error.h"
#include "she/event.h" #include "she/event.h"

View File

@ -15,7 +15,6 @@
namespace she { namespace she {
class Clipboard;
class Display; class Display;
class EventQueue; class EventQueue;
class Font; class Font;
@ -49,7 +48,6 @@ namespace she {
virtual Surface* loadRgbaSurface(const char* filename) = 0; virtual Surface* loadRgbaSurface(const char* filename) = 0;
virtual Font* loadSpriteSheetFont(const char* filename, int scale = 1) = 0; virtual Font* loadSpriteSheetFont(const char* filename, int scale = 1) = 0;
virtual Font* loadTrueTypeFont(const char* filename, int height) = 0; virtual Font* loadTrueTypeFont(const char* filename, int height) = 0;
virtual Clipboard* createClipboard() = 0;
}; };
System* create_system(); System* create_system();

View File

@ -1,94 +0,0 @@
// SHE library
// Copyright (C) 2012-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "base/string.h"
#include "she/win/clipboard.h"
#include <windows.h>
namespace {
bool open_clipboard(HWND hwnd)
{
for (int i=0; i<5; ++i) {
if (OpenClipboard(hwnd))
return true;
Sleep(100);
}
return false;
}
}
namespace she {
void ClipboardWin32::dispose()
{
delete this;
}
std::string ClipboardWin32::getText(DisplayHandle hwnd)
{
std::string text;
if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
if (open_clipboard((HWND)hwnd)) {
HGLOBAL hglobal = GetClipboardData(CF_UNICODETEXT);
if (hglobal) {
LPWSTR lpstr = static_cast<LPWSTR>(GlobalLock(hglobal));
if (lpstr) {
text = base::to_utf8(lpstr).c_str();
GlobalUnlock(hglobal);
}
}
CloseClipboard();
}
}
else if (IsClipboardFormatAvailable(CF_TEXT)) {
if (open_clipboard((HWND)hwnd)) {
HGLOBAL hglobal = GetClipboardData(CF_TEXT);
if (hglobal) {
LPSTR lpstr = static_cast<LPSTR>(GlobalLock(hglobal));
if (lpstr) {
text = lpstr;
GlobalUnlock(hglobal);
}
}
CloseClipboard();
}
}
return text;
}
void ClipboardWin32::setText(DisplayHandle hwnd, const std::string& text)
{
if (open_clipboard((HWND)hwnd)) {
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();
}
}
} // namespace she

View File

@ -1,28 +0,0 @@
// SHE library
// Copyright (C) 2012-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef SHE_WIN_CLIPBOARD_H_INCLUDED
#define SHE_WIN_CLIPBOARD_H_INCLUDED
#pragma once
#include "base/string.h"
#include "she/clipboard.h"
namespace she {
class ClipboardWin32 : public Clipboard {
public:
void dispose() override;
std::string getText(DisplayHandle hwnd) override;
void setText(DisplayHandle hwnd, const std::string& text) override;
private:
std::string m_text;
};
} // namespace she
#endif

View File

@ -10,7 +10,6 @@ add_library(ui-lib
alert.cpp alert.cpp
box.cpp box.cpp
button.cpp button.cpp
clipboard.cpp
combobox.cpp combobox.cpp
component.cpp component.cpp
cursor.cpp cursor.cpp

View File

@ -1,42 +0,0 @@
// Aseprite UI Library
// Copyright (C) 2001-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ui/clipboard.h"
#include "she/clipboard.h"
#include "she/display.h"
#include "ui/manager.h"
#include <string>
static std::string clipboard_text;
namespace ui {
namespace clipboard {
const char* get_text()
{
Manager* manager = Manager::getDefault();
clipboard_text = manager->getClipboard()->getText(
manager->getDisplay()->nativeHandle());
return clipboard_text.c_str();
}
void set_text(const char* text)
{
Manager* manager = Manager::getDefault();
clipboard_text = (text ? text: "");
manager->getClipboard()->setText(
manager->getDisplay()->nativeHandle(),
clipboard_text);
}
} // namespace clipboard
} // namespace ui

View File

@ -1,22 +0,0 @@
// Aseprite UI Library
// Copyright (C) 2001-2013 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef UI_CLIPBOARD_H_INCLUDED
#define UI_CLIPBOARD_H_INCLUDED
#pragma once
#include "ui/base.h"
namespace ui {
namespace clipboard {
const char* get_text();
void set_text(const char* text);
} // namespace clipboard
} // namespace ui
#endif

View File

@ -1,55 +0,0 @@
// Aseprite UI Library
// Copyright (C) 2001-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "base/string.h"
#include <algorithm>
#include <windows.h>
namespace {
void get_system_clipboard_text(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();
}
}
}
void set_system_clipboard_text(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();
}
}
}
}

View File

@ -12,8 +12,8 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/string.h" #include "base/string.h"
#include "clip/clip.h"
#include "she/font.h" #include "she/font.h"
#include "ui/clipboard.h"
#include "ui/manager.h" #include "ui/manager.h"
#include "ui/menu.h" #include "ui/menu.h"
#include "ui/message.h" #include "ui/message.h"
@ -611,7 +611,7 @@ void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
// *cut* text! // *cut* text!
if (cmd == EntryCmd::Cut) { if (cmd == EntryCmd::Cut) {
std::wstring selected = text.substr(selbeg, selend - selbeg + 1); std::wstring selected = text.substr(selbeg, selend - selbeg + 1);
clipboard::set_text(base::to_utf8(selected).c_str()); clip::set_text(base::to_utf8(selected));
} }
// remove text // remove text
@ -629,11 +629,8 @@ void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
break; break;
case EntryCmd::Paste: { case EntryCmd::Paste: {
const char* clipboard_str; std::string clipboard;
if (clip::get_text(clipboard)) {
if ((clipboard_str = clipboard::get_text())) {
std::string clipboard(clipboard_str);
// delete the entire selection // delete the entire selection
if (selbeg >= 0) { if (selbeg >= 0) {
text.erase(selbeg, selend-selbeg+1); text.erase(selbeg, selend-selbeg+1);
@ -659,7 +656,7 @@ void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
case EntryCmd::Copy: case EntryCmd::Copy:
if (selbeg >= 0) { if (selbeg >= 0) {
std::wstring selected = text.substr(selbeg, selend - selbeg + 1); std::wstring selected = text.substr(selbeg, selend - selbeg + 1);
clipboard::set_text(base::to_utf8(selected).c_str()); clip::set_text(base::to_utf8(selected));
} }
break; break;

View File

@ -87,7 +87,6 @@ static int cmp_down(Widget* widget, int x, int y);
Manager::Manager() Manager::Manager()
: Widget(kManagerWidget) : Widget(kManagerWidget)
, m_display(NULL) , m_display(NULL)
, m_clipboard(NULL)
, m_eventQueue(NULL) , m_eventQueue(NULL)
, m_lockedWindow(NULL) , m_lockedWindow(NULL)
, m_mouseButtons(kButtonNone) , m_mouseButtons(kButtonNone)
@ -154,11 +153,6 @@ void Manager::setDisplay(she::Display* display)
onNewDisplayConfiguration(); onNewDisplayConfiguration();
} }
void Manager::setClipboard(she::Clipboard* clipboard)
{
m_clipboard = clipboard;
}
void Manager::run() void Manager::run()
{ {
MessageLoop loop(this); MessageLoop loop(this);

View File

@ -16,7 +16,6 @@
#include "ui/widget.h" #include "ui/widget.h"
namespace she { namespace she {
class Clipboard;
class Display; class Display;
class EventQueue; class EventQueue;
} }
@ -37,10 +36,8 @@ namespace ui {
~Manager(); ~Manager();
she::Display* getDisplay() { return m_display; } she::Display* getDisplay() { return m_display; }
she::Clipboard* getClipboard() { return m_clipboard; }
void setDisplay(she::Display* display); void setDisplay(she::Display* display);
void setClipboard(she::Clipboard* clipboard);
// Executes the main message loop. // Executes the main message loop.
void run(); void run();
@ -166,7 +163,6 @@ namespace ui {
WidgetsList m_garbage; WidgetsList m_garbage;
she::Display* m_display; she::Display* m_display;
she::Clipboard* m_clipboard;
she::EventQueue* m_eventQueue; she::EventQueue* m_eventQueue;
gfx::Region m_invalidRegion; // Invalid region (we didn't receive paint messages yet for this). gfx::Region m_invalidRegion; // Invalid region (we didn't receive paint messages yet for this).

View File

@ -13,7 +13,6 @@
#include "ui/base.h" #include "ui/base.h"
#include "ui/box.h" #include "ui/box.h"
#include "ui/button.h" #include "ui/button.h"
#include "ui/clipboard.h"
#include "ui/combobox.h" #include "ui/combobox.h"
#include "ui/component.h" #include "ui/component.h"
#include "ui/cursor.h" #include "ui/cursor.h"