4 __copyright__
= "Copyright (c) 2002-2005 Free Software Foundation, Inc."
6 Straw is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 2 of the License, or (at your option) any later
11 Straw is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 Place - Suite 330, Boston, MA 02111-1307, USA. """
26 def schedule(function
, *args
, **kw
):
28 Schedules a function call in the main loop thread.
29 The given arguments and keywords are passed along if any.
30 Threads may not access Straw and GTK elsewhere.
31 XXX occasional "Trying re-entry!" warnings
34 assert function
.__call
__
38 return False # call only once
40 gobject
.timeout_add(0, callback
) # call as soon as possible
43 class MainloopManager
:
47 self
._idle
_handlers
= []
51 def add_idle_handler(self
, handler
):
52 oid
= self
._add
_idle
(handler
)
53 self
._idle
_handlers
.append((oid
, handler
))
55 def _add_idle(self
, handler
):
56 oid
= gtk
.idle_add(handler
)
59 def remove_idle_handler(self
, handler
):
60 for oid
, h
in self
._idle
_handlers
:
65 error
.log("no handler ", handler
, " found!")
67 def remove_idle_handler_by_id(self
, oid
):
70 def call_pending(self
):
71 if self
._call
_depth
> 0:
72 error
.log("Trying re-entry! Not allowed.")
76 for oid
, handler
in self
._idle
_handlers
:
77 self
.remove_idle_handler_by_id(oid
)
78 while gtk
.events_pending():
79 gtk
.main_iteration(False)
81 for index
in xrange(len(self
._idle
_handlers
)):
82 oid
, handler
= self
._idle
_handlers
[index
]
83 new_id
= self
._add
_idle
(handler
)
84 self
._idle
_handlers
[index
] = (new_id
, handler
)
88 def set_repeating_timer(self
, timeout
, function
, data
= None):
90 if not self
._timers
.has_key(function
):
99 gobject
.timeout_add(timeout
, f
, data
)
101 gobject
.timeout_add(timeout
, f
)
103 gobject
.timeout_add(timeout
, f
, data
)
105 gobject
.timeout_add(timeout
, f
)
106 self
._timers
[function
] = True
108 def end_repeating_timer(self
, function
):
109 if self
._timers
.has_key(function
):
110 del self
._timers
[function
]
112 def get_instance(klass
):
113 if klass
.instance
is None:
114 klass
.instance
= klass()
115 return klass
.instance
116 get_instance
= classmethod(get_instance
)