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