]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/widgets/ColorDialog.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / widgets / ColorDialog.java
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         Display display;
42         int width, height;
43         RGB rgb;
44         RGB [] rgbs;
45         int [] colors = new int [CUSTOM_COLOR_COUNT];
46
47 /**
48  * Constructs a new instance of this class given only its parent.
49  *
50  * @param parent a composite control which will be the parent of the new instance
51  *
52  * @exception IllegalArgumentException <ul>
53  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
54  * </ul>
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>
58  * </ul>
59  *
60  * @see SWT
61  * @see Widget#checkSubclass
62  * @see Widget#getStyle
63  */
64 public ColorDialog (Shell parent) {
65         this (parent, SWT.APPLICATION_MODAL);
66 }
67
68 /**
69  * Constructs a new instance of this class given its parent
70  * and a style value describing its behavior and appearance.
71  * <p>
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.
79  * </p>
80  *
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
83  *
84  * @exception IllegalArgumentException <ul>
85  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
86  * </ul>
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>
90  * </ul>
91  *
92  * @see SWT
93  * @see Widget#checkSubclass
94  * @see Widget#getStyle
95  */
96 public ColorDialog (Shell parent, int style) {
97         super (parent, checkStyle (parent, style));
98         checkSubclass ();
99 }
100
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);
112                         }
113                         break;
114                 }
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;
122                         } else {
123                                 if (newWidth > width || newHeight > height) {
124                                         //display.fullOpen = true;
125                                 }
126                         }
127                         break;
128                 }
129         }
130         return 0;
131 }
132
133 /**
134  * Returns the currently selected color in the receiver.
135  *
136  * @return the RGB value for the selected color, may be null
137  *
138  * @see PaletteData#getRGBs
139  */
140 public RGB getRGB () {
141         return rgb;
142 }
143
144 /**
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.
148  *
149  * @return the array of RGBs, which may be null
150  *
151  * @since 3.8
152  */
153 public RGB[] getRGBs() {
154         return rgbs;
155 }
156
157 /**
158  * Makes the receiver visible and brings it to the front
159  * of the display.
160  *
161  * @return the selected color, or null if the dialog was
162  *         cancelled, no color was selected, or an error
163  *         occurred
164  *
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>
168  * </ul>
169  */
170 public RGB open () {
171
172         /* Get the owner HWND for the dialog */
173         long hwndOwner = parent.handle;
174         long hwndParent = parent.handle;
175
176         /*
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.
181         */
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 (
189                         exStyle,
190                         Shell.DialogClass,
191                         null,
192                         0,
193                         OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0,
194                         hwndParent,
195                         0,
196                         OS.GetModuleHandle (null),
197                         null);
198                 enabled = OS.IsWindowEnabled (hwndParent);
199                 if (enabled) OS.EnableWindow (hwndParent, false);
200         }
201
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);
206
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;
214                 }
215                 OS.MoveMemory (display.lpCustColors, colors, CUSTOM_COLOR_COUNT * 4);
216         }
217
218         /* Set the Custom Colors (if any) into the dialog */
219         if (rgbs != null) {
220                 int length = rgbs.length > CUSTOM_COLOR_COUNT ? CUSTOM_COLOR_COUNT : rgbs.length;
221                 for (int i=0; i<length; i++) {
222                         RGB rgb = rgbs [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;
227                 }
228                 for (int i=length; i < CUSTOM_COLOR_COUNT; i++) {
229                         colors[i] = 0x00FFFFFF;
230                 }
231                 OS.MoveMemory (display.lpCustColors, colors, CUSTOM_COLOR_COUNT * 4);
232         }
233
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;
242         if (rgb != null) {
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;
248         }
249
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);
255         }
256
257         display.externalEventLoop = true;
258         display.sendPreExternalEventDispatchEvent ();
259         /* Open the dialog */
260         boolean success = OS.ChooseColor (lpcc);
261         display.externalEventLoop = false;
262         display.sendPostExternalEventDispatchEvent ();
263
264         /* Clear the temporary dialog modal parent */
265         if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
266                 display.setModalDialog (oldModal);
267         }
268
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) {
274                         customColor = true;
275                         break;
276                 }
277         }
278         if (customColor) {
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);
286                 }
287         }
288
289         if (success) {
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);
294         }
295
296         /* Free the CCHookProc */
297         callback.dispose ();
298
299         /* Free the Custom Colors */
300         /*
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.
305         */
306 //      if (lpCustColors != 0) OS.HeapFree (hHeap, 0, lpCustColors);
307
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);
313         }
314
315         /*
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.
320         */
321 //      if (hwndOwner != 0) OS.UpdateWindow (hwndOwner);
322
323         display = null;
324         if (!success) return null;
325         return rgb;
326 }
327
328 /**
329  * Sets the receiver's selected color to be the argument.
330  *
331  * @param rgb the new RGB value for the selected color, may be
332  *        null to let the platform select a default when
333  *        open() is called
334  * @see PaletteData#getRGBs
335  */
336 public void setRGB (RGB rgb) {
337         this.rgb = rgb;
338 }
339
340 /**
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.
344  *
345  * @param rgbs the array of RGBs, which may be null
346  *
347  * @exception IllegalArgumentException <ul>
348  *    <li>ERROR_INVALID_ARGUMENT - if an RGB in the rgbs array is null</li>
349  * </ul>
350  *
351  * @since 3.8
352  */
353 public void setRGBs(RGB[] rgbs) {
354         if (rgbs != null) {
355                 for (int i=0; i<rgbs.length; i++) {
356                         if (rgbs [i] == null) error (SWT.ERROR_INVALID_ARGUMENT);
357                 }
358         }
359         this.rgbs = rgbs;
360 }
361
362 }