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