1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought pyface package component>
#------------------------------------------------------------------------------
""" The abstract base class for all Pyface windows. """
# Standard library imports.
import logging
# Major package imports.
import wx
# Enthought library imports.
from enthought.traits.api import Any, Bool, Event, Instance, Int, Property, Str
from enthought.traits.api import Tuple
# Local imports.
from constant import OK
from key_pressed_event import KeyPressedEvent
from widget import Widget
# Create a logger for this module.
logger = logging.getLogger(__name__)
class Window(Widget):
""" The abstract base class for all Pyface windows.
A Pyface window has no visual representation until it is opened (ie. its
'control' trait will be None until it is opened).
Usage: Create a sub-class of this class and override the protected
'_create_control' (if necessary), '_create_contents', and '_size_control'
(if necessary) methods.
"""
#### 'Window' interface ###################################################
# The sizer which handles the main layout of the content (None if the
# layout is handled exclusively by the frame)
main_sizer = Instance(wx.BoxSizer)
# The window's parent (None if the window is a top-level window).
parent = Any
# The position of the window.
position = Property(Tuple)
# The return code after the window is closed (this is useful in dialogs
# etc, to indicate whether the dialog was closed via 'Ok' or 'Cancel').
return_code = Int(OK)
# The size of the window.
size = Property(Tuple)
# The sizer which contains the content area.
sizer = Instance(wx.BoxSizer)
# Thw window title.
title = Str
#### Events #####
# The window has been activated.
activated = Event
# The window has been deactivated.
deactivated = Event
# The window is about to be closed.
closing = Event
# The window has been closed.
closed = Event
# The window is about to open.
opening = Event
# The window has been opened.
opened = Event
# A key was pressed while the window had focus.
key_pressed = Event(KeyPressedEvent)
#### Private interface ####################################################
# Shadow trait for size.
_size = Tuple((-1, -1))
# Shadow trait for position.
_position = Tuple((-1, -1))
###########################################################################
# 'Window' interface.
###########################################################################
#### Properties ###########################################################
def _get_position(self):
""" Property getter for position. """
return self._position
def _set_position(self, position):
""" Property setter for position. """
# Save for event notification.
old = self._position
# Set the shadow trait.
self._position = position
# Notify interested parties.
self.trait_property_changed('position', old, position)
return
def _get_size(self):
""" Property getter for size. """
return self._size
def _set_size(self, size):
""" Property setter for size. """
# Save for event notification.
old = self._size
# Set the shadow trait.
self._size = size
# Notify interested parties.
self.trait_property_changed('size', old, size)
return
#### Methods ##############################################################
def open(self):
""" Opens the window. """
# Trait notification.
self.opening = self
if self.control is None:
self._create()
self.control.Show(True)
# Trait notification.
self.opened = self
return
def close(self):
""" Closes the window. """
if self.control is not None:
# Trait notification.
self.closing = self
# Cleanup the toolkit-specific control.
self.control.Destroy()
# Cleanup our reference to the control so that we can (at least in
# theory!) be opened again.
self.control = None
# Trait notification.
self.closed = self
else:
logger.debug('window is not open %s' % str(self))
return
def refresh(self):
""" Workaround for VTK render window sizing bug. """
self.control.SendSizeEvent()
return
###########################################################################
# Protected 'Window' interface.
###########################################################################
def _create(self):
""" Creates the window's widget hierarchy. """
# Create the toolkit-specific control that represents the window.
self.control = self._create_control(self.parent)
# Create the contents of the window.
content = self._create_contents(self.control)
if self.sizer is not None:
self.sizer.Add(content, 1, wx.ALL | wx.EXPAND)
if self.main_sizer is not None:
self.main_sizer.Fit(self.control)
self._size_control(self.control, content)
# Wire up event any event listeners required by the window.
self._add_event_listeners(self.control)
return
def _add_event_listeners(self, control):
""" Adds any event listeners required by the window. """
# Frame events.
wx.EVT_ACTIVATE(self.control, self._on_activate)
wx.EVT_CLOSE(self.control, self._on_close)
wx.EVT_SIZE(self.control, self._on_control_size)
wx.EVT_MOVE(self.control, self._on_control_move)
wx.EVT_CHAR(self.control, self._on_char)
return
def _create_control(self, parent):
""" Create the toolkit-specific control that represents the window.
This method is intended to be overridden if necessary. By default we
just create an empty frame.
"""
control = wx.Frame(
parent, -1, self.title, size=self.size, pos=self.position
)
return control
def _create_contents(self, parent):
""" Creates the window contents.
This method is intended to be overridden if necessary. By default we
just create an empty (and blue!) panel.
"""
panel = wx.Panel(parent, -1)
panel.SetSize((500, 400))
panel.SetBackgroundColour('blue')
return panel
def _size_control(self, control, content):
""" Set the size of the toolkit specific control.
This method is intended to be overridden if necessary. By default we
set the size to the value of the 'size' trait. Subclasses may choose
another strategy for their initial size, if for instance, they wish
to set the size based on the size of the contents.
"""
if self.size != (-1, -1):
self.control.SetSize(self.size)
return
###########################################################################
# Private interface.
###########################################################################
#### Trait event handlers #################################################
#### Static ####
def _position_changed(self, position):
""" Static trait change handler. """
if self.control is not None:
self.control.SetPosition(position)
return
def _size_changed(self, size):
""" Static trait change handler. """
if self.control is not None:
self.control.SetSize(size)
return
def _title_changed(self, title):
""" Static trait change handler. """
if self.control is not None:
self.control.SetTitle(title)
return
#### wx event handlers ####################################################
def _on_activate(self, event):
""" Called when the frame is being activated or deactivated. """
# Trait notification.
if event.GetActive():
self.activated = self
else:
self.deactivated = self
return
def _on_close(self, event):
""" Called when the frame is being closed. """
logger.debug('window [%s] closed by user', self)
self.close()
return
def _on_control_move(self, event):
""" Called when the window is resized. """
# Get the real position and set the trait without performing
# notification.
# WXBUG - From the API documentation you would think that you could
# call event.GetPosition directly, but that would be wrong. The pixel
# reported by that call is the pixel just below the window menu and
# just right of the Windows-drawn border.
self._position = event.GetEventObject().GetPositionTuple()
event.Skip()
return
def _on_control_size(self, event):
""" Called when the window is resized. """
# Get the new size and set the shadow trait without performing
# notification.
wxsize = event.GetSize()
self._size = (wxsize.GetWidth(), wxsize.GetHeight())
event.Skip()
return
def _on_char(self, event):
""" Called when a key is pressed when the tree has focus. """
self.key_pressed = KeyPressedEvent(
alt_down = event.AltDown() == 1,
control_down = event.ControlDown() == 1,
shift_down = event.ShiftDown() == 1,
key_code = event.GetKeyCode(),
event = event
)
event.Skip()
return
#### EOF ######################################################################
|