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