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