Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / background_gradient_view.mm
blob1ed51139bb6977ad8fe5d425c1e7689f9622e6b5
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.
5 #include "chrome/browser/ui/cocoa/background_gradient_view.h"
7 #import "chrome/browser/themes/theme_properties.h"
8 #import "chrome/browser/themes/theme_service.h"
9 #import "chrome/browser/ui/cocoa/nsview_additions.h"
10 #import "chrome/browser/ui/cocoa/themed_window.h"
11 #include "grit/theme_resources.h"
13 @interface BackgroundGradientView (Private)
14 - (void)commonInit;
15 - (NSColor*)backgroundImageColor;
16 @end
18 @implementation BackgroundGradientView
20 @synthesize showsDivider = showsDivider_;
22 - (id)initWithFrame:(NSRect)frameRect {
23   if ((self = [super initWithFrame:frameRect])) {
24     [self commonInit];
25   }
26   return self;
29 - (id)initWithCoder:(NSCoder*)decoder {
30   if ((self = [super initWithCoder:decoder])) {
31     [self commonInit];
32   }
33   return self;
36 - (void)dealloc {
37   [[NSNotificationCenter defaultCenter] removeObserver:self];
38   [super dealloc];
41 - (void)commonInit {
42   showsDivider_ = YES;
43   [[NSNotificationCenter defaultCenter]
44       addObserver:self
45          selector:@selector(windowFocusDidChange:)
46              name:NSApplicationWillBecomeActiveNotification
47            object:NSApp];
48   [[NSNotificationCenter defaultCenter]
49       addObserver:self
50          selector:@selector(windowFocusDidChange:)
51              name:NSApplicationWillResignActiveNotification
52            object:NSApp];
55 - (void)setShowsDivider:(BOOL)show {
56   if (showsDivider_ == show)
57     return;
58   showsDivider_ = show;
59   [self setNeedsDisplay:YES];
62 - (void)drawBackgroundWithOpaque:(BOOL)opaque {
63   const NSRect bounds = [self bounds];
65   if (opaque) {
66     // If the background image is semi transparent then we need something
67     // to blend against. Using 20% black gives us a color similar to Windows.
68     [[NSColor colorWithCalibratedWhite:0.2 alpha:1.0] set];
69     NSRectFill(bounds);
70   }
72   [[self backgroundImageColor] set];
73   NSRectFillUsingOperation(bounds, NSCompositeSourceOver);
75   if (showsDivider_) {
76     // Draw bottom stroke
77     [[self strokeColor] set];
78     NSRect borderRect, contentRect;
79     NSDivideRect(bounds, &borderRect, &contentRect, [self cr_lineWidth],
80                  NSMinYEdge);
81     NSRectFillUsingOperation(borderRect, NSCompositeSourceOver);
82   }
85 - (NSColor*)strokeColor {
86   NSWindow* window = [self window];
87   if ([window parentWindow])
88     window = [window parentWindow];
90   BOOL isActive = [window isMainWindow];
91   ui::ThemeProvider* themeProvider = [window themeProvider];
92   if (!themeProvider)
93     return [NSColor blackColor];
94   return themeProvider->GetNSColor(
95       isActive ? ThemeProperties::COLOR_TOOLBAR_STROKE :
96                  ThemeProperties::COLOR_TOOLBAR_STROKE_INACTIVE);
99 - (NSColor*)backgroundImageColor {
100   ThemeService* themeProvider =
101       static_cast<ThemeService*>([[self window] themeProvider]);
102   if (!themeProvider)
103     return [[self window] backgroundColor];
105   // Themes don't have an inactive image so only look for one if there's no
106   // theme.
107   if (![[self window] isMainWindow] && themeProvider->UsingDefaultTheme()) {
108     NSColor* color = themeProvider->GetNSImageColorNamed(
109         IDR_THEME_TOOLBAR_INACTIVE);
110     if (color)
111       return color;
112   }
114   return themeProvider->GetNSImageColorNamed(IDR_THEME_TOOLBAR);
117 - (void)windowFocusDidChange:(NSNotification*)notification {
118   // The background color depends on the window's focus state.
119   [self cr_recursivelySetNeedsDisplay:YES];
122 - (void)viewWillMoveToWindow:(NSWindow*)window {
123   if ([self window]) {
124     [[NSNotificationCenter defaultCenter]
125         removeObserver:self
126                   name:NSWindowDidBecomeKeyNotification
127                 object:[self window]];
128     [[NSNotificationCenter defaultCenter]
129         removeObserver:self
130                   name:NSWindowDidBecomeMainNotification
131                 object:[self window]];
132   }
133   if (window) {
134     [[NSNotificationCenter defaultCenter]
135         addObserver:self
136            selector:@selector(windowFocusDidChange:)
137                name:NSWindowDidBecomeKeyNotification
138              object:[self window]];
139     [[NSNotificationCenter defaultCenter]
140         addObserver:self
141            selector:@selector(windowFocusDidChange:)
142                name:NSWindowDidBecomeMainNotification
143              object:[self window]];
144   }
145   [super viewWillMoveToWindow:window];
148 - (void)setFrameOrigin:(NSPoint)origin {
149   // The background color depends on the view's vertical position.
150   if (NSMinY([self frame]) != origin.y)
151     [self setNeedsDisplay:YES];
153   [super setFrameOrigin:origin];
156 @end