src/video/cocoa/SDL_cocoaclipboard.m
author Andreas Schiffler
Sat, 29 Oct 2011 23:43:59 -0700
changeset 6036 f2a89f2a6157
parent 5646 fe1df351ca3d
child 6044 35448a5ea044
permissions -rw-r--r--
Update SDL_HasClipboardText functions to return value based on clipboard content; Fix memory leak in fallback SetClipboard implementation
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 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_config.h"
    22 
    23 #include "SDL_cocoavideo.h"
    24 #include "../../events/SDL_clipboardevents_c.h"
    25 
    26 static NSString *
    27 GetTextFormat(_THIS)
    28 {
    29 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
    30     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    31 
    32     if (data->osversion >= 0x1060) {
    33         return NSPasteboardTypeString;
    34     } else {
    35         return NSStringPboardType;
    36     }
    37 #else
    38     return NSStringPboardType;
    39 #endif
    40 }
    41 
    42 int
    43 Cocoa_SetClipboardText(_THIS, const char *text)
    44 {
    45     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    46     NSAutoreleasePool *pool;
    47     NSPasteboard *pasteboard;
    48     NSString *format = GetTextFormat(_this);
    49 
    50     pool = [[NSAutoreleasePool alloc] init];
    51 
    52     pasteboard = [NSPasteboard generalPasteboard];
    53     data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
    54     [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
    55 
    56     [pool release];
    57 
    58     return 0;
    59 }
    60 
    61 char *
    62 Cocoa_GetClipboardText(_THIS)
    63 {
    64     NSAutoreleasePool *pool;
    65     NSPasteboard *pasteboard;
    66     NSString *format = GetTextFormat(_this);
    67     NSString *available;
    68     char *text;
    69 
    70     pool = [[NSAutoreleasePool alloc] init];
    71 
    72     pasteboard = [NSPasteboard generalPasteboard];
    73     available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
    74     if ([available isEqualToString:format]) {
    75         NSString* string;
    76         const char *utf8;
    77 
    78         string = [pasteboard stringForType:format];
    79         if (string == nil) {
    80             utf8 = "";
    81         } else {
    82             utf8 = [string UTF8String];
    83         }
    84         text = SDL_strdup(utf8);
    85     } else {
    86         text = SDL_strdup("");
    87     }
    88 
    89     [pool release];
    90 
    91     return text;
    92 }
    93 
    94 SDL_bool
    95 Cocoa_HasClipboardText(_THIS)
    96 {
    97     SDL_bool result = SDL_FALSE;
    98     char *text = Cocoa_GetClipboardText(_this);
    99     if (text) {
   100 	result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
   101 	SDL_free(text);
   102     }
   103     return result;
   104 }
   105 
   106 void
   107 Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
   108 {
   109     NSAutoreleasePool *pool;
   110     NSPasteboard *pasteboard;
   111     NSInteger count;
   112 
   113     pool = [[NSAutoreleasePool alloc] init];
   114 
   115     pasteboard = [NSPasteboard generalPasteboard];
   116     count = [pasteboard changeCount];
   117     if (count != data->clipboard_count) {
   118         if (data->clipboard_count) {
   119             SDL_SendClipboardUpdate();
   120         }
   121         data->clipboard_count = count;
   122     }
   123 
   124     [pool release];
   125 }
   126 
   127 /* vi: set ts=4 sw=4 expandtab: */