]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/floatbar.py
ce846b4a2280c2642ae3c0f0e89b48fd0da7f8e6
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.
19 def __init__(self
,*_args
,**_kwargs
):
21 In addition to the usual arguments, wxFloatBar accepts keyword
22 args of: title(string): the title that should appear on the
23 toolbar's frame when it is floating. floatable(bool): whether
24 user actions (i.e., dragging) can float the toolbar or not.
26 args
= (self
,) + _args
27 apply(wxToolBar
.__init
__, args
, _kwargs
)
28 if _kwargs
.has_key('floatable'):
29 self
.floatable
= _kwargs
['floatable']
30 assert type(self
.floatable
) == type(0)
34 if _kwargs
.has_key('title'):
35 self
.title
= _kwargs
['title']
36 assert type(self
.title
) == type("")
39 EVT_MOUSE_EVENTS(self
, self
.OnMouse
)
40 self
.parentframe
= wxPyTypeCast(args
[1], 'wxFrame')
41 def IsFloatable(self
):
43 def SetFloatable(self
, float):
44 self
.floatable
= float
45 #Find the size of a title bar.
46 if not hasattr(self
, 'titleheight'):
47 test
= wxFrame(NULL
, -1, "TEST")
48 test
.SetClientSize(wxSize(0,0))
49 self
.titleheight
= test
.GetSizeTuple()[1]
54 wxToolBar
.Realize(self
)
58 def SetTitle(self
, title
):
61 self
.floatframe
.SetTitle(self
.title
)
64 Returns the frame which this toolbar will return to when
65 docked, or the parent if currently docked.
67 if hasattr(self
, 'parentframe'):
68 return self
.parentframe
70 return wxPyTypeCast(self
.GetParent(), 'wxFrame')
71 def SetHome(self
, frame
):
73 Called when docked, this will remove the toolbar from its
74 current frame and attach it to another. If called when
75 floating, it will dock to the frame specified when the toolbar
79 self
.parentframe
= frame
80 self
.floatframe
.Reparent(frame
)
82 parent
= wxPyTypeCast(self
.GetParent(), 'wxFrame')
84 parent
.SetToolBar(None)
85 size
= parent
.GetSize()
86 parent
.SetSize(wxSize(0,0))
88 frame
.SetToolBar(self
)
89 size
= frame
.GetSize()
90 frame
.SetSize(wxSize(0,0))
92 def Float(self
, bool):
93 "Floats or docks the toolbar programmatically."
95 self
.parentframe
= wxPyTypeCast(self
.GetParent(), 'wxFrame')
96 clientsize
= self
.parentframe
.GetClientSizeTuple()
97 self
.floatframe
= wxMiniFrame(self
.parentframe
, -1, self
.title
, wxDefaultPosition
, wxDefaultSize
, wxTHICK_FRAME
)
98 self
.Reparent(self
.floatframe
)
99 self
.parentframe
.SetToolBar(None)
101 size
= self
.parentframe
.GetSize()
102 self
.parentframe
.SetSize(wxSize(0,0))
103 self
.parentframe
.SetSize(size
)
104 self
.floatframe
.SetToolBar(self
)
105 self
.oldcolor
= self
.GetBackgroundColour()
106 barsize
= self
.GetSizeTuple()
107 # self.floatframe.SetSize(wxSize(barsize[0], barsize[1] + self.titleheight))
108 # self.floatframe.SetClientSize(wxSize(barsize[0], barsize[1]))
109 self
.floatframe
.SetClientSize(wxSize(50,200))
110 newpos
= self
.parentframe
.GetPosition()
111 newpos
.y
= newpos
.y
+ self
.titleheight
112 self
.floatframe
.SetPosition(newpos
)
113 self
.floatframe
.Show(true
)
114 EVT_CLOSE(self
.floatframe
, self
.OnDock
)
115 # EVT_MOVE(self.floatframe, self.OnMove)
117 self
.Reparent(self
.parentframe
)
118 self
.parentframe
.SetToolBar(self
)
120 self
.floatframe
.Destroy()
121 size
= self
.parentframe
.GetSize()
122 self
.parentframe
.SetSize(wxSize(0,0))
123 self
.parentframe
.SetSize(size
)
124 self
.SetBackgroundColour(self
.oldcolor
)
127 if hasattr(self
, 'oldpos'):
131 homepos
= self
.parentframe
.GetPositionTuple()
132 homepos
= homepos
[0], homepos
[1] + self
.titleheight
133 floatpos
= self
.floatframe
.GetPositionTuple()
134 if abs(homepos
[0]-floatpos
[0]) < 35 and abs(homepos
[1]-floatpos
[1]) < 35:
135 self
._SetFauxBarVisible
(true
)
137 self
._SetFauxBarVisible
(false
)
139 def OnMouse(self
, e
):
140 if not self
.IsFloatable():
143 if e
.ButtonDown() or e
.ButtonUp() or e
.ButtonDClick(1) or e
.ButtonDClick(2) or e
.ButtonDClick(3):
146 self
.oldpos
= (e
.GetX(), e
.GetY())
148 self
.oldpos
= (e
.GetX(), e
.GetY())
150 if self
.IsFloating():
151 homepos
= self
.parentframe
.GetPositionTuple()
152 homepos
= homepos
[0], homepos
[1] + self
.titleheight
153 floatpos
= self
.floatframe
.GetPositionTuple()
154 if abs(homepos
[0]-floatpos
[0]) < 25 and abs(homepos
[1]-floatpos
[1]) < 25:
157 if self
.IsFloatable():
159 if not self
.IsFloating():
161 self
.oldpos
= (e
.GetX(), e
.GetY())
163 if hasattr(self
, 'oldpos'):
164 loc
= self
.floatframe
.GetPosition()
165 pt
= wxPoint(loc
.x
- (self
.oldpos
[0]-e
.GetX()), loc
.y
- (self
.oldpos
[1]-e
.GetY()))
166 self
.floatframe
.SetPosition(pt
)
168 def _SetFauxBarVisible(self
, vis
):
171 if self
.parentframe
.GetToolBar() == None:
172 if not hasattr(self
, 'nullbar'):
173 self
.nullbar
= wxToolBar(self
.parentframe
, -1)
174 print "Adding fauxbar."
175 self
.nullbar
.Reparent(self
.parentframe
)
177 self
.parentframe
.SetToolBar(self
.nullbar
)
179 col
= wxNamedColour("GREY")
180 self
.nullbar
.SetBackgroundColour(col
)
182 size
= self
.parentframe
.GetSize()
183 self
.parentframe
.SetSize(wxSize(0,0))
184 self
.parentframe
.SetSize(size
)
187 print self
.parentframe
.GetToolBar()
189 if self
.parentframe
.GetToolBar() != None:
190 print "Removing fauxbar"
191 self
.nullbar
.Reparent(self
.floatframe
)
192 self
.parentframe
.SetToolBar(None)
193 size
= self
.parentframe
.GetSize()
194 self
.parentframe
.SetSize(wxSize(0,0))
195 self
.parentframe
.SetSize(size
)