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