/******************************************************************************* * Copyright (c) 2000, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 * * Contributors: * IBM Corporation - initial API and implementation * Paul Pazderski - Bug 205199: setImage(null) on Label overrides text *******************************************************************************/ package org.eclipse.swt.widgets; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.internal.*; import org.eclipse.swt.internal.win32.*; /** * Instances of this class represent a non-selectable * user interface object that displays a string or image. * When SEPARATOR is specified, displays a single * vertical or horizontal line. *
* Shadow styles are hints and may not be honored * by the platform. To create a separator label * with the default shadow style for the platform, * do not specify a shadow style. *
** Note: Only one of SHADOW_IN, SHADOW_OUT and SHADOW_NONE may be specified. * SHADOW_NONE is a HINT. Only one of HORIZONTAL and VERTICAL may be specified. * Only one of CENTER, LEFT and RIGHT may be specified. *
* IMPORTANT: This class is not intended to be subclassed. *
* * @see Label snippets * @see SWT Example: ControlExample * @see Sample code and further information * @noextend This class is not intended to be subclassed by clients. */ public class Label extends Control { String text = ""; Image image; static final int MARGIN = 4; static /*final*/ boolean IMAGE_AND_TEXT = false; static final long LabelProc; static final TCHAR LabelClass = new TCHAR (0, "STATIC", true); static { WNDCLASS lpWndClass = new WNDCLASS (); OS.GetClassInfo (0, LabelClass, lpWndClass); LabelProc = lpWndClass.lpfnWndProc; } /** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. *
* The style value is either one of the style constants defined in
* class SWT
which is applicable to instances of this
* class, or must be built by bitwise OR'ing together
* (that is, using the int
"|" operator) two or more
* of those SWT
style constants. The class description
* lists the style constants that are applicable to the class.
* Style bits are also inherited from superclasses.
*
LEFT
, RIGHT
or CENTER
* unless the receiver is a SEPARATOR
label, in
* which case, NONE
is returned.
*
* @return the alignment
*
* @exception SWTException SEPARATOR
label.
*
* @return the receiver's text
*
* @exception SWTException LEFT
, RIGHT
* or CENTER
. If the receiver is a SEPARATOR
* label, the argument is ignored and the alignment is not changed.
*
* @param alignment the new alignment
*
* @exception SWTException * This method sets the widget label. The label may include * the mnemonic character and line delimiters. *
** Mnemonics are indicated by an '&' that causes the next * character to be the mnemonic. When the user presses a * key sequence that matches the mnemonic, focus is assigned * to the control that follows the label. On most platforms, * the mnemonic appears underlined but may be emphasised in a * platform specific manner. The mnemonic indicator character * '&' can be escaped by doubling it in the string, causing * a single '&' to be displayed. *
** Note: If control characters like '\n', '\t' etc. are used * in the string, then the behavior is platform dependent. *
* * @param string the new text * * @exception IllegalArgumentExceptiontrue
set required style bits to render text
* otherwise to render the image
*/
void updateStyleBits(boolean showText) {
if (showText) {
int oldBits = OS.GetWindowLong (handle, OS.GWL_STYLE), newBits = oldBits;
newBits &= ~OS.SS_OWNERDRAW;
if ((style & SWT.LEFT) != 0) {
if ((style & SWT.WRAP) != 0) {
newBits |= OS.SS_LEFT;
} else {
newBits |= OS.SS_LEFTNOWORDWRAP;
}
}
if ((style & SWT.CENTER) != 0) newBits |= OS.SS_CENTER;
if ((style & SWT.RIGHT) != 0) newBits |= OS.SS_RIGHT;
if (oldBits != newBits) OS.SetWindowLong (handle, OS.GWL_STYLE, newBits);
} else {
int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
if ((bits & OS.SS_OWNERDRAW) != OS.SS_OWNERDRAW) {
bits &= ~(OS.SS_LEFTNOWORDWRAP | OS.SS_CENTER | OS.SS_RIGHT);
bits |= OS.SS_OWNERDRAW;
OS.SetWindowLong (handle, OS.GWL_STYLE, bits);
}
}
}
@Override
int widgetExtStyle () {
int bits = super.widgetExtStyle () & ~OS.WS_EX_CLIENTEDGE;
if ((style & SWT.BORDER) != 0) return bits | OS.WS_EX_STATICEDGE;
return bits;
}
@Override
int widgetStyle () {
int bits = super.widgetStyle () | OS.SS_NOTIFY;
if ((style & SWT.SEPARATOR) != 0) return bits | OS.SS_OWNERDRAW;
if ((style & SWT.WRAP) != 0) bits |= OS.SS_EDITCONTROL;
if ((style & SWT.CENTER) != 0) return bits | OS.SS_CENTER;
if ((style & SWT.RIGHT) != 0) return bits | OS.SS_RIGHT;
if ((style & SWT.WRAP) != 0) return bits | OS.SS_LEFT;
return bits | OS.SS_LEFTNOWORDWRAP;
}
@Override
TCHAR windowClass () {
return LabelClass;
}
@Override
long windowProc () {
return LabelProc;
}
@Override
LRESULT WM_ERASEBKGND (long wParam, long lParam) {
LRESULT result = super.WM_ERASEBKGND (wParam, lParam);
if (result != null) return result;
int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
if ((bits & OS.SS_OWNERDRAW) == OS.SS_OWNERDRAW) {
return LRESULT.ONE;
}
return result;
}
@Override
LRESULT WM_SIZE (long wParam, long lParam) {
LRESULT result = super.WM_SIZE (wParam, lParam);
if (isDisposed ()) return result;
if ((style & SWT.SEPARATOR) != 0) {
OS.InvalidateRect (handle, null, true);
return result;
}
int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
if ((bits & OS.SS_OWNERDRAW) == OS.SS_OWNERDRAW) {
OS.InvalidateRect (handle, null, true);
return result;
}
/*
* Bug in Windows. For some reason, a label with
* style SS_LEFT, SS_CENTER or SS_RIGHT does not
* redraw the text in the new position when resized.
* Note that SS_LEFTNOWORDWRAP does not have the
* problem. The fix is to force the redraw.
*/
if ((bits & OS.SS_LEFTNOWORDWRAP) != OS.SS_LEFTNOWORDWRAP) {
OS.InvalidateRect (handle, null, true);
return result;
}
return result;
}
@Override
LRESULT WM_UPDATEUISTATE (long wParam, long lParam) {
LRESULT result = super.WM_UPDATEUISTATE (wParam, lParam);
if (result != null) return result;
/*
* Feature in Windows. When WM_UPDATEUISTATE is sent to
* a static control, it sends WM_CTLCOLORSTATIC to get the
* foreground and background. If any drawing happens in
* WM_CTLCOLORSTATIC, it overwrites the contents of the control.
* The fix is draw the static without drawing the background
* and avoid the static window proc.
*/
boolean redraw = findImageControl () != null;
if (!redraw) {
if ((state & THEME_BACKGROUND) != 0) {
if (OS.IsAppThemed ()) {
redraw = findThemeControl () != null;
}
}
}
if (redraw) {
OS.InvalidateRect (handle, null, false);
long code = OS.DefWindowProc (handle, OS.WM_UPDATEUISTATE, wParam, lParam);
return new LRESULT (code);
}
return result;
}
@Override
LRESULT wmDrawChild (long wParam, long lParam) {
DRAWITEMSTRUCT struct = new DRAWITEMSTRUCT ();
OS.MoveMemory (struct, lParam, DRAWITEMSTRUCT.sizeof);
drawBackground (struct.hDC);
if ((style & SWT.SEPARATOR) != 0) {
if ((style & SWT.SHADOW_NONE) != 0) return null;
RECT rect = new RECT ();
int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER);
int flags = (style & SWT.SHADOW_IN) != 0 ? OS.EDGE_SUNKEN : OS.EDGE_ETCHED;
if ((style & SWT.HORIZONTAL) != 0) {
int bottom = struct.top + Math.max (lineWidth * 2, (struct.bottom - struct.top) / 2);
OS.SetRect (rect, struct.left, struct.top, struct.right, bottom);
OS.DrawEdge (struct.hDC, rect, flags, OS.BF_BOTTOM);
} else {
int right = struct.left + Math.max (lineWidth * 2, (struct.right - struct.left) / 2);
OS.SetRect (rect, struct.left, struct.top, right, struct.bottom);
OS.DrawEdge (struct.hDC, rect, flags, OS.BF_RIGHT);
}
} else {
int width = struct.right - struct.left;
int height = struct.bottom - struct.top;
if (width != 0 && height != 0) {
boolean drawImage = image != null;
boolean drawText = IMAGE_AND_TEXT && text.length () != 0;
int margin = drawText && drawImage ? MARGIN : 0;
int imageWidth = 0, imageHeight = 0;
if (drawImage) {
Rectangle rect = image.getBoundsInPixels ();
imageWidth = rect.width;
imageHeight = rect.height;
}
RECT rect = null;
char [] buffer = null;
int textWidth = 0, textHeight = 0, flags = 0;
if (drawText) {
rect = new RECT ();
flags = OS.DT_CALCRECT | OS.DT_EDITCONTROL | OS.DT_EXPANDTABS;
if ((style & SWT.LEFT) != 0) flags |= OS.DT_LEFT;
if ((style & SWT.CENTER) != 0) flags |= OS.DT_CENTER;
if ((style & SWT.RIGHT) != 0) flags |= OS.DT_RIGHT;
if ((style & SWT.WRAP) != 0) {
flags |= OS.DT_WORDBREAK;
rect.right = Math.max (0, width - imageWidth - margin);
}
buffer = text.toCharArray ();
OS.DrawText (struct.hDC, buffer, buffer.length, rect, flags);
textWidth = rect.right - rect.left;
textHeight = rect.bottom - rect.top;
}
int x = 0;
if ((style & SWT.CENTER) != 0) {
x = Math.max (0, (width - imageWidth - textWidth - margin) / 2);
} else {
if ((style & SWT.RIGHT) != 0) {
x = width - imageWidth - textWidth - margin;
}
}
if (drawImage) {
GCData data = new GCData();
data.device = display;
GC gc = GC.win32_new (struct.hDC, data);
Image image = getEnabled () ? this.image : new Image (display, this.image, SWT.IMAGE_DISABLE);
gc.drawImage (image, DPIUtil.autoScaleDown(x), DPIUtil.autoScaleDown(Math.max (0, (height - imageHeight) / 2)));
if (image != this.image) image.dispose ();
gc.dispose ();
x += imageWidth + margin;
}
if (drawText) {
flags &= ~OS.DT_CALCRECT;
rect.left = x;
rect.right += rect.left;
rect.top = Math.max (0, (height - textHeight) / 2);
rect.bottom += rect.top;
OS.DrawText (struct.hDC, buffer, buffer.length, rect, flags);
}
}
}
return null;
}
}