1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module org.eclipse.swt.widgets.ColorDialog;
14 
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.SWTException;
17 import org.eclipse.swt.graphics.PaletteData;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.internal.gtk.OS;
20 import org.eclipse.swt.widgets.Dialog;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.swt.widgets.Display;
23 
24 import java.lang.all;
25 
26 /**
27  * Instances of this class allow the user to select a color
28  * from a predefined set of available colors.
29  * <dl>
30  * <dt><b>Styles:</b></dt>
31  * <dd>(none)</dd>
32  * <dt><b>Events:</b></dt>
33  * <dd>(none)</dd>
34  * </dl>
35  * <p>
36  * IMPORTANT: This class is intended to be subclassed <em>only</em>
37  * within the SWT implementation.
38  * </p>
39  * 
40  * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample, Dialog tab</a>
41  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
42  */
43 public class ColorDialog : Dialog {
44     RGB rgb;
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  */
62 public this (Shell parent) {
63     this (parent, SWT.APPLICATION_MODAL);
64 }
65 /**
66  * Constructs a new instance of this class given its parent
67  * and a style value describing its behavior and appearance.
68  * <p>
69  * The style value is either one of the style constants defined in
70  * class <code>SWT</code> which is applicable to instances of this
71  * class, or must be built by <em>bitwise OR</em>'ing together
72  * (that is, using the <code>int</code> "|" operator) two or more
73  * of those <code>SWT</code> style constants. The class description
74  * lists the style constants that are applicable to the class.
75  * Style bits are also inherited from superclasses.
76  * </p>
77  *
78  * @param parent a composite control which will be the parent of the new instance (cannot be null)
79  * @param style the style of control to construct
80  *
81  * @exception IllegalArgumentException <ul>
82  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
83  * </ul>
84  * @exception SWTException <ul>
85  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
86  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
87  * </ul>
88  *
89  * @see SWT
90  * @see Widget#checkSubclass
91  * @see Widget#getStyle
92  */
93 public this (Shell parent, int style) {
94     super (parent, checkStyle (parent, style));
95     checkSubclass ();
96 }
97 
98 /**
99  * Returns the currently selected color in the receiver.
100  *
101  * @return the RGB value for the selected color, may be null
102  *
103  * @see PaletteData#getRGBs
104  */
105 public RGB getRGB () {
106     return rgb;
107 }
108 /**
109  * Makes the receiver visible and brings it to the front
110  * of the display.
111  *
112  * @return the selected color, or null if the dialog was
113  *         cancelled, no color was selected, or an error
114  *         occurred
115  *
116  * @exception SWTException <ul>
117  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
118  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
119  * </ul>
120  */
121 public RGB open () {
122     char* buffer = toStringz(title);
123     auto handle = cast(GtkWidget*)OS.gtk_color_selection_dialog_new (buffer);
124     Display display = parent !is null ? parent.getDisplay (): Display.getCurrent ();
125     if (parent !is null) {
126         auto shellHandle = parent.topHandle ();
127         OS.gtk_window_set_transient_for (handle, shellHandle);
128         auto pixbufs = OS.gtk_window_get_icon_list (shellHandle);
129         if (pixbufs !is null) {
130             OS.gtk_window_set_icon_list (handle, pixbufs);
131             OS.g_list_free (pixbufs);
132         }
133     }
134     GtkColorSelectionDialog* dialog = cast(GtkColorSelectionDialog*)handle;
135     GdkColor color;
136     if (rgb !is null) {
137         color.red = cast(short)((rgb.red & 0xFF) | ((rgb.red & 0xFF) << 8));
138         color.green = cast(short)((rgb.green & 0xFF) | ((rgb.green & 0xFF) << 8));
139         color.blue = cast(short)((rgb.blue & 0xFF) | ((rgb.blue & 0xFF) << 8));
140         OS.gtk_color_selection_set_current_color (dialog.colorsel, &color);
141     }
142     OS.gtk_color_selection_set_has_palette (dialog.colorsel, true);
143     display.addIdleProc ();
144     Dialog oldModal = null;
145     if (OS.gtk_window_get_modal (handle)) {
146         oldModal = display.getModalDialog ();
147         display.setModalDialog (this);
148     }
149     int signalId = 0;
150     ptrdiff_t hookId = 0;
151     CallbackData emissionData;
152     emissionData.display = display;
153     emissionData.data = handle;
154     if ((style & SWT.RIGHT_TO_LEFT) !is 0) {
155         signalId = OS.g_signal_lookup (OS.map.ptr, OS.GTK_TYPE_WIDGET());
156         hookId = OS.g_signal_add_emission_hook (signalId, 0, &Display.emissionFunc, &emissionData, null);
157     }
158     int response = OS.gtk_dialog_run (handle);
159     if ((style & SWT.RIGHT_TO_LEFT) !is 0) {
160         OS.g_signal_remove_emission_hook (signalId, hookId);
161     }
162     if (OS.gtk_window_get_modal (handle)) {
163         display.setModalDialog (oldModal);
164     }
165     bool success = response is OS.GTK_RESPONSE_OK;
166     if (success) {
167         OS.gtk_color_selection_get_current_color (dialog.colorsel, &color);
168         int red = (color.red >> 8) & 0xFF;
169         int green = (color.green >> 8) & 0xFF;
170         int blue = (color.blue >> 8) & 0xFF;
171         rgb = new RGB (red, green, blue);
172     }
173     display.removeIdleProc ();
174     OS.gtk_widget_destroy (handle);
175     if (!success) return null;
176     return rgb;
177 }
178 /**
179  * Sets the receiver's selected color to be the argument.
180  *
181  * @param rgb the new RGB value for the selected color, may be
182  *        null to let the platform select a default when
183  *        open() is called
184  * @see PaletteData#getRGBs
185  */
186 public void setRGB (RGB rgb) {
187     this.rgb = rgb;
188 }
189 }