Mercurial > projects > dwt-linux
comparison dwt/widgets/TrayItem.d @ 47:f646579f309c
Tray, Tooltip, TrayItem, Item
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 11 Jan 2008 08:34:26 +0100 |
parents | |
children | 8cec8f536af3 |
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.TrayItem; | |
12 | |
13 import dwt.SWT; | |
14 import dwt.widgets.Tray; | |
15 import dwt.widgets.ToolTip; | |
16 import dwt.widgets.ImageList; | |
17 import dwt.widgets.Item; | |
18 import dwt.events.SelectionListener; | |
19 import dwt.events.SelectionEvent; | |
20 import dwt.events.MenuDetectListener; | |
21 import dwt.widgets.TypedListener; | |
22 import dwt.graphics.Image; | |
23 import dwt.graphics.Rectangle; | |
24 import dwt.graphics.Region; | |
25 import dwt.internal.gtk.OS; | |
26 | |
27 import Math = tango.math.Math; | |
28 import tango.stdc.stringz; | |
29 import tango.util.Convert; | |
30 | |
31 /** | |
32 * Instances of this class represent icons that can be placed on the | |
33 * system tray or task bar status area. | |
34 * <p> | |
35 * <dl> | |
36 * <dt><b>Styles:</b></dt> | |
37 * <dd>(none)</dd> | |
38 * <dt><b>Events:</b></dt> | |
39 * <dd>DefaultSelection, MenuDetect, Selection</dd> | |
40 * </dl> | |
41 * </p><p> | |
42 * IMPORTANT: This class is <em>not</em> intended to be subclassed. | |
43 * </p> | |
44 * | |
45 * @since 3.0 | |
46 */ | |
47 public class TrayItem : Item { | |
48 Tray parent; | |
49 ToolTip toolTip; | |
50 char[] toolTipText; | |
51 GtkWidget* imageHandle; | |
52 GtkWidget* tooltipsHandle; | |
53 ImageList imageList; | |
54 | |
55 /** | |
56 * Constructs a new instance of this class given its parent | |
57 * (which must be a <code>Tray</code>) and a style value | |
58 * describing its behavior and appearance. The item is added | |
59 * to the end of the items maintained by its parent. | |
60 * <p> | |
61 * The style value is either one of the style constants defined in | |
62 * class <code>SWT</code> which is applicable to instances of this | |
63 * class, or must be built by <em>bitwise OR</em>'ing together | |
64 * (that is, using the <code>int</code> "|" operator) two or more | |
65 * of those <code>SWT</code> style constants. The class description | |
66 * lists the style constants that are applicable to the class. | |
67 * Style bits are also inherited from superclasses. | |
68 * </p> | |
69 * | |
70 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
71 * @param style the style of control to construct | |
72 * | |
73 * @exception IllegalArgumentException <ul> | |
74 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
75 * </ul> | |
76 * @exception SWTException <ul> | |
77 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
78 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
79 * </ul> | |
80 * | |
81 * @see SWT | |
82 * @see Widget#checkSubclass | |
83 * @see Widget#getStyle | |
84 */ | |
85 public this (Tray parent, int style) { | |
86 super (parent, style); | |
87 this.parent = parent; | |
88 createWidget (parent.getItemCount ()); | |
89 } | |
90 | |
91 /** | |
92 * Adds the listener to the collection of listeners who will | |
93 * be notified when the platform-specific context menu trigger | |
94 * has occurred, by sending it one of the messages defined in | |
95 * the <code>MenuDetectListener</code> interface. | |
96 * | |
97 * @param listener the listener which should be notified | |
98 * | |
99 * @exception IllegalArgumentException <ul> | |
100 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
101 * </ul> | |
102 * @exception SWTException <ul> | |
103 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
104 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
105 * </ul> | |
106 * | |
107 * @see MenuDetectListener | |
108 * @see #removeMenuDetectListener | |
109 * | |
110 * @since 3.3 | |
111 */ | |
112 public void addMenuDetectListener (MenuDetectListener listener) { | |
113 checkWidget (); | |
114 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT); | |
115 TypedListener typedListener = new TypedListener (listener); | |
116 addListener (SWT.MenuDetect, typedListener); | |
117 } | |
118 | |
119 /** | |
120 * Adds the listener to the collection of listeners who will | |
121 * be notified when the receiver is selected by the user, by sending | |
122 * it one of the messages defined in the <code>SelectionListener</code> | |
123 * interface. | |
124 * <p> | |
125 * <code>widgetSelected</code> is called when the receiver is selected | |
126 * <code>widgetDefaultSelected</code> is called when the receiver is double-clicked | |
127 * </p> | |
128 * | |
129 * @param listener the listener which should be notified when the receiver is selected by the user | |
130 * | |
131 * @exception IllegalArgumentException <ul> | |
132 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
133 * </ul> | |
134 * @exception SWTException <ul> | |
135 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
136 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
137 * </ul> | |
138 * | |
139 * @see SelectionListener | |
140 * @see #removeSelectionListener | |
141 * @see SelectionEvent | |
142 */ | |
143 public void addSelectionListener(SelectionListener listener) { | |
144 checkWidget (); | |
145 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT); | |
146 TypedListener typedListener = new TypedListener (listener); | |
147 addListener (SWT.Selection, typedListener); | |
148 addListener (SWT.DefaultSelection, typedListener); | |
149 } | |
150 | |
151 protected void checkSubclass () { | |
152 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); | |
153 } | |
154 | |
155 void createWidget (int index) { | |
156 super.createWidget (index); | |
157 parent.createItem (this, index); | |
158 } | |
159 | |
160 void createHandle (int index) { | |
161 state |= HANDLE; | |
162 handle = OS.gtk_plug_new (0); | |
163 if (handle is null) error (SWT.ERROR_NO_HANDLES); | |
164 imageHandle = OS.gtk_image_new (); | |
165 if (imageHandle is null) error (SWT.ERROR_NO_HANDLES); | |
166 OS.gtk_container_add (cast(GtkContainer*)handle, imageHandle); | |
167 OS.gtk_widget_show (handle); | |
168 OS.gtk_widget_show (imageHandle); | |
169 auto id = OS.gtk_plug_get_id (cast(GtkPlug*)handle); | |
170 int monitor = 0; | |
171 auto screen = OS.gdk_screen_get_default (); | |
172 if (screen !is null) { | |
173 monitor = OS.gdk_screen_get_number (screen); | |
174 } | |
175 auto trayAtom = OS.gdk_atom_intern (toStringz("_NET_SYSTEM_TRAY_S" ~ to!(char[])(monitor)), true); | |
176 auto xTrayAtom = OS.gdk_x11_atom_to_xatom (trayAtom); | |
177 auto xDisplay = OS.GDK_DISPLAY (); | |
178 auto trayWindow = OS.XGetSelectionOwner (xDisplay, xTrayAtom); | |
179 auto messageAtom = OS.gdk_atom_intern (toStringz("_NET_SYSTEM_TRAY_OPCODE"), true); | |
180 auto xMessageAtom = OS.gdk_x11_atom_to_xatom (messageAtom); | |
181 XClientMessageEvent* event = cast(XClientMessageEvent*)OS.g_malloc (XClientMessageEvent.sizeof);; | |
182 event.type = OS.ClientMessage; | |
183 event.window = trayWindow; | |
184 event.message_type = xMessageAtom; | |
185 event.format = 32; | |
186 event.data.l [0] = OS.GDK_CURRENT_TIME; | |
187 event.data.l [1] = OS.SYSTEM_TRAY_REQUEST_DOCK; | |
188 event.data.l [2] = id; | |
189 OS.XSendEvent (xDisplay, trayWindow, false, OS.NoEventMask, cast(XEvent*) event); | |
190 OS.g_free (event); | |
191 } | |
192 | |
193 void deregister () { | |
194 super.deregister (); | |
195 display.removeWidget (imageHandle); | |
196 } | |
197 | |
198 void destroyWidget () { | |
199 parent.destroyItem (this); | |
200 releaseHandle (); | |
201 } | |
202 | |
203 /** | |
204 * Returns the receiver's parent, which must be a <code>Tray</code>. | |
205 * | |
206 * @return the receiver's parent | |
207 * | |
208 * @exception SWTException <ul> | |
209 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
210 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
211 * </ul> | |
212 * | |
213 * @since 3.2 | |
214 */ | |
215 public Tray getParent () { | |
216 checkWidget (); | |
217 return parent; | |
218 } | |
219 | |
220 /** | |
221 * Returns the receiver's tool tip, or null if it has | |
222 * not been set. | |
223 * | |
224 * @return the receiver's tool tip text | |
225 * | |
226 * @exception SWTException <ul> | |
227 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
228 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
229 * </ul> | |
230 * | |
231 * @since 3.2 | |
232 */ | |
233 public ToolTip getToolTip () { | |
234 checkWidget (); | |
235 return toolTip; | |
236 } | |
237 | |
238 /** | |
239 * Returns the receiver's tool tip text, or null if it has | |
240 * not been set. | |
241 * | |
242 * @return the receiver's tool tip text | |
243 * | |
244 * @exception SWTException <ul> | |
245 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
246 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
247 * </ul> | |
248 */ | |
249 public char[] getToolTipText () { | |
250 checkWidget (); | |
251 return toolTipText; | |
252 } | |
253 | |
254 override int /*long*/ gtk_button_press_event (GtkWidget* widget, GdkEventButton* event) { | |
255 if (event.type is OS.GDK_3BUTTON_PRESS) return 0; | |
256 if (event.button is 3 && event.type is OS.GDK_BUTTON_PRESS) { | |
257 sendEvent (SWT.MenuDetect); | |
258 return 0; | |
259 } | |
260 if (event.type is OS.GDK_2BUTTON_PRESS) { | |
261 postEvent (SWT.DefaultSelection); | |
262 } else { | |
263 postEvent (SWT.Selection); | |
264 } | |
265 return 0; | |
266 } | |
267 | |
268 override int /*long*/ gtk_size_allocate (GtkWidget* widget, int /*long*/ allocation) { | |
269 if (image !is null && image.mask !is null) { | |
270 if (OS.gdk_drawable_get_depth (image.mask) is 1) { | |
271 int xoffset = cast(int) Math.floor (OS.GTK_WIDGET_X (widget) + ((OS.GTK_WIDGET_WIDTH (widget) - OS.GTK_WIDGET_REQUISITION_WIDTH (widget)) * 0.5) + 0.5); | |
272 int yoffset = cast(int) Math.floor (OS.GTK_WIDGET_Y (widget) + ((OS.GTK_WIDGET_HEIGHT (widget) - OS.GTK_WIDGET_REQUISITION_HEIGHT (widget)) * 0.5) + 0.5); | |
273 Rectangle b = image.getBounds(); | |
274 auto gdkImage = OS.gdk_drawable_get_image (image.mask, 0, 0, b.width, b.height); | |
275 if (gdkImage is null) SWT.error(SWT.ERROR_NO_HANDLES); | |
276 byte[] maskData = (cast(byte*)gdkImage.mem)[ 0 .. gdkImage.bpl * gdkImage.height].dup; | |
277 Region region = new Region (display); | |
278 for (int y = 0; y < b.height; y++) { | |
279 for (int x = 0; x < b.width; x++) { | |
280 int index = (y * gdkImage.bpl) + (x >> 3); | |
281 int theByte = maskData [index] & 0xFF; | |
282 int mask = 1 << (x & 0x7); | |
283 if ((theByte & mask) !is 0) { | |
284 region.add (xoffset + x, yoffset + y, 1, 1); | |
285 } | |
286 } | |
287 } | |
288 OS.g_object_unref (gdkImage); | |
289 OS.gtk_widget_realize (handle); | |
290 auto window = OS.GTK_WIDGET_WINDOW (handle); | |
291 OS.gdk_window_shape_combine_region (window, region.handle, 0, 0); | |
292 region.dispose (); | |
293 } | |
294 } | |
295 return 0; | |
296 } | |
297 | |
298 void hookEvents () { | |
299 int eventMask = OS.GDK_BUTTON_PRESS_MASK; | |
300 OS.gtk_widget_add_events (handle, eventMask); | |
301 OS.g_signal_connect_closure_by_id (handle, display.signalIds [BUTTON_PRESS_EVENT], 0, display.closures [BUTTON_PRESS_EVENT], false); | |
302 OS.g_signal_connect_closure_by_id (imageHandle, display.signalIds [SIZE_ALLOCATE], 0, display.closures [SIZE_ALLOCATE], false); | |
303 } | |
304 | |
305 /** | |
306 * Returns <code>true</code> if the receiver is visible and | |
307 * <code>false</code> otherwise. | |
308 * | |
309 * @return the receiver's visibility | |
310 * | |
311 * @exception SWTException <ul> | |
312 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
313 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
314 * </ul> | |
315 */ | |
316 public bool getVisible () { | |
317 checkWidget (); | |
318 return OS.GTK_WIDGET_VISIBLE (handle); | |
319 } | |
320 | |
321 void register () { | |
322 super.register (); | |
323 display.addWidget (imageHandle, this); | |
324 } | |
325 | |
326 void releaseHandle () { | |
327 if (handle !is null) OS.gtk_widget_destroy (handle); | |
328 handle = imageHandle = null; | |
329 super.releaseHandle (); | |
330 parent = null; | |
331 } | |
332 | |
333 void releaseWidget () { | |
334 super.releaseWidget (); | |
335 if (tooltipsHandle !is null) OS.g_object_unref (tooltipsHandle); | |
336 tooltipsHandle = null; | |
337 if (imageList !is null) imageList.dispose (); | |
338 imageList = null; | |
339 toolTipText = null; | |
340 } | |
341 | |
342 /** | |
343 * Removes the listener from the collection of listeners who will | |
344 * be notified when the platform-specific context menu trigger has | |
345 * occurred. | |
346 * | |
347 * @param listener the listener which should no longer be notified | |
348 * | |
349 * @exception IllegalArgumentException <ul> | |
350 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
351 * </ul> | |
352 * @exception SWTException <ul> | |
353 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
354 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
355 * </ul> | |
356 * | |
357 * @see MenuDetectListener | |
358 * @see #addMenuDetectListener | |
359 * | |
360 * @since 3.3 | |
361 */ | |
362 public void removeMenuDetectListener (MenuDetectListener listener) { | |
363 checkWidget (); | |
364 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT); | |
365 if (eventTable is null) return; | |
366 eventTable.unhook (SWT.MenuDetect, listener); | |
367 } | |
368 | |
369 /** | |
370 * Removes the listener from the collection of listeners who will | |
371 * be notified when the receiver is selected by the user. | |
372 * | |
373 * @param listener the listener which should no longer be notified | |
374 * | |
375 * @exception IllegalArgumentException <ul> | |
376 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
377 * </ul> | |
378 * @exception SWTException <ul> | |
379 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
380 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
381 * </ul> | |
382 * | |
383 * @see SelectionListener | |
384 * @see #addSelectionListener | |
385 */ | |
386 public void removeSelectionListener (SelectionListener listener) { | |
387 checkWidget (); | |
388 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT); | |
389 if (eventTable is null) return; | |
390 eventTable.unhook (SWT.Selection, listener); | |
391 eventTable.unhook (SWT.DefaultSelection, listener); | |
392 } | |
393 | |
394 /** | |
395 * Sets the receiver's image. | |
396 * | |
397 * @param image the new image | |
398 * | |
399 * @exception IllegalArgumentException <ul> | |
400 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li> | |
401 * </ul> | |
402 * @exception SWTException <ul> | |
403 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
404 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
405 * </ul> | |
406 */ | |
407 public void setImage (Image image) { | |
408 checkWidget (); | |
409 if (image !is null && image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); | |
410 this.image = image; | |
411 if (image !is null) { | |
412 Rectangle rect = image.getBounds (); | |
413 OS.gtk_widget_set_size_request (handle, rect.width, rect.height); | |
414 if (imageList is null) imageList = new ImageList (); | |
415 int imageIndex = imageList.indexOf (image); | |
416 if (imageIndex is -1) { | |
417 imageIndex = imageList.add (image); | |
418 } else { | |
419 imageList.put (imageIndex, image); | |
420 } | |
421 auto pixbuf = imageList.getPixbuf (imageIndex); | |
422 OS.gtk_image_set_from_pixbuf (cast(GtkImage*)imageHandle, pixbuf); | |
423 OS.gtk_widget_show (imageHandle); | |
424 } else { | |
425 OS.gtk_widget_set_size_request (handle, 1, 1); | |
426 OS.gtk_image_set_from_pixbuf (cast(GtkImage*)imageHandle, null); | |
427 OS.gtk_widget_hide (imageHandle); | |
428 } | |
429 } | |
430 | |
431 /** | |
432 * Sets the receiver's tool tip to the argument, which | |
433 * may be null indicating that no tool tip should be shown. | |
434 * | |
435 * @param toolTip the new tool tip (or null) | |
436 * | |
437 * @exception SWTException <ul> | |
438 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
439 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
440 * </ul> | |
441 * | |
442 * @since 3.2 | |
443 */ | |
444 public void setToolTip (ToolTip toolTip) { | |
445 checkWidget (); | |
446 ToolTip oldTip = this.toolTip, newTip = toolTip; | |
447 if (oldTip !is null) oldTip.item = null; | |
448 this.toolTip = newTip; | |
449 if (newTip !is null) newTip.item = this; | |
450 } | |
451 | |
452 /** | |
453 * Sets the receiver's tool tip text to the argument, which | |
454 * may be null indicating that no tool tip text should be shown. | |
455 * | |
456 * @param value the new tool tip text (or null) | |
457 * | |
458 * @exception SWTException <ul> | |
459 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
460 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
461 * </ul> | |
462 */ | |
463 public void setToolTipText (char[] string) { | |
464 checkWidget (); | |
465 toolTipText = string; | |
466 char* buffer = null; | |
467 if (string !is null && string.length > 0) { | |
468 buffer = toStringz( string ); | |
469 } | |
470 if (tooltipsHandle is null) { | |
471 tooltipsHandle = cast(GtkWidget*)OS.gtk_tooltips_new (); | |
472 if (tooltipsHandle is null) error (SWT.ERROR_NO_HANDLES); | |
473 OS.g_object_ref (cast(GObject*)tooltipsHandle); | |
474 OS.gtk_object_sink (cast(GtkObject*)tooltipsHandle); | |
475 } | |
476 OS.gtk_tooltips_set_tip (cast(GtkTooltips*)tooltipsHandle, handle, buffer, null); | |
477 } | |
478 | |
479 /** | |
480 * Makes the receiver visible if the argument is <code>true</code>, | |
481 * and makes it invisible otherwise. | |
482 * | |
483 * @param visible the new visibility state | |
484 * | |
485 * @exception SWTException <ul> | |
486 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
487 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
488 * </ul> | |
489 */ | |
490 public void setVisible (bool visible) { | |
491 checkWidget (); | |
492 if (OS.GTK_WIDGET_VISIBLE (handle) is visible) return; | |
493 if (visible) { | |
494 /* | |
495 * It is possible (but unlikely), that application | |
496 * code could have disposed the widget in the show | |
497 * event. If this happens, just return. | |
498 */ | |
499 sendEvent (SWT.Show); | |
500 if (isDisposed ()) return; | |
501 OS.gtk_widget_show (handle); | |
502 } else { | |
503 OS.gtk_widget_hide (handle); | |
504 sendEvent (SWT.Hide); | |
505 } | |
506 } | |
507 } |