Mercurial > projects > dwt-linux
annotate dwt/widgets/Label.d @ 255:5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 15 Jun 2008 22:32:20 +0200 |
parents | ce446666f5a2 |
children | c0d810de7093 |
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; | |
238 | 60 String text; |
64 | 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; |
240 | 217 if ((style & DWT.WRAP) !is 0) { |
218 OS.gtk_label_set_line_wrap (labelHandle, true); | |
219 if (OS.GTK_VERSION >= OS.buildVERSION (2, 10, 0)) { | |
220 OS.gtk_label_set_line_wrap_mode (labelHandle, OS.PANGO_WRAP_WORD_CHAR); | |
221 } | |
222 } | |
71 | 223 if ((style & DWT.LEFT) !is 0) { |
64 | 224 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0.0f, 0.0f); |
225 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_LEFT); | |
226 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 0.0f, 0.5f); | |
227 return; | |
228 } | |
71 | 229 if ((style & DWT.CENTER) !is 0) { |
64 | 230 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0.5f, 0.0f); |
231 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_CENTER); | |
232 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 0.5f, 0.5f); | |
233 return; | |
234 } | |
71 | 235 if ((style & DWT.RIGHT) !is 0) { |
64 | 236 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 1.0f, 0.0f); |
237 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_RIGHT); | |
238 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 1.0f, 0.5f); | |
239 return; | |
240 } | |
241 } | |
242 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
243 override void createWidget (int index) { |
64 | 244 super.createWidget (index); |
245 text = ""; | |
246 } | |
247 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
248 override void deregister () { |
64 | 249 super.deregister (); |
250 if (frameHandle !is null) display.removeWidget (frameHandle); | |
251 if (labelHandle !is null) display.removeWidget (labelHandle); | |
252 if (imageHandle !is null) display.removeWidget (imageHandle); | |
253 } | |
254 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
255 override GtkWidget* eventHandle () { |
64 | 256 return fixedHandle; |
257 } | |
258 | |
259 /** | |
260 * Returns a value which describes the position of the | |
261 * text or image in the receiver. The value will be one of | |
262 * <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code> | |
263 * unless the receiver is a <code>SEPARATOR</code> label, in | |
264 * which case, <code>NONE</code> is returned. | |
265 * | |
266 * @return the alignment | |
267 * | |
71 | 268 * @exception DWTException <ul> |
64 | 269 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
270 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
271 * </ul> | |
272 */ | |
273 public int getAlignment () { | |
274 checkWidget (); | |
71 | 275 if ((style & DWT.SEPARATOR) !is 0) return 0; |
276 if ((style & DWT.LEFT) !is 0) return DWT.LEFT; | |
277 if ((style & DWT.CENTER) !is 0) return DWT.CENTER; | |
278 if ((style & DWT.RIGHT) !is 0) return DWT.RIGHT; | |
279 return DWT.LEFT; | |
64 | 280 } |
281 | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
105
diff
changeset
|
282 public override int getBorderWidth () { |
64 | 283 checkWidget(); |
284 if (frameHandle !is null) { | |
285 return OS.gtk_style_get_xthickness (OS.gtk_widget_get_style (frameHandle)); | |
286 } | |
287 return 0; | |
288 } | |
289 | |
290 /** | |
291 * Returns the receiver's image if it has one, or null | |
292 * if it does not. | |
293 * | |
294 * @return the receiver's image | |
295 * | |
71 | 296 * @exception DWTException <ul> |
64 | 297 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
298 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
299 * </ul> | |
300 */ | |
301 public Image getImage () { | |
302 checkWidget (); | |
303 return image; | |
304 } | |
305 | |
238 | 306 override String getNameText () { |
64 | 307 return getText (); |
308 } | |
309 | |
310 /** | |
311 * Returns the receiver's text, which will be an empty | |
312 * string if it has never been set or if the receiver is | |
313 * a <code>SEPARATOR</code> label. | |
314 * | |
315 * @return the receiver's text | |
316 * | |
71 | 317 * @exception DWTException <ul> |
64 | 318 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
319 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
320 * </ul> | |
321 */ | |
238 | 322 public String getText () { |
64 | 323 checkWidget (); |
71 | 324 if ((style & DWT.SEPARATOR) !is 0) return ""; |
64 | 325 return text; |
326 } | |
327 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
328 override void hookEvents () { |
64 | 329 super.hookEvents(); |
330 if (labelHandle !is null) { | |
331 OS.g_signal_connect_closure_by_id (labelHandle, display.signalIds [MNEMONIC_ACTIVATE], 0, display.closures [MNEMONIC_ACTIVATE], false); | |
332 } | |
333 } | |
334 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
335 override bool isDescribedByLabel () { |
64 | 336 return false; |
337 } | |
338 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
339 override bool mnemonicHit (wchar key) { |
64 | 340 if (labelHandle is null) return false; |
341 bool result = super.mnemonicHit (labelHandle, key); | |
342 if (result) { | |
343 Composite control = this.parent; | |
344 while (control !is null) { | |
345 Control [] children = control._getChildren (); | |
346 int index = 0; | |
347 while (index < children.length) { | |
348 if (children [index] is this) break; | |
349 index++; | |
350 } | |
351 index++; | |
352 if (index < children.length) { | |
353 if (children [index].setFocus ()) return result; | |
354 } | |
355 control = control.parent; | |
356 } | |
357 } | |
358 return result; | |
359 } | |
360 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
361 override bool mnemonicMatch (wchar key) { |
64 | 362 if (labelHandle is null) return false; |
363 return mnemonicMatch (labelHandle, key); | |
364 } | |
365 | |
366 override void register () { | |
367 super.register (); | |
368 if (frameHandle !is null) display.addWidget (frameHandle, this); | |
369 if (labelHandle !is null) display.addWidget (labelHandle, this); | |
370 if (imageHandle !is null) display.addWidget (imageHandle, this); | |
371 } | |
372 | |
373 override void releaseHandle () { | |
374 super.releaseHandle (); | |
375 frameHandle = imageHandle = labelHandle = null; | |
376 } | |
377 | |
378 override void releaseWidget () { | |
379 super.releaseWidget (); | |
380 if (imageList !is null) imageList.dispose (); | |
381 imageList = null; | |
382 image = null; | |
383 text = null; | |
384 } | |
385 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
386 override void resizeHandle (int width, int height) { |
64 | 387 OS.gtk_widget_set_size_request (fixedHandle, width, height); |
388 OS.gtk_widget_set_size_request (frameHandle !is null ? frameHandle : handle, width, height); | |
389 } | |
390 | |
391 /** | |
392 * Controls how text and images will be displayed in the receiver. | |
393 * The argument should be one of <code>LEFT</code>, <code>RIGHT</code> | |
394 * or <code>CENTER</code>. If the receiver is a <code>SEPARATOR</code> | |
395 * label, the argument is ignored and the alignment is not changed. | |
396 * | |
397 * @param alignment the new alignment | |
398 * | |
71 | 399 * @exception DWTException <ul> |
64 | 400 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
401 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
402 * </ul> | |
403 */ | |
404 public void setAlignment (int alignment) { | |
405 checkWidget (); | |
71 | 406 if ((style & DWT.SEPARATOR) !is 0) return; |
407 if ((alignment & (DWT.LEFT | DWT.RIGHT | DWT.CENTER)) is 0) return; | |
408 style &= ~(DWT.LEFT | DWT.RIGHT | DWT.CENTER); | |
409 style |= alignment & (DWT.LEFT | DWT.RIGHT | DWT.CENTER); | |
410 bool isRTL = (style & DWT.RIGHT_TO_LEFT) !is 0; | |
411 if ((style & DWT.LEFT) !is 0) { | |
64 | 412 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0.0f, 0.0f); |
413 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, isRTL ? OS.GTK_JUSTIFY_RIGHT : OS.GTK_JUSTIFY_LEFT); | |
414 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 0.0f, 0.5f); | |
415 return; | |
416 } | |
71 | 417 if ((style & DWT.CENTER) !is 0) { |
64 | 418 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0.5f, 0.0f); |
419 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_CENTER); | |
420 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 0.5f, 0.5f); | |
421 return; | |
422 } | |
71 | 423 if ((style & DWT.RIGHT) !is 0) { |
64 | 424 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 1.0f, 0.0f); |
425 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, isRTL ? OS.GTK_JUSTIFY_LEFT : OS.GTK_JUSTIFY_RIGHT); | |
426 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 1.0f, 0.5f); | |
427 return; | |
428 } | |
429 } | |
430 | |
431 override void setBackgroundColor (GdkColor* color) { | |
432 super.setBackgroundColor (color); | |
433 setBackgroundColor(fixedHandle, color); | |
434 if (labelHandle !is null) setBackgroundColor(labelHandle, color); | |
435 if (imageHandle !is null) setBackgroundColor(imageHandle, color); | |
436 } | |
437 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
438 override int setBounds (int x, int y, int width, int height, bool move, bool resize) { |
64 | 439 /* |
440 * Bug in GTK. For some reason, when the label is | |
441 * wrappable and its container is resized, it does not | |
442 * cause the label to be wrapped. The fix is to | |
443 * determine the size that will wrap the label | |
444 * and expilictly set that size to force the label | |
445 * to wrap. | |
446 * | |
447 * This part of the fix causes the label to be | |
448 * resized to the preferred size but it still | |
449 * won't draw properly. | |
450 */ | |
71 | 451 bool fixWrap = resize && labelHandle !is null && (style & DWT.WRAP) !is 0; |
64 | 452 if (fixWrap) OS.gtk_widget_set_size_request (labelHandle, -1, -1); |
453 int result = super.setBounds (x, y, width, height, move, resize); | |
454 /* | |
455 * Bug in GTK. For some reason, when the label is | |
456 * wrappable and its container is resized, it does not | |
457 * cause the label to be wrapped. The fix is to | |
458 * determine the size that will wrap the label | |
459 * and expilictly set that size to force the label | |
460 * to wrap. | |
461 * | |
462 * This part of the fix forces the label to be | |
463 * resized so that it will draw wrapped. | |
464 */ | |
465 if (fixWrap) { | |
466 int labelWidth = OS.GTK_WIDGET_WIDTH (handle); | |
467 int labelHeight = OS.GTK_WIDGET_HEIGHT (handle); | |
468 OS.gtk_widget_set_size_request (labelHandle, labelWidth, labelHeight); | |
469 /* | |
470 * Bug in GTK. Setting the size request should invalidate the label's | |
471 * layout, but it does not. The fix is to resize the label directly. | |
472 */ | |
473 GtkRequisition requisition; | |
474 OS.gtk_widget_size_request (labelHandle, &requisition); | |
475 GtkAllocation allocation; | |
476 allocation.x = OS.GTK_WIDGET_X (labelHandle); | |
477 allocation.y = OS.GTK_WIDGET_Y (labelHandle); | |
478 allocation.width = labelWidth; | |
479 allocation.height = labelHeight; | |
480 OS.gtk_widget_size_allocate (labelHandle, &allocation); | |
481 } | |
482 return result; | |
483 } | |
484 | |
485 override void setFontDescription (PangoFontDescription* font) { | |
486 super.setFontDescription (font); | |
487 if (labelHandle !is null) OS.gtk_widget_modify_font (labelHandle, font); | |
488 if (imageHandle !is null) OS.gtk_widget_modify_font (imageHandle, font); | |
489 } | |
490 | |
491 override void setForegroundColor (GdkColor* color) { | |
492 super.setForegroundColor (color); | |
493 setForegroundColor (fixedHandle, color); | |
494 if (labelHandle !is null) setForegroundColor (labelHandle, color); | |
495 if (imageHandle !is null) setForegroundColor (imageHandle, color); | |
496 } | |
497 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
498 override void setOrientation () { |
64 | 499 super.setOrientation (); |
71 | 500 if ((style & DWT.RIGHT_TO_LEFT) !is 0) { |
64 | 501 if (labelHandle !is null) OS.gtk_widget_set_direction (labelHandle, OS.GTK_TEXT_DIR_RTL); |
502 if (imageHandle !is null) OS.gtk_widget_set_direction (imageHandle, OS.GTK_TEXT_DIR_RTL); | |
71 | 503 if ((style & DWT.LEAD) !is 0) { |
64 | 504 if (labelHandle !is null) OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_RIGHT); |
505 } | |
71 | 506 if ((style & DWT.TRAIL) !is 0) { |
64 | 507 if (labelHandle !is null) OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_LEFT); |
508 } | |
509 } | |
510 } | |
511 | |
512 /** | |
513 * Sets the receiver's image to the argument, which may be | |
514 * null indicating that no image should be displayed. | |
515 * | |
516 * @param image the image to display on the receiver (may be null) | |
517 * | |
518 * @exception IllegalArgumentException <ul> | |
519 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li> | |
520 * </ul> | |
71 | 521 * @exception DWTException <ul> |
64 | 522 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
523 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
524 * </ul> | |
525 */ | |
526 public void setImage (Image image) { | |
527 checkWidget (); | |
71 | 528 if ((style & DWT.SEPARATOR) !is 0) return; |
64 | 529 this.image = image; |
530 if (imageList !is null) imageList.dispose (); | |
531 imageList = null; | |
532 if (image !is null) { | |
533 imageList = new ImageList (); | |
534 int imageIndex = imageList.add (image); | |
535 auto pixbuf = imageList.getPixbuf (imageIndex); | |
536 OS.gtk_image_set_from_pixbuf (cast(GtkImage*)imageHandle, pixbuf); | |
537 OS.gtk_widget_hide (labelHandle); | |
538 OS.gtk_widget_show (imageHandle); | |
539 } else { | |
540 OS.gtk_image_set_from_pixbuf (cast(GtkImage*)imageHandle, null); | |
541 OS.gtk_widget_show (labelHandle); | |
542 OS.gtk_widget_hide (imageHandle); | |
543 } | |
544 } | |
545 | |
546 /** | |
547 * Sets the receiver's text. | |
548 * <p> | |
549 * This method sets the widget label. The label may include | |
550 * the mnemonic character and line delimiters. | |
551 * </p> | |
552 * <p> | |
553 * Mnemonics are indicated by an '&' that causes the next | |
554 * character to be the mnemonic. When the user presses a | |
555 * key sequence that matches the mnemonic, focus is assigned | |
556 * to the control that follows the label. On most platforms, | |
557 * the mnemonic appears underlined but may be emphasised in a | |
558 * platform specific manner. The mnemonic indicator character | |
559 * '&' can be escaped by doubling it in the string, causing | |
560 * a single '&' to be displayed. | |
561 * </p> | |
562 * | |
563 * @param string the new text | |
564 * | |
71 | 565 * @exception DWTException <ul> |
64 | 566 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
567 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
568 * </ul> | |
569 */ | |
238 | 570 public void setText (String string) { |
64 | 571 checkWidget (); |
207
12feeed18183
Allow Label.setText with null argument.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
572 // DWT extension: allow null for zero length string |
12feeed18183
Allow Label.setText with null argument.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
573 //if (string is null) error (DWT.ERROR_NULL_ARGUMENT); |
71 | 574 if ((style & DWT.SEPARATOR) !is 0) return; |
64 | 575 text = string; |
576 char [] chars = fixMnemonic (string); | |
207
12feeed18183
Allow Label.setText with null argument.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
577 OS.gtk_label_set_text_with_mnemonic (cast(GtkLabel*)labelHandle, chars.toStringzValidPtr()); |
64 | 578 OS.gtk_widget_hide (imageHandle); |
579 OS.gtk_widget_show (labelHandle); | |
580 } | |
581 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
582 override void showWidget () { |
64 | 583 super.showWidget (); |
584 if (frameHandle !is null) OS.gtk_widget_show (frameHandle); | |
585 if (labelHandle !is null) OS.gtk_widget_show (labelHandle); | |
586 } | |
587 } |