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 ] 31320 321 Factory.registerClass('pyjamas.ui.MenuBar', 'MenuBar', MenuBar) 32233 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)127129 if self.vertical: 130 tr = DOM.createTR() 131 DOM.insertChild(self.body, tr, index) 132 else: 133 self._checkVerticalContainer() 134 tr = DOM.getChild(self.body, 0) 135 DOM.insertChild(tr, item.getElement(), index) 136 item.setParentMenu(self) 137 item.setSelectionStyle(False) 138 self.items.insert(index, item)139 140 # also callable as: 141 # addItem(item) 142 # addItem(text, cmd) 143 # addItem(text, popup) 144 # addItem(text, asHTML, cmd)146 if not hasattr(item, "setSubMenu"): 147 item = MenuItem(item, asHTML, popup) 148 149 if self.vertical: 150 tr = DOM.createTR() 151 DOM.appendChild(self.body, tr) 152 else: 153 self._checkVerticalContainer() 154 tr = DOM.getChild(self.body, 0) 155 156 DOM.appendChild(tr, item.getElement()) 157 158 item.setParentMenu(self) 159 item.setSelectionStyle(False) 160 self.items.append(item) 161 return item162164 container = self.getItemContainerElement() 165 while DOM.getChildCount(container) > 0: 166 DOM.removeChild(container, DOM.getChild(container, 0)) 167 self.items = []168 171173 Widget.onBrowserEvent(self, event) 174 175 item = self.findItem(DOM.eventGetTarget(event)) 176 if item is None: 177 return False 178 179 type = DOM.eventGetType(event) 180 if type == "click": 181 self.doItemAction(item, True) 182 return True 183 elif type == "mouseover": 184 self.itemOver(item) 185 elif type == "mouseout": 186 self.itemOver(None) 187 188 return False189191 if autoClosed: 192 self.closeAllParents() 193 194 self.onHide() 195 self.shownChildMenu = None 196 self.popup = None197199 try: 200 idx = self.items.index(item) 201 except ValueError: 202 return 203 container = self.getItemContainerElement() 204 DOM.removeChild(container, DOM.getChild(container, idx)) 205 del self.items[idx]206 209211 curMenu = self 212 while curMenu is not None: 213 curMenu.close() 214 215 if curMenu.parentMenu is None and \ 216 curMenu.selectedItem is not None: 217 curMenu.selectedItem.setSelectionStyle(False) 218 curMenu.selectedItem = None 219 220 curMenu = curMenu.parentMenu221223 if (self.shownChildMenu is not None) and \ 224 (item.getSubMenu() == self.shownChildMenu): 225 return 226 227 if (self.shownChildMenu is not None): 228 self.shownChildMenu.onHide() 229 self.popup.hide() 230 231 if item.getSubMenu() is None: 232 if fireCommand: 233 self.closeAllParents() 234 235 cmd = item.getCommand() 236 if cmd is not None: 237 DeferredCommand.add(cmd) 238 return 239 240 self.selectItem(item) 241 self.popup = MenuBarPopupPanel(item) 242 self.popup.addPopupListener(self) 243 244 if self.vertical: 245 self.popup.setPopupPosition(self.getAbsoluteLeft() + 246 self.getOffsetWidth() - 1, 247 item.getAbsoluteTop()) 248 else: 249 self.popup.setPopupPosition(item.getAbsoluteLeft(), 250 self.getAbsoluteTop() + 251 self.getOffsetHeight() - 1) 252 253 self.shownChildMenu = item.getSubMenu() 254 sub_menu = item.getSubMenu() 255 sub_menu.parentMenu = self 256 257 self.popup.show()258 264266 if item is None: 267 if (self.selectedItem is not None): 268 if self.selectedItem.getSubMenu() != None: 269 if (self.shownChildMenu == self.selectedItem.getSubMenu()): 270 return 271 else: 272 self.selectItem(item) 273 return 274 275 self.selectItem(item) 276 277 if item is not None: 278 if (self.shownChildMenu is not None) or \ 279 (self.parentMenu is not None) or self.autoOpen: 280 self.doItemAction(item, False)281 285287 for item in self.items: 288 if DOM.isOrHasChild(item.getElement(), hItem): 289 return item 290 291 return None292294 if self.vertical: 295 return self.body 296 else: 297 self._checkVerticalContainer() 298 return DOM.getChild(self.body, 0)299 304 308310 if item == self.selectedItem: 311 return 312 313 if self.selectedItem is not None: 314 self.selectedItem.setSelectionStyle(False) 315 316 if item is not None: 317 item.setSelectionStyle(True) 318 319 self.selectedItem = item
Home | Trees | Indices | Help |
|
---|
http://epydoc.sourceforge.net |