/**
   * Hook the given Listener to the Mac OS X application Quit menu and the IActions to the About and
   * Preferences menus.
   *
   * @param display The Display to use.
   * @param quitListener The listener to invoke when the Quit menu is invoked.
   * @param aboutAction The action to run when the About menu is invoked.
   * @param preferencesAction The action to run when the Preferences menu is invoked.
   */
  public void hookApplicationMenu(
      Display display, Listener quitListener, IAction aboutAction, IAction preferencesAction) {
    // This is our callbackObject whose 'actionProc' method will be called when the About or
    // Preferences menuItem is invoked.
    MenuHookObject target = new MenuHookObject(aboutAction, preferencesAction);

    try {
      // Initialize the menuItems.
      initialize(target);
    } catch (Exception e) {
      throw new IllegalStateException(e);
    }

    // Connect the quit/exit menu.
    if (!display.isDisposed()) {
      display.addListener(SWT.Close, quitListener);
    }

    // Schedule disposal of callback object
    display.disposeExec(
        new Runnable() {
          public void run() {
            invoke(proc3Args, "dispose");
          }
        });
  }
Exemple #2
0
  /*
   * @see IColorManager#getColor(RGB)
   */
  public Color getColor(RGB rgb) {

    if (rgb == null) {
      return null;
    }

    final Display display = Display.getCurrent();
    Map<RGB, Color> colorTable = fDisplayTable.get(display);
    if (colorTable == null) {
      colorTable = new HashMap<RGB, Color>(10);
      fDisplayTable.put(display, colorTable);
      if (fAutoDisposeOnDisplayDispose) {
        display.disposeExec(
            new Runnable() {
              public void run() {
                dispose(display);
              }
            });
      }
    }

    Color color = colorTable.get(rgb);
    if (color == null) {
      color = new Color(Display.getCurrent(), rgb);
      colorTable.put(rgb, color);
    }

    return color;
  }
 private void hookDisplay() {
   fDisplay.disposeExec(
       new Runnable() {
         @Override
         public void run() {
           dispose();
         }
       });
 }
  /** @param display */
  protected void allocateDelegate(Display display) {
    try {
      delegate = new SWTCocoaEnhancerDelegate();
      delegate.alloc().init();
      // call OS.NewGlobalRef
      Method method = OS.class.getMethod("NewGlobalRef", new Class[] {Object.class}); // $NON-NLS-1$
      Object object = method.invoke(OS.class, new Object[] {CocoaUIHandler.this});
      delegateJniRef = convertToLong(object);
    } catch (Exception e) {
      // theoretically, one of
      // SecurityException,Illegal*Exception,InvocationTargetException,NoSuch*Exception
      // not expected to happen at all.
      log(e);
    }
    if (delegateJniRef == 0) SWT.error(SWT.ERROR_NO_HANDLES);

    try {
      Field idField = SWTCocoaEnhancerDelegate.class.getField("id"); // $NON-NLS-1$
      Object idValue = idField.get(delegate);
      invokeMethod(
          OS.class,
          "object_setInstanceVariable", //$NON-NLS-1$
          new Object[] {idValue, SWT_OBJECT, wrapPointer(delegateJniRef)});
      display.disposeExec(
          new Runnable() {
            public void run() {
              // TODO Auto-generated method stub
              if (delegateJniRef != 0) {
                try {
                  invokeMethod(
                      OS.class,
                      "DeleteGlobalRef",
                      new Object[] {wrapPointer(delegateJniRef)}); // $NON-NLS-1$
                } catch (Exception e) {
                  // theoretically, one of
                  // SecurityException,Illegal*Exception,InvocationTargetException,NoSuch*Exception
                  // not expected to happen at all.
                  log(e);
                }
              }
              delegateJniRef = 0;

              if (delegate != null) delegate.release();
              delegate = null;
            }
          });
    } catch (Exception e) {
      // theoretically, one of
      // SecurityException,Illegal*Exception,InvocationTargetException,NoSuch*Exception
      // not expected to happen at all.
      log(e);
    }
  }
Exemple #5
0
  /** See Apple Technical Q&A 1079 (http://developer.apple.com/qa/qa2001/qa1079.html) */
  @Override
  public void initPlatformMenu(Display display, final PlatformMenuActions actions) {
    // Callback target
    Object target =
        new Object() {
          int commandProc(int nextHandler, int theEvent, int userData) {
            if (OS.GetEventKind(theEvent) == OS.kEventProcessCommand) {
              HICommand command = new HICommand();
              OS.GetEventParameter(
                  theEvent,
                  OS.kEventParamDirectObject,
                  OS.typeHICommand,
                  null,
                  HICommand.sizeof,
                  null,
                  command);
              switch (command.commandID) {
                case kHICommandPreferences:
                  actions.doPreferences();
                  return OS.noErr;
                case kHICommandAbout:
                  actions.doAbout();
                  return OS.noErr;
                case OS.kHICommandQuit:
                  actions.doExitApplication();
                  return OS.noErr;
                default:
                  break;
              }
            }
            return OS.eventNotHandledErr;
          }
        };

    final Callback commandCallback = new Callback(target, "commandProc", 3); // $NON-NLS-1$
    int commandProc = commandCallback.getAddress();
    if (commandProc == 0) {
      commandCallback.dispose();
      return; // give up
    }

    // Install event handler for commands
    int[] mask = new int[] {OS.kEventClassCommand, OS.kEventProcessCommand};
    OS.InstallEventHandler(
        OS.GetApplicationEventTarget(), commandProc, mask.length / 2, mask, 0, null);

    // create About Eclipse menu command
    int[] outMenu = new int[1];
    short[] outIndex = new short[1];
    if (OS.GetIndMenuItemWithCommandID(0, kHICommandPreferences, 1, outMenu, outIndex) == OS.noErr
        && outMenu[0] != 0) {
      int menu = outMenu[0];

      int l = fAboutActionName.length();
      char buffer[] = new char[l];
      fAboutActionName.getChars(0, l, buffer, 0);
      int str = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, buffer, l);
      OS.InsertMenuItemTextWithCFString(menu, str, (short) 0, 0, kHICommandAbout);
      OS.CFRelease(str);

      // add separator between About & Preferences
      OS.InsertMenuItemTextWithCFString(menu, 0, (short) 1, OS.kMenuItemAttrSeparator, 0);

      // enable pref menu
      OS.EnableMenuCommand(menu, kHICommandPreferences);

      // disable services menu
      OS.DisableMenuCommand(menu, kHICommandServices);
    }

    // schedule disposal of callback object
    display.disposeExec(
        new Runnable() {
          public void run() {
            commandCallback.dispose();
          }
        });
  }
 /** Hook a dispose listener on the SWT display. */
 private void hookDisplayDispose() {
   display.disposeExec(displayRunnable);
 }
  /**
   * See Apple Technical Q&A 1079 (http://developer.apple.com/qa/qa2001/qa1079.html)<br>
   * Also
   * http://developer.apple.com/documentation/Carbon/Reference/Menu_Manager/menu_mgr_ref/function_group_10.html
   */
  public void hookApplicationMenu(final Display display) {
    try {
      final Object commandCallback =
          constCallback3.newInstance(CarbonUIEnhancer.class, "commandProc", 3); // $NON-NLS-1$
      int commandProc =
          ((Number) mCallback_getAddress.invoke(commandCallback, new Object[] {})).intValue();
      if (commandProc == 0) {
        mCallback_dispose.invoke(commandCallback, new Object[] {});
        return; // give up
      }

      // Install event handler for commands
      int[] mask = new int[] {kEventClassCommand, kEventProcessCommand};
      int appTarget =
          ((Number) invoke(claOS, null, "GetApplicationEventTarget", new Object[] {})).intValue();
      invoke(
          claOS,
          null,
          "InstallEventHandler",
          new Class[] {int.class, int.class, int.class, int[].class, int.class, int[].class},
          new Object[] {appTarget, commandProc, mask.length / 2, mask, 0, null});

      // create About menu command
      int[] outMenu = new int[1];
      short[] outIndex = new short[1];
      // int GetIndMenuItemWithCommandID(int mHandle, int commandId, int index, int[] outMenu,
      // short[] outIndex);
      int ind =
          ((Number)
                  invoke(
                      claOS,
                      null,
                      "GetIndMenuItemWithCommandID",
                      new Class[] {int.class, int.class, int.class, int[].class, short[].class},
                      new Object[] {0, kHICommandPreferences, 1, outMenu, outIndex}))
              .intValue();
      if (ind == noErr && outMenu[0] != 0) {
        int menu = outMenu[0];

        int l = fgAboutActionName.length();
        char buffer[] = new char[l];
        fgAboutActionName.getChars(0, l, buffer, 0);
        int str = CFStringCreateWithCharacters(kCFAllocatorDefault, buffer, l);
        InsertMenuItemTextWithCFString(menu, str, (short) 0, 0, kHICommandAbout);
        invoke(claOS, null, "CFRelease", new Object[] {str});
        // add separator between About & Preferences
        InsertMenuItemTextWithCFString(menu, 0, (short) 1, kMenuItemAttrSeparator, 0);

        // enable pref menu
        invoke(claOS, null, "EnableMenuCommand", new Object[] {menu, kHICommandPreferences});
        // disable services menu
        invoke(claOS, null, "DisableMenuCommand", new Object[] {menu, kHICommandServices});

        if (!isAZ3) {
          // wizard menu
          l = fgWizardActionName.length();
          buffer = new char[l];
          fgWizardActionName.getChars(0, l, buffer, 0);
          str = CFStringCreateWithCharacters(kCFAllocatorDefault, buffer, l);
          InsertMenuItemTextWithCFString(menu, str, (short) 3, 0, kHICommandWizard);
          invoke(claOS, null, "CFRelease", new Object[] {str});

          // NAT test menu
          l = fgNatTestActionName.length();
          buffer = new char[l];
          fgNatTestActionName.getChars(0, l, buffer, 0);
          str = CFStringCreateWithCharacters(kCFAllocatorDefault, buffer, l);
          InsertMenuItemTextWithCFString(menu, str, (short) 4, 0, kHICommandNatTest);
          invoke(claOS, null, "CFRelease", new Object[] {str});

          // SpeedTest
          l = fgSpeedTestActionName.length();
          buffer = new char[l];
          fgSpeedTestActionName.getChars(0, l, buffer, 0);
          str = CFStringCreateWithCharacters(kCFAllocatorDefault, buffer, l);
          InsertMenuItemTextWithCFString(menu, str, (short) 5, 0, kHICommandSpeedTest);
          invoke(claOS, null, "CFRelease", new Object[] {str});
        }

        InsertMenuItemTextWithCFString(menu, 0, (short) 6, kMenuItemAttrSeparator, 0);

        // restart menu
        l = fgRestartActionName.length();
        buffer = new char[l];
        fgRestartActionName.getChars(0, l, buffer, 0);
        str = CFStringCreateWithCharacters(kCFAllocatorDefault, buffer, l);
        InsertMenuItemTextWithCFString(menu, str, (short) 7, 0, kHICommandRestart);
        invoke(claOS, null, "CFRelease", new Object[] {str});

        InsertMenuItemTextWithCFString(menu, 0, (short) 8, kMenuItemAttrSeparator, 0);
      }

      // schedule disposal of callback object
      display.disposeExec(
          new AERunnable() {
            public void runSupport() {
              try {
                mCallback_dispose.invoke(commandCallback, new Object[] {});
              } catch (Throwable e) {
              }
              //               stopSidekick();
            }
          });
    } catch (Throwable e) {
      Debug.out("Failed hookApplicatioMenu", e);
    }
  }