]>
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.
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 newpos
= self
.parentframe
.GetPosition()
110 newpos
.y
= newpos
.y
+ self
.titleheight
111 self
.floatframe
.SetPosition(newpos
)
112 self
.floatframe
.Show(true
)
113 EVT_CLOSE(self
.floatframe
, self
.OnDock
)
114 # EVT_MOVE(self.floatframe, self.OnMove)
116 self
.Reparent(self
.parentframe
)
117 self
.parentframe
.SetToolBar(self
)
119 self
.floatframe
.Destroy()
120 size
= self
.parentframe
.GetSize()
121 self
.parentframe
.SetSize(wxSize(0,0))
122 self
.parentframe
.SetSize(size
)
123 self
.SetBackgroundColour(self
.oldcolor
)
126 if hasattr(self
, 'oldpos'):
130 homepos
= self
.parentframe
.GetPositionTuple()
131 homepos
= homepos
[0], homepos
[1] + self
.titleheight
132 floatpos
= self
.floatframe
.GetPositionTuple()
133 if abs(homepos
[0]-floatpos
[0]) < 35 and abs(homepos
[1]-floatpos
[1]) < 35:
134 self
._SetFauxBarVisible
(true
)
136 self
._SetFauxBarVisible
(false
)
138 def OnMouse(self
, e
):
139 if not self
.IsFloatable():
142 if e
.ButtonDown() or e
.ButtonUp() or e
.ButtonDClick(1) or e
.ButtonDClick(2) or e
.ButtonDClick(3):
145 self
.oldpos
= (e
.GetX(), e
.GetY())
147 self
.oldpos
= (e
.GetX(), e
.GetY())
149 if self
.IsFloating():
150 homepos
= self
.parentframe
.GetPositionTuple()
151 homepos
= homepos
[0], homepos
[1] + self
.titleheight
152 floatpos
= self
.floatframe
.GetPositionTuple()
153 if abs(homepos
[0]-floatpos
[0]) < 25 and abs(homepos
[1]-floatpos
[1]) < 25:
156 if self
.IsFloatable():
158 if not self
.IsFloating():
160 self
.oldpos
= (e
.GetX(), e
.GetY())
162 if hasattr(self
, 'oldpos'):
163 loc
= self
.floatframe
.GetPosition()
164 pt
= wxPoint(loc
.x
- (self
.oldpos
[0]-e
.GetX()), loc
.y
- (self
.oldpos
[1]-e
.GetY()))
165 self
.floatframe
.SetPosition(pt
)
167 def _SetFauxBarVisible(self
, vis
):
170 if self
.parentframe
.GetToolBar() == None:
171 if not hasattr(self
, 'nullbar'):
172 self
.nullbar
= wxToolBar(self
.parentframe
, -1)
173 print "Adding fauxbar."
174 self
.nullbar
.Reparent(self
.parentframe
)
176 self
.parentframe
.SetToolBar(self
.nullbar
)
178 col
= wxNamedColour("GREY")
179 self
.nullbar
.SetBackgroundColour(col
)
181 size
= self
.parentframe
.GetSize()
182 self
.parentframe
.SetSize(wxSize(0,0))
183 self
.parentframe
.SetSize(size
)
186 print self
.parentframe
.GetToolBar()
188 if self
.parentframe
.GetToolBar() != None:
189 print "Removing fauxbar"
190 self
.nullbar
.Reparent(self
.floatframe
)
191 self
.parentframe
.SetToolBar(None)
192 size
= self
.parentframe
.GetSize()
193 self
.parentframe
.SetSize(wxSize(0,0))
194 self
.parentframe
.SetSize(size
)