2
|
1 |
/*
|
|
2 |
* Copyright (c) 2007 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 |
include("../implLibrary.js")
|
|
20 |
include("../renderLibrary.js")
|
|
21 |
|
|
22 |
function CAknButton() {
|
|
23 |
}
|
|
24 |
|
|
25 |
CAknButton.prototype.draw = function(instance, laf, graphics) {
|
|
26 |
var borderPadding = 3;
|
|
27 |
var properties = instance.properties;
|
|
28 |
var width = properties.size.width - borderPadding;
|
|
29 |
var height = properties.size.height - borderPadding;
|
|
30 |
var x = 0;
|
|
31 |
var y = 0;
|
|
32 |
|
|
33 |
// fill
|
|
34 |
if (properties.buttonNoFrame == false){
|
|
35 |
graphics.setBackground(laf.getColor("control.button.fill"));
|
|
36 |
graphics.setForeground(laf.getColor("screen.background"));
|
|
37 |
graphics.setBackground(laf.getColor("control.button.fill"));
|
|
38 |
graphics.fillGradientRectangle(x, y, width, height, true)
|
|
39 |
// edge
|
|
40 |
graphics.setForeground(laf.getColor("control.button.outline"))
|
|
41 |
graphics.drawRectangle(new Rectangle(0, 0, width, height))
|
|
42 |
|
|
43 |
// shadows
|
|
44 |
graphics.setForeground(laf.getColor("control.button.shadow.inner"))
|
|
45 |
graphics.drawLine(x + 1, y + height + 1, x + width + 1, y + height + 1)
|
|
46 |
graphics.drawLine(x + width + 1, y + 1, x + width + 1, y + height + 1)
|
|
47 |
|
|
48 |
graphics.setForeground(laf.getColor("control.button.shadow.outer"))
|
|
49 |
graphics.drawLine(x + 0, y + height + 0, x + width + 0, y + height + 0)
|
|
50 |
graphics.drawLine(x + width + 0, y + 0, x + width + 0, y + height + 0)
|
|
51 |
}
|
|
52 |
var fontFlags = getFontFlagsFromProperties(properties);
|
|
53 |
|
|
54 |
if (properties.images.bmpfile != ""){
|
|
55 |
|
|
56 |
var inToolbar = instance.parent.component.isOfType("com.nokia.sdt.series60.Toolbar");
|
|
57 |
if (instance.properties.buttonText != "" && !inToolbar) {
|
|
58 |
// there's an image and text and it's a button on the main pane (not in toolbar)
|
|
59 |
renderImage(CAknButton.prototype, instance, laf, graphics,
|
|
60 |
borderPadding, borderPadding, "images", true, "normal");
|
|
61 |
var imageBounds = getImageBounds(instance, properties.images, "normal");
|
|
62 |
var textOffsetForImage = getTextOffsetForImage(imageBounds, properties);
|
|
63 |
drawButtonTextItem(instance, instance.properties.buttonText, new Point(0, 0), new Point(width, height), laf, graphics, textOffsetForImage, fontFlags);
|
|
64 |
} else {
|
|
65 |
// there's only an image: draw the specified image on the button
|
|
66 |
if (!inToolbar) // not in toolbar so center image horizontally
|
|
67 |
x = width / 2 - height + 3*borderPadding;
|
|
68 |
y = borderPadding;
|
|
69 |
renderImage(CAknButton.prototype, instance, laf, graphics,
|
|
70 |
x, y, "images", true, "normal");
|
|
71 |
}
|
|
72 |
|
|
73 |
} else {
|
|
74 |
// there's only text
|
|
75 |
graphics.setForeground(laf.getColor("EEikColorButtonText"));
|
|
76 |
var textOffsetForImage = new Point(0, 0);
|
|
77 |
drawButtonTextItem(instance, instance.properties.buttonText, new Point(0, 0), new Point(width, height), laf, graphics, textOffsetForImage, fontFlags);
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Get text offsets when text and image are together so that the text is not overwriting the image
|
|
83 |
*/
|
|
84 |
function getTextOffsetForImage(imageBounds, properties){
|
|
85 |
var imageDim = 0;
|
|
86 |
if (imageBounds != null)
|
|
87 |
imageDim = properties.size.height;
|
|
88 |
// if it's left aligned and image is on left
|
|
89 |
if (properties.textAndIconAlignment == "CAknButton::EIconBeforeText" &&
|
|
90 |
(properties.horizTextAlign == "CGraphicsContext::ELeft" ||
|
|
91 |
properties.horizTextAlign == "CGraphicsContext::ECenter"))
|
|
92 |
{
|
|
93 |
return new Point(imageDim, 0);
|
|
94 |
}
|
|
95 |
// if it's right aligned and image is on right
|
|
96 |
if (properties.textAndIconAlignment == "CAknButton::EIconAfterText" &&
|
|
97 |
properties.horizTextAlign == "CGraphicsContext::ERight")
|
|
98 |
{
|
|
99 |
return new Point(imageDim, 0);
|
|
100 |
}
|
|
101 |
// if it's top aligned and image is on top
|
|
102 |
if (properties.textAndIconAlignment == "CAknButton::EIconOverText" &&
|
|
103 |
properties.vertTextAlign == "CAknButton::ETop")
|
|
104 |
{
|
|
105 |
return new Point(0, imageDim);
|
|
106 |
}
|
|
107 |
// if it's bottom aligned and image is on bottom
|
|
108 |
if (properties.textAndIconAlignment == "CAknButton::EIconUnderText" &&
|
|
109 |
properties.vertTextAlign == "CAknButton::EBottom")
|
|
110 |
{
|
|
111 |
return new Point(0, imageDim);
|
|
112 |
}
|
|
113 |
return new Point(0, 0);
|
|
114 |
}
|
|
115 |
|
|
116 |
function getFontFlagsFromProperties(properties){
|
|
117 |
|
|
118 |
// text can be offset as: center, left hand side, right hand side, top, or bottom
|
|
119 |
var fontFlags;
|
|
120 |
if (properties.horizTextAlign == "CGraphicsContext::ELeft" || (properties.buttonTextLeft == true) ) {
|
|
121 |
fontFlags |= Font.ALIGN_LEFT;
|
|
122 |
}else if (properties.horizTextAlign == "CGraphicsContext::ERight") {
|
|
123 |
fontFlags |= Font.ALIGN_RIGHT;
|
|
124 |
} else {
|
|
125 |
fontFlags |= Font.ALIGN_CENTER; // default alignment
|
|
126 |
}
|
|
127 |
|
|
128 |
if (properties.vertTextAlign == "CAknButton::ETop") {
|
|
129 |
fontFlags |= Font.VERTICAL_ALIGN_TOP;
|
|
130 |
}else if (properties.vertTextAlign == "CAknButton::EBottom") {
|
|
131 |
fontFlags |= Font.VERTICAL_ALIGN_BOTTOM;
|
|
132 |
} else {
|
|
133 |
fontFlags |= Font.VERTICAL_ALIGN_CENTER;
|
|
134 |
}
|
|
135 |
return fontFlags;
|
|
136 |
}
|
|
137 |
|
|
138 |
|
|
139 |
/**
|
|
140 |
* Get the rectangle in which to draw the icon
|
|
141 |
*/
|
|
142 |
function getIconRect(properties, laf) {
|
|
143 |
var iconSize = laf.getDimension("note.icon.size");
|
|
144 |
var padding = laf.getInteger("note.padding", 2);
|
|
145 |
var iconRect = new Rectangle(properties.size.width - iconSize.x - padding*2,
|
|
146 |
0,
|
|
147 |
iconSize.x, iconSize.y);
|
|
148 |
return iconRect;
|
|
149 |
}
|
|
150 |
|
|
151 |
|
|
152 |
CAknButton.prototype.getViewableSize = function(instance, propertyId, laf) {
|
|
153 |
var size = instance.properties.size;
|
|
154 |
var padding = laf.getInteger("note.padding", 2);
|
|
155 |
return new Point(size.height, size.height - padding*2);
|
|
156 |
}
|
|
157 |
|
|
158 |
CAknButton.prototype.isScaling = function(instance, propertyId, laf) {
|
|
159 |
return true;
|
|
160 |
}
|
|
161 |
|
|
162 |
|
|
163 |
CAknButton.prototype.getAlignmentWeights = function(instance, propertyId, laf) {
|
|
164 |
// If there's no button text align in the center, else align on the left
|
|
165 |
var properties = instance.properties;
|
|
166 |
|
|
167 |
if (properties.buttonText == "") {
|
|
168 |
return new Point(ImageUtils.ALIGN_CENTER_OR_TOP, ImageUtils.ALIGN_CENTER_OR_TOP);
|
|
169 |
} else {
|
|
170 |
// image and text
|
|
171 |
if (properties.textAndIconAlignment == "CAknButton::EIconBeforeText"){
|
|
172 |
return new Point(ImageUtils.ALIGN_LEFT, ImageUtils.ALIGN_CENTER_OR_TOP);
|
|
173 |
} else if (properties.textAndIconAlignment == "CAknButton::EIconAfterText"){
|
|
174 |
return new Point(ImageUtils.ALIGN_RIGHT, ImageUtils.ALIGN_CENTER_OR_TOP);
|
|
175 |
} else if (properties.textAndIconAlignment == "CAknButton::EIconUnderText"){
|
|
176 |
return new Point(ImageUtils.ALIGN_CENTER, ImageUtils.ALIGN_BOTTOM);
|
|
177 |
} else if (properties.textAndIconAlignment == "CAknButton::EIconOverText"){
|
|
178 |
return new Point(ImageUtils.ALIGN_CENTER, ImageUtils.ALIGN_TOP);
|
|
179 |
} else if (properties.textAndIconAlignment == "CAknButton::EOverlay"){
|
|
180 |
return new Point(0, 0);
|
|
181 |
} else {
|
|
182 |
return new Point(0, 0);
|
|
183 |
}
|
|
184 |
}
|
|
185 |
}
|
|
186 |
|
|
187 |
CAknButton.prototype.isPreservingAspectRatio = function(instance, propertyId, laf) {
|
|
188 |
// Preserving aspect ratio only when button is child of toolbar
|
|
189 |
if (instance.parent.component.isOfType("com.nokia.sdt.series60.Toolbar")){
|
|
190 |
return true;
|
|
191 |
} else {
|
|
192 |
return false;
|
|
193 |
}
|
|
194 |
}
|
|
195 |
|
|
196 |
|
|
197 |
CAknButton.prototype.getPreferredSize = function(instance, laf, wHint, hHint) {
|
|
198 |
var properties = instance.properties;
|
|
199 |
|
|
200 |
var width = -1;
|
|
201 |
var height = -1;
|
|
202 |
if (wHint >= 0)
|
|
203 |
width = wHint;
|
|
204 |
if (hHint >= 0)
|
|
205 |
height = hHint;
|
|
206 |
|
|
207 |
var imgBounds = getImageBounds(instance, properties.images, "normal");
|
|
208 |
if (imgBounds) {
|
|
209 |
if (imgBounds.width > width)
|
|
210 |
width = imgBounds.width;
|
|
211 |
if (imgBounds.height > height)
|
|
212 |
height = imgBounds.height;
|
|
213 |
} else {
|
|
214 |
if (width < 0)
|
|
215 |
width = 16;
|
|
216 |
if (height < 0)
|
|
217 |
height = 16;
|
|
218 |
}
|
|
219 |
|
|
220 |
var bounds = new Point(width, height);
|
|
221 |
return bounds;
|
|
222 |
}
|
|
223 |
|
|
224 |
setupCommonDirectImageEditing(CAknButton.prototype, "images",
|
|
225 |
null // areafunction
|
|
226 |
);
|
|
227 |
|
|
228 |
|
|
229 |
function drawButtonTextItem(instance, text, location, size, laf,
|
|
230 |
graphics, textOffsetForImage, fontFlags) {
|
|
231 |
|
|
232 |
var x = location.x;
|
|
233 |
var y = location.y;
|
|
234 |
var width = size.x;
|
|
235 |
var height = size.y;
|
|
236 |
var parentIsToolbar = instance.parent.component.isOfType("com.nokia.sdt.series60.Toolbar");
|
|
237 |
var font = laf.getFont("ButtonFont");
|
|
238 |
graphics.setFont(font);
|
|
239 |
|
|
240 |
var textMargin = 1;
|
|
241 |
if (!parentIsToolbar){
|
|
242 |
textMargin = laf.getInteger("control.button.text.margin", 0);
|
|
243 |
}
|
|
244 |
if (size.x <= textMargin) {
|
|
245 |
return; // will crash when drawing if no return
|
|
246 |
}
|
|
247 |
if (textOffsetForImage.x > textMargin){
|
|
248 |
textMargin = textOffsetForImage.x
|
|
249 |
} else {
|
|
250 |
textMargin = textMargin + textOffsetForImage.x
|
|
251 |
}
|
|
252 |
|
|
253 |
var extent = font.stringExtent(text)
|
|
254 |
|
|
255 |
var fontVertOffset = 0;
|
|
256 |
if ((fontFlags & Font.VERTICAL_ALIGN_BOTTOM) != 0){
|
|
257 |
fontVertOffset = (height - extent.y) - textMargin - textOffsetForImage.y;
|
|
258 |
} else if ((fontFlags & Font.VERTICAL_ALIGN_CENTER) != 0){
|
|
259 |
fontVertOffset = (height - extent.y) / 2 - (laf.getInteger("control.button.text.margin", 0)/2); // center
|
|
260 |
} else {
|
|
261 |
// top aligned
|
|
262 |
fontVertOffset = textMargin + textOffsetForImage.y;
|
|
263 |
}
|
|
264 |
|
|
265 |
if (parentIsToolbar){
|
|
266 |
// always vertically center text in toolbar
|
|
267 |
fontVertOffset = ((height - extent.y) / 2) - 5; // center
|
|
268 |
}
|
|
269 |
|
|
270 |
if (laf.getBoolean("is.portrait", true)) {
|
|
271 |
var fontHorizOffset = 0
|
|
272 |
var rect = new Rectangle(x + fontHorizOffset, y + fontVertOffset,
|
|
273 |
width - (2*fontHorizOffset), height );
|
|
274 |
|
|
275 |
rect.y = fontVertOffset;
|
|
276 |
rect.height = extent.y + 5;
|
|
277 |
rect.x = textMargin;
|
|
278 |
rect.width = rect.width - textMargin;
|
|
279 |
text = chooseScalableText(text, font, rect.width);
|
|
280 |
if (text != ""){
|
|
281 |
if (rect.width > 0 && rect.height > 0) {
|
|
282 |
graphics.drawFormattedString(text, rect, fontFlags | Font.OVERFLOW_ELLIPSIS, 0);
|
|
283 |
}
|
|
284 |
}
|
|
285 |
}
|
|
286 |
else {
|
|
287 |
var fontHeight = font.getHeight();
|
|
288 |
var sbar1Bounds = laf.getRectangle("status.bar1.bounds");
|
|
289 |
var sbar2Bounds = laf.getRectangle("status.bar2.bounds");
|
|
290 |
|
|
291 |
var fontOffset = (sbar2Bounds.height - extent.y) / 2
|
|
292 |
|
|
293 |
var rect = new Rectangle(x, y + fontOffset, width, height);
|
|
294 |
|
|
295 |
rect.y = fontVertOffset;
|
|
296 |
rect.height = extent.y + 5;
|
|
297 |
rect.x = textMargin;
|
|
298 |
rect.width = rect.width - textMargin*2;
|
|
299 |
graphics.fillRectangle(x, y + sbar2Bounds.y, width, sbar2Bounds.height);
|
|
300 |
text = chooseScalableText(text, font, rect.width);
|
|
301 |
|
|
302 |
if (text != "" && rect.width > 0 && rect.height > 0) {
|
|
303 |
graphics.drawFormattedString(text, rect, fontFlags | Font.OVERFLOW_ELLIPSIS, 0);
|
|
304 |
}
|
|
305 |
}
|
|
306 |
}
|
|
307 |
|
|
308 |
|
|
309 |
//////////////////////////////
|
|
310 |
// IDirectLabelEdit
|
|
311 |
//////////////////////////////
|
|
312 |
CAknButton.prototype.getPropertyPaths = function(instance) {
|
|
313 |
return new Array("buttonText");
|
|
314 |
}
|
|
315 |
|
|
316 |
CAknButton.prototype.getLabelBounds = function(instance, propertyPath, laf) {
|
|
317 |
return new Rectangle(0, 0,
|
|
318 |
instance.properties.size.width, instance.properties.size.height);
|
|
319 |
}
|
|
320 |
|
|
321 |
CAknButton.prototype.getLabelFont = function(instance, propertyPath, laf) {
|
|
322 |
return laf.getFont("ButtonFont");
|
|
323 |
}
|
|
324 |
|
|
325 |
//////////////////////////////////////////
|
|
326 |
// IComponentEventInfo
|
|
327 |
//////////////////////////////////////////
|
|
328 |
CAknButton.prototype.getEventGroups = function(instance) {
|
|
329 |
// do not provide Control events when child of Toolbar
|
|
330 |
if (instance.parent.component.isOfType("com.nokia.sdt.series60.Toolbar")){
|
|
331 |
return ["ToolbarEvent"];
|
|
332 |
} else {
|
|
333 |
return ["CCoeControl"];
|
|
334 |
}
|
|
335 |
}
|