diff dwt/widgets/Label.d @ 45:d8635bb48c7c

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents e831403a80a9
children cfa563df4fdd
line wrap: on
line diff
--- a/dwt/widgets/Label.d	Tue Oct 21 15:20:04 2008 +0200
+++ b/dwt/widgets/Label.d	Mon Dec 01 17:07:00 2008 +0100
@@ -1,5 +1,5 @@
-/*******************************************************************************
- * Copyright (c) 2000, 2006 IBM Corporation and others.
+/*******************************************************************************
+ * Copyright (c) 2000, 2008 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -15,29 +15,42 @@
 
 import dwt.DWT;
 import dwt.DWTException;
+import dwt.accessibility.ACC;
 import dwt.graphics.Image;
 import dwt.graphics.Point;
-import dwt.graphics.Rectangle;
+import dwt.internal.cocoa.NSArray;
 import dwt.internal.cocoa.NSAttributedString;
 import dwt.internal.cocoa.NSBox;
 import dwt.internal.cocoa.NSCell;
 import dwt.internal.cocoa.NSColor;
+import dwt.internal.cocoa.NSFont;
+import dwt.internal.cocoa.NSImage;
 import dwt.internal.cocoa.NSImageView;
+import dwt.internal.cocoa.NSMutableArray;
 import dwt.internal.cocoa.NSMutableDictionary;
 import dwt.internal.cocoa.NSRect;
+import dwt.internal.cocoa.NSSize;
 import dwt.internal.cocoa.NSString;
 import dwt.internal.cocoa.NSTextField;
 import dwt.internal.cocoa.NSTextFieldCell;
+import dwt.internal.cocoa.NSView;
 import dwt.internal.cocoa.OS;
 import dwt.internal.cocoa.SWTBox;
 import dwt.internal.cocoa.SWTImageView;
 import dwt.internal.cocoa.SWTTextField;
+import dwt.internal.cocoa.id;
 
 /**
  * Instances of this class represent a non-selectable
  * user interface object that displays a string or image.
  * When SEPARATOR is specified, displays a single
  * vertical or horizontal line.
+ * <p>
+ * Shadow styles are hints and may not be honoured
+ * by the platform.  To create a separator label
+ * with the default shadow style for the platform,
+ * do not specify a shadow style.
+ * </p>
  * <dl>
  * <dt><b>Styles:</b></dt>
  * <dd>SEPARATOR, HORIZONTAL, VERTICAL</dd>
@@ -54,6 +67,10 @@
  * IMPORTANT: This class is intended to be subclassed <em>only</em>
  * within the DWT implementation.
  * </p>
+ *
+ * @see <a href="http://www.eclipse.org/swt/snippets/#label">Label snippets</a>
+ * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample</a>
+ * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
  */
 public class Label : Control {
     String text = "";
@@ -103,6 +120,50 @@
     super (parent, checkStyle (style));
 }
 
+int accessibilityAttributeNames(int /*long*/ id, int /*long*/ sel) {
+    
+    if (accessible !is null) {      
+        if ((textView !is null && (id is textView.id || id is textView.cell().id)) || (imageView !is null && (id is imageView.id || id is imageView.cell().id))) {
+            // See if the accessible will override or augment the standard list.
+            // Help, title, and description can be overridden.
+            NSMutableArray extraAttributes = NSMutableArray.arrayWithCapacity(3);
+            extraAttributes.addObject(OS.NSAccessibilityHelpAttribute);
+            extraAttributes.addObject(OS.NSAccessibilityDescriptionAttribute);
+            extraAttributes.addObject(OS.NSAccessibilityTitleAttribute);
+
+            for (int i = extraAttributes.count() - 1; i >= 0; i--) {
+                NSString attribute = new NSString(extraAttributes.objectAtIndex(i).id);
+                if (accessible.internal_accessibilityAttributeValue(attribute, ACC.CHILDID_SELF) is null) {
+                    extraAttributes.removeObjectAtIndex(i);
+                }
+            }
+
+            if (extraAttributes.count() > 0) {
+                int superResult = super.accessibilityAttributeNames(id, sel);
+                NSArray baseAttributes = new NSArray(superResult);
+                NSMutableArray mutableAttributes = NSMutableArray.arrayWithCapacity(baseAttributes.count() + 1);
+                mutableAttributes.addObjectsFromArray(baseAttributes);
+                
+                for (int i = 0; i < extraAttributes.count(); i++) {
+                    id currAttribute = extraAttributes.objectAtIndex(i);
+                    if (!mutableAttributes.containsObject(currAttribute)) {
+                        mutableAttributes.addObject(currAttribute);
+                    }
+                }
+                
+                return mutableAttributes.id;
+            }
+        }
+    }
+
+    return super.accessibilityAttributeNames(id, sel);
+}
+
+bool accessibilityIsIgnored(int /*long*/ id, int /*long*/ sel) {
+    if (id is view.id) return true;
+    return super.accessibilityIsIgnored(id, sel);   
+}
+
 static int checkStyle (int style) {
     style |= DWT.NO_FOCUS;
     if ((style & DWT.SEPARATOR) !is 0) {
@@ -114,27 +175,32 @@
 
 public Point computeSize (int wHint, int hHint, bool changed) {
     checkWidget();
-    int width = 0, height = 0;
+    NSRect oldRect = view.frame ();
+    int width = DEFAULT_WIDTH;
+    int height = DEFAULT_HEIGHT;
     if ((style & DWT.SEPARATOR) !is 0) {
-        if ((style & DWT.HORIZONTAL) !is 0) {
-            width = DEFAULT_WIDTH;
-            height = 3;
-        } else {
-            width = 3;
-            height = DEFAULT_HEIGHT;
-        }
+        ((NSBox) view).sizeToFit ();
+        NSRect newRect = view.frame ();
+        width = (int) newRect.width;
+        height = (int) newRect.height;
+        view.setFrame (oldRect);
     } else {
-        if (image !is null && isImage) {
-            Rectangle bounds = image.getBounds();
-            width = bounds.width;
-            height = bounds.height;
+        if (isImage) {
+            if (image !is null) {
+                NSImage nsimage = image.handle;
+                NSSize size = nsimage.size ();
+                width = (int)size.width;
+                height = (int)size.height;
+            } else {
+                width = height = 0;
+            }
         } else {
-            NSRect oldRect = textView.frame();
-            textView.sizeToFit();
-            NSRect newRect = textView.frame();
-            textView.setFrame (oldRect);
-            width = cast(int)newRect.width;
-            height = cast(int)newRect.height;
+            NSRect rect = new NSRect ();
+            rect.width = wHint !is DWT.DEFAULT ? wHint : Float.MAX_VALUE;
+            rect.height = hHint !is DWT.DEFAULT ? hHint : Float.MAX_VALUE;
+            NSSize size = textView.cell ().cellSizeForBounds (rect);
+            width = cast(int)Math.ceil (size.width);
+            height = cast(int)Math.ceil (size.height);
         }
     }
     if (wHint !is DWT.DEFAULT) width = wHint;
@@ -143,32 +209,34 @@
 }
 
 void createHandle () {
-    SWTBox widget = cast(SWTBox)new SWTBox().alloc();
+    NSBox widget = cast(NSBox)(new SWTBox()).alloc();
     widget.initWithFrame(new NSRect());
-    widget.setTag(jniRef);
     widget.setTitle(NSString.stringWith(""));
     if ((style & DWT.SEPARATOR) !is 0) {
         widget.setBoxType(OS.NSBoxSeparator);
+        NSView child = (NSView) new NSView().alloc().init().autorelease();
+        widget.setContentView(child);
     } else {
         widget.setBorderType(OS.NSNoBorder);
+        widget.setBorderWidth (0);
+        widget.setBoxType (OS.NSBoxCustom);
+        NSSize offsetSize = new NSSize ();
+        widget.setContentViewMargins (offsetSize);
 
-        NSImageView imageWidget = cast(NSImageView)new SWTImageView().alloc();
-        imageWidget.initWithFrame(new NSRect());
-        imageWidget.setTag(jniRef);
+        NSImageView imageWidget = cast(NSImageView) new SWTImageView ().alloc ();
+        imageWidget.initWithFrame(new NSRect ());
+        imageWidget.setImageScaling (OS.NSScaleNone);
         
-        SWTTextField textWidget = cast(SWTTextField)new SWTTextField().alloc();
+        NSTextField textWidget = cast(NSTextField)(new SWTTextField()).alloc();
         textWidget.initWithFrame(new NSRect());
         textWidget.setBordered(false);
         textWidget.setEditable(false);
         textWidget.setDrawsBackground(false);
-        textWidget.setTag(jniRef);
-        if ((style & DWT.WRAP) !is 0) {
-            NSTextFieldCell cell = new NSTextFieldCell(textWidget.cell());
-            cell.setWraps(true);
-        }
+        NSTextFieldCell cell = new NSTextFieldCell(textWidget.cell());
+        cell.setWraps ((style & DWT.WRAP) !is 0);
         
-        widget.addSubview_(imageWidget);
-        widget.addSubview_(textWidget);
+        widget.addSubview(imageWidget);
+        widget.addSubview(textWidget);
         widget.setContentView(textWidget);
         
         imageView = imageWidget;
@@ -176,28 +244,39 @@
         _setAlignment();
     }
     view = widget;
-    parent.contentView().addSubview_(widget);
 }
 
 NSAttributedString createString() {
     NSMutableDictionary dict = NSMutableDictionary.dictionaryWithCapacity(4);
     if (foreground !is null) {
         NSColor color = NSColor.colorWithDeviceRed(foreground.handle[0], foreground.handle[1], foreground.handle[2], 1);
-        dict.setObject(color, OS.NSForegroundColorAttributeName());
+        dict.setObject(color, OS.NSForegroundColorAttributeName);
     }
     if (font !is null) {
-        dict.setObject(font.handle, OS.NSFontAttributeName());
+        dict.setObject(font.handle, OS.NSFontAttributeName);
     }
     char [] chars = new char [text.length ()];
     text.getChars (0, chars.length, chars, 0);
     int length = fixMnemonic (chars);
 
     NSString str = NSString.stringWithCharacters(chars, length);
-    NSAttributedString attribStr = (cast(NSAttributedString)new NSAttributedString().alloc()).initWithString_attributes_(str, dict);
+    NSAttributedString attribStr = (cast(NSAttributedString)new NSAttributedString().alloc()).initWithString(str, dict);
     attribStr.autorelease();
     return attribStr;
 }
 
+void deregister () {
+    super.deregister ();
+    if (textView !is null) {
+        display.removeWidget(textView);
+        display.removeWidget(textView.cell());
+    }
+    if (imageView !is null) {
+        display.removeWidget (imageView);
+        display.removeWidget (imageView.cell());
+    }
+}
+
 /**
  * Returns a value which describes the position of the
  * text or image in the receiver. The value will be one of
@@ -258,6 +337,26 @@
     return text;
 }
 
+void register () {
+    super.register ();
+    if (textView !is null) {
+        display.addWidget (textView, this);
+        display.addWidget (textView.cell(), this);
+    }
+    if (imageView !is null) {
+        display.addWidget (imageView, this);
+        display.addWidget (imageView.cell(), this);
+    }
+}
+
+void releaseHandle () {
+    super.releaseHandle ();
+    if (textView !is null) textView.release();
+    if (imageView !is null) imageView.release();
+    textView = null;
+    imageView = null;
+}
+
 /**
  * Controls how text and images will be displayed in the receiver.
  * The argument should be one of <code>LEFT</code>, <code>RIGHT</code>
@@ -285,8 +384,7 @@
     textView.setDrawsBackground(color !is null);
     if (color is null) return;
     NSColor nsColor = NSColor.colorWithDeviceRed(color[0], color[1], color[2], 1);
-    NSTextFieldCell cell = new NSTextFieldCell(textView.cell());
-    cell.setBackgroundColor(nsColor);
+    ((NSTextField)textView).setBackgroundColor(nsColor);
 }
 
 void _setAlignment() {
@@ -304,16 +402,11 @@
     }
 }
 
-int setBounds (int x, int y, int width, int height, bool move, bool resize) {
-    int result = super.setBounds(x, y, width, height, move, resize);
-    if ((result & RESIZED) !is 0) {
-        if (imageView !is null || textView !is null) {
-            NSRect rect = view.bounds();
-            imageView.setFrame(rect);
-            textView.setFrame(rect);
-        }
+void setFont(NSFont font) {
+    if (textView !is null) {
+        NSCell cell = new NSCell(textView.cell());
+        cell.setAttributedStringValue(createString());
     }
-    return result;
 }
 
 void setForeground (float [] color) {
@@ -390,4 +483,5 @@
     (cast(NSBox)view).setContentView(textView);
 }
 
+
 }