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