src/video/cocoa/SDL_cocoaclipboard.m
author Sam Lantinga
Thu, 08 Jul 2010 00:35:58 -0700
changeset 4499 c2ebe3e020c6
child 4501 0cf025066b6f
permissions -rw-r--r--
Added Mac OS X implementation of clipboard support
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@4499
    26
slouken@4499
    27
int
slouken@4499
    28
Cocoa_SetClipboardText(_THIS, const char *text)
slouken@4499
    29
{
slouken@4499
    30
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4499
    31
    NSAutoreleasePool *pool;
slouken@4499
    32
	NSPasteboard *pasteboard;
slouken@4499
    33
    NSString *format;
slouken@4499
    34
slouken@4499
    35
    if (data->osversion >= 0x1060) {
slouken@4499
    36
        format = NSPasteboardTypeString;
slouken@4499
    37
    } else {
slouken@4499
    38
        format = NSStringPboardType;
slouken@4499
    39
    }
slouken@4499
    40
slouken@4499
    41
    pool = [[NSAutoreleasePool alloc] init];
slouken@4499
    42
slouken@4499
    43
    pasteboard = [NSPasteboard generalPasteboard];
slouken@4499
    44
    [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
slouken@4499
    45
    [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
slouken@4499
    46
slouken@4499
    47
    [pool release];
slouken@4499
    48
slouken@4499
    49
    return 0;
slouken@4499
    50
}
slouken@4499
    51
slouken@4499
    52
char *
slouken@4499
    53
Cocoa_GetClipboardText(_THIS)
slouken@4499
    54
{
slouken@4499
    55
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4499
    56
    NSAutoreleasePool *pool;
slouken@4499
    57
	NSPasteboard *pasteboard;
slouken@4499
    58
    NSString *format;
slouken@4499
    59
    NSString *available;
slouken@4499
    60
    char *text;
slouken@4499
    61
slouken@4499
    62
    if (data->osversion >= 0x1060) {
slouken@4499
    63
        format = NSPasteboardTypeString;
slouken@4499
    64
    } else {
slouken@4499
    65
        format = NSStringPboardType;
slouken@4499
    66
    }
slouken@4499
    67
slouken@4499
    68
    pool = [[NSAutoreleasePool alloc] init];
slouken@4499
    69
slouken@4499
    70
    pasteboard = [NSPasteboard generalPasteboard];
slouken@4499
    71
    available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
slouken@4499
    72
    if ([available isEqualToString:format]) {
slouken@4499
    73
        NSString* string;
slouken@4499
    74
        const char *utf8;
slouken@4499
    75
slouken@4499
    76
        string = [pasteboard stringForType:format];
slouken@4499
    77
        if (string == nil) {
slouken@4499
    78
            utf8 = "";
slouken@4499
    79
        } else {
slouken@4499
    80
            utf8 = [string UTF8String];
slouken@4499
    81
        }
slouken@4499
    82
        text = SDL_strdup(utf8);
slouken@4499
    83
    } else {
slouken@4499
    84
        text = SDL_strdup("");
slouken@4499
    85
    }
slouken@4499
    86
slouken@4499
    87
    [pool release];
slouken@4499
    88
slouken@4499
    89
    return text;
slouken@4499
    90
}
slouken@4499
    91
slouken@4499
    92
SDL_bool
slouken@4499
    93
Cocoa_HasClipboardText(_THIS)
slouken@4499
    94
{
slouken@4499
    95
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4499
    96
    NSAutoreleasePool *pool;
slouken@4499
    97
	NSPasteboard *pasteboard;
slouken@4499
    98
    NSString *format;
slouken@4499
    99
    NSString *available;
slouken@4499
   100
    SDL_bool result;
slouken@4499
   101
slouken@4499
   102
    if (data->osversion >= 0x1060) {
slouken@4499
   103
        format = NSPasteboardTypeString;
slouken@4499
   104
    } else {
slouken@4499
   105
        format = NSStringPboardType;
slouken@4499
   106
    }
slouken@4499
   107
slouken@4499
   108
    pool = [[NSAutoreleasePool alloc] init];
slouken@4499
   109
slouken@4499
   110
    pasteboard = [NSPasteboard generalPasteboard];
slouken@4499
   111
    available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
slouken@4499
   112
    if ([available isEqualToString:format]) {
slouken@4499
   113
        result = SDL_TRUE;
slouken@4499
   114
    } else {
slouken@4499
   115
        result = SDL_FALSE;
slouken@4499
   116
    }
slouken@4499
   117
slouken@4499
   118
    [pool release];
slouken@4499
   119
slouken@4499
   120
    return result;
slouken@4499
   121
}
slouken@4499
   122
slouken@4499
   123
/* vi: set ts=4 sw=4 expandtab: */