src/video/cocoa/SDL_cocoaclipboard.m
author Ryan C. Gordon <icculus@icculus.org>
Fri, 02 Nov 2012 16:48:47 -0400
changeset 6639 c7e81fae23c4
parent 6138 4c64952a58fb
child 6832 156e608ec4ef
permissions -rw-r--r--
Cocoa_ShowMessageBox() should call Cocoa_RegisterApp().

Otherwise, message boxes can fail if SDL_Init(SDL_INIT_VIDEO) wasn't completed.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 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 #if SDL_VIDEO_DRIVER_COCOA
    24 
    25 #include "SDL_cocoavideo.h"
    26 #include "../../events/SDL_clipboardevents_c.h"
    27 
    28 static NSString *
    29 GetTextFormat(_THIS)
    30 {
    31 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
    32     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    33 
    34     if (data->osversion >= 0x1060) {
    35         return NSPasteboardTypeString;
    36     } else {
    37         return NSStringPboardType;
    38     }
    39 #else
    40     return NSStringPboardType;
    41 #endif
    42 }
    43 
    44 int
    45 Cocoa_SetClipboardText(_THIS, const char *text)
    46 {
    47     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    48     NSAutoreleasePool *pool;
    49     NSPasteboard *pasteboard;
    50     NSString *format = GetTextFormat(_this);
    51 
    52     pool = [[NSAutoreleasePool alloc] init];
    53 
    54     pasteboard = [NSPasteboard generalPasteboard];
    55     data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
    56     [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
    57 
    58     [pool release];
    59 
    60     return 0;
    61 }
    62 
    63 char *
    64 Cocoa_GetClipboardText(_THIS)
    65 {
    66     NSAutoreleasePool *pool;
    67     NSPasteboard *pasteboard;
    68     NSString *format = GetTextFormat(_this);
    69     NSString *available;
    70     char *text;
    71 
    72     pool = [[NSAutoreleasePool alloc] init];
    73 
    74     pasteboard = [NSPasteboard generalPasteboard];
    75     available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
    76     if ([available isEqualToString:format]) {
    77         NSString* string;
    78         const char *utf8;
    79 
    80         string = [pasteboard stringForType:format];
    81         if (string == nil) {
    82             utf8 = "";
    83         } else {
    84             utf8 = [string UTF8String];
    85         }
    86         text = SDL_strdup(utf8);
    87     } else {
    88         text = SDL_strdup("");
    89     }
    90 
    91     [pool release];
    92 
    93     return text;
    94 }
    95 
    96 SDL_bool
    97 Cocoa_HasClipboardText(_THIS)
    98 {
    99     SDL_bool result = SDL_FALSE;
   100     char *text = Cocoa_GetClipboardText(_this);
   101     if (text) {
   102 	result = (SDL_strlen(text)>0) ? SDL_TRUE : SDL_FALSE;
   103 	SDL_free(text);
   104     }
   105     return result;
   106 }
   107 
   108 void
   109 Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
   110 {
   111     NSAutoreleasePool *pool;
   112     NSPasteboard *pasteboard;
   113     NSInteger count;
   114 
   115     pool = [[NSAutoreleasePool alloc] init];
   116 
   117     pasteboard = [NSPasteboard generalPasteboard];
   118     count = [pasteboard changeCount];
   119     if (count != data->clipboard_count) {
   120         if (data->clipboard_count) {
   121             SDL_SendClipboardUpdate();
   122         }
   123         data->clipboard_count = count;
   124     }
   125 
   126     [pool release];
   127 }
   128 
   129 #endif /* SDL_VIDEO_DRIVER_COCOA */
   130 
   131 /* vi: set ts=4 sw=4 expandtab: */