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

Source Code for Module library.pyjamas.ui.DialogWindow

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009, 2010 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 DOM 
 16  from pyjamas import Factory 
 17   
 18  from pyjamas.ui.UIObject import UIObject 
 19  from DialogBox import DialogBox 
 20  from HTML import HTML 
 21  from FlexTable import FlexTable 
 22  from pyjamas.ui.HorizontalPanel import HorizontalPanel 
 23  from pyjamas.ui.SimplePanel import SimplePanel 
 24  from pyjamas.ui import HasHorizontalAlignment 
 25  from pyjamas.ui import HasVerticalAlignment 
 26  from pyjamas.ui.Image import Image 
 27  from pyjamas.ui.Button import Button 
 28  from pyjamas.ui import GlassWidget 
 29  from pyjamas.ui import HasAlignment 
 30  import pyjamas.Window 
 31   
 32  # Depends on CSS 
 33  # .gwt-DialogBox .Minimize { 
 34  #     width: 18px; 
 35  #     height: 22px; 
 36  #     margin: 0; 
 37  #     padding: 0; 
 38  #     border: 0; 
 39  #     background: transparent url(window_minimize.gif) no-repeat center top; 
 40  #     text-indent: -1000em; 
 41  # } 
 42  # And the same for .Maximize and .Close 
 43   
 44   
45 -class DialogWindow(DialogBox):
46 _props = [ 47 ("caption", "Caption", "HTML", None), 48 ] 49
50 - def __init__(self, autoHide=None, modal=True, centered=False, 51 caption=None, minimize=None, maximize=None, close=None, 52 captionStyle=None, 53 **kwargs):
54 # Init section 55 DialogBox.__init__(self, autoHide, modal, centered, **kwargs) 56 self._dialogListeners = [] 57 self._minimized = None 58 self._maximized = None 59 60 # Arguments section 61 if isinstance(caption, basestring): 62 self.caption.setText(caption) 63 elif caption is not None: 64 self.caption = caption 65 self.caption.addMouseListener(self) 66 if captionStyle is not None: 67 self.caption.setStyleName(captionStyle) 68 else: 69 self.caption.addStyleName('WindowCaption') 70 self.setControls(minimize, maximize, close)
71
72 - def createDefaultControl(self, control_type):
73 if control_type == 'minimize': 74 return Button("_") 75 elif control_type == 'maximize': 76 return Button("^") 77 elif control_type == 'close': 78 return Button("X") 79 raise ValueError("Invalid control '%s'" % control_type)
80
81 - def setControls(self, minimize, maximize, close):
82 if minimize is True: 83 self.minimizeWidget = self.createDefaultControl('minimize') 84 elif isinstance(minimize, basestring): 85 self.minimizeWidget = Image(minimize) 86 else: 87 self.minimizeWidget = minimize 88 if maximize is True: 89 self.maximizeWidget = self.createDefaultControl('maximize') 90 elif isinstance(maximize, basestring): 91 self.maximizeWidget = Image(maximize) 92 else: 93 self.maximizeWidget = maximize 94 if close is True: 95 self.closeWidget = self.createDefaultControl('close') 96 elif isinstance(close, basestring): 97 self.closeWidget = Image(close) 98 else: 99 self.closeWidget = close 100 if (isinstance(self.minimizeWidget, UIObject) or 101 isinstance(self.maximizeWidget, UIObject) or 102 isinstance(self.closeWidget, UIObject)): 103 cf = self.panel.getCellFormatter() 104 captionStyle = self.caption.getStyleName() 105 captionPanel = FlexTable( 106 Width="99%", 107 BorderWidth="0", 108 CellPadding="0", 109 CellSpacing="0", 110 ) 111 controls = HorizontalPanel() 112 controls.setStyleName("Controls") 113 controls.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE) 114 if isinstance(self.minimizeWidget, UIObject): 115 self.minimizeWidget.setStyleName("Minimize") 116 controls.add(self.minimizeWidget) 117 self.minimizeWidget.addClickListener( 118 getattr(self, "onMinimize"), 119 ) 120 if isinstance(self.maximizeWidget, UIObject): 121 self.maximizeWidget.setStyleName("Maximize") 122 controls.add(self.maximizeWidget) 123 self.maximizeWidget.addClickListener( 124 getattr(self, "onMaximize"), 125 ) 126 if isinstance(self.closeWidget, UIObject): 127 self.closeWidget.setStyleName("Close") 128 controls.add(self.closeWidget) 129 self.closeWidget.addClickListener( 130 getattr(self, "onClose"), 131 ) 132 self.closeable = True 133 self.caption.removeStyleName(captionStyle) 134 self.panel.setWidget(0, 0, captionPanel) 135 captionPanel.setWidget(0, 0, self.caption) 136 captionPanel.setWidget(0, 1, controls) 137 captionPanel.setStyleName(captionStyle) 138 cf = captionPanel.getCellFormatter() 139 cf.setWidth(0, 1, '1%')
140
141 - def getDialogListeners(self, listener):
142 return self._dialogListeners
143
144 - def addDialogListener(self, listener):
145 self._dialogListeners.append(listener)
146
147 - def removeDialogListener(self, listener):
148 self._dialogListeners.remove(listener)
149
150 - def _runDialogListener(self, action):
151 cont = True 152 for listener in self._dialogListeners: 153 if listener is not self: 154 onListener = getattr(listener, action, None) 155 if onListener is not None: 156 if onListener() is False: 157 cont = False 158 return cont
159
160 - def onActivate(self):
161 if self._runDialogListener("onActivate") is False: 162 return 163 self.hide() 164 self.show()
165
166 - def _toggleMaximize(self):
167 if self._maximized: 168 top, left, height, width = self._maximized 169 self._maximized = None 170 height = width = "" 171 else: 172 top = self.getPopupTop() 173 left = self.getPopupLeft() 174 height = self.getHeight() 175 width = self.getWidth() 176 self._maximized = (top, left, height, width) 177 top = left = 0 178 height = int(pyjamas.Window.getClientHeight()) - 4 179 width = int(pyjamas.Window.getClientWidth()) - 4 180 self.setPopupPosition(left, top) 181 self.panel.setHeight(height) 182 self.panel.setWidth(width)
183
184 - def onMaximize(self, sender=None):
185 if self._runDialogListener("onMaximize") is False: 186 return 187 self._toggleMaximize() 188 widget = self.panel.getWidget(1, 0) 189 if not widget.isVisible(): 190 widget.setVisible(True) 191 self.hide() 192 self.show()
193
194 - def onMinimize(self, sender=None):
195 if self._runDialogListener("onMinimize") is False: 196 return 197 widget = self.child 198 if widget is not None: 199 if widget.isVisible(): 200 widget.setVisible(False) 201 self.setHeight("") 202 self.setWidth("") 203 if self._maximized: 204 self._minimized = self._maximized 205 self._toggleMaximize() 206 else: 207 self._minimized = None 208 else: 209 if self._minimized is not None: 210 self._toggleMaximize() 211 widget.setVisible(True)
212
213 - def onClose(self, sender=None):
214 if self._runDialogListener("onClose") is False: 215 return 216 self.hide()
217
218 - def onMouseUp(self, sender, x, y):
219 if self.dragStartX != x or self.dragStartY != y: 220 self.onActivate() 221 DialogBox.endDragging(self)
222 223 Factory.registerClass('pyjamas.ui.DialogWindow', 'DialogWindow', DialogWindow) 224