src/video/cocoa/SDL_cocoaclipboard.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 01 Jan 2017 18:33:28 -0800
changeset 10737 3406a0f8b041
parent 10177 faa36f2de933
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Updated copyright for 2017
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../../SDL_internal.h"
    22 
    23 #if SDL_VIDEO_DRIVER_COCOA
    24 
    25 #include "SDL_cocoavideo.h"
    26 #include "../../events/SDL_clipboardevents_c.h"
    27 
    28 int
    29 Cocoa_SetClipboardText(_THIS, const char *text)
    30 { @autoreleasepool
    31 {
    32     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    33     NSPasteboard *pasteboard;
    34     NSString *format = NSPasteboardTypeString;
    35 
    36     pasteboard = [NSPasteboard generalPasteboard];
    37     data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
    38     [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
    39 
    40     return 0;
    41 }}
    42 
    43 char *
    44 Cocoa_GetClipboardText(_THIS)
    45 { @autoreleasepool
    46 {
    47     NSPasteboard *pasteboard;
    48     NSString *format = NSPasteboardTypeString;
    49     NSString *available;
    50     char *text;
    51 
    52     pasteboard = [NSPasteboard generalPasteboard];
    53     available = [pasteboard availableTypeFromArray:[NSArray arrayWithObject:format]];
    54     if ([available isEqualToString:format]) {
    55         NSString* string;
    56         const char *utf8;
    57 
    58         string = [pasteboard stringForType:format];
    59         if (string == nil) {
    60             utf8 = "";
    61         } else {
    62             utf8 = [string UTF8String];
    63         }
    64         text = SDL_strdup(utf8);
    65     } else {
    66         text = SDL_strdup("");
    67     }
    68 
    69     return text;
    70 }}
    71 
    72 SDL_bool
    73 Cocoa_HasClipboardText(_THIS)
    74 {
    75     SDL_bool result = SDL_FALSE;
    76     char *text = Cocoa_GetClipboardText(_this);
    77     if (text) {
    78         result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
    79         SDL_free(text);
    80     }
    81     return result;
    82 }
    83 
    84 void
    85 Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
    86 { @autoreleasepool
    87 {
    88     NSPasteboard *pasteboard;
    89     NSInteger count;
    90 
    91     pasteboard = [NSPasteboard generalPasteboard];
    92     count = [pasteboard changeCount];
    93     if (count != data->clipboard_count) {
    94         if (data->clipboard_count) {
    95             SDL_SendClipboardUpdate();
    96         }
    97         data->clipboard_count = count;
    98     }
    99 }}
   100 
   101 #endif /* SDL_VIDEO_DRIVER_COCOA */
   102 
   103 /* vi: set ts=4 sw=4 expandtab: */