Annotation of modules/damieng/graphical_editor/daxe/lib/src/menu.dart, revision 1.1
1.1 ! damieng 1: /*
! 2: This file is part of Daxe.
! 3:
! 4: Daxe is free software: you can redistribute it and/or modify
! 5: it under the terms of the GNU General Public License as published by
! 6: the Free Software Foundation, either version 3 of the License, or
! 7: (at your option) any later version.
! 8:
! 9: Daxe is distributed in the hope that it will be useful,
! 10: but WITHOUT ANY WARRANTY; without even the implied warranty of
! 11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 12: GNU General Public License for more details.
! 13:
! 14: You should have received a copy of the GNU General Public License
! 15: along with Daxe. If not, see <http://www.gnu.org/licenses/>.
! 16: */
! 17:
! 18: part of daxe;
! 19:
! 20: class Menu extends MenuItem {
! 21: List<MenuItem> items;
! 22: String menuid;
! 23:
! 24: Menu(String title) : super(title, null) {
! 25: items = new List<MenuItem>();
! 26: this.menuid = "menu_${MenuItem.itemidcount-1}";
! 27: }
! 28:
! 29: add(MenuItem item) {
! 30: item.parent = this;
! 31: items.add(item);
! 32: }
! 33:
! 34: @override
! 35: h.Element htmlItem() {
! 36: h.TableRowElement tr = new h.TableRowElement();
! 37: tr.id = itemid;
! 38: tr.setAttribute('tabindex', '-1');
! 39: tr.onKeyDown.listen((h.KeyboardEvent event) {
! 40: if (h.document.activeElement != tr)
! 41: return; // an item could have the focus, and the onKeyDown will trigger
! 42: int keyCode = event.keyCode;
! 43: if (keyCode == h.KeyCode.ENTER) {
! 44: select();
! 45: } else if (keyCode == h.KeyCode.UP) {
! 46: (parent as Menu).selectPrevious(this);
! 47: } else if (keyCode == h.KeyCode.DOWN) {
! 48: (parent as Menu).selectNext(this);
! 49: } else if (keyCode == h.KeyCode.LEFT) {
! 50: if (parent is Menu) {
! 51: if ((parent as Menu).parent is Menu)
! 52: (parent as Menu).select();
! 53: else if ((parent as Menu).parent is MenuBar)
! 54: ((parent as Menu).parent as MenuBar).selectPrevious(parent as Menu);
! 55: }
! 56: } else if (keyCode == h.KeyCode.RIGHT) {
! 57: for (MenuItem item in items) {
! 58: if (item.enabled) {
! 59: item.select();
! 60: break;
! 61: }
! 62: }
! 63: } else if (keyCode == h.KeyCode.TAB) {
! 64: Timer.run(closeMenu);
! 65: }
! 66: });
! 67: h.TableCellElement td = new h.TableCellElement();
! 68: td.text = _title;
! 69: td.onMouseOver.listen((h.MouseEvent event) {
! 70: if (enabled) {
! 71: select();
! 72: show();
! 73: }
! 74: });
! 75: tr.append(td);
! 76: td = new h.TableCellElement();
! 77: td.text = ">";
! 78: h.DivElement divSubMenu = htmlMenu();
! 79: divSubMenu.classes.remove('dropdown_menu');
! 80: divSubMenu.classes.add('submenu');
! 81: divSubMenu.style.display = 'none';
! 82: td.append(divSubMenu);
! 83: td.onMouseOver.listen((h.MouseEvent event) {
! 84: if (enabled) {
! 85: select();
! 86: show();
! 87: }
! 88: });
! 89: tr.append(td);
! 90: if (!enabled)
! 91: tr.classes.add('disabled');
! 92: if (toolTipText != null)
! 93: tr.title = toolTipText;
! 94: return(tr);
! 95: }
! 96:
! 97: h.Element htmlMenu() {
! 98: h.DivElement div = new h.DivElement();
! 99: div.classes.add('dropdown_menu');
! 100: div.id = menuid;
! 101: h.TableElement table = new h.TableElement();
! 102: table.classes.add('menu');
! 103: for (MenuItem item in items) {
! 104: table.append(item.htmlItem());
! 105: }
! 106: div.append(table);
! 107: return(div);
! 108: }
! 109:
! 110: h.Element getMenuHTMLNode() {
! 111: return(h.querySelector("#$menuid"));
! 112: }
! 113:
! 114: void show() {
! 115: h.DivElement div = getMenuHTMLNode();
! 116: div.style.display = 'block';
! 117: //h.SpanElement spanTitle = h.query("#menutitle_$id");
! 118: // to avoid setting coordinates, we use a div with position absolute
! 119: // and top 100% inside a div with position relative...
! 120: //int top = spanTitle.offsetTop + spanTitle.offsetHeight;
! 121: //int left = spanTitle.offsetLeft;
! 122: //hMenu.style.top = "${top}px";
! 123: //hMenu.style.left = "${left}px";
! 124: }
! 125:
! 126: void hide() {
! 127: for (MenuItem item in items) {
! 128: if (item is Menu)
! 129: item.hide();
! 130: item.deselect();
! 131: }
! 132: h.DivElement div = getMenuHTMLNode();
! 133: div.style.display = 'none';
! 134: }
! 135:
! 136: bool isVisible() {
! 137: h.DivElement div = getMenuHTMLNode();
! 138: return(div.style.display != 'none');
! 139: }
! 140:
! 141: void addSeparator() {
! 142: items.add(new MenuItem.separator());
! 143: }
! 144:
! 145: void deselectOtherItems(MenuItem menuitem) {
! 146: for (MenuItem item in items) {
! 147: if (item != menuitem)
! 148: item.deselect();
! 149: }
! 150: }
! 151:
! 152: void set title(String title) {
! 153: _title = title;
! 154: h.Element hel = h.querySelector("#$itemid");
! 155: if (hel is h.TableRowElement) {
! 156: h.TableRowElement tr = hel;
! 157: h.TableCellElement td = tr.nodes.first;
! 158: td.text = title;
! 159: } else if (hel is h.DivElement) {
! 160: h.Node firstNode = hel.firstChild;
! 161: if (firstNode is h.Text)
! 162: firstNode.text = title;
! 163: }
! 164: }
! 165:
! 166: void checkEnabled() {
! 167: bool en = false;
! 168: for (MenuItem item in items) {
! 169: if (item.enabled) {
! 170: en = true;
! 171: break;
! 172: }
! 173: }
! 174: if (en == enabled)
! 175: return;
! 176: h.Element el = h.querySelector("#$itemid");
! 177: if (en)
! 178: el.classes.remove('disabled');
! 179: else
! 180: el.classes.add('disabled');
! 181: enabled = en;
! 182: if (parent is Menu)
! 183: (parent as Menu).checkEnabled();
! 184: }
! 185:
! 186: /*
! 187: @override
! 188: void close() {
! 189: if (parent != null)
! 190: parent.close();
! 191: else
! 192: hide();
! 193: }
! 194: */
! 195:
! 196: void selectFirst() {
! 197: for (MenuItem item in items) {
! 198: if (item.enabled) {
! 199: item.select();
! 200: return;
! 201: }
! 202: }
! 203: }
! 204:
! 205: void selectPrevious(MenuItem current) {
! 206: bool found = false;
! 207: for (MenuItem item in items.reversed) {
! 208: if (item == current) {
! 209: found = true;
! 210: } else if (found && item.enabled) {
! 211: current.deselect();
! 212: item.select();
! 213: break;
! 214: }
! 215: }
! 216: }
! 217:
! 218: void selectNext(MenuItem current) {
! 219: bool found = false;
! 220: for (MenuItem item in items) {
! 221: if (item == current) {
! 222: found = true;
! 223: } else if (found && item.enabled) {
! 224: current.deselect();
! 225: item.select();
! 226: break;
! 227: }
! 228: }
! 229: }
! 230:
! 231: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>