src/video/cocoa/SDL_cocoaclipboard.m
author Sam Lantinga
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8093 b43765095a6f
child 8986 1c81316ac642
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

Is it that time already??
slouken@4499
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@4499
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_COCOA
slouken@6044
    24
slouken@4499
    25
#include "SDL_cocoavideo.h"
slouken@4503
    26
#include "../../events/SDL_clipboardevents_c.h"
slouken@4499
    27
slouken@4501
    28
static NSString *
slouken@4501
    29
GetTextFormat(_THIS)
slouken@4501
    30
{
slouken@4501
    31
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4501
    32
slouken@4501
    33
    if (data->osversion >= 0x1060) {
slouken@4501
    34
        return NSPasteboardTypeString;
slouken@4501
    35
    } else {
slouken@4501
    36
        return NSStringPboardType;
slouken@4501
    37
    }
slouken@4501
    38
}
slouken@4499
    39
slouken@4499
    40
int
slouken@4499
    41
Cocoa_SetClipboardText(_THIS, const char *text)
slouken@4499
    42
{
slouken@4503
    43
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@6848
    44
    NSAutoreleasePool *pool;
slouken@4504
    45
    NSPasteboard *pasteboard;
slouken@4501
    46
    NSString *format = GetTextFormat(_this);
slouken@4499
    47
slouken@6848
    48
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
    49
slouken@6848
    50
    pasteboard = [NSPasteboard generalPasteboard];
slouken@6848
    51
    data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
slouken@6848
    52
    [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
slouken@6848
    53
slouken@6848
    54
    [pool release];
slouken@6848
    55
slouken@4499
    56
    return 0;
slouken@4499
    57
}
slouken@4499
    58
slouken@4499
    59
char *
slouken@4499
    60
Cocoa_GetClipboardText(_THIS)
slouken@4499
    61
{
slouken@6848
    62
    NSAutoreleasePool *pool;
slouken@4504
    63
    NSPasteboard *pasteboard;
slouken@4501
    64
    NSString *format = GetTextFormat(_this);
slouken@4499
    65
    NSString *available;
slouken@4499
    66
    char *text;
slouken@4499
    67
slouken@6848
    68
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
    69
slouken@6848
    70
    pasteboard = [NSPasteboard generalPasteboard];
slouken@6848
    71
    available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
slouken@6848
    72
    if ([available isEqualToString:format]) {
slouken@6848
    73
        NSString* string;
slouken@6848
    74
        const char *utf8;
slouken@4499
    75
slouken@6848
    76
        string = [pasteboard stringForType:format];
slouken@6848
    77
        if (string == nil) {
slouken@6848
    78
            utf8 = "";
slouken@4499
    79
        } else {
slouken@6848
    80
            utf8 = [string UTF8String];
slouken@4499
    81
        }
slouken@6848
    82
        text = SDL_strdup(utf8);
slouken@6848
    83
    } else {
slouken@6848
    84
        text = SDL_strdup("");
slouken@4499
    85
    }
slouken@6848
    86
slouken@6848
    87
    [pool release];
slouken@6848
    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
{
aschiffler@6036
    95
    SDL_bool result = SDL_FALSE;
aschiffler@6036
    96
    char *text = Cocoa_GetClipboardText(_this);
aschiffler@6036
    97
    if (text) {
slouken@7721
    98
        result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
slouken@7191
    99
    SDL_free(text);
slouken@4499
   100
    }
slouken@4499
   101
    return result;
slouken@4499
   102
}
slouken@4499
   103
slouken@4503
   104
void
slouken@4503
   105
Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
slouken@4503
   106
{
slouken@6848
   107
    NSAutoreleasePool *pool;
slouken@4504
   108
    NSPasteboard *pasteboard;
slouken@4503
   109
    NSInteger count;
slouken@4503
   110
slouken@6848
   111
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   112
slouken@6848
   113
    pasteboard = [NSPasteboard generalPasteboard];
slouken@6848
   114
    count = [pasteboard changeCount];
slouken@6848
   115
    if (count != data->clipboard_count) {
slouken@6848
   116
        if (data->clipboard_count) {
slouken@6848
   117
            SDL_SendClipboardUpdate();
slouken@4503
   118
        }
slouken@6848
   119
        data->clipboard_count = count;
slouken@4503
   120
    }
slouken@6848
   121
slouken@6848
   122
    [pool release];
slouken@4503
   123
}
slouken@4503
   124
slouken@6044
   125
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   126
slouken@4499
   127
/* vi: set ts=4 sw=4 expandtab: */