""" """ 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: """Template object - base for all widgets.
Widget(**params)
A number of optional params may be passed to the Widget initializer.
This example shows which methods are template methods.
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
"""
def __init__(self,**params):
#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'):
if att in params: s[att] = params[att]
self.style = style.Style(self,s)
self.cls = 'default'
if 'cls' in params: self.cls = params['cls']
if 'name' in params:
import form
self.name = params['name']
if hasattr(form.Form,'form') and form.Form.form != None:
form.Form.form.add(self)
self.form = form.Form.form
if 'value' in params: self.value = params['value']
self.pcls = ""
if params['decorate'] != False:
if (not pguglobals.app):
# TODO - fix this somehow
import app
print 'gui.widget: creating an App'
app.App()
pguglobals.app.theme.decorate(self,params['decorate'])
def focus(self):
"""Focus this Widget.
Widget.focus()""" if getattr(self,'container',None) != None: if self.container.myfocus != self: ## by Gal Koren self.container.focus(self) def blur(self): """Blur this Widget.
Widget.blur()""" if getattr(self,'container',None) != None: self.container.blur(self) def open(self): """Open this Widget as a modal dialog.
Widget.open()""" if getattr(self,'container',None) != None: self.container.open(self) def close(self): """Close this Widget (if it is a modal dialog.)
Widget.close()""" if getattr(self,'container',None) != None: self.container.close(self) def resize(self,width=None,height=None): """Template method - return the size and width of this widget.
Responsible for also resizing all sub-widgets.
Widget.resize(width,height): return width,height
If not overridden, will return self.style.width, self.style.height
""" return self.style.width, self.style.height def chsize(self): """Change the size of this widget.Calling this method will cause a resize on all the widgets, including this one.
Widget.chsize()""" if not hasattr(self,'_painted'): return if not hasattr(self,'container'): return if pguglobals.app: if pguglobals.app._chsize: return 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): """Template method - update the surface
Widget.update(s): return list of pygame.Rect(s)
return - a list of the updated areas as pygame.Rect(s).
""" return def paint(self,s): """Template method - paint the surfaceWidget.paint(s)
Widget.repaint()""" if getattr(self,'container',None) != None: self.container.repaint(self) def repaintall(self): """Request a repaint of all Widgets.
Widget.repaintall()""" if getattr(self,'container',None) != None: self.container.repaintall() def reupdate(self): """Request a reupdate of this Widget
Widget.reupdate()""" if getattr(self,'container',None) != None: self.container.reupdate(self) def next(self): """Pass focus to next Widget.
Widget order determined by the order they were added to their container.
Widget.next()""" if getattr(self,'container',None) != None: self.container.next(self) def previous(self): """Pass focus to previous Widget.
Widget order determined by the order they were added to their container.
Widget.previous()""" if getattr(self,'container',None) != None: self.container.previous(self) def get_abs_rect(self): """Get the absolute rect of this widget on the App screen
Widget.get_abs_rect(): return pygame.Rect""" x, y = self.rect.x , self.rect.y x += self._rect_content.x y += self._rect_content.y c = getattr(self,'container',None) while c: x += c.rect.x y += c.rect.y if hasattr(c,'_rect_content'): x += c._rect_content.x y += c._rect_content.y c = getattr(c,'container',None) 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.
There may be multiple callbacks per event code.
Object.connect(code,fnc,value)
def onclick(value):
print 'click',value
w = Button("PGU!")
w.connect(gui.CLICK,onclick,'PGU Button Clicked')
"""
# Wrap the callback function and add it to the list
cb = SignalCallback()
cb.func = func
cb.params = params
if (not code in self.connects):
self.connects[code] = []
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):
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)):
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.
Object.send(code,event=None)
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.)