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

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents 642f460a0908
children 4444d15131d5
line wrap: on
line diff
--- a/dwt/widgets/MenuItem.d	Tue Oct 21 15:20:04 2008 +0200
+++ b/dwt/widgets/MenuItem.d	Mon Dec 01 17:07:00 2008 +0100
@@ -1,5 +1,5 @@
-/*******************************************************************************
- * Copyright (c) 2000, 2007 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
@@ -8,9 +8,9 @@
  * Contributors:
  *     IBM Corporation - initial API and implementation
 *     
- * Port to the D programming language:
- *     Jacob Carlborg <jacob.carlborg@gmail.com>
- *******************************************************************************/
+* Port to the D programming language:
+*     Jacob Carlborg <doob@me.com>
+*******************************************************************************/
 module dwt.widgets.MenuItem;
 
 
@@ -21,6 +21,8 @@
 import dwt.events.SelectionEvent;
 import dwt.events.SelectionListener;
 import dwt.graphics.Image;
+import dwt.internal.cocoa.NSApplication;
+import dwt.internal.cocoa.NSEvent;
 import dwt.internal.cocoa.NSMenu;
 import dwt.internal.cocoa.NSMenuItem;
 import dwt.internal.cocoa.NSString;
@@ -51,12 +53,13 @@
  * </p><p>
  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
  * </p>
+ *
+ * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
  */
 public class MenuItem : Item {
     NSMenuItem nsItem;
     Menu parent, menu;
     int accelerator;
-//  int x, y, width, height;
 
 /**
  * Constructs a new instance of this class given its parent
@@ -239,6 +242,11 @@
     return null;
 }
 
+void deregister () {
+    super.deregister ();
+    display.removeWidget (nsItem);
+}
+
 void destroyWidget () {
     parent.destroyItem (this);
     releaseHandle ();
@@ -340,32 +348,9 @@
 public bool getSelection () {
     checkWidget ();
     if ((style & (DWT.CHECK | DWT.RADIO)) is 0) return false;
-    return (cast(NSMenuItem)nsItem).state() is OS.NSOnState;
+    return nsItem.state() is OS.NSOnState;
 }
 
-//int kEventProcessCommand (int nextHandler, int theEvent, int userData) {
-//  //TEMPORARY CODE
-//  if (!isEnabled ()) return OS.noErr;
-//
-//  if ((style & DWT.CHECK) !is 0) {
-//      setSelection (!getSelection ());
-//  } else {
-//      if ((style & DWT.RADIO) !is 0) {
-//          if ((parent.getStyle () & DWT.NO_RADIO_GROUP) !is 0) {
-//              setSelection (!getSelection ());
-//          } else {
-//              selectRadio ();
-//          }
-//      }
-//  }
-//  int [] modifiers = new int [1];
-//  OS.GetEventParameter (theEvent, OS.kEventParamKeyModifiers, OS.typeUInt32, null, 4, null, modifiers);
-//  Event event = new Event ();
-//  setInputState (event, cast(short) 0, OS.GetCurrentEventButtonState (), modifiers [0]);
-//  postEvent (DWT.Selection, event);
-//  return OS.noErr;
-//}
-
 /**
  * Returns <code>true</code> if the receiver is enabled and all
  * of the receiver's ancestors are enabled, and <code>false</code>
@@ -385,41 +370,60 @@
     return getEnabled () && parent.isEnabled ();
 }
 
-//int keyGlyph (int key) {
-//  switch (key) {
-//      case DWT.BS: return OS.kMenuDeleteLeftGlyph;
-//      case DWT.CR: return OS.kMenuReturnGlyph;
-//      case DWT.DEL: return OS.kMenuDeleteRightGlyph;
-//      case DWT.ESC: return OS.kMenuEscapeGlyph;
-//      case DWT.LF: return OS.kMenuReturnGlyph;
-//      case DWT.TAB: return OS.kMenuTabRightGlyph;
+int keyChar (int key) {
+    //TODO - use the NS key constants
+    switch (key) {
+        case DWT.BS: return OS.NSBackspaceCharacter;
+        case DWT.CR: return OS.NSCarriageReturnCharacter;
+        case DWT.DEL: return OS.NSDeleteCharacter;
+        case DWT.ESC: return DWT.ESC;
+        case DWT.LF: return OS.NSNewlineCharacter;
+        case DWT.TAB: return OS.NSTabCharacter;
 //      case ' ': return OS.kMenuBlankGlyph;
-////        case ' ': return OS.kMenuSpaceGlyph;
-//      case DWT.ALT: return OS.kMenuOptionGlyph;
-//      case DWT.SHIFT: return OS.kMenuShiftGlyph;
-//      case DWT.CONTROL: return OS.kMenuControlISOGlyph;
-//      case DWT.COMMAND: return OS.kMenuCommandGlyph;
-//      case DWT.ARROW_UP: return OS.kMenuUpArrowGlyph;
-//      case DWT.ARROW_DOWN: return OS.kMenuDownArrowGlyph;
-//      case DWT.ARROW_LEFT: return OS.kMenuLeftArrowGlyph;
-//      case DWT.ARROW_RIGHT: return OS.kMenuRightArrowGlyph;
-//      case DWT.PAGE_UP: return OS.kMenuPageUpGlyph;
-//      case DWT.PAGE_DOWN: return OS.kMenuPageDownGlyph;
-//      case DWT.F1: return OS.kMenuF1Glyph;
-//      case DWT.F2: return OS.kMenuF2Glyph;
-//      case DWT.F3: return OS.kMenuF3Glyph;
-//      case DWT.F4: return OS.kMenuF4Glyph;
-//      case DWT.F5: return OS.kMenuF5Glyph;
-//      case DWT.F6: return OS.kMenuF6Glyph;
-//      case DWT.F7: return OS.kMenuF7Glyph;
-//      case DWT.F8: return OS.kMenuF8Glyph;
-//      case DWT.F9: return OS.kMenuF9Glyph;
-//      case DWT.F10: return OS.kMenuF10Glyph;
-//      case DWT.F11: return OS.kMenuF11Glyph;
-//      case DWT.F12: return OS.kMenuF12Glyph;
-//  }
-//  return OS.kMenuNullGlyph;
-//}
+//      case ' ': return OS.kMenuSpaceGlyph;        
+        case DWT.ALT: return 0x2325;
+        case DWT.SHIFT: return 0x21E7;
+        case DWT.CONTROL: return 0xF2303;
+        case DWT.COMMAND: return 0x2318;
+        case DWT.ARROW_UP: return 0x2191;
+        case DWT.ARROW_DOWN: return 0x2193;
+        case DWT.ARROW_LEFT: return 0x2190;
+        case DWT.ARROW_RIGHT: return 0x2192;
+        case DWT.PAGE_UP: return 0x21DE;
+        case DWT.PAGE_DOWN: return 0x21DF;
+        case DWT.KEYPAD_CR: return OS.NSEnterCharacter;
+        case DWT.HELP: return OS.NSHelpFunctionKey;
+        case DWT.HOME: return 0xF729;
+        case DWT.END: return 0xF72B;
+//      case DWT.CAPS_LOCK: return ??;
+        case DWT.F1: return 0xF704;
+        case DWT.F2: return 0xF705;
+        case DWT.F3: return 0xF706;
+        case DWT.F4: return 0xF707;
+        case DWT.F5: return 0xF708;
+        case DWT.F6: return 0xF709;
+        case DWT.F7: return 0xF70A;
+        case DWT.F8: return 0xF70B;
+        case DWT.F9: return 0xF70C;
+        case DWT.F10: return 0xF70D;
+        case DWT.F11: return 0xF70E;
+        case DWT.F12: return 0xF70F;
+        case DWT.F13: return 0xF710;
+        case DWT.F14: return 0xF711;
+        case DWT.F15: return 0xF712;
+        /*
+        * The following lines are intentionally commented.
+        */
+//      case DWT.INSERT: return ??;
+    }
+    return 0;
+}
+
+
+void register () {
+    super.register ();
+    display.addWidget (nsItem, this);
+}
 
 void releaseHandle () {
     super.releaseHandle ();
@@ -539,8 +543,8 @@
         }
     }
     Event event = new Event ();
-    //TODO state mask
-//  setInputState (event, cast(short) 0, OS.GetCurrentEventButtonState (), modifiers [0]);
+    NSEvent nsEvent = NSApplication.sharedApplication ().currentEvent ();
+    if (nsEvent !is null) setInputState (event, nsEvent, 0);
     postEvent (DWT.Selection, event);
 }
 
@@ -562,23 +566,24 @@
  */
 public void setAccelerator (int accelerator) {
     checkWidget ();
+    if (this.accelerator is accelerator) return;
     this.accelerator = accelerator;
     int key = accelerator & DWT.KEY_MASK;
-    int virtualKey = Display.untranslateKey (key);
+    int virtualKey = keyChar (key);
     NSString str =  null;
     if (virtualKey !is 0) {
         str = NSString.stringWith (Integer.toString(virtualKey));
     } else {
-        str = NSString.stringWith (Integer.toString(key)).lowercaseString();
+        str = NSString.stringWith (Integer.toString(key));
     }
-    nsItem.setKeyEquivalent (str);
+    nsItem.setKeyEquivalent (str.lowercaseString());
     int mask = 0;
     if ((accelerator & DWT.SHIFT) !is 0) mask |= OS.NSShiftKeyMask;
     if ((accelerator & DWT.CONTROL) !is 0) mask |= OS.NSControlKeyMask;
-//  if ((accelerator & DWT.COMMAND) !is 0) mask &= ~OS.kMenuNoCommandModifier;
     if ((accelerator & DWT.COMMAND) !is 0) mask |= OS.NSCommandKeyMask;
     if ((accelerator & DWT.ALT) !is 0) mask |= OS.NSAlternateKeyMask;
     nsItem.setKeyEquivalentModifierMask (cast(NSUInteger) mask);
+    nsItem.setHidden (false);
     if ((this.accelerator is 0 && accelerator !is 0) || (this.accelerator !is 0 && accelerator is 0)) {
         updateText ();
     }
@@ -599,7 +604,12 @@
  */
 public void setEnabled (bool enabled) {
     checkWidget ();
-    (cast(NSMenuItem)nsItem).setEnabled(enabled);
+    if (enabled) {
+        state &= ~DISABLED;
+    } else {
+        state |= DISABLED;
+    }
+    nsItem.setEnabled(enabled);
 }
 
 /**
@@ -620,7 +630,7 @@
     checkWidget ();
     if ((style & DWT.SEPARATOR) !is 0) return;
     super.setImage (image);
-    (cast(NSMenuItem)nsItem).setImage(image !is null? image.handle : null);
+    nsItem.setImage(image !is null? image.handle : null);
 }
 
 /**
@@ -709,7 +719,7 @@
 public void setSelection (bool selected) {
     checkWidget ();
     if ((style & (DWT.CHECK | DWT.RADIO)) is 0) return;
-    (cast(NSMenuItem)nsItem).setState(selected ? OS.NSOnState : OS.NSOffState);
+    nsItem.setState(selected ? OS.NSOnState : OS.NSOffState);
 }
 
 /**
@@ -778,6 +788,60 @@
     } else {
         nsItem.setTitle (label);
     }
+    if (accelerator is 0) {
+        int mask = 0, key = 0;
+        if (i < buffer.length && buffer [i] is '\t') {
+            for (j = i + 1; j < buffer.length; j++) {
+                switch (buffer [j]) {
+                    case '\u2303': mask |= OS.NSControlKeyMask; i++; break;
+                    case '\u2325': mask |= OS.NSAlternateKeyMask; i++; break;
+                    case '\u21E7': mask |= OS.NSShiftKeyMask; i++; break;
+                    case '\u2318': mask |= OS.NSCommandKeyMask; i++; break;
+                    default:
+                        j = buffer.length;
+                        break;
+                }
+            }
+            switch (buffer.length - i - 1) {
+                case 1:
+                    key = buffer [i + 1];
+                    if (key is 0x2423) key = ' ';
+                    break;
+                case 2:
+                    if (buffer [i + 1] is 'F') {
+                        switch (buffer [i + 2]) {
+                            case '1': key = 0xF704; break;
+                            case '2': key = 0xF705; break;
+                            case '3': key = 0xF706; break;
+                            case '4': key = 0xF707; break;
+                            case '5': key = 0xF708; break;
+                            case '6': key = 0xF709; break;
+                            case '7': key = 0xF70A; break;
+                            case '8': key = 0xF70B; break;
+                            case '9': key = 0xF70C; break;
+                        }
+                    }
+                    break;
+                case 3:
+                    if (buffer [i + 1] is 'F' && buffer [i + 2] is '1') {
+                        switch (buffer [i + 3]) {
+                            case '0': key = 0xF70D; break;
+                            case '1': key = 0xF70E; break;
+                            case '2': key = 0xF70F; break;
+                            case '3': key = 0xF710; break;
+                            case '4': key = 0xF711; break;
+                            case '5': key = 0xF712; break;
+                        }
+                    }
+                    break;
+            }
+        }
+        NSString string = NSString.stringWith (key is 0 ? "" : (char)key + "");
+        nsItem.setKeyEquivalentModifierMask (mask);
+        nsItem.setKeyEquivalent (string.lowercaseString ());
+        //TODO - only hide when key !is 0 (fix all places)
+        nsItem.setHidden (key !is 0 || mask !is 0);
+    }
 }
 
 }