Package pyjamas :: Package ui :: Module DockPanel
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.DockPanel

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  #     http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  from __pyjamas__ import console 
 16  from pyjamas import Factory 
 17  from pyjamas import DOM 
 18   
 19  from CellPanel import CellPanel 
 20  from pyjamas.ui import HasHorizontalAlignment 
 21  from pyjamas.ui import HasVerticalAlignment 
 22   
23 -class DockPanelTmpRow:
24 center = 0 25 tr = None
26
27 -class LayoutData:
28 - def __init__(self, direction):
29 self.direction = direction 30 self.hAlign = "left" 31 self.height = "" 32 self.td = None 33 self.vAlign = "top" 34 self.width = ""
35 36
37 -class DockPanel(CellPanel):
38 39 CENTER = "center" 40 EAST = "east" 41 NORTH = "north" 42 SOUTH = "south" 43 WEST = "west" 44
45 - def __init__(self, **kwargs):
46 47 if not kwargs.has_key('Spacing'): kwargs['Spacing'] = 0 48 if not kwargs.has_key('Padding'): kwargs['Padding'] = 0 49 50 self.horzAlign = HasHorizontalAlignment.ALIGN_LEFT 51 self.vertAlign = HasVerticalAlignment.ALIGN_TOP 52 self.center = None 53 self.dock_children = [] # TODO: can self.children be used instead? 54 55 CellPanel.__init__(self, **kwargs)
56
57 - def add(self, widget, direction):
58 if direction == self.CENTER: 59 if self.center is not None: 60 console.error("Only one CENTER widget may be added") 61 self.center = widget 62 63 layout = LayoutData(direction) 64 widget.setLayoutData(layout) 65 self.setCellHorizontalAlignment(widget, self.horzAlign) 66 self.setCellVerticalAlignment(widget, self.vertAlign) 67 68 self.dock_children.append(widget) 69 self.realizeTable(widget)
70
71 - def getHorizontalAlignment(self):
72 return self.horzAlign
73
74 - def getVerticalAlignment(self):
75 return self.vertAlign
76
77 - def getWidgetDirection(self, widget):
78 if widget.getParent() != self: 79 return None 80 return widget.getLayoutData().direction
81
82 - def remove(self, widget):
83 if widget == self.center: 84 self.center = None 85 86 ret = CellPanel.remove(self, widget) 87 if ret: 88 self.dock_children.remove(widget) 89 self.realizeTable(None) 90 return ret
91
92 - def setCellHeight(self, widget, height):
93 data = widget.getLayoutData() 94 data.height = height 95 if data.td: 96 DOM.setStyleAttribute(data.td, "height", data.height)
97
98 - def setCellHorizontalAlignment(self, widget, align):
99 data = widget.getLayoutData() 100 data.hAlign = align 101 if data.td: 102 DOM.setAttribute(data.td, "align", data.hAlign)
103
104 - def setCellVerticalAlignment(self, widget, align):
105 data = widget.getLayoutData() 106 data.vAlign = align 107 if data.td: 108 DOM.setStyleAttribute(data.td, "verticalAlign", data.vAlign)
109
110 - def setCellWidth(self, widget, width):
111 data = widget.getLayoutData() 112 data.width = width 113 if data.td: 114 DOM.setStyleAttribute(data.td, "width", data.width)
115
116 - def setHorizontalAlignment(self, align):
117 self.horzAlign = align
118
119 - def setVerticalAlignment(self, align):
120 self.vertAlign = align
121
122 - def realizeTable(self, beingAdded):
123 bodyElement = self.getBody() 124 125 while DOM.getChildCount(bodyElement) > 0: 126 DOM.removeChild(bodyElement, DOM.getChild(bodyElement, 0)) 127 128 rowCount = 1 129 colCount = 1 130 for child in self.dock_children: 131 dir = child.getLayoutData().direction 132 if dir == self.NORTH or dir == self.SOUTH: 133 rowCount += 1 134 elif dir == self.EAST or dir == self.WEST: 135 colCount += 1 136 137 rows = [] 138 for i in range(rowCount): 139 rows.append(DockPanelTmpRow()) 140 rows[i].tr = DOM.createTR() 141 DOM.appendChild(bodyElement, rows[i].tr) 142 143 westCol = 0 144 eastCol = colCount - 1 145 northRow = 0 146 southRow = rowCount - 1 147 centerTd = None 148 149 for child in self.dock_children: 150 layout = child.getLayoutData() 151 152 td = DOM.createTD() 153 layout.td = td 154 DOM.setAttribute(layout.td, "align", layout.hAlign) 155 DOM.setStyleAttribute(layout.td, "verticalAlign", layout.vAlign) 156 DOM.setAttribute(layout.td, "width", layout.width) 157 DOM.setAttribute(layout.td, "height", layout.height) 158 159 if layout.direction == self.NORTH: 160 DOM.insertChild(rows[northRow].tr, td, rows[northRow].center) 161 self.appendAndMaybeAdopt(td, child.getElement(), beingAdded) 162 DOM.setIntAttribute(td, "colSpan", eastCol - westCol + 1) 163 northRow += 1 164 elif layout.direction == self.SOUTH: 165 DOM.insertChild(rows[southRow].tr, td, rows[southRow].center) 166 self.appendAndMaybeAdopt(td, child.getElement(), beingAdded) 167 DOM.setIntAttribute(td, "colSpan", eastCol - westCol + 1) 168 southRow -= 1 169 elif layout.direction == self.WEST: 170 row = rows[northRow] 171 DOM.insertChild(row.tr, td, row.center) 172 row.center += 1 173 self.appendAndMaybeAdopt(td, child.getElement(), beingAdded) 174 DOM.setIntAttribute(td, "rowSpan", southRow - northRow + 1) 175 westCol += 1 176 elif layout.direction == self.EAST: 177 row = rows[northRow] 178 DOM.insertChild(row.tr, td, row.center) 179 self.appendAndMaybeAdopt(td, child.getElement(), beingAdded) 180 DOM.setIntAttribute(td, "rowSpan", southRow - northRow + 1) 181 eastCol -= 1 182 elif layout.direction == self.CENTER: 183 centerTd = td 184 185 if self.center is not None: 186 row = rows[northRow] 187 DOM.insertChild(row.tr, centerTd, row.center) 188 self.appendAndMaybeAdopt(centerTd, self.center.getElement(), beingAdded)
189
190 - def appendAndMaybeAdopt(self, parent, child, beingAdded):
191 if beingAdded is not None: 192 if DOM.compare(child, beingAdded.getElement()): 193 CellPanel.add(self, beingAdded, parent) 194 return 195 DOM.appendChild(parent, child)
196 197 Factory.registerClass('pyjamas.ui.DockPanel', DockPanel) 198