- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / autofill / autofill_pop_up_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/autofill/autofill_pop_up_button.h"
6
7 #include <ApplicationServices/ApplicationServices.h>
8
9 #include "base/mac/scoped_nsobject.h"
10 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
11
12 @interface AutofillPopUpButton ()
13 - (void)didSelectItem:(id)sender;
14 @end
15
16 @implementation AutofillPopUpButton
17
18 @synthesize delegate = delegate_;
19
20 + (Class)cellClass {
21   return [AutofillPopUpCell class];
22 }
23
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:)];
28   }
29   return self;
30 }
31
32 - (BOOL)becomeFirstResponder {
33   BOOL result = [super becomeFirstResponder];
34   if (result && delegate_)
35     [delegate_ fieldBecameFirstResponder:self];
36   return result;
37 }
38
39 - (NSString*)fieldValue {
40   return [[self cell] fieldValue];
41 }
42
43 - (void)setFieldValue:(NSString*)fieldValue {
44   [[self cell] setFieldValue:fieldValue];
45 }
46
47 - (NSString*)validityMessage {
48   return validityMessage_;
49 }
50
51 - (void)setValidityMessage:(NSString*)validityMessage {
52   validityMessage_.reset([validityMessage copy]);
53   [[self cell] setInvalid:[self invalid]];
54   [self setNeedsDisplay:YES];
55 }
56
57 - (BOOL)invalid {
58   return [validityMessage_ length] != 0;
59 }
60
61 - (NSString*)defaultValue {
62   return [[self cell] defaultValue];
63 }
64
65 - (void)setDefaultValue:(NSString*)defaultValue {
66   [[self cell] setDefaultValue:defaultValue];
67 }
68
69 - (BOOL)isDefault {
70   return [[[self cell] fieldValue] isEqualToString:[[self cell] defaultValue]];
71 }
72
73 - (void)didSelectItem:(id)sender {
74   if (delegate_)
75     [delegate_ didEndEditing:self];
76 }
77
78 @end
79
80
81 @implementation AutofillPopUpCell
82
83 @synthesize invalid = invalid_;
84 @synthesize defaultValue = defaultValue_;
85
86 // Draw a bezel that's highlighted.
87 - (void)drawBezelWithFrame:(NSRect)frame inView:(NSView*)controlView {
88   if (invalid_) {
89     CGContextRef context = static_cast<CGContextRef>(
90         [[NSGraphicsContext currentContext] graphicsPort]);
91
92     // Create a highlight-shaded bezel in a transparency layer.
93     CGContextBeginTransparencyLayerWithRect(context, NSRectToCGRect(frame), 0);
94     // 1. Draw bezel.
95     [super drawBezelWithFrame:frame inView:controlView];
96
97     // 2. Use that as stencil against solid color rect.
98     [[NSColor redColor] set];
99     NSRectFillUsingOperation(frame, NSCompositeSourceAtop);
100
101     // 3. Composite the solid color bezel and the actual bezel.
102     CGContextSetBlendMode(context, kCGBlendModePlusDarker);
103     [super drawBezelWithFrame:frame inView:controlView];
104     CGContextEndTransparencyLayer(context);
105   } else {
106     [super drawBezelWithFrame:frame inView:controlView];
107   }
108 }
109
110 - (NSRect)drawTitle:(NSAttributedString*)title
111           withFrame:(NSRect)frame
112              inView:(NSView*)controlView {
113   if (invalid_) {
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];
121   } else {
122     return [super drawTitle:title withFrame:frame inView:controlView];
123   }
124 }
125
126 - (NSString*)fieldValue {
127   if (![self selectedItem])
128     return defaultValue_;
129   return [self titleOfSelectedItem];
130 }
131
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];
138 }
139
140 @end