]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/ClipboardCocoa.mm
Canvas: update for new bounding box getters.
[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 #include <GUI/CocoaHelpers_private.h>
28
29 /**
30  */
31 class ClipboardCocoa: public NasalClipboard
32 {
33   public:
34
35     /**
36      * Get clipboard contents as text
37      */
38     virtual std::string getText(Type type)
39     {
40       CocoaAutoreleasePool pool;
41       
42       if( type == CLIPBOARD )
43       {
44        NSPasteboard* pboard = [NSPasteboard generalPasteboard];
45        #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
46          NSString* nstext = [pboard stringForType:NSStringPboardType];
47        #else // > 10.5
48          NSString* nstext = [pboard stringForType:NSPasteboardTypeString];
49        #endif // MAC_OS_X_VERSION_MIN_REQUIRED
50        return stdStringFromCocoa(nstext);
51       }
52       
53       return "";
54     }
55
56     /**
57      * Set clipboard contents as text
58      */
59     virtual bool setText(const std::string& text, Type type)
60     {
61       CocoaAutoreleasePool pool;
62       
63       if( type == CLIPBOARD )
64       {
65         NSPasteboard* pboard = [NSPasteboard generalPasteboard];
66         NSString* nstext = stdStringToCocoa(text);
67         #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
68           NSString* type = NSStringPboardType;
69           NSArray* types = [NSArray arrayWithObjects: type, nil];
70           [pboard declareTypes:types owner:nil];
71           [pboard setString:nstext forType: NSStringPboardType];
72         #else // > 10.5
73           NSString* type = NSPasteboardTypeString;
74           [pboard clearContents];
75           [pboard setString:nstext forType:NSPasteboardTypeString];
76         #endif // MAC_OS_X_VERSION_MIN_REQUIRED
77         return true;
78       }
79       
80       return false;
81     }
82
83   protected:
84
85     std::string _selection;
86 };
87
88 //------------------------------------------------------------------------------
89 NasalClipboard::Ptr NasalClipboard::create()
90 {
91   return NasalClipboard::Ptr(new ClipboardCocoa);
92 }