• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2011 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/background_gradient_view.h"
6
7#import "chrome/browser/themes/theme_service.h"
8#import "chrome/browser/ui/cocoa/nsview_additions.h"
9#import "chrome/browser/ui/cocoa/themed_window.h"
10#include "grit/theme_resources.h"
11
12#define kToolbarTopOffset 12
13#define kToolbarMaxHeight 100
14
15@implementation BackgroundGradientView
16@synthesize showsDivider = showsDivider_;
17
18- (id)initWithFrame:(NSRect)frameRect {
19  self = [super initWithFrame:frameRect];
20  if (self != nil) {
21    showsDivider_ = YES;
22  }
23  return self;
24}
25
26- (void)awakeFromNib {
27  showsDivider_ = YES;
28}
29
30- (void)setShowsDivider:(BOOL)show {
31  showsDivider_ = show;
32  [self setNeedsDisplay:YES];
33}
34
35- (void)drawBackground {
36  BOOL isKey = [[self window] isKeyWindow];
37  ui::ThemeProvider* themeProvider = [[self window] themeProvider];
38  if (themeProvider) {
39    NSColor* backgroundImageColor =
40        themeProvider->GetNSImageColorNamed(IDR_THEME_TOOLBAR, false);
41    if (backgroundImageColor) {
42      [backgroundImageColor set];
43      NSRectFill([self bounds]);
44    } else {
45      CGFloat winHeight = NSHeight([[self window] frame]);
46      NSGradient* gradient = themeProvider->GetNSGradient(
47          isKey ? ThemeService::GRADIENT_TOOLBAR :
48                  ThemeService::GRADIENT_TOOLBAR_INACTIVE);
49      NSPoint startPoint =
50          [self convertPoint:NSMakePoint(0, winHeight - kToolbarTopOffset)
51                    fromView:nil];
52      NSPoint endPoint =
53          NSMakePoint(0, winHeight - kToolbarTopOffset - kToolbarMaxHeight);
54      endPoint = [self convertPoint:endPoint fromView:nil];
55
56      [gradient drawFromPoint:startPoint
57                      toPoint:endPoint
58                      options:(NSGradientDrawsBeforeStartingLocation |
59                               NSGradientDrawsAfterEndingLocation)];
60    }
61
62    if (showsDivider_) {
63      // Draw bottom stroke
64      [[self strokeColor] set];
65      NSRect borderRect, contentRect;
66      NSDivideRect([self bounds], &borderRect, &contentRect,
67                   [self cr_lineWidth], NSMinYEdge);
68      NSRectFillUsingOperation(borderRect, NSCompositeSourceOver);
69    }
70  }
71}
72
73- (NSColor*)strokeColor {
74  BOOL isKey = [[self window] isKeyWindow];
75  ui::ThemeProvider* themeProvider = [[self window] themeProvider];
76  if (!themeProvider)
77    return [NSColor blackColor];
78  return themeProvider->GetNSColor(
79      isKey ? ThemeService::COLOR_TOOLBAR_STROKE :
80              ThemeService::COLOR_TOOLBAR_STROKE_INACTIVE, true);
81}
82
83@end
84