Add a lua module for reading from/writing to the clipboard

Originally committed to SVN as r6501.
This commit is contained in:
Thomas Goyne 2012-02-22 20:47:34 +00:00
parent 74fcf68fe2
commit c4daf860e5
2 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,18 @@
-- Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org>
--
-- 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

View File

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