src/video/cocoa/SDL_cocoaclipboard.m
author Ryan C. Gordon
Thu, 25 Aug 2011 03:11:28 -0400
changeset 5646 fe1df351ca3d
parent 5535 96594ac5fd1a
child 6036 f2a89f2a6157
permissions -rw-r--r--
Removed the MAC_OS_X_VERSION_10_x macros from the 1.3 branch.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #include "SDL_cocoavideo.h"
    24 #include "../../events/SDL_clipboardevents_c.h"
    25 
    26 static NSString *
    27 GetTextFormat(_THIS)
    28 {
    29 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
    30     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    31 
    32     if (data->osversion >= 0x1060) {
    33         return NSPasteboardTypeString;
    34     } else {
    35         return NSStringPboardType;
    36     }
    37 #else
    38     return NSStringPboardType;
    39 #endif
    40 }
    41 
    42 int
    43 Cocoa_SetClipboardText(_THIS, const char *text)
    44 {
    45     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    46     NSAutoreleasePool *pool;
    47     NSPasteboard *pasteboard;
    48     NSString *format = GetTextFormat(_this);
    49 
    50     pool = [[NSAutoreleasePool alloc] init];
    51 
    52     pasteboard = [NSPasteboard generalPasteboard];
    53     data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
    54     [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
    55 
    56     [pool release];
    57 
    58     return 0;
    59 }
    60 
    61 char *
    62 Cocoa_GetClipboardText(_THIS)
    63 {
    64     NSAutoreleasePool *pool;
    65     NSPasteboard *pasteboard;
    66     NSString *format = GetTextFormat(_this);
    67     NSString *available;
    68     char *text;
    69 
    70     pool = [[NSAutoreleasePool alloc] init];
    71 
    72     pasteboard = [NSPasteboard generalPasteboard];
    73     available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
    74     if ([available isEqualToString:format]) {
    75         NSString* string;
    76         const char *utf8;
    77 
    78         string = [pasteboard stringForType:format];
    79         if (string == nil) {
    80             utf8 = "";
    81         } else {
    82             utf8 = [string UTF8String];
    83         }
    84         text = SDL_strdup(utf8);
    85     } else {
    86         text = SDL_strdup("");
    87     }
    88 
    89     [pool release];
    90 
    91     return text;
    92 }
    93 
    94 SDL_bool
    95 Cocoa_HasClipboardText(_THIS)
    96 {
    97     NSAutoreleasePool *pool;
    98     NSPasteboard *pasteboard;
    99     NSString *format = GetTextFormat(_this);
   100     NSString *available;
   101     SDL_bool result;
   102 
   103     pool = [[NSAutoreleasePool alloc] init];
   104 
   105     pasteboard = [NSPasteboard generalPasteboard];
   106     available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
   107     if ([available isEqualToString:format]) {
   108         result = SDL_TRUE;
   109     } else {
   110         result = SDL_FALSE;
   111     }
   112 
   113     [pool release];
   114 
   115     return result;
   116 }
   117 
   118 void
   119 Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
   120 {
   121     NSAutoreleasePool *pool;
   122     NSPasteboard *pasteboard;
   123     NSInteger count;
   124 
   125     pool = [[NSAutoreleasePool alloc] init];
   126 
   127     pasteboard = [NSPasteboard generalPasteboard];
   128     count = [pasteboard changeCount];
   129     if (count != data->clipboard_count) {
   130         if (data->clipboard_count) {
   131             SDL_SendClipboardUpdate();
   132         }
   133         data->clipboard_count = count;
   134     }
   135 
   136     [pool release];
   137 }
   138 
   139 /* vi: set ts=4 sw=4 expandtab: */