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.
5 #import "chrome/browser/ui/cocoa/autofill/autofill_pop_up_button.h"
7 #include <ApplicationServices/ApplicationServices.h>
9 #include "base/mac/scoped_nsobject.h"
10 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
12 @interface AutofillPopUpButton ()
13 - (void)didSelectItem:(id)sender;
16 @implementation AutofillPopUpButton
18 @synthesize delegate = delegate_;
21 return [AutofillPopUpCell class];
24 - (id)initWithFrame:(NSRect)frame pullsDown:(BOOL)pullsDown{
25 if (self = [super initWithFrame:frame pullsDown:pullsDown]) {
26 [self setTarget:self];
27 [self setAction:@selector(didSelectItem:)];
32 - (BOOL)becomeFirstResponder {
33 BOOL result = [super becomeFirstResponder];
34 if (result && delegate_)
35 [delegate_ fieldBecameFirstResponder:self];
39 - (NSString*)fieldValue {
40 return [[self cell] fieldValue];
43 - (void)setFieldValue:(NSString*)fieldValue {
44 [[self cell] setFieldValue:fieldValue];
47 - (NSString*)validityMessage {
48 return validityMessage_;
51 - (void)setValidityMessage:(NSString*)validityMessage {
52 validityMessage_.reset([validityMessage copy]);
53 [[self cell] setInvalid:[self invalid]];
54 [self setNeedsDisplay:YES];
58 return [validityMessage_ length] != 0;
61 - (NSString*)defaultValue {
62 return [[self cell] defaultValue];
65 - (void)setDefaultValue:(NSString*)defaultValue {
66 [[self cell] setDefaultValue:defaultValue];
70 return [[[self cell] fieldValue] isEqualToString:[[self cell] defaultValue]];
73 - (void)didSelectItem:(id)sender {
75 [delegate_ didEndEditing:self];
81 @implementation AutofillPopUpCell
83 @synthesize invalid = invalid_;
84 @synthesize defaultValue = defaultValue_;
86 // Draw a bezel that's highlighted.
87 - (void)drawBezelWithFrame:(NSRect)frame inView:(NSView*)controlView {
89 CGContextRef context = static_cast<CGContextRef>(
90 [[NSGraphicsContext currentContext] graphicsPort]);
92 // Create a highlight-shaded bezel in a transparency layer.
93 CGContextBeginTransparencyLayerWithRect(context, NSRectToCGRect(frame), 0);
95 [super drawBezelWithFrame:frame inView:controlView];
97 // 2. Use that as stencil against solid color rect.
98 [[NSColor redColor] set];
99 NSRectFillUsingOperation(frame, NSCompositeSourceAtop);
101 // 3. Composite the solid color bezel and the actual bezel.
102 CGContextSetBlendMode(context, kCGBlendModePlusDarker);
103 [super drawBezelWithFrame:frame inView:controlView];
104 CGContextEndTransparencyLayer(context);
106 [super drawBezelWithFrame:frame inView:controlView];
110 - (NSRect)drawTitle:(NSAttributedString*)title
111 withFrame:(NSRect)frame
112 inView:(NSView*)controlView {
114 // Draw with a color that has high contrast against the custom background.
115 base::scoped_nsobject<NSMutableAttributedString> coloredTitle(
116 [[NSMutableAttributedString alloc] initWithAttributedString:title]);
117 [coloredTitle addAttribute:NSForegroundColorAttributeName
118 value:[NSColor whiteColor]
119 range:NSMakeRange(0, [title length])];
120 return [super drawTitle:coloredTitle withFrame:frame inView:controlView];
122 return [super drawTitle:title withFrame:frame inView:controlView];
126 - (NSString*)fieldValue {
127 if (![self selectedItem])
128 return defaultValue_;
129 return [self titleOfSelectedItem];
132 - (void)setFieldValue:(NSString*)fieldValue {
133 [self selectItemWithTitle:fieldValue];
134 if (![self selectedItem])
135 [self selectItemWithTitle:defaultValue_];
136 if (![self selectedItem])
137 [self selectItemAtIndex:0];