Mercurial > projects > dwt-linux
annotate dwt/widgets/Group.d @ 248:34409a2fc053
Fix call OS.gtk_label_set_text_with_mnemonic with valid ptr, even for zero length strings.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 19:00:47 +0200 |
parents | 380bad9f6852 |
children | 5a30aa9820f3 |
rev | line source |
---|---|
72 | 1 /******************************************************************************* |
65 | 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> | |
65 | 12 *******************************************************************************/ |
13 module dwt.widgets.Group; | |
14 | |
238 | 15 import dwt.dwthelper.utils; |
16 | |
65 | 17 |
71 | 18 import dwt.DWT; |
65 | 19 import dwt.internal.gtk.OS; |
20 import dwt.graphics.Point; | |
21 import dwt.graphics.Rectangle; | |
22 import dwt.widgets.Composite; | |
23 | |
24 import Math = tango.math.Math; | |
25 import tango.stdc.stringz; | |
26 | |
27 /** | |
28 * Instances of this class provide an etched border | |
29 * with an optional title. | |
30 * <p> | |
31 * Shadow styles are hints and may not be honoured | |
32 * by the platform. To create a group with the | |
33 * default shadow style for the platform, do not | |
34 * specify a shadow style. | |
35 * <dl> | |
36 * <dt><b>Styles:</b></dt> | |
37 * <dd>SHADOW_ETCHED_IN, SHADOW_ETCHED_OUT, SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd> | |
38 * <dt><b>Events:</b></dt> | |
39 * <dd>(none)</dd> | |
40 * </dl> | |
41 * <p> | |
42 * Note: Only one of the above styles may be specified. | |
43 * </p><p> | |
44 * IMPORTANT: This class is <em>not</em> intended to be subclassed. | |
45 * </p> | |
46 */ | |
47 public class Group : Composite { | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
72
diff
changeset
|
48 |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
72
diff
changeset
|
49 alias Composite.computeSize computeSize; |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
72
diff
changeset
|
50 alias Composite.createHandle createHandle; |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
72
diff
changeset
|
51 alias Composite.mnemonicHit mnemonicHit; |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
72
diff
changeset
|
52 alias Composite.mnemonicMatch mnemonicMatch; |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
72
diff
changeset
|
53 alias Composite.setBackgroundColor setBackgroundColor; |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
72
diff
changeset
|
54 alias Composite.setForegroundColor setForegroundColor; |
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
72
diff
changeset
|
55 |
65 | 56 GtkWidget* clientHandle_, labelHandle; |
238 | 57 String text = ""; |
65 | 58 |
59 /** | |
60 * Constructs a new instance of this class given its parent | |
61 * and a style value describing its behavior and appearance. | |
62 * <p> | |
63 * The style value is either one of the style constants defined in | |
71 | 64 * class <code>DWT</code> which is applicable to instances of this |
65 | 65 * class, or must be built by <em>bitwise OR</em>'ing together |
66 * (that is, using the <code>int</code> "|" operator) two or more | |
71 | 67 * of those <code>DWT</code> style constants. The class description |
65 | 68 * lists the style constants that are applicable to the class. |
69 * Style bits are also inherited from superclasses. | |
70 * </p> | |
71 * | |
72 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
73 * @param style the style of control to construct | |
74 * | |
75 * @exception IllegalArgumentException <ul> | |
76 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
77 * </ul> | |
71 | 78 * @exception DWTException <ul> |
65 | 79 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> |
80 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
81 * </ul> | |
82 * | |
71 | 83 * @see DWT#SHADOW_ETCHED_IN |
84 * @see DWT#SHADOW_ETCHED_OUT | |
85 * @see DWT#SHADOW_IN | |
86 * @see DWT#SHADOW_OUT | |
87 * @see DWT#SHADOW_NONE | |
65 | 88 * @see Widget#checkSubclass |
89 * @see Widget#getStyle | |
90 */ | |
91 public this (Composite parent, int style) { | |
92 super (parent, checkStyle (style)); | |
93 } | |
94 | |
95 static int checkStyle (int style) { | |
71 | 96 style |= DWT.NO_FOCUS; |
65 | 97 /* |
98 * Even though it is legal to create this widget | |
99 * with scroll bars, they serve no useful purpose | |
100 * because they do not automatically scroll the | |
101 * widget's client area. The fix is to clear | |
71 | 102 * the DWT style. |
65 | 103 */ |
71 | 104 return style & ~(DWT.H_SCROLL | DWT.V_SCROLL); |
65 | 105 } |
106 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
107 protected override void checkSubclass () { |
71 | 108 if (!isValidSubclass ()) error (DWT.ERROR_INVALID_SUBCLASS); |
65 | 109 } |
110 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
111 override GtkWidget* clientHandle () { |
65 | 112 return clientHandle_; |
113 } | |
114 | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
72
diff
changeset
|
115 public override Point computeSize (int wHint, int hHint, bool changed) { |
65 | 116 Point size = super.computeSize(wHint, hHint, changed); |
71 | 117 int width = computeNativeSize (handle, DWT.DEFAULT, DWT.DEFAULT, false).x; |
65 | 118 size.x = Math.max (size.x, width); |
119 return size; | |
120 } | |
150
f2e04420fd6c
reworked overrides and superclass aliases
Frank Benoit <benoit@tionex.de>
parents:
72
diff
changeset
|
121 public override Rectangle computeTrim (int x, int y, int width, int height) { |
65 | 122 checkWidget(); |
123 forceResize (); | |
124 int clientX = OS.GTK_WIDGET_X (clientHandle_); | |
125 int clientY = OS.GTK_WIDGET_Y (clientHandle_); | |
126 x -= clientX; | |
127 y -= clientY; | |
128 width += clientX + clientX; | |
129 height += clientX + clientY; | |
130 return new Rectangle (x, y, width, height); | |
131 } | |
132 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
133 override void createHandle(int index) { |
65 | 134 state |= HANDLE | THEME_BACKGROUND; |
135 fixedHandle = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null); | |
71 | 136 if (fixedHandle is null) error (DWT.ERROR_NO_HANDLES); |
65 | 137 OS.gtk_fixed_set_has_window (cast(GtkFixed*)fixedHandle, true); |
138 handle = OS.gtk_frame_new (null); | |
71 | 139 if (handle is null) error (DWT.ERROR_NO_HANDLES); |
65 | 140 labelHandle = cast(GtkWidget*)OS.gtk_label_new (null); |
71 | 141 if (labelHandle is null) error (DWT.ERROR_NO_HANDLES); |
65 | 142 OS.g_object_ref (labelHandle); |
143 OS.gtk_object_sink (cast(GtkObject*)labelHandle); | |
144 clientHandle_ = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null); | |
71 | 145 if (clientHandle_ is null) error (DWT.ERROR_NO_HANDLES); |
65 | 146 OS.gtk_container_add (cast(GtkContainer*)fixedHandle, handle); |
147 OS.gtk_container_add (cast(GtkContainer*)handle, clientHandle_); | |
71 | 148 if ((style & DWT.SHADOW_IN) !is 0) { |
65 | 149 OS.gtk_frame_set_shadow_type (cast(GtkFrame*)handle, OS.GTK_SHADOW_IN); |
150 } | |
71 | 151 if ((style & DWT.SHADOW_OUT) !is 0) { |
65 | 152 OS.gtk_frame_set_shadow_type (cast(GtkFrame*)handle, OS.GTK_SHADOW_OUT); |
153 } | |
71 | 154 if ((style & DWT.SHADOW_ETCHED_IN) !is 0) { |
65 | 155 OS.gtk_frame_set_shadow_type (cast(GtkFrame*)handle, OS.GTK_SHADOW_ETCHED_IN); |
156 } | |
71 | 157 if ((style & DWT.SHADOW_ETCHED_OUT) !is 0) { |
65 | 158 OS.gtk_frame_set_shadow_type (cast(GtkFrame*)handle, OS.GTK_SHADOW_ETCHED_OUT); |
159 } | |
160 } | |
161 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
162 override void deregister () { |
65 | 163 super.deregister (); |
164 display.removeWidget (clientHandle_); | |
165 display.removeWidget (labelHandle); | |
166 } | |
167 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
168 override void enableWidget (bool enabled) { |
65 | 169 OS.gtk_widget_set_sensitive (labelHandle, enabled); |
170 } | |
171 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
172 override GtkWidget* eventHandle () { |
65 | 173 return fixedHandle; |
174 } | |
175 | |
238 | 176 override String getNameText () { |
65 | 177 return getText (); |
178 } | |
179 | |
180 /** | |
181 * Returns the receiver's text, which is the string that the | |
182 * is used as the <em>title</em>. If the text has not previously | |
183 * been set, returns an empty string. | |
184 * | |
185 * @return the text | |
186 * | |
71 | 187 * @exception DWTException <ul> |
65 | 188 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
189 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
190 * </ul> | |
191 */ | |
238 | 192 public String getText () { |
65 | 193 checkWidget(); |
194 return text; | |
195 } | |
196 | |
197 override void hookEvents () { | |
198 super.hookEvents(); | |
199 if (labelHandle !is null) { | |
200 OS.g_signal_connect_closure_by_id (labelHandle, display.signalIds [MNEMONIC_ACTIVATE], 0, display.closures [MNEMONIC_ACTIVATE], false); | |
201 } | |
202 } | |
203 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
204 override bool mnemonicHit (wchar key) { |
65 | 205 if (labelHandle is null) return false; |
206 bool result = super.mnemonicHit (labelHandle, key); | |
207 if (result) setFocus (); | |
208 return result; | |
209 } | |
210 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
211 override bool mnemonicMatch (wchar key) { |
65 | 212 if (labelHandle is null) return false; |
213 return mnemonicMatch (labelHandle, key); | |
214 } | |
215 | |
216 override GtkWidget* parentingHandle() { | |
217 return fixedHandle; | |
218 } | |
219 | |
220 override void register () { | |
221 super.register (); | |
222 display.addWidget (clientHandle_, this); | |
223 display.addWidget (labelHandle, this); | |
224 } | |
225 | |
226 override void releaseHandle () { | |
227 super.releaseHandle (); | |
228 clientHandle_ = labelHandle = null; | |
229 } | |
230 | |
231 override void releaseWidget () { | |
232 super.releaseWidget (); | |
233 if (labelHandle !is null) OS.g_object_unref (labelHandle); | |
234 text = null; | |
235 } | |
236 | |
237 override void setBackgroundColor (GdkColor* color) { | |
238 super.setBackgroundColor (color); | |
239 setBackgroundColor(fixedHandle, color); | |
240 } | |
241 | |
242 override void setFontDescription (PangoFontDescription* font) { | |
243 super.setFontDescription (font); | |
244 OS.gtk_widget_modify_font (labelHandle, font); | |
245 } | |
246 | |
247 override void setForegroundColor (GdkColor* color) { | |
248 super.setForegroundColor (color); | |
249 setForegroundColor (labelHandle, color); | |
250 } | |
251 | |
252 override void setOrientation () { | |
253 super.setOrientation (); | |
71 | 254 if ((style & DWT.RIGHT_TO_LEFT) !is 0) { |
65 | 255 OS.gtk_widget_set_direction (labelHandle, OS.GTK_TEXT_DIR_RTL); |
256 } | |
257 } | |
258 | |
259 /** | |
260 * Sets the receiver's text, which is the string that will | |
261 * be displayed as the receiver's <em>title</em>, to the argument, | |
262 * which may not be null. The string may include the mnemonic character. | |
263 * </p> | |
264 * Mnemonics are indicated by an '&' that causes the next | |
265 * character to be the mnemonic. When the user presses a | |
266 * key sequence that matches the mnemonic, focus is assigned | |
267 * to the first child of the group. On most platforms, the | |
268 * mnemonic appears underlined but may be emphasised in a | |
269 * platform specific manner. The mnemonic indicator character | |
270 * '&' can be escaped by doubling it in the string, causing | |
271 * a single '&' to be displayed. | |
272 * </p> | |
273 * @param string the new text | |
274 * | |
275 * @exception IllegalArgumentException <ul> | |
276 * <li>ERROR_NULL_ARGUMENT - if the text is null</li> | |
277 * </ul> | |
71 | 278 * @exception DWTException <ul> |
65 | 279 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
280 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
281 * </ul> | |
282 */ | |
238 | 283 public void setText (String string) { |
65 | 284 checkWidget(); |
71 | 285 if (string is null) error (DWT.ERROR_NULL_ARGUMENT); |
65 | 286 text = string; |
287 char [] chars = fixMnemonic (string); | |
248
34409a2fc053
Fix call OS.gtk_label_set_text_with_mnemonic with valid ptr, even for zero length strings.
Frank Benoit <benoit@tionex.de>
parents:
238
diff
changeset
|
288 OS.gtk_label_set_text_with_mnemonic (cast(GtkLabel*)labelHandle, chars.toStringzValidPtr()); |
65 | 289 if (string.length !is 0) { |
290 if (OS.gtk_frame_get_label_widget (cast(GtkFrame*)handle) is null) { | |
291 OS.gtk_frame_set_label_widget (cast(GtkFrame*)handle, labelHandle); | |
292 } | |
293 } else { | |
294 OS.gtk_frame_set_label_widget (cast(GtkFrame*)handle, null); | |
295 } | |
296 } | |
297 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
298 override void showWidget () { |
65 | 299 super.showWidget (); |
300 if (clientHandle_ !is null) OS.gtk_widget_show (clientHandle_); | |
301 if (labelHandle !is null) OS.gtk_widget_show (labelHandle); | |
302 } | |
303 } |