]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/foldmenu.py
Fixed the code to actually work.
[wxWidgets.git] / wxPython / wx / lib / foldmenu.py
1
2 import wx
3 from wx.lib.evtmgr import eventManager
4
5 class FoldOutWindow(wx.PopupWindow):
6 def __init__(self,parent,style=0):
7 wx.PopupWindow.__init__(self,parent,style)
8 self.SetAutoLayout(True)
9 self.sizer=wx.BoxSizer(wx.HORIZONTAL)
10 self.SetSizer(self.sizer, deleteOld=False)
11 self.handlers={}
12 self.InitColors()
13 self.inWindow=False
14 wx.EVT_ENTER_WINDOW(self,self.evEnter)
15 wx.EVT_LEAVE_WINDOW(self,self.evLeave)
16
17 def InitColors(self):
18 faceClr = wx.SystemSettings_GetSystemColour(wx.SYS_COLOUR_WINDOW)
19 self.SetBackgroundColour(faceClr)
20
21 def AddButton(self,bitmap,handler=None):
22 id=wx.NewId()
23 btn=wx.BitmapButton(self,id,bitmap)
24 self.sizer.Add(btn, 1, wx.ALIGN_CENTER|wx.ALL|wx.EXPAND, 2)
25 wx.EVT_BUTTON(self,id,self.OnBtnClick)
26 self.sizer.Fit(self)
27 self.Layout()
28 if handler:
29 self.handlers[id]=handler
30 return id
31
32 def Popup(self):
33 if not self.IsShown():
34 self.Show()
35
36 def OnBtnClick(self,event):
37 id=event.GetEventObject().GetId()
38 if self.handlers.has_key(id):
39 self.handlers[id](event)
40 self.Hide()
41 self.inWindow=False
42 event.Skip()
43
44 def evEnter(self,event):
45 self.inWindow=True
46 self.rect=self.GetRect()
47 event.Skip()
48
49 def evLeave(self,event):
50 if self.inWindow:
51 if not self.rect.Inside(self.ClientToScreen(event.GetPosition())):
52 self.Hide()
53 event.Skip()
54
55
56
57
58
59 class FoldOutMenu(wx.BitmapButton):
60 def __init__(self,parent,id,bitmap,pos = wx.DefaultPosition,
61 size = wx.DefaultSize, style = wx.BU_AUTODRAW,
62 validator = wx.DefaultValidator, name = "button"):
63 wx.BitmapButton.__init__(self, parent, id, bitmap, pos, size, style,
64 validator, name)
65 self.parent=parent
66 wx.EVT_BUTTON(self.parent, self.GetId(), self.click)
67 self.popwin=FoldOutWindow(self.parent)
68
69 def AddButton(self,bitmap,handler=None):
70 return self.popwin.AddButton(bitmap,handler=handler)
71
72 def click(self,event):
73 pos=self.GetPosition()
74 sz=self.GetSize()
75 pos.x=pos.x+sz.width
76 pos.y=pos.y+sz.height/2
77 self.popwin.Position(pos,sz)
78 self.popwin.Popup()