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