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