uidesigner/com.nokia.carbide.cpp.uiq.components/components/renderLibrary.js
changeset 0 fb279309251b
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     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  * Return a reference to the IImageRenderingClass for static constant values access
       
    96  */
       
    97  
       
    98 IImageRendering = null;
       
    99  
       
   100 function getIImageRenderingClass() {
       
   101 	if (IImageRendering == null)
       
   102 		IImageRendering = getPluginClass("com.nokia.sdt.uimodel", "com.nokia.sdt.datamodel.images.IImageRendering");
       
   103 
       
   104 	return IImageRendering;
       
   105 }
       
   106 
       
   107 /**
       
   108  *	Get the real size of an image from property.
       
   109  *	This retrieves the unscaled image, which should 
       
   110  *	already be cached.
       
   111  *	@param instance the instance
       
   112  *	@param imageProperty the property, i.e. instance.properties.image
       
   113  *	@param multiImageAbstractImageId for a multi-image property, the abstract image id
       
   114  *	@return the Rectangle bounds, or null
       
   115  */
       
   116 function getImageBounds(instance, imageProperty, multiImageAbstractImageId) {
       
   117  	var imageRendering = createImagePropertyRendering();
       
   118  	imageRendering.setImageProperty(instance, null, null);
       
   119  	imageRendering.setImagePropertySource(imageProperty);
       
   120  	imageRendering.setViewableSize(null);
       
   121  	if (multiImageAbstractImageId)
       
   122  		imageRendering.setMultiImagePropertyAbstractImageId(multiImageAbstractImageId);
       
   123 	var imgData = imageRendering.getImageData();
       
   124 	if (imgData == null)
       
   125 		return null;
       
   126 	return new Rectangle(0, 0, imgData.width, imgData.height);
       
   127 }
       
   128 
       
   129 /**
       
   130  *	Scale a size to fit in a given size
       
   131  *	@param insize incoming size to scale
       
   132  *	@param size the size to fit
       
   133  *	@param preserveAspect true: keep aspect ratio, false: use instance size exactly
       
   134  *	@return a Point
       
   135  */
       
   136 function getSizeScaledToSize(insize, size, preserveAspect) {
       
   137 	if (!preserveAspect || size.x == 0 || size.y == 0)	{
       
   138 		return size;
       
   139 	}
       
   140 	
       
   141 	if (insize.x == 0 || insize.y == 0)
       
   142 		return insize;
       
   143 		
       
   144 	var iw, ih;
       
   145 	if (size.x / size.y > insize.x / insize.y) {
       
   146 		iw = insize.x * size.y / insize.y;
       
   147 		ih = size.y;
       
   148 	} else {
       
   149 		iw = size.x;
       
   150 		ih = insize.y * size.x / insize.x;
       
   151 	}
       
   152 	var scaled = new Point(iw, ih);
       
   153 	//println("scaled to " + scaled);
       
   154 	return scaled;
       
   155 }
       
   156 
       
   157 /**
       
   158  *	Scale a bounds to fit in a given bounding rectangle, and centered therein.
       
   159  *	@param inBounds incoming bounds to scale
       
   160  *	@param bounds the bounds to fit
       
   161  *	@param preserveAspect true: keep aspect ratio, false: use instance size exactly
       
   162  *	@return a Rectangle
       
   163  */
       
   164 function getBoundsScaledToBounds(inBounds, bounds, preserveAspect) {
       
   165 	var size = getSizeScaledToSize(new Point(inBounds.width, inBounds.height),
       
   166 		new Point(bounds.width, bounds.height),
       
   167 		preserveAspect);
       
   168 		
       
   169 	var scaled = new Rectangle((bounds.width - size.x) / 2, (bounds.height - size.y) / 2,
       
   170 			size.x, size.y);
       
   171 	//println("scaled to " + scaled);
       
   172 	return scaled;
       
   173 }
       
   174 
       
   175 //	Draw an image into the given rectangle in the gc
       
   176 //	@param instance the instance
       
   177 //	@param graphics the GC
       
   178 //	@param rect the rectangle to draw to.  If the rectangle is null
       
   179 //		or the width/height are 0, no scaling is performed.
       
   180 //		The x/y offset are used, if non-null, to offset the image.
       
   181 //	@param doBlend true: blend smoothly with background (only works with solid background)
       
   182 //	@return the Image, or null
       
   183 function drawImage(instance, graphics, image, rect, doBlend) {
       
   184 	if (image) {
       
   185 		// show image in dialog
       
   186 		//var dump = new ImageDump(null, image);
       
   187 		//dump.open();
       
   188 
       
   189 		var imgRect = image.getBounds()
       
   190 		//println("image is " + imgRect);
       
   191 		
       
   192 		var blended = ImageUtils.flattenAlphaMaskedImage(graphics.getDevice(), image, 
       
   193 			graphics.getBackground(), doBlend, true /* transparent */);
       
   194 
       
   195 		// show blended image in dialog
       
   196 		//var dump = new ImageDump(null, blended);
       
   197 		//dump.open();
       
   198 
       
   199 		if (rect) {
       
   200 			var imgRect = blended.getBounds()
       
   201 			if (rect.width != 0 && rect.height != 0)
       
   202 				graphics.drawImage(blended, 0, 0, imgRect.width, imgRect.height, rect.x, rect.y, rect.width, rect.height);
       
   203 			else
       
   204 				graphics.drawImage(blended, 0, 0, imgRect.width, imgRect.height, rect.x, rect.y, imgRect.width, imgRect.height);
       
   205 		} else {
       
   206 			graphics.drawImage(blended, 0, 0);
       
   207 		}
       
   208 		
       
   209 		blended.dispose();
       
   210 	}
       
   211 }
       
   212 
       
   213 
       
   214 /**
       
   215  * Get the height of a font which properly encompasses its leading,
       
   216  * descent, and ascent.  font.getHeight() is not quite accurate, for some reason...
       
   217  */
       
   218 function getFontHeight(font) {
       
   219 	return font.formattedStringExtent("x", new Point(0, 0), 0, 0).y;
       
   220 }
       
   221 
       
   222 /**
       
   223  *	Render an image to the gc
       
   224  *	@param prototype a prototype implementing IImagePropertyRenderingInfo
       
   225  *	@param instance the component instance holding the image property
       
   226  *	@param laf the look and feel information
       
   227  *	@param graphics the GC
       
   228  *	@param x x offset of image (left)
       
   229  *	@param y y offset of image (top)
       
   230  *	@param propertyId the property path of the image compound property
       
   231  *	@param doBlend true: blend image with background when flattening alpha
       
   232  *  @param multiImageAbstractImageId if non-null, then the image property houses multiple images, and draw this one
       
   233  *	(@see IMultiImagePropertyInfo)
       
   234  */
       
   235 function renderImage(prototype, instance, laf, graphics, x, y, propertyId, doBlend, multiImageAbstractImageId) {
       
   236 	var imagePropertyRendering = createImagePropertyRendering();
       
   237 	imagePropertyRendering.setImageProperty(instance, propertyId, laf);
       
   238 	imagePropertyRendering.setViewableSize(prototype.getViewableSize(instance, propertyId, laf));
       
   239 	imagePropertyRendering.setAlignmentWeights(prototype.getAlignmentWeights(instance, propertyId, laf));
       
   240 	imagePropertyRendering.setScaling(prototype.isScaling(instance, propertyId, laf));
       
   241 	imagePropertyRendering.setPreservingAspectRatio(prototype.isPreservingAspectRatio(instance, propertyId, laf));
       
   242 	
       
   243 	if (doBlend)
       
   244 		imagePropertyRendering.setTransparencyHandling(getIImageRenderingClass().TRANSPARENCY_FLATTEN_AND_BLEND);
       
   245 	else
       
   246 		imagePropertyRendering.setTransparencyHandling(getIImageRenderingClass().TRANSPARENCY_FLATTEN);
       
   247 	if (multiImageAbstractImageId)
       
   248 		imagePropertyRendering.setMultiImagePropertyAbstractImageId(multiImageAbstractImageId);
       
   249 	
       
   250 	imagePropertyRendering.render(graphics.getWrappedGC(), x, y);
       
   251 }
       
   252 
       
   253 
       
   254 /**
       
   255  *	Get the bounds consumed by wrappable text, given a limiting width and 
       
   256  *	maximum number of lines.  This detects newlines embedded in text.
       
   257  *	@param string the text to measure
       
   258  *	@param width width in pixels
       
   259  *	@param font the font
       
   260  *	@param flags mask of IFont.XXX flags (wrapping flags ignored!)
       
   261  *	@param lineGap pixel gap b/t lines
       
   262  *	@param maxLines maximum # lines to emit
       
   263  *	@return Point (maxWidthUsed, requiredHeight)
       
   264  */
       
   265 function calculateWrappedTextSize(string, width, font, flags, lineGap, maxLines) {
       
   266 	var lines = TextRendering.formatIntoLines(font, string, width, flags, maxLines);
       
   267 	var fontHeight = font.getHeight();
       
   268 	var gappedLineHeight = fontHeight + lineGap;
       
   269 	var maxWidth = 0;
       
   270 	for (var i in lines) {
       
   271 		var line = lines[i];
       
   272 		maxWidth = Math.max(maxWidth, font.stringExtent(line).x);
       
   273 	}
       
   274 	return new Point(maxWidth, lines.length * gappedLineHeight);
       
   275 }