Re: [pysystray-users] Patch: Add arbitrary PackMENUITEMINFO keyword args, remove mandatory/automati
Status: Beta
Brought to you by:
essiene
From: Essien I. E. <es...@da...> - 2007-01-10 09:58:53
|
Thnx for the patch. Will review it add include as appropriate. I'll also be putting up a public svn repo on google code to make creating patches a bit easier. cheers, Essien Charles Duffy wrote: > The attached patch allows arbitrary keyword arguments to be passed > into PackMENUITEMINFO (to allow the user to perform bolding and > graying out; add separator bars, etc), gets rid of the > automatically-added QUIT menu item (so the user can put QUIT wherever > on the menu they like, and name it appropriately). > > Enjoy! > ------------------------------------------------------------------------ > > diff -ru systray-0.6.1.orig/src/SysTrayIcon.py systray-0.6.1/src/SysTrayIcon.py > --- systray-0.6.1.orig/src/SysTrayIcon.py 2005-09-29 00:42:08.000000000 -0500 > +++ systray-0.6.1/src/SysTrayIcon.py 2007-01-09 11:46:18.105000000 -0600 > @@ -21,6 +21,26 @@ > > import winutils > > +class ImmutableDict(dict): > + '''A hashable dict.''' > + > + def __init__(self,*args,**kwds): > + dict.__init__(self,*args,**kwds) > + def __setitem__(self,key,value): > + raise NotImplementedError, "dict is immutable" > + def __delitem__(self,key): > + raise NotImplementedError, "dict is immutable" > + def clear(self): > + raise NotImplementedError, "dict is immutable" > + def setdefault(self,k,default=None): > + raise NotImplementedError, "dict is immutable" > + def popitem(self): > + raise NotImplementedError, "dict is immutable" > + def update(self,other): > + raise NotImplementedError, "dict is immutable" > + def __hash__(self): > + return hash(tuple(self.iteritems())) > + > class SysTrayIcon(object): > '''TODO''' > #TODO: move into local method namespaces. No need to be global really > @@ -69,7 +89,7 @@ > def _add_ids_to_menu_options(self, menu_options): > result = [] > for menu_option in menu_options: > - option_text, option_icon, option_action = menu_option > + option_text, option_icon, option_action, option_kwargs = menu_option > if callable(option_action) or option_action in self.SPECIAL_ACTIONS: > self.menu_actions_by_id.add((self._next_action_id, option_action)) > result.append(menu_option + (self._next_action_id,)) > @@ -77,16 +97,17 @@ > result.append((option_text, > option_icon, > self._add_ids_to_menu_options(option_action), > + option_kwargs, > self._next_action_id)) > else: > - #print 'Unknown item', option_text, option_icon, option_action > + #print 'Unknown item', option_text, option_icon, option_action, option_kwargs > pass > self._next_action_id += 1 > return result > > def _prepare_menu(self, menu_options, default_menu_index=None): > #prepare menu > - menu_options = menu_options + (('Quit', None, self.QUIT),) > + #menu_options = menu_options + (('Quit', None, self.QUIT, ImmutableDict()),) > self._next_action_id = self.FIRST_ID > self.menu_actions_by_id = set() > self.menu_options = self._add_ids_to_menu_options(list(menu_options)) > @@ -202,21 +223,24 @@ > win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0) > > def create_menu(self, menu, menu_options): > - for option_text, option_icon, option_action, option_id in menu_options[::-1]: > + for menu_option in menu_options[::-1]: > + option_text, option_icon, option_action, option_kwargs, option_id = menu_option > if option_icon: > option_icon = self.prep_menu_icon(option_icon) > > - if option_id in self.menu_actions_by_id: > + if option_id in self.menu_actions_by_id: > item, extras = win32gui_struct.PackMENUITEMINFO(text=option_text, > hbmpItem=option_icon, > - wID=option_id) > + wID=option_id, > + **option_kwargs) > win32gui.InsertMenuItem(menu, 0, 1, item) > else: > submenu = win32gui.CreatePopupMenu() > self.create_menu(submenu, option_action) > item, extras = win32gui_struct.PackMENUITEMINFO(text=option_text, > hbmpItem=option_icon, > - hSubMenu=submenu) > + hSubMenu=submenu, > + **option_kwargs) > win32gui.InsertMenuItem(menu, 0, 1, item) > > def prep_menu_icon(self, icon): > diff -ru systray-0.6.1.orig/src/menu.py systray-0.6.1/src/menu.py > --- systray-0.6.1.orig/src/menu.py 2005-09-29 01:29:00.000000000 -0500 > +++ systray-0.6.1/src/menu.py 2007-01-09 11:46:18.105000000 -0600 > @@ -1,7 +1,7 @@ > import utils > > __all__ = ['Menu', 'MenuItem'] > - > +from SysTrayIcon import ImmutableDict > > #TODO: find a mature OrderedDict implementation > > @@ -28,10 +28,21 @@ > self.append(title) > self.append(None) > self.append(utils.DEFAULT_HANDLER) > + self.append({}) > self.name = name > #if not name: > # self.name = title > - > + > + def __getitem__(self, item, *args): > + if isinstance(item, int): > + return list.__getitem__(self, item, *args) > + return self[3].__getitem__(item, *args) > + > + def __setitem__(self, item, value): > + if isinstance(item, int): > + return list.__setitem__(self, item, value) > + return self[3].__setitem__(item, value) > + > def __get_title(self): > return self[0] > > @@ -51,7 +62,7 @@ > icon = property(__get_icon, __set_icon, None, "Sets/Gets the menu icon") > > def get_tuple(self): > - return tuple(self) > + return tuple(self)[:3] + ((ImmutableDict(self[3]),)) > > > > @@ -115,4 +126,4 @@ > self[2] = h > > onclick = property(__get_onclick, __set_onclick, None, "Sets/Gets the onclick handler for the MenuItem") > - > \ No newline at end of file > + > > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share your > opinions on IT & business topics through brief surveys - and earn cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > ------------------------------------------------------------------------ > > _______________________________________________ > pysystray-users mailing list > pys...@li... > https://lists.sourceforge.net/lists/listinfo/pysystray-users > |