Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / find_bar / find_bar_view.mm
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 #import "chrome/browser/ui/cocoa/find_bar/find_bar_view.h"
6
7 #import "chrome/browser/ui/cocoa/nsview_additions.h"
8 #import "chrome/browser/ui/cocoa/themed_window.h"
9 #import "chrome/browser/ui/cocoa/url_drop_target.h"
10 #import "chrome/browser/ui/cocoa/view_id_util.h"
11 #import "ui/base/cocoa/nsgraphics_context_additions.h"
12 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
13
14 namespace {
15 CGFloat kCurveSize = 8;
16 }  // end namespace
17
18 @implementation FindBarView
19
20 - (id)initWithFrame:(NSRect)frame {
21   if ((self = [super initWithFrame:frame])) {
22     // Give this view its own layer so that it can appear over the web contents
23     // view's layer. Layer squashing is not helpful for this view because
24     // NSTextField will correctly anti-alias text on 10.8 and beyond.
25     [self setWantsLayer:YES];
26   }
27   return self;
28 }
29
30 - (void)awakeFromNib {
31   // Register for all the drag types handled by the RWHVCocoa.
32   [self registerForDraggedTypes:[URLDropTargetHandler handledDragTypes]];
33 }
34
35 - (void)drawRect:(NSRect)rect {
36   const CGFloat lineWidth = [self cr_lineWidth];
37   const CGFloat halfLineWidth = lineWidth / 2.0;
38
39   // TODO(rohitrao): Make this prettier.
40   rect = NSInsetRect([self bounds], halfLineWidth, halfLineWidth);
41   rect = NSOffsetRect(rect, 0, lineWidth);
42
43   NSPoint topLeft = NSMakePoint(NSMinX(rect), NSMaxY(rect));
44   NSPoint topRight = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
45   NSPoint midLeft1 =
46       NSMakePoint(NSMinX(rect) + kCurveSize, NSMaxY(rect) - kCurveSize);
47   NSPoint midLeft2 =
48       NSMakePoint(NSMinX(rect) + kCurveSize, NSMinY(rect) + kCurveSize);
49   NSPoint midRight1 =
50       NSMakePoint(NSMaxX(rect) - kCurveSize, NSMinY(rect) + kCurveSize);
51   NSPoint midRight2 =
52       NSMakePoint(NSMaxX(rect) - kCurveSize, NSMaxY(rect) - kCurveSize);
53   NSPoint bottomLeft =
54       NSMakePoint(NSMinX(rect) + (2 * kCurveSize), NSMinY(rect));
55   NSPoint bottomRight =
56       NSMakePoint(NSMaxX(rect) - (2 * kCurveSize), NSMinY(rect));
57
58   NSBezierPath* path = [NSBezierPath bezierPath];
59   [path moveToPoint:topLeft];
60   [path curveToPoint:midLeft1
61         controlPoint1:NSMakePoint(midLeft1.x, topLeft.y)
62         controlPoint2:NSMakePoint(midLeft1.x, topLeft.y)];
63   [path lineToPoint:midLeft2];
64   [path curveToPoint:bottomLeft
65         controlPoint1:NSMakePoint(midLeft2.x, bottomLeft.y)
66         controlPoint2:NSMakePoint(midLeft2.x, bottomLeft.y)];
67
68   [path lineToPoint:bottomRight];
69   [path curveToPoint:midRight1
70         controlPoint1:NSMakePoint(midRight1.x, bottomLeft.y)
71         controlPoint2:NSMakePoint(midRight1.x, bottomLeft.y)];
72   [path lineToPoint:midRight2];
73   [path curveToPoint:topRight
74         controlPoint1:NSMakePoint(midRight2.x, topLeft.y)
75         controlPoint2:NSMakePoint(midRight2.x, topLeft.y)];
76
77   {
78     gfx::ScopedNSGraphicsContextSaveGState scopedGState;
79     NSGraphicsContext* context = [NSGraphicsContext currentContext];
80     [path addClip];
81
82     // Set the pattern phase
83     NSPoint position = [[self window]
84         themeImagePositionForAlignment:THEME_IMAGE_ALIGN_WITH_TAB_STRIP];
85     [context cr_setPatternPhase:position forView:self];
86
87     [super drawBackgroundWithOpaque:YES];
88   }
89
90   [[self strokeColor] set];
91   [path setLineWidth:lineWidth];
92   [path stroke];
93 }
94
95 // The findbar is mostly opaque, but has an 8px transparent border on the left
96 // and right sides (see |kCurveSize|).  This is an artifact of the way it is
97 // drawn.  We override hitTest to return nil for points in this transparent
98 // area.
99 - (NSView*)hitTest:(NSPoint)point {
100   NSView* hitView = [super hitTest:point];
101   if (hitView == self) {
102     // |rect| is approximately equivalent to the opaque area of the findbar.
103     NSRect rect = NSInsetRect([self bounds], kCurveSize, 0);
104     if (!NSMouseInRect(point, rect, [self isFlipped]))
105       return nil;
106   }
107
108   return hitView;
109 }
110
111 // Eat all mouse events, to prevent clicks from falling through to views below.
112 - (void)mouseDown:(NSEvent *)theEvent {
113 }
114
115 - (void)rightMouseDown:(NSEvent *)theEvent {
116 }
117
118 - (void)otherMouseDown:(NSEvent *)theEvent {
119 }
120
121 - (void)mouseUp:(NSEvent *)theEvent {
122 }
123
124 - (void)rightMouseUp:(NSEvent *)theEvent {
125 }
126
127 - (void)otherMouseUp:(NSEvent *)theEvent {
128 }
129
130 - (void)mouseMoved:(NSEvent *)theEvent {
131 }
132
133 - (void)mouseDragged:(NSEvent *)theEvent {
134 }
135
136 - (void)rightMouseDragged:(NSEvent *)theEvent {
137 }
138
139 - (void)otherMouseDragged:(NSEvent *)theEvent {
140 }
141
142 // Eat drag operations, to prevent drags from going through to the views below.
143 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)info {
144   return NSDragOperationNone;
145 }
146
147 // Specifies that mouse events over this view should be ignored by the
148 // render host.
149 - (BOOL)nonWebContentView {
150   return YES;
151 }
152
153 @end