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