instrumentation: add next-share/
[cs-p2p-next.git] / instrumentation / next-share / BaseLib / Video / Buttons.py
1 # Written by Jelle Roozenburg, Maarten ten Brinke 
2 # see LICENSE.txt for license information
3 import wx, os, sys
4 from traceback import print_exc
5
6 DEBUG = False
7
8 class PlayerButton(wx.Panel):
9     """
10     Button that changes the image shown if you move your mouse over it.
11     It redraws the background of the parent Panel, if this is an imagepanel with
12     a variable self.bitmap.
13     """
14
15     def __init__(self, parent, imagedir, imagename):
16         self.imagedir = imagedir
17         self.imagename = imagename
18         self.parent = parent
19         self.init()
20         
21     def init(self):
22         self.initDone = False
23         self.enabled = True
24         self.backgroundColor = wx.WHITE
25         wx.Panel.__init__(self, self.parent) 
26         self.selected = False
27         self.tooltip = None
28         self.Bind(wx.EVT_MOUSE_EVENTS, self.mouseAction)
29         
30         
31         self.searchBitmaps()
32         self.createBackgroundImage()
33         
34         #<mluc> on mac, the button doesn't get a size
35         #if self.bitmaps[0] and self.GetSize()==(0,0):
36         if self.bitmaps[0]:
37             self.SetSize(self.bitmaps[0].GetSize())
38 #        print self.Name
39 #        print 'size'
40 #        print self.Size
41         
42         
43         self.initDone = True
44         self.Refresh(True)
45         self.Update()
46
47
48     def GetImageName(self):
49         return self.imagename
50         
51         
52     def searchBitmaps(self):
53         self.bitmaps = [None, None ,None]
54         self.parentBitmap = None
55         self.mouseOver = False
56                 
57         if not os.path.isdir(self.imagedir):
58             print 'Error: no image directory found in %s' % self.imagedir
59             return
60         
61         # find a file with same name as this panel
62         self.bitmapPath = [os.path.join(self.imagedir, self.imagename+'.png'), 
63                            os.path.join(self.imagedir, self.imagename+'_hover.png'),
64                            os.path.join(self.imagedir, self.imagename+'_dis.png')]
65         
66         i = 0
67         for img in self.bitmapPath:
68             if os.path.isfile(img):
69                 self.bitmaps[i] = wx.Bitmap(img, wx.BITMAP_TYPE_ANY)
70                 i+=1
71             elif DEBUG:
72                 print 'Could not find image: %s' % img
73          
74            
75         
76         
77     def createBackgroundImage(self):
78         if self.bitmaps[0]:
79             wx.EVT_PAINT(self, self.OnPaint)
80             self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnErase)
81                 
82     
83     def OnErase(self, event):
84         pass
85         #event.Skip()
86         
87     def setSelected(self, sel):
88         self.selected = sel
89         self.Refresh()
90         
91     def isSelected(self):
92         return self.selected
93         
94     def mouseAction(self, event):
95         event.Skip()
96         if event.Entering():
97             #print 'enter' 
98             self.mouseOver = True
99             self.Refresh()
100         elif event.Leaving():
101             self.mouseOver = False
102             #print 'leave'
103             self.Refresh()
104
105
106     def getParentBitmap(self):
107         try:
108             parent = self.GetParent()
109             bitmap = parent.bitmap
110             #print bitmap
111         except:
112             return None
113         
114         if bitmap:
115             location = self.GetPosition()
116             #location[0] -= parent.GetPosition()[0]
117             #location[1] -= parent.GetPosition()[1]
118             #if DEBUG:
119             #    print '(button %s) Mypos: %s, Parentpos: %s' % (self.GetName(), self.GetPosition(), parent.GetPosition())
120             rect = [location[0], location[1], self.GetClientSize()[0], self.GetClientSize()[1]]
121             #if DEBUG:
122             #    print '(button %s) Slicing rect(%d,%d) size(%s) from parent image size(%s)' % (self.GetName(), location[0], location[1], str(self.GetClientSize()), str(bitmap.GetSize()))
123             bitmap = self.getBitmapSlice(bitmap, rect)
124             return bitmap
125         else:
126             return None
127     
128     def joinImage(self, im1,im2,offsetx=0,offsety=0):
129         "Draw im2 on im1"
130         stopx = im2.GetWidth()
131         if stopx > (im1.GetWidth()-offsetx):
132             stopx = im1.GetWidth()-offsetx
133         stopy = im2.GetHeight()
134         if stopy > (im1.GetHeight()-offsety):
135             stopy = im1.GetHeight()-offsety
136         if stopx>0 and stopy>0:
137             for x in range(0,stopx):
138                 for y in range(0,stopy):
139                     rgb2 = (im2.GetRed(x,y),im2.GetGreen(x,y),im2.GetBlue(x,y))
140                     if rgb2 !=(255,0,255):
141                         im1.SetRGB(x+offsetx,y+offsety,rgb2[0],rgb2[1],rgb2[2])
142         return im1
143  
144     def getBitmapSlice(self, bitmap, rect):
145         try:
146             #print rect
147             bitmapSize = bitmap.GetSize()
148             rect[0] %= bitmapSize[0]
149             rect[1] %= bitmapSize[1]
150             rects = [rect]
151             if rect[0]+rect[2] > bitmapSize[0]:
152                 rect1 = (rect[0], rect[1], bitmapSize[0]-rect[0], rect[3])
153                 rect2 = (0, rect[1], rect[0]+rect[2] - bitmapSize[0], rect[3])
154                 rects = [rect1, rect2]
155             if rect[1]+ rect[3] > bitmapSize[1]:
156                 rects2 = []
157                 for r in rects:
158                     r1 = (r[0], r[1], r[2], bitmapSize[1] - r[3])
159                     r2 = (r[0], 0, r[2], r[1]+r[3] - bitmapSize[1])
160                     rects2.append(r1)
161                     rects2.append(r2)
162                 rects = rects2
163             images = []
164             if len(rects) > 1:
165                 if DEBUG:
166                     print "(button %s) Result: %s" % (self.GetName(), rects)
167                 image = wx.EmptyImage(rect[2], rect[3])
168                 for r in rects:    
169                     rect = wx.Rect(r[0], r[1], r[2], r[3])
170                     if DEBUG:
171                         print '(button %s) Trying to get rect: %s from bitmap: %s' % (self.GetName(), rect, bitmap.GetSize())
172                     subBitmap = bitmap.GetSubBitmap(rect)
173                     subImage = subBitmap.ConvertToImage()
174                     if len(rects) == 2:
175                         if r == rects[0]:
176                             place = (0,0)
177                         elif r == rects[1]:
178                             place = (rects[0][2], 0)
179                     elif len(rects) == 4:
180                         if r == rects[0]:
181                             place = (0,0)
182                         elif r == rects[1]:
183                             place = (0, rects[0][3])
184                         elif r == rects[2]:
185                             place = (rects[0][2],0)
186                         elif r == rects[3]:
187                             place = (rects[0][2], rects[0][3])
188                     if DEBUG:
189                         print "(button %s) Place subbitmap: %s" % (self.GetName(), str(place))
190                     self.joinImage(image, subImage, place[0], place[1])
191                 if DEBUG:
192                     print '(button %s) Result img size: %s' % (self.GetName(), str(image.GetSize()))
193                 return image.ConvertToBitmap()
194             else:
195                 return bitmap.GetSubBitmap(wx.Rect(rect[0], rect[1], rect[2], rect[3]))
196         except:
197             if DEBUG:
198                 print_exc()
199             return None
200                                             
201     def setEnabled(self, e):
202         self.enabled = e
203         if not e:
204             self.SetToolTipString('')
205 #        else:
206 #            if self.tooltip:
207 #                self.SetToolTipString(self.tooltip)
208         self.Refresh()
209         
210     def isEnabled(self):
211         return self.enabled
212     
213     def setBackground(self, wxColor):
214         self.backgroundColor = wxColor
215         self.Refresh()
216         
217     def OnPaint(self, evt):
218         dc = wx.BufferedPaintDC(self)
219         dc.SetBackground(wx.Brush(self.backgroundColor))
220         dc.Clear()
221         
222         if self.parentBitmap:
223             dc.DrawBitmap(self.parentBitmap, 0,0, True)
224         else:
225             self.parentBitmap = self.getParentBitmap()
226             if self.parentBitmap:
227                 dc.DrawBitmap(self.parentBitmap, 0,0, True)
228         
229         if not self.enabled:
230             return
231
232
233         if self.selected == 2:
234             dc.DrawBitmap(self.bitmaps[2], 0,0, True)
235             return
236
237
238         if self.bitmaps[0]:
239             dc.DrawBitmap(self.bitmaps[0], 0,0, True)
240         if (self.mouseOver or self.selected) and self.bitmaps[1]:
241             dc.DrawBitmap(self.bitmaps[1], 0,0, True)
242         
243
244 class PlayerSwitchButton(PlayerButton):
245         
246     def __init__(self, parent, imagedir, imagename, imagename2):
247         self.imagedir = imagedir
248         self.imagename = imagename
249         self.imagename2 = imagename2
250         self.parent = parent
251         self.init()
252         
253     def searchBitmaps(self):
254         self.toggled = False
255         self.allBitmaps = [None, None, None, None, None, None]
256         self.parentBitmap = None
257         self.mouseOver = False
258                 
259                     
260         if not os.path.isdir(self.imagedir):
261             print >>sys.stderr,'PlayerSwitchButton: Error: no image directory found in',self.imagedir
262             return
263         
264         # find a file with same name as this panel
265         self.bitmapPath = [os.path.join(self.imagedir, self.imagename+'.png'), 
266                            os.path.join(self.imagedir, self.imagename+'_hover.png'),
267                            os.path.join(self.imagedir, self.imagename+'_dis.png'),
268                            os.path.join(self.imagedir, self.imagename2+'.png'), 
269                            os.path.join(self.imagedir, self.imagename2+'_hover.png'),
270                            os.path.join(self.imagedir, self.imagename2+'_dis.png')
271                            ]
272         
273         i = 0
274         for img in self.bitmapPath:
275             if os.path.isfile(img):
276                 self.allBitmaps[i] = wx.Bitmap(img, wx.BITMAP_TYPE_ANY)
277                 i+=1
278             elif DEBUG:
279                 print 'Could not find image: %s' % img
280                 
281
282         if self.toggled:
283             self.bitmaps = self.allBitmaps[3:]
284         else:
285             self.bitmaps = self.allBitmaps[:3]
286                 
287     def setToggled(self, b, tooltip = { "enabled": "", "disabled": ""}):
288         self.toggled = b
289
290         if not self.initDone:
291             return
292
293         if b:
294             self.bitmaps=self.allBitmaps[3:]
295             if self.enabled:
296                 self.SetToolTipString(tooltip["enabled"])
297         else:
298             self.bitmaps=self.allBitmaps[:3]
299             if self.enabled:
300                 self.SetToolTipString(tooltip["disabled"])
301             
302         #print 'Bitmaps is now: %s' % self.bitmaps
303         #should Refresh?
304         self.Refresh()
305         
306     def isToggled(self):
307         return self.toggled
308
309
310
311 ##class VolumeButton(PlayerButton):
312
313
314
315
316