1 /*******************************************************************************
2  * Copyright (c) 2000, 2012 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.widgets;
15 
16 
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.internal.*;
20 import org.eclipse.swt.internal.win32.*;
21 
22 /**
23  * Instances of this class allow the user to select a color
24  * from a predefined set of available colors.
25  * <dl>
26  * <dt><b>Styles:</b></dt>
27  * <dd>(none)</dd>
28  * <dt><b>Events:</b></dt>
29  * <dd>(none)</dd>
30  * </dl>
31  * <p>
32  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
33  * </p>
34  *
35  * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample, Dialog tab</a>
36  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
37  * @noextend This class is not intended to be subclassed by clients.
38  */
39 public class ColorDialog extends Dialog {
40 	static final int CUSTOM_COLOR_COUNT = 16; // from the MS spec for CHOOSECOLOR.lpCustColors
41 	RGB rgb;
42 	RGB [] rgbs;
43 	int [] colors = new int [CUSTOM_COLOR_COUNT];
44 
45 /**
46  * Constructs a new instance of this class given only its parent.
47  *
48  * @param parent a composite control which will be the parent of the new instance
49  *
50  * @exception IllegalArgumentException <ul>
51  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
52  * </ul>
53  * @exception SWTException <ul>
54  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
55  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
56  * </ul>
57  *
58  * @see SWT
59  * @see Widget#checkSubclass
60  * @see Widget#getStyle
61  */
ColorDialog(Shell parent)62 public ColorDialog (Shell parent) {
63 	this (parent, SWT.APPLICATION_MODAL);
64 }
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
91  * @see Widget#checkSubclass
92  * @see Widget#getStyle
93  */
ColorDialog(Shell parent, int style)94 public ColorDialog (Shell parent, int style) {
95 	super (parent, checkStyle (parent, style));
96 	checkSubclass ();
97 }
98 
CCHookProc(long hdlg, long uiMsg, long lParam, long lpData)99 long CCHookProc (long hdlg, long uiMsg, long lParam, long lpData) {
100 	switch ((int)uiMsg) {
101 		case OS.WM_INITDIALOG: {
102 			if (title != null && title.length () != 0) {
103 				TCHAR buffer = new TCHAR (0, title, true);
104 				OS.SetWindowText (hdlg, buffer);
105 			}
106 			break;
107 		}
108 	}
109 	return 0;
110 }
111 
112 /**
113  * Returns the currently selected color in the receiver.
114  *
115  * @return the RGB value for the selected color, may be null
116  *
117  * @see PaletteData#getRGBs
118  */
getRGB()119 public RGB getRGB () {
120 	return rgb;
121 }
122 
123 /**
124  * Returns an array of <code>RGB</code>s which are the list of
125  * custom colors selected by the user in the receiver, or null
126  * if no custom colors were selected.
127  *
128  * @return the array of RGBs, which may be null
129  *
130  * @since 3.8
131  */
getRGBs()132 public RGB[] getRGBs() {
133 	return rgbs;
134 }
135 
136 /**
137  * Makes the receiver visible and brings it to the front
138  * of the display.
139  *
140  * @return the selected color, or null if the dialog was
141  *         cancelled, no color was selected, or an error
142  *         occurred
143  *
144  * @exception SWTException <ul>
145  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
146  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
147  * </ul>
148  */
open()149 public RGB open () {
150 
151 	/* Get the owner HWND for the dialog */
152 	long hwndOwner = parent.handle;
153 	long hwndParent = parent.handle;
154 
155 	/*
156 	* Feature in Windows.  There is no API to set the orientation of a
157 	* color dialog.  It is always inherited from the parent.  The fix is
158 	* to create a hidden parent and set the orientation in the hidden
159 	* parent for the dialog to inherit.
160 	*/
161 	boolean enabled = false;
162 	int dialogOrientation = style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
163 	int parentOrientation = parent.style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
164 	if (dialogOrientation != parentOrientation) {
165 		int exStyle = OS.WS_EX_NOINHERITLAYOUT;
166 		if (dialogOrientation == SWT.RIGHT_TO_LEFT) exStyle |= OS.WS_EX_LAYOUTRTL;
167 		hwndOwner = OS.CreateWindowEx (
168 			exStyle,
169 			Shell.DialogClass,
170 			null,
171 			0,
172 			OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0,
173 			hwndParent,
174 			0,
175 			OS.GetModuleHandle (null),
176 			null);
177 		enabled = OS.IsWindowEnabled (hwndParent);
178 		if (enabled) OS.EnableWindow (hwndParent, false);
179 	}
180 
181 	/* Create the CCHookProc */
182 	Callback callback = new Callback (this, "CCHookProc", 4); //$NON-NLS-1$
183 	long lpfnHook = callback.getAddress ();
184 
185 	/* Allocate the Custom Colors and initialize to white */
186 	Display display = parent.display;
187 	if (display.lpCustColors == 0) {
188 		long hHeap = OS.GetProcessHeap ();
189 		display.lpCustColors = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, CUSTOM_COLOR_COUNT * 4);
190 		for (int i=0; i < CUSTOM_COLOR_COUNT; i++) {
191 			colors[i] = 0x00FFFFFF;
192 		}
193 		OS.MoveMemory (display.lpCustColors, colors, CUSTOM_COLOR_COUNT * 4);
194 	}
195 
196 	/* Set the Custom Colors (if any) into the dialog */
197 	if (rgbs != null) {
198 		int length = rgbs.length > CUSTOM_COLOR_COUNT ? CUSTOM_COLOR_COUNT : rgbs.length;
199 		for (int i=0; i<length; i++) {
200 			RGB rgb = rgbs [i];
201 			int red = rgb.red & 0xFF;
202 			int green = (rgb.green << 8) & 0xFF00;
203 			int blue = (rgb.blue << 16) & 0xFF0000;
204 			colors[i] = red | green | blue;
205 		}
206 		for (int i=length; i < CUSTOM_COLOR_COUNT; i++) {
207 			colors[i] = 0x00FFFFFF;
208 		}
209 		OS.MoveMemory (display.lpCustColors, colors, CUSTOM_COLOR_COUNT * 4);
210 	}
211 
212 	/* Open the dialog */
213 	CHOOSECOLOR lpcc = new CHOOSECOLOR ();
214 	lpcc.lStructSize = CHOOSECOLOR.sizeof;
215 	lpcc.Flags = OS.CC_ANYCOLOR | OS.CC_ENABLEHOOK;
216 	lpcc.lpfnHook = lpfnHook;
217 	lpcc.hwndOwner = hwndOwner;
218 	lpcc.lpCustColors = display.lpCustColors;
219 	if (rgb != null) {
220 		lpcc.Flags |= OS.CC_RGBINIT;
221 		int red = rgb.red & 0xFF;
222 		int green = (rgb.green << 8) & 0xFF00;
223 		int blue = (rgb.blue << 16) & 0xFF0000;
224 		lpcc.rgbResult = red | green | blue;
225 	}
226 
227 	/* Make the parent shell be temporary modal */
228 	Dialog oldModal = null;
229 	if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
230 		oldModal = display.getModalDialog ();
231 		display.setModalDialog (this);
232 	}
233 
234 	display.externalEventLoop = true;
235 	display.sendPreExternalEventDispatchEvent ();
236 	/* Open the dialog */
237 	boolean success = OS.ChooseColor (lpcc);
238 	display.externalEventLoop = false;
239 	display.sendPostExternalEventDispatchEvent ();
240 
241 	/* Clear the temporary dialog modal parent */
242 	if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
243 		display.setModalDialog (oldModal);
244 	}
245 
246 	/* Get the Custom Colors (if the user defined any) from the dialog */
247 	boolean customColor = false;
248 	OS.MoveMemory (colors, display.lpCustColors, colors.length * 4);
249 	for (int color : colors) {
250 		if (color != 0x00FFFFFF) {
251 			customColor = true;
252 			break;
253 		}
254 	}
255 	if (customColor) {
256 		rgbs = new RGB [CUSTOM_COLOR_COUNT];
257 		for (int i=0; i<colors.length; i++) {
258 			int color = colors[i];
259 			int red = color & 0xFF;
260 			int green = (color >> 8) & 0xFF;
261 			int blue = (color >> 16) & 0xFF;
262 			rgbs[i] = new RGB (red, green, blue);
263 		}
264 	}
265 
266 	if (success) {
267 		int red = lpcc.rgbResult & 0xFF;
268 		int green = (lpcc.rgbResult >> 8) & 0xFF;
269 		int blue = (lpcc.rgbResult >> 16) & 0xFF;
270 		rgb = new RGB (red, green, blue);
271 	}
272 
273 	/* Free the CCHookProc */
274 	callback.dispose ();
275 
276 	/* Destroy the BIDI orientation window */
277 	if (hwndParent != hwndOwner) {
278 		if (enabled) OS.EnableWindow (hwndParent, true);
279 		OS.SetActiveWindow (hwndParent);
280 		OS.DestroyWindow (hwndOwner);
281 	}
282 
283 	if (!success) return null;
284 	return rgb;
285 }
286 
287 /**
288  * Sets the receiver's selected color to be the argument.
289  *
290  * @param rgb the new RGB value for the selected color, may be
291  *        null to let the platform select a default when
292  *        open() is called
293  * @see PaletteData#getRGBs
294  */
setRGB(RGB rgb)295 public void setRGB (RGB rgb) {
296 	this.rgb = rgb;
297 }
298 
299 /**
300  * Sets the receiver's list of custom colors to be the given array
301  * of <code>RGB</code>s, which may be null to let the platform select
302  * a default when open() is called.
303  *
304  * @param rgbs the array of RGBs, which may be null
305  *
306  * @exception IllegalArgumentException <ul>
307  *    <li>ERROR_INVALID_ARGUMENT - if an RGB in the rgbs array is null</li>
308  * </ul>
309  *
310  * @since 3.8
311  */
setRGBs(RGB[] rgbs)312 public void setRGBs(RGB[] rgbs) {
313 	if (rgbs != null) {
314 		for (RGB rgb : rgbs) {
315 			if (rgb == null) error (SWT.ERROR_INVALID_ARGUMENT);
316 		}
317 	}
318 	this.rgbs = rgbs;
319 }
320 
321 }