]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/floatbar.py
system libtiff can now be used instead of the built-in one
[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 if wxPlatform == '__WXGTK__':
12 #
13 # For wxGTK all we have to do is set the wxTB_DOCKABLE flag
14 #
15 class wxFloatBar(wxToolBar):
16 def __init__(self, parent, ID,
17 pos = wxDefaultPosition,
18 size = wxDefaultSize,
19 style = 0,
20 name = 'toolbar'):
21 wxToolBar.__init__(self, parent, ID, pos, size,
22 style|wxTB_DOCKABLE, name)
23
24 # these other methods just become no-ops
25 def SetFloatable(self, float):
26 pass
27
28 def IsFloating(self):
29 return 1
30
31 def GetTitle(self):
32 return ""
33
34
35 def SetTitle(self, title):
36 pass
37
38 else:
39 _DOCKTHRESHOLD = 25
40
41 class wxFloatBar(wxToolBar):
42 """
43 wxToolBar subclass which can be dragged off its frame and later
44 replaced there. Drag on the toolbar to release it, close it like
45 a normal window to make it return to its original
46 position. Programmatically, call SetFloatable(true) and then
47 Float(true) to float, Float(false) to dock.
48 """
49
50 def __init__(self,*_args,**_kwargs):
51 """
52 In addition to the usual arguments, wxFloatBar accepts keyword
53 args of: title(string): the title that should appear on the
54 toolbar's frame when it is floating. floatable(bool): whether
55 user actions (i.e., dragging) can float the toolbar or not.
56 """
57 args = (self,) + _args
58 apply(wxToolBar.__init__, args, _kwargs)
59 if _kwargs.has_key('floatable'):
60 self.floatable = _kwargs['floatable']
61 assert type(self.floatable) == type(0)
62 else:
63 self.floatable = 0
64 self.floating = 0
65 if _kwargs.has_key('title'):
66 self.title = _kwargs['title']
67 assert type(self.title) == type("")
68 else:
69 self.title = ""
70 EVT_MOUSE_EVENTS(self, self.OnMouse)
71 self.parentframe = wxPyTypeCast(args[1], 'wxFrame')
72
73
74 def IsFloatable(self):
75 return self.floatable
76
77
78 def SetFloatable(self, float):
79 self.floatable = float
80 #Find the size of a title bar.
81 if not hasattr(self, 'titleheight'):
82 test = wxMiniFrame(NULL, -1, "TEST")
83 test.SetClientSize(wxSize(0,0))
84 self.titleheight = test.GetSizeTuple()[1]
85 test.Destroy()
86
87
88 def IsFloating(self):
89 return self.floating
90
91
92 def Realize(self):
93 wxToolBar.Realize(self)
94
95
96 def GetTitle(self):
97 return self.title
98
99
100 def SetTitle(self, title):
101 print 'SetTitle', title
102 self.title = title
103 if self.IsFloating():
104 self.floatframe.SetTitle(self.title)
105
106
107 ## def GetHome(self):
108 ## """
109 ## Returns the frame which this toolbar will return to when
110 ## docked, or the parent if currently docked.
111 ## """
112 ## if hasattr(self, 'parentframe'):
113 ## return self.parentframe
114 ## else:
115 ## return wxPyTypeCast(self.GetParent(), 'wxFrame')
116
117
118 ## def SetHome(self, frame):
119 ## """
120 ## Called when docked, this will remove the toolbar from its
121 ## current frame and attach it to another. If called when
122 ## floating, it will dock to the frame specified when the toolbar
123 ## window is closed.
124 ## """
125 ## if self.IsFloating():
126 ## self.parentframe = frame
127 ## self.floatframe.Reparent(frame)
128 ## else:
129 ## parent = wxPyTypeCast(self.GetParent(), 'wxFrame')
130 ## self.Reparent(frame)
131 ## parent.SetToolBar(None)
132 ## size = parent.GetSize()
133 ## parent.SetSize(wxSize(0,0))
134 ## parent.SetSize(size)
135 ## frame.SetToolBar(self)
136 ## size = frame.GetSize()
137 ## frame.SetSize(wxSize(0,0))
138 ## frame.SetSize(size)
139
140
141 def Float(self, bool):
142 "Floats or docks the toolbar programmatically."
143 if bool:
144 self.parentframe = wxPyTypeCast(self.GetParent(), 'wxFrame')
145 print self.title
146 if self.title:
147 useStyle = wxDEFAULT_FRAME_STYLE
148 else:
149 useStyle = wxTHICK_FRAME
150 self.floatframe = wxFrame(self.parentframe, -1, self.title,
151 style = useStyle)
152
153 self.Reparent(self.floatframe)
154 self.parentframe.SetToolBar(None)
155 self.floating = 1
156 psize = self.parentframe.GetSize()
157 self.parentframe.SetSize(wxSize(0,0))
158 self.parentframe.SetSize(psize)
159 self.floatframe.SetToolBar(self)
160 self.oldcolor = self.GetBackgroundColour()
161
162 w = psize.width
163 h = self.GetSize().height
164 if self.title:
165 h = h + self.titleheight
166 self.floatframe.SetSize(wxSize(w,h))
167 self.floatframe.SetClientSize(self.GetSize())
168 newpos = self.parentframe.GetPosition()
169 newpos.y = newpos.y + _DOCKTHRESHOLD * 2
170 self.floatframe.SetPosition(newpos)
171 self.floatframe.Show(true)
172
173 EVT_CLOSE(self.floatframe, self.OnDock)
174 #EVT_MOVE(self.floatframe, self.OnMove)
175
176 else:
177 self.Reparent(self.parentframe)
178 self.parentframe.SetToolBar(self)
179 self.floating = 0
180 self.floatframe.SetToolBar(None)
181 self.floatframe.Destroy()
182 size = self.parentframe.GetSize()
183 self.parentframe.SetSize(wxSize(0,0))
184 self.parentframe.SetSize(size)
185 self.SetBackgroundColour(self.oldcolor)
186
187
188 def OnDock(self, e):
189 self.Float(0)
190 if hasattr(self, 'oldpos'):
191 del self.oldpos
192
193
194 def OnMove(self, e):
195 homepos = self.parentframe.ClientToScreen(wxPoint(0,0))
196 floatpos = self.floatframe.GetPosition()
197 if (abs(homepos.x - floatpos.x) < _DOCKTHRESHOLD and
198 abs(homepos.y - floatpos.y) < _DOCKTHRESHOLD):
199 self.Float(0)
200 #homepos = self.parentframe.GetPositionTuple()
201 #homepos = homepos[0], homepos[1] + self.titleheight
202 #floatpos = self.floatframe.GetPositionTuple()
203 #if abs(homepos[0] - floatpos[0]) < 35 and abs(homepos[1] - floatpos[1]) < 35:
204 # self._SetFauxBarVisible(true)
205 #else:
206 # self._SetFauxBarVisible(false)
207
208
209 def OnMouse(self, e):
210 if not self.IsFloatable():
211 e.Skip()
212 return
213
214 if e.ButtonDClick(1) or e.ButtonDClick(2) or e.ButtonDClick(3) or e.ButtonDown() or e.ButtonUp():
215 e.Skip()
216
217 if e.ButtonDown():
218 self.CaptureMouse()
219 self.oldpos = (e.GetX(), e.GetY())
220
221 if e.Entering():
222 self.oldpos = (e.GetX(), e.GetY())
223
224 if e.ButtonUp():
225 self.ReleaseMouse()
226 if self.IsFloating():
227 homepos = self.parentframe.ClientToScreen(wxPoint(0,0))
228 floatpos = self.floatframe.GetPosition()
229 if (abs(homepos.x - floatpos.x) < _DOCKTHRESHOLD and
230 abs(homepos.y - floatpos.y) < _DOCKTHRESHOLD):
231 self.Float(0)
232 return
233
234 if e.Dragging():
235 if not self.IsFloating():
236 self.Float(true)
237 self.oldpos = (e.GetX(), e.GetY())
238 else:
239 if hasattr(self, 'oldpos'):
240 loc = self.floatframe.GetPosition()
241 pt = wxPoint(loc.x - (self.oldpos[0]-e.GetX()), loc.y - (self.oldpos[1]-e.GetY()))
242 self.floatframe.Move(pt)
243
244
245
246 def _SetFauxBarVisible(self, vis):
247 return
248 if vis:
249 if self.parentframe.GetToolBar() == None:
250 if not hasattr(self, 'nullbar'):
251 self.nullbar = wxToolBar(self.parentframe, -1)
252 print "Adding fauxbar."
253 self.nullbar.Reparent(self.parentframe)
254 print "Reparented."
255 self.parentframe.SetToolBar(self.nullbar)
256 print "Set toolbar"
257 col = wxNamedColour("GREY")
258 self.nullbar.SetBackgroundColour(col)
259 print "Set color"
260 size = self.parentframe.GetSize()
261 self.parentframe.SetSize(wxSize(0,0))
262 self.parentframe.SetSize(size)
263 print "Set size"
264 else:
265 print self.parentframe.GetToolBar()
266 else:
267 if self.parentframe.GetToolBar() != None:
268 print "Removing fauxbar"
269 self.nullbar.Reparent(self.floatframe)
270 self.parentframe.SetToolBar(None)
271 size = self.parentframe.GetSize()
272 self.parentframe.SetSize(wxSize(0,0))
273 self.parentframe.SetSize(size)
274
275
276