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