1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
3 *
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/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
15
16
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.accessibility.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.internal.*;
21 import org.eclipse.swt.internal.gtk.*;
22
23 /**
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.
28 * <p>
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.
33 * </p>
34 * <dl>
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>
40 * <dd>(none)</dd>
41 * </dl>
42 * <p>
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.
46 * </p><p>
47 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
48 * </p>
49 *
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.
54 */
55 public class Label extends Control {
56 long frameHandle, labelHandle, imageHandle, boxHandle;
57 ImageList imageList;
58 Image image;
59 String text;
60
61 /**
62 * Constructs a new instance of this class given its parent
63 * and a style value describing its behavior and appearance.
64 * <p>
65 * The style value is either one of the style constants defined in
66 * class <code>SWT</code> which is applicable to instances of this
67 * class, or must be built by <em>bitwise OR</em>'ing together
68 * (that is, using the <code>int</code> "|" operator) two or more
69 * of those <code>SWT</code> style constants. The class description
70 * lists the style constants that are applicable to the class.
71 * Style bits are also inherited from superclasses.
72 * </p>
73 *
74 * @param parent a composite control which will be the parent of the new instance (cannot be null)
75 * @param style the style of control to construct
76 *
77 * @exception IllegalArgumentException <ul>
78 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
79 * </ul>
80 * @exception SWTException <ul>
81 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
82 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
83 * </ul>
84 *
85 * @see SWT#SEPARATOR
86 * @see SWT#HORIZONTAL
87 * @see SWT#VERTICAL
88 * @see SWT#SHADOW_IN
89 * @see SWT#SHADOW_OUT
90 * @see SWT#SHADOW_NONE
91 * @see SWT#CENTER
92 * @see SWT#LEFT
93 * @see SWT#RIGHT
94 * @see SWT#WRAP
95 * @see Widget#checkSubclass
96 * @see Widget#getStyle
97 */
Label(Composite parent, int style)98 public Label (Composite parent, int style) {
99 super (parent, checkStyle (style));
100 }
101
checkStyle(int style)102 static int checkStyle (int style) {
103 style |= SWT.NO_FOCUS;
104 if ((style & SWT.SEPARATOR) != 0) {
105 style = checkBits (style, SWT.VERTICAL, SWT.HORIZONTAL, 0, 0, 0, 0);
106 return checkBits (style, SWT.SHADOW_OUT, SWT.SHADOW_IN, SWT.SHADOW_NONE, 0, 0, 0);
107 }
108 return checkBits (style, SWT.LEFT, SWT.CENTER, SWT.RIGHT, 0, 0, 0);
109 }
110
111 @Override
addRelation(Control control)112 void addRelation (Control control) {
113 if (!control.isDescribedByLabel ()) return;
114 if (labelHandle == 0) return;
115 control._getAccessible().addRelation(ACC.RELATION_LABELLED_BY, _getAccessible());
116 control.labelRelation = this;
117 }
118
119 @Override
computeNativeSize(long h, int wHint, int hHint, boolean changed)120 Point computeNativeSize (long h, int wHint, int hHint, boolean changed) {
121 int width = wHint, height = hHint;
122 /*
123 * Feature in GTK3: Labels with long text have an extremely large natural width.
124 * As a result, such Labels created with SWT.WRAP end up being too big. The fix
125 * is to use the minimum width instead of the natural width when SWT.WRAP is specified.
126 * In all other cases, use the natural width. See bug 534768.
127 */
128 boolean wrap = labelHandle != 0 && (style & SWT.WRAP) != 0 && GTK.gtk_widget_get_visible (labelHandle);
129 if (wrap && wHint == SWT.DEFAULT && hHint == SWT.DEFAULT) {
130 GtkRequisition naturalSize = new GtkRequisition ();
131 GtkRequisition minimumSize = new GtkRequisition ();
132 GTK.gtk_widget_get_preferred_size(h, minimumSize, naturalSize);
133 width = minimumSize.width;
134 height = naturalSize.height;
135 return new Point(width, height);
136 } else {
137 return super.computeNativeSize(h, wHint, hHint, changed);
138 }
139 }
140 @Override
computeSizeInPixels(int wHint, int hHint, boolean changed)141 Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
142 checkWidget ();
143 if (wHint != SWT.DEFAULT && wHint < 0) wHint = 0;
144 if (hHint != SWT.DEFAULT && hHint < 0) hHint = 0;
145 if ((style & SWT.SEPARATOR) != 0) {
146 if ((style & SWT.HORIZONTAL) != 0) {
147 if (wHint == SWT.DEFAULT) wHint = DEFAULT_WIDTH;
148 } else {
149 if (hHint == SWT.DEFAULT) hHint = DEFAULT_HEIGHT;
150 }
151 }
152 Point size;
153 /*
154 * Feature in GTK. GTK has a predetermined maximum width for wrapping text.
155 * The fix is to use pango layout directly instead of the label size request
156 * to calculate its preferred size.
157 */
158 boolean fixWrap = labelHandle != 0 && (style & SWT.WRAP) != 0 && GTK.gtk_widget_get_visible (labelHandle);
159 if (fixWrap || frameHandle != 0) forceResize ();
160 if (fixWrap) {
161 long labelLayout = GTK.gtk_label_get_layout (labelHandle);
162 int pangoWidth = OS.pango_layout_get_width (labelLayout);
163 if (wHint != SWT.DEFAULT) {
164 OS.pango_layout_set_width (labelLayout, wHint * OS.PANGO_SCALE);
165 } else {
166 OS.pango_layout_set_width (labelLayout, -1);
167 }
168 int [] w = new int [1], h = new int [1];
169 OS.pango_layout_get_pixel_size (labelLayout, w, h);
170 OS.pango_layout_set_width (labelLayout, pangoWidth);
171 if (frameHandle != 0) {
172 int [] labelWidth = new int [1], labelHeight = new int [1];
173 GTK.gtk_widget_get_size_request (labelHandle, labelWidth, labelHeight);
174 GTK.gtk_widget_set_size_request (labelHandle, 1, 1);
175 size = computeNativeSize (frameHandle, -1, -1, changed);
176 GTK.gtk_widget_set_size_request (labelHandle, labelWidth [0], labelHeight [0]);
177 size.x = size.x - 1;
178 size.y = size.y - 1;
179 } else {
180 size = new Point (0,0);
181 }
182 size.x += wHint == SWT.DEFAULT ? w [0] : wHint;
183 size.y += hHint == SWT.DEFAULT ? h [0] : hHint;
184 } else {
185 if (frameHandle != 0) {
186 int [] reqWidth = new int [1], reqHeight = new int [1];
187 GTK.gtk_widget_get_size_request (handle, reqWidth, reqHeight);
188 GTK.gtk_widget_set_size_request (handle, wHint, hHint);
189 size = computeNativeSize (frameHandle, -1, -1, changed);
190 GTK.gtk_widget_set_size_request (handle, reqWidth [0], reqHeight [0]);
191 } else {
192 size = computeNativeSize (handle, wHint, hHint, changed);
193 }
194 }
195 /*
196 * Feature in GTK. Instead of using the font height to determine
197 * the preferred height of the widget, GTK uses the text metrics.
198 * The fix is to ensure that the preferred height is at least as
199 * tall as the font height.
200 *
201 * NOTE: This work around does not fix the case when there are
202 * muliple lines of text.
203 */
204 if (hHint == SWT.DEFAULT && labelHandle != 0) {
205 long layout = GTK.gtk_label_get_layout (labelHandle);
206 long context = OS.pango_layout_get_context (layout);
207 long lang = OS.pango_context_get_language (context);
208 long font = getFontDescription ();
209 long metrics = OS.pango_context_get_metrics (context, font, lang);
210 int ascent = OS.PANGO_PIXELS (OS.pango_font_metrics_get_ascent (metrics));
211 int descent = OS.PANGO_PIXELS (OS.pango_font_metrics_get_descent (metrics));
212 OS.pango_font_metrics_unref (metrics);
213 int fontHeight = ascent + descent;
214 int [] bufferBottom = new int [1];
215 int [] bufferTop = new int [1];
216 OS.g_object_get(labelHandle, OS.margin_bottom, bufferBottom, 0);
217 OS.g_object_get(labelHandle, OS.margin_top, bufferTop, 0);
218 fontHeight += bufferBottom [0] + bufferTop [0];
219 if (frameHandle != 0) {
220 fontHeight += 2 * getThickness (frameHandle).y;
221 fontHeight += 2 * gtk_container_get_border_width_or_margin (frameHandle);
222 }
223 size.y = Math.max (size.y, fontHeight);
224 }
225 return size;
226 }
227
228 @Override
createHandle(int index)229 void createHandle (int index) {
230 state |= HANDLE | THEME_BACKGROUND;
231 fixedHandle = OS.g_object_new (display.gtk_fixed_get_type (), 0);
232 if (fixedHandle == 0) error (SWT.ERROR_NO_HANDLES);
233 gtk_widget_set_has_surface_or_window (fixedHandle, true);
234 if ((style & SWT.SEPARATOR) != 0) {
235 if ((style & SWT.HORIZONTAL)!= 0) {
236 handle = GTK.gtk_separator_new (GTK.GTK_ORIENTATION_HORIZONTAL);
237 if (handle != 0) {
238 GTK.gtk_widget_set_valign(handle, GTK.GTK_ALIGN_CENTER);
239 }
240 } else {
241 handle = GTK.gtk_separator_new (GTK.GTK_ORIENTATION_VERTICAL);
242 if (handle != 0) {
243 GTK.gtk_widget_set_halign(handle, GTK.GTK_ALIGN_CENTER);
244 }
245 }
246 if (handle == 0) error (SWT.ERROR_NO_HANDLES);
247 } else {
248 if (GTK.GTK4) {
249 handle = gtk_box_new (GTK.GTK_ORIENTATION_HORIZONTAL, false, 0);
250 } else {
251 handle = GTK.gtk_event_box_new();
252 boxHandle = gtk_box_new (GTK.GTK_ORIENTATION_HORIZONTAL, false, 0);
253 if (boxHandle == 0) error (SWT.ERROR_NO_HANDLES);
254 }
255 if (handle == 0) error (SWT.ERROR_NO_HANDLES);
256 labelHandle = GTK.gtk_label_new_with_mnemonic (null);
257 if (labelHandle == 0) error (SWT.ERROR_NO_HANDLES);
258 imageHandle = GTK.gtk_image_new ();
259 if (imageHandle == 0) error (SWT.ERROR_NO_HANDLES);
260 if (GTK.GTK4) {
261 GTK.gtk_container_add (handle, labelHandle);
262 GTK.gtk_container_add (handle, imageHandle);
263 gtk_box_set_child_packing(handle, labelHandle, true, true, 0, GTK.GTK_PACK_START);
264 gtk_box_set_child_packing(handle, imageHandle, true, true, 0, GTK.GTK_PACK_START);
265 } else {
266 GTK.gtk_container_add (handle, boxHandle);
267 GTK.gtk_container_add (boxHandle, labelHandle);
268 GTK.gtk_container_add (boxHandle, imageHandle);
269 gtk_box_set_child_packing(boxHandle, labelHandle, true, true, 0, GTK.GTK_PACK_START);
270 gtk_box_set_child_packing(boxHandle, imageHandle, true, true, 0, GTK.GTK_PACK_START);
271 }
272 }
273 if ((style & SWT.BORDER) != 0) {
274 frameHandle = GTK.gtk_frame_new (null);
275 if (frameHandle == 0) error (SWT.ERROR_NO_HANDLES);
276 GTK.gtk_container_add (fixedHandle, frameHandle);
277 GTK.gtk_container_add (frameHandle, handle);
278 GTK.gtk_frame_set_shadow_type (frameHandle, GTK.GTK_SHADOW_ETCHED_IN);
279 } else {
280 GTK.gtk_container_add (fixedHandle, handle);
281 }
282 if ((style & SWT.SEPARATOR) != 0) return;
283 if ((style & SWT.WRAP) != 0) {
284 GTK.gtk_label_set_line_wrap (labelHandle, true);
285 GTK.gtk_label_set_line_wrap_mode (labelHandle, OS.PANGO_WRAP_WORD_CHAR);
286 }
287 // In GTK 3 font description is inherited from parent widget which is not how SWT has always worked,
288 // reset to default font to get the usual behavior
289 setFontDescription(defaultFont ().handle);
290 setAlignment ();
291 }
292
293 @Override
createWidget(int index)294 void createWidget (int index) {
295 super.createWidget (index);
296 text = "";
297 }
298
299 @Override
deregister()300 void deregister () {
301 super.deregister ();
302 if (frameHandle != 0) display.removeWidget (frameHandle);
303 if (labelHandle != 0) display.removeWidget (labelHandle);
304 if (imageHandle != 0) display.removeWidget (imageHandle);
305 if (boxHandle != 0) display.removeWidget (boxHandle);
306 }
307
308 @Override
eventHandle()309 long eventHandle () {
310 return fixedHandle;
311 }
312
313 @Override
cssHandle()314 long cssHandle () {
315 if ((style & SWT.SEPARATOR) == 0) {
316 return labelHandle;
317 }
318 return handle;
319 }
320
321 /**
322 * Returns a value which describes the position of the
323 * text or image in the receiver. The value will be one of
324 * <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code>
325 * unless the receiver is a <code>SEPARATOR</code> label, in
326 * which case, <code>NONE</code> is returned.
327 *
328 * @return the alignment
329 *
330 * @exception SWTException <ul>
331 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
332 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
333 * </ul>
334 */
getAlignment()335 public int getAlignment () {
336 checkWidget ();
337 if ((style & SWT.SEPARATOR) != 0) return 0;
338 if ((style & SWT.LEFT) != 0) return SWT.LEFT;
339 if ((style & SWT.CENTER) != 0) return SWT.CENTER;
340 if ((style & SWT.RIGHT) != 0) return SWT.RIGHT;
341 return SWT.LEFT;
342 }
343
344 @Override
getBorderWidthInPixels()345 int getBorderWidthInPixels () {
346 checkWidget();
347 if (frameHandle != 0) {
348 return getThickness (frameHandle).x;
349 }
350 return 0;
351 }
352
353 /**
354 * Returns the receiver's image if it has one, or null
355 * if it does not.
356 *
357 * @return the receiver's image
358 *
359 * @exception SWTException <ul>
360 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
361 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
362 * </ul>
363 */
getImage()364 public Image getImage () {
365 checkWidget ();
366 return image;
367 }
368
369 @Override
getNameText()370 String getNameText () {
371 return getText ();
372 }
373
374 /**
375 * Returns the receiver's text, which will be an empty
376 * string if it has never been set or if the receiver is
377 * a <code>SEPARATOR</code> label.
378 *
379 * @return the receiver's text
380 *
381 * @exception SWTException <ul>
382 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
383 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
384 * </ul>
385 */
getText()386 public String getText () {
387 checkWidget ();
388 if ((style & SWT.SEPARATOR) != 0) return "";
389 return text;
390 }
391
392 @Override
hookEvents()393 void hookEvents () {
394 super.hookEvents();
395 if (labelHandle != 0) {
396 OS.g_signal_connect_closure_by_id (labelHandle, display.signalIds [MNEMONIC_ACTIVATE], 0, display.getClosure (MNEMONIC_ACTIVATE), false);
397 }
398 }
399
400 @Override
isDescribedByLabel()401 boolean isDescribedByLabel () {
402 return false;
403 }
404
405 @Override
mnemonicHit(char key)406 boolean mnemonicHit (char key) {
407 if (labelHandle == 0) return false;
408 boolean result = super.mnemonicHit (labelHandle, key);
409 if (result) {
410 Control control = this;
411 while (control.parent != null) {
412 Control [] children = control.parent._getChildren ();
413 int index = 0;
414 while (index < children.length) {
415 if (children [index] == control) break;
416 index++;
417 }
418 index++;
419 if (index < children.length) {
420 if (children [index].setFocus ()) return result;
421 }
422 control = control.parent;
423 }
424 }
425 return result;
426 }
427
428 @Override
mnemonicMatch(char key)429 boolean mnemonicMatch (char key) {
430 if (labelHandle == 0) return false;
431 return mnemonicMatch (labelHandle, key);
432 }
433
434 @Override
register()435 void register () {
436 super.register ();
437 if (boxHandle != 0) display.addWidget (boxHandle, this);
438 if (frameHandle != 0) display.addWidget (frameHandle, this);
439 if (labelHandle != 0) display.addWidget (labelHandle, this);
440 if (imageHandle != 0) display.addWidget (imageHandle, this);
441 }
442
443 @Override
releaseHandle()444 void releaseHandle () {
445 super.releaseHandle ();
446 frameHandle = imageHandle = labelHandle = boxHandle = 0;
447 }
448
449 @Override
releaseWidget()450 void releaseWidget () {
451 super.releaseWidget ();
452 if (imageList != null) imageList.dispose ();
453 imageList = null;
454 image = null;
455 text = null;
456 }
457
458 @Override
resizeHandle(int width, int height)459 void resizeHandle (int width, int height) {
460 OS.swt_fixed_resize (GTK.gtk_widget_get_parent (fixedHandle), fixedHandle, width, height);
461 long child = frameHandle != 0 ? frameHandle : handle;
462 Point sizes = resizeCalculationsGTK3 (child, width, height);
463 width = sizes.x;
464 height = sizes.y;
465 OS.swt_fixed_resize (GTK.gtk_widget_get_parent (child), child, width, height);
466 }
467
468 /**
469 * Controls how text and images will be displayed in the receiver.
470 * The argument should be one of <code>LEFT</code>, <code>RIGHT</code>
471 * or <code>CENTER</code>. If the receiver is a <code>SEPARATOR</code>
472 * label, the argument is ignored and the alignment is not changed.
473 *
474 * @param alignment the new alignment
475 *
476 * @exception SWTException <ul>
477 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
478 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
479 * </ul>
480 */
setAlignment(int alignment)481 public void setAlignment (int alignment) {
482 checkWidget ();
483 if ((style & SWT.SEPARATOR) != 0) return;
484 if ((alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER)) == 0) return;
485 style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER);
486 style |= alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER);
487 setAlignment ();
488 }
489
setAlignment()490 void setAlignment () {
491 if ((style & SWT.LEFT) != 0) {
492 gtk_widget_set_align(labelHandle,GTK.GTK_ALIGN_START, GTK.GTK_ALIGN_START); //Aligns widget
493 gtk_label_set_align (0.0f, 0.0f); //Aligns text inside the widget.
494 gtk_widget_set_align(imageHandle, GTK.GTK_ALIGN_START, GTK.GTK_ALIGN_CENTER);
495 GTK.gtk_label_set_justify (labelHandle, GTK.GTK_JUSTIFY_LEFT);
496 return;
497 }
498 if ((style & SWT.CENTER) != 0) {
499 gtk_widget_set_align(labelHandle,GTK.GTK_ALIGN_CENTER, GTK.GTK_ALIGN_START); //Aligns widget
500 gtk_label_set_align (0.5f, 0.0f); //Aligns text inside the widget.
501 gtk_widget_set_align(imageHandle, GTK.GTK_ALIGN_CENTER, GTK.GTK_ALIGN_CENTER);
502
503 GTK.gtk_label_set_justify (labelHandle, GTK.GTK_JUSTIFY_CENTER);
504 return;
505 }
506 if ((style & SWT.RIGHT) != 0) {
507 gtk_widget_set_align(labelHandle,GTK.GTK_ALIGN_END, GTK.GTK_ALIGN_START); //Aligns widget.
508 gtk_label_set_align (1.0f, 0.0f); //Aligns text inside the widget.
509 gtk_widget_set_align(imageHandle, GTK.GTK_ALIGN_END, GTK.GTK_ALIGN_CENTER);
510 GTK.gtk_label_set_justify (labelHandle, GTK.GTK_JUSTIFY_RIGHT);
511 return;
512 }
513 }
514
gtk_label_set_align(float xalign, float yalign)515 private void gtk_label_set_align (float xalign, float yalign) {
516 GTK.gtk_label_set_xalign (labelHandle, xalign);
517 GTK.gtk_label_set_yalign (labelHandle, yalign);
518 }
519
520 @Override
setBounds(int x, int y, int width, int height, boolean move, boolean resize)521 int setBounds (int x, int y, int width, int height, boolean move, boolean resize) {
522 /*
523 * Bug in GTK. For some reason, when the label is
524 * wrappable and its container is resized, it does not
525 * cause the label to be wrapped. The fix is to
526 * determine the size that will wrap the label
527 * and expilictly set that size to force the label
528 * to wrap.
529 *
530 * This part of the fix causes the label to be
531 * resized to the preferred size but it still
532 * won't draw properly.
533 */
534 boolean fixWrap = resize && labelHandle != 0 && (style & SWT.WRAP) != 0;
535 if (fixWrap) GTK.gtk_widget_set_size_request (labelHandle, -1, -1);
536 int result = super.setBounds (x, y, width, height, move, resize);
537 /*
538 * Bug in GTK. For some reason, when the label is
539 * wrappable and its container is resized, it does not
540 * cause the label to be wrapped. The fix is to
541 * determine the size that will wrap the label
542 * and expilictly set that size to force the label
543 * to wrap.
544 *
545 * This part of the fix forces the label to be
546 * resized so that it will draw wrapped.
547 */
548 if (fixWrap) {
549 GtkAllocation allocation = new GtkAllocation();
550 GTK.gtk_widget_get_allocation (handle, allocation);
551 int labelWidth = allocation.width;
552 int labelHeight = allocation.height;
553 GTK.gtk_widget_set_size_request (labelHandle, labelWidth, labelHeight);
554 /*
555 * Bug in GTK. Setting the size request should invalidate the label's
556 * layout, but it does not. The fix is to resize the label directly.
557 */
558 GtkRequisition requisition = new GtkRequisition ();
559 gtk_widget_get_preferred_size (labelHandle, requisition);
560 GTK.gtk_widget_get_allocation(labelHandle, allocation);
561 allocation.width = labelWidth;
562 allocation.height = labelHeight;
563 GTK.gtk_widget_size_allocate (labelHandle, allocation);
564 }
565 return result;
566 }
567
568 @Override
setFontDescription(long font)569 void setFontDescription (long font) {
570 super.setFontDescription (font);
571 if (labelHandle != 0) setFontDescription (labelHandle, font);
572 if (imageHandle != 0) setFontDescription (imageHandle, font);
573
574 // Bug 445801: Work around for computeSize not returning a different value after
575 // changing font, see https://bugzilla.gnome.org/show_bug.cgi?id=753116
576 // This updates the pango context and also clears the size request cache on the GTK side.
577 int originalDirection = (style & SWT.RIGHT_TO_LEFT) != 0 ? GTK.GTK_TEXT_DIR_RTL : GTK.GTK_TEXT_DIR_LTR;
578 int tempDirection = (style & SWT.RIGHT_TO_LEFT) != 0 ? GTK.GTK_TEXT_DIR_LTR : GTK.GTK_TEXT_DIR_RTL;
579 GTK.gtk_widget_set_direction (labelHandle, tempDirection);
580 GTK.gtk_widget_set_direction (labelHandle, originalDirection);
581 }
582
583 @Override
setForegroundGdkRGBA(GdkRGBA rgba)584 void setForegroundGdkRGBA (GdkRGBA rgba) {
585 super.setForegroundGdkRGBA (rgba);
586 setForegroundGdkRGBA (fixedHandle, rgba);
587 if (labelHandle != 0) setForegroundGdkRGBA (labelHandle, rgba);
588 if (imageHandle != 0) setForegroundGdkRGBA (imageHandle, rgba);
589 }
590
591 @Override
setOrientation(boolean create)592 void setOrientation (boolean create) {
593 super.setOrientation (create);
594 if ((style & SWT.RIGHT_TO_LEFT) != 0 || !create) {
595 int dir = (style & SWT.RIGHT_TO_LEFT) != 0 ? GTK.GTK_TEXT_DIR_RTL : GTK.GTK_TEXT_DIR_LTR;
596 if (labelHandle != 0) GTK.gtk_widget_set_direction (labelHandle, dir);
597 if (imageHandle != 0) GTK.gtk_widget_set_direction (imageHandle, dir);
598 }
599 }
600
601 /**
602 * Sets the receiver's image to the argument, which may be
603 * null indicating that no image should be displayed.
604 *
605 * @param image the image to display on the receiver (may be null)
606 *
607 * @exception IllegalArgumentException <ul>
608 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
609 * </ul>
610 * @exception SWTException <ul>
611 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
612 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
613 * </ul>
614 */
setImage(Image image)615 public void setImage (Image image) {
616 checkWidget ();
617 if (image != null && image.isDisposed ()) {
618 error(SWT.ERROR_INVALID_ARGUMENT);
619 }
620 if ((style & SWT.SEPARATOR) != 0) return;
621 this.image = image;
622 if (imageList != null) imageList.dispose ();
623 imageList = null;
624 if (image != null) {
625 imageList = new ImageList ();
626 int imageIndex = imageList.add (image);
627 long pixbuf = imageList.getPixbuf (imageIndex);
628 gtk_image_set_from_gicon (imageHandle, pixbuf);
629 GTK.gtk_widget_hide (labelHandle);
630 GTK.gtk_widget_show (imageHandle);
631 } else {
632 gtk_image_set_from_gicon (imageHandle, 0);
633 GTK.gtk_widget_show (labelHandle);
634 GTK.gtk_widget_hide (imageHandle);
635 }
636 }
637
638 /**
639 * Sets the receiver's text.
640 * <p>
641 * This method sets the widget label. The label may include
642 * the mnemonic character and line delimiters.
643 * </p>
644 * <p>
645 * Mnemonics are indicated by an '&' that causes the next
646 * character to be the mnemonic. When the user presses a
647 * key sequence that matches the mnemonic, focus is assigned
648 * to the control that follows the label. On most platforms,
649 * the mnemonic appears underlined but may be emphasised in a
650 * platform specific manner. The mnemonic indicator character
651 * '&' can be escaped by doubling it in the string, causing
652 * a single '&' to be displayed.
653 * </p>
654 * <p>
655 * Note: If control characters like '\n', '\t' etc. are used
656 * in the string, then the behavior is platform dependent.
657 * </p>
658 *
659 * @param string the new text
660 *
661 * @exception IllegalArgumentException <ul>
662 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
663 * </ul>
664 * @exception SWTException <ul>
665 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
666 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
667 * </ul>
668 */
setText(String string)669 public void setText (String string) {
670 checkWidget ();
671 if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
672 if ((style & SWT.SEPARATOR) != 0) return;
673 text = string;
674 char [] chars = fixMnemonic (string);
675 byte [] buffer = Converter.wcsToMbcs (chars, true);
676 GTK.gtk_label_set_text_with_mnemonic (labelHandle, buffer);
677 GTK.gtk_widget_hide (imageHandle);
678 GTK.gtk_widget_show (labelHandle);
679 }
680
681 @Override
setWidgetBackground()682 void setWidgetBackground () {
683 GdkRGBA rgba = (state & BACKGROUND) != 0 ? getBackgroundGdkRGBA () : null;
684 super.setBackgroundGdkRGBA (handle, rgba);
685 }
686
687 @Override
showWidget()688 void showWidget () {
689 super.showWidget ();
690 if (frameHandle != 0) GTK.gtk_widget_show (frameHandle);
691 if (labelHandle != 0) GTK.gtk_widget_show (labelHandle);
692 if (boxHandle != 0) GTK.gtk_widget_show (boxHandle);
693 }
694
695 @Override
windowProc(long handle, long arg0, long user_data)696 long windowProc (long handle, long arg0, long user_data) {
697 /*
698 * For Labels/Buttons, the first widget in the tree with a GdkWindow is SwtFixed.
699 * Unfortunately this fails the check in !GTK_IS_CONTAINER check Widget.windowProc().
700 * Instead lets override windowProc() here and check for paintHandle() compatibility.
701 * Fixes bug 481485 without re-introducing bug 483791.
702 */
703 switch ((int)user_data) {
704 case DRAW: {
705 if (paintHandle() == handle) {
706 return gtk_draw(handle, arg0);
707 }
708 }
709 }
710 return super.windowProc(handle, arg0, user_data);
711 }
712 }
713