Subpixel rendering[iOS]: <select> decoration is misaligned when the renderer is on...
[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 "Document.h"
29 #import "Element.h"
30 #import "ExceptionCodePlaceholder.h"
31 #import "FileList.h"
32 #import "FloatRoundedRect.h"
33 #import "FocusController.h"
34 #import "Frame.h"
35 #import "FrameView.h"
36 #import "GraphicsContextCG.h"
37 #import "HTMLAudioElement.h"
38 #import "HTMLInputElement.h"
39 #import "HTMLMediaElement.h"
40 #import "HTMLNames.h"
41 #import "HTMLPlugInImageElement.h"
42 #import "Image.h"
43 #import "ImageBuffer.h"
44 #import "LocalCurrentGraphicsContext.h"
45 #import "LocalizedStrings.h"
46 #import "MediaControlElements.h"
47 #import "Page.h"
48 #import "PaintInfo.h"
49 #import "RenderLayer.h"
50 #import "RenderMedia.h"
51 #import "RenderMediaControlElements.h"
52 #import "RenderMediaControls.h"
53 #import "RenderProgress.h"
54 #import "RenderSlider.h"
55 #import "RenderSnapshottedPlugIn.h"
56 #import "RenderView.h"
57 #import "SharedBuffer.h"
58 #import "StringTruncator.h"
59 #import "StyleResolver.h"
60 #import "ThemeMac.h"
61 #import "TimeRanges.h"
62 #import "UserAgentScripts.h"
63 #import "UserAgentStyleSheets.h"
64 #import "WebCoreSystemInterface.h"
65 #import <wtf/RetainPtr.h>
66 #import <wtf/RetainPtr.h>
67 #import <wtf/StdLibExtras.h>
68 #import <wtf/text/StringBuilder.h>
69 #import <Carbon/Carbon.h>
70 #import <Cocoa/Cocoa.h>
71 #import <math.h>
72
73 #if ENABLE(METER_ELEMENT)
74 #import "RenderMeter.h"
75 #import "HTMLMeterElement.h"
76 #endif
77
78 #if defined(__LP64__) && __LP64__
79 #define HAVE_APPKIT_SERVICE_CONTROLS_SUPPORT 1
80 #else
81 #define HAVE_APPKIT_SERVICE_CONTROLS_SUPPORT 0
82 #endif
83
84 #if ENABLE(SERVICE_CONTROLS) && HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
85
86 #if __has_include(<AppKit/AppKitDefines_Private.h>)
87 #import <AppKit/AppKitDefines_Private.h>
88 #else
89 #define APPKIT_PRIVATE_CLASS
90 #endif
91
92 #if __has_include(<AppKit/NSServicesRolloverButtonCell.h>)
93 #import <AppKit/NSServicesRolloverButtonCell.h>
94 #endif
95
96 @interface NSServicesRolloverButtonCell (Details)
97 + (NSServicesRolloverButtonCell *)serviceRolloverButtonCellForStyle:(NSSharingServicePickerStyle)style;
98 @end
99
100 #if __has_include(<AppKit/NSSharingService_Private.h>)
101 #import <AppKit/NSSharingService_Private.h>
102 #else
103 typedef enum {
104     NSSharingServicePickerStyleRollover = 1
105 } NSSharingServicePickerStyle;
106 #endif
107
108 #endif // ENABLE(SERVICE_CONTROLS)
109
110 // The methods in this file are specific to the Mac OS X platform.
111
112 // FIXME: The platform-independent code in this class should be factored out and merged with RenderThemeSafari.
113
114 // We estimate the animation rate of a Mac OS X progress bar is 33 fps.
115 // Hard code the value here because we haven't found API for it.
116 const double progressAnimationFrameRate = 0.033;
117
118 // Mac OS X progress bar animation seems to have 256 frames.
119 const double progressAnimationNumFrames = 256;
120
121 @interface WebCoreRenderThemeNotificationObserver : NSObject
122 {
123     WebCore::RenderTheme *_theme;
124 }
125
126 - (id)initWithTheme:(WebCore::RenderTheme *)theme;
127 - (void)systemColorsDidChange:(NSNotification *)notification;
128
129 @end
130
131 @implementation WebCoreRenderThemeNotificationObserver
132
133 - (id)initWithTheme:(WebCore::RenderTheme *)theme
134 {
135     if (!(self = [super init]))
136         return nil;
137
138     _theme = theme;
139     return self;
140 }
141
142 - (void)systemColorsDidChange:(NSNotification *)unusedNotification
143 {
144     ASSERT_UNUSED(unusedNotification, [[unusedNotification name] isEqualToString:NSSystemColorsDidChangeNotification]);
145     _theme->platformColorsDidChange();
146 }
147
148 @end
149
150 @interface NSTextFieldCell (WKDetails)
151 - (CFDictionaryRef)_coreUIDrawOptionsWithFrame:(NSRect)cellFrame inView:(NSView *)controlView includeFocus:(BOOL)includeFocus;
152 @end
153
154
155 @interface WebCoreTextFieldCell : NSTextFieldCell
156 - (CFDictionaryRef)_coreUIDrawOptionsWithFrame:(NSRect)cellFrame inView:(NSView *)controlView includeFocus:(BOOL)includeFocus;
157 @end
158
159 @implementation WebCoreTextFieldCell
160 - (CFDictionaryRef)_coreUIDrawOptionsWithFrame:(NSRect)cellFrame inView:(NSView *)controlView includeFocus:(BOOL)includeFocus
161 {
162     // FIXME: This is a post-Lion-only workaround for <rdar://problem/11385461>. When that bug is resolved, we should remove this code.
163     CFMutableDictionaryRef coreUIDrawOptions = CFDictionaryCreateMutableCopy(NULL, 0, [super _coreUIDrawOptionsWithFrame:cellFrame inView:controlView includeFocus:includeFocus]);
164     CFDictionarySetValue(coreUIDrawOptions, @"borders only", kCFBooleanTrue);
165     return (CFDictionaryRef)[NSMakeCollectable(coreUIDrawOptions) autorelease];
166 }
167 @end
168
169 @interface WebCoreRenderThemeBundle : NSObject
170 @end
171
172 @implementation WebCoreRenderThemeBundle
173 @end
174
175 namespace WebCore {
176
177 using namespace HTMLNames;
178
179 enum {
180     topMargin,
181     rightMargin,
182     bottomMargin,
183     leftMargin
184 };
185
186 enum {
187     topPadding,
188     rightPadding,
189     bottomPadding,
190     leftPadding
191 };
192
193 PassRefPtr<RenderTheme> RenderTheme::themeForPage(Page*)
194 {
195     static RenderTheme* rt = RenderThemeMac::create().leakRef();
196     return rt;
197 }
198
199 PassRefPtr<RenderTheme> RenderThemeMac::create()
200 {
201     return adoptRef(new RenderThemeMac);
202 }
203
204 RenderThemeMac::RenderThemeMac()
205     : m_isSliderThumbHorizontalPressed(false)
206     , m_isSliderThumbVerticalPressed(false)
207     , m_notificationObserver(adoptNS([[WebCoreRenderThemeNotificationObserver alloc] initWithTheme:this]))
208 {
209     [[NSNotificationCenter defaultCenter] addObserver:m_notificationObserver.get()
210                                                         selector:@selector(systemColorsDidChange:)
211                                                             name:NSSystemColorsDidChangeNotification
212                                                           object:nil];
213 }
214
215 RenderThemeMac::~RenderThemeMac()
216 {
217     [[NSNotificationCenter defaultCenter] removeObserver:m_notificationObserver.get()];
218 }
219
220 NSView* RenderThemeMac::documentViewFor(const RenderObject& o) const
221 {
222     ControlStates states(extractControlStatesForRenderer(o));
223     return ThemeMac::ensuredView(&o.view().frameView(), &states);
224 }
225
226 #if ENABLE(VIDEO)
227 String RenderThemeMac::mediaControlsStyleSheet()
228 {
229 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
230     if (m_mediaControlsStyleSheet.isEmpty())
231         m_mediaControlsStyleSheet = [NSString stringWithContentsOfFile:[[NSBundle bundleForClass:[WebCoreRenderThemeBundle class]] pathForResource:@"mediaControlsApple" ofType:@"css"] encoding:NSUTF8StringEncoding error:nil];
232     return m_mediaControlsStyleSheet;
233 #else
234     return emptyString();
235 #endif
236 }
237
238 String RenderThemeMac::mediaControlsScript()
239 {
240 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
241     if (m_mediaControlsScript.isEmpty()) {
242         StringBuilder scriptBuilder;
243         scriptBuilder.append([NSString stringWithContentsOfFile:[[NSBundle bundleForClass:[WebCoreRenderThemeBundle class]] pathForResource:@"mediaControlsLocalizedStrings" ofType:@"js"] encoding:NSUTF8StringEncoding error:nil]);
244         scriptBuilder.append([NSString stringWithContentsOfFile:[[NSBundle bundleForClass:[WebCoreRenderThemeBundle class]] pathForResource:@"mediaControlsApple" ofType:@"js"] encoding:NSUTF8StringEncoding error:nil]);
245         m_mediaControlsScript = scriptBuilder.toString();
246     }
247     return m_mediaControlsScript;
248 #else
249     return emptyString();
250 #endif
251 }
252
253 #endif // ENABLE(VIDEO)
254
255
256 #if ENABLE(SERVICE_CONTROLS)
257 String RenderThemeMac::imageControlsStyleSheet() const
258 {
259     return String(imageControlsMacUserAgentStyleSheet, sizeof(imageControlsMacUserAgentStyleSheet));
260 }
261 #endif
262
263 Color RenderThemeMac::platformActiveSelectionBackgroundColor() const
264 {
265     NSColor* color = [[NSColor selectedTextBackgroundColor] colorUsingColorSpaceName:NSDeviceRGBColorSpace];
266     return Color(static_cast<int>(255.0 * [color redComponent]), static_cast<int>(255.0 * [color greenComponent]), static_cast<int>(255.0 * [color blueComponent]));
267 }
268
269 Color RenderThemeMac::platformInactiveSelectionBackgroundColor() const
270 {
271     NSColor* color = [[NSColor secondarySelectedControlColor] colorUsingColorSpaceName:NSDeviceRGBColorSpace];
272     return Color(static_cast<int>(255.0 * [color redComponent]), static_cast<int>(255.0 * [color greenComponent]), static_cast<int>(255.0 * [color blueComponent]));
273 }
274
275 Color RenderThemeMac::platformActiveListBoxSelectionBackgroundColor() const
276 {
277     NSColor* color = [[NSColor alternateSelectedControlColor] colorUsingColorSpaceName:NSDeviceRGBColorSpace];
278     return Color(static_cast<int>(255.0 * [color redComponent]), static_cast<int>(255.0 * [color greenComponent]), static_cast<int>(255.0 * [color blueComponent]));
279 }
280
281 Color RenderThemeMac::platformActiveListBoxSelectionForegroundColor() const
282 {
283     return Color::white;
284 }
285
286 Color RenderThemeMac::platformInactiveListBoxSelectionForegroundColor() const
287 {
288     return Color::black;
289 }
290
291 Color RenderThemeMac::platformFocusRingColor() const
292 {
293     if (usesTestModeFocusRingColor())
294         return oldAquaFocusRingColor();
295
296     return systemColor(CSSValueWebkitFocusRingColor);
297 }
298
299 int RenderThemeMac::platformFocusRingMaxWidth() const
300 {
301 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 10100
302     return 9;
303 #else
304     return 0;
305 #endif
306 }
307
308 Color RenderThemeMac::platformInactiveListBoxSelectionBackgroundColor() const
309 {
310     return platformInactiveSelectionBackgroundColor();
311 }
312
313 static FontWeight toFontWeight(NSInteger appKitFontWeight)
314 {
315     ASSERT(appKitFontWeight > 0 && appKitFontWeight < 15);
316     if (appKitFontWeight > 14)
317         appKitFontWeight = 14;
318     else if (appKitFontWeight < 1)
319         appKitFontWeight = 1;
320
321     static FontWeight fontWeights[] = {
322         FontWeight100,
323         FontWeight100,
324         FontWeight200,
325         FontWeight300,
326         FontWeight400,
327         FontWeight500,
328         FontWeight600,
329         FontWeight600,
330         FontWeight700,
331         FontWeight800,
332         FontWeight800,
333         FontWeight900,
334         FontWeight900,
335         FontWeight900
336     };
337     return fontWeights[appKitFontWeight - 1];
338 }
339
340 void RenderThemeMac::systemFont(CSSValueID cssValueId, FontDescription& fontDescription) const
341 {
342     DEPRECATED_DEFINE_STATIC_LOCAL(FontDescription, systemFont, ());
343     DEPRECATED_DEFINE_STATIC_LOCAL(FontDescription, smallSystemFont, ());
344     DEPRECATED_DEFINE_STATIC_LOCAL(FontDescription, menuFont, ());
345     DEPRECATED_DEFINE_STATIC_LOCAL(FontDescription, labelFont, ());
346     DEPRECATED_DEFINE_STATIC_LOCAL(FontDescription, miniControlFont, ());
347     DEPRECATED_DEFINE_STATIC_LOCAL(FontDescription, smallControlFont, ());
348     DEPRECATED_DEFINE_STATIC_LOCAL(FontDescription, controlFont, ());
349
350     FontDescription* cachedDesc;
351     NSFont* font = nil;
352     switch (cssValueId) {
353         case CSSValueSmallCaption:
354             cachedDesc = &smallSystemFont;
355             if (!smallSystemFont.isAbsoluteSize())
356                 font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
357             break;
358         case CSSValueMenu:
359             cachedDesc = &menuFont;
360             if (!menuFont.isAbsoluteSize())
361                 font = [NSFont menuFontOfSize:[NSFont systemFontSize]];
362             break;
363         case CSSValueStatusBar:
364             cachedDesc = &labelFont;
365             if (!labelFont.isAbsoluteSize())
366                 font = [NSFont labelFontOfSize:[NSFont labelFontSize]];
367             break;
368         case CSSValueWebkitMiniControl:
369             cachedDesc = &miniControlFont;
370             if (!miniControlFont.isAbsoluteSize())
371                 font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]];
372             break;
373         case CSSValueWebkitSmallControl:
374             cachedDesc = &smallControlFont;
375             if (!smallControlFont.isAbsoluteSize())
376                 font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]];
377             break;
378         case CSSValueWebkitControl:
379             cachedDesc = &controlFont;
380             if (!controlFont.isAbsoluteSize())
381                 font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]];
382             break;
383         default:
384             cachedDesc = &systemFont;
385             if (!systemFont.isAbsoluteSize())
386                 font = [NSFont systemFontOfSize:[NSFont systemFontSize]];
387     }
388
389     if (font) {
390         NSFontManager *fontManager = [NSFontManager sharedFontManager];
391         cachedDesc->setIsAbsoluteSize(true);
392         cachedDesc->setGenericFamily(FontDescription::NoFamily);
393         cachedDesc->setOneFamily([font webCoreFamilyName]);
394         cachedDesc->setSpecifiedSize([font pointSize]);
395         cachedDesc->setWeight(toFontWeight([fontManager weightOfFont:font]));
396         cachedDesc->setItalic([fontManager traitsOfFont:font] & NSItalicFontMask);
397     }
398     fontDescription = *cachedDesc;
399 }
400
401 static RGBA32 convertNSColorToColor(NSColor *color)
402 {
403     NSColor *colorInColorSpace = [color colorUsingColorSpaceName:NSDeviceRGBColorSpace];
404     if (colorInColorSpace) {
405         static const double scaleFactor = nextafter(256.0, 0.0);
406         return makeRGB(static_cast<int>(scaleFactor * [colorInColorSpace redComponent]),
407             static_cast<int>(scaleFactor * [colorInColorSpace greenComponent]),
408             static_cast<int>(scaleFactor * [colorInColorSpace blueComponent]));
409     }
410
411     // This conversion above can fail if the NSColor in question is an NSPatternColor
412     // (as many system colors are). These colors are actually a repeating pattern
413     // not just a solid color. To work around this we simply draw a 1x1 image of
414     // the color and use that pixel's color. It might be better to use an average of
415     // the colors in the pattern instead.
416     NSBitmapImageRep *offscreenRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
417                                                                              pixelsWide:1
418                                                                              pixelsHigh:1
419                                                                           bitsPerSample:8
420                                                                         samplesPerPixel:4
421                                                                                hasAlpha:YES
422                                                                                isPlanar:NO
423                                                                          colorSpaceName:NSDeviceRGBColorSpace
424                                                                             bytesPerRow:4
425                                                                            bitsPerPixel:32];
426
427     [NSGraphicsContext saveGraphicsState];
428     [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep]];
429     NSEraseRect(NSMakeRect(0, 0, 1, 1));
430     [color drawSwatchInRect:NSMakeRect(0, 0, 1, 1)];
431     [NSGraphicsContext restoreGraphicsState];
432
433     NSUInteger pixel[4];
434     [offscreenRep getPixel:pixel atX:0 y:0];
435
436     [offscreenRep release];
437
438     return makeRGB(pixel[0], pixel[1], pixel[2]);
439 }
440
441 static RGBA32 menuBackgroundColor()
442 {
443     NSBitmapImageRep *offscreenRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
444                                                                              pixelsWide:1
445                                                                              pixelsHigh:1
446                                                                           bitsPerSample:8
447                                                                         samplesPerPixel:4
448                                                                                hasAlpha:YES
449                                                                                isPlanar:NO
450                                                                          colorSpaceName:NSDeviceRGBColorSpace
451                                                                             bytesPerRow:4
452                                                                            bitsPerPixel:32];
453
454     CGContextRef context = static_cast<CGContextRef>([[NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep] graphicsPort]);
455     CGRect rect = CGRectMake(0, 0, 1, 1);
456     HIThemeMenuDrawInfo drawInfo;
457     drawInfo.version =  0;
458     drawInfo.menuType = kThemeMenuTypePopUp;
459     HIThemeDrawMenuBackground(&rect, &drawInfo, context, kHIThemeOrientationInverted);
460
461     NSUInteger pixel[4];
462     [offscreenRep getPixel:pixel atX:0 y:0];
463
464     [offscreenRep release];
465
466     return makeRGB(pixel[0], pixel[1], pixel[2]);
467 }
468
469 void RenderThemeMac::platformColorsDidChange()
470 {
471     m_systemColorCache.clear();
472     RenderTheme::platformColorsDidChange();
473 }
474
475 Color RenderThemeMac::systemColor(CSSValueID cssValueId) const
476 {
477     {
478         HashMap<int, RGBA32>::iterator it = m_systemColorCache.find(cssValueId);
479         if (it != m_systemColorCache.end())
480             return it->value;
481     }
482
483     Color color;
484     switch (cssValueId) {
485     case CSSValueActiveborder:
486         color = convertNSColorToColor([NSColor keyboardFocusIndicatorColor]);
487         break;
488     case CSSValueActivecaption:
489         color = convertNSColorToColor([NSColor windowFrameTextColor]);
490         break;
491     case CSSValueAppworkspace:
492         color = convertNSColorToColor([NSColor headerColor]);
493         break;
494     case CSSValueBackground:
495         // Use theme independent default
496         break;
497     case CSSValueButtonface:
498         // We use this value instead of NSColor's controlColor to avoid website incompatibilities.
499         // We may want to change this to use the NSColor in future.
500         color = 0xFFC0C0C0;
501         break;
502     case CSSValueButtonhighlight:
503         color = convertNSColorToColor([NSColor controlHighlightColor]);
504         break;
505     case CSSValueButtonshadow:
506         color = convertNSColorToColor([NSColor controlShadowColor]);
507         break;
508     case CSSValueButtontext:
509         color = convertNSColorToColor([NSColor controlTextColor]);
510         break;
511     case CSSValueCaptiontext:
512         color = convertNSColorToColor([NSColor textColor]);
513         break;
514     case CSSValueGraytext:
515         color = convertNSColorToColor([NSColor disabledControlTextColor]);
516         break;
517     case CSSValueHighlight:
518         color = convertNSColorToColor([NSColor selectedTextBackgroundColor]);
519         break;
520     case CSSValueHighlighttext:
521         color = convertNSColorToColor([NSColor selectedTextColor]);
522         break;
523     case CSSValueInactiveborder:
524         color = convertNSColorToColor([NSColor controlBackgroundColor]);
525         break;
526     case CSSValueInactivecaption:
527         color = convertNSColorToColor([NSColor controlBackgroundColor]);
528         break;
529     case CSSValueInactivecaptiontext:
530         color = convertNSColorToColor([NSColor textColor]);
531         break;
532     case CSSValueInfobackground:
533         // There is no corresponding NSColor for this so we use a hard coded value.
534         color = 0xFFFBFCC5;
535         break;
536     case CSSValueInfotext:
537         color = convertNSColorToColor([NSColor textColor]);
538         break;
539     case CSSValueMenu:
540         color = menuBackgroundColor();
541         break;
542     case CSSValueMenutext:
543         color = convertNSColorToColor([NSColor selectedMenuItemTextColor]);
544         break;
545     case CSSValueScrollbar:
546         color = convertNSColorToColor([NSColor scrollBarColor]);
547         break;
548     case CSSValueText:
549         color = convertNSColorToColor([NSColor textColor]);
550         break;
551     case CSSValueThreeddarkshadow:
552         color = convertNSColorToColor([NSColor controlDarkShadowColor]);
553         break;
554     case CSSValueThreedshadow:
555         color = convertNSColorToColor([NSColor shadowColor]);
556         break;
557     case CSSValueThreedface:
558         // We use this value instead of NSColor's controlColor to avoid website incompatibilities.
559         // We may want to change this to use the NSColor in future.
560         color = 0xFFC0C0C0;
561         break;
562     case CSSValueThreedhighlight:
563         color = convertNSColorToColor([NSColor highlightColor]);
564         break;
565     case CSSValueThreedlightshadow:
566         color = convertNSColorToColor([NSColor controlLightHighlightColor]);
567         break;
568     case CSSValueWebkitFocusRingColor:
569         color = convertNSColorToColor([NSColor keyboardFocusIndicatorColor]);
570         break;
571     case CSSValueWindow:
572         color = convertNSColorToColor([NSColor windowBackgroundColor]);
573         break;
574     case CSSValueWindowframe:
575         color = convertNSColorToColor([NSColor windowFrameColor]);
576         break;
577     case CSSValueWindowtext:
578         color = convertNSColorToColor([NSColor windowFrameTextColor]);
579         break;
580     default:
581         break;
582     }
583
584     if (!color.isValid())
585         color = RenderTheme::systemColor(cssValueId);
586
587     if (color.isValid())
588         m_systemColorCache.set(cssValueId, color.rgb());
589
590     return color;
591 }
592
593 bool RenderThemeMac::usesTestModeFocusRingColor() const
594 {
595     return WebCore::usesTestModeFocusRingColor();
596 }
597
598 bool RenderThemeMac::isControlStyled(const RenderStyle* style, const BorderData& border,
599                                      const FillLayer& background, const Color& backgroundColor) const
600 {
601     if (style->appearance() == TextFieldPart || style->appearance() == TextAreaPart || style->appearance() == ListboxPart)
602         return style->border() != border;
603
604     // FIXME: This is horrible, but there is not much else that can be done.  Menu lists cannot draw properly when
605     // scaled.  They can't really draw properly when transformed either.  We can't detect the transform case at style
606     // adjustment time so that will just have to stay broken.  We can however detect that we're zooming.  If zooming
607     // is in effect we treat it like the control is styled.
608     if (style->appearance() == MenulistPart && style->effectiveZoom() != 1.0f)
609         return true;
610
611     return RenderTheme::isControlStyled(style, border, background, backgroundColor);
612 }
613
614 void RenderThemeMac::adjustRepaintRect(const RenderObject& o, IntRect& r)
615 {
616     ControlPart part = o.style().appearance();
617
618 #if USE(NEW_THEME)
619     switch (part) {
620         case CheckboxPart:
621         case RadioPart:
622         case PushButtonPart:
623         case SquareButtonPart:
624         case DefaultButtonPart:
625         case ButtonPart:
626         case InnerSpinButtonPart:
627             return RenderTheme::adjustRepaintRect(o, r);
628         default:
629             break;
630     }
631 #endif
632
633     float zoomLevel = o.style().effectiveZoom();
634
635     if (part == MenulistPart) {
636         setPopupButtonCellState(o, r);
637         IntSize size = popupButtonSizes()[[popupButton() controlSize]];
638         size.setHeight(size.height() * zoomLevel);
639         size.setWidth(r.width());
640         r = inflateRect(r, size, popupButtonMargins(), zoomLevel);
641     }
642 }
643
644 IntRect RenderThemeMac::inflateRect(const IntRect& r, const IntSize& size, const int* margins, float zoomLevel) const
645 {
646     // Only do the inflation if the available width/height are too small.  Otherwise try to
647     // fit the glow/check space into the available box's width/height.
648     int widthDelta = r.width() - (size.width() + margins[leftMargin] * zoomLevel + margins[rightMargin] * zoomLevel);
649     int heightDelta = r.height() - (size.height() + margins[topMargin] * zoomLevel + margins[bottomMargin] * zoomLevel);
650     IntRect result(r);
651     if (widthDelta < 0) {
652         result.setX(result.x() - margins[leftMargin] * zoomLevel);
653         result.setWidth(result.width() - widthDelta);
654     }
655     if (heightDelta < 0) {
656         result.setY(result.y() - margins[topMargin] * zoomLevel);
657         result.setHeight(result.height() - heightDelta);
658     }
659     return result;
660 }
661
662 FloatRect RenderThemeMac::convertToPaintingRect(const RenderObject& inputRenderer, const RenderObject& partRenderer, const FloatRect& inputRect, const IntRect& r) const
663 {
664     FloatRect partRect(inputRect);
665
666     // Compute an offset between the part renderer and the input renderer
667     FloatSize offsetFromInputRenderer;
668     const RenderObject* renderer = &partRenderer;
669     while (renderer && renderer != &inputRenderer) {
670         RenderElement* containingRenderer = renderer->container();
671         offsetFromInputRenderer -= roundedIntSize(renderer->offsetFromContainer(containingRenderer, LayoutPoint()));
672         renderer = containingRenderer;
673     }
674     // If the input renderer was not a container, something went wrong
675     ASSERT(renderer == &inputRenderer);
676     // Move the rect into partRenderer's coords
677     partRect.move(offsetFromInputRenderer);
678     // Account for the local drawing offset (tx, ty)
679     partRect.move(r.x(), r.y());
680
681     return partRect;
682 }
683
684 void RenderThemeMac::updateCheckedState(NSCell* cell, const RenderObject& o)
685 {
686     bool oldIndeterminate = [cell state] == NSMixedState;
687     bool indeterminate = isIndeterminate(o);
688     bool checked = isChecked(o);
689
690     if (oldIndeterminate != indeterminate) {
691         [cell setState:indeterminate ? NSMixedState : (checked ? NSOnState : NSOffState)];
692         return;
693     }
694
695     bool oldChecked = [cell state] == NSOnState;
696     if (checked != oldChecked)
697         [cell setState:checked ? NSOnState : NSOffState];
698 }
699
700 void RenderThemeMac::updateEnabledState(NSCell* cell, const RenderObject& o)
701 {
702     bool oldEnabled = [cell isEnabled];
703     bool enabled = isEnabled(o);
704     if (enabled != oldEnabled)
705         [cell setEnabled:enabled];
706 }
707
708 void RenderThemeMac::updateFocusedState(NSCell* cell, const RenderObject& o)
709 {
710     bool oldFocused = [cell showsFirstResponder];
711     bool focused = isFocused(o) && o.style().outlineStyleIsAuto();
712     if (focused != oldFocused)
713         [cell setShowsFirstResponder:focused];
714 }
715
716 void RenderThemeMac::updatePressedState(NSCell* cell, const RenderObject& o)
717 {
718     bool oldPressed = [cell isHighlighted];
719     bool pressed = o.node() && o.node()->isElementNode() && toElement(o.node())->active();
720     if (pressed != oldPressed)
721         [cell setHighlighted:pressed];
722 }
723
724 bool RenderThemeMac::controlSupportsTints(const RenderObject& o) const
725 {
726     // An alternate way to implement this would be to get the appropriate cell object
727     // and call the private _needRedrawOnWindowChangedKeyState method. An advantage of
728     // that would be that we would match AppKit behavior more closely, but a disadvantage
729     // would be that we would rely on an AppKit SPI method.
730
731     if (!isEnabled(o))
732         return false;
733
734     // Checkboxes only have tint when checked.
735     if (o.style().appearance() == CheckboxPart)
736         return isChecked(o);
737
738     // For now assume other controls have tint if enabled.
739     return true;
740 }
741
742 NSControlSize RenderThemeMac::controlSizeForFont(RenderStyle* style) const
743 {
744     int fontSize = style->fontSize();
745     if (fontSize >= 16)
746         return NSRegularControlSize;
747     if (fontSize >= 11)
748         return NSSmallControlSize;
749     return NSMiniControlSize;
750 }
751
752 NSControlSize RenderThemeMac::controlSizeForCell(NSCell*, const IntSize* sizes, const IntSize& minSize, float zoomLevel) const
753 {
754     if (minSize.width() >= static_cast<int>(sizes[NSRegularControlSize].width() * zoomLevel)
755         && minSize.height() >= static_cast<int>(sizes[NSRegularControlSize].height() * zoomLevel))
756         return NSRegularControlSize;
757
758     if (minSize.width() >= static_cast<int>(sizes[NSSmallControlSize].width() * zoomLevel)
759         && minSize.height() >= static_cast<int>(sizes[NSSmallControlSize].height() * zoomLevel))
760         return NSSmallControlSize;
761
762     return NSMiniControlSize;
763 }
764
765 void RenderThemeMac::setControlSize(NSCell* cell, const IntSize* sizes, const IntSize& minSize, float zoomLevel)
766 {
767     NSControlSize size = controlSizeForCell(cell, sizes, minSize, zoomLevel);
768     if (size != [cell controlSize]) // Only update if we have to, since AppKit does work even if the size is the same.
769         [cell setControlSize:size];
770 }
771
772 IntSize RenderThemeMac::sizeForFont(RenderStyle* style, const IntSize* sizes) const
773 {
774     if (style->effectiveZoom() != 1.0f) {
775         IntSize result = sizes[controlSizeForFont(style)];
776         return IntSize(result.width() * style->effectiveZoom(), result.height() * style->effectiveZoom());
777     }
778     return sizes[controlSizeForFont(style)];
779 }
780
781 IntSize RenderThemeMac::sizeForSystemFont(RenderStyle* style, const IntSize* sizes) const
782 {
783     if (style->effectiveZoom() != 1.0f) {
784         IntSize result = sizes[controlSizeForSystemFont(style)];
785         return IntSize(result.width() * style->effectiveZoom(), result.height() * style->effectiveZoom());
786     }
787     return sizes[controlSizeForSystemFont(style)];
788 }
789
790 void RenderThemeMac::setSizeFromFont(RenderStyle* style, const IntSize* sizes) const
791 {
792     // FIXME: Check is flawed, since it doesn't take min-width/max-width into account.
793     IntSize size = sizeForFont(style, sizes);
794     if (style->width().isIntrinsicOrAuto() && size.width() > 0)
795         style->setWidth(Length(size.width(), Fixed));
796     if (style->height().isAuto() && size.height() > 0)
797         style->setHeight(Length(size.height(), Fixed));
798 }
799
800 void RenderThemeMac::setFontFromControlSize(StyleResolver*, RenderStyle* style, NSControlSize controlSize) const
801 {
802     FontDescription fontDescription;
803     fontDescription.setIsAbsoluteSize(true);
804     fontDescription.setGenericFamily(FontDescription::SerifFamily);
805
806     NSFont* font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:controlSize]];
807     fontDescription.setOneFamily([font webCoreFamilyName]);
808     fontDescription.setComputedSize([font pointSize] * style->effectiveZoom());
809     fontDescription.setSpecifiedSize([font pointSize] * style->effectiveZoom());
810
811     // Reset line height
812     style->setLineHeight(RenderStyle::initialLineHeight());
813
814     if (style->setFontDescription(fontDescription))
815         style->font().update(0);
816 }
817
818 NSControlSize RenderThemeMac::controlSizeForSystemFont(RenderStyle* style) const
819 {
820     int fontSize = style->fontSize();
821     if (fontSize >= [NSFont systemFontSizeForControlSize:NSRegularControlSize])
822         return NSRegularControlSize;
823     if (fontSize >= [NSFont systemFontSizeForControlSize:NSSmallControlSize])
824         return NSSmallControlSize;
825     return NSMiniControlSize;
826 }
827
828 bool RenderThemeMac::paintTextField(const RenderObject& o, const PaintInfo& paintInfo, const FloatRect& r)
829 {
830     LocalCurrentGraphicsContext localContext(paintInfo.context);
831
832     NSTextFieldCell *textField = this->textField();
833
834     GraphicsContextStateSaver stateSaver(*paintInfo.context);
835
836     [textField setEnabled:(isEnabled(o) && !isReadOnlyControl(o))];
837     [textField drawWithFrame:NSRect(r) inView:documentViewFor(o)];
838
839     [textField setControlView:nil];
840
841     return false;
842 }
843
844 void RenderThemeMac::adjustTextFieldStyle(StyleResolver*, RenderStyle*, Element*) const
845 {
846 }
847
848 bool RenderThemeMac::paintCapsLockIndicator(const RenderObject&, const PaintInfo& paintInfo, const IntRect& r)
849 {
850     if (paintInfo.context->paintingDisabled())
851         return true;
852
853     LocalCurrentGraphicsContext localContext(paintInfo.context);
854     wkDrawCapsLockIndicator(localContext.cgContext(), r);
855
856     return false;
857 }
858
859 bool RenderThemeMac::paintTextArea(const RenderObject& o, const PaintInfo& paintInfo, const FloatRect& r)
860 {
861     LocalCurrentGraphicsContext localContext(paintInfo.context);
862     wkDrawBezeledTextArea(r, isEnabled(o) && !isReadOnlyControl(o));
863     return false;
864 }
865
866 void RenderThemeMac::adjustTextAreaStyle(StyleResolver*, RenderStyle*, Element*) const
867 {
868 }
869
870 const int* RenderThemeMac::popupButtonMargins() const
871 {
872     static const int margins[3][4] =
873     {
874         { 0, 3, 1, 3 },
875         { 0, 3, 2, 3 },
876         { 0, 1, 0, 1 }
877     };
878     return margins[[popupButton() controlSize]];
879 }
880
881 const IntSize* RenderThemeMac::popupButtonSizes() const
882 {
883     static const IntSize sizes[3] = { IntSize(0, 21), IntSize(0, 18), IntSize(0, 15) };
884     return sizes;
885 }
886
887 const int* RenderThemeMac::popupButtonPadding(NSControlSize size) const
888 {
889     static const int padding[3][4] =
890     {
891         { 2, 26, 3, 8 },
892         { 2, 23, 3, 8 },
893         { 2, 22, 3, 10 }
894     };
895     return padding[size];
896 }
897
898 bool RenderThemeMac::paintMenuList(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
899 {
900     LocalCurrentGraphicsContext localContext(paintInfo.context);
901     setPopupButtonCellState(o, r);
902
903     NSPopUpButtonCell* popupButton = this->popupButton();
904
905     float zoomLevel = o.style().effectiveZoom();
906     IntSize size = popupButtonSizes()[[popupButton controlSize]];
907     size.setHeight(size.height() * zoomLevel);
908     size.setWidth(r.width());
909
910     // Now inflate it to account for the shadow.
911     IntRect inflatedRect = r;
912     if (r.width() >= minimumMenuListSize(&o.style()))
913         inflatedRect = inflateRect(inflatedRect, size, popupButtonMargins(), zoomLevel);
914
915     GraphicsContextStateSaver stateSaver(*paintInfo.context);
916
917     // On Leopard, the cell will draw outside of the given rect, so we have to clip to the rect
918     paintInfo.context->clip(inflatedRect);
919
920     if (zoomLevel != 1.0f) {
921         inflatedRect.setWidth(inflatedRect.width() / zoomLevel);
922         inflatedRect.setHeight(inflatedRect.height() / zoomLevel);
923         paintInfo.context->translate(inflatedRect.x(), inflatedRect.y());
924         paintInfo.context->scale(FloatSize(zoomLevel, zoomLevel));
925         paintInfo.context->translate(-inflatedRect.x(), -inflatedRect.y());
926     }
927
928     NSView *view = documentViewFor(o);
929     [popupButton drawWithFrame:inflatedRect inView:view];
930     if (isFocused(o) && o.style().outlineStyleIsAuto()) {
931         double timeSinceFocused = o.document().page()->focusController().timeSinceFocusWasSet();
932         if (wkDrawCellFocusRingWithFrameAtTime(popupButton, inflatedRect, view, timeSinceFocused))
933             o.document().page()->focusController().setFocusedElementNeedsRepaint();
934     }
935
936     [popupButton setControlView:nil];
937
938     return false;
939 }
940
941 #if ENABLE(METER_ELEMENT)
942
943 IntSize RenderThemeMac::meterSizeForBounds(const RenderMeter* renderMeter, const IntRect& bounds) const
944 {
945     if (NoControlPart == renderMeter->style().appearance())
946         return bounds.size();
947
948     NSLevelIndicatorCell* cell = levelIndicatorFor(renderMeter);
949     // Makes enough room for cell's intrinsic size.
950     NSSize cellSize = [cell cellSizeForBounds:IntRect(IntPoint(), bounds.size())];
951     return IntSize(bounds.width() < cellSize.width ? cellSize.width : bounds.width(),
952                    bounds.height() < cellSize.height ? cellSize.height : bounds.height());
953 }
954
955 bool RenderThemeMac::paintMeter(const RenderObject& renderObject, const PaintInfo& paintInfo, const IntRect& rect)
956 {
957     if (!renderObject.isMeter())
958         return true;
959
960     LocalCurrentGraphicsContext localContext(paintInfo.context);
961
962     NSLevelIndicatorCell* cell = levelIndicatorFor(toRenderMeter(&renderObject));
963     GraphicsContextStateSaver stateSaver(*paintInfo.context);
964
965     [cell drawWithFrame:rect inView:documentViewFor(renderObject)];
966     [cell setControlView:nil];
967     return false;
968 }
969
970 bool RenderThemeMac::supportsMeter(ControlPart part) const
971 {
972     switch (part) {
973     case RelevancyLevelIndicatorPart:
974     case DiscreteCapacityLevelIndicatorPart:
975     case RatingLevelIndicatorPart:
976     case MeterPart:
977     case ContinuousCapacityLevelIndicatorPart:
978         return true;
979     default:
980         return false;
981     }
982 }
983
984 NSLevelIndicatorStyle RenderThemeMac::levelIndicatorStyleFor(ControlPart part) const
985 {
986     switch (part) {
987     case RelevancyLevelIndicatorPart:
988         return NSRelevancyLevelIndicatorStyle;
989     case DiscreteCapacityLevelIndicatorPart:
990         return NSDiscreteCapacityLevelIndicatorStyle;
991     case RatingLevelIndicatorPart:
992         return NSRatingLevelIndicatorStyle;
993     case MeterPart:
994     case ContinuousCapacityLevelIndicatorPart:
995     default:
996         return NSContinuousCapacityLevelIndicatorStyle;
997     }
998
999 }
1000
1001 NSLevelIndicatorCell* RenderThemeMac::levelIndicatorFor(const RenderMeter* renderMeter) const
1002 {
1003     const RenderStyle& style = renderMeter->style();
1004     ASSERT(style.appearance() != NoControlPart);
1005
1006     if (!m_levelIndicator)
1007         m_levelIndicator = adoptNS([[NSLevelIndicatorCell alloc] initWithLevelIndicatorStyle:NSContinuousCapacityLevelIndicatorStyle]);
1008     NSLevelIndicatorCell* cell = m_levelIndicator.get();
1009
1010     HTMLMeterElement* element = renderMeter->meterElement();
1011     double value = element->value();
1012
1013     // Because NSLevelIndicatorCell does not support optimum-in-the-middle type coloring,
1014     // we explicitly control the color instead giving low and high value to NSLevelIndicatorCell as is.
1015     switch (element->gaugeRegion()) {
1016     case HTMLMeterElement::GaugeRegionOptimum:
1017         // Make meter the green
1018         [cell setWarningValue:value + 1];
1019         [cell setCriticalValue:value + 2];
1020         break;
1021     case HTMLMeterElement::GaugeRegionSuboptimal:
1022         // Make the meter yellow
1023         [cell setWarningValue:value - 1];
1024         [cell setCriticalValue:value + 1];
1025         break;
1026     case HTMLMeterElement::GaugeRegionEvenLessGood:
1027         // Make the meter red
1028         [cell setWarningValue:value - 2];
1029         [cell setCriticalValue:value - 1];
1030         break;
1031     }
1032
1033     [cell setLevelIndicatorStyle:levelIndicatorStyleFor(style.appearance())];
1034     [cell setBaseWritingDirection:style.isLeftToRightDirection() ? NSWritingDirectionLeftToRight : NSWritingDirectionRightToLeft];
1035     [cell setMinValue:element->min()];
1036     [cell setMaxValue:element->max()];
1037     RetainPtr<NSNumber> valueObject = [NSNumber numberWithDouble:value];
1038     [cell setObjectValue:valueObject.get()];
1039
1040     return cell;
1041 }
1042
1043 #endif
1044
1045 #if ENABLE(PROGRESS_ELEMENT)
1046 const IntSize* RenderThemeMac::progressBarSizes() const
1047 {
1048     static const IntSize sizes[3] = { IntSize(0, 20), IntSize(0, 12), IntSize(0, 12) };
1049     return sizes;
1050 }
1051
1052 const int* RenderThemeMac::progressBarMargins(NSControlSize controlSize) const
1053 {
1054     static const int margins[3][4] =
1055     {
1056         { 0, 0, 1, 0 },
1057         { 0, 0, 1, 0 },
1058         { 0, 0, 1, 0 },
1059     };
1060     return margins[controlSize];
1061 }
1062
1063 IntRect RenderThemeMac::progressBarRectForBounds(const RenderObject& renderObject, const IntRect& bounds) const
1064 {
1065     // Workaround until <rdar://problem/15855086> is fixed.
1066     int maxDimension = static_cast<int>(std::numeric_limits<ushort>::max());
1067     IntRect progressBarBounds(bounds.x(), bounds.y(), std::min(bounds.width(), maxDimension), std::min(bounds.height(), maxDimension));
1068     if (NoControlPart == renderObject.style().appearance())
1069         return progressBarBounds;
1070
1071     float zoomLevel = renderObject.style().effectiveZoom();
1072     NSControlSize controlSize = controlSizeForFont(&renderObject.style());
1073     IntSize size = progressBarSizes()[controlSize];
1074     size.setHeight(size.height() * zoomLevel);
1075     size.setWidth(progressBarBounds.width());
1076
1077     // Now inflate it to account for the shadow.
1078     IntRect inflatedRect = progressBarBounds;
1079     if (progressBarBounds.height() <= minimumProgressBarHeight(&renderObject.style()))
1080         inflatedRect = inflateRect(inflatedRect, size, progressBarMargins(controlSize), zoomLevel);
1081
1082     return inflatedRect;
1083 }
1084
1085 int RenderThemeMac::minimumProgressBarHeight(RenderStyle* style) const
1086 {
1087     return sizeForSystemFont(style, progressBarSizes()).height();
1088 }
1089
1090 double RenderThemeMac::animationRepeatIntervalForProgressBar(RenderProgress*) const
1091 {
1092     return progressAnimationFrameRate;
1093 }
1094
1095 double RenderThemeMac::animationDurationForProgressBar(RenderProgress*) const
1096 {
1097     return progressAnimationNumFrames * progressAnimationFrameRate;
1098 }
1099
1100 void RenderThemeMac::adjustProgressBarStyle(StyleResolver*, RenderStyle*, Element*) const
1101 {
1102 }
1103
1104 bool RenderThemeMac::paintProgressBar(const RenderObject& renderObject, const PaintInfo& paintInfo, const IntRect& rect)
1105 {
1106     if (!renderObject.isProgress())
1107         return true;
1108
1109     IntRect inflatedRect = progressBarRectForBounds(renderObject, rect);
1110     NSControlSize controlSize = controlSizeForFont(&renderObject.style());
1111
1112     const RenderProgress& renderProgress = *toRenderProgress(&renderObject);
1113     HIThemeTrackDrawInfo trackInfo;
1114     trackInfo.version = 0;
1115     if (controlSize == NSRegularControlSize)
1116         trackInfo.kind = renderProgress.position() < 0 ? kThemeLargeIndeterminateBar : kThemeLargeProgressBar;
1117     else
1118         trackInfo.kind = renderProgress.position() < 0 ? kThemeMediumIndeterminateBar : kThemeMediumProgressBar;
1119
1120     float deviceScaleFactor = renderObject.document().deviceScaleFactor();
1121     trackInfo.bounds = IntRect(IntPoint(), inflatedRect.size());
1122     trackInfo.min = 0;
1123     trackInfo.max = std::numeric_limits<SInt32>::max();
1124     trackInfo.value = lround(renderProgress.position() * nextafter(trackInfo.max, 0));
1125     trackInfo.trackInfo.progress.phase = lround(renderProgress.animationProgress() * nextafter(progressAnimationNumFrames, 0) * deviceScaleFactor);
1126     trackInfo.attributes = kThemeTrackHorizontal;
1127     trackInfo.enableState = isActive(renderObject) ? kThemeTrackActive : kThemeTrackInactive;
1128     trackInfo.reserved = 0;
1129     trackInfo.filler1 = 0;
1130
1131     std::unique_ptr<ImageBuffer> imageBuffer = ImageBuffer::create(inflatedRect.size(), deviceScaleFactor);
1132     if (!imageBuffer)
1133         return true;
1134
1135     ContextContainer cgContextContainer(imageBuffer->context());
1136     CGContextRef cgContext = cgContextContainer.context();
1137     HIThemeDrawTrack(&trackInfo, 0, cgContext, kHIThemeOrientationNormal);
1138
1139     GraphicsContextStateSaver stateSaver(*paintInfo.context);
1140
1141     if (!renderProgress.style().isLeftToRightDirection()) {
1142         paintInfo.context->translate(2 * inflatedRect.x() + inflatedRect.width(), 0);
1143         paintInfo.context->scale(FloatSize(-1, 1));
1144     }
1145
1146     paintInfo.context->drawImageBuffer(imageBuffer.get(), ColorSpaceDeviceRGB, inflatedRect.location());
1147     return false;
1148 }
1149 #endif
1150
1151 const float baseFontSize = 11.0f;
1152 const float baseArrowHeight = 4.0f;
1153 const float baseArrowWidth = 5.0f;
1154 const float baseSpaceBetweenArrows = 2.0f;
1155 const int arrowPaddingLeft = 6;
1156 const int arrowPaddingRight = 6;
1157 const int paddingBeforeSeparator = 4;
1158 const int baseBorderRadius = 5;
1159 const int styledPopupPaddingLeft = 8;
1160 const int styledPopupPaddingTop = 1;
1161 const int styledPopupPaddingBottom = 2;
1162
1163 static void TopGradientInterpolate(void*, const CGFloat* inData, CGFloat* outData)
1164 {
1165     static float dark[4] = { 1.0f, 1.0f, 1.0f, 0.4f };
1166     static float light[4] = { 1.0f, 1.0f, 1.0f, 0.15f };
1167     float a = inData[0];
1168     int i = 0;
1169     for (i = 0; i < 4; i++)
1170         outData[i] = (1.0f - a) * dark[i] + a * light[i];
1171 }
1172
1173 static void BottomGradientInterpolate(void*, const CGFloat* inData, CGFloat* outData)
1174 {
1175     static float dark[4] = { 1.0f, 1.0f, 1.0f, 0.0f };
1176     static float light[4] = { 1.0f, 1.0f, 1.0f, 0.3f };
1177     float a = inData[0];
1178     int i = 0;
1179     for (i = 0; i < 4; i++)
1180         outData[i] = (1.0f - a) * dark[i] + a * light[i];
1181 }
1182
1183 static void MainGradientInterpolate(void*, const CGFloat* inData, CGFloat* outData)
1184 {
1185     static float dark[4] = { 0.0f, 0.0f, 0.0f, 0.15f };
1186     static float light[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
1187     float a = inData[0];
1188     int i = 0;
1189     for (i = 0; i < 4; i++)
1190         outData[i] = (1.0f - a) * dark[i] + a * light[i];
1191 }
1192
1193 static void TrackGradientInterpolate(void*, const CGFloat* inData, CGFloat* outData)
1194 {
1195     static float dark[4] = { 0.0f, 0.0f, 0.0f, 0.678f };
1196     static float light[4] = { 0.0f, 0.0f, 0.0f, 0.13f };
1197     float a = inData[0];
1198     int i = 0;
1199     for (i = 0; i < 4; i++)
1200         outData[i] = (1.0f - a) * dark[i] + a * light[i];
1201 }
1202
1203 void RenderThemeMac::paintMenuListButtonGradients(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1204 {
1205     if (r.isEmpty())
1206         return;
1207
1208     ContextContainer cgContextContainer(paintInfo.context);
1209     CGContextRef context = cgContextContainer.context();
1210
1211     GraphicsContextStateSaver stateSaver(*paintInfo.context);
1212
1213     FloatRoundedRect border = FloatRoundedRect(o.style().getRoundedBorderFor(r, &o.view()));
1214     int radius = border.radii().topLeft().width();
1215
1216     CGColorSpaceRef cspace = deviceRGBColorSpaceRef();
1217
1218     FloatRect topGradient(r.x(), r.y(), r.width(), r.height() / 2.0f);
1219     struct CGFunctionCallbacks topCallbacks = { 0, TopGradientInterpolate, NULL };
1220     RetainPtr<CGFunctionRef> topFunction = adoptCF(CGFunctionCreate(NULL, 1, NULL, 4, NULL, &topCallbacks));
1221     RetainPtr<CGShadingRef> topShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(topGradient.x(), topGradient.y()), CGPointMake(topGradient.x(), topGradient.maxY()), topFunction.get(), false, false));
1222
1223     FloatRect bottomGradient(r.x() + radius, r.y() + r.height() / 2.0f, r.width() - 2.0f * radius, r.height() / 2.0f);
1224     struct CGFunctionCallbacks bottomCallbacks = { 0, BottomGradientInterpolate, NULL };
1225     RetainPtr<CGFunctionRef> bottomFunction = adoptCF(CGFunctionCreate(NULL, 1, NULL, 4, NULL, &bottomCallbacks));
1226     RetainPtr<CGShadingRef> bottomShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(bottomGradient.x(),  bottomGradient.y()), CGPointMake(bottomGradient.x(), bottomGradient.maxY()), bottomFunction.get(), false, false));
1227
1228     struct CGFunctionCallbacks mainCallbacks = { 0, MainGradientInterpolate, NULL };
1229     RetainPtr<CGFunctionRef> mainFunction = adoptCF(CGFunctionCreate(NULL, 1, NULL, 4, NULL, &mainCallbacks));
1230     RetainPtr<CGShadingRef> mainShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(r.x(),  r.y()), CGPointMake(r.x(), r.maxY()), mainFunction.get(), false, false));
1231
1232     RetainPtr<CGShadingRef> leftShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(r.x(),  r.y()), CGPointMake(r.x() + radius, r.y()), mainFunction.get(), false, false));
1233
1234     RetainPtr<CGShadingRef> rightShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(r.maxX(),  r.y()), CGPointMake(r.maxX() - radius, r.y()), mainFunction.get(), false, false));
1235
1236     {
1237         GraphicsContextStateSaver stateSaver(*paintInfo.context);
1238         CGContextClipToRect(context, r);
1239         paintInfo.context->clipRoundedRect(border);
1240         context = cgContextContainer.context();
1241         CGContextDrawShading(context, mainShading.get());
1242     }
1243
1244     {
1245         GraphicsContextStateSaver stateSaver(*paintInfo.context);
1246         CGContextClipToRect(context, topGradient);
1247         paintInfo.context->clipRoundedRect(FloatRoundedRect(enclosingIntRect(topGradient), border.radii().topLeft(), border.radii().topRight(), IntSize(), IntSize()));
1248         context = cgContextContainer.context();
1249         CGContextDrawShading(context, topShading.get());
1250     }
1251
1252     if (!bottomGradient.isEmpty()) {
1253         GraphicsContextStateSaver stateSaver(*paintInfo.context);
1254         CGContextClipToRect(context, bottomGradient);
1255         paintInfo.context->clipRoundedRect(FloatRoundedRect(enclosingIntRect(bottomGradient), IntSize(), IntSize(), border.radii().bottomLeft(), border.radii().bottomRight()));
1256         context = cgContextContainer.context();
1257         CGContextDrawShading(context, bottomShading.get());
1258     }
1259
1260     {
1261         GraphicsContextStateSaver stateSaver(*paintInfo.context);
1262         CGContextClipToRect(context, r);
1263         paintInfo.context->clipRoundedRect(border);
1264         context = cgContextContainer.context();
1265         CGContextDrawShading(context, leftShading.get());
1266         CGContextDrawShading(context, rightShading.get());
1267     }
1268 }
1269
1270 bool RenderThemeMac::paintMenuListButtonDecorations(const RenderObject& renderer, const PaintInfo& paintInfo, const FloatRect& rect)
1271 {
1272     IntRect bounds = IntRect(rect.x() + renderer.style().borderLeftWidth(),
1273         rect.y() + renderer.style().borderTopWidth(),
1274         rect.width() - renderer.style().borderLeftWidth() - renderer.style().borderRightWidth(),
1275         rect.height() - renderer.style().borderTopWidth() - renderer.style().borderBottomWidth());
1276     // Draw the gradients to give the styled popup menu a button appearance
1277     paintMenuListButtonGradients(renderer, paintInfo, bounds);
1278
1279     // 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
1280     float fontScale = std::min(renderer.style().fontSize() / baseFontSize, bounds.height() / (baseArrowHeight * 2 + baseSpaceBetweenArrows));
1281     float centerY = bounds.y() + bounds.height() / 2.0f;
1282     float arrowHeight = baseArrowHeight * fontScale;
1283     float arrowWidth = baseArrowWidth * fontScale;
1284     float leftEdge = bounds.maxX() - arrowPaddingRight * renderer.style().effectiveZoom() - arrowWidth;
1285     float spaceBetweenArrows = baseSpaceBetweenArrows * fontScale;
1286
1287     if (bounds.width() < arrowWidth + arrowPaddingLeft * renderer.style().effectiveZoom())
1288         return false;
1289
1290     GraphicsContextStateSaver stateSaver(*paintInfo.context);
1291
1292     paintInfo.context->setFillColor(renderer.style().visitedDependentColor(CSSPropertyColor), renderer.style().colorSpace());
1293     paintInfo.context->setStrokeStyle(NoStroke);
1294
1295     FloatPoint arrow1[3];
1296     arrow1[0] = FloatPoint(leftEdge, centerY - spaceBetweenArrows / 2.0f);
1297     arrow1[1] = FloatPoint(leftEdge + arrowWidth, centerY - spaceBetweenArrows / 2.0f);
1298     arrow1[2] = FloatPoint(leftEdge + arrowWidth / 2.0f, centerY - spaceBetweenArrows / 2.0f - arrowHeight);
1299
1300     // Draw the top arrow
1301     paintInfo.context->drawConvexPolygon(3, arrow1, true);
1302
1303     FloatPoint arrow2[3];
1304     arrow2[0] = FloatPoint(leftEdge, centerY + spaceBetweenArrows / 2.0f);
1305     arrow2[1] = FloatPoint(leftEdge + arrowWidth, centerY + spaceBetweenArrows / 2.0f);
1306     arrow2[2] = FloatPoint(leftEdge + arrowWidth / 2.0f, centerY + spaceBetweenArrows / 2.0f + arrowHeight);
1307
1308     // Draw the bottom arrow
1309     paintInfo.context->drawConvexPolygon(3, arrow2, true);
1310
1311     Color leftSeparatorColor(0, 0, 0, 40);
1312     Color rightSeparatorColor(255, 255, 255, 40);
1313
1314     // FIXME: Should the separator thickness and space be scaled up by fontScale?
1315     int separatorSpace = 2; // Deliberately ignores zoom since it looks nicer if it stays thin.
1316     int leftEdgeOfSeparator = static_cast<int>(leftEdge - arrowPaddingLeft * renderer.style().effectiveZoom()); // FIXME: Round?
1317
1318     // Draw the separator to the left of the arrows
1319     paintInfo.context->setStrokeThickness(1); // Deliberately ignores zoom since it looks nicer if it stays thin.
1320     paintInfo.context->setStrokeStyle(SolidStroke);
1321     paintInfo.context->setStrokeColor(leftSeparatorColor, ColorSpaceDeviceRGB);
1322     paintInfo.context->drawLine(IntPoint(leftEdgeOfSeparator, bounds.y()),
1323                                 IntPoint(leftEdgeOfSeparator, bounds.maxY()));
1324
1325     paintInfo.context->setStrokeColor(rightSeparatorColor, ColorSpaceDeviceRGB);
1326     paintInfo.context->drawLine(IntPoint(leftEdgeOfSeparator + separatorSpace, bounds.y()),
1327                                 IntPoint(leftEdgeOfSeparator + separatorSpace, bounds.maxY()));
1328     return false;
1329 }
1330
1331 static const IntSize* menuListButtonSizes()
1332 {
1333     static const IntSize sizes[3] = { IntSize(0, 21), IntSize(0, 18), IntSize(0, 15) };
1334     return sizes;
1335 }
1336
1337 void RenderThemeMac::adjustMenuListStyle(StyleResolver* styleResolver, RenderStyle* style, Element* e) const
1338 {
1339     NSControlSize controlSize = controlSizeForFont(style);
1340
1341     style->resetBorder();
1342     style->resetPadding();
1343
1344     // Height is locked to auto.
1345     style->setHeight(Length(Auto));
1346
1347     // White-space is locked to pre
1348     style->setWhiteSpace(PRE);
1349
1350     // Set the foreground color to black or gray when we have the aqua look.
1351     // Cast to RGB32 is to work around a compiler bug.
1352     style->setColor(e && !e->isDisabledFormControl() ? static_cast<RGBA32>(Color::black) : Color::darkGray);
1353
1354     // Set the button's vertical size.
1355     setSizeFromFont(style, menuListButtonSizes());
1356
1357     // 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
1358     // a reasonable control size, but once that control size is determined, we throw that font away and use the appropriate
1359     // system font for the control size instead.
1360     setFontFromControlSize(styleResolver, style, controlSize);
1361
1362     style->setBoxShadow(nullptr);
1363 }
1364
1365 int RenderThemeMac::popupInternalPaddingLeft(RenderStyle* style) const
1366 {
1367     if (style->appearance() == MenulistPart)
1368         return popupButtonPadding(controlSizeForFont(style))[leftPadding] * style->effectiveZoom();
1369     if (style->appearance() == MenulistButtonPart)
1370         return styledPopupPaddingLeft * style->effectiveZoom();
1371     return 0;
1372 }
1373
1374 int RenderThemeMac::popupInternalPaddingRight(RenderStyle* style) const
1375 {
1376     if (style->appearance() == MenulistPart)
1377         return popupButtonPadding(controlSizeForFont(style))[rightPadding] * style->effectiveZoom();
1378     if (style->appearance() == MenulistButtonPart) {
1379         float fontScale = style->fontSize() / baseFontSize;
1380         float arrowWidth = baseArrowWidth * fontScale;
1381         return static_cast<int>(ceilf(arrowWidth + (arrowPaddingLeft + arrowPaddingRight + paddingBeforeSeparator) * style->effectiveZoom()));
1382     }
1383     return 0;
1384 }
1385
1386 int RenderThemeMac::popupInternalPaddingTop(RenderStyle* style) const
1387 {
1388     if (style->appearance() == MenulistPart)
1389         return popupButtonPadding(controlSizeForFont(style))[topPadding] * style->effectiveZoom();
1390     if (style->appearance() == MenulistButtonPart)
1391         return styledPopupPaddingTop * style->effectiveZoom();
1392     return 0;
1393 }
1394
1395 int RenderThemeMac::popupInternalPaddingBottom(RenderStyle* style) const
1396 {
1397     if (style->appearance() == MenulistPart)
1398         return popupButtonPadding(controlSizeForFont(style))[bottomPadding] * style->effectiveZoom();
1399     if (style->appearance() == MenulistButtonPart)
1400         return styledPopupPaddingBottom * style->effectiveZoom();
1401     return 0;
1402 }
1403
1404 PopupMenuStyle::PopupMenuSize RenderThemeMac::popupMenuSize(const RenderStyle* style, IntRect& rect) const
1405 {
1406     NSPopUpButtonCell* popupButton = this->popupButton();
1407     NSControlSize size = controlSizeForCell(popupButton, popupButtonSizes(), rect.size(), style->effectiveZoom());
1408     switch (size) {
1409     case NSRegularControlSize:
1410         return PopupMenuStyle::PopupMenuSizeNormal;
1411     case NSSmallControlSize:
1412         return PopupMenuStyle::PopupMenuSizeSmall;
1413     case NSMiniControlSize:
1414         return PopupMenuStyle::PopupMenuSizeMini;
1415     default:
1416         return PopupMenuStyle::PopupMenuSizeNormal;
1417     }
1418 }
1419
1420 void RenderThemeMac::adjustMenuListButtonStyle(StyleResolver*, RenderStyle* style, Element*) const
1421 {
1422     float fontScale = style->fontSize() / baseFontSize;
1423
1424     style->resetPadding();
1425     style->setBorderRadius(IntSize(int(baseBorderRadius + fontScale - 1), int(baseBorderRadius + fontScale - 1))); // FIXME: Round up?
1426
1427     const int minHeight = 15;
1428     style->setMinHeight(Length(minHeight, Fixed));
1429
1430     style->setLineHeight(RenderStyle::initialLineHeight());
1431 }
1432
1433 void RenderThemeMac::setPopupButtonCellState(const RenderObject& o, const IntRect& r)
1434 {
1435     NSPopUpButtonCell* popupButton = this->popupButton();
1436
1437     // Set the control size based off the rectangle we're painting into.
1438     setControlSize(popupButton, popupButtonSizes(), r.size(), o.style().effectiveZoom());
1439
1440     // Update the various states we respond to.
1441     updateActiveState(popupButton, o);
1442     updateCheckedState(popupButton, o);
1443     updateEnabledState(popupButton, o);
1444     updatePressedState(popupButton, o);
1445 }
1446
1447 const IntSize* RenderThemeMac::menuListSizes() const
1448 {
1449     static const IntSize sizes[3] = { IntSize(9, 0), IntSize(5, 0), IntSize(0, 0) };
1450     return sizes;
1451 }
1452
1453 int RenderThemeMac::minimumMenuListSize(RenderStyle* style) const
1454 {
1455     return sizeForSystemFont(style, menuListSizes()).width();
1456 }
1457
1458 const int trackWidth = 5;
1459 const int trackRadius = 2;
1460
1461 void RenderThemeMac::adjustSliderTrackStyle(StyleResolver*, RenderStyle* style, Element*) const
1462 {
1463     style->setBoxShadow(nullptr);
1464 }
1465
1466 bool RenderThemeMac::paintSliderTrack(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1467 {
1468     IntRect bounds = r;
1469     float zoomLevel = o.style().effectiveZoom();
1470     float zoomedTrackWidth = trackWidth * zoomLevel;
1471
1472     if (o.style().appearance() ==  SliderHorizontalPart || o.style().appearance() ==  MediaSliderPart) {
1473         bounds.setHeight(zoomedTrackWidth);
1474         bounds.setY(r.y() + r.height() / 2 - zoomedTrackWidth / 2);
1475     } else if (o.style().appearance() == SliderVerticalPart) {
1476         bounds.setWidth(zoomedTrackWidth);
1477         bounds.setX(r.x() + r.width() / 2 - zoomedTrackWidth / 2);
1478     }
1479
1480     LocalCurrentGraphicsContext localContext(paintInfo.context);
1481     CGContextRef context = localContext.cgContext();
1482     CGColorSpaceRef cspace = deviceRGBColorSpaceRef();
1483
1484 #if ENABLE(DATALIST_ELEMENT)
1485     paintSliderTicks(o, paintInfo, r);
1486 #endif
1487
1488     GraphicsContextStateSaver stateSaver(*paintInfo.context);
1489     CGContextClipToRect(context, bounds);
1490
1491     struct CGFunctionCallbacks mainCallbacks = { 0, TrackGradientInterpolate, NULL };
1492     RetainPtr<CGFunctionRef> mainFunction = adoptCF(CGFunctionCreate(NULL, 1, NULL, 4, NULL, &mainCallbacks));
1493     RetainPtr<CGShadingRef> mainShading;
1494     if (o.style().appearance() == SliderVerticalPart)
1495         mainShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(bounds.x(),  bounds.maxY()), CGPointMake(bounds.maxX(), bounds.maxY()), mainFunction.get(), false, false));
1496     else
1497         mainShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(bounds.x(),  bounds.y()), CGPointMake(bounds.x(), bounds.maxY()), mainFunction.get(), false, false));
1498
1499     IntSize radius(trackRadius, trackRadius);
1500     paintInfo.context->clipRoundedRect(FloatRoundedRect(bounds, radius, radius, radius, radius));
1501     context = localContext.cgContext();
1502     CGContextDrawShading(context, mainShading.get());
1503
1504     return false;
1505 }
1506
1507 void RenderThemeMac::adjustSliderThumbStyle(StyleResolver* styleResolver, RenderStyle* style, Element* element) const
1508 {
1509     RenderTheme::adjustSliderThumbStyle(styleResolver, style, element);
1510     style->setBoxShadow(nullptr);
1511 }
1512
1513 const float verticalSliderHeightPadding = 0.1f;
1514
1515 bool RenderThemeMac::paintSliderThumb(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1516 {
1517     NSSliderCell* sliderThumbCell = o.style().appearance() == SliderThumbVerticalPart
1518         ? sliderThumbVertical()
1519         : sliderThumbHorizontal();
1520
1521     LocalCurrentGraphicsContext localContext(paintInfo.context);
1522
1523     // Update the various states we respond to.
1524     updateActiveState(sliderThumbCell, o);
1525     updateEnabledState(sliderThumbCell, o);
1526         Element* focusDelegate = (o.node() && o.node()->isElementNode()) ? toElement(o.node())->focusDelegate() : 0;
1527     if (focusDelegate)
1528         updateFocusedState(sliderThumbCell, *focusDelegate->renderer());
1529
1530     // Update the pressed state using the NSCell tracking methods, since that's how NSSliderCell keeps track of it.
1531     bool oldPressed;
1532     if (o.style().appearance() == SliderThumbVerticalPart)
1533         oldPressed = m_isSliderThumbVerticalPressed;
1534     else
1535         oldPressed = m_isSliderThumbHorizontalPressed;
1536
1537     bool pressed = isPressed(o);
1538
1539     if (o.style().appearance() == SliderThumbVerticalPart)
1540         m_isSliderThumbVerticalPressed = pressed;
1541     else
1542         m_isSliderThumbHorizontalPressed = pressed;
1543
1544     if (pressed != oldPressed) {
1545         if (pressed)
1546             [sliderThumbCell startTrackingAt:NSPoint() inView:nil];
1547         else
1548             [sliderThumbCell stopTracking:NSPoint() at:NSPoint() inView:nil mouseIsUp:YES];
1549     }
1550
1551     FloatRect bounds = r;
1552     // Make the height of the vertical slider slightly larger so NSSliderCell will draw a vertical slider.
1553     if (o.style().appearance() == SliderThumbVerticalPart)
1554         bounds.setHeight(bounds.height() + verticalSliderHeightPadding * o.style().effectiveZoom());
1555
1556     GraphicsContextStateSaver stateSaver(*paintInfo.context);
1557     float zoomLevel = o.style().effectiveZoom();
1558
1559     FloatRect unzoomedRect = bounds;
1560     if (zoomLevel != 1.0f) {
1561         unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
1562         unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
1563         paintInfo.context->translate(unzoomedRect.x(), unzoomedRect.y());
1564         paintInfo.context->scale(FloatSize(zoomLevel, zoomLevel));
1565         paintInfo.context->translate(-unzoomedRect.x(), -unzoomedRect.y());
1566     }
1567
1568     [sliderThumbCell drawInteriorWithFrame:unzoomedRect inView:documentViewFor(o)];
1569     [sliderThumbCell setControlView:nil];
1570
1571     return false;
1572 }
1573
1574 bool RenderThemeMac::paintSearchField(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1575 {
1576     LocalCurrentGraphicsContext localContext(paintInfo.context);
1577     NSSearchFieldCell* search = this->search();
1578
1579     setSearchCellState(o, r);
1580
1581     GraphicsContextStateSaver stateSaver(*paintInfo.context);
1582
1583     float zoomLevel = o.style().effectiveZoom();
1584
1585     IntRect unzoomedRect = r;
1586
1587     if (zoomLevel != 1.0f) {
1588         unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
1589         unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
1590         paintInfo.context->translate(unzoomedRect.x(), unzoomedRect.y());
1591         paintInfo.context->scale(FloatSize(zoomLevel, zoomLevel));
1592         paintInfo.context->translate(-unzoomedRect.x(), -unzoomedRect.y());
1593     }
1594
1595     // Set the search button to nil before drawing.  Then reset it so we can draw it later.
1596     [search setSearchButtonCell:nil];
1597
1598     [search drawWithFrame:NSRect(unzoomedRect) inView:documentViewFor(o)];
1599
1600     [search setControlView:nil];
1601     [search resetSearchButtonCell];
1602
1603     return false;
1604 }
1605
1606 void RenderThemeMac::setSearchCellState(const RenderObject& o, const IntRect&)
1607 {
1608     NSSearchFieldCell* search = this->search();
1609
1610     [search setControlSize:controlSizeForFont(&o.style())];
1611
1612     // Update the various states we respond to.
1613     updateActiveState(search, o);
1614     updateEnabledState(search, o);
1615     updateFocusedState(search, o);
1616 }
1617
1618 const IntSize* RenderThemeMac::searchFieldSizes() const
1619 {
1620     static const IntSize sizes[3] = { IntSize(0, 22), IntSize(0, 19), IntSize(0, 17) };
1621     return sizes;
1622 }
1623
1624 void RenderThemeMac::setSearchFieldSize(RenderStyle* style) const
1625 {
1626     // If the width and height are both specified, then we have nothing to do.
1627     if (!style->width().isIntrinsicOrAuto() && !style->height().isAuto())
1628         return;
1629
1630     // Use the font size to determine the intrinsic width of the control.
1631     setSizeFromFont(style, searchFieldSizes());
1632 }
1633
1634 void RenderThemeMac::adjustSearchFieldStyle(StyleResolver* styleResolver, RenderStyle* style, Element*) const
1635 {
1636     // Override border.
1637     style->resetBorder();
1638     const short borderWidth = 2 * style->effectiveZoom();
1639     style->setBorderLeftWidth(borderWidth);
1640     style->setBorderLeftStyle(INSET);
1641     style->setBorderRightWidth(borderWidth);
1642     style->setBorderRightStyle(INSET);
1643     style->setBorderBottomWidth(borderWidth);
1644     style->setBorderBottomStyle(INSET);
1645     style->setBorderTopWidth(borderWidth);
1646     style->setBorderTopStyle(INSET);
1647
1648     // Override height.
1649     style->setHeight(Length(Auto));
1650     setSearchFieldSize(style);
1651
1652     // Override padding size to match AppKit text positioning.
1653     const int padding = 1 * style->effectiveZoom();
1654     style->setPaddingLeft(Length(padding, Fixed));
1655     style->setPaddingRight(Length(padding, Fixed));
1656     style->setPaddingTop(Length(padding, Fixed));
1657     style->setPaddingBottom(Length(padding, Fixed));
1658
1659     NSControlSize controlSize = controlSizeForFont(style);
1660     setFontFromControlSize(styleResolver, style, controlSize);
1661
1662     style->setBoxShadow(nullptr);
1663 }
1664
1665 bool RenderThemeMac::paintSearchFieldCancelButton(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1666 {
1667     Element* input = o.node()->shadowHost();
1668     if (!input)
1669         input = toElement(o.node());
1670
1671     if (!input->renderer()->isBox())
1672         return false;
1673
1674     LocalCurrentGraphicsContext localContext(paintInfo.context);
1675     setSearchCellState(*input->renderer(), r);
1676
1677     NSSearchFieldCell* search = this->search();
1678
1679     if (!input->isDisabledFormControl() && (input->isTextFormControl() && !toHTMLTextFormControlElement(input)->isReadOnly())) {
1680         updateActiveState([search cancelButtonCell], o);
1681         updatePressedState([search cancelButtonCell], o);
1682     }
1683     else if ([[search cancelButtonCell] isHighlighted])
1684         [[search cancelButtonCell] setHighlighted:NO];
1685
1686     GraphicsContextStateSaver stateSaver(*paintInfo.context);
1687
1688     float zoomLevel = o.style().effectiveZoom();
1689
1690     FloatRect localBounds = [search cancelButtonRectForBounds:NSRect(input->renderBox()->pixelSnappedBorderBoxRect())];
1691
1692 #if ENABLE(INPUT_SPEECH)
1693     // Take care of cases where the cancel button was not aligned with the right border of the input element (for e.g.
1694     // when speech input is enabled for the input element.
1695     IntRect absBoundingBox = input->renderer()->absoluteBoundingBoxRect();
1696     int absRight = absBoundingBox.x() + absBoundingBox.width() - input->renderBox()->paddingRight() - input->renderBox()->borderRight();
1697     int spaceToRightOfCancelButton = absRight - (r.x() + r.width());
1698     localBounds.setX(localBounds.x() - spaceToRightOfCancelButton);
1699 #endif
1700
1701     localBounds = convertToPaintingRect(*input->renderer(), o, localBounds, r);
1702
1703     FloatRect unzoomedRect(localBounds);
1704     if (zoomLevel != 1.0f) {
1705         unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
1706         unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
1707         paintInfo.context->translate(unzoomedRect.x(), unzoomedRect.y());
1708         paintInfo.context->scale(FloatSize(zoomLevel, zoomLevel));
1709         paintInfo.context->translate(-unzoomedRect.x(), -unzoomedRect.y());
1710     }
1711
1712     [[search cancelButtonCell] drawWithFrame:unzoomedRect inView:documentViewFor(o)];
1713     [[search cancelButtonCell] setControlView:nil];
1714     return false;
1715 }
1716
1717 const IntSize* RenderThemeMac::cancelButtonSizes() const
1718 {
1719     static const IntSize sizes[3] = { IntSize(16, 13), IntSize(13, 11), IntSize(13, 9) };
1720     return sizes;
1721 }
1722
1723 void RenderThemeMac::adjustSearchFieldCancelButtonStyle(StyleResolver*, RenderStyle* style, Element*) const
1724 {
1725     IntSize size = sizeForSystemFont(style, cancelButtonSizes());
1726     style->setWidth(Length(size.width(), Fixed));
1727     style->setHeight(Length(size.height(), Fixed));
1728     style->setBoxShadow(nullptr);
1729 }
1730
1731 const IntSize* RenderThemeMac::resultsButtonSizes() const
1732 {
1733     static const IntSize sizes[3] = { IntSize(19, 13), IntSize(17, 11), IntSize(17, 9) };
1734     return sizes;
1735 }
1736
1737 const int emptyResultsOffset = 9;
1738 void RenderThemeMac::adjustSearchFieldDecorationPartStyle(StyleResolver*, RenderStyle* style, Element*) const
1739 {
1740     IntSize size = sizeForSystemFont(style, resultsButtonSizes());
1741     style->setWidth(Length(size.width() - emptyResultsOffset, Fixed));
1742     style->setHeight(Length(size.height(), Fixed));
1743     style->setBoxShadow(nullptr);
1744 }
1745
1746 bool RenderThemeMac::paintSearchFieldDecorationPart(const RenderObject&, const PaintInfo&, const IntRect&)
1747 {
1748     return false;
1749 }
1750
1751 void RenderThemeMac::adjustSearchFieldResultsDecorationPartStyle(StyleResolver*, RenderStyle* style, Element*) const
1752 {
1753     IntSize size = sizeForSystemFont(style, resultsButtonSizes());
1754     style->setWidth(Length(size.width(), Fixed));
1755     style->setHeight(Length(size.height(), Fixed));
1756     style->setBoxShadow(nullptr);
1757 }
1758
1759 bool RenderThemeMac::paintSearchFieldResultsDecorationPart(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1760 {
1761     Node* input = o.node()->shadowHost();
1762     if (!input)
1763         input = o.node();
1764     if (!input->renderer()->isBox())
1765         return false;
1766
1767     LocalCurrentGraphicsContext localContext(paintInfo.context);
1768     setSearchCellState(*input->renderer(), r);
1769
1770     NSSearchFieldCell* search = this->search();
1771
1772     if ([search searchMenuTemplate] != nil)
1773         [search setSearchMenuTemplate:nil];
1774
1775     updateActiveState([search searchButtonCell], o);
1776
1777     FloatRect localBounds = [search searchButtonRectForBounds:NSRect(input->renderBox()->pixelSnappedBorderBoxRect())];
1778     localBounds = convertToPaintingRect(*input->renderer(), o, localBounds, r);
1779
1780     [[search searchButtonCell] drawWithFrame:localBounds inView:documentViewFor(o)];
1781     [[search searchButtonCell] setControlView:nil];
1782     return false;
1783 }
1784
1785 const int resultsArrowWidth = 5;
1786 void RenderThemeMac::adjustSearchFieldResultsButtonStyle(StyleResolver*, RenderStyle* style, Element*) const
1787 {
1788     IntSize size = sizeForSystemFont(style, resultsButtonSizes());
1789     style->setWidth(Length(size.width() + resultsArrowWidth, Fixed));
1790     style->setHeight(Length(size.height(), Fixed));
1791     style->setBoxShadow(nullptr);
1792 }
1793
1794 bool RenderThemeMac::paintSearchFieldResultsButton(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1795 {
1796     Node* input = o.node()->shadowHost();
1797     if (!input)
1798         input = o.node();
1799     if (!input->renderer()->isBox())
1800         return false;
1801
1802     LocalCurrentGraphicsContext localContext(paintInfo.context);
1803     setSearchCellState(*input->renderer(), r);
1804
1805     NSSearchFieldCell* search = this->search();
1806
1807     updateActiveState([search searchButtonCell], o);
1808
1809     if (![search searchMenuTemplate])
1810         [search setSearchMenuTemplate:searchMenuTemplate()];
1811
1812     GraphicsContextStateSaver stateSaver(*paintInfo.context);
1813     float zoomLevel = o.style().effectiveZoom();
1814
1815     FloatRect localBounds = [search searchButtonRectForBounds:NSRect(input->renderBox()->pixelSnappedBorderBoxRect())];
1816     localBounds = convertToPaintingRect(*input->renderer(), o, localBounds, r);
1817
1818     IntRect unzoomedRect(localBounds);
1819     if (zoomLevel != 1.0f) {
1820         unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
1821         unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
1822         paintInfo.context->translate(unzoomedRect.x(), unzoomedRect.y());
1823         paintInfo.context->scale(FloatSize(zoomLevel, zoomLevel));
1824         paintInfo.context->translate(-unzoomedRect.x(), -unzoomedRect.y());
1825     }
1826
1827     [[search searchButtonCell] drawWithFrame:unzoomedRect inView:documentViewFor(o)];
1828     [[search searchButtonCell] setControlView:nil];
1829
1830     return false;
1831 }
1832
1833 bool RenderThemeMac::paintSnapshottedPluginOverlay(const RenderObject& o, const PaintInfo& paintInfo, const IntRect&)
1834 {
1835     if (paintInfo.phase != PaintPhaseBlockBackground)
1836         return true;
1837
1838     if (!o.isRenderBlock())
1839         return true;
1840
1841     const RenderBlock& renderBlock = *toRenderBlock(&o);
1842
1843     LayoutUnit contentWidth = renderBlock.contentWidth();
1844     LayoutUnit contentHeight = renderBlock.contentHeight();
1845     if (!contentWidth || !contentHeight)
1846         return true;
1847
1848     GraphicsContext* context = paintInfo.context;
1849
1850     LayoutSize contentSize(contentWidth, contentHeight);
1851     LayoutPoint contentLocation = renderBlock.location();
1852     contentLocation.move(renderBlock.borderLeft() + renderBlock.paddingLeft(), renderBlock.borderTop() + renderBlock.paddingTop());
1853
1854     LayoutRect rect(contentLocation, contentSize);
1855     IntRect alignedRect = pixelSnappedIntRect(rect);
1856     if (alignedRect.width() <= 0 || alignedRect.height() <= 0)
1857         return true;
1858
1859     // We need to get the snapshot image from the plugin element, which should be available
1860     // from our node. Assuming this node is the plugin overlay element, we should get to the
1861     // plugin itself by asking for the shadow root parent, and then its parent.
1862
1863     if (!renderBlock.element()->isHTMLElement())
1864         return true;
1865
1866     HTMLElement* plugInOverlay = toHTMLElement(renderBlock.element());
1867     Element* parent = plugInOverlay->parentOrShadowHostElement();
1868     while (parent && !parent->isPluginElement())
1869         parent = parent->parentOrShadowHostElement();
1870
1871     if (!parent)
1872         return true;
1873
1874     HTMLPlugInElement* plugInElement = toHTMLPlugInElement(parent);
1875     if (!plugInElement->isPlugInImageElement())
1876         return true;
1877
1878     HTMLPlugInImageElement* plugInImageElement = toHTMLPlugInImageElement(plugInElement);
1879
1880     Image* snapshot = plugInImageElement->snapshotImage();
1881     if (!snapshot)
1882         return true;
1883
1884     RenderSnapshottedPlugIn* plugInRenderer = toRenderSnapshottedPlugIn(plugInImageElement->renderer());
1885     FloatPoint snapshotAbsPos = plugInRenderer->localToAbsolute();
1886     snapshotAbsPos.move(plugInRenderer->borderLeft() + plugInRenderer->paddingLeft(), plugInRenderer->borderTop() + plugInRenderer->paddingTop());
1887
1888     // We could draw the snapshot with that coordinates, but we need to make sure there
1889     // isn't a composited layer between us and the plugInRenderer.
1890     const RenderBox* renderBox = toRenderBox(&o);
1891     while (renderBox != plugInRenderer) {
1892         if (renderBox->hasLayer() && renderBox->layer() && renderBox->layer()->isComposited()) {
1893             snapshotAbsPos = -renderBox->location();
1894             break;
1895         }
1896         renderBox = renderBox->parentBox();
1897     }
1898
1899     LayoutSize pluginSize(plugInRenderer->contentWidth(), plugInRenderer->contentHeight());
1900     LayoutRect pluginRect(snapshotAbsPos, pluginSize);
1901     IntRect alignedPluginRect = pixelSnappedIntRect(pluginRect);
1902
1903     if (alignedPluginRect.width() <= 0 || alignedPluginRect.height() <= 0)
1904         return true;
1905
1906     context->drawImage(snapshot, plugInRenderer->style().colorSpace(), alignedPluginRect, CompositeSourceOver);
1907     return false;
1908 }
1909
1910 #if ENABLE(DATALIST_ELEMENT)
1911 IntSize RenderThemeMac::sliderTickSize() const
1912 {
1913     return IntSize(1, 3);
1914 }
1915
1916 int RenderThemeMac::sliderTickOffsetFromTrackCenter() const
1917 {
1918     return -9;
1919 }
1920 #endif
1921
1922 const int sliderThumbWidth = 15;
1923 const int sliderThumbHeight = 15;
1924
1925 void RenderThemeMac::adjustSliderThumbSize(RenderStyle* style, Element*) const
1926 {
1927     float zoomLevel = style->effectiveZoom();
1928     if (style->appearance() == SliderThumbHorizontalPart || style->appearance() == SliderThumbVerticalPart) {
1929         style->setWidth(Length(static_cast<int>(sliderThumbWidth * zoomLevel), Fixed));
1930         style->setHeight(Length(static_cast<int>(sliderThumbHeight * zoomLevel), Fixed));
1931     }
1932 }
1933
1934 bool RenderThemeMac::shouldShowPlaceholderWhenFocused() const
1935 {
1936     return true;
1937 }
1938
1939 NSPopUpButtonCell* RenderThemeMac::popupButton() const
1940 {
1941     if (!m_popupButton) {
1942         m_popupButton = adoptNS([[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO]);
1943         [m_popupButton.get() setUsesItemFromMenu:NO];
1944         [m_popupButton.get() setFocusRingType:NSFocusRingTypeExterior];
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     }
1959
1960     return m_search.get();
1961 }
1962
1963 NSMenu* RenderThemeMac::searchMenuTemplate() const
1964 {
1965     if (!m_searchMenuTemplate)
1966         m_searchMenuTemplate = adoptNS([[NSMenu alloc] initWithTitle:@""]);
1967
1968     return m_searchMenuTemplate.get();
1969 }
1970
1971 NSSliderCell* RenderThemeMac::sliderThumbHorizontal() const
1972 {
1973     if (!m_sliderThumbHorizontal) {
1974         m_sliderThumbHorizontal = adoptNS([[NSSliderCell alloc] init]);
1975         [m_sliderThumbHorizontal.get() setSliderType:NSLinearSlider];
1976         [m_sliderThumbHorizontal.get() setControlSize:NSSmallControlSize];
1977         [m_sliderThumbHorizontal.get() setFocusRingType:NSFocusRingTypeExterior];
1978     }
1979
1980     return m_sliderThumbHorizontal.get();
1981 }
1982
1983 NSSliderCell* RenderThemeMac::sliderThumbVertical() const
1984 {
1985     if (!m_sliderThumbVertical) {
1986         m_sliderThumbVertical = adoptNS([[NSSliderCell alloc] init]);
1987         [m_sliderThumbVertical.get() setSliderType:NSLinearSlider];
1988         [m_sliderThumbVertical.get() setControlSize:NSSmallControlSize];
1989         [m_sliderThumbVertical.get() setFocusRingType:NSFocusRingTypeExterior];
1990     }
1991
1992     return m_sliderThumbVertical.get();
1993 }
1994
1995 NSTextFieldCell* RenderThemeMac::textField() const
1996 {
1997     if (!m_textField) {
1998         m_textField = adoptNS([[WebCoreTextFieldCell alloc] initTextCell:@""]);
1999         [m_textField.get() setBezeled:YES];
2000         [m_textField.get() setEditable:YES];
2001         [m_textField.get() setFocusRingType:NSFocusRingTypeExterior];
2002         // Post-Lion, WebCore can be in charge of paintinng the background thanks to
2003         // the workaround in place for <rdar://problem/11385461>, which is implemented
2004         // above as _coreUIDrawOptionsWithFrame.
2005         [m_textField.get() setDrawsBackground:NO];
2006     }
2007
2008     return m_textField.get();
2009 }
2010
2011 String RenderThemeMac::fileListNameForWidth(const FileList* fileList, const Font& font, int width, bool multipleFilesAllowed) const
2012 {
2013     if (width <= 0)
2014         return String();
2015
2016     String strToTruncate;
2017     if (fileList->isEmpty())
2018         strToTruncate = fileListDefaultLabel(multipleFilesAllowed);
2019     else if (fileList->length() == 1)
2020         strToTruncate = [[NSFileManager defaultManager] displayNameAtPath:(fileList->item(0)->path())];
2021     else
2022         return StringTruncator::rightTruncate(multipleFileUploadText(fileList->length()), width, font, StringTruncator::EnableRoundingHacks);
2023
2024     return StringTruncator::centerTruncate(strToTruncate, width, font, StringTruncator::EnableRoundingHacks);
2025 }
2026
2027 #if ENABLE(SERVICE_CONTROLS)
2028 NSServicesRolloverButtonCell* RenderThemeMac::servicesRolloverButtonCell() const
2029 {
2030 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2031     if (!m_servicesRolloverButton) {
2032         m_servicesRolloverButton = [NSServicesRolloverButtonCell serviceRolloverButtonCellForStyle:NSSharingServicePickerStyleRollover];
2033         [m_servicesRolloverButton setBordered:NO];
2034     }
2035
2036     return m_servicesRolloverButton.get();
2037 #else
2038     return nil;
2039 #endif
2040 }
2041
2042 bool RenderThemeMac::paintImageControlsButton(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect& rect)
2043 {
2044     if (paintInfo.phase != PaintPhaseBlockBackground)
2045         return true;
2046
2047 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2048     NSServicesRolloverButtonCell *cell = servicesRolloverButtonCell();
2049
2050     LocalCurrentGraphicsContext localContext(paintInfo.context);
2051     GraphicsContextStateSaver stateSaver(*paintInfo.context);
2052
2053     paintInfo.context->translate(rect.x(), rect.y());
2054
2055     IntRect innerFrame(IntPoint(), rect.size());
2056     [cell drawWithFrame:innerFrame inView:documentViewFor(renderer)];
2057     [cell setControlView:nil];
2058 #else
2059     UNUSED_PARAM(renderer);
2060     UNUSED_PARAM(rect);
2061 #endif
2062
2063     return true;
2064 }
2065
2066 IntSize RenderThemeMac::imageControlsButtonSize(const RenderObject&) const
2067 {
2068 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2069     return IntSize(servicesRolloverButtonCell().cellSize);
2070 #else
2071     return IntSize();
2072 #endif
2073 }
2074 #endif
2075
2076 } // namespace WebCore
2077
2078 #endif // !PLATFORM(IOS)