- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / infobars / infobar_gradient_view.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 #include "chrome/browser/ui/cocoa/infobars/infobar_gradient_view.h"
6
7 #include "base/mac/scoped_nsobject.h"
8 #include "chrome/browser/infobars/infobar.h"
9 #import "chrome/browser/themes/theme_properties.h"
10 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
11 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h"
12 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
13 #import "chrome/browser/ui/cocoa/themed_window.h"
14 #include "skia/ext/skia_utils_mac.h"
15 #include "ui/base/theme_provider.h"
16
17 @implementation InfoBarGradientView
18
19 @synthesize arrowHeight = arrowHeight_;
20 @synthesize arrowHalfWidth = arrowHalfWidth_;
21 @synthesize arrowX = arrowX_;
22 @synthesize hasTip = hasTip_;
23
24 - (id)initWithFrame:(NSRect)frame {
25   if ((self = [super initWithFrame:frame])) {
26     hasTip_ = YES;
27   }
28   return self;
29 }
30
31 - (id)initWithCoder:(NSCoder*)decoder {
32   if ((self = [super initWithCoder:decoder])) {
33     hasTip_ = YES;
34   }
35   return self;
36 }
37
38 - (void)setInfobarType:(InfoBarDelegate::Type)infobarType {
39   SkColor topColor = GetInfoBarTopColor(infobarType);
40   SkColor bottomColor = GetInfoBarBottomColor(infobarType);
41   base::scoped_nsobject<NSGradient> gradient([[NSGradient alloc]
42       initWithStartingColor:gfx::SkColorToCalibratedNSColor(topColor)
43                 endingColor:gfx::SkColorToCalibratedNSColor(bottomColor)]);
44   [self setGradient:gradient];
45 }
46
47 - (NSColor*)strokeColor {
48   ui::ThemeProvider* themeProvider = [[self window] themeProvider];
49   if (!themeProvider)
50     return [NSColor blackColor];
51
52   BOOL active = [[self window] isMainWindow];
53   return themeProvider->GetNSColor(
54       active ? ThemeProperties::COLOR_TOOLBAR_STROKE :
55                ThemeProperties::COLOR_TOOLBAR_STROKE_INACTIVE);
56 }
57
58 - (void)drawRect:(NSRect)rect {
59   NSRect bounds = [self bounds];
60   bounds.size.height = InfoBar::kDefaultBarTargetHeight;
61
62   CGFloat tipXOffset = arrowX_ - arrowHalfWidth_;
63
64   // Around the bounds of the infobar, continue drawing the path into which the
65   // gradient will be drawn.
66   NSBezierPath* infoBarPath = [NSBezierPath bezierPath];
67   [infoBarPath moveToPoint:NSMakePoint(NSMinX(bounds), NSMaxY(bounds))];
68
69   // Draw the tip.
70   if (hasTip_) {
71     [infoBarPath lineToPoint:NSMakePoint(tipXOffset, NSMaxY(bounds))];
72     [infoBarPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_,
73                                                  arrowHeight_)];
74     [infoBarPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_,
75                                                  -arrowHeight_)];
76   }
77   [infoBarPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds))];
78
79   // Save off the top path of the infobar.
80   base::scoped_nsobject<NSBezierPath> topPath([infoBarPath copy]);
81
82   [infoBarPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMinY(bounds))];
83   [infoBarPath lineToPoint:NSMakePoint(NSMinX(bounds), NSMinY(bounds))];
84   [infoBarPath closePath];
85
86   // Draw the gradient.
87   [[self gradient] drawInBezierPath:infoBarPath angle:270];
88
89   NSColor* strokeColor = [self strokeColor];
90   if (strokeColor) {
91     [strokeColor set];
92
93     // Stroke the bottom of the infobar.
94     NSRect borderRect, contentRect;
95     NSDivideRect(bounds, &borderRect, &contentRect, 1, NSMinYEdge);
96     NSRectFillUsingOperation(borderRect, NSCompositeSourceOver);
97
98     // Re-stroke the top because the tip will have no stroke. This will draw
99     // over the divider drawn by the bottom of the tabstrip.
100     [topPath stroke];
101   }
102
103   // Add an inner stroke.
104   const CGFloat kHighlightTipHeight = arrowHeight_ - 1;
105   NSBezierPath* highlightPath = [NSBezierPath bezierPath];
106   [highlightPath moveToPoint:NSMakePoint(NSMinX(bounds), NSMaxY(bounds) - 1)];
107   if (hasTip_) {
108     [highlightPath relativeLineToPoint:NSMakePoint(tipXOffset + 1, 0)];
109     [highlightPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_ - 1,
110                                                    kHighlightTipHeight)];
111     [highlightPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_ - 1,
112                                                    -kHighlightTipHeight)];
113   }
114   [highlightPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds) - 1)];
115
116   [[NSColor colorWithDeviceWhite:1.0 alpha:1.0] setStroke];
117   [highlightPath stroke];
118 }
119
120 - (BOOL)mouseDownCanMoveWindow {
121   return NO;
122 }
123
124 // This view is intentionally not opaque because it overlaps with the findbar.
125
126 - (BOOL)accessibilityIsIgnored {
127   return NO;
128 }
129
130 - (id)accessibilityAttributeValue:(NSString*)attribute {
131   if ([attribute isEqual:NSAccessibilityRoleAttribute])
132     return NSAccessibilityGroupRole;
133
134   return [super accessibilityAttributeValue:attribute];
135 }
136
137 - (void)setHasTip:(BOOL)hasTip {
138   if (hasTip_ == hasTip)
139     return;
140   hasTip_ = hasTip;
141   [self setNeedsDisplay:YES];
142 }
143
144 @end