FlightGear next
NasalClipboard.cxx
Go to the documentation of this file.
1// X11 implementation of clipboard access for Nasal
2//
3// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
4//
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.
9//
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.
14//
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.
18
19#ifdef HAVE_CONFIG_H
20# include "config.h"
21#endif
22
23#include "NasalClipboard.hxx"
24#include "NasalSys.hxx"
25#include <simgear/nasal/cppbind/NasalCallContext.hxx>
26
27#include <cstddef>
28
29/*
30 * Nasal wrappers for setting/getting clipboard text
31 */
32//------------------------------------------------------------------------------
33static NasalClipboard::Type parseType(const nasal::CallContext& ctx, size_t i)
34{
35 if( ctx.argc > i )
36 {
37 if( ctx.isNumeric(i) )
38 {
39 if( ctx.requireArg<int>(i) == NasalClipboard::CLIPBOARD )
41 if( ctx.requireArg<int>(i) == NasalClipboard::PRIMARY )
43 }
44
45 ctx.runtimeError("clipboard: invalid arg "
46 "(expected clipboard.CLIPBOARD or clipboard.SELECTION)");
47 }
48
50}
51
52//------------------------------------------------------------------------------
53static naRef f_setClipboardText(const nasal::CallContext& ctx)
54{
55 if( ctx.argc < 1 || ctx.argc > 2 )
56 ctx.runtimeError("clipboard.setText() expects 1 or 2 arguments: "
57 "text, [, type = clipboard.CLIPBOARD]");
58
59 return
60 naNum
61 (
62 NasalClipboard::getInstance()->setText( ctx.requireArg<std::string>(0),
63 parseType(ctx, 1) )
64 );
65}
66
67//------------------------------------------------------------------------------
68static naRef f_getClipboardText(const nasal::CallContext& ctx)
69{
70 if( ctx.argc > 1 )
71 ctx.runtimeError("clipboard.getText() accepts max 1 arg: "
72 "[type = clipboard.CLIPBOARD]");
73
74 return ctx.to_nasal
75 (
76 NasalClipboard::getInstance()->getText(parseType(ctx, 0))
77 );
78}
79
80//------------------------------------------------------------------------------
82
83//------------------------------------------------------------------------------
88
89//------------------------------------------------------------------------------
91{
93
94 nasal::Hash clipboard = nasal->getGlobals().createHash("clipboard");
95
96 clipboard.set("setText", f_setClipboardText);
97 clipboard.set("getText", f_getClipboardText);
98 clipboard.set("CLIPBOARD", NasalClipboard::CLIPBOARD);
99 clipboard.set("SELECTION", NasalClipboard::PRIMARY);
100}
101
102//------------------------------------------------------------------------------
static naRef f_setClipboardText(const nasal::CallContext &ctx)
static NasalClipboard::Type parseType(const nasal::CallContext &ctx, size_t i)
static naRef f_getClipboardText(const nasal::CallContext &ctx)
#define i(x)
static Ptr create()
Implementation supplied by actual platform implementation.
@ PRIMARY
X11 platforms support also a mode called PRIMARY selection which contains the current (mouse) selecti...
@ CLIPBOARD
Standard clipboard as supported by nearly all operating systems.
virtual ~NasalClipboard()=0
static Ptr getInstance()
Get clipboard platform specific instance.
std::shared_ptr< NasalClipboard > Ptr
static void init(FGNasalSys *nasal)
Sets up the clipboard and puts all the extension functions into a new "clipboard" namespace.
static Ptr _clipboard