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 inputDelegate = inputDelegate_;
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 && inputDelegate_)
35 [inputDelegate_ 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 [inputDelegate_ didChange:self];
76 [inputDelegate_ didEndEditing:self];
83 @implementation AutofillPopUpCell
85 @synthesize invalid = invalid_;
86 @synthesize defaultValue = defaultValue_;
88 // Draw a bezel that's highlighted.
89 - (void)drawBezelWithFrame:(NSRect)frame inView:(NSView*)controlView {
90 [super drawBezelWithFrame:frame inView:controlView];
92 // Mimic the rounded rect of default popup bezel in size, outline in red.
93 NSRect outlineRect = NSOffsetRect(NSInsetRect(frame, 3.0, 3.5), -.5, -1.0);
94 NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:outlineRect
97 [path setLineWidth:0];
98 [[NSColor redColor] setStroke];
103 - (NSString*)fieldValue {
104 if (![self selectedItem])
105 return defaultValue_;
106 return [self titleOfSelectedItem];
109 - (void)setFieldValue:(NSString*)fieldValue {
110 [self selectItemWithTitle:fieldValue];
111 if (![self selectedItem])
112 [self selectItemWithTitle:defaultValue_];
113 if (![self selectedItem])
114 [self selectItemAtIndex:0];