cl_taskbaricon.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #! /usr/bin/env python
  2. #-*- coding: utf-8 -*-
  3. # Classe permettant l'affichage d'une icone dans la barre des taches
  4. # avec menu
  5. # avec choix de l'icone ou choix d'une grandeur à monitorer
  6. import wx
  7. import string
  8. class Tray(wx.TaskBarIcon):
  9. """ Classe pour l'affichage de l'icone et des menus
  10. dans la barre des taches. """
  11. #Entrées du menu
  12. TBMENU_CREATE = []
  13. def __init__(self, app):
  14. wx.TaskBarIcon.__init__(self)
  15. self.app = app
  16. self.menu_tmp = []
  17. #liaisons Boutons-Méthodes (actions en dur)
  18. self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.OnTaskBarActivate)
  19. def MenuAddItem(self, nom, action):
  20. """ Add an item to the menu and link it to corresponding method """
  21. if action != None :
  22. new_item = wx.NewId()
  23. self.menu_tmp.append({nom:new_item})
  24. self.Bind(wx.EVT_MENU, action, id=new_item)
  25. else:
  26. self.menu_tmp.append({nom:None})
  27. def CreatePopupMenu(self):
  28. """ Method called when we need to display the menu """
  29. menu = wx.Menu()
  30. for item in self.menu_tmp:
  31. for nom,action in item.items():
  32. if action!=None:
  33. menu.Append(action, nom)
  34. else:
  35. menu.AppendSeparator()
  36. return menu
  37. def ChangeIcon(self, imgpath):
  38. """ Change taskbar icon """
  39. img = wx.Image(imgpath,wx.BITMAP_TYPE_PNG)
  40. bmp = img.ConvertToBitmap()
  41. # bmp.SetMask(wx.Mask(bmp, wx.Colour().Alpha()))
  42. icon = wx.IconFromBitmap(img.ConvertToBitmap())
  43. icon = wx.EmptyIcon()
  44. icon.CopyFromBitmap(bmp)
  45. self.SetIcon(icon, "pycpufreq")
  46. def IconMonitor(self, l=2, r=4):
  47. l_off=[128,0,0] # couleurs
  48. l_on=[255,0,0]
  49. r_off=[0,128,0]
  50. r_on=[0,255,0]
  51. s_line = "\xff\xff\xff"+"\0"*45
  52. s_border = "\xff\xff\xff\0\0\0"
  53. s_point = "\0"*3
  54. sl_off = string.join(map(chr,l_off),'')*6
  55. sl_on = string.join(map(chr,l_on),'')*6
  56. sr_off = string.join(map(chr,r_off),'')*6
  57. sr_on = string.join(map(chr,r_on),'')*6
  58. s=""+s_line
  59. for i in range(5):
  60. if i<(5-l):
  61. sl = sl_off
  62. else:
  63. sl = sl_on
  64. if i<(5-r):
  65. sr = sr_off
  66. else:
  67. sr = sr_on
  68. s+=s_border+sl+s_point+sr+s_point
  69. s+=s_border+sl+s_point+sr+s_point
  70. s+=s_line
  71. image = wx.EmptyImage(16,16)
  72. image.SetData(s)
  73. bmp = image.ConvertToBitmap()
  74. bmp.SetMask(wx.Mask(bmp, wx.WHITE)) #sets the transparency colour to white
  75. icon = wx.EmptyIcon()
  76. icon.CopyFromBitmap(bmp)
  77. self.SetIcon(icon, "")