src/video/cocoa/SDL_cocoaclipboard.m
author stopiccot
Sun, 06 Jan 2013 19:04:53 +0300
changeset 6832 156e608ec4ef
parent 6138 4c64952a58fb
child 6848 478ecc8a58b3
permissions -rw-r--r--
Replaced manual NSAutoreleasePool handing with @autorelease
slouken@4499
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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@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
{
icculus@5646
    31
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
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@4925
    39
#else
slouken@4925
    40
    return NSStringPboardType;
slouken@4501
    41
#endif
slouken@4501
    42
}
slouken@4499
    43
slouken@4499
    44
int
slouken@4499
    45
Cocoa_SetClipboardText(_THIS, const char *text)
slouken@4499
    46
{
slouken@4503
    47
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
alexey@6832
    48
slouken@4504
    49
    NSPasteboard *pasteboard;
slouken@4501
    50
    NSString *format = GetTextFormat(_this);
slouken@4499
    51
alexey@6832
    52
    @autoreleasepool {
alexey@6832
    53
        pasteboard = [NSPasteboard generalPasteboard];
alexey@6832
    54
        data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
alexey@6832
    55
        [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
alexey@6832
    56
    }
alexey@6832
    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@4504
    64
    NSPasteboard *pasteboard;
slouken@4501
    65
    NSString *format = GetTextFormat(_this);
slouken@4499
    66
    NSString *available;
slouken@4499
    67
    char *text;
slouken@4499
    68
alexey@6832
    69
    @autoreleasepool {
alexey@6832
    70
        pasteboard = [NSPasteboard generalPasteboard];
alexey@6832
    71
        available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
alexey@6832
    72
        if ([available isEqualToString:format]) {
alexey@6832
    73
            NSString* string;
alexey@6832
    74
            const char *utf8;
slouken@4499
    75
alexey@6832
    76
            string = [pasteboard stringForType:format];
alexey@6832
    77
            if (string == nil) {
alexey@6832
    78
                utf8 = "";
alexey@6832
    79
            } else {
alexey@6832
    80
                utf8 = [string UTF8String];
alexey@6832
    81
            }
alexey@6832
    82
            text = SDL_strdup(utf8);
slouken@4499
    83
        } else {
alexey@6832
    84
            text = SDL_strdup("");
slouken@4499
    85
        }
slouken@4499
    86
    }
alexey@6832
    87
    
slouken@4499
    88
    return text;
slouken@4499
    89
}
slouken@4499
    90
slouken@4499
    91
SDL_bool
slouken@4499
    92
Cocoa_HasClipboardText(_THIS)
slouken@4499
    93
{
aschiffler@6036
    94
    SDL_bool result = SDL_FALSE;
aschiffler@6036
    95
    char *text = Cocoa_GetClipboardText(_this);
aschiffler@6036
    96
    if (text) {
aschiffler@6036
    97
	result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
aschiffler@6036
    98
	SDL_free(text);
slouken@4499
    99
    }
slouken@4499
   100
    return result;
slouken@4499
   101
}
slouken@4499
   102
slouken@4503
   103
void
slouken@4503
   104
Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
slouken@4503
   105
{
slouken@4504
   106
    NSPasteboard *pasteboard;
slouken@4503
   107
    NSInteger count;
slouken@4503
   108
alexey@6832
   109
    @autoreleasepool {
alexey@6832
   110
        pasteboard = [NSPasteboard generalPasteboard];
alexey@6832
   111
        count = [pasteboard changeCount];
alexey@6832
   112
        if (count != data->clipboard_count) {
alexey@6832
   113
            if (data->clipboard_count) {
alexey@6832
   114
                SDL_SendClipboardUpdate();
alexey@6832
   115
            }
alexey@6832
   116
            data->clipboard_count = count;
slouken@4503
   117
        }
slouken@4503
   118
    }
slouken@4503
   119
}
slouken@4503
   120
slouken@6044
   121
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   122
slouken@4499
   123
/* vi: set ts=4 sw=4 expandtab: */