blob: 879477edfa68bc9a3fa90d197d09a018f8f4d3f2 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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#import "chrome/browser/ui/cocoa/download/download_show_all_cell.h"
6
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00007#import "chrome/browser/themes/theme_properties.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00008#import "chrome/browser/themes/theme_service.h"
9#import "chrome/browser/ui/cocoa/download/background_theme.h"
10#import "chrome/browser/ui/cocoa/themed_window.h"
11#include "grit/theme_resources.h"
12
13// Distance from top border to icon.
14const CGFloat kImagePaddingTop = 7;
15
16// Distance from left border to icon.
17const CGFloat kImagePaddingLeft = 11;
18
19// Width of icon.
20const CGFloat kImageWidth = 16;
21
22// Height of icon.
23const CGFloat kImageHeight = 16;
24
25// x distance between image and title.
26const CGFloat kImageTextPadding = 4;
27
28// x coordinate of download name string, in view coords.
29const CGFloat kTextPosLeft =
30 kImagePaddingLeft + kImageWidth + kImageTextPadding;
31
32// Distance from end of title to right border.
33const CGFloat kTextPaddingRight = 13;
34
35// y coordinate of title, in view coords.
36const CGFloat kTextPosTop = 10;
37
38// Width of outer stroke
39const CGFloat kOuterStrokeWidth = 1;
40
41@interface DownloadShowAllCell(Private)
42- (ui::ThemeProvider*)backgroundThemeWrappingProvider:
43 (ui::ThemeProvider*)provider;
44- (BOOL)pressedWithDefaultTheme;
45- (NSColor*)titleColor;
46@end
47
48@implementation DownloadShowAllCell
49
50- (void)setInitialState {
51 [self setFont:[NSFont systemFontOfSize:
52 [NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
53}
54
55// For nib instantiations
56- (id)initWithCoder:(NSCoder*)decoder {
57 if ((self = [super initWithCoder:decoder])) {
58 [self setInitialState];
59 }
60 return self;
61}
62
63// For programmatic instantiations.
64- (id)initTextCell:(NSString*)string {
65 if ((self = [super initTextCell:string])) {
66 [self setInitialState];
67 }
68 return self;
69}
70
71- (void)setShowsBorderOnlyWhileMouseInside:(BOOL)showOnly {
72 // Override to make sure it doesn't do anything if it's called accidentally.
73}
74
75- (ui::ThemeProvider*)backgroundThemeWrappingProvider:
76 (ui::ThemeProvider*)provider {
77 if (!themeProvider_.get()) {
78 themeProvider_.reset(new BackgroundTheme(provider));
79 }
80
81 return themeProvider_.get();
82}
83
84// Returns if the button was pressed while the default theme was active.
85- (BOOL)pressedWithDefaultTheme {
86 ui::ThemeProvider* themeProvider =
87 [[[self controlView] window] themeProvider];
88 bool isDefaultTheme =
89 !themeProvider->HasCustomImage(IDR_THEME_BUTTON_BACKGROUND);
90 return isDefaultTheme && [self isHighlighted];
91}
92
93// Returns the text color that should be used to draw title text.
94- (NSColor*)titleColor {
95 ui::ThemeProvider* themeProvider =
96 [[[self controlView] window] themeProvider];
97 if (!themeProvider || [self pressedWithDefaultTheme])
98 return [NSColor alternateSelectedControlTextColor];
Ben Murdochbb1529c2013-08-08 10:24:53 +010099 return themeProvider->GetNSColor(ThemeProperties::COLOR_BOOKMARK_TEXT);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000100}
101
102- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
103 NSRect drawFrame = NSInsetRect(cellFrame, 1.5, 1.5);
104 NSRect innerFrame = NSInsetRect(cellFrame, 2, 2);
105
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000106 const float radius = 3;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000107 NSWindow* window = [controlView window];
108 BOOL active = [window isKeyWindow] || [window isMainWindow];
109
110 // In the default theme, draw download items with the bookmark button
111 // gradient. For some themes, this leads to unreadable text, so draw the item
112 // with a background that looks like windows (some transparent white) if a
113 // theme is used. Use custom theme object with a white color gradient to trick
114 // the superclass into drawing what we want.
115 ui::ThemeProvider* themeProvider =
116 [[[self controlView] window] themeProvider];
117 if (!themeProvider)
118 return;
119
120 bool isDefaultTheme =
121 !themeProvider->HasCustomImage(IDR_THEME_BUTTON_BACKGROUND);
122
123 NSGradient* bgGradient = nil;
124 if (!isDefaultTheme) {
125 themeProvider = [self backgroundThemeWrappingProvider:themeProvider];
126 bgGradient = themeProvider->GetNSGradient(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000127 active ? ThemeProperties::GRADIENT_TOOLBAR_BUTTON :
128 ThemeProperties::GRADIENT_TOOLBAR_BUTTON_INACTIVE);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000129 }
130
131 NSBezierPath* buttonInnerPath =
132 [NSBezierPath bezierPathWithRoundedRect:drawFrame
133 xRadius:radius
134 yRadius:radius];
135
136 // Stroke the borders and appropriate fill gradient.
137 [self drawBorderAndFillForTheme:themeProvider
138 controlView:controlView
139 innerPath:buttonInnerPath
140 showClickedGradient:[self isHighlighted]
141 showHighlightGradient:[self isMouseInside]
142 hoverAlpha:0.0
143 active:active
144 cellFrame:cellFrame
145 defaultGradient:bgGradient];
146
147 [self drawInteriorWithFrame:innerFrame inView:controlView];
148}
149
150- (NSDictionary*)textAttributes {
151 return [NSDictionary dictionaryWithObjectsAndKeys:
152 [self titleColor], NSForegroundColorAttributeName,
153 [self font], NSFontAttributeName,
154 nil];
155}
156
157- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
158 // Draw title
159 NSPoint primaryPos = NSMakePoint(
160 cellFrame.origin.x + kTextPosLeft, kTextPosTop);
161 [[self title] drawAtPoint:primaryPos withAttributes:[self textAttributes]];
162
163 // Draw icon
164 [[self image] drawInRect:[self imageRectForBounds:cellFrame]
165 fromRect:NSZeroRect
166 operation:NSCompositeSourceOver
167 fraction:[self isEnabled] ? 1.0 : 0.5
168 respectFlipped:YES
169 hints:nil];
170}
171
172- (NSRect)imageRectForBounds:(NSRect)cellFrame {
173 return NSMakeRect(cellFrame.origin.x + kImagePaddingLeft,
174 cellFrame.origin.y + kImagePaddingTop,
175 kImageWidth,
176 kImageHeight);
177}
178
179- (NSSize)cellSize {
180 NSSize size = [super cellSize];
181
182 // Custom width:
183 NSSize textSize = [[self title] sizeWithAttributes:[self textAttributes]];
184 size.width = kTextPosLeft + textSize.width + kTextPaddingRight +
185 kOuterStrokeWidth * 2;
186 return size;
187}
188@end