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