Mercurial > projects > dwt-linux
annotate dwt/widgets/Label.d @ 207:12feeed18183
Allow Label.setText with null argument.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 09 Mar 2008 10:58:49 +0100 |
parents | 17f8449522fd |
children | 380bad9f6852 |
rev | line source |
---|---|
72 | 1 /******************************************************************************* |
64 | 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 | |
72 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
64 | 12 *******************************************************************************/ |
13 module dwt.widgets.Label; | |
14 | |
15 | |
71 | 16 import dwt.DWT; |
64 | 17 import dwt.internal.gtk.OS; |
18 import dwt.graphics.Point; | |
19 import dwt.graphics.Image; | |
20 import dwt.widgets.Control; | |
21 import dwt.widgets.Composite; | |
22 import dwt.widgets.ImageList; | |
23 | |
207
12feeed18183
Allow Label.setText with null argument.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
24 import dwt.dwthelper.utils; |
64 | 25 |
26 /** | |
27 * Instances of this class represent a non-selectable | |
28 * user interface object that displays a string or image. | |
29 * When SEPARATOR is specified, displays a single | |
30 * vertical or horizontal line. | |
31 * <dl> | |
32 * <dt><b>Styles:</b></dt> | |
33 * <dd>SEPARATOR, HORIZONTAL, VERTICAL</dd> | |
34 * <dd>SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd> | |
35 * <dd>CENTER, LEFT, RIGHT, WRAP</dd> | |
36 * <dt><b>Events:</b></dt> | |
37 * <dd>(none)</dd> | |
38 * </dl> | |
39 * <p> | |
40 * Note: Only one of SHADOW_IN, SHADOW_OUT and SHADOW_NONE may be specified. | |
41 * SHADOW_NONE is a HINT. Only one of HORIZONTAL and VERTICAL may be specified. | |
42 * Only one of CENTER, LEFT and RIGHT may be specified. | |
43 * </p><p> | |
44 * IMPORTANT: This class is intended to be subclassed <em>only</em> | |
71 | 45 * within the DWT implementation. |
64 | 46 * </p> |
47 */ | |
48 public class Label : Control { | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
105
diff
changeset
|
49 |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
105
diff
changeset
|
50 alias Control.computeSize computeSize; |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
105
diff
changeset
|
51 alias Control.mnemonicHit mnemonicHit; |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
105
diff
changeset
|
52 alias Control.mnemonicMatch mnemonicMatch; |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
105
diff
changeset
|
53 alias Control.setBackgroundColor setBackgroundColor; |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
105
diff
changeset
|
54 alias Control.setBounds setBounds; |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
105
diff
changeset
|
55 alias Control.setForegroundColor setForegroundColor; |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
105
diff
changeset
|
56 |
64 | 57 GtkWidget* frameHandle, labelHandle, imageHandle; |
58 ImageList imageList; | |
59 Image image; | |
60 char[] text; | |
61 | |
62 /** | |
63 * Constructs a new instance of this class given its parent | |
64 * and a style value describing its behavior and appearance. | |
65 * <p> | |
66 * The style value is either one of the style constants defined in | |
71 | 67 * class <code>DWT</code> which is applicable to instances of this |
64 | 68 * class, or must be built by <em>bitwise OR</em>'ing together |
69 * (that is, using the <code>int</code> "|" operator) two or more | |
71 | 70 * of those <code>DWT</code> style constants. The class description |
64 | 71 * lists the style constants that are applicable to the class. |
72 * Style bits are also inherited from superclasses. | |
73 * </p> | |
74 * | |
75 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
76 * @param style the style of control to construct | |
77 * | |
78 * @exception IllegalArgumentException <ul> | |
79 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
80 * </ul> | |
71 | 81 * @exception DWTException <ul> |
64 | 82 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> |
83 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
84 * </ul> | |
85 * | |
71 | 86 * @see DWT#SEPARATOR |
87 * @see DWT#HORIZONTAL | |
88 * @see DWT#VERTICAL | |
89 * @see DWT#SHADOW_IN | |
90 * @see DWT#SHADOW_OUT | |
91 * @see DWT#SHADOW_NONE | |
92 * @see DWT#CENTER | |
93 * @see DWT#LEFT | |
94 * @see DWT#RIGHT | |
95 * @see DWT#WRAP | |
64 | 96 * @see Widget#checkSubclass |
97 * @see Widget#getStyle | |
98 */ | |
99 public this (Composite parent, int style) { | |
100 super (parent, checkStyle (style)); | |
101 } | |
102 | |
103 static int checkStyle (int style) { | |
71 | 104 style |= DWT.NO_FOCUS; |
105 if ((style & DWT.SEPARATOR) !is 0) { | |
106 style = checkBits (style, DWT.VERTICAL, DWT.HORIZONTAL, 0, 0, 0, 0); | |
107 return checkBits (style, DWT.SHADOW_OUT, DWT.SHADOW_IN, DWT.SHADOW_NONE, 0, 0, 0); | |
64 | 108 } |
71 | 109 return checkBits (style, DWT.LEFT, DWT.CENTER, DWT.RIGHT, 0, 0, 0); |
64 | 110 } |
111 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
112 override void addRelation (Control control) { |
64 | 113 if (!control.isDescribedByLabel ()) return; |
114 if (labelHandle is null) return; | |
115 auto accessible = OS.gtk_widget_get_accessible (labelHandle); | |
116 auto controlAccessible = OS.gtk_widget_get_accessible (control.handle); | |
117 if (accessible !is null && controlAccessible !is null) { | |
118 OS.atk_object_add_relationship (controlAccessible, OS.ATK_RELATION_LABELLED_BY, accessible); | |
119 } | |
120 } | |
121 | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
105
diff
changeset
|
122 public override Point computeSize (int wHint, int hHint, bool changed) { |
64 | 123 checkWidget (); |
71 | 124 if (wHint !is DWT.DEFAULT && wHint < 0) wHint = 0; |
125 if (hHint !is DWT.DEFAULT && hHint < 0) hHint = 0; | |
126 if ((style & DWT.SEPARATOR) !is 0) { | |
127 if ((style & DWT.HORIZONTAL) !is 0) { | |
128 if (wHint is DWT.DEFAULT) wHint = DEFAULT_WIDTH; | |
64 | 129 } else { |
71 | 130 if (hHint is DWT.DEFAULT) hHint = DEFAULT_HEIGHT; |
64 | 131 } |
132 } | |
71 | 133 bool fixWrap = labelHandle !is null && (style & DWT.WRAP) !is 0; |
64 | 134 if (fixWrap || frameHandle !is null) forceResize (); |
135 int labelWidth, labelHeight; | |
136 if (fixWrap) { | |
137 OS.gtk_widget_get_size_request (labelHandle, &labelWidth, &labelHeight); | |
138 OS.gtk_widget_set_size_request (labelHandle, wHint, hHint); | |
139 } | |
140 Point size; | |
141 if (frameHandle !is null) { | |
142 int reqWidth, reqHeight; | |
143 OS.gtk_widget_get_size_request (handle, &reqWidth, &reqHeight); | |
144 OS.gtk_widget_set_size_request (handle, wHint, hHint); | |
145 size = computeNativeSize (frameHandle, -1, -1, changed); | |
146 OS.gtk_widget_set_size_request (handle, reqWidth, reqHeight); | |
147 } else { | |
148 size = computeNativeSize (handle, wHint, hHint, changed); | |
149 } | |
150 if (fixWrap) { | |
151 OS.gtk_widget_set_size_request (labelHandle, labelWidth, labelHeight); | |
152 } | |
153 /* | |
154 * Feature in GTK. Instead of using the font height to determine | |
155 * the preferred height of the widget, GTK uses the text metrics. | |
156 * The fix is to ensure that the preferred height is at least as | |
157 * tall as the font height. | |
158 * | |
159 * NOTE: This work around does not fix the case when there are | |
160 * muliple lines of text. | |
161 */ | |
71 | 162 if (hHint is DWT.DEFAULT && labelHandle !is null) { |
64 | 163 auto layout = OS.gtk_label_get_layout (cast(GtkLabel*)labelHandle); |
164 auto context = OS.pango_layout_get_context (layout); | |
165 auto lang = OS.pango_context_get_language (context); | |
166 auto font = getFontDescription (); | |
167 auto metrics = OS.pango_context_get_metrics (context, font, lang); | |
168 int ascent = OS.PANGO_PIXELS (OS.pango_font_metrics_get_ascent (metrics)); | |
169 int descent = OS.PANGO_PIXELS (OS.pango_font_metrics_get_descent (metrics)); | |
170 OS.pango_font_metrics_unref (metrics); | |
171 int fontHeight = ascent + descent; | |
172 int buffer; | |
173 OS.g_object_get1 (labelHandle, OS.ypad.ptr, &buffer); | |
174 fontHeight += 2 * buffer; | |
175 if (frameHandle !is null) { | |
176 auto style = OS.gtk_widget_get_style (frameHandle); | |
177 fontHeight += 2 * OS.gtk_style_get_ythickness (style); | |
178 fontHeight += 2 * OS.gtk_container_get_border_width (cast(GtkContainer*)frameHandle); | |
179 } | |
180 size.y = Math.max (size.y, fontHeight); | |
181 } | |
182 return size; | |
183 } | |
184 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
185 override void createHandle (int index) { |
64 | 186 state |= HANDLE | THEME_BACKGROUND; |
187 fixedHandle = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null); | |
71 | 188 if (fixedHandle is null) error (DWT.ERROR_NO_HANDLES); |
64 | 189 OS.gtk_fixed_set_has_window (cast(GtkFixed*)fixedHandle, true); |
71 | 190 if ((style & DWT.SEPARATOR) !is 0) { |
191 if ((style & DWT.HORIZONTAL)!is 0) { | |
64 | 192 handle = OS.gtk_hseparator_new (); |
193 } else { | |
194 handle = OS.gtk_vseparator_new (); | |
195 } | |
71 | 196 if (handle is null) error (DWT.ERROR_NO_HANDLES); |
64 | 197 } else { |
198 handle = OS.gtk_hbox_new (false, 0); | |
71 | 199 if (handle is null) error (DWT.ERROR_NO_HANDLES); |
64 | 200 labelHandle = OS.gtk_label_new_with_mnemonic (null); |
71 | 201 if (labelHandle is null) error (DWT.ERROR_NO_HANDLES); |
64 | 202 imageHandle = OS.gtk_image_new (); |
71 | 203 if (imageHandle is null) error (DWT.ERROR_NO_HANDLES); |
64 | 204 OS.gtk_container_add (cast(GtkContainer*)handle, labelHandle); |
205 OS.gtk_container_add (cast(GtkContainer*)handle, imageHandle); | |
206 } | |
71 | 207 if ((style & DWT.BORDER) !is 0) { |
64 | 208 frameHandle = OS.gtk_frame_new (null); |
71 | 209 if (frameHandle is null) error (DWT.ERROR_NO_HANDLES); |
64 | 210 OS.gtk_container_add (cast(GtkContainer*)fixedHandle, frameHandle); |
211 OS.gtk_container_add (cast(GtkContainer*)frameHandle, handle); | |
212 OS.gtk_frame_set_shadow_type (cast(GtkFrame*)frameHandle, OS.GTK_SHADOW_ETCHED_IN); | |
213 } else { | |
214 OS.gtk_container_add (cast(GtkContainer*)fixedHandle, handle); | |
215 } | |
71 | 216 if ((style & DWT.SEPARATOR) !is 0) return; |
217 if ((style & DWT.WRAP) !is 0) OS.gtk_label_set_line_wrap (cast(GtkLabel*)labelHandle, true); | |
218 if ((style & DWT.LEFT) !is 0) { | |
64 | 219 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0.0f, 0.0f); |
220 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_LEFT); | |
221 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 0.0f, 0.5f); | |
222 return; | |
223 } | |
71 | 224 if ((style & DWT.CENTER) !is 0) { |
64 | 225 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0.5f, 0.0f); |
226 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_CENTER); | |
227 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 0.5f, 0.5f); | |
228 return; | |
229 } | |
71 | 230 if ((style & DWT.RIGHT) !is 0) { |
64 | 231 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 1.0f, 0.0f); |
232 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_RIGHT); | |
233 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 1.0f, 0.5f); | |
234 return; | |
235 } | |
236 } | |
237 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
238 override void createWidget (int index) { |
64 | 239 super.createWidget (index); |
240 text = ""; | |
241 } | |
242 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
243 override void deregister () { |
64 | 244 super.deregister (); |
245 if (frameHandle !is null) display.removeWidget (frameHandle); | |
246 if (labelHandle !is null) display.removeWidget (labelHandle); | |
247 if (imageHandle !is null) display.removeWidget (imageHandle); | |
248 } | |
249 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
250 override GtkWidget* eventHandle () { |
64 | 251 return fixedHandle; |
252 } | |
253 | |
254 /** | |
255 * Returns a value which describes the position of the | |
256 * text or image in the receiver. The value will be one of | |
257 * <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code> | |
258 * unless the receiver is a <code>SEPARATOR</code> label, in | |
259 * which case, <code>NONE</code> is returned. | |
260 * | |
261 * @return the alignment | |
262 * | |
71 | 263 * @exception DWTException <ul> |
64 | 264 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
265 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
266 * </ul> | |
267 */ | |
268 public int getAlignment () { | |
269 checkWidget (); | |
71 | 270 if ((style & DWT.SEPARATOR) !is 0) return 0; |
271 if ((style & DWT.LEFT) !is 0) return DWT.LEFT; | |
272 if ((style & DWT.CENTER) !is 0) return DWT.CENTER; | |
273 if ((style & DWT.RIGHT) !is 0) return DWT.RIGHT; | |
274 return DWT.LEFT; | |
64 | 275 } |
276 | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
105
diff
changeset
|
277 public override int getBorderWidth () { |
64 | 278 checkWidget(); |
279 if (frameHandle !is null) { | |
280 return OS.gtk_style_get_xthickness (OS.gtk_widget_get_style (frameHandle)); | |
281 } | |
282 return 0; | |
283 } | |
284 | |
285 /** | |
286 * Returns the receiver's image if it has one, or null | |
287 * if it does not. | |
288 * | |
289 * @return the receiver's image | |
290 * | |
71 | 291 * @exception DWTException <ul> |
64 | 292 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
293 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
294 * </ul> | |
295 */ | |
296 public Image getImage () { | |
297 checkWidget (); | |
298 return image; | |
299 } | |
300 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
301 override char[] getNameText () { |
64 | 302 return getText (); |
303 } | |
304 | |
305 /** | |
306 * Returns the receiver's text, which will be an empty | |
307 * string if it has never been set or if the receiver is | |
308 * a <code>SEPARATOR</code> label. | |
309 * | |
310 * @return the receiver's text | |
311 * | |
71 | 312 * @exception DWTException <ul> |
64 | 313 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
314 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
315 * </ul> | |
316 */ | |
317 public char[] getText () { | |
318 checkWidget (); | |
71 | 319 if ((style & DWT.SEPARATOR) !is 0) return ""; |
64 | 320 return text; |
321 } | |
322 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
323 override void hookEvents () { |
64 | 324 super.hookEvents(); |
325 if (labelHandle !is null) { | |
326 OS.g_signal_connect_closure_by_id (labelHandle, display.signalIds [MNEMONIC_ACTIVATE], 0, display.closures [MNEMONIC_ACTIVATE], false); | |
327 } | |
328 } | |
329 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
330 override bool isDescribedByLabel () { |
64 | 331 return false; |
332 } | |
333 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
334 override bool mnemonicHit (wchar key) { |
64 | 335 if (labelHandle is null) return false; |
336 bool result = super.mnemonicHit (labelHandle, key); | |
337 if (result) { | |
338 Composite control = this.parent; | |
339 while (control !is null) { | |
340 Control [] children = control._getChildren (); | |
341 int index = 0; | |
342 while (index < children.length) { | |
343 if (children [index] is this) break; | |
344 index++; | |
345 } | |
346 index++; | |
347 if (index < children.length) { | |
348 if (children [index].setFocus ()) return result; | |
349 } | |
350 control = control.parent; | |
351 } | |
352 } | |
353 return result; | |
354 } | |
355 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
356 override bool mnemonicMatch (wchar key) { |
64 | 357 if (labelHandle is null) return false; |
358 return mnemonicMatch (labelHandle, key); | |
359 } | |
360 | |
361 override void register () { | |
362 super.register (); | |
363 if (frameHandle !is null) display.addWidget (frameHandle, this); | |
364 if (labelHandle !is null) display.addWidget (labelHandle, this); | |
365 if (imageHandle !is null) display.addWidget (imageHandle, this); | |
366 } | |
367 | |
368 override void releaseHandle () { | |
369 super.releaseHandle (); | |
370 frameHandle = imageHandle = labelHandle = null; | |
371 } | |
372 | |
373 override void releaseWidget () { | |
374 super.releaseWidget (); | |
375 if (imageList !is null) imageList.dispose (); | |
376 imageList = null; | |
377 image = null; | |
378 text = null; | |
379 } | |
380 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
381 override void resizeHandle (int width, int height) { |
64 | 382 OS.gtk_widget_set_size_request (fixedHandle, width, height); |
383 OS.gtk_widget_set_size_request (frameHandle !is null ? frameHandle : handle, width, height); | |
384 } | |
385 | |
386 /** | |
387 * Controls how text and images will be displayed in the receiver. | |
388 * The argument should be one of <code>LEFT</code>, <code>RIGHT</code> | |
389 * or <code>CENTER</code>. If the receiver is a <code>SEPARATOR</code> | |
390 * label, the argument is ignored and the alignment is not changed. | |
391 * | |
392 * @param alignment the new alignment | |
393 * | |
71 | 394 * @exception DWTException <ul> |
64 | 395 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
396 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
397 * </ul> | |
398 */ | |
399 public void setAlignment (int alignment) { | |
400 checkWidget (); | |
71 | 401 if ((style & DWT.SEPARATOR) !is 0) return; |
402 if ((alignment & (DWT.LEFT | DWT.RIGHT | DWT.CENTER)) is 0) return; | |
403 style &= ~(DWT.LEFT | DWT.RIGHT | DWT.CENTER); | |
404 style |= alignment & (DWT.LEFT | DWT.RIGHT | DWT.CENTER); | |
405 bool isRTL = (style & DWT.RIGHT_TO_LEFT) !is 0; | |
406 if ((style & DWT.LEFT) !is 0) { | |
64 | 407 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0.0f, 0.0f); |
408 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, isRTL ? OS.GTK_JUSTIFY_RIGHT : OS.GTK_JUSTIFY_LEFT); | |
409 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 0.0f, 0.5f); | |
410 return; | |
411 } | |
71 | 412 if ((style & DWT.CENTER) !is 0) { |
64 | 413 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0.5f, 0.0f); |
414 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_CENTER); | |
415 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 0.5f, 0.5f); | |
416 return; | |
417 } | |
71 | 418 if ((style & DWT.RIGHT) !is 0) { |
64 | 419 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 1.0f, 0.0f); |
420 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, isRTL ? OS.GTK_JUSTIFY_LEFT : OS.GTK_JUSTIFY_RIGHT); | |
421 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 1.0f, 0.5f); | |
422 return; | |
423 } | |
424 } | |
425 | |
426 override void setBackgroundColor (GdkColor* color) { | |
427 super.setBackgroundColor (color); | |
428 setBackgroundColor(fixedHandle, color); | |
429 if (labelHandle !is null) setBackgroundColor(labelHandle, color); | |
430 if (imageHandle !is null) setBackgroundColor(imageHandle, color); | |
431 } | |
432 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
433 override int setBounds (int x, int y, int width, int height, bool move, bool resize) { |
64 | 434 /* |
435 * Bug in GTK. For some reason, when the label is | |
436 * wrappable and its container is resized, it does not | |
437 * cause the label to be wrapped. The fix is to | |
438 * determine the size that will wrap the label | |
439 * and expilictly set that size to force the label | |
440 * to wrap. | |
441 * | |
442 * This part of the fix causes the label to be | |
443 * resized to the preferred size but it still | |
444 * won't draw properly. | |
445 */ | |
71 | 446 bool fixWrap = resize && labelHandle !is null && (style & DWT.WRAP) !is 0; |
64 | 447 if (fixWrap) OS.gtk_widget_set_size_request (labelHandle, -1, -1); |
448 int result = super.setBounds (x, y, width, height, move, resize); | |
449 /* | |
450 * Bug in GTK. For some reason, when the label is | |
451 * wrappable and its container is resized, it does not | |
452 * cause the label to be wrapped. The fix is to | |
453 * determine the size that will wrap the label | |
454 * and expilictly set that size to force the label | |
455 * to wrap. | |
456 * | |
457 * This part of the fix forces the label to be | |
458 * resized so that it will draw wrapped. | |
459 */ | |
460 if (fixWrap) { | |
461 int labelWidth = OS.GTK_WIDGET_WIDTH (handle); | |
462 int labelHeight = OS.GTK_WIDGET_HEIGHT (handle); | |
463 OS.gtk_widget_set_size_request (labelHandle, labelWidth, labelHeight); | |
464 /* | |
465 * Bug in GTK. Setting the size request should invalidate the label's | |
466 * layout, but it does not. The fix is to resize the label directly. | |
467 */ | |
468 GtkRequisition requisition; | |
469 OS.gtk_widget_size_request (labelHandle, &requisition); | |
470 GtkAllocation allocation; | |
471 allocation.x = OS.GTK_WIDGET_X (labelHandle); | |
472 allocation.y = OS.GTK_WIDGET_Y (labelHandle); | |
473 allocation.width = labelWidth; | |
474 allocation.height = labelHeight; | |
475 OS.gtk_widget_size_allocate (labelHandle, &allocation); | |
476 } | |
477 return result; | |
478 } | |
479 | |
480 override void setFontDescription (PangoFontDescription* font) { | |
481 super.setFontDescription (font); | |
482 if (labelHandle !is null) OS.gtk_widget_modify_font (labelHandle, font); | |
483 if (imageHandle !is null) OS.gtk_widget_modify_font (imageHandle, font); | |
484 } | |
485 | |
486 override void setForegroundColor (GdkColor* color) { | |
487 super.setForegroundColor (color); | |
488 setForegroundColor (fixedHandle, color); | |
489 if (labelHandle !is null) setForegroundColor (labelHandle, color); | |
490 if (imageHandle !is null) setForegroundColor (imageHandle, color); | |
491 } | |
492 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
493 override void setOrientation () { |
64 | 494 super.setOrientation (); |
71 | 495 if ((style & DWT.RIGHT_TO_LEFT) !is 0) { |
64 | 496 if (labelHandle !is null) OS.gtk_widget_set_direction (labelHandle, OS.GTK_TEXT_DIR_RTL); |
497 if (imageHandle !is null) OS.gtk_widget_set_direction (imageHandle, OS.GTK_TEXT_DIR_RTL); | |
71 | 498 if ((style & DWT.LEAD) !is 0) { |
64 | 499 if (labelHandle !is null) OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_RIGHT); |
500 } | |
71 | 501 if ((style & DWT.TRAIL) !is 0) { |
64 | 502 if (labelHandle !is null) OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_LEFT); |
503 } | |
504 } | |
505 } | |
506 | |
507 /** | |
508 * Sets the receiver's image to the argument, which may be | |
509 * null indicating that no image should be displayed. | |
510 * | |
511 * @param image the image to display on the receiver (may be null) | |
512 * | |
513 * @exception IllegalArgumentException <ul> | |
514 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li> | |
515 * </ul> | |
71 | 516 * @exception DWTException <ul> |
64 | 517 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
518 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
519 * </ul> | |
520 */ | |
521 public void setImage (Image image) { | |
522 checkWidget (); | |
71 | 523 if ((style & DWT.SEPARATOR) !is 0) return; |
64 | 524 this.image = image; |
525 if (imageList !is null) imageList.dispose (); | |
526 imageList = null; | |
527 if (image !is null) { | |
528 imageList = new ImageList (); | |
529 int imageIndex = imageList.add (image); | |
530 auto pixbuf = imageList.getPixbuf (imageIndex); | |
531 OS.gtk_image_set_from_pixbuf (cast(GtkImage*)imageHandle, pixbuf); | |
532 OS.gtk_widget_hide (labelHandle); | |
533 OS.gtk_widget_show (imageHandle); | |
534 } else { | |
535 OS.gtk_image_set_from_pixbuf (cast(GtkImage*)imageHandle, null); | |
536 OS.gtk_widget_show (labelHandle); | |
537 OS.gtk_widget_hide (imageHandle); | |
538 } | |
539 } | |
540 | |
541 /** | |
542 * Sets the receiver's text. | |
543 * <p> | |
544 * This method sets the widget label. The label may include | |
545 * the mnemonic character and line delimiters. | |
546 * </p> | |
547 * <p> | |
548 * Mnemonics are indicated by an '&' that causes the next | |
549 * character to be the mnemonic. When the user presses a | |
550 * key sequence that matches the mnemonic, focus is assigned | |
551 * to the control that follows the label. On most platforms, | |
552 * the mnemonic appears underlined but may be emphasised in a | |
553 * platform specific manner. The mnemonic indicator character | |
554 * '&' can be escaped by doubling it in the string, causing | |
555 * a single '&' to be displayed. | |
556 * </p> | |
557 * | |
558 * @param string the new text | |
559 * | |
560 * @exception IllegalArgumentException <ul> | |
561 * <li>ERROR_NULL_ARGUMENT - if the text is null</li> | |
562 * </ul> | |
71 | 563 * @exception DWTException <ul> |
64 | 564 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
565 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
566 * </ul> | |
567 */ | |
568 public void setText (char[] string) { | |
569 checkWidget (); | |
207
12feeed18183
Allow Label.setText with null argument.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
570 // DWT extension: allow null for zero length string |
12feeed18183
Allow Label.setText with null argument.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
571 //if (string is null) error (DWT.ERROR_NULL_ARGUMENT); |
71 | 572 if ((style & DWT.SEPARATOR) !is 0) return; |
64 | 573 text = string; |
574 char [] chars = fixMnemonic (string); | |
207
12feeed18183
Allow Label.setText with null argument.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
575 OS.gtk_label_set_text_with_mnemonic (cast(GtkLabel*)labelHandle, chars.toStringzValidPtr()); |
64 | 576 OS.gtk_widget_hide (imageHandle); |
577 OS.gtk_widget_show (labelHandle); | |
578 } | |
579 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
580 override void showWidget () { |
64 | 581 super.showWidget (); |
582 if (frameHandle !is null) OS.gtk_widget_show (frameHandle); | |
583 if (labelHandle !is null) OS.gtk_widget_show (labelHandle); | |
584 } | |
585 } |