]>
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 *
11 class wxFloatBar(wxToolBar
):
13 wxToolBar subclass which can be dragged off its frame and later
14 replaced there. Drag on the toolbar to release it, close it like
15 a normal window to make it return to its original
16 position. Programmatically, call SetFloatable(true) and then
17 Float(true) to float, Float(false) to dock.
20 def __init__(self
,*_args
,**_kwargs
):
22 In addition to the usual arguments, wxFloatBar accepts keyword
23 args of: title(string): the title that should appear on the
24 toolbar's frame when it is floating. floatable(bool): whether
25 user actions (i.e., dragging) can float the toolbar or not.
27 args
= (self
,) + _args
28 apply(wxToolBar
.__init
__, args
, _kwargs
)
29 if _kwargs
.has_key('floatable'):
30 self
.floatable
= _kwargs
['floatable']
31 assert type(self
.floatable
) == type(0)
35 if _kwargs
.has_key('title'):
36 self
.title
= _kwargs
['title']
37 assert type(self
.title
) == type("")
40 EVT_MOUSE_EVENTS(self
, self
.OnMouse
)
41 self
.parentframe
= wxPyTypeCast(args
[1], 'wxFrame')
43 def IsFloatable(self
):
46 def SetFloatable(self
, float):
47 self
.floatable
= float
48 #Find the size of a title bar.
49 if not hasattr(self
, 'titleheight'):
50 test
= wxFrame(NULL
, -1, "TEST")
51 test
.SetClientSize(wxSize(0,0))
52 self
.titleheight
= test
.GetSizeTuple()[1]
59 wxToolBar
.Realize(self
)
65 def SetTitle(self
, title
):
68 self
.floatframe
.SetTitle(self
.title
)
72 Returns the frame which this toolbar will return to when
73 docked, or the parent if currently docked.
75 if hasattr(self
, 'parentframe'):
76 return self
.parentframe
78 return wxPyTypeCast(self
.GetParent(), 'wxFrame')
80 def SetHome(self
, frame
):
82 Called when docked, this will remove the toolbar from its
83 current frame and attach it to another. If called when
84 floating, it will dock to the frame specified when the toolbar
88 self
.parentframe
= frame
89 self
.floatframe
.Reparent(frame
)
91 parent
= wxPyTypeCast(self
.GetParent(), 'wxFrame')
93 parent
.SetToolBar(None)
94 size
= parent
.GetSize()
95 parent
.SetSize(wxSize(0,0))
97 frame
.SetToolBar(self
)
98 size
= frame
.GetSize()
99 frame
.SetSize(wxSize(0,0))
102 def Float(self
, bool):
103 "Floats or docks the toolbar programmatically."
105 self
.parentframe
= wxPyTypeCast(self
.GetParent(), 'wxFrame')
106 clientsize
= self
.parentframe
.GetClientSizeTuple()
108 useStyle
= wxDEFAULT_FRAME_STYLE
110 useStyle
= 0 #wxTHICK_FRAME
111 self
.floatframe
= wxMiniFrame(self
.parentframe
, -1, self
.title
,
113 self
.Reparent(self
.floatframe
)
114 self
.parentframe
.SetToolBar(None)
116 size
= self
.parentframe
.GetSize()
117 self
.parentframe
.SetSize(wxSize(0,0))
118 self
.parentframe
.SetSize(size
)
119 self
.floatframe
.SetToolBar(self
)
120 self
.oldcolor
= self
.GetBackgroundColour()
121 barsize
= self
.GetSizeTuple()
122 self
.floatframe
.SetSize(wxSize(barsize
[0], barsize
[1] + self
.titleheight
))
123 self
.floatframe
.SetClientSize(wxSize(barsize
[0], barsize
[1]))
124 newpos
= self
.parentframe
.GetPosition()
125 newpos
.y
= newpos
.y
+ self
.titleheight
126 self
.floatframe
.SetPosition(newpos
)
127 self
.floatframe
.Show(true
)
128 EVT_CLOSE(self
.floatframe
, self
.OnDock
)
129 EVT_MOVE(self
.floatframe
, self
.OnMove
)
131 self
.Reparent(self
.parentframe
)
132 self
.parentframe
.SetToolBar(self
)
134 self
.floatframe
.Destroy()
135 size
= self
.parentframe
.GetSize()
136 self
.parentframe
.SetSize(wxSize(0,0))
137 self
.parentframe
.SetSize(size
)
138 self
.SetBackgroundColour(self
.oldcolor
)
142 if hasattr(self
, 'oldpos'):
146 homepos
= self
.parentframe
.GetPositionTuple()
147 homepos
= homepos
[0], homepos
[1] + self
.titleheight
148 floatpos
= self
.floatframe
.GetPositionTuple()
149 if abs(homepos
[0] - floatpos
[0]) < 35 and abs(homepos
[1] - floatpos
[1]) < 35:
150 self
._SetFauxBarVisible
(true
)
152 self
._SetFauxBarVisible
(false
)
154 def OnMouse(self
, e
):
155 if not self
.IsFloatable():
158 if e
.ButtonDown() or e
.ButtonUp() or e
.ButtonDClick(1) or e
.ButtonDClick(2) or e
.ButtonDClick(3):
162 self
.oldpos
= (e
.GetX(), e
.GetY())
164 self
.oldpos
= (e
.GetX(), e
.GetY())
167 if self
.IsFloating():
168 homepos
= self
.parentframe
.GetPositionTuple()
169 homepos
= homepos
[0], homepos
[1] + self
.titleheight
170 floatpos
= self
.floatframe
.GetPositionTuple()
171 if abs(homepos
[0]-floatpos
[0]) < 25 and abs(homepos
[1]-floatpos
[1]) < 25:
174 if self
.IsFloatable():
176 if not self
.IsFloating():
178 self
.oldpos
= (e
.GetX(), e
.GetY())
180 if hasattr(self
, 'oldpos'):
181 loc
= self
.floatframe
.GetPosition()
182 pt
= wxPoint(loc
.x
- (self
.oldpos
[0]-e
.GetX()), loc
.y
- (self
.oldpos
[1]-e
.GetY()))
183 self
.floatframe
.SetPosition(pt
)
185 def _SetFauxBarVisible(self
, vis
):
188 if self
.parentframe
.GetToolBar() == None:
189 if not hasattr(self
, 'nullbar'):
190 self
.nullbar
= wxToolBar(self
.parentframe
, -1)
191 print "Adding fauxbar."
192 self
.nullbar
.Reparent(self
.parentframe
)
194 self
.parentframe
.SetToolBar(self
.nullbar
)
196 col
= wxNamedColour("GREY")
197 self
.nullbar
.SetBackgroundColour(col
)
199 size
= self
.parentframe
.GetSize()
200 self
.parentframe
.SetSize(wxSize(0,0))
201 self
.parentframe
.SetSize(size
)
204 print self
.parentframe
.GetToolBar()
206 if self
.parentframe
.GetToolBar() != None:
207 print "Removing fauxbar"
208 self
.nullbar
.Reparent(self
.floatframe
)
209 self
.parentframe
.SetToolBar(None)
210 size
= self
.parentframe
.GetSize()
211 self
.parentframe
.SetSize(wxSize(0,0))
212 self
.parentframe
.SetSize(size
)