]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/ClipboardCocoa.mm
Minor main loop/init clean-up
[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 #include "NasalClipboard.hxx"
20
21 #include <simgear/debug/logstream.hxx>
22
23 #include <Cocoa/Cocoa.h>
24
25 namespace {
26   class CocoaAutoreleasePool
27   {
28   public:
29     CocoaAutoreleasePool()
30     {
31       pool = [[NSAutoreleasePool alloc] init];
32     }
33     
34     ~CocoaAutoreleasePool()
35     {
36       [pool release];
37     }
38     
39   private:
40     NSAutoreleasePool* pool;
41   };
42 }
43
44 static NSString* stdStringToCocoa(const std::string& s)
45 {
46   return [NSString stringWithUTF8String:s.c_str()];
47 }
48
49 static std::string stdStringFromCocoa(NSString* s)
50 {
51   return std::string([s UTF8String]);
52 }
53
54 /**
55  */
56 class ClipboardCocoa: public NasalClipboard
57 {
58   public:
59
60     /**
61      * Get clipboard contents as text
62      */
63     virtual std::string getText(Type type)
64     {
65       CocoaAutoreleasePool pool;
66       
67       if( type == CLIPBOARD )
68       {
69         NSPasteboard* pboard = [NSPasteboard generalPasteboard];
70         NSString* nstext = [pboard stringForType:NSPasteboardTypeString];
71         return stdStringFromCocoa(nstext);
72       }
73       
74       return "";
75     }
76
77     /**
78      * Set clipboard contents as text
79      */
80     virtual bool setText(const std::string& text, Type type)
81     {
82       CocoaAutoreleasePool pool;
83       
84       if( type == CLIPBOARD )
85       {
86         NSPasteboard* pboard = [NSPasteboard generalPasteboard];
87         NSString* nstext = stdStringToCocoa(text);
88         [pboard clearContents];
89         [pboard setString:nstext forType:NSPasteboardTypeString];
90         return true;
91       }
92       
93       return false;
94     }
95
96   protected:
97
98     std::string _selection;
99 };
100
101 //------------------------------------------------------------------------------
102 NasalClipboard::Ptr NasalClipboard::create()
103 {
104   return NasalClipboard::Ptr(new ClipboardCocoa);
105 }