uidesigner/com.nokia.carbide.cpp.uiq.components/components/editorLibrary.js
author tzelaw
Tue, 14 Apr 2009 15:03:19 -0500
changeset 94 d74b720418db
parent 2 d760517a8095
permissions -rw-r--r--
Test framework support: Ask debugger to remember DebugTarget so test framework can use it to setup test framework related utility. With this we can use the DebugUI way of launching while keeping test framework functionality
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
include("containerLibrary.js")
cawthron
parents:
diff changeset
    20
include("embeddedControlImplLibrary.js")// for isSettingItemList
cawthron
parents:
diff changeset
    21
cawthron
parents:
diff changeset
    22
editorLibraryStrings = getLocalizedStrings("editorLibrary");
cawthron
parents:
diff changeset
    23
cawthron
parents:
diff changeset
    24
/**
cawthron
parents:
diff changeset
    25
 *	Set up rendering support for editor components.  This clears out
cawthron
parents:
diff changeset
    26
 *	a rectangle of the component's size and draws text inside it
cawthron
parents:
diff changeset
    27
 *	according to a component-specific formatter and MFNE layout.
cawthron
parents:
diff changeset
    28
 *	This sets up a component to appear in a form.
cawthron
parents:
diff changeset
    29
 *
cawthron
parents:
diff changeset
    30
 *	@param prototype the prototype to add IVisualAppearance to.
cawthron
parents:
diff changeset
    31
 *	The prototype must implement these functions:
cawthron
parents:
diff changeset
    32
 *	<p>
cawthron
parents:
diff changeset
    33
 *	<li>
cawthron
parents:
diff changeset
    34
 *		getDisplayText(instance): return the text formatted for display
cawthron
parents:
diff changeset
    35
 *							(may be empty or null).  Return the entire
cawthron
parents:
diff changeset
    36
 *							string, even if longer than getMaxLength()
cawthron
parents:
diff changeset
    37
 *	<li>
cawthron
parents:
diff changeset
    38
 *		getMaxLength(instance): return the character limit on the text
cawthron
parents:
diff changeset
    39
 *	<li>
cawthron
parents:
diff changeset
    40
 *		getFlags(instance, laf): return an IFont#xxx mask desribing
cawthron
parents:
diff changeset
    41
 *								parameters to font.drawFormattedString()
cawthron
parents:
diff changeset
    42
 *
cawthron
parents:
diff changeset
    43
 */
cawthron
parents:
diff changeset
    44
function setupEditorRendering(prototype) {
cawthron
parents:
diff changeset
    45
	prototype.drawContent = function(instance, laf, graphics, rect) {
cawthron
parents:
diff changeset
    46
		var properties = instance.properties;
cawthron
parents:
diff changeset
    47
		
cawthron
parents:
diff changeset
    48
		if( properties.isVisible != null && properties.isVisible == false)
cawthron
parents:
diff changeset
    49
			return;
cawthron
parents:
diff changeset
    50
cawthron
parents:
diff changeset
    51
		var flags = this.getFlags(instance, laf);
cawthron
parents:
diff changeset
    52
		if (isSettingItemList(instance.parent)) {
cawthron
parents:
diff changeset
    53
			flags = (flags & ~Font.ALIGN_MASK) | Font.ALIGN_CENTER;
cawthron
parents:
diff changeset
    54
			
cawthron
parents:
diff changeset
    55
			// white on blue doesn't look good antialiased
cawthron
parents:
diff changeset
    56
			flags = (flags & ~Font.ANTIALIAS_MASK) | Font.ANTIALIAS_OFF;
cawthron
parents:
diff changeset
    57
		}
cawthron
parents:
diff changeset
    58
		
cawthron
parents:
diff changeset
    59
		var font = this.getFont(instance, laf);
cawthron
parents:
diff changeset
    60
		if (font == null)
cawthron
parents:
diff changeset
    61
			font = laf.getFont("NormalFont");
cawthron
parents:
diff changeset
    62
			
cawthron
parents:
diff changeset
    63
		graphics.setFont(font);
cawthron
parents:
diff changeset
    64
	
cawthron
parents:
diff changeset
    65
		//graphics.setBackground(laf.getColor("CAknSettingItemList.ContentBackground"));
cawthron
parents:
diff changeset
    66
		graphics.setBackground(getBackgroundColor(instance, laf));
cawthron
parents:
diff changeset
    67
		graphics.fillRectangle(rect);
cawthron
parents:
diff changeset
    68
	
cawthron
parents:
diff changeset
    69
		var color = null;
cawthron
parents:
diff changeset
    70
		if (prototype.getDisplayColor) {
cawthron
parents:
diff changeset
    71
			color = prototype.getDisplayColor(instance, laf);
cawthron
parents:
diff changeset
    72
		}
cawthron
parents:
diff changeset
    73
		if (color == null && isSettingItemList(instance.parent))
cawthron
parents:
diff changeset
    74
			color = laf.getColor("CAknSettingItemList.ContentForeground");
cawthron
parents:
diff changeset
    75
		
cawthron
parents:
diff changeset
    76
		if (color == null)
cawthron
parents:
diff changeset
    77
			color = laf.getColor("EEikColorControlText");
cawthron
parents:
diff changeset
    78
		graphics.setForeground(color);
cawthron
parents:
diff changeset
    79
cawthron
parents:
diff changeset
    80
		var text = "" + this.getDisplayText(instance);
cawthron
parents:
diff changeset
    81
		if (instance.isInstanceOf("com.nokia.carbide.uiq.CEikEdwinBase"))
cawthron
parents:
diff changeset
    82
			text = text.replace(/\n/,"");
cawthron
parents:
diff changeset
    83
		else
cawthron
parents:
diff changeset
    84
			text = chooseScalableText(text, font, rect.width);
cawthron
parents:
diff changeset
    85
		var maxLength = this.getMaxLength(instance);
cawthron
parents:
diff changeset
    86
		if (maxLength == 0 || text.length <= maxLength) {
cawthron
parents:
diff changeset
    87
			// in settings list, blank text is replaced
cawthron
parents:
diff changeset
    88
			if ((!text || text == "") && isSettingItemList(instance.parent)) {
cawthron
parents:
diff changeset
    89
				text = instance.properties.itemEmptyText;
cawthron
parents:
diff changeset
    90
				if (!text || text == "")
cawthron
parents:
diff changeset
    91
					text = editorLibraryStrings.getString("EmptyText");
cawthron
parents:
diff changeset
    92
			}
cawthron
parents:
diff changeset
    93
cawthron
parents:
diff changeset
    94
			graphics.drawFormattedString(text, rect, flags, 0);	
cawthron
parents:
diff changeset
    95
		} else {
cawthron
parents:
diff changeset
    96
			var visibleText = text.substring(0, maxLength);
cawthron
parents:
diff changeset
    97
			var truncatedText = text.substring(maxLength);
cawthron
parents:
diff changeset
    98
cawthron
parents:
diff changeset
    99
			var visibleSize = font.formattedStringExtent(visibleText, new Point(rect.width, rect.height), flags, 0);
cawthron
parents:
diff changeset
   100
			var truncatedSize = font.formattedStringExtent(truncatedText, new Point(rect.width, rect.height), flags, 0);
cawthron
parents:
diff changeset
   101
cawthron
parents:
diff changeset
   102
			// indicate truncated text 
cawthron
parents:
diff changeset
   103
			if (false) {
cawthron
parents:
diff changeset
   104
				// dim background of truncated part
cawthron
parents:
diff changeset
   105
				graphics.setBackground(laf.getColor("EEikColorControlHighlightBackground"));
cawthron
parents:
diff changeset
   106
				graphics.fillRectangle(rect.x + visibleSize.x, rect.y, truncatedSize.x, rect.height);
cawthron
parents:
diff changeset
   107
				graphics.drawFormattedString(text, rect, flags, 0);	
cawthron
parents:
diff changeset
   108
			} else {
cawthron
parents:
diff changeset
   109
				// dim letters of truncated part
cawthron
parents:
diff changeset
   110
				graphics.drawFormattedString(visibleText, rect, flags, 0);	
cawthron
parents:
diff changeset
   111
				graphics.setForeground(Colors.getColor(200, 200, 200));
cawthron
parents:
diff changeset
   112
				rect.x += visibleSize.x;
cawthron
parents:
diff changeset
   113
				graphics.drawFormattedString(truncatedText, rect, flags, 0);	
cawthron
parents:
diff changeset
   114
			}
cawthron
parents:
diff changeset
   115
		}
cawthron
parents:
diff changeset
   116
		
cawthron
parents:
diff changeset
   117
	}
cawthron
parents:
diff changeset
   118
	
cawthron
parents:
diff changeset
   119
	prototype.getContentSize = function(instance, laf, size) {
cawthron
parents:
diff changeset
   120
		var properties = instance.properties;
cawthron
parents:
diff changeset
   121
		var isInForm = isForm(instance.parent);
cawthron
parents:
diff changeset
   122
		
cawthron
parents:
diff changeset
   123
		var font = this.getFont(instance, laf);
cawthron
parents:
diff changeset
   124
		if (font == null)
cawthron
parents:
diff changeset
   125
			font = laf.getFont("NormalFont");
cawthron
parents:
diff changeset
   126
cawthron
parents:
diff changeset
   127
		var flags = this.getFlags(instance, laf);
cawthron
parents:
diff changeset
   128
		var text = this.getDisplayText(instance);
cawthron
parents:
diff changeset
   129
		if (text == null || text == "")
cawthron
parents:
diff changeset
   130
			text = "Temporary";		// provide enough space so click-added 
cawthron
parents:
diff changeset
   131
									// editor has some perceivable width
cawthron
parents:
diff changeset
   132
			
cawthron
parents:
diff changeset
   133
		// use wrapping width to get adjusted height
cawthron
parents:
diff changeset
   134
		var size = font.formattedStringExtent(text, size, flags, 0);
cawthron
parents:
diff changeset
   135
		
cawthron
parents:
diff changeset
   136
		// this property is not necessarily present, in which case only one line is used
cawthron
parents:
diff changeset
   137
		var lines = properties.lines;
cawthron
parents:
diff changeset
   138
		if (isNaN(lines) || lines < 1)
cawthron
parents:
diff changeset
   139
			lines = 1;
cawthron
parents:
diff changeset
   140
			
cawthron
parents:
diff changeset
   141
		var linesHeight = getFontHeight(font) * lines;
cawthron
parents:
diff changeset
   142
		var textHeight = size.y;
cawthron
parents:
diff changeset
   143
cawthron
parents:
diff changeset
   144
		// this property flag is not necessarily present, in which case this code falls through
cawthron
parents:
diff changeset
   145
		var isResizable = 'flags' in properties
cawthron
parents:
diff changeset
   146
			&& 'EEikEdwinResizable' in properties.flags
cawthron
parents:
diff changeset
   147
			&& properties.flags.EEikEdwinResizable;
cawthron
parents:
diff changeset
   148
			
cawthron
parents:
diff changeset
   149
		//println("lines("+lines+"), linesHeight(" + linesHeight + "), textHeight(" + textHeight + "), resizable(" + isResizable + ")");
cawthron
parents:
diff changeset
   150
		
cawthron
parents:
diff changeset
   151
		if (isResizable && (textHeight > linesHeight))
cawthron
parents:
diff changeset
   152
			size.y = textHeight;
cawthron
parents:
diff changeset
   153
		else if (!isInForm)
cawthron
parents:
diff changeset
   154
			size.y = linesHeight;	// don't resize if in container
cawthron
parents:
diff changeset
   155
		
cawthron
parents:
diff changeset
   156
		return size;
cawthron
parents:
diff changeset
   157
	}
cawthron
parents:
diff changeset
   158
	
cawthron
parents:
diff changeset
   159
	// now make this form-able
cawthron
parents:
diff changeset
   160
	setupEmbeddedRendering(prototype);
cawthron
parents:
diff changeset
   161
	
cawthron
parents:
diff changeset
   162
}
cawthron
parents:
diff changeset
   163
cawthron
parents:
diff changeset
   164
cawthron
parents:
diff changeset
   165
cawthron
parents:
diff changeset
   166
/**
cawthron
parents:
diff changeset
   167
 *	Format the number to have zeroes on the left.
cawthron
parents:
diff changeset
   168
 */
cawthron
parents:
diff changeset
   169
function padWithZeroes(number, count) {
cawthron
parents:
diff changeset
   170
	var str = "" + number;
cawthron
parents:
diff changeset
   171
	if (str.length >= count)
cawthron
parents:
diff changeset
   172
		return str;
cawthron
parents:
diff changeset
   173
	var zeroes = "0000000000000000000".substring(0, count);
cawthron
parents:
diff changeset
   174
	return zeroes.substring(0, count - str.length) + str;
cawthron
parents:
diff changeset
   175
}
cawthron
parents:
diff changeset
   176
cawthron
parents:
diff changeset
   177
/**
cawthron
parents:
diff changeset
   178
 *	Format the number to have spaces on the left.
cawthron
parents:
diff changeset
   179
 */
cawthron
parents:
diff changeset
   180
function padWithSpaces(number, count) {
cawthron
parents:
diff changeset
   181
	var str = "" + number;
cawthron
parents:
diff changeset
   182
	if (str.length >= count)
cawthron
parents:
diff changeset
   183
		return str;
cawthron
parents:
diff changeset
   184
	var zeroes = "                   ".substring(0, count);
cawthron
parents:
diff changeset
   185
	return zeroes.substring(0, count - str.length) + str;
cawthron
parents:
diff changeset
   186
}
cawthron
parents:
diff changeset
   187
cawthron
parents:
diff changeset
   188
/**
cawthron
parents:
diff changeset
   189
 *	Format a time the way S60 would.  This is piggishly restricted
cawthron
parents:
diff changeset
   190
 *	to the American format, since S60 has some weirdness with formatting
cawthron
parents:
diff changeset
   191
 *	(hiding hours -- but not AM/PM!, and hiding seconds) which would make
cawthron
parents:
diff changeset
   192
 *	our task of mirroring these in a locale-dependent way too difficult.
cawthron
parents:
diff changeset
   193
 *
cawthron
parents:
diff changeset
   194
 *	Likely it would be better to use a format string in the future.
cawthron
parents:
diff changeset
   195
 */
cawthron
parents:
diff changeset
   196
 
cawthron
parents:
diff changeset
   197
function formatTime(h, m, s, showHours, showMinutes, showSeconds, showAmPm) {
cawthron
parents:
diff changeset
   198
	var str = "";
cawthron
parents:
diff changeset
   199
	var suffix = "";
cawthron
parents:
diff changeset
   200
		
cawthron
parents:
diff changeset
   201
	if (showAmPm) {
cawthron
parents:
diff changeset
   202
		if (h >= 12) {
cawthron
parents:
diff changeset
   203
			h -= 12;
cawthron
parents:
diff changeset
   204
			suffix = " PM";
cawthron
parents:
diff changeset
   205
		} else {
cawthron
parents:
diff changeset
   206
			suffix = " AM";
cawthron
parents:
diff changeset
   207
		}
cawthron
parents:
diff changeset
   208
		if (h == 0)
cawthron
parents:
diff changeset
   209
			h = 12;
cawthron
parents:
diff changeset
   210
	}
cawthron
parents:
diff changeset
   211
cawthron
parents:
diff changeset
   212
	if (showHours) {
cawthron
parents:
diff changeset
   213
		str += padWithZeroes(h, 2) + ":";
cawthron
parents:
diff changeset
   214
	}
cawthron
parents:
diff changeset
   215
	
cawthron
parents:
diff changeset
   216
	if (showMinutes) {
cawthron
parents:
diff changeset
   217
		str += padWithZeroes(m, 2);
cawthron
parents:
diff changeset
   218
	}
cawthron
parents:
diff changeset
   219
	
cawthron
parents:
diff changeset
   220
	if (showSeconds) {
cawthron
parents:
diff changeset
   221
		str += ":" + padWithZeroes(s, 2);
cawthron
parents:
diff changeset
   222
	}
cawthron
parents:
diff changeset
   223
	
cawthron
parents:
diff changeset
   224
	str += suffix;
cawthron
parents:
diff changeset
   225
	
cawthron
parents:
diff changeset
   226
	return str;
cawthron
parents:
diff changeset
   227
cawthron
parents:
diff changeset
   228
}
cawthron
parents:
diff changeset
   229
cawthron
parents:
diff changeset
   230
/**
cawthron
parents:
diff changeset
   231
 *	Format a date the way S60 would.  
cawthron
parents:
diff changeset
   232
 */
cawthron
parents:
diff changeset
   233
 
cawthron
parents:
diff changeset
   234
function formatDate(d) {
cawthron
parents:
diff changeset
   235
	return formatDateDMY(d.day, d.month, d.year);
cawthron
parents:
diff changeset
   236
}
cawthron
parents:
diff changeset
   237
cawthron
parents:
diff changeset
   238
function formatDateDMY(day, month, year) {
cawthron
parents:
diff changeset
   239
	var str = "";
cawthron
parents:
diff changeset
   240
	str = padWithZeroes(day - 0 + 1, 2) + "/" +
cawthron
parents:
diff changeset
   241
		padWithZeroes(month - 0 + 1, 2) + "/" +
cawthron
parents:
diff changeset
   242
		padWithZeroes(year, 4);
cawthron
parents:
diff changeset
   243
	return str;
cawthron
parents:
diff changeset
   244
cawthron
parents:
diff changeset
   245
}
cawthron
parents:
diff changeset
   246
cawthron
parents:
diff changeset
   247
function commonEditorGetFlags(instance, laf) {
cawthron
parents:
diff changeset
   248
	var properties = instance.properties;
cawthron
parents:
diff changeset
   249
	var flags = 0;
cawthron
parents:
diff changeset
   250
cawthron
parents:
diff changeset
   251
	if (properties.alignment == undefined)
cawthron
parents:
diff changeset
   252
		return flags;
cawthron
parents:
diff changeset
   253
cawthron
parents:
diff changeset
   254
	switch (properties.alignment) {
cawthron
parents:
diff changeset
   255
		case "EAknEditorAlignCenter":
cawthron
parents:
diff changeset
   256
			flags = Font.ALIGN_CENTER; break;
cawthron
parents:
diff changeset
   257
		case "EAknEditorAlignLeft":
cawthron
parents:
diff changeset
   258
			flags = Font.ALIGN_LEFT; break;
cawthron
parents:
diff changeset
   259
		case "EAknEditorAlignRight":
cawthron
parents:
diff changeset
   260
			flags = Font.ALIGN_RIGHT; break;
cawthron
parents:
diff changeset
   261
	}
cawthron
parents:
diff changeset
   262
cawthron
parents:
diff changeset
   263
	return flags;
cawthron
parents:
diff changeset
   264
}
cawthron
parents:
diff changeset
   265
cawthron
parents:
diff changeset
   266
var CONTROL_ID = "com.nokia.sdt.series60.CCoeControl";
cawthron
parents:
diff changeset
   267
cawthron
parents:
diff changeset
   268
function isControl(instance) {
cawthron
parents:
diff changeset
   269
	return instance.componentId == CONTROL_ID;
cawthron
parents:
diff changeset
   270
}
cawthron
parents:
diff changeset
   271