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