initial import
[vuplus_webkit] / Source / WebCore / rendering / RenderMediaControlsChromium.cpp
1 /*
2  * Copyright (C) 2009 Apple Inc.
3  * Copyright (C) 2009 Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
26  */
27
28 #include "config.h"
29 #include "RenderMediaControlsChromium.h"
30
31 #include "Gradient.h"
32 #include "GraphicsContext.h"
33 #include "HTMLMediaElement.h"
34 #include "HTMLNames.h"
35 #include "PaintInfo.h"
36
37 namespace WebCore {
38
39 #if ENABLE(VIDEO)
40
41 typedef WTF::HashMap<const char*, Image*> MediaControlImageMap;
42 static MediaControlImageMap* gMediaControlImageMap = 0;
43
44 static Image* platformResource(const char* name)
45 {
46     if (!gMediaControlImageMap)
47         gMediaControlImageMap = new MediaControlImageMap();
48     if (Image* image = gMediaControlImageMap->get(name))
49         return image;
50     if (Image* image = Image::loadPlatformResource(name).releaseRef()) {
51         gMediaControlImageMap->set(name, image);
52         return image;
53     }
54     ASSERT_NOT_REACHED();
55     return 0;
56 }
57
58 static bool hasSource(const HTMLMediaElement* mediaElement)
59 {
60     return mediaElement->networkState() != HTMLMediaElement::NETWORK_EMPTY
61         && mediaElement->networkState() != HTMLMediaElement::NETWORK_NO_SOURCE;
62 }
63
64 static bool paintMediaButton(GraphicsContext* context, const IntRect& rect, Image* image)
65 {
66     IntRect imageRect = image->rect();
67     context->drawImage(image, ColorSpaceDeviceRGB, rect);
68     return true;
69 }
70
71 static bool paintMediaMuteButton(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
72 {
73     HTMLMediaElement* mediaElement = toParentMediaElement(object);
74     if (!mediaElement)
75       return false;
76
77     static Image* soundFull = platformResource("mediaSoundFull");
78     static Image* soundNone = platformResource("mediaSoundNone");
79     static Image* soundDisabled = platformResource("mediaSoundDisabled");
80
81     if (!hasSource(mediaElement) || !mediaElement->hasAudio())
82         return paintMediaButton(paintInfo.context, rect, soundDisabled);
83
84     return paintMediaButton(paintInfo.context, rect, mediaElement->muted() ? soundNone: soundFull);
85 }
86
87 static bool paintMediaPlayButton(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
88 {
89     HTMLMediaElement* mediaElement = toParentMediaElement(object);
90     if (!mediaElement)
91         return false;
92
93     static Image* mediaPlay = platformResource("mediaPlay");
94     static Image* mediaPause = platformResource("mediaPause");
95     static Image* mediaPlayDisabled = platformResource("mediaPlayDisabled");
96
97     if (!hasSource(mediaElement))
98         return paintMediaButton(paintInfo.context, rect, mediaPlayDisabled);
99
100     return paintMediaButton(paintInfo.context, rect, mediaElement->canPlay() ? mediaPlay : mediaPause);
101 }
102
103 static Image* getMediaSliderThumb()
104 {
105     static Image* mediaSliderThumb = platformResource("mediaSliderThumb");
106     return mediaSliderThumb;
107 }
108
109 static bool paintMediaSlider(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
110 {
111     HTMLMediaElement* mediaElement = toParentMediaElement(object);
112     if (!mediaElement)
113         return false;
114
115     RenderStyle* style = object->style();
116     GraphicsContext* context = paintInfo.context;
117
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
121     context->save();
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);
128     context->restore();
129
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());
134
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;
141         if (rectWidth < 0.0)
142             rectWidth = 0.0;
143         bufferedWidth = rectWidth * mediaElement->percentLoaded() + thumbWidth;
144     }
145     bufferedRect.setWidth(bufferedWidth);
146
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());
152
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()));
157
158         context->save();
159         context->setStrokeStyle(NoStroke);
160         context->setFillGradient(gradient);
161         context->fillRect(bufferedRect);
162         context->restore();
163     }
164
165     return true;
166 }
167
168 static bool paintMediaSliderThumb(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
169 {
170     ASSERT(object->node());
171     Node* hostNode = object->node()->shadowAncestorNode();
172     ASSERT(hostNode);
173     HTMLMediaElement* mediaElement = toParentMediaElement(hostNode);
174     if (!mediaElement)
175         return false;
176
177     if (!hasSource(mediaElement))
178         return true;
179
180     Image* mediaSliderThumb = getMediaSliderThumb();
181     return paintMediaButton(paintInfo.context, rect, mediaSliderThumb);
182 }
183
184 static bool paintMediaVolumeSlider(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
185 {
186     HTMLMediaElement* mediaElement = toParentMediaElement(object);
187     if (!mediaElement)
188         return false;
189
190     GraphicsContext* context = paintInfo.context;
191     Color originalColor = context->strokeColor();
192     if (originalColor != Color::white)
193         context->setStrokeColor(Color::white, ColorSpaceDeviceRGB);
194
195     int x = rect.x() + rect.width() / 2;
196     context->drawLine(IntPoint(x, rect.y()),  IntPoint(x, rect.y() + rect.height()));
197
198     if (originalColor != Color::white)
199         context->setStrokeColor(originalColor, ColorSpaceDeviceRGB);
200     return true;
201 }
202
203 static bool paintMediaVolumeSliderThumb(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
204 {
205     static Image* mediaVolumeSliderThumb = platformResource("mediaVolumeSliderThumb");
206     return paintMediaButton(paintInfo.context, rect, mediaVolumeSliderThumb);
207 }
208
209 static bool paintMediaTimelineContainer(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
210 {
211     HTMLMediaElement* mediaElement = toParentMediaElement(object);
212     if (!mediaElement)
213         return false;
214
215     if (!rect.isEmpty()) {
216         GraphicsContext* context = paintInfo.context;
217         Color originalColor = context->strokeColor();
218         float originalThickness = context->strokeThickness();
219         StrokeStyle originalStyle = context->strokeStyle();
220
221         context->setStrokeStyle(SolidStroke);
222
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()));
228
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()));
234
235         context->setStrokeColor(originalColor, ColorSpaceDeviceRGB);
236         context->setStrokeThickness(originalThickness);
237         context->setStrokeStyle(originalStyle);
238     }
239     return true;
240 }
241
242 bool RenderMediaControlsChromium::paintMediaControlsPart(MediaControlElementType part, RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
243 {
244     switch (part) {
245     case MediaMuteButton:
246     case MediaUnMuteButton:
247         return paintMediaMuteButton(object, paintInfo, rect);
248     case MediaPauseButton:
249     case MediaPlayButton:
250         return paintMediaPlayButton(object, paintInfo, rect);
251     case MediaSlider:
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();
275         break;
276     }
277     return false;
278 }
279
280 void RenderMediaControlsChromium::adjustMediaSliderThumbSize(RenderStyle* style)
281 {
282     static Image* mediaSliderThumb = platformResource("mediaSliderThumb");
283     static Image* mediaVolumeSliderThumb = platformResource("mediaVolumeSliderThumb");
284
285     Image* thumbImage = 0;
286     if (style->appearance() == MediaSliderThumbPart)
287         thumbImage = mediaSliderThumb;
288     else if (style->appearance() == MediaVolumeSliderThumbPart)
289         thumbImage = mediaVolumeSliderThumb;
290
291     float zoomLevel = style->effectiveZoom();
292     if (thumbImage) {
293         style->setWidth(Length(static_cast<int>(thumbImage->width() * zoomLevel), Fixed));
294         style->setHeight(Length(static_cast<int>(thumbImage->height() * zoomLevel), Fixed));
295     }
296 }
297
298 #endif  // #if ENABLE(VIDEO)
299
300 } // namespace WebCore