KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > ColorDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 package org.eclipse.swt.widgets;
12
13
14 import org.eclipse.swt.internal.*;
15 import org.eclipse.swt.internal.win32.*;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
18
19 /**
20  * Instances of this class allow the user to select a color
21  * from a predefined set of available colors.
22  * <dl>
23  * <dt><b>Styles:</b></dt>
24  * <dd>(none)</dd>
25  * <dt><b>Events:</b></dt>
26  * <dd>(none)</dd>
27  * </dl>
28  * <p>
29  * IMPORTANT: This class is intended to be subclassed <em>only</em>
30  * within the SWT implementation.
31  * </p>
32  */

33
34 public class ColorDialog extends Dialog {
35     Display display;
36     int width, height;
37     RGB rgb;
38     
39 /**
40  * Constructs a new instance of this class given only its parent.
41  *
42  * @param parent a composite control which will be the parent of the new instance
43  *
44  * @exception IllegalArgumentException <ul>
45  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
46  * </ul>
47  * @exception SWTException <ul>
48  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
49  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
50  * </ul>
51  *
52  * @see SWT
53  * @see Widget#checkSubclass
54  * @see Widget#getStyle
55  */

56 public ColorDialog (Shell parent) {
57     this (parent, SWT.PRIMARY_MODAL);
58 }
59
60 /**
61  * Constructs a new instance of this class given its parent
62  * and a style value describing its behavior and appearance.
63  * <p>
64  * The style value is either one of the style constants defined in
65  * class <code>SWT</code> which is applicable to instances of this
66  * class, or must be built by <em>bitwise OR</em>'ing together
67  * (that is, using the <code>int</code> "|" operator) two or more
68  * of those <code>SWT</code> style constants. The class description
69  * lists the style constants that are applicable to the class.
70  * Style bits are also inherited from superclasses.
71  * </p>
72  *
73  * @param parent a composite control which will be the parent of the new instance (cannot be null)
74  * @param style the style of control to construct
75  *
76  * @exception IllegalArgumentException <ul>
77  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
78  * </ul>
79  * @exception SWTException <ul>
80  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
81  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
82  * </ul>
83  *
84  * @see SWT
85  * @see Widget#checkSubclass
86  * @see Widget#getStyle
87  */

88 public ColorDialog (Shell parent, int style) {
89     super (parent, style);
90     checkSubclass ();
91 }
92
93 int CCHookProc (int hdlg, int uiMsg, int lParam, int lpData) {
94     switch (uiMsg) {
95         case OS.WM_INITDIALOG: {
96             RECT rect = new RECT ();
97             OS.GetWindowRect (hdlg, rect);
98             width = rect.right - rect.left;
99             height = rect.bottom - rect.top;
100             if (title != null && title.length () != 0) {
101                 /* Use the character encoding for the default locale */
102                 TCHAR buffer = new TCHAR (0, title, true);
103                 OS.SetWindowText (hdlg, buffer);
104             }
105             break;
106         }
107         case OS.WM_DESTROY: {
108             RECT rect = new RECT ();
109             OS.GetWindowRect (hdlg, rect);
110             int newWidth = rect.right - rect.left;
111             int newHeight = rect.bottom - rect.top;
112             if (newWidth < width || newHeight < height) {
113                 //display.fullOpen = false;
114
} else {
115                 if (newWidth > width || newHeight > height) {
116                     //display.fullOpen = true;
117
}
118             }
119             break;
120         }
121     }
122     return 0;
123 }
124
125 /**
126  * Returns the currently selected color in the receiver.
127  *
128  * @return the RGB value for the selected color, may be null
129  *
130  * @see PaletteData#getRGBs
131  */

132 public RGB getRGB () {
133     return rgb;
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  */

149 public RGB open () {
150     
151     /* Get the owner HWND for the dialog */
152     int hwndOwner = parent.handle;
153
154     /* Create the CCHookProc */
155     Callback callback = new Callback (this, "CCHookProc", 4); //$NON-NLS-1$
156
int lpfnHook = callback.getAddress ();
157     if (lpfnHook == 0) SWT.error(SWT.ERROR_NO_MORE_CALLBACKS);
158     
159     /* Allocate the Custom Colors */
160     display = parent.display;
161     if (display.lpCustColors == 0) {
162         int hHeap = OS.GetProcessHeap ();
163         display.lpCustColors = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, 16 * 4);
164     }
165     
166     /* Open the dialog */
167     CHOOSECOLOR lpcc = new CHOOSECOLOR ();
168     lpcc.lStructSize = CHOOSECOLOR.sizeof;
169     lpcc.Flags = OS.CC_ANYCOLOR | OS.CC_ENABLEHOOK;
170     //if (display.fullOpen) lpcc.Flags |= OS.CC_FULLOPEN;
171
lpcc.lpfnHook = lpfnHook;
172     lpcc.hwndOwner = hwndOwner;
173     lpcc.lpCustColors = display.lpCustColors;
174     if (rgb != null) {
175         lpcc.Flags |= OS.CC_RGBINIT;
176         int red = rgb.red & 0xFF;
177         int green = (rgb.green << 8) & 0xFF00;
178         int blue = (rgb.blue << 16) & 0xFF0000;
179         lpcc.rgbResult = red | green | blue;
180     }
181     
182     /* Make the parent shell be temporary modal */
183     Shell oldModal = null;
184     if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
185         oldModal = display.getModalDialogShell ();
186         display.setModalDialogShell (parent);
187     }
188     
189     /* Open the dialog */
190     boolean success = OS.ChooseColor (lpcc);
191     
192     /* Clear the temporary dialog modal parent */
193     if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
194         display.setModalDialogShell (oldModal);
195     }
196     
197     if (success) {
198         int red = lpcc.rgbResult & 0xFF;
199         int green = (lpcc.rgbResult >> 8) & 0xFF;
200         int blue = (lpcc.rgbResult >> 16) & 0xFF;
201         rgb = new RGB (red, green, blue);
202     }
203     
204     /* Free the CCHookProc */
205     callback.dispose ();
206     
207     /* Free the Custom Colors */
208     /*
209     * This code is intentionally commented. Currently,
210     * there is exactly one set of custom colors per display.
211     * The memory associated with these colors is released
212     * when the display is disposed.
213     */

214 // if (lpCustColors != 0) OS.HeapFree (hHeap, 0, lpCustColors);
215

216     /*
217     * This code is intentionally commented. On some
218     * platforms, the owner window is repainted right
219     * away when a dialog window exits. This behavior
220     * is currently unspecified.
221     */

222 // if (hwndOwner != 0) OS.UpdateWindow (hwndOwner);
223

224     if (!success) return null;
225     return rgb;
226 }
227
228 /**
229  * Sets the receiver's selected color to be the argument.
230  *
231  * @param rgb the new RGB value for the selected color, may be
232  * null to let the platform select a default when
233  * open() is called
234  * @see PaletteData#getRGBs
235  */

236 public void setRGB (RGB rgb) {
237     this.rgb = rgb;
238 }
239
240 }
241
Popular Tags