BACKENDS: Add a default clipboard implementation

This commit is contained in:
Cameron Cawley 2019-09-19 22:20:08 +01:00 committed by Filippos Karapetis
parent bdd7b6baed
commit 014bef9eab
6 changed files with 34 additions and 37 deletions

View file

@ -457,18 +457,14 @@ Common::String OSystem_SDL::getSystemLanguage() const {
#endif // USE_DETECTLANG
}
bool OSystem_SDL::hasTextInClipboard() {
#if SDL_VERSION_ATLEAST(2, 0, 0)
bool OSystem_SDL::hasTextInClipboard() {
return SDL_HasClipboardText() == SDL_TRUE;
#else
return false;
#endif
}
Common::String OSystem_SDL::getTextFromClipboard() {
if (!hasTextInClipboard()) return "";
#if SDL_VERSION_ATLEAST(2, 0, 0)
char *text = SDL_GetClipboardText();
// The string returned by SDL is in UTF-8. Convert to the
// current TranslationManager encoding or ISO-8859-1.
@ -485,13 +481,9 @@ Common::String OSystem_SDL::getTextFromClipboard() {
SDL_free(text);
return strText;
#else
return "";
#endif
}
bool OSystem_SDL::setTextInClipboard(const Common::String &text) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
// The encoding we need to use is UTF-8. Assume we currently have the
// current TranslationManager encoding or ISO-8859-1.
#ifdef USE_TRANSLATION
@ -505,10 +497,8 @@ bool OSystem_SDL::setTextInClipboard(const Common::String &text) {
return status == 0;
}
return SDL_SetClipboardText(text.c_str()) == 0;
#else
return false;
#endif
}
#endif
uint32 OSystem_SDL::getMillis(bool skipRecord) {
uint32 millis = SDL_GetTicks();

View file

@ -69,10 +69,12 @@ public:
virtual Common::String getSystemLanguage() const;
#if SDL_VERSION_ATLEAST(2, 0, 0)
// Clipboard
virtual bool hasTextInClipboard();
virtual Common::String getTextFromClipboard();
virtual bool setTextInClipboard(const Common::String &text);
#endif
virtual void setWindowCaption(const char *caption);
virtual void addSysArchivesToSearchSet(Common::SearchSet &s, int priority = 0);

View file

@ -216,6 +216,12 @@ protected:
*/
FilesystemFactory *_fsFactory;
/**
* Used by the default clipboard implementation, for backends that don't
* implement clipboard support.
*/
Common::String _clipboard;
private:
/**
* Indicate if initBackend() has been called.
@ -374,8 +380,11 @@ public:
kFeatureDisplayLogFile,
/**
* The presence of this feature indicates whether the hasTextInClipboard(),
* getTextFromClipboard() and setTextInClipboard() calls are supported.
* The presence of this feature indicates whether the system clipboard is
* available. If this feature is not present, the hasTextInClipboard(),
* getTextFromClipboard() and setTextInClipboard() calls can still be used,
* however it should not be used in scenarios where the user is expected to
* copy data outside of the application.
*
* This feature has no associated state.
*/
@ -1451,7 +1460,7 @@ public:
*
* @return true if there is text in the clipboard, false otherwise
*/
virtual bool hasTextInClipboard() { return false; }
virtual bool hasTextInClipboard() { return !_clipboard.empty(); }
/**
* Returns clipboard contents as a String.
@ -1462,7 +1471,7 @@ public:
*
* @return clipboard contents ("" if hasTextInClipboard() == false)
*/
virtual Common::String getTextFromClipboard() { return ""; }
virtual Common::String getTextFromClipboard() { return _clipboard; }
/**
* Set the content of the clipboard to the given string.
@ -1473,7 +1482,7 @@ public:
*
* @return true if the text was properly set in the clipboard, false otherwise
*/
virtual bool setTextInClipboard(const Common::String &text) { return false; }
virtual bool setTextInClipboard(const Common::String &text) { _clipboard = text; return true; }
/**
* Open the given Url in the default browser (if available on the target

View file

@ -33,7 +33,6 @@ void Clipboard::clipboardStore(const Common::U32String &text) {
}
void Clipboard::clipboardSend(ClipSource source) {
if (g_system->hasFeature(OSystem::kFeatureClipboardSupport)) {
// Convert unicode string to standard string, since that's all ScummVM supports
Common::String text;
for (uint idx = 0; idx < _text.size(); ++idx)
@ -41,10 +40,8 @@ void Clipboard::clipboardSend(ClipSource source) {
g_system->setTextInClipboard(text);
}
}
void Clipboard::clipboardReceive(ClipSource source) {
if (g_system->hasFeature(OSystem::kFeatureClipboardSupport)) {
Windows &windows = *g_vm->_windows;
if (g_system->hasTextInClipboard()) {
@ -56,7 +53,6 @@ void Clipboard::clipboardReceive(ClipSource source) {
}
}
}
}
/*--------------------------------------------------------------------------*/

View file

@ -523,7 +523,7 @@ void ConsoleDialog::specialKeys(Common::KeyCode keycode) {
g_gui.scheduleTopDialogRedraw();
break;
case Common::KEYCODE_v:
if (g_system->hasFeature(OSystem::kFeatureClipboardSupport) && g_system->hasTextInClipboard()) {
if (g_system->hasTextInClipboard()) {
Common::String text = g_system->getTextFromClipboard();
insertIntoPrompt(text.c_str());
scrollToCurrent();
@ -531,7 +531,7 @@ void ConsoleDialog::specialKeys(Common::KeyCode keycode) {
}
break;
case Common::KEYCODE_c:
if (g_system->hasFeature(OSystem::kFeatureClipboardSupport)) {
{
Common::String userInput = getUserInput();
if (!userInput.empty())
g_system->setTextInClipboard(userInput);

View file

@ -186,7 +186,7 @@ bool EditableWidget::handleKeyDown(Common::KeyState state) {
break;
case Common::KEYCODE_v:
if (g_system->hasFeature(OSystem::kFeatureClipboardSupport) && state.flags & Common::KBD_CTRL) {
if (state.flags & Common::KBD_CTRL) {
if (g_system->hasTextInClipboard()) {
String text = g_system->getTextFromClipboard();
for (uint32 i = 0; i < text.size(); ++i) {
@ -201,7 +201,7 @@ bool EditableWidget::handleKeyDown(Common::KeyState state) {
break;
case Common::KEYCODE_c:
if (g_system->hasFeature(OSystem::kFeatureClipboardSupport) && state.flags & Common::KBD_CTRL) {
if (state.flags & Common::KBD_CTRL) {
if (!getEditString().empty())
g_system->setTextInClipboard(getEditString());
} else {