diff dwt/widgets/Spinner.d @ 77:990305995bc6

Ported dwt.widgets.Spinner
author Jacob Carlborg <doob@me.com>
date Wed, 24 Dec 2008 13:06:45 +0100
parents cfa563df4fdd
children c7f7f4d7091a
line wrap: on
line diff
--- a/dwt/widgets/Spinner.d	Wed Dec 24 12:32:20 2008 +0100
+++ b/dwt/widgets/Spinner.d	Wed Dec 24 13:06:45 2008 +0100
@@ -7,6 +7,9 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ *     
+ * Port to the D programming language:
+ *     Jacob Carlborg <doob@me.com>
  *******************************************************************************/
 module dwt.widgets.Spinner;
 
@@ -39,6 +42,12 @@
 import dwt.internal.cocoa.SWTTextField;
 import dwt.internal.cocoa.SWTView;
 
+import dwt.internal.c.Carbon;
+import objc = dwt.internal.objc.runtime;
+import dwt.widgets.Composite;
+import dwt.widgets.Event;
+import dwt.widgets.TypedListener;
+
 /**
  * Instances of this class are selectable user interface
  * objects that allow the user to enter and modify numeric
@@ -105,7 +114,7 @@
     super (parent, checkStyle (style));
 }
 
-bool acceptsFirstResponder(int /*long*/ id, int /*long*/ sel) {
+bool acceptsFirstResponder(objc.id id, objc.SEL sel) {
     if (id is view.id) return false;
     return super.acceptsFirstResponder (id, sel);
 }
@@ -211,19 +220,19 @@
 
 public Point computeSize (int wHint, int hHint, bool changed) {
     checkWidget ();
-    float width = 0, height = 0;
+    CGFloat width = 0, height = 0;
     String string = Double.toString (buttonView.maxValue ());
     NSMutableDictionary dict = NSMutableDictionary.dictionaryWithCapacity (1);
     dict.setObject(textView.font (), OS.NSFontAttributeName);
     int length = string.length ();
     char [] chars = new char [length];
     string.getChars (0, length, chars, 0);
-    NSString nsString = NSString.stringWithCharacters (chars, length);
-    NSAttributedString str = (cast(NSAttributedString) new NSAttributedString ().alloc ()).initWithString (nsString, dict);
+    NSString nsString = NSString.stringWithCharacters (chars.toString16().ptr, length);
+    NSAttributedString str = (cast(NSAttributedString) (new NSAttributedString ()).alloc ()).initWithString (nsString, dict);
     NSSize size = str.size ();
     str.release ();
-    width = (int)/*64*/size.width;
-    height = (int)/*64*/size.height;
+    width = size.width;
+    height = size.height;
     NSRect frameRect = textView.frame();
     NSCell cell = new NSCell (textView.cell ());
     NSRect cellRect = cell.drawingRectForBounds(frameRect);
@@ -235,7 +244,7 @@
     NSRect newRect = buttonView.frame ();
     buttonView.setFrame (oldRect);
     width += newRect.width;
-    height = Math.max (height, (int)/*64*/newRect.height);
+    height = Math.max (height, newRect.height);
     if (wHint !is DWT.DEFAULT) width = wHint;
     if (hHint !is DWT.DEFAULT) height = hHint;
     Rectangle trim = computeTrim (0, 0, cast(int) width, cast(int) height);
@@ -271,15 +280,15 @@
 }
 
 void createHandle () {
-    NSView widget = cast(NSView)new SWTView().alloc();
+    NSView widget = cast(NSView)(new SWTView()).alloc();
     widget.initWithFrame(NSRect());
 //  widget.setDrawsBackground(false);
-    NSStepper buttonWidget = cast(NSStepper)new SWTStepper().alloc();
+    NSStepper buttonWidget = cast(NSStepper)(new SWTStepper()).alloc();
     buttonWidget.initWithFrame(NSRect());
     buttonWidget.setValueWraps((style & DWT.WRAP) !is 0);
     buttonWidget.setTarget(buttonWidget);
     buttonWidget.setAction(OS.sel_sendSelection);
-    NSTextField textWidget = cast(NSTextField)new SWTTextField().alloc();
+    NSTextField textWidget = cast(NSTextField)(new SWTTextField()).alloc();
     textWidget.initWithFrame(NSRect());
 //  textWidget.setTarget(widget);
     textWidget.setEditable((style & DWT.READ_ONLY) is 0);
@@ -473,12 +482,12 @@
     return -1;
 }
 
-void keyDown (int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) {
+void keyDown (objc.id id, objc.SEL sel, objc.id theEvent) {
     NSEvent event = new NSEvent(theEvent);
     NSString chars = event.charactersIgnoringModifiers();
     
     int delta = 0;
-    int keyChar = 0;
+    wchar keyChar = 0;
 
     if (chars.length() !is 1) return;
     
@@ -496,7 +505,7 @@
         
         default: {
             NSCharacterSet numbers = new NSCharacterSet(NSCharacterSet.decimalDigitCharacterSet().id);
-            bool isANumber = numbers.characterIsMember((short) keyChar);
+            bool isANumber = numbers.characterIsMember(cast(short) keyChar);
             bool isSeparator = (keyChar is textFormatter.decimalSeparator().characterAtIndex(0));
             bool isMathSymbol = (keyChar is 0x2d || keyChar is 0x2b); // Minus sign, plus sign
             if (isANumber || (isSeparator && digits > 0) || isMathSymbol) super.keyDown(id, sel, theEvent);
@@ -507,11 +516,11 @@
         bool [] parseFail = new bool [1];
         int value = getSelectionText (parseFail);
         if (parseFail [0]) {
-            value = (int)buttonView.doubleValue();
+            value = cast(int)buttonView.doubleValue();
         }
         int newValue = value + delta;
-        int max = (int)buttonView.maxValue();
-        int min = (int)buttonView.minValue();
+        int max = cast(int)buttonView.maxValue();
+        int min = cast(int)buttonView.minValue();
         if ((style & DWT.WRAP) !is 0) {
             if (newValue > max) newValue = min;
             if (newValue < min) newValue = max;
@@ -523,7 +532,7 @@
         bool [] parseFail = new bool [1];
         int value = getSelectionText (parseFail);
         if (!parseFail [0]) {
-            int pos = (int)buttonView.doubleValue();
+            int pos = cast(int)buttonView.doubleValue();
             if (pos !is value) setSelection (value, true, false, true);
         }
     }
@@ -689,7 +698,7 @@
     if (value < 0) error (DWT.ERROR_INVALID_ARGUMENT);
     if (value is digits) return;
     digits = value;
-    int pos = (int)buttonView.doubleValue();    
+    int pos = cast(int)buttonView.doubleValue();    
     textFormatter.setMaximumFractionDigits(digits);
     setSelection (pos, false, true, false);
 }
@@ -808,7 +817,7 @@
         (cast(NSStepper)buttonView).setDoubleValue(value);
     }
     if (setText) {
-        String string = String.valueOf (value);
+        String string = String_.valueOf (value);
         if (digits > 0) {
             String decimalSeparator = textFormatter.decimalSeparator().getString();
             int index = string.length () - digits;
@@ -827,7 +836,7 @@
         }
         NSCell cell = new NSCell(textView.cell());
         if (hooks (DWT.Verify) || filters (DWT.Verify)) {
-            int length = (int)/*64*/cell.title().length();
+            int length = cast(int)/*64*/cell.title().length();
             string = verifyText (string, 0, length, null);
             if (string is null) return;
         }
@@ -876,15 +885,15 @@
     setSelection (selection, true, true, false);
 }
 
-void textDidChange (int /*long*/ id, int /*long*/ sel, int /*long*/ aNotification) {
+void textDidChange (objc.id id, objc.SEL sel, objc.id aNotification) {
     postEvent (DWT.Modify);
 }
 
-void textDidEndEditing(int /*long*/ id, int /*long*/ sel, int /*long*/ aNotification) {
+void textDidEndEditing(objc.id id, objc.SEL sel, objc.id aNotification) {
     bool [] parseFail = new bool [1];
     int value = getSelectionText (parseFail);
     if (parseFail [0]) {
-        value = (int)buttonView.doubleValue();
+        value = cast(int)buttonView.doubleValue();
         setSelection (value, false, true, false);
     }
 }