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

Source Code for Module 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 hasattr(listener, action): 154 if getattr(listener, action)() is False: 155 cont = False 156 return cont
157
158 - def onActivate(self):
159 if self._runDialogListener("onActivate") is False: 160 return 161 self.hide() 162 self.show()
163
164 - def _toggleMaximize(self):
165 if self._maximized: 166 top, left, height, width = self._maximized 167 self._maximized = None 168 height = width = "" 169 else: 170 top = self.getPopupTop() 171 left = self.getPopupLeft() 172 height = self.getHeight() 173 width = self.getWidth() 174 self._maximized = (top, left, height, width) 175 top = left = 0 176 height = int(pyjamas.Window.getClientHeight()) - 4 177 width = int(pyjamas.Window.getClientWidth()) - 4 178 self.setPopupPosition(left, top) 179 self.panel.setHeight(height) 180 self.panel.setWidth(width)
181
182 - def onMaximize(self, sender):
183 if self._runDialogListener("onMaximize") is False: 184 return 185 self._toggleMaximize() 186 widget = self.panel.getWidget(1, 0) 187 if not widget.isVisible(): 188 widget.setVisible(True) 189 self.hide() 190 self.show()
191
192 - def onMinimize(self, sender):
193 if self._runDialogListener("onMinimize") is False: 194 return 195 widget = self.child 196 if widget is not None: 197 if widget.isVisible(): 198 widget.setVisible(False) 199 self.setHeight("") 200 self.setWidth("") 201 if self._maximized: 202 self._minimized = self._maximized 203 self._toggleMaximize() 204 else: 205 self._minimized = None 206 else: 207 if self._minimized is not None: 208 self._toggleMaximize() 209 widget.setVisible(True)
210
211 - def onClose(self, sender):
212 if self._runDialogListener("onClose") is False: 213 return 214 self.hide()
215
216 - def onMouseUp(self, sender, x, y):
217 if self.dragStartX != x or self.dragStartY != y: 218 self.onActivate() 219 DialogBox.endDragging(self)
220 221 Factory.registerClass('pyjamas.ui.DialogWindow', 'DialogWindow', DialogWindow) 222