From c4daf860e5dce567142c67d440b65da7a71e6c57 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Wed, 22 Feb 2012 20:47:34 +0000 Subject: [PATCH] Add a lua module for reading from/writing to the clipboard Originally committed to SVN as r6501. --- aegisub/automation/include/clipboard.lua | 18 +++++++++ aegisub/src/auto4_lua.cpp | 50 ++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 aegisub/automation/include/clipboard.lua diff --git a/aegisub/automation/include/clipboard.lua b/aegisub/automation/include/clipboard.lua new file mode 100644 index 000000000..ffc87be55 --- /dev/null +++ b/aegisub/automation/include/clipboard.lua @@ -0,0 +1,18 @@ +-- Copyright (c) 2012, Thomas Goyne +-- +-- Permission to use, copy, modify, and distribute this software for any +-- purpose with or without fee is hereby granted, provided that the above +-- copyright notice and this permission notice appear in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- +-- $Id$ + +_G.clipboard = aegisub.__init_clipboard() +return _G.clipboard diff --git a/aegisub/src/auto4_lua.cpp b/aegisub/src/auto4_lua.cpp index 9538b1dc2..cec39ecb3 100644 --- a/aegisub/src/auto4_lua.cpp +++ b/aegisub/src/auto4_lua.cpp @@ -270,6 +270,55 @@ namespace { set_field(L, "init_flags", regex_init_flags); return 1; } + + int clipboard_get(lua_State *L) + { + if (wxTheClipboard->Open()) { + if (wxTheClipboard->IsSupported(wxDF_TEXT)) { + wxTextDataObject rawdata; + wxTheClipboard->GetData(rawdata); + lua_pushstring(L, rawdata.GetText().utf8_str()); + } + else + lua_pushnil(L); + wxTheClipboard->Close(); + } + else + lua_pushnil(L); + return 1; + } + + int clipboard_set(lua_State *L) + { + wxString str(check_wxstring(L, 1)); + + bool succeeded = false; + +#if wxUSE_OLE + // OLE needs to be initialized on each thread that wants to write to + // the clipboard, which wx does not handle automatically + wxClipboard cb; + wxClipboard *theCB = &cb; +#else + wxClipboard *theCB = wxTheClipboard; +#endif + if (theCB->Open()) { + succeeded = theCB->SetData(new wxTextDataObject(str)); + theCB->Close(); + theCB->Flush(); + } + + lua_pushboolean(L, succeeded); + return 1; + } + + int clipboard_init(lua_State *L) + { + lua_newtable(L); + set_field(L, "get", clipboard_get); + set_field(L, "set", clipboard_set); + return 1; + } } // LuaStackcheck @@ -402,6 +451,7 @@ namespace Automation4 { set_field(L, "cancel", LuaCancel); set_field(L, "lua_automation_version", 4); set_field(L, "__init_regex", regex_init); + set_field(L, "__init_clipboard", clipboard_init); set_field(L, "file_name", get_file_name); // store aegisub table to globals