2009-06-04 Eric Carlson <eric.carlson@apple.com>
[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     if (theme()->isActive(o))
55         result |= SafariTheme::ActiveState;
56     if (theme()->isEnabled(o) && !theme()->isReadOnlyControl(o))
57         result |= SafariTheme::EnabledState;
58     if (theme()->isPressed(o))
59         result |= SafariTheme::PressedState;
60     if (theme()->isChecked(o))
61         result |= SafariTheme::CheckedState;
62     if (theme()->isIndeterminate(o))
63         result |= SafariTheme::IndeterminateCheckedState;
64     if (theme()->isFocused(o))
65         result |= SafariTheme::FocusedState;
66     if (theme()->isDefault(o))
67         result |= SafariTheme::DefaultState;
68     return result;
69 }
70
71 static const int mediaSliderThumbWidth = 13;
72 static const int mediaSliderThumbHeight = 14;
73
74 void RenderMediaControls::adjustMediaSliderThumbSize(RenderObject* o)
75 {
76     if (o->style()->appearance() != MediaSliderThumbPart)
77         return;
78
79     float zoomLevel = o->style()->effectiveZoom();
80     o->style()->setWidth(Length(static_cast<int>(mediaSliderThumbWidth * zoomLevel), Fixed));
81     o->style()->setHeight(Length(static_cast<int>(mediaSliderThumbHeight * zoomLevel), Fixed));
82 }
83
84 static HTMLMediaElement* parentMediaElement(RenderObject* o)
85 {
86     Node* node = o->node();
87     Node* mediaNode = node ? node->shadowAncestorNode() : 0;
88     if (!mediaNode || (!mediaNode->hasTagName(HTMLNames::videoTag) && !mediaNode->hasTagName(HTMLNames::audioTag)))
89         return 0;
90
91     return static_cast<HTMLMediaElement*>(mediaNode);
92 }
93
94 bool RenderMediaControls::paintMediaControlsPart(MediaControlElementType part, RenderObject* o, const RenderObject::PaintInfo& paintInfo, const IntRect& r)
95 {
96     ASSERT(SafariThemeLibrary());
97
98     switch (part) {
99         case MediaFullscreenButton:
100             paintThemePart(SafariTheme::MediaFullscreenButtonPart, paintInfo.context->platformContext(), r, NSRegularControlSize, determineState(o));
101             break;
102         case MediaMuteButton:
103         case MediaUnMuteButton:
104             if (HTMLMediaElement* mediaElement = parentMediaElement(o))
105                 paintThemePart(mediaElement->muted() ? SafariTheme::MediaUnMuteButtonPart : SafariTheme::MediaMuteButtonPart, paintInfo.context->platformContext(), r, NSRegularControlSize, determineState(o));
106             break;
107         case MediaPauseButton:
108         case MediaPlayButton:
109             if (HTMLMediaElement* mediaElement = parentMediaElement(o))
110                 paintThemePart(mediaElement->canPlay() ? SafariTheme::MediaPlayButtonPart : SafariTheme::MediaPauseButtonPart, paintInfo.context->platformContext(), r, NSRegularControlSize, determineState(o));
111             break;
112         case MediaSeekBackButton:
113             paintThemePart(SafariTheme::MediaSeekBackButtonPart, paintInfo.context->platformContext(), r, NSRegularControlSize, determineState(o));
114             break;
115         case MediaSeekForwardButton:
116             paintThemePart(SafariTheme::MediaSeekForwardButtonPart, paintInfo.context->platformContext(), r, NSRegularControlSize, determineState(o));
117             break;
118         case MediaSlider: {
119             HTMLMediaElement* mediaElement = parentMediaElement(o);
120             if (!mediaElement)
121                 break;
122
123             MediaPlayer* player = mediaElement->player();
124             float duration = player ? player->duration() : 0;
125             float percentLoaded = duration ? player->maxTimeBuffered() /duration : 0;
126
127             STPaintProgressIndicator(SafariTheme::MediaType, paintInfo.context->platformContext(), r, NSRegularControlSize, 0, percentLoaded);
128             break;
129         }
130         case MediaSliderThumb:
131             paintThemePart(SafariTheme::MediaSliderThumbPart, paintInfo.context->platformContext(), r, NSRegularControlSize, determineState(o));
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
152