Mercurial > projects > dwt-mac
comparison dwt/widgets/Label.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 649b8e223d5a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2006 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 *******************************************************************************/ | |
11 module dwt.widgets.Label; | |
12 | |
13 import dwt.dwthelper.utils; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.DWTException; | |
18 import dwt.graphics.Image; | |
19 import dwt.graphics.Point; | |
20 import dwt.graphics.Rectangle; | |
21 import dwt.internal.cocoa.NSAttributedString; | |
22 import dwt.internal.cocoa.NSBox; | |
23 import dwt.internal.cocoa.NSCell; | |
24 import dwt.internal.cocoa.NSColor; | |
25 import dwt.internal.cocoa.NSImageView; | |
26 import dwt.internal.cocoa.NSMutableDictionary; | |
27 import dwt.internal.cocoa.NSRect; | |
28 import dwt.internal.cocoa.NSString; | |
29 import dwt.internal.cocoa.NSTextField; | |
30 import dwt.internal.cocoa.NSTextFieldCell; | |
31 import dwt.internal.cocoa.OS; | |
32 import dwt.internal.cocoa.SWTBox; | |
33 import dwt.internal.cocoa.SWTImageView; | |
34 import dwt.internal.cocoa.SWTTextField; | |
35 | |
36 /** | |
37 * Instances of this class represent a non-selectable | |
38 * user interface object that displays a string or image. | |
39 * When SEPARATOR is specified, displays a single | |
40 * vertical or horizontal line. | |
41 * <dl> | |
42 * <dt><b>Styles:</b></dt> | |
43 * <dd>SEPARATOR, HORIZONTAL, VERTICAL</dd> | |
44 * <dd>SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd> | |
45 * <dd>CENTER, LEFT, RIGHT, WRAP</dd> | |
46 * <dt><b>Events:</b></dt> | |
47 * <dd>(none)</dd> | |
48 * </dl> | |
49 * <p> | |
50 * Note: Only one of SHADOW_IN, SHADOW_OUT and SHADOW_NONE may be specified. | |
51 * SHADOW_NONE is a HINT. Only one of HORIZONTAL and VERTICAL may be specified. | |
52 * Only one of CENTER, LEFT and RIGHT may be specified. | |
53 * </p><p> | |
54 * IMPORTANT: This class is intended to be subclassed <em>only</em> | |
55 * within the DWT implementation. | |
56 * </p> | |
57 */ | |
58 public class Label extends Control { | |
59 String text = ""; | |
60 Image image; | |
61 bool isImage; | |
62 NSTextField textView; | |
63 NSImageView imageView; | |
64 | |
65 /** | |
66 * Constructs a new instance of this class given its parent | |
67 * and a style value describing its behavior and appearance. | |
68 * <p> | |
69 * The style value is either one of the style constants defined in | |
70 * class <code>DWT</code> which is applicable to instances of this | |
71 * class, or must be built by <em>bitwise OR</em>'ing together | |
72 * (that is, using the <code>int</code> "|" operator) two or more | |
73 * of those <code>DWT</code> style constants. The class description | |
74 * lists the style constants that are applicable to the class. | |
75 * Style bits are also inherited from superclasses. | |
76 * </p> | |
77 * | |
78 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
79 * @param style the style of control to construct | |
80 * | |
81 * @exception IllegalArgumentException <ul> | |
82 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
83 * </ul> | |
84 * @exception DWTException <ul> | |
85 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
86 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
87 * </ul> | |
88 * | |
89 * @see DWT#SEPARATOR | |
90 * @see DWT#HORIZONTAL | |
91 * @see DWT#VERTICAL | |
92 * @see DWT#SHADOW_IN | |
93 * @see DWT#SHADOW_OUT | |
94 * @see DWT#SHADOW_NONE | |
95 * @see DWT#CENTER | |
96 * @see DWT#LEFT | |
97 * @see DWT#RIGHT | |
98 * @see DWT#WRAP | |
99 * @see Widget#checkSubclass | |
100 * @see Widget#getStyle | |
101 */ | |
102 public Label (Composite parent, int style) { | |
103 super (parent, checkStyle (style)); | |
104 } | |
105 | |
106 static int checkStyle (int style) { | |
107 style |= DWT.NO_FOCUS; | |
108 if ((style & DWT.SEPARATOR) !is 0) { | |
109 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); | |
111 } | |
112 return checkBits (style, DWT.LEFT, DWT.CENTER, DWT.RIGHT, 0, 0, 0); | |
113 } | |
114 | |
115 public Point computeSize (int wHint, int hHint, bool changed) { | |
116 checkWidget(); | |
117 int width = 0, height = 0; | |
118 if ((style & DWT.SEPARATOR) !is 0) { | |
119 if ((style & DWT.HORIZONTAL) !is 0) { | |
120 width = DEFAULT_WIDTH; | |
121 height = 3; | |
122 } else { | |
123 width = 3; | |
124 height = DEFAULT_HEIGHT; | |
125 } | |
126 } else { | |
127 if (image !is null && isImage) { | |
128 Rectangle bounds = image.getBounds(); | |
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 = (int)newRect.width; | |
137 height = (int)newRect.height; | |
138 } | |
139 } | |
140 if (wHint !is DWT.DEFAULT) width = wHint; | |
141 if (hHint !is DWT.DEFAULT) height = hHint; | |
142 return new Point (width, height); | |
143 } | |
144 | |
145 void createHandle () { | |
146 SWTBox widget = (SWTBox)new SWTBox().alloc(); | |
147 widget.initWithFrame(new NSRect()); | |
148 widget.setTag(jniRef); | |
149 widget.setTitle(NSString.stringWith("")); | |
150 if ((style & DWT.SEPARATOR) !is 0) { | |
151 widget.setBoxType(OS.NSBoxSeparator); | |
152 } else { | |
153 widget.setBorderType(OS.NSNoBorder); | |
154 | |
155 NSImageView imageWidget = (NSImageView)new SWTImageView().alloc(); | |
156 imageWidget.initWithFrame(new NSRect()); | |
157 imageWidget.setTag(jniRef); | |
158 | |
159 SWTTextField textWidget = (SWTTextField)new SWTTextField().alloc(); | |
160 textWidget.initWithFrame(new NSRect()); | |
161 textWidget.setBordered(false); | |
162 textWidget.setEditable(false); | |
163 textWidget.setDrawsBackground(false); | |
164 textWidget.setTag(jniRef); | |
165 if ((style & DWT.WRAP) !is 0) { | |
166 NSTextFieldCell cell = new NSTextFieldCell(textWidget.cell()); | |
167 cell.setWraps(true); | |
168 } | |
169 | |
170 widget.addSubview_(imageWidget); | |
171 widget.addSubview_(textWidget); | |
172 widget.setContentView(textWidget); | |
173 | |
174 imageView = imageWidget; | |
175 textView = textWidget; | |
176 _setAlignment(); | |
177 } | |
178 view = widget; | |
179 parent.contentView().addSubview_(widget); | |
180 } | |
181 | |
182 NSAttributedString createString() { | |
183 NSMutableDictionary dict = NSMutableDictionary.dictionaryWithCapacity(4); | |
184 if (foreground !is null) { | |
185 NSColor color = NSColor.colorWithDeviceRed(foreground.handle[0], foreground.handle[1], foreground.handle[2], 1); | |
186 dict.setObject(color, OS.NSForegroundColorAttributeName()); | |
187 } | |
188 if (font !is null) { | |
189 dict.setObject(font.handle, OS.NSFontAttributeName()); | |
190 } | |
191 char [] chars = new char [text.length ()]; | |
192 text.getChars (0, chars.length, chars, 0); | |
193 int length = fixMnemonic (chars); | |
194 | |
195 NSString str = NSString.stringWithCharacters(chars, length); | |
196 NSAttributedString attribStr = ((NSAttributedString)new NSAttributedString().alloc()).initWithString_attributes_(str, dict); | |
197 attribStr.autorelease(); | |
198 return attribStr; | |
199 } | |
200 | |
201 /** | |
202 * Returns a value which describes the position of the | |
203 * text or image in the receiver. The value will be one of | |
204 * <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code> | |
205 * unless the receiver is a <code>SEPARATOR</code> label, in | |
206 * which case, <code>NONE</code> is returned. | |
207 * | |
208 * @return the alignment | |
209 * | |
210 * @exception DWTException <ul> | |
211 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
212 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
213 * </ul> | |
214 */ | |
215 public int getAlignment () { | |
216 checkWidget(); | |
217 if ((style & DWT.SEPARATOR) !is 0) return DWT.LEFT; | |
218 if ((style & DWT.CENTER) !is 0) return DWT.CENTER; | |
219 if ((style & DWT.RIGHT) !is 0) return DWT.RIGHT; | |
220 return DWT.LEFT; | |
221 } | |
222 | |
223 /** | |
224 * Returns the receiver's image if it has one, or null | |
225 * if it does not. | |
226 * | |
227 * @return the receiver's image | |
228 * | |
229 * @exception DWTException <ul> | |
230 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
231 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
232 * </ul> | |
233 */ | |
234 public Image getImage () { | |
235 checkWidget(); | |
236 return image; | |
237 } | |
238 | |
239 String getNameText () { | |
240 return getText (); | |
241 } | |
242 | |
243 /** | |
244 * Returns the receiver's text, which will be an empty | |
245 * string if it has never been set or if the receiver is | |
246 * a <code>SEPARATOR</code> label. | |
247 * | |
248 * @return the receiver's text | |
249 * | |
250 * @exception DWTException <ul> | |
251 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
252 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
253 * </ul> | |
254 */ | |
255 public String getText () { | |
256 checkWidget(); | |
257 if ((style & DWT.SEPARATOR) !is 0) return ""; | |
258 return text; | |
259 } | |
260 | |
261 /** | |
262 * Controls how text and images will be displayed in the receiver. | |
263 * 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> | |
265 * label, the argument is ignored and the alignment is not changed. | |
266 * | |
267 * @param alignment the new alignment | |
268 * | |
269 * @exception DWTException <ul> | |
270 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
271 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
272 * </ul> | |
273 */ | |
274 public void setAlignment (int alignment) { | |
275 checkWidget(); | |
276 if ((style & DWT.SEPARATOR) !is 0) return; | |
277 if ((alignment & (DWT.LEFT | DWT.RIGHT | DWT.CENTER)) is 0) return; | |
278 style &= ~(DWT.LEFT | DWT.RIGHT | DWT.CENTER); | |
279 style |= alignment & (DWT.LEFT | DWT.RIGHT | DWT.CENTER); | |
280 _setAlignment(); | |
281 } | |
282 | |
283 void setBackground (float [] color) { | |
284 if ((style & DWT.SEPARATOR) !is 0) return; | |
285 textView.setDrawsBackground(color !is null); | |
286 if (color is null) return; | |
287 NSColor nsColor = NSColor.colorWithDeviceRed(color[0], color[1], color[2], 1); | |
288 NSTextFieldCell cell = new NSTextFieldCell(textView.cell()); | |
289 cell.setBackgroundColor(nsColor); | |
290 } | |
291 | |
292 void _setAlignment() { | |
293 if ((style & DWT.RIGHT) !is 0) { | |
294 textView.setAlignment(OS.NSRightTextAlignment); | |
295 imageView.setImageAlignment(OS.NSImageAlignRight); | |
296 } | |
297 if ((style & DWT.LEFT) !is 0) { | |
298 textView.setAlignment(OS.NSLeftTextAlignment); | |
299 imageView.setImageAlignment(OS.NSImageAlignLeft); | |
300 } | |
301 if ((style & DWT.CENTER) !is 0) { | |
302 textView.setAlignment(OS.NSCenterTextAlignment); | |
303 imageView.setImageAlignment(OS.NSImageAlignCenter); | |
304 } | |
305 } | |
306 | |
307 int setBounds (int x, int y, int width, int height, bool move, bool resize) { | |
308 int result = super.setBounds(x, y, width, height, move, resize); | |
309 if ((result & RESIZED) !is 0) { | |
310 if (imageView !is null || textView !is null) { | |
311 NSRect rect = view.bounds(); | |
312 imageView.setFrame(rect); | |
313 textView.setFrame(rect); | |
314 } | |
315 } | |
316 return result; | |
317 } | |
318 | |
319 void setForeground (float [] color) { | |
320 if ((style & DWT.SEPARATOR) !is 0) return; | |
321 NSCell cell = new NSCell(textView.cell()); | |
322 cell.setAttributedStringValue(createString()); | |
323 } | |
324 | |
325 bool setTabItemFocus () { | |
326 return false; | |
327 } | |
328 | |
329 /** | |
330 * Sets the receiver's image to the argument, which may be | |
331 * null indicating that no image should be displayed. | |
332 * | |
333 * @param image the image to display on the receiver (may be null) | |
334 * | |
335 * @exception IllegalArgumentException <ul> | |
336 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li> | |
337 * </ul> | |
338 * @exception DWTException <ul> | |
339 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
340 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
341 * </ul> | |
342 */ | |
343 public void setImage (Image image) { | |
344 checkWidget(); | |
345 if ((style & DWT.SEPARATOR) !is 0) return; | |
346 if (image !is null && image.isDisposed ()) { | |
347 error (DWT.ERROR_INVALID_ARGUMENT); | |
348 } | |
349 this.image = image; | |
350 isImage = true; | |
351 imageView.setImage(image !is null ? image.handle : null); | |
352 ((NSBox)view).setContentView(imageView); | |
353 } | |
354 | |
355 /** | |
356 * Sets the receiver's text. | |
357 * <p> | |
358 * This method sets the widget label. The label may include | |
359 * the mnemonic character and line delimiters. | |
360 * </p> | |
361 * <p> | |
362 * Mnemonics are indicated by an '&' that causes the next | |
363 * character to be the mnemonic. When the user presses a | |
364 * key sequence that matches the mnemonic, focus is assigned | |
365 * to the control that follows the label. On most platforms, | |
366 * the mnemonic appears underlined but may be emphasised in a | |
367 * platform specific manner. The mnemonic indicator character | |
368 * '&' can be escaped by doubling it in the string, causing | |
369 * a single '&' to be displayed. | |
370 * </p> | |
371 * | |
372 * @param string the new text | |
373 * | |
374 * @exception IllegalArgumentException <ul> | |
375 * <li>ERROR_NULL_ARGUMENT - if the text is null</li> | |
376 * </ul> | |
377 * @exception DWTException <ul> | |
378 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
379 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
380 * </ul> | |
381 */ | |
382 public void setText (String string) { | |
383 checkWidget(); | |
384 if (string is null) error (DWT.ERROR_NULL_ARGUMENT); | |
385 if ((style & DWT.SEPARATOR) !is 0) return; | |
386 isImage = false; | |
387 text = string; | |
388 NSCell cell = new NSCell(textView.cell()); | |
389 cell.setAttributedStringValue(createString()); | |
390 ((NSBox)view).setContentView(textView); | |
391 } | |
392 | |
393 } |