]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/ClipboardCocoa.mm
Reset: Nasal can be shutdown.
[flightgear.git] / src / Scripting / ClipboardCocoa.mm
1 // Cocoa implementation of clipboard access for Nasal
2 //
3 // Copyright (C) 2012  James Turner <zakalawe@mac.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
20
21 #include "NasalClipboard.hxx"
22
23 #include <simgear/debug/logstream.hxx>
24
25 #include <Cocoa/Cocoa.h>
26
27 namespace {
28   class CocoaAutoreleasePool
29   {
30   public:
31     CocoaAutoreleasePool()
32     {
33       pool = [[NSAutoreleasePool alloc] init];
34     }
35     
36     ~CocoaAutoreleasePool()
37     {
38       [pool release];
39     }
40     
41   private:
42     NSAutoreleasePool* pool;
43   };
44 }
45
46 static NSString* stdStringToCocoa(const std::string& s)
47 {
48   return [NSString stringWithUTF8String:s.c_str()];
49 }
50
51 static std::string stdStringFromCocoa(NSString* s)
52 {
53   return std::string([s UTF8String]);
54 }
55
56 /**
57  */
58 class ClipboardCocoa: public NasalClipboard
59 {
60   public:
61
62     /**
63      * Get clipboard contents as text
64      */
65     virtual std::string getText(Type type)
66     {
67       CocoaAutoreleasePool pool;
68       
69       if( type == CLIPBOARD )
70       {
71        NSPasteboard* pboard = [NSPasteboard generalPasteboard];
72        #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
73          NSString* nstext = [pboard stringForType:NSStringPboardType];
74        #else // > 10.5
75          NSString* nstext = [pboard stringForType:NSPasteboardTypeString];
76        #endif // MAC_OS_X_VERSION_MIN_REQUIRED
77        return stdStringFromCocoa(nstext);
78       }
79       
80       return "";
81     }
82
83     /**
84      * Set clipboard contents as text
85      */
86     virtual bool setText(const std::string& text, Type type)
87     {
88       CocoaAutoreleasePool pool;
89       
90       if( type == CLIPBOARD )
91       {
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];
99         #else // > 10.5
100           NSString* type = NSPasteboardTypeString;
101           [pboard clearContents];
102           [pboard setString:nstext forType:NSPasteboardTypeString];
103         #endif // MAC_OS_X_VERSION_MIN_REQUIRED
104         return true;
105       }
106       
107       return false;
108     }
109
110   protected:
111
112     std::string _selection;
113 };
114
115 //------------------------------------------------------------------------------
116 NasalClipboard::Ptr NasalClipboard::create()
117 {
118   return NasalClipboard::Ptr(new ClipboardCocoa);
119 }