Debian Codegraph @

src/Scripting/ClipboardCocoa.mm

raw
1 // Cocoa implementation of clipboard access for Nasal
2 //
3 // Copyright (C) 2012 James Turner <[email protected]>
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
20
21 #include "NasalClipboard.hxx"
22
23 #include <simgear/debug/logstream.hxx>
24
25 #include <AppKit/NSPasteboard.h>
26 #include <Foundation/NSArray.h>
27
28 #include <GUI/CocoaHelpers_private.h>
29
30 /**
31 */
32 class ClipboardCocoa: public NasalClipboard
33 {
34 public:
35
36 /**
37 * Get clipboard contents as text
38 */
39 virtual std::string getText(Type type)
40 {
41 CocoaAutoreleasePool pool;
42
43 if( type == CLIPBOARD )
44 {
45 NSPasteboard* pboard = [NSPasteboard generalPasteboard];
46 NSString *nstext = [pboard stringForType:NSPasteboardTypeString];
47 return stdStringFromCocoa(nstext);
48 }
49
50 return "";
51 }
52
53 /**
54 * Set clipboard contents as text
55 */
56 virtual bool setText(const std::string& text, Type type)
57 {
58 CocoaAutoreleasePool pool;
59
60 if( type == CLIPBOARD )
61 {
62 NSPasteboard* pboard = [NSPasteboard generalPasteboard];
63 NSString* nstext = stdStringToCocoa(text);
64 [pboard clearContents];
65 [pboard setString:nstext forType:NSPasteboardTypeString];
66 return true;
67 }
68
69 return false;
70 }
71
72 protected:
73
74 std::string _selection;
75 };
76
77 //------------------------------------------------------------------------------
78 NasalClipboard::Ptr NasalClipboard::create()
79 {
80 return NasalClipboard::Ptr(new ClipboardCocoa);
81 }