]>
Commit | Line | Data |
---|---|---|
f0261a72 RD |
1 | #---------------------------------------------------------------------------- |
2 | # Name: floatbar.py | |
3 | # Purpose: Contains floating toolbar class | |
4 | # | |
5 | # Author: Bryn Keller | |
6 | # | |
7 | # Created: 10/4/99 | |
8 | #---------------------------------------------------------------------------- | |
9 | from wxPython.wx import * | |
10 | ||
8c588298 RD |
11 | _DOCKTHRESHOLD = 25 |
12 | ||
f0261a72 RD |
13 | class wxFloatBar(wxToolBar): |
14 | """ | |
15 | wxToolBar subclass which can be dragged off its frame and later | |
16 | replaced there. Drag on the toolbar to release it, close it like | |
17 | a normal window to make it return to its original | |
18 | position. Programmatically, call SetFloatable(true) and then | |
19 | Float(true) to float, Float(false) to dock. | |
20 | """ | |
944930d5 | 21 | |
f0261a72 RD |
22 | def __init__(self,*_args,**_kwargs): |
23 | """ | |
24 | In addition to the usual arguments, wxFloatBar accepts keyword | |
25 | args of: title(string): the title that should appear on the | |
26 | toolbar's frame when it is floating. floatable(bool): whether | |
27 | user actions (i.e., dragging) can float the toolbar or not. | |
28 | """ | |
29 | args = (self,) + _args | |
30 | apply(wxToolBar.__init__, args, _kwargs) | |
31 | if _kwargs.has_key('floatable'): | |
32 | self.floatable = _kwargs['floatable'] | |
33 | assert type(self.floatable) == type(0) | |
34 | else: | |
35 | self.floatable = 0 | |
36 | self.floating = 0 | |
37 | if _kwargs.has_key('title'): | |
38 | self.title = _kwargs['title'] | |
39 | assert type(self.title) == type("") | |
40 | else: | |
41 | self.title = "" | |
42 | EVT_MOUSE_EVENTS(self, self.OnMouse) | |
43 | self.parentframe = wxPyTypeCast(args[1], 'wxFrame') | |
944930d5 | 44 | |
8c588298 | 45 | |
f0261a72 RD |
46 | def IsFloatable(self): |
47 | return self.floatable | |
944930d5 | 48 | |
8c588298 | 49 | |
f0261a72 RD |
50 | def SetFloatable(self, float): |
51 | self.floatable = float | |
52 | #Find the size of a title bar. | |
53 | if not hasattr(self, 'titleheight'): | |
8c588298 | 54 | test = wxMiniFrame(NULL, -1, "TEST") |
f0261a72 RD |
55 | test.SetClientSize(wxSize(0,0)) |
56 | self.titleheight = test.GetSizeTuple()[1] | |
57 | test.Destroy() | |
944930d5 | 58 | |
8c588298 | 59 | |
f0261a72 RD |
60 | def IsFloating(self): |
61 | return self.floating | |
944930d5 | 62 | |
8c588298 | 63 | |
f0261a72 RD |
64 | def Realize(self): |
65 | wxToolBar.Realize(self) | |
8c588298 | 66 | |
944930d5 | 67 | |
f0261a72 RD |
68 | def GetTitle(self): |
69 | return self.title | |
944930d5 | 70 | |
8c588298 | 71 | |
f0261a72 | 72 | def SetTitle(self, title): |
b7e72427 | 73 | print 'SetTitle', title |
f0261a72 RD |
74 | self.title = title |
75 | if self.IsFloating(): | |
76 | self.floatframe.SetTitle(self.title) | |
944930d5 | 77 | |
944930d5 | 78 | |
8c588298 RD |
79 | ## def GetHome(self): |
80 | ## """ | |
81 | ## Returns the frame which this toolbar will return to when | |
82 | ## docked, or the parent if currently docked. | |
83 | ## """ | |
84 | ## if hasattr(self, 'parentframe'): | |
85 | ## return self.parentframe | |
86 | ## else: | |
87 | ## return wxPyTypeCast(self.GetParent(), 'wxFrame') | |
88 | ||
89 | ||
90 | ## def SetHome(self, frame): | |
91 | ## """ | |
92 | ## Called when docked, this will remove the toolbar from its | |
93 | ## current frame and attach it to another. If called when | |
94 | ## floating, it will dock to the frame specified when the toolbar | |
95 | ## window is closed. | |
96 | ## """ | |
97 | ## if self.IsFloating(): | |
98 | ## self.parentframe = frame | |
99 | ## self.floatframe.Reparent(frame) | |
100 | ## else: | |
101 | ## parent = wxPyTypeCast(self.GetParent(), 'wxFrame') | |
102 | ## self.Reparent(frame) | |
103 | ## parent.SetToolBar(None) | |
104 | ## size = parent.GetSize() | |
105 | ## parent.SetSize(wxSize(0,0)) | |
106 | ## parent.SetSize(size) | |
107 | ## frame.SetToolBar(self) | |
108 | ## size = frame.GetSize() | |
109 | ## frame.SetSize(wxSize(0,0)) | |
110 | ## frame.SetSize(size) | |
111 | ||
944930d5 | 112 | |
f0261a72 RD |
113 | def Float(self, bool): |
114 | "Floats or docks the toolbar programmatically." | |
115 | if bool: | |
116 | self.parentframe = wxPyTypeCast(self.GetParent(), 'wxFrame') | |
b7e72427 | 117 | print self.title |
944930d5 RD |
118 | if self.title: |
119 | useStyle = wxDEFAULT_FRAME_STYLE | |
120 | else: | |
b7e72427 RD |
121 | useStyle = wxTHICK_FRAME |
122 | self.floatframe = wxFrame(self.parentframe, -1, self.title, | |
944930d5 | 123 | style = useStyle) |
8c588298 | 124 | |
f0261a72 RD |
125 | self.Reparent(self.floatframe) |
126 | self.parentframe.SetToolBar(None) | |
127 | self.floating = 1 | |
8c588298 | 128 | psize = self.parentframe.GetSize() |
f0261a72 | 129 | self.parentframe.SetSize(wxSize(0,0)) |
8c588298 | 130 | self.parentframe.SetSize(psize) |
f0261a72 RD |
131 | self.floatframe.SetToolBar(self) |
132 | self.oldcolor = self.GetBackgroundColour() | |
8c588298 RD |
133 | |
134 | w = psize.width | |
135 | h = self.GetSize().height | |
136 | if self.title: | |
137 | h = h + self.titleheight | |
138 | self.floatframe.SetSize(wxSize(w,h)) | |
139 | self.floatframe.SetClientSize(self.GetSize()) | |
f0261a72 | 140 | newpos = self.parentframe.GetPosition() |
8c588298 | 141 | newpos.y = newpos.y + _DOCKTHRESHOLD * 2 |
f0261a72 RD |
142 | self.floatframe.SetPosition(newpos) |
143 | self.floatframe.Show(true) | |
8c588298 | 144 | |
f0261a72 | 145 | EVT_CLOSE(self.floatframe, self.OnDock) |
8c588298 RD |
146 | #EVT_MOVE(self.floatframe, self.OnMove) |
147 | ||
f0261a72 RD |
148 | else: |
149 | self.Reparent(self.parentframe) | |
150 | self.parentframe.SetToolBar(self) | |
151 | self.floating = 0 | |
8c588298 | 152 | self.floatframe.SetToolBar(None) |
f0261a72 RD |
153 | self.floatframe.Destroy() |
154 | size = self.parentframe.GetSize() | |
155 | self.parentframe.SetSize(wxSize(0,0)) | |
156 | self.parentframe.SetSize(size) | |
157 | self.SetBackgroundColour(self.oldcolor) | |
944930d5 | 158 | |
8c588298 | 159 | |
f0261a72 RD |
160 | def OnDock(self, e): |
161 | self.Float(0) | |
162 | if hasattr(self, 'oldpos'): | |
163 | del self.oldpos | |
164 | ||
8c588298 | 165 | |
f0261a72 | 166 | def OnMove(self, e): |
8c588298 RD |
167 | homepos = self.parentframe.ClientToScreen(wxPoint(0,0)) |
168 | floatpos = self.floatframe.GetPosition() | |
169 | if (abs(homepos.x - floatpos.x) < _DOCKTHRESHOLD and | |
170 | abs(homepos.y - floatpos.y) < _DOCKTHRESHOLD): | |
171 | self.Float(0) | |
172 | #homepos = self.parentframe.GetPositionTuple() | |
173 | #homepos = homepos[0], homepos[1] + self.titleheight | |
174 | #floatpos = self.floatframe.GetPositionTuple() | |
175 | #if abs(homepos[0] - floatpos[0]) < 35 and abs(homepos[1] - floatpos[1]) < 35: | |
176 | # self._SetFauxBarVisible(true) | |
177 | #else: | |
178 | # self._SetFauxBarVisible(false) | |
179 | ||
f0261a72 RD |
180 | |
181 | def OnMouse(self, e): | |
182 | if not self.IsFloatable(): | |
183 | e.Skip() | |
184 | return | |
b7e72427 RD |
185 | |
186 | if e.ButtonDClick(1) or e.ButtonDClick(2) or e.ButtonDClick(3) or e.ButtonDown() or e.ButtonUp(): | |
f0261a72 | 187 | e.Skip() |
b7e72427 | 188 | |
f0261a72 | 189 | if e.ButtonDown(): |
944930d5 | 190 | self.CaptureMouse() |
f0261a72 | 191 | self.oldpos = (e.GetX(), e.GetY()) |
b7e72427 | 192 | |
f0261a72 RD |
193 | if e.Entering(): |
194 | self.oldpos = (e.GetX(), e.GetY()) | |
b7e72427 | 195 | |
f0261a72 | 196 | if e.ButtonUp(): |
944930d5 | 197 | self.ReleaseMouse() |
f0261a72 | 198 | if self.IsFloating(): |
8c588298 RD |
199 | homepos = self.parentframe.ClientToScreen(wxPoint(0,0)) |
200 | floatpos = self.floatframe.GetPosition() | |
201 | if (abs(homepos.x - floatpos.x) < _DOCKTHRESHOLD and | |
202 | abs(homepos.y - floatpos.y) < _DOCKTHRESHOLD): | |
f0261a72 RD |
203 | self.Float(0) |
204 | return | |
b7e72427 RD |
205 | |
206 | if e.Dragging(): | |
207 | if not self.IsFloating(): | |
208 | self.Float(true) | |
209 | self.oldpos = (e.GetX(), e.GetY()) | |
210 | else: | |
211 | if hasattr(self, 'oldpos'): | |
212 | loc = self.floatframe.GetPosition() | |
213 | pt = wxPoint(loc.x - (self.oldpos[0]-e.GetX()), loc.y - (self.oldpos[1]-e.GetY())) | |
214 | self.floatframe.Move(pt) | |
215 | ||
f0261a72 | 216 | |
8c588298 | 217 | |
f0261a72 | 218 | def _SetFauxBarVisible(self, vis): |
944930d5 | 219 | return |
f0261a72 RD |
220 | if vis: |
221 | if self.parentframe.GetToolBar() == None: | |
222 | if not hasattr(self, 'nullbar'): | |
223 | self.nullbar = wxToolBar(self.parentframe, -1) | |
224 | print "Adding fauxbar." | |
225 | self.nullbar.Reparent(self.parentframe) | |
226 | print "Reparented." | |
227 | self.parentframe.SetToolBar(self.nullbar) | |
228 | print "Set toolbar" | |
229 | col = wxNamedColour("GREY") | |
230 | self.nullbar.SetBackgroundColour(col) | |
231 | print "Set color" | |
232 | size = self.parentframe.GetSize() | |
233 | self.parentframe.SetSize(wxSize(0,0)) | |
234 | self.parentframe.SetSize(size) | |
235 | print "Set size" | |
236 | else: | |
237 | print self.parentframe.GetToolBar() | |
238 | else: | |
239 | if self.parentframe.GetToolBar() != None: | |
240 | print "Removing fauxbar" | |
241 | self.nullbar.Reparent(self.floatframe) | |
242 | self.parentframe.SetToolBar(None) | |
243 | size = self.parentframe.GetSize() | |
244 | self.parentframe.SetSize(wxSize(0,0)) | |
245 | self.parentframe.SetSize(size) | |
246 | ||
247 | ||
248 | ||
249 | ||
250 | ||
251 | ||
252 | ||
253 | ||
254 | ||
255 | ||
256 | ||
257 | ||
258 |