src/video/cocoa/SDL_cocoaclipboard.m
author Sam Lantinga
Mon, 07 Jul 2014 12:48:25 -0700
changeset 8986 1c81316ac642
parent 8149 681eb46b8ac4
child 9087 eef2e43f60d1
permissions -rw-r--r--
Fixed bug 2631 - Mac: minor code cleanup

Alex Szpakowski

Some minor changes to the Mac-specific backend code:

- Fixed up some code style issues (mostly brace style inconsistencies).

- Fixed a compiler warning in SDL_cocoaevents.m.

- Removed some useless code now that the 10.7 SDK is required to build SDL.

- Removed Gestalt(gestaltSystemVersion, ...) call and switched to NSAppKitVersionNumber for version checking code. Using Gestalt with gestaltSystemVersion will give 0x1090 in Mac OS 10.10+, and the whole Gestalt function was deprecated in Mac OS 10.8.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 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 static NSString *
    29 GetTextFormat(_THIS)
    30 {
    31     if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
    32         return NSPasteboardTypeString;
    33     } else {
    34         return NSStringPboardType;
    35     }
    36 }
    37 
    38 int
    39 Cocoa_SetClipboardText(_THIS, const char *text)
    40 {
    41     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    42     NSAutoreleasePool *pool;
    43     NSPasteboard *pasteboard;
    44     NSString *format = GetTextFormat(_this);
    45 
    46     pool = [[NSAutoreleasePool alloc] init];
    47 
    48     pasteboard = [NSPasteboard generalPasteboard];
    49     data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
    50     [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
    51 
    52     [pool release];
    53 
    54     return 0;
    55 }
    56 
    57 char *
    58 Cocoa_GetClipboardText(_THIS)
    59 {
    60     NSAutoreleasePool *pool;
    61     NSPasteboard *pasteboard;
    62     NSString *format = GetTextFormat(_this);
    63     NSString *available;
    64     char *text;
    65 
    66     pool = [[NSAutoreleasePool alloc] init];
    67 
    68     pasteboard = [NSPasteboard generalPasteboard];
    69     available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
    70     if ([available isEqualToString:format]) {
    71         NSString* string;
    72         const char *utf8;
    73 
    74         string = [pasteboard stringForType:format];
    75         if (string == nil) {
    76             utf8 = "";
    77         } else {
    78             utf8 = [string UTF8String];
    79         }
    80         text = SDL_strdup(utf8);
    81     } else {
    82         text = SDL_strdup("");
    83     }
    84 
    85     [pool release];
    86 
    87     return text;
    88 }
    89 
    90 SDL_bool
    91 Cocoa_HasClipboardText(_THIS)
    92 {
    93     SDL_bool result = SDL_FALSE;
    94     char *text = Cocoa_GetClipboardText(_this);
    95     if (text) {
    96         result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
    97         SDL_free(text);
    98     }
    99     return result;
   100 }
   101 
   102 void
   103 Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
   104 {
   105     NSAutoreleasePool *pool;
   106     NSPasteboard *pasteboard;
   107     NSInteger count;
   108 
   109     pool = [[NSAutoreleasePool alloc] init];
   110 
   111     pasteboard = [NSPasteboard generalPasteboard];
   112     count = [pasteboard changeCount];
   113     if (count != data->clipboard_count) {
   114         if (data->clipboard_count) {
   115             SDL_SendClipboardUpdate();
   116         }
   117         data->clipboard_count = count;
   118     }
   119 
   120     [pool release];
   121 }
   122 
   123 #endif /* SDL_VIDEO_DRIVER_COCOA */
   124 
   125 /* vi: set ts=4 sw=4 expandtab: */