home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Java / netscape / application / MenuView.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-04-12  |  12.9 KB  |  655 lines

  1. package netscape.application;
  2.  
  3. import netscape.util.ClassInfo;
  4. import netscape.util.CodingException;
  5. import netscape.util.Decoder;
  6. import netscape.util.Encoder;
  7.  
  8. public class MenuView extends View {
  9.    Menu menu;
  10.    MenuItem selectedItem;
  11.    public MenuView owner;
  12.    public MenuView child;
  13.    InternalWindow menuWindow;
  14.    int type;
  15.    int itemHeight;
  16.    boolean transparent;
  17.    static final String MENU_KEY = "menu";
  18.    static final String OWNER_KEY = "owner";
  19.    static final String CHILD_KEY = "child";
  20.    static final String MENUWINDOW_KEY = "menuWindow";
  21.    static final String MENUITEMHEIGHT_KEY = "itemHeight";
  22.    static final String TYPE_KEY = "type";
  23.    static final String TRANSPARENT_KEY = "transparent";
  24.    public static final int HORIZONTAL = 0;
  25.    public static final int VERTICAL = 1;
  26.  
  27.    public MenuView() {
  28.       this(0, 0, 0, 0, (Menu)null, (MenuView)null);
  29.    }
  30.  
  31.    public MenuView(Menu var1) {
  32.       this(0, 0, 0, 0, var1, (MenuView)null);
  33.    }
  34.  
  35.    public MenuView(int var1, int var2, int var3, int var4) {
  36.       this(var1, var2, var3, var4, (Menu)null, (MenuView)null);
  37.    }
  38.  
  39.    public MenuView(int var1, int var2, int var3, int var4, Menu var5) {
  40.       this(var1, var2, var3, var4, var5, (MenuView)null);
  41.    }
  42.  
  43.    public MenuView(int var1, int var2, int var3, int var4, Menu var5, MenuView var6) {
  44.       super(var1, var2, var3, var4);
  45.       this.itemHeight = 17;
  46.       this.transparent = false;
  47.       if (var5 != null) {
  48.          this.menu = var5;
  49.       } else {
  50.          this.menu = new Menu(true);
  51.       }
  52.  
  53.       this.owner = var6;
  54.       this.menuWindow = this.createMenuWindow();
  55.       this.menuWindow.addSubview(this);
  56.       if (this.menu.isTopLevel()) {
  57.          this.type = 0;
  58.          ((View)this).setHorizResizeInstruction(2);
  59.       } else {
  60.          this.type = 1;
  61.       }
  62.  
  63.       this.menu.menuView = this;
  64.    }
  65.  
  66.    protected InternalWindow createMenuWindow() {
  67.       InternalWindow var1 = new InternalWindow(0, 0, 0, 0);
  68.       var1.setType(0);
  69.       var1.setLayer(511);
  70.       var1.setCanBecomeMain(false);
  71.       var1._contentView.setTransparent(true);
  72.       var1.setScrollsToVisible(true);
  73.       return var1;
  74.    }
  75.  
  76.    public void setType(int var1) {
  77.       this.type = var1;
  78.    }
  79.  
  80.    public int type() {
  81.       return this.type;
  82.    }
  83.  
  84.    public void setMenu(Menu var1) {
  85.       this.menu = var1;
  86.    }
  87.  
  88.    public Menu menu() {
  89.       return this.menu;
  90.    }
  91.  
  92.    public void setOwner(MenuView var1) {
  93.       this.owner = var1;
  94.    }
  95.  
  96.    public MenuView owner() {
  97.       return this.owner;
  98.    }
  99.  
  100.    public Color backgroundColor() {
  101.       return this.menu.backgroundColor();
  102.    }
  103.  
  104.    public Border border() {
  105.       return this.menu.border();
  106.    }
  107.  
  108.    public void setTransparent(boolean var1) {
  109.       this.transparent = var1;
  110.       this.menuWindow.setTransparent(var1);
  111.    }
  112.  
  113.    public boolean isTransparent() {
  114.       return this.transparent;
  115.    }
  116.  
  117.    public void setItemHeight(int var1) {
  118.       if (var1 > 0) {
  119.          this.itemHeight = var1;
  120.       } else {
  121.          this.itemHeight = this.minItemHeight();
  122.       }
  123.    }
  124.  
  125.    public int itemHeight() {
  126.       if (this.itemHeight > 0) {
  127.          return this.itemHeight;
  128.       } else {
  129.          this.setItemHeight(this.minItemHeight());
  130.          return this.itemHeight;
  131.       }
  132.    }
  133.  
  134.    public int minItemHeight() {
  135.       int var2 = 0;
  136.       int var4 = this.menu.itemCount();
  137.  
  138.       for(int var1 = 0; var1 < var4; ++var1) {
  139.          int var3 = this.menu.itemAt(var1).minHeight();
  140.          if (var3 > var2) {
  141.             var2 = var3;
  142.          }
  143.       }
  144.  
  145.       return var2;
  146.    }
  147.  
  148.    public int minItemWidth() {
  149.       int var3 = 0;
  150.  
  151.       for(int var1 = 0; var1 < this.menu.itemCount(); ++var1) {
  152.          int var2 = this.menu.itemAt(var1).minWidth();
  153.          if (var2 > var3) {
  154.             var3 = var2;
  155.          }
  156.       }
  157.  
  158.       return var3;
  159.    }
  160.  
  161.    public Size minSize() {
  162.       int var3 = this.menu.itemCount();
  163.       int var1;
  164.       int var2;
  165.       if (this.type == 0) {
  166.          var1 = 0;
  167.  
  168.          for(int var4 = 0; var4 < var3; ++var4) {
  169.             MenuItem var5 = this.menu.itemAt(var4);
  170.             var1 += var5.minWidth();
  171.          }
  172.  
  173.          var2 = this.itemHeight();
  174.       } else {
  175.          var1 = this.minItemWidth();
  176.          var2 = var3 * this.itemHeight();
  177.       }
  178.  
  179.       var1 += this.border().widthMargin();
  180.       var2 += this.border().heightMargin();
  181.       return new Size(var1, var2);
  182.    }
  183.  
  184.    public MenuItem itemForPoint(int var1, int var2) {
  185.       int var4 = -1;
  186.  
  187.       for(int var3 = 0; var3 < this.menu.itemCount(); ++var3) {
  188.          Rect var6 = this.rectForItemAt(var3);
  189.          if (var6.contains(var1, var2)) {
  190.             var4 = var3;
  191.             break;
  192.          }
  193.       }
  194.  
  195.       MenuItem var5;
  196.       if (var4 >= 0) {
  197.          var5 = this.menu.itemAt(var4);
  198.       } else {
  199.          var5 = null;
  200.       }
  201.  
  202.       return var5;
  203.    }
  204.  
  205.    public int selectedIndex() {
  206.       MenuItem var1 = this.selectedItem();
  207.       return var1 == null ? -1 : this.menu.indexOfItem(var1);
  208.    }
  209.  
  210.    public MenuItem selectedItem() {
  211.       return this.selectedItem;
  212.    }
  213.  
  214.    public void selectItem(MenuItem var1) {
  215.       if (var1.isEnabled()) {
  216.          if (this.selectedItem != var1) {
  217.             if (this.selectedItem != null) {
  218.                this.selectedItem.setSelected(false);
  219.                ((View)this).addDirtyRect(this.rectForItemAt(this.menu.indexOfItem(this.selectedItem)));
  220.             }
  221.  
  222.             var1.setSelected(true);
  223.             this.selectedItem = var1;
  224.             ((View)this).addDirtyRect(this.rectForItemAt(this.menu.indexOfItem(var1)));
  225.          }
  226.  
  227.       }
  228.    }
  229.  
  230.    public void deselectItem() {
  231.       if (this.selectedItem != null) {
  232.          this.selectedItem.setSelected(false);
  233.          ((View)this).addDirtyRect(this.rectForItemAt(this.menu.indexOfItem(this.selectedItem)));
  234.          this.selectedItem = null;
  235.       }
  236.  
  237.    }
  238.  
  239.    public Rect rectForItemAt(int var1) {
  240.       if (var1 >= 0 && var1 < this.menu.itemCount()) {
  241.          int var5 = 0;
  242.          int var6;
  243.          int var7;
  244.          if (this.type == 0) {
  245.             var6 = 0;
  246.  
  247.             for(int var4 = 0; var4 < var1; ++var4) {
  248.                MenuItem var3 = this.menu.itemAt(var4);
  249.                var5 += var3.minWidth();
  250.             }
  251.  
  252.             var7 = this.menu.itemAt(var1).minWidth();
  253.          } else {
  254.             var6 = this.itemHeight() * var1;
  255.             var7 = super.bounds.width - this.border().widthMargin();
  256.          }
  257.  
  258.          var5 += this.border().leftMargin();
  259.          var6 += this.border().topMargin();
  260.          Rect var2 = Rect.newRect(var5, var6, var7, this.itemHeight());
  261.          return var2;
  262.       } else {
  263.          return null;
  264.       }
  265.    }
  266.  
  267.    public Rect interiorRect() {
  268.       Rect var1 = new Rect(this.border().leftMargin(), this.border().topMargin(), super.bounds.width - this.border().widthMargin(), super.bounds.height - this.border().heightMargin());
  269.       return var1;
  270.    }
  271.  
  272.    public void drawItemAt(int var1) {
  273.       Rect var2 = this.rectForItemAt(var1);
  274.       ((View)this).draw(var2);
  275.    }
  276.  
  277.    protected MenuView createMenuView(Menu var1) {
  278.       return new MenuView(0, 0, 0, 0, var1, this);
  279.    }
  280.  
  281.    MenuView mainOwner() {
  282.       MenuView var1;
  283.       for(var1 = this.owner; var1.owner() != null; var1 = var1.owner()) {
  284.       }
  285.  
  286.       return var1;
  287.    }
  288.  
  289.    boolean performCommandForKeyStroke(KeyStroke var1, int var2) {
  290.       if (Application.application().activeMenuViews.count() > 0) {
  291.          return false;
  292.       } else if (((View)this).window() != null && !((View)this).descendsFrom(((View)this).rootView()._mainWindow)) {
  293.          return false;
  294.       } else if (var2 != 2 && var2 != 1) {
  295.          return false;
  296.       } else {
  297.          KeyEvent var3 = new KeyEvent(0L, var1.key, var1.modifiers, true);
  298.          return this.menu.handleCommandKeyEvent(var3);
  299.       }
  300.    }
  301.  
  302.    void mouseWillDown(MouseEvent var1) {
  303.       RootView var5 = Application.application().firstRootView();
  304.       if (var5 != ((View)this).rootView()) {
  305.          this.hideAll();
  306.       } else {
  307.          MouseEvent var4 = ((View)this).rootView().convertEventToView(this, var1);
  308.          if (!((View)this).containsPoint(var4.x, var4.y)) {
  309.             if (this.selectedItem() != null) {
  310.                for(MenuView var3 = this.child; var3 != null; var3 = var3.child) {
  311.                   Rect var2 = new Rect(var3.bounds);
  312.                   ((View)var3).superview().convertRectToView((View)null, var2, var2);
  313.                   if (Rect.contains(var2.x, var2.y, var2.width, var2.height, var1.x, var1.y)) {
  314.                      return;
  315.                   }
  316.                }
  317.             }
  318.  
  319.             this.hideAll();
  320.          }
  321.       }
  322.    }
  323.  
  324.    void hideAll() {
  325.       Application.application().removeActiveMenuView(this);
  326.       if (this.child != null) {
  327.          this.child.hide();
  328.       }
  329.  
  330.       if (this.isVisible()) {
  331.          this.hide();
  332.       }
  333.  
  334.       this.deselectItem();
  335.    }
  336.  
  337.    public boolean mouseDown(MouseEvent var1) {
  338.       if (this.owner != null) {
  339.          return true;
  340.       } else {
  341.          MenuItem var2 = this.itemForPoint(var1.x, var1.y);
  342.          if (var2 != null && var2.isEnabled()) {
  343.             if (this.selectedItem != null && var2 == this.selectedItem) {
  344.                if (this.isVisible()) {
  345.                   return true;
  346.                } else {
  347.                   Application.application().removeActiveMenuView(this);
  348.                   if (this.child != null) {
  349.                      this.child.hide();
  350.                      this.child = null;
  351.                   }
  352.  
  353.                   this.deselectItem();
  354.                   return true;
  355.                }
  356.             } else {
  357.                this.selectItem(var2);
  358.                if (var2.isEnabled() && var2.hasSubmenu()) {
  359.                   MenuView var3 = this.createMenuView(var2.submenu());
  360.                   var3.show(((View)this).rootView(), var1);
  361.                   this.child = var3;
  362.                }
  363.  
  364.                return true;
  365.             }
  366.          } else {
  367.             if (this.child != null) {
  368.                this.child.hide();
  369.             }
  370.  
  371.             this.deselectItem();
  372.             return true;
  373.          }
  374.       }
  375.    }
  376.  
  377.    public void mouseDragged(MouseEvent var1) {
  378.       MenuItem var2 = this.itemForPoint(var1.x, var1.y);
  379.       if (!Rect.contains(0, 0, ((View)this).width(), ((View)this).height(), var1.x, var1.y)) {
  380.          var2 = null;
  381.          if (this.owner != null && this.child == null || this.owner == null && this.child == null && this.isVisible()) {
  382.             this.deselectItem();
  383.          }
  384.       }
  385.  
  386.       if (var2 == null || var2.isEnabled()) {
  387.          for(MenuView var4 = this.child; var4 != null && var4.isVisible(); var4 = var4.child) {
  388.             Rect var5 = new Rect(var4.bounds);
  389.             MouseEvent var3 = ((View)this).convertEventToView(var4, var1);
  390.             if (Rect.contains(var5.x, var5.y, var5.width, var5.height, var3.x, var3.y)) {
  391.                var2 = var4.itemForPoint(var3.x, var3.y);
  392.                if (var2 != null) {
  393.                   var4.mouseDragged(var3);
  394.                   var4.autoscroll(var3);
  395.                   return;
  396.                }
  397.             } else if (var4.child == null) {
  398.                if (var4.selectedItem() == null) {
  399.                   var4.autoscroll(var3);
  400.                }
  401.  
  402.                var4.deselectItem();
  403.             }
  404.          }
  405.  
  406.          if (var2 != null && var2 != this.selectedItem()) {
  407.             if (this.selectedItem != null && this.selectedItem.hasSubmenu() && this.child != null && this.child.isVisible()) {
  408.                this.child.hide();
  409.                this.child = null;
  410.             }
  411.  
  412.             this.selectItem(var2);
  413.             if (var2.hasSubmenu()) {
  414.                MenuView var6 = this.createMenuView(var2.submenu());
  415.                var6.show(((View)this).rootView(), var1);
  416.                this.child = var6;
  417.             }
  418.          }
  419.  
  420.       }
  421.    }
  422.  
  423.    public void mouseUp(MouseEvent var1) {
  424.       Object var2 = null;
  425.       MenuItem var4 = this.itemForPoint(var1.x, var1.y);
  426.       if (this.owner == null && this.selectedItem == var4) {
  427.          if (!this.isVisible() || this.selectedItem == null) {
  428.             return;
  429.          }
  430.  
  431.          Application.application().removeActiveMenuView(this);
  432.          this.hide();
  433.          this.menu.performCommand("", var4);
  434.       }
  435.  
  436.       if (this.owner == null && var4 == null) {
  437.          MenuView var3;
  438.          for(var3 = this.child; var3.child != null; var3 = var3.child) {
  439.          }
  440.  
  441.          var3.mouseUp(((View)this).convertEventToView(var3, var1));
  442.       }
  443.  
  444.       if (this.owner != null && this.selectedItem != null) {
  445.          var4 = this.selectedItem;
  446.          Application.application().removeActiveMenuView(this.mainOwner());
  447.          this.hide();
  448.  
  449.          MenuView var6;
  450.          for(var6 = this.owner; var6.owner() != null; var6 = var6.owner()) {
  451.             var6.hide();
  452.          }
  453.  
  454.          var6.hide();
  455.          this.menu.performCommand("", var4);
  456.       }
  457.  
  458.    }
  459.  
  460.    public void mouseEntered(MouseEvent var1) {
  461.       if (this.owner == null && this.selectedItem != null || this.isVisible()) {
  462.          this.mouseDragged(var1);
  463.       }
  464.  
  465.    }
  466.  
  467.    public void mouseMoved(MouseEvent var1) {
  468.       if (this.owner == null && this.selectedItem != null || this.isVisible()) {
  469.          this.mouseDragged(var1);
  470.       }
  471.  
  472.    }
  473.  
  474.    public void mouseExited(MouseEvent var1) {
  475.       if (this.owner == null && this.selectedItem != null || this.isVisible()) {
  476.          this.mouseDragged(var1);
  477.       }
  478.  
  479.    }
  480.  
  481.    public void drawView(Graphics var1) {
  482.       this.border().drawInRect(var1, 0, 0, ((View)this).width(), ((View)this).height());
  483.       int var5 = this.menu.itemCount();
  484.       int var6 = 0;
  485.  
  486.       for(int var4 = 0; var4 < var5; ++var4) {
  487.          MenuItem var3 = this.menu.itemAt(var4);
  488.          Rect var2 = this.rectForItemAt(var4);
  489.          var6 += var2.width;
  490.          if (var1.clipRect().intersects(var2)) {
  491.             var1.pushState();
  492.             var1.setClipRect(var2);
  493.             if (!this.isTransparent()) {
  494.                var1.setColor(this.backgroundColor());
  495.                var1.fillRect(var2.x, var2.y, var2.width, var2.height);
  496.             }
  497.  
  498.             boolean var7;
  499.             if (this.type == 0) {
  500.                var7 = false;
  501.             } else {
  502.                var7 = true;
  503.             }
  504.  
  505.             var3.drawInRect(var1, var2, var7);
  506.             var1.popState();
  507.             Rect.returnRect(var2);
  508.          }
  509.       }
  510.  
  511.       if (var6 < this.interiorRect().width && this.type == 0 && !this.isTransparent()) {
  512.          var1.setColor(this.backgroundColor());
  513.          var1.fillRect(this.interiorRect().x + var6, this.interiorRect().y, this.interiorRect().width - var6, this.interiorRect().height);
  514.       }
  515.  
  516.    }
  517.  
  518.    public void show(RootView var1, MouseEvent var2) {
  519.       if (this.menu.itemCount() != 0) {
  520.          int var5 = this.minItemWidth() + this.border().widthMargin() + 20;
  521.          int var6 = this.itemHeight() * this.menu.itemCount() + this.border().heightMargin();
  522.          ((View)this).sizeTo(var5, var6);
  523.          Menu var9;
  524.          if (this.menu.superitem() != null) {
  525.             var9 = this.menu.superitem().supermenu();
  526.          } else {
  527.             var9 = this.menu;
  528.          }
  529.  
  530.          MenuView var10;
  531.          if (this.owner != null) {
  532.             var10 = this.owner;
  533.          } else {
  534.             var10 = this;
  535.          }
  536.  
  537.          Rect var3 = Rect.newRect(0, 0, ((View)this).width(), ((View)this).height());
  538.          ((View)var10).convertRectToView((View)null, var3, var3);
  539.          if (var1.windowClipView() != null && this.menuWindow.layer() != 511) {
  540.             ((View)var1).convertRectToView(var1.windowClipView(), var3, var3);
  541.          }
  542.  
  543.          Rect var4 = var10.rectForItemAt(var10.selectedIndex());
  544.          if (var4 == null) {
  545.             var4 = new Rect(0, 0, 0, 0);
  546.          }
  547.  
  548.          int var7;
  549.          int var8;
  550.          if (var9.isTopLevel() && var10.type == 0) {
  551.             var7 = var3.x + var4.x - var10.border().leftMargin();
  552.             var8 = var3.y + ((View)var10).height();
  553.          } else if (this.menu.isTopLevel() && this.type == 1) {
  554.             var7 = var2.x;
  555.             var8 = var2.y;
  556.          } else {
  557.             var7 = var3.x + var4.width + var10.border().widthMargin() - 3;
  558.             var8 = var3.y + var4.maxY() - var4.height - var10.border().topMargin();
  559.          }
  560.  
  561.          int var11 = ((View)this).width();
  562.          this.menuWindow.setBounds(var7, var8, ((View)this).width(), ((View)this).height());
  563.          ((View)this).sizeTo(var11, ((View)this).height());
  564.          Rect.returnRect(var3);
  565.          Rect.returnRect(var4);
  566.          if (this.owner != null) {
  567.             var10 = this.mainOwner();
  568.          } else {
  569.             var10 = this;
  570.          }
  571.  
  572.          Application.application().addActiveMenuView(var10);
  573.          this.menuWindow.setRootView(var1);
  574.          this.menuWindow.show();
  575.       }
  576.    }
  577.  
  578.    public void hide() {
  579.       if (this.menuWindow.isVisible()) {
  580.          this.menuWindow.hide();
  581.       }
  582.  
  583.       if (this.selectedItem != null && this.selectedItem.hasSubmenu()) {
  584.          this.child.hide();
  585.          this.child = null;
  586.       }
  587.  
  588.       this.deselectItem();
  589.    }
  590.  
  591.    public boolean isVisible() {
  592.       return this.menuWindow.isVisible();
  593.    }
  594.  
  595.    public boolean wantsAutoscrollEvents() {
  596.       return true;
  597.    }
  598.  
  599.    void autoscroll(MouseEvent var1) {
  600.       Rect var2 = Rect.newRect();
  601.       ((View)this).computeVisibleRect(var2);
  602.       if (!var2.contains(var1.x, var1.y)) {
  603.          if (var1.y < var2.y) {
  604.             Rect var3 = Rect.newRect(var2.x, var1.y, var2.width, this.itemHeight());
  605.             ((View)this).scrollRectToVisible(var3);
  606.             Rect.returnRect(var3);
  607.          } else if (var1.y > var2.maxY()) {
  608.             Rect var4 = Rect.newRect(var2.x, var1.y - this.itemHeight(), var2.width, this.itemHeight());
  609.             ((View)this).scrollRectToVisible(var4);
  610.             Rect.returnRect(var4);
  611.          }
  612.       }
  613.  
  614.       Rect.returnRect(var2);
  615.    }
  616.  
  617.    public void describeClassInfo(ClassInfo var1) {
  618.       super.describeClassInfo(var1);
  619.       var1.addClass("netscape.application.MenuView", 1);
  620.       var1.addField("menu", (byte)18);
  621.       var1.addField("owner", (byte)18);
  622.       var1.addField("child", (byte)18);
  623.       var1.addField("menuWindow", (byte)18);
  624.       var1.addField("itemHeight", (byte)8);
  625.       var1.addField("type", (byte)8);
  626.       var1.addField("transparent", (byte)0);
  627.    }
  628.  
  629.    public void encode(Encoder var1) throws CodingException {
  630.       super.encode(var1);
  631.       var1.encodeObject("menu", this.menu);
  632.       var1.encodeObject("owner", this.owner);
  633.       var1.encodeObject("child", this.child);
  634.       var1.encodeObject("menuWindow", this.menuWindow);
  635.       var1.encodeInt("itemHeight", this.itemHeight);
  636.       var1.encodeInt("type", this.type);
  637.       var1.encodeBoolean("transparent", this.transparent);
  638.    }
  639.  
  640.    public void decode(Decoder var1) throws CodingException {
  641.       super.decode(var1);
  642.       this.menu = (Menu)var1.decodeObject("menu");
  643.       this.owner = (MenuView)var1.decodeObject("owner");
  644.       this.child = (MenuView)var1.decodeObject("child");
  645.       this.menuWindow = (InternalWindow)var1.decodeObject("menuWindow");
  646.       this.itemHeight = var1.decodeInt("itemHeight");
  647.       this.type = var1.decodeInt("type");
  648.       this.transparent = var1.decodeBoolean("transparent");
  649.    }
  650.  
  651.    public void finishDecoding() throws CodingException {
  652.       super.finishDecoding();
  653.    }
  654. }
  655.