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

Source Code for Module library.pyjamas.ui.DialogBox

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009, 2010 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  3  # Copyright (C) 2012 Alok Parlikar <aup@cs.cmu.edu> 
  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.ui.PopupPanel import PopupPanel 
 20  from pyjamas.ui.HTML import HTML 
 21  from pyjamas.ui.FlexTable import FlexTable 
 22  from pyjamas.ui.SimplePanel import SimplePanel 
 23  from pyjamas.ui import HasHorizontalAlignment 
 24  from pyjamas.ui import HasVerticalAlignment 
 25  from pyjamas.ui import GlassWidget 
26 27 28 -class DialogBox(PopupPanel):
29 _props = [ 30 ("caption", "Caption", "HTML", None), 31 ] 32
33 - def __init__(self, autoHide=None, modal=True, centered=False, 34 **kwargs):
35 # Init section 36 self.dragging = False 37 self.dragStartX = 0 38 self.dragStartY = 0 39 self.child = None 40 self.panel = FlexTable( 41 Height="100%", 42 BorderWidth="0", 43 CellPadding="0", 44 CellSpacing="0", 45 ) 46 47 cf = self.panel.getCellFormatter() 48 rf = self.panel.getRowFormatter() 49 50 # Arguments section 51 self.modal = modal 52 self.caption = HTML() 53 self.caption.setStyleName("Caption") 54 self.caption.addMouseListener(self) 55 56 # Make the DialogBox a 3x3 table, like GWT does, with 57 # empty elements with specific style names. These can be 58 # used with CSS to, for example, create border around the 59 # dialog box. 60 self.generate_gwt15 = kwargs.pop('gwt15', False) and True 61 62 if not self.generate_gwt15: 63 cf.setHeight(1, 0, "100%") 64 cf.setWidth(1, 0, "100%") 65 cf.setAlignment( 66 1, 0, 67 HasHorizontalAlignment.ALIGN_CENTER, 68 HasVerticalAlignment.ALIGN_MIDDLE, 69 ) 70 self.panel.setWidget(0, 0, self.caption) 71 else: 72 row_labels = ['Top', 'Middle', 'Bottom'] 73 col_labels = ['Left', 'Center', 'Right'] 74 75 for r in range(3): 76 rf.setStyleName(r, 'dialog%s' % row_labels[r]) 77 for c in range(3): 78 cf.setStyleName(r, c, 'dialog%s%s' % (row_labels[r], 79 col_labels[c])) 80 sp = SimplePanel() 81 sp.setStyleName('dialog%s%sInner' % (row_labels[r], 82 col_labels[c])) 83 self.panel.setWidget(r, c, sp) 84 85 cf.setAlignment( 86 1, 1, 87 HasHorizontalAlignment.ALIGN_CENTER, 88 HasVerticalAlignment.ALIGN_MIDDLE, 89 ) 90 91 self.dialog_content = SimplePanel() 92 self.dialog_content.setStyleName('dialogContent') 93 94 self.panel.getWidget(0, 1).add(self.caption) 95 self.panel.getWidget(1, 1).add(self.dialog_content) 96 97 # Finalize 98 kwargs['StyleName'] = kwargs.get('StyleName', "gwt-DialogBox") 99 PopupPanel.__init__(self, autoHide, modal, **kwargs) 100 PopupPanel.setWidget(self, self.panel) 101 102 self.centered = centered
103
104 - def onWindowResized(self, width, height):
105 super(DialogBox, self).onWindowResized(width, height) 106 if self.centered: 107 self.centerBox()
108
109 - def show(self):
110 super(DialogBox, self).show() 111 if self.centered: 112 self.centerBox()
113 114 @classmethod
115 - def _getProps(self):
116 return PopupPanel._getProps() + self._props
117
118 - def onEventPreview(self, event):
119 # preventDefault on mousedown events, outside of the 120 # dialog, to stop text-selection on dragging 121 type = DOM.eventGetType(event) 122 if type == 'mousedown': 123 target = DOM.eventGetTarget(event) 124 elem = self.caption.getElement() 125 event_targets_popup = target and DOM.isOrHasChild(elem, target) 126 if event_targets_popup: 127 DOM.eventPreventDefault(event) 128 return PopupPanel.onEventPreview(self, event)
129
130 - def getHTML(self):
131 return self.caption.getHTML()
132
133 - def getText(self):
134 return self.caption.getText()
135
136 - def setHTML(self, html):
137 self.caption.setHTML(html)
138
139 - def setText(self, text):
140 self.caption.setText(text)
141
142 - def onMouseDown(self, sender, x, y):
143 self.dragging = True 144 GlassWidget.show(self.caption) 145 self.dragStartX = x 146 self.dragStartY = y
147
148 - def onMouseEnter(self, sender):
149 pass
150
151 - def onMouseLeave(self, sender):
152 pass
153
154 - def onMouseMove(self, sender, x, y):
155 if not self.dragging: 156 return 157 absX = x + self.getAbsoluteLeft() 158 absY = y + self.getAbsoluteTop() 159 self.setPopupPosition(absX - self.dragStartX, 160 absY - self.dragStartY)
161
162 - def onMouseUp(self, sender, x, y):
163 self.endDragging()
164
165 - def onMouseGlassEnter(self, sender):
166 pass
167
168 - def onMouseGlassLeave(self, sender):
169 self.endDragging()
170
171 - def endDragging(self):
172 if not self.dragging: 173 return 174 self.dragging = False 175 GlassWidget.hide()
176
177 - def remove(self, widget):
178 if self.child != widget: 179 return False 180 181 self.panel.remove(widget) 182 self.child = None 183 return True
184
185 - def doAttachChildren(self):
186 PopupPanel.doAttachChildren(self) 187 self.caption.onAttach()
188
189 - def doDetachChildren(self):
190 PopupPanel.doDetachChildren(self) 191 self.caption.onDetach()
192
193 - def setWidget(self, widget):
194 if self.child is not None: 195 if not self.generate_gwt15: 196 self.panel.remove(self.child) 197 else: 198 self.dialog_content.remove(self.child) 199 200 if widget is not None: 201 if not self.generate_gwt15: 202 self.panel.setWidget(1, 0, widget) 203 else: 204 self.dialog_content.setWidget(widget) 205 206 self.child = widget
207 208 Factory.registerClass('pyjamas.ui.DialogBox', 'DialogBox', DialogBox) 209