]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/floatbar.py
new contributions
[wxWidgets.git] / utils / wxPython / lib / floatbar.py
1 #----------------------------------------------------------------------------
2 # Name: floatbar.py
3 # Purpose: Contains floating toolbar class
4 #
5 # Author: Bryn Keller
6 #
7 # Created: 10/4/99
8 #----------------------------------------------------------------------------
9 from wxPython.wx import *
10
11 class wxFloatBar(wxToolBar):
12 """
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.
18 """
19 def __init__(self,*_args,**_kwargs):
20 """
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.
25 """
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)
31 else:
32 self.floatable = 0
33 self.floating = 0
34 if _kwargs.has_key('title'):
35 self.title = _kwargs['title']
36 assert type(self.title) == type("")
37 else:
38 self.title = ""
39 EVT_MOUSE_EVENTS(self, self.OnMouse)
40 self.parentframe = wxPyTypeCast(args[1], 'wxFrame')
41 def IsFloatable(self):
42 return self.floatable
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]
50 test.Destroy()
51 def IsFloating(self):
52 return self.floating
53 def Realize(self):
54 wxToolBar.Realize(self)
55 self.barheight = -1
56 def GetTitle(self):
57 return self.title
58 def SetTitle(self, title):
59 self.title = title
60 if self.IsFloating():
61 self.floatframe.SetTitle(self.title)
62 def GetHome(self):
63 """
64 Returns the frame which this toolbar will return to when
65 docked, or the parent if currently docked.
66 """
67 if hasattr(self, 'parentframe'):
68 return self.parentframe
69 else:
70 return wxPyTypeCast(self.GetParent(), 'wxFrame')
71 def SetHome(self, frame):
72 """
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
76 window is closed.
77 """
78 if self.IsFloating():
79 self.parentframe = frame
80 self.floatframe.Reparent(frame)
81 else:
82 parent = wxPyTypeCast(self.GetParent(), 'wxFrame')
83 self.Reparent(frame)
84 parent.SetToolBar(None)
85 size = parent.GetSize()
86 parent.SetSize(wxSize(0,0))
87 parent.SetSize(size)
88 frame.SetToolBar(self)
89 size = frame.GetSize()
90 frame.SetSize(wxSize(0,0))
91 frame.SetSize(size)
92 def Float(self, bool):
93 "Floats or docks the toolbar programmatically."
94 if bool:
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)
100 self.floating = 1
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)
115 else:
116 self.Reparent(self.parentframe)
117 self.parentframe.SetToolBar(self)
118 self.floating = 0
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)
124 def OnDock(self, e):
125 self.Float(0)
126 if hasattr(self, 'oldpos'):
127 del self.oldpos
128
129 def OnMove(self, e):
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)
135 else:
136 self._SetFauxBarVisible(false)
137
138 def OnMouse(self, e):
139 if not self.IsFloatable():
140 e.Skip()
141 return
142 if e.ButtonDown() or e.ButtonUp() or e.ButtonDClick(1) or e.ButtonDClick(2) or e.ButtonDClick(3):
143 e.Skip()
144 if e.ButtonDown():
145 self.oldpos = (e.GetX(), e.GetY())
146 if e.Entering():
147 self.oldpos = (e.GetX(), e.GetY())
148 if e.ButtonUp():
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:
154 self.Float(0)
155 return
156 if self.IsFloatable():
157 if e.Dragging():
158 if not self.IsFloating():
159 self.Float(true)
160 self.oldpos = (e.GetX(), e.GetY())
161 else:
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)
166
167 def _SetFauxBarVisible(self, vis):
168 # return
169 if 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)
175 print "Reparented."
176 self.parentframe.SetToolBar(self.nullbar)
177 print "Set toolbar"
178 col = wxNamedColour("GREY")
179 self.nullbar.SetBackgroundColour(col)
180 print "Set color"
181 size = self.parentframe.GetSize()
182 self.parentframe.SetSize(wxSize(0,0))
183 self.parentframe.SetSize(size)
184 print "Set size"
185 else:
186 print self.parentframe.GetToolBar()
187 else:
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)
195
196
197
198
199
200
201
202
203
204
205
206
207