- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / omnibox / omnibox_popup_cell.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/omnibox/omnibox_popup_cell.h"
6
7 #include <cmath>
8
9 namespace {
10
11 // How far to offset image column from the left.
12 const CGFloat kImageXOffset = 5.0;
13
14 // How far to offset the text column from the left.
15 const CGFloat kTextXOffset = 28.0;
16
17 // Rounding radius of selection and hover background on popup items.
18 const CGFloat kCellRoundingRadius = 2.0;
19
20 NSColor* SelectedBackgroundColor() {
21   return [NSColor selectedControlColor];
22 }
23 NSColor* HoveredBackgroundColor() {
24   return [NSColor controlHighlightColor];
25 }
26
27 }  // namespace
28
29 @implementation OmniboxPopupCell
30
31 - (id)init {
32   self = [super init];
33   if (self) {
34     [self setImagePosition:NSImageLeft];
35     [self setBordered:NO];
36     [self setButtonType:NSRadioButton];
37
38     // Without this highlighting messes up white areas of images.
39     [self setHighlightsBy:NSNoCellMask];
40   }
41   return self;
42 }
43
44 // The default NSButtonCell drawing leaves the image flush left and
45 // the title next to the image.  This spaces things out to line up
46 // with the star button and autocomplete field.
47 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
48   if ([self state] == NSOnState || [self isHighlighted]) {
49     if ([self state] == NSOnState)
50       [SelectedBackgroundColor() set];
51     else
52       [HoveredBackgroundColor() set];
53     NSBezierPath* path =
54         [NSBezierPath bezierPathWithRoundedRect:cellFrame
55                                         xRadius:kCellRoundingRadius
56                                         yRadius:kCellRoundingRadius];
57     [path fill];
58   }
59
60   // Put the image centered vertically but in a fixed column.
61   NSImage* image = [self image];
62   if (image) {
63     NSRect imageRect = cellFrame;
64     imageRect.size = [image size];
65     imageRect.origin.y +=
66         std::floor((NSHeight(cellFrame) - NSHeight(imageRect)) / 2.0);
67     imageRect.origin.x += kImageXOffset;
68     [image drawInRect:imageRect
69              fromRect:NSZeroRect  // Entire image
70             operation:NSCompositeSourceOver
71              fraction:1.0
72        respectFlipped:YES
73                 hints:nil];
74   }
75
76   // Adjust the title position to be lined up under the field's text.
77   NSAttributedString* title = [self attributedTitle];
78   if (title && [title length]) {
79     NSRect titleRect = cellFrame;
80     titleRect.size.width -= kTextXOffset;
81     titleRect.origin.x += kTextXOffset;
82     [self drawTitle:title withFrame:titleRect inView:controlView];
83   }
84 }
85
86 @end