src/video/cocoa/SDL_cocoaclipboard.m
author Sam Lantinga
Thu, 08 Jul 2010 06:16:27 -0700
changeset 4501 0cf025066b6f
parent 4499 c2ebe3e020c6
child 4503 524dfefd554c
permissions -rw-r--r--
Updated Xcode project files for clipboard API
Fixed generation of SDL_revision.h when hg is in the path set up in .bash_profile
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_cocoavideo.h"
    25 
    26 static NSString *
    27 GetTextFormat(_THIS)
    28 {
    29 #if MAC_OS_X_VERSION_MAX_ALLOWED < 1060
    30     return NSStringPboardType;
    31 #else
    32     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    33 
    34     if (data->osversion >= 0x1060) {
    35         return NSPasteboardTypeString;
    36     } else {
    37         return NSStringPboardType;
    38     }
    39 #endif
    40 }
    41 
    42 int
    43 Cocoa_SetClipboardText(_THIS, const char *text)
    44 {
    45     NSAutoreleasePool *pool;
    46 	NSPasteboard *pasteboard;
    47     NSString *format = GetTextFormat(_this);
    48 
    49     pool = [[NSAutoreleasePool alloc] init];
    50 
    51     pasteboard = [NSPasteboard generalPasteboard];
    52     [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
    53     [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
    54 
    55     [pool release];
    56 
    57     return 0;
    58 }
    59 
    60 char *
    61 Cocoa_GetClipboardText(_THIS)
    62 {
    63     NSAutoreleasePool *pool;
    64 	NSPasteboard *pasteboard;
    65     NSString *format = GetTextFormat(_this);
    66     NSString *available;
    67     char *text;
    68 
    69     pool = [[NSAutoreleasePool alloc] init];
    70 
    71     pasteboard = [NSPasteboard generalPasteboard];
    72     available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
    73     if ([available isEqualToString:format]) {
    74         NSString* string;
    75         const char *utf8;
    76 
    77         string = [pasteboard stringForType:format];
    78         if (string == nil) {
    79             utf8 = "";
    80         } else {
    81             utf8 = [string UTF8String];
    82         }
    83         text = SDL_strdup(utf8);
    84     } else {
    85         text = SDL_strdup("");
    86     }
    87 
    88     [pool release];
    89 
    90     return text;
    91 }
    92 
    93 SDL_bool
    94 Cocoa_HasClipboardText(_THIS)
    95 {
    96     NSAutoreleasePool *pool;
    97 	NSPasteboard *pasteboard;
    98     NSString *format = GetTextFormat(_this);
    99     NSString *available;
   100     SDL_bool result;
   101 
   102     pool = [[NSAutoreleasePool alloc] init];
   103 
   104     pasteboard = [NSPasteboard generalPasteboard];
   105     available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
   106     if ([available isEqualToString:format]) {
   107         result = SDL_TRUE;
   108     } else {
   109         result = SDL_FALSE;
   110     }
   111 
   112     [pool release];
   113 
   114     return result;
   115 }
   116 
   117 /* vi: set ts=4 sw=4 expandtab: */