1 // X11 implementation of clipboard access for Nasal
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "NasalClipboard.hxx"
24 #include "NasalSys.hxx"
25 #include <simgear/nasal/cppbind/NasalCallContext.hxx>
27 #include <boost/algorithm/string/case_conv.hpp>
31 * Nasal wrappers for setting/getting clipboard text
33 //------------------------------------------------------------------------------
34 static NasalClipboard::Type parseType(const nasal::CallContext& ctx, size_t i)
38 if( ctx.isNumeric(i) )
40 if( ctx.requireArg<int>(i) == NasalClipboard::CLIPBOARD )
41 return NasalClipboard::CLIPBOARD;
42 if( ctx.requireArg<int>(i) == NasalClipboard::PRIMARY )
43 return NasalClipboard::PRIMARY;
49 "clipboard: invalid arg "
50 "(expected clipboard.CLIPBOARD or clipboard.SELECTION)"
54 return NasalClipboard::CLIPBOARD;
57 //------------------------------------------------------------------------------
58 static naRef f_setClipboardText(const nasal::CallContext& ctx)
60 if( ctx.argc < 1 || ctx.argc > 2 )
61 naRuntimeError( ctx.c, "clipboard.setText() expects 1 or 2 arguments: "
62 "text, [, type = clipboard.CLIPBOARD]" );
67 NasalClipboard::getInstance()->setText( ctx.requireArg<std::string>(0),
72 //------------------------------------------------------------------------------
73 static naRef f_getClipboardText(const nasal::CallContext& ctx)
76 naRuntimeError(ctx.c, "clipboard.getText() accepts max 1 arg: "
77 "[type = clipboard.CLIPBOARD]");
81 NasalClipboard::getInstance()->getText(parseType(ctx, 0))
85 //------------------------------------------------------------------------------
86 NasalClipboard::Ptr NasalClipboard::_clipboard;
88 //------------------------------------------------------------------------------
89 NasalClipboard::~NasalClipboard()
94 //------------------------------------------------------------------------------
95 void NasalClipboard::init(FGNasalSys *nasal)
97 _clipboard = create();
99 nasal::Hash clipboard = nasal->getGlobals().createHash("clipboard");
101 clipboard.set("setText", f_setClipboardText);
102 clipboard.set("getText", f_getClipboardText);
103 clipboard.set("CLIPBOARD", NasalClipboard::CLIPBOARD);
104 clipboard.set("SELECTION", NasalClipboard::PRIMARY);
107 //------------------------------------------------------------------------------
108 NasalClipboard::Ptr NasalClipboard::getInstance()