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