comparison dwt/widgets/ToolTip.d @ 47:f646579f309c

Tray, Tooltip, TrayItem, Item
author Frank Benoit <benoit@tionex.de>
date Fri, 11 Jan 2008 08:34:26 +0100
parents
children 93981635e709
comparison
equal deleted inserted replaced
46:8015c460f713 47:f646579f309c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 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
10 *******************************************************************************/
11 module dwt.widgets.ToolTip;
12
13 import dwt.widgets.Widget;
14 import dwt.widgets.TrayItem;
15 import dwt.widgets.Shell;
16 import dwt.SWT;
17 //import dwt.internal.*;
18 import dwt.internal.gtk.OS;
19 import dwt.graphics.Point;
20 import dwt.graphics.Color;
21 import dwt.events.SelectionListener;
22 import dwt.events.SelectionEvent;
23 import dwt.widgets.TypedListener;
24 import dwt.widgets.Event;
25 import dwt.widgets.Display;
26
27 import tango.stdc.stringz;
28
29 /**
30 * Instances of this class represent popup windows that are used
31 * to inform or warn the user.
32 * <p>
33 * <dl>
34 * <dt><b>Styles:</b></dt>
35 * <dd>BALLOON, ICON_ERROR, ICON_INFORMATION, ICON_WARNING</dd>
36 * <dt><b>Events:</b></dt>
37 * <dd>Selection</dd>
38 * </dl>
39 * </p><p>
40 * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION,
41 * and ICON_WARNING may be specified.
42 * </p><p>
43 * IMPORTANT: This class is intended to be subclassed <em>only</em>
44 * within the SWT implementation.
45 * </p>
46 *
47 * @since 3.2
48 */
49 public class ToolTip : Widget {
50 Shell parent;
51 char[] text, message;
52 TrayItem item;
53 int x, y, timerId;
54 void* layoutText, layoutMessage;
55 int [] borderPolygon;
56 bool spikeAbove, autohide;
57 CallbackData timerProcCallbackData;
58
59 static final int BORDER = 5;
60 static final int PADDING = 5;
61 static final int INSET = 4;
62 static final int TIP_HEIGHT = 20;
63 static final int IMAGE_SIZE = 16;
64 static final int DELAY = 8000;
65
66 /**
67 * Constructs a new instance of this class given its parent
68 * and a style value describing its behavior and appearance.
69 * <p>
70 * The style value is either one of the style constants defined in
71 * class <code>SWT</code> which is applicable to instances of this
72 * class, or must be built by <em>bitwise OR</em>'ing together
73 * (that is, using the <code>int</code> "|" operator) two or more
74 * of those <code>SWT</code> style constants. The class description
75 * lists the style constants that are applicable to the class.
76 * Style bits are also inherited from superclasses.
77 * </p>
78 *
79 * @param parent a composite control which will be the parent of the new instance (cannot be null)
80 * @param style the style of control to construct
81 *
82 * @exception IllegalArgumentException <ul>
83 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
84 * </ul>
85 * @exception SWTException <ul>
86 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
87 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
88 * </ul>
89 *
90 * @see SWT#ICON_ERROR
91 * @see SWT#ICON_INFORMATION
92 * @see SWT#ICON_WARNING
93 * @see Widget#checkSubclass
94 * @see Widget#getStyle
95 */
96 public this (Shell parent, int style) {
97 super (parent, checkStyle (style));
98 this.parent = parent;
99 createWidget (0);
100 }
101
102 static int checkStyle (int style) {
103 int mask = SWT.ICON_ERROR | SWT.ICON_INFORMATION | SWT.ICON_WARNING;
104 if ((style & mask) is 0) return style;
105 return checkBits (style, SWT.ICON_INFORMATION, SWT.ICON_WARNING, SWT.ICON_ERROR, 0, 0, 0);
106 }
107
108 /**
109 * Adds the listener to the collection of listeners who will
110 * be notified when the receiver is selected by the user, by sending
111 * it one of the messages defined in the <code>SelectionListener</code>
112 * interface.
113 * <p>
114 * <code>widgetSelected</code> is called when the receiver is selected.
115 * <code>widgetDefaultSelected</code> is not called.
116 * </p>
117 *
118 * @param listener the listener which should be notified when the receiver is selected by the user
119 *
120 * @exception IllegalArgumentException <ul>
121 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
122 * </ul>
123 * @exception SWTException <ul>
124 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
125 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
126 * </ul>
127 *
128 * @see SelectionListener
129 * @see #removeSelectionListener
130 * @see SelectionEvent
131 */
132 public void addSelectionListener (SelectionListener listener) {
133 checkWidget ();
134 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
135 TypedListener typedListener = new TypedListener (listener);
136 addListener (SWT.Selection,typedListener);
137 addListener (SWT.DefaultSelection,typedListener);
138 }
139
140 void configure () {
141 auto screen = OS.gdk_screen_get_default ();
142 OS.gtk_widget_realize (handle);
143 int monitorNumber = OS.gdk_screen_get_monitor_at_window (screen, OS.GTK_WIDGET_WINDOW (handle));
144 GdkRectangle* dest = new GdkRectangle ();
145 OS.gdk_screen_get_monitor_geometry (screen, monitorNumber, dest);
146 Point point = getSize (dest.width / 4);
147 int w = point.x;
148 int h = point.y;
149 point = getLocation ();
150 int x = point.x;
151 int y = point.y;
152 OS.gtk_window_resize (cast(GtkWindow*)handle, w, h + TIP_HEIGHT);
153 int[] polyline;
154 spikeAbove = dest.height >= y + h + TIP_HEIGHT;
155 if (dest.width >= x + w) {
156 if (dest.height >= y + h + TIP_HEIGHT) {
157 int t = TIP_HEIGHT;
158 polyline = [
159 0, 5+t, 1, 5+t, 1, 3+t, 3, 1+t, 5, 1+t, 5, t,
160 16, t, 16, 0, 35, t,
161 w-5, t, w-5, 1+t, w-3, 1+t, w-1, 3+t, w-1, 5+t, w, 5+t,
162 w, h-5+t, w-1, h-5+t, w-1, h-3+t, w-2, h-3+t, w-2, h-2+t, w-3, h-2+t, w-3, h-1+t, w-5, h-1+t, w-5, h+t,
163 5, h+t, 5, h-1+t, 3, h-1+t, 3, h-2+t, 2, h-2+t, 2, h-3+t, 1, h-3+t, 1, h-5+t, 0, h-5+t,
164 0, 5+t];
165 borderPolygon = [
166 0, 5+t, 1, 4+t, 1, 3+t, 3, 1+t, 4, 1+t, 5, t,
167 16, t, 16, 1, 35, t,
168 w-6, 0+t, w-5, 1+t, w-4, 1+t, w-2, 3+t, w-2, 4+t, w-1, 5+t,
169 w-1, h-6+t, w-2, h-5+t, w-2, h-4+t, w-4, h-2+t, w-5, h-2+t, w-6, h-1+t,
170 5, h-1+t, 4, h-2+t, 3, h-2+t, 1, h-4+t, 1, h-5+t, 0, h-6+t,
171 0, 5+t];
172 OS.gtk_window_move (cast(GtkWindow*)handle, Math.max(0, x - 17), y);
173 } else {
174 polyline = [
175 0, 5, 1, 5, 1, 3, 3, 1, 5, 1, 5, 0,
176 w-5, 0, w-5, 1, w-3, 1, w-1, 3, w-1, 5, w, 5,
177 w, h-5, w-1, h-5, w-1, h-3, w-2, h-3, w-2, h-2, w-3, h-2, w-3, h-1, w-5, h-1, w-5, h,
178 35, h, 16, h+TIP_HEIGHT, 16, h,
179 5, h, 5, h-1, 3, h-1, 3, h-2, 2, h-2, 2, h-3, 1, h-3, 1, h-5, 0, h-5,
180 0, 5];
181 borderPolygon = [
182 0, 5, 1, 4, 1, 3, 3, 1, 4, 1, 5, 0,
183 w-6, 0, w-5, 1, w-4, 1, w-2, 3, w-2, 4, w-1, 5,
184 w-1, h-6, w-2, h-5, w-2, h-4, w-4, h-2, w-5, h-2, w-6, h-1,
185 35, h-1, 17, h+TIP_HEIGHT-2, 17, h-1,
186 5, h-1, 4, h-2, 3, h-2, 1, h-4, 1, h-5, 0, h-6,
187 0, 5];
188 OS.gtk_window_move (cast(GtkWindow*)handle, Math.max(0, x - 17), y - h - TIP_HEIGHT);
189 }
190 } else {
191 if (dest.height >= y + h + TIP_HEIGHT) {
192 int t = TIP_HEIGHT;
193 polyline = [
194 0, 5+t, 1, 5+t, 1, 3+t, 3, 1+t, 5, 1+t, 5, t,
195 w-35, t, w-16, 0, w-16, t,
196 w-5, t, w-5, 1+t, w-3, 1+t, w-1, 3+t, w-1, 5+t, w, 5+t,
197 w, h-5+t, w-1, h-5+t, w-1, h-3+t, w-2, h-3+t, w-2, h-2+t, w-3, h-2+t, w-3, h-1+t, w-5, h-1+t, w-5, h+t,
198 5, h+t, 5, h-1+t, 3, h-1+t, 3, h-2+t, 2, h-2+t, 2, h-3+t, 1, h-3+t, 1, h-5+t, 0, h-5+t,
199 0, 5+t];
200 borderPolygon = [
201 0, 5+t, 1, 4+t, 1, 3+t, 3, 1+t, 4, 1+t, 5, t,
202 w-35, t, w-17, 2, w-17, t,
203 w-6, t, w-5, 1+t, w-4, 1+t, w-2, 3+t, w-2, 4+t, w-1, 5+t,
204 w-1, h-6+t, w-2, h-5+t, w-2, h-4+t, w-4, h-2+t, w-5, h-2+t, w-6, h-1+t,
205 5, h-1+t, 4, h-2+t, 3, h-2+t, 1, h-4+t, 1, h-5+t, 0, h-6+t,
206 0, 5+t];
207 OS.gtk_window_move (cast(GtkWindow*)handle, Math.min(dest.width - w, x - w + 17), y);
208 } else {
209 polyline = [
210 0, 5, 1, 5, 1, 3, 3, 1, 5, 1, 5, 0,
211 w-5, 0, w-5, 1, w-3, 1, w-1, 3, w-1, 5, w, 5,
212 w, h-5, w-1, h-5, w-1, h-3, w-2, h-3, w-2, h-2, w-3, h-2, w-3, h-1, w-5, h-1, w-5, h,
213 w-16, h, w-16, h+TIP_HEIGHT, w-35, h,
214 5, h, 5, h-1, 3, h-1, 3, h-2, 2, h-2, 2, h-3, 1, h-3, 1, h-5, 0, h-5,
215 0, 5];
216 borderPolygon = [
217 0, 5, 1, 4, 1, 3, 3, 1, 4, 1, 5, 0,
218 w-6, 0, w-5, 1, w-4, 1, w-2, 3, w-2, 4, w-1, 5,
219 w-1, h-6, w-2, h-5, w-2, h-4, w-4, h-2, w-5, h-2, w-6, h-1,
220 w-17, h-1, w-17, h+TIP_HEIGHT-2, w-36, h-1,
221 5, h-1, 4, h-2, 3, h-2, 1, h-4, 1, h-5, 0, h-6,
222 0, 5];
223 OS.gtk_window_move (cast(GtkWindow*)handle, Math.min(dest.width - w, x - w + 17), y - h - TIP_HEIGHT);
224 }
225 }
226 auto rgn = OS.gdk_region_polygon ( cast(GdkPoint*)polyline.ptr, polyline.length / 2, OS.GDK_EVEN_ODD_RULE);
227 OS.gtk_widget_realize (handle);
228 auto window = OS.GTK_WIDGET_WINDOW (handle);
229 OS.gdk_window_shape_combine_region (window, rgn, 0, 0);
230 OS.gdk_region_destroy (rgn);
231 }
232
233 void createHandle (int index) {
234 state |= HANDLE;
235 if ((style & SWT.BALLOON) !is 0) {
236 handle = OS.gtk_window_new (OS.GTK_WINDOW_POPUP);
237 Color background = display.getSystemColor (SWT.COLOR_INFO_BACKGROUND);
238 OS.gtk_widget_modify_bg (handle, OS.GTK_STATE_NORMAL, background.handle);
239 OS.gtk_widget_set_app_paintable (handle, true);
240 } else {
241 handle = cast(GtkWidget*)OS.gtk_tooltips_new ();
242 if (handle is null) SWT.error (SWT.ERROR_NO_HANDLES);
243 /*
244 * Bug in Solaris-GTK. Invoking gtk_tooltips_force_window()
245 * can cause a crash in older versions of GTK. The fix is
246 * to avoid this call if the GTK version is older than 2.2.x.
247 */
248 if (OS.GTK_VERSION >= OS.buildVERSION (2, 2, 1)) {
249 OS.gtk_tooltips_force_window (cast(GtkTooltips*)handle);
250 }
251 OS.g_object_ref (handle);
252 OS.gtk_object_sink (cast(GtkObject*)handle);
253 }
254 }
255
256 void createWidget (int index) {
257 super.createWidget (index);
258 text = "";
259 message = "";
260 x = y = -1;
261 autohide = true;
262 }
263
264 void deregister () {
265 super.deregister ();
266 if ((style & SWT.BALLOON) is 0) {
267 auto tipWindow = OS.GTK_TOOLTIPS_TIP_WINDOW (cast(GtkTooltips*)handle);
268 if (tipWindow !is null) display.removeWidget (tipWindow);
269 }
270 }
271
272 void destroyWidget () {
273 auto topHandle = topHandle ();
274 releaseHandle ();
275 if (topHandle !is null && (state & HANDLE) !is 0) {
276 if ((style & SWT.BALLOON) !is 0) {
277 OS.gtk_widget_destroy (topHandle);
278 } else {
279 OS.g_object_unref (topHandle);
280 }
281 }
282 }
283
284 /**
285 * Returns <code>true</code> if the receiver is automatically
286 * hidden by the platform, and <code>false</code> otherwise.
287 *
288 * @return the receiver's auto hide state
289 *
290 * @exception SWTException <ul>
291 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
292 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
293 * </ul>
294 *
295 */
296 public bool getAutoHide () {
297 checkWidget ();
298 return autohide;
299 }
300
301 Point getLocation () {
302 int x = this.x;
303 int y = this.y;
304 if (item !is null) {
305 auto itemHandle = item.handle;
306 OS.gtk_widget_realize (itemHandle);
307 auto window = OS.GTK_WIDGET_WINDOW (itemHandle);
308 int px, py;
309 OS.gdk_window_get_origin (cast(GdkWindow*)window, &px, &py);
310 x = px + OS.GTK_WIDGET_WIDTH (itemHandle) / 2;
311 y = py + OS.GTK_WIDGET_HEIGHT (itemHandle) / 2;
312 }
313 if (x is -1 || y is -1) {
314 int px, py;
315 OS.gdk_window_get_pointer (null, &px, &py, null);
316 x = px;
317 y = py;
318 }
319 return new Point(x, y);
320 }
321
322 /**
323 * Returns the receiver's message, which will be an empty
324 * string if it has never been set.
325 *
326 * @return the receiver's message
327 *
328 * @exception SWTException <ul>
329 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
330 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
331 * </ul>
332 */
333 public char[] getMessage () {
334 checkWidget ();
335 return message;
336 }
337
338 char[] getNameText () {
339 return getText ();
340 }
341
342 /**
343 * Returns the receiver's parent, which must be a <code>Shell</code>.
344 *
345 * @return the receiver's parent
346 *
347 * @exception SWTException <ul>
348 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
349 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
350 * </ul>
351 */
352 public Shell getParent () {
353 checkWidget ();
354 return parent;
355 }
356
357 Point getSize (int maxWidth) {
358 int textWidth = 0, messageWidth = 0;
359 int w, h;
360 if (layoutText !is null) {
361 OS.pango_layout_set_width (layoutText, -1);
362 OS.pango_layout_get_size (layoutText, &w, &h);
363 textWidth = OS.PANGO_PIXELS (w );
364 }
365 if (layoutMessage !is null) {
366 OS.pango_layout_set_width (layoutMessage, -1);
367 OS.pango_layout_get_size (layoutMessage, &w, &h);
368 messageWidth = OS.PANGO_PIXELS (w );
369 }
370 int messageTrim = 2 * INSET + 2 * BORDER + 2 * PADDING;
371 bool hasImage = layoutText !is null && (style & (SWT.ICON_ERROR | SWT.ICON_INFORMATION | SWT.ICON_WARNING)) !is 0;
372 int textTrim = messageTrim + (hasImage ? IMAGE_SIZE : 0);
373 int width = Math.min (maxWidth, Math.max (textWidth + textTrim, messageWidth + messageTrim));
374 int textHeight = 0, messageHeight = 0;
375 if (layoutText !is null) {
376 OS.pango_layout_set_width (layoutText, (maxWidth - textTrim) * OS.PANGO_SCALE);
377 OS.pango_layout_get_size (layoutText, &w, &h);
378 textHeight = OS.PANGO_PIXELS (h );
379 }
380 if (layoutMessage !is null) {
381 OS.pango_layout_set_width (layoutMessage, (maxWidth - messageTrim) * OS.PANGO_SCALE);
382 OS.pango_layout_get_size (layoutMessage, &w, &h);
383 messageHeight = OS.PANGO_PIXELS (h);
384 }
385 int height = 2 * BORDER + 2 * PADDING + messageHeight;
386 if (layoutText !is null) height += Math.max (IMAGE_SIZE, textHeight) + 2 * PADDING;
387 return new Point(width, height);
388 }
389
390 /**
391 * Returns the receiver's text, which will be an empty
392 * string if it has never been set.
393 *
394 * @return the receiver's text
395 *
396 * @exception SWTException <ul>
397 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
398 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
399 * </ul>
400 */
401 public char[] getText () {
402 checkWidget ();
403 return text;
404 }
405
406 /**
407 * Returns <code>true</code> if the receiver is visible, and
408 * <code>false</code> otherwise.
409 * <p>
410 * If one of the receiver's ancestors is not visible or some
411 * other condition makes the receiver not visible, this method
412 * may still indicate that it is considered visible even though
413 * it may not actually be showing.
414 * </p>
415 *
416 * @return the receiver's visibility state
417 *
418 * @exception SWTException <ul>
419 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
420 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
421 * </ul>
422 */
423 public bool getVisible () {
424 checkWidget ();
425 if ((style & SWT.BALLOON) !is 0) return OS.GTK_WIDGET_VISIBLE (handle);
426 auto tipWindow = OS.GTK_TOOLTIPS_TIP_WINDOW (cast(GtkTooltips*)handle);
427 return OS.GTK_WIDGET_VISIBLE (tipWindow);
428 }
429
430 override int /*long*/ gtk_button_press_event (GtkWidget* widget, GdkEventButton* event) {
431 notifyListeners (SWT.Selection, new Event ());
432 setVisible (false);
433 return 0;
434 }
435
436 override int /*long*/ gtk_expose_event (GtkWidget* widget, GdkEventExpose* event) {
437 auto window = OS.GTK_WIDGET_WINDOW (handle);
438 auto gdkGC = cast(GdkGC*)OS.gdk_gc_new (window);
439 OS.gdk_draw_polygon (window, gdkGC, 0, cast(GdkPoint*)borderPolygon.ptr, borderPolygon.length / 2);
440 int x = BORDER + PADDING;
441 int y = BORDER + PADDING;
442 if (spikeAbove) y += TIP_HEIGHT;
443 if (layoutText !is null) {
444 char[] buffer = null;
445 int id = style & (SWT.ICON_ERROR | SWT.ICON_INFORMATION | SWT.ICON_WARNING);
446 switch (id) {
447 case SWT.ICON_ERROR: buffer = "gtk-dialog-error"; break;
448 case SWT.ICON_INFORMATION: buffer = "gtk-dialog-info"; break;
449 case SWT.ICON_WARNING: buffer = "gtk-dialog-warning"; break;
450 }
451 if (buffer !is null) {
452 auto style = OS.gtk_widget_get_default_style ();
453 auto pixbuf = OS.gtk_icon_set_render_icon (
454 OS.gtk_icon_factory_lookup_default (buffer.ptr),
455 style,
456 OS.GTK_TEXT_DIR_NONE,
457 OS.GTK_STATE_NORMAL,
458 OS.GTK_ICON_SIZE_MENU,
459 null,
460 null);
461 OS.gdk_draw_pixbuf (window, gdkGC, pixbuf, 0, 0, x, y, IMAGE_SIZE, IMAGE_SIZE, OS.GDK_RGB_DITHER_NORMAL, 0, 0);
462 OS.g_object_unref (pixbuf);
463 x += IMAGE_SIZE;
464 }
465 x += INSET;
466 OS.gdk_draw_layout (window, gdkGC, x, y, layoutText);
467 int w, h;
468 OS.pango_layout_get_size (layoutText, &w, &h);
469 y += 2 * PADDING + Math.max (IMAGE_SIZE, OS.PANGO_PIXELS (h ));
470 }
471 if (layoutMessage !is null) {
472 x = BORDER + PADDING + INSET;
473 OS.gdk_draw_layout (window, gdkGC, x, y, layoutMessage);
474 }
475 OS.g_object_unref (gdkGC);
476 return 0;
477 }
478
479 override int /*long*/ gtk_size_allocate (GtkWidget* widget, int /*long*/ allocation) {
480 Point point = getLocation ();
481 int x = point.x;
482 int y = point.y;
483 auto screen = OS.gdk_screen_get_default ();
484 OS.gtk_widget_realize (widget);
485 int monitorNumber = OS.gdk_screen_get_monitor_at_window (screen, OS.GTK_WIDGET_WINDOW (widget));
486 GdkRectangle* dest = new GdkRectangle ();
487 OS.gdk_screen_get_monitor_geometry (screen, monitorNumber, dest);
488 int w = OS.GTK_WIDGET_WIDTH (widget);
489 int h = OS.GTK_WIDGET_HEIGHT (widget);
490 if (dest.height < y + h) y -= h;
491 if (dest.width < x + w) x -= w;
492 OS.gtk_window_move (cast(GtkWindow*)widget, x, y);
493 return 0;
494 }
495
496 void hookEvents () {
497 if ((style & SWT.BALLOON) !is 0) {
498 OS.g_signal_connect_closure (handle, OS.expose_event.ptr, display.closures [EXPOSE_EVENT], false);
499 OS.gtk_widget_add_events (handle, OS.GDK_BUTTON_PRESS_MASK);
500 OS.g_signal_connect_closure (handle, OS.button_press_event.ptr, display.closures [BUTTON_PRESS_EVENT], false);
501 } else {
502 auto tipWindow = OS.GTK_TOOLTIPS_TIP_WINDOW (cast(GtkTooltips*)handle);
503 if (tipWindow !is null) {
504 OS.g_signal_connect_closure (tipWindow, OS.size_allocate.ptr, display.closures [SIZE_ALLOCATE], false);
505 OS.gtk_widget_add_events (tipWindow, OS.GDK_BUTTON_PRESS_MASK);
506 OS.g_signal_connect_closure (tipWindow, OS.button_press_event.ptr, display.closures [BUTTON_PRESS_EVENT], false);
507 }
508 }
509 }
510
511 /**
512 * Returns <code>true</code> if the receiver is visible and all
513 * of the receiver's ancestors are visible and <code>false</code>
514 * otherwise.
515 *
516 * @return the receiver's visibility state
517 *
518 * @exception SWTException <ul>
519 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
520 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
521 * </ul>
522 *
523 * @see #getVisible
524 */
525 public bool isVisible () {
526 checkWidget ();
527 return getVisible ();
528 }
529
530 void register () {
531 super.register ();
532 if ((style & SWT.BALLOON) is 0) {
533 auto tipWindow = OS.GTK_TOOLTIPS_TIP_WINDOW (cast(GtkTooltips*)handle);
534 if (tipWindow !is null) display.addWidget (tipWindow, this);
535 }
536 }
537
538 void releaseWidget () {
539 super.releaseWidget ();
540 if (layoutText !is null) OS.g_object_unref (layoutText);
541 layoutText = null;
542 if (layoutMessage !is null) OS.g_object_unref (layoutMessage);
543 layoutMessage = null;
544 if (timerId !is 0) OS.gtk_timeout_remove(timerId);
545 timerId = 0;
546 text = null;
547 message = null;
548 borderPolygon = null;
549 }
550
551 /**
552 * Removes the listener from the collection of listeners who will
553 * be notified when the receiver is selected by the user.
554 *
555 * @param listener the listener which should no longer be notified
556 *
557 * @exception IllegalArgumentException <ul>
558 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
559 * </ul>
560 * @exception SWTException <ul>
561 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
562 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
563 * </ul>
564 *
565 * @see SelectionListener
566 * @see #addSelectionListener
567 */
568 public void removeSelectionListener (SelectionListener listener) {
569 checkWidget();
570 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
571 if (eventTable is null) return;
572 eventTable.unhook (SWT.Selection, listener);
573 eventTable.unhook (SWT.DefaultSelection, listener);
574 }
575
576 /**
577 * Makes the receiver hide automatically when <code>true</code>,
578 * and remain visible when <code>false</code>.
579 *
580 * @param autoHide the auto hide state
581 *
582 * @exception SWTException <ul>
583 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
584 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
585 * </ul>
586 *
587 * @see #getVisible
588 * @see #setVisible
589 */
590 public void setAutoHide (bool autohide) {
591 checkWidget ();
592 this.autohide = autohide;
593 //TODO - update when visible
594 }
595
596 /**
597 * Sets the location of the receiver, which must be a tooltip,
598 * to the point specified by the arguments which are relative
599 * to the display.
600 * <p>
601 * Note that this is different from most widgets where the
602 * location of the widget is relative to the parent.
603 * </p>
604 *
605 * @param x the new x coordinate for the receiver
606 * @param y the new y coordinate for the receiver
607 *
608 * @exception SWTException <ul>
609 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
610 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
611 * </ul>
612 */
613 public void setLocation (int x, int y) {
614 checkWidget ();
615 this.x = x;
616 this.y = y;
617 if ((style & SWT.BALLOON) !is 0) {
618 if (OS.GTK_WIDGET_VISIBLE (handle)) configure ();
619 } else {
620 auto tipWindow = OS.GTK_TOOLTIPS_TIP_WINDOW (cast(GtkTooltips*)handle);
621 if (OS.GTK_WIDGET_VISIBLE (tipWindow)) {
622 OS.gtk_window_move (cast(GtkWindow*)tipWindow, x, y);
623 }
624 }
625 }
626
627 /**
628 * Sets the location of the receiver, which must be a tooltip,
629 * to the point specified by the argument which is relative
630 * to the display.
631 * <p>
632 * Note that this is different from most widgets where the
633 * location of the widget is relative to the parent.
634 * </p><p>
635 * Note that the platform window manager ultimately has control
636 * over the location of tooltips.
637 * </p>
638 *
639 * @param location the new location for the receiver
640 *
641 * @exception IllegalArgumentException <ul>
642 * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
643 * </ul>
644 * @exception SWTException <ul>
645 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
646 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
647 * </ul>
648 */
649 public void setLocation (Point location) {
650 checkWidget ();
651 if (location is null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
652 setLocation (location.x, location.y);
653 }
654
655 /**
656 * Sets the receiver's message.
657 *
658 * @param string the new message
659 *
660 * @exception IllegalArgumentException <ul>
661 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
662 * </ul>
663 * @exception SWTException <ul>
664 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
665 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
666 * </ul>
667 */
668 public void setMessage (char[] string) {
669 checkWidget ();
670 if (string is null) error (SWT.ERROR_NULL_ARGUMENT);
671 message = string;
672 if ((style & SWT.BALLOON) is 0) return;
673 if (layoutMessage !is null) OS.g_object_unref (layoutMessage);
674 layoutMessage = null;
675 if (message.length !is 0) {
676 layoutMessage = OS.gtk_widget_create_pango_layout (handle, toStringz( message ));
677 OS.pango_layout_set_wrap (layoutMessage, OS.PANGO_WRAP_WORD_CHAR);
678 }
679 if (OS.GTK_WIDGET_VISIBLE (handle)) configure ();
680 }
681
682 /**
683 * Sets the receiver's text.
684 *
685 * @param string the new text
686 *
687 * @exception IllegalArgumentException <ul>
688 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
689 * </ul>
690 * @exception SWTException <ul>
691 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
692 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
693 * </ul>
694 */
695 public void setText (char[] string) {
696 checkWidget ();
697 if (string is null) error (SWT.ERROR_NULL_ARGUMENT);
698 text = string;
699 if ((style & SWT.BALLOON) is 0) return;
700 if (layoutText !is null) OS.g_object_unref (layoutText);
701 layoutText = null;
702 if (text.length !is 0) {
703 layoutText = OS.gtk_widget_create_pango_layout (handle, toStringz(text));
704 auto boldAttr = OS.pango_attr_weight_new (OS.PANGO_WEIGHT_BOLD);
705 boldAttr.start_index = 0;
706 boldAttr.end_index = text.length+1;
707 auto attrList = OS.pango_attr_list_new ();
708 OS.pango_attr_list_insert (attrList, boldAttr);
709 OS.pango_layout_set_attributes (layoutText, attrList);
710 OS.pango_attr_list_unref (attrList);
711 OS.pango_layout_set_wrap (layoutText, OS.PANGO_WRAP_WORD_CHAR);
712 }
713 if (OS.GTK_WIDGET_VISIBLE (handle)) configure ();
714 }
715
716 /**
717 * Marks the receiver as visible if the argument is <code>true</code>,
718 * and marks it invisible otherwise.
719 * <p>
720 * If one of the receiver's ancestors is not visible or some
721 * other condition makes the receiver not visible, marking
722 * it visible may not actually cause it to be displayed.
723 * </p>
724 *
725 * @param visible the new visibility state
726 *
727 * @exception SWTException <ul>
728 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
729 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
730 * </ul>
731 */
732 public void setVisible (bool visible) {
733 if (timerId !is 0) OS.gtk_timeout_remove(timerId);
734 timerId = 0;
735 if (visible) {
736 if ((style & SWT.BALLOON) !is 0) {
737 configure ();
738 OS.gtk_widget_show (handle);
739 } else {
740 auto vboxHandle = parent.vboxHandle;
741 char[] string = text;
742 if (text.length > 0) string ~= "\n\n";
743 string ~= message;
744 char* buffer = toStringz( string );
745 OS.gtk_tooltips_set_tip (cast(GtkTooltips*)handle, vboxHandle, buffer, null);
746 auto data = OS.gtk_tooltips_data_get (vboxHandle);
747 OS.GTK_TOOLTIPS_SET_ACTIVE (cast(GtkTooltips*)handle, data);
748 OS.gtk_tooltips_set_tip (cast(GtkTooltips*)handle, vboxHandle, buffer, null);
749 }
750 if (autohide) timerId = display.doWindowTimerAdd( &timerProcCallbackData, DELAY, handle);
751 } else {
752 if ((style & SWT.BALLOON) !is 0) {
753 OS.gtk_widget_hide (handle);
754 } else {
755 auto tipWindow = OS.GTK_TOOLTIPS_TIP_WINDOW (cast(GtkTooltips*)handle);
756 OS.gtk_widget_hide (tipWindow);
757 }
758 }
759 }
760
761 override int /*long*/ timerProc (GtkWidget* widget) {
762 if ((style & SWT.BALLOON) !is 0) {
763 OS.gtk_widget_hide (handle);
764 } else {
765 auto tipWindow = OS.GTK_TOOLTIPS_TIP_WINDOW (cast(GtkTooltips*)handle);
766 OS.gtk_widget_hide (tipWindow);
767 }
768 return 0;
769 }
770
771 }