]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/floatbar.py
wxFloatbar fixes
[wxWidgets.git] / utils / wxPython / lib / floatbar.py
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
11 _DOCKTHRESHOLD = 25
12
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 """
21
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')
44
45
46 def IsFloatable(self):
47 return self.floatable
48
49
50 def SetFloatable(self, float):
51 self.floatable = float
52 #Find the size of a title bar.
53 if not hasattr(self, 'titleheight'):
54 test = wxMiniFrame(NULL, -1, "TEST")
55 test.SetClientSize(wxSize(0,0))
56 self.titleheight = test.GetSizeTuple()[1]
57 test.Destroy()
58
59
60 def IsFloating(self):
61 return self.floating
62
63
64 def Realize(self):
65 wxToolBar.Realize(self)
66
67
68 def GetTitle(self):
69 return self.title
70
71
72 def SetTitle(self, title):
73 print 'SetTitle', title
74 self.title = title
75 if self.IsFloating():
76 self.floatframe.SetTitle(self.title)
77
78
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
112
113 def Float(self, bool):
114 "Floats or docks the toolbar programmatically."
115 if bool:
116 self.parentframe = wxPyTypeCast(self.GetParent(), 'wxFrame')
117 print self.title
118 if self.title:
119 useStyle = wxDEFAULT_FRAME_STYLE
120 else:
121 useStyle = wxTHICK_FRAME
122 self.floatframe = wxFrame(self.parentframe, -1, self.title,
123 style = useStyle)
124
125 self.Reparent(self.floatframe)
126 self.parentframe.SetToolBar(None)
127 self.floating = 1
128 psize = self.parentframe.GetSize()
129 self.parentframe.SetSize(wxSize(0,0))
130 self.parentframe.SetSize(psize)
131 self.floatframe.SetToolBar(self)
132 self.oldcolor = self.GetBackgroundColour()
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())
140 newpos = self.parentframe.GetPosition()
141 newpos.y = newpos.y + _DOCKTHRESHOLD * 2
142 self.floatframe.SetPosition(newpos)
143 self.floatframe.Show(true)
144
145 EVT_CLOSE(self.floatframe, self.OnDock)
146 #EVT_MOVE(self.floatframe, self.OnMove)
147
148 else:
149 self.Reparent(self.parentframe)
150 self.parentframe.SetToolBar(self)
151 self.floating = 0
152 self.floatframe.SetToolBar(None)
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)
158
159
160 def OnDock(self, e):
161 self.Float(0)
162 if hasattr(self, 'oldpos'):
163 del self.oldpos
164
165
166 def OnMove(self, e):
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
180
181 def OnMouse(self, e):
182 if not self.IsFloatable():
183 e.Skip()
184 return
185
186 if e.ButtonDClick(1) or e.ButtonDClick(2) or e.ButtonDClick(3) or e.ButtonDown() or e.ButtonUp():
187 e.Skip()
188
189 if e.ButtonDown():
190 self.CaptureMouse()
191 self.oldpos = (e.GetX(), e.GetY())
192
193 if e.Entering():
194 self.oldpos = (e.GetX(), e.GetY())
195
196 if e.ButtonUp():
197 self.ReleaseMouse()
198 if self.IsFloating():
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):
203 self.Float(0)
204 return
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
216
217
218 def _SetFauxBarVisible(self, vis):
219 return
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