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