| 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 dwt.widgets.ColorDialog; |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
import dwt.DWT; |
|---|
| 18 |
import dwt.DWTException; |
|---|
| 19 |
import dwt.graphics.PaletteData; |
|---|
| 20 |
import dwt.graphics.RGB; |
|---|
| 21 |
import dwt.internal.win32.OS; |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
import dwt.widgets.Dialog; |
|---|
| 25 |
import dwt.widgets.Shell; |
|---|
| 26 |
import dwt.widgets.Display; |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
/** |
|---|
| 30 |
* Instances of this class allow the user to select a color |
|---|
| 31 |
* from a predefined set of available colors. |
|---|
| 32 |
* <dl> |
|---|
| 33 |
* <dt><b>Styles:</b></dt> |
|---|
| 34 |
* <dd>(none)</dd> |
|---|
| 35 |
* <dt><b>Events:</b></dt> |
|---|
| 36 |
* <dd>(none)</dd> |
|---|
| 37 |
* </dl> |
|---|
| 38 |
* <p> |
|---|
| 39 |
* IMPORTANT: This class is intended to be subclassed <em>only</em> |
|---|
| 40 |
* within the DWT implementation. |
|---|
| 41 |
* </p> |
|---|
| 42 |
* |
|---|
| 43 |
* @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample, Dialog tab</a> |
|---|
| 44 |
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> |
|---|
| 45 |
*/ |
|---|
| 46 |
|
|---|
| 47 |
public class ColorDialog : Dialog { |
|---|
| 48 |
Display display; |
|---|
| 49 |
int width, height; |
|---|
| 50 |
RGB rgb; |
|---|
| 51 |
private static ColorDialog sThis; |
|---|
| 52 |
|
|---|
| 53 |
/** |
|---|
| 54 |
* Constructs a new instance of this class given only its parent. |
|---|
| 55 |
* |
|---|
| 56 |
* @param parent a composite control which will be the parent of the new instance |
|---|
| 57 |
* |
|---|
| 58 |
* @exception IllegalArgumentException <ul> |
|---|
| 59 |
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
|---|
| 60 |
* </ul> |
|---|
| 61 |
* @exception DWTException <ul> |
|---|
| 62 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> |
|---|
| 63 |
* <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> |
|---|
| 64 |
* </ul> |
|---|
| 65 |
* |
|---|
| 66 |
* @see DWT |
|---|
| 67 |
* @see Widget#checkSubclass |
|---|
| 68 |
* @see Widget#getStyle |
|---|
| 69 |
*/ |
|---|
| 70 |
public this (Shell parent) { |
|---|
| 71 |
this (parent, DWT.APPLICATION_MODAL); |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
/** |
|---|
| 75 |
* Constructs a new instance of this class given its parent |
|---|
| 76 |
* and a style value describing its behavior and appearance. |
|---|
| 77 |
* <p> |
|---|
| 78 |
* The style value is either one of the style constants defined in |
|---|
| 79 |
* class <code>DWT</code> which is applicable to instances of this |
|---|
| 80 |
* class, or must be built by <em>bitwise OR</em>'ing together |
|---|
| 81 |
* (that is, using the <code>int</code> "|" operator) two or more |
|---|
| 82 |
* of those <code>DWT</code> style constants. The class description |
|---|
| 83 |
* lists the style constants that are applicable to the class. |
|---|
| 84 |
* Style bits are also inherited from superclasses. |
|---|
| 85 |
* </p> |
|---|
| 86 |
* |
|---|
| 87 |
* @param parent a composite control which will be the parent of the new instance (cannot be null) |
|---|
| 88 |
* @param style the style of control to construct |
|---|
| 89 |
* |
|---|
| 90 |
* @exception IllegalArgumentException <ul> |
|---|
| 91 |
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
|---|
| 92 |
* </ul> |
|---|
| 93 |
* @exception DWTException <ul> |
|---|
| 94 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> |
|---|
| 95 |
* <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> |
|---|
| 96 |
* </ul> |
|---|
| 97 |
* |
|---|
| 98 |
* @see DWT |
|---|
| 99 |
* @see Widget#checkSubclass |
|---|
| 100 |
* @see Widget#getStyle |
|---|
| 101 |
*/ |
|---|
| 102 |
public this (Shell parent, int style) { |
|---|
| 103 |
super (parent, checkStyle (parent, style)); |
|---|
| 104 |
checkSubclass (); |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
private static extern(Windows) uint CCHookFunc (HWND hdlg, uint uiMsg, uint lParam, int lpData) { |
|---|
| 108 |
return sThis.CCHookProc( hdlg, uiMsg, lParam ); |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
int CCHookProc (HWND hdlg, uint uiMsg, uint lParam ) { |
|---|
| 112 |
switch (uiMsg) { |
|---|
| 113 |
case OS.WM_INITDIALOG: { |
|---|
| 114 |
RECT rect; |
|---|
| 115 |
OS.GetWindowRect (hdlg, &rect); |
|---|
| 116 |
width = rect.right - rect.left; |
|---|
| 117 |
height = rect.bottom - rect.top; |
|---|
| 118 |
if (title !is null && title.length !is 0) { |
|---|
| 119 |
/* Use the character encoding for the default locale */ |
|---|
| 120 |
OS.SetWindowText (hdlg, StrToTCHARz(title)); |
|---|
| 121 |
} |
|---|
| 122 |
break; |
|---|
| 123 |
} |
|---|
| 124 |
case OS.WM_DESTROY: { |
|---|
| 125 |
RECT rect; |
|---|
| 126 |
OS.GetWindowRect (hdlg, &rect); |
|---|
| 127 |
int newWidth = rect.right - rect.left; |
|---|
| 128 |
int newHeight = rect.bottom - rect.top; |
|---|
| 129 |
if (newWidth < width || newHeight < height) { |
|---|
| 130 |
//display.fullOpen = false; |
|---|
| 131 |
} else { |
|---|
| 132 |
if (newWidth > width || newHeight > height) { |
|---|
| 133 |
//display.fullOpen = true; |
|---|
| 134 |
} |
|---|
| 135 |
} |
|---|
| 136 |
break; |
|---|
| 137 |
} |
|---|
| 138 |
default: |
|---|
| 139 |
} |
|---|
| 140 |
return 0; |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
/** |
|---|
| 144 |
* Returns the currently selected color in the receiver. |
|---|
| 145 |
* |
|---|
| 146 |
* @return the RGB value for the selected color, may be null |
|---|
| 147 |
* |
|---|
| 148 |
* @see PaletteData#getRGBs |
|---|
| 149 |
*/ |
|---|
| 150 |
public RGB getRGB () { |
|---|
| 151 |
return rgb; |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
/** |
|---|
| 155 |
* Makes the receiver visible and brings it to the front |
|---|
| 156 |
* of the display. |
|---|
| 157 |
* |
|---|
| 158 |
* @return the selected color, or null if the dialog was |
|---|
| 159 |
* cancelled, no color was selected, or an error |
|---|
| 160 |
* occurred |
|---|
| 161 |
* |
|---|
| 162 |
* @exception DWTException <ul> |
|---|
| 163 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
|---|
| 164 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
|---|
| 165 |
* </ul> |
|---|
| 166 |
*/ |
|---|
| 167 |
public RGB open () { |
|---|
| 168 |
|
|---|
| 169 |
/* Get the owner HWND for the dialog */ |
|---|
| 170 |
auto hwndOwner = parent.handle; |
|---|
| 171 |
auto hwndParent = parent.handle; |
|---|
| 172 |
|
|---|
| 173 |
/* |
|---|
| 174 |
* Feature in Windows. There is no API to set the orientation of a |
|---|
| 175 |
* color dialog. It is always inherited from the parent. The fix is |
|---|
| 176 |
* to create a hidden parent and set the orientation in the hidden |
|---|
| 177 |
* parent for the dialog to inherit. |
|---|
| 178 |
*/ |
|---|
| 179 |
bool enabled = false; |
|---|
| 180 |
if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(4, 10)) { |
|---|
| 181 |
int dialogOrientation = style & (DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT); |
|---|
| 182 |
int parentOrientation = parent.style & (DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT); |
|---|
| 183 |
if (dialogOrientation !is parentOrientation) { |
|---|
| 184 |
int exStyle = OS.WS_EX_NOINHERITLAYOUT; |
|---|
| 185 |
if (dialogOrientation is DWT.RIGHT_TO_LEFT) exStyle |= OS.WS_EX_LAYOUTRTL; |
|---|
| 186 |
hwndOwner = OS.CreateWindowEx ( |
|---|
| 187 |
exStyle, |
|---|
| 188 |
Shell.DialogClass.ptr, |
|---|
| 189 |
null, |
|---|
| 190 |
0, |
|---|
| 191 |
OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0, |
|---|
| 192 |
hwndParent, |
|---|
| 193 |
null, |
|---|
| 194 |
OS.GetModuleHandle (null), |
|---|
| 195 |
null); |
|---|
| 196 |
enabled = OS.IsWindowEnabled (hwndParent) !is 0; |
|---|
| 197 |
if (enabled) OS.EnableWindow (hwndParent, false); |
|---|
| 198 |
} |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
/* Create the CCHookProc */ |
|---|
| 202 |
//Callback callback = new Callback (this, "CCHookProc", 4); //$NON-NLS-1$ |
|---|
| 203 |
//int lpfnHook = callback.getAddress (); |
|---|
| 204 |
//if (lpfnHook is 0) DWT.error(DWT.ERROR_NO_MORE_CALLBACKS); |
|---|
| 205 |
|
|---|
| 206 |
/* Allocate the Custom Colors */ |
|---|
| 207 |
display = parent.display; |
|---|
| 208 |
if (display.lpCustColors is null) { |
|---|
| 209 |
auto hHeap = OS.GetProcessHeap (); |
|---|
| 210 |
display.lpCustColors = cast(uint*)OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, 16 * 4); |
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
/* Open the dialog */ |
|---|
| 214 |
CHOOSECOLOR lpcc; |
|---|
| 215 |
lpcc.lCustData = cast(int)cast(void*)this; |
|---|
| 216 |
lpcc.lStructSize = CHOOSECOLOR.sizeof; |
|---|
| 217 |
lpcc.Flags = OS.CC_ANYCOLOR | OS.CC_ENABLEHOOK; |
|---|
| 218 |
//if (display.fullOpen) lpcc.Flags |= OS.CC_FULLOPEN; |
|---|
| 219 |
lpcc.lpfnHook = &CCHookFunc; |
|---|
| 220 |
lpcc.hwndOwner = hwndOwner; |
|---|
| 221 |
lpcc.lpCustColors = display.lpCustColors; |
|---|
| 222 |
if (rgb !is null) { |
|---|
| 223 |
lpcc.Flags |= OS.CC_RGBINIT; |
|---|
| 224 |
int red = rgb.red & 0xFF; |
|---|
| 225 |
int green = (rgb.green << 8) & 0xFF00; |
|---|
| 226 |
int blue = (rgb.blue << 16) & 0xFF0000; |
|---|
| 227 |
lpcc.rgbResult = red | green | blue; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
/* Make the parent shell be temporary modal */ |
|---|
| 231 |
Dialog oldModal = null; |
|---|
| 232 |
if ((style & (DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) !is 0) { |
|---|
| 233 |
oldModal = display.getModalDialog (); |
|---|
| 234 |
display.setModalDialog (this); |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
/* Open the dialog */ |
|---|
| 238 |
bool success; |
|---|
| 239 |
synchronized { |
|---|
| 240 |
sThis = this; |
|---|
| 241 |
success = cast(bool) OS.ChooseColor (&lpcc); |
|---|
| 242 |
sThis = null; |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
/* Clear the temporary dialog modal parent */ |
|---|
| 246 |
if ((style & (DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) !is 0) { |
|---|
| 247 |
display.setModalDialog (oldModal); |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
if (success) { |
|---|
| 251 |
int red = lpcc.rgbResult & 0xFF; |
|---|
| 252 |
int green = (lpcc.rgbResult >> 8) & 0xFF; |
|---|
| 253 |
int blue = (lpcc.rgbResult >> 16) & 0xFF; |
|---|
| 254 |
rgb = new RGB (red, green, blue); |
|---|
| 255 |
} |
|---|
| 256 |
|
|---|
| 257 |
/* Free the CCHookProc */ |
|---|
| 258 |
//callback.dispose (); |
|---|
| 259 |
|
|---|
| 260 |
/* Free the Custom Colors */ |
|---|
| 261 |
/* |
|---|
| 262 |
* This code is intentionally commented. Currently, |
|---|
| 263 |
* there is exactly one set of custom colors per display. |
|---|
| 264 |
* The memory associated with these colors is released |
|---|
| 265 |
* when the display is disposed. |
|---|
| 266 |
*/ |
|---|
| 267 |
// if (lpCustColors !is 0) OS.HeapFree (hHeap, 0, lpCustColors); |
|---|
| 268 |
|
|---|
| 269 |
/* Destroy the BIDI orientation window */ |
|---|
| 270 |
if (hwndParent !is hwndOwner) { |
|---|
| 271 |
if (enabled) OS.EnableWindow (hwndParent, true); |
|---|
| 272 |
OS.SetActiveWindow (hwndParent); |
|---|
| 273 |
OS.DestroyWindow (hwndOwner); |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
/* |
|---|
| 277 |
* This code is intentionally commented. On some |
|---|
| 278 |
* platforms, the owner window is repainted right |
|---|
| 279 |
* away when a dialog window exits. This behavior |
|---|
| 280 |
* is currently unspecified. |
|---|
| 281 |
*/ |
|---|
| 282 |
// if (hwndOwner !is 0) OS.UpdateWindow (hwndOwner); |
|---|
| 283 |
|
|---|
| 284 |
display = null; |
|---|
| 285 |
if (!success) return null; |
|---|
| 286 |
return rgb; |
|---|
| 287 |
} |
|---|
| 288 |
|
|---|
| 289 |
/** |
|---|
| 290 |
* Sets the receiver's selected color to be the argument. |
|---|
| 291 |
* |
|---|
| 292 |
* @param rgb the new RGB value for the selected color, may be |
|---|
| 293 |
* null to let the platform select a default when |
|---|
| 294 |
* open() is called |
|---|
| 295 |
* @see PaletteData#getRGBs |
|---|
| 296 |
*/ |
|---|
| 297 |
public void setRGB (RGB rgb) { |
|---|
| 298 |
this.rgb = rgb; |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
} |
|---|