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