2 * Copyright 2010, The Android Open Source Project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * 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.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER 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.
26 #define LOG_TAG "WebCore"
29 #include "android_graphics.h"
33 #include "RenderSkinMediaButton.h"
35 #include "SkNinePatch.h"
37 #include <utils/Debug.h>
38 #include <utils/Log.h>
39 #include <wtf/text/CString.h>
43 int8_t outset, margin;
46 static const PatchData gFiles[] =
48 { "btn_media_player.9.png", 0, 0 }, // DEFAULT BGD BUTTON
49 { "ic_media_pause.png", 0, 0}, // PAUSE
50 { "ic_media_play.png", 0, 0 }, // PLAY
51 { "ic_media_pause.png", 0, 0 }, // MUTE
52 { "ic_media_rew.png", 0, 0 }, // REWIND
53 { "ic_media_ff.png", 0, 0 }, // FORWARD
54 { "ic_media_fullscreen.png", 0, 0 }, // FULLSCREEN
55 { "btn_media_player_disabled.9.png", 0, 0 }, // BACKGROUND_SLIDER
56 { "scrubber_track_holo_dark.9.png", 0, 0 }, // SLIDER_TRACK
57 { "scrubber_control_holo.png", 0, 0 } // SLIDER_THUMB
60 static SkBitmap gButton[sizeof(gFiles)/sizeof(gFiles[0])];
66 void RenderSkinMediaButton::Init(android::AssetManager* am, String drawableDirectory)
74 gHighRes = drawableDirectory[drawableDirectory.length() - 5] == 'h';
75 for (size_t i = 0; i < sizeof(gFiles)/sizeof(gFiles[0]); i++) {
76 String path = drawableDirectory + gFiles[i].name;
77 if (!RenderSkinAndroid::DecodeBitmap(am, path.utf8().data(), &gButton[i])) {
79 LOGD("RenderSkinButton::Init: button assets failed to decode\n\tBrowser buttons will not draw");
85 void RenderSkinMediaButton::Draw(SkCanvas* canvas, const IntRect& r, int buttonType, bool translucent)
87 // If we failed to decode, do nothing. This way the browser still works,
88 // and webkit will still draw the label and layout space for us.
93 bool drawsNinePatch = false;
94 bool drawsImage = true;
95 bool drawsBackgroundColor = true;
97 int ninePatchIndex = 0;
101 SkScalar imageMargin = 8;
108 SkColor backgroundColor = SkColorSetARGB(alpha, 34, 34, 34);
109 paint.setColor(backgroundColor);
111 switch (buttonType) {
119 imageIndex = buttonType + 1;
120 paint.setColor(backgroundColor);
123 case BACKGROUND_SLIDER:
125 drawsBackgroundColor = false;
131 drawsNinePatch = true;
133 ninePatchIndex = buttonType + 1;
138 drawsBackgroundColor = false;
140 imageIndex = buttonType + 1;
147 if (drawsBackgroundColor) {
148 canvas->drawRect(r, paint);
151 if (drawsNinePatch) {
152 const PatchData& pd = gFiles[ninePatchIndex];
153 int marginValue = pd.margin + pd.outset;
156 margin.set(marginValue, marginValue, marginValue, marginValue);
157 SkNinePatch::DrawNine(canvas, bounds, gButton[ninePatchIndex], margin);
161 SkScalar SIZE = gButton[imageIndex].width();
162 SkScalar width = r.width();
163 SkScalar scale = SkScalarDiv(width - 2*imageMargin, SIZE);
164 int saveScaleCount = canvas->save();
165 canvas->translate(bounds.fLeft + imageMargin, bounds.fTop + imageMargin);
166 canvas->scale(scale, scale);
167 canvas->drawBitmap(gButton[imageIndex], 0, 0, &paint);
168 canvas->restoreToCount(saveScaleCount);