2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "platform/scroll/ScrollbarThemeMacOverlayAPI.h"
34 #include "platform/graphics/GraphicsContext.h"
35 #include "platform/graphics/GraphicsContextStateSaver.h"
36 #include "platform/mac/LocalCurrentGraphicsContext.h"
37 #include "platform/mac/NSScrollerImpDetails.h"
38 #include "platform/scroll/ScrollbarThemeClient.h"
42 typedef HashMap<ScrollbarThemeClient*, RetainPtr<ScrollbarPainter> > ScrollbarPainterMap;
44 static ScrollbarPainterMap* scrollbarPainterMap()
46 static ScrollbarPainterMap* map = new ScrollbarPainterMap;
50 static bool supportsExpandedScrollbars()
52 // FIXME: This is temporary until all platforms that support ScrollbarPainter support this part of the API.
53 static bool globalSupportsExpandedScrollbars = [NSClassFromString(@"NSScrollerImp") instancesRespondToSelector:@selector(setExpanded:)];
54 return globalSupportsExpandedScrollbars;
57 void ScrollbarThemeMacOverlayAPI::registerScrollbar(ScrollbarThemeClient* scrollbar)
59 ScrollbarThemeMacCommon::registerScrollbar(scrollbar);
61 bool isHorizontal = scrollbar->orientation() == HorizontalScrollbar;
62 ScrollbarPainter scrollbarPainter = [NSClassFromString(@"NSScrollerImp") scrollerImpWithStyle:recommendedScrollerStyle() controlSize:(NSControlSize)scrollbar->controlSize() horizontal:isHorizontal replacingScrollerImp:nil];
63 scrollbarPainterMap()->add(scrollbar, scrollbarPainter);
64 updateEnabledState(scrollbar);
65 updateScrollbarOverlayStyle(scrollbar);
68 void ScrollbarThemeMacOverlayAPI::unregisterScrollbar(ScrollbarThemeClient* scrollbar)
70 scrollbarPainterMap()->remove(scrollbar);
72 ScrollbarThemeMacCommon::unregisterScrollbar(scrollbar);
75 void ScrollbarThemeMacOverlayAPI::setNewPainterForScrollbar(ScrollbarThemeClient* scrollbar, ScrollbarPainter newPainter)
77 scrollbarPainterMap()->set(scrollbar, newPainter);
78 updateEnabledState(scrollbar);
79 updateScrollbarOverlayStyle(scrollbar);
82 ScrollbarPainter ScrollbarThemeMacOverlayAPI::painterForScrollbar(ScrollbarThemeClient* scrollbar)
84 return scrollbarPainterMap()->get(scrollbar).get();
87 void ScrollbarThemeMacOverlayAPI::paintTrackBackground(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect) {
88 ASSERT(isOverlayAPIAvailable());
90 GraphicsContextStateSaver stateSaver(*context);
91 context->translate(rect.x(), rect.y());
92 LocalCurrentGraphicsContext localContext(context, IntRect(IntPoint(), rect.size()));
94 CGRect frameRect = scrollbar->frameRect();
95 ScrollbarPainter scrollbarPainter = painterForScrollbar(scrollbar);
96 [scrollbarPainter setEnabled:scrollbar->enabled()];
97 [scrollbarPainter setBoundsSize: NSSizeFromCGSize(frameRect.size)];
99 NSRect trackRect = NSMakeRect(0, 0, frameRect.size.width, frameRect.size.height);
100 [scrollbarPainter drawKnobSlotInRect:trackRect highlight:NO];
103 void ScrollbarThemeMacOverlayAPI::paintThumb(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect) {
104 ASSERT(isOverlayAPIAvailable());
106 GraphicsContextStateSaver stateSaver(*context);
107 context->translate(rect.x(), rect.y());
108 LocalCurrentGraphicsContext localContext(context, IntRect(IntPoint(), rect.size()));
110 ScrollbarPainter scrollbarPainter = painterForScrollbar(scrollbar);
111 [scrollbarPainter setEnabled:scrollbar->enabled()];
112 [scrollbarPainter setBoundsSize:NSSizeFromCGSize(rect.size())];
113 [scrollbarPainter setDoubleValue:0];
114 [scrollbarPainter setKnobProportion:1];
115 if (scrollbar->enabled())
116 [scrollbarPainter drawKnob];
118 // If this state is not set, then moving the cursor over the scrollbar area will only cause the
119 // scrollbar to engorge when moved over the top of the scrollbar area.
120 [scrollbarPainter setBoundsSize: NSSizeFromCGSize(scrollbar->frameRect().size())];
123 int ScrollbarThemeMacOverlayAPI::scrollbarThickness(ScrollbarControlSize controlSize)
125 ScrollbarPainter scrollbarPainter = [NSClassFromString(@"NSScrollerImp") scrollerImpWithStyle:recommendedScrollerStyle() controlSize:controlSize horizontal:NO replacingScrollerImp:nil];
126 if (supportsExpandedScrollbars())
127 [scrollbarPainter setExpanded:YES];
128 return [scrollbarPainter trackBoxWidth];
131 bool ScrollbarThemeMacOverlayAPI::usesOverlayScrollbars() const
133 return recommendedScrollerStyle() == NSScrollerStyleOverlay;
136 void ScrollbarThemeMacOverlayAPI::updateScrollbarOverlayStyle(ScrollbarThemeClient* scrollbar)
138 ScrollbarPainter painter = painterForScrollbar(scrollbar);
139 switch (scrollbar->scrollbarOverlayStyle()) {
140 case ScrollbarOverlayStyleDefault:
141 [painter setKnobStyle:NSScrollerKnobStyleDefault];
143 case ScrollbarOverlayStyleDark:
144 [painter setKnobStyle:NSScrollerKnobStyleDark];
146 case ScrollbarOverlayStyleLight:
147 [painter setKnobStyle:NSScrollerKnobStyleLight];
152 ScrollbarButtonsPlacement ScrollbarThemeMacOverlayAPI::buttonsPlacement() const
154 return ScrollbarButtonsNone;
157 bool ScrollbarThemeMacOverlayAPI::hasThumb(ScrollbarThemeClient* scrollbar)
159 ScrollbarPainter painter = painterForScrollbar(scrollbar);
160 int minLengthForThumb = [painter knobMinLength] + [painter trackOverlapEndInset] + [painter knobOverlapEndInset]
161 + 2 * ([painter trackEndInset] + [painter knobEndInset]);
162 return scrollbar->enabled() && (scrollbar->orientation() == HorizontalScrollbar ?
164 scrollbar->height()) >= minLengthForThumb;
167 IntRect ScrollbarThemeMacOverlayAPI::backButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool painting)
169 ASSERT(buttonsPlacement() == ScrollbarButtonsNone);
173 IntRect ScrollbarThemeMacOverlayAPI::forwardButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool painting)
175 ASSERT(buttonsPlacement() == ScrollbarButtonsNone);
179 IntRect ScrollbarThemeMacOverlayAPI::trackRect(ScrollbarThemeClient* scrollbar, bool painting)
181 ASSERT(!hasButtons(scrollbar));
182 return scrollbar->frameRect();
185 int ScrollbarThemeMacOverlayAPI::minimumThumbLength(ScrollbarThemeClient* scrollbar)
187 return [painterForScrollbar(scrollbar) knobMinLength];
190 void ScrollbarThemeMacOverlayAPI::updateEnabledState(ScrollbarThemeClient* scrollbar)
192 [painterForScrollbar(scrollbar) setEnabled:scrollbar->enabled()];