1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/*
* Copyright 2006, The Android Open Source Project
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "RenderSkinCombo.h"
#include "Document.h"
#include "Element.h"
#include "Node.h"
#include "NodeRenderStyle.h"
#include "RenderStyle.h"
#include "SkCanvas.h"
#include "SkNinePatch.h"
#include <utils/AssetManager.h>
#include <wtf/text/CString.h>
extern android::AssetManager* globalAssetManager();
namespace WebCore {
// Indicates if the entire asset is being drawn, or if the border is being
// excluded and just the arrow drawn.
enum BorderStyle {
FullAsset,
NoBorder,
BorderStyleCount // Keep at the end.
};
// There are 2.5 different concepts of a 'border' here, which results
// in rather a lot of magic constants.
// Firstly, we have the extra padding that webkit needs to know about,
// which defines how much bigger this element is made by the
// asset. This is actually a bit broader than the actual border on the
// asset, to make things look less cramped. The border is the same
// width on all sides, except on the right when it's significantly
// wider to allow for the arrow.
const int RenderSkinCombo::arrowMargin[ResolutionCount] = {
22, // Medium resolution
34, // High resolution
46 // Extra high resolution
};
const int RenderSkinCombo::padMargin[ResolutionCount] = {
2, // Medium resolution
5, // High resolution
6 // Extra high resolution
};
namespace {
// Then we have the borders used for the 9-patch stretch. The
// rectangle at the centre of these borders is entirely below and to
// the left of the arrow in the asset. Hence the border widths are the
// same for the bottom and left, but are different for the top. The
// right hand border width happens to be the same as arrowMargin
// defined above.
const int stretchMargin[RenderSkinAndroid::ResolutionCount] = { // border width for the bottom and left of the 9-patch
3, // Medium resolution
5, // High resolution
6 // Extra high resolution
};
const int stretchTop[RenderSkinAndroid::ResolutionCount] = { // border width for the top of the 9-patch
15, // Medium resolution
23, // High resolution
34 // Extra high resolution
};
// Finally, if the border is defined by the CSS, we only draw the
// arrow and not the border. We do this by drawing the relevant subset
// of the bitmap, which must now be precisely determined by what's in
// the asset with no extra padding to make things look properly
// spaced. The border to remove at the top, right and bottom of the
// image is the same as stretchMargin above, but we need to know the width
// of the arrow.
const int arrowWidth[RenderSkinAndroid::ResolutionCount] = {
22, // Medium resolution
31, // High resolution
42 // Extra high resolution
};
// Store the calculated 9 patch margins for each border style.
SkIRect margin[BorderStyleCount];
SkBitmap bitmaps[2][BorderStyleCount]; // Collection of assets for a combo box - 2 states (enabled/disabled)
bool isDecodingAttempted = false; // True if we've tried to decode the assets
bool isDecoded = false; // True if all assets were decoded
} // namespace
void RenderSkinCombo::Decode()
{
if (isDecodingAttempted)
return;
isDecodingAttempted = true;
isDecoded = false;
android::AssetManager* am = globalAssetManager();
String drawableDirectory = RenderSkinAndroid::DrawableDirectory();
Resolution res = RenderSkinAndroid::DrawableResolution();
isDecoded = RenderSkinAndroid::DecodeBitmap(am, (drawableDirectory + "combobox_nohighlight.png").utf8().data(), &bitmaps[kNormal][FullAsset]);
isDecoded &= RenderSkinAndroid::DecodeBitmap(am, (drawableDirectory + "combobox_disabled.png").utf8().data(), &bitmaps[kDisabled][FullAsset]);
int width = bitmaps[kNormal][FullAsset].width();
int height = bitmaps[kNormal][FullAsset].height();
SkIRect subset;
subset.set(width - arrowWidth[res], 0, width, height);
bitmaps[kNormal][FullAsset].extractSubset(&bitmaps[kNormal][NoBorder], subset);
bitmaps[kDisabled][FullAsset].extractSubset(&bitmaps[kDisabled][NoBorder], subset);
// Calculate 9 patch margins.
SkIRect fullAssetMargin;
fullAssetMargin.fLeft = stretchMargin[res];
fullAssetMargin.fTop = stretchTop[res];
fullAssetMargin.fRight = arrowMargin[res] + stretchMargin[res];
fullAssetMargin.fBottom = stretchMargin[res];
SkIRect noBorderMargin;
noBorderMargin.fLeft = 0;
noBorderMargin.fTop = stretchTop[res];
noBorderMargin.fRight = 0;
noBorderMargin.fBottom = stretchMargin[res];
margin[FullAsset] = fullAssetMargin;
margin[NoBorder] = noBorderMargin;
}
bool RenderSkinCombo::Draw(SkCanvas* canvas, Node* element, int x, int y, int width, int height)
{
if (!isDecodingAttempted)
Decode();
if (!isDecoded)
return true;
State state = (element->isElementNode() && static_cast<Element*>(element)->isEnabledFormControl()) ? kNormal : kDisabled;
height = std::max(height, (stretchMargin[RenderSkinAndroid::DrawableResolution()]<<1) + 1);
SkRect bounds;
BorderStyle drawBorder = FullAsset;
bounds.set(SkIntToScalar(x+1), SkIntToScalar(y+1), SkIntToScalar(x + width-1), SkIntToScalar(y + height-1));
RenderStyle* style = element->renderStyle();
SkPaint paint;
paint.setColor(style->visitedDependentColor(CSSPropertyBackgroundColor).rgb());
canvas->drawRect(bounds, paint);
bounds.set(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + width), SkIntToScalar(y + height));
// If this is an appearance where RenderTheme::paint returns true
// without doing anything, this means that
// RenderBox::PaintBoxDecorationWithSize will end up painting the
// border, so we shouldn't paint a border here.
if (style->appearance() == MenulistButtonPart ||
style->appearance() == ListboxPart ||
style->appearance() == TextFieldPart ||
style->appearance() == TextAreaPart) {
bounds.fLeft += SkIntToScalar(width - RenderSkinCombo::extraWidth());
bounds.fRight -= SkIntToScalar(style->borderRightWidth());
bounds.fTop += SkIntToScalar(style->borderTopWidth());
bounds.fBottom -= SkIntToScalar(style->borderBottomWidth());
drawBorder = NoBorder;
}
SkNinePatch::DrawNine(canvas, bounds, bitmaps[state][drawBorder], margin[drawBorder]);
return false;
}
} // namspace WebCore
|