- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / browser / avatar_label_button.mm
1 // Copyright 2013 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/browser/avatar_label_button.h"
6
7 #include "chrome/browser/themes/theme_properties.h"
8 #include "chrome/browser/ui/cocoa/themed_window.h"
9 #include "grit/generated_resources.h"
10 #include "grit/theme_resources.h"
11 #include "ui/base/cocoa/appkit_utils.h"
12 #include "ui/base/l10n/l10n_util_mac.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/base/theme_provider.h"
15
16 namespace {
17
18 // Space between the left edge of the label background and the left edge of the
19 // label text.
20 const CGFloat kLabelTextLeftSpacing = 10;
21
22 // Space between the right edge of the label text and the avatar icon.
23 const CGFloat kLabelTextRightSpacing = 4;
24
25 // Space between the top edge of the label background and the top edge of the
26 // label text.
27 const CGFloat kLabelTextTopSpacing = 3;
28
29 // Space between the bottom edge of the label background and the bottom edge of
30 // the label text.
31 const CGFloat kLabelTextBottomSpacing = 4;
32
33 }  // namespace
34
35 @implementation AvatarLabelButton
36
37 - (id)initWithFrame:(NSRect)frameRect {
38   if ((self = [super initWithFrame:frameRect])) {
39     [self setBezelStyle:NSSmallSquareBezelStyle];
40     [self setTitle:l10n_util::GetNSString(IDS_MANAGED_USER_AVATAR_LABEL)];
41     [self setFont:[NSFont labelFontOfSize:12.0]];
42     // Increase the frame by the size of the label to be displayed.
43     NSSize textSize = [[self cell] labelTextSize];
44     frameRect.size = NSMakeSize(frameRect.size.width + textSize.width,
45                                 frameRect.size.height + textSize.height);
46     [self setFrame:frameRect];
47   }
48   return self;
49 }
50
51 + (Class)cellClass {
52   return [AvatarLabelButtonCell class];
53 }
54
55 @end
56
57 @implementation AvatarLabelButtonCell
58
59 - (NSSize)labelTextSize {
60   NSSize size = [[self attributedTitle] size];
61   size.width += kLabelTextLeftSpacing + kLabelTextRightSpacing;
62   size.height += kLabelTextTopSpacing + kLabelTextBottomSpacing;
63   return size;
64 }
65
66 - (void)drawBezelWithFrame:(NSRect)frame inView:(NSView*)controlView {
67   ui::NinePartImageIds imageIds = {
68     IDR_MANAGED_USER_LABEL_TOP_LEFT,
69     IDR_MANAGED_USER_LABEL_TOP,
70     IDR_MANAGED_USER_LABEL_TOP_RIGHT,
71     IDR_MANAGED_USER_LABEL_LEFT,
72     IDR_MANAGED_USER_LABEL_CENTER,
73     IDR_MANAGED_USER_LABEL_RIGHT,
74     IDR_MANAGED_USER_LABEL_BOTTOM_LEFT,
75     IDR_MANAGED_USER_LABEL_BOTTOM,
76     IDR_MANAGED_USER_LABEL_BOTTOM_RIGHT
77   };
78   ui::DrawNinePartImage(frame, imageIds, NSCompositeSourceOver, 1.0, true);
79 }
80
81 - (NSRect)titleRectForBounds:(NSRect)theRect {
82   theRect.origin = NSMakePoint(kLabelTextLeftSpacing, kLabelTextBottomSpacing);
83   theRect.size = [[self attributedTitle] size];
84   return theRect;
85 }
86
87 - (NSRect)drawTitle:(NSAttributedString*)title
88           withFrame:(NSRect)frame
89              inView:(NSView*)controlView {
90   base::scoped_nsobject<NSMutableAttributedString> themedTitle(
91       [[NSMutableAttributedString alloc] initWithAttributedString:title]);
92   ui::ThemeProvider* themeProvider = [[controlView window] themeProvider];
93   if (themeProvider) {
94     NSColor* textColor = themeProvider->GetNSColor(
95         ThemeProperties::COLOR_MANAGED_USER_LABEL);
96     [themedTitle addAttribute:NSForegroundColorAttributeName
97                         value:textColor
98                         range:NSMakeRange(0, title.length)];
99   }
100   [themedTitle drawInRect:frame];
101   return frame;
102 }
103
104 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
105   ui::ThemeProvider* themeProvider = [[controlView window] themeProvider];
106   if (themeProvider) {
107     // Draw the label button background using the color provided by
108     // |themeProvider|. First paint the border.
109     NSColor* borderColor = themeProvider->GetNSColor(
110         ThemeProperties::COLOR_MANAGED_USER_LABEL_BORDER);
111     if ([self isHighlighted]) {
112       borderColor = [borderColor blendedColorWithFraction:0.5
113                                                   ofColor:[NSColor blackColor]];
114     }
115     NSSize frameSize = cellFrame.size;
116     NSRect backgroundRect;
117     backgroundRect.origin = NSMakePoint(1, 1);
118     backgroundRect.size = NSMakeSize(frameSize.width - 2, frameSize.height - 2);
119     NSBezierPath* path =
120         [NSBezierPath bezierPathWithRoundedRect:backgroundRect
121                                         xRadius:2.0
122                                         yRadius:2.0];
123     [borderColor set];
124     [path fill];
125
126     // Now paint the background.
127     NSColor* backgroundColor = themeProvider->GetNSColor(
128         ThemeProperties::COLOR_MANAGED_USER_LABEL_BACKGROUND);
129     if ([self isHighlighted]) {
130       backgroundColor =
131           [backgroundColor blendedColorWithFraction:0.5
132                                             ofColor:[NSColor blackColor]];
133     }
134     backgroundRect.origin = NSMakePoint(2, 2);
135     backgroundRect.size = NSMakeSize(frameSize.width - 4, frameSize.height - 4);
136     path = [NSBezierPath bezierPathWithRoundedRect:backgroundRect
137                                            xRadius:2.0
138                                            yRadius:2.0];
139     [backgroundColor set];
140     [path fill];
141   }
142   [super drawInteriorWithFrame:cellFrame inView:controlView];
143 }
144
145 @end