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