1 // Cocoa implementation of clipboard access for Nasal
3 // Copyright (C) 2012 James Turner <zakalawe@mac.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.
19 #include "NasalClipboard.hxx"
21 #include <simgear/debug/logstream.hxx>
23 #include <Cocoa/Cocoa.h>
26 class CocoaAutoreleasePool
29 CocoaAutoreleasePool()
31 pool = [[NSAutoreleasePool alloc] init];
34 ~CocoaAutoreleasePool()
40 NSAutoreleasePool* pool;
44 static NSString* stdStringToCocoa(const std::string& s)
46 return [NSString stringWithUTF8String:s.c_str()];
49 static std::string stdStringFromCocoa(NSString* s)
51 return std::string([s UTF8String]);
56 class ClipboardCocoa: public NasalClipboard
61 * Get clipboard contents as text
63 virtual std::string getText(Type type)
65 CocoaAutoreleasePool pool;
67 if( type == CLIPBOARD )
69 NSPasteboard* pboard = [NSPasteboard generalPasteboard];
70 NSString* nstext = [pboard stringForType:NSPasteboardTypeString];
71 return stdStringFromCocoa(nstext);
78 * Set clipboard contents as text
80 virtual bool setText(const std::string& text, Type type)
82 CocoaAutoreleasePool pool;
84 if( type == CLIPBOARD )
86 NSPasteboard* pboard = [NSPasteboard generalPasteboard];
87 NSString* nstext = stdStringToCocoa(text);
88 [pboard clearContents];
89 [pboard setString:nstext forType:NSPasteboardTypeString];
98 std::string _selection;
101 //------------------------------------------------------------------------------
102 NasalClipboard::Ptr NasalClipboard::create()
104 return NasalClipboard::Ptr(new ClipboardCocoa);