Package traylib :: Module tray
[frames] | no frames]

Source Code for Module traylib.tray

  1  import gtk, gobject, os, rox 
  2  from rox import filer, InfoWin 
  3   
  4  import traylib 
  5  from traylib import * 
  6  import traylib.pixmaps as pixmaps 
  7  from traylib.config import Config 
  8  from traylib.icon import Icon 
  9  from traylib.icon_config import IconConfig 
 10  from traylib.tray_config import TrayConfig 
 11  from traylib.menu_icon import MenuIcon 
 12   
 13  _ = rox.i18n.translation(os.path.join(os.path.dirname( 
 14                                                                                  os.path.dirname( 
 15                                                                                          os.path.dirname(traylib.__file__))), 
 16                                                                          'Messages')) 
 17   
 18   
19 -class Tray(object):
20
21 - def __init__(self, icon_config, tray_config, menu_icon_class = MenuIcon):
22 """ 23 Creates a new C{Tray}. 24 25 @param icon_config: The L{IconConfig} of the C{Tray}. 26 @param tray_config: The L{TrayConfig} of the C{Tray}. 27 """ 28 assert isinstance(icon_config, IconConfig) 29 assert isinstance(tray_config, TrayConfig) 30 assert issubclass(menu_icon_class, MenuIcon) 31 32 object.__init__(self) 33 34 self.__icon_config = icon_config 35 self.__tray_config = tray_config 36 tray_config.add_configurable(self) 37 38 self.__container = None 39 40 self.__icons = {} 41 self.__boxes = {} 42 43 if self.__icon_config.vertical: 44 self.__separator_left = gtk.HSeparator() 45 self.__separator_right = gtk.HSeparator() 46 else: 47 self.__separator_left = gtk.VSeparator() 48 self.__separator_right = gtk.VSeparator() 49 self.__menuicon = menu_icon_class(self, icon_config, tray_config) 50 51 if self.__icon_config.vertical: 52 self.__main_box = gtk.VBox() 53 self.__box_left = gtk.VBox() 54 self.__box = gtk.VBox() 55 self.__box_right = gtk.VBox() 56 else: 57 self.__main_box = gtk.HBox() 58 self.__box_left = gtk.HBox() 59 self.__box = gtk.HBox() 60 self.__box_right = gtk.HBox() 61 62 self.__main_box.pack_start(self.__box_left) 63 self.__main_box.pack_start(self.__box) 64 self.__main_box.pack_start(self.__box_right) 65 66 self.__icon_theme_changed_handler = icon_theme.connect('changed', 67 self.__theme_changed) 68 69 self.__main_box.show() 70 self.__box_left.show() 71 self.__box.show() 72 self.__box_right.show() 73 74 self.update_option_separators() 75 self.update_option_menus() 76 77 self.__main_box.connect("destroy", self.__main_box_destroyed) 78 79 assert self.__icon_config == icon_config 80 assert self.__tray_config == tray_config 81 assert self.__tray_config.has_configurable(self)
82
83 - def add_box(self, box_id, separator = False):
84 """ 85 Adds a box to the C{Tray} to which icons can be added via the 86 L{add_icon()} method. 87 88 @param box_id: An identifier for the box. 89 """ 90 assert box_id not in self.__boxes 91 92 if self.__icon_config.vertical: 93 box = gtk.VBox() 94 else: 95 box = gtk.HBox() 96 box.show() 97 98 if separator: 99 if self.__icon_config.vertical: 100 separator = gtk.HSeparator() 101 else: 102 separator = gtk.VSeparator() 103 separator.show() 104 self.__box.pack_start(separator) 105 106 self.__box.pack_start(box) 107 self.__boxes[box_id] = box 108 109 assert box_id in self.__boxes 110 assert self.__boxes[box_id] == box 111 assert box in self.__box.get_children()
112
113 - def add_icon(self, box_id, icon_id, icon):
114 """ 115 Adds an C{Icon} to the C{Tray}. 116 117 @param box_id: The identifier of the box the icon should be added to. 118 @param icon_id: The identifier of the icon. 119 @param icon: The C{Icon} to be added. 120 """ 121 assert box_id in self.__boxes 122 assert icon_id not in self.__icons 123 124 self.__boxes[box_id].pack_start(icon) 125 self.__icons[icon_id] = icon 126 127 assert self.__icons[icon_id] == icon 128 assert icon in self.__boxes[box_id].get_children()
129
130 - def remove_icon(self, icon_id):
131 """ 132 Removes an icon from the C{Tray}. 133 134 @param icon_id: The identifier of the icon. 135 """ 136 assert icon_id in self.__icons 137 138 icon = self.__icons[icon_id] 139 icon.destroy() 140 del self.__icons[icon_id] 141 142 assert self.__icons.get(icon_id) == None
143
144 - def destroy(self):
145 """ 146 Destroys the C{Tray} and the container containing it. 147 """ 148 if self.__container: 149 self.__container.destroy() 150 else: 151 self.__main_box.destroy()
152
153 - def forget_menus(self):
154 """ 155 Makes the C{Tray} forget its main menu. Call this if something affecting 156 the main menu has changed. 157 """ 158 if self.__menuicon_left: 159 self.__menuicon_left.forget_menu() 160 if self.__menuicon_right: 161 self.__menuicon_right.forget_menu()
162
163 - def get_icon(self, id):
164 """ 165 Returns the C{Icon} with the identifier C{id} 166 167 @param id: The identifier of the C{Icon} 168 @return: The C{Icon} with the identifier C{id}. Returns C{None} if the 169 C{Tray} has no C{Icon} with the identifier C{id}. 170 """ 171 return self.__icons.get(id)
172
173 - def set_container(self, container):
174 """ 175 Adds the C{Tray} to the C{gtk.Container} C{container} after removing it 176 from its current container. 177 178 @param container: The C{gtk.Container} the C{Tray} will be added to 179 (may be C{None}). 180 """ 181 assert container == None or isinstance(container, gtk.Container) 182 if self.__container == container: 183 return 184 if self.__container: 185 self.__container.remove(self.__main_box) 186 self.__container = container 187 if self.__container: 188 self.__container.add(self.__main_box)
189 190 191 # Signal callbacks 192
193 - def __theme_changed(self, icon_theme):
194 for id in self.__icons: 195 icon = self.__icons[id] 196 icon.update_icon()
197
198 - def __main_box_destroyed(self, main_box):
199 assert main_box == self.__main_box 200 self.__tray_config.remove_configurable(self) 201 self.quit()
202 203 204 # Methods called when config options of the associated TrayConfig changed 205
206 - def update_option_separators(self):
207 separators = self.__tray_config.separators 208 vertical = self.__icon_config.vertical 209 if separators & LEFT: 210 if self.__separator_left not in self.__box_left.get_children(): 211 self.__box_left.pack_start(self.__separator_left) 212 self.__separator_left.show() 213 else: 214 if self.__separator_left in self.__box_left.get_children(): 215 self.__box_left.remove(self.__separator_left) 216 217 if separators & RIGHT: 218 if self.__separator_right not in self.__box_right.get_children(): 219 self.__box_right.pack_end(self.__separator_right) 220 self.__separator_right.show() 221 else: 222 if self.__separator_right in self.__box_right.get_children(): 223 self.__box_right.remove(self.__separator_right)
224
225 - def update_option_menus(self):
226 menus = self.__tray_config.menus 227 menuicon = self.__menuicon 228 old_box, new_box = (menus == LEFT and (self.__box_right, self.__box_left) 229 or (self.__box_left, self.__box_right)) 230 231 if menuicon in old_box.get_children(): 232 old_box.remove(menuicon) 233 if menuicon not in new_box.get_children(): 234 new_box.pack_end(menuicon) 235 menuicon.update_visibility() 236 menuicon.update_icon() 237 menuicon.update_tooltip() 238 menuicon.update_is_drop_target() 239 menuicon.show_all()
240 241 242 # Methods to be implemented by subclasses 243
244 - def add_custom_menu_items(self, menu):
245 """ 246 Override this to add custom items to the menu. Return C{True} if you did 247 add any items. 248 """ 249 return False
250
251 - def quit(self):
252 """ 253 Override this to clean up when the container containing the tray is 254 destroyed. 255 """ 256 pass
257 258 icon_config = property(lambda self : self.__icon_config) 259 tray_config = property(lambda self : self.__tray_config) 260 icon_ids = property(lambda self : self.__icons.keys()) 261 icons = property(lambda self : self.__icons.values()) 262 menu_icon = property(lambda self : self.__menu_icon)
263