]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalClipboard.cxx
Canvas.MouseEvent: expose button/modifier state.
[flightgear.git] / src / Scripting / NasalClipboard.cxx
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 <boost/algorithm/string/case_conv.hpp>
28 #include <cstddef>
29
30 /*
31  *  Nasal wrappers for setting/getting clipboard text
32  */
33 //------------------------------------------------------------------------------
34 static NasalClipboard::Type parseType(const nasal::CallContext& ctx, size_t i)
35 {
36   if( ctx.argc > i )
37   {
38     if( ctx.isNumeric(i) )
39     {
40       if( ctx.requireArg<int>(i) == NasalClipboard::CLIPBOARD )
41         return NasalClipboard::CLIPBOARD;
42       if( ctx.requireArg<int>(i) == NasalClipboard::PRIMARY )
43         return NasalClipboard::PRIMARY;
44     }
45
46     naRuntimeError
47     (
48       ctx.c,
49       "clipboard: invalid arg "
50       "(expected clipboard.CLIPBOARD or clipboard.SELECTION)"
51     );
52   }
53
54   return NasalClipboard::CLIPBOARD;
55 }
56
57 //------------------------------------------------------------------------------
58 static naRef f_setClipboardText(const nasal::CallContext& ctx)
59 {
60   if( ctx.argc < 1 || ctx.argc > 2 )
61     naRuntimeError( ctx.c, "clipboard.setText() expects 1 or 2 arguments: "
62                            "text, [, type = clipboard.CLIPBOARD]" );
63
64   return
65     naNum
66     (
67       NasalClipboard::getInstance()->setText( ctx.requireArg<std::string>(0),
68                                               parseType(ctx, 1) )
69     );
70 }
71
72 //------------------------------------------------------------------------------
73 static naRef f_getClipboardText(const nasal::CallContext& ctx)
74 {
75   if( ctx.argc > 1 )
76     naRuntimeError(ctx.c, "clipboard.getText() accepts max 1 arg: "
77                           "[type = clipboard.CLIPBOARD]");
78
79   return ctx.to_nasal
80   (
81     NasalClipboard::getInstance()->getText(parseType(ctx, 0))
82   );
83 }
84
85 //------------------------------------------------------------------------------
86 NasalClipboard::Ptr NasalClipboard::_clipboard;
87
88 //------------------------------------------------------------------------------
89 NasalClipboard::~NasalClipboard()
90 {
91
92 }
93
94 //------------------------------------------------------------------------------
95 void NasalClipboard::init(FGNasalSys *nasal)
96 {
97   _clipboard = create();
98
99   nasal::Hash clipboard = nasal->getGlobals().createHash("clipboard");
100
101   clipboard.set("setText", f_setClipboardText);
102   clipboard.set("getText", f_getClipboardText);
103   clipboard.set("CLIPBOARD", NasalClipboard::CLIPBOARD);
104   clipboard.set("SELECTION", NasalClipboard::PRIMARY);
105 }
106
107 //------------------------------------------------------------------------------
108 NasalClipboard::Ptr NasalClipboard::getInstance()
109 {
110   return _clipboard;
111 }