Mercurial > projects > dwt-mac
changeset 62:10eaa644646f
Ported dwt.widgets.Combo
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Tue, 23 Dec 2008 22:21:02 +0100 |
parents | c74ba20de292 |
children | a3f5118c8b37 |
files | dwt/dwthelper/utils.d dwt/internal/cocoa/OS.d dwt/widgets/Combo.d dwt/widgets/Display.d dwt/widgets/Widget.d |
diffstat | 5 files changed, 103 insertions(+), 87 deletions(-) [+] |
line wrap: on
line diff
--- a/dwt/dwthelper/utils.d Tue Dec 23 00:35:24 2008 +0100 +++ b/dwt/dwthelper/utils.d Tue Dec 23 22:21:02 2008 +0100 @@ -7,6 +7,7 @@ public import Math = tango.math.Math; public import tango.core.Exception : IllegalArgumentException, IOException, PlatformException; +public import tango.text.convert.Utf; import tango.io.Stdout; @@ -537,6 +538,14 @@ return cont[ offset .. offset+len ].dup; } +String new_String(String cont){ + return cont.dup; +} + +String new_String(wchar[] cont){ + return cont.toString16(); +} + public String toUpperCase( String str ){ return tango.text.Unicode.toUpper( str ); }
--- a/dwt/internal/cocoa/OS.d Tue Dec 23 00:35:24 2008 +0100 +++ b/dwt/internal/cocoa/OS.d Tue Dec 23 22:21:02 2008 +0100 @@ -2696,7 +2696,7 @@ alias Cocoa.NSBitmapFormat.NSAlphaFirstBitmapFormat NSAlphaFirstBitmapFormat; alias Cocoa.NSBitmapFormat.NSAlphaNonpremultipliedBitmapFormat NSAlphaNonpremultipliedBitmapFormat; public static const int NSAlphaShiftKeyMask = 65536; -public static const int NSAlternateKeyMask = 524288; +public static const Cocoa.NSUInteger NSAlternateKeyMask = 524288; public static const int NSAnimationBlocking = 0; public static const int NSAnimationEaseIn = 1; public static const int NSAnimationEaseInOut = 0; @@ -2832,7 +2832,7 @@ public static const int NSColorRenderingIntentPerceptual = 3; public static const int NSColorRenderingIntentRelativeColorimetric = 2; public static const int NSColorRenderingIntentSaturation = 4; -public static const int NSCommandKeyMask = 1048576; +public static const Cocoa.NSUInteger NSCommandKeyMask = 1048576; public static const int NSCompositeClear = 0; public static const int NSCompositeCopy = 1; public static const int NSCompositeDestinationAtop = 9; @@ -2852,7 +2852,7 @@ public static const int NSContentsCellMask = 1; public static const int NSContinuousCapacityLevelIndicatorStyle = 1; public static const int NSControlGlyph = 16777215; -public static const int NSControlKeyMask = 262144; +public static const Cocoa.NSUInteger NSControlKeyMask = 262144; public static const int NSCrayonModeColorPanel = 7; alias Cocoa.NSAlertStyle.NSCriticalAlertStyle NSCriticalAlertStyle; public static const int NSCriticalRequest = 0; @@ -3401,7 +3401,7 @@ public static const int NSServiceMiscellaneousError = 66800; public static const int NSServiceRequestTimedOutError = 66562; alias Cocoa.NSBezelStyle.NSShadowlessSquareBezelStyle NSShadowlessSquareBezelStyle; -public static const int NSShiftKeyMask = 131072; +public static const Cocoa.NSUInteger NSShiftKeyMask = 131072; public static const int NSShowControlGlyphs = 1; public static const int NSShowInvisibleGlyphs = 2; public static const int NSSingleDateMode = 0;
--- a/dwt/widgets/Combo.d Tue Dec 23 00:35:24 2008 +0100 +++ b/dwt/widgets/Combo.d Tue Dec 23 22:21:02 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.Combo; @@ -41,6 +44,11 @@ import dwt.internal.cocoa.SWTComboBox; import dwt.internal.cocoa.SWTPopUpButton; +import dwt.internal.c.Carbon; +import objc = dwt.internal.objc.runtime; +import dwt.widgets.Composite; +import dwt.widgets.TypedListener; + /** * Instances of this class are controls that allow the user * to choose an item from a list of items, or optionally @@ -88,14 +96,14 @@ * the operating system limit for the number of characters * that the text field in an instance of this class can hold */ - public static final int LIMIT; + public static const int LIMIT; /* * These values can be different on different platforms. * Therefore they are not initialized in the declaration * to stop the compiler from inlining. */ - static { + static this () { LIMIT = 0x7FFFFFFF; } @@ -155,8 +163,8 @@ NSString str = NSString.stringWith(string); if ((style & DWT.READ_ONLY) !is 0) { NSMenu nsMenu = (cast(NSPopUpButton)view).menu(); - NSMenuItem nsItem = cast(NSMenuItem)new NSMenuItem().alloc(); - nsItem.initWithTitle(str, 0, NSString.stringWith("")); + NSMenuItem nsItem = cast(NSMenuItem)(new NSMenuItem()).alloc(); + nsItem.initWithTitle(str, null, NSString.stringWith("")); nsMenu.addItem(nsItem); nsItem.release(); } else { @@ -195,8 +203,8 @@ NSString str = NSString.stringWith(string); if ((style & DWT.READ_ONLY) !is 0) { NSMenu nsMenu = (cast(NSPopUpButton)view).menu(); - NSMenuItem nsItem = cast(NSMenuItem)new NSMenuItem().alloc(); - nsItem.initWithTitle(str, 0, NSString.stringWith("")); + NSMenuItem nsItem = cast(NSMenuItem)(new NSMenuItem()).alloc(); + nsItem.initWithTitle(str, null, NSString.stringWith("")); nsMenu.insertItem(nsItem, index); nsItem.release(); } else { @@ -290,7 +298,7 @@ addListener (DWT.Verify, typedListener); } -bool becomeFirstResponder (int /*long*/ id, int /*long*/ sel) { +bool becomeFirstResponder (objc.id id, objc.SEL sel) { receivingFocus = true; bool result = super.becomeFirstResponder (id, sel); receivingFocus = false; @@ -363,10 +371,10 @@ checkWidget (); int width = 0, height = 0; NSControl widget = cast(NSControl)view; - NSRect rect = new NSRect (); + NSRect rect = NSRect (); rect.width = rect.height = Float.MAX_VALUE; NSSize size = widget.cell ().cellSizeForBounds (rect); - width = (int)Math.ceil (size.width); + width = cast(int)Math.ceil (size.width); height = cast(int)Math.ceil (size.height); /* * Feature in Cocoa. Attempting to create an NSComboBox with a @@ -404,14 +412,14 @@ void createHandle () { if ((style & DWT.READ_ONLY) !is 0) { - NSPopUpButton widget = cast(NSPopUpButton)new SWTPopUpButton().alloc(); + NSPopUpButton widget = cast(NSPopUpButton)(new SWTPopUpButton()).alloc(); widget.initWithFrame(NSRect(), false); widget.menu().setAutoenablesItems(false); widget.setTarget(widget); widget.setAction(OS.sel_sendSelection); view = widget; } else { - NSComboBox widget = cast(NSComboBox)new SWTComboBox().alloc(); + NSComboBox widget = cast(NSComboBox)(new SWTComboBox()).alloc(); widget.initWithFrame(NSRect()); widget.setDelegate(widget); view = widget; @@ -467,7 +475,7 @@ void deregister() { super.deregister(); - display.removeWidget(((NSControl)view).cell()); + display.removeWidget((cast(NSControl)view).cell()); } /** @@ -519,14 +527,14 @@ } } -int getCharCount() { +NSUInteger getCharCount() { NSString str; if ((style & DWT.READ_ONLY) !is 0) { str = (cast(NSPopUpButton)view).titleOfSelectedItem(); } else { - str = new NSCell((cast(NSComboBox)view).cell()).title(); + str = (new NSCell((cast(NSComboBox)view).cell())).title(); } - return (int)/*64*/str.length(); + return str.length(); } /** @@ -613,9 +621,9 @@ */ public String [] getItems () { checkWidget (); - int count = getItemCount (); + NSUInteger count = getItemCount (); String [] result = new String [count]; - for (int i=0; i<count; i++) result [i] = getItem (i); + for (NSUInteger i=0; i<count; i++) result [i] = getItem (i); return result; } @@ -662,13 +670,13 @@ public Point getSelection () { checkWidget (); if ((style & DWT.READ_ONLY) !is 0) { - return new Point (0, getCharCount ()); + return new Point (0, cast(int) getCharCount ()); } else { if (selectionRange is null) { - NSString str = new NSTextFieldCell (((NSTextField) view).cell ()).title (); - return new Point((int)/*64*/str.length (), (int)/*64*/str.length ()); + NSString str = (new NSTextFieldCell ((cast(NSTextField) view).cell ())).title (); + return new Point(cast(int)/*64*/str.length (), cast(int)/*64*/str.length ()); } - return new Point((int)/*64*/selectionRange.location, (int)/*64*/(selectionRange.location + selectionRange.length)); + return new Point(cast(int)/*64*/selectionRange.location, cast(int)/*64*/(selectionRange.location + selectionRange.length)); } } @@ -706,27 +714,27 @@ */ public String getText () { checkWidget (); - return new String (getText(0, -1)); + return new_String (getText(0, -1)); } -char [] getText (int start, int end) { +wchar [] getText (int start, int end) { NSString str; if ((style & DWT.READ_ONLY) !is 0) { str = (cast(NSPopUpButton)view).titleOfSelectedItem(); } else { - str = new NSCell((cast(NSComboBox)view).cell()).title(); + str = (new NSCell((cast(NSComboBox)view).cell())).title(); } - if (str is null) return new char[0]; - NSRange range = new NSRange (); + if (str is null) return new wchar[0]; + NSRange range = NSRange (); range.location = start; if (end is -1) { - int /*long*/ length = str.length(); + NSUInteger length = str.length(); range.length = length - start; } else { range.length = end - start; } - char [] buffer= new char [(int)/*64*/range.length]; - str.getCharacters(buffer, range); + wchar [] buffer= new wchar [range.length]; + str.getCharacters(buffer.ptr, range); return buffer; } @@ -846,7 +854,7 @@ } void insertEditText (String string) { - int length = string.length (); + NSUInteger length = string.length (); Point selection = getSelection (); if (hasFocus ()) { if (textLimit !is LIMIT) { @@ -855,10 +863,10 @@ length = textLimit - charCount + (selection.y - selection.x); } } - char [] buffer = new char [length]; + wchar [] buffer = new wchar [length]; string.getChars (0, buffer.length, buffer, 0); - NSString nsstring = NSString.stringWithCharacters (buffer, buffer.length); - NSText editor = ((NSTextField) view).currentEditor (); + NSString nsstring = NSString.stringWithCharacters (buffer.ptr, buffer.length); + NSText editor = (cast(NSTextField) view).currentEditor (); editor.replaceCharactersInRange (editor.selectedRange (), nsstring); selectionRange = null; } else { @@ -871,7 +879,7 @@ } String newText = oldText.substring (0, selection.x) + string + oldText.substring (selection.y); NSString nsstring = NSString.stringWith(newText); - new NSCell (((NSTextField) view).cell ()).setTitle (nsstring); + (new NSCell ((cast(NSTextField) view).cell ())).setTitle (nsstring); selectionRange = null; setSelection (new Point(selection.x + string.length (), 0)); } @@ -918,7 +926,7 @@ void register() { super.register(); - display.addWidget(((NSControl)view).cell(), this); + display.addWidget((cast(NSControl)view).cell(), this); } void releaseWidget () { @@ -1188,14 +1196,14 @@ bool result = super.sendKeyEvent (nsEvent, type); if (!result) return result; int stateMask = 0; - int /*long*/ modifierFlags = nsEvent.modifierFlags(); + NSUInteger modifierFlags = nsEvent.modifierFlags(); if ((modifierFlags & OS.NSAlternateKeyMask) !is 0) stateMask |= DWT.ALT; if ((modifierFlags & OS.NSShiftKeyMask) !is 0) stateMask |= DWT.SHIFT; if ((modifierFlags & OS.NSControlKeyMask) !is 0) stateMask |= DWT.CONTROL; if ((modifierFlags & OS.NSCommandKeyMask) !is 0) stateMask |= DWT.COMMAND; if (type !is DWT.KeyDown) return result; if (stateMask is DWT.COMMAND) { - short keyCode = nsEvent.keyCode (); + ushort keyCode = nsEvent.keyCode (); switch (keyCode) { case 7: /* X */ cut (); @@ -1209,7 +1217,7 @@ } } if ((style & DWT.SINGLE) !is 0) { - short keyCode = nsEvent.keyCode (); + ushort keyCode = nsEvent.keyCode (); switch (keyCode) { case 76: /* KP Enter */ case 36: /* Return */ @@ -1222,9 +1230,9 @@ int charCount = getCharCount (); Point selection = getSelection (); int start = selection.x, end = selection.y; - short keyCode = nsEvent.keyCode (); + ushort keyCode = nsEvent.keyCode (); NSString characters = nsEvent.charactersIgnoringModifiers(); - char character = (char) characters.characterAtIndex(0); + wchar character = characters.characterAtIndex(0); switch (keyCode) { case 51: /* Backspace */ if (start is end) { @@ -1240,7 +1248,7 @@ break; default: if (character !is '\t' && character < 0x20) return true; - oldText = new String (new char [] {character}); + oldText = new_String ([character]); } String newText = verifyText (oldText, start, end, nsEvent); if (newText is null) return false; @@ -1255,7 +1263,7 @@ return result; } -void setBackground (float [] color) { +void setBackground (CGFloat [] color) { NSColor nsColor; if (color is null) { return; // TODO reset to OS default @@ -1278,16 +1286,16 @@ * to display their text. */ if ((style & DWT.READ_ONLY) is 0) { - NSControl widget = (NSControl)view; - NSRect rect = new NSRect (); + NSControl widget = cast(NSControl)view; + NSRect rect = NSRect (); rect.width = rect.height = Float.MAX_VALUE; NSSize size = widget.cell ().cellSizeForBounds (rect); - height = Math.min (height, (int)Math.ceil (size.height)); + height = Math.min (height, cast(int)Math.ceil (size.height)); } super.setBounds (x, y, width, height, move, resize); } -void setForeground (float [] color) { +void setForeground (CGFloat [] color) { NSColor nsColor; if (color is null) { return; // TODO reset to OS default @@ -1319,7 +1327,7 @@ */ public void setItem (int index, String string) { checkWidget (); - if (string is null) error (DWT.ERROR_NULL_ARGUMENT); + //if (string is null) error (DWT.ERROR_NULL_ARGUMENT); int count = getItemCount (); if (0 > index || index >= count) error (DWT.ERROR_INVALID_RANGE); NSString str = NSString.stringWith(string); @@ -1359,8 +1367,8 @@ NSString str = NSString.stringWith(items[i]); if ((style & DWT.READ_ONLY) !is 0) { NSMenu nsMenu = (cast(NSPopUpButton)view).menu(); - NSMenuItem nsItem = cast(NSMenuItem)new NSMenuItem().alloc(); - nsItem.initWithTitle(str, 0, NSString.stringWith("")); + NSMenuItem nsItem = cast(NSMenuItem)(new NSMenuItem()).alloc(); + nsItem.initWithTitle(str, null, NSString.stringWith("")); nsMenu.addItem(nsItem); nsItem.release(); } else { @@ -1433,10 +1441,10 @@ checkWidget (); if (selection is null) error (DWT.ERROR_NULL_ARGUMENT); if ((style & DWT.READ_ONLY) is 0) { - NSString str = new NSCell(((NSComboBox)view).cell()).title(); - int length = (int)/*64*/str.length(); - int start = Math.min (Math.max (Math.min (selection.x, selection.y), 0), length); - int end = Math.min (Math.max (Math.max (selection.x, selection.y), 0), length); + NSString str = (new NSCell((cast(NSComboBox)view).cell())).title(); + NSUInteger length = str.length(); + NSUInteger start = Math.min (Math.max (Math.min (selection.x, selection.y), 0), length); + NSUInteger end = Math.min (Math.max (Math.max (selection.x, selection.y), 0), length); selectionRange = NSRange(); selectionRange.location = start; selectionRange.length = end - start; @@ -1488,7 +1496,7 @@ if (notify) sendEvent (DWT.Modify); } } else { - new NSCell((cast(NSComboBox)view).cell()).setTitle(NSString.stringWith(string)); + (new NSCell((cast(NSComboBox)view).cell())).setTitle(NSString.stringWith(string)); if (notify) sendEvent (DWT.Modify); } selectionRange = null; @@ -1547,17 +1555,17 @@ } } -void textViewDidChangeSelection(int /*long*/ id, int /*long*/ sel, int /*long*/ aNotification) { +void textViewDidChangeSelection(objc.id id, objc.SEL sel, objc.id aNotification) { NSNotification notification = new NSNotification(aNotification); NSText editor = new NSText(notification.object().id); selectionRange = editor.selectedRange(); } -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); } -NSRange textView_willChangeSelectionFromCharacterRange_toCharacterRange(int /*long*/ id, int /*long*/ sel, int /*long*/ aTextView, int /*long*/ oldSelectedCharRange, int /*long*/ newSelectedCharRange) { +NSRange textView_willChangeSelectionFromCharacterRange_toCharacterRange(objc.id id, objc.SEL sel, objc.id aTextView, objc.id oldSelectedCharRange, objc.id newSelectedCharRange) { /* * If the selection is changing as a result of the receiver getting focus * then return the receiver's last selection range, otherwise the full @@ -1567,7 +1575,7 @@ /* allow the selection change to proceed */ NSRange result = NSRange(); - OS.memmove(result, newSelectedCharRange, NSRange.sizeof); + OS.memmove(&result, newSelectedCharRange, NSRange.sizeof); return result; }
--- a/dwt/widgets/Display.d Tue Dec 23 00:35:24 2008 +0100 +++ b/dwt/widgets/Display.d Tue Dec 23 22:21:02 2008 +0100 @@ -215,7 +215,7 @@ static const short SWT_IDLE_TYPE = 1; int[] screenID; - NSPoint[] screenCascade; + NSPoint*[] screenCascade; /* Menus */ // Menu menuBar; @@ -559,15 +559,14 @@ int index = 0; while (screenID[index] !is 0 && screenID[index] !is screenNumber) index++; screenID[index] = screenNumber; - NSPoint cascade = screenCascade[index]; - //FIXME Jacob Carlborg - /*if (cascade is null) { - NSRect frame = screen.frame(); - cascade = NSPoint(); - cascade.x = frame.x; - cascade.y = frame.y + frame.height; - }*/ - screenCascade[index] = window.cascadeTopLeftFromPoint(cascade); + NSPoint* cascade = screenCascade[index]; + if (cascade is null) { + NSRect frame = screen.frame(); + cascade = &NSPoint(); + cascade.x = frame.x; + cascade.y = frame.y + frame.height; + } + screenCascade[index] = &window.cascadeTopLeftFromPoint(*cascade); } protected void checkDevice () { @@ -623,7 +622,7 @@ public this (DeviceData data) { super (data); screenID = new int[32]; - screenCascade = new NSPoint[32]; + screenCascade = new NSPoint*[32]; cursors = new Cursor [DWT.CURSOR_HAND + 1]; timerDelegate = cast(SWTWindowDelegate)(new SWTWindowDelegate()).alloc().init(); @@ -1770,18 +1769,18 @@ super.init_ (); initClasses (); - /*if (!isEmbedded) { - initApplicationDelegate(); - application.finishLaunching(); - } - - timerDelegate = cast(SWTWindowDelegate)(new SWTWindowDelegate()).alloc().init(); - - NSTextView textView = cast(NSTextView)(new NSTextView()).alloc(); - textView.initWithFrame (NSRect ()); - markedAttributes = textView.markedTextAttributes (); - markedAttributes.retain (); - textView.release ();*/ + if (!isEmbedded) { + initApplicationDelegate(); + application.finishLaunching(); + } + + timerDelegate = cast(SWTWindowDelegate)(new SWTWindowDelegate()).alloc().init(); + + NSTextView textView = cast(NSTextView)(new NSTextView()).alloc(); + textView.initWithFrame (NSRect ()); + markedAttributes = textView.markedTextAttributes (); + markedAttributes.retain (); + textView.release (); } void initApplicationDelegate() {
--- a/dwt/widgets/Widget.d Tue Dec 23 00:35:24 2008 +0100 +++ b/dwt/widgets/Widget.d Tue Dec 23 22:21:02 2008 +0100 @@ -451,11 +451,11 @@ void comboBoxSelectionDidChange(objc.id id, objc.SEL sel, objc.id notification) { } -void copyToClipboard (wchar [] buffer) { +void copyToClipboard (char [] buffer) { if (buffer.length is 0) return; NSPasteboard pasteboard = NSPasteboard.generalPasteboard (); pasteboard.declareTypes (NSArray.arrayWithObject (OS.NSStringPboardType), null); - pasteboard.setString (NSString.stringWithCharacters (buffer.ptr, buffer.length), OS.NSStringPboardType); + pasteboard.setString (NSString.stringWithCharacters (buffer.toString16().ptr, buffer.length), OS.NSStringPboardType); } void createHandle () {