1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.glass.ui.swt;
  26 
  27 import org.eclipse.swt.SWT;
  28 import org.eclipse.swt.widgets.Display;
  29 import org.eclipse.swt.widgets.Menu;
  30 
  31 import com.sun.glass.ui.delegate.MenuBarDelegate;
  32 import com.sun.glass.ui.delegate.MenuDelegate;
  33 
  34 final class SWTMenuBarDelegate implements MenuBarDelegate {
  35     Menu menuBar;
  36 
  37     public boolean createMenuBar() {
  38         return (menuBar = Display.getDefault().getMenuBar()) != null;
  39     }
  40 
  41     //TODO - work around for possible bug in Quantum?
  42     //TODO = add 0, 1, 2, 3 => remove 3, 2, 1, leaving 0
  43     //TODO - for that point on, indexing starts from one
  44     boolean firstRemove = true;
  45     
  46     public boolean insert(MenuDelegate menu, int pos) {
  47         //System.out.println("INSERT: " + pos);
  48         if (!firstRemove) --pos;
  49         if (menuBar == null) return false;
  50         ((SWTMenuDelegate)menu).item = new org.eclipse.swt.widgets.MenuItem(menuBar, SWT.CASCADE, pos);
  51         ((SWTMenuDelegate)menu).setTitle(((SWTMenuDelegate)menu).title);
  52         ((SWTMenuDelegate)menu).setEnabled(((SWTMenuDelegate)menu).enabled);
  53         ((SWTMenuDelegate)menu).item.setMenu(((SWTMenuDelegate)menu).menu);
  54         return true;
  55     }
  56 
  57     public boolean remove(MenuDelegate menu, int pos) {
  58         //System.out.println("REMOVE: " + pos);
  59         if (!firstRemove) --pos;
  60         if (0 <= pos && pos < menuBar.getItemCount()) {
  61             menuBar.getItem(pos).dispose();
  62         }
  63         if (firstRemove && pos == 1) {
  64             --pos;
  65             firstRemove = false;
  66             //System.out.println("REMOVE (work around): " + pos);
  67             menuBar.getItem(pos).dispose();
  68         }
  69         return true;
  70     }
  71 
  72     public long getNativeMenu() {
  73         //TODO - return menu bar handle from the operating system
  74         return 1L;
  75     }
  76 }