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