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