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