1 jls.loader.provide('jls.win32.MenuItem'); 2 3 jls.loader.require('jls.gui.Element'); 4 jls.loader.require('jls.win32.WindowElement'); 5 jls.loader.require('jls.win32.Menu'); 6 7 /* 8 * TODO Separate Menu and MenuItem 9 */ 10 jls.win32.MenuItem = jls.lang.Class.create( 11 { 12 initialize : function(parameters, parent) { 13 parameters = parameters || {}; 14 this._parent = null; 15 this._label = (('label' in parameters) && (typeof parameters.label == 'string')) ? parameters.label : null; 16 this._event = (('event' in parameters) && (typeof parameters.event == 'string')) ? parameters.event : null; 17 this._menu = null; 18 this._id = 0; 19 this._items = null; 20 this._popup = false; 21 this._eventsHandlers = null; 22 this._checked = ('checked' in parameters) && (typeof parameters.checked == 'boolean') && parameters.checked; 23 if (('popup' in parameters) && (typeof parameters.popup == 'boolean')) { 24 this._menu = new jls.win32.Menu(parameters.popup); 25 this._items = []; 26 this._popup = parameters.popup; 27 this._eventsHandlers = {}; 28 } 29 if (parent) { 30 parent.addMenuItem(this); 31 } 32 }, 33 onStartObserving: jls.lang.Class.emptyFunction, 34 onStopObserving: jls.lang.Class.emptyFunction, 35 dispatch : jls.gui.Element.prototype.dispatch, 36 observe : jls.gui.Element.prototype.observe, 37 isMenu : function() { 38 return (this._menu != null) && (this._items != null); 39 }, 40 isPopupMenu : function() { 41 return this._popup && this.isMenu(); 42 }, 43 addMenuItem : function(menu) { 44 if ((! this.isMenu()) || ((! menu.isPopupMenu()) && (menu.isMenu()))) { 45 throw new jls.lang.Exception('Cannot add menu item to this menu'); 46 } 47 var flags = 0; 48 var id = 0; 49 var label = menu._label; 50 if (label != null) { 51 flags |= jls.win32.Menu.MF_STRING; 52 } else { 53 label = ''; 54 flags |= jls.win32.Menu.MF_SEPARATOR; 55 } 56 if (menu._checked) { 57 flags |= jls.win32.Menu.MF_CHECKED; 58 } 59 if (menu.isPopupMenu()) { 60 flags |= jls.win32.Menu.MF_POPUP; 61 id = menu._menu; 62 } else { 63 id = menu._id = jls.win32.MenuItem._nextUid(); 64 } 65 menu._parent = this; 66 this._items.push(menu); 67 this._menu.append(flags, id, label); 68 }, 69 getId : function() { 70 return this._id; 71 }, 72 getLabel : function() { 73 return this._label; 74 }, 75 getItemCount : function(index) { 76 return this._items.length; 77 }, 78 getItem : function(index) { 79 return this._items[index]; 80 }, 81 getItemById : function(id) { 82 if (! this.isMenu()) { 83 throw new jls.lang.Exception('Cannot get menu item from this menu'); 84 } 85 for (var i = 0; i < this._items.length; i++) { 86 var item = this._items[i]; 87 if (item.getId() == id) { 88 return item; 89 } 90 if (item.isMenu()) { 91 item = item.getItemById(id); 92 if (item != null) { 93 return item; 94 } 95 } 96 } 97 return null; 98 }, 99 removeMenuItem : function(item) { 100 var index = 0; 101 if (typeof item == 'number') { 102 index = item; 103 if ((index < 0) || (index >= this._items.length)) { 104 throw new jls.lang.Exception('Index out of bound'); 105 } 106 } else { 107 for (index = this._items.length - 1; (index >= 0) && (this._items[index] !== item); index--); 108 if (index < 0) { 109 throw new jls.lang.Exception('Menu Item not found'); 110 } 111 } 112 var removed = this._items[index]; 113 this._items.splice(index, 1); 114 this._menu.remove(index, jls.win32.Menu.MF_BYPOSITION); 115 return removed; 116 }, 117 getRootMenu : function() { 118 var root = this; 119 while (root._parent != null) { 120 root = root._parent; 121 } 122 return root; 123 }/*, 124 trackPopup : function(element, x, y) { 125 if (! (element instanceof jls.win32.WindowElement)) { 126 throw new jls.lang.Exception('Invalid element argument'); 127 } 128 return this._menu.trackPopup(element._window, x, y); 129 }*/ 130 }); 131 132 Object.extend(jls.win32.MenuItem, 133 { 134 _uid : 1, 135 _nextUid : function() { 136 return jls.win32.MenuItem._uid++; 137 }, 138 createMenu : function() { 139 return new jls.win32.MenuItem({popup: false}); 140 }, 141 createPopupMenu : function() { 142 return new jls.win32.MenuItem({popup: true}); 143 }, 144 createMenuSeparator : function(menu) { 145 return new jls.win32.MenuItem({}, menu); 146 } 147 }); 148