Added Mac OS X implementation of clipboard support
authorSam Lantinga
Thu, 08 Jul 2010 00:35:58 -0700
changeset 4499c2ebe3e020c6
parent 4498 3d91e31fcf71
child 4500 eff4e88cc1e8
Added Mac OS X implementation of clipboard support
src/video/cocoa/SDL_cocoaclipboard.h
src/video/cocoa/SDL_cocoaclipboard.m
src/video/cocoa/SDL_cocoavideo.h
src/video/cocoa/SDL_cocoavideo.m
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/cocoa/SDL_cocoaclipboard.h	Thu Jul 08 00:35:58 2010 -0700
     1.3 @@ -0,0 +1,33 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#ifndef _SDL_cocoaclipboard_h
    1.28 +#define _SDL_cocoaclipboard_h
    1.29 +
    1.30 +extern int Cocoa_SetClipboardText(_THIS, const char *text);
    1.31 +extern char *Cocoa_GetClipboardText(_THIS);
    1.32 +extern SDL_bool Cocoa_HasClipboardText(_THIS);
    1.33 +
    1.34 +#endif /* _SDL_cocoaclipboard_h */
    1.35 +
    1.36 +/* vi: set ts=4 sw=4 expandtab: */
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/video/cocoa/SDL_cocoaclipboard.m	Thu Jul 08 00:35:58 2010 -0700
     2.3 @@ -0,0 +1,123 @@
     2.4 +/*
     2.5 +    SDL - Simple DirectMedia Layer
     2.6 +    Copyright (C) 1997-2010 Sam Lantinga
     2.7 +
     2.8 +    This library is free software; you can redistribute it and/or
     2.9 +    modify it under the terms of the GNU Lesser General Public
    2.10 +    License as published by the Free Software Foundation; either
    2.11 +    version 2.1 of the License, or (at your option) any later version.
    2.12 +
    2.13 +    This library is distributed in the hope that it will be useful,
    2.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.16 +    Lesser General Public License for more details.
    2.17 +
    2.18 +    You should have received a copy of the GNU Lesser General Public
    2.19 +    License along with this library; if not, write to the Free Software
    2.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2.21 +
    2.22 +    Sam Lantinga
    2.23 +    slouken@libsdl.org
    2.24 +*/
    2.25 +#include "SDL_config.h"
    2.26 +
    2.27 +#include "SDL_cocoavideo.h"
    2.28 +
    2.29 +
    2.30 +int
    2.31 +Cocoa_SetClipboardText(_THIS, const char *text)
    2.32 +{
    2.33 +    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    2.34 +    NSAutoreleasePool *pool;
    2.35 +	NSPasteboard *pasteboard;
    2.36 +    NSString *format;
    2.37 +
    2.38 +    if (data->osversion >= 0x1060) {
    2.39 +        format = NSPasteboardTypeString;
    2.40 +    } else {
    2.41 +        format = NSStringPboardType;
    2.42 +    }
    2.43 +
    2.44 +    pool = [[NSAutoreleasePool alloc] init];
    2.45 +
    2.46 +    pasteboard = [NSPasteboard generalPasteboard];
    2.47 +    [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
    2.48 +    [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
    2.49 +
    2.50 +    [pool release];
    2.51 +
    2.52 +    return 0;
    2.53 +}
    2.54 +
    2.55 +char *
    2.56 +Cocoa_GetClipboardText(_THIS)
    2.57 +{
    2.58 +    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    2.59 +    NSAutoreleasePool *pool;
    2.60 +	NSPasteboard *pasteboard;
    2.61 +    NSString *format;
    2.62 +    NSString *available;
    2.63 +    char *text;
    2.64 +
    2.65 +    if (data->osversion >= 0x1060) {
    2.66 +        format = NSPasteboardTypeString;
    2.67 +    } else {
    2.68 +        format = NSStringPboardType;
    2.69 +    }
    2.70 +
    2.71 +    pool = [[NSAutoreleasePool alloc] init];
    2.72 +
    2.73 +    pasteboard = [NSPasteboard generalPasteboard];
    2.74 +    available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
    2.75 +    if ([available isEqualToString:format]) {
    2.76 +        NSString* string;
    2.77 +        const char *utf8;
    2.78 +
    2.79 +        string = [pasteboard stringForType:format];
    2.80 +        if (string == nil) {
    2.81 +            utf8 = "";
    2.82 +        } else {
    2.83 +            utf8 = [string UTF8String];
    2.84 +        }
    2.85 +        text = SDL_strdup(utf8);
    2.86 +    } else {
    2.87 +        text = SDL_strdup("");
    2.88 +    }
    2.89 +
    2.90 +    [pool release];
    2.91 +
    2.92 +    return text;
    2.93 +}
    2.94 +
    2.95 +SDL_bool
    2.96 +Cocoa_HasClipboardText(_THIS)
    2.97 +{
    2.98 +    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    2.99 +    NSAutoreleasePool *pool;
   2.100 +	NSPasteboard *pasteboard;
   2.101 +    NSString *format;
   2.102 +    NSString *available;
   2.103 +    SDL_bool result;
   2.104 +
   2.105 +    if (data->osversion >= 0x1060) {
   2.106 +        format = NSPasteboardTypeString;
   2.107 +    } else {
   2.108 +        format = NSStringPboardType;
   2.109 +    }
   2.110 +
   2.111 +    pool = [[NSAutoreleasePool alloc] init];
   2.112 +
   2.113 +    pasteboard = [NSPasteboard generalPasteboard];
   2.114 +    available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
   2.115 +    if ([available isEqualToString:format]) {
   2.116 +        result = SDL_TRUE;
   2.117 +    } else {
   2.118 +        result = SDL_FALSE;
   2.119 +    }
   2.120 +
   2.121 +    [pool release];
   2.122 +
   2.123 +    return result;
   2.124 +}
   2.125 +
   2.126 +/* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/video/cocoa/SDL_cocoavideo.h	Thu Jul 08 00:03:39 2010 -0700
     3.2 +++ b/src/video/cocoa/SDL_cocoavideo.h	Thu Jul 08 00:35:58 2010 -0700
     3.3 @@ -32,6 +32,7 @@
     3.4  #include "SDL_keysym.h"
     3.5  #include "../SDL_sysvideo.h"
     3.6  
     3.7 +#include "SDL_cocoaclipboard.h"
     3.8  #include "SDL_cocoaevents.h"
     3.9  #include "SDL_cocoakeyboard.h"
    3.10  #include "SDL_cocoamodes.h"
     4.1 --- a/src/video/cocoa/SDL_cocoavideo.m	Thu Jul 08 00:03:39 2010 -0700
     4.2 +++ b/src/video/cocoa/SDL_cocoavideo.m	Thu Jul 08 00:35:58 2010 -0700
     4.3 @@ -108,6 +108,10 @@
     4.4      device->StopTextInput = Cocoa_StopTextInput;
     4.5      device->SetTextInputRect = Cocoa_SetTextInputRect;
     4.6  
     4.7 +    device->SetClipboardText = Cocoa_SetClipboardText;
     4.8 +    device->GetClipboardText = Cocoa_GetClipboardText;
     4.9 +    device->HasClipboardText = Cocoa_HasClipboardText;
    4.10 +
    4.11      device->free = Cocoa_DeleteDevice;
    4.12  
    4.13      return device;