"""

import pygame

import pguglobals import style

class SignalCallback:
# The function to call func = None # The parameters to pass to the function (as a list) params = None
class Widget:

"""Base class for all PGU graphical objects.

Example - Creating your own Widget:

class Draw(gui.Widget):
def paint(self,s):
# Paint the pygame.Surface return
def update(self,s):
# Update the pygame.Surface and return the update rects return [pygame.Rect(0,0,self.rect.w,self.rect.h)]
def event(self,e):
# Handle the pygame.Event return
def resize(self,width=None,height=None):
# Return the width and height of this widget return 256,256

System Message: WARNING/2 (gui/widget.py, line 35)

Definition list ends without a blank line; unexpected unindent.

"""

# The name of the widget (or None if not defined) name = None # The container this widget belongs to container = None # Whether this widget has been painted yet _painted = False # The widget used to paint the background background = None # ... _rect_content = None

def __init__(self,**params):

"""Widget constructor.

Keyword arguments:

decorate -- whether to call theme.decorate(self) to allow the theme
a chance to decorate the widget. Default is true.

System Message: WARNING/2 (gui/widget.py, line 55)

Definition list ends without a blank line; unexpected unindent.

style -- a dict of style parameters. x, y -- position parameters width, height -- size parameters align, valign -- alignment parameters, passed along to style font, color, background -- other common parameters that are passed

System Message: ERROR/3 (gui/widget.py, line 60)

Unexpected indentation.
along to style

System Message: WARNING/2 (gui/widget.py, line 61)

Block quote ends without a blank line; unexpected unindent.

cls -- class name as used by Theme name -- name of widget as used by Form. If set, will call

System Message: ERROR/3 (gui/widget.py, line 63)

Unexpected indentation.
form.add(self,name) to add the widget to the most recently created Form.

System Message: WARNING/2 (gui/widget.py, line 65)

Block quote ends without a blank line; unexpected unindent.
focusable -- True if this widget can receive focus via Tab, etc.
Defaults to True.

System Message: WARNING/2 (gui/widget.py, line 67)

Definition list ends without a blank line; unexpected unindent.

disabled -- True of this widget is disabled. Defaults to False. value -- initial value

""" #object.Object.__init__(self) self.connects = {} params.setdefault('decorate',True) params.setdefault('style',{}) params.setdefault('focusable',True) params.setdefault('disabled',False)

self.focusable = params['focusable'] self.disabled = params['disabled']

self.rect = pygame.Rect(params.get('x',0),
params.get('y',0), params.get('width',0), params.get('height',0))

s = params['style'] #some of this is a bit "theme-ish" but it is very handy, so these #things don't have to be put directly into the style. for att in ('align','valign','x','y','width','height','color','font','background'):

System Message: ERROR/3 (gui/widget.py, line 90)

Unexpected indentation.
if att in params: s[att] = params[att]

System Message: WARNING/2 (gui/widget.py, line 91)

Block quote ends without a blank line; unexpected unindent.

self.style = style.Style(self,s)

self.cls = 'default' if 'cls' in params: self.cls = params['cls'] if 'name' in params:

System Message: ERROR/3 (gui/widget.py, line 96)

Unexpected indentation.

import form self.name = params['name'] if form.Form.form:

System Message: ERROR/3 (gui/widget.py, line 99)

Unexpected indentation.
form.Form.form.add(self) self.form = form.Form.form

System Message: WARNING/2 (gui/widget.py, line 101)

Block quote ends without a blank line; unexpected unindent.

if 'value' in params: self.value = params['value'] self.pcls = ""

if params['decorate'] != False:
if (not pguglobals.app):
# TODO - fix this somehow import app app.App()

System Message: WARNING/2 (gui/widget.py, line 109)

Definition list ends without a blank line; unexpected unindent.

pguglobals.app.theme.decorate(self,params['decorate'])

def focus(self):

"""Focus this Widget.""" if self.container:

System Message: ERROR/3 (gui/widget.py, line 114)

Unexpected indentation.
if self.container.myfocus != self: ## by Gal Koren
self.container.focus(self)
def blur(self):
"""Blur this Widget.""" if self.container: self.container.blur(self)
def open(self):
"""Open this widget as a modal dialog.""" #if getattr(self,'container',None) != None: self.container.open(self) pguglobals.app.open(self)
def close(self, w=None):

"""Close this widget, if it is currently an open dialog.""" #if getattr(self,'container',None) != None: self.container.close(self) if (not w):

System Message: ERROR/3 (gui/widget.py, line 130)

Unexpected indentation.
w = self

System Message: WARNING/2 (gui/widget.py, line 131)

Block quote ends without a blank line; unexpected unindent.

pguglobals.app.close(w)

def is_open(self):
return (self in pguglobals.app.windows)
def is_hovering(self):

"""Returns true if the mouse is hovering over this widget.""" if self.container:

System Message: ERROR/3 (gui/widget.py, line 139)

Unexpected indentation.
return (self.container.myhover is self)

System Message: WARNING/2 (gui/widget.py, line 140)

Block quote ends without a blank line; unexpected unindent.

return False

def resize(self,width=None,height=None):

"""Resize this widget and all sub-widgets, returning the new size.

This should be implemented by a subclass.

""" return (self.style.width, self.style.height)

def chsize(self):

"""Change the size of this widget."""

if (not self._painted): return

if (not self.container): return

if pguglobals.app:
if pguglobals.app._chsize:
return

System Message: WARNING/2 (gui/widget.py, line 160)

Definition list ends without a blank line; unexpected unindent.

pguglobals.app.chsize() return

#if hasattr(app.App,'app'): # w,h = self.rect.w,self.rect.h # w2,h2 = self.resize() # if w2 != w or h2 != h: # app.App.app.chsize() # else: # self.repaint()

def update(self,s):

"""Updates the surface and returns a rect list of updated areas

This should be implemented by a subclass.

""" return

def paint(self,s):

"""Render this widget onto the given surface

This should be implemented by a subclass.

""" return

def repaint(self):
"""Request a repaint of this Widget.""" if self.container: self.container.repaint(self)
def repaintall(self):
"""Request a repaint of all Widgets.""" if self.container: self.container.repaintall()
def reupdate(self):
"""Request a reupdate of this Widget.""" if self.container: self.container.reupdate(self)
def next(self):

"""Pass focus to next Widget.

Widget order determined by the order they were added to their container.

""" if self.container: self.container.next(self)

def previous(self):

"""Pass focus to previous Widget.

Widget order determined by the order they were added to their container.

"""

if self.container: self.container.previous(self)

def get_abs_rect(self):

"""Returns the absolute rect of this widget on the App screen.""" x, y = self.rect.x, self.rect.y cnt = self.container while cnt:

System Message: ERROR/3 (gui/widget.py, line 222)

Unexpected indentation.

x += cnt.rect.x y += cnt.rect.y if cnt._rect_content:

System Message: ERROR/3 (gui/widget.py, line 225)

Unexpected indentation.
x += cnt._rect_content.x y += cnt._rect_content.y

System Message: WARNING/2 (gui/widget.py, line 227)

Block quote ends without a blank line; unexpected unindent.

cnt = cnt.container

System Message: WARNING/2 (gui/widget.py, line 228)

Block quote ends without a blank line; unexpected unindent.

return pygame.Rect(x, y, self.rect.w, self.rect.h)

def connect(self,code,func,*params):

"""Connect a event code to a callback function.

<p>There may be multiple callbacks per event code.</p>

Arguments: code -- event type fnc -- callback function *values -- values to pass to callback.

System Message: WARNING/2 (gui/widget.py, line 235); backlink

Inline emphasis start-string without end-string.

Please note that callbacks may also have "magicaly" parameters. Such as:

_event -- receive the event _code -- receive the event code _widget -- receive the sending widget

Example:

def onclick(value):
print 'click', value

w = Button("PGU!") w.connect(gui.CLICK,onclick,'PGU Button Clicked')

""" if (not code in self.connects):

System Message: ERROR/3 (gui/widget.py, line 257)

Unexpected indentation.
self.connects[code] = []

System Message: WARNING/2 (gui/widget.py, line 258)

Block quote ends without a blank line; unexpected unindent.
for cb in self.connects[code]:
if (cb.func == func):
# Already connected to this callback function return

System Message: WARNING/2 (gui/widget.py, line 262)

Definition list ends without a blank line; unexpected unindent.

# Wrap the callback function and add it to the list cb = SignalCallback() cb.func = func cb.params = params self.connects[code].append(cb)

# Remove signal handlers from the given event code. If func is specified, # only those handlers will be removed. If func is None, all handlers # will be removed. def disconnect(self, code, func=None):

System Message: ERROR/3 (gui/widget.py, line 272)

Unexpected indentation.
if (not code in self.connects):
return
if (not func):
# Remove all signal handlers del self.connects[code]
else:

# Remove handlers that call 'func' n = 0 callbacks = self.connects[code] while (n < len(callbacks)):

System Message: ERROR/3 (gui/widget.py, line 282)

Unexpected indentation.
if (callbacks[n].func == func):
# Remove this callback del callbacks[n]
else:
n += 1
def send(self,code,event=None):

"""Send a code, event callback trigger.""" if (not code in self.connects):

System Message: ERROR/3 (gui/widget.py, line 291)

Unexpected indentation.
return

System Message: WARNING/2 (gui/widget.py, line 292)

Block quote ends without a blank line; unexpected unindent.

# Trigger all connected signal handlers for cb in self.connects[code]:

System Message: ERROR/3 (gui/widget.py, line 294)

Unexpected indentation.

func = cb.func values = list(cb.params)

nargs = func.func_code.co_argcount names = list(func.func_code.co_varnames)[:nargs] if hasattr(func,'im_class'): names.pop(0)

args = [] magic = {'_event':event,'_code':code,'_widget':self} for name in names:

System Message: ERROR/3 (gui/widget.py, line 304)

Unexpected indentation.
if name in magic.keys():
args.append(magic[name])
elif len(values):
args.append(values.pop(0))
else:
break

System Message: WARNING/2 (gui/widget.py, line 310)

Block quote ends without a blank line; unexpected unindent.

args.extend(values) func(*args)

System Message: WARNING/2 (gui/widget.py, line 310); backlink

Inline emphasis start-string without end-string.
def _event(self,e):
if self.disabled: return self.send(e.type,e) return self.event(e)
def event(self,e):

"""Called when an event is passed to this object.

Please note that if you use an event, returning the value True will stop parent containers from also using the event. (For example, if your widget handles TABs or arrow keys, and you don't want those to also alter the focus.)

This should be implemented by a subclass.

""" return

def get_toplevel(self):

"""Returns the top-level widget (usually the Desktop) by following the chain of 'container' references.

""" top = self while (top.container):

System Message: ERROR/3 (gui/widget.py, line 338)

Unexpected indentation.
top = top.container

System Message: WARNING/2 (gui/widget.py, line 339)

Block quote ends without a blank line; unexpected unindent.

return top

def collidepoint(self, pos):

"""Test if the given point hits this widget. Over-ride this function for more advanced collision testing.

""" return self.rect.collidepoint(pos)