REGRESSION (188611): Search field Cancel button should not overlap search text on...
[WebKit-https.git] / Source / WebCore / rendering / RenderThemeMac.mm
1 /*
2  * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 #import "config.h"
20
21 #if !PLATFORM(IOS)
22
23 #import "RenderThemeMac.h"
24
25 #import "BitmapImage.h"
26 #import "CSSValueKeywords.h"
27 #import "CSSValueList.h"
28 #import "ColorMac.h"
29 #import "CoreGraphicsSPI.h"
30 #import "Document.h"
31 #import "Element.h"
32 #import "ExceptionCodePlaceholder.h"
33 #import "FileList.h"
34 #import "FloatRoundedRect.h"
35 #import "FocusController.h"
36 #import "Frame.h"
37 #import "FrameSelection.h"
38 #import "FrameView.h"
39 #import "GraphicsContextCG.h"
40 #import "HTMLAttachmentElement.h"
41 #import "HTMLAudioElement.h"
42 #import "HTMLInputElement.h"
43 #import "HTMLMediaElement.h"
44 #import "HTMLNames.h"
45 #import "HTMLPlugInImageElement.h"
46 #import "Icon.h"
47 #import "Image.h"
48 #import "ImageBuffer.h"
49 #import "LocalCurrentGraphicsContext.h"
50 #import "LocalizedStrings.h"
51 #import "MediaControlElements.h"
52 #import "NSColorSPI.h"
53 #import "NSSharingServicePickerSPI.h"
54 #import "Page.h"
55 #import "PaintInfo.h"
56 #import "PathUtilities.h"
57 #import "RenderAttachment.h"
58 #import "RenderLayer.h"
59 #import "RenderMedia.h"
60 #import "RenderMediaControlElements.h"
61 #import "RenderMediaControls.h"
62 #import "RenderProgress.h"
63 #import "RenderSlider.h"
64 #import "RenderSnapshottedPlugIn.h"
65 #import "RenderView.h"
66 #import "SharedBuffer.h"
67 #import "StringTruncator.h"
68 #import "StyleResolver.h"
69 #import "ThemeMac.h"
70 #import "TimeRanges.h"
71 #import "UserAgentScripts.h"
72 #import "UserAgentStyleSheets.h"
73 #import "WebCoreSystemInterface.h"
74 #import <wtf/RetainPtr.h>
75 #import <wtf/RetainPtr.h>
76 #import <wtf/StdLibExtras.h>
77 #import <wtf/text/StringBuilder.h>
78 #import <Carbon/Carbon.h>
79 #import <Cocoa/Cocoa.h>
80 #import <math.h>
81
82 #if ENABLE(METER_ELEMENT)
83 #import "RenderMeter.h"
84 #import "HTMLMeterElement.h"
85 #endif
86
87 #if defined(__LP64__) && __LP64__
88 #define HAVE_APPKIT_SERVICE_CONTROLS_SUPPORT 1
89 #else
90 #define HAVE_APPKIT_SERVICE_CONTROLS_SUPPORT 0
91 #endif
92
93 #if ENABLE(SERVICE_CONTROLS) && HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
94
95 #if USE(APPLE_INTERNAL_SDK)
96 #import <AppKit/AppKitDefines_Private.h>
97 #import <AppKit/NSServicesRolloverButtonCell.h>
98 #else
99 #define APPKIT_PRIVATE_CLASS
100 @interface NSServicesRolloverButtonCell : NSButtonCell
101 @end
102 #endif
103
104 @interface NSServicesRolloverButtonCell ()
105 + (NSServicesRolloverButtonCell *)serviceRolloverButtonCellForStyle:(NSSharingServicePickerStyle)style;
106 - (NSRect)rectForBounds:(NSRect)bounds preferredEdge:(NSRectEdge)preferredEdge;
107 @end
108
109 #endif // ENABLE(SERVICE_CONTROLS)
110
111 // The methods in this file are specific to the Mac OS X platform.
112
113 // We estimate the animation rate of a Mac OS X progress bar is 33 fps.
114 // Hard code the value here because we haven't found API for it.
115 const double progressAnimationFrameRate = 0.033;
116
117 // Mac OS X progress bar animation seems to have 256 frames.
118 const double progressAnimationNumFrames = 256;
119
120 @interface WebCoreRenderThemeNotificationObserver : NSObject
121 {
122     WebCore::RenderTheme *_theme;
123 }
124
125 - (id)initWithTheme:(WebCore::RenderTheme *)theme;
126 - (void)systemColorsDidChange:(NSNotification *)notification;
127
128 @end
129
130 @implementation WebCoreRenderThemeNotificationObserver
131
132 - (id)initWithTheme:(WebCore::RenderTheme *)theme
133 {
134     if (!(self = [super init]))
135         return nil;
136
137     _theme = theme;
138     return self;
139 }
140
141 - (void)systemColorsDidChange:(NSNotification *)unusedNotification
142 {
143     ASSERT_UNUSED(unusedNotification, [[unusedNotification name] isEqualToString:NSSystemColorsDidChangeNotification]);
144     _theme->platformColorsDidChange();
145 }
146
147 @end
148
149 @interface NSTextFieldCell (WKDetails)
150 - (CFDictionaryRef)_coreUIDrawOptionsWithFrame:(NSRect)cellFrame inView:(NSView *)controlView includeFocus:(BOOL)includeFocus;
151 @end
152
153
154 @interface WebCoreTextFieldCell : NSTextFieldCell
155 - (CFDictionaryRef)_coreUIDrawOptionsWithFrame:(NSRect)cellFrame inView:(NSView *)controlView includeFocus:(BOOL)includeFocus;
156 @end
157
158 @implementation WebCoreTextFieldCell
159 - (CFDictionaryRef)_coreUIDrawOptionsWithFrame:(NSRect)cellFrame inView:(NSView *)controlView includeFocus:(BOOL)includeFocus
160 {
161     // FIXME: This is a post-Lion-only workaround for <rdar://problem/11385461>. When that bug is resolved, we should remove this code.
162     CFMutableDictionaryRef coreUIDrawOptions = CFDictionaryCreateMutableCopy(NULL, 0, [super _coreUIDrawOptionsWithFrame:cellFrame inView:controlView includeFocus:includeFocus]);
163     CFDictionarySetValue(coreUIDrawOptions, @"borders only", kCFBooleanTrue);
164     return (CFDictionaryRef)[NSMakeCollectable(coreUIDrawOptions) autorelease];
165 }
166 @end
167
168 @interface WebCoreRenderThemeBundle : NSObject
169 @end
170
171 @implementation WebCoreRenderThemeBundle
172 @end
173
174 @interface NSSearchFieldCell()
175 @property (getter=isCenteredLook) BOOL centeredLook;
176 @end
177
178 namespace WebCore {
179
180 using namespace HTMLNames;
181
182 enum {
183     topMargin,
184     rightMargin,
185     bottomMargin,
186     leftMargin
187 };
188
189 enum {
190     topPadding,
191     rightPadding,
192     bottomPadding,
193     leftPadding
194 };
195
196 PassRefPtr<RenderTheme> RenderTheme::themeForPage(Page*)
197 {
198     static RenderTheme& rt = RenderThemeMac::create().leakRef();
199     return &rt;
200 }
201
202 Ref<RenderTheme> RenderThemeMac::create()
203 {
204     return adoptRef(*new RenderThemeMac);
205 }
206
207 RenderThemeMac::RenderThemeMac()
208     : m_isSliderThumbHorizontalPressed(false)
209     , m_isSliderThumbVerticalPressed(false)
210     , m_notificationObserver(adoptNS([[WebCoreRenderThemeNotificationObserver alloc] initWithTheme:this]))
211 {
212     [[NSNotificationCenter defaultCenter] addObserver:m_notificationObserver.get()
213                                                         selector:@selector(systemColorsDidChange:)
214                                                             name:NSSystemColorsDidChangeNotification
215                                                           object:nil];
216 }
217
218 RenderThemeMac::~RenderThemeMac()
219 {
220     [[NSNotificationCenter defaultCenter] removeObserver:m_notificationObserver.get()];
221 }
222
223 NSView* RenderThemeMac::documentViewFor(const RenderObject& o) const
224 {
225     ControlStates states(extractControlStatesForRenderer(o));
226     return ThemeMac::ensuredView(&o.view().frameView(), states);
227 }
228
229 #if ENABLE(VIDEO)
230 String RenderThemeMac::mediaControlsStyleSheet()
231 {
232 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
233     if (m_mediaControlsStyleSheet.isEmpty())
234         m_mediaControlsStyleSheet = [NSString stringWithContentsOfFile:[[NSBundle bundleForClass:[WebCoreRenderThemeBundle class]] pathForResource:@"mediaControlsApple" ofType:@"css"] encoding:NSUTF8StringEncoding error:nil];
235     return m_mediaControlsStyleSheet;
236 #else
237     return emptyString();
238 #endif
239 }
240
241 String RenderThemeMac::mediaControlsScript()
242 {
243 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
244     if (m_mediaControlsScript.isEmpty()) {
245         StringBuilder scriptBuilder;
246         scriptBuilder.append([NSString stringWithContentsOfFile:[[NSBundle bundleForClass:[WebCoreRenderThemeBundle class]] pathForResource:@"mediaControlsLocalizedStrings" ofType:@"js"] encoding:NSUTF8StringEncoding error:nil]);
247         scriptBuilder.append([NSString stringWithContentsOfFile:[[NSBundle bundleForClass:[WebCoreRenderThemeBundle class]] pathForResource:@"mediaControlsApple" ofType:@"js"] encoding:NSUTF8StringEncoding error:nil]);
248         m_mediaControlsScript = scriptBuilder.toString();
249     }
250     return m_mediaControlsScript;
251 #else
252     return emptyString();
253 #endif
254 }
255
256 #endif // ENABLE(VIDEO)
257
258
259 #if ENABLE(SERVICE_CONTROLS)
260 String RenderThemeMac::imageControlsStyleSheet() const
261 {
262     return String(imageControlsMacUserAgentStyleSheet, sizeof(imageControlsMacUserAgentStyleSheet));
263 }
264 #endif
265
266 Color RenderThemeMac::platformActiveSelectionBackgroundColor() const
267 {
268     NSColor* color = [[NSColor selectedTextBackgroundColor] colorUsingColorSpaceName:NSDeviceRGBColorSpace];
269     return Color(static_cast<int>(255.0 * [color redComponent]), static_cast<int>(255.0 * [color greenComponent]), static_cast<int>(255.0 * [color blueComponent]));
270 }
271
272 Color RenderThemeMac::platformInactiveSelectionBackgroundColor() const
273 {
274     NSColor* color = [[NSColor secondarySelectedControlColor] colorUsingColorSpaceName:NSDeviceRGBColorSpace];
275     return Color(static_cast<int>(255.0 * [color redComponent]), static_cast<int>(255.0 * [color greenComponent]), static_cast<int>(255.0 * [color blueComponent]));
276 }
277
278 Color RenderThemeMac::platformActiveListBoxSelectionBackgroundColor() const
279 {
280     NSColor* color = [[NSColor alternateSelectedControlColor] colorUsingColorSpaceName:NSDeviceRGBColorSpace];
281     return Color(static_cast<int>(255.0 * [color redComponent]), static_cast<int>(255.0 * [color greenComponent]), static_cast<int>(255.0 * [color blueComponent]));
282 }
283
284 Color RenderThemeMac::platformActiveListBoxSelectionForegroundColor() const
285 {
286     return Color::white;
287 }
288
289 Color RenderThemeMac::platformInactiveListBoxSelectionForegroundColor() const
290 {
291     return Color::black;
292 }
293
294 Color RenderThemeMac::platformFocusRingColor() const
295 {
296     if (usesTestModeFocusRingColor())
297         return oldAquaFocusRingColor();
298
299     return systemColor(CSSValueWebkitFocusRingColor);
300 }
301
302 Color RenderThemeMac::platformInactiveListBoxSelectionBackgroundColor() const
303 {
304     return platformInactiveSelectionBackgroundColor();
305 }
306
307 static FontWeight toFontWeight(NSInteger appKitFontWeight)
308 {
309     ASSERT(appKitFontWeight > 0 && appKitFontWeight < 15);
310     if (appKitFontWeight > 14)
311         appKitFontWeight = 14;
312     else if (appKitFontWeight < 1)
313         appKitFontWeight = 1;
314
315     static const FontWeight fontWeights[] = {
316         FontWeight100,
317         FontWeight100,
318         FontWeight200,
319         FontWeight300,
320         FontWeight400,
321         FontWeight500,
322         FontWeight600,
323         FontWeight600,
324         FontWeight700,
325         FontWeight800,
326         FontWeight800,
327         FontWeight900,
328         FontWeight900,
329         FontWeight900
330     };
331     return fontWeights[appKitFontWeight - 1];
332 }
333
334 void RenderThemeMac::updateCachedSystemFontDescription(CSSValueID cssValueId, FontCascadeDescription& fontDescription) const
335 {
336     NSFont* font;
337     // System-font-ness can't be encapsulated by simply a font name. Instead, we must use a token
338     // which FontCache will look for.
339     // Make sure we keep this list of possible tokens in sync with FontCascade::primaryFontIsSystemFont()
340     AtomicString fontName;
341     switch (cssValueId) {
342         case CSSValueSmallCaption:
343             font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
344             break;
345         case CSSValueMenu:
346             font = [NSFont menuFontOfSize:[NSFont systemFontSize]];
347             fontName = AtomicString("-apple-menu", AtomicString::ConstructFromLiteral);
348             break;
349         case CSSValueStatusBar:
350             font = [NSFont labelFontOfSize:[NSFont labelFontSize]];
351             fontName = AtomicString("-apple-status-bar", AtomicString::ConstructFromLiteral);
352             break;
353         case CSSValueWebkitMiniControl:
354             font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]];
355             break;
356         case CSSValueWebkitSmallControl:
357             font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]];
358             break;
359         case CSSValueWebkitControl:
360             font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]];
361             break;
362         default:
363             font = [NSFont systemFontOfSize:[NSFont systemFontSize]];
364     }
365
366     if (!font)
367         return;
368
369     if (fontName.isNull())
370         fontName = AtomicString("-apple-system", AtomicString::ConstructFromLiteral);
371
372     NSFontManager *fontManager = [NSFontManager sharedFontManager];
373     fontDescription.setIsAbsoluteSize(true);
374     fontDescription.setOneFamily(fontName);
375     fontDescription.setSpecifiedSize([font pointSize]);
376     fontDescription.setWeight(toFontWeight([fontManager weightOfFont:font]));
377     fontDescription.setIsItalic([fontManager traitsOfFont:font] & NSItalicFontMask);
378 }
379
380 static RGBA32 convertNSColorToColor(NSColor *color)
381 {
382     NSColor *colorInColorSpace = [color colorUsingColorSpaceName:NSDeviceRGBColorSpace];
383     if (colorInColorSpace) {
384         static const double scaleFactor = nextafter(256.0, 0.0);
385         return makeRGB(static_cast<int>(scaleFactor * [colorInColorSpace redComponent]),
386             static_cast<int>(scaleFactor * [colorInColorSpace greenComponent]),
387             static_cast<int>(scaleFactor * [colorInColorSpace blueComponent]));
388     }
389
390     // This conversion above can fail if the NSColor in question is an NSPatternColor
391     // (as many system colors are). These colors are actually a repeating pattern
392     // not just a solid color. To work around this we simply draw a 1x1 image of
393     // the color and use that pixel's color. It might be better to use an average of
394     // the colors in the pattern instead.
395     NSBitmapImageRep *offscreenRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
396                                                                              pixelsWide:1
397                                                                              pixelsHigh:1
398                                                                           bitsPerSample:8
399                                                                         samplesPerPixel:4
400                                                                                hasAlpha:YES
401                                                                                isPlanar:NO
402                                                                          colorSpaceName:NSDeviceRGBColorSpace
403                                                                             bytesPerRow:4
404                                                                            bitsPerPixel:32];
405
406     [NSGraphicsContext saveGraphicsState];
407     [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep]];
408     NSEraseRect(NSMakeRect(0, 0, 1, 1));
409     [color drawSwatchInRect:NSMakeRect(0, 0, 1, 1)];
410     [NSGraphicsContext restoreGraphicsState];
411
412     NSUInteger pixel[4];
413     [offscreenRep getPixel:pixel atX:0 y:0];
414
415     [offscreenRep release];
416
417     return makeRGB(pixel[0], pixel[1], pixel[2]);
418 }
419
420 static RGBA32 menuBackgroundColor()
421 {
422     NSBitmapImageRep *offscreenRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
423                                                                              pixelsWide:1
424                                                                              pixelsHigh:1
425                                                                           bitsPerSample:8
426                                                                         samplesPerPixel:4
427                                                                                hasAlpha:YES
428                                                                                isPlanar:NO
429                                                                          colorSpaceName:NSDeviceRGBColorSpace
430                                                                             bytesPerRow:4
431                                                                            bitsPerPixel:32];
432
433     CGContextRef context = static_cast<CGContextRef>([[NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep] graphicsPort]);
434     CGRect rect = CGRectMake(0, 0, 1, 1);
435     HIThemeMenuDrawInfo drawInfo;
436     drawInfo.version =  0;
437     drawInfo.menuType = kThemeMenuTypePopUp;
438     HIThemeDrawMenuBackground(&rect, &drawInfo, context, kHIThemeOrientationInverted);
439
440     NSUInteger pixel[4];
441     [offscreenRep getPixel:pixel atX:0 y:0];
442
443     [offscreenRep release];
444
445     return makeRGB(pixel[0], pixel[1], pixel[2]);
446 }
447
448 void RenderThemeMac::platformColorsDidChange()
449 {
450     m_systemColorCache.clear();
451     RenderTheme::platformColorsDidChange();
452 }
453
454 Color RenderThemeMac::systemColor(CSSValueID cssValueID) const
455 {
456     auto addResult = m_systemColorCache.add(cssValueID, Color());
457     if (!addResult.isNewEntry)
458         return addResult.iterator->value;
459
460     Color color;
461     switch (cssValueID) {
462     case CSSValueActiveborder:
463         color = convertNSColorToColor([NSColor keyboardFocusIndicatorColor]);
464         break;
465     case CSSValueActivebuttontext:
466         // There is no corresponding NSColor for this so we use a hard coded value.
467         color = Color::white;
468         break;
469     case CSSValueActivecaption:
470         color = convertNSColorToColor([NSColor windowFrameTextColor]);
471         break;
472     case CSSValueAppworkspace:
473         color = convertNSColorToColor([NSColor headerColor]);
474         break;
475     case CSSValueBackground:
476         // Use theme independent default
477         break;
478     case CSSValueButtonface:
479         // We use this value instead of NSColor's controlColor to avoid website incompatibilities.
480         // We may want to change this to use the NSColor in future.
481         color = 0xFFC0C0C0;
482         break;
483     case CSSValueButtonhighlight:
484         color = convertNSColorToColor([NSColor controlHighlightColor]);
485         break;
486     case CSSValueButtonshadow:
487         color = convertNSColorToColor([NSColor controlShadowColor]);
488         break;
489     case CSSValueButtontext:
490         color = convertNSColorToColor([NSColor controlTextColor]);
491         break;
492     case CSSValueCaptiontext:
493         color = convertNSColorToColor([NSColor textColor]);
494         break;
495     case CSSValueGraytext:
496         color = convertNSColorToColor([NSColor disabledControlTextColor]);
497         break;
498     case CSSValueHighlight:
499         color = convertNSColorToColor([NSColor selectedTextBackgroundColor]);
500         break;
501     case CSSValueHighlighttext:
502         color = convertNSColorToColor([NSColor selectedTextColor]);
503         break;
504     case CSSValueInactiveborder:
505         color = convertNSColorToColor([NSColor controlBackgroundColor]);
506         break;
507     case CSSValueInactivecaption:
508         color = convertNSColorToColor([NSColor controlBackgroundColor]);
509         break;
510     case CSSValueInactivecaptiontext:
511         color = convertNSColorToColor([NSColor textColor]);
512         break;
513     case CSSValueInfobackground:
514         // There is no corresponding NSColor for this so we use a hard coded value.
515         color = 0xFFFBFCC5;
516         break;
517     case CSSValueInfotext:
518         color = convertNSColorToColor([NSColor textColor]);
519         break;
520     case CSSValueMenu:
521         color = menuBackgroundColor();
522         break;
523     case CSSValueMenutext:
524         color = convertNSColorToColor([NSColor selectedMenuItemTextColor]);
525         break;
526     case CSSValueScrollbar:
527         color = convertNSColorToColor([NSColor scrollBarColor]);
528         break;
529     case CSSValueText:
530         color = convertNSColorToColor([NSColor textColor]);
531         break;
532     case CSSValueThreeddarkshadow:
533         color = convertNSColorToColor([NSColor controlDarkShadowColor]);
534         break;
535     case CSSValueThreedshadow:
536         color = convertNSColorToColor([NSColor shadowColor]);
537         break;
538     case CSSValueThreedface:
539         // We use this value instead of NSColor's controlColor to avoid website incompatibilities.
540         // We may want to change this to use the NSColor in future.
541         color = 0xFFC0C0C0;
542         break;
543     case CSSValueThreedhighlight:
544         color = convertNSColorToColor([NSColor highlightColor]);
545         break;
546     case CSSValueThreedlightshadow:
547         color = convertNSColorToColor([NSColor controlLightHighlightColor]);
548         break;
549     case CSSValueWebkitFocusRingColor:
550         color = convertNSColorToColor([NSColor keyboardFocusIndicatorColor]);
551         break;
552     case CSSValueWindow:
553         color = convertNSColorToColor([NSColor windowBackgroundColor]);
554         break;
555     case CSSValueWindowframe:
556         color = convertNSColorToColor([NSColor windowFrameColor]);
557         break;
558     case CSSValueWindowtext:
559         color = convertNSColorToColor([NSColor windowFrameTextColor]);
560         break;
561     case CSSValueAppleWirelessPlaybackTargetActive:
562         color = convertNSColorToColor([NSColor systemBlueColor]);
563         break;
564     case CSSValueAppleSystemBlue:
565         color = convertNSColorToColor([NSColor systemBlueColor]);
566         break;
567     case CSSValueAppleSystemBrown:
568         color = convertNSColorToColor([NSColor systemBrownColor]);
569         break;
570     case CSSValueAppleSystemGray:
571         color = convertNSColorToColor([NSColor systemGrayColor]);
572         break;
573     case CSSValueAppleSystemGreen:
574         color = convertNSColorToColor([NSColor systemGreenColor]);
575         break;
576     case CSSValueAppleSystemOrange:
577         color = convertNSColorToColor([NSColor systemOrangeColor]);
578         break;
579     case CSSValueAppleSystemPink:
580         color = convertNSColorToColor([NSColor systemPinkColor]);
581         break;
582     case CSSValueAppleSystemPurple:
583         color = convertNSColorToColor([NSColor systemPurpleColor]);
584         break;
585     case CSSValueAppleSystemRed:
586         color = convertNSColorToColor([NSColor systemRedColor]);
587         break;
588     case CSSValueAppleSystemYellow:
589         color = convertNSColorToColor([NSColor systemYellowColor]);
590         break;
591     default:
592         break;
593     }
594
595     if (!color.isValid())
596         color = RenderTheme::systemColor(cssValueID);
597
598     addResult.iterator->value = color;
599
600     return addResult.iterator->value;
601 }
602
603 bool RenderThemeMac::usesTestModeFocusRingColor() const
604 {
605     return WebCore::usesTestModeFocusRingColor();
606 }
607
608 bool RenderThemeMac::isControlStyled(const RenderStyle& style, const BorderData& border,
609                                      const FillLayer& background, const Color& backgroundColor) const
610 {
611     if (style.appearance() == TextFieldPart || style.appearance() == TextAreaPart || style.appearance() == ListboxPart)
612         return style.border() != border;
613
614     // FIXME: This is horrible, but there is not much else that can be done.  Menu lists cannot draw properly when
615     // scaled.  They can't really draw properly when transformed either.  We can't detect the transform case at style
616     // adjustment time so that will just have to stay broken.  We can however detect that we're zooming.  If zooming
617     // is in effect we treat it like the control is styled.
618     if (style.appearance() == MenulistPart && style.effectiveZoom() != 1.0f)
619         return true;
620
621     return RenderTheme::isControlStyled(style, border, background, backgroundColor);
622 }
623
624 static FloatRect inflateRect(const FloatRect& rect, const IntSize& size, const int* margins, float zoomLevel)
625 {
626     // Only do the inflation if the available width/height are too small. Otherwise try to
627     // fit the glow/check space into the available box's width/height.
628     int widthDelta = rect.width() - (size.width() + margins[leftMargin] * zoomLevel + margins[rightMargin] * zoomLevel);
629     int heightDelta = rect.height() - (size.height() + margins[topMargin] * zoomLevel + margins[bottomMargin] * zoomLevel);
630     FloatRect result(rect);
631     if (widthDelta < 0) {
632         result.setX(result.x() - margins[leftMargin] * zoomLevel);
633         result.setWidth(result.width() - widthDelta);
634     }
635     if (heightDelta < 0) {
636         result.setY(result.y() - margins[topMargin] * zoomLevel);
637         result.setHeight(result.height() - heightDelta);
638     }
639     return result;
640 }
641
642 void RenderThemeMac::adjustRepaintRect(const RenderObject& renderer, FloatRect& rect)
643 {
644     ControlPart part = renderer.style().appearance();
645
646 #if USE(NEW_THEME)
647     switch (part) {
648         case CheckboxPart:
649         case RadioPart:
650         case PushButtonPart:
651         case SquareButtonPart:
652         case DefaultButtonPart:
653         case ButtonPart:
654         case InnerSpinButtonPart:
655             return RenderTheme::adjustRepaintRect(renderer, rect);
656         default:
657             break;
658     }
659 #endif
660
661     float zoomLevel = renderer.style().effectiveZoom();
662
663     if (part == MenulistPart) {
664         setPopupButtonCellState(renderer, IntSize(rect.size()));
665         IntSize size = popupButtonSizes()[[popupButton() controlSize]];
666         size.setHeight(size.height() * zoomLevel);
667         size.setWidth(rect.width());
668         rect = inflateRect(rect, size, popupButtonMargins(), zoomLevel);
669     }
670 }
671
672 static FloatPoint convertToPaintingPosition(const RenderBox& inputRenderer, const RenderBox& customButtonRenderer, const FloatPoint& customButtonLocalPosition,
673     const IntPoint& paintOffset)
674 {
675     IntPoint offsetFromInputRenderer = roundedIntPoint(customButtonRenderer.localToContainerPoint(customButtonRenderer.contentBoxRect().location(), &inputRenderer));
676     FloatPoint paintingPosition = customButtonLocalPosition;
677     paintingPosition.moveBy(-offsetFromInputRenderer);
678     paintingPosition.moveBy(paintOffset);
679     return paintingPosition;
680 }
681
682 void RenderThemeMac::updateCheckedState(NSCell* cell, const RenderObject& o)
683 {
684     bool oldIndeterminate = [cell state] == NSMixedState;
685     bool indeterminate = isIndeterminate(o);
686     bool checked = isChecked(o);
687
688     if (oldIndeterminate != indeterminate) {
689         [cell setState:indeterminate ? NSMixedState : (checked ? NSOnState : NSOffState)];
690         return;
691     }
692
693     bool oldChecked = [cell state] == NSOnState;
694     if (checked != oldChecked)
695         [cell setState:checked ? NSOnState : NSOffState];
696 }
697
698 void RenderThemeMac::updateEnabledState(NSCell* cell, const RenderObject& o)
699 {
700     bool oldEnabled = [cell isEnabled];
701     bool enabled = isEnabled(o);
702     if (enabled != oldEnabled)
703         [cell setEnabled:enabled];
704 }
705
706 void RenderThemeMac::updateFocusedState(NSCell* cell, const RenderObject& o)
707 {
708     bool oldFocused = [cell showsFirstResponder];
709     bool focused = isFocused(o) && o.style().outlineStyleIsAuto();
710     if (focused != oldFocused)
711         [cell setShowsFirstResponder:focused];
712 }
713
714 void RenderThemeMac::updatePressedState(NSCell* cell, const RenderObject& o)
715 {
716     bool oldPressed = [cell isHighlighted];
717     bool pressed = is<Element>(o.node()) && downcast<Element>(*o.node()).active();
718     if (pressed != oldPressed)
719         [cell setHighlighted:pressed];
720 }
721
722 bool RenderThemeMac::controlSupportsTints(const RenderObject& o) const
723 {
724     // An alternate way to implement this would be to get the appropriate cell object
725     // and call the private _needRedrawOnWindowChangedKeyState method. An advantage of
726     // that would be that we would match AppKit behavior more closely, but a disadvantage
727     // would be that we would rely on an AppKit SPI method.
728
729     if (!isEnabled(o))
730         return false;
731
732     // Checkboxes only have tint when checked.
733     if (o.style().appearance() == CheckboxPart)
734         return isChecked(o);
735
736     // For now assume other controls have tint if enabled.
737     return true;
738 }
739
740 NSControlSize RenderThemeMac::controlSizeForFont(const RenderStyle& style) const
741 {
742     int fontSize = style.fontSize();
743     if (fontSize >= 16)
744         return NSRegularControlSize;
745     if (fontSize >= 11)
746         return NSSmallControlSize;
747     return NSMiniControlSize;
748 }
749
750 NSControlSize RenderThemeMac::controlSizeForCell(NSCell*, const IntSize* sizes, const IntSize& minSize, float zoomLevel) const
751 {
752     if (minSize.width() >= static_cast<int>(sizes[NSRegularControlSize].width() * zoomLevel)
753         && minSize.height() >= static_cast<int>(sizes[NSRegularControlSize].height() * zoomLevel))
754         return NSRegularControlSize;
755
756     if (minSize.width() >= static_cast<int>(sizes[NSSmallControlSize].width() * zoomLevel)
757         && minSize.height() >= static_cast<int>(sizes[NSSmallControlSize].height() * zoomLevel))
758         return NSSmallControlSize;
759
760     return NSMiniControlSize;
761 }
762
763 void RenderThemeMac::setControlSize(NSCell* cell, const IntSize* sizes, const IntSize& minSize, float zoomLevel)
764 {
765     NSControlSize size = controlSizeForCell(cell, sizes, minSize, zoomLevel);
766     if (size != [cell controlSize]) // Only update if we have to, since AppKit does work even if the size is the same.
767         [cell setControlSize:size];
768 }
769
770 IntSize RenderThemeMac::sizeForFont(const RenderStyle& style, const IntSize* sizes) const
771 {
772     if (style.effectiveZoom() != 1.0f) {
773         IntSize result = sizes[controlSizeForFont(style)];
774         return IntSize(result.width() * style.effectiveZoom(), result.height() * style.effectiveZoom());
775     }
776     return sizes[controlSizeForFont(style)];
777 }
778
779 IntSize RenderThemeMac::sizeForSystemFont(const RenderStyle& style, const IntSize* sizes) const
780 {
781     if (style.effectiveZoom() != 1.0f) {
782         IntSize result = sizes[controlSizeForSystemFont(style)];
783         return IntSize(result.width() * style.effectiveZoom(), result.height() * style.effectiveZoom());
784     }
785     return sizes[controlSizeForSystemFont(style)];
786 }
787
788 void RenderThemeMac::setSizeFromFont(RenderStyle& style, const IntSize* sizes) const
789 {
790     // FIXME: Check is flawed, since it doesn't take min-width/max-width into account.
791     IntSize size = sizeForFont(style, sizes);
792     if (style.width().isIntrinsicOrAuto() && size.width() > 0)
793         style.setWidth(Length(size.width(), Fixed));
794     if (style.height().isAuto() && size.height() > 0)
795         style.setHeight(Length(size.height(), Fixed));
796 }
797
798 void RenderThemeMac::setFontFromControlSize(StyleResolver&, RenderStyle& style, NSControlSize controlSize) const
799 {
800     FontCascadeDescription fontDescription;
801     fontDescription.setIsAbsoluteSize(true);
802
803     NSFont* font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:controlSize]];
804     fontDescription.setOneFamily(AtomicString("-apple-system", AtomicString::ConstructFromLiteral));
805     fontDescription.setComputedSize([font pointSize] * style.effectiveZoom());
806     fontDescription.setSpecifiedSize([font pointSize] * style.effectiveZoom());
807
808     // Reset line height
809     style.setLineHeight(RenderStyle::initialLineHeight());
810
811     if (style.setFontDescription(fontDescription))
812         style.fontCascade().update(0);
813 }
814
815 NSControlSize RenderThemeMac::controlSizeForSystemFont(const RenderStyle& style) const
816 {
817     int fontSize = style.fontSize();
818     if (fontSize >= [NSFont systemFontSizeForControlSize:NSRegularControlSize])
819         return NSRegularControlSize;
820     if (fontSize >= [NSFont systemFontSizeForControlSize:NSSmallControlSize])
821         return NSSmallControlSize;
822     return NSMiniControlSize;
823 }
824
825 bool RenderThemeMac::paintTextField(const RenderObject& o, const PaintInfo& paintInfo, const FloatRect& r)
826 {
827     LocalCurrentGraphicsContext localContext(paintInfo.context());
828
829     // <rdar://problem/22896977> We adjust the paint rect here to account for how AppKit draws the text
830     // field cell slightly smaller than the rect we pass to drawWithFrame.
831     FloatRect adjustedPaintRect(r);
832     AffineTransform transform = paintInfo.context().getCTM();
833     if (transform.xScale() > 1 || transform.yScale() > 1) {
834         adjustedPaintRect.inflateX(1 / transform.xScale());
835         adjustedPaintRect.inflateY(1 / transform.yScale());
836     }
837     NSTextFieldCell *textField = this->textField();
838
839     GraphicsContextStateSaver stateSaver(paintInfo.context());
840
841     [textField setEnabled:(isEnabled(o) && !isReadOnlyControl(o))];
842     [textField drawWithFrame:NSRect(adjustedPaintRect) inView:documentViewFor(o)];
843
844     [textField setControlView:nil];
845
846     return false;
847 }
848
849 void RenderThemeMac::adjustTextFieldStyle(StyleResolver&, RenderStyle&, Element*) const
850 {
851 }
852
853 bool RenderThemeMac::paintTextArea(const RenderObject& o, const PaintInfo& paintInfo, const FloatRect& r)
854 {
855     LocalCurrentGraphicsContext localContext(paintInfo.context());
856     wkDrawBezeledTextArea(r, isEnabled(o) && !isReadOnlyControl(o));
857     return false;
858 }
859
860 void RenderThemeMac::adjustTextAreaStyle(StyleResolver&, RenderStyle&, Element*) const
861 {
862 }
863
864 const int* RenderThemeMac::popupButtonMargins() const
865 {
866     static const int margins[3][4] =
867     {
868         { 0, 3, 1, 3 },
869         { 0, 3, 2, 3 },
870         { 0, 1, 0, 1 }
871     };
872     return margins[[popupButton() controlSize]];
873 }
874
875 const IntSize* RenderThemeMac::popupButtonSizes() const
876 {
877     static const IntSize sizes[3] = { IntSize(0, 21), IntSize(0, 18), IntSize(0, 15) };
878     return sizes;
879 }
880
881 const int* RenderThemeMac::popupButtonPadding(NSControlSize size) const
882 {
883     static const int padding[3][4] =
884     {
885         { 2, 26, 3, 8 },
886         { 2, 23, 3, 8 },
887         { 2, 22, 3, 10 }
888     };
889     return padding[size];
890 }
891
892 bool RenderThemeMac::paintMenuList(const RenderObject& renderer, const PaintInfo& paintInfo, const FloatRect& rect)
893 {
894     LocalCurrentGraphicsContext localContext(paintInfo.context());
895     setPopupButtonCellState(renderer, IntSize(rect.size()));
896
897     NSPopUpButtonCell* popupButton = this->popupButton();
898
899     float zoomLevel = renderer.style().effectiveZoom();
900     IntSize size = popupButtonSizes()[[popupButton controlSize]];
901     size.setHeight(size.height() * zoomLevel);
902     size.setWidth(rect.width());
903
904     // Now inflate it to account for the shadow.
905     FloatRect inflatedRect = rect;
906     if (rect.width() >= minimumMenuListSize(renderer.style()))
907         inflatedRect = inflateRect(rect, size, popupButtonMargins(), zoomLevel);
908
909     GraphicsContextStateSaver stateSaver(paintInfo.context());
910
911     if (zoomLevel != 1.0f) {
912         inflatedRect.setWidth(inflatedRect.width() / zoomLevel);
913         inflatedRect.setHeight(inflatedRect.height() / zoomLevel);
914         paintInfo.context().translate(inflatedRect.x(), inflatedRect.y());
915         paintInfo.context().scale(FloatSize(zoomLevel, zoomLevel));
916         paintInfo.context().translate(-inflatedRect.x(), -inflatedRect.y());
917     }
918
919     paintCellAndSetFocusedElementNeedsRepaintIfNecessary(popupButton, renderer, paintInfo, inflatedRect);
920     [popupButton setControlView:nil];
921
922     return false;
923 }
924
925 #if ENABLE(METER_ELEMENT)
926
927 IntSize RenderThemeMac::meterSizeForBounds(const RenderMeter& renderMeter, const IntRect& bounds) const
928 {
929     if (NoControlPart == renderMeter.style().appearance())
930         return bounds.size();
931
932     NSLevelIndicatorCell* cell = levelIndicatorFor(renderMeter);
933     // Makes enough room for cell's intrinsic size.
934     NSSize cellSize = [cell cellSizeForBounds:IntRect(IntPoint(), bounds.size())];
935     return IntSize(bounds.width() < cellSize.width ? cellSize.width : bounds.width(),
936                    bounds.height() < cellSize.height ? cellSize.height : bounds.height());
937 }
938
939 bool RenderThemeMac::paintMeter(const RenderObject& renderObject, const PaintInfo& paintInfo, const IntRect& rect)
940 {
941     if (!is<RenderMeter>(renderObject))
942         return true;
943
944     LocalCurrentGraphicsContext localContext(paintInfo.context());
945
946     NSLevelIndicatorCell* cell = levelIndicatorFor(downcast<RenderMeter>(renderObject));
947     GraphicsContextStateSaver stateSaver(paintInfo.context());
948
949     [cell drawWithFrame:rect inView:documentViewFor(renderObject)];
950     [cell setControlView:nil];
951     return false;
952 }
953
954 bool RenderThemeMac::supportsMeter(ControlPart part) const
955 {
956     switch (part) {
957     case RelevancyLevelIndicatorPart:
958     case DiscreteCapacityLevelIndicatorPart:
959     case RatingLevelIndicatorPart:
960     case MeterPart:
961     case ContinuousCapacityLevelIndicatorPart:
962         return true;
963     default:
964         return false;
965     }
966 }
967
968 NSLevelIndicatorStyle RenderThemeMac::levelIndicatorStyleFor(ControlPart part) const
969 {
970     switch (part) {
971     case RelevancyLevelIndicatorPart:
972         return NSRelevancyLevelIndicatorStyle;
973     case DiscreteCapacityLevelIndicatorPart:
974         return NSDiscreteCapacityLevelIndicatorStyle;
975     case RatingLevelIndicatorPart:
976         return NSRatingLevelIndicatorStyle;
977     case MeterPart:
978     case ContinuousCapacityLevelIndicatorPart:
979     default:
980         return NSContinuousCapacityLevelIndicatorStyle;
981     }
982
983 }
984
985 NSLevelIndicatorCell* RenderThemeMac::levelIndicatorFor(const RenderMeter& renderMeter) const
986 {
987     const RenderStyle& style = renderMeter.style();
988     ASSERT(style.appearance() != NoControlPart);
989
990     if (!m_levelIndicator)
991         m_levelIndicator = adoptNS([[NSLevelIndicatorCell alloc] initWithLevelIndicatorStyle:NSContinuousCapacityLevelIndicatorStyle]);
992     NSLevelIndicatorCell* cell = m_levelIndicator.get();
993
994     HTMLMeterElement* element = renderMeter.meterElement();
995     double value = element->value();
996
997     // Because NSLevelIndicatorCell does not support optimum-in-the-middle type coloring,
998     // we explicitly control the color instead giving low and high value to NSLevelIndicatorCell as is.
999     switch (element->gaugeRegion()) {
1000     case HTMLMeterElement::GaugeRegionOptimum:
1001         // Make meter the green
1002         [cell setWarningValue:value + 1];
1003         [cell setCriticalValue:value + 2];
1004         break;
1005     case HTMLMeterElement::GaugeRegionSuboptimal:
1006         // Make the meter yellow
1007         [cell setWarningValue:value - 1];
1008         [cell setCriticalValue:value + 1];
1009         break;
1010     case HTMLMeterElement::GaugeRegionEvenLessGood:
1011         // Make the meter red
1012         [cell setWarningValue:value - 2];
1013         [cell setCriticalValue:value - 1];
1014         break;
1015     }
1016
1017     [cell setLevelIndicatorStyle:levelIndicatorStyleFor(style.appearance())];
1018     [cell setBaseWritingDirection:style.isLeftToRightDirection() ? NSWritingDirectionLeftToRight : NSWritingDirectionRightToLeft];
1019     [cell setMinValue:element->min()];
1020     [cell setMaxValue:element->max()];
1021     RetainPtr<NSNumber> valueObject = [NSNumber numberWithDouble:value];
1022     [cell setObjectValue:valueObject.get()];
1023
1024     return cell;
1025 }
1026
1027 #endif
1028
1029 const IntSize* RenderThemeMac::progressBarSizes() const
1030 {
1031     static const IntSize sizes[3] = { IntSize(0, 20), IntSize(0, 12), IntSize(0, 12) };
1032     return sizes;
1033 }
1034
1035 const int* RenderThemeMac::progressBarMargins(NSControlSize controlSize) const
1036 {
1037     static const int margins[3][4] =
1038     {
1039         { 0, 0, 1, 0 },
1040         { 0, 0, 1, 0 },
1041         { 0, 0, 1, 0 },
1042     };
1043     return margins[controlSize];
1044 }
1045
1046 IntRect RenderThemeMac::progressBarRectForBounds(const RenderObject& renderObject, const IntRect& bounds) const
1047 {
1048     // Workaround until <rdar://problem/15855086> is fixed.
1049     int maxDimension = static_cast<int>(std::numeric_limits<ushort>::max());
1050     IntRect progressBarBounds(bounds.x(), bounds.y(), std::min(bounds.width(), maxDimension), std::min(bounds.height(), maxDimension));
1051     if (NoControlPart == renderObject.style().appearance())
1052         return progressBarBounds;
1053
1054     float zoomLevel = renderObject.style().effectiveZoom();
1055     NSControlSize controlSize = controlSizeForFont(renderObject.style());
1056     IntSize size = progressBarSizes()[controlSize];
1057     size.setHeight(size.height() * zoomLevel);
1058     size.setWidth(progressBarBounds.width());
1059
1060     // Now inflate it to account for the shadow.
1061     IntRect inflatedRect = progressBarBounds;
1062     if (progressBarBounds.height() <= minimumProgressBarHeight(renderObject.style()))
1063         inflatedRect = IntRect(inflateRect(inflatedRect, size, progressBarMargins(controlSize), zoomLevel));
1064
1065     return inflatedRect;
1066 }
1067
1068 int RenderThemeMac::minimumProgressBarHeight(const RenderStyle& style) const
1069 {
1070     return sizeForSystemFont(style, progressBarSizes()).height();
1071 }
1072
1073 double RenderThemeMac::animationRepeatIntervalForProgressBar(RenderProgress&) const
1074 {
1075     return progressAnimationFrameRate;
1076 }
1077
1078 double RenderThemeMac::animationDurationForProgressBar(RenderProgress&) const
1079 {
1080     return progressAnimationNumFrames * progressAnimationFrameRate;
1081 }
1082
1083 void RenderThemeMac::adjustProgressBarStyle(StyleResolver&, RenderStyle&, Element*) const
1084 {
1085 }
1086
1087 bool RenderThemeMac::paintProgressBar(const RenderObject& renderObject, const PaintInfo& paintInfo, const IntRect& rect)
1088 {
1089     if (!is<RenderProgress>(renderObject))
1090         return true;
1091
1092     IntRect inflatedRect = progressBarRectForBounds(renderObject, rect);
1093     NSControlSize controlSize = controlSizeForFont(renderObject.style());
1094
1095     const auto& renderProgress = downcast<RenderProgress>(renderObject);
1096     HIThemeTrackDrawInfo trackInfo;
1097     trackInfo.version = 0;
1098     if (controlSize == NSRegularControlSize)
1099         trackInfo.kind = renderProgress.position() < 0 ? kThemeLargeIndeterminateBar : kThemeLargeProgressBar;
1100     else
1101         trackInfo.kind = renderProgress.position() < 0 ? kThemeMediumIndeterminateBar : kThemeMediumProgressBar;
1102
1103     float deviceScaleFactor = renderObject.document().deviceScaleFactor();
1104     trackInfo.bounds = IntRect(IntPoint(), inflatedRect.size());
1105     trackInfo.min = 0;
1106     trackInfo.max = std::numeric_limits<SInt32>::max();
1107     trackInfo.value = lround(renderProgress.position() * nextafter(trackInfo.max, 0));
1108     trackInfo.trackInfo.progress.phase = lround(renderProgress.animationProgress() * nextafter(progressAnimationNumFrames, 0) * deviceScaleFactor);
1109     trackInfo.attributes = kThemeTrackHorizontal;
1110     trackInfo.enableState = isActive(renderObject) ? kThemeTrackActive : kThemeTrackInactive;
1111     trackInfo.reserved = 0;
1112     trackInfo.filler1 = 0;
1113
1114     std::unique_ptr<ImageBuffer> imageBuffer = ImageBuffer::createCompatibleBuffer(inflatedRect.size(), deviceScaleFactor, ColorSpaceSRGB, paintInfo.context(), true);
1115     if (!imageBuffer)
1116         return true;
1117
1118     ContextContainer cgContextContainer(imageBuffer->context());
1119     CGContextRef cgContext = cgContextContainer.context();
1120     HIThemeDrawTrack(&trackInfo, 0, cgContext, kHIThemeOrientationNormal);
1121
1122     GraphicsContextStateSaver stateSaver(paintInfo.context());
1123
1124     if (!renderProgress.style().isLeftToRightDirection()) {
1125         paintInfo.context().translate(2 * inflatedRect.x() + inflatedRect.width(), 0);
1126         paintInfo.context().scale(FloatSize(-1, 1));
1127     }
1128
1129     paintInfo.context().drawConsumingImageBuffer(WTFMove(imageBuffer), inflatedRect.location());
1130     return false;
1131 }
1132
1133 const float baseFontSize = 11.0f;
1134 const float baseArrowHeight = 4.0f;
1135 const float baseArrowWidth = 5.0f;
1136 const float baseSpaceBetweenArrows = 2.0f;
1137 const int arrowPaddingLeft = 6;
1138 const int arrowPaddingRight = 6;
1139 const int paddingBeforeSeparator = 4;
1140 const int baseBorderRadius = 5;
1141 const int styledPopupPaddingLeft = 8;
1142 const int styledPopupPaddingTop = 1;
1143 const int styledPopupPaddingBottom = 2;
1144
1145 static void TopGradientInterpolate(void*, const CGFloat* inData, CGFloat* outData)
1146 {
1147     static const float dark[4] = { 1.0f, 1.0f, 1.0f, 0.4f };
1148     static const float light[4] = { 1.0f, 1.0f, 1.0f, 0.15f };
1149     float a = inData[0];
1150     int i = 0;
1151     for (i = 0; i < 4; i++)
1152         outData[i] = (1.0f - a) * dark[i] + a * light[i];
1153 }
1154
1155 static void BottomGradientInterpolate(void*, const CGFloat* inData, CGFloat* outData)
1156 {
1157     static const float dark[4] = { 1.0f, 1.0f, 1.0f, 0.0f };
1158     static const float light[4] = { 1.0f, 1.0f, 1.0f, 0.3f };
1159     float a = inData[0];
1160     int i = 0;
1161     for (i = 0; i < 4; i++)
1162         outData[i] = (1.0f - a) * dark[i] + a * light[i];
1163 }
1164
1165 static void MainGradientInterpolate(void*, const CGFloat* inData, CGFloat* outData)
1166 {
1167     static const float dark[4] = { 0.0f, 0.0f, 0.0f, 0.15f };
1168     static const float light[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
1169     float a = inData[0];
1170     int i = 0;
1171     for (i = 0; i < 4; i++)
1172         outData[i] = (1.0f - a) * dark[i] + a * light[i];
1173 }
1174
1175 static void TrackGradientInterpolate(void*, const CGFloat* inData, CGFloat* outData)
1176 {
1177     static const float dark[4] = { 0.0f, 0.0f, 0.0f, 0.678f };
1178     static const float light[4] = { 0.0f, 0.0f, 0.0f, 0.13f };
1179     float a = inData[0];
1180     int i = 0;
1181     for (i = 0; i < 4; i++)
1182         outData[i] = (1.0f - a) * dark[i] + a * light[i];
1183 }
1184
1185 void RenderThemeMac::paintMenuListButtonGradients(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1186 {
1187     if (r.isEmpty())
1188         return;
1189
1190     ContextContainer cgContextContainer(paintInfo.context());
1191     CGContextRef context = cgContextContainer.context();
1192
1193     GraphicsContextStateSaver stateSaver(paintInfo.context());
1194
1195     FloatRoundedRect border = FloatRoundedRect(o.style().getRoundedBorderFor(r));
1196     int radius = border.radii().topLeft().width();
1197
1198     CGColorSpaceRef cspace = sRGBColorSpaceRef();
1199
1200     FloatRect topGradient(r.x(), r.y(), r.width(), r.height() / 2.0f);
1201     struct CGFunctionCallbacks topCallbacks = { 0, TopGradientInterpolate, NULL };
1202     RetainPtr<CGFunctionRef> topFunction = adoptCF(CGFunctionCreate(NULL, 1, NULL, 4, NULL, &topCallbacks));
1203     RetainPtr<CGShadingRef> topShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(topGradient.x(), topGradient.y()), CGPointMake(topGradient.x(), topGradient.maxY()), topFunction.get(), false, false));
1204
1205     FloatRect bottomGradient(r.x() + radius, r.y() + r.height() / 2.0f, r.width() - 2.0f * radius, r.height() / 2.0f);
1206     struct CGFunctionCallbacks bottomCallbacks = { 0, BottomGradientInterpolate, NULL };
1207     RetainPtr<CGFunctionRef> bottomFunction = adoptCF(CGFunctionCreate(NULL, 1, NULL, 4, NULL, &bottomCallbacks));
1208     RetainPtr<CGShadingRef> bottomShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(bottomGradient.x(),  bottomGradient.y()), CGPointMake(bottomGradient.x(), bottomGradient.maxY()), bottomFunction.get(), false, false));
1209
1210     struct CGFunctionCallbacks mainCallbacks = { 0, MainGradientInterpolate, NULL };
1211     RetainPtr<CGFunctionRef> mainFunction = adoptCF(CGFunctionCreate(NULL, 1, NULL, 4, NULL, &mainCallbacks));
1212     RetainPtr<CGShadingRef> mainShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(r.x(),  r.y()), CGPointMake(r.x(), r.maxY()), mainFunction.get(), false, false));
1213
1214     RetainPtr<CGShadingRef> leftShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(r.x(),  r.y()), CGPointMake(r.x() + radius, r.y()), mainFunction.get(), false, false));
1215
1216     RetainPtr<CGShadingRef> rightShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(r.maxX(),  r.y()), CGPointMake(r.maxX() - radius, r.y()), mainFunction.get(), false, false));
1217
1218     {
1219         GraphicsContextStateSaver stateSaver(paintInfo.context());
1220         CGContextClipToRect(context, r);
1221         paintInfo.context().clipRoundedRect(border);
1222         context = cgContextContainer.context();
1223         CGContextDrawShading(context, mainShading.get());
1224     }
1225
1226     {
1227         GraphicsContextStateSaver stateSaver(paintInfo.context());
1228         CGContextClipToRect(context, topGradient);
1229         paintInfo.context().clipRoundedRect(FloatRoundedRect(enclosingIntRect(topGradient), border.radii().topLeft(), border.radii().topRight(), IntSize(), IntSize()));
1230         context = cgContextContainer.context();
1231         CGContextDrawShading(context, topShading.get());
1232     }
1233
1234     if (!bottomGradient.isEmpty()) {
1235         GraphicsContextStateSaver stateSaver(paintInfo.context());
1236         CGContextClipToRect(context, bottomGradient);
1237         paintInfo.context().clipRoundedRect(FloatRoundedRect(enclosingIntRect(bottomGradient), IntSize(), IntSize(), border.radii().bottomLeft(), border.radii().bottomRight()));
1238         context = cgContextContainer.context();
1239         CGContextDrawShading(context, bottomShading.get());
1240     }
1241
1242     {
1243         GraphicsContextStateSaver stateSaver(paintInfo.context());
1244         CGContextClipToRect(context, r);
1245         paintInfo.context().clipRoundedRect(border);
1246         context = cgContextContainer.context();
1247         CGContextDrawShading(context, leftShading.get());
1248         CGContextDrawShading(context, rightShading.get());
1249     }
1250 }
1251
1252 bool RenderThemeMac::paintMenuListButtonDecorations(const RenderBox& renderer, const PaintInfo& paintInfo, const FloatRect& rect)
1253 {
1254     IntRect bounds = IntRect(rect.x() + renderer.style().borderLeftWidth(),
1255         rect.y() + renderer.style().borderTopWidth(),
1256         rect.width() - renderer.style().borderLeftWidth() - renderer.style().borderRightWidth(),
1257         rect.height() - renderer.style().borderTopWidth() - renderer.style().borderBottomWidth());
1258     // Draw the gradients to give the styled popup menu a button appearance
1259     paintMenuListButtonGradients(renderer, paintInfo, bounds);
1260
1261     // Since we actually know the size of the control here, we restrict the font scale to make sure the arrows will fit vertically in the bounds
1262     float fontScale = std::min(renderer.style().fontSize() / baseFontSize, bounds.height() / (baseArrowHeight * 2 + baseSpaceBetweenArrows));
1263     float centerY = bounds.y() + bounds.height() / 2.0f;
1264     float arrowHeight = baseArrowHeight * fontScale;
1265     float arrowWidth = baseArrowWidth * fontScale;
1266     float leftEdge = bounds.maxX() - arrowPaddingRight * renderer.style().effectiveZoom() - arrowWidth;
1267     float spaceBetweenArrows = baseSpaceBetweenArrows * fontScale;
1268
1269     if (bounds.width() < arrowWidth + arrowPaddingLeft * renderer.style().effectiveZoom())
1270         return false;
1271
1272     GraphicsContextStateSaver stateSaver(paintInfo.context());
1273
1274     paintInfo.context().setFillColor(renderer.style().visitedDependentColor(CSSPropertyColor));
1275     paintInfo.context().setStrokeStyle(NoStroke);
1276
1277     // Draw the top arrow
1278     Vector<FloatPoint> arrow1 = {
1279         { leftEdge, centerY - spaceBetweenArrows / 2.0f },
1280         { leftEdge + arrowWidth, centerY - spaceBetweenArrows / 2.0f },
1281         { leftEdge + arrowWidth / 2.0f, centerY - spaceBetweenArrows / 2.0f - arrowHeight }
1282     };
1283     paintInfo.context().fillPath(Path::polygonPathFromPoints(arrow1));
1284
1285     // Draw the bottom arrow
1286     Vector<FloatPoint> arrow2 = {
1287         { leftEdge, centerY + spaceBetweenArrows / 2.0f },
1288         { leftEdge + arrowWidth, centerY + spaceBetweenArrows / 2.0f },
1289         { leftEdge + arrowWidth / 2.0f, centerY + spaceBetweenArrows / 2.0f + arrowHeight }
1290     };
1291     paintInfo.context().fillPath(Path::polygonPathFromPoints(arrow2));
1292
1293     Color leftSeparatorColor(0, 0, 0, 40);
1294     Color rightSeparatorColor(255, 255, 255, 40);
1295
1296     // FIXME: Should the separator thickness and space be scaled up by fontScale?
1297     int separatorSpace = 2; // Deliberately ignores zoom since it looks nicer if it stays thin.
1298     int leftEdgeOfSeparator = static_cast<int>(leftEdge - arrowPaddingLeft * renderer.style().effectiveZoom()); // FIXME: Round?
1299
1300     // Draw the separator to the left of the arrows
1301     paintInfo.context().setStrokeThickness(1); // Deliberately ignores zoom since it looks nicer if it stays thin.
1302     paintInfo.context().setStrokeStyle(SolidStroke);
1303     paintInfo.context().setStrokeColor(leftSeparatorColor);
1304     paintInfo.context().drawLine(IntPoint(leftEdgeOfSeparator, bounds.y()),
1305                                 IntPoint(leftEdgeOfSeparator, bounds.maxY()));
1306
1307     paintInfo.context().setStrokeColor(rightSeparatorColor);
1308     paintInfo.context().drawLine(IntPoint(leftEdgeOfSeparator + separatorSpace, bounds.y()),
1309                                 IntPoint(leftEdgeOfSeparator + separatorSpace, bounds.maxY()));
1310     return false;
1311 }
1312
1313 static const IntSize* menuListButtonSizes()
1314 {
1315     static const IntSize sizes[3] = { IntSize(0, 21), IntSize(0, 18), IntSize(0, 15) };
1316     return sizes;
1317 }
1318
1319 void RenderThemeMac::adjustMenuListStyle(StyleResolver& styleResolver, RenderStyle& style, Element* e) const
1320 {
1321     NSControlSize controlSize = controlSizeForFont(style);
1322
1323     style.resetBorder();
1324     style.resetPadding();
1325
1326     // Height is locked to auto.
1327     style.setHeight(Length(Auto));
1328
1329     // White-space is locked to pre
1330     style.setWhiteSpace(PRE);
1331
1332     // Set the foreground color to black or gray when we have the aqua look.
1333     // Cast to RGB32 is to work around a compiler bug.
1334     style.setColor(e && !e->isDisabledFormControl() ? static_cast<RGBA32>(Color::black) : Color::darkGray);
1335
1336     // Set the button's vertical size.
1337     setSizeFromFont(style, menuListButtonSizes());
1338
1339     // Our font is locked to the appropriate system font size for the control.  To clarify, we first use the CSS-specified font to figure out
1340     // a reasonable control size, but once that control size is determined, we throw that font away and use the appropriate
1341     // system font for the control size instead.
1342     setFontFromControlSize(styleResolver, style, controlSize);
1343
1344     style.setBoxShadow(nullptr);
1345 }
1346
1347 int RenderThemeMac::popupInternalPaddingLeft(const RenderStyle& style) const
1348 {
1349     if (style.appearance() == MenulistPart)
1350         return popupButtonPadding(controlSizeForFont(style))[leftPadding] * style.effectiveZoom();
1351     if (style.appearance() == MenulistButtonPart)
1352         return styledPopupPaddingLeft * style.effectiveZoom();
1353     return 0;
1354 }
1355
1356 int RenderThemeMac::popupInternalPaddingRight(const RenderStyle& style) const
1357 {
1358     if (style.appearance() == MenulistPart)
1359         return popupButtonPadding(controlSizeForFont(style))[rightPadding] * style.effectiveZoom();
1360     if (style.appearance() == MenulistButtonPart) {
1361         float fontScale = style.fontSize() / baseFontSize;
1362         float arrowWidth = baseArrowWidth * fontScale;
1363         return static_cast<int>(ceilf(arrowWidth + (arrowPaddingLeft + arrowPaddingRight + paddingBeforeSeparator) * style.effectiveZoom()));
1364     }
1365     return 0;
1366 }
1367
1368 int RenderThemeMac::popupInternalPaddingTop(const RenderStyle& style) const
1369 {
1370     if (style.appearance() == MenulistPart)
1371         return popupButtonPadding(controlSizeForFont(style))[topPadding] * style.effectiveZoom();
1372     if (style.appearance() == MenulistButtonPart)
1373         return styledPopupPaddingTop * style.effectiveZoom();
1374     return 0;
1375 }
1376
1377 int RenderThemeMac::popupInternalPaddingBottom(const RenderStyle& style) const
1378 {
1379     if (style.appearance() == MenulistPart)
1380         return popupButtonPadding(controlSizeForFont(style))[bottomPadding] * style.effectiveZoom();
1381     if (style.appearance() == MenulistButtonPart)
1382         return styledPopupPaddingBottom * style.effectiveZoom();
1383     return 0;
1384 }
1385
1386 PopupMenuStyle::PopupMenuSize RenderThemeMac::popupMenuSize(const RenderStyle& style, IntRect& rect) const
1387 {
1388     NSPopUpButtonCell* popupButton = this->popupButton();
1389     NSControlSize size = controlSizeForCell(popupButton, popupButtonSizes(), rect.size(), style.effectiveZoom());
1390     switch (size) {
1391     case NSRegularControlSize:
1392         return PopupMenuStyle::PopupMenuSizeNormal;
1393     case NSSmallControlSize:
1394         return PopupMenuStyle::PopupMenuSizeSmall;
1395     case NSMiniControlSize:
1396         return PopupMenuStyle::PopupMenuSizeMini;
1397     default:
1398         return PopupMenuStyle::PopupMenuSizeNormal;
1399     }
1400 }
1401
1402 void RenderThemeMac::adjustMenuListButtonStyle(StyleResolver&, RenderStyle& style, Element*) const
1403 {
1404     float fontScale = style.fontSize() / baseFontSize;
1405
1406     style.resetPadding();
1407     style.setBorderRadius(IntSize(int(baseBorderRadius + fontScale - 1), int(baseBorderRadius + fontScale - 1))); // FIXME: Round up?
1408
1409     const int minHeight = 15;
1410     style.setMinHeight(Length(minHeight, Fixed));
1411
1412     style.setLineHeight(RenderStyle::initialLineHeight());
1413 }
1414
1415 void RenderThemeMac::setPopupButtonCellState(const RenderObject& o, const IntSize& buttonSize)
1416 {
1417     NSPopUpButtonCell* popupButton = this->popupButton();
1418
1419     // Set the control size based off the rectangle we're painting into.
1420     setControlSize(popupButton, popupButtonSizes(), buttonSize, o.style().effectiveZoom());
1421
1422     // Update the various states we respond to.
1423     updateCheckedState(popupButton, o);
1424     updateEnabledState(popupButton, o);
1425     updatePressedState(popupButton, o);
1426 }
1427
1428 void RenderThemeMac::paintCellAndSetFocusedElementNeedsRepaintIfNecessary(NSCell* cell, const RenderObject& renderer, const PaintInfo& paintInfo, const FloatRect& rect)
1429 {
1430     Page* page = renderer.document().page();
1431     bool shouldDrawFocusRing = isFocused(renderer) && renderer.style().outlineStyleIsAuto();
1432     bool shouldUseImageBuffer = renderer.style().effectiveZoom() != 1 || page->pageScaleFactor() != 1;
1433     bool shouldDrawCell = true;
1434     if (ThemeMac::drawCellOrFocusRingWithViewIntoContext(cell, paintInfo.context(), rect, documentViewFor(renderer), shouldDrawCell, shouldDrawFocusRing, shouldUseImageBuffer, page->deviceScaleFactor()))
1435         page->focusController().setFocusedElementNeedsRepaint();
1436 }
1437
1438 const IntSize* RenderThemeMac::menuListSizes() const
1439 {
1440     static const IntSize sizes[3] = { IntSize(9, 0), IntSize(5, 0), IntSize(0, 0) };
1441     return sizes;
1442 }
1443
1444 int RenderThemeMac::minimumMenuListSize(const RenderStyle& style) const
1445 {
1446     return sizeForSystemFont(style, menuListSizes()).width();
1447 }
1448
1449 const int trackWidth = 5;
1450 const int trackRadius = 2;
1451
1452 void RenderThemeMac::adjustSliderTrackStyle(StyleResolver&, RenderStyle& style, Element*) const
1453 {
1454     style.setBoxShadow(nullptr);
1455 }
1456
1457 bool RenderThemeMac::paintSliderTrack(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1458 {
1459     IntRect bounds = r;
1460     float zoomLevel = o.style().effectiveZoom();
1461     float zoomedTrackWidth = trackWidth * zoomLevel;
1462
1463     if (o.style().appearance() ==  SliderHorizontalPart || o.style().appearance() ==  MediaSliderPart) {
1464         bounds.setHeight(zoomedTrackWidth);
1465         bounds.setY(r.y() + r.height() / 2 - zoomedTrackWidth / 2);
1466     } else if (o.style().appearance() == SliderVerticalPart) {
1467         bounds.setWidth(zoomedTrackWidth);
1468         bounds.setX(r.x() + r.width() / 2 - zoomedTrackWidth / 2);
1469     }
1470
1471     LocalCurrentGraphicsContext localContext(paintInfo.context());
1472     CGContextRef context = localContext.cgContext();
1473     CGColorSpaceRef cspace = sRGBColorSpaceRef();
1474
1475 #if ENABLE(DATALIST_ELEMENT)
1476     paintSliderTicks(o, paintInfo, r);
1477 #endif
1478
1479     GraphicsContextStateSaver stateSaver(paintInfo.context());
1480     CGContextClipToRect(context, bounds);
1481
1482     struct CGFunctionCallbacks mainCallbacks = { 0, TrackGradientInterpolate, NULL };
1483     RetainPtr<CGFunctionRef> mainFunction = adoptCF(CGFunctionCreate(NULL, 1, NULL, 4, NULL, &mainCallbacks));
1484     RetainPtr<CGShadingRef> mainShading;
1485     if (o.style().appearance() == SliderVerticalPart)
1486         mainShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(bounds.x(),  bounds.maxY()), CGPointMake(bounds.maxX(), bounds.maxY()), mainFunction.get(), false, false));
1487     else
1488         mainShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(bounds.x(),  bounds.y()), CGPointMake(bounds.x(), bounds.maxY()), mainFunction.get(), false, false));
1489
1490     IntSize radius(trackRadius, trackRadius);
1491     paintInfo.context().clipRoundedRect(FloatRoundedRect(bounds, radius, radius, radius, radius));
1492     context = localContext.cgContext();
1493     CGContextDrawShading(context, mainShading.get());
1494
1495     return false;
1496 }
1497
1498 void RenderThemeMac::adjustSliderThumbStyle(StyleResolver& styleResolver, RenderStyle& style, Element* element) const
1499 {
1500     RenderTheme::adjustSliderThumbStyle(styleResolver, style, element);
1501     style.setBoxShadow(nullptr);
1502 }
1503
1504 const float verticalSliderHeightPadding = 0.1f;
1505
1506 bool RenderThemeMac::paintSliderThumb(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1507 {
1508     NSSliderCell* sliderThumbCell = o.style().appearance() == SliderThumbVerticalPart
1509         ? sliderThumbVertical()
1510         : sliderThumbHorizontal();
1511
1512     LocalCurrentGraphicsContext localContext(paintInfo.context());
1513
1514     // Update the various states we respond to.
1515     updateEnabledState(sliderThumbCell, o);
1516         Element* focusDelegate = is<Element>(o.node()) ? downcast<Element>(*o.node()).focusDelegate() : nullptr;
1517     if (focusDelegate)
1518         updateFocusedState(sliderThumbCell, *focusDelegate->renderer());
1519
1520     // Update the pressed state using the NSCell tracking methods, since that's how NSSliderCell keeps track of it.
1521     bool oldPressed;
1522     if (o.style().appearance() == SliderThumbVerticalPart)
1523         oldPressed = m_isSliderThumbVerticalPressed;
1524     else
1525         oldPressed = m_isSliderThumbHorizontalPressed;
1526
1527     bool pressed = isPressed(o);
1528
1529     if (o.style().appearance() == SliderThumbVerticalPart)
1530         m_isSliderThumbVerticalPressed = pressed;
1531     else
1532         m_isSliderThumbHorizontalPressed = pressed;
1533
1534     NSView *view = documentViewFor(o);
1535
1536     if (pressed != oldPressed) {
1537         if (pressed)
1538             [sliderThumbCell startTrackingAt:NSPoint() inView:view];
1539         else
1540             [sliderThumbCell stopTracking:NSPoint() at:NSPoint() inView:view mouseIsUp:YES];
1541     }
1542
1543     FloatRect bounds = r;
1544     // Make the height of the vertical slider slightly larger so NSSliderCell will draw a vertical slider.
1545     if (o.style().appearance() == SliderThumbVerticalPart)
1546         bounds.setHeight(bounds.height() + verticalSliderHeightPadding * o.style().effectiveZoom());
1547
1548     GraphicsContextStateSaver stateSaver(paintInfo.context());
1549     float zoomLevel = o.style().effectiveZoom();
1550
1551     FloatRect unzoomedRect = bounds;
1552     if (zoomLevel != 1.0f) {
1553         unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
1554         unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
1555         paintInfo.context().translate(unzoomedRect.x(), unzoomedRect.y());
1556         paintInfo.context().scale(FloatSize(zoomLevel, zoomLevel));
1557         paintInfo.context().translate(-unzoomedRect.x(), -unzoomedRect.y());
1558     }
1559
1560     bool shouldDrawCell = true;
1561     bool shouldDrawFocusRing = false;
1562     float deviceScaleFactor = o.document().page()->deviceScaleFactor();
1563     bool shouldUseImageBuffer = deviceScaleFactor != 1 || zoomLevel != 1;
1564     ThemeMac::drawCellOrFocusRingWithViewIntoContext(sliderThumbCell, paintInfo.context(), unzoomedRect, view, shouldDrawCell, shouldDrawFocusRing, shouldUseImageBuffer, deviceScaleFactor);
1565     [sliderThumbCell setControlView:nil];
1566
1567     return false;
1568 }
1569
1570 bool RenderThemeMac::paintSearchField(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1571 {
1572     LocalCurrentGraphicsContext localContext(paintInfo.context());
1573     NSSearchFieldCell* search = this->search();
1574
1575     setSearchCellState(o, r);
1576
1577     GraphicsContextStateSaver stateSaver(paintInfo.context());
1578
1579     float zoomLevel = o.style().effectiveZoom();
1580
1581     IntRect unzoomedRect = r;
1582
1583     if (zoomLevel != 1.0f) {
1584         unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
1585         unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
1586         paintInfo.context().translate(unzoomedRect.x(), unzoomedRect.y());
1587         paintInfo.context().scale(FloatSize(zoomLevel, zoomLevel));
1588         paintInfo.context().translate(-unzoomedRect.x(), -unzoomedRect.y());
1589     }
1590
1591     // Set the search button to nil before drawing.  Then reset it so we can draw it later.
1592     [search setSearchButtonCell:nil];
1593
1594     paintCellAndSetFocusedElementNeedsRepaintIfNecessary(search, o, paintInfo, unzoomedRect);
1595     [search setControlView:nil];
1596     [search resetSearchButtonCell];
1597
1598     return false;
1599 }
1600
1601 void RenderThemeMac::setSearchCellState(const RenderObject& o, const IntRect&)
1602 {
1603     NSSearchFieldCell* search = this->search();
1604
1605     [search setPlaceholderString:@""];
1606     [search setControlSize:controlSizeForFont(o.style())];
1607
1608     // Update the various states we respond to.
1609     updateEnabledState(search, o);
1610     updateFocusedState(search, o);
1611 }
1612
1613 const IntSize* RenderThemeMac::searchFieldSizes() const
1614 {
1615     static const IntSize sizes[3] = { IntSize(0, 22), IntSize(0, 19), IntSize(0, 17) };
1616     return sizes;
1617 }
1618
1619 void RenderThemeMac::setSearchFieldSize(RenderStyle& style) const
1620 {
1621     // If the width and height are both specified, then we have nothing to do.
1622     if (!style.width().isIntrinsicOrAuto() && !style.height().isAuto())
1623         return;
1624
1625     // Use the font size to determine the intrinsic width of the control.
1626     setSizeFromFont(style, searchFieldSizes());
1627 }
1628
1629 void RenderThemeMac::adjustSearchFieldStyle(StyleResolver& styleResolver, RenderStyle& style, Element*) const
1630 {
1631     // Override border.
1632     style.resetBorder();
1633     const short borderWidth = 2 * style.effectiveZoom();
1634     style.setBorderLeftWidth(borderWidth);
1635     style.setBorderLeftStyle(INSET);
1636     style.setBorderRightWidth(borderWidth);
1637     style.setBorderRightStyle(INSET);
1638     style.setBorderBottomWidth(borderWidth);
1639     style.setBorderBottomStyle(INSET);
1640     style.setBorderTopWidth(borderWidth);
1641     style.setBorderTopStyle(INSET);
1642
1643     // Override height.
1644     style.setHeight(Length(Auto));
1645     setSearchFieldSize(style);
1646
1647     // Override padding size to match AppKit text positioning.
1648     const int padding = 1 * style.effectiveZoom();
1649     style.setPaddingLeft(Length(padding, Fixed));
1650     style.setPaddingRight(Length(padding, Fixed));
1651     style.setPaddingTop(Length(padding, Fixed));
1652     style.setPaddingBottom(Length(padding, Fixed));
1653
1654     NSControlSize controlSize = controlSizeForFont(style);
1655     setFontFromControlSize(styleResolver, style, controlSize);
1656
1657     style.setBoxShadow(nullptr);
1658 }
1659
1660 bool RenderThemeMac::paintSearchFieldCancelButton(const RenderBox& box, const PaintInfo& paintInfo, const IntRect& r)
1661 {
1662     auto adjustedCancelButtonRect = [this, &box] (const FloatRect& localBoundsForCancelButton) -> FloatRect
1663     {
1664         IntSize cancelButtonSizeBasedOnFontSize = sizeForSystemFont(box.style(), cancelButtonSizes());
1665         FloatSize diff = localBoundsForCancelButton.size() - FloatSize(cancelButtonSizeBasedOnFontSize);
1666         if (!diff.width() && !diff.height())
1667             return localBoundsForCancelButton;
1668         // Vertically centered and right aligned.
1669         FloatRect adjustedLocalBoundsForCancelButton = localBoundsForCancelButton;
1670         adjustedLocalBoundsForCancelButton.move(diff.width(), floorToDevicePixel(diff.height() / 2, box.document().deviceScaleFactor()));
1671         adjustedLocalBoundsForCancelButton.setSize(cancelButtonSizeBasedOnFontSize);
1672         return adjustedLocalBoundsForCancelButton;
1673     };
1674
1675     if (!box.element())
1676         return false;
1677     Element* input = box.element()->shadowHost();
1678     if (!input)
1679         input = box.element();
1680
1681     if (!is<RenderBox>(input->renderer()))
1682         return false;
1683
1684     const RenderBox& inputBox = downcast<RenderBox>(*input->renderer());
1685     LocalCurrentGraphicsContext localContext(paintInfo.context());
1686     setSearchCellState(inputBox, r);
1687
1688     NSSearchFieldCell* search = this->search();
1689
1690     if (!input->isDisabledFormControl() && (is<HTMLTextFormControlElement>(*input) && !downcast<HTMLTextFormControlElement>(*input).isReadOnly()))
1691         updatePressedState([search cancelButtonCell], box);
1692     else if ([[search cancelButtonCell] isHighlighted])
1693         [[search cancelButtonCell] setHighlighted:NO];
1694
1695     GraphicsContextStateSaver stateSaver(paintInfo.context());
1696
1697     float zoomLevel = box.style().effectiveZoom();
1698
1699     FloatRect localBounds = adjustedCancelButtonRect([search cancelButtonRectForBounds:NSRect(snappedIntRect(inputBox.contentBoxRect()))]);
1700     FloatPoint paintingPos = convertToPaintingPosition(inputBox, box, localBounds.location(), r.location());
1701
1702     FloatRect unzoomedRect(paintingPos, localBounds.size());
1703     if (zoomLevel != 1.0f) {
1704         unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
1705         unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
1706         paintInfo.context().translate(unzoomedRect.x(), unzoomedRect.y());
1707         paintInfo.context().scale(FloatSize(zoomLevel, zoomLevel));
1708         paintInfo.context().translate(-unzoomedRect.x(), -unzoomedRect.y());
1709     }
1710     [[search cancelButtonCell] drawWithFrame:unzoomedRect inView:documentViewFor(box)];
1711     [[search cancelButtonCell] setControlView:nil];
1712     return false;
1713 }
1714
1715 const IntSize* RenderThemeMac::cancelButtonSizes() const
1716 {
1717     static const IntSize sizes[3] = { IntSize(22, 22), IntSize(19, 19), IntSize(15, 15) };
1718     return sizes;
1719 }
1720
1721 void RenderThemeMac::adjustSearchFieldCancelButtonStyle(StyleResolver&, RenderStyle& style, Element*) const
1722 {
1723     IntSize size = sizeForSystemFont(style, cancelButtonSizes());
1724     style.setWidth(Length(size.width(), Fixed));
1725     style.setHeight(Length(size.height(), Fixed));
1726     style.setBoxShadow(nullptr);
1727 }
1728
1729 const IntSize* RenderThemeMac::resultsButtonSizes() const
1730 {
1731     static const IntSize sizes[3] = { IntSize(19, 13), IntSize(17, 11), IntSize(17, 9) };
1732     return sizes;
1733 }
1734
1735 const int emptyResultsOffset = 9;
1736 void RenderThemeMac::adjustSearchFieldDecorationPartStyle(StyleResolver&, RenderStyle& style, Element*) const
1737 {
1738     IntSize size = sizeForSystemFont(style, resultsButtonSizes());
1739     style.setWidth(Length(size.width() - emptyResultsOffset, Fixed));
1740     style.setHeight(Length(size.height(), Fixed));
1741     style.setBoxShadow(nullptr);
1742 }
1743
1744 bool RenderThemeMac::paintSearchFieldDecorationPart(const RenderObject&, const PaintInfo&, const IntRect&)
1745 {
1746     return false;
1747 }
1748
1749 void RenderThemeMac::adjustSearchFieldResultsDecorationPartStyle(StyleResolver&, RenderStyle& style, Element*) const
1750 {
1751     IntSize size = sizeForSystemFont(style, resultsButtonSizes());
1752     style.setWidth(Length(size.width(), Fixed));
1753     style.setHeight(Length(size.height(), Fixed));
1754     style.setBoxShadow(nullptr);
1755 }
1756
1757 bool RenderThemeMac::paintSearchFieldResultsDecorationPart(const RenderBox& box, const PaintInfo& paintInfo, const IntRect& r)
1758 {
1759     if (!box.element())
1760         return false;
1761     Element* input = box.element()->shadowHost();
1762     if (!input)
1763         input = box.element();
1764     if (!is<RenderBox>(input->renderer()))
1765         return false;
1766     
1767     const RenderBox& inputBox = downcast<RenderBox>(*input->renderer());
1768     LocalCurrentGraphicsContext localContext(paintInfo.context());
1769     setSearchCellState(inputBox, r);
1770
1771     NSSearchFieldCell* search = this->search();
1772
1773     if ([search searchMenuTemplate] != nil)
1774         [search setSearchMenuTemplate:nil];
1775
1776     FloatRect localBounds = [search searchButtonRectForBounds:NSRect(snappedIntRect(inputBox.borderBoxRect()))];
1777     FloatPoint paintingPos = convertToPaintingPosition(inputBox, box, localBounds.location(), r.location());
1778     localBounds.setLocation(paintingPos);
1779
1780     [[search searchButtonCell] drawWithFrame:localBounds inView:documentViewFor(box)];
1781     [[search searchButtonCell] setControlView:nil];
1782     return false;
1783 }
1784
1785 const int resultsArrowWidth = 5;
1786 void RenderThemeMac::adjustSearchFieldResultsButtonStyle(StyleResolver&, RenderStyle& style, Element*) const
1787 {
1788     IntSize size = sizeForSystemFont(style, resultsButtonSizes());
1789     style.setWidth(Length(size.width() + resultsArrowWidth, Fixed));
1790     style.setHeight(Length(size.height(), Fixed));
1791     style.setBoxShadow(nullptr);
1792 }
1793
1794 bool RenderThemeMac::paintSearchFieldResultsButton(const RenderBox& box, const PaintInfo& paintInfo, const IntRect& r)
1795 {
1796     Element* input = box.element()->shadowHost();
1797     if (!input)
1798         input = box.element();
1799     if (!is<RenderBox>(input->renderer()))
1800         return false;
1801     
1802     const RenderBox& inputBox = downcast<RenderBox>(*input->renderer());
1803     LocalCurrentGraphicsContext localContext(paintInfo.context());
1804     setSearchCellState(inputBox, r);
1805
1806     NSSearchFieldCell* search = this->search();
1807
1808     if (![search searchMenuTemplate])
1809         [search setSearchMenuTemplate:searchMenuTemplate()];
1810
1811     GraphicsContextStateSaver stateSaver(paintInfo.context());
1812     float zoomLevel = box.style().effectiveZoom();
1813
1814     FloatRect localBounds = [search searchButtonRectForBounds:NSRect(snappedIntRect(inputBox.contentBoxRect()))];
1815     FloatPoint paintingPos = convertToPaintingPosition(inputBox, box, localBounds.location(), r.location());
1816     
1817     FloatRect unzoomedRect(paintingPos, localBounds.size());
1818     if (zoomLevel != 1.0f) {
1819         unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
1820         unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
1821         paintInfo.context().translate(unzoomedRect.x(), unzoomedRect.y());
1822         paintInfo.context().scale(FloatSize(zoomLevel, zoomLevel));
1823         paintInfo.context().translate(-unzoomedRect.x(), -unzoomedRect.y());
1824     }
1825
1826     [[search searchButtonCell] drawWithFrame:unzoomedRect inView:documentViewFor(box)];
1827     [[search searchButtonCell] setControlView:nil];
1828
1829     return false;
1830 }
1831
1832 bool RenderThemeMac::paintSnapshottedPluginOverlay(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect&)
1833 {
1834     if (paintInfo.phase != PaintPhaseBlockBackground)
1835         return true;
1836
1837     if (!is<RenderBlock>(renderer))
1838         return true;
1839
1840     const RenderBlock& renderBlock = downcast<RenderBlock>(renderer);
1841
1842     LayoutUnit contentWidth = renderBlock.contentWidth();
1843     LayoutUnit contentHeight = renderBlock.contentHeight();
1844     if (!contentWidth || !contentHeight)
1845         return true;
1846
1847     GraphicsContext& context = paintInfo.context();
1848
1849     LayoutSize contentSize(contentWidth, contentHeight);
1850     LayoutPoint contentLocation = renderBlock.location();
1851     contentLocation.move(renderBlock.borderLeft() + renderBlock.paddingLeft(), renderBlock.borderTop() + renderBlock.paddingTop());
1852
1853     LayoutRect rect(contentLocation, contentSize);
1854     IntRect alignedRect = snappedIntRect(rect);
1855     if (alignedRect.width() <= 0 || alignedRect.height() <= 0)
1856         return true;
1857
1858     // We need to get the snapshot image from the plugin element, which should be available
1859     // from our node. Assuming this node is the plugin overlay element, we should get to the
1860     // plugin itself by asking for the shadow root parent, and then its parent.
1861
1862     if (!is<HTMLElement>(*renderBlock.element()))
1863         return true;
1864
1865     HTMLElement& plugInOverlay = downcast<HTMLElement>(*renderBlock.element());
1866     Element* parent = plugInOverlay.parentOrShadowHostElement();
1867     while (parent && !is<HTMLPlugInElement>(*parent))
1868         parent = parent->parentOrShadowHostElement();
1869
1870     if (!parent)
1871         return true;
1872
1873     HTMLPlugInElement& plugInElement = downcast<HTMLPlugInElement>(*parent);
1874     if (!is<HTMLPlugInImageElement>(plugInElement))
1875         return true;
1876
1877     HTMLPlugInImageElement& plugInImageElement = downcast<HTMLPlugInImageElement>(plugInElement);
1878
1879     Image* snapshot = plugInImageElement.snapshotImage();
1880     if (!snapshot)
1881         return true;
1882
1883     RenderSnapshottedPlugIn& plugInRenderer = downcast<RenderSnapshottedPlugIn>(*plugInImageElement.renderer());
1884     FloatPoint snapshotAbsPos = plugInRenderer.localToAbsolute();
1885     snapshotAbsPos.move(plugInRenderer.borderLeft() + plugInRenderer.paddingLeft(), plugInRenderer.borderTop() + plugInRenderer.paddingTop());
1886
1887     // We could draw the snapshot with that coordinates, but we need to make sure there
1888     // isn't a composited layer between us and the plugInRenderer.
1889     for (auto* renderBox = &downcast<RenderBox>(renderer); renderBox != &plugInRenderer; renderBox = renderBox->parentBox()) {
1890         if (renderBox->isComposited()) {
1891             snapshotAbsPos = -renderBox->location();
1892             break;
1893         }
1894     }
1895
1896     LayoutSize pluginSize(plugInRenderer.contentWidth(), plugInRenderer.contentHeight());
1897     LayoutRect pluginRect(snapshotAbsPos, pluginSize);
1898     IntRect alignedPluginRect = snappedIntRect(pluginRect);
1899
1900     if (alignedPluginRect.width() <= 0 || alignedPluginRect.height() <= 0)
1901         return true;
1902
1903     context.drawImage(*snapshot, alignedPluginRect, CompositeSourceOver);
1904     return false;
1905 }
1906
1907 #if ENABLE(DATALIST_ELEMENT)
1908 IntSize RenderThemeMac::sliderTickSize() const
1909 {
1910     return IntSize(1, 3);
1911 }
1912
1913 int RenderThemeMac::sliderTickOffsetFromTrackCenter() const
1914 {
1915     return -9;
1916 }
1917 #endif
1918
1919 const int sliderThumbWidth = 15;
1920 const int sliderThumbHeight = 15;
1921
1922 void RenderThemeMac::adjustSliderThumbSize(RenderStyle& style, Element*) const
1923 {
1924     float zoomLevel = style.effectiveZoom();
1925     if (style.appearance() == SliderThumbHorizontalPart || style.appearance() == SliderThumbVerticalPart) {
1926         style.setWidth(Length(static_cast<int>(sliderThumbWidth * zoomLevel), Fixed));
1927         style.setHeight(Length(static_cast<int>(sliderThumbHeight * zoomLevel), Fixed));
1928     }
1929 }
1930
1931 bool RenderThemeMac::shouldHaveCapsLockIndicator(HTMLInputElement& element) const
1932 {
1933     return element.isPasswordField();
1934 }
1935
1936 NSPopUpButtonCell* RenderThemeMac::popupButton() const
1937 {
1938     if (!m_popupButton) {
1939         m_popupButton = adoptNS([[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO]);
1940         [m_popupButton.get() setUsesItemFromMenu:NO];
1941         [m_popupButton.get() setFocusRingType:NSFocusRingTypeExterior];
1942         // We don't want the app's UI layout direction to affect the appearance of popup buttons in
1943         // web content, which has its own layout direction.
1944         // FIXME: Make this depend on the directionality of the select element, once the rest of the
1945         // rendering code can account for the popup arrows appearing on the other side.
1946         [m_popupButton setUserInterfaceLayoutDirection:NSUserInterfaceLayoutDirectionLeftToRight];
1947     }
1948
1949     return m_popupButton.get();
1950 }
1951
1952 NSSearchFieldCell* RenderThemeMac::search() const
1953 {
1954     if (!m_search) {
1955         m_search = adoptNS([[NSSearchFieldCell alloc] initTextCell:@""]);
1956         [m_search.get() setBezelStyle:NSTextFieldRoundedBezel];
1957         [m_search.get() setBezeled:YES];
1958         [m_search.get() setEditable:YES];
1959         [m_search.get() setFocusRingType:NSFocusRingTypeExterior];
1960         [m_search.get() setCenteredLook:NO];
1961     }
1962
1963     return m_search.get();
1964 }
1965
1966 NSMenu* RenderThemeMac::searchMenuTemplate() const
1967 {
1968     if (!m_searchMenuTemplate)
1969         m_searchMenuTemplate = adoptNS([[NSMenu alloc] initWithTitle:@""]);
1970
1971     return m_searchMenuTemplate.get();
1972 }
1973
1974 NSSliderCell* RenderThemeMac::sliderThumbHorizontal() const
1975 {
1976     if (!m_sliderThumbHorizontal) {
1977         m_sliderThumbHorizontal = adoptNS([[NSSliderCell alloc] init]);
1978         [m_sliderThumbHorizontal.get() setSliderType:NSLinearSlider];
1979         [m_sliderThumbHorizontal.get() setControlSize:NSSmallControlSize];
1980         [m_sliderThumbHorizontal.get() setFocusRingType:NSFocusRingTypeExterior];
1981     }
1982
1983     return m_sliderThumbHorizontal.get();
1984 }
1985
1986 NSSliderCell* RenderThemeMac::sliderThumbVertical() const
1987 {
1988     if (!m_sliderThumbVertical) {
1989         m_sliderThumbVertical = adoptNS([[NSSliderCell alloc] init]);
1990         [m_sliderThumbVertical.get() setSliderType:NSLinearSlider];
1991         [m_sliderThumbVertical.get() setControlSize:NSSmallControlSize];
1992         [m_sliderThumbVertical.get() setFocusRingType:NSFocusRingTypeExterior];
1993     }
1994
1995     return m_sliderThumbVertical.get();
1996 }
1997
1998 NSTextFieldCell* RenderThemeMac::textField() const
1999 {
2000     if (!m_textField) {
2001         m_textField = adoptNS([[WebCoreTextFieldCell alloc] initTextCell:@""]);
2002         [m_textField.get() setBezeled:YES];
2003         [m_textField.get() setEditable:YES];
2004         [m_textField.get() setFocusRingType:NSFocusRingTypeExterior];
2005         // Post-Lion, WebCore can be in charge of paintinng the background thanks to
2006         // the workaround in place for <rdar://problem/11385461>, which is implemented
2007         // above as _coreUIDrawOptionsWithFrame.
2008         [m_textField.get() setDrawsBackground:NO];
2009     }
2010
2011     return m_textField.get();
2012 }
2013
2014 String RenderThemeMac::fileListNameForWidth(const FileList* fileList, const FontCascade& font, int width, bool multipleFilesAllowed) const
2015 {
2016     if (width <= 0)
2017         return String();
2018
2019     String strToTruncate;
2020     if (fileList->isEmpty())
2021         strToTruncate = fileListDefaultLabel(multipleFilesAllowed);
2022     else if (fileList->length() == 1)
2023         strToTruncate = [[NSFileManager defaultManager] displayNameAtPath:(fileList->item(0)->path())];
2024     else
2025         return StringTruncator::rightTruncate(multipleFileUploadText(fileList->length()), width, font);
2026
2027     return StringTruncator::centerTruncate(strToTruncate, width, font);
2028 }
2029
2030 #if ENABLE(SERVICE_CONTROLS)
2031 NSServicesRolloverButtonCell* RenderThemeMac::servicesRolloverButtonCell() const
2032 {
2033 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2034     if (!m_servicesRolloverButton) {
2035         m_servicesRolloverButton = [NSServicesRolloverButtonCell serviceRolloverButtonCellForStyle:NSSharingServicePickerStyleRollover];
2036         [m_servicesRolloverButton setBezelStyle:NSRoundedDisclosureBezelStyle];
2037         [m_servicesRolloverButton setButtonType:NSPushOnPushOffButton];
2038         [m_servicesRolloverButton setImagePosition:NSImageOnly];
2039         [m_servicesRolloverButton setState:NO];
2040     }
2041
2042     return m_servicesRolloverButton.get();
2043 #else
2044     return nil;
2045 #endif
2046 }
2047
2048 bool RenderThemeMac::paintImageControlsButton(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect& rect)
2049 {
2050     if (paintInfo.phase != PaintPhaseBlockBackground)
2051         return true;
2052
2053 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2054     NSServicesRolloverButtonCell *cell = servicesRolloverButtonCell();
2055
2056     LocalCurrentGraphicsContext localContext(paintInfo.context());
2057     GraphicsContextStateSaver stateSaver(paintInfo.context());
2058
2059     paintInfo.context().translate(rect.x(), rect.y());
2060
2061     IntRect innerFrame(IntPoint(), rect.size());
2062     [cell drawWithFrame:innerFrame inView:documentViewFor(renderer)];
2063     [cell setControlView:nil];
2064 #else
2065     UNUSED_PARAM(renderer);
2066     UNUSED_PARAM(rect);
2067 #endif
2068
2069     return true;
2070 }
2071
2072 IntSize RenderThemeMac::imageControlsButtonSize(const RenderObject&) const
2073 {
2074 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2075     return IntSize(servicesRolloverButtonCell().cellSize);
2076 #else
2077     return IntSize();
2078 #endif
2079 }
2080
2081 IntSize RenderThemeMac::imageControlsButtonPositionOffset() const
2082 {
2083 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2084     // FIXME: Currently the offsets will always be the same no matter what image rect you try with.
2085     // This may not always be true in the future.
2086     static const int dummyDimension = 100;
2087     IntRect dummyImageRect(0, 0, dummyDimension, dummyDimension);
2088     NSRect bounds = [servicesRolloverButtonCell() rectForBounds:dummyImageRect preferredEdge:NSMinYEdge];
2089
2090     return IntSize(dummyDimension - bounds.origin.x, bounds.origin.y);
2091 #else
2092     return IntSize();
2093 #endif
2094 }
2095 #endif
2096
2097 #if ENABLE(ATTACHMENT_ELEMENT)
2098 const CGFloat attachmentIconSize = 48;
2099 const CGFloat attachmentIconBackgroundPadding = 6;
2100 const CGFloat attachmentIconBackgroundSize = attachmentIconSize + attachmentIconBackgroundPadding;
2101 const CGFloat attachmentIconSelectionBorderThickness = 1;
2102 const CGFloat attachmentIconBackgroundRadius = 3;
2103 const CGFloat attachmentIconToTitleMargin = 2;
2104
2105 static Color attachmentIconBackgroundColor() { return Color(0, 0, 0, 30); }
2106 static Color attachmentIconBorderColor() { return Color(255, 255, 255, 125); }
2107
2108 const CGFloat attachmentTitleFontSize = 12;
2109 const CGFloat attachmentTitleBackgroundRadius = 3;
2110 const CGFloat attachmentTitleBackgroundPadding = 3;
2111 const CGFloat attachmentTitleMaximumWidth = 100 - (attachmentTitleBackgroundPadding * 2);
2112 const CFIndex attachmentTitleMaximumLineCount = 2;
2113
2114 static Color attachmentTitleInactiveBackgroundColor() { return Color(204, 204, 204, 255); }
2115 static Color attachmentTitleInactiveTextColor() { return Color(100, 100, 100, 255); }
2116
2117 const CGFloat attachmentSubtitleFontSize = 10;
2118 static Color attachmentSubtitleTextColor() { return Color(82, 145, 214, 255); }
2119
2120 const CGFloat attachmentProgressBarWidth = 30;
2121 const CGFloat attachmentProgressBarHeight = 5;
2122 const CGFloat attachmentProgressBarOffset = -9;
2123 const CGFloat attachmentProgressBarBorderWidth = 1;
2124 static Color attachmentProgressBarBackgroundColor() { return Color(0, 0, 0, 89); }
2125 static Color attachmentProgressBarFillColor() { return Color(Color::white); }
2126 static Color attachmentProgressBarBorderColor() { return Color(0, 0, 0, 128); }
2127
2128 const CGFloat attachmentMargin = 3;
2129
2130 struct AttachmentLayout {
2131     explicit AttachmentLayout(const RenderAttachment&);
2132
2133     struct LabelLine {
2134         FloatRect backgroundRect;
2135         FloatPoint origin;
2136         RetainPtr<CTLineRef> line;
2137     };
2138
2139     Vector<LabelLine> lines;
2140
2141     FloatRect iconRect;
2142     FloatRect iconBackgroundRect;
2143     FloatRect attachmentRect;
2144
2145     int baseline;
2146
2147     RetainPtr<CTLineRef> subtitleLine;
2148     FloatRect subtitleTextRect;
2149
2150 private:
2151     void layOutTitle(const RenderAttachment&);
2152     void layOutSubtitle(const RenderAttachment&);
2153
2154     void addTitleLine(CTLineRef, CGFloat& yOffset, Vector<CGPoint> origins, CFIndex lineIndex, const RenderAttachment&);
2155 };
2156
2157 static NSColor *titleTextColorForAttachment(const RenderAttachment& attachment)
2158 {
2159     if (attachment.selectionState() != RenderObject::SelectionNone) {
2160         if (attachment.frame().selection().isFocusedAndActive())
2161             return [NSColor alternateSelectedControlTextColor];    
2162         return (NSColor *)cachedCGColor(attachmentTitleInactiveTextColor());
2163     }
2164
2165     return [NSColor blackColor];
2166 }
2167
2168 void AttachmentLayout::addTitleLine(CTLineRef line, CGFloat& yOffset, Vector<CGPoint> origins, CFIndex lineIndex, const RenderAttachment& attachment)
2169 {
2170     CGRect lineBounds = CTLineGetBoundsWithOptions(line, 0);
2171     CGFloat trailingWhitespaceWidth = CTLineGetTrailingWhitespaceWidth(line);
2172     CGFloat lineWidthIgnoringTrailingWhitespace = lineBounds.size.width - trailingWhitespaceWidth;
2173     CGFloat lineHeight = CGCeiling(lineBounds.size.height);
2174
2175     // Center the line relative to the icon.
2176     CGFloat xOffset = (attachmentIconBackgroundSize / 2) - (lineWidthIgnoringTrailingWhitespace / 2);
2177
2178     if (lineIndex)
2179         yOffset += origins[lineIndex - 1].y - origins[lineIndex].y;
2180
2181     LabelLine labelLine;
2182     labelLine.origin = FloatPoint(xOffset, yOffset + lineHeight - origins.last().y);
2183     labelLine.line = line;
2184     labelLine.backgroundRect = FloatRect(xOffset, yOffset, lineWidthIgnoringTrailingWhitespace, lineHeight);
2185     labelLine.backgroundRect.inflateX(attachmentTitleBackgroundPadding);
2186     labelLine.backgroundRect = encloseRectToDevicePixels(labelLine.backgroundRect, attachment.document().deviceScaleFactor());
2187
2188     // If the text rects are close in size, the curved enclosing background won't
2189     // look right, so make them the same exact size.
2190     if (!lines.isEmpty()) {
2191         float previousBackgroundRectWidth = lines.last().backgroundRect.width();
2192         if (fabs(labelLine.backgroundRect.width() - previousBackgroundRectWidth) < attachmentTitleBackgroundRadius * 4) {
2193             float newBackgroundRectWidth = std::max(previousBackgroundRectWidth, labelLine.backgroundRect.width());
2194             labelLine.backgroundRect.inflateX((newBackgroundRectWidth - labelLine.backgroundRect.width()) / 2);
2195             lines.last().backgroundRect.inflateX((newBackgroundRectWidth - previousBackgroundRectWidth) / 2);
2196         }
2197     }
2198
2199     lines.append(labelLine);
2200 }
2201
2202 void AttachmentLayout::layOutTitle(const RenderAttachment& attachment)
2203 {
2204     CFStringRef language = 0; // By not specifying a language we use the system language.
2205     RetainPtr<CTFontRef> font = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, attachmentTitleFontSize, language));
2206     baseline = CGRound(attachmentIconBackgroundSize + attachmentIconToTitleMargin + CTFontGetAscent(font.get()));
2207
2208     String title = attachment.attachmentElement().attachmentTitle();
2209     if (title.isEmpty())
2210         return;
2211
2212     NSDictionary *textAttributes = @{
2213         (id)kCTFontAttributeName: (id)font.get(),
2214         (id)kCTForegroundColorAttributeName: titleTextColorForAttachment(attachment)
2215     };
2216     RetainPtr<NSAttributedString> attributedTitle = adoptNS([[NSAttributedString alloc] initWithString:title attributes:textAttributes]);
2217     RetainPtr<CTFramesetterRef> titleFramesetter = adoptCF(CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedTitle.get()));
2218
2219     CFRange fitRange;
2220     CGSize titleTextSize = CTFramesetterSuggestFrameSizeWithConstraints(titleFramesetter.get(), CFRangeMake(0, 0), nullptr, CGSizeMake(attachmentTitleMaximumWidth, CGFLOAT_MAX), &fitRange);
2221
2222     RetainPtr<CGPathRef> titlePath = adoptCF(CGPathCreateWithRect(CGRectMake(0, 0, titleTextSize.width, titleTextSize.height), nullptr));
2223     RetainPtr<CTFrameRef> titleFrame = adoptCF(CTFramesetterCreateFrame(titleFramesetter.get(), fitRange, titlePath.get(), nullptr));
2224
2225     CFArrayRef ctLines = CTFrameGetLines(titleFrame.get());
2226     CFIndex lineCount = CFArrayGetCount(ctLines);
2227     if (!lineCount)
2228         return;
2229
2230     Vector<CGPoint> origins(lineCount);
2231     CTFrameGetLineOrigins(titleFrame.get(), CFRangeMake(0, 0), origins.data());
2232
2233     // Lay out and record the first (attachmentTitleMaximumLineCount - 1) lines.
2234     CFIndex lineIndex = 0;
2235     CGFloat yOffset = attachmentIconBackgroundSize + attachmentIconToTitleMargin;
2236     for (; lineIndex < std::min(attachmentTitleMaximumLineCount - 1, lineCount); ++lineIndex) {
2237         CTLineRef line = (CTLineRef)CFArrayGetValueAtIndex(ctLines, lineIndex);
2238         addTitleLine(line, yOffset, origins, lineIndex, attachment);
2239     }
2240
2241     if (lineIndex == lineCount)
2242         return;
2243
2244     // We had text that didn't fit in the first (attachmentTitleMaximumLineCount - 1) lines.
2245     // Combine it into one last line, and center-truncate it.
2246     CTLineRef firstRemainingLine = (CTLineRef)CFArrayGetValueAtIndex(ctLines, lineIndex);
2247     CFIndex remainingRangeStart = CTLineGetStringRange(firstRemainingLine).location;
2248     NSRange remainingRange = NSMakeRange(remainingRangeStart, [attributedTitle length] - remainingRangeStart);
2249     NSAttributedString *remainingString = [attributedTitle attributedSubstringFromRange:remainingRange];
2250     RetainPtr<CTLineRef> remainingLine = adoptCF(CTLineCreateWithAttributedString((CFAttributedStringRef)remainingString));
2251     RetainPtr<NSAttributedString> ellipsisString = adoptNS([[NSAttributedString alloc] initWithString:@"\u2026" attributes:textAttributes]);
2252     RetainPtr<CTLineRef> ellipsisLine = adoptCF(CTLineCreateWithAttributedString((CFAttributedStringRef)ellipsisString.get()));
2253     RetainPtr<CTLineRef> truncatedLine = adoptCF(CTLineCreateTruncatedLine(remainingLine.get(), attachmentTitleMaximumWidth, kCTLineTruncationMiddle, ellipsisLine.get()));
2254
2255     if (!truncatedLine)
2256         truncatedLine = remainingLine;
2257
2258     addTitleLine(truncatedLine.get(), yOffset, origins, lineIndex, attachment);
2259 }
2260
2261 void AttachmentLayout::layOutSubtitle(const RenderAttachment& attachment)
2262 {
2263     String subtitleText = attachment.attachmentElement().fastGetAttribute(subtitleAttr);
2264
2265     if (subtitleText.isEmpty())
2266         return;
2267
2268     CFStringRef language = 0; // By not specifying a language we use the system language.
2269     RetainPtr<CTFontRef> font = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, attachmentSubtitleFontSize, language));
2270     NSDictionary *textAttributes = @{
2271         (id)kCTFontAttributeName: (id)font.get(),
2272         (id)kCTForegroundColorAttributeName: (NSColor *)cachedCGColor(attachmentSubtitleTextColor())
2273     };
2274     RetainPtr<NSAttributedString> attributedSubtitleText = adoptNS([[NSAttributedString alloc] initWithString:subtitleText attributes:textAttributes]);
2275     subtitleLine = adoptCF(CTLineCreateWithAttributedString((CFAttributedStringRef)attributedSubtitleText.get()));
2276
2277     CGRect lineBounds = CTLineGetBoundsWithOptions(subtitleLine.get(), 0);
2278
2279     // Center the line relative to the icon.
2280     CGFloat xOffset = (attachmentIconBackgroundSize / 2) - (lineBounds.size.width / 2);
2281     CGFloat yOffset = 0;
2282
2283     if (!lines.isEmpty())
2284         yOffset = lines.last().backgroundRect.maxY();
2285     else
2286         yOffset = attachmentIconBackgroundSize + attachmentIconToTitleMargin;
2287
2288     LabelLine labelLine;
2289     subtitleTextRect = FloatRect(xOffset, yOffset, lineBounds.size.width, lineBounds.size.height);
2290 }
2291
2292 AttachmentLayout::AttachmentLayout(const RenderAttachment& attachment)
2293 {
2294     layOutTitle(attachment);
2295     layOutSubtitle(attachment);
2296
2297     iconBackgroundRect = FloatRect(0, 0, attachmentIconBackgroundSize, attachmentIconBackgroundSize);
2298
2299     iconRect = iconBackgroundRect;
2300     iconRect.setSize(FloatSize(attachmentIconSize, attachmentIconSize));
2301     iconRect.move(attachmentIconBackgroundPadding / 2, attachmentIconBackgroundPadding / 2);
2302
2303     attachmentRect = iconBackgroundRect;
2304     for (const auto& line : lines)
2305         attachmentRect.unite(line.backgroundRect);
2306     attachmentRect.unite(subtitleTextRect);
2307     attachmentRect.inflate(attachmentMargin);
2308     attachmentRect = encloseRectToDevicePixels(attachmentRect, attachment.document().deviceScaleFactor());
2309 }
2310
2311 LayoutSize RenderThemeMac::attachmentIntrinsicSize(const RenderAttachment& attachment) const
2312 {
2313     AttachmentLayout layout(attachment);
2314     return LayoutSize(layout.attachmentRect.size());
2315 }
2316
2317 int RenderThemeMac::attachmentBaseline(const RenderAttachment& attachment) const
2318 {
2319     AttachmentLayout layout(attachment);
2320     return layout.baseline;
2321 }
2322
2323 static void paintAttachmentIconBackground(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout)
2324 {
2325     // FIXME: Finder has a discontinuous behavior here when you have a background color other than white,
2326     // where it switches into 'bordered mode' and the border pops in on top of the background.
2327     bool paintBorder = true;
2328
2329     FloatRect backgroundRect = layout.iconBackgroundRect;
2330     if (paintBorder)
2331         backgroundRect.inflate(-attachmentIconSelectionBorderThickness);
2332
2333     context.fillRoundedRect(FloatRoundedRect(backgroundRect, FloatRoundedRect::Radii(attachmentIconBackgroundRadius)), attachmentIconBackgroundColor());
2334
2335     if (paintBorder) {
2336         FloatRect borderRect = layout.iconBackgroundRect;
2337         borderRect.inflate(-attachmentIconSelectionBorderThickness / 2);
2338
2339         FloatSize iconBackgroundRadiusSize(attachmentIconBackgroundRadius, attachmentIconBackgroundRadius);
2340         Path borderPath;
2341         borderPath.addRoundedRect(borderRect, iconBackgroundRadiusSize);
2342         context.setStrokeColor(attachmentIconBorderColor());
2343         context.setStrokeThickness(attachmentIconSelectionBorderThickness);
2344         context.strokePath(borderPath);
2345     }
2346 }
2347
2348 static RefPtr<Icon> iconForAttachment(const RenderAttachment& attachment)
2349 {
2350     String MIMEType = attachment.attachmentElement().attachmentType();
2351     if (!MIMEType.isEmpty()) {
2352         if (equalIgnoringASCIICase(MIMEType, "multipart/x-folder")) {
2353             if (auto icon = Icon::createIconForUTI("public.directory"))
2354                 return icon;
2355         } else if (auto icon = Icon::createIconForMIMEType(MIMEType))
2356             return icon;
2357     }
2358
2359     if (File* file = attachment.attachmentElement().file()) {
2360         if (auto icon = Icon::createIconForFiles({ file->path() }))
2361             return icon;
2362     }
2363
2364     NSString *fileExtension = [static_cast<NSString *>(attachment.attachmentElement().attachmentTitle()) pathExtension];
2365     if (fileExtension.length) {
2366         if (auto icon = Icon::createIconForFileExtension(fileExtension))
2367             return icon;
2368     }
2369
2370     return Icon::createIconForUTI("public.data");
2371 }
2372
2373 static void paintAttachmentIcon(const RenderAttachment& attachment, GraphicsContext& context, AttachmentLayout& layout)
2374 {
2375     auto icon = iconForAttachment(attachment);
2376     if (!icon)
2377         return;
2378     icon->paint(context, layout.iconRect);
2379 }
2380
2381 static void paintAttachmentTitleBackground(const RenderAttachment& attachment, GraphicsContext& context, AttachmentLayout& layout)
2382 {
2383     if (layout.lines.isEmpty())
2384         return;
2385
2386     Vector<FloatRect> backgroundRects;
2387
2388     for (size_t i = 0; i < layout.lines.size(); ++i)
2389         backgroundRects.append(layout.lines[i].backgroundRect);
2390
2391     Color backgroundColor;
2392     if (attachment.frame().selection().isFocusedAndActive())
2393         backgroundColor = convertNSColorToColor([NSColor alternateSelectedControlColor]);
2394     else
2395         backgroundColor = attachmentTitleInactiveBackgroundColor();
2396
2397     context.setFillColor(backgroundColor);
2398
2399     Path backgroundPath = PathUtilities::pathWithShrinkWrappedRects(backgroundRects, attachmentTitleBackgroundRadius);
2400     context.fillPath(backgroundPath);
2401 }
2402
2403 static void paintAttachmentTitle(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout)
2404 {
2405     for (const auto& line : layout.lines) {
2406         GraphicsContextStateSaver saver(context);
2407
2408         context.translate(toFloatSize(line.origin));
2409         context.scale(FloatSize(1, -1));
2410
2411         CGContextSetTextMatrix(context.platformContext(), CGAffineTransformIdentity);
2412         CTLineDraw(line.line.get(), context.platformContext());
2413     }
2414 }
2415
2416 static void paintAttachmentSubtitle(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout)
2417 {
2418     GraphicsContextStateSaver saver(context);
2419
2420     context.translate(toFloatSize(layout.subtitleTextRect.minXMaxYCorner()));
2421     context.scale(FloatSize(1, -1));
2422
2423     CGContextSetTextMatrix(context.platformContext(), CGAffineTransformIdentity);
2424     CTLineDraw(layout.subtitleLine.get(), context.platformContext());
2425 }
2426
2427 static void paintAttachmentProgress(const RenderAttachment& attachment, GraphicsContext& context, AttachmentLayout& layout)
2428 {
2429     String progressString = attachment.attachmentElement().fastGetAttribute(progressAttr);
2430     if (progressString.isEmpty())
2431         return;
2432     bool validProgress;
2433     float progress = progressString.toFloat(&validProgress);
2434     if (!validProgress)
2435         return;
2436
2437     GraphicsContextStateSaver saver(context);
2438
2439     FloatRect progressBounds((attachmentIconBackgroundSize - attachmentProgressBarWidth) / 2, layout.iconBackgroundRect.maxY() + attachmentProgressBarOffset - attachmentProgressBarHeight, attachmentProgressBarWidth, attachmentProgressBarHeight);
2440
2441     FloatRect borderRect = progressBounds;
2442     borderRect.inflate(-0.5);
2443     FloatRect backgroundRect = borderRect;
2444     backgroundRect.inflate(-attachmentProgressBarBorderWidth / 2);
2445
2446     FloatRoundedRect backgroundRoundedRect(backgroundRect, FloatRoundedRect::Radii(backgroundRect.height() / 2));
2447     context.fillRoundedRect(backgroundRoundedRect, attachmentProgressBarBackgroundColor());
2448
2449     {
2450         GraphicsContextStateSaver clipSaver(context);
2451         context.clipRoundedRect(backgroundRoundedRect);
2452
2453         FloatRect progressRect = progressBounds;
2454         progressRect.setWidth(progressRect.width() * progress);
2455         progressRect = encloseRectToDevicePixels(progressRect, attachment.document().deviceScaleFactor());
2456
2457         context.fillRect(progressRect, attachmentProgressBarFillColor());
2458     }
2459
2460     Path borderPath;
2461     float borderRadius = borderRect.height() / 2;
2462     borderPath.addRoundedRect(borderRect, FloatSize(borderRadius, borderRadius));
2463     context.setStrokeColor(attachmentProgressBarBorderColor());
2464     context.setStrokeThickness(attachmentProgressBarBorderWidth);
2465     context.strokePath(borderPath);
2466 }
2467
2468 bool RenderThemeMac::paintAttachment(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect& paintRect)
2469 {
2470     if (!is<RenderAttachment>(renderer))
2471         return false;
2472
2473     const RenderAttachment& attachment = downcast<RenderAttachment>(renderer);
2474
2475     AttachmentLayout layout(attachment);
2476
2477     GraphicsContext& context = paintInfo.context();
2478     LocalCurrentGraphicsContext localContext(context);
2479     GraphicsContextStateSaver saver(context);
2480
2481     context.translate(toFloatSize(paintRect.location()));
2482     context.translate(FloatSize((layout.attachmentRect.width() - attachmentIconBackgroundSize) / 2, 0));
2483
2484     bool useSelectedStyle = attachment.selectionState() != RenderObject::SelectionNone;
2485
2486     if (useSelectedStyle)
2487         paintAttachmentIconBackground(attachment, context, layout);
2488     paintAttachmentIcon(attachment, context, layout);
2489     if (useSelectedStyle)
2490         paintAttachmentTitleBackground(attachment, context, layout);
2491     paintAttachmentTitle(attachment, context, layout);
2492     paintAttachmentSubtitle(attachment, context, layout);
2493     paintAttachmentProgress(attachment, context, layout);
2494
2495     return true;
2496 }
2497
2498 #endif // ENABLE(ATTACHMENT_ELEMENT)
2499
2500 } // namespace WebCore
2501
2502 #endif // !PLATFORM(IOS)