uidesigner/com.nokia.sdt.series60.componentlibrary/components/controls/CEikLabel_visual.js
author cawthron
Tue, 24 Mar 2009 22:20:21 -0500
changeset 2 d760517a8095
permissions -rw-r--r--
new
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("../renderLibrary.js")
cawthron
parents:
diff changeset
    20
include("../implLibrary.js")
cawthron
parents:
diff changeset
    21
cawthron
parents:
diff changeset
    22
function CEikLabelVisual() {
cawthron
parents:
diff changeset
    23
}
cawthron
parents:
diff changeset
    24
cawthron
parents:
diff changeset
    25
function isDialog(inst) {
cawthron
parents:
diff changeset
    26
	return inst.componentId == "com.nokia.sdt.series60.CAknDialog";
cawthron
parents:
diff changeset
    27
}
cawthron
parents:
diff changeset
    28
cawthron
parents:
diff changeset
    29
CEikLabelVisual.prototype.draw = function(instance, laf, graphics) {
cawthron
parents:
diff changeset
    30
	draw(instance, laf, graphics, false);
cawthron
parents:
diff changeset
    31
}
cawthron
parents:
diff changeset
    32
cawthron
parents:
diff changeset
    33
CEikLabelVisual.prototype.getPreferredSize = function(instance, laf, wHint, hHint) {
cawthron
parents:
diff changeset
    34
	return getPreferredSize(instance, laf, wHint, hHint, !isDialog(instance.parent));
cawthron
parents:
diff changeset
    35
}
cawthron
parents:
diff changeset
    36
cawthron
parents:
diff changeset
    37
cawthron
parents:
diff changeset
    38
setupCommonDirectLabelEditing(CEikLabelVisual.prototype, 
cawthron
parents:
diff changeset
    39
	"text", 
cawthron
parents:
diff changeset
    40
	null,
cawthron
parents:
diff changeset
    41
	function(instance, laf) { return laf.getFont(instance.properties.font); } 
cawthron
parents:
diff changeset
    42
	)
cawthron
parents:
diff changeset
    43
cawthron
parents:
diff changeset
    44
function draw(instance, laf, graphics, wrap) {
cawthron
parents:
diff changeset
    45
	var properties = instance.properties
cawthron
parents:
diff changeset
    46
	var flags = 0;
cawthron
parents:
diff changeset
    47
	
cawthron
parents:
diff changeset
    48
	if (wrap)
cawthron
parents:
diff changeset
    49
		flags |= Font.WRAPPING_ENABLED;
cawthron
parents:
diff changeset
    50
	else
cawthron
parents:
diff changeset
    51
		flags |= Font.WRAPPING_NONE;
cawthron
parents:
diff changeset
    52
		
cawthron
parents:
diff changeset
    53
	switch (properties.alignment) {
cawthron
parents:
diff changeset
    54
	case "EEikLabelAlignHCenter":
cawthron
parents:
diff changeset
    55
		flags |= Font.ALIGN_CENTER; break;
cawthron
parents:
diff changeset
    56
	case "EEikLabelAlignHLeft":
cawthron
parents:
diff changeset
    57
		flags |= Font.ALIGN_LEFT; break;
cawthron
parents:
diff changeset
    58
	case "EEikLabelAlignHRight":
cawthron
parents:
diff changeset
    59
		flags |= Font.ALIGN_RIGHT; break;
cawthron
parents:
diff changeset
    60
	}
cawthron
parents:
diff changeset
    61
	if (properties.strikethrough != false)
cawthron
parents:
diff changeset
    62
		flags |= Font.OPTIONS_STRIKETHROUGH;
cawthron
parents:
diff changeset
    63
	if (properties.underline != false)
cawthron
parents:
diff changeset
    64
		flags |= Font.OPTIONS_UNDERLINE;
cawthron
parents:
diff changeset
    65
cawthron
parents:
diff changeset
    66
/*
cawthron
parents:
diff changeset
    67
	switch (properties.emphasis) {
cawthron
parents:
diff changeset
    68
	case "EPartialEmphasis":
cawthron
parents:
diff changeset
    69
		flags |= Font.OPTIONS_BOLD;
cawthron
parents:
diff changeset
    70
		break;
cawthron
parents:
diff changeset
    71
	case "EFullEmphasis":
cawthron
parents:
diff changeset
    72
		flags |= Font.OPTIONS_EXTRABOLD;
cawthron
parents:
diff changeset
    73
	}
cawthron
parents:
diff changeset
    74
*/
cawthron
parents:
diff changeset
    75
	
cawthron
parents:
diff changeset
    76
	var font = laf.getFont(properties.font);
cawthron
parents:
diff changeset
    77
	graphics.setFont(font);
cawthron
parents:
diff changeset
    78
		
cawthron
parents:
diff changeset
    79
	var pattern = null;
cawthron
parents:
diff changeset
    80
	var drawBg = false;
cawthron
parents:
diff changeset
    81
	graphics.setBackground(getBackgroundColor(instance, laf))
cawthron
parents:
diff changeset
    82
cawthron
parents:
diff changeset
    83
	switch (properties.emphasis) {
cawthron
parents:
diff changeset
    84
	case "EPartialEmphasis":
cawthron
parents:
diff changeset
    85
		drawBg = true;
cawthron
parents:
diff changeset
    86
		graphics.setBackground(laf.getColor("EEikColorLabelBackgroundPartialEmphasis"));
cawthron
parents:
diff changeset
    87
		break;
cawthron
parents:
diff changeset
    88
	case "EFullEmphasis":
cawthron
parents:
diff changeset
    89
		drawBg = true;
cawthron
parents:
diff changeset
    90
		graphics.setBackground(laf.getColor("EEikColorLabelBackgroundFullEmphasis"));
cawthron
parents:
diff changeset
    91
	}
cawthron
parents:
diff changeset
    92
cawthron
parents:
diff changeset
    93
	// get bounding rect
cawthron
parents:
diff changeset
    94
	var rect = instance.getRenderingBounds();
cawthron
parents:
diff changeset
    95
	var text = chooseScalableText(properties.text, font, rect.width);
cawthron
parents:
diff changeset
    96
cawthron
parents:
diff changeset
    97
	if (drawBg) {
cawthron
parents:
diff changeset
    98
		graphics.fillRectangle(rect);
cawthron
parents:
diff changeset
    99
	}
cawthron
parents:
diff changeset
   100
	
cawthron
parents:
diff changeset
   101
	if (properties.brushStyle != "ENullBrush") {
cawthron
parents:
diff changeset
   102
		pattern = getPattern(graphics, properties.brushStyle);
cawthron
parents:
diff changeset
   103
		graphics.setBackgroundPattern(pattern);
cawthron
parents:
diff changeset
   104
		graphics.fillRectangle(rect);
cawthron
parents:
diff changeset
   105
	}
cawthron
parents:
diff changeset
   106
	
cawthron
parents:
diff changeset
   107
		
cawthron
parents:
diff changeset
   108
	// Series 60 draws left justified if the extent is at least the bounds
cawthron
parents:
diff changeset
   109
	oversize = new Point(rect.width * 2, rect.height);
cawthron
parents:
diff changeset
   110
	size = graphics.formattedStringExtent(text, oversize, flags | Font.WRAPPING_NONE);
cawthron
parents:
diff changeset
   111
	if (size.x >= rect.width) {
cawthron
parents:
diff changeset
   112
		flags &= ~Font.ALIGN_MASK;
cawthron
parents:
diff changeset
   113
		flags |= Font.ALIGN_LEFT;
cawthron
parents:
diff changeset
   114
	}
cawthron
parents:
diff changeset
   115
	
cawthron
parents:
diff changeset
   116
	text = text.replace(/\u2028|\u2029/,"\u7fff");
cawthron
parents:
diff changeset
   117
	graphics.drawFormattedString(text,
cawthron
parents:
diff changeset
   118
			rect,
cawthron
parents:
diff changeset
   119
			flags,
cawthron
parents:
diff changeset
   120
			properties.pixelGapBetweenLines);
cawthron
parents:
diff changeset
   121
	
cawthron
parents:
diff changeset
   122
	if (pattern != null)
cawthron
parents:
diff changeset
   123
		pattern.dispose();
cawthron
parents:
diff changeset
   124
}
cawthron
parents:
diff changeset
   125
cawthron
parents:
diff changeset
   126
// N.B.: we must define this function outside the prototype
cawthron
parents:
diff changeset
   127
// in order for strings to persist as such.  Otherwise they
cawthron
parents:
diff changeset
   128
// are converted to Object and switch() no longer works!
cawthron
parents:
diff changeset
   129
cawthron
parents:
diff changeset
   130
function getPattern(graphics, patt) {
cawthron
parents:
diff changeset
   131
cawthron
parents:
diff changeset
   132
	if (this.image3 == null) {
cawthron
parents:
diff changeset
   133
		this.image3 = Images.newImage(graphics.getDevice(), 3, 3)
cawthron
parents:
diff changeset
   134
	}
cawthron
parents:
diff changeset
   135
	if (this.image4 == null) {
cawthron
parents:
diff changeset
   136
		this.image4 = Images.newImage(graphics.getDevice(), 4, 4)
cawthron
parents:
diff changeset
   137
	}
cawthron
parents:
diff changeset
   138
cawthron
parents:
diff changeset
   139
	//println("brush type: " +typeof(patt));
cawthron
parents:
diff changeset
   140
	
cawthron
parents:
diff changeset
   141
	var img;
cawthron
parents:
diff changeset
   142
	if (patt != "EDiamondCrossHatchBrush" 
cawthron
parents:
diff changeset
   143
	&& patt != "EVerticalHatchBrush" 
cawthron
parents:
diff changeset
   144
	&& patt != "EHorizontalHatchBrush")
cawthron
parents:
diff changeset
   145
		img = this.image3
cawthron
parents:
diff changeset
   146
	else
cawthron
parents:
diff changeset
   147
		img = this.image4
cawthron
parents:
diff changeset
   148
		
cawthron
parents:
diff changeset
   149
	var gc = new GC(graphics.getDevice(), img)
cawthron
parents:
diff changeset
   150
cawthron
parents:
diff changeset
   151
	//gc.setBackground(Colors.getColor(255, 255, 255))
cawthron
parents:
diff changeset
   152
	gc.setBackground(graphics.getBackground())
cawthron
parents:
diff changeset
   153
	gc.fillRectangle(0, 0, 4, 4);
cawthron
parents:
diff changeset
   154
	gc.setBackground(Colors.getColor(255, 255,255))
cawthron
parents:
diff changeset
   155
	gc.setForeground(Colors.getColor(0, 0, 0))
cawthron
parents:
diff changeset
   156
	
cawthron
parents:
diff changeset
   157
	switch (patt) {
cawthron
parents:
diff changeset
   158
	case "EVerticalHatchBrush":
cawthron
parents:
diff changeset
   159
		gc.drawLine(1, 0, 1, 3); 
cawthron
parents:
diff changeset
   160
		gc.drawLine(3, 0, 3, 3); 
cawthron
parents:
diff changeset
   161
		break;
cawthron
parents:
diff changeset
   162
	case "EForwardDiagonalHatchBrush":
cawthron
parents:
diff changeset
   163
		gc.drawLine(0, 2, 2, 0); 
cawthron
parents:
diff changeset
   164
		break;
cawthron
parents:
diff changeset
   165
	case "EHorizontalHatchBrush":
cawthron
parents:
diff changeset
   166
		gc.drawLine(0, 1, 3, 1);
cawthron
parents:
diff changeset
   167
		gc.drawLine(0, 3, 3, 3);
cawthron
parents:
diff changeset
   168
		break;
cawthron
parents:
diff changeset
   169
	case "ERearwardDiagonalHatchBrush":
cawthron
parents:
diff changeset
   170
		gc.drawLine(0, 0, 2, 2);
cawthron
parents:
diff changeset
   171
		break;
cawthron
parents:
diff changeset
   172
	case "ESquareCrossHatchBrush":
cawthron
parents:
diff changeset
   173
		gc.drawLine(1, 0, 1, 3);
cawthron
parents:
diff changeset
   174
		gc.drawLine(0, 1, 3, 1);
cawthron
parents:
diff changeset
   175
		break;
cawthron
parents:
diff changeset
   176
	case "EDiamondCrossHatchBrush":
cawthron
parents:
diff changeset
   177
		gc.drawLine(0, 0, 3, 3); 
cawthron
parents:
diff changeset
   178
		gc.drawLine(2, 0, 0, 2);
cawthron
parents:
diff changeset
   179
		break;
cawthron
parents:
diff changeset
   180
	case "ESolidBrush":
cawthron
parents:
diff changeset
   181
	default:
cawthron
parents:
diff changeset
   182
		// note: if all cases come here, it's because switch(string)
cawthron
parents:
diff changeset
   183
		// only makes sense with actual string types.  
cawthron
parents:
diff changeset
   184
		// If this function is part of a prototype that's wrapped
cawthron
parents:
diff changeset
   185
		// into JS by UI Designer, then Rhino tends to coerce the arguments
cawthron
parents:
diff changeset
   186
		// to Object.
cawthron
parents:
diff changeset
   187
		
cawthron
parents:
diff changeset
   188
		//println("default brush type: " +patt);
cawthron
parents:
diff changeset
   189
		gc.setBackground(Colors.getColor(0, 0, 0))
cawthron
parents:
diff changeset
   190
		gc.fillRectangle(0, 0, 5, 5); 
cawthron
parents:
diff changeset
   191
		break;
cawthron
parents:
diff changeset
   192
	}
cawthron
parents:
diff changeset
   193
	
cawthron
parents:
diff changeset
   194
	gc.dispose()
cawthron
parents:
diff changeset
   195
	
cawthron
parents:
diff changeset
   196
	try {
cawthron
parents:
diff changeset
   197
		return new Pattern(graphics.getDevice(), img)
cawthron
parents:
diff changeset
   198
	} catch (e) {
cawthron
parents:
diff changeset
   199
		// not GDI+
cawthron
parents:
diff changeset
   200
		return null;
cawthron
parents:
diff changeset
   201
	}
cawthron
parents:
diff changeset
   202
}
cawthron
parents:
diff changeset
   203
cawthron
parents:
diff changeset
   204
function getPreferredSize(instance, laf, wHint, hHint, wrap) {
cawthron
parents:
diff changeset
   205
	var properties = instance.properties;
cawthron
parents:
diff changeset
   206
	var flags = 0;
cawthron
parents:
diff changeset
   207
	
cawthron
parents:
diff changeset
   208
	if (wrap)
cawthron
parents:
diff changeset
   209
		flags |= Font.WRAPPING_ENABLED;
cawthron
parents:
diff changeset
   210
	else
cawthron
parents:
diff changeset
   211
		flags |= Font.WRAPPING_NONE;
cawthron
parents:
diff changeset
   212
		
cawthron
parents:
diff changeset
   213
	font = laf.getFont(properties.font);
cawthron
parents:
diff changeset
   214
	switch (properties.alignment) {
cawthron
parents:
diff changeset
   215
	case "EEikLabelAlignHCenter":
cawthron
parents:
diff changeset
   216
		flags |= Font.ALIGN_CENTER; break;
cawthron
parents:
diff changeset
   217
	case "EEikLabelAlignHLeft":
cawthron
parents:
diff changeset
   218
		flags |= Font.ALIGN_LEFT; break;
cawthron
parents:
diff changeset
   219
	case "EEikLabelAlignHRight":
cawthron
parents:
diff changeset
   220
		flags |= Font.ALIGN_RIGHT; break;
cawthron
parents:
diff changeset
   221
	}
cawthron
parents:
diff changeset
   222
	
cawthron
parents:
diff changeset
   223
	// get bounding rect
cawthron
parents:
diff changeset
   224
	var layoutBounds = instance.getLayoutBounds();
cawthron
parents:
diff changeset
   225
	width = layoutBounds.width;
cawthron
parents:
diff changeset
   226
	height = layoutBounds.height;
cawthron
parents:
diff changeset
   227
	// if either of these are empty, use the parent's bounds as starting point
cawthron
parents:
diff changeset
   228
	if ((width == 0) || (height == 0)) {
cawthron
parents:
diff changeset
   229
		layoutBounds = instance.parent.getLayoutBounds();
cawthron
parents:
diff changeset
   230
		width = layoutBounds.width;
cawthron
parents:
diff changeset
   231
		height = layoutBounds.height;
cawthron
parents:
diff changeset
   232
	}
cawthron
parents:
diff changeset
   233
	
cawthron
parents:
diff changeset
   234
	if (wHint >= 0)
cawthron
parents:
diff changeset
   235
		width = wHint;
cawthron
parents:
diff changeset
   236
	if (hHint >= 0)
cawthron
parents:
diff changeset
   237
		height = hHint;
cawthron
parents:
diff changeset
   238
	var bounds = new Point(width, height);
cawthron
parents:
diff changeset
   239
	
cawthron
parents:
diff changeset
   240
	var text = chooseScalableText(properties.text.toString(), font, width);
cawthron
parents:
diff changeset
   241
	if (text.length == 0)
cawthron
parents:
diff changeset
   242
		text = " ";
cawthron
parents:
diff changeset
   243
	return font.formattedStringExtent(text, bounds, flags, properties.pixelGapBetweenLines);
cawthron
parents:
diff changeset
   244
}
cawthron
parents:
diff changeset
   245