1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 IBM Corporation and others.
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/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.internal.*;
20 import org.eclipse.swt.internal.win32.*;
23 * Instances of this class allow the user to select a color
24 * from a predefined set of available colors.
26 * <dt><b>Styles:</b></dt>
28 * <dt><b>Events:</b></dt>
32 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
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.
39 public class ColorDialog extends Dialog {
40 static final int CUSTOM_COLOR_COUNT = 16; // from the MS spec for CHOOSECOLOR.lpCustColors
45 int [] colors = new int [CUSTOM_COLOR_COUNT];
48 * Constructs a new instance of this class given only its parent.
50 * @param parent a composite control which will be the parent of the new instance
52 * @exception IllegalArgumentException <ul>
53 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
55 * @exception SWTException <ul>
56 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
57 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
61 * @see Widget#checkSubclass
62 * @see Widget#getStyle
64 public ColorDialog (Shell parent) {
65 this (parent, SWT.APPLICATION_MODAL);
69 * Constructs a new instance of this class given its parent
70 * and a style value describing its behavior and appearance.
72 * The style value is either one of the style constants defined in
73 * class <code>SWT</code> which is applicable to instances of this
74 * class, or must be built by <em>bitwise OR</em>'ing together
75 * (that is, using the <code>int</code> "|" operator) two or more
76 * of those <code>SWT</code> style constants. The class description
77 * lists the style constants that are applicable to the class.
78 * Style bits are also inherited from superclasses.
81 * @param parent a composite control which will be the parent of the new instance (cannot be null)
82 * @param style the style of control to construct
84 * @exception IllegalArgumentException <ul>
85 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
87 * @exception SWTException <ul>
88 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
89 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
93 * @see Widget#checkSubclass
94 * @see Widget#getStyle
96 public ColorDialog (Shell parent, int style) {
97 super (parent, checkStyle (parent, style));
101 long CCHookProc (long hdlg, long uiMsg, long lParam, long lpData) {
102 switch ((int)uiMsg) {
103 case OS.WM_INITDIALOG: {
104 RECT rect = new RECT ();
105 OS.GetWindowRect (hdlg, rect);
106 width = rect.right - rect.left;
107 height = rect.bottom - rect.top;
108 if (title != null && title.length () != 0) {
109 /* Use the character encoding for the default locale */
110 TCHAR buffer = new TCHAR (0, title, true);
111 OS.SetWindowText (hdlg, buffer);
115 case OS.WM_DESTROY: {
116 RECT rect = new RECT ();
117 OS.GetWindowRect (hdlg, rect);
118 int newWidth = rect.right - rect.left;
119 int newHeight = rect.bottom - rect.top;
120 if (newWidth < width || newHeight < height) {
121 //display.fullOpen = false;
123 if (newWidth > width || newHeight > height) {
124 //display.fullOpen = true;
134 * Returns the currently selected color in the receiver.
136 * @return the RGB value for the selected color, may be null
138 * @see PaletteData#getRGBs
140 public RGB getRGB () {
145 * Returns an array of <code>RGB</code>s which are the list of
146 * custom colors selected by the user in the receiver, or null
147 * if no custom colors were selected.
149 * @return the array of RGBs, which may be null
153 public RGB[] getRGBs() {
158 * Makes the receiver visible and brings it to the front
161 * @return the selected color, or null if the dialog was
162 * cancelled, no color was selected, or an error
165 * @exception SWTException <ul>
166 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
167 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
172 /* Get the owner HWND for the dialog */
173 long hwndOwner = parent.handle;
174 long hwndParent = parent.handle;
177 * Feature in Windows. There is no API to set the orientation of a
178 * color dialog. It is always inherited from the parent. The fix is
179 * to create a hidden parent and set the orientation in the hidden
180 * parent for the dialog to inherit.
182 boolean enabled = false;
183 int dialogOrientation = style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
184 int parentOrientation = parent.style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
185 if (dialogOrientation != parentOrientation) {
186 int exStyle = OS.WS_EX_NOINHERITLAYOUT;
187 if (dialogOrientation == SWT.RIGHT_TO_LEFT) exStyle |= OS.WS_EX_LAYOUTRTL;
188 hwndOwner = OS.CreateWindowEx (
193 OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0,
196 OS.GetModuleHandle (null),
198 enabled = OS.IsWindowEnabled (hwndParent);
199 if (enabled) OS.EnableWindow (hwndParent, false);
202 /* Create the CCHookProc */
203 Callback callback = new Callback (this, "CCHookProc", 4); //$NON-NLS-1$
204 long lpfnHook = callback.getAddress ();
205 if (lpfnHook == 0) error(SWT.ERROR_NO_MORE_CALLBACKS);
207 /* Allocate the Custom Colors and initialize to white */
208 display = parent.display;
209 if (display.lpCustColors == 0) {
210 long hHeap = OS.GetProcessHeap ();
211 display.lpCustColors = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, CUSTOM_COLOR_COUNT * 4);
212 for (int i=0; i < CUSTOM_COLOR_COUNT; i++) {
213 colors[i] = 0x00FFFFFF;
215 OS.MoveMemory (display.lpCustColors, colors, CUSTOM_COLOR_COUNT * 4);
218 /* Set the Custom Colors (if any) into the dialog */
220 int length = rgbs.length > CUSTOM_COLOR_COUNT ? CUSTOM_COLOR_COUNT : rgbs.length;
221 for (int i=0; i<length; i++) {
223 int red = rgb.red & 0xFF;
224 int green = (rgb.green << 8) & 0xFF00;
225 int blue = (rgb.blue << 16) & 0xFF0000;
226 colors[i] = red | green | blue;
228 for (int i=length; i < CUSTOM_COLOR_COUNT; i++) {
229 colors[i] = 0x00FFFFFF;
231 OS.MoveMemory (display.lpCustColors, colors, CUSTOM_COLOR_COUNT * 4);
234 /* Open the dialog */
235 CHOOSECOLOR lpcc = new CHOOSECOLOR ();
236 lpcc.lStructSize = CHOOSECOLOR.sizeof;
237 lpcc.Flags = OS.CC_ANYCOLOR | OS.CC_ENABLEHOOK;
238 //if (display.fullOpen) lpcc.Flags |= OS.CC_FULLOPEN;
239 lpcc.lpfnHook = lpfnHook;
240 lpcc.hwndOwner = hwndOwner;
241 lpcc.lpCustColors = display.lpCustColors;
243 lpcc.Flags |= OS.CC_RGBINIT;
244 int red = rgb.red & 0xFF;
245 int green = (rgb.green << 8) & 0xFF00;
246 int blue = (rgb.blue << 16) & 0xFF0000;
247 lpcc.rgbResult = red | green | blue;
250 /* Make the parent shell be temporary modal */
251 Dialog oldModal = null;
252 if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
253 oldModal = display.getModalDialog ();
254 display.setModalDialog (this);
257 display.externalEventLoop = true;
258 display.sendPreExternalEventDispatchEvent ();
259 /* Open the dialog */
260 boolean success = OS.ChooseColor (lpcc);
261 display.externalEventLoop = false;
262 display.sendPostExternalEventDispatchEvent ();
264 /* Clear the temporary dialog modal parent */
265 if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
266 display.setModalDialog (oldModal);
269 /* Get the Custom Colors (if the user defined any) from the dialog */
270 boolean customColor = false;
271 OS.MoveMemory (colors, display.lpCustColors, colors.length * 4);
272 for (int i=0; i<colors.length; i++) {
273 if (colors[i] != 0x00FFFFFF) {
279 rgbs = new RGB [CUSTOM_COLOR_COUNT];
280 for (int i=0; i<colors.length; i++) {
281 int color = colors[i];
282 int red = color & 0xFF;
283 int green = (color >> 8) & 0xFF;
284 int blue = (color >> 16) & 0xFF;
285 rgbs[i] = new RGB (red, green, blue);
290 int red = lpcc.rgbResult & 0xFF;
291 int green = (lpcc.rgbResult >> 8) & 0xFF;
292 int blue = (lpcc.rgbResult >> 16) & 0xFF;
293 rgb = new RGB (red, green, blue);
296 /* Free the CCHookProc */
299 /* Free the Custom Colors */
301 * This code is intentionally commented. Currently,
302 * there is exactly one set of custom colors per display.
303 * The memory associated with these colors is released
304 * when the display is disposed.
306 // if (lpCustColors != 0) OS.HeapFree (hHeap, 0, lpCustColors);
308 /* Destroy the BIDI orientation window */
309 if (hwndParent != hwndOwner) {
310 if (enabled) OS.EnableWindow (hwndParent, true);
311 OS.SetActiveWindow (hwndParent);
312 OS.DestroyWindow (hwndOwner);
316 * This code is intentionally commented. On some
317 * platforms, the owner window is repainted right
318 * away when a dialog window exits. This behavior
319 * is currently unspecified.
321 // if (hwndOwner != 0) OS.UpdateWindow (hwndOwner);
324 if (!success) return null;
329 * Sets the receiver's selected color to be the argument.
331 * @param rgb the new RGB value for the selected color, may be
332 * null to let the platform select a default when
334 * @see PaletteData#getRGBs
336 public void setRGB (RGB rgb) {
341 * Sets the receiver's list of custom colors to be the given array
342 * of <code>RGB</code>s, which may be null to let the platform select
343 * a default when open() is called.
345 * @param rgbs the array of RGBs, which may be null
347 * @exception IllegalArgumentException <ul>
348 * <li>ERROR_INVALID_ARGUMENT - if an RGB in the rgbs array is null</li>
353 public void setRGBs(RGB[] rgbs) {
355 for (int i=0; i<rgbs.length; i++) {
356 if (rgbs [i] == null) error (SWT.ERROR_INVALID_ARGUMENT);