1 /*
2 * Copyright 2006, The Android Open Source Project
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 * * 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.
12 *
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.
24 */
25
26 #include "config.h"
27 #include "RenderSkinRadio.h"
28
29 #include "android_graphics.h"
30 #include "Document.h"
31 #include "Element.h"
32 #include "InputElement.h"
33 #include "IntRect.h"
34 #include "Node.h"
35 #include "RenderSkinAndroid.h"
36 #include "SkBitmap.h"
37 #include "SkCanvas.h"
38 #include "SkRect.h"
39 #include <utils/AssetManager.h>
40 #include <wtf/text/CString.h>
41
42 extern android::AssetManager* globalAssetManager();
43
44 static const char* checks[] = { "btn_check_off_holo.png",
45 "btn_check_on_holo.png",
46 "btn_radio_off_holo.png",
47 "btn_radio_on_holo.png"};
48 // Matches the width of the bitmap
49 static SkScalar s_bitmapWidth;
50
51 namespace WebCore {
52
53 static SkBitmap s_bitmap[4];
54 static bool s_decodingAttempted = false;
55 static bool s_decoded = false;
56
Decode()57 void RenderSkinRadio::Decode() {
58 if (s_decodingAttempted)
59 return;
60
61 s_decodingAttempted = true;
62 s_decoded = false;
63
64 android::AssetManager* am = globalAssetManager();
65 String drawableDir = RenderSkinAndroid::DrawableDirectory();
66 for (int i = 0; i < 4; i++) {
67 String path = drawableDir + checks[i];
68 if (!RenderSkinAndroid::DecodeBitmap(am, path.utf8().data(), &s_bitmap[i]))
69 return;
70 }
71 s_decoded = true;
72 s_bitmapWidth = SkIntToScalar(s_bitmap[0].width());
73 }
74
Draw(SkCanvas * canvas,Node * element,const IntRect & ir,bool isCheckBox)75 void RenderSkinRadio::Draw(SkCanvas* canvas, Node* element, const IntRect& ir,
76 bool isCheckBox)
77 {
78 if (!element)
79 return;
80
81 if (!s_decodingAttempted)
82 Decode();
83
84 if (!s_decoded)
85 return;
86
87 SkRect r(ir);
88 // Set up a paint to with filtering to look better.
89 SkPaint paint;
90 paint.setFlags(SkPaint::kFilterBitmap_Flag);
91 int saveScaleCount = 0;
92
93 if (!element->isElementNode() ||
94 !static_cast<Element*>(element)->isEnabledFormControl()) {
95 paint.setAlpha(0x80);
96 }
97 SkScalar width = r.width();
98 SkScalar scale = SkScalarDiv(width, s_bitmapWidth);
99 saveScaleCount = canvas->save();
100 canvas->translate(r.fLeft, r.fTop);
101 canvas->scale(scale, scale);
102
103 bool checked = false;
104 if (InputElement* inputElement = element->toInputElement()) {
105 checked = inputElement->isChecked();
106 }
107
108 canvas->drawBitmap(s_bitmap[checked + 2*(!isCheckBox)],
109 0, 0, &paint);
110 canvas->restoreToCount(saveScaleCount);
111 }
112
113 } //WebCore
114