comparison dwt/widgets/ColorDialog.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 649b8e223d5a
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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.ColorDialog;
12
13 import dwt.dwthelper.utils;
14
15
16 import dwt.DWT;
17 import dwt.DWTException;
18 import dwt.graphics.PaletteData;
19 import dwt.graphics.RGB;
20 import dwt.internal.cocoa.NSApplication;
21 import dwt.internal.cocoa.NSColor;
22 import dwt.internal.cocoa.NSColorPanel;
23 import dwt.internal.cocoa.NSString;
24 import dwt.internal.cocoa.OS;
25 import dwt.internal.cocoa.SWTPanelDelegate;
26
27 /**
28 * Instances of this class allow the user to select a color
29 * from a predefined set of available colors.
30 * <dl>
31 * <dt><b>Styles:</b></dt>
32 * <dd>(none)</dd>
33 * <dt><b>Events:</b></dt>
34 * <dd>(none)</dd>
35 * </dl>
36 * <p>
37 * IMPORTANT: This class is intended to be subclassed <em>only</em>
38 * within the DWT implementation.
39 * </p>
40 */
41 public class ColorDialog extends Dialog {
42 RGB rgb;
43
44 /**
45 * Constructs a new instance of this class given only its parent.
46 *
47 * @param parent a composite control which will be the parent of the new instance
48 *
49 * @exception IllegalArgumentException <ul>
50 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
51 * </ul>
52 * @exception DWTException <ul>
53 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
54 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
55 * </ul>
56 *
57 * @see DWT
58 * @see Widget#checkSubclass
59 * @see Widget#getStyle
60 */
61 public ColorDialog(Shell parent) {
62 this(parent, DWT.APPLICATION_MODAL);
63 }
64
65 /**
66 * Constructs a new instance of this class given its parent
67 * and a style value describing its behavior and appearance.
68 * <p>
69 * The style value is either one of the style constants defined in
70 * class <code>DWT</code> which is applicable to instances of this
71 * class, or must be built by <em>bitwise OR</em>'ing together
72 * (that is, using the <code>int</code> "|" operator) two or more
73 * of those <code>DWT</code> style constants. The class description
74 * lists the style constants that are applicable to the class.
75 * Style bits are also inherited from superclasses.
76 * </p>
77 *
78 * @param parent a composite control which will be the parent of the new instance (cannot be null)
79 * @param style the style of control to construct
80 *
81 * @exception IllegalArgumentException <ul>
82 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
83 * </ul>
84 * @exception DWTException <ul>
85 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
86 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
87 * </ul>
88 *
89 * @see DWT
90 * @see Widget#checkSubclass
91 * @see Widget#getStyle
92 */
93 public ColorDialog(Shell parent, int style) {
94 super(parent, style);
95 checkSubclass ();
96 }
97
98 void changeColor(int sender) {
99 //TODO
100 }
101
102 /**
103 * Returns the currently selected color in the receiver.
104 *
105 * @return the RGB value for the selected color, may be null
106 *
107 * @see PaletteData#getRGBs
108 */
109 public RGB getRGB() {
110 return rgb;
111 }
112
113 /**
114 * Makes the receiver visible and brings it to the front
115 * of the display.
116 *
117 * @return the selected color, or null if the dialog was
118 * cancelled, no color was selected, or an error
119 * occurred
120 *
121 * @exception DWTException <ul>
122 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
123 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
124 * </ul>
125 */
126 public RGB open() {
127 NSColorPanel panel = NSColorPanel.sharedColorPanel();
128 if (rgb !is null) {
129 NSColor color = NSColor.colorWithDeviceRed(rgb.red / 255f, rgb.green / 255f, rgb.blue / 255f, 1);
130 panel.setColor(color);
131 }
132 SWTPanelDelegate delegate = (SWTPanelDelegate)new SWTPanelDelegate().alloc().init();
133 int jniRef = OS.NewGlobalRef(this);
134 if (jniRef is 0) DWT.error(DWT.ERROR_NO_HANDLES);
135 delegate.setTag(jniRef);
136 panel.setDelegate(delegate);
137 rgb = null;
138 panel.orderFront(null);
139 NSApplication.sharedApplication().runModalForWindow_(panel);
140 panel.setDelegate(null);
141 delegate.release();
142 OS.DeleteGlobalRef(jniRef);
143 NSColor color = panel.color();
144 if (color !is null) {
145 color = color.colorUsingColorSpaceName_(NSString.stringWith("NSCalibratedRGBColorSpace"));
146 rgb = new RGB((int)(color.redComponent() * 255), (int)(color.greenComponent() * 255), (int)(color.blueComponent() * 255));
147 }
148 return rgb;
149 }
150
151 /**
152 * Sets the receiver's selected color to be the argument.
153 *
154 * @param rgb the new RGB value for the selected color, may be
155 * null to let the platform select a default when
156 * open() is called
157 * @see PaletteData#getRGBs
158 */
159 public void setRGB(RGB rgb) {
160 this.rgb = rgb;
161 }
162
163 void windowWillClose(int sender) {
164 NSApplication.sharedApplication().stop(null);
165 }
166 }