- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / constrained_window / constrained_window_custom_window.mm
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_window.h"
6
7 #import "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/ui/chrome_style.h"
9 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_controller.h"
10 #include "skia/ext/skia_utils_mac.h"
11 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
12
13 @implementation ConstrainedWindowCustomWindow
14
15 - (id)initWithContentRect:(NSRect)contentRect {
16   if ((self = [self initWithContentRect:contentRect
17                               styleMask:NSBorderlessWindowMask
18                                 backing:NSBackingStoreBuffered
19                                   defer:NO])) {
20     base::scoped_nsobject<NSView> contentView(
21         [[ConstrainedWindowCustomWindowContentView alloc]
22             initWithFrame:NSZeroRect]);
23     [self setContentView:contentView];
24   }
25   return self;
26 }
27
28 - (id)initWithContentRect:(NSRect)contentRect
29                 styleMask:(NSUInteger)windowStyle
30                   backing:(NSBackingStoreType)bufferingType
31                     defer:(BOOL)deferCreation {
32   if ((self = [super initWithContentRect:contentRect
33                                styleMask:NSBorderlessWindowMask
34                                  backing:bufferingType
35                                    defer:NO])) {
36     [self setHasShadow:YES];
37     [self setBackgroundColor:gfx::SkColorToCalibratedNSColor(
38         chrome_style::GetBackgroundColor())];
39     [self setOpaque:NO];
40     [self setReleasedWhenClosed:NO];
41   }
42   return self;
43 }
44
45 - (BOOL)canBecomeKeyWindow {
46   return YES;
47 }
48
49 - (NSRect)frameRectForContentRect:(NSRect)windowContent {
50   id<ConstrainedWindowSheet> sheet = [ConstrainedWindowSheetController
51       sheetForOverlayWindow:[self parentWindow]];
52   ConstrainedWindowSheetController* sheetController =
53       [ConstrainedWindowSheetController controllerForSheet:sheet];
54
55   // Sheet controller may be nil if this window hasn't been shown yet.
56   if (!sheetController)
57     return windowContent;
58
59   NSRect frame;
60   frame.origin = [sheetController originForSheet:sheet
61                                   withWindowSize:windowContent.size];
62   frame.size = windowContent.size;
63   return frame;
64 }
65
66 @end
67
68 @implementation ConstrainedWindowCustomWindowContentView
69
70 - (void)drawRect:(NSRect)rect {
71   gfx::ScopedNSGraphicsContextSaveGState state;
72
73   // Draw symmetric difference between rect path and oval path as "clear".
74   NSBezierPath* ovalPath = [NSBezierPath
75       bezierPathWithRoundedRect:[self bounds]
76                         xRadius:chrome_style::kBorderRadius
77                         yRadius:chrome_style::kBorderRadius];
78   NSBezierPath* path = [NSBezierPath bezierPathWithRect:[self bounds]];
79   [path appendBezierPath:ovalPath];
80   [path setWindingRule:NSEvenOddWindingRule];
81   [[NSGraphicsContext currentContext] setCompositingOperation:
82       NSCompositeCopy];
83   [[NSColor clearColor] set];
84   [path fill];
85
86   [[self window] invalidateShadow];
87 }
88
89 @end