1 /*******************************************************************************
2 * Copyright (c) 2000, 2019 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 * Paul Pazderski - Bug 205199: setImage(null) on Label overrides text
14 *******************************************************************************/
15 package org.eclipse.swt.widgets;
18 import org.eclipse.swt.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.internal.*;
21 import org.eclipse.swt.internal.win32.*;
24 * Instances of this class represent a non-selectable
25 * user interface object that displays a string or image.
26 * When SEPARATOR is specified, displays a single
27 * vertical or horizontal line.
29 * Shadow styles are hints and may not be honored
30 * by the platform. To create a separator label
31 * with the default shadow style for the platform,
32 * do not specify a shadow style.
35 * <dt><b>Styles:</b></dt>
36 * <dd>SEPARATOR, HORIZONTAL, VERTICAL</dd>
37 * <dd>SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd>
38 * <dd>CENTER, LEFT, RIGHT, WRAP</dd>
39 * <dt><b>Events:</b></dt>
43 * Note: Only one of SHADOW_IN, SHADOW_OUT and SHADOW_NONE may be specified.
44 * SHADOW_NONE is a HINT. Only one of HORIZONTAL and VERTICAL may be specified.
45 * Only one of CENTER, LEFT and RIGHT may be specified.
47 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
50 * @see <a href="http://www.eclipse.org/swt/snippets/#label">Label snippets</a>
51 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample</a>
52 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
53 * @noextend This class is not intended to be subclassed by clients.
55 public class Label extends Control {
58 static final int MARGIN = 4;
59 static /*final*/ boolean IMAGE_AND_TEXT = false;
60 static final long LabelProc;
61 static final TCHAR LabelClass = new TCHAR (0, "STATIC", true);
63 WNDCLASS lpWndClass = new WNDCLASS ();
64 OS.GetClassInfo (0, LabelClass, lpWndClass);
65 LabelProc = lpWndClass.lpfnWndProc;
69 * Constructs a new instance of this class given its parent
70 * and a style value describing its behavior and appearance.
72 * The style value is either one of the style constants defined in
73 * class <code>SWT</code> which is applicable to instances of this
74 * class, or must be built by <em>bitwise OR</em>'ing together
75 * (that is, using the <code>int</code> "|" operator) two or more
76 * of those <code>SWT</code> style constants. The class description
77 * lists the style constants that are applicable to the class.
78 * Style bits are also inherited from superclasses.
81 * @param parent a composite control which will be the parent of the new instance (cannot be null)
82 * @param style the style of control to construct
84 * @exception IllegalArgumentException <ul>
85 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
87 * @exception SWTException <ul>
88 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
89 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
97 * @see SWT#SHADOW_NONE
102 * @see Widget#checkSubclass
103 * @see Widget#getStyle
105 public Label (Composite parent, int style) {
106 super (parent, checkStyle (style));
110 long callWindowProc (long hwnd, int msg, long wParam, long lParam) {
111 if (handle == 0) return 0;
113 * Feature in Windows 7. When the user double clicks
114 * on the label, the text of the label is copied to the
115 * clipboard. This is unwanted. The fix is to avoid
116 * calling the label window proc.
118 if (OS.WIN32_VERSION >= OS.VERSION(6, 1)) {
120 case OS.WM_LBUTTONDBLCLK: return OS.DefWindowProc (hwnd, msg, wParam, lParam);
123 return OS.CallWindowProc (LabelProc, hwnd, msg, wParam, lParam);
126 static int checkStyle (int style) {
127 style |= SWT.NO_FOCUS;
128 if ((style & SWT.SEPARATOR) != 0) {
129 style = checkBits (style, SWT.VERTICAL, SWT.HORIZONTAL, 0, 0, 0, 0);
130 return checkBits (style, SWT.SHADOW_OUT, SWT.SHADOW_IN, SWT.SHADOW_NONE, 0, 0, 0);
132 return checkBits (style, SWT.LEFT, SWT.CENTER, SWT.RIGHT, 0, 0, 0);
135 @Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
137 int width = 0, height = 0, border = getBorderWidthInPixels ();
138 if ((style & SWT.SEPARATOR) != 0) {
139 int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER);
140 if ((style & SWT.HORIZONTAL) != 0) {
141 width = DEFAULT_WIDTH; height = lineWidth * 2;
143 width = lineWidth * 2; height = DEFAULT_HEIGHT;
145 if (wHint != SWT.DEFAULT) width = wHint;
146 if (hHint != SWT.DEFAULT) height = hHint;
147 width += border * 2; height += border * 2;
148 return new Point (width, height);
150 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
151 boolean drawText = true;
152 boolean drawImage = (bits & OS.SS_OWNERDRAW) == OS.SS_OWNERDRAW;
155 Rectangle rect = image.getBoundsInPixels();
157 height += rect.height;
158 if (IMAGE_AND_TEXT) {
159 if (text.length () != 0) width += MARGIN;
166 long hDC = OS.GetDC (handle);
167 long newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
168 long oldFont = OS.SelectObject (hDC, newFont);
169 int length = OS.GetWindowTextLength (handle);
171 TEXTMETRIC tm = new TEXTMETRIC ();
172 OS.GetTextMetrics (hDC, tm);
173 height = Math.max (height, tm.tmHeight);
175 RECT rect = new RECT ();
176 int flags = OS.DT_CALCRECT | OS.DT_EDITCONTROL | OS.DT_EXPANDTABS;
177 if ((style & SWT.WRAP) != 0 && wHint != SWT.DEFAULT) {
178 flags |= OS.DT_WORDBREAK;
179 rect.right = Math.max (0, wHint - width);
181 char [] buffer = new char [length + 1];
182 OS.GetWindowText (handle, buffer, length + 1);
183 OS.DrawText (hDC, buffer, length, rect, flags);
184 width += rect.right - rect.left;
185 height = Math.max (height, rect.bottom - rect.top);
187 if (newFont != 0) OS.SelectObject (hDC, oldFont);
188 OS.ReleaseDC (handle, hDC);
190 if (wHint != SWT.DEFAULT) width = wHint;
191 if (hHint != SWT.DEFAULT) height = hHint;
193 height += border * 2;
194 return new Point (width, height);
198 void createHandle () {
199 super.createHandle ();
200 state |= THEME_BACKGROUND;
204 * Returns a value which describes the position of the
205 * text or image in the receiver. The value will be one of
206 * <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code>
207 * unless the receiver is a <code>SEPARATOR</code> label, in
208 * which case, <code>NONE</code> is returned.
210 * @return the alignment
212 * @exception SWTException <ul>
213 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
214 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
217 public int getAlignment () {
219 if ((style & SWT.SEPARATOR) != 0) return 0;
220 if ((style & SWT.LEFT) != 0) return SWT.LEFT;
221 if ((style & SWT.CENTER) != 0) return SWT.CENTER;
222 if ((style & SWT.RIGHT) != 0) return SWT.RIGHT;
227 * Returns the receiver's image if it has one, or null
230 * @return the receiver's image
232 * @exception SWTException <ul>
233 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
234 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
237 public Image getImage () {
243 String getNameText () {
248 * Returns the receiver's text, which will be an empty
249 * string if it has never been set or if the receiver is
250 * a <code>SEPARATOR</code> label.
252 * @return the receiver's text
254 * @exception SWTException <ul>
255 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
256 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
259 public String getText () {
261 if ((style & SWT.SEPARATOR) != 0) return "";
266 boolean mnemonicHit (char key) {
267 Control control = this;
268 while (control.parent != null) {
269 Control [] children = control.parent._getChildren ();
271 while (index < children.length) {
272 if (children [index] == control) break;
276 if (index < children.length) {
277 if (children [index].setFocus ()) return true;
279 control = control.parent;
285 boolean mnemonicMatch (char key) {
286 char mnemonic = findMnemonic (getText ());
287 if (mnemonic == '\0') return false;
288 return Character.toUpperCase (key) == Character.toUpperCase (mnemonic);
292 void releaseWidget () {
293 super.releaseWidget ();
299 int resolveTextDirection() {
300 return (style & SWT.SEPARATOR) != 0 ? SWT.NONE : BidiUtil.resolveTextDirection (text);
304 * Controls how text and images will be displayed in the receiver.
305 * The argument should be one of <code>LEFT</code>, <code>RIGHT</code>
306 * or <code>CENTER</code>. If the receiver is a <code>SEPARATOR</code>
307 * label, the argument is ignored and the alignment is not changed.
309 * @param alignment the new alignment
311 * @exception SWTException <ul>
312 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
313 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
316 public void setAlignment (int alignment) {
318 if ((style & SWT.SEPARATOR) != 0) return;
319 if ((alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER)) == 0) return;
320 style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER);
321 style |= alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER);
322 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
323 if ((bits & OS.SS_OWNERDRAW) != OS.SS_OWNERDRAW) {
324 bits &= ~(OS.SS_LEFTNOWORDWRAP | OS.SS_CENTER | OS.SS_RIGHT);
325 if ((style & SWT.LEFT) != 0) {
326 if ((style & SWT.WRAP) != 0) {
329 bits |= OS.SS_LEFTNOWORDWRAP;
332 if ((style & SWT.CENTER) != 0) bits |= OS.SS_CENTER;
333 if ((style & SWT.RIGHT) != 0) bits |= OS.SS_RIGHT;
334 OS.SetWindowLong (handle, OS.GWL_STYLE, bits);
336 OS.InvalidateRect (handle, null, true);
340 * Sets the receiver's image to the argument, which may be
341 * null indicating that no image should be displayed.
343 * @param image the image to display on the receiver (may be null)
345 * @exception IllegalArgumentException <ul>
346 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
348 * @exception SWTException <ul>
349 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
350 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
353 public void setImage (Image image) {
355 if ((style & SWT.SEPARATOR) != 0) return;
356 if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
358 updateStyleBits(image == null);
359 OS.InvalidateRect (handle, null, true);
363 * Sets the receiver's text.
365 * This method sets the widget label. The label may include
366 * the mnemonic character and line delimiters.
369 * Mnemonics are indicated by an '&' that causes the next
370 * character to be the mnemonic. When the user presses a
371 * key sequence that matches the mnemonic, focus is assigned
372 * to the control that follows the label. On most platforms,
373 * the mnemonic appears underlined but may be emphasised in a
374 * platform specific manner. The mnemonic indicator character
375 * '&' can be escaped by doubling it in the string, causing
376 * a single '&' to be displayed.
379 * Note: If control characters like '\n', '\t' etc. are used
380 * in the string, then the behavior is platform dependent.
383 * @param string the new text
385 * @exception IllegalArgumentException <ul>
386 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
388 * @exception SWTException <ul>
389 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
390 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
393 public void setText (String string) {
395 if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
396 if ((style & SWT.SEPARATOR) != 0) return;
397 updateStyleBits(true);
399 * Feature in Windows. For some reason, SetWindowText() for
400 * static controls redraws the control, even when the text has
401 * has not changed. The fix is to check for this case and do
404 if (string.equals (text)) return;
406 string = Display.withCrLf (string);
407 TCHAR buffer = new TCHAR (getCodePage (), string, true);
408 OS.SetWindowText (handle, buffer);
409 if ((state & HAS_AUTO_DIRECTION) != 0) {
410 updateTextDirection (AUTO_TEXT_DIRECTION);
415 * Update the control's static style bits to reflect the changed image vs. text
418 * @param showText if <code>true</code> set required style bits to render text
419 * otherwise to render the image
421 void updateStyleBits(boolean showText) {
423 int oldBits = OS.GetWindowLong (handle, OS.GWL_STYLE), newBits = oldBits;
424 newBits &= ~OS.SS_OWNERDRAW;
425 if ((style & SWT.LEFT) != 0) {
426 if ((style & SWT.WRAP) != 0) {
427 newBits |= OS.SS_LEFT;
429 newBits |= OS.SS_LEFTNOWORDWRAP;
432 if ((style & SWT.CENTER) != 0) newBits |= OS.SS_CENTER;
433 if ((style & SWT.RIGHT) != 0) newBits |= OS.SS_RIGHT;
434 if (oldBits != newBits) OS.SetWindowLong (handle, OS.GWL_STYLE, newBits);
436 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
437 if ((bits & OS.SS_OWNERDRAW) != OS.SS_OWNERDRAW) {
438 bits &= ~(OS.SS_LEFTNOWORDWRAP | OS.SS_CENTER | OS.SS_RIGHT);
439 bits |= OS.SS_OWNERDRAW;
440 OS.SetWindowLong (handle, OS.GWL_STYLE, bits);
446 int widgetExtStyle () {
447 int bits = super.widgetExtStyle () & ~OS.WS_EX_CLIENTEDGE;
448 if ((style & SWT.BORDER) != 0) return bits | OS.WS_EX_STATICEDGE;
454 int bits = super.widgetStyle () | OS.SS_NOTIFY;
455 if ((style & SWT.SEPARATOR) != 0) return bits | OS.SS_OWNERDRAW;
456 if ((style & SWT.WRAP) != 0) bits |= OS.SS_EDITCONTROL;
457 if ((style & SWT.CENTER) != 0) return bits | OS.SS_CENTER;
458 if ((style & SWT.RIGHT) != 0) return bits | OS.SS_RIGHT;
459 if ((style & SWT.WRAP) != 0) return bits | OS.SS_LEFT;
460 return bits | OS.SS_LEFTNOWORDWRAP;
464 TCHAR windowClass () {
474 LRESULT WM_ERASEBKGND (long wParam, long lParam) {
475 LRESULT result = super.WM_ERASEBKGND (wParam, lParam);
476 if (result != null) return result;
477 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
478 if ((bits & OS.SS_OWNERDRAW) == OS.SS_OWNERDRAW) {
485 LRESULT WM_SIZE (long wParam, long lParam) {
486 LRESULT result = super.WM_SIZE (wParam, lParam);
487 if (isDisposed ()) return result;
488 if ((style & SWT.SEPARATOR) != 0) {
489 OS.InvalidateRect (handle, null, true);
492 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
493 if ((bits & OS.SS_OWNERDRAW) == OS.SS_OWNERDRAW) {
494 OS.InvalidateRect (handle, null, true);
498 * Bug in Windows. For some reason, a label with
499 * style SS_LEFT, SS_CENTER or SS_RIGHT does not
500 * redraw the text in the new position when resized.
501 * Note that SS_LEFTNOWORDWRAP does not have the
502 * problem. The fix is to force the redraw.
504 if ((bits & OS.SS_LEFTNOWORDWRAP) != OS.SS_LEFTNOWORDWRAP) {
505 OS.InvalidateRect (handle, null, true);
512 LRESULT WM_UPDATEUISTATE (long wParam, long lParam) {
513 LRESULT result = super.WM_UPDATEUISTATE (wParam, lParam);
514 if (result != null) return result;
516 * Feature in Windows. When WM_UPDATEUISTATE is sent to
517 * a static control, it sends WM_CTLCOLORSTATIC to get the
518 * foreground and background. If any drawing happens in
519 * WM_CTLCOLORSTATIC, it overwrites the contents of the control.
520 * The fix is draw the static without drawing the background
521 * and avoid the static window proc.
523 boolean redraw = findImageControl () != null;
525 if ((state & THEME_BACKGROUND) != 0) {
526 if (OS.IsAppThemed ()) {
527 redraw = findThemeControl () != null;
532 OS.InvalidateRect (handle, null, false);
533 long code = OS.DefWindowProc (handle, OS.WM_UPDATEUISTATE, wParam, lParam);
534 return new LRESULT (code);
540 LRESULT wmDrawChild (long wParam, long lParam) {
541 DRAWITEMSTRUCT struct = new DRAWITEMSTRUCT ();
542 OS.MoveMemory (struct, lParam, DRAWITEMSTRUCT.sizeof);
543 drawBackground (struct.hDC);
544 if ((style & SWT.SEPARATOR) != 0) {
545 if ((style & SWT.SHADOW_NONE) != 0) return null;
546 RECT rect = new RECT ();
547 int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER);
548 int flags = (style & SWT.SHADOW_IN) != 0 ? OS.EDGE_SUNKEN : OS.EDGE_ETCHED;
549 if ((style & SWT.HORIZONTAL) != 0) {
550 int bottom = struct.top + Math.max (lineWidth * 2, (struct.bottom - struct.top) / 2);
551 OS.SetRect (rect, struct.left, struct.top, struct.right, bottom);
552 OS.DrawEdge (struct.hDC, rect, flags, OS.BF_BOTTOM);
554 int right = struct.left + Math.max (lineWidth * 2, (struct.right - struct.left) / 2);
555 OS.SetRect (rect, struct.left, struct.top, right, struct.bottom);
556 OS.DrawEdge (struct.hDC, rect, flags, OS.BF_RIGHT);
559 int width = struct.right - struct.left;
560 int height = struct.bottom - struct.top;
561 if (width != 0 && height != 0) {
562 boolean drawImage = image != null;
563 boolean drawText = IMAGE_AND_TEXT && text.length () != 0;
564 int margin = drawText && drawImage ? MARGIN : 0;
565 int imageWidth = 0, imageHeight = 0;
567 Rectangle rect = image.getBoundsInPixels ();
568 imageWidth = rect.width;
569 imageHeight = rect.height;
572 char [] buffer = null;
573 int textWidth = 0, textHeight = 0, flags = 0;
576 flags = OS.DT_CALCRECT | OS.DT_EDITCONTROL | OS.DT_EXPANDTABS;
577 if ((style & SWT.LEFT) != 0) flags |= OS.DT_LEFT;
578 if ((style & SWT.CENTER) != 0) flags |= OS.DT_CENTER;
579 if ((style & SWT.RIGHT) != 0) flags |= OS.DT_RIGHT;
580 if ((style & SWT.WRAP) != 0) {
581 flags |= OS.DT_WORDBREAK;
582 rect.right = Math.max (0, width - imageWidth - margin);
584 buffer = text.toCharArray ();
585 OS.DrawText (struct.hDC, buffer, buffer.length, rect, flags);
586 textWidth = rect.right - rect.left;
587 textHeight = rect.bottom - rect.top;
590 if ((style & SWT.CENTER) != 0) {
591 x = Math.max (0, (width - imageWidth - textWidth - margin) / 2);
593 if ((style & SWT.RIGHT) != 0) {
594 x = width - imageWidth - textWidth - margin;
598 GCData data = new GCData();
599 data.device = display;
600 GC gc = GC.win32_new (struct.hDC, data);
601 Image image = getEnabled () ? this.image : new Image (display, this.image, SWT.IMAGE_DISABLE);
602 gc.drawImage (image, DPIUtil.autoScaleDown(x), DPIUtil.autoScaleDown(Math.max (0, (height - imageHeight) / 2)));
603 if (image != this.image) image.dispose ();
605 x += imageWidth + margin;
608 flags &= ~OS.DT_CALCRECT;
610 rect.right += rect.left;
611 rect.top = Math.max (0, (height - textHeight) / 2);
612 rect.bottom += rect.top;
613 OS.DrawText (struct.hDC, buffer, buffer.length, rect, flags);