BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / download / download_show_all_cell.mm
blob879477edfa68bc9a3fa90d197d09a018f8f4d3f2
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.
5 #import "chrome/browser/ui/cocoa/download/download_show_all_cell.h"
7 #import "chrome/browser/themes/theme_properties.h"
8 #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"
13 // Distance from top border to icon.
14 const CGFloat kImagePaddingTop = 7;
16 // Distance from left border to icon.
17 const CGFloat kImagePaddingLeft = 11;
19 // Width of icon.
20 const CGFloat kImageWidth = 16;
22 // Height of icon.
23 const CGFloat kImageHeight = 16;
25 // x distance between image and title.
26 const CGFloat kImageTextPadding = 4;
28 // x coordinate of download name string, in view coords.
29 const CGFloat kTextPosLeft =
30     kImagePaddingLeft + kImageWidth + kImageTextPadding;
32 // Distance from end of title to right border.
33 const CGFloat kTextPaddingRight = 13;
35 // y coordinate of title, in view coords.
36 const CGFloat kTextPosTop = 10;
38 // Width of outer stroke
39 const CGFloat kOuterStrokeWidth = 1;
41 @interface DownloadShowAllCell(Private)
42 - (ui::ThemeProvider*)backgroundThemeWrappingProvider:
43     (ui::ThemeProvider*)provider;
44 - (BOOL)pressedWithDefaultTheme;
45 - (NSColor*)titleColor;
46 @end
48 @implementation DownloadShowAllCell
50 - (void)setInitialState {
51   [self setFont:[NSFont systemFontOfSize:
52       [NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
55 // For nib instantiations
56 - (id)initWithCoder:(NSCoder*)decoder {
57   if ((self = [super initWithCoder:decoder])) {
58     [self setInitialState];
59   }
60   return self;
63 // For programmatic instantiations.
64 - (id)initTextCell:(NSString*)string {
65   if ((self = [super initTextCell:string])) {
66     [self setInitialState];
67   }
68   return self;
71 - (void)setShowsBorderOnlyWhileMouseInside:(BOOL)showOnly {
72   // Override to make sure it doesn't do anything if it's called accidentally.
75 - (ui::ThemeProvider*)backgroundThemeWrappingProvider:
76     (ui::ThemeProvider*)provider {
77   if (!themeProvider_.get()) {
78     themeProvider_.reset(new BackgroundTheme(provider));
79   }
81   return themeProvider_.get();
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];
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];
99   return themeProvider->GetNSColor(ThemeProperties::COLOR_BOOKMARK_TEXT);
102 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
103   NSRect drawFrame = NSInsetRect(cellFrame, 1.5, 1.5);
104   NSRect innerFrame = NSInsetRect(cellFrame, 2, 2);
106   const float radius = 3;
107   NSWindow* window = [controlView window];
108   BOOL active = [window isKeyWindow] || [window isMainWindow];
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;
120   bool isDefaultTheme =
121       !themeProvider->HasCustomImage(IDR_THEME_BUTTON_BACKGROUND);
123   NSGradient* bgGradient = nil;
124   if (!isDefaultTheme) {
125     themeProvider = [self backgroundThemeWrappingProvider:themeProvider];
126     bgGradient = themeProvider->GetNSGradient(
127         active ? ThemeProperties::GRADIENT_TOOLBAR_BUTTON :
128                  ThemeProperties::GRADIENT_TOOLBAR_BUTTON_INACTIVE);
129   }
131   NSBezierPath* buttonInnerPath =
132       [NSBezierPath bezierPathWithRoundedRect:drawFrame
133                                       xRadius:radius
134                                       yRadius:radius];
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];
147   [self drawInteriorWithFrame:innerFrame inView:controlView];
150 - (NSDictionary*)textAttributes {
151   return [NSDictionary dictionaryWithObjectsAndKeys:
152       [self titleColor], NSForegroundColorAttributeName,
153       [self font], NSFontAttributeName,
154       nil];
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]];
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];
172 - (NSRect)imageRectForBounds:(NSRect)cellFrame {
173   return NSMakeRect(cellFrame.origin.x + kImagePaddingLeft,
174                     cellFrame.origin.y + kImagePaddingTop,
175                     kImageWidth,
176                     kImageHeight);
179 - (NSSize)cellSize {
180   NSSize size = [super cellSize];
182   // Custom width:
183   NSSize textSize = [[self title] sizeWithAttributes:[self textAttributes]];
184   size.width = kTextPosLeft + textSize.width + kTextPaddingRight +
185       kOuterStrokeWidth * 2;
186   return size;
188 @end