dfb5f690a0436c0999b73d369dc81d6f83c1c74c
[WebKit-https.git] / Source / WebCore / platform / mac / ThemeMac.mm
1 /*
2  * Copyright (C) 2008, 2010, 2011, 2012 Apple Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #import "config.h"
27 #import "ThemeMac.h"
28
29 #import "BlockExceptions.h"
30 #import "GraphicsContext.h"
31 #import "LocalCurrentGraphicsContext.h"
32 #import "ScrollView.h"
33 #import "WebCoreSystemInterface.h"
34 #import <Carbon/Carbon.h>
35 #include <wtf/StdLibExtras.h>
36
37 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 10100
38 @interface NSButtonCell(Details)
39 - (void)_setState:(NSInteger)value animated:(BOOL)animated;
40 - (void)_setHighlighted:(BOOL)flag animated:(BOOL)animated;
41 - (void)_renderCurrentAnimationFrameInContext:(CGContextRef)ctxt atLocation:(NSPoint)where;
42 - (BOOL)_stateAnimationRunning;
43 @end
44 #endif
45
46 static NSRect focusRingClipRect;
47 static BOOL themeWindowHasKeyAppearance;
48
49 @interface WebCoreThemeWindow : NSWindow {
50 }
51
52 @end
53
54 @implementation WebCoreThemeWindow
55
56 - (BOOL)hasKeyAppearance
57 {
58     return themeWindowHasKeyAppearance;
59 }
60
61 @end
62
63 @interface WebCoreThemeView : NSControl
64 @end
65
66 @implementation WebCoreThemeView
67
68 - (NSWindow *)window
69 {
70     static WebCoreThemeWindow *window = [[WebCoreThemeWindow alloc] init];
71
72     return window;
73 }
74
75 - (BOOL)isFlipped
76 {
77     return YES;
78 }
79
80 - (NSText *)currentEditor
81 {
82     return nil;
83 }
84
85 - (BOOL)_automaticFocusRingDisabled
86 {
87     return YES;
88 }
89
90 - (NSRect)_focusRingVisibleRect
91 {
92     if (NSIsEmptyRect(focusRingClipRect))
93         return [self visibleRect];
94
95     NSRect rect = focusRingClipRect;
96 #if __MAC_OS_X_VERSION_MIN_REQUIRED < 1090
97     rect.origin.y = [self bounds].size.height - NSMaxY(rect);
98 #endif
99
100     return rect;
101 }
102
103 - (NSView *)_focusRingClipAncestor
104 {
105     return self;
106 }
107
108 - (void)addSubview:(NSView*)subview
109 {
110     // By doing nothing in this method we forbid controls from adding subviews.
111     // This tells AppKit to not use Layer-backed animation for control rendering.
112     UNUSED_PARAM(subview);
113 }
114 @end
115
116 @implementation NSFont (WebCoreTheme)
117
118 - (NSString*)webCoreFamilyName
119 {
120     if ([[self familyName] hasPrefix:@"."])
121         return [self fontName];
122
123     return [self familyName];
124 }
125
126 @end
127
128 // FIXME: Default buttons really should be more like push buttons and not like buttons.
129
130 namespace WebCore {
131
132 enum {
133     topMargin,
134     rightMargin,
135     bottomMargin,
136     leftMargin
137 };
138
139 Theme* platformTheme()
140 {
141     DEPRECATED_DEFINE_STATIC_LOCAL(ThemeMac, themeMac, ());
142     return &themeMac;
143 }
144
145 // Helper functions used by a bunch of different control parts.
146
147 static NSControlSize controlSizeForFont(const Font& font)
148 {
149     int fontSize = font.pixelSize();
150     if (fontSize >= 16)
151         return NSRegularControlSize;
152     if (fontSize >= 11)
153         return NSSmallControlSize;
154     return NSMiniControlSize;
155 }
156
157 static LengthSize sizeFromNSControlSize(NSControlSize nsControlSize, const LengthSize& zoomedSize, float zoomFactor, const std::array<IntSize, 3>& sizes)
158 {
159     IntSize controlSize = sizes[nsControlSize];
160     if (zoomFactor != 1.0f)
161         controlSize = IntSize(controlSize.width() * zoomFactor, controlSize.height() * zoomFactor);
162     LengthSize result = zoomedSize;
163     if (zoomedSize.width().isIntrinsicOrAuto() && controlSize.width() > 0)
164         result.setWidth(Length(controlSize.width(), Fixed));
165     if (zoomedSize.height().isIntrinsicOrAuto() && controlSize.height() > 0)
166         result.setHeight(Length(controlSize.height(), Fixed));
167     return result;
168 }
169
170 static LengthSize sizeFromFont(const Font& font, const LengthSize& zoomedSize, float zoomFactor, const std::array<IntSize, 3>& sizes)
171 {
172     return sizeFromNSControlSize(controlSizeForFont(font), zoomedSize, zoomFactor, sizes);
173 }
174
175 static ControlSize controlSizeFromPixelSize(const std::array<IntSize, 3>& sizes, const IntSize& minZoomedSize, float zoomFactor)
176 {
177     if (minZoomedSize.width() >= static_cast<int>(sizes[NSRegularControlSize].width() * zoomFactor) &&
178         minZoomedSize.height() >= static_cast<int>(sizes[NSRegularControlSize].height() * zoomFactor))
179         return NSRegularControlSize;
180     if (minZoomedSize.width() >= static_cast<int>(sizes[NSSmallControlSize].width() * zoomFactor) &&
181         minZoomedSize.height() >= static_cast<int>(sizes[NSSmallControlSize].height() * zoomFactor))
182         return NSSmallControlSize;
183     return NSMiniControlSize;
184 }
185
186 static void setControlSize(NSCell* cell, const std::array<IntSize, 3>& sizes, const IntSize& minZoomedSize, float zoomFactor)
187 {
188     ControlSize size = controlSizeFromPixelSize(sizes, minZoomedSize, zoomFactor);
189     if (size != [cell controlSize]) // Only update if we have to, since AppKit does work even if the size is the same.
190         [cell setControlSize:(NSControlSize)size];
191 }
192
193 static void updateStates(NSCell* cell, const ControlStates* controlStates, bool useAnimation = false)
194 {
195 #if __MAC_OS_X_VERSION_MIN_REQUIRED < 10100
196     UNUSED_PARAM(useAnimation);
197 #endif
198     ControlStates::States states = controlStates->states();
199
200     // Hover state is not supported by Aqua.
201     
202     // Pressed state
203     bool oldPressed = [cell isHighlighted];
204     bool pressed = states & ControlStates::PressedState;
205     if (pressed != oldPressed) {
206 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 10100
207         [(NSButtonCell*)cell _setHighlighted:pressed animated:useAnimation];
208 #else
209         [cell setHighlighted:pressed];
210 #endif
211     }
212     
213     // Enabled state
214     bool oldEnabled = [cell isEnabled];
215     bool enabled = states & ControlStates::EnabledState;
216     if (enabled != oldEnabled)
217         [cell setEnabled:enabled];
218
219     // Checked and Indeterminate
220     bool oldIndeterminate = [cell state] == NSMixedState;
221     bool indeterminate = (states & ControlStates::IndeterminateState);
222     bool checked = states & ControlStates::CheckedState;
223     bool oldChecked = [cell state] == NSOnState;
224     if (oldIndeterminate != indeterminate || checked != oldChecked) {
225         NSCellStateValue newState = indeterminate ? NSMixedState : (checked ? NSOnState : NSOffState);
226 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 10100
227         [(NSButtonCell*)cell _setState:newState animated:useAnimation];
228 #else
229         [cell setState:newState];
230 #endif
231     }
232
233     // Window inactive state does not need to be checked explicitly, since we paint parented to 
234     // a view in a window whose key state can be detected.
235 }
236
237 static ThemeDrawState convertControlStatesToThemeDrawState(ThemeButtonKind kind, const ControlStates* controlStates)
238 {
239     ControlStates::States states = controlStates->states();
240
241     if (states & ControlStates::ReadOnlyState)
242         return kThemeStateUnavailableInactive;
243     if (!(states & ControlStates::EnabledState))
244         return kThemeStateUnavailableInactive;
245
246     // Do not process PressedState if !EnabledState or ReadOnlyState.
247     if (states & ControlStates::PressedState) {
248         if (kind == kThemeIncDecButton || kind == kThemeIncDecButtonSmall || kind == kThemeIncDecButtonMini)
249             return states & ControlStates::SpinUpState ? kThemeStatePressedUp : kThemeStatePressedDown;
250         return kThemeStatePressed;
251     }
252     return kThemeStateActive;
253 }
254
255 static FloatRect inflateRect(const FloatRect& zoomedRect, const IntSize& zoomedSize, const int* margins, float zoomFactor)
256 {
257     // Only do the inflation if the available width/height are too small.  Otherwise try to
258     // fit the glow/check space into the available box's width/height.
259     int widthDelta = zoomedRect.width() - (zoomedSize.width() + margins[leftMargin] * zoomFactor + margins[rightMargin] * zoomFactor);
260     int heightDelta = zoomedRect.height() - (zoomedSize.height() + margins[topMargin] * zoomFactor + margins[bottomMargin] * zoomFactor);
261     FloatRect result(zoomedRect);
262     if (widthDelta < 0) {
263         result.setX(result.x() - margins[leftMargin] * zoomFactor);
264         result.setWidth(result.width() - widthDelta);
265     }
266     if (heightDelta < 0) {
267         result.setY(result.y() - margins[topMargin] * zoomFactor);
268         result.setHeight(result.height() - heightDelta);
269     }
270     return result;
271 }
272
273 // Checkboxes and radio buttons
274
275 static const std::array<IntSize, 3>& checkboxSizes()
276 {
277     static const std::array<IntSize, 3> sizes = { { IntSize(14, 14), IntSize(12, 12), IntSize(10, 10) } };
278     return sizes;
279 }
280
281 static const int* checkboxMargins(NSControlSize controlSize)
282 {
283     static const int margins[3][4] =
284     {
285         // top right bottom left
286         { 2, 2, 2, 2 },
287         { 2, 1, 2, 1 },
288         { 0, 0, 1, 0 },
289     };
290     return margins[controlSize];
291 }
292
293 static LengthSize checkboxSize(const Font& font, const LengthSize& zoomedSize, float zoomFactor)
294 {
295     // If the width and height are both specified, then we have nothing to do.
296     if (!zoomedSize.width().isIntrinsicOrAuto() && !zoomedSize.height().isIntrinsicOrAuto())
297         return zoomedSize;
298
299     // Use the font size to determine the intrinsic width of the control.
300     return sizeFromFont(font, zoomedSize, zoomFactor, checkboxSizes());
301 }
302
303 // Radio Buttons
304
305 static const std::array<IntSize, 3>& radioSizes()
306 {
307     static const std::array<IntSize, 3> sizes = { { IntSize(16, 16), IntSize(12, 12), IntSize(10, 10) } };
308     return sizes;
309 }
310
311 static const int* radioMargins(NSControlSize controlSize)
312 {
313     static const int margins[3][4] =
314     {
315         // top right bottom left
316         { 1, 0, 1, 2 },
317         { 1, 1, 2, 1 },
318         { 0, 0, 1, 1 },
319     };
320     return margins[controlSize];
321 }
322
323 static LengthSize radioSize(const Font& font, const LengthSize& zoomedSize, float zoomFactor)
324 {
325     // If the width and height are both specified, then we have nothing to do.
326     if (!zoomedSize.width().isIntrinsicOrAuto() && !zoomedSize.height().isIntrinsicOrAuto())
327         return zoomedSize;
328
329     // Use the font size to determine the intrinsic width of the control.
330     return sizeFromFont(font, zoomedSize, zoomFactor, radioSizes());
331 }
332     
333 static void configureToggleButton(NSCell* cell, ControlPart buttonType, const ControlStates* states, const IntSize& zoomedSize, float zoomFactor, bool isStateChange)
334 {
335     // Set the control size based off the rectangle we're painting into.
336     setControlSize(cell, buttonType == CheckboxPart ? checkboxSizes() : radioSizes(), zoomedSize, zoomFactor);
337
338     // Update the various states we respond to.
339     updateStates(cell, states, isStateChange);
340 }
341     
342 static NSButtonCell *createToggleButtonCell(ControlPart buttonType)
343 {
344     NSButtonCell *toggleButtonCell = [[NSButtonCell alloc] init];
345     
346     if (buttonType == CheckboxPart) {
347         [toggleButtonCell setButtonType:NSSwitchButton];
348         [toggleButtonCell setAllowsMixedState:YES];
349     } else {
350         ASSERT(buttonType == RadioPart);
351         [toggleButtonCell setButtonType:NSRadioButton];
352     }
353     
354     [toggleButtonCell setTitle:nil];
355     [toggleButtonCell setFocusRingType:NSFocusRingTypeExterior];
356     return toggleButtonCell;
357 }
358     
359 static NSButtonCell *sharedRadioCell(const ControlStates* states, const IntSize& zoomedSize, float zoomFactor)
360 {
361     static NSButtonCell *radioCell;
362     if (!radioCell)
363         radioCell = createToggleButtonCell(RadioPart);
364
365     configureToggleButton(radioCell, RadioPart, states, zoomedSize, zoomFactor, false);
366     return radioCell;
367 }
368     
369 static NSButtonCell *sharedCheckboxCell(const ControlStates* states, const IntSize& zoomedSize, float zoomFactor)
370 {
371     static NSButtonCell *checkboxCell;
372     if (!checkboxCell)
373         checkboxCell = createToggleButtonCell(CheckboxPart);
374
375     configureToggleButton(checkboxCell, CheckboxPart, states, zoomedSize, zoomFactor, false);
376     return checkboxCell;
377 }
378
379 static bool drawCellFocusRing(NSCell *cell, NSRect cellFrame, NSView *controlView)
380 {
381     wkDrawCellFocusRingWithFrameAtTime(cell, cellFrame, controlView, std::numeric_limits<double>::max());
382     return false;
383 }
384
385 static void paintToggleButton(ControlPart buttonType, ControlStates* controlStates, GraphicsContext* context, const FloatRect& zoomedRect, float zoomFactor, ScrollView* scrollView)
386 {
387     BEGIN_BLOCK_OBJC_EXCEPTIONS
388
389     NSButtonCell *toggleButtonCell = static_cast<NSButtonCell*>(controlStates->platformControl());
390     IntSize zoomedRectSize = IntSize(zoomedRect.size());
391
392     if (controlStates->isDirty()) {
393         if (!toggleButtonCell)
394             toggleButtonCell = createToggleButtonCell(buttonType);
395         configureToggleButton(toggleButtonCell, buttonType, controlStates, zoomedRectSize, zoomFactor, true);
396     } else {
397         if (!toggleButtonCell) {
398             if (buttonType == CheckboxPart)
399                 toggleButtonCell = sharedCheckboxCell(controlStates, zoomedRectSize, zoomFactor);
400             else {
401                 ASSERT(buttonType == RadioPart);
402                 toggleButtonCell = sharedRadioCell(controlStates, zoomedRectSize, zoomFactor);
403             }
404         }
405         configureToggleButton(toggleButtonCell, buttonType, controlStates, zoomedRectSize, zoomFactor, false);
406     }
407     controlStates->setDirty(false);
408
409     GraphicsContextStateSaver stateSaver(*context);
410
411     NSControlSize controlSize = [toggleButtonCell controlSize];
412     IntSize zoomedSize = buttonType == CheckboxPart ? checkboxSizes()[controlSize] : radioSizes()[controlSize];
413     zoomedSize.setWidth(zoomedSize.width() * zoomFactor);
414     zoomedSize.setHeight(zoomedSize.height() * zoomFactor);
415     const int* controlMargins = buttonType == CheckboxPart ? checkboxMargins(controlSize) : radioMargins(controlSize);
416     FloatRect inflatedRect = inflateRect(zoomedRect, zoomedSize, controlMargins, zoomFactor);
417
418     if (zoomFactor != 1.0f) {
419         inflatedRect.setWidth(inflatedRect.width() / zoomFactor);
420         inflatedRect.setHeight(inflatedRect.height() / zoomFactor);
421         context->translate(inflatedRect.x(), inflatedRect.y());
422         context->scale(FloatSize(zoomFactor, zoomFactor));
423         context->translate(-inflatedRect.x(), -inflatedRect.y());
424     }
425
426     LocalCurrentGraphicsContext localContext(context);
427     NSView *view = ThemeMac::ensuredView(scrollView, controlStates);
428
429     bool isAnimating = false;
430     bool needsRepaint = false;
431
432 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 10100
433     if ([toggleButtonCell _stateAnimationRunning]) {
434         // AppKit's drawWithFrame appears to render the cell centered in the
435         // provided rectangle/frame, so we need to manually position the
436         // animated cell at the correct location.
437         context->translate(inflatedRect.x(), inflatedRect.y());
438         context->scale(FloatSize(1, -1));
439         context->translate(0, -inflatedRect.height());
440         [toggleButtonCell _renderCurrentAnimationFrameInContext:context->platformContext() atLocation:NSMakePoint(0, 0)];
441         isAnimating = [toggleButtonCell _stateAnimationRunning];
442     } else
443         [toggleButtonCell drawWithFrame:NSRect(inflatedRect) inView:view];
444 #else
445     [toggleButtonCell drawWithFrame:NSRect(inflatedRect) inView:view];
446 #endif
447
448     if (!isAnimating && (controlStates->states() & ControlStates::FocusState))
449         needsRepaint = drawCellFocusRing(toggleButtonCell, inflatedRect, view);
450     [toggleButtonCell setControlView:nil];
451
452     needsRepaint |= isAnimating;
453     controlStates->setNeedsRepaint(needsRepaint);
454     if (needsRepaint)
455         controlStates->setPlatformControl(toggleButtonCell);
456
457     END_BLOCK_OBJC_EXCEPTIONS
458 }
459
460 // Buttons
461
462 // Buttons really only constrain height. They respect width.
463 static const std::array<IntSize, 3>& buttonSizes()
464 {
465     static const std::array<IntSize, 3> sizes = { { IntSize(0, 21), IntSize(0, 18), IntSize(0, 15) } };
466     return sizes;
467 }
468
469 static const int* buttonMargins(NSControlSize controlSize)
470 {
471     static const int margins[3][4] =
472     {
473         { 4, 6, 7, 6 },
474         { 4, 5, 6, 5 },
475         { 0, 1, 1, 1 },
476     };
477     return margins[controlSize];
478 }
479
480 enum ButtonCellType { NormalButtonCell, DefaultButtonCell };
481
482 static NSButtonCell *leakButtonCell(ButtonCellType type)
483 {
484     NSButtonCell *cell = [[NSButtonCell alloc] init];
485     [cell setTitle:nil];
486     [cell setButtonType:NSMomentaryPushInButton];
487     if (type == DefaultButtonCell)
488         [cell setKeyEquivalent:@"\r"];
489     return cell;
490 }
491
492 static void setUpButtonCell(NSButtonCell *cell, ControlPart part, const ControlStates* states, const IntSize& zoomedSize, float zoomFactor)
493 {
494     // Set the control size based off the rectangle we're painting into.
495     const std::array<IntSize, 3>& sizes = buttonSizes();
496     if (part == SquareButtonPart || zoomedSize.height() > buttonSizes()[NSRegularControlSize].height() * zoomFactor) {
497         // Use the square button
498         if ([cell bezelStyle] != NSShadowlessSquareBezelStyle)
499             [cell setBezelStyle:NSShadowlessSquareBezelStyle];
500     } else if ([cell bezelStyle] != NSRoundedBezelStyle)
501         [cell setBezelStyle:NSRoundedBezelStyle];
502
503     setControlSize(cell, sizes, zoomedSize, zoomFactor);
504
505     // Update the various states we respond to.
506     updateStates(cell, states);
507 }
508
509 static NSButtonCell *button(ControlPart part, const ControlStates* controlStates, const IntSize& zoomedSize, float zoomFactor)
510 {
511     ControlStates::States states = controlStates->states();
512     NSButtonCell *cell;
513     if (states & ControlStates::DefaultState) {
514         static NSButtonCell *defaultCell = leakButtonCell(DefaultButtonCell);
515         cell = defaultCell;
516     } else {
517         static NSButtonCell *normalCell = leakButtonCell(NormalButtonCell);
518         cell = normalCell;
519     }
520     setUpButtonCell(cell, part, controlStates, zoomedSize, zoomFactor);
521     return cell;
522 }
523
524 static void paintButton(ControlPart part, ControlStates* controlStates, GraphicsContext* context, const FloatRect& zoomedRect, float zoomFactor, ScrollView* scrollView)
525 {
526     BEGIN_BLOCK_OBJC_EXCEPTIONS
527     
528     // Determine the width and height needed for the control and prepare the cell for painting.
529     ControlStates::States states = controlStates->states();
530     NSButtonCell *buttonCell = button(part, controlStates, IntSize(zoomedRect.size()), zoomFactor);
531     GraphicsContextStateSaver stateSaver(*context);
532
533     NSControlSize controlSize = [buttonCell controlSize];
534     IntSize zoomedSize = buttonSizes()[controlSize];
535     zoomedSize.setWidth(zoomedRect.width()); // Buttons don't ever constrain width, so the zoomed width can just be honored.
536     zoomedSize.setHeight(zoomedSize.height() * zoomFactor);
537     FloatRect inflatedRect = zoomedRect;
538     if ([buttonCell bezelStyle] == NSRoundedBezelStyle) {
539         // Center the button within the available space.
540         if (inflatedRect.height() > zoomedSize.height()) {
541             inflatedRect.setY(inflatedRect.y() + (inflatedRect.height() - zoomedSize.height()) / 2);
542             inflatedRect.setHeight(zoomedSize.height());
543         }
544
545         // Now inflate it to account for the shadow.
546         inflatedRect = inflateRect(inflatedRect, zoomedSize, buttonMargins(controlSize), zoomFactor);
547
548         if (zoomFactor != 1.0f) {
549             inflatedRect.setWidth(inflatedRect.width() / zoomFactor);
550             inflatedRect.setHeight(inflatedRect.height() / zoomFactor);
551             context->translate(inflatedRect.x(), inflatedRect.y());
552             context->scale(FloatSize(zoomFactor, zoomFactor));
553             context->translate(-inflatedRect.x(), -inflatedRect.y());
554         }
555     } 
556
557     LocalCurrentGraphicsContext localContext(context);
558     NSView *view = ThemeMac::ensuredView(scrollView, controlStates);
559     NSWindow *window = [view window];
560     NSButtonCell *previousDefaultButtonCell = [window defaultButtonCell];
561
562     if (states & ControlStates::DefaultState) {
563         [window setDefaultButtonCell:buttonCell];
564         wkAdvanceDefaultButtonPulseAnimation(buttonCell);
565     } else if ([previousDefaultButtonCell isEqual:buttonCell])
566         [window setDefaultButtonCell:nil];
567
568     [buttonCell drawWithFrame:NSRect(inflatedRect) inView:view];
569     bool needsRepaint = false;
570     if (states & ControlStates::FocusState)
571         needsRepaint = drawCellFocusRing(buttonCell, inflatedRect, view);
572
573     controlStates->setNeedsRepaint(needsRepaint);
574
575     [buttonCell setControlView:nil];
576
577     if (![previousDefaultButtonCell isEqual:buttonCell])
578         [window setDefaultButtonCell:previousDefaultButtonCell];
579
580     END_BLOCK_OBJC_EXCEPTIONS
581 }
582
583 // Stepper
584
585 static const std::array<IntSize, 3>& stepperSizes()
586 {
587     static const std::array<IntSize, 3> sizes = { { IntSize(19, 27), IntSize(15, 22), IntSize(13, 15) } };
588     return sizes;
589 }
590
591 // We don't use controlSizeForFont() for steppers because the stepper height
592 // should be equal to or less than the corresponding text field height,
593 static NSControlSize stepperControlSizeForFont(const Font& font)
594 {
595     int fontSize = font.pixelSize();
596     if (fontSize >= 18)
597         return NSRegularControlSize;
598     if (fontSize >= 13)
599         return NSSmallControlSize;
600     return NSMiniControlSize;
601 }
602
603 static void paintStepper(ControlStates* states, GraphicsContext* context, const FloatRect& zoomedRect, float zoomFactor, ScrollView*)
604 {
605     // We don't use NSStepperCell because there are no ways to draw an
606     // NSStepperCell with the up button highlighted.
607
608     HIThemeButtonDrawInfo drawInfo;
609     drawInfo.version = 0;
610     drawInfo.state = convertControlStatesToThemeDrawState(kThemeIncDecButton, states);
611     drawInfo.adornment = kThemeAdornmentDefault;
612     ControlSize controlSize = controlSizeFromPixelSize(stepperSizes(), IntSize(zoomedRect.size()), zoomFactor);
613     if (controlSize == NSSmallControlSize)
614         drawInfo.kind = kThemeIncDecButtonSmall;
615     else if (controlSize == NSMiniControlSize)
616         drawInfo.kind = kThemeIncDecButtonMini;
617     else
618         drawInfo.kind = kThemeIncDecButton;
619
620     IntRect rect(zoomedRect);
621     GraphicsContextStateSaver stateSaver(*context);
622     if (zoomFactor != 1.0f) {
623         rect.setWidth(rect.width() / zoomFactor);
624         rect.setHeight(rect.height() / zoomFactor);
625         context->translate(rect.x(), rect.y());
626         context->scale(FloatSize(zoomFactor, zoomFactor));
627         context->translate(-rect.x(), -rect.y());
628     }
629     CGRect bounds(rect);
630     CGRect backgroundBounds;
631     HIThemeGetButtonBackgroundBounds(&bounds, &drawInfo, &backgroundBounds);
632     // Center the stepper rectangle in the specified area.
633     backgroundBounds.origin.x = bounds.origin.x + (bounds.size.width - backgroundBounds.size.width) / 2;
634     if (backgroundBounds.size.height < bounds.size.height) {
635         int heightDiff = clampToInteger(bounds.size.height - backgroundBounds.size.height);
636         backgroundBounds.origin.y = bounds.origin.y + (heightDiff / 2) + 1;
637     }
638
639     LocalCurrentGraphicsContext localContext(context);
640     HIThemeDrawButton(&backgroundBounds, &drawInfo, localContext.cgContext(), kHIThemeOrientationNormal, 0);
641 }
642
643 // This will ensure that we always return a valid NSView, even if ScrollView doesn't have an associated document NSView.
644 // If the ScrollView doesn't have an NSView, we will return a fake NSView set up in the way AppKit expects.
645 NSView *ThemeMac::ensuredView(ScrollView* scrollView, const ControlStates* controlStates)
646 {
647     if (NSView *documentView = scrollView->documentView())
648         return documentView;
649
650     // Use a fake view.
651     static WebCoreThemeView *themeView = [[WebCoreThemeView alloc] init];
652     [themeView setFrameSize:NSSizeFromCGSize(scrollView->totalContentsSize())];
653
654     themeWindowHasKeyAppearance = !(controlStates->states() & ControlStates::WindowInactiveState);
655
656     return themeView;
657 }
658
659 void ThemeMac::setFocusRingClipRect(const FloatRect& rect)
660 {
661     focusRingClipRect = rect;
662 }
663
664 // Theme overrides
665
666 int ThemeMac::baselinePositionAdjustment(ControlPart part) const
667 {
668     if (part == CheckboxPart || part == RadioPart)
669         return -2;
670     return Theme::baselinePositionAdjustment(part);
671 }
672
673 FontDescription ThemeMac::controlFont(ControlPart part, const Font& font, float zoomFactor) const
674 {
675     switch (part) {
676         case PushButtonPart: {
677             FontDescription fontDescription;
678             fontDescription.setIsAbsoluteSize(true);
679             fontDescription.setGenericFamily(FontDescription::SerifFamily);
680
681             NSFont* nsFont = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:controlSizeForFont(font)]];
682             fontDescription.setOneFamily([nsFont webCoreFamilyName]);
683             fontDescription.setComputedSize([nsFont pointSize] * zoomFactor);
684             fontDescription.setSpecifiedSize([nsFont pointSize] * zoomFactor);
685             return fontDescription;
686         }
687         default:
688             return Theme::controlFont(part, font, zoomFactor);
689     }
690 }
691
692 LengthSize ThemeMac::controlSize(ControlPart part, const Font& font, const LengthSize& zoomedSize, float zoomFactor) const
693 {
694     switch (part) {
695         case CheckboxPart:
696             return checkboxSize(font, zoomedSize, zoomFactor);
697         case RadioPart:
698             return radioSize(font, zoomedSize, zoomFactor);
699         case PushButtonPart:
700             // Height is reset to auto so that specified heights can be ignored.
701             return sizeFromFont(font, LengthSize(zoomedSize.width(), Length()), zoomFactor, buttonSizes());
702         case InnerSpinButtonPart:
703             if (!zoomedSize.width().isIntrinsicOrAuto() && !zoomedSize.height().isIntrinsicOrAuto())
704                 return zoomedSize;
705             return sizeFromNSControlSize(stepperControlSizeForFont(font), zoomedSize, zoomFactor, stepperSizes());
706         default:
707             return zoomedSize;
708     }
709 }
710
711 LengthSize ThemeMac::minimumControlSize(ControlPart part, const Font& font, float zoomFactor) const
712 {
713     switch (part) {
714         case SquareButtonPart:
715         case DefaultButtonPart:
716         case ButtonPart:
717             return LengthSize(Length(0, Fixed), Length(static_cast<int>(15 * zoomFactor), Fixed));
718         case InnerSpinButtonPart:{
719             IntSize base = stepperSizes()[NSMiniControlSize];
720             return LengthSize(Length(static_cast<int>(base.width() * zoomFactor), Fixed),
721                               Length(static_cast<int>(base.height() * zoomFactor), Fixed));
722         }
723         default:
724             return Theme::minimumControlSize(part, font, zoomFactor);
725     }
726 }
727
728 LengthBox ThemeMac::controlBorder(ControlPart part, const Font& font, const LengthBox& zoomedBox, float zoomFactor) const
729 {
730     switch (part) {
731         case SquareButtonPart:
732         case DefaultButtonPart:
733         case ButtonPart:
734             return LengthBox(0, zoomedBox.right().value(), 0, zoomedBox.left().value());
735         default:
736             return Theme::controlBorder(part, font, zoomedBox, zoomFactor);
737     }
738 }
739
740 LengthBox ThemeMac::controlPadding(ControlPart part, const Font& font, const LengthBox& zoomedBox, float zoomFactor) const
741 {
742     switch (part) {
743         case PushButtonPart: {
744             // Just use 8px.  AppKit wants to use 11px for mini buttons, but that padding is just too large
745             // for real-world Web sites (creating a huge necessary minimum width for buttons whose space is
746             // by definition constrained, since we select mini only for small cramped environments.
747             // This also guarantees the HTML <button> will match our rendering by default, since we're using a consistent
748             // padding.
749             const int padding = 8 * zoomFactor;
750             return LengthBox(2, padding, 3, padding);
751         }
752         default:
753             return Theme::controlPadding(part, font, zoomedBox, zoomFactor);
754     }
755 }
756
757 void ThemeMac::inflateControlPaintRect(ControlPart part, const ControlStates* states, FloatRect& zoomedRect, float zoomFactor) const
758 {
759     BEGIN_BLOCK_OBJC_EXCEPTIONS
760     IntSize zoomRectSize = IntSize(zoomedRect.size());
761     switch (part) {
762         case CheckboxPart: {
763             // We inflate the rect as needed to account for padding included in the cell to accommodate the checkbox
764             // shadow" and the check.  We don't consider this part of the bounds of the control in WebKit.
765             NSCell *cell = sharedCheckboxCell(states, zoomRectSize, zoomFactor);
766             NSControlSize controlSize = [cell controlSize];
767             IntSize zoomedSize = checkboxSizes()[controlSize];
768             zoomedSize.setHeight(zoomedSize.height() * zoomFactor);
769             zoomedSize.setWidth(zoomedSize.width() * zoomFactor);
770             zoomedRect = inflateRect(zoomedRect, zoomedSize, checkboxMargins(controlSize), zoomFactor);
771             break;
772         }
773         case RadioPart: {
774             // We inflate the rect as needed to account for padding included in the cell to accommodate the radio button
775             // shadow".  We don't consider this part of the bounds of the control in WebKit.
776             NSCell *cell = sharedRadioCell(states, zoomRectSize, zoomFactor);
777             NSControlSize controlSize = [cell controlSize];
778             IntSize zoomedSize = radioSizes()[controlSize];
779             zoomedSize.setHeight(zoomedSize.height() * zoomFactor);
780             zoomedSize.setWidth(zoomedSize.width() * zoomFactor);
781             zoomedRect = inflateRect(zoomedRect, zoomedSize, radioMargins(controlSize), zoomFactor);
782             break;
783         }
784         case PushButtonPart:
785         case DefaultButtonPart:
786         case ButtonPart: {
787             NSButtonCell *cell = button(part, states, zoomRectSize, zoomFactor);
788             NSControlSize controlSize = [cell controlSize];
789
790             // We inflate the rect as needed to account for the Aqua button's shadow.
791             if ([cell bezelStyle] == NSRoundedBezelStyle) {
792                 IntSize zoomedSize = buttonSizes()[controlSize];
793                 zoomedSize.setHeight(zoomedSize.height() * zoomFactor);
794                 zoomedSize.setWidth(zoomedRect.width()); // Buttons don't ever constrain width, so the zoomed width can just be honored.
795                 zoomedRect = inflateRect(zoomedRect, zoomedSize, buttonMargins(controlSize), zoomFactor);
796             }
797             break;
798         }
799         case InnerSpinButtonPart: {
800             static const int stepperMargin[4] = { 0, 0, 0, 0 };
801             ControlSize controlSize = controlSizeFromPixelSize(stepperSizes(), zoomRectSize, zoomFactor);
802             IntSize zoomedSize = stepperSizes()[controlSize];
803             zoomedSize.setHeight(zoomedSize.height() * zoomFactor);
804             zoomedSize.setWidth(zoomedSize.width() * zoomFactor);
805             zoomedRect = inflateRect(zoomedRect, zoomedSize, stepperMargin, zoomFactor);
806             break;
807         }
808         default:
809             break;
810     }
811     END_BLOCK_OBJC_EXCEPTIONS
812 }
813
814 void ThemeMac::paint(ControlPart part, ControlStates* states, GraphicsContext* context, const FloatRect& zoomedRect, float zoomFactor, ScrollView* scrollView)
815 {
816     switch (part) {
817         case CheckboxPart:
818             paintToggleButton(part, states, context, zoomedRect, zoomFactor, scrollView);
819             break;
820         case RadioPart:
821             paintToggleButton(part, states, context, zoomedRect, zoomFactor, scrollView);
822             break;
823         case PushButtonPart:
824         case DefaultButtonPart:
825         case ButtonPart:
826         case SquareButtonPart:
827             paintButton(part, states, context, zoomedRect, zoomFactor, scrollView);
828             break;
829         case InnerSpinButtonPart:
830             paintStepper(states, context, zoomedRect, zoomFactor, scrollView);
831             break;
832         default:
833             break;
834     }
835 }
836
837 }