src/video/cocoa/SDL_cocoaclipboard.m
author Sam Lantinga
Thu, 08 Jul 2010 22:54:03 -0700
changeset 4503 524dfefd554c
parent 4501 0cf025066b6f
child 4504 9faebccfefb3
permissions -rw-r--r--
Added an event when the clipboard is updated, triggered after the window gains the keyboard focus.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_cocoavideo.h"
    25 #include "../../events/SDL_clipboardevents_c.h"
    26 
    27 static NSString *
    28 GetTextFormat(_THIS)
    29 {
    30 #if MAC_OS_X_VERSION_MAX_ALLOWED < 1060
    31     return NSStringPboardType;
    32 #else
    33     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    34 
    35     if (data->osversion >= 0x1060) {
    36         return NSPasteboardTypeString;
    37     } else {
    38         return NSStringPboardType;
    39     }
    40 #endif
    41 }
    42 
    43 int
    44 Cocoa_SetClipboardText(_THIS, const char *text)
    45 {
    46     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    47     NSAutoreleasePool *pool;
    48 	NSPasteboard *pasteboard;
    49     NSString *format = GetTextFormat(_this);
    50 
    51     pool = [[NSAutoreleasePool alloc] init];
    52 
    53     pasteboard = [NSPasteboard generalPasteboard];
    54     data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
    55     [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
    56 
    57     [pool release];
    58 
    59     return 0;
    60 }
    61 
    62 char *
    63 Cocoa_GetClipboardText(_THIS)
    64 {
    65     NSAutoreleasePool *pool;
    66 	NSPasteboard *pasteboard;
    67     NSString *format = GetTextFormat(_this);
    68     NSString *available;
    69     char *text;
    70 
    71     pool = [[NSAutoreleasePool alloc] init];
    72 
    73     pasteboard = [NSPasteboard generalPasteboard];
    74     available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
    75     if ([available isEqualToString:format]) {
    76         NSString* string;
    77         const char *utf8;
    78 
    79         string = [pasteboard stringForType:format];
    80         if (string == nil) {
    81             utf8 = "";
    82         } else {
    83             utf8 = [string UTF8String];
    84         }
    85         text = SDL_strdup(utf8);
    86     } else {
    87         text = SDL_strdup("");
    88     }
    89 
    90     [pool release];
    91 
    92     return text;
    93 }
    94 
    95 SDL_bool
    96 Cocoa_HasClipboardText(_THIS)
    97 {
    98     NSAutoreleasePool *pool;
    99 	NSPasteboard *pasteboard;
   100     NSString *format = GetTextFormat(_this);
   101     NSString *available;
   102     SDL_bool result;
   103 
   104     pool = [[NSAutoreleasePool alloc] init];
   105 
   106     pasteboard = [NSPasteboard generalPasteboard];
   107     available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
   108     if ([available isEqualToString:format]) {
   109         result = SDL_TRUE;
   110     } else {
   111         result = SDL_FALSE;
   112     }
   113 
   114     [pool release];
   115 
   116     return result;
   117 }
   118 
   119 void
   120 Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
   121 {
   122     NSAutoreleasePool *pool;
   123 	NSPasteboard *pasteboard;
   124     NSInteger count;
   125 
   126     pool = [[NSAutoreleasePool alloc] init];
   127 
   128     pasteboard = [NSPasteboard generalPasteboard];
   129     count = [pasteboard changeCount];
   130     if (count != data->clipboard_count) {
   131         if (data->clipboard_count) {
   132             SDL_SendClipboardUpdate();
   133         }
   134         data->clipboard_count = count;
   135     }
   136 
   137     [pool release];
   138 }
   139 
   140 /* vi: set ts=4 sw=4 expandtab: */