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