9287d4009307c82fc79647cea1ee59cae09ffd9a
[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 "Document.h"
30 #import "Element.h"
31 #import "FileList.h"
32 #import "FloatRoundedRect.h"
33 #import "FocusController.h"
34 #import "Frame.h"
35 #import "FrameSelection.h"
36 #import "FrameView.h"
37 #import "GeometryUtilities.h"
38 #import "GraphicsContextCG.h"
39 #import "HTMLAttachmentElement.h"
40 #import "HTMLInputElement.h"
41 #import "HTMLMediaElement.h"
42 #import "HTMLNames.h"
43 #import "HTMLPlugInImageElement.h"
44 #import "Icon.h"
45 #import "Image.h"
46 #import "ImageBuffer.h"
47 #import "LocalCurrentGraphicsContext.h"
48 #import "LocalizedStrings.h"
49 #import "MediaControlElements.h"
50 #import "Page.h"
51 #import "PaintInfo.h"
52 #import "PathUtilities.h"
53 #import "RenderAttachment.h"
54 #import "RenderLayer.h"
55 #import "RenderMedia.h"
56 #import "RenderMediaControlElements.h"
57 #import "RenderMediaControls.h"
58 #import "RenderProgress.h"
59 #import "RenderSlider.h"
60 #import "RenderSnapshottedPlugIn.h"
61 #import "RenderView.h"
62 #import "RuntimeEnabledFeatures.h"
63 #import "SharedBuffer.h"
64 #import "StringTruncator.h"
65 #import "StyleResolver.h"
66 #import "ThemeMac.h"
67 #import "TimeRanges.h"
68 #import "UTIUtilities.h"
69 #import "UserAgentScripts.h"
70 #import "UserAgentStyleSheets.h"
71 #import "WebCoreSystemInterface.h"
72 #import <Carbon/Carbon.h>
73 #import <Cocoa/Cocoa.h>
74 #import <math.h>
75 #import <pal/spi/cg/CoreGraphicsSPI.h>
76 #import <pal/spi/cocoa/NSColorSPI.h>
77 #import <pal/spi/mac/NSCellSPI.h>
78 #import <pal/spi/mac/NSSharingServicePickerSPI.h>
79 #import <wtf/MathExtras.h>
80 #import <wtf/RetainPtr.h>
81 #import <wtf/StdLibExtras.h>
82 #import <wtf/text/StringBuilder.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     _NSDrawCarbonThemeListBox(r, isEnabled(o) && !isReadOnlyControl(o), YES, YES);
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.location());
953         paintInfo.context().scale(zoomLevel);
954         paintInfo.context().translate(-inflatedRect.location());
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.setSize(unzoomedRect.size() / zoomLevel);
1587         paintInfo.context().translate(unzoomedRect.location());
1588         paintInfo.context().scale(zoomLevel);
1589         paintInfo.context().translate(-unzoomedRect.location());
1590     }
1591
1592     bool shouldDrawCell = true;
1593     bool shouldDrawFocusRing = false;
1594     float deviceScaleFactor = o.page().deviceScaleFactor();
1595     bool shouldUseImageBuffer = deviceScaleFactor != 1 || zoomLevel != 1;
1596     ThemeMac::drawCellOrFocusRingWithViewIntoContext(sliderThumbCell, paintInfo.context(), unzoomedRect, view, shouldDrawCell, shouldDrawFocusRing, shouldUseImageBuffer, deviceScaleFactor);
1597     [sliderThumbCell setControlView:nil];
1598
1599     return false;
1600 }
1601
1602 bool RenderThemeMac::paintSearchField(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1603 {
1604     LocalCurrentGraphicsContext localContext(paintInfo.context());
1605     NSSearchFieldCell* search = this->search();
1606
1607     setSearchCellState(o, r);
1608
1609     GraphicsContextStateSaver stateSaver(paintInfo.context());
1610
1611     float zoomLevel = o.style().effectiveZoom();
1612
1613     FloatRect unzoomedRect = r;
1614     if (zoomLevel != 1.0f) {
1615         unzoomedRect.setSize(unzoomedRect.size() / zoomLevel);
1616         paintInfo.context().translate(unzoomedRect.location());
1617         paintInfo.context().scale(zoomLevel);
1618         paintInfo.context().translate(-unzoomedRect.location());
1619     }
1620
1621     // Set the search button to nil before drawing.  Then reset it so we can draw it later.
1622     [search setSearchButtonCell:nil];
1623
1624     paintCellAndSetFocusedElementNeedsRepaintIfNecessary(search, o, paintInfo, unzoomedRect);
1625     [search setControlView:nil];
1626     [search resetSearchButtonCell];
1627
1628     return false;
1629 }
1630
1631 void RenderThemeMac::setSearchCellState(const RenderObject& o, const IntRect&)
1632 {
1633     NSSearchFieldCell* search = this->search();
1634
1635     [search setPlaceholderString:@""];
1636     [search setControlSize:controlSizeForFont(o.style())];
1637
1638     // Update the various states we respond to.
1639     updateEnabledState(search, o);
1640     updateFocusedState(search, o);
1641 }
1642
1643 const IntSize* RenderThemeMac::searchFieldSizes() const
1644 {
1645     static const IntSize sizes[3] = { IntSize(0, 22), IntSize(0, 19), IntSize(0, 17) };
1646     return sizes;
1647 }
1648
1649 void RenderThemeMac::setSearchFieldSize(RenderStyle& style) const
1650 {
1651     // If the width and height are both specified, then we have nothing to do.
1652     if (!style.width().isIntrinsicOrAuto() && !style.height().isAuto())
1653         return;
1654
1655     // Use the font size to determine the intrinsic width of the control.
1656     setSizeFromFont(style, searchFieldSizes());
1657 }
1658
1659 void RenderThemeMac::adjustSearchFieldStyle(StyleResolver& styleResolver, RenderStyle& style, const Element*) const
1660 {
1661     // Override border.
1662     style.resetBorder();
1663     const short borderWidth = 2 * style.effectiveZoom();
1664     style.setBorderLeftWidth(borderWidth);
1665     style.setBorderLeftStyle(INSET);
1666     style.setBorderRightWidth(borderWidth);
1667     style.setBorderRightStyle(INSET);
1668     style.setBorderBottomWidth(borderWidth);
1669     style.setBorderBottomStyle(INSET);
1670     style.setBorderTopWidth(borderWidth);
1671     style.setBorderTopStyle(INSET);
1672
1673     // Override height.
1674     style.setHeight(Length(Auto));
1675     setSearchFieldSize(style);
1676
1677     // Override padding size to match AppKit text positioning.
1678     const int padding = 1 * style.effectiveZoom();
1679     style.setPaddingLeft(Length(padding, Fixed));
1680     style.setPaddingRight(Length(padding, Fixed));
1681     style.setPaddingTop(Length(padding, Fixed));
1682     style.setPaddingBottom(Length(padding, Fixed));
1683
1684     NSControlSize controlSize = controlSizeForFont(style);
1685     setFontFromControlSize(styleResolver, style, controlSize);
1686
1687     style.setBoxShadow(nullptr);
1688 }
1689
1690 bool RenderThemeMac::paintSearchFieldCancelButton(const RenderBox& box, const PaintInfo& paintInfo, const IntRect& r)
1691 {
1692     auto adjustedCancelButtonRect = [this, &box] (const FloatRect& localBoundsForCancelButton) -> FloatRect
1693     {
1694         IntSize cancelButtonSizeBasedOnFontSize = sizeForSystemFont(box.style(), cancelButtonSizes());
1695         FloatSize diff = localBoundsForCancelButton.size() - FloatSize(cancelButtonSizeBasedOnFontSize);
1696         if (!diff.width() && !diff.height())
1697             return localBoundsForCancelButton;
1698         // Vertically centered and right aligned.
1699         FloatRect adjustedLocalBoundsForCancelButton = localBoundsForCancelButton;
1700         adjustedLocalBoundsForCancelButton.move(diff.width(), floorToDevicePixel(diff.height() / 2, box.document().deviceScaleFactor()));
1701         adjustedLocalBoundsForCancelButton.setSize(cancelButtonSizeBasedOnFontSize);
1702         return adjustedLocalBoundsForCancelButton;
1703     };
1704
1705     if (!box.element())
1706         return false;
1707     Element* input = box.element()->shadowHost();
1708     if (!input)
1709         input = box.element();
1710
1711     if (!is<RenderBox>(input->renderer()))
1712         return false;
1713
1714     const RenderBox& inputBox = downcast<RenderBox>(*input->renderer());
1715     LocalCurrentGraphicsContext localContext(paintInfo.context());
1716     setSearchCellState(inputBox, r);
1717
1718     NSSearchFieldCell* search = this->search();
1719
1720     if (!input->isDisabledFormControl() && (is<HTMLTextFormControlElement>(*input) && !downcast<HTMLTextFormControlElement>(*input).isReadOnly()))
1721         updatePressedState([search cancelButtonCell], box);
1722     else if ([[search cancelButtonCell] isHighlighted])
1723         [[search cancelButtonCell] setHighlighted:NO];
1724
1725     GraphicsContextStateSaver stateSaver(paintInfo.context());
1726
1727     float zoomLevel = box.style().effectiveZoom();
1728
1729     FloatRect localBounds = adjustedCancelButtonRect([search cancelButtonRectForBounds:NSRect(snappedIntRect(inputBox.contentBoxRect()))]);
1730     // Adjust position based on the content direction.
1731     float adjustedXPosition;
1732     if (box.style().direction() == RTL)
1733         adjustedXPosition = inputBox.contentBoxRect().x();
1734     else
1735         adjustedXPosition = inputBox.contentBoxRect().maxX() - localBounds.size().width();
1736     
1737     localBounds.setX(adjustedXPosition);
1738     FloatPoint paintingPos = convertToPaintingPosition(inputBox, box, localBounds.location(), r.location());
1739
1740     FloatRect unzoomedRect(paintingPos, localBounds.size());
1741     if (zoomLevel != 1.0f) {
1742         unzoomedRect.setSize(unzoomedRect.size() / zoomLevel);
1743         paintInfo.context().translate(unzoomedRect.location());
1744         paintInfo.context().scale(zoomLevel);
1745         paintInfo.context().translate(-unzoomedRect.location());
1746     }
1747     [[search cancelButtonCell] drawWithFrame:unzoomedRect inView:documentViewFor(box)];
1748     [[search cancelButtonCell] setControlView:nil];
1749     return false;
1750 }
1751
1752 const IntSize* RenderThemeMac::cancelButtonSizes() const
1753 {
1754     static const IntSize sizes[3] = { IntSize(22, 22), IntSize(19, 19), IntSize(15, 15) };
1755     return sizes;
1756 }
1757
1758 void RenderThemeMac::adjustSearchFieldCancelButtonStyle(StyleResolver&, RenderStyle& style, const Element*) const
1759 {
1760     IntSize size = sizeForSystemFont(style, cancelButtonSizes());
1761     style.setWidth(Length(size.width(), Fixed));
1762     style.setHeight(Length(size.height(), Fixed));
1763     style.setBoxShadow(nullptr);
1764 }
1765
1766 const int resultsArrowWidth = 5;
1767 const IntSize* RenderThemeMac::resultsButtonSizes() const
1768 {
1769     static const IntSize sizes[3] = { IntSize(19, 22), IntSize(17, 19), IntSize(17, 15) };
1770     return sizes;
1771 }
1772
1773 const int emptyResultsOffset = 9;
1774 void RenderThemeMac::adjustSearchFieldDecorationPartStyle(StyleResolver&, RenderStyle& style, const Element*) const
1775 {
1776     IntSize size = sizeForSystemFont(style, resultsButtonSizes());
1777     style.setWidth(Length(size.width() - emptyResultsOffset, Fixed));
1778     style.setHeight(Length(size.height(), Fixed));
1779     style.setBoxShadow(nullptr);
1780 }
1781
1782 bool RenderThemeMac::paintSearchFieldDecorationPart(const RenderObject&, const PaintInfo&, const IntRect&)
1783 {
1784     return false;
1785 }
1786
1787 void RenderThemeMac::adjustSearchFieldResultsDecorationPartStyle(StyleResolver&, RenderStyle& style, const Element*) const
1788 {
1789     IntSize size = sizeForSystemFont(style, resultsButtonSizes());
1790     style.setWidth(Length(size.width(), Fixed));
1791     style.setHeight(Length(size.height(), Fixed));
1792     style.setBoxShadow(nullptr);
1793 }
1794
1795 bool RenderThemeMac::paintSearchFieldResultsDecorationPart(const RenderBox& box, const PaintInfo& paintInfo, const IntRect& r)
1796 {
1797     if (!box.element())
1798         return false;
1799     Element* input = box.element()->shadowHost();
1800     if (!input)
1801         input = box.element();
1802     if (!is<RenderBox>(input->renderer()))
1803         return false;
1804     
1805     const RenderBox& inputBox = downcast<RenderBox>(*input->renderer());
1806     LocalCurrentGraphicsContext localContext(paintInfo.context());
1807     setSearchCellState(inputBox, r);
1808
1809     NSSearchFieldCell* search = this->search();
1810
1811     if ([search searchMenuTemplate] != nil)
1812         [search setSearchMenuTemplate:nil];
1813
1814     FloatRect localBounds = [search searchButtonRectForBounds:NSRect(snappedIntRect(inputBox.borderBoxRect()))];
1815     FloatPoint paintingPos = convertToPaintingPosition(inputBox, box, localBounds.location(), r.location());
1816     localBounds.setLocation(paintingPos);
1817
1818     [[search searchButtonCell] drawWithFrame:localBounds inView:documentViewFor(box)];
1819     [[search searchButtonCell] setControlView:nil];
1820     return false;
1821 }
1822
1823 void RenderThemeMac::adjustSearchFieldResultsButtonStyle(StyleResolver&, RenderStyle& style, const Element*) const
1824 {
1825     IntSize size = sizeForSystemFont(style, resultsButtonSizes());
1826     style.setWidth(Length(size.width() + resultsArrowWidth, Fixed));
1827     style.setHeight(Length(size.height(), Fixed));
1828     style.setBoxShadow(nullptr);
1829 }
1830
1831 bool RenderThemeMac::paintSearchFieldResultsButton(const RenderBox& box, const PaintInfo& paintInfo, const IntRect& r)
1832 {
1833     auto adjustedResultButtonRect = [this, &box] (const FloatRect& localBounds) -> FloatRect
1834     {
1835         IntSize buttonSize = sizeForSystemFont(box.style(), resultsButtonSizes());
1836         buttonSize.expand(resultsArrowWidth, 0);
1837         FloatSize diff = localBounds.size() - FloatSize(buttonSize);
1838         if (!diff.isZero())
1839             return localBounds;
1840         // Vertically centered and left aligned.
1841         FloatRect adjustedLocalBounds = localBounds;
1842         adjustedLocalBounds.move(0, floorToDevicePixel(diff.height() / 2, box.document().deviceScaleFactor()));
1843         adjustedLocalBounds.setSize(buttonSize);
1844         return adjustedLocalBounds;
1845     };
1846
1847     Element* input = box.element()->shadowHost();
1848     if (!input)
1849         input = box.element();
1850     if (!is<RenderBox>(input->renderer()))
1851         return false;
1852     
1853     const RenderBox& inputBox = downcast<RenderBox>(*input->renderer());
1854     LocalCurrentGraphicsContext localContext(paintInfo.context());
1855     setSearchCellState(inputBox, r);
1856
1857     NSSearchFieldCell* search = this->search();
1858
1859     if (![search searchMenuTemplate])
1860         [search setSearchMenuTemplate:searchMenuTemplate()];
1861
1862     GraphicsContextStateSaver stateSaver(paintInfo.context());
1863     float zoomLevel = box.style().effectiveZoom();
1864
1865     FloatRect localBounds = adjustedResultButtonRect([search searchButtonRectForBounds:NSRect(snappedIntRect(inputBox.contentBoxRect()))]);
1866     // Adjust position based on the content direction.
1867     float adjustedXPosition;
1868     if (box.style().direction() == RTL)
1869         adjustedXPosition = inputBox.contentBoxRect().maxX() - localBounds.size().width();
1870     else
1871         adjustedXPosition = inputBox.contentBoxRect().x();
1872     localBounds.setX(adjustedXPosition);
1873     FloatPoint paintingPos = convertToPaintingPosition(inputBox, box, localBounds.location(), r.location());
1874     
1875     FloatRect unzoomedRect(paintingPos, localBounds.size());
1876     if (zoomLevel != 1.0f) {
1877         unzoomedRect.setSize(unzoomedRect.size() / zoomLevel);
1878         paintInfo.context().translate(unzoomedRect.location());
1879         paintInfo.context().scale(zoomLevel);
1880         paintInfo.context().translate(-unzoomedRect.location());
1881     }
1882
1883     [[search searchButtonCell] drawWithFrame:unzoomedRect inView:documentViewFor(box)];
1884     [[search searchButtonCell] setControlView:nil];
1885
1886     return false;
1887 }
1888
1889 bool RenderThemeMac::paintSnapshottedPluginOverlay(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect&)
1890 {
1891     if (paintInfo.phase != PaintPhaseBlockBackground)
1892         return true;
1893
1894     if (!is<RenderBlock>(renderer))
1895         return true;
1896
1897     const RenderBlock& renderBlock = downcast<RenderBlock>(renderer);
1898
1899     LayoutUnit contentWidth = renderBlock.contentWidth();
1900     LayoutUnit contentHeight = renderBlock.contentHeight();
1901     if (!contentWidth || !contentHeight)
1902         return true;
1903
1904     GraphicsContext& context = paintInfo.context();
1905
1906     LayoutSize contentSize(contentWidth, contentHeight);
1907     LayoutPoint contentLocation = renderBlock.location();
1908     contentLocation.move(renderBlock.borderLeft() + renderBlock.paddingLeft(), renderBlock.borderTop() + renderBlock.paddingTop());
1909
1910     LayoutRect rect(contentLocation, contentSize);
1911     IntRect alignedRect = snappedIntRect(rect);
1912     if (alignedRect.width() <= 0 || alignedRect.height() <= 0)
1913         return true;
1914
1915     // We need to get the snapshot image from the plugin element, which should be available
1916     // from our node. Assuming this node is the plugin overlay element, we should get to the
1917     // plugin itself by asking for the shadow root parent, and then its parent.
1918
1919     if (!is<HTMLElement>(*renderBlock.element()))
1920         return true;
1921
1922     HTMLElement& plugInOverlay = downcast<HTMLElement>(*renderBlock.element());
1923     Element* parent = plugInOverlay.parentOrShadowHostElement();
1924     while (parent && !is<HTMLPlugInElement>(*parent))
1925         parent = parent->parentOrShadowHostElement();
1926
1927     if (!parent)
1928         return true;
1929
1930     HTMLPlugInElement& plugInElement = downcast<HTMLPlugInElement>(*parent);
1931     if (!is<HTMLPlugInImageElement>(plugInElement))
1932         return true;
1933
1934     HTMLPlugInImageElement& plugInImageElement = downcast<HTMLPlugInImageElement>(plugInElement);
1935
1936     Image* snapshot = plugInImageElement.snapshotImage();
1937     if (!snapshot)
1938         return true;
1939
1940     RenderSnapshottedPlugIn& plugInRenderer = downcast<RenderSnapshottedPlugIn>(*plugInImageElement.renderer());
1941     FloatPoint snapshotAbsPos = plugInRenderer.localToAbsolute();
1942     snapshotAbsPos.move(plugInRenderer.borderLeft() + plugInRenderer.paddingLeft(), plugInRenderer.borderTop() + plugInRenderer.paddingTop());
1943
1944     // We could draw the snapshot with that coordinates, but we need to make sure there
1945     // isn't a composited layer between us and the plugInRenderer.
1946     for (auto* renderBox = &downcast<RenderBox>(renderer); renderBox != &plugInRenderer; renderBox = renderBox->parentBox()) {
1947         if (renderBox->isComposited()) {
1948             snapshotAbsPos = -renderBox->location();
1949             break;
1950         }
1951     }
1952
1953     LayoutSize pluginSize(plugInRenderer.contentWidth(), plugInRenderer.contentHeight());
1954     LayoutRect pluginRect(snapshotAbsPos, pluginSize);
1955     IntRect alignedPluginRect = snappedIntRect(pluginRect);
1956
1957     if (alignedPluginRect.width() <= 0 || alignedPluginRect.height() <= 0)
1958         return true;
1959
1960     context.drawImage(*snapshot, alignedPluginRect, CompositeSourceOver);
1961     return false;
1962 }
1963
1964 #if ENABLE(DATALIST_ELEMENT)
1965 IntSize RenderThemeMac::sliderTickSize() const
1966 {
1967     return IntSize(1, 3);
1968 }
1969
1970 int RenderThemeMac::sliderTickOffsetFromTrackCenter() const
1971 {
1972     return -9;
1973 }
1974 #endif
1975
1976 const int sliderThumbWidth = 15;
1977 const int sliderThumbHeight = 15;
1978
1979 void RenderThemeMac::adjustSliderThumbSize(RenderStyle& style, const Element*) const
1980 {
1981     float zoomLevel = style.effectiveZoom();
1982     if (style.appearance() == SliderThumbHorizontalPart || style.appearance() == SliderThumbVerticalPart) {
1983         style.setWidth(Length(static_cast<int>(sliderThumbWidth * zoomLevel), Fixed));
1984         style.setHeight(Length(static_cast<int>(sliderThumbHeight * zoomLevel), Fixed));
1985     }
1986 }
1987
1988 bool RenderThemeMac::shouldHaveCapsLockIndicator(const HTMLInputElement& element) const
1989 {
1990     return element.isPasswordField();
1991 }
1992
1993 NSPopUpButtonCell* RenderThemeMac::popupButton() const
1994 {
1995     if (!m_popupButton) {
1996         m_popupButton = adoptNS([[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO]);
1997         [m_popupButton.get() setUsesItemFromMenu:NO];
1998         [m_popupButton.get() setFocusRingType:NSFocusRingTypeExterior];
1999         [m_popupButton setUserInterfaceLayoutDirection:NSUserInterfaceLayoutDirectionLeftToRight];
2000     }
2001
2002     return m_popupButton.get();
2003 }
2004
2005 NSSearchFieldCell* RenderThemeMac::search() const
2006 {
2007     if (!m_search) {
2008         m_search = adoptNS([[NSSearchFieldCell alloc] initTextCell:@""]);
2009         [m_search.get() setBezelStyle:NSTextFieldRoundedBezel];
2010         [m_search.get() setBezeled:YES];
2011         [m_search.get() setEditable:YES];
2012         [m_search.get() setFocusRingType:NSFocusRingTypeExterior];
2013         [m_search.get() setCenteredLook:NO];
2014     }
2015
2016     return m_search.get();
2017 }
2018
2019 NSMenu* RenderThemeMac::searchMenuTemplate() const
2020 {
2021     if (!m_searchMenuTemplate)
2022         m_searchMenuTemplate = adoptNS([[NSMenu alloc] initWithTitle:@""]);
2023
2024     return m_searchMenuTemplate.get();
2025 }
2026
2027 NSSliderCell* RenderThemeMac::sliderThumbHorizontal() const
2028 {
2029     if (!m_sliderThumbHorizontal) {
2030         m_sliderThumbHorizontal = adoptNS([[NSSliderCell alloc] init]);
2031         [m_sliderThumbHorizontal.get() setSliderType:NSSliderTypeLinear];
2032         [m_sliderThumbHorizontal.get() setControlSize:NSControlSizeSmall];
2033         [m_sliderThumbHorizontal.get() setFocusRingType:NSFocusRingTypeExterior];
2034     }
2035
2036     return m_sliderThumbHorizontal.get();
2037 }
2038
2039 NSSliderCell* RenderThemeMac::sliderThumbVertical() const
2040 {
2041     if (!m_sliderThumbVertical) {
2042         m_sliderThumbVertical = adoptNS([[NSSliderCell alloc] init]);
2043         [m_sliderThumbVertical.get() setSliderType:NSSliderTypeLinear];
2044         [m_sliderThumbVertical.get() setControlSize:NSControlSizeSmall];
2045         [m_sliderThumbVertical.get() setFocusRingType:NSFocusRingTypeExterior];
2046     }
2047
2048     return m_sliderThumbVertical.get();
2049 }
2050
2051 NSTextFieldCell* RenderThemeMac::textField() const
2052 {
2053     if (!m_textField) {
2054         m_textField = adoptNS([[WebCoreTextFieldCell alloc] initTextCell:@""]);
2055         [m_textField.get() setBezeled:YES];
2056         [m_textField.get() setEditable:YES];
2057         [m_textField.get() setFocusRingType:NSFocusRingTypeExterior];
2058         // Post-Lion, WebCore can be in charge of paintinng the background thanks to
2059         // the workaround in place for <rdar://problem/11385461>, which is implemented
2060         // above as _coreUIDrawOptionsWithFrame.
2061         [m_textField.get() setDrawsBackground:NO];
2062     }
2063
2064     return m_textField.get();
2065 }
2066
2067 String RenderThemeMac::fileListNameForWidth(const FileList* fileList, const FontCascade& font, int width, bool multipleFilesAllowed) const
2068 {
2069     if (width <= 0)
2070         return String();
2071
2072     String strToTruncate;
2073     if (fileList->isEmpty())
2074         strToTruncate = fileListDefaultLabel(multipleFilesAllowed);
2075     else if (fileList->length() == 1)
2076         strToTruncate = [[NSFileManager defaultManager] displayNameAtPath:(fileList->item(0)->path())];
2077     else
2078         return StringTruncator::rightTruncate(multipleFileUploadText(fileList->length()), width, font);
2079
2080     return StringTruncator::centerTruncate(strToTruncate, width, font);
2081 }
2082
2083 #if ENABLE(SERVICE_CONTROLS)
2084 NSServicesRolloverButtonCell* RenderThemeMac::servicesRolloverButtonCell() const
2085 {
2086 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2087     if (!m_servicesRolloverButton) {
2088         m_servicesRolloverButton = [NSServicesRolloverButtonCell serviceRolloverButtonCellForStyle:NSSharingServicePickerStyleRollover];
2089         [m_servicesRolloverButton setBezelStyle:NSRoundedDisclosureBezelStyle];
2090         [m_servicesRolloverButton setButtonType:NSPushOnPushOffButton];
2091         [m_servicesRolloverButton setImagePosition:NSImageOnly];
2092         [m_servicesRolloverButton setState:NO];
2093     }
2094
2095     return m_servicesRolloverButton.get();
2096 #else
2097     return nil;
2098 #endif
2099 }
2100
2101 bool RenderThemeMac::paintImageControlsButton(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect& rect)
2102 {
2103     if (paintInfo.phase != PaintPhaseBlockBackground)
2104         return true;
2105
2106 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2107     NSServicesRolloverButtonCell *cell = servicesRolloverButtonCell();
2108
2109     LocalCurrentGraphicsContext localContext(paintInfo.context());
2110     GraphicsContextStateSaver stateSaver(paintInfo.context());
2111
2112     paintInfo.context().translate(rect.location());
2113
2114     IntRect innerFrame(IntPoint(), rect.size());
2115     [cell drawWithFrame:innerFrame inView:documentViewFor(renderer)];
2116     [cell setControlView:nil];
2117 #else
2118     UNUSED_PARAM(renderer);
2119     UNUSED_PARAM(rect);
2120 #endif
2121
2122     return true;
2123 }
2124
2125 IntSize RenderThemeMac::imageControlsButtonSize(const RenderObject&) const
2126 {
2127 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2128     return IntSize(servicesRolloverButtonCell().cellSize);
2129 #else
2130     return IntSize();
2131 #endif
2132 }
2133
2134 IntSize RenderThemeMac::imageControlsButtonPositionOffset() const
2135 {
2136 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2137     // FIXME: Currently the offsets will always be the same no matter what image rect you try with.
2138     // This may not always be true in the future.
2139     static const int dummyDimension = 100;
2140     IntRect dummyImageRect(0, 0, dummyDimension, dummyDimension);
2141     NSRect bounds = [servicesRolloverButtonCell() rectForBounds:dummyImageRect preferredEdge:NSMinYEdge];
2142
2143     return IntSize(dummyDimension - bounds.origin.x, bounds.origin.y);
2144 #else
2145     return IntSize();
2146 #endif
2147 }
2148 #endif
2149
2150 #if ENABLE(ATTACHMENT_ELEMENT)
2151 const CGFloat attachmentIconSize = 48;
2152 const CGFloat attachmentIconBackgroundPadding = 6;
2153 const CGFloat attachmentIconBackgroundSize = attachmentIconSize + attachmentIconBackgroundPadding;
2154 const CGFloat attachmentIconSelectionBorderThickness = 1;
2155 const CGFloat attachmentIconBackgroundRadius = 3;
2156 const CGFloat attachmentIconToTitleMargin = 2;
2157
2158 static Color attachmentIconBackgroundColor() { return Color(0, 0, 0, 30); }
2159 static Color attachmentIconBorderColor() { return Color(255, 255, 255, 125); }
2160
2161 const CGFloat attachmentTitleFontSize = 12;
2162 const CGFloat attachmentTitleBackgroundRadius = 3;
2163 const CGFloat attachmentTitleBackgroundPadding = 3;
2164 const CGFloat attachmentTitleMaximumWidth = 100 - (attachmentTitleBackgroundPadding * 2);
2165 const CFIndex attachmentTitleMaximumLineCount = 2;
2166
2167 static Color attachmentTitleInactiveBackgroundColor() { return Color(204, 204, 204, 255); }
2168 static Color attachmentTitleInactiveTextColor() { return Color(100, 100, 100, 255); }
2169
2170 const CGFloat attachmentSubtitleFontSize = 10;
2171 const int attachmentSubtitleWidthIncrement = 10;
2172 static Color attachmentSubtitleTextColor() { return Color(82, 145, 214, 255); }
2173
2174 const CGFloat attachmentProgressBarWidth = 30;
2175 const CGFloat attachmentProgressBarHeight = 5;
2176 const CGFloat attachmentProgressBarOffset = -9;
2177 const CGFloat attachmentProgressBarBorderWidth = 1;
2178 static Color attachmentProgressBarBackgroundColor() { return Color(0, 0, 0, 89); }
2179 static Color attachmentProgressBarFillColor() { return Color(Color::white); }
2180 static Color attachmentProgressBarBorderColor() { return Color(0, 0, 0, 128); }
2181
2182 const CGFloat attachmentPlaceholderBorderRadius = 5;
2183 static Color attachmentPlaceholderBorderColor() { return Color(0, 0, 0, 56); }
2184 const CGFloat attachmentPlaceholderBorderWidth = 2;
2185 const CGFloat attachmentPlaceholderBorderDashLength = 6;
2186
2187 const CGFloat attachmentMargin = 3;
2188
2189 struct AttachmentLayout {
2190     explicit AttachmentLayout(const RenderAttachment&);
2191
2192     struct LabelLine {
2193         FloatRect backgroundRect;
2194         FloatPoint origin;
2195         RetainPtr<CTLineRef> line;
2196     };
2197
2198     Vector<LabelLine> lines;
2199
2200     FloatRect iconRect;
2201     FloatRect iconBackgroundRect;
2202     FloatRect attachmentRect;
2203
2204     int baseline;
2205
2206     RetainPtr<CTLineRef> subtitleLine;
2207     FloatRect subtitleTextRect;
2208
2209 private:
2210     void layOutTitle(const RenderAttachment&);
2211     void layOutSubtitle(const RenderAttachment&);
2212
2213     void addTitleLine(CTLineRef, CGFloat& yOffset, Vector<CGPoint> origins, CFIndex lineIndex, const RenderAttachment&);
2214 };
2215
2216 static NSColor *titleTextColorForAttachment(const RenderAttachment& attachment)
2217 {
2218     if (attachment.selectionState() != RenderObject::SelectionNone) {
2219         if (attachment.frame().selection().isFocusedAndActive())
2220             return [NSColor alternateSelectedControlTextColor];    
2221         return (NSColor *)cachedCGColor(attachmentTitleInactiveTextColor());
2222     }
2223
2224     return [NSColor blackColor];
2225 }
2226
2227 void AttachmentLayout::addTitleLine(CTLineRef line, CGFloat& yOffset, Vector<CGPoint> origins, CFIndex lineIndex, const RenderAttachment& attachment)
2228 {
2229     CGRect lineBounds = CTLineGetBoundsWithOptions(line, 0);
2230     CGFloat trailingWhitespaceWidth = CTLineGetTrailingWhitespaceWidth(line);
2231     CGFloat lineWidthIgnoringTrailingWhitespace = lineBounds.size.width - trailingWhitespaceWidth;
2232     CGFloat lineHeight = CGCeiling(lineBounds.size.height);
2233
2234     // Center the line relative to the icon.
2235     CGFloat xOffset = (attachmentIconBackgroundSize / 2) - (lineWidthIgnoringTrailingWhitespace / 2);
2236
2237     if (lineIndex)
2238         yOffset += origins[lineIndex - 1].y - origins[lineIndex].y;
2239
2240     LabelLine labelLine;
2241     labelLine.origin = FloatPoint(xOffset, yOffset + lineHeight - origins.last().y);
2242     labelLine.line = line;
2243     labelLine.backgroundRect = FloatRect(xOffset, yOffset, lineWidthIgnoringTrailingWhitespace, lineHeight);
2244     labelLine.backgroundRect.inflateX(attachmentTitleBackgroundPadding);
2245     labelLine.backgroundRect = encloseRectToDevicePixels(labelLine.backgroundRect, attachment.document().deviceScaleFactor());
2246
2247     // If the text rects are close in size, the curved enclosing background won't
2248     // look right, so make them the same exact size.
2249     if (!lines.isEmpty()) {
2250         float previousBackgroundRectWidth = lines.last().backgroundRect.width();
2251         if (fabs(labelLine.backgroundRect.width() - previousBackgroundRectWidth) < attachmentTitleBackgroundRadius * 4) {
2252             float newBackgroundRectWidth = std::max(previousBackgroundRectWidth, labelLine.backgroundRect.width());
2253             labelLine.backgroundRect.inflateX((newBackgroundRectWidth - labelLine.backgroundRect.width()) / 2);
2254             lines.last().backgroundRect.inflateX((newBackgroundRectWidth - previousBackgroundRectWidth) / 2);
2255         }
2256     }
2257
2258     lines.append(labelLine);
2259 }
2260
2261 void AttachmentLayout::layOutTitle(const RenderAttachment& attachment)
2262 {
2263     CFStringRef language = 0; // By not specifying a language we use the system language.
2264     RetainPtr<CTFontRef> font = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, attachmentTitleFontSize, language));
2265     baseline = CGRound(attachmentIconBackgroundSize + attachmentIconToTitleMargin + CTFontGetAscent(font.get()));
2266
2267     String title = attachment.attachmentElement().attachmentTitle();
2268     if (title.isEmpty())
2269         return;
2270
2271     NSDictionary *textAttributes = @{
2272         (id)kCTFontAttributeName: (id)font.get(),
2273         (id)kCTForegroundColorAttributeName: titleTextColorForAttachment(attachment)
2274     };
2275     RetainPtr<NSAttributedString> attributedTitle = adoptNS([[NSAttributedString alloc] initWithString:title attributes:textAttributes]);
2276     RetainPtr<CTFramesetterRef> titleFramesetter = adoptCF(CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedTitle.get()));
2277
2278     CFRange fitRange;
2279     CGSize titleTextSize = CTFramesetterSuggestFrameSizeWithConstraints(titleFramesetter.get(), CFRangeMake(0, 0), nullptr, CGSizeMake(attachmentTitleMaximumWidth, CGFLOAT_MAX), &fitRange);
2280
2281     RetainPtr<CGPathRef> titlePath = adoptCF(CGPathCreateWithRect(CGRectMake(0, 0, titleTextSize.width, titleTextSize.height), nullptr));
2282     RetainPtr<CTFrameRef> titleFrame = adoptCF(CTFramesetterCreateFrame(titleFramesetter.get(), fitRange, titlePath.get(), nullptr));
2283
2284     CFArrayRef ctLines = CTFrameGetLines(titleFrame.get());
2285     CFIndex lineCount = CFArrayGetCount(ctLines);
2286     if (!lineCount)
2287         return;
2288
2289     Vector<CGPoint> origins(lineCount);
2290     CTFrameGetLineOrigins(titleFrame.get(), CFRangeMake(0, 0), origins.data());
2291
2292     // Lay out and record the first (attachmentTitleMaximumLineCount - 1) lines.
2293     CFIndex lineIndex = 0;
2294     CGFloat yOffset = attachmentIconBackgroundSize + attachmentIconToTitleMargin;
2295     for (; lineIndex < std::min(attachmentTitleMaximumLineCount - 1, lineCount); ++lineIndex) {
2296         CTLineRef line = (CTLineRef)CFArrayGetValueAtIndex(ctLines, lineIndex);
2297         addTitleLine(line, yOffset, origins, lineIndex, attachment);
2298     }
2299
2300     if (lineIndex == lineCount)
2301         return;
2302
2303     // We had text that didn't fit in the first (attachmentTitleMaximumLineCount - 1) lines.
2304     // Combine it into one last line, and center-truncate it.
2305     CTLineRef firstRemainingLine = (CTLineRef)CFArrayGetValueAtIndex(ctLines, lineIndex);
2306     CFIndex remainingRangeStart = CTLineGetStringRange(firstRemainingLine).location;
2307     NSRange remainingRange = NSMakeRange(remainingRangeStart, [attributedTitle length] - remainingRangeStart);
2308     NSAttributedString *remainingString = [attributedTitle attributedSubstringFromRange:remainingRange];
2309     RetainPtr<CTLineRef> remainingLine = adoptCF(CTLineCreateWithAttributedString((CFAttributedStringRef)remainingString));
2310     RetainPtr<NSAttributedString> ellipsisString = adoptNS([[NSAttributedString alloc] initWithString:@"\u2026" attributes:textAttributes]);
2311     RetainPtr<CTLineRef> ellipsisLine = adoptCF(CTLineCreateWithAttributedString((CFAttributedStringRef)ellipsisString.get()));
2312     RetainPtr<CTLineRef> truncatedLine = adoptCF(CTLineCreateTruncatedLine(remainingLine.get(), attachmentTitleMaximumWidth, kCTLineTruncationMiddle, ellipsisLine.get()));
2313
2314     if (!truncatedLine)
2315         truncatedLine = remainingLine;
2316
2317     addTitleLine(truncatedLine.get(), yOffset, origins, lineIndex, attachment);
2318 }
2319
2320 void AttachmentLayout::layOutSubtitle(const RenderAttachment& attachment)
2321 {
2322     auto& subtitleText = attachment.attachmentElement().attributeWithoutSynchronization(subtitleAttr);
2323     if (subtitleText.isEmpty())
2324         return;
2325
2326     CFStringRef language = 0; // By not specifying a language we use the system language.
2327     RetainPtr<CTFontRef> font = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, attachmentSubtitleFontSize, language));
2328     NSDictionary *textAttributes = @{
2329         (id)kCTFontAttributeName: (id)font.get(),
2330         (id)kCTForegroundColorAttributeName: (NSColor *)cachedCGColor(attachmentSubtitleTextColor())
2331     };
2332     RetainPtr<NSAttributedString> attributedSubtitleText = adoptNS([[NSAttributedString alloc] initWithString:subtitleText attributes:textAttributes]);
2333     subtitleLine = adoptCF(CTLineCreateWithAttributedString((CFAttributedStringRef)attributedSubtitleText.get()));
2334
2335     CGRect lineBounds = CTLineGetBoundsWithOptions(subtitleLine.get(), 0);
2336
2337     // Center the line relative to the icon.
2338     CGFloat xOffset = (attachmentIconBackgroundSize / 2) - (lineBounds.size.width / 2);
2339     CGFloat yOffset = 0;
2340
2341     if (!lines.isEmpty())
2342         yOffset = lines.last().backgroundRect.maxY();
2343     else
2344         yOffset = attachmentIconBackgroundSize + attachmentIconToTitleMargin;
2345
2346     LabelLine labelLine;
2347     subtitleTextRect = FloatRect(xOffset, yOffset, lineBounds.size.width, lineBounds.size.height);
2348 }
2349
2350 AttachmentLayout::AttachmentLayout(const RenderAttachment& attachment)
2351 {
2352     layOutTitle(attachment);
2353     layOutSubtitle(attachment);
2354
2355     iconBackgroundRect = FloatRect(0, 0, attachmentIconBackgroundSize, attachmentIconBackgroundSize);
2356
2357     iconRect = iconBackgroundRect;
2358     iconRect.setSize(FloatSize(attachmentIconSize, attachmentIconSize));
2359     iconRect.move(attachmentIconBackgroundPadding / 2, attachmentIconBackgroundPadding / 2);
2360
2361     attachmentRect = iconBackgroundRect;
2362     for (const auto& line : lines)
2363         attachmentRect.unite(line.backgroundRect);
2364     if (!subtitleTextRect.isEmpty()) {
2365         FloatRect roundedSubtitleTextRect = subtitleTextRect;
2366         roundedSubtitleTextRect.inflateX(attachmentSubtitleWidthIncrement - clampToInteger(ceilf(subtitleTextRect.width())) % attachmentSubtitleWidthIncrement);
2367         attachmentRect.unite(roundedSubtitleTextRect);
2368     }
2369     attachmentRect.inflate(attachmentMargin);
2370     attachmentRect = encloseRectToDevicePixels(attachmentRect, attachment.document().deviceScaleFactor());
2371 }
2372
2373 LayoutSize RenderThemeMac::attachmentIntrinsicSize(const RenderAttachment& attachment) const
2374 {
2375     AttachmentLayout layout(attachment);
2376     return LayoutSize(layout.attachmentRect.size());
2377 }
2378
2379 int RenderThemeMac::attachmentBaseline(const RenderAttachment& attachment) const
2380 {
2381     AttachmentLayout layout(attachment);
2382     return layout.baseline;
2383 }
2384
2385 static void paintAttachmentIconBackground(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout)
2386 {
2387     // FIXME: Finder has a discontinuous behavior here when you have a background color other than white,
2388     // where it switches into 'bordered mode' and the border pops in on top of the background.
2389     bool paintBorder = true;
2390
2391     FloatRect backgroundRect = layout.iconBackgroundRect;
2392     if (paintBorder)
2393         backgroundRect.inflate(-attachmentIconSelectionBorderThickness);
2394
2395     context.fillRoundedRect(FloatRoundedRect(backgroundRect, FloatRoundedRect::Radii(attachmentIconBackgroundRadius)), attachmentIconBackgroundColor());
2396
2397     if (paintBorder) {
2398         FloatRect borderRect = layout.iconBackgroundRect;
2399         borderRect.inflate(-attachmentIconSelectionBorderThickness / 2);
2400
2401         FloatSize iconBackgroundRadiusSize(attachmentIconBackgroundRadius, attachmentIconBackgroundRadius);
2402         Path borderPath;
2403         borderPath.addRoundedRect(borderRect, iconBackgroundRadiusSize);
2404         context.setStrokeColor(attachmentIconBorderColor());
2405         context.setStrokeThickness(attachmentIconSelectionBorderThickness);
2406         context.strokePath(borderPath);
2407     }
2408 }
2409
2410 static RefPtr<Icon> iconForAttachment(const RenderAttachment& attachment)
2411 {
2412     String attachmentType = attachment.attachmentElement().attachmentType();
2413     
2414     if (!attachmentType.isEmpty()) {
2415         if (equalIgnoringASCIICase(attachmentType, "multipart/x-folder") || equalIgnoringASCIICase(attachmentType, "application/vnd.apple.folder")) {
2416             if (auto icon = Icon::createIconForUTI("public.directory"))
2417                 return icon;
2418         } else {
2419             String UTI;
2420             if (isDeclaredUTI(attachmentType))
2421                 UTI = attachmentType;
2422             else
2423                 UTI = UTIFromMIMEType(attachmentType);
2424
2425             if (auto icon = Icon::createIconForUTI(UTI))
2426                 return icon;
2427         }
2428     }
2429
2430     if (File* file = attachment.attachmentElement().file()) {
2431         if (auto icon = Icon::createIconForFiles({ file->path() }))
2432             return icon;
2433     }
2434
2435     NSString *fileExtension = [static_cast<NSString *>(attachment.attachmentElement().attachmentTitle()) pathExtension];
2436     if (fileExtension.length) {
2437         if (auto icon = Icon::createIconForFileExtension(fileExtension))
2438             return icon;
2439     }
2440
2441     return Icon::createIconForUTI("public.data");
2442 }
2443
2444 static void paintAttachmentIcon(const RenderAttachment& attachment, GraphicsContext& context, AttachmentLayout& layout)
2445 {
2446     auto icon = iconForAttachment(attachment);
2447     if (!icon)
2448         return;
2449     icon->paint(context, layout.iconRect);
2450 }
2451
2452 static void paintAttachmentIconPlaceholder(const RenderAttachment& attachment, GraphicsContext& context, AttachmentLayout& layout)
2453 {
2454     RefPtr<Image> placeholderImage;
2455     float imageScale = 1;
2456     if (attachment.document().deviceScaleFactor() >= 2) {
2457         placeholderImage = Image::loadPlatformResource("AttachmentPlaceholder@2x");
2458         imageScale = 2;
2459     } else
2460         placeholderImage = Image::loadPlatformResource("AttachmentPlaceholder");
2461
2462     // Center the placeholder image where the icon would usually be.
2463     FloatRect placeholderRect(0, 0, placeholderImage->width() / imageScale, placeholderImage->height() / imageScale);
2464     placeholderRect.setX(layout.iconRect.x() + (layout.iconRect.width() - placeholderRect.width()) / 2);
2465     placeholderRect.setY(layout.iconRect.y() + (layout.iconRect.height() - placeholderRect.height()) / 2);
2466
2467     context.drawImage(*placeholderImage, placeholderRect);
2468 }
2469
2470 static void paintAttachmentTitleBackground(const RenderAttachment& attachment, GraphicsContext& context, AttachmentLayout& layout)
2471 {
2472     if (layout.lines.isEmpty())
2473         return;
2474
2475     Vector<FloatRect> backgroundRects;
2476
2477     for (size_t i = 0; i < layout.lines.size(); ++i)
2478         backgroundRects.append(layout.lines[i].backgroundRect);
2479
2480     Color backgroundColor;
2481     if (attachment.frame().selection().isFocusedAndActive())
2482         backgroundColor = convertNSColorToColor([NSColor alternateSelectedControlColor]);
2483     else
2484         backgroundColor = attachmentTitleInactiveBackgroundColor();
2485
2486     context.setFillColor(backgroundColor);
2487
2488     Path backgroundPath = PathUtilities::pathWithShrinkWrappedRects(backgroundRects, attachmentTitleBackgroundRadius);
2489     context.fillPath(backgroundPath);
2490 }
2491
2492 static void paintAttachmentTitle(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout)
2493 {
2494     for (const auto& line : layout.lines) {
2495         GraphicsContextStateSaver saver(context);
2496
2497         context.translate(toFloatSize(line.origin));
2498         context.scale(FloatSize(1, -1));
2499
2500         CGContextSetTextPosition(context.platformContext(), 0, 0);
2501         CTLineDraw(line.line.get(), context.platformContext());
2502     }
2503 }
2504
2505 static void paintAttachmentSubtitle(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout)
2506 {
2507     GraphicsContextStateSaver saver(context);
2508
2509     context.translate(toFloatSize(layout.subtitleTextRect.minXMaxYCorner()));
2510     context.scale(FloatSize(1, -1));
2511
2512     CGContextSetTextPosition(context.platformContext(), 0, 0);
2513     CTLineDraw(layout.subtitleLine.get(), context.platformContext());
2514 }
2515
2516 static void paintAttachmentProgress(const RenderAttachment& attachment, GraphicsContext& context, AttachmentLayout& layout, float progress)
2517 {
2518     GraphicsContextStateSaver saver(context);
2519
2520     FloatRect progressBounds((attachmentIconBackgroundSize - attachmentProgressBarWidth) / 2, layout.iconBackgroundRect.maxY() + attachmentProgressBarOffset - attachmentProgressBarHeight, attachmentProgressBarWidth, attachmentProgressBarHeight);
2521
2522     FloatRect borderRect = progressBounds;
2523     borderRect.inflate(-0.5);
2524     FloatRect backgroundRect = borderRect;
2525     backgroundRect.inflate(-attachmentProgressBarBorderWidth / 2);
2526
2527     FloatRoundedRect backgroundRoundedRect(backgroundRect, FloatRoundedRect::Radii(backgroundRect.height() / 2));
2528     context.fillRoundedRect(backgroundRoundedRect, attachmentProgressBarBackgroundColor());
2529
2530     {
2531         GraphicsContextStateSaver clipSaver(context);
2532         context.clipRoundedRect(backgroundRoundedRect);
2533
2534         FloatRect progressRect = progressBounds;
2535         progressRect.setWidth(progressRect.width() * progress);
2536         progressRect = encloseRectToDevicePixels(progressRect, attachment.document().deviceScaleFactor());
2537
2538         context.fillRect(progressRect, attachmentProgressBarFillColor());
2539     }
2540
2541     Path borderPath;
2542     float borderRadius = borderRect.height() / 2;
2543     borderPath.addRoundedRect(borderRect, FloatSize(borderRadius, borderRadius));
2544     context.setStrokeColor(attachmentProgressBarBorderColor());
2545     context.setStrokeThickness(attachmentProgressBarBorderWidth);
2546     context.strokePath(borderPath);
2547 }
2548
2549 static void paintAttachmentPlaceholderBorder(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout)
2550 {
2551     Path borderPath;
2552     borderPath.addRoundedRect(layout.attachmentRect, FloatSize(attachmentPlaceholderBorderRadius, attachmentPlaceholderBorderRadius));
2553     context.setStrokeColor(attachmentPlaceholderBorderColor());
2554     context.setStrokeThickness(attachmentPlaceholderBorderWidth);
2555     context.setStrokeStyle(DashedStroke);
2556     context.setLineDash({attachmentPlaceholderBorderDashLength}, 0);
2557     context.strokePath(borderPath);
2558 }
2559
2560 bool RenderThemeMac::paintAttachment(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect& paintRect)
2561 {
2562     if (!is<RenderAttachment>(renderer))
2563         return false;
2564
2565     const RenderAttachment& attachment = downcast<RenderAttachment>(renderer);
2566
2567     AttachmentLayout layout(attachment);
2568
2569     auto& progressString = attachment.attachmentElement().attributeWithoutSynchronization(progressAttr);
2570     bool validProgress = false;
2571     float progress = 0;
2572     if (!progressString.isEmpty())
2573         progress = progressString.toFloat(&validProgress);
2574
2575     GraphicsContext& context = paintInfo.context();
2576     LocalCurrentGraphicsContext localContext(context);
2577     GraphicsContextStateSaver saver(context);
2578
2579     context.translate(toFloatSize(paintRect.location()));
2580     context.translate(floorSizeToDevicePixels(LayoutSize((paintRect.width() - attachmentIconBackgroundSize) / 2, 0), renderer.document().deviceScaleFactor()));
2581
2582     bool useSelectedStyle = attachment.selectionState() != RenderObject::SelectionNone;
2583     bool usePlaceholder = validProgress && !progress;
2584
2585     if (useSelectedStyle)
2586         paintAttachmentIconBackground(attachment, context, layout);
2587     if (usePlaceholder)
2588         paintAttachmentIconPlaceholder(attachment, context, layout);
2589     else
2590         paintAttachmentIcon(attachment, context, layout);
2591
2592     if (useSelectedStyle)
2593         paintAttachmentTitleBackground(attachment, context, layout);
2594     paintAttachmentTitle(attachment, context, layout);
2595     paintAttachmentSubtitle(attachment, context, layout);
2596
2597     if (validProgress && progress)
2598         paintAttachmentProgress(attachment, context, layout, progress);
2599
2600     if (usePlaceholder)
2601         paintAttachmentPlaceholderBorder(attachment, context, layout);
2602
2603     return true;
2604 }
2605
2606 #endif // ENABLE(ATTACHMENT_ELEMENT)
2607
2608 } // namespace WebCore
2609
2610 #endif // PLATFORM(MAC)