/usr/share/pyshared/freevo/gui/GUIObject.py is in python-freevo 1.9.2b2-4.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | # -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# GUIObject - Common object for all GUI Classes
# -----------------------------------------------------------------------
# $Id: GUIObject.py 11905 2011-11-14 21:54:46Z adam $
#
# Todo: o Add move function
# -----------------------------------------------------------------------
#
# Freevo - A Home Theater PC framework
#
# Copyright (C) 2002 Krister Lagerstrom, et al.
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY 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 Object Oriented GUI Widget library for Freevo
This is aimed at being a general library for GUI programming with Freevo.
It is built directly on top of SDL with pygame, and it's aimed at being
fairly fast.
Event though the library is built from the ground the design is heavy
influenced by other GUI toolkits, such as Java JDK and QT.
Currently not many classes are in place, but hopefully we will add more
in time.
"""
import logging
logger = logging.getLogger("freevo.gui.GUIObject")
__date__ = "$Date: 2011-11-14 22:54:46 +0100(lun, 14 nov 2011) $"
__version__ = "$Revision: 11905 $".split()[1]
__author__ = "Thomas Malt <thomas@malt.no>"
import rc
import osd
import config
import skin
import traceback
import pygame
from pygame import Rect
from Color import *
class GUIObject:
"""
Common parent class of all GUI objects. You can override this to make new
Widgets.
"""
def __init__(self, left=0, top=0, width=0, height=0, bg_color=None, fg_color=None):
self.left = left or 0
self.top = top or 0
self.width = width or 0
self.height = height or 0
try:
self.rect = Rect(self.left, self.top, self.width, self.height)
except TypeError:
_debug_('Invalid Rect: left=%r, top=%r, width=%r, height=%r' % (left, top, width, height))
self.osd = osd.get_singleton()
self.label = None
self.icon = None
self.surface = None
self.bg_surface = None
self.bg_image = None
self.parent = None
self.children = []
self.enabled = 1
self.visible = 1
self.selected = 0
self.rect = pygame.Rect(0, 0, 0, 0)
self.refresh_abs_position()
self.rect.width = self.width
self.rect.height = self.height
self.bg_color = bg_color
self.fg_color = fg_color
self.event_context = None
style = skin.get_singleton().get_popupbox_style(self)
self.content_layout, self.background_layout = style
self.skin_info_widget = self.content_layout.types['widget']
self.skin_info_font = self.skin_info_widget.font
self.h_margin = self.content_layout.spacing
self.v_margin = self.content_layout.spacing
ci = self.content_layout.types['widget'].rectangle
self.bg_color = self.bg_color or Color(ci.bgcolor)
self.fg_color = self.fg_color or Color(ci.color)
self.set_v_align(Align.NONE)
self.set_h_align(Align.NONE)
def get_event_context(self):
"""
return the event context for that gui widget
"""
return self.event_context
def get_rect(self):
"""
Return SDL rect information for the object.
Returns: left,top,width,height
"""
return (self.left, self.top, self.width, self.height)
def get_position(self):
"""
Gets the coordinates of the GUIObject
Arguments: None
Returns: (x, y) - The coordinates of top left coner as list.
"""
return (self.left, self.top)
def set_position(self, left, top=None):
"""
Set the position of the GUIObject
"""
# XXX Please tell if you know of a better way to accept both
# XXX tuples and lists.
if type(left) is ListType or type(left) is TupleType:
self.left, self.top = left
else:
self.left = left
self.top = top
self.refresh_abs_position()
def refresh_abs_position(self):
if self.rect is not None:
if self.parent is not None and self.parent.rect is not None:
if self.top is not None and self.left is not None:
self.rect.left = self.parent.rect.left + self.left
self.rect.top = self.parent.rect.top + self.top
else:
self.rect.left = self.parent.rect.left
self.rect.top = self.parent.rect.top
else:
if self.top is not None and self.left is not None:
self.rect.left = self.left
self.rect.top = self.top
for child in self.children:
child.refresh_abs_position()
def get_size(self):
"""
Get the width and height of box
Arguments: None
Returns: (width, height) - as list.
"""
return (int(self.width), int(self.height))
def set_size(self, width, height=None):
"""
Set the width adn height of box
"""
if type(width) is ListType or TupleType:
self.width, self.height = width
self.rect.width, self.rect.height = width
else:
self.width = width
self.height = height
self.rect.width = width
self.rect.height = height
def toggle_selected(self):
self.selected = not self.selected
def get_selected_child(self):
for child in self.children:
if not child.is_visible():
continue
if child.selected == 1:
return child
else:
selected = child.get_selected_child()
if selected:
return child
def show(self):
"""
Shows the object.
This is really handled by the render object.
"""
self.visible = 1
self.draw()
self.osd.update(self.get_rect())
def hide(self):
"""
Hide the object.
"""
self.visible = 0
if self.parent and self.parent.visible and self.bg_surface:
self.osd.dialog_layer.blit(self.bg_surface, self.get_position())
self.osd.update(self.get_rect())
def move(self, x, y):
"""
Move the object by a certain amount
@note: Either the user would have to hide and show the object
moving, or we do it for him. Not decided yet.
@param x: amount to move along x axis.
@type x: Integer
@param y: amount to move along y axis.
@type y: Integer
"""
self.hide()
self.set_position(self.left+x, self.top+y)
self.show()
def is_visible(self):
"""
Returns whether the object is visible or not.
Returns: 0 or 1
"""
return self.visible
def enable(self):
self.enabled = 1
def disable(self):
self.enabled = 0
def refresh(self):
"""
At the moment not implemented.
"""
self.draw()
def layout(self):
"""
To be overriden by Container.
"""
pass
def draw(self, update=False):
_debug_('GUIObject::draw %s' % self, 2)
if self.is_visible() == 0:
return False
self.layout()
self._draw()
if not update:
return
# now blit all parent to really update
object = self.parent
while object:
if not self.parent or not self.parent.surface or not object.surface:
break
if object.surface == object.surface.get_abs_parent():
object.blit_parent(restore=False)
object = object.parent
self.osd.update()
def _draw(self, surface=None):
"""
This function should be overriden by those
objects that inherit this.
"""
pass
def blit_parent(self, restore=True):
"""
blit self.surface to the parent.surface
"""
if self.parent.surface:
p = True
else:
p = False
if self.surface != self.surface.get_abs_parent():
print '******************************************************************'
print 'Error, surface is a subsurface (%s)' % self
print 'Please report the following lines to the freevo mailing list'
print
print 'Error, surface is a subsurface (%s)' % self
print traceback.print_exc()
print
print 'GUIObject stack:'
c = self
while c:
print ' %s' % c
c = c.parent
print
print '******************************************************************'
return
if not self.bg_surface:
self.bg_surface = self.osd.Surface((int(self.width), int(self.height)))
if p:
self.bg_surface.blit(self.parent.surface, (0,0), self.get_rect())
else:
self.bg_surface.blit(self.osd.dialog_layer, (0,0), self.get_rect())
elif restore:
if p:
self.parent.surface.blit(self.bg_surface, (self.left, self.top))
else:
self.osd.dialog_layer.blit(self.bg_surface, (self.left, self.top))
if p:
self.parent.surface.blit(self.surface, self.get_position())
else:
self.osd.dialog_layer.blit(self.surface, self.get_position())
self.osd.dialog_layer_enabled = True
self.osd.update(self.get_rect())
def get_surface(self):
"""
get a subsurface from the parent to draw in
"""
try:
return self.parent.surface.subsurface(self.get_rect())
except Exception, e:
print '******************************************************************'
print 'Exception: %s' % e
print 'Please report the following lines to the freevo mailing list'
print
if not self.parent:
print 'object has no parent:'
print self
elif not hasattr(self.parent, 'surface'):
print 'parent has no surface:'
print self, self.parent
elif not self.parent.surface:
print 'parent surface is None:'
print self, self.parent
else:
print 'wanted %s for %s' % (self.get_rect(), self.parent.surface)
print traceback.print_exc()
print
print 'GUIObject stack:'
c = self
while c:
print ' %s: %s' % (c, c.get_rect())
c = c.parent
print
print 'Configuration: %sx%s %s %s' % \
(self.osd.width, self.osd.height, config.OSD_OVERSCAN_LEFT,
config.OSD_OVERSCAN_TOP)
print
raise Exception, e
def set_parent(self, parent):
"""
Set the parent of this widget
"""
if self.parent != parent and self.parent and self in self.parent.children:
self.parent.children.remove(self)
self.parent = parent
self.refresh_abs_position()
def get_parent(self):
return self.parent
def add_child(self, child):
"""
Add a child widget.
"""
self.children.append(child)
child.set_parent(self)
def get_children(self):
return children
def eventhandler(self, event):
return self.parent.eventhandler(event)
def destroy(self):
self.visible = 0
if self.children:
while self.children:
child = self.children[0]
child.destroy() # the child will remove itself from children
_debug_('parent: %s' % self.parent, 2)
if self.parent:
self.parent.children.remove(self)
self.hide()
if self.parent:
self.parent.refresh()
self.set_parent(None)
def get_h_align(self):
"""
Returns horisontal align of text.
"""
return self.h_align
def get_v_align(self):
"""
returns vertical alignment of text
"""
return self.v_align
def set_h_align(self, align):
"""
Sets horizontal alignment of text.
"""
if type(align) is IntType and align >= 1000 and align < 1004:
self.h_align = align
else:
raise TypeError, align
def set_v_align(self, align):
"""
Sets vertical alignment of text.
"""
# XXX: fix this ugly statement
if type(align) is IntType and \
(align == 1000 or align == 1001 or (align > 1003 and align < 1007)):
self.v_align = align
else:
raise TypeError, align
def get_v_margin(self):
"""
Returns the margin for objects drawing directly on the osd.
"""
return self.v_margin
def get_h_margin(self):
"""
Returns the margin for objects drawing directly on the osd.
This is not optimal and I'll probably remove this function soon.
"""
return self.h_margin
def set_v_margin(self, marg):
"""
Sets the vertical margin.
"""
self.v_margin = marg
def set_h_margin(self, marg):
"""
Sets the horisontal margin
"""
self.h_margin = marg
def calc_position(self):
"""
Private function to calculate correct positon of a widget.
"""
if not self.parent: raise ParentException
# if not self.font: raise TypeError, 'No font'
# Render the surface if we don't have it to get correct size.
# if not self.surface: self.render()
lx = 0
ly = 0
bx,by,bw,bh = self.parent.get_rect()
lw,lh = self.get_size()
va = self.v_align
ha = self.h_align
hm = self.h_margin
vm = self.v_margin
if ha == Align.LEFT:
if self.parent.icon:
iw = self.parent.icon.get_width()
pm = hm
lx = bx+iw+(pm*2)
else:
lx = bx+hm
elif ha == Align.CENTER:
lx = bx+((bw-lw)/2)
elif ha == Align.RIGHT:
lx = bx+bw-lw-hm
elif ha == Align.NONE:
lx = self.left
else:
raise TypeError, 'Wrong h_align'
if va == Align.TOP:
ly = by+vm
elif va == Align.BOTTOM:
ly = by+bh-lh-vm
elif va == Align.CENTER:
ly = by+((bh-lh)/2)
elif va == Align.NONE:
ly = self.top
else:
raise TypeError, 'Wrong v_align'
# for child in self.children:
# child.calc_position()
return (lx,ly)
class Align:
NONE = 1000
CENTER = 1001
LEFT = 1002
RIGHT = 1003
TOP = 1004
BOTTOM = 1005
|