src/video/cocoa/SDL_cocoashape.m
author Ryan C. Gordon <icculus@icculus.org>
Fri, 02 Nov 2012 16:48:47 -0400
changeset 6639 c7e81fae23c4
parent 6281 e46d6f4b469e
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.
eligottlieb@4809
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
eligottlieb@4809
     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.
eligottlieb@4809
     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:
eligottlieb@4809
    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.
eligottlieb@4809
    20
*/
eligottlieb@4809
    21
slouken@6044
    22
#include "SDL_config.h"
slouken@6044
    23
slouken@6044
    24
#if SDL_VIDEO_DRIVER_COCOA
slouken@6044
    25
eligottlieb@4811
    26
#include "SDL_cocoavideo.h"
eligottlieb@4809
    27
#include "SDL_shape.h"
eligottlieb@4810
    28
#include "SDL_cocoashape.h"
aschiffler@4907
    29
#include "../SDL_sysvideo.h"
icculus@6281
    30
#include "SDL_assert.h"
eligottlieb@4809
    31
eligottlieb@4838
    32
SDL_WindowShaper*
eligottlieb@4838
    33
Cocoa_CreateShaper(SDL_Window* window) {
egottlieb@4859
    34
    SDL_WindowData* windata = (SDL_WindowData*)window->driverdata;
egottlieb@4859
    35
    [windata->nswindow setOpaque:NO];
icculus@5646
    36
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
egottlieb@4859
    37
    [windata->nswindow setStyleMask:NSBorderlessWindowMask];
slouken@4925
    38
#endif
egottlieb@4859
    39
    SDL_WindowShaper* result = result = malloc(sizeof(SDL_WindowShaper));
eligottlieb@4838
    40
    result->window = window;
eligottlieb@4838
    41
    result->mode.mode = ShapeModeDefault;
eligottlieb@4838
    42
    result->mode.parameters.binarizationCutoff = 1;
egottlieb@4859
    43
    result->userx = result->usery = 0;
eligottlieb@4838
    44
    window->shaper = result;
eligottlieb@4838
    45
    
eligottlieb@4838
    46
    SDL_ShapeData* data = malloc(sizeof(SDL_ShapeData));
eligottlieb@4838
    47
    result->driverdata = data;
egottlieb@4859
    48
    data->context = [windata->nswindow graphicsContext];
egottlieb@4859
    49
    data->saved = SDL_FALSE;
egottlieb@4859
    50
    data->shape = NULL;
eligottlieb@4838
    51
    
eligottlieb@4838
    52
    int resized_properly = Cocoa_ResizeWindowShape(window);
icculus@6281
    53
    SDL_assert(resized_properly == 0);
eligottlieb@4838
    54
    return result;
eligottlieb@4810
    55
}
eligottlieb@4810
    56
egottlieb@4859
    57
typedef struct {
egottlieb@4859
    58
    NSView* view;
egottlieb@4859
    59
    NSBezierPath* path;
egottlieb@4860
    60
    SDL_Window* window;
egottlieb@4859
    61
} SDL_CocoaClosure;
egottlieb@4859
    62
egottlieb@4859
    63
void
egottlieb@4859
    64
ConvertRects(SDL_ShapeTree* tree,void* closure) {
egottlieb@4859
    65
    SDL_CocoaClosure* data = (SDL_CocoaClosure*)closure;
egottlieb@4859
    66
    if(tree->kind == OpaqueShape) {
egottlieb@4860
    67
        NSRect rect = NSMakeRect(tree->data.shape.x,data->window->h - tree->data.shape.y,tree->data.shape.w,tree->data.shape.h);
egottlieb@4859
    68
        [data->path appendBezierPathWithRect:[data->view convertRect:rect toView:nil]];
egottlieb@4859
    69
    }
egottlieb@4859
    70
}
egottlieb@4859
    71
eligottlieb@4838
    72
int
egottlieb@4859
    73
Cocoa_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode) {
egottlieb@4859
    74
    SDL_ShapeData* data = (SDL_ShapeData*)shaper->driverdata;
egottlieb@4859
    75
	SDL_WindowData* windata = (SDL_WindowData*)shaper->window->driverdata;
egottlieb@4859
    76
	SDL_CocoaClosure closure;
egottlieb@4859
    77
	NSAutoreleasePool *pool = NULL;
egottlieb@4859
    78
    if(data->saved == SDL_TRUE) {
eligottlieb@4838
    79
        [data->context restoreGraphicsState];
egottlieb@4859
    80
        data->saved = SDL_FALSE;
eligottlieb@4838
    81
    }
eligottlieb@4838
    82
        
egottlieb@4859
    83
    //[data->context saveGraphicsState];
egottlieb@4859
    84
    //data->saved = SDL_TRUE;
egottlieb@4860
    85
	[NSGraphicsContext setCurrentContext:data->context];
eligottlieb@4838
    86
    
eligottlieb@4838
    87
    [[NSColor clearColor] set];
egottlieb@4859
    88
    NSRectFill([[windata->nswindow contentView] frame]);
egottlieb@4859
    89
    data->shape = SDL_CalculateShapeTree(*shape_mode,shape);
egottlieb@4859
    90
	
egottlieb@4859
    91
	pool = [[NSAutoreleasePool alloc] init];
egottlieb@4859
    92
    closure.view = [windata->nswindow contentView];
egottlieb@4859
    93
    closure.path = [[NSBezierPath bezierPath] autorelease];
egottlieb@4860
    94
	closure.window = shaper->window;
egottlieb@4859
    95
    SDL_TraverseShapeTree(data->shape,&ConvertRects,&closure);
egottlieb@4860
    96
    [closure.path addClip];
slouken@6044
    97
slouken@6044
    98
    return 0;
egottlieb@4829
    99
}
egottlieb@4829
   100
eligottlieb@4838
   101
int
eligottlieb@4838
   102
Cocoa_ResizeWindowShape(SDL_Window *window) {
eligottlieb@4838
   103
    SDL_ShapeData* data = window->shaper->driverdata;
icculus@6281
   104
    SDL_assert(data != NULL);
eligottlieb@4838
   105
    return 0;
egottlieb@4829
   106
}
slouken@6044
   107
slouken@6044
   108
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   109
slouken@6044
   110
/* vi: set ts=4 sw=4 expandtab: */