Home | Trees | Indices | Help |
|
---|
|
1 # Copyright 2006 James Tauber and contributors 2 # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 3 # Copyright (C) 2009 Pavel Mironchyk <p.mironchyk@gmail.com> 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 from pyjamas import DOM 17 from pyjamas import Factory 18 19 from pyjamas import DeferredCommand 20 from pyjamas.ui.Widget import Widget 21 from pyjamas.ui.MenuItem import MenuItem 22 from pyjamas.ui.MenuBarPopupPanel import MenuBarPopupPanel 23 from pyjamas.ui import Event 24 from pyjamas.ui.MultiListener import MultiListener28 29 _props = [("vertical", "Vertical", "Vertical", None), 30 ] 31308 309 Factory.registerClass('pyjamas.ui.MenuBar', 'MenuBar', MenuBar) 31033 self.setVertical(kwargs.pop('Vertical', vertical)) 34 if 'StyleName' not in kwargs or kwargs['StyleName'] == 'gwt-MenuBar': 35 kwargs['StyleName'] = self.getDefaultStyleName() 36 self.body = None 37 self.items = [] 38 self.parentMenu = None 39 self.popup = None 40 self.selectedItem = None 41 self.shownChildMenu = None 42 self.autoOpen = False 43 44 if kwargs.has_key('Element'): 45 table = kwargs.pop('Element') 46 fc = DOM.getFirstChild(table) 47 if fc: 48 self.body = fc 49 else: 50 self.body = DOM.createTBody() 51 DOM.appendChild(table, self.body) 52 else: 53 table = DOM.createTable() 54 self.body = DOM.createTBody() 55 DOM.appendChild(table, self.body) 56 57 outer = DOM.createDiv() 58 DOM.appendChild(outer, table) 59 self.setElement(outer) 60 Widget.__init__(self, **kwargs)61 62 @classmethod 6567 """ covers creating the sub-menus and linking the event handlers. 68 """ 69 self.clearItems() # really tricky to update, so just blow away. 70 71 items = {} 72 for (k, v) in props.items(): 73 if not isinstance(k, int): 74 continue 75 items[int(k)] = v 76 items = items.items() 77 items.sort() 78 79 last_level = 0 80 menu = self 81 menus = [menu] 82 for prop in items: 83 print prop 84 level, name, label, handler = prop[1] 85 if level < last_level: 86 menus = menus[:level+1] 87 menu = menus[level] 88 elif level > last_level: 89 menu = MenuBar(vertical=True) 90 lastitem = menus[-1].items[-1] 91 lastitem.setSubMenu(menu) 92 setattr(lastitem, name, menu) 93 menus.append(menu) 94 item = menu.addItem(label) 95 if handler and builderstate is not None and \ 96 builderstate.eventTarget is not None: 97 # add a menu listener callback 98 menuItemListener = MultiListener(builderstate.eventTarget, 99 execute=handler) 100 item.setCommand(menuItemListener) 101 102 last_level = level103 106 109111 """ use this to delay effect of self.vertical being set. 112 self.setVertical can now be used, rather than self.vertical 113 force-set in constructor 114 """ 115 if DOM.getChildCount(self.body) == 0: 116 DOM.appendChild(self.body, DOM.createTR())117119 if self.vertical: 120 return "gwt-MenuBar " + "gwt-MenuBar-vertical" 121 return "gwt-MenuBar-horizontal"122124 if not StyleName or StyleName == 'gwt-MenuBar': 125 StyleName = self.getDefaultStyleName() 126 super(MenuBar, self).setStyleName(StyleName, **kwargs)127 128 # also callable as: 129 # addItem(item) 130 # addItem(text, cmd) 131 # addItem(text, popup) 132 # addItem(text, asHTML, cmd)134 if not hasattr(item, "setSubMenu"): 135 item = MenuItem(item, asHTML, popup) 136 137 if self.vertical: 138 tr = DOM.createTR() 139 DOM.appendChild(self.body, tr) 140 else: 141 self._checkVerticalContainer() 142 tr = DOM.getChild(self.body, 0) 143 144 DOM.appendChild(tr, item.getElement()) 145 146 item.setParentMenu(self) 147 item.setSelectionStyle(False) 148 self.items.append(item) 149 return item150152 container = self.getItemContainerElement() 153 while DOM.getChildCount(container) > 0: 154 DOM.removeChild(container, DOM.getChild(container, 0)) 155 self.items = []156 159161 Widget.onBrowserEvent(self, event) 162 163 item = self.findItem(DOM.eventGetTarget(event)) 164 if item is None: 165 return False 166 167 type = DOM.eventGetType(event) 168 if type == "click": 169 self.doItemAction(item, True) 170 return True 171 elif type == "mouseover": 172 self.itemOver(item) 173 elif type == "mouseout": 174 self.itemOver(None) 175 176 return False177179 if autoClosed: 180 self.closeAllParents() 181 182 self.onHide() 183 self.shownChildMenu = None 184 self.popup = None185187 try: 188 idx = self.items.index(item) 189 except ValueError: 190 return 191 container = self.getItemContainerElement() 192 DOM.removeChild(container, DOM.getChild(container, idx)) 193 del self.items[idx]194 197199 curMenu = self 200 while curMenu is not None: 201 curMenu.close() 202 203 if curMenu.parentMenu is None and \ 204 curMenu.selectedItem is not None: 205 curMenu.selectedItem.setSelectionStyle(False) 206 curMenu.selectedItem = None 207 208 curMenu = curMenu.parentMenu209211 if (self.shownChildMenu is not None) and \ 212 (item.getSubMenu() == self.shownChildMenu): 213 return 214 215 if (self.shownChildMenu is not None): 216 self.shownChildMenu.onHide() 217 self.popup.hide() 218 219 if item.getSubMenu() is None: 220 if fireCommand: 221 self.closeAllParents() 222 223 cmd = item.getCommand() 224 if cmd is not None: 225 DeferredCommand.add(cmd) 226 return 227 228 self.selectItem(item) 229 self.popup = MenuBarPopupPanel(item) 230 self.popup.addPopupListener(self) 231 232 if self.vertical: 233 self.popup.setPopupPosition(self.getAbsoluteLeft() + 234 self.getOffsetWidth() - 1, 235 item.getAbsoluteTop()) 236 else: 237 self.popup.setPopupPosition(item.getAbsoluteLeft(), 238 self.getAbsoluteTop() + 239 self.getOffsetHeight() - 1) 240 241 self.shownChildMenu = item.getSubMenu() 242 sub_menu = item.getSubMenu() 243 sub_menu.parentMenu = self 244 245 self.popup.show()246 252254 if item is None: 255 if (self.selectedItem is not None): 256 if self.selectedItem.getSubMenu() != None: 257 if (self.shownChildMenu == self.selectedItem.getSubMenu()): 258 return 259 else: 260 self.selectItem(item) 261 return 262 263 self.selectItem(item) 264 265 if item is not None: 266 if (self.shownChildMenu is not None) or \ 267 (self.parentMenu is not None) or self.autoOpen: 268 self.doItemAction(item, False)269 273275 for item in self.items: 276 if DOM.isOrHasChild(item.getElement(), hItem): 277 return item 278 279 return None280282 if self.vertical: 283 return self.body 284 else: 285 self._checkVerticalContainer() 286 return DOM.getChild(self.body, 0)287 292 296298 if item == self.selectedItem: 299 return 300 301 if self.selectedItem is not None: 302 self.selectedItem.setSelectionStyle(False) 303 304 if item is not None: 305 item.setSelectionStyle(True) 306 307 self.selectedItem = item
Home | Trees | Indices | Help |
|
---|
http://epydoc.sourceforge.net |