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.
21 #include "NasalClipboard.hxx"
23 #include <simgear/debug/logstream.hxx>
25 #include <Cocoa/Cocoa.h>
28 class CocoaAutoreleasePool
31 CocoaAutoreleasePool()
33 pool = [[NSAutoreleasePool alloc] init];
36 ~CocoaAutoreleasePool()
42 NSAutoreleasePool* pool;
46 static NSString* stdStringToCocoa(const std::string& s)
48 return [NSString stringWithUTF8String:s.c_str()];
51 static std::string stdStringFromCocoa(NSString* s)
53 return std::string([s UTF8String]);
58 class ClipboardCocoa: public NasalClipboard
63 * Get clipboard contents as text
65 virtual std::string getText(Type type)
67 CocoaAutoreleasePool pool;
69 if( type == CLIPBOARD )
71 NSPasteboard* pboard = [NSPasteboard generalPasteboard];
72 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
73 NSString* nstext = [pboard stringForType:NSStringPboardType];
75 NSString* nstext = [pboard stringForType:NSPasteboardTypeString];
76 #endif // MAC_OS_X_VERSION_MIN_REQUIRED
77 return stdStringFromCocoa(nstext);
84 * Set clipboard contents as text
86 virtual bool setText(const std::string& text, Type type)
88 CocoaAutoreleasePool pool;
90 if( type == CLIPBOARD )
92 NSPasteboard* pboard = [NSPasteboard generalPasteboard];
93 NSString* nstext = stdStringToCocoa(text);
94 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
95 NSString* type = NSStringPboardType;
96 NSArray* types = [NSArray arrayWithObjects: type, nil];
97 [pboard declareTypes:types owner:nil];
98 [pboard setString:nstext forType: NSStringPboardType];
100 NSString* type = NSPasteboardTypeString;
101 [pboard clearContents];
102 [pboard setString:nstext forType:NSPasteboardTypeString];
103 #endif // MAC_OS_X_VERSION_MIN_REQUIRED
112 std::string _selection;
115 //------------------------------------------------------------------------------
116 NasalClipboard::Ptr NasalClipboard::create()
118 return NasalClipboard::Ptr(new ClipboardCocoa);