4 from pygame
.locals import *
10 class App(container
.Container
):
11 """The top-level widget for an application.
13 <pre>App(theme=None)</pre>
16 <dt>theme<dd>an instance of a Theme, optional as it will use the default Theme class.
19 <strong>Basic Example</strong>
22 app.run(widget=widget,screen=screen)
25 <strong>Integrated Example</strong>
28 gui.init(widget=widget)
30 for e in pygame.event.get():
38 def __init__(self
,theme
=None,**params
):
42 from theme
import Theme
46 params
['decorate'] = 'app'
47 container
.Container
.__init
__(self
,**params
)
57 def set_global_app(self
):
58 # Keep a global reference to this application instance so that PGU
59 # components can easily find it.
61 # For backwards compatibility we keep a reference in the class
73 #input screen is already set use its size
76 width
,height
= screen
.get_width(),screen
.get_height()
79 elif pygame
.display
.get_surface():
80 screen
= pygame
.display
.get_surface()
82 width
,height
= screen
.get_width(),screen
.get_height()
85 elif self
.style
.width
!= 0 and self
.style
.height
!= 0:
86 screen
= pygame
.display
.set_mode((self
.style
.width
,self
.style
.height
),SWSURFACE
)
88 width
,height
= screen
.get_width(),screen
.get_height()
90 #widget has width,height, or its own size..
93 width
,height
= w
.rect
.w
,w
.rect
.h
= w
.resize()
95 screen
= pygame
.display
.set_mode((width
,height
),SWSURFACE
)
98 #use screen to set up size of this widget
99 self
.style
.width
,self
.style
.height
= width
,height
100 self
.rect
.w
,self
.rect
.h
= width
,height
101 self
.rect
.x
,self
.rect
.y
= 0,0
103 w
.rect
.x
,w
.rect
.y
= 0,0
104 w
.rect
.w
,w
.rect
.h
= w
.resize(width
,height
)
106 for w
in self
.windows
:
107 w
.rect
.w
,w
.rect
.h
= w
.resize()
112 def init(self
,widget
=None,screen
=None): #TODO widget= could conflict with module widget
113 """Initialize the application.
115 <pre>App.init(widget=None,screen=None)</pre>
118 <dt>widget<dd>main widget
119 <dt>screen<dd>pygame.Surface to render to
123 self
.set_global_app()
125 if widget
: self
.widget
= widget
126 if screen
: self
.screen
= screen
133 self
.widgets
.append(w
)
137 pygame
.key
.set_repeat(500,30)
145 """Pass an event to the main widget.
147 <pre>App.event(e)</pre>
153 self
.set_global_app()
155 #NOTE: might want to deal with ACTIVEEVENT in the future.
157 container
.Container
.event(self
,e
)
158 if e
.type == MOUSEBUTTONUP
:
159 if e
.button
not in (4,5): #ignore mouse wheel
160 sub
= pygame
.event
.Event(CLICK
,{
163 self
.send(sub
.type,sub
)
164 container
.Container
.event(self
,sub
)
168 self
.set_global_app()
171 for e
in pygame
.event
.get():
172 if not (e
.type == QUIT
and self
.mywindow
):
175 pygame
.display
.update(us
)
178 def paint(self
,screen
):
183 if hasattr(self
,'background'):
184 self
.background
.paint(screen
)
185 container
.Container
.paint(self
,screen
)
187 def update(self
,screen
):
188 """Update the screen.
191 <dt>screen<dd>pygame surface
200 self
._repaint
= False
201 return [pygame
.Rect(0,0,screen
.get_width(),screen
.get_height())]
203 us
= container
.Container
.update(self
,screen
)
206 def run(self
,widget
=None,screen
=None):
207 """Run an application.
209 <p>Automatically calls <tt>App.init</tt> and then forever loops <tt>App.event</tt> and <tt>App.update</tt></p>
212 <dt>widget<dd>main widget
213 <dt>screen<dd>pygame.Surface to render to
216 self
.init(widget
,screen
)
217 while not self
._quit
:
221 def reupdate(self
,w
=None): pass
222 def repaint(self
,w
=None): self
._repaint
= True
223 def repaintall(self
): self
._repaint
= True
228 def quit(self
,value
=None): self
._quit
= True
230 def open(self
, w
, pos
=None):
233 if (w
.rect
.w
== 0 or w
.rect
.h
== 0):
234 w
.rect
.size
= w
.resize()
237 # Auto-center the window
238 w
.rect
.center
= self
.rect
.center
239 #w.rect.topleft = ((self.rect.w - w.rect.w)/2,
240 # (self.rect.h - w.rect.h)/2)
242 # Show the window in a particular location
245 self
.windows
.append(w
)
252 if self
.myfocus
is w
: self
.blur(w
)
254 if w
not in self
.windows
: return #no need to remove it twice! happens.
256 self
.windows
.remove(w
)
260 self
.mywindow
= self
.windows
[-1]
261 self
.focus(self
.mywindow
)
263 if not self
.mywindow
:
264 self
.myfocus
= self
.widget
#HACK: should be done fancier, i think..
266 self
.enter(self
.widget
)
273 """Create an App using the <tt>desktop</tt> theme class.
277 def __init__(self
,**params
):
278 params
.setdefault('cls','desktop')
279 App
.__init
__(self
,**params
)