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