root/dwt/widgets/Button.d

Revision 249:b3dbd786541a, 52.7 kB (checked in by Frank Benoit <benoit@tionex.de>, 5 months ago)

Fix: compile errors

Line 
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.Button;
14
15 import dwt.DWT;
16 import dwt.DWTException;
17 import dwt.events.SelectionEvent;
18 import dwt.events.SelectionListener;
19 import dwt.graphics.GC;
20 import dwt.graphics.Image;
21 import dwt.graphics.ImageData;
22 import dwt.graphics.Point;
23 import dwt.graphics.Rectangle;
24 import dwt.internal.ImageList;
25 import dwt.internal.win32.OS;
26
27 import dwt.widgets.Control;
28 import dwt.widgets.Composite;
29 import dwt.widgets.TypedListener;
30
31 import dwt.dwthelper.utils;
32
33 /**
34  * Instances of this class represent a selectable user interface object that
35  * issues notification when pressed and released.
36  * <dl>
37  * <dt><b>Styles:</b></dt>
38  * <dd>ARROW, CHECK, PUSH, RADIO, TOGGLE, FLAT</dd>
39  * <dd>UP, DOWN, LEFT, RIGHT, CENTER</dd>
40  * <dt><b>Events:</b></dt>
41  * <dd>Selection</dd>
42  * </dl>
43  * <p>
44  * Note: Only one of the styles ARROW, CHECK, PUSH, RADIO, and TOGGLE
45  * may be specified.
46  * </p><p>
47  * Note: Only one of the styles LEFT, RIGHT, and CENTER may be specified.
48  * </p><p>
49  * Note: Only one of the styles UP, DOWN, LEFT, and RIGHT may be specified
50  * when the ARROW style is specified.
51  * </p><p>
52  * IMPORTANT: This class is intended to be subclassed <em>only</em>
53  * within the DWT implementation.
54  * </p>
55  *
56  * @see <a href="http://www.eclipse.org/swt/snippets/#button">Button snippets</a>
57  * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample</a>
58  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
59  */
60
61 public class Button : Control {
62
63     alias Control.computeSize computeSize;
64     alias Control.windowProc windowProc;
65
66     alias extern(Windows) int function( HWND, uint, uint, int ) TWindowProc;
67     String text = "", message = "";
68     Image image, image2, disabledImage;
69     ImageList imageList;
70     bool ignoreMouse, grayed;
71     static const int MARGIN = 4;
72     private static /+const+/ int CHECK_WIDTH, CHECK_HEIGHT;
73     static const int ICON_WIDTH = 128, ICON_HEIGHT = 128;
74     static const bool COMMAND_LINK = false;
75     private static /+const+/ TWindowProc ButtonProc;
76     static const TCHAR[] ButtonClass = "BUTTON\0";
77
78     private static bool static_this_completed = false;
79     private static void static_this() {
80         if( static_this_completed ){
81             return;
82         }
83         synchronized {
84             if( static_this_completed ){
85                 return;
86             }
87             auto hBitmap = OS.LoadBitmap (null, OS.OBM_CHECKBOXES);
88             if (hBitmap is null) {
89                 CHECK_WIDTH = OS.GetSystemMetrics (OS.IsWinCE ? OS.SM_CXSMICON : OS.SM_CXVSCROLL);
90                 CHECK_HEIGHT = OS.GetSystemMetrics (OS.IsWinCE ? OS.SM_CYSMICON : OS.SM_CYVSCROLL);
91             } else {
92                 BITMAP bitmap;
93                 OS.GetObject (hBitmap, BITMAP.sizeof, &bitmap);
94                 OS.DeleteObject (hBitmap);
95                 CHECK_WIDTH = bitmap.bmWidth / 4;
96                 CHECK_HEIGHT =  bitmap.bmHeight / 3;
97             }
98             WNDCLASS lpWndClass;
99             OS.GetClassInfo (null, ButtonClass.ptr, &lpWndClass);
100             ButtonProc = lpWndClass.lpfnWndProc;
101             static_this_completed = true;
102         }
103     }
104
105 /**
106  * Constructs a new instance of this class given its parent
107  * and a style value describing its behavior and appearance.
108  * <p>
109  * The style value is either one of the style constants defined in
110  * class <code>DWT</code> which is applicable to instances of this
111  * class, or must be built by <em>bitwise OR</em>'ing together
112  * (that is, using the <code>int</code> "|" operator) two or more
113  * of those <code>DWT</code> style constants. The class description
114  * lists the style constants that are applicable to the class.
115  * Style bits are also inherited from superclasses.
116  * </p>
117  *
118  * @param parent a composite control which will be the parent of the new instance (cannot be null)
119  * @param style the style of control to construct
120  *
121  * @exception IllegalArgumentException <ul>
122  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
123  * </ul>
124  * @exception DWTException <ul>
125  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
126  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
127  * </ul>
128  *
129  * @see DWT#ARROW
130  * @see DWT#CHECK
131  * @see DWT#PUSH
132  * @see DWT#RADIO
133  * @see DWT#TOGGLE
134  * @see DWT#FLAT
135  * @see DWT#LEFT
136  * @see DWT#RIGHT
137  * @see DWT#CENTER
138  * @see Widget#checkSubclass
139  * @see Widget#getStyle
140  */
141 public this (Composite parent, int style) {
142     static_this();
143     super (parent, checkStyle (style));
144 }
145
146 void _setImage (Image image) {
147     if ((style & DWT.COMMAND) !is 0) return;
148     if (OS.COMCTL32_MAJOR >= 6) {
149         if (imageList !is null) imageList.dispose ();
150         imageList = null;
151         if (image !is null) {
152             imageList = new ImageList (style & DWT.RIGHT_TO_LEFT);
153             if (OS.IsWindowEnabled (handle)) {
154                 imageList.add (image);
155             } else {
156                 if (disabledImage !is null) disabledImage.dispose ();
157                 disabledImage = new Image (display, image, DWT.IMAGE_DISABLE);
158                 imageList.add (disabledImage);
159             }
160             BUTTON_IMAGELIST buttonImageList;
161             buttonImageList.himl = imageList.getHandle ();
162             int oldBits = OS.GetWindowLong (handle, OS.GWL_STYLE), newBits = oldBits;
163             newBits &= ~(OS.BS_LEFT | OS.BS_CENTER | OS.BS_RIGHT);
164             if ((style & DWT.LEFT) !is 0) newBits |= OS.BS_LEFT;
165             if ((style & DWT.CENTER) !is 0) newBits |= OS.BS_CENTER;
166             if ((style & DWT.RIGHT) !is 0) newBits |= OS.BS_RIGHT;
167             if (text.length is 0) {
168                 if ((style & DWT.LEFT) !is 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_LEFT;
169                 if ((style & DWT.CENTER) !is 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_CENTER;
170                 if ((style & DWT.RIGHT) !is 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_RIGHT;
171             } else {
172                 buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_LEFT;
173                 buttonImageList.margin.left = computeLeftMargin ();
174                 buttonImageList.margin.right = MARGIN;
175                 newBits &= ~(OS.BS_CENTER | OS.BS_RIGHT);
176                 newBits |= OS.BS_LEFT;
177             }
178             if (newBits !is oldBits) {
179                 OS.SetWindowLong (handle, OS.GWL_STYLE, newBits);
180                 OS.InvalidateRect (handle, null, true);
181             }
182             OS.SendMessage (handle, OS.BCM_SETIMAGELIST, 0, &buttonImageList);
183         } else {
184             OS.SendMessage (handle, OS.BCM_SETIMAGELIST, 0, 0);
185         }
186         /*
187         * Bug in Windows.  Under certain cirumstances yet to be
188         * isolated, BCM_SETIMAGELIST does not redraw the control
189         * when a new image is set.  The fix is to force a redraw.
190         */
191         OS.InvalidateRect (handle, null, true);
192     } else {
193         if (image2 !is null) image2.dispose ();
194         image2 = null;
195         HBITMAP hImage;
196         int imageBits = 0, fImageType = 0;
197         if (image !is null) {
198             switch (image.type) {
199                 case DWT.BITMAP: {
200                     Rectangle rect = image.getBounds ();
201                     ImageData data = image.getImageData ();
202                     switch (data.getTransparencyType ()) {
203                         case DWT.TRANSPARENCY_PIXEL:
204                             if (rect.width <= ICON_WIDTH && rect.height <= ICON_HEIGHT) {
205                                 image2 = new Image (display, data, data.getTransparencyMask ());
206                                 hImage = image2.handle;
207                                 imageBits = OS.BS_ICON;
208                                 fImageType = OS.IMAGE_ICON;
209                                 break;
210                             }
211                             //FALL THROUGH
212                         case DWT.TRANSPARENCY_ALPHA:
213                             image2 = new Image (display, rect.width, rect.height);
214                             GC gc = new GC (image2);
215                             gc.setBackground (getBackground ());
216                             gc.fillRectangle (rect);
217                             gc.drawImage (image, 0, 0);
218                             gc.dispose ();
219                             hImage = image2.handle;
220                             imageBits = OS.BS_BITMAP;
221                             fImageType = OS.IMAGE_BITMAP;
222                             break;
223                         case DWT.TRANSPARENCY_NONE:
224                             hImage = image.handle;
225                             imageBits = OS.BS_BITMAP;
226                             fImageType = OS.IMAGE_BITMAP;
227                             break;
228                         default:
229                     }
230                     break;
231                 }
232                 case DWT.ICON: {
233                     hImage = image.handle;
234                     imageBits = OS.BS_ICON;
235                     fImageType = OS.IMAGE_ICON;
236                     break;
237                 }
238                 default:
239             }
240             /*
241             * Feature in Windows.  The button control mirrors its image when the
242             * flag WS_EX_LAYOUTRTL is set. This behaviour is not desirable in DWT.
243             * The fix is to set a mirrored version of real image in the button.
244             */
245             if ((style & DWT.RIGHT_TO_LEFT) !is 0) {
246                 if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (4, 10)) {
247                     Rectangle rect = image.getBounds ();
248                     auto hDC = OS.GetDC (handle);
249                     auto dstHdc = OS.CreateCompatibleDC (hDC);
250                     auto hBitmap = OS.CreateCompatibleBitmap (hDC, rect.width, rect.height);
251                     auto oldBitmap = OS.SelectObject (dstHdc, hBitmap);
252                     OS.SetLayout (dstHdc, OS.LAYOUT_RTL);
253                     if (fImageType is OS.IMAGE_BITMAP) {
254                         auto srcHdc = OS.CreateCompatibleDC (hDC);
255                         auto oldSrcBitmap = OS.SelectObject (srcHdc, hImage);
256                         OS.SetLayout (dstHdc, 0);
257                         OS.BitBlt (dstHdc, 0, 0, rect.width, rect.height, srcHdc, 0, 0, OS.SRCCOPY);
258                         OS.SelectObject (srcHdc, oldSrcBitmap);
259                         OS.DeleteDC (srcHdc);
260                     } else {
261                         Control control = findBackgroundControl ();
262                         if (control is null) control = this;
263                         auto newBrush = OS.CreateSolidBrush (control.getBackgroundPixel ());
264                         auto oldBrush = OS.SelectObject (dstHdc, newBrush);
265                         OS.PatBlt (dstHdc, 0, 0, rect.width, rect.height, OS.PATCOPY);
266                         OS.DrawIconEx (dstHdc, 0, 0, hImage, 0, 0, 0, null, OS.DI_NORMAL);
267                         OS.SelectObject (dstHdc, oldBrush);
268                         OS.DeleteObject (newBrush);
269                     }
270                     OS.SelectObject (dstHdc, oldBitmap);
271                     OS.DeleteDC (dstHdc);
272                     OS.ReleaseDC (handle, hDC);
273                     if (image2 !is null) image2.dispose ();
274                     image2 = Image.win32_new (display, DWT.BITMAP, hBitmap);
275                     imageBits = OS.BS_BITMAP;
276                     fImageType = OS.IMAGE_BITMAP;
277                     hImage = hBitmap;
278                 }
279             }
280         }
281         int newBits = OS.GetWindowLong (handle, OS.GWL_STYLE), oldBits = newBits;
282         newBits &= ~(OS.BS_BITMAP | OS.BS_ICON);
283         newBits |= imageBits;
284         if (newBits !is oldBits) OS.SetWindowLong (handle, OS.GWL_STYLE, newBits);
285         OS.SendMessage (handle, OS.BM_SETIMAGE, fImageType, hImage);
286     }
287 }
288
289 void _setText (String text) {
290     int oldBits = OS.GetWindowLong (handle, OS.GWL_STYLE), newBits = oldBits;
291     if (OS.COMCTL32_MAJOR >= 6) {
292         newBits &= ~(OS.BS_LEFT | OS.BS_CENTER | OS.BS_RIGHT);
293         if ((style & DWT.LEFT) !is 0) newBits |= OS.BS_LEFT;
294         if ((style & DWT.CENTER) !is 0) newBits |= OS.BS_CENTER;
295         if ((style & DWT.RIGHT) !is 0) newBits |= OS.BS_RIGHT;
296         if (imageList !is null) {
297             BUTTON_IMAGELIST buttonImageList;
298             buttonImageList.himl = imageList.getHandle ();
299             if (text.length is 0) {
300                 if ((style & DWT.LEFT) !is 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_LEFT;
301                 if ((style & DWT.CENTER) !is 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_CENTER;
302                 if ((style & DWT.RIGHT) !is 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_RIGHT;
303             } else {
304                 buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_LEFT;
305                 buttonImageList.margin.left = computeLeftMargin ();
306                 buttonImageList.margin.right = MARGIN;
307                 newBits &= ~(OS.BS_CENTER | OS.BS_RIGHT);
308                 newBits |= OS.BS_LEFT;
309             }
310             OS.SendMessage (handle, OS.BCM_SETIMAGELIST, 0, &buttonImageList);
311         }
312     } else {
313         newBits &= ~(OS.BS_BITMAP | OS.BS_ICON);
314     }
315     if (newBits !is oldBits) {
316         OS.SetWindowLong (handle, OS.GWL_STYLE, newBits);
317         OS.InvalidateRect (handle, null, true);
318     }
319     /*
320     * Bug in Windows.  When a Button control is right-to-left and
321     * is disabled, the first pixel of the text is clipped.  The
322     * fix is to add a space to both sides of the text.
323     */
324     if ((style & DWT.RIGHT_TO_LEFT) !is 0) {
325         if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) {
326             text = OS.IsWindowEnabled (handle) ? text : " " ~ text ~ " ";
327         }
328     }
329     TCHAR* buffer = StrToTCHARz ( text );
330     OS.SetWindowText (handle, buffer);
331 }
332
333 /**
334  * Adds the listener to the collection of listeners who will
335  * be notified when the control is selected by the user, by sending
336  * it one of the messages defined in the <code>SelectionListener</code>
337  * interface.
338  * <p>
339  * <code>widgetSelected</code> is called when the control is selected by the user.
340  * <code>widgetDefaultSelected</code> is not called.
341  * </p>
342  *
343  * @param listener the listener which should be notified
344  *
345  * @exception IllegalArgumentException <ul>
346  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
347  * </ul>
348  * @exception DWTException <ul>
349  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
350  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
351  * </ul>
352  *
353  * @see SelectionListener
354  * @see #removeSelectionListener
355  * @see SelectionEvent
356  */
357 public void addSelectionListener (SelectionListener listener) {
358     checkWidget ();
359     if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
360     TypedListener typedListener = new TypedListener (listener);
361     addListener (DWT.Selection,typedListener);
362     addListener (DWT.DefaultSelection,typedListener);
363 }
364
365 override int callWindowProc (HWND hwnd, int msg, int wParam, int lParam) {
366     if (handle is null) return 0;
367     return OS.CallWindowProc ( ButtonProc, hwnd, msg, wParam, lParam);
368 }
369
370 static int checkStyle (int style) {
371     style = checkBits (style, DWT.PUSH, DWT.ARROW, DWT.CHECK, DWT.RADIO, DWT.TOGGLE, COMMAND_LINK ? DWT.COMMAND : 0);
372     if ((style & (DWT.PUSH | DWT.TOGGLE)) !is 0) {
373         return checkBits (style, DWT.CENTER, DWT.LEFT, DWT.RIGHT, 0, 0, 0);
374     }
375     if ((style & (DWT.CHECK | DWT.RADIO)) !is 0) {
376         return checkBits (style, DWT.LEFT, DWT.RIGHT, DWT.CENTER, 0, 0, 0);
377     }
378     if ((style & DWT.ARROW) !is 0) {
379         style |= DWT.NO_FOCUS;
380         return checkBits (style, DWT.UP, DWT.DOWN, DWT.LEFT, DWT.RIGHT, 0, 0);
381     }
382     return style;
383 }
384
385 void click () {
386     /*
387     * Feature in Windows.  BM_CLICK sends a fake WM_LBUTTONDOWN and
388     * WM_LBUTTONUP in order to click the button.  This causes the
389     * application to get unexpected mouse events.  The fix is to
390     * ignore mouse events when they are caused by BM_CLICK.
391     */
392     ignoreMouse = true;
393     OS.SendMessage (handle, OS.BM_CLICK, 0, 0);
394     ignoreMouse = false;
395 }
396
397 int computeLeftMargin () {
398     if (OS.COMCTL32_MAJOR < 6) return MARGIN;
399     if ((style & (DWT.PUSH | DWT.TOGGLE)) is 0) return MARGIN;
400     int margin = 0;
401     if (image !is null && text.length !is 0) {
402         Rectangle bounds = image.getBounds ();
403         margin += bounds.width + MARGIN * 2;
404         HFONT oldFont;
405         auto hDC = OS.GetDC (handle);
406         HFONT newFont = cast(HFONT) OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
407         if (newFont !is null) oldFont = OS.SelectObject (hDC, newFont);
408         TCHAR* buffer = StrToTCHARz( getCodePage (), text);
409         RECT rect;
410         int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE;
411         OS.DrawText (hDC, buffer, -1, &rect, flags);
412         margin += rect.right - rect.left;
413         if (newFont !is null) OS.SelectObject (hDC, oldFont);
414         OS.ReleaseDC (handle, hDC);
415         OS.GetClientRect (handle, &rect);
416         margin = Math.max (MARGIN, (rect.right - rect.left - margin) / 2);
417     }
418     return margin;
419 }
420
421 override public Point computeSize (int wHint, int hHint, bool changed) {
422     checkWidget ();
423     int width = 0, height = 0, border = getBorderWidth ();
424     if ((style & DWT.ARROW) !is 0) {
425         if ((style & (DWT.UP | DWT.DOWN)) !is 0) {
426             width += OS.GetSystemMetrics (OS.SM_CXVSCROLL);
427             height += OS.GetSystemMetrics (OS.SM_CYVSCROLL);
428         } else {
429             width += OS.GetSystemMetrics (OS.SM_CXHSCROLL);
430             height += OS.GetSystemMetrics (OS.SM_CYHSCROLL);
431         }
432     } else {
433         if ((style & DWT.COMMAND) !is 0) {
434             SIZE size;
435             if (wHint !is DWT.DEFAULT) {
436                 size.cx = wHint;
437                 OS.SendMessage (handle, OS.BCM_GETIDEALSIZE, 0, &size);
438                 width = size.cx;
439                 height = size.cy;
440             } else {
441                 OS.SendMessage (handle, OS.BCM_GETIDEALSIZE, 0, &size);
442                 width = size.cy;
443                 height = size.cy;
444                 size.cy = 0;
445                 while (size.cy !is height) {
446                     size.cx = width++;
447                     size.cy = 0;
448                     OS.SendMessage (handle, OS.BCM_GETIDEALSIZE, 0, &size);
449                 }
450             }
451         } else {
452             int extra = 0;
453             bool hasImage = image !is null, hasText = true;
454             if (OS.COMCTL32_MAJOR < 6) {
455                 if ((style & DWT.PUSH) is 0) {
456                     int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
457                     hasImage = (bits & (OS.BS_BITMAP | OS.BS_ICON)) !is 0;
458                     if (hasImage) hasText = false;
459                 }
460             }
461             if (hasImage) {
462                 if (image !is null) {
463                     Rectangle rect = image.getBounds ();
464                     width = rect.width;
465                     if (hasText && text.length !is 0) {
466                         width += MARGIN * 2;
467                     }
468                     height = rect.height;
469                     extra = MARGIN <