Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
osx_pasteboard.mm
Go to the documentation of this file.
1 /* Copyright (c) 2012 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #import <AppKit/AppKit.h>
24 #import <AvailabilityMacros.h> // MAC_OS_X_VERSION_MIN_REQUIRED
25 #import <Foundation/Foundation.h>
26 #import <string>
27 
28 #import "osx_pasteboard.h"
29 
30 bool osx_GetStringFromPasteboard(std::string& out)
31 {
32  NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
33  NSString* string = nil;
34 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
35  // As of 10.6, pasteboards can hold multiple items
36  NSArray* classes = [NSArray arrayWithObjects:[NSString class], nil];
37  NSDictionary* options = [NSDictionary dictionary];
38  NSArray* copiedItems = [pasteboard readObjectsForClasses:classes options:options];
39  if (copiedItems != nil && [copiedItems count] > 0)
40  // We only need to support a single item, so grab the first string
41  string = [copiedItems objectAtIndex:0];
42  else
43  return false; // No strings found on pasteboard
44 #else // 10.5
45  // Verify that there is a string available for us
46  NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil];
47  if ([pasteboard availableTypeFromArray:types] != nil)
48  string = [pasteboard stringForType:NSStringPboardType];
49  else
50  return false; // No strings found on pasteboard
51 #endif // MAC_OS_X_VERSION_MIN_REQUIRED
52 
53  if (string != nil)
54  out = std::string([string UTF8String]);
55  else
56  return false; // fail
57 
58  return true; // success
59 }
60 
61 bool osx_SendStringToPasteboard(const std::string& string)
62 {
63  // We're only working with strings, so we don't need to lazily write
64  // anything (otherwise we'd need to set up an owner and data provider)
65  NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
66 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
67  NSString* type = NSPasteboardTypeString;
68  [pasteboard clearContents];
69 #else // 10.5
70  NSString* type = NSStringPboardType;
71  NSArray* types = [NSArray arrayWithObjects: type, nil];
72  // Roughly equivalent to clearContents followed by addTypes:owner
73  [pasteboard declareTypes:types owner:nil];
74 #endif // MAC_OS_X_VERSION_MIN_REQUIRED
75 
76  // May raise a NSPasteboardCommunicationException
77  BOOL ok = [pasteboard setString:[NSString stringWithUTF8String:string.c_str()] forType:type];
78  return ok == YES;
79 }
C++ interface to Cocoa implementation for pasteboards.
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:419
int BOOL
Definition: wgl.h:51
bool osx_GetStringFromPasteboard(std::string &out)
Get a string from the pasteboard.
bool osx_SendStringToPasteboard(const std::string &string)
Store a string on the pasteboard.