]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/floatbar.py
ce846b4a2280c2642ae3c0f0e89b48fd0da7f8e6
[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 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)
116 else:
117 self.Reparent(self.parentframe)
118 self.parentframe.SetToolBar(self)
119 self.floating = 0
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)
125 def OnDock(self, e):
126 self.Float(0)
127 if hasattr(self, 'oldpos'):
128 del self.oldpos
129
130 def OnMove(self, e):
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)
136 else:
137 self._SetFauxBarVisible(false)
138
139 def OnMouse(self, e):
140 if not self.IsFloatable():
141 e.Skip()
142 return
143 if e.ButtonDown() or e.ButtonUp() or e.ButtonDClick(1) or e.ButtonDClick(2) or e.ButtonDClick(3):
144 e.Skip()
145 if e.ButtonDown():
146 self.oldpos = (e.GetX(), e.GetY())
147 if e.Entering():
148 self.oldpos = (e.GetX(), e.GetY())
149 if e.ButtonUp():
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:
155 self.Float(0)
156 return
157 if self.IsFloatable():
158 if e.Dragging():
159 if not self.IsFloating():
160 self.Float(true)
161 self.oldpos = (e.GetX(), e.GetY())
162 else:
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)
167
168 def _SetFauxBarVisible(self, vis):
169 # return
170 if 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)
176 print "Reparented."
177 self.parentframe.SetToolBar(self.nullbar)
178 print "Set toolbar"
179 col = wxNamedColour("GREY")
180 self.nullbar.SetBackgroundColour(col)
181 print "Set color"
182 size = self.parentframe.GetSize()
183 self.parentframe.SetSize(wxSize(0,0))
184 self.parentframe.SetSize(size)
185 print "Set size"
186 else:
187 print self.parentframe.GetToolBar()
188 else:
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)
196
197
198
199
200
201
202
203
204
205
206
207
208