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