changeset 121:e1c48e37e0f5

Ported dwt.program.Program
author Jacob Carlborg <doob@me.com>
date Wed, 31 Dec 2008 16:50:37 +0100
parents 7046ca5a6d77
children 2e671fa40eec
files dwt/internal/c/Carbon.d dwt/internal/cocoa/NSFileManager.d dwt/internal/cocoa/NSWorkspace.d dwt/internal/cocoa/OS.d dwt/internal/objc/cocoa/Cocoa.d dwt/internal/objc/cocoa/bindings.d dwt/program/Program.d
diffstat 7 files changed, 110 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/dwt/internal/c/Carbon.d	Wed Dec 31 16:16:40 2008 +0100
+++ b/dwt/internal/c/Carbon.d	Wed Dec 31 16:50:37 2008 +0100
@@ -220,7 +220,24 @@
 
 
 // CFString.h
-alias uint CFStringEncoding;
+enum CFStringEncoding // CFStringBuiltInEncodings
+{
+    kCFStringEncodingMacRoman = 0,
+    kCFStringEncodingWindowsLatin1 = 0x0500,
+    kCFStringEncodingISOLatin1 = 0x0201,
+    kCFStringEncodingNextStepLatin = 0x0B01,
+    kCFStringEncodingASCII = 0x0600,
+    kCFStringEncodingUnicode = 0x0100,
+    kCFStringEncodingUTF8 = 0x08000100,
+    kCFStringEncodingNonLossyASCII = 0x0BFF,
+    
+    kCFStringEncodingUTF16 = 0x0100,
+    kCFStringEncodingUTF16BE = 0x10000100,
+    kCFStringEncodingUTF16LE = 0x14000100,
+    kCFStringEncodingUTF32 = 0x0c000100,
+    kCFStringEncodingUTF32BE = 0x18000100,
+    kCFStringEncodingUTF32LE = 0x1c000100
+}
 
 
 
--- a/dwt/internal/cocoa/NSFileManager.d	Wed Dec 31 16:16:40 2008 +0100
+++ b/dwt/internal/cocoa/NSFileManager.d	Wed Dec 31 16:50:37 2008 +0100
@@ -52,8 +52,8 @@
     return result !is null ? new NSDirectoryEnumerator(result) : null;
 }
 
-public bool removeItemAtPath(NSString path, ref NSError error) {
-    return OS.objc_msgSend_bool(this.id, OS.sel_removeItemAtPath_error_, path !is null ? path.id : null, error !is null ? &error.id : null);
+public bool removeItemAtPath(NSString path, objc.id* error) {
+    return OS.objc_msgSend_bool(this.id, OS.sel_removeItemAtPath_error_, path !is null ? path.id : null, error !is null ? error : null);
 }
 
 }
--- a/dwt/internal/cocoa/NSWorkspace.d	Wed Dec 31 16:16:40 2008 +0100
+++ b/dwt/internal/cocoa/NSWorkspace.d	Wed Dec 31 16:50:37 2008 +0100
@@ -59,7 +59,7 @@
 }
 
 public bool getInfoForFile(NSString fullPath, ref objc.id appName, ref objc.id type) {
-    return OS.objc_msgSend_bool(this.id, OS.sel_getInfoForFile_application_type_, fullPath !is null ? fullPath.id : null, appName, type);
+    return OS.objc_msgSend_bool(this.id, OS.sel_getInfoForFile_application_type_, fullPath !is null ? fullPath.id : null, &appName, &type);
 }
 
 public NSImage iconForFile(NSString fullPath) {
--- a/dwt/internal/cocoa/OS.d	Wed Dec 31 16:16:40 2008 +0100
+++ b/dwt/internal/cocoa/OS.d	Wed Dec 31 16:50:37 2008 +0100
@@ -3626,12 +3626,12 @@
 public static const int NSWritingDirectionRightToLeft = 1;
 alias NSDatePickerElementFlags.NSYearMonthDatePickerElementFlag NSYearMonthDatePickerElementFlag;
 alias NSDatePickerElementFlags.NSYearMonthDayDatePickerElementFlag NSYearMonthDayDatePickerElementFlag;
-public static const int kCFStringEncodingUTF8 = 134217984;
+alias Carbon.CFStringEncoding.kCFStringEncodingUTF8 kCFStringEncodingUTF8;
 public static const int NSASCIIStringEncoding = 1;
 public static const int NSAdminApplicationDirectory = 4;
 public static const int NSAggregateExpressionType = 14;
-public static const int NSAllApplicationsDirectory = 100;
-public static const int NSAllDomainsMask = 65535;
+alias Cocoa.NSSearchPathDirectory.NSAllApplicationsDirectory NSAllApplicationsDirectory;
+alias Cocoa.NSSearchPathDomainMask.NSAllDomainsMask NSAllDomainsMask;
 public static const int NSAllLibrariesDirectory = 101;
 public static const int NSAllPredicateModifier = 1;
 public static const int NSAnchoredSearch = 8;
--- a/dwt/internal/objc/cocoa/Cocoa.d	Wed Dec 31 16:16:40 2008 +0100
+++ b/dwt/internal/objc/cocoa/Cocoa.d	Wed Dec 31 16:50:37 2008 +0100
@@ -147,6 +147,35 @@
     }
 }
 
+enum NSSearchPathDirectory
+{
+    NSApplicationDirectory = 1,
+    NSDemoApplicationDirectory,
+    NSDeveloperApplicationDirectory,
+    NSAdminApplicationDirectory,
+    NSLibraryDirectory,
+    NSDeveloperDirectory,
+    NSUserDirectory,
+    NSDocumentationDirectory,
+    NSDocumentDirectory,
+    NSCoreServiceDirectory,
+    NSDesktopDirectory = 12,
+    NSCachesDirectory = 13,
+    NSApplicationSupportDirectory = 14,
+    NSDownloadsDirectory = 15,
+    NSAllApplicationsDirectory = 100,
+    NSAllLibrariesDirectory = 101
+}
+
+enum NSSearchPathDomainMask
+{
+    NSUserDomainMask = 1,
+    NSLocalDomainMask = 2,
+    NSNetworkDomainMask = 4,
+    NSSystemDomainMask = 8,
+    NSAllDomainsMask = 0x0ffff,
+}
+
 
 
 
--- a/dwt/internal/objc/cocoa/bindings.d	Wed Dec 31 16:16:40 2008 +0100
+++ b/dwt/internal/objc/cocoa/bindings.d	Wed Dec 31 16:50:37 2008 +0100
@@ -14,35 +14,6 @@
 import dwt.internal.objc.cocoa.Cocoa;
 import dwt.internal.objc.runtime;
 
-enum NSSearchPathDirectory
-{
-    NSApplicationDirectory = 1,
-    NSDemoApplicationDirectory,
-    NSDeveloperApplicationDirectory,
-    NSAdminApplicationDirectory,
-    NSLibraryDirectory,
-    NSDeveloperDirectory,
-    NSUserDirectory,
-    NSDocumentationDirectory,
-    NSDocumentDirectory,
-    NSCoreServiceDirectory,
-    NSDesktopDirectory = 12,
-    NSCachesDirectory = 13,
-    NSApplicationSupportDirectory = 14,
-    NSDownloadsDirectory = 15,
-    NSAllApplicationsDirectory = 100,
-    NSAllLibrariesDirectory = 101
-}
-
-enum NSSearchPathDomainMask
-{
-    NSUserDomainMask = 1,
-    NSLocalDomainMask = 2,
-    NSNetworkDomainMask = 4,
-    NSSystemDomainMask = 8,
-    NSAllDomainsMask = 0x0ffff,
-}
-
 alias c_long NSInteger;
 alias c_ulong NSUInteger;
 
--- a/dwt/program/Program.d	Wed Dec 31 16:16:40 2008 +0100
+++ b/dwt/program/Program.d	Wed Dec 31 16:50:37 2008 +0100
@@ -7,14 +7,15 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ *     
+ * Port to the D programming language:
+ *     Jacob Carlborg <doob@me.com>
  *******************************************************************************/
 module dwt.program.Program;
 
 import dwt.dwthelper.utils;
 
 
-import java.util.Vector;
-
 import dwt.DWT;
 import dwt.graphics.ImageData;
 import dwt.graphics.PaletteData;
@@ -35,7 +36,12 @@
 import dwt.internal.cocoa.NSURL;
 import dwt.internal.cocoa.NSWorkspace;
 import dwt.internal.cocoa.OS;
-import dwt.internal.cocoa.id;
+import cocoa = dwt.internal.cocoa.id;
+
+import dwt.dwthelper.array;
+import dwt.internal.c.Carbon;
+import dwt.internal.objc.cocoa.Cocoa;
+import objc = dwt.internal.objc.runtime;
 
 /**
  * Instances of this class represent programs and
@@ -48,14 +54,14 @@
 public final class Program {
     String name, fullPath, identifier;
 
-    static final String PREFIX_FILE = "file://"; //$NON-NLS-1$
-    static final String PREFIX_HTTP = "http://"; //$NON-NLS-1$
-    static final String PREFIX_HTTPS = "https://"; //$NON-NLS-1$
+    static const String PREFIX_FILE = "file://"; //$NON-NLS-1$
+    static const String PREFIX_HTTP = "http://"; //$NON-NLS-1$
+    static const String PREFIX_HTTPS = "https://"; //$NON-NLS-1$
 
 /**
  * Prevents uninitialized instances from being created outside the package.
  */
-Program () {
+this () {
 }
 
 /**
@@ -74,25 +80,25 @@
 public static Program findProgram (String extension) {
     if (extension is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
     if (extension.length () is 0) return null;
-    if (extension.charAt(0) !is '.') extension = "." + extension;
-    NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    if (extension.charAt(0) !is '.') extension = "." ~ extension;
+    NSAutoreleasePool pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
     try {
         NSWorkspace workspace = NSWorkspace.sharedWorkspace();
-        int /*long*/ appName = OS.malloc(C.PTR_SIZEOF);
-        int /*long*/ type = OS.malloc(C.PTR_SIZEOF);
+        objc.id appName = cast(objc.id) OS.malloc(C.PTR_SIZEOF);
+        objc.id type = cast(objc.id) OS.malloc(C.PTR_SIZEOF);
         NSString temp = new NSString(OS.NSTemporaryDirectory());
-        NSString fileName = NSString.stringWith("swt" + System.currentTimeMillis() + extension);
+        NSString fileName = NSString.stringWith("swt" ~ Format("{}", System.currentTimeMillis()) ~ extension);
         NSString fullPath = temp.stringByAppendingPathComponent(fileName);
         NSFileManager fileManager = NSFileManager.defaultManager();
         fileManager.createFileAtPath(fullPath, null, null);
         workspace.getInfoForFile(fullPath, appName, type);
-        fileManager.removeItemAtPath(fullPath, 0);
-        int /*long*/ [] buffer = new int /*long*/[1];
+        fileManager.removeItemAtPath(fullPath, null);
+        objc.id buffer;
         OS.memmove(buffer, appName, C.PTR_SIZEOF);
         OS.free(appName);
         OS.free(type);
-        if (buffer [0] !is 0) {
-            NSString appPath = new NSString(buffer[0]);
+        if (buffer !is null) {
+            NSString appPath = new NSString(buffer);
             NSBundle bundle = NSBundle.bundleWithPath(appPath);
             if (bundle !is null) return getProgram(bundle);
         }
@@ -110,20 +116,20 @@
  * @return an array of extensions
  */
 public static String [] getExtensions () {
-    NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    NSAutoreleasePool pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
     try {
-        NSMutableSet supportedDocumentTypes = (NSMutableSet)NSMutableSet.set();
+        NSMutableSet supportedDocumentTypes = cast(NSMutableSet)NSMutableSet.set();
         NSWorkspace workspace = NSWorkspace.sharedWorkspace();
         NSString CFBundleDocumentTypes = NSString.stringWith("CFBundleDocumentTypes");
         NSString CFBundleTypeExtensions = NSString.stringWith("CFBundleTypeExtensions");
         NSArray array = new NSArray(OS.NSSearchPathForDirectoriesInDomains(OS.NSAllApplicationsDirectory, OS.NSAllDomainsMask, true));
-        int count = (int)/*64*/array.count();
-        for (int i = 0; i < count; i++) {
+        NSUInteger count = array.count();
+        for (NSUInteger i = 0; i < count; i++) {
             NSString path = new NSString(array.objectAtIndex(i));
             NSFileManager fileManager = NSFileManager.defaultManager();
             NSDirectoryEnumerator enumerator = fileManager.enumeratorAtPath(path);
             if (enumerator !is null) {
-                id id;
+                cocoa.id id;
                 while ((id = enumerator.nextObject()) !is null) {
                     enumerator.skipDescendents();
                     NSString filePath = new NSString(id.id);
@@ -147,12 +153,12 @@
             }
         }
         int i = 0;
-        String[] exts = new String[(int)/*64*/supportedDocumentTypes.count()];
+        String[] exts = new String[supportedDocumentTypes.count()];
         NSEnumerator enumerator = supportedDocumentTypes.objectEnumerator();
-        id id;
+        cocoa.id id;
         while ((id = enumerator.nextObject()) !is null) {
-            String ext = new NSString(id.id).getString();
-            if (!ext.equals("*")) exts[i++] = "." + ext;
+            String ext = (new NSString(id.id)).getString();
+            if (ext != "*") exts[i++] = "." ~ ext;
         }
         if (i !is exts.length) {
             String[] temp = new String[i];
@@ -170,7 +176,7 @@
     NSString CFBundleDisplayName = NSString.stringWith("CFBundleDisplayName");
     NSString fullPath = bundle.bundlePath();
     NSString identifier = bundle.bundleIdentifier();
-    id bundleName = bundle.objectForInfoDictionaryKey(CFBundleDisplayName);
+    cocoa.id bundleName = bundle.objectForInfoDictionaryKey(CFBundleDisplayName);
     if (bundleName is null) {
         bundleName = bundle.objectForInfoDictionaryKey(CFBundleName);
     }
@@ -193,18 +199,18 @@
  * @return an array of programs
  */
 public static Program [] getPrograms () {
-    NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    NSAutoreleasePool pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
     try {
-        Vector vector = new Vector();
+        Program[] vector;
         NSWorkspace workspace = NSWorkspace.sharedWorkspace();
         NSArray array = new NSArray(OS.NSSearchPathForDirectoriesInDomains(OS.NSAllApplicationsDirectory, OS.NSAllDomainsMask, true));
-        int count = (int)/*64*/array.count();
-        for (int i = 0; i < count; i++) {
+        NSUInteger count = array.count();
+        for (NSUInteger i = 0; i < count; i++) {
             NSString path = new NSString(array.objectAtIndex(i));
             NSFileManager fileManager = NSFileManager.defaultManager();
             NSDirectoryEnumerator enumerator = fileManager.enumeratorAtPath(path);
             if (enumerator !is null) {
-                id id;
+                cocoa.id id;
                 while ((id = enumerator.nextObject()) !is null) {
                     enumerator.skipDescendents();
                     NSString fullPath = path.stringByAppendingPathComponent(new NSString(id.id));
@@ -215,9 +221,7 @@
                 }
             }
         }
-        Program[] programs = new Program[vector.size()];
-        vector.copyInto(programs);
-        return programs;
+        vector.dup;
     } finally {
         pool.release();
     }
@@ -238,7 +242,7 @@
  */
 public static bool launch (String fileName) {
     if (fileName is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
-    NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    NSAutoreleasePool pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
     try {
         NSString unescapedStr = NSString.stringWith("%"); //$NON-NLS-1$
         if (fileName.indexOf(':') is -1) {
@@ -250,7 +254,7 @@
             }
         }
         NSString fullPath = NSString.stringWith(fileName);
-        int /*long*/ ptr = OS.CFURLCreateStringByAddingPercentEscapes(0, fullPath.id, unescapedStr.id, 0, OS.kCFStringEncodingUTF8);
+        CFStringRef ptr = OS.CFURLCreateStringByAddingPercentEscapes(null, fullPath.id, unescapedStr.id, null, OS.kCFStringEncodingUTF8);
         NSString escapedString = new NSString(ptr);
         NSWorkspace workspace = NSWorkspace.sharedWorkspace();
         bool result = workspace.openURL(NSURL.URLWithString(escapedString));
@@ -276,7 +280,7 @@
  */
 public bool execute (String fileName) {
     if (fileName is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
-    NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    NSAutoreleasePool pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
     try {
         NSWorkspace workspace = NSWorkspace.sharedWorkspace();
         NSString fullPath = NSString.stringWith(fileName);
@@ -288,7 +292,7 @@
         if (lowercaseName.startsWith (PREFIX_HTTP) || lowercaseName.startsWith (PREFIX_HTTPS)) {
             unescapedStr = NSString.stringWith("%#"); //$NON-NLS-1$
         }
-        int /*long*/ ptr = OS.CFURLCreateStringByAddingPercentEscapes(0, fullPath.id, unescapedStr.id, 0, OS.kCFStringEncodingUTF8);
+        CFStringRef ptr = OS.CFURLCreateStringByAddingPercentEscapes(0, fullPath.id, unescapedStr.id, 0, OS.kCFStringEncodingUTF8);
         NSString escapedString = new NSString(ptr);
         NSArray urls = NSArray.arrayWithObject(NSURL.URLWithString(escapedString));
         OS.CFRelease(ptr);
@@ -306,7 +310,7 @@
  * @return the image data for the program, may be null
  */
 public ImageData getImageData () {
-    NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    NSAutoreleasePool pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
     try {
         NSWorkspace workspace = NSWorkspace.sharedWorkspace();
         NSString fullPath;
@@ -327,10 +331,10 @@
                     imageRep = new NSBitmapImageRep(rep.id);
                 }
                 if (imageRep !is null) {
-                    int width = (int)/*64*/imageRep.pixelsWide();
-                    int height = (int)/*64*/imageRep.pixelsHigh();
-                    int bpr = (int)/*64*/imageRep.bytesPerRow();
-                    int bpp = (int)/*64*/imageRep.bitsPerPixel();
+                    int width = cast(int)/*64*/imageRep.pixelsWide();
+                    int height = cast(int)/*64*/imageRep.pixelsHigh();
+                    int bpr = cast(int)/*64*/imageRep.bytesPerRow();
+                    int bpp = cast(int)/*64*/imageRep.bitsPerPixel();
                     int dataSize = height * bpr;
                     byte[] srcData = new byte[dataSize];
                     OS.memmove(srcData, imageRep.bitmapData(), dataSize);
@@ -374,15 +378,17 @@
  *
  * @see #hashCode()
  */
-public bool equals(Object other) {
+public int opEquals(Object other) {
     if (this is other) return true;
-    if (other instanceof Program) {
-        final Program program = (Program) other;
+    if (cast(Program) other) {
+        final Program program = cast(Program) other;
         return name.equals(program.name);
     }
     return false;
 }
 
+alias opEquals equals;
+
 /**
  * Returns an integer hash code for the receiver. Any two 
  * objects that return <code>true</code> when passed to 
@@ -393,10 +399,12 @@
  *
  * @see #equals(Object)
  */
-public int hashCode() {
-    return name.hashCode();
+public hash_t toHash() {
+    return .toHash(name);
 }
 
+alias toHash hashCode;
+
 /**
  * Returns a string containing a concise, human-readable
  * description of the receiver.
@@ -404,7 +412,7 @@
  * @return a string representation of the program
  */
 public String toString () {
-    return "Program {" + name + "}";
+    return "Program {" ~ name ~ "}";
 }
 
 }