1 /* 2 * Copyright (C) 2010 Google 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 are 6 * met: 7 * 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 13 * distribution. 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. 17 * 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. 29 */ 30 31 #include "WebTestThemeEngineMac.h" 32 33 #include "public/platform/WebCanvas.h" 34 #include "public/platform/WebRect.h" 35 #include "skia/ext/skia_utils_mac.h" 36 #import <AppKit/NSAffineTransform.h> 37 #import <AppKit/NSGraphicsContext.h> 38 #import <AppKit/NSScroller.h> 39 #import <AppKit/NSWindow.h> 40 #include <Carbon/Carbon.h> 41 42 using blink::WebCanvas; 43 using blink::WebRect; 44 using blink::WebThemeEngine; 45 46 // We can't directly tell the NSScroller to draw itself as active or inactive, 47 // instead we have to make it a child of an (in)active window. This class lets 48 // us fake that parent window. 49 @interface FakeActiveWindow : NSWindow { 50 @private 51 BOOL hasActiveControls; 52 } 53 + (NSWindow*)alwaysActiveWindow; 54 + (NSWindow*)alwaysInactiveWindow; 55 - (id)initWithActiveControls:(BOOL)_hasActiveControls; 56 - (BOOL)_hasActiveControls; 57 @end 58 59 @implementation FakeActiveWindow 60 61 static NSWindow* alwaysActiveWindow = nil; 62 static NSWindow* alwaysInactiveWindow = nil; 63 64 + (NSWindow*)alwaysActiveWindow 65 { 66 if (alwaysActiveWindow == nil) 67 alwaysActiveWindow = [[self alloc] initWithActiveControls:YES]; 68 return alwaysActiveWindow; 69 } 70 71 + (NSWindow*)alwaysInactiveWindow 72 { 73 if (alwaysInactiveWindow == nil) 74 alwaysInactiveWindow = [[self alloc] initWithActiveControls:NO]; 75 return alwaysInactiveWindow; 76 } 77 78 - (id)initWithActiveControls:(BOOL)_hasActiveControls 79 { 80 self = [super init]; 81 hasActiveControls = _hasActiveControls; 82 return self; 83 } 84 85 - (BOOL)_hasActiveControls 86 { 87 return hasActiveControls; 88 } 89 90 @end 91 92 namespace WebTestRunner { 93 94 namespace { 95 96 ThemeTrackEnableState stateToHIEnableState(WebThemeEngine::State state) 97 { 98 switch (state) { 99 case WebThemeEngine::StateDisabled: 100 return kThemeTrackDisabled; 101 case WebThemeEngine::StateInactive: 102 return kThemeTrackInactive; 103 default: 104 return kThemeTrackActive; 105 } 106 } 107 108 } 109 110 void WebTestThemeEngineMac::paintScrollbarThumb( 111 WebCanvas* canvas, 112 WebThemeEngine::State state, 113 WebThemeEngine::Size size, 114 const WebRect& rect, 115 const WebThemeEngine::ScrollbarInfo& scrollbarInfo) 116 { 117 // To match the Mac port, we still use HITheme for inner scrollbars. 118 if (scrollbarInfo.parent == WebThemeEngine::ScrollbarParentRenderLayer) 119 paintHIThemeScrollbarThumb(canvas, state, size, rect, scrollbarInfo); 120 else 121 paintNSScrollerScrollbarThumb(canvas, state, size, rect, scrollbarInfo); 122 } 123 124 // Duplicated from webkit/glue/webthemeengine_impl_mac.cc in the downstream 125 // Chromium WebThemeEngine implementation. 126 void WebTestThemeEngineMac::paintHIThemeScrollbarThumb( 127 WebCanvas* canvas, 128 WebThemeEngine::State state, 129 WebThemeEngine::Size size, 130 const WebRect& rect, 131 const WebThemeEngine::ScrollbarInfo& scrollbarInfo) 132 { 133 HIThemeTrackDrawInfo trackInfo; 134 trackInfo.version = 0; 135 trackInfo.kind = size == WebThemeEngine::SizeRegular ? kThemeMediumScrollBar : kThemeSmallScrollBar; 136 trackInfo.bounds = CGRectMake(rect.x, rect.y, rect.width, rect.height); 137 trackInfo.min = 0; 138 trackInfo.max = scrollbarInfo.maxValue; 139 trackInfo.value = scrollbarInfo.currentValue; 140 trackInfo.trackInfo.scrollbar.viewsize = scrollbarInfo.visibleSize; 141 trackInfo.attributes = 0; 142 if (scrollbarInfo.orientation == WebThemeEngine::ScrollbarOrientationHorizontal) 143 trackInfo.attributes |= kThemeTrackHorizontal; 144 145 trackInfo.enableState = stateToHIEnableState(state); 146 147 trackInfo.trackInfo.scrollbar.pressState = 148 state == WebThemeEngine::StatePressed ? kThemeThumbPressed : 0; 149 trackInfo.attributes |= (kThemeTrackShowThumb | kThemeTrackHideTrack); 150 gfx::SkiaBitLocker bitLocker(canvas); 151 CGContextRef cgContext = bitLocker.cgContext(); 152 HIThemeDrawTrack(&trackInfo, 0, cgContext, kHIThemeOrientationNormal); 153 } 154 155 void WebTestThemeEngineMac::paintNSScrollerScrollbarThumb( 156 WebCanvas* canvas, 157 WebThemeEngine::State state, 158 WebThemeEngine::Size size, 159 const WebRect& rect, 160 const WebThemeEngine::ScrollbarInfo& scrollbarInfo) 161 { 162 [NSGraphicsContext saveGraphicsState]; 163 NSScroller* scroller = [[NSScroller alloc] initWithFrame:NSMakeRect(rect.x, rect.y, rect.width, rect.height)]; 164 [scroller setEnabled:state != WebThemeEngine::StateDisabled]; 165 if (state == WebThemeEngine::StateInactive) 166 [[[FakeActiveWindow alwaysInactiveWindow] contentView] addSubview:scroller]; 167 else 168 [[[FakeActiveWindow alwaysActiveWindow] contentView] addSubview:scroller]; 169 170 [scroller setControlSize:size == WebThemeEngine::SizeRegular ? NSRegularControlSize : NSSmallControlSize]; 171 172 double value = double(scrollbarInfo.currentValue) / double(scrollbarInfo.maxValue); 173 [scroller setDoubleValue: value]; 174 175 float knobProportion = float(scrollbarInfo.visibleSize) / float(scrollbarInfo.totalSize); 176 [scroller setKnobProportion: knobProportion]; 177 178 gfx::SkiaBitLocker bitLocker(canvas); 179 CGContextRef cgContext = bitLocker.cgContext(); 180 NSGraphicsContext* nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:YES]; 181 [NSGraphicsContext setCurrentContext:nsGraphicsContext]; 182 183 // Despite passing in frameRect() to the scroller, it always draws at (0, 0). 184 // Force it to draw in the right location by translating the whole graphics 185 // context. 186 CGContextSaveGState(cgContext); 187 NSAffineTransform *transform = [NSAffineTransform transform]; 188 [transform translateXBy:rect.x yBy:rect.y]; 189 [transform concat]; 190 191 [scroller drawKnob]; 192 CGContextRestoreGState(cgContext); 193 194 [scroller release]; 195 196 [NSGraphicsContext restoreGraphicsState]; 197 } 198 199 } 200