javauis/eswt_qt/org.eclipse.swt/Eclipse SWT/qt/org/eclipse/ercp/swt/mobile/CaptionedControl.java
changeset 35 85266cc22c7f
parent 21 2a9601315dfc
child 80 d6dafc5d983f
equal deleted inserted replaced
26:dc7c549001d5 35:85266cc22c7f
     1 /*******************************************************************************
     1 /*******************************************************************************
     2  * Copyright (c) 2004, 2008 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2004, 2008 Nokia Corporation and/or its subsidiary(-ies).
     3  * All rights reserved. This program and the accompanying materials 
     3  * All rights reserved. This program and the accompanying materials
     4  * are made available under the terms of the Eclipse Public License v1.0
     4  * are made available under the terms of the Eclipse Public License v1.0
     5  * which accompanies this distribution, and is available at
     5  * which accompanies this distribution, and is available at
     6  * http://www.eclipse.org/legal/epl-v10.html
     6  * http://www.eclipse.org/legal/epl-v10.html
     7  * 
     7  *
     8  * Contributors:
     8  * Contributors:
     9  *     Yu You (Nokia Corp.)- initial API specification 
     9  *     Yu You (Nokia Corp.)- initial API specification
    10  *     Nokia Corporation - S60 implementation
    10  *     Nokia Corporation - S60 implementation
    11  *     Nokia Corporation - QT implementation
    11  *     Nokia Corporation - QT implementation
    12  *******************************************************************************/
    12  *******************************************************************************/
    13 package org.eclipse.ercp.swt.mobile;
    13 package org.eclipse.ercp.swt.mobile;
    14 
    14 
    24 import org.eclipse.swt.widgets.*;
    24 import org.eclipse.swt.widgets.*;
    25 
    25 
    26 import java.lang.Math;
    26 import java.lang.Math;
    27 
    27 
    28 /**
    28 /**
    29  * 
    29  *
    30  * A CaptionedControl is used to display a label (caption) in front of a
    30  * A CaptionedControl is used to display a label (caption) in front of a
    31  * control. An optional trailing text can be used after the control, for
    31  * control. An optional trailing text can be used after the control, for
    32  * example, to indicate units of measurement.
    32  * example, to indicate units of measurement.
    33  * <p>
    33  * <p>
    34  * The highlighting is implementation dependent. Typically, a focused
    34  * The highlighting is implementation dependent. Typically, a focused
    35  * CaptionedControl will highlight all its elements, i.e. the leading label, the
    35  * CaptionedControl will highlight all its elements, i.e. the leading label, the
    36  * control, and optional trailing label, when it gets focus. The positioning
    36  * control, and optional trailing label, when it gets focus. The positioning
    37  * order for the captions is determined by the <code>SWT.LEFT_TO_RIGHT</code>
    37  * order for the captions is determined by the <code>SWT.LEFT_TO_RIGHT</code>
    38  * and <code>SWT.RIGHT_TO_LEFT</code> styles hints.
    38  * and <code>SWT.RIGHT_TO_LEFT</code> styles hints.
    39  * </p>
    39  * </p>
    40  * 
    40  *
    41  * <p>
    41  * <p>
    42  * CaptionedControl does not support nested CaptionedControls. An exception will
    42  * CaptionedControl does not support nested CaptionedControls. An exception will
    43  * be thrown when an instance of CaptionedControl is given as the constructor's
    43  * be thrown when an instance of CaptionedControl is given as the constructor's
    44  * argument. To change the control contained by the CaptionedControl, a new
    44  * argument. To change the control contained by the CaptionedControl, a new
    45  * control can be created using the CaptionedControl as its parent control. The
    45  * control can be created using the CaptionedControl as its parent control. The
    57  *      ConstraintedText.NUMERIC);
    57  *      ConstraintedText.NUMERIC);
    58  * control.setText(&quot;Velocity&quot;);
    58  * control.setText(&quot;Velocity&quot;);
    59  * control.getTrailingText(&quot;cm&quot;);
    59  * control.getTrailingText(&quot;cm&quot;);
    60  * </pre></code>
    60  * </pre></code>
    61  * </p>
    61  * </p>
    62  * 
    62  *
    63  * <p>
    63  * <p>
    64  * <dl>
    64  * <dl>
    65  * <dt><b>Styles: </b></dt>
    65  * <dt><b>Styles: </b></dt>
    66  * <dd>SWT.LEFT_TO_RIGHT (default)</dd>
    66  * <dd>SWT.LEFT_TO_RIGHT (default)</dd>
    67  * <dd>SWT.RIGHT_TO_LEFT</dd>
    67  * <dd>SWT.RIGHT_TO_LEFT</dd>
    73  * set a layout on it.
    73  * set a layout on it.
    74  * </p>
    74  * </p>
    75  * <p>
    75  * <p>
    76  * IMPORTANT: This class is not intended to be subclassed.
    76  * IMPORTANT: This class is not intended to be subclassed.
    77  * </p>
    77  * </p>
    78  * 
    78  *
    79  */
    79  */
    80 public final class CaptionedControl extends Composite {
    80 public final class CaptionedControl extends Composite {
    81     boolean initPhase;
    81     boolean initPhase;
    82     private int focusSignalProxy;
    82     private int focusSignalProxy;
    83     private MouseListener mouseListener;
    83     private MouseListener mouseListener;
    91         ((CaptionedControl)w).addControl(control);
    91         ((CaptionedControl)w).addControl(control);
    92     }
    92     }
    93     public void qt_signal_qapplication_focusChanged(int old, int now) {
    93     public void qt_signal_qapplication_focusChanged(int old, int now) {
    94         ((CaptionedControl)w).qt_signal_qapplication_focusChanged(old, now);
    94         ((CaptionedControl)w).qt_signal_qapplication_focusChanged(old, now);
    95     }
    95     }
    96     
    96 
    97     public void removeControl(Control control) {
    97     public void removeControl(Control control) {
    98         ((CaptionedControl)w).removeControl(control);
    98         ((CaptionedControl)w).removeControl(control);
    99     }
    99     }
   100 }
   100 }
   101 
   101 
   102 class CaptionedControlLayout extends Layout {
   102 class CaptionedControlLayout extends Layout {
   103 
   103 
   104 int checkHint(int hint, int spacing) {
   104 int checkHint(int hint, int spacing) {
   105     
   105 
   106     // Adjust a hint by spacing, check that result is not negative
   106     // Adjust a hint by spacing, check that result is not negative
   107     if (hint == SWT.DEFAULT) {
   107     if (hint == SWT.DEFAULT) {
   108         return hint;
   108         return hint;
   109     }
   109     }
   110     
   110 
   111     hint -= spacing;
   111     hint -= spacing;
   112     
   112 
   113     if (hint < 0) {
   113     if (hint < 0) {
   114         hint = 0;
   114         hint = 0;
   115     }
   115     }
   116     
   116 
   117     return hint;
   117     return hint;
   118 }
   118 }
   119 
   119 
   120 
   120 
   121 protected Point computeSize(Composite composite, int hint, int hint2, boolean flushCache) {
   121 protected Point computeSize(Composite composite, int hint, int hint2, boolean flushCache) {
   122     Point res = new Point(0, 0);
   122     Point res = new Point(0, 0);
   123     int wHint = checkHint(hint, 2 * margin);
   123     int wHint = checkHint(hint, 2 * margin);
   124     int hHint = checkHint(hint2, 2 * margin);
   124     int hHint = checkHint(hint2, 2 * margin);
   125     
   125 
   126     CaptionedControlLayoutData data = getData(wHint, hHint);
   126     CaptionedControlLayoutData data = getData(wHint, hHint);
   127     if ((getStyle() & SWT.HORIZONTAL) != 0) {
   127     if ((getStyle() & SWT.HORIZONTAL) != 0) {
   128         if (hint == SWT.DEFAULT) {
   128         if (hint == SWT.DEFAULT) {
   129             res.x = data.row1.x;
   129             res.x = data.row1.x;
   130         }
   130         }
   150         }
   150         }
   151         else {
   151         else {
   152             res.y = data.row1.y + Math.max(hint2, data.trail.y);
   152             res.y = data.row1.y + Math.max(hint2, data.trail.y);
   153         }
   153         }
   154     }
   154     }
   155     
   155 
   156     // Since the margins are big enough, adding the border is pointless.
   156     // Since the margins are big enough, adding the border is pointless.
   157     res.x += 2 * margin;
   157     res.x += 2 * margin;
   158     res.y += 2 * margin;
   158     res.y += 2 * margin;
   159 
   159 
   160     return res;
   160     return res;
   161 }
   161 }
   162 
   162 
   163 protected void layout(Composite composite, boolean flushCache) {
   163 protected void layout(Composite composite, boolean flushCache) {
   164 
   164 
   165     Point size = composite.getSize();    
   165     Point size = composite.getSize();
   166     if (size.x <= 0 || size.y <= 0) {
   166     if (size.x <= 0 || size.y <= 0) {
   167         return;
   167         return;
   168     }
   168     }
   169     
   169 
   170     CaptionedControlLayoutData data = getData(size.x, size.y);
   170     CaptionedControlLayoutData data = getData(size.x, size.y);
   171     
   171 
   172     int x = margin;
   172     int x = margin;
   173     int y = margin;
   173     int y = margin;
   174     int w = size.x - 2 * margin;
   174     int w = size.x - 2 * margin;
   175     int h = 0;
   175     int h = 0;
   176     
   176 
   177     if ((getStyle() & SWT.HORIZONTAL) != 0) {
   177     if ((getStyle() & SWT.HORIZONTAL) != 0) {
   178         h = size.y - 2 * margin;
   178         h = size.y - 2 * margin;
   179     }
   179     }
   180     else {
   180     else {
   181         h = data.row1.y;
   181         h = data.row1.y;
   182     }
   182     }
   183     
   183 
   184     if (h < 0) h = 0;
   184     if (h < 0) h = 0;
   185     
   185 
   186     imageLabel.setBounds(x, y, data.img.x, h);
   186     imageLabel.setBounds(x, y, data.img.x, h);
   187     x += data.img.x;
   187     x += data.img.x;
   188     
   188 
   189     titleLabel.setBounds(x, y, data.title.x, h);
   189     titleLabel.setBounds(x, y, data.title.x, h);
   190     x += data.title.x;
   190     x += data.title.x;
   191 
   191 
   192     int trailW = data.trail.x;
   192     int trailW = data.trail.x;
   193 
   193 
   194     if ((getStyle() & SWT.VERTICAL) != 0) {
   194     if ((getStyle() & SWT.VERTICAL) != 0) {
   195         y += h;
   195         y += h;
   196         h = size.y - data.row1.y - 2 * margin;
   196         h = size.y - data.row1.y - 2 * margin;
   197         if (h < 0) h = 0;
   197         if (h < 0) h = 0;
   198         x = margin;
   198         x = margin;
   199         
   199 
   200         if (child == null) {
   200         if (child == null) {
   201             trailW = data.row1.x - 2 * margin;
   201             trailW = data.row1.x - 2 * margin;
   202         }
   202         }
   203     }
   203     }
   204     
   204 
   205     int childW = 0;
   205     int childW = 0;
   206     if (child != null) {
   206     if (child != null) {
   207         if ((getStyle() & SWT.HORIZONTAL) != 0) {
   207         if ((getStyle() & SWT.HORIZONTAL) != 0) {
   208             childW = w - data.img.x - data.title.x - data.trail.x;
   208             childW = w - data.img.x - data.title.x - data.trail.x;
   209         }
   209         }
   214             childW = 0;
   214             childW = 0;
   215         }
   215         }
   216 
   216 
   217         child.setBounds(x, y, childW, h);
   217         child.setBounds(x, y, childW, h);
   218         x += childW;
   218         x += childW;
   219     } 
   219     }
   220     
   220 
   221     trailLabel.setBounds(x, y, trailW, h);
   221     trailLabel.setBounds(x, y, trailW, h);
   222 }
   222 }
   223 
   223 
   224 protected CaptionedControlLayoutData getData() {
   224 protected CaptionedControlLayoutData getData() {
   225     return getData(SWT.DEFAULT, SWT.DEFAULT);
   225     return getData(SWT.DEFAULT, SWT.DEFAULT);
   227 
   227 
   228 protected CaptionedControlLayoutData getData(int wHint, int hHint) {
   228 protected CaptionedControlLayoutData getData(int wHint, int hHint) {
   229     CaptionedControlLayoutData res = new CaptionedControlLayoutData();
   229     CaptionedControlLayoutData res = new CaptionedControlLayoutData();
   230 
   230 
   231     int extra = spacing * 2;
   231     int extra = spacing * 2;
   232     
   232 
   233     wHint = checkHint(wHint, extra);
   233     wHint = checkHint(wHint, extra);
   234     hHint = checkHint(hHint, extra);
   234     hHint = checkHint(hHint, extra);
   235     
   235 
   236     if ((getStyle() & SWT.HORIZONTAL) != 0) {
   236     if ((getStyle() & SWT.HORIZONTAL) != 0) {
   237         if (imageLabel.getImage() != null) {
   237         if (imageLabel.getImage() != null) {
   238             res.img = imageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   238             res.img = imageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   239         }
   239         }
   240         String caption = titleLabel.getText();
   240         String caption = titleLabel.getText();
   243         }
   243         }
   244         String trail = trailLabel.getText();
   244         String trail = trailLabel.getText();
   245         if (trail != null && trail.length() > 0) {
   245         if (trail != null && trail.length() > 0) {
   246             res.trail = trailLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   246             res.trail = trailLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   247         }
   247         }
   248         
   248 
   249     } else {
   249     } else {
   250         if (imageLabel.getImage() != null) {
   250         if (imageLabel.getImage() != null) {
   251             res.img = imageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   251             res.img = imageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   252         }
   252         }
   253         String caption = titleLabel.getText();
   253         String caption = titleLabel.getText();
   254         if (caption != null && caption.length() > 0) {
   254         if (caption != null && caption.length() > 0) {
   255             
   255 
   256             if (wHint != SWT.DEFAULT) {
   256             if (wHint != SWT.DEFAULT) {
   257                 int hint = wHint - res.img.x;
   257                 int hint = wHint - res.img.x;
   258                 if (hint < 0) {
   258                 if (hint < 0) {
   259                     hint = 0;
   259                     hint = 0;
   260                 }
   260                 }
   261                 res.title = titleLabel.computeSize(hint, SWT.DEFAULT);
   261                 res.title = titleLabel.computeSize(hint, SWT.DEFAULT);
   262             } else {
   262             } else {
   263                 res.title = titleLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   263                 res.title = titleLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   264             }
   264             }
   265         }
   265         }
   266         
   266 
   267         String trail = trailLabel.getText();
   267         String trail = trailLabel.getText();
   268         if (trail != null && trail.length() > 0) {
   268         if (trail != null && trail.length() > 0) {
   269             res.trail = trailLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   269             res.trail = trailLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   270         }
   270         }
   271         
   271 
   272         
   272 
   273     }
   273     }
   274     
   274 
   275     if (res.img.x > 0 && res.img.y > 0) {
   275     if (res.img.x > 0 && res.img.y > 0) {
   276         res.img.x += extra;
   276         res.img.x += extra;
   277         res.img.y += extra;
   277         res.img.y += extra;
   278     }
   278     }
   279     if (res.title.x > 0 && res.title.y > 0) {
   279     if (res.title.x > 0 && res.title.y > 0) {
   282     }
   282     }
   283     if (res.trail.x > 0 && res.trail.y > 0) {
   283     if (res.trail.x > 0 && res.trail.y > 0) {
   284         res.trail.x += extra;
   284         res.trail.x += extra;
   285         res.trail.y += extra;
   285         res.trail.y += extra;
   286     }
   286     }
   287     
   287 
   288     if ((getStyle() & SWT.HORIZONTAL) != 0) {
   288     if ((getStyle() & SWT.HORIZONTAL) != 0) {
   289         if (child != null) {
   289         if (child != null) {
   290             res.child = child.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   290             res.child = child.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   291             
   291 
   292             if (wHint != SWT.DEFAULT) {
   292             if (wHint != SWT.DEFAULT) {
   293                 res.child.x = wHint - res.img.x - res.title.x - res.trail.x;
   293                 res.child.x = wHint - res.img.x - res.title.x - res.trail.x;
   294                 if (res.child.x < 0) {
   294                 if (res.child.x < 0) {
   295                     res.child.x = 0;
   295                     res.child.x = 0;
   296                 }
   296                 }
   301             if (wHint != SWT.DEFAULT) {
   301             if (wHint != SWT.DEFAULT) {
   302                 res.child = child.computeSize(wHint-res.trail.x, SWT.DEFAULT);
   302                 res.child = child.computeSize(wHint-res.trail.x, SWT.DEFAULT);
   303             } else {
   303             } else {
   304                 res.child = child.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   304                 res.child = child.computeSize(SWT.DEFAULT, SWT.DEFAULT);
   305             }
   305             }
   306             
   306 
   307             if (hHint != SWT.DEFAULT) {
   307             if (hHint != SWT.DEFAULT) {
   308                 res.child.y = hHint - res.row1.y;
   308                 res.child.y = hHint - res.row1.y;
   309             }
   309             }
   310         }
   310         }
   311     }
   311     }
   312     
   312 
   313     if ((getStyle() & SWT.HORIZONTAL) != 0) {
   313     if ((getStyle() & SWT.HORIZONTAL) != 0) {
   314         res.row1.x = res.img.x + res.title.x + res.child.x + res.trail.x;
   314         res.row1.x = res.img.x + res.title.x + res.child.x + res.trail.x;
   315         res.row1.y = Math.max(res.row1.y, res.img.y);
   315         res.row1.y = Math.max(res.row1.y, res.img.y);
   316         res.row1.y = Math.max(res.row1.y, res.title.y);
   316         res.row1.y = Math.max(res.row1.y, res.title.y);
   317         res.row1.y = Math.max(res.row1.y, res.trail.y);
   317         res.row1.y = Math.max(res.row1.y, res.trail.y);
   318         res.row1.y = Math.max(res.row1.y, res.child.y);
   318         res.row1.y = Math.max(res.row1.y, res.child.y);
   319     }
   319     }
   320     else {
   320     else {
   321         // If the first row would expand to be bigger than the contained 
   321         // If the first row would expand to be bigger than the contained
   322         // control, adjust title caption width.
   322         // control, adjust title caption width.
   323         if ((child != null) && (res.img.x + res.title.x > res.child.x + res.trail.x)) {
   323         if ((child != null) && (res.img.x + res.title.x > res.child.x + res.trail.x)) {
   324             int width = res.child.x + res.trail.x - res.img.x;
   324             int width = res.child.x + res.trail.x - res.img.x;
   325             
   325 
   326             if (width < 0) {
   326             if (width < 0) {
   327                 width = 0;
   327                 width = 0;
   328             }
   328             }
   329             
   329 
   330             Point newTitle = titleLabel.computeSize(width, SWT.DEFAULT);
   330             Point newTitle = titleLabel.computeSize(width, SWT.DEFAULT);
   331             
   331 
   332             // Adjust title size only if the adjustment causes line count to
   332             // Adjust title size only if the adjustment causes line count to
   333             // change (i.e. title height increases)
   333             // change (i.e. title height increases)
   334             if (newTitle.y > res.title.y) {
   334             if (newTitle.y > res.title.y) {
   335                 res.title = newTitle;
   335                 res.title = newTitle;
   336             }
   336             }
   337         }
   337         }
   338         
   338 
   339         res.row1.x = res.img.x + res.title.x;
   339         res.row1.x = res.img.x + res.title.x;
   340         res.row1.y = Math.max(res.row1.y, res.img.y);
   340         res.row1.y = Math.max(res.row1.y, res.img.y);
   341         res.row1.y = Math.max(res.row1.y, res.title.y);
   341         res.row1.y = Math.max(res.row1.y, res.title.y);
   342         res.row2.x = res.child.x + res.trail.x;
   342         res.row2.x = res.child.x + res.trail.x;
   343         res.row2.y = Math.max(res.row2.y, res.trail.y);
   343         res.row2.y = Math.max(res.row2.y, res.trail.y);
   344         res.row2.y = Math.max(res.row2.y, res.child.y);
   344         res.row2.y = Math.max(res.row2.y, res.child.y);
   345     }
   345     }
   346     
   346 
   347     return res;
   347     return res;
   348 }
   348 }
   349 }
   349 }
   350 
   350 
   351 class CaptionedControlLayoutData {
   351 class CaptionedControlLayoutData {
   374  * be built by <em>bitwise OR</em> 'ing together (that is, using the
   374  * be built by <em>bitwise OR</em> 'ing together (that is, using the
   375  * <code>int</code> "|" operator) two or more of those <code>SWT</code>
   375  * <code>int</code> "|" operator) two or more of those <code>SWT</code>
   376  * style constants. The class description lists the style constants that are
   376  * style constants. The class description lists the style constants that are
   377  * applicable to the class. Style bits are also inherited from superclasses.
   377  * applicable to the class. Style bits are also inherited from superclasses.
   378  * </p>
   378  * </p>
   379  * 
   379  *
   380  * @param parent
   380  * @param parent
   381  *            a widget which will be the parent of the new instance (cannot
   381  *            a widget which will be the parent of the new instance (cannot
   382  *            be null)
   382  *            be null)
   383  * @param style
   383  * @param style
   384  *            the style of widget to construct
   384  *            the style of widget to construct
   385  * 
   385  *
   386  * @exception IllegalArgumentException
   386  * @exception IllegalArgumentException
   387  *                <ul>
   387  *                <ul>
   388  *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
   388  *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
   389  *                </ul>
   389  *                </ul>
   390  * @exception SWTException
   390  * @exception SWTException
   392  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
   392  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
   393  *                thread that created the parent</li>
   393  *                thread that created the parent</li>
   394  *                <li>ERROR_INVALID_SUBCLASS - if this class is not an
   394  *                <li>ERROR_INVALID_SUBCLASS - if this class is not an
   395  *                allowed subclass</li>
   395  *                allowed subclass</li>
   396  *                </ul>
   396  *                </ul>
   397  * 
   397  *
   398  * @see SWT#LEFT_TO_RIGHT
   398  * @see SWT#LEFT_TO_RIGHT
   399  * @see SWT#RIGHT_TO_LEFT
   399  * @see SWT#RIGHT_TO_LEFT
   400  */
   400  */
   401 public CaptionedControl (Composite parent, int style) {
   401 public CaptionedControl (Composite parent, int style) {
   402     super(parent, Internal_PackageSupport.checkBits(style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0), 0, 
   402     super(parent, Internal_PackageSupport.checkBits(style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0), 0,
   403             new CaptionedControlPackageProxy(), false);
   403             new CaptionedControlPackageProxy(), false);
   404     if (parent instanceof CaptionedControl) {
   404     if (parent instanceof CaptionedControl) {
   405         SWT.error(SWT.ERROR_INVALID_PARENT);
   405         SWT.error(SWT.ERROR_INVALID_PARENT);
   406     }
   406     }
   407     Internal_PackageSupport.setScrollAreaHandle(this, 0);
   407     Internal_PackageSupport.setScrollAreaHandle(this, 0);
   412     } else {
   412     } else {
   413         titleLabel = new Label(this, SWT.CENTER);
   413         titleLabel = new Label(this, SWT.CENTER);
   414     }
   414     }
   415     trailLabel = new Label(this, SWT.CENTER);
   415     trailLabel = new Label(this, SWT.CENTER);
   416     initPhase = false;
   416     initPhase = false;
   417     
   417 
   418         
   418 
   419 
   419 
   420     layout = new CaptionedControlLayout();
   420     layout = new CaptionedControlLayout();
   421     super.setLayout(layout);
   421     super.setLayout(layout);
   422     layout();
   422     layout();
   423 }
   423 }
   553     }
   553     }
   554 }
   554 }
   555 
   555 
   556 public Rectangle computeTrim(int x, int y, int width, int height) {
   556 public Rectangle computeTrim(int x, int y, int width, int height) {
   557     checkWidget();
   557     checkWidget();
   558     
   558 
   559     Rectangle res = new Rectangle(0, 0, 0, 0);
   559     Rectangle res = new Rectangle(0, 0, 0, 0);
   560     
   560 
   561     if (layout != null) {
   561     if (layout != null) {
   562         CaptionedControlLayoutData data = layout.getData();
   562         CaptionedControlLayoutData data = layout.getData();
   563         
   563 
   564         if ((getStyle() & SWT.HORIZONTAL) != 0) { 
   564         if ((getStyle() & SWT.HORIZONTAL) != 0) {
   565             res.width = data.img.x + data.title.x + data.trail.x + width;
   565             res.width = data.img.x + data.title.x + data.trail.x + width;
   566             res.height = height;
   566             res.height = height;
   567             res.x = x - data.img.x - data.title.x - margin;
   567             res.x = x - data.img.x - data.title.x - margin;
   568             res.y = y - margin;
   568             res.y = y - margin;
   569         }
   569         }
   575         }
   575         }
   576     }
   576     }
   577     else {
   577     else {
   578         res.width = width;
   578         res.width = width;
   579         res.height = height;
   579         res.height = height;
   580         res.x = x; 
   580         res.x = x;
   581         res.y = y;
   581         res.y = y;
   582     }
   582     }
   583     res.width += 2 * margin;
   583     res.width += 2 * margin;
   584     res.height += 2 * margin;
   584     res.height += 2 * margin;
   585     
   585 
   586     return res;
   586     return res;
   587 }
   587 }
   588 
   588 
   589 void createHandle (int index) {
   589 void createHandle (int index) {
   590     int handle = OS.QCaptionedWidget_new();
   590     int handle = OS.QCaptionedWidget_new();
   685 public Control[] getChildren() {
   685 public Control[] getChildren() {
   686     checkWidget();
   686     checkWidget();
   687     Control[] children = super.getChildren();
   687     Control[] children = super.getChildren();
   688     Control[] res = new Control[children.length - 3];
   688     Control[] res = new Control[children.length - 3];
   689     for (int i = 0, j = 0; i < children.length; i++) {
   689     for (int i = 0, j = 0; i < children.length; i++) {
   690         if (children[i] != imageLabel 
   690         if (children[i] != imageLabel
   691             && children[i] != titleLabel
   691             && children[i] != titleLabel
   692             && children[i] != trailLabel ) {
   692             && children[i] != trailLabel ) {
   693             res[j] = children[i];
   693             res[j] = children[i];
   694             j++;
   694             j++;
   695         }
   695         }
   697     return res;
   697     return res;
   698 }
   698 }
   699 
   699 
   700 public Rectangle getClientArea() {
   700 public Rectangle getClientArea() {
   701     checkWidget();
   701     checkWidget();
   702     
   702 
   703     Rectangle res = new Rectangle(0, 0, 0, 0);
   703     Rectangle res = new Rectangle(0, 0, 0, 0);
   704     
   704 
   705     Point size = getSize();
   705     Point size = getSize();
   706     size.x -= 2 * margin;
   706     size.x -= 2 * margin;
   707     size.y -= 2 * margin;    
   707     size.y -= 2 * margin;
   708     
   708 
   709     if (layout != null) {
   709     if (layout != null) {
   710         
   710 
   711         CaptionedControlLayoutData data = layout.getData(size.x, size.y);
   711         CaptionedControlLayoutData data = layout.getData(size.x, size.y);
   712         if ((getStyle() & SWT.HORIZONTAL) != 0) {
   712         if ((getStyle() & SWT.HORIZONTAL) != 0) {
   713             res.width = size.x - data.img.x - data.title.x - data.trail.x;
   713             res.width = size.x - data.img.x - data.title.x - data.trail.x;
   714             res.height = size.y;
   714             res.height = size.y;
   715             res.x = data.img.x + data.title.x + margin;
   715             res.x = data.img.x + data.title.x + margin;
   716             res.y = margin;
   716             res.y = margin;
   717         }
   717         }
   718         else {
   718         else {
   719             res.width = size.x - data.trail.x; 
   719             res.width = size.x - data.trail.x;
   720             res.height = size.y - data.row1.y;
   720             res.height = size.y - data.row1.y;
   721             res.x = margin;
   721             res.x = margin;
   722             res.y = data.row1.y + margin;
   722             res.y = data.row1.y + margin;
   723         }
   723         }
   724     }
   724     }
   725     else {
   725     else {
   726         res.width = size.x;
   726         res.width = size.x;
   727         res.height = size.y;
   727         res.height = size.y;
   728     }
   728     }
   729     
   729 
   730     if (res.width < 0) res.width = 0;
   730     if (res.width < 0) res.width = 0;
   731     if (res.height < 0) res.height = 0;
   731     if (res.height < 0) res.height = 0;
   732     
   732 
   733     return res;
   733     return res;
   734 }
   734 }
   735 
   735 
   736 /**
   736 /**
   737  * Returns the CaptionedControl's icon image, or null if it has never been
   737  * Returns the CaptionedControl's icon image, or null if it has never been
   738  * set.
   738  * set.
   739  * 
   739  *
   740  * @return the icon image or null.
   740  * @return the icon image or null.
   741  * 
   741  *
   742  * @exception SWTException
   742  * @exception SWTException
   743  *                <ul>
   743  *                <ul>
   744  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
   744  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
   745  *                disposed</li>
   745  *                disposed</li>
   746  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
   746  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
   754 }
   754 }
   755 
   755 
   756 /**
   756 /**
   757  * Gets the caption text, which will be an empty string if it has never been
   757  * Gets the caption text, which will be an empty string if it has never been
   758  * set.
   758  * set.
   759  * 
   759  *
   760  * @return The label text.
   760  * @return The label text.
   761  * @exception SWTException
   761  * @exception SWTException
   762  *                <ul>
   762  *                <ul>
   763  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
   763  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
   764  *                disposed</li>
   764  *                disposed</li>
   765  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
   765  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
   766  *                thread that created the parent</li>
   766  *                thread that created the parent</li>
   767  *                </ul>
   767  *                </ul>
   768  * 
   768  *
   769  * @see #setText(java.lang.String)
   769  * @see #setText(java.lang.String)
   770  */
   770  */
   771 public String getText () {
   771 public String getText () {
   772     checkWidget();
   772     checkWidget();
   773     return titleLabel.getText();
   773     return titleLabel.getText();
   774 }
   774 }
   775 
   775 
   776 /**
   776 /**
   777  * Gets the trailing text, which will be an empty string if it has never
   777  * Gets the trailing text, which will be an empty string if it has never
   778  * been set.
   778  * been set.
   779  * 
   779  *
   780  * @return The trailing text.
   780  * @return The trailing text.
   781  * 
   781  *
   782  * @exception SWTException
   782  * @exception SWTException
   783  *                <ul>
   783  *                <ul>
   784  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
   784  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
   785  *                disposed</li>
   785  *                disposed</li>
   786  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
   786  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
   787  *                thread that created the parent</li>
   787  *                thread that created the parent</li>
   788  *                </ul>
   788  *                </ul>
   789  * 
   789  *
   790  * @see #setTrailingText(java.lang.String)
   790  * @see #setTrailingText(java.lang.String)
   791  */
   791  */
   792 public String getTrailingText () {
   792 public String getTrailingText () {
   793     checkWidget();
   793     checkWidget();
   794     return trailLabel.getText();
   794     return trailLabel.getText();
   845 }
   845 }
   846 
   846 
   847 /**
   847 /**
   848  * Sets the image as an icon to the CaptionedControl. The icon can co-exist
   848  * Sets the image as an icon to the CaptionedControl. The icon can co-exist
   849  * with caption text. The icon position is platform-dependent.
   849  * with caption text. The icon position is platform-dependent.
   850  * 
   850  *
   851  * <p>
   851  * <p>
   852  * The parameter can be null indicating that no image should be displayed.
   852  * The parameter can be null indicating that no image should be displayed.
   853  * The implementation will adjust the image size to make it best fit the
   853  * The implementation will adjust the image size to make it best fit the
   854  * CaptionedControl.
   854  * CaptionedControl.
   855  * </p>
   855  * </p>
   856  * 
   856  *
   857  * @param image
   857  * @param image
   858  *            the image to display on the receiver
   858  *            the image to display on the receiver
   859  * 
   859  *
   860  * @exception IllegalArgumentException
   860  * @exception IllegalArgumentException
   861  *                <ul>
   861  *                <ul>
   862  *                <li>ERROR_NULL_ARGUMENT - if the image is null</li>
       
   863  *                <li>ERROR_INVALID_ARGUMENT - if the image has been
   862  *                <li>ERROR_INVALID_ARGUMENT - if the image has been
   864  *                disposed</li>
   863  *                disposed</li>
   865  *                </ul>
   864  *                </ul>
   866  * @exception SWTException
   865  * @exception SWTException
   867  *                <ul>
   866  *                <ul>
   868  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
   867  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
   869  *                disposed</li>
   868  *                disposed</li>
   870  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
   869  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
   871  *                thread that created the receiver</li>
   870  *                thread that created the receiver</li>
   872  *                </ul>
   871  *                </ul>
   873  * 
   872  *
   874  */
   873  */
   875 public void setImage (Image image) {
   874 public void setImage (Image image) {
   876     checkWidget();
   875     checkWidget();
   877     if (image == null) {
       
   878         SWT.error(SWT.ERROR_NULL_ARGUMENT);
       
   879     }
       
   880     if (image != null && image.isDisposed()) {
   876     if (image != null && image.isDisposed()) {
   881         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
   877         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
   882     }
   878     }
   883     imageLabel.setImage(image);
   879     imageLabel.setImage(image);
   884     layout();
   880     layout();
   890     checkWidget();
   886     checkWidget();
   891 }
   887 }
   892 
   888 
   893 /**
   889 /**
   894  * Sets the caption label
   890  * Sets the caption label
   895  * 
   891  *
   896  * @param string
   892  * @param string
   897  *            the new caption label
   893  *            the new caption label
   898  * 
   894  *
   899  * @throws java.lang.IllegalArgumentException
   895  * @throws java.lang.IllegalArgumentException
   900  *             <code>ERROR_NULL_ARGUMENT</code> if the text is null
   896  *             <code>ERROR_NULL_ARGUMENT</code> if the text is null
   901  * @exception SWTException
   897  * @exception SWTException
   902  *                <ul>
   898  *                <ul>
   903  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
   899  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
   916     layout();
   912     layout();
   917 }
   913 }
   918 
   914 
   919 /**
   915 /**
   920  * Sets the trailing label
   916  * Sets the trailing label
   921  * 
   917  *
   922  * @param string
   918  * @param string
   923  *            the new trailing label
   919  *            the new trailing label
   924  * 
   920  *
   925  * @exception IllegalArgumentException
   921  * @exception IllegalArgumentException
   926  *                <ul>
   922  *                <ul>
   927  *                <li>ERROR_NULL_ARGUMENT - if the string is null</li>
   923  *                <li>ERROR_NULL_ARGUMENT - if the string is null</li>
   928  *                </ul>
   924  *                </ul>
   929  * @exception SWTException
   925  * @exception SWTException
   931  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
   927  *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
   932  *                disposed</li>
   928  *                disposed</li>
   933  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
   929  *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
   934  *                thread that created the parent</li>
   930  *                thread that created the parent</li>
   935  *                </ul>
   931  *                </ul>
   936  * 
   932  *
   937  * @see #getTrailingText
   933  * @see #getTrailingText
   938  */
   934  */
   939 public void setTrailingText (java.lang.String string) {
   935 public void setTrailingText (java.lang.String string) {
   940     checkWidget();
   936     checkWidget();
   941     if (string == null) {
   937     if (string == null) {
   944     trailLabel.setText(string);
   940     trailLabel.setText(string);
   945     layout();
   941     layout();
   946 }
   942 }
   947 
   943 
   948 private final int handle() {
   944 private final int handle() {
   949     return Internal_PackageSupport.handle(this); 
   945     return Internal_PackageSupport.handle(this);
   950 }
   946 }
   951 
   947 
   952 
   948 
   953 void hookFocusChangedEvent() {
   949 void hookFocusChangedEvent() {
   954     focusSignalProxy = OS.SignalHandler_new(topHandle(),
   950     focusSignalProxy = OS.SignalHandler_new(topHandle(),
   955             Internal_PackageSupport.display(this),
       
   956             OS.QSIGNAL_QAPPLICATION_FOCUSCHANGED_TO_WIDDGET);
   951             OS.QSIGNAL_QAPPLICATION_FOCUSCHANGED_TO_WIDDGET);
   957     OS.QObject_connectOrThrow(OS.QCoreApplication_instance(),
   952     OS.QObject_connectOrThrow(OS.QCoreApplication_instance(),
   958             "focusChanged(QWidget*, QWidget*)", focusSignalProxy,
   953             "focusChanged(QWidget*, QWidget*)", focusSignalProxy,
   959             "widgetSignal(QWidget*, QWidget*)", OS.QT_AUTOCONNECTION);
   954             "widgetSignal(QWidget*, QWidget*)", OS.QT_AUTOCONNECTION);
   960 }
   955 }