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