KickJava   Java API By Example, From Geeks To Geeks.

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


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.win32.*;
15 import org.eclipse.swt.*;
16 import org.eclipse.swt.graphics.*;
17
18 /**
19  * Instances of this class represent a non-selectable
20  * user interface object that displays a string or image.
21  * When SEPARATOR is specified, displays a single
22  * vertical or horizontal line.
23  * <dl>
24  * <dt><b>Styles:</b></dt>
25  * <dd>SEPARATOR, HORIZONTAL, VERTICAL</dd>
26  * <dd>SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd>
27  * <dd>CENTER, LEFT, RIGHT, WRAP</dd>
28  * <dt><b>Events:</b></dt>
29  * <dd>(none)</dd>
30  * </dl>
31  * <p>
32  * Note: Only one of SHADOW_IN, SHADOW_OUT and SHADOW_NONE may be specified.
33  * SHADOW_NONE is a HINT. Only one of HORIZONTAL and VERTICAL may be specified.
34  * Only one of CENTER, LEFT and RIGHT may be specified.
35  * </p><p>
36  * IMPORTANT: This class is intended to be subclassed <em>only</em>
37  * within the SWT implementation.
38  * </p>
39  */

40 public class Label extends Control {
41     String JavaDoc text = "";
42     Image image;
43     static final int MARGIN = 4;
44     static final boolean IMAGE_AND_TEXT = false;
45     static final int LabelProc;
46     static final TCHAR LabelClass = new TCHAR (0, "STATIC", true);
47     static {
48         WNDCLASS lpWndClass = new WNDCLASS ();
49         OS.GetClassInfo (0, LabelClass, lpWndClass);
50         LabelProc = lpWndClass.lpfnWndProc;
51     }
52
53 /**
54  * Constructs a new instance of this class given its parent
55  * and a style value describing its behavior and appearance.
56  * <p>
57  * The style value is either one of the style constants defined in
58  * class <code>SWT</code> which is applicable to instances of this
59  * class, or must be built by <em>bitwise OR</em>'ing together
60  * (that is, using the <code>int</code> "|" operator) two or more
61  * of those <code>SWT</code> style constants. The class description
62  * lists the style constants that are applicable to the class.
63  * Style bits are also inherited from superclasses.
64  * </p>
65  *
66  * @param parent a composite control which will be the parent of the new instance (cannot be null)
67  * @param style the style of control to construct
68  *
69  * @exception IllegalArgumentException <ul>
70  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
71  * </ul>
72  * @exception SWTException <ul>
73  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
74  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
75  * </ul>
76  *
77  * @see SWT#SEPARATOR
78  * @see SWT#HORIZONTAL
79  * @see SWT#VERTICAL
80  * @see SWT#SHADOW_IN
81  * @see SWT#SHADOW_OUT
82  * @see SWT#SHADOW_NONE
83  * @see SWT#CENTER
84  * @see SWT#LEFT
85  * @see SWT#RIGHT
86  * @see SWT#WRAP
87  * @see Widget#checkSubclass
88  * @see Widget#getStyle
89  */

90 public Label (Composite parent, int style) {
91     super (parent, checkStyle (style));
92 }
93
94 int callWindowProc (int hwnd, int msg, int wParam, int lParam) {
95     if (handle == 0) return 0;
96     return OS.CallWindowProc (LabelProc, hwnd, msg, wParam, lParam);
97 }
98
99 static int checkStyle (int style) {
100     style |= SWT.NO_FOCUS;
101     if ((style & SWT.SEPARATOR) != 0) {
102         style = checkBits (style, SWT.VERTICAL, SWT.HORIZONTAL, 0, 0, 0, 0);
103         return checkBits (style, SWT.SHADOW_OUT, SWT.SHADOW_IN, SWT.SHADOW_NONE, 0, 0, 0);
104     }
105     return checkBits (style, SWT.LEFT, SWT.CENTER, SWT.RIGHT, 0, 0, 0);
106 }
107
108 public Point computeSize (int wHint, int hHint, boolean changed) {
109     checkWidget ();
110     int width = 0, height = 0, border = getBorderWidth ();
111     if ((style & SWT.SEPARATOR) != 0) {
112         int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER);
113         if ((style & SWT.HORIZONTAL) != 0) {
114             width = DEFAULT_WIDTH; height = lineWidth * 2;
115         } else {
116             width = lineWidth * 2; height = DEFAULT_HEIGHT;
117         }
118         if (wHint != SWT.DEFAULT) width = wHint;
119         if (hHint != SWT.DEFAULT) height = hHint;
120         width += border * 2; height += border * 2;
121         return new Point (width, height);
122     }
123     int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
124     boolean drawText = true;
125     boolean drawImage = (bits & OS.SS_OWNERDRAW) == OS.SS_OWNERDRAW;
126     if (drawImage) {
127         if (image != null) {
128             Rectangle rect = image.getBounds();
129             width += rect.width;
130             height += rect.height;
131             if (IMAGE_AND_TEXT) {
132                 if (text.length () != 0) width += MARGIN;
133             } else {
134                 drawText = false;
135             }
136         }
137     }
138     if (drawText) {
139         int hDC = OS.GetDC (handle);
140         int newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
141         int oldFont = OS.SelectObject (hDC, newFont);
142         int length = OS.GetWindowTextLength (handle);
143         if (length == 0) {
144             TEXTMETRIC tm = OS.IsUnicode ? (TEXTMETRIC) new TEXTMETRICW () : new TEXTMETRICA ();
145             OS.GetTextMetrics (hDC, tm);
146             height = Math.max (height, tm.tmHeight);
147         } else {
148             RECT rect = new RECT ();
149             int flags = OS.DT_CALCRECT | OS.DT_EDITCONTROL | OS.DT_EXPANDTABS;
150             if ((style & SWT.WRAP) != 0 && wHint != SWT.DEFAULT) {
151                 flags |= OS.DT_WORDBREAK;
152                 rect.right = Math.max (0, wHint - width);
153             }
154             TCHAR buffer = new TCHAR (getCodePage (), length + 1);
155             OS.GetWindowText (handle, buffer, length + 1);
156             OS.DrawText (hDC, buffer, length, rect, flags);
157             width += rect.right - rect.left;
158             height = Math.max (height, rect.bottom - rect.top);
159         }
160         if (newFont != 0) OS.SelectObject (hDC, oldFont);
161         OS.ReleaseDC (handle, hDC);
162     }
163     if (wHint != SWT.DEFAULT) width = wHint;
164     if (hHint != SWT.DEFAULT) height = hHint;
165     width += border * 2;
166     height += border * 2;
167     /*
168     * Feature in WinCE PPC. Text labels have a trim
169     * of one pixel wide on the right and left side.
170     * The fix is to increase the width to include
171     * this trim.
172     */

173     if (OS.IsWinCE && !drawImage) width += 2;
174     return new Point (width, height);
175 }
176
177 void createHandle () {
178     super.createHandle ();
179     state |= THEME_BACKGROUND;
180 }
181
182 /**
183  * Returns a value which describes the position of the
184  * text or image in the receiver. The value will be one of
185  * <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code>
186  * unless the receiver is a <code>SEPARATOR</code> label, in
187  * which case, <code>NONE</code> is returned.
188  *
189  * @return the alignment
190  *
191  * @exception SWTException <ul>
192  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
193  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
194  * </ul>
195  */

196 public int getAlignment () {
197     checkWidget ();
198     if ((style & SWT.SEPARATOR) != 0) return 0;
199     if ((style & SWT.LEFT) != 0) return SWT.LEFT;
200     if ((style & SWT.CENTER) != 0) return SWT.CENTER;
201     if ((style & SWT.RIGHT) != 0) return SWT.RIGHT;
202     return SWT.LEFT;
203 }
204
205 /**
206  * Returns the receiver's image if it has one, or null
207  * if it does not.
208  *
209  * @return the receiver's image
210  *
211  * @exception SWTException <ul>
212  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
213  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
214  * </ul>
215  */

216 public Image getImage () {
217     checkWidget ();
218     return image;
219 }
220
221 String JavaDoc getNameText () {
222     return getText ();
223 }
224
225 /**
226  * Returns the receiver's text, which will be an empty
227  * string if it has never been set or if the receiver is
228  * a <code>SEPARATOR</code> label.
229  *
230  * @return the receiver's text
231  *
232  * @exception SWTException <ul>
233  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
234  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
235  * </ul>
236  */

237 public String JavaDoc getText () {
238     checkWidget ();
239     if ((style & SWT.SEPARATOR) != 0) return "";
240     return text;
241 }
242
243 boolean mnemonicHit (char key) {
244     Composite control = this.parent;
245     while (control != null) {
246         Control [] children = control._getChildren ();
247         int index = 0;
248         while (index < children.length) {
249             if (children [index] == this) break;
250             index++;
251         }
252         index++;
253         if (index < children.length) {
254             if (children [index].setFocus ()) return true;
255         }
256         control = control.parent;
257     }
258     return false;
259 }
260
261 boolean mnemonicMatch (char key) {
262     char mnemonic = findMnemonic (getText ());
263     if (mnemonic == '\0') return false;
264     return Character.toUpperCase (key) == Character.toUpperCase (mnemonic);
265 }
266
267 void releaseWidget () {
268     super.releaseWidget ();
269     text = null;
270     image = null;
271 }
272
273 /**
274  * Controls how text and images will be displayed in the receiver.
275  * The argument should be one of <code>LEFT</code>, <code>RIGHT</code>
276  * or <code>CENTER</code>. If the receiver is a <code>SEPARATOR</code>
277  * label, the argument is ignored and the alignment is not changed.
278  *
279  * @param alignment the new alignment
280  *
281  * @exception SWTException <ul>
282  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
283  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
284  * </ul>
285  */

286 public void setAlignment (int alignment) {
287     checkWidget ();
288     if ((style & SWT.SEPARATOR) != 0) return;
289     if ((alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER)) == 0) return;
290     style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER);
291     style |= alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER);
292     int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
293     if ((bits & OS.SS_OWNERDRAW) != OS.SS_OWNERDRAW) {
294         bits &= ~(OS.SS_LEFTNOWORDWRAP | OS.SS_CENTER | OS.SS_RIGHT);
295         if ((style & SWT.LEFT) != 0) {
296             if ((style & SWT.WRAP) != 0) {
297                 bits |= OS.SS_LEFT;
298             } else {
299                 bits |= OS.SS_LEFTNOWORDWRAP;
300             }
301         }
302         if ((style & SWT.CENTER) != 0) bits |= OS.SS_CENTER;
303         if ((style & SWT.RIGHT) != 0) bits |= OS.SS_RIGHT;
304         OS.SetWindowLong (handle, OS.GWL_STYLE, bits);
305     }
306     OS.InvalidateRect (handle, null, true);
307 }
308
309 /**
310  * Sets the receiver's image to the argument, which may be
311  * null indicating that no image should be displayed.
312  *
313  * @param image the image to display on the receiver (may be null)
314  *
315  * @exception IllegalArgumentException <ul>
316  * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
317  * </ul>
318  * @exception SWTException <ul>
319  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
320  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
321  * </ul>
322  */

323 public void setImage (Image image) {
324     checkWidget ();
325     if ((style & SWT.SEPARATOR) != 0) return;
326     if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
327     this.image = image;
328     int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
329     if ((bits & OS.SS_OWNERDRAW) != OS.SS_OWNERDRAW) {
330         bits &= ~(OS.SS_LEFTNOWORDWRAP | OS.SS_CENTER | OS.SS_RIGHT);
331         bits |= OS.SS_OWNERDRAW;
332         OS.SetWindowLong (handle, OS.GWL_STYLE, bits);
333     }
334     OS.InvalidateRect (handle, null, true);
335 }
336
337 /**
338  * Sets the receiver's text.
339  * <p>
340  * This method sets the widget label. The label may include
341  * the mnemonic character and line delimiters.
342  * </p>
343  * <p>
344  * Mnemonics are indicated by an '&amp;' that causes the next
345  * character to be the mnemonic. When the user presses a
346  * key sequence that matches the mnemonic, focus is assigned
347  * to the control that follows the label. On most platforms,
348  * the mnemonic appears underlined but may be emphasised in a
349  * platform specific manner. The mnemonic indicator character
350  * '&amp;' can be escaped by doubling it in the string, causing
351  * a single '&amp;' to be displayed.
352  * </p>
353  *
354  * @param string the new text
355  *
356  * @exception IllegalArgumentException <ul>
357  * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
358  * </ul>
359  * @exception SWTException <ul>
360  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
361  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
362  * </ul>
363  */

364 public void setText (String JavaDoc string) {
365     checkWidget ();
366     if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
367     if ((style & SWT.SEPARATOR) != 0) return;
368     /*
369     * Feature in Windows. For some reason, SetWindowText() for
370     * static controls redraws the control, even when the text has
371     * has not changed. The fix is to check for this case and do
372     * nothing.
373     */

374     if (string.equals (text)) return;
375     text = string;
376     if (image == null || !IMAGE_AND_TEXT) {
377         int oldBits = OS.GetWindowLong (handle, OS.GWL_STYLE), newBits = oldBits;
378         newBits &= ~OS.SS_OWNERDRAW;
379         if ((style & SWT.LEFT) != 0) {
380             if ((style & SWT.WRAP) != 0) {
381                 newBits |= OS.SS_LEFT;
382             } else {
383                 newBits |= OS.SS_LEFTNOWORDWRAP;
384             }
385         }
386         if ((style & SWT.CENTER) != 0) newBits |= OS.SS_CENTER;
387         if ((style & SWT.RIGHT) != 0) newBits |= OS.SS_RIGHT;
388         if (oldBits != newBits) OS.SetWindowLong (handle, OS.GWL_STYLE, newBits);
389     }
390     string = Display.withCrLf (string);
391     TCHAR buffer = new TCHAR (getCodePage (), string, true);
392     OS.SetWindowText (handle, buffer);
393     /*
394     * Bug in Windows. For some reason, the HBRUSH that
395     * is returned from WM_CTRLCOLOR is misaligned when
396     * the label uses it to draw. If the brush is a solid
397     * color, this does not matter. However, if the brush
398     * contains an image, the image is misaligned. The
399     * fix is to draw the background in WM_ERASEBKGND.
400     */

401     if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) {
402         if (findImageControl () != null) OS.InvalidateRect (handle, null, true);
403     }
404 }
405
406 int widgetExtStyle () {
407     int bits = super.widgetExtStyle () & ~OS.WS_EX_CLIENTEDGE;
408     if ((style & SWT.BORDER) != 0) return bits | OS.WS_EX_STATICEDGE;
409     return bits;
410 }
411
412 int widgetStyle () {
413     int bits = super.widgetStyle () | OS.SS_NOTIFY;
414     if ((style & SWT.SEPARATOR) != 0) return bits | OS.SS_OWNERDRAW;
415     if (OS.WIN32_VERSION >= OS.VERSION (5, 0)) {
416         if ((style & SWT.WRAP) != 0) bits |= OS.SS_EDITCONTROL;
417     }
418     if ((style & SWT.CENTER) != 0) return bits | OS.SS_CENTER;
419     if ((style & SWT.RIGHT) != 0) return bits | OS.SS_RIGHT;
420     if ((style & SWT.WRAP) != 0) return bits | OS.SS_LEFT;
421     return bits | OS.SS_LEFTNOWORDWRAP;
422 }
423
424 TCHAR windowClass () {
425     return LabelClass;
426 }
427
428 int windowProc () {
429     return LabelProc;
430 }
431
432 LRESULT WM_ERASEBKGND (int wParam, int lParam) {
433     LRESULT result = super.WM_ERASEBKGND (wParam, lParam);
434     if (result != null) return result;
435     int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
436     if ((bits & OS.SS_OWNERDRAW) == OS.SS_OWNERDRAW) {
437         return LRESULT.ONE;
438     }
439     /*
440     * Bug in Windows. For some reason, the HBRUSH that
441     * is returned from WM_CTRLCOLOR is misaligned when
442     * the label uses it to draw. If the brush is a solid
443     * color, this does not matter. However, if the brush
444     * contains an image, the image is misaligned. The
445     * fix is to draw the background in WM_ERASEBKGND.
446     */

447     if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) {
448         if (findImageControl () != null) {
449             drawBackground (wParam);
450             return LRESULT.ONE;
451         }
452     }
453     return result;
454 }
455
456 LRESULT WM_SIZE (int wParam, int lParam) {
457     LRESULT result = super.WM_SIZE (wParam, lParam);
458     if (isDisposed ()) return result;
459     if ((style & SWT.SEPARATOR) != 0) {
460         OS.InvalidateRect (handle, null, true);
461         return result;
462     }
463     int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
464     if ((bits & OS.SS_OWNERDRAW) == OS.SS_OWNERDRAW) {
465         OS.InvalidateRect (handle, null, true);
466         return result;
467     }
468     /*
469     * Bug in Windows. For some reason, a label with
470     * style SS_LEFT, SS_CENTER or SS_RIGHT does not
471     * redraw the text in the new position when resized.
472     * Note that SS_LEFTNOWORDWRAP does not have the
473     * problem. The fix is to force the redraw.
474     */

475     if ((bits & OS.SS_LEFTNOWORDWRAP) != OS.SS_LEFTNOWORDWRAP) {
476         OS.InvalidateRect (handle, null, true);
477         return result;
478     }
479     return result;
480 }
481
482 LRESULT WM_UPDATEUISTATE (int wParam, int lParam) {
483     LRESULT result = super.WM_UPDATEUISTATE (wParam, lParam);
484     /*
485     * Feature in Windows. When WM_UPDATEUISTATE is sent to
486     * a static control, it sends WM_CTLCOLORSTATIC to get the
487     * foreground and background. If any drawing happens in
488     * WM_CTLCOLORSTATIC, it overwrites the contents of the control.
489     * The fix is draw the static without drawing the background
490     * and avoid the static window proc.
491     */

492     boolean redraw = findImageControl () != null;
493     if (!redraw) {
494         if ((state & THEME_BACKGROUND) != 0) {
495             if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
496                 redraw = findThemeControl () != null;
497             }
498         }
499     }
500     if (redraw) {
501         OS.InvalidateRect (handle, null, false);
502         int code = OS.DefWindowProc (handle, OS.WM_UPDATEUISTATE, wParam, lParam);
503         return new LRESULT (code);
504     }
505     return result;
506 }
507
508 LRESULT wmColorChild (int wParam, int lParam) {
509     /*
510     * Bug in Windows. For some reason, the HBRUSH that
511     * is returned from WM_CTRLCOLOR is misaligned when
512     * the label uses it to draw. If the brush is a solid
513     * color, this does not matter. However, if the brush
514     * contains an image, the image is misaligned. The
515     * fix is to draw the background in WM_ERASEBKGND.
516     */

517     LRESULT result = super.wmColorChild (wParam, lParam);
518     if (OS.COMCTL32_MAJOR < 6 || !OS.IsAppThemed ()) {
519         int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
520         if ((bits & OS.SS_OWNERDRAW) != OS.SS_OWNERDRAW) {
521             if (findImageControl () != null) {
522                 OS.SetBkMode (wParam, OS.TRANSPARENT);
523                 return new LRESULT (OS.GetStockObject (OS.NULL_BRUSH));
524             }
525         }
526     }
527     return result;
528 }
529
530 LRESULT WM_PAINT (int wParam, int lParam) {
531     if (OS.IsWinCE) {
532         boolean drawImage = image != null;
533         boolean drawSeparator = (style & SWT.SEPARATOR) != 0 && (style & SWT.SHADOW_NONE) == 0;
534         if (drawImage || drawSeparator) {
535             LRESULT result = null;
536             PAINTSTRUCT ps = new PAINTSTRUCT ();
537             GCData data = new GCData ();
538             data.ps = ps;
539             data.hwnd = handle;
540             GC gc = new_GC (data);
541             if (gc != null) {
542                 drawBackground (gc.handle);
543                 RECT clientRect = new RECT();
544                 OS.GetClientRect (handle, clientRect);
545                 if (drawSeparator) {
546                     RECT rect = new RECT ();
547                     int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER);
548                     int flags = (style & SWT.SHADOW_IN) != 0 ? OS.EDGE_SUNKEN : OS.EDGE_ETCHED;
549                     if ((style & SWT.HORIZONTAL) != 0) {
550                         int bottom = clientRect.top + Math.max (lineWidth * 2, (clientRect.bottom - clientRect.top) / 2);
551                         OS.SetRect (rect, clientRect.left, clientRect.top, clientRect.right, bottom);
552                         OS.DrawEdge (gc.handle, rect, flags, OS.BF_BOTTOM);
553                     } else {
554                         int right = clientRect.left + Math.max (lineWidth * 2, (clientRect.right - clientRect.left) / 2);
555                         OS.SetRect (rect, clientRect.left, clientRect.top, right, clientRect.bottom);
556                         OS.DrawEdge (gc.handle, rect, flags, OS.BF_RIGHT);
557                     }
558                     result = LRESULT.ONE;
559                 }
560                 if (drawImage) {
561                     Rectangle imageBounds = image.getBounds ();
562                     int x = 0;
563                     if ((style & SWT.CENTER) != 0) {
564                         x = Math.max (0, (clientRect.right - imageBounds.width) / 2);
565                     } else {
566                         if ((style & SWT.RIGHT) != 0) {
567                             x = Math.max (0, (clientRect.right - imageBounds.width));
568                         }
569                     }
570                     gc.drawImage (image, x, Math.max (0, (clientRect.bottom - imageBounds.height) / 2));
571                     result = LRESULT.ONE;
572                 }
573                 int width = ps.right - ps.left;
574                 int height = ps.bottom - ps.top;
575                 if (width != 0 && height != 0) {
576                     Event event = new Event ();
577                     event.gc = gc;
578                     event.x = ps.left;
579                     event.y = ps.top;
580                     event.width = width;
581                     event.height = height;
582                     sendEvent (SWT.Paint, event);
583                     // widget could be disposed at this point
584
event.gc = null;
585                 }
586                 gc.dispose ();
587             }
588             return result;
589         }
590     }
591     return super.WM_PAINT(wParam, lParam);
592 }
593
594 LRESULT wmDrawChild (int wParam, int lParam) {
595     DRAWITEMSTRUCT struct = new DRAWITEMSTRUCT ();
596     OS.MoveMemory (struct, lParam, DRAWITEMSTRUCT.sizeof);
597     drawBackground (struct.hDC);
598     if ((style & SWT.SEPARATOR) != 0) {
599         if ((style & SWT.SHADOW_NONE) != 0) return null;
600         RECT rect = new RECT ();
601         int lineWidth = OS.GetSystemMetrics (OS.SM_CXBORDER);
602         int flags = (style & SWT.SHADOW_IN) != 0 ? OS.EDGE_SUNKEN : OS.EDGE_ETCHED;
603         if ((style & SWT.HORIZONTAL) != 0) {
604             int bottom = struct.top + Math.max (lineWidth * 2, (struct.bottom - struct.top) / 2);
605             OS.SetRect (rect, struct.left, struct.top, struct.right, bottom);
606             OS.DrawEdge (struct.hDC, rect, flags, OS.BF_BOTTOM);
607         } else {
608             int right = struct.left + Math.max (lineWidth * 2, (struct.right - struct.left) / 2);
609             OS.SetRect (rect, struct.left, struct.top, right, struct.bottom);
610             OS.DrawEdge (struct.hDC, rect, flags, OS.BF_RIGHT);
611         }
612     } else {
613         int width = struct.right - struct.left;
614         int height = struct.bottom - struct.top;
615         if (width != 0 && height != 0) {
616             boolean drawImage = image != null;
617             boolean drawText = IMAGE_AND_TEXT && text.length () != 0;
618             int margin = drawText && drawImage ? MARGIN : 0;
619             int imageWidth = 0, imageHeight = 0;
620             if (drawImage) {
621                 Rectangle rect = image.getBounds ();
622                 imageWidth = rect.width;
623                 imageHeight = rect.height;
624             }
625             RECT rect = null;
626             TCHAR buffer = null;
627             int textWidth = 0, textHeight = 0, flags = 0;
628             if (drawText) {
629                 rect = new RECT ();
630                 flags = OS.DT_CALCRECT | OS.DT_EDITCONTROL | OS.DT_EXPANDTABS;
631                 if ((style & SWT.LEFT) != 0) flags |= OS.DT_LEFT;
632                 if ((style & SWT.CENTER) != 0) flags |= OS.DT_CENTER;
633                 if ((style & SWT.RIGHT) != 0) flags |= OS.DT_RIGHT;
634                 if ((style & SWT.WRAP) != 0) {
635                     flags |= OS.DT_WORDBREAK;
636                     rect.right = Math.max (0, width - imageWidth - margin);
637                 }
638                 buffer = new TCHAR (getCodePage (), text, true);
639                 OS.DrawText (struct.hDC, buffer, -1, rect, flags);
640                 textWidth = rect.right - rect.left;
641                 textHeight = rect.bottom - rect.top;
642             }
643             int x = 0;
644             if ((style & SWT.CENTER) != 0) {
645                 x = Math.max (0, (width - imageWidth - textWidth - margin) / 2);
646             } else {
647                 if ((style & SWT.RIGHT) != 0) {
648                     x = width - imageWidth - textWidth - margin;
649                 }
650             }
651             if (drawImage) {
652                 GCData data = new GCData();
653                 data.device = display;
654                 GC gc = GC.win32_new (struct.hDC, data);
655                 Image image = getEnabled () ? this.image : new Image (display, this.image, SWT.IMAGE_DISABLE);
656                 gc.drawImage (image, x, Math.max (0, (height - imageHeight) / 2));
657                 if (image != this.image) image.dispose ();
658                 gc.dispose ();
659                 x += imageWidth + margin;
660             }
661             if (drawText) {
662                 flags &= ~OS.DT_CALCRECT;
663                 rect.left = x;
664                 rect.right += rect.left;
665                 rect.top = Math.max (0, (height - textHeight) / 2);
666                 rect.bottom += rect.top;
667                 OS.DrawText (struct.hDC, buffer, -1, rect, flags);
668             }
669         }
670     }
671     return null;
672 }
673
674 }
675
Popular Tags