]> git.saurik.com Git - wxWidgets.git/blame_incremental - utils/wxPython/lib/floatbar.py
Unicode compilation fix
[wxWidgets.git] / utils / wxPython / lib / floatbar.py
... / ...
CommitLineData
1#----------------------------------------------------------------------------
2# Name: floatbar.py
3# Purpose: Contains floating toolbar class
4#
5# Author: Bryn Keller
6#
7# Created: 10/4/99
8#----------------------------------------------------------------------------
9from wxPython.wx import *
10
11_DOCKTHRESHOLD = 25
12
13class 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 self.title = title
74 if self.IsFloating():
75 self.floatframe.SetTitle(self.title)
76
77
78## def GetHome(self):
79## """
80## Returns the frame which this toolbar will return to when
81## docked, or the parent if currently docked.
82## """
83## if hasattr(self, 'parentframe'):
84## return self.parentframe
85## else:
86## return wxPyTypeCast(self.GetParent(), 'wxFrame')
87
88
89## def SetHome(self, frame):
90## """
91## Called when docked, this will remove the toolbar from its
92## current frame and attach it to another. If called when
93## floating, it will dock to the frame specified when the toolbar
94## window is closed.
95## """
96## if self.IsFloating():
97## self.parentframe = frame
98## self.floatframe.Reparent(frame)
99## else:
100## parent = wxPyTypeCast(self.GetParent(), 'wxFrame')
101## self.Reparent(frame)
102## parent.SetToolBar(None)
103## size = parent.GetSize()
104## parent.SetSize(wxSize(0,0))
105## parent.SetSize(size)
106## frame.SetToolBar(self)
107## size = frame.GetSize()
108## frame.SetSize(wxSize(0,0))
109## frame.SetSize(size)
110
111
112 def Float(self, bool):
113 "Floats or docks the toolbar programmatically."
114 if bool:
115 self.parentframe = wxPyTypeCast(self.GetParent(), 'wxFrame')
116 if self.title:
117 useStyle = wxDEFAULT_FRAME_STYLE
118 else:
119 useStyle = 0 #wxTHICK_FRAME
120 self.floatframe = wxMiniFrame(self.parentframe, -1, self.title,
121 style = useStyle)
122
123 self.Reparent(self.floatframe)
124 self.parentframe.SetToolBar(None)
125 self.floating = 1
126 psize = self.parentframe.GetSize()
127 self.parentframe.SetSize(wxSize(0,0))
128 self.parentframe.SetSize(psize)
129 self.floatframe.SetToolBar(self)
130 self.oldcolor = self.GetBackgroundColour()
131
132 w = psize.width
133 h = self.GetSize().height
134 if self.title:
135 h = h + self.titleheight
136 self.floatframe.SetSize(wxSize(w,h))
137 self.floatframe.SetClientSize(self.GetSize())
138 newpos = self.parentframe.GetPosition()
139 newpos.y = newpos.y + _DOCKTHRESHOLD * 2
140 self.floatframe.SetPosition(newpos)
141 self.floatframe.Show(true)
142
143 EVT_CLOSE(self.floatframe, self.OnDock)
144 #EVT_MOVE(self.floatframe, self.OnMove)
145
146 else:
147 self.Reparent(self.parentframe)
148 self.parentframe.SetToolBar(self)
149 self.floating = 0
150 self.floatframe.SetToolBar(None)
151 self.floatframe.Destroy()
152 size = self.parentframe.GetSize()
153 self.parentframe.SetSize(wxSize(0,0))
154 self.parentframe.SetSize(size)
155 self.SetBackgroundColour(self.oldcolor)
156
157
158 def OnDock(self, e):
159 self.Float(0)
160 if hasattr(self, 'oldpos'):
161 del self.oldpos
162
163
164 def OnMove(self, e):
165 homepos = self.parentframe.ClientToScreen(wxPoint(0,0))
166 floatpos = self.floatframe.GetPosition()
167 if (abs(homepos.x - floatpos.x) < _DOCKTHRESHOLD and
168 abs(homepos.y - floatpos.y) < _DOCKTHRESHOLD):
169 self.Float(0)
170 #homepos = self.parentframe.GetPositionTuple()
171 #homepos = homepos[0], homepos[1] + self.titleheight
172 #floatpos = self.floatframe.GetPositionTuple()
173 #if abs(homepos[0] - floatpos[0]) < 35 and abs(homepos[1] - floatpos[1]) < 35:
174 # self._SetFauxBarVisible(true)
175 #else:
176 # self._SetFauxBarVisible(false)
177
178
179 def OnMouse(self, e):
180 if not self.IsFloatable():
181 e.Skip()
182 return
183 if e.ButtonDown() or e.ButtonUp() or e.ButtonDClick(1) or e.ButtonDClick(2) or e.ButtonDClick(3):
184 e.Skip()
185 if e.ButtonDown():
186 self.CaptureMouse()
187 self.oldpos = (e.GetX(), e.GetY())
188 if e.Entering():
189 self.oldpos = (e.GetX(), e.GetY())
190 if e.ButtonUp():
191 self.ReleaseMouse()
192 if self.IsFloating():
193 homepos = self.parentframe.ClientToScreen(wxPoint(0,0))
194 floatpos = self.floatframe.GetPosition()
195 if (abs(homepos.x - floatpos.x) < _DOCKTHRESHOLD and
196 abs(homepos.y - floatpos.y) < _DOCKTHRESHOLD):
197 self.Float(0)
198 return
199 if self.IsFloatable():
200 if e.Dragging():
201 if not self.IsFloating():
202 self.Float(true)
203 self.oldpos = (e.GetX(), e.GetY())
204 else:
205 if hasattr(self, 'oldpos'):
206 loc = self.floatframe.GetPosition()
207 pt = wxPoint(loc.x - (self.oldpos[0]-e.GetX()), loc.y - (self.oldpos[1]-e.GetY()))
208 self.floatframe.SetPosition(pt)
209
210
211 def _SetFauxBarVisible(self, vis):
212 return
213 if vis:
214 if self.parentframe.GetToolBar() == None:
215 if not hasattr(self, 'nullbar'):
216 self.nullbar = wxToolBar(self.parentframe, -1)
217 print "Adding fauxbar."
218 self.nullbar.Reparent(self.parentframe)
219 print "Reparented."
220 self.parentframe.SetToolBar(self.nullbar)
221 print "Set toolbar"
222 col = wxNamedColour("GREY")
223 self.nullbar.SetBackgroundColour(col)
224 print "Set color"
225 size = self.parentframe.GetSize()
226 self.parentframe.SetSize(wxSize(0,0))
227 self.parentframe.SetSize(size)
228 print "Set size"
229 else:
230 print self.parentframe.GetToolBar()
231 else:
232 if self.parentframe.GetToolBar() != None:
233 print "Removing fauxbar"
234 self.nullbar.Reparent(self.floatframe)
235 self.parentframe.SetToolBar(None)
236 size = self.parentframe.GetSize()
237 self.parentframe.SetSize(wxSize(0,0))
238 self.parentframe.SetSize(size)
239
240
241
242
243
244
245
246
247
248
249
250
251