|
1 /* |
|
2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // Turn a color property value string into |
|
21 // a color. The value is expected to either |
|
22 // be a comma delimited RGB or a system color |
|
23 |
|
24 function colorFromString(laf, colorStr) { |
|
25 if (colorStr == null || colorStr == "") |
|
26 return null; |
|
27 |
|
28 var result = null; |
|
29 var elements = colorStr.split(","); |
|
30 if (elements.length == 3) { |
|
31 var valid = true; |
|
32 for (var i in elements) { |
|
33 var num = parseInt(elements[i]); |
|
34 if (isNaN(num)) |
|
35 valid = false; |
|
36 } |
|
37 if (valid) |
|
38 result = Colors.getColor(elements[0], elements[1], elements[2]); |
|
39 } |
|
40 else { |
|
41 result = laf.getColor(colorStr); |
|
42 } |
|
43 return result; |
|
44 } |
|
45 |
|
46 // Get the effective background color, assuming that everything is transparent |
|
47 // until we get to a component with an attribute describing how its |
|
48 // background will be drawn |
|
49 function getBackgroundColor(instance, laf) { |
|
50 var color = null; |
|
51 while (instance != null) { |
|
52 // children of form and settings list have parent-derived colors |
|
53 if (instance.parent != null && instance.parent.componentId == "com.nokia.sdt.series60.CAknForm") |
|
54 break; |
|
55 if (instance.parent != null && instance.parent.componentId == "com.nokia.sdt.series60.CAknSettingItemList") { |
|
56 color = laf.getColor("CAknSettingItemList.ContentBackground"); |
|
57 break; |
|
58 } |
|
59 |
|
60 var bgProperty = null; |
|
61 var bgColor = null; |
|
62 if (instance.component != null) { |
|
63 bgProperty = instance.component.attributes |
|
64 ["container-background-color-property-name"]; |
|
65 bgColor = instance.component.attributes |
|
66 ["container-background-color"]; |
|
67 } |
|
68 if (bgProperty != null) { |
|
69 color = colorFromString(laf, instance.properties[bgProperty]); |
|
70 if (color != null) { |
|
71 //println("used attribute for " + color); |
|
72 break; |
|
73 } |
|
74 } |
|
75 if (bgColor != null) { |
|
76 color = laf.getColor(bgColor); |
|
77 if (color != null) { |
|
78 //println("used property for " + color); |
|
79 break; |
|
80 } |
|
81 } |
|
82 instance = instance.parent; |
|
83 } |
|
84 if (color == null) { |
|
85 color = laf.getColor("EEikColorWindowBackground"); |
|
86 //println("using background color " + color); |
|
87 if (color == null) { |
|
88 color = Colors.getColor(255, 255, 255); |
|
89 } |
|
90 } |
|
91 return color; |
|
92 } |
|
93 |
|
94 /** |
|
95 * Get the real size of an image from property. |
|
96 * This retrieves the unscaled image, which should |
|
97 * already be cached. |
|
98 * @param instance the instance |
|
99 * @param imageProperty the property, i.e. instance.properties.image |
|
100 * @param multiImageAbstractImageId for a multi-image property, the abstract image id |
|
101 * @return the Rectangle bounds, or null |
|
102 */ |
|
103 function getImageBounds(instance, imageProperty, multiImageAbstractImageId) { |
|
104 var imageRendering = createImagePropertyRendering(); |
|
105 imageRendering.setImageProperty(instance, null, null); |
|
106 imageRendering.setImagePropertySource(imageProperty); |
|
107 if (multiImageAbstractImageId) |
|
108 imageRendering.setMultiImagePropertyAbstractImageId(multiImageAbstractImageId); |
|
109 var img = imageRendering.getImageData(); |
|
110 if (img == null) |
|
111 return null; |
|
112 return new Rectangle(0, 0, img.width, img.height); |
|
113 } |
|
114 |
|
115 /** |
|
116 * Scale a size to fit in a given size |
|
117 * @param insize incoming size to scale |
|
118 * @param size the size to fit |
|
119 * @param preserveAspect true: keep aspect ratio, false: use instance size exactly |
|
120 * @return a Point |
|
121 */ |
|
122 function getSizeScaledToSize(insize, size, preserveAspect) { |
|
123 if (!preserveAspect || size.x == 0 || size.y == 0) { |
|
124 return size; |
|
125 } |
|
126 |
|
127 if (insize.x == 0 || insize.y == 0) |
|
128 return insize; |
|
129 |
|
130 var iw, ih; |
|
131 if (size.x / size.y > insize.x / insize.y) { |
|
132 iw = insize.x * size.y / insize.y; |
|
133 ih = size.y; |
|
134 } else { |
|
135 iw = size.x; |
|
136 ih = insize.y * size.x / insize.x; |
|
137 } |
|
138 var scaled = new Point(iw, ih); |
|
139 //println("scaled to " + scaled); |
|
140 return scaled; |
|
141 } |
|
142 |
|
143 /** |
|
144 * Scale a bounds to fit in a given bounding rectangle, and centered therein. |
|
145 * @param inBounds incoming bounds to scale |
|
146 * @param bounds the bounds to fit |
|
147 * @param preserveAspect true: keep aspect ratio, false: use instance size exactly |
|
148 * @return a Rectangle |
|
149 */ |
|
150 function getBoundsScaledToBounds(inBounds, bounds, preserveAspect) { |
|
151 var size = getSizeScaledToSize(new Point(inBounds.width, inBounds.height), |
|
152 new Point(bounds.width, bounds.height), |
|
153 preserveAspect); |
|
154 |
|
155 var scaled = new Rectangle((bounds.width - size.x) / 2, (bounds.height - size.y) / 2, |
|
156 size.x, size.y); |
|
157 //println("scaled to " + scaled); |
|
158 return scaled; |
|
159 } |
|
160 |
|
161 // Draw an image into the given rectangle in the gc |
|
162 // @param instance the instance |
|
163 // @param graphics the GC |
|
164 // @param rect the rectangle to draw to. If the rectangle is null |
|
165 // or the width/height are 0, no scaling is performed. |
|
166 // The x/y offset are used, if non-null, to offset the image. |
|
167 // @param doBlend true: blend smoothly with background (only works with solid background) |
|
168 // @return the Image, or null |
|
169 function drawImage(instance, graphics, image, rect, doBlend) { |
|
170 if (image) { |
|
171 // show image in dialog |
|
172 //var dump = new ImageDump(null, image); |
|
173 //dump.open(); |
|
174 |
|
175 var imgRect = image.getBounds() |
|
176 //println("image is " + imgRect); |
|
177 |
|
178 var blended = ImageUtils.flattenAlphaMaskedImage(graphics.getDevice(), image, |
|
179 graphics.getBackground(), doBlend, true /* transparent */); |
|
180 |
|
181 // show blended image in dialog |
|
182 //var dump = new ImageDump(null, blended); |
|
183 //dump.open(); |
|
184 |
|
185 if (rect) { |
|
186 var imgRect = blended.getBounds() |
|
187 if (rect.width != 0 && rect.height != 0) |
|
188 graphics.drawImage(blended, 0, 0, imgRect.width, imgRect.height, rect.x, rect.y, rect.width, rect.height); |
|
189 else |
|
190 graphics.drawImage(blended, 0, 0, imgRect.width, imgRect.height, rect.x, rect.y, imgRect.width, imgRect.height); |
|
191 } else { |
|
192 graphics.drawImage(blended, 0, 0); |
|
193 } |
|
194 |
|
195 blended.dispose(); |
|
196 } |
|
197 } |
|
198 |
|
199 |
|
200 /** |
|
201 * Get the height of a font which properly encompasses its leading, |
|
202 * descent, and ascent. font.getHeight() is not quite accurate, for some reason... |
|
203 */ |
|
204 function getFontHeight(font) { |
|
205 return font.formattedStringExtent("x", new Point(0, 0), 0, 0).y; |
|
206 } |
|
207 |
|
208 /** |
|
209 * Render an image to the gc |
|
210 * @param prototype a prototype implementing IImagePropertyRenderingInfo |
|
211 * @param instance the component instance holding the image property |
|
212 * @param laf the look and feel information |
|
213 * @param graphics the GC |
|
214 * @param x x offset of image (left) |
|
215 * @param y y offset of image (top) |
|
216 * @param propertyId the property path of the image compound property |
|
217 * @param doBlend true: blend image with background when flattening alpha |
|
218 * @param multiImageAbstractImageId if non-null, then the image property houses multiple images, and draw this one |
|
219 * (@see IMultiImagePropertyInfo) |
|
220 */ |
|
221 function renderImage(prototype, instance, laf, graphics, x, y, propertyId, doBlend, multiImageAbstractImageId) { |
|
222 var imagePropertyRendering = createImagePropertyRendering(); |
|
223 imagePropertyRendering.setImageProperty(instance, propertyId, laf); |
|
224 imagePropertyRendering.setViewableSize(prototype.getViewableSize(instance, propertyId, laf)); |
|
225 imagePropertyRendering.setAlignmentWeights(prototype.getAlignmentWeights(instance, propertyId, laf)); |
|
226 imagePropertyRendering.setScaling(prototype.isScaling(instance, propertyId, laf)); |
|
227 imagePropertyRendering.setPreservingAspectRatio(prototype.isPreservingAspectRatio(instance, propertyId, laf)); |
|
228 |
|
229 imagePropertyRendering.setTransparencyHandling(doBlend |
|
230 ? imagePropertyRendering.TRANSPARENCY_FLATTEN_AND_BLEND |
|
231 : imagePropertyRendering.TRANSPARENCY_FLATTEN); |
|
232 |
|
233 if (multiImageAbstractImageId) |
|
234 imagePropertyRendering.setMultiImagePropertyAbstractImageId(multiImageAbstractImageId); |
|
235 |
|
236 imagePropertyRendering.render(graphics.getWrappedGC(), x, y); |
|
237 } |
|
238 |
|
239 |
|
240 /** |
|
241 * Get the bounds consumed by wrappable text, given a limiting width and |
|
242 * maximum number of lines. This detects newlines embedded in text. |
|
243 * @param string the text to measure |
|
244 * @param width width in pixels |
|
245 * @param font the font |
|
246 * @param flags mask of IFont.XXX flags (wrapping flags ignored!) |
|
247 * @param lineGap pixel gap b/t lines |
|
248 * @param maxLines maximum # lines to emit |
|
249 * @return Point (maxWidthUsed, requiredHeight) |
|
250 */ |
|
251 function calculateWrappedTextSize(string, width, font, flags, lineGap, maxLines) { |
|
252 var lines = TextRendering.formatIntoLines(font, string, width, flags, maxLines); |
|
253 var fontHeight = font.getHeight(); |
|
254 var gappedLineHeight = fontHeight + lineGap; |
|
255 var maxWidth = 0; |
|
256 for (var i in lines) { |
|
257 var line = lines[i]; |
|
258 maxWidth = Math.max(maxWidth, font.stringExtent(line).x); |
|
259 } |
|
260 return new Point(maxWidth, lines.length * gappedLineHeight); |
|
261 } |