1 /******************************************************************************* 2 * Copyright (c) 2000, 2008 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 * Port to the D programming language: 11 * Frank Benoit <benoit@tionex.de> 12 *******************************************************************************/ 13 module org.eclipse.swt.widgets.Tray; 14 15 import java.lang.all; 16 17 import org.eclipse.swt.widgets.Display; 18 import org.eclipse.swt.widgets.Widget; 19 import org.eclipse.swt.widgets.TrayItem; 20 import org.eclipse.swt.SWT; 21 22 /** 23 * Instances of this class represent the system tray that is part 24 * of the task bar status area on some operating systems. 25 * 26 * <dl> 27 * <dt><b>Styles:</b></dt> 28 * <dd>(none)</dd> 29 * <dt><b>Events:</b></dt> 30 * <dd>(none)</dd> 31 * </dl> 32 * <p> 33 * IMPORTANT: This class is <em>not</em> intended to be subclassed. 34 * </p> 35 * 36 * @see Display#getSystemTray 37 * @see <a href="http://www.eclipse.org/swt/snippets/#tray">Tray, TrayItem snippets</a> 38 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> 39 * 40 * @since 3.0 41 */ 42 public class Tray : Widget { 43 int itemCount; 44 TrayItem [] items; 45 46 this (Display display, int style) { 47 items = new TrayItem [4]; 48 if (display is null) display = Display.getCurrent (); 49 if (display is null) display = Display.getDefault (); 50 if (!display.isValidThread ()) { 51 error (SWT.ERROR_THREAD_INVALID_ACCESS); 52 } 53 this.display = display; 54 } 55 56 void createItem (TrayItem item, int index) { 57 if (!(0 <= index && index <= itemCount)) error (SWT.ERROR_INVALID_RANGE); 58 if (itemCount is items.length) { 59 TrayItem [] newItems = new TrayItem [items.length + 4]; 60 System.arraycopy (items, 0, newItems, 0, items.length); 61 items = newItems; 62 } 63 System.arraycopy (items, index, items, index + 1, itemCount++ - index); 64 items [index] = item; 65 } 66 67 void destroyItem (TrayItem item) { 68 int index = 0; 69 while (index < itemCount) { 70 if (items [index] is item) break; 71 index++; 72 } 73 if (index is itemCount) return; 74 System.arraycopy (items, index + 1, items, index, --itemCount - index); 75 items [itemCount] = null; 76 } 77 78 /** 79 * Returns the item at the given, zero-relative index in the 80 * receiver. Throws an exception if the index is out of range. 81 * 82 * @param index the index of the item to return 83 * @return the item at the given index 84 * 85 * @exception IllegalArgumentException <ul> 86 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li> 87 * </ul> 88 * @exception SWTException <ul> 89 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 90 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 91 * </ul> 92 */ 93 public TrayItem getItem (int index) { 94 checkWidget (); 95 if (!(0 <= index && index < itemCount)) error (SWT.ERROR_INVALID_RANGE); 96 return items [index]; 97 } 98 99 /** 100 * Returns the number of items contained in the receiver. 101 * 102 * @return the number of items 103 * 104 * @exception SWTException <ul> 105 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 106 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 107 * </ul> 108 */ 109 public int getItemCount () { 110 checkWidget (); 111 return itemCount; 112 } 113 114 /** 115 * Returns an array of <code>TrayItem</code>s which are the items 116 * in the receiver. 117 * <p> 118 * Note: This is not the actual structure used by the receiver 119 * to maintain its list of items, so modifying the array will 120 * not affect the receiver. 121 * </p> 122 * 123 * @return the items in the receiver 124 * 125 * @exception SWTException <ul> 126 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 127 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 128 * </ul> 129 */ 130 public TrayItem [] getItems () { 131 checkWidget (); 132 TrayItem [] result = new TrayItem [itemCount]; 133 System.arraycopy (items, 0, result, 0, result.length); 134 return result; 135 } 136 137 override void releaseChildren (bool destroy) { 138 if (items !is null) { 139 for (int i=0; i<items.length; i++) { 140 TrayItem item = items [i]; 141 if (item !is null && !item.isDisposed ()) { 142 item.release (false); 143 } 144 } 145 items = null; 146 } 147 super.releaseChildren (destroy); 148 } 149 150 override void releaseParent () { 151 super.releaseParent (); 152 if (display.tray is this) display.tray = null; 153 } 154 155 }