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.
slouken@4499
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@4499
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@4499
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@4499
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@4499
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@4499
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_COCOA
slouken@6044
    24
slouken@4499
    25
#include "SDL_cocoavideo.h"
slouken@4503
    26
#include "../../events/SDL_clipboardevents_c.h"
slouken@4499
    27
slouken@4501
    28
static NSString *
slouken@4501
    29
GetTextFormat(_THIS)
slouken@4501
    30
{
slouken@8986
    31
    if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
slouken@4501
    32
        return NSPasteboardTypeString;
slouken@4501
    33
    } else {
slouken@4501
    34
        return NSStringPboardType;
slouken@4501
    35
    }
slouken@4501
    36
}
slouken@4499
    37
slouken@4499
    38
int
slouken@4499
    39
Cocoa_SetClipboardText(_THIS, const char *text)
slouken@4499
    40
{
slouken@4503
    41
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@6848
    42
    NSAutoreleasePool *pool;
slouken@4504
    43
    NSPasteboard *pasteboard;
slouken@4501
    44
    NSString *format = GetTextFormat(_this);
slouken@4499
    45
slouken@6848
    46
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
    47
slouken@6848
    48
    pasteboard = [NSPasteboard generalPasteboard];
slouken@6848
    49
    data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
slouken@6848
    50
    [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
slouken@6848
    51
slouken@6848
    52
    [pool release];
slouken@6848
    53
slouken@4499
    54
    return 0;
slouken@4499
    55
}
slouken@4499
    56
slouken@4499
    57
char *
slouken@4499
    58
Cocoa_GetClipboardText(_THIS)
slouken@4499
    59
{
slouken@6848
    60
    NSAutoreleasePool *pool;
slouken@4504
    61
    NSPasteboard *pasteboard;
slouken@4501
    62
    NSString *format = GetTextFormat(_this);
slouken@4499
    63
    NSString *available;
slouken@4499
    64
    char *text;
slouken@4499
    65
slouken@6848
    66
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
    67
slouken@6848
    68
    pasteboard = [NSPasteboard generalPasteboard];
slouken@6848
    69
    available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
slouken@6848
    70
    if ([available isEqualToString:format]) {
slouken@6848
    71
        NSString* string;
slouken@6848
    72
        const char *utf8;
slouken@4499
    73
slouken@6848
    74
        string = [pasteboard stringForType:format];
slouken@6848
    75
        if (string == nil) {
slouken@6848
    76
            utf8 = "";
slouken@4499
    77
        } else {
slouken@6848
    78
            utf8 = [string UTF8String];
slouken@4499
    79
        }
slouken@6848
    80
        text = SDL_strdup(utf8);
slouken@6848
    81
    } else {
slouken@6848
    82
        text = SDL_strdup("");
slouken@4499
    83
    }
slouken@6848
    84
slouken@6848
    85
    [pool release];
slouken@6848
    86
slouken@4499
    87
    return text;
slouken@4499
    88
}
slouken@4499
    89
slouken@4499
    90
SDL_bool
slouken@4499
    91
Cocoa_HasClipboardText(_THIS)
slouken@4499
    92
{
aschiffler@6036
    93
    SDL_bool result = SDL_FALSE;
aschiffler@6036
    94
    char *text = Cocoa_GetClipboardText(_this);
aschiffler@6036
    95
    if (text) {
slouken@7721
    96
        result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
slouken@8986
    97
        SDL_free(text);
slouken@4499
    98
    }
slouken@4499
    99
    return result;
slouken@4499
   100
}
slouken@4499
   101
slouken@4503
   102
void
slouken@4503
   103
Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
slouken@4503
   104
{
slouken@6848
   105
    NSAutoreleasePool *pool;
slouken@4504
   106
    NSPasteboard *pasteboard;
slouken@4503
   107
    NSInteger count;
slouken@4503
   108
slouken@6848
   109
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   110
slouken@6848
   111
    pasteboard = [NSPasteboard generalPasteboard];
slouken@6848
   112
    count = [pasteboard changeCount];
slouken@6848
   113
    if (count != data->clipboard_count) {
slouken@6848
   114
        if (data->clipboard_count) {
slouken@6848
   115
            SDL_SendClipboardUpdate();
slouken@4503
   116
        }
slouken@6848
   117
        data->clipboard_count = count;
slouken@4503
   118
    }
slouken@6848
   119
slouken@6848
   120
    [pool release];
slouken@4503
   121
}
slouken@4503
   122
slouken@6044
   123
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   124
slouken@4499
   125
/* vi: set ts=4 sw=4 expandtab: */