initial import
[vuplus_webkit] / Tools / DumpRenderTree / chromium / WebThemeEngineDRTMac.mm
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 "WebThemeEngineDRTMac.h"
32
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>
40
41 #if WEBKIT_USING_SKIA
42 #include "skia/ext/skia_utils_mac.h"
43 #endif
44
45 using WebKit::WebCanvas;
46 using WebKit::WebRect;
47 using WebKit::WebThemeEngine;
48
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 {
53 @private
54     BOOL hasActiveControls;
55 }
56 + (NSWindow*)alwaysActiveWindow;
57 + (NSWindow*)alwaysInactiveWindow;
58 - (id)initWithActiveControls:(BOOL)_hasActiveControls;
59 - (BOOL)_hasActiveControls;
60 @end
61
62 @implementation FakeActiveWindow
63
64 static NSWindow* alwaysActiveWindow = nil;
65 static NSWindow* alwaysInactiveWindow = nil;
66
67 + (NSWindow*)alwaysActiveWindow
68 {
69     if (alwaysActiveWindow == nil)
70         alwaysActiveWindow = [[self alloc] initWithActiveControls:YES];
71     return alwaysActiveWindow;
72 }
73
74 + (NSWindow*)alwaysInactiveWindow
75 {
76     if (alwaysInactiveWindow == nil)
77         alwaysInactiveWindow = [[self alloc] initWithActiveControls:NO];
78     return alwaysInactiveWindow;
79 }
80
81 - (id)initWithActiveControls:(BOOL)_hasActiveControls
82 {
83     self = [super init];
84     hasActiveControls = _hasActiveControls;
85     return self;
86 }
87
88 - (BOOL)_hasActiveControls
89 {
90     return hasActiveControls;
91 }
92
93 @end
94
95 void WebThemeEngineDRTMac::paintScrollbarThumb(
96     WebCanvas* canvas,
97     WebThemeEngine::State state,
98     WebThemeEngine::Size size,
99     const WebRect& rect,
100     const WebThemeEngine::ScrollbarInfo& scrollbarInfo)
101 {
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);
105     else
106         paintNSScrollerScrollbarThumb(canvas, state, size, rect, scrollbarInfo);
107 }
108
109 static ThemeTrackEnableState stateToHIEnableState(WebThemeEngine::State state)
110 {
111     switch (state) {
112     case WebThemeEngine::StateDisabled:
113         return kThemeTrackDisabled;
114     case WebThemeEngine::StateInactive:
115         return kThemeTrackInactive;
116     default:
117         return kThemeTrackActive;
118     }
119 }
120
121 // Duplicated from webkit/glue/webthemeengine_impl_mac.cc in the downstream
122 // Chromium WebThemeEngine implementation.
123 void WebThemeEngineDRTMac::paintHIThemeScrollbarThumb(
124     WebCanvas* canvas,
125     WebThemeEngine::State state,
126     WebThemeEngine::Size size,
127     const WebRect& rect,
128     const WebThemeEngine::ScrollbarInfo& scrollbarInfo)
129 {
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);
134     trackInfo.min = 0;
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;
141
142     trackInfo.enableState = stateToHIEnableState(state);
143
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();
150 #else
151     CGContextRef cgContext = canvas;
152 #endif
153     HIThemeDrawTrack(&trackInfo, 0, cgContext, kHIThemeOrientationNormal);
154 }
155
156 void WebThemeEngineDRTMac::paintNSScrollerScrollbarThumb(
157     WebCanvas* canvas,
158     WebThemeEngine::State state,
159     WebThemeEngine::Size size,
160     const WebRect& rect,
161     const WebThemeEngine::ScrollbarInfo& scrollbarInfo)
162 {
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];
168     else
169         [[[FakeActiveWindow alwaysActiveWindow] contentView] addSubview:scroller];
170
171     [scroller setControlSize:size == WebThemeEngine::SizeRegular ? NSRegularControlSize : NSSmallControlSize];
172
173     double value = double(scrollbarInfo.currentValue) / double(scrollbarInfo.maxValue);
174     [scroller setDoubleValue: value];
175
176     float knobProportion = float(scrollbarInfo.visibleSize) / float(scrollbarInfo.totalSize);
177     [scroller setKnobProportion: knobProportion];
178
179 #if WEBKIT_USING_SKIA
180     gfx::SkiaBitLocker bitLocker(canvas);
181     CGContextRef cgContext = bitLocker.cgContext();
182 #else
183     CGContextRef cgContext = canvas;
184 #endif
185     NSGraphicsContext* nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:YES];
186     [NSGraphicsContext setCurrentContext:nsGraphicsContext];
187
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
190     // context.
191     CGContextSaveGState(cgContext);
192     NSAffineTransform *transform = [NSAffineTransform transform];
193     [transform translateXBy:rect.x yBy:rect.y];
194     [transform concat];
195
196     [scroller drawKnob];
197     CGContextRestoreGState(cgContext);
198
199     [scroller release];
200
201     [NSGraphicsContext restoreGraphicsState];
202 }