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