]>
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
):
73 print 'SetTitle', title
76 self
.floatframe
.SetTitle(self
.title
)
81 ## Returns the frame which this toolbar will return to when
82 ## docked, or the parent if currently docked.
84 ## if hasattr(self, 'parentframe'):
85 ## return self.parentframe
87 ## return wxPyTypeCast(self.GetParent(), 'wxFrame')
90 ## def SetHome(self, frame):
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
97 ## if self.IsFloating():
98 ## self.parentframe = frame
99 ## self.floatframe.Reparent(frame)
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)
113 def Float(self
, bool):
114 "Floats or docks the toolbar programmatically."
116 self
.parentframe
= wxPyTypeCast(self
.GetParent(), 'wxFrame')
119 useStyle
= wxDEFAULT_FRAME_STYLE
121 useStyle
= wxTHICK_FRAME
122 self
.floatframe
= wxFrame(self
.parentframe
, -1, self
.title
,
125 self
.Reparent(self
.floatframe
)
126 self
.parentframe
.SetToolBar(None)
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()
135 h
= self
.GetSize().height
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
)
145 EVT_CLOSE(self
.floatframe
, self
.OnDock
)
146 #EVT_MOVE(self.floatframe, self.OnMove)
149 self
.Reparent(self
.parentframe
)
150 self
.parentframe
.SetToolBar(self
)
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
)
162 if hasattr(self
, 'oldpos'):
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
):
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)
178 # self._SetFauxBarVisible(false)
181 def OnMouse(self
, e
):
182 if not self
.IsFloatable():
186 if e
.ButtonDClick(1) or e
.ButtonDClick(2) or e
.ButtonDClick(3) or e
.ButtonDown() or e
.ButtonUp():
191 self
.oldpos
= (e
.GetX(), e
.GetY())
194 self
.oldpos
= (e
.GetX(), e
.GetY())
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
):
207 if not self
.IsFloating():
209 self
.oldpos
= (e
.GetX(), e
.GetY())
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
)
218 def _SetFauxBarVisible(self
, 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
)
227 self
.parentframe
.SetToolBar(self
.nullbar
)
229 col
= wxNamedColour("GREY")
230 self
.nullbar
.SetBackgroundColour(col
)
232 size
= self
.parentframe
.GetSize()
233 self
.parentframe
.SetSize(wxSize(0,0))
234 self
.parentframe
.SetSize(size
)
237 print self
.parentframe
.GetToolBar()
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
)