1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebCoreFullScreenWarningView.h"
28
29#include "LocalizedStrings.h"
30#include <wtf/text/WTFString.h>
31
32static const CGFloat WarningViewTextWhite = 0.9;
33static const CGFloat WarningViewTextAlpha = 1;
34static const CGFloat WarningViewTextSize = 48;
35static const CGFloat WarningViewPadding = 20;
36static const CGFloat WarningViewCornerRadius = 10;
37static const CGFloat WarningViewBorderWhite = 0.9;
38static const CGFloat WarningViewBorderAlpha = 0.2;
39static const CGFloat WarningViewBackgroundWhite = 0.1;
40static const CGFloat WarningViewBackgroundAlpha = 0.9;
41static const CGFloat WarningViewShadowWhite = 0.1;
42static const CGFloat WarningViewShadowAlpha = 1;
43static const float WarningViewShadowOpacity = 0.25;
44static const NSSize WarningViewShadowOffset = {0, -2};
45static const CGFloat WarningViewShadowRadius = 5;
46static const NSTimeInterval WarningViewHideDelay = 3;
47static const NSTimeInterval WarningViewFadeDuration = 0.5;
48
49@implementation WebCoreFullScreenWarningView
50
51- (id)initWithTitle:(NSString*)title
52{
53    self = [super initWithFrame:NSZeroRect];
54    if (!self)
55        return nil;
56
57    [self setAutoresizingMask:(NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin | NSViewMaxYMargin)];
58    [self setBoxType:NSBoxCustom];
59    [self setTitlePosition:NSNoTitle];
60
61    _textField = adoptNS([[NSTextField alloc] initWithFrame:NSZeroRect]);
62    [_textField.get() setEditable:NO];
63    [_textField.get() setSelectable:NO];
64    [_textField.get() setBordered:NO];
65    [_textField.get() setDrawsBackground:NO];
66
67    NSFont* textFont = [NSFont boldSystemFontOfSize:WarningViewTextSize];
68    NSColor* textColor = [NSColor colorWithCalibratedWhite:WarningViewTextWhite alpha:WarningViewTextAlpha];
69    RetainPtr<NSDictionary> attributes = adoptNS([[NSDictionary alloc] initWithObjectsAndKeys:
70                                                  textFont, NSFontAttributeName,
71                                                  textColor, NSForegroundColorAttributeName,
72                                                  nil]);
73    RetainPtr<NSAttributedString> text = adoptNS([[NSAttributedString alloc] initWithString:title attributes:attributes.get()]);
74    [_textField.get() setAttributedStringValue:text.get()];
75    [_textField.get() sizeToFit];
76    NSRect textFieldFrame = [_textField.get() frame];
77    NSSize frameSize = textFieldFrame.size;
78    frameSize.width += WarningViewPadding * 2;
79    frameSize.height += WarningViewPadding * 2;
80    [self setFrameSize:frameSize];
81
82    textFieldFrame.origin = NSMakePoint(
83        (frameSize.width - textFieldFrame.size.width) / 2,
84        (frameSize.height - textFieldFrame.size.height) / 2);
85
86    // Offset the origin by the font's descender, to center the text field about the baseline:
87    textFieldFrame.origin.y += [[_textField.get() font] descender];
88
89    [_textField.get() setFrame:NSIntegralRect(textFieldFrame)];
90    [self addSubview:_textField.get()];
91
92    NSColor* backgroundColor = [NSColor colorWithCalibratedWhite:WarningViewBackgroundWhite alpha:WarningViewBackgroundAlpha];
93    [self setFillColor:backgroundColor];
94    [self setCornerRadius:WarningViewCornerRadius];
95
96    NSColor* borderColor = [NSColor colorWithCalibratedWhite:WarningViewBorderWhite alpha:WarningViewBorderAlpha];
97    [self setBorderColor:borderColor];
98
99    RetainPtr<NSShadow> shadow = adoptNS([[NSShadow alloc] init]);
100    RetainPtr<NSColor> shadowColor = [NSColor colorWithCalibratedWhite:WarningViewShadowWhite alpha:WarningViewShadowAlpha];
101    [shadow.get() setShadowColor:shadowColor.get()];
102    [shadow.get() setShadowOffset:WarningViewShadowOffset];
103    [shadow.get() setShadowBlurRadius:WarningViewShadowRadius];
104    [self setShadow:shadow.get()];
105
106    return self;
107}
108@end
109