4303aaa600776278cf8e439e9d33cabc6ba42e4f
[WebKit-https.git] / WebCore / rendering / RenderMediaControls.cpp
1 /*
2  * Copyright (C) 2009 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "RenderMediaControls.h"
28
29 #include "GraphicsContext.h"
30 #include "HTMLMediaElement.h"
31 #include "HTMLNames.h"
32 #include "RenderThemeSafari.h"
33 #include "SoftLinking.h"
34 #include <CoreGraphics/CoreGraphics.h>
35  
36 using namespace std;
37  
38 namespace WebCore {
39
40 #if !defined(NDEBUG) && defined(USE_DEBUG_SAFARI_THEME)
41 SOFT_LINK_DEBUG_LIBRARY(SafariTheme)
42 #else
43 SOFT_LINK_LIBRARY(SafariTheme)
44 #endif
45
46 SOFT_LINK(SafariTheme, paintThemePart, void, __stdcall, (ThemePart part, CGContextRef context, const CGRect& rect, NSControlSize size, ThemeControlState state), (part, context, rect, size, state))
47 SOFT_LINK(SafariTheme, STPaintProgressIndicator, void, APIENTRY, (ProgressIndicatorType type, CGContextRef context, const CGRect& rect, NSControlSize size, ThemeControlState state, float value), (type, context, rect, size, state, value))
48
49 #if ENABLE(VIDEO)
50
51 static ThemeControlState determineState(RenderObject* o)
52 {
53     ThemeControlState result = 0;
54     RenderTheme* theme = o->theme();
55     if (theme->isActive(o))
56         result |= SafariTheme::ActiveState;
57     if (theme->isEnabled(o) && !theme->isReadOnlyControl(o))
58         result |= SafariTheme::EnabledState;
59     if (theme->isPressed(o))
60         result |= SafariTheme::PressedState;
61     if (theme->isChecked(o))
62         result |= SafariTheme::CheckedState;
63     if (theme->isIndeterminate(o))
64         result |= SafariTheme::IndeterminateCheckedState;
65     if (theme->isFocused(o))
66         result |= SafariTheme::FocusedState;
67     if (theme->isDefault(o))
68         result |= SafariTheme::DefaultState;
69     return result;
70 }
71
72 static const int mediaSliderThumbWidth = 13;
73 static const int mediaSliderThumbHeight = 14;
74
75 void RenderMediaControls::adjustMediaSliderThumbSize(RenderObject* o)
76 {
77     if (o->style()->appearance() != MediaSliderThumbPart)
78         return;
79
80     float zoomLevel = o->style()->effectiveZoom();
81     o->style()->setWidth(Length(static_cast<int>(mediaSliderThumbWidth * zoomLevel), Fixed));
82     o->style()->setHeight(Length(static_cast<int>(mediaSliderThumbHeight * zoomLevel), Fixed));
83 }
84
85 bool RenderMediaControls::paintMediaControlsPart(MediaControlElementType part, RenderObject* o, const RenderObject::PaintInfo& paintInfo, const IntRect& r)
86 {
87     ASSERT(SafariThemeLibrary());
88
89     switch (part) {
90         case MediaFullscreenButton:
91             paintThemePart(SafariTheme::MediaFullscreenButtonPart, paintInfo.context->platformContext(), r, NSRegularControlSize, determineState(o));
92             break;
93         case MediaMuteButton:
94         case MediaUnMuteButton:
95             if (MediaControlMuteButtonElement* btn = static_cast<MediaControlMuteButtonElement*>(o->node())) {
96                 bool audioEnabled = btn->displayType() == MediaMuteButton;
97                 paintThemePart(audioEnabled ? SafariTheme::MediaMuteButtonPart : SafariTheme::MediaUnMuteButtonPart, paintInfo.context->platformContext(), r, NSRegularControlSize, determineState(o));
98             }
99             break;
100         case MediaPauseButton:
101         case MediaPlayButton:
102             if (MediaControlPlayButtonElement* btn = static_cast<MediaControlPlayButtonElement*>(o->node())) {
103                 bool canPlay = btn->displayType() == MediaPlayButton;
104                 paintThemePart(canPlay ? SafariTheme::MediaPlayButtonPart : SafariTheme::MediaPauseButtonPart, paintInfo.context->platformContext(), r, NSRegularControlSize, determineState(o));
105             }
106             break;
107         case MediaSeekBackButton:
108             paintThemePart(SafariTheme::MediaSeekBackButtonPart, paintInfo.context->platformContext(), r, NSRegularControlSize, determineState(o));
109             break;
110         case MediaSeekForwardButton:
111             paintThemePart(SafariTheme::MediaSeekForwardButtonPart, paintInfo.context->platformContext(), r, NSRegularControlSize, determineState(o));
112             break;
113         case MediaSlider: {
114             if (HTMLMediaElement* mediaElement = toParentMediaElement(o))
115                 STPaintProgressIndicator(SafariTheme::MediaType, paintInfo.context->platformContext(), r, NSRegularControlSize, 0, mediaElement->percentLoaded());
116             break;
117         }
118         case MediaSliderThumb:
119             paintThemePart(SafariTheme::MediaSliderThumbPart, paintInfo.context->platformContext(), r, NSRegularControlSize, determineState(o));
120             break;
121         case MediaVolumeSliderContainer:
122             // FIXME: Implement volume slider.
123             ASSERT_NOT_REACHED();
124             break;
125         case MediaVolumeSlider:
126             // FIXME: Implement volume slider.
127             ASSERT_NOT_REACHED();
128             break;
129         case MediaVolumeSliderThumb:
130             // FIXME: Implement volume slider.
131             ASSERT_NOT_REACHED();
132             break;
133         case MediaTimelineContainer:
134             ASSERT_NOT_REACHED();
135             break;
136         case MediaCurrentTimeDisplay:
137             ASSERT_NOT_REACHED();
138             break;
139         case MediaTimeRemainingDisplay:
140             ASSERT_NOT_REACHED();
141             break;
142         case MediaControlsPanel:
143             ASSERT_NOT_REACHED();
144             break;
145     }
146     return false;
147 }
148
149 #endif  // #if ENABLE(VIDEO)
150
151 } // namespace WebCore