package inferno.awt; import java.awt.*; import java.awt.event.*; import java.awt.peer.*; public class iMenuBarPeer implements MenuBarPeer,MouseListener,MouseMotionListener,zMenuParent { MenuBar target; int height; zMenuBarObject selectedMenuObject; zMenuBarObject menuObjects[]; int menuObjectCount; Window child; public void closeChild(Window w) { // Called from child via zMenuParent interface to // close itself down. Since Window may take some time to // close down, check to see if we are still the child window // not the previous window if (child!=null && child==w) { child.setVisible(false); // Hide Window child.dispose(); // Destroy window child=null; // Lose reference to Window if (selectedMenuObject!=null) { // If menu item selected, display not selected Frame frm=(Frame)target.getParent(); Graphics g=frm.getGraphics(); selectedMenuObject.paint(g,0); g.dispose(); } } } public void closeEverything() { // Close our child closeChild(child); } void create(Frame f) { // Calculate sizes recalculate(); } public iMenuBarPeer(MenuBar target) { Frame frm=(Frame)target.getParent(); frm.addMouseListener(this); frm.addMouseMotionListener(this); this.target = target; create(frm); } // Required by MenuBarPeer interface but not used public void dispose() { } public void addMenu(Menu m) { // Calculate sizes recalculate(); } public void delMenu(int index) { // Calculate sizes recalculate(); } public void addHelpMenu(Menu m) { // Calculate sizes recalculate(); } // Calculate height of menubar public int getHeight() { height=0; if (target!=null) { Frame frm=(Frame)target.getParent(); for (int i=target.getMenuCount()-1;i>=0;--i) { Menu m=target.getMenu(i); Font f=m.getFont(); FontMetrics fm=frm.getFontMetrics(f); height=Math.max(height,fm.getHeight()+2); } } return height; } public void recalculate() { // Create all the objects if (target!=null) { Frame frm=(Frame)target.getParent(); Rectangle d=frm.getBounds(); Insets is=frm.getInsets(); Menu hm=target.getHelpMenu(); selectedMenuObject=null; menuObjects=null; menuObjectCount=target.getMenuCount(); if (menuObjectCount!=0) { int x=is.left; int y=is.top-height; menuObjects=new zMenuBarObject[menuObjectCount]; for (int i=0;i0,g); else g.fillRect(area.x,area.y,area.width,area.height); g.translate(-mode,-mode); g.setFont(f); if (menu.isEnabled()) { g.setColor(SystemColor.menuText); g.drawString(" "+menu.getLabel(),area.x+1,area.y+fm.getAscent()+1); } else { Misc.drawDisabledString(" "+menu.getLabel(),area.x+1,area.y+fm.getAscent()+1,g); } g.translate(+mode,+mode); } } // This the window containing the dropdown menus // the painting is done by zMenuDisplay // zMenuDisplay returns its size and the Window sizes itself // accordingly class zMenuWindow extends Window implements ComponentListener,zMenuParent{ zMenuDisplay menudisplay; zMenuParent ancestor; int xoffset,yoffset; public zMenuWindow(Frame f,zMenuParent ancestor,Menu menu,int xoffset,int yoffset) { super(f); this.xoffset=xoffset; this.yoffset=yoffset; this.ancestor=ancestor; Point p=f.getLocation(); p.translate(this.xoffset,this.yoffset); setLocation(p); f.addComponentListener(this); this.menudisplay=new zMenuDisplay(menu); add(this.menudisplay,BorderLayout.CENTER); } public void closeChild(Window w) { if (menudisplay!=null) { if (!menudisplay.gotFocus() && ancestor!=null) ancestor.closeChild(this); else menudisplay.closeChild(w); } } public void closeEverything() { if (ancestor!=null) ancestor.closeEverything(); } public boolean gotFocus() { if (menudisplay==null) return false; else return menudisplay.gotFocus(); } public void componentHidden(ComponentEvent e) { } public void componentMoved(ComponentEvent e) { Point p=e.getComponent().getLocation(); p.translate(xoffset,yoffset); setLocation(p); } public void componentResized(ComponentEvent e) { } public void componentShown(ComponentEvent e) { } public void dispose() { if (menudisplay!=null) { menudisplay.closeChild(this); } super.dispose(); } } // This is the display part of the zMenuWindow // it covers the whole window class zMenuDisplay extends Canvas implements MouseListener,MouseMotionListener, FocusListener{ zMenuObject menuObjects[]; zMenuObject selectedMenuObject; zMenuWindow child; int menuObjectCount; Dimension pSize; boolean focus; Menu menu; static Color highlight = SystemColor.controlHighlight; static Color light = SystemColor.controlLtHighlight; static Color medium = SystemColor.controlShadow; static Color dark = SystemColor.controlDkShadow; static final int TickBoxSize=13; static final int TickLabelGap=4; static final int SeparatorHeight=6; public zMenuDisplay(Menu menu) { super(); setMenu(menu); addMouseListener(this); addMouseMotionListener(this); addFocusListener(this); } public boolean isFocusTraversable() { return false; } void setMenu(Menu menu) { int maxwidth=0; int yy=4; int xx=4; this.menu=menu; menuObjectCount=menu.getItemCount(); selectedMenuObject=null; if (menuObjectCount==0) menuObjects=null; else { menuObjects=new zMenuObject[menuObjectCount]; boolean submenu=false; for (int i=0;i0,g); else g.fillRect(xx,yy,area.width,area.height); xx+=2; yy+=2; g.setColor(SystemColor.menuText); g.translate(-mode,-mode); if (checked) Misc.drawTick(g,xx,yy,zMenuDisplay.TickBoxSize); g.drawString(s,zMenuDisplay.TickBoxSize+zMenuDisplay.TickLabelGap+xx,yy+fa); if (submenu) drawArrow(g,area.x+area.width-6,area.y+area.height/2); g.translate(+mode,+mode); } else { xx+=2; yy+=2; if (checked) { g.setColor(SystemColor.controlLtHighlight); Misc.drawTick(g,xx+1,yy+1,zMenuDisplay.TickBoxSize); g.setColor(SystemColor.controlShadow); Misc.drawTick(g,xx,yy,zMenuDisplay.TickBoxSize); } Misc.drawDisabledString(s,zMenuDisplay.TickBoxSize+zMenuDisplay.TickLabelGap+xx,yy+fa,g); if (submenu) { g.setColor(SystemColor.controlLtHighlight); drawArrow(g,area.x+area.width-6+1,area.y+area.height/2+1); g.setColor(SystemColor.controlShadow); drawArrow(g,area.x+area.width-6,area.y+area.height/2); } } } } // separator bar class zMenuSeparator extends zMenuObject { public zMenuSeparator(Rectangle area) { super(area); } public void paint(Graphics g,int mode) { int y=area.y+zMenuDisplay.SeparatorHeight/2-1; g.setColor(zMenuDisplay.medium); g.drawLine(area.x,y,area.x+area.width,y); y+=1; g.setColor(zMenuDisplay.light); g.drawLine(area.x,y,area.x+area.width,y); } } // The interface to look backwards to parent to close ourselves interface zMenuParent { // Called by child (or parent) to close and delete itself public void closeChild(Window W); // Close all the windows back to the menubar public void closeEverything(); }