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