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