1 | |
---|
2 | import wx |
---|
3 | from wx.lib.popupctl import PopButton |
---|
4 | |
---|
5 | class GuiPopButton(PopButton): |
---|
6 | def __init__(self, parent, *args, **kwrds): |
---|
7 | PopButton.__init__(self, parent, *args, **kwrds) |
---|
8 | self.parent = parent |
---|
9 | self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) |
---|
10 | |
---|
11 | def OnLeftDown(self, event): |
---|
12 | if not self.IsEnabled(): |
---|
13 | return |
---|
14 | self.didDown = True |
---|
15 | self.up = False |
---|
16 | self.CaptureMouse() |
---|
17 | self.parent.SetFocus() |
---|
18 | self.Refresh() |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | class BitmapPopUpButton(wx.PyControl): |
---|
23 | def __init__(self, parent, gui_bitmap=None, gui_size=(-1, -1),*args, **kwrds): |
---|
24 | if kwrds.has_key('value'): |
---|
25 | del kwrds['value'] |
---|
26 | style = kwrds.get('style', 0) |
---|
27 | if (style & wx.BORDER_MASK) == 0: |
---|
28 | style |= wx.BORDER_NONE |
---|
29 | kwrds['style'] = wx.BORDER_NONE |
---|
30 | wx.PyControl.__init__(self, parent, *args, **kwrds) |
---|
31 | if gui_bitmap is None: |
---|
32 | gui_bitmap = wx.NullBitmap |
---|
33 | self._button = wx.BitmapButton(parent=self, id=-1, |
---|
34 | bitmap=gui_bitmap, size=gui_size) |
---|
35 | self.bCtrl = GuiPopButton(self, wx.ID_ANY, style=wx.BORDER_NONE) |
---|
36 | print "hello" |
---|
37 | sizer = wx.GridBagSizer() |
---|
38 | self.SetSizer(sizer) |
---|
39 | sizer.Add(self._button, pos=(0, 0)) |
---|
40 | sizer.Add(self.bCtrl, pos=(0, 1)) |
---|
41 | |
---|
42 | self.pop = None |
---|
43 | self.content = None |
---|
44 | |
---|
45 | self.Bind(wx.EVT_SIZE, self.OnSize) |
---|
46 | self.bCtrl.Bind(wx.EVT_BUTTON, self.OnButton, self.bCtrl) |
---|
47 | self.Bind(wx.EVT_SET_FOCUS, self.OnFocus) |
---|
48 | |
---|
49 | self.SetInitialSize(kwrds.get('size', wx.DefaultSize)) |
---|
50 | self.Layout() |
---|
51 | self.SendSizeEvent() |
---|
52 | |
---|
53 | def Create(self, parent, id, bitmap, pos, size, style, validator, name): |
---|
54 | """ |
---|
55 | Acutally create the GUI BitmapButton for 2-phase creation. |
---|
56 | """ |
---|
57 | raise NotImplemented |
---|
58 | |
---|
59 | def GetBitmapDisabled(self): |
---|
60 | """ |
---|
61 | Returns the bitmap for the disabled state |
---|
62 | """ |
---|
63 | return self._button.GetBitmapDisabled() |
---|
64 | |
---|
65 | def GetBitmapFocus(self): |
---|
66 | """ |
---|
67 | Returns the bitmap for the focused state. |
---|
68 | """ |
---|
69 | return self._button.GetBitmapFocus() |
---|
70 | |
---|
71 | def GetBitmapHover(self): |
---|
72 | """ |
---|
73 | Returns the bitmap used when |
---|
74 | the mouse is over the button, may be invalid. |
---|
75 | """ |
---|
76 | return self._button.GetBitmapHover() |
---|
77 | |
---|
78 | def GetBitmapLabel(self): |
---|
79 | """ |
---|
80 | Returns the label bitmap (the one passed to the constructor). |
---|
81 | """ |
---|
82 | return self._button.GetBitmapLabel() |
---|
83 | |
---|
84 | def GetBitmapSelected(self) : |
---|
85 | """ |
---|
86 | Returns the bitmap for the selected state. |
---|
87 | """ |
---|
88 | return self._button.GetBitmapSelected() |
---|
89 | |
---|
90 | def GetMarginX(self): |
---|
91 | """ |
---|
92 | """ |
---|
93 | return self._button.GetMarginX() |
---|
94 | |
---|
95 | def GetMarginY(self): |
---|
96 | """ |
---|
97 | """ |
---|
98 | return self._button.GetMarginY() |
---|
99 | |
---|
100 | def SetBitmapDisabled(self, bitmap): |
---|
101 | """ |
---|
102 | Sets the bitmap for the disabled button appearance. |
---|
103 | """ |
---|
104 | def SetBitmapFocus(self, bitmap): |
---|
105 | """ |
---|
106 | Sets the bitmap for the button appearance |
---|
107 | when it has the keyboard focus. |
---|
108 | """ |
---|
109 | self._button.SetBitmapFocus(bitmap) |
---|
110 | |
---|
111 | def SetBitmapHover(self, hover): |
---|
112 | """ |
---|
113 | Sets the bitmap to be shown when the mouse is over the button. |
---|
114 | """ |
---|
115 | self._button.SetBitmapHover(hover) |
---|
116 | |
---|
117 | def SetBitmapLabel(self, bitmap): |
---|
118 | """Sets the bitmap label for the button. |
---|
119 | """ |
---|
120 | self._button.SetBitmapLabel(bitmap) |
---|
121 | |
---|
122 | def SetBitmapSelected(self, bitmap): |
---|
123 | """ |
---|
124 | """ |
---|
125 | self._button.SetBitmapSelected(bitmap) |
---|
126 | |
---|
127 | def SetMargins(self, x, y): |
---|
128 | """ |
---|
129 | """ |
---|
130 | self._button.SetMargins(x, y) |
---|
131 | |
---|
132 | def OnFocus(self,evt): |
---|
133 | # embedded control should get focus on TAB keypress |
---|
134 | self._button.SetFocus() |
---|
135 | evt.Skip() |
---|
136 | |
---|
137 | def OnSize(self, evt): |
---|
138 | # layout the child widgets |
---|
139 | w,h = self.GetClientSize() |
---|
140 | self._button.SetDimensions(0, 0, |
---|
141 | w - self.marginWidth - self.buttonWidth, h) |
---|
142 | self.bCtrl.SetDimensions(w - self.buttonWidth, 0, self.buttonWidth, h) |
---|
143 | |
---|
144 | def DoGetBestSize(self): |
---|
145 | # calculate the best size of the combined control based on the |
---|
146 | # needs of the child widgets. |
---|
147 | tbs = self._button.GetBestSize() |
---|
148 | return wx.Size(tbs.width + self.marginWidth + self.buttonWidth, |
---|
149 | tbs.height) |
---|
150 | def OnButton(self, evt): |
---|
151 | if not self.pop: |
---|
152 | if self.content: |
---|
153 | self.pop = PopupDialog(self, self.content) |
---|
154 | del self.content |
---|
155 | else: |
---|
156 | print 'No Content to pop' |
---|
157 | if self.pop: |
---|
158 | self.pop.Display() |
---|
159 | |
---|
160 | def Enable(self, flag): |
---|
161 | wx.PyControl.Enable(self,flag) |
---|
162 | self._button.Enable(flag) |
---|
163 | self.bCtrl.Enable(flag) |
---|
164 | |
---|
165 | def SetPopupContent(self, content): |
---|
166 | if not self.pop: |
---|
167 | self.content = content |
---|
168 | self.content.Show(False) |
---|
169 | else: |
---|
170 | self.pop.SetContent(content) |
---|
171 | |
---|
172 | def FormatContent(self): |
---|
173 | pass |
---|
174 | |
---|
175 | def PopDown(self): |
---|
176 | if self.pop: |
---|
177 | self.pop.EndModal(1) |
---|
178 | |
---|
179 | def SetBitmapDisabled(self, bitmap): |
---|
180 | self._button.SetBitmapDisabled(bitmap) |
---|
181 | |
---|
182 | def SetMargins(self, x, y): |
---|
183 | self._button.SetMargins(x, y) |
---|
184 | |
---|
185 | def SetFont(self, font): |
---|
186 | self._button.SetFont(font) |
---|
187 | |
---|
188 | def GetFont(self): |
---|
189 | return self._button.GetFont() |
---|
190 | |
---|
191 | def _get_marginWidth(self): |
---|
192 | if 'wxMac' in wx.PlatformInfo: |
---|
193 | return 6 |
---|
194 | else: |
---|
195 | return 3 |
---|
196 | marginWidth = property(_get_marginWidth) |
---|
197 | |
---|
198 | def _get_buttonWidth(self): |
---|
199 | return 20 |
---|
200 | buttonWidth = property(_get_buttonWidth) |
---|