Package evas :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module evas.utils

  1  # Copyright (C) 2007-2008 Gustavo Sverzut Barbieri 
  2  # 
  3  # This file is part of Python-Evas. 
  4  # 
  5  # Python-Evas is free software; you can redistribute it and/or 
  6  # modify it under the terms of the GNU Lesser General Public 
  7  # License as published by the Free Software Foundation; either 
  8  # version 2.1 of the License, or (at your option) any later version. 
  9  # 
 10  # Python-Evas is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 13  # Lesser General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU Lesser General Public License 
 16  # along with this Python-Evas.  If not, see <http://www.gnu.org/licenses/>. 
 17   
 18  __callbacks = ( 
 19      "mouse_in", 
 20      "mouse_out", 
 21      "mouse_down", 
 22      "mouse_up", 
 23      "mouse_move", 
 24      "mouse_wheel", 
 25      "free", 
 26      "del", 
 27      "key_down", 
 28      "key_up", 
 29      "focus_in", 
 30      "focus_out", 
 31      "show", 
 32      "hide", 
 33      "move", 
 34      "resize", 
 35      "restack", 
 36      ) 
 37   
 38   
39 -def __get_callback(observer, name):
40 try: 41 attr = getattr(observer, "cb_on_%s" % name) 42 if callable(attr): 43 return attr 44 except AttributeError, e: 45 return None
46 47
48 -def connect_observer(evas, observer):
49 """Connect methods from observer to Evas callbacks. 50 51 Observer must have methods with name scheme: C{cb_on_<callback>}, 52 examples: 53 - cb_on_resize 54 - cb_on_move 55 - cb_on_show 56 """ 57 for cb_name in __callbacks: 58 cb = __get_callback(observer, cb_name) 59 if cb: 60 setter = getattr(evas, "on_%s_add" % cb_name) 61 setter(cb)
62 63
64 -def disconnect_observer(evas, observer):
65 """Disconnect observer connected using connect_observer()""" 66 for cb_name in __callbacks: 67 cb = __get_callback(observer, cb_name) 68 if cb: 69 unsetter = getattr(evas, "on_%s_del" % cb_name) 70 unsetter(cb)
71 72
73 -def connect_callbacks_by_name(evas, mapping):
74 """Connect callbacks specified in mapping to Evas callbacks. 75 76 Mapping must be a dict or a list of tuples with callback name and 77 desired function, example: 78 - mapping = C{(("resize", my_on_resize), ("show", my_on_show))} 79 - mapping = C{{"resize": my_on_resize, "show": my_on_show}} 80 """ 81 if isinstance(mapping, dict): 82 mapping = mapping.iteritems() 83 for name, func in mapping: 84 try: 85 setter = getattr(evas, "on_%s_add" % name) 86 except AttributeError, e: 87 raise ValueError("invalid callback name: %s" % name) 88 setter(func)
89
90 -def disconnect_callbacks_by_name(evas, mapping):
91 """Disconnect callbacks specified in mapping to Evas callbacks.""" 92 if isinstance(mapping, dict): 93 mapping = mapping.iteritems() 94 for name, func in mapping: 95 try: 96 unsetter = getattr(evas, "on_%s_del" % name) 97 except AttributeError, e: 98 raise ValueError("invalid callback name: %s" % name) 99 unsetter(func)
100