1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/shell/renderer/test_runner/WebTestThemeEngineMac.h"
7 #import <AppKit/NSAffineTransform.h>
8 #import <AppKit/NSGraphicsContext.h>
9 #import <AppKit/NSScroller.h>
10 #import <AppKit/NSWindow.h>
11 #include <Carbon/Carbon.h>
12 #include "skia/ext/skia_utils_mac.h"
13 #include "third_party/WebKit/public/platform/WebCanvas.h"
14 #include "third_party/WebKit/public/platform/WebRect.h"
16 using blink::WebCanvas;
18 using blink::WebThemeEngine;
20 // We can't directly tell the NSScroller to draw itself as active or inactive,
21 // instead we have to make it a child of an (in)active window. This class lets
22 // us fake that parent window.
23 @interface FakeActiveWindow : NSWindow {
25 BOOL hasActiveControls;
27 + (NSWindow*)alwaysActiveWindow;
28 + (NSWindow*)alwaysInactiveWindow;
29 - (id)initWithActiveControls:(BOOL)_hasActiveControls;
30 - (BOOL)_hasActiveControls;
33 @implementation FakeActiveWindow
35 static NSWindow* alwaysActiveWindow = nil;
36 static NSWindow* alwaysInactiveWindow = nil;
38 + (NSWindow*)alwaysActiveWindow
40 if (alwaysActiveWindow == nil)
41 alwaysActiveWindow = [[self alloc] initWithActiveControls:YES];
42 return alwaysActiveWindow;
45 + (NSWindow*)alwaysInactiveWindow
47 if (alwaysInactiveWindow == nil)
48 alwaysInactiveWindow = [[self alloc] initWithActiveControls:NO];
49 return alwaysInactiveWindow;
52 - (id)initWithActiveControls:(BOOL)_hasActiveControls
54 if ((self = [super initWithContentRect:NSMakeRect(0, 0, 100, 100)
56 backing:NSBackingStoreBuffered
58 hasActiveControls = _hasActiveControls;
63 - (BOOL)_hasActiveControls
65 return hasActiveControls;
70 namespace WebTestRunner {
74 ThemeTrackEnableState stateToHIEnableState(WebThemeEngine::State state)
77 case WebThemeEngine::StateDisabled:
78 return kThemeTrackDisabled;
79 case WebThemeEngine::StateInactive:
80 return kThemeTrackInactive;
82 return kThemeTrackActive;
88 void WebTestThemeEngineMac::paintScrollbarThumb(
90 WebThemeEngine::State state,
91 WebThemeEngine::Size size,
93 const WebThemeEngine::ScrollbarInfo& scrollbarInfo)
95 // To match the Mac port, we still use HITheme for inner scrollbars.
96 if (scrollbarInfo.parent == WebThemeEngine::ScrollbarParentRenderLayer)
97 paintHIThemeScrollbarThumb(canvas, state, size, rect, scrollbarInfo);
99 paintNSScrollerScrollbarThumb(canvas, state, size, rect, scrollbarInfo);
102 // Duplicated from webkit/glue/webthemeengine_impl_mac.cc in the downstream
103 // Chromium WebThemeEngine implementation.
104 void WebTestThemeEngineMac::paintHIThemeScrollbarThumb(
106 WebThemeEngine::State state,
107 WebThemeEngine::Size size,
109 const WebThemeEngine::ScrollbarInfo& scrollbarInfo)
111 HIThemeTrackDrawInfo trackInfo;
112 trackInfo.version = 0;
113 trackInfo.kind = size == WebThemeEngine::SizeRegular ? kThemeMediumScrollBar : kThemeSmallScrollBar;
114 trackInfo.bounds = CGRectMake(rect.x, rect.y, rect.width, rect.height);
116 trackInfo.max = scrollbarInfo.maxValue;
117 trackInfo.value = scrollbarInfo.currentValue;
118 trackInfo.trackInfo.scrollbar.viewsize = scrollbarInfo.visibleSize;
119 trackInfo.attributes = 0;
120 if (scrollbarInfo.orientation == WebThemeEngine::ScrollbarOrientationHorizontal)
121 trackInfo.attributes |= kThemeTrackHorizontal;
123 trackInfo.enableState = stateToHIEnableState(state);
125 trackInfo.trackInfo.scrollbar.pressState =
126 state == WebThemeEngine::StatePressed ? kThemeThumbPressed : 0;
127 trackInfo.attributes |= (kThemeTrackShowThumb | kThemeTrackHideTrack);
128 gfx::SkiaBitLocker bitLocker(canvas);
129 CGContextRef cgContext = bitLocker.cgContext();
130 HIThemeDrawTrack(&trackInfo, 0, cgContext, kHIThemeOrientationNormal);
133 void WebTestThemeEngineMac::paintNSScrollerScrollbarThumb(
135 WebThemeEngine::State state,
136 WebThemeEngine::Size size,
138 const WebThemeEngine::ScrollbarInfo& scrollbarInfo)
140 [NSGraphicsContext saveGraphicsState];
141 NSScroller* scroller = [[NSScroller alloc] initWithFrame:NSMakeRect(rect.x, rect.y, rect.width, rect.height)];
142 [scroller setEnabled:state != WebThemeEngine::StateDisabled];
143 if (state == WebThemeEngine::StateInactive)
144 [[[FakeActiveWindow alwaysInactiveWindow] contentView] addSubview:scroller];
146 [[[FakeActiveWindow alwaysActiveWindow] contentView] addSubview:scroller];
148 [scroller setControlSize:size == WebThemeEngine::SizeRegular ? NSRegularControlSize : NSSmallControlSize];
150 double value = double(scrollbarInfo.currentValue) / double(scrollbarInfo.maxValue);
151 [scroller setDoubleValue: value];
153 float knobProportion = float(scrollbarInfo.visibleSize) / float(scrollbarInfo.totalSize);
154 [scroller setKnobProportion: knobProportion];
156 gfx::SkiaBitLocker bitLocker(canvas);
157 CGContextRef cgContext = bitLocker.cgContext();
158 NSGraphicsContext* nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:YES];
159 [NSGraphicsContext setCurrentContext:nsGraphicsContext];
161 // Despite passing in frameRect() to the scroller, it always draws at (0, 0).
162 // Force it to draw in the right location by translating the whole graphics
164 CGContextSaveGState(cgContext);
165 NSAffineTransform *transform = [NSAffineTransform transform];
166 [transform translateXBy:rect.x yBy:rect.y];
170 CGContextRestoreGState(cgContext);
174 [NSGraphicsContext restoreGraphicsState];