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 <AppKit/NSPasteboard.h>
26 #include <Foundation/NSArray.h>
28 #include <GUI/CocoaHelpers_private.h>
32 class ClipboardCocoa: public NasalClipboard
37 * Get clipboard contents as text
39 virtual std::string getText(Type type)
41 CocoaAutoreleasePool pool;
43 if( type == CLIPBOARD )
45 NSPasteboard* pboard = [NSPasteboard generalPasteboard];
46 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
47 NSString* nstext = [pboard stringForType:NSStringPboardType];
49 NSString* nstext = [pboard stringForType:NSPasteboardTypeString];
50 #endif // MAC_OS_X_VERSION_MIN_REQUIRED
51 return stdStringFromCocoa(nstext);
58 * Set clipboard contents as text
60 virtual bool setText(const std::string& text, Type type)
62 CocoaAutoreleasePool pool;
64 if( type == CLIPBOARD )
66 NSPasteboard* pboard = [NSPasteboard generalPasteboard];
67 NSString* nstext = stdStringToCocoa(text);
68 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
69 NSString* type = NSStringPboardType;
70 NSArray* types = [NSArray arrayWithObjects: type, nil];
71 [pboard declareTypes:types owner:nil];
72 [pboard setString:nstext forType: NSStringPboardType];
74 NSString* type = NSPasteboardTypeString;
75 [pboard clearContents];
76 [pboard setString:nstext forType:NSPasteboardTypeString];
77 #endif // MAC_OS_X_VERSION_MIN_REQUIRED
86 std::string _selection;
89 //------------------------------------------------------------------------------
90 NasalClipboard::Ptr NasalClipboard::create()
92 return NasalClipboard::Ptr(new ClipboardCocoa);