Source code: org/eclipse/swt/widgets/ColorDialog.java
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 Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-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.*;
15 import org.eclipse.swt.internal.*;
16 import org.eclipse.swt.internal.gtk.*;
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 public class ColorDialog extends Dialog {
34 RGB rgb;
35 /**
36 * Constructs a new instance of this class given only its parent.
37 *
38 * @param parent a composite control which will be the parent of the new instance
39 *
40 * @exception IllegalArgumentException <ul>
41 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
42 * </ul>
43 * @exception SWTException <ul>
44 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
45 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
46 * </ul>
47 *
48 * @see SWT
49 * @see Widget#checkSubclass
50 * @see Widget#getStyle
51 */
52 public ColorDialog (Shell parent) {
53 this (parent, SWT.NULL);
54 }
55 /**
56 * Constructs a new instance of this class given its parent
57 * and a style value describing its behavior and appearance.
58 * <p>
59 * The style value is either one of the style constants defined in
60 * class <code>SWT</code> which is applicable to instances of this
61 * class, or must be built by <em>bitwise OR</em>'ing together
62 * (that is, using the <code>int</code> "|" operator) two or more
63 * of those <code>SWT</code> style constants. The class description
64 * lists the style constants that are applicable to the class.
65 * Style bits are also inherited from superclasses.
66 * </p>
67 *
68 * @param parent a composite control which will be the parent of the new instance (cannot be null)
69 * @param style the style of control to construct
70 *
71 * @exception IllegalArgumentException <ul>
72 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
73 * </ul>
74 * @exception SWTException <ul>
75 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
76 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
77 * </ul>
78 *
79 * @see SWT
80 * @see Widget#checkSubclass
81 * @see Widget#getStyle
82 */
83 public ColorDialog (Shell parent, int style) {
84 super (parent, style);
85 checkSubclass ();
86 }
87
88 /**
89 * Returns the currently selected color in the receiver.
90 *
91 * @return the RGB value for the selected color, may be null
92 *
93 * @see PaletteData#getRGBs
94 */
95 public RGB getRGB () {
96 return rgb;
97 }
98 /**
99 * Makes the receiver visible and brings it to the front
100 * of the display.
101 *
102 * @return the selected color, or null if the dialog was
103 * cancelled, no color was selected, or an error
104 * occurred
105 *
106 * @exception SWTException <ul>
107 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
108 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
109 * </ul>
110 */
111 public RGB open () {
112 byte [] buffer = Converter.wcsToMbcs (null, title, true);
113 long /*int*/ handle = OS.gtk_color_selection_dialog_new (buffer);
114 if (parent!=null) {
115 OS.gtk_window_set_transient_for(handle, parent.topHandle());
116 }
117 GtkColorSelectionDialog dialog = new GtkColorSelectionDialog ();
118 OS.memmove(dialog, handle);
119 GdkColor color = new GdkColor();
120 if (rgb != null) {
121 color.red = (short)((rgb.red & 0xFF) | ((rgb.red & 0xFF) << 8));
122 color.green = (short)((rgb.green & 0xFF) | ((rgb.green & 0xFF) << 8));
123 color.blue = (short)((rgb.blue & 0xFF) | ((rgb.blue & 0xFF) << 8));
124 OS.gtk_color_selection_set_current_color (dialog.colorsel, color);
125 }
126 int response = OS.gtk_dialog_run(handle);
127 boolean success = response == OS.GTK_RESPONSE_OK;
128 if (success) {
129 OS.gtk_color_selection_get_current_color (dialog.colorsel, color);
130 int red = (color.red >> 8) & 0xFF;
131 int green = (color.green >> 8) & 0xFF;
132 int blue = (color.blue >> 8) & 0xFF;
133 rgb = new RGB (red, green, blue);
134 }
135 OS.gtk_widget_destroy(handle);
136 if (!success) return null;
137 return rgb;
138 }
139 /**
140 * Sets the receiver's selected color to be the argument.
141 *
142 * @param rgb the new RGB value for the selected color, may be
143 * null to let the platform select a default when
144 * open() is called
145 * @see PaletteData#getRGBs
146 */
147 public void setRGB (RGB rgb) {
148 this.rgb = rgb;
149 }
150 }