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
|
## $Id: ttoolkit.py,v 1.4 2003/09/09 21:32:32 riq Exp $
##
## Tenes Empanadas Graciela
##
## Copyright (C) 2000,2003 Ricardo Quesada
##
## Author: Ricardo Calixto Quesada <riq@coresecurity.com>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; only version 2 of the License
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
## A simple Toolkit for TEG using pygame, the TToolkit :-)
import pygame
# TEG imports
import font
from events import *
class TToolkitException( Exception ):
pass
class TDisplay( object ):
def __init__( self, surface = None ):
TToolkit.surface = surface
self._listTk = []
def addWidget( self, tk ):
if not isinstance( tk, TToolkit ):
raise TToolkitException('TDisplay.add() accepts only TToolkit objects: %s' % tk )
self._listTk.append( tk )
if tk.isDirty():
tk.update()
def remove( self, tk ):
if tk in self._listTk:
self._listTk.remove( tk )
else:
raise TToolkitException('Object is not in TDisplay')
def update( self ):
pygame.display.flip()
def notify( self, event ):
for i in self._listTk:
i.notify( event )
def setEventMgr( self, mgr ):
pass
class TToolkit( object ):
surface = None
# static methods
def setSurface( aSurface ):
TToolkit.surface = aSurface
setSurface = staticmethod( setSurface )
def __init__( self ):
self._size = (0,0)
self._origin = (0,0)
self._dirty = True
self._update = False
self._mouseOver = False
self._mousePressed = False
if TToolkit.surface == None:
raise TToolkitException('No surface given!')
def isDirty( self ):
return self._dirty
def setSize( self, size ):
self._size = size
self._dirty = True
def getSize( self ):
return self._size
def getOrigin( self ):
return self._origin
def setOrigin( self, origin ):
self._origin = origin
self._dirty = True
def update( self ):
""" re-draws the widget """
pass
def notify( self, event ):
""" default manager for MouseMotionEvent """
if isinstance( event, MouseButtonDown ):
if not self._mousePressed:
self._mousePressed = True
self._dirty = True
elif isinstance( event, MouseButtonUp ):
if self._mousePressed:
self._mousePressed = False
self._dirty = True
elif isinstance( event, MouseMotionEvent ):
pos = event.getPos()
if pos[0] >= self._origin[0] and pos[0] <= self._origin[0] + self._size[0] and \
pos[1] >= self._origin[1] and pos[1] <= self._origin[1] + self._size[1]:
if not self._mouseOver:
self._mouseOver = True
self._dirty = True
else:
if self._mouseOver:
self._mouseOver = False
self._dirty = True
if self._dirty:
self.update()
class TLayout( TToolkit ):
def __init__( self ):
TToolkit.__init__( self )
self._spacing = 5
def setSpacing( self, spacing ):
self._spacing = spacing
def getSpacing( self ):
return self._spacing
class TBoxLayout( TLayout ):
LeftToRight= 0
RightToLeft = 1
TopToBottom = 2
BottomToTop = 3
def __init__( self ):
TLayout.__init__( self )
self._widgets = []
self._direction = TBoxLayout.LeftToRight
# public methods
def setOrigin( self, origin ):
TLayout.setOrigin( self, origin )
self._calculateChildsOrigin()
def addWidget( self, widget ):
if not isinstance( widget, TToolkit ):
raise TToolkitException("TBoxLayout only accepts TToolkit objects")
self._widgets.append( widget )
self._updateWidgetOrigin( widget )
def setDirection( self, direction ):
self._direction = direction
def update( self ):
for w in self._widgets:
w.update()
def notify( self, event ):
for w in self._widgets:
w.notify( event )
class THBoxLayout( TBoxLayout ):
""" TToolkit Horizontal Box Layout """
def __init__( self ):
TBoxLayout.__init__( self )
self.setDirection( TBoxLayout.LeftToRight )
# protected methods
def _calculateChildsOrigin( self ):
pass
def _updateWidgetOrigin( self, widget ):
widget.setOrigin( (self._origin[0] + self._size[0], self._origin[1] ) )
y = self._size[1]
if self._size[1] < widget.getSize()[1]:
y = widget.getSize()[1]
self._size = ( self._size[0] + widget.getSize()[0], y )
class TVBoxLayout( TBoxLayout ):
""" TToolkit Vertical Box Layout """
def __init__( self ):
TBoxLayout.__init__( self )
self.setDirection( TBoxLayout.TopToBottom )
def _calculateChildsOrigin( self ):
pass
def _updateWidgetOrigin( self, widget ):
widget.setOrigin( (self._origin[0], self._origin[1] + self._size[1] ) )
x = self._size[0]
if self._size[0] < widget.getSize()[0]:
x = widget.getSize()[0]
self._size = ( x, self._size[1] + widget.getSize()[1] )
class TButton( TToolkit ):
buttonFont = font.Fonts.getInstance().get('helvetica 16')
def __init__( self, label = 'NO LABEL', icon = None ):
TToolkit.__init__( self )
self._label = label
self.__label_size = (0,0)
self._icon = icon
self.__icon_size = (0,0)
self._recalculateSize()
# public
def setLabel( self, label ):
""" Sets the label of a button. """
self._label = label
self._recalculateSize()
self._dirty = True
def getLabel( self, label ):
""" Returns the label of a button. """
return self._label
def setIcon( self, icon ):
self._icon = icon
self._recalculateSize()
self._dirty = True
def update( self ):
""" Redraw the button only if it is necesary. """
if self._dirty:
offset = 0
color = pygame.color.Color('white')
if self._mouseOver:
# color = pygame.color.Color('grey')
color = (240,240,240,255)
if self._mousePressed:
offset = 2
rect = pygame.Rect( self._origin[0] + offset, self._origin[1] + offset, self._size[0] - offset, self._size[1] -offset )
pygame.draw.rect( TToolkit.surface, color, rect, 0)
if self._mouseOver and self._mousePressed:
rect = pygame.Rect( self._origin[0] + 2 , self._origin[1] + 2, self._size[0] -4, self._size[1] -4 )
pygame.draw.rect( self.surface, (192,192,192,255), rect, 2)
if self._icon:
x = (self._size[0] - self.__icon_size[0]) /2
TToolkit.surface.blit( self._icon, (self._origin[0] + offset + x , self._origin[1] + 5 + offset ) )
if self._label:
x = (self._size[0] - self.__label_size[0]) /2
render = TButton.buttonFont.render( self._label, 1, (0,0,0))
TToolkit.surface.blit( render, (self._origin[0] + offset + x, self._origin[1] + 5 + offset + self.__icon_size[1] ) )
self._update = True
self._dirty = False
# protected
def _recalculateSize( self ):
spacing_x = 10
spacing_y = 10
self.__label_size = (0,0)
self.__icon_size = (0,0)
if self._icon:
self.__icon_size = self._icon.get_rect()[2:]
self.__label_size = TButton.buttonFont.size( self._label )
if self.__icon_size[0] > self.__label_size[0]:
size_x = self.__icon_size[0]
else:
size_x = self.__label_size[0]
size_y = self.__icon_size[1] + self.__label_size[1]
self._size = (size_x + spacing_x, size_y + spacing_y )
class TToolbar( THBoxLayout ):
Border = 2
def addSeparator( self ):
separator = TSeparator()
self.addWidget(separator)
def addWidget( self, widget ):
THBoxLayout.addWidget( self, widget )
for w in self._widgets:
if isinstance(w, TSeparator):
s = w.getSize()
w.setSize( (s[0], self._size[1]) )
class TSeparator( TToolkit ):
""" A Separator, useful in Toolbars """
XSize = 20
def __init__( self ):
TToolkit.__init__( self )
self.setSize( (TSeparator.XSize,0) )
def update( self ):
if self._dirty:
color = pygame.color.Color('white')
rect = pygame.Rect( self._origin[0], self._origin[1], self._size[0], self._size[1] )
pygame.draw.rect( TToolkit.surface, color, rect, 0)
yy = self._size[1] / 2
y = yy - yy /2
x = self._size[0] / 2 - 1
rect = pygame.Rect( self._origin[0] + x, self._origin[1]+y, 4, yy )
pygame.draw.rect( TToolkit.surface, (192,192,192,255), rect, 2)
self._dirty = False
class TWindow( TToolkit ):
titleFont = font.Fonts.getInstance().get('helvetica 10')
titleY = 25
titleColor = (0,192,204,255)
windowColor = (224,240,240,255)
def __init__( self, title = '' ):
TToolkit.__init__(self)
self._title = title
self.__title_size = (0,0)
self._calcTitleSize()
def setTitle( self, title ):
self._title = title
def getTitle( self ):
return self._title
def update( self ):
if self._dirty:
self._dirty = True
rect = pygame.Rect( self._origin[0], self._origin[1], self._size[0], TWindow.titleY )
pygame.draw.rect( TToolkit.surface, TWindow.titleColor, rect, 0)
render = TButton.buttonFont.render( self._title, 1, (0,0,0))
x_offset = (self._size[0] - self.__title_size[0]) / 2
TToolkit.surface.blit( render, (self._origin[0] + x_offset, self._origin[1] + 3 ) )
rect = pygame.Rect( self._origin[0], self._origin[1] + TWindow.titleY, self._size[0], self._size[1] - TWindow.titleY )
pygame.draw.rect( TToolkit.surface, TWindow.windowColor, rect, 0)
def _calcTitleSize( self ):
self.__title_size = TButton.buttonFont.size( self._title )
|