2 * Copyright (C) 2009 Apple Inc.
3 * Copyright (C) 2009 Google Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "RenderMediaControlsChromium.h"
32 #include "GraphicsContext.h"
33 #include "HTMLMediaElement.h"
34 #include "HTMLNames.h"
35 #include "PaintInfo.h"
41 typedef WTF::HashMap<const char*, Image*> MediaControlImageMap;
42 static MediaControlImageMap* gMediaControlImageMap = 0;
44 static Image* platformResource(const char* name)
46 if (!gMediaControlImageMap)
47 gMediaControlImageMap = new MediaControlImageMap();
48 if (Image* image = gMediaControlImageMap->get(name))
50 if (Image* image = Image::loadPlatformResource(name).releaseRef()) {
51 gMediaControlImageMap->set(name, image);
58 static bool hasSource(const HTMLMediaElement* mediaElement)
60 return mediaElement->networkState() != HTMLMediaElement::NETWORK_EMPTY
61 && mediaElement->networkState() != HTMLMediaElement::NETWORK_NO_SOURCE;
64 static bool paintMediaButton(GraphicsContext* context, const IntRect& rect, Image* image)
66 IntRect imageRect = image->rect();
67 context->drawImage(image, ColorSpaceDeviceRGB, rect);
71 static bool paintMediaMuteButton(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
73 HTMLMediaElement* mediaElement = toParentMediaElement(object);
77 static Image* soundFull = platformResource("mediaSoundFull");
78 static Image* soundNone = platformResource("mediaSoundNone");
79 static Image* soundDisabled = platformResource("mediaSoundDisabled");
81 if (!hasSource(mediaElement) || !mediaElement->hasAudio())
82 return paintMediaButton(paintInfo.context, rect, soundDisabled);
84 return paintMediaButton(paintInfo.context, rect, mediaElement->muted() ? soundNone: soundFull);
87 static bool paintMediaPlayButton(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
89 HTMLMediaElement* mediaElement = toParentMediaElement(object);
93 static Image* mediaPlay = platformResource("mediaPlay");
94 static Image* mediaPause = platformResource("mediaPause");
95 static Image* mediaPlayDisabled = platformResource("mediaPlayDisabled");
97 if (!hasSource(mediaElement))
98 return paintMediaButton(paintInfo.context, rect, mediaPlayDisabled);
100 return paintMediaButton(paintInfo.context, rect, mediaElement->canPlay() ? mediaPlay : mediaPause);
103 static Image* getMediaSliderThumb()
105 static Image* mediaSliderThumb = platformResource("mediaSliderThumb");
106 return mediaSliderThumb;
109 static bool paintMediaSlider(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
111 HTMLMediaElement* mediaElement = toParentMediaElement(object);
115 RenderStyle* style = object->style();
116 GraphicsContext* context = paintInfo.context;
118 // Draw the border of the time bar.
119 // FIXME: this should be a rounded rect but need to fix GraphicsContextSkia first.
120 // https://bugs.webkit.org/show_bug.cgi?id=30143
122 context->setShouldAntialias(true);
123 context->setStrokeStyle(SolidStroke);
124 context->setStrokeColor(style->visitedDependentColor(CSSPropertyBorderLeftColor), ColorSpaceDeviceRGB);
125 context->setStrokeThickness(style->borderLeftWidth());
126 context->setFillColor(style->visitedDependentColor(CSSPropertyBackgroundColor), ColorSpaceDeviceRGB);
127 context->drawRect(rect);
130 // Draw the buffered ranges.
131 // FIXME: Draw multiple ranges if there are multiple buffered ranges.
132 IntRect bufferedRect = rect;
133 bufferedRect.inflate(-style->borderLeftWidth());
135 double bufferedWidth = 0.0;
136 if (mediaElement->percentLoaded() > 0.0) {
137 // Account for the width of the slider thumb.
138 Image* mediaSliderThumb = getMediaSliderThumb();
139 double thumbWidth = mediaSliderThumb->width() / 2.0 + 1.0;
140 double rectWidth = bufferedRect.width() - thumbWidth;
143 bufferedWidth = rectWidth * mediaElement->percentLoaded() + thumbWidth;
145 bufferedRect.setWidth(bufferedWidth);
147 // Don't bother drawing an empty area.
148 if (!bufferedRect.isEmpty()) {
149 IntPoint sliderTopLeft = bufferedRect.location();
150 IntPoint sliderTopRight = sliderTopLeft;
151 sliderTopRight.move(0, bufferedRect.height());
153 RefPtr<Gradient> gradient = Gradient::create(sliderTopLeft, sliderTopRight);
154 Color startColor = object->style()->visitedDependentColor(CSSPropertyColor);
155 gradient->addColorStop(0.0, startColor);
156 gradient->addColorStop(1.0, Color(startColor.red() / 2, startColor.green() / 2, startColor.blue() / 2, startColor.alpha()));
159 context->setStrokeStyle(NoStroke);
160 context->setFillGradient(gradient);
161 context->fillRect(bufferedRect);
168 static bool paintMediaSliderThumb(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
170 ASSERT(object->node());
171 Node* hostNode = object->node()->shadowAncestorNode();
173 HTMLMediaElement* mediaElement = toParentMediaElement(hostNode);
177 if (!hasSource(mediaElement))
180 Image* mediaSliderThumb = getMediaSliderThumb();
181 return paintMediaButton(paintInfo.context, rect, mediaSliderThumb);
184 static bool paintMediaVolumeSlider(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
186 HTMLMediaElement* mediaElement = toParentMediaElement(object);
190 GraphicsContext* context = paintInfo.context;
191 Color originalColor = context->strokeColor();
192 if (originalColor != Color::white)
193 context->setStrokeColor(Color::white, ColorSpaceDeviceRGB);
195 int x = rect.x() + rect.width() / 2;
196 context->drawLine(IntPoint(x, rect.y()), IntPoint(x, rect.y() + rect.height()));
198 if (originalColor != Color::white)
199 context->setStrokeColor(originalColor, ColorSpaceDeviceRGB);
203 static bool paintMediaVolumeSliderThumb(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
205 static Image* mediaVolumeSliderThumb = platformResource("mediaVolumeSliderThumb");
206 return paintMediaButton(paintInfo.context, rect, mediaVolumeSliderThumb);
209 static bool paintMediaTimelineContainer(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
211 HTMLMediaElement* mediaElement = toParentMediaElement(object);
215 if (!rect.isEmpty()) {
216 GraphicsContext* context = paintInfo.context;
217 Color originalColor = context->strokeColor();
218 float originalThickness = context->strokeThickness();
219 StrokeStyle originalStyle = context->strokeStyle();
221 context->setStrokeStyle(SolidStroke);
223 // Draw the left border using CSS defined width and color.
224 context->setStrokeThickness(object->style()->borderLeftWidth());
225 context->setStrokeColor(object->style()->visitedDependentColor(CSSPropertyBorderLeftColor).rgb(), ColorSpaceDeviceRGB);
226 context->drawLine(IntPoint(rect.x() + 1, rect.y()),
227 IntPoint(rect.x() + 1, rect.y() + rect.height()));
229 // Draw the right border using CSS defined width and color.
230 context->setStrokeThickness(object->style()->borderRightWidth());
231 context->setStrokeColor(object->style()->visitedDependentColor(CSSPropertyBorderRightColor).rgb(), ColorSpaceDeviceRGB);
232 context->drawLine(IntPoint(rect.x() + rect.width() - 1, rect.y()),
233 IntPoint(rect.x() + rect.width() - 1, rect.y() + rect.height()));
235 context->setStrokeColor(originalColor, ColorSpaceDeviceRGB);
236 context->setStrokeThickness(originalThickness);
237 context->setStrokeStyle(originalStyle);
242 bool RenderMediaControlsChromium::paintMediaControlsPart(MediaControlElementType part, RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
245 case MediaMuteButton:
246 case MediaUnMuteButton:
247 return paintMediaMuteButton(object, paintInfo, rect);
248 case MediaPauseButton:
249 case MediaPlayButton:
250 return paintMediaPlayButton(object, paintInfo, rect);
252 return paintMediaSlider(object, paintInfo, rect);
253 case MediaSliderThumb:
254 return paintMediaSliderThumb(object, paintInfo, rect);
255 case MediaVolumeSlider:
256 return paintMediaVolumeSlider(object, paintInfo, rect);
257 case MediaVolumeSliderThumb:
258 return paintMediaVolumeSliderThumb(object, paintInfo, rect);
259 case MediaTimelineContainer:
260 return paintMediaTimelineContainer(object, paintInfo, rect);
261 case MediaVolumeSliderMuteButton:
262 case MediaFullscreenButton:
263 case MediaSeekBackButton:
264 case MediaSeekForwardButton:
265 case MediaVolumeSliderContainer:
266 case MediaCurrentTimeDisplay:
267 case MediaTimeRemainingDisplay:
268 case MediaControlsPanel:
269 case MediaRewindButton:
270 case MediaReturnToRealtimeButton:
271 case MediaStatusDisplay:
272 case MediaShowClosedCaptionsButton:
273 case MediaHideClosedCaptionsButton:
274 ASSERT_NOT_REACHED();
280 void RenderMediaControlsChromium::adjustMediaSliderThumbSize(RenderStyle* style)
282 static Image* mediaSliderThumb = platformResource("mediaSliderThumb");
283 static Image* mediaVolumeSliderThumb = platformResource("mediaVolumeSliderThumb");
285 Image* thumbImage = 0;
286 if (style->appearance() == MediaSliderThumbPart)
287 thumbImage = mediaSliderThumb;
288 else if (style->appearance() == MediaVolumeSliderThumbPart)
289 thumbImage = mediaVolumeSliderThumb;
291 float zoomLevel = style->effectiveZoom();
293 style->setWidth(Length(static_cast<int>(thumbImage->width() * zoomLevel), Fixed));
294 style->setHeight(Length(static_cast<int>(thumbImage->height() * zoomLevel), Fixed));
298 #endif // #if ENABLE(VIDEO)
300 } // namespace WebCore