]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/floatbar.py
1 #----------------------------------------------------------------------------
3 # Purpose: Contains floating toolbar class
8 #----------------------------------------------------------------------------
9 from wxPython
.wx
import *
13 class wxFloatBar(wxToolBar
):
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.
22 def __init__(self
,*_args
,**_kwargs
):
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.
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)
37 if _kwargs
.has_key('title'):
38 self
.title
= _kwargs
['title']
39 assert type(self
.title
) == type("")
42 EVT_MOUSE_EVENTS(self
, self
.OnMouse
)
43 self
.parentframe
= wxPyTypeCast(args
[1], 'wxFrame')
46 def IsFloatable(self
):
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]
65 wxToolBar
.Realize(self
)
72 def SetTitle(self
, title
):
75 self
.floatframe
.SetTitle(self
.title
)
80 ## Returns the frame which this toolbar will return to when
81 ## docked, or the parent if currently docked.
83 ## if hasattr(self, 'parentframe'):
84 ## return self.parentframe
86 ## return wxPyTypeCast(self.GetParent(), 'wxFrame')
89 ## def SetHome(self, frame):
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
96 ## if self.IsFloating():
97 ## self.parentframe = frame
98 ## self.floatframe.Reparent(frame)
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)
112 def Float(self
, bool):
113 "Floats or docks the toolbar programmatically."
115 self
.parentframe
= wxPyTypeCast(self
.GetParent(), 'wxFrame')
117 useStyle
= wxDEFAULT_FRAME_STYLE
119 useStyle
= 0 #wxTHICK_FRAME
120 self
.floatframe
= wxMiniFrame(self
.parentframe
, -1, self
.title
,
123 self
.Reparent(self
.floatframe
)
124 self
.parentframe
.SetToolBar(None)
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()
133 h
= self
.GetSize().height
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
)
143 EVT_CLOSE(self
.floatframe
, self
.OnDock
)
144 #EVT_MOVE(self.floatframe, self.OnMove)
147 self
.Reparent(self
.parentframe
)
148 self
.parentframe
.SetToolBar(self
)
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
)
160 if hasattr(self
, 'oldpos'):
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
):
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)
176 # self._SetFauxBarVisible(false)
179 def OnMouse(self
, e
):
180 if not self
.IsFloatable():
183 if e
.ButtonDown() or e
.ButtonUp() or e
.ButtonDClick(1) or e
.ButtonDClick(2) or e
.ButtonDClick(3):
187 self
.oldpos
= (e
.GetX(), e
.GetY())
189 self
.oldpos
= (e
.GetX(), e
.GetY())
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
):
199 if self
.IsFloatable():
201 if not self
.IsFloating():
203 self
.oldpos
= (e
.GetX(), e
.GetY())
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
)
211 def _SetFauxBarVisible(self
, 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
)
220 self
.parentframe
.SetToolBar(self
.nullbar
)
222 col
= wxNamedColour("GREY")
223 self
.nullbar
.SetBackgroundColour(col
)
225 size
= self
.parentframe
.GetSize()
226 self
.parentframe
.SetSize(wxSize(0,0))
227 self
.parentframe
.SetSize(size
)
230 print self
.parentframe
.GetToolBar()
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
)