Mercurial > projects > dwt-mac
comparison dwt/widgets/Label.d @ 45:d8635bb48c7c
Merge with SWT 3.5
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 01 Dec 2008 17:07:00 +0100 |
parents | e831403a80a9 |
children | cfa563df4fdd |
comparison
equal
deleted
inserted
replaced
44:ca5e494f2bbf | 45:d8635bb48c7c |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2006 IBM Corporation and others. | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
13 import dwt.dwthelper.utils; | 13 import dwt.dwthelper.utils; |
14 | 14 |
15 | 15 |
16 import dwt.DWT; | 16 import dwt.DWT; |
17 import dwt.DWTException; | 17 import dwt.DWTException; |
18 import dwt.accessibility.ACC; | |
18 import dwt.graphics.Image; | 19 import dwt.graphics.Image; |
19 import dwt.graphics.Point; | 20 import dwt.graphics.Point; |
20 import dwt.graphics.Rectangle; | 21 import dwt.internal.cocoa.NSArray; |
21 import dwt.internal.cocoa.NSAttributedString; | 22 import dwt.internal.cocoa.NSAttributedString; |
22 import dwt.internal.cocoa.NSBox; | 23 import dwt.internal.cocoa.NSBox; |
23 import dwt.internal.cocoa.NSCell; | 24 import dwt.internal.cocoa.NSCell; |
24 import dwt.internal.cocoa.NSColor; | 25 import dwt.internal.cocoa.NSColor; |
26 import dwt.internal.cocoa.NSFont; | |
27 import dwt.internal.cocoa.NSImage; | |
25 import dwt.internal.cocoa.NSImageView; | 28 import dwt.internal.cocoa.NSImageView; |
29 import dwt.internal.cocoa.NSMutableArray; | |
26 import dwt.internal.cocoa.NSMutableDictionary; | 30 import dwt.internal.cocoa.NSMutableDictionary; |
27 import dwt.internal.cocoa.NSRect; | 31 import dwt.internal.cocoa.NSRect; |
32 import dwt.internal.cocoa.NSSize; | |
28 import dwt.internal.cocoa.NSString; | 33 import dwt.internal.cocoa.NSString; |
29 import dwt.internal.cocoa.NSTextField; | 34 import dwt.internal.cocoa.NSTextField; |
30 import dwt.internal.cocoa.NSTextFieldCell; | 35 import dwt.internal.cocoa.NSTextFieldCell; |
36 import dwt.internal.cocoa.NSView; | |
31 import dwt.internal.cocoa.OS; | 37 import dwt.internal.cocoa.OS; |
32 import dwt.internal.cocoa.SWTBox; | 38 import dwt.internal.cocoa.SWTBox; |
33 import dwt.internal.cocoa.SWTImageView; | 39 import dwt.internal.cocoa.SWTImageView; |
34 import dwt.internal.cocoa.SWTTextField; | 40 import dwt.internal.cocoa.SWTTextField; |
41 import dwt.internal.cocoa.id; | |
35 | 42 |
36 /** | 43 /** |
37 * Instances of this class represent a non-selectable | 44 * Instances of this class represent a non-selectable |
38 * user interface object that displays a string or image. | 45 * user interface object that displays a string or image. |
39 * When SEPARATOR is specified, displays a single | 46 * When SEPARATOR is specified, displays a single |
40 * vertical or horizontal line. | 47 * vertical or horizontal line. |
48 * <p> | |
49 * Shadow styles are hints and may not be honoured | |
50 * by the platform. To create a separator label | |
51 * with the default shadow style for the platform, | |
52 * do not specify a shadow style. | |
53 * </p> | |
41 * <dl> | 54 * <dl> |
42 * <dt><b>Styles:</b></dt> | 55 * <dt><b>Styles:</b></dt> |
43 * <dd>SEPARATOR, HORIZONTAL, VERTICAL</dd> | 56 * <dd>SEPARATOR, HORIZONTAL, VERTICAL</dd> |
44 * <dd>SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd> | 57 * <dd>SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd> |
45 * <dd>CENTER, LEFT, RIGHT, WRAP</dd> | 58 * <dd>CENTER, LEFT, RIGHT, WRAP</dd> |
52 * Only one of CENTER, LEFT and RIGHT may be specified. | 65 * Only one of CENTER, LEFT and RIGHT may be specified. |
53 * </p><p> | 66 * </p><p> |
54 * IMPORTANT: This class is intended to be subclassed <em>only</em> | 67 * IMPORTANT: This class is intended to be subclassed <em>only</em> |
55 * within the DWT implementation. | 68 * within the DWT implementation. |
56 * </p> | 69 * </p> |
70 * | |
71 * @see <a href="http://www.eclipse.org/swt/snippets/#label">Label snippets</a> | |
72 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample</a> | |
73 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
57 */ | 74 */ |
58 public class Label : Control { | 75 public class Label : Control { |
59 String text = ""; | 76 String text = ""; |
60 Image image; | 77 Image image; |
61 bool isImage; | 78 bool isImage; |
101 */ | 118 */ |
102 public this (Composite parent, int style) { | 119 public this (Composite parent, int style) { |
103 super (parent, checkStyle (style)); | 120 super (parent, checkStyle (style)); |
104 } | 121 } |
105 | 122 |
123 int accessibilityAttributeNames(int /*long*/ id, int /*long*/ sel) { | |
124 | |
125 if (accessible !is null) { | |
126 if ((textView !is null && (id is textView.id || id is textView.cell().id)) || (imageView !is null && (id is imageView.id || id is imageView.cell().id))) { | |
127 // See if the accessible will override or augment the standard list. | |
128 // Help, title, and description can be overridden. | |
129 NSMutableArray extraAttributes = NSMutableArray.arrayWithCapacity(3); | |
130 extraAttributes.addObject(OS.NSAccessibilityHelpAttribute); | |
131 extraAttributes.addObject(OS.NSAccessibilityDescriptionAttribute); | |
132 extraAttributes.addObject(OS.NSAccessibilityTitleAttribute); | |
133 | |
134 for (int i = extraAttributes.count() - 1; i >= 0; i--) { | |
135 NSString attribute = new NSString(extraAttributes.objectAtIndex(i).id); | |
136 if (accessible.internal_accessibilityAttributeValue(attribute, ACC.CHILDID_SELF) is null) { | |
137 extraAttributes.removeObjectAtIndex(i); | |
138 } | |
139 } | |
140 | |
141 if (extraAttributes.count() > 0) { | |
142 int superResult = super.accessibilityAttributeNames(id, sel); | |
143 NSArray baseAttributes = new NSArray(superResult); | |
144 NSMutableArray mutableAttributes = NSMutableArray.arrayWithCapacity(baseAttributes.count() + 1); | |
145 mutableAttributes.addObjectsFromArray(baseAttributes); | |
146 | |
147 for (int i = 0; i < extraAttributes.count(); i++) { | |
148 id currAttribute = extraAttributes.objectAtIndex(i); | |
149 if (!mutableAttributes.containsObject(currAttribute)) { | |
150 mutableAttributes.addObject(currAttribute); | |
151 } | |
152 } | |
153 | |
154 return mutableAttributes.id; | |
155 } | |
156 } | |
157 } | |
158 | |
159 return super.accessibilityAttributeNames(id, sel); | |
160 } | |
161 | |
162 bool accessibilityIsIgnored(int /*long*/ id, int /*long*/ sel) { | |
163 if (id is view.id) return true; | |
164 return super.accessibilityIsIgnored(id, sel); | |
165 } | |
166 | |
106 static int checkStyle (int style) { | 167 static int checkStyle (int style) { |
107 style |= DWT.NO_FOCUS; | 168 style |= DWT.NO_FOCUS; |
108 if ((style & DWT.SEPARATOR) !is 0) { | 169 if ((style & DWT.SEPARATOR) !is 0) { |
109 style = checkBits (style, DWT.VERTICAL, DWT.HORIZONTAL, 0, 0, 0, 0); | 170 style = checkBits (style, DWT.VERTICAL, DWT.HORIZONTAL, 0, 0, 0, 0); |
110 return checkBits (style, DWT.SHADOW_OUT, DWT.SHADOW_IN, DWT.SHADOW_NONE, 0, 0, 0); | 171 return checkBits (style, DWT.SHADOW_OUT, DWT.SHADOW_IN, DWT.SHADOW_NONE, 0, 0, 0); |
112 return checkBits (style, DWT.LEFT, DWT.CENTER, DWT.RIGHT, 0, 0, 0); | 173 return checkBits (style, DWT.LEFT, DWT.CENTER, DWT.RIGHT, 0, 0, 0); |
113 } | 174 } |
114 | 175 |
115 public Point computeSize (int wHint, int hHint, bool changed) { | 176 public Point computeSize (int wHint, int hHint, bool changed) { |
116 checkWidget(); | 177 checkWidget(); |
117 int width = 0, height = 0; | 178 NSRect oldRect = view.frame (); |
179 int width = DEFAULT_WIDTH; | |
180 int height = DEFAULT_HEIGHT; | |
118 if ((style & DWT.SEPARATOR) !is 0) { | 181 if ((style & DWT.SEPARATOR) !is 0) { |
119 if ((style & DWT.HORIZONTAL) !is 0) { | 182 ((NSBox) view).sizeToFit (); |
120 width = DEFAULT_WIDTH; | 183 NSRect newRect = view.frame (); |
121 height = 3; | 184 width = (int) newRect.width; |
185 height = (int) newRect.height; | |
186 view.setFrame (oldRect); | |
187 } else { | |
188 if (isImage) { | |
189 if (image !is null) { | |
190 NSImage nsimage = image.handle; | |
191 NSSize size = nsimage.size (); | |
192 width = (int)size.width; | |
193 height = (int)size.height; | |
194 } else { | |
195 width = height = 0; | |
196 } | |
122 } else { | 197 } else { |
123 width = 3; | 198 NSRect rect = new NSRect (); |
124 height = DEFAULT_HEIGHT; | 199 rect.width = wHint !is DWT.DEFAULT ? wHint : Float.MAX_VALUE; |
125 } | 200 rect.height = hHint !is DWT.DEFAULT ? hHint : Float.MAX_VALUE; |
126 } else { | 201 NSSize size = textView.cell ().cellSizeForBounds (rect); |
127 if (image !is null && isImage) { | 202 width = cast(int)Math.ceil (size.width); |
128 Rectangle bounds = image.getBounds(); | 203 height = cast(int)Math.ceil (size.height); |
129 width = bounds.width; | |
130 height = bounds.height; | |
131 } else { | |
132 NSRect oldRect = textView.frame(); | |
133 textView.sizeToFit(); | |
134 NSRect newRect = textView.frame(); | |
135 textView.setFrame (oldRect); | |
136 width = cast(int)newRect.width; | |
137 height = cast(int)newRect.height; | |
138 } | 204 } |
139 } | 205 } |
140 if (wHint !is DWT.DEFAULT) width = wHint; | 206 if (wHint !is DWT.DEFAULT) width = wHint; |
141 if (hHint !is DWT.DEFAULT) height = hHint; | 207 if (hHint !is DWT.DEFAULT) height = hHint; |
142 return new Point (width, height); | 208 return new Point (width, height); |
143 } | 209 } |
144 | 210 |
145 void createHandle () { | 211 void createHandle () { |
146 SWTBox widget = cast(SWTBox)new SWTBox().alloc(); | 212 NSBox widget = cast(NSBox)(new SWTBox()).alloc(); |
147 widget.initWithFrame(new NSRect()); | 213 widget.initWithFrame(new NSRect()); |
148 widget.setTag(jniRef); | |
149 widget.setTitle(NSString.stringWith("")); | 214 widget.setTitle(NSString.stringWith("")); |
150 if ((style & DWT.SEPARATOR) !is 0) { | 215 if ((style & DWT.SEPARATOR) !is 0) { |
151 widget.setBoxType(OS.NSBoxSeparator); | 216 widget.setBoxType(OS.NSBoxSeparator); |
217 NSView child = (NSView) new NSView().alloc().init().autorelease(); | |
218 widget.setContentView(child); | |
152 } else { | 219 } else { |
153 widget.setBorderType(OS.NSNoBorder); | 220 widget.setBorderType(OS.NSNoBorder); |
154 | 221 widget.setBorderWidth (0); |
155 NSImageView imageWidget = cast(NSImageView)new SWTImageView().alloc(); | 222 widget.setBoxType (OS.NSBoxCustom); |
156 imageWidget.initWithFrame(new NSRect()); | 223 NSSize offsetSize = new NSSize (); |
157 imageWidget.setTag(jniRef); | 224 widget.setContentViewMargins (offsetSize); |
225 | |
226 NSImageView imageWidget = cast(NSImageView) new SWTImageView ().alloc (); | |
227 imageWidget.initWithFrame(new NSRect ()); | |
228 imageWidget.setImageScaling (OS.NSScaleNone); | |
158 | 229 |
159 SWTTextField textWidget = cast(SWTTextField)new SWTTextField().alloc(); | 230 NSTextField textWidget = cast(NSTextField)(new SWTTextField()).alloc(); |
160 textWidget.initWithFrame(new NSRect()); | 231 textWidget.initWithFrame(new NSRect()); |
161 textWidget.setBordered(false); | 232 textWidget.setBordered(false); |
162 textWidget.setEditable(false); | 233 textWidget.setEditable(false); |
163 textWidget.setDrawsBackground(false); | 234 textWidget.setDrawsBackground(false); |
164 textWidget.setTag(jniRef); | 235 NSTextFieldCell cell = new NSTextFieldCell(textWidget.cell()); |
165 if ((style & DWT.WRAP) !is 0) { | 236 cell.setWraps ((style & DWT.WRAP) !is 0); |
166 NSTextFieldCell cell = new NSTextFieldCell(textWidget.cell()); | |
167 cell.setWraps(true); | |
168 } | |
169 | 237 |
170 widget.addSubview_(imageWidget); | 238 widget.addSubview(imageWidget); |
171 widget.addSubview_(textWidget); | 239 widget.addSubview(textWidget); |
172 widget.setContentView(textWidget); | 240 widget.setContentView(textWidget); |
173 | 241 |
174 imageView = imageWidget; | 242 imageView = imageWidget; |
175 textView = textWidget; | 243 textView = textWidget; |
176 _setAlignment(); | 244 _setAlignment(); |
177 } | 245 } |
178 view = widget; | 246 view = widget; |
179 parent.contentView().addSubview_(widget); | |
180 } | 247 } |
181 | 248 |
182 NSAttributedString createString() { | 249 NSAttributedString createString() { |
183 NSMutableDictionary dict = NSMutableDictionary.dictionaryWithCapacity(4); | 250 NSMutableDictionary dict = NSMutableDictionary.dictionaryWithCapacity(4); |
184 if (foreground !is null) { | 251 if (foreground !is null) { |
185 NSColor color = NSColor.colorWithDeviceRed(foreground.handle[0], foreground.handle[1], foreground.handle[2], 1); | 252 NSColor color = NSColor.colorWithDeviceRed(foreground.handle[0], foreground.handle[1], foreground.handle[2], 1); |
186 dict.setObject(color, OS.NSForegroundColorAttributeName()); | 253 dict.setObject(color, OS.NSForegroundColorAttributeName); |
187 } | 254 } |
188 if (font !is null) { | 255 if (font !is null) { |
189 dict.setObject(font.handle, OS.NSFontAttributeName()); | 256 dict.setObject(font.handle, OS.NSFontAttributeName); |
190 } | 257 } |
191 char [] chars = new char [text.length ()]; | 258 char [] chars = new char [text.length ()]; |
192 text.getChars (0, chars.length, chars, 0); | 259 text.getChars (0, chars.length, chars, 0); |
193 int length = fixMnemonic (chars); | 260 int length = fixMnemonic (chars); |
194 | 261 |
195 NSString str = NSString.stringWithCharacters(chars, length); | 262 NSString str = NSString.stringWithCharacters(chars, length); |
196 NSAttributedString attribStr = (cast(NSAttributedString)new NSAttributedString().alloc()).initWithString_attributes_(str, dict); | 263 NSAttributedString attribStr = (cast(NSAttributedString)new NSAttributedString().alloc()).initWithString(str, dict); |
197 attribStr.autorelease(); | 264 attribStr.autorelease(); |
198 return attribStr; | 265 return attribStr; |
266 } | |
267 | |
268 void deregister () { | |
269 super.deregister (); | |
270 if (textView !is null) { | |
271 display.removeWidget(textView); | |
272 display.removeWidget(textView.cell()); | |
273 } | |
274 if (imageView !is null) { | |
275 display.removeWidget (imageView); | |
276 display.removeWidget (imageView.cell()); | |
277 } | |
199 } | 278 } |
200 | 279 |
201 /** | 280 /** |
202 * Returns a value which describes the position of the | 281 * Returns a value which describes the position of the |
203 * text or image in the receiver. The value will be one of | 282 * text or image in the receiver. The value will be one of |
256 checkWidget(); | 335 checkWidget(); |
257 if ((style & DWT.SEPARATOR) !is 0) return ""; | 336 if ((style & DWT.SEPARATOR) !is 0) return ""; |
258 return text; | 337 return text; |
259 } | 338 } |
260 | 339 |
340 void register () { | |
341 super.register (); | |
342 if (textView !is null) { | |
343 display.addWidget (textView, this); | |
344 display.addWidget (textView.cell(), this); | |
345 } | |
346 if (imageView !is null) { | |
347 display.addWidget (imageView, this); | |
348 display.addWidget (imageView.cell(), this); | |
349 } | |
350 } | |
351 | |
352 void releaseHandle () { | |
353 super.releaseHandle (); | |
354 if (textView !is null) textView.release(); | |
355 if (imageView !is null) imageView.release(); | |
356 textView = null; | |
357 imageView = null; | |
358 } | |
359 | |
261 /** | 360 /** |
262 * Controls how text and images will be displayed in the receiver. | 361 * Controls how text and images will be displayed in the receiver. |
263 * The argument should be one of <code>LEFT</code>, <code>RIGHT</code> | 362 * The argument should be one of <code>LEFT</code>, <code>RIGHT</code> |
264 * or <code>CENTER</code>. If the receiver is a <code>SEPARATOR</code> | 363 * or <code>CENTER</code>. If the receiver is a <code>SEPARATOR</code> |
265 * label, the argument is ignored and the alignment is not changed. | 364 * label, the argument is ignored and the alignment is not changed. |
283 void setBackground (float [] color) { | 382 void setBackground (float [] color) { |
284 if ((style & DWT.SEPARATOR) !is 0) return; | 383 if ((style & DWT.SEPARATOR) !is 0) return; |
285 textView.setDrawsBackground(color !is null); | 384 textView.setDrawsBackground(color !is null); |
286 if (color is null) return; | 385 if (color is null) return; |
287 NSColor nsColor = NSColor.colorWithDeviceRed(color[0], color[1], color[2], 1); | 386 NSColor nsColor = NSColor.colorWithDeviceRed(color[0], color[1], color[2], 1); |
288 NSTextFieldCell cell = new NSTextFieldCell(textView.cell()); | 387 ((NSTextField)textView).setBackgroundColor(nsColor); |
289 cell.setBackgroundColor(nsColor); | |
290 } | 388 } |
291 | 389 |
292 void _setAlignment() { | 390 void _setAlignment() { |
293 if ((style & DWT.RIGHT) !is 0) { | 391 if ((style & DWT.RIGHT) !is 0) { |
294 textView.setAlignment(OS.NSRightTextAlignment); | 392 textView.setAlignment(OS.NSRightTextAlignment); |
302 textView.setAlignment(OS.NSCenterTextAlignment); | 400 textView.setAlignment(OS.NSCenterTextAlignment); |
303 imageView.setImageAlignment(OS.NSImageAlignCenter); | 401 imageView.setImageAlignment(OS.NSImageAlignCenter); |
304 } | 402 } |
305 } | 403 } |
306 | 404 |
307 int setBounds (int x, int y, int width, int height, bool move, bool resize) { | 405 void setFont(NSFont font) { |
308 int result = super.setBounds(x, y, width, height, move, resize); | 406 if (textView !is null) { |
309 if ((result & RESIZED) !is 0) { | 407 NSCell cell = new NSCell(textView.cell()); |
310 if (imageView !is null || textView !is null) { | 408 cell.setAttributedStringValue(createString()); |
311 NSRect rect = view.bounds(); | 409 } |
312 imageView.setFrame(rect); | |
313 textView.setFrame(rect); | |
314 } | |
315 } | |
316 return result; | |
317 } | 410 } |
318 | 411 |
319 void setForeground (float [] color) { | 412 void setForeground (float [] color) { |
320 if ((style & DWT.SEPARATOR) !is 0) return; | 413 if ((style & DWT.SEPARATOR) !is 0) return; |
321 NSCell cell = new NSCell(textView.cell()); | 414 NSCell cell = new NSCell(textView.cell()); |
388 NSCell cell = new NSCell(textView.cell()); | 481 NSCell cell = new NSCell(textView.cell()); |
389 cell.setAttributedStringValue(createString()); | 482 cell.setAttributedStringValue(createString()); |
390 (cast(NSBox)view).setContentView(textView); | 483 (cast(NSBox)view).setContentView(textView); |
391 } | 484 } |
392 | 485 |
393 } | 486 |
487 } |