2 * Copyright (C) 2010 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.
31 #include "WebThemeEngineDRTMac.h"
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h"
34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h"
35 #import <AppKit/NSAffineTransform.h>
36 #import <AppKit/NSGraphicsContext.h>
37 #import <AppKit/NSScroller.h>
38 #import <AppKit/NSWindow.h>
39 #include <Carbon/Carbon.h>
42 #include "skia/ext/skia_utils_mac.h"
45 using WebKit::WebCanvas;
46 using WebKit::WebRect;
47 using WebKit::WebThemeEngine;
49 // We can't directly tell the NSScroller to draw itself as active or inactive,
50 // instead we have to make it a child of an (in)active window. This class lets
51 // us fake that parent window.
52 @interface FakeActiveWindow : NSWindow {
54 BOOL hasActiveControls;
56 + (NSWindow*)alwaysActiveWindow;
57 + (NSWindow*)alwaysInactiveWindow;
58 - (id)initWithActiveControls:(BOOL)_hasActiveControls;
59 - (BOOL)_hasActiveControls;
62 @implementation FakeActiveWindow
64 static NSWindow* alwaysActiveWindow = nil;
65 static NSWindow* alwaysInactiveWindow = nil;
67 + (NSWindow*)alwaysActiveWindow
69 if (alwaysActiveWindow == nil)
70 alwaysActiveWindow = [[self alloc] initWithActiveControls:YES];
71 return alwaysActiveWindow;
74 + (NSWindow*)alwaysInactiveWindow
76 if (alwaysInactiveWindow == nil)
77 alwaysInactiveWindow = [[self alloc] initWithActiveControls:NO];
78 return alwaysInactiveWindow;
81 - (id)initWithActiveControls:(BOOL)_hasActiveControls
84 hasActiveControls = _hasActiveControls;
88 - (BOOL)_hasActiveControls
90 return hasActiveControls;
95 void WebThemeEngineDRTMac::paintScrollbarThumb(
97 WebThemeEngine::State state,
98 WebThemeEngine::Size size,
100 const WebThemeEngine::ScrollbarInfo& scrollbarInfo)
102 // To match the Mac port, we still use HITheme for inner scrollbars.
103 if (scrollbarInfo.parent == WebThemeEngine::ScrollbarParentRenderLayer)
104 paintHIThemeScrollbarThumb(canvas, state, size, rect, scrollbarInfo);
106 paintNSScrollerScrollbarThumb(canvas, state, size, rect, scrollbarInfo);
109 static ThemeTrackEnableState stateToHIEnableState(WebThemeEngine::State state)
112 case WebThemeEngine::StateDisabled:
113 return kThemeTrackDisabled;
114 case WebThemeEngine::StateInactive:
115 return kThemeTrackInactive;
117 return kThemeTrackActive;
121 // Duplicated from webkit/glue/webthemeengine_impl_mac.cc in the downstream
122 // Chromium WebThemeEngine implementation.
123 void WebThemeEngineDRTMac::paintHIThemeScrollbarThumb(
125 WebThemeEngine::State state,
126 WebThemeEngine::Size size,
128 const WebThemeEngine::ScrollbarInfo& scrollbarInfo)
130 HIThemeTrackDrawInfo trackInfo;
131 trackInfo.version = 0;
132 trackInfo.kind = size == WebThemeEngine::SizeRegular ? kThemeMediumScrollBar : kThemeSmallScrollBar;
133 trackInfo.bounds = CGRectMake(rect.x, rect.y, rect.width, rect.height);
135 trackInfo.max = scrollbarInfo.maxValue;
136 trackInfo.value = scrollbarInfo.currentValue;
137 trackInfo.trackInfo.scrollbar.viewsize = scrollbarInfo.visibleSize;
138 trackInfo.attributes = 0;
139 if (scrollbarInfo.orientation == WebThemeEngine::ScrollbarOrientationHorizontal)
140 trackInfo.attributes |= kThemeTrackHorizontal;
142 trackInfo.enableState = stateToHIEnableState(state);
144 trackInfo.trackInfo.scrollbar.pressState =
145 state == WebThemeEngine::StatePressed ? kThemeThumbPressed : 0;
146 trackInfo.attributes |= (kThemeTrackShowThumb | kThemeTrackHideTrack);
147 #if WEBKIT_USING_SKIA
148 gfx::SkiaBitLocker bitLocker(canvas);
149 CGContextRef cgContext = bitLocker.cgContext();
151 CGContextRef cgContext = canvas;
153 HIThemeDrawTrack(&trackInfo, 0, cgContext, kHIThemeOrientationNormal);
156 void WebThemeEngineDRTMac::paintNSScrollerScrollbarThumb(
158 WebThemeEngine::State state,
159 WebThemeEngine::Size size,
161 const WebThemeEngine::ScrollbarInfo& scrollbarInfo)
163 [NSGraphicsContext saveGraphicsState];
164 NSScroller* scroller = [[NSScroller alloc] initWithFrame:NSMakeRect(rect.x, rect.y, rect.width, rect.height)];
165 [scroller setEnabled:state != WebThemeEngine::StateDisabled];
166 if (state == WebThemeEngine::StateInactive)
167 [[[FakeActiveWindow alwaysInactiveWindow] contentView] addSubview:scroller];
169 [[[FakeActiveWindow alwaysActiveWindow] contentView] addSubview:scroller];
171 [scroller setControlSize:size == WebThemeEngine::SizeRegular ? NSRegularControlSize : NSSmallControlSize];
173 double value = double(scrollbarInfo.currentValue) / double(scrollbarInfo.maxValue);
174 [scroller setDoubleValue: value];
176 float knobProportion = float(scrollbarInfo.visibleSize) / float(scrollbarInfo.totalSize);
177 [scroller setKnobProportion: knobProportion];
179 #if WEBKIT_USING_SKIA
180 gfx::SkiaBitLocker bitLocker(canvas);
181 CGContextRef cgContext = bitLocker.cgContext();
183 CGContextRef cgContext = canvas;
185 NSGraphicsContext* nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:YES];
186 [NSGraphicsContext setCurrentContext:nsGraphicsContext];
188 // Despite passing in frameRect() to the scroller, it always draws at (0, 0).
189 // Force it to draw in the right location by translating the whole graphics
191 CGContextSaveGState(cgContext);
192 NSAffineTransform *transform = [NSAffineTransform transform];
193 [transform translateXBy:rect.x yBy:rect.y];
197 CGContextRestoreGState(cgContext);
201 [NSGraphicsContext restoreGraphicsState];