]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/floatbar.py
Stupid makeprog.vc corruption fixed
[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
20 def __init__(self,*_args,**_kwargs):
21 """
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.
26 """
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)
32 else:
33 self.floatable = 0
34 self.floating = 0
35 if _kwargs.has_key('title'):
36 self.title = _kwargs['title']
37 assert type(self.title) == type("")
38 else:
39 self.title = ""
40 EVT_MOUSE_EVENTS(self, self.OnMouse)
41 self.parentframe = wxPyTypeCast(args[1], 'wxFrame')
42
43 def IsFloatable(self):
44 return self.floatable
45
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]
53 test.Destroy()
54
55 def IsFloating(self):
56 return self.floating
57
58 def Realize(self):
59 wxToolBar.Realize(self)
60 self.barheight = -1
61
62 def GetTitle(self):
63 return self.title
64
65 def SetTitle(self, title):
66 self.title = title
67 if self.IsFloating():
68 self.floatframe.SetTitle(self.title)
69
70 def GetHome(self):
71 """
72 Returns the frame which this toolbar will return to when
73 docked, or the parent if currently docked.
74 """
75 if hasattr(self, 'parentframe'):
76 return self.parentframe
77 else:
78 return wxPyTypeCast(self.GetParent(), 'wxFrame')
79
80 def SetHome(self, frame):
81 """
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
85 window is closed.
86 """
87 if self.IsFloating():
88 self.parentframe = frame
89 self.floatframe.Reparent(frame)
90 else:
91 parent = wxPyTypeCast(self.GetParent(), 'wxFrame')
92 self.Reparent(frame)
93 parent.SetToolBar(None)
94 size = parent.GetSize()
95 parent.SetSize(wxSize(0,0))
96 parent.SetSize(size)
97 frame.SetToolBar(self)
98 size = frame.GetSize()
99 frame.SetSize(wxSize(0,0))
100 frame.SetSize(size)
101
102 def Float(self, bool):
103 "Floats or docks the toolbar programmatically."
104 if bool:
105 self.parentframe = wxPyTypeCast(self.GetParent(), 'wxFrame')
106 clientsize = self.parentframe.GetClientSizeTuple()
107 if self.title:
108 useStyle = wxDEFAULT_FRAME_STYLE
109 else:
110 useStyle = 0 #wxTHICK_FRAME
111 self.floatframe = wxMiniFrame(self.parentframe, -1, self.title,
112 style = useStyle)
113 self.Reparent(self.floatframe)
114 self.parentframe.SetToolBar(None)
115 self.floating = 1
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)
130 else:
131 self.Reparent(self.parentframe)
132 self.parentframe.SetToolBar(self)
133 self.floating = 0
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)
139
140 def OnDock(self, e):
141 self.Float(0)
142 if hasattr(self, 'oldpos'):
143 del self.oldpos
144
145 def OnMove(self, e):
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)
151 else:
152 self._SetFauxBarVisible(false)
153
154 def OnMouse(self, e):
155 if not self.IsFloatable():
156 e.Skip()
157 return
158 if e.ButtonDown() or e.ButtonUp() or e.ButtonDClick(1) or e.ButtonDClick(2) or e.ButtonDClick(3):
159 e.Skip()
160 if e.ButtonDown():
161 self.CaptureMouse()
162 self.oldpos = (e.GetX(), e.GetY())
163 if e.Entering():
164 self.oldpos = (e.GetX(), e.GetY())
165 if e.ButtonUp():
166 self.ReleaseMouse()
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:
172 self.Float(0)
173 return
174 if self.IsFloatable():
175 if e.Dragging():
176 if not self.IsFloating():
177 self.Float(true)
178 self.oldpos = (e.GetX(), e.GetY())
179 else:
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)
184
185 def _SetFauxBarVisible(self, vis):
186 return
187 if 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)
193 print "Reparented."
194 self.parentframe.SetToolBar(self.nullbar)
195 print "Set toolbar"
196 col = wxNamedColour("GREY")
197 self.nullbar.SetBackgroundColour(col)
198 print "Set color"
199 size = self.parentframe.GetSize()
200 self.parentframe.SetSize(wxSize(0,0))
201 self.parentframe.SetSize(size)
202 print "Set size"
203 else:
204 print self.parentframe.GetToolBar()
205 else:
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)
213
214
215
216
217
218
219
220
221
222
223
224
225