/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/dock.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/UI/dock.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 566 by jonathan, Wed Mar 26 11:10:41 2003 UTC revision 631 by jonathan, Wed Apr 9 10:10:38 2003 UTC
# Line 19  from dialogs import NonModalDialog Line 19  from dialogs import NonModalDialog
19    
20  from messages import DOCKABLE_DOCKED, DOCKABLE_UNDOCKED, DOCKABLE_CLOSED  from messages import DOCKABLE_DOCKED, DOCKABLE_UNDOCKED, DOCKABLE_CLOSED
21    
22    import gc
23    
24  ID_BUTTON_DOCK = 4001  ID_BUTTON_DOCK = 4001
25  ID_BUTTON_UNDOCK = 4002  ID_BUTTON_CLOSE = 4002
26    
27    PANEL_ID = 3141
28    
29  class DockPanel(wxPanel):  class DockPanel(wxPanel):
30    
31      def __init__(self, parent, id):      def __init__(self, parent, id):
32    
33          wxPanel.__init__(self, parent, id)          if not isinstance(parent, DockableWindow):
34                raise TypeError("")
35    
36            wxPanel.__init__(self, parent.GetCurrentParent(), id)
37    
38          self.SetDockParent(None)          #self.SetDockParent(None)
39            parent.SetPanel(self)
40    
41      def SetDockParent(self, parent):      def SetDockParent(self, parent):
42          self.dockParent = parent          self.__dockParent = parent
43    
44      def GetDockParent(self):      def GetDockParent(self):
45          return self.dockParent          return self.__dockParent
46    
47        def SetDock(self, dock):
48            #if dock == self.IsDocked(): return
49    
50            if dock:
51                self.Dock()
52            else:
53                self.UnDock()
54    
55      def Dock(self):      def Dock(self):
56          self.GetDockParent().Dock()          self.GetDockParent().Dock()
# Line 45  class DockPanel(wxPanel): Line 61  class DockPanel(wxPanel):
61      def IsDocked(self):      def IsDocked(self):
62          return self.GetDockParent().IsDocked()          return self.GetDockParent().IsDocked()
63    
64  class DockableWindow(NonModalDialog, Publisher):  class DockableWindow(Publisher):
65    
66        def __getattr__(self, attr):
67    
68            #print attr
69            return getattr(self.__topWindow, attr)
70            #try:
71            #except AttributeError:
72                #raise
73                #return self.__dict__[attr]
74                #return getattr(self, attr)
75                #pass
76    
77      def __init__(self, parent, id, name,      def __init__(self, parent, id, name,
78                   title, dockWindow, panel, docked = False, show = True):                   title, dockWindow, orient): #, panel, docked = False, show = True):
79    
80            if not isinstance(parent, DockFrame): raise TypeError("")
81    
82            self.__parent = parent
83            self.__id     = id
84            self.__name   = name
85    
86            self.__orientation = orient
87    
88            self.__dockWindow  = dockWindow
89            #self.__floatWindow = NonModalDialog(parent, name, title)
90            self.__floatWindow = wxFrame(parent, id, title)
91    
92            self.__docked      = False
93    
94            self.__dockPanel  = None
95    
96            if self.__docked:
97                self.__topWindow = self.__dockWindow
98            else:
99                self.__topWindow = self.__floatWindow
100    
101          NonModalDialog.__init__(self, parent, name, title)          self.__floatSize     = None
102            self.__floatPosition = None
103    
104          self.id = id          self.__panel = None
         self.dockWindow = dockWindow  
         self.docked = docked  
105    
106          self.dockBorder = None          self.__dockWindow.Hide()
107          self.dockBorderParent = self          self.__floatWindow.Hide()
108    
109          self.size = None          EVT_CLOSE(self, self._OnClose)
         self.position = None  
110    
111          self.SetPanel(panel)      ##
112          self.SetDock(self.docked)      # Public methods
113        #
114    
115          self.Show(show)      def GetName(self):
116            return self.__name
117    
118      def SetPanel(self, panel, orient = wxVERTICAL):      def SetPanel(self, panel):
119    
120          if not isinstance(panel, DockPanel):          if not isinstance(panel, DockPanel):
121              raise TypeError("")              raise TypeError("")
122    
123          self.panel = panel          self.__panel = panel
124          self.panel.SetDockParent(self)          self.__panel.SetDockParent(self)
         self.orientation = orient  
125          self.__CreateBorder()          self.__CreateBorder()
126    
127            self.SetDock(self.__docked)
128    
129        def GetPanel(self):
130            return self.__panel
131                    
132        def GetCurrentParent(self):
133            return self.__topWindow
134    
135      def SetDock(self, dock):      def SetDock(self, dock):
136    
137            self.__CheckAllGood()
138    
139          if dock:          if dock:
140              self.Dock()              self.Dock()
141          else:          else:
142              self.UnDock()              self.UnDock()
143    
144      def Dock(self):      def Dock(self):
145          #if self.IsDocked(): return          self.__CheckAllGood()
146    
147            wasVisible = self.IsShown()
148    
149          self.docked = True          if wasVisible: self.Show(False)
150    
151          self.size = self.GetSize()          self.__docked = True
         self.position = self.GetPosition()  
         #print self.position  
152    
153            #
154            # save window information
155            #
156            self.__floatSize = self.GetSize()
157            self.__floatPosition = self.GetPosition()
158    
159            #
160            # reparent
161            #
162            self.__topWindow = self.__dockWindow
163            self.__dockPanel.Reparent(self.__topWindow)
164    
165          NonModalDialog.Hide(self)          self.__dockButton.SetLabel(_("Undock"))
166    
167          self.dockBorderParent = self.dockWindow          self.SetDockSize(self.__dockWindow.GetSize())
         self.dockBorder.Reparent(self.dockBorderParent)  
168    
169          self.dockButton.SetLabel(_("Undock"))          if wasVisible: self.Show(True)
170    
171          self.issue(DOCKABLE_DOCKED, self.id, self)          #self.__parent._UpdateDocks()
172    
173            self.issue(DOCKABLE_DOCKED, self.__id, self)
174    
175      def UnDock(self):      def UnDock(self):
176            self.__CheckAllGood()
177    
178            wasVisible = self.IsShown()
179    
180          #if not self.IsDocked(): return          if wasVisible: self.Show(False)
181    
182          self.docked = False          self.__docked = False
183    
184          self.dockBorderParent = self          #
185          self.dockBorder.Reparent(self.dockBorderParent)          # reparent
186            #
187            self.__topWindow = self.__floatWindow
188            self.__dockPanel.Reparent(self.__topWindow)
189    
190          self.dockButton.SetLabel(_("Dock"))          self.__dockButton.SetLabel(_("Dock"))
191    
192          if self.position is not None:          if wasVisible: self.Show()
             #print self.position  
             self.SetPosition(self.position)  
193    
194          NonModalDialog.Show(self) # this needs to come before SetSize()          #
195            # restore window information
196            #
197            if self.__floatPosition is not None: self.SetPosition(self.__floatPosition)
198            if self.__floatSize     is not None: self.SetSize(self.__floatSize)
199    
200          if self.size is not None:          self.__dockPanel.SetSize(self.__topWindow.GetClientSize())
             self.SetSize(self.size)  
201    
202          self.issue(DOCKABLE_UNDOCKED, self.id, self)          self.issue(DOCKABLE_UNDOCKED, self.__id, self)
203    
204      def IsDocked(self):      def IsDocked(self):
205          return self.docked          self.__CheckAllGood()
206    
207      def OnClose(self, event):          return self.__docked
         self.issue(DOCKABLE_CLOSED, self.id, self)  
         NonModalDialog.OnClose(self, event)  
208    
 #   def Show(self, show = True):  
209    
210  #       if show:      def Show(self, show = True):
211            if show:
212                self.__DoShow()
213            else:
214                self.__DoHide()
215    
216  #           if self.IsDocked():      def SetDockSize(self, rect = None):
 #               self.docked = False  
 #               self.Dock()  
 #           else:  
 #               self.docked = True  
 #               self.UnDock()  
 #               #NonModalDialog.Show(self)  
 #       else:  
 #           self.docked = True  
 #           self.UnDock()  
 #           NonModalDialog.Hide(self)  
217    
218      def GetSize(self):          #
219          return self.dockBorder.GetSize()          # adjust the size to get the
220            w0, h0 = self.__dockPanel.GetBestSize()
221            w, h = self.__panel.GetBestSize()
222    
223            #print (w0, h0), (w, h)
224    
225            if (w, h) < (w0, h0):
226                w = w0
227                h = h0
228                                                                                          
229            if rect is not None:
230                rw = rect.width
231                rh = rect.height
232                #print "   ", (rw, rh)
233                if rw < w: rw = w
234                if rh < h: rh = h
235            else:
236                rw = w
237                rh = h
238                                                                                          
239            # these are to account for the border?!!?
240            rw += 8 # XXX: without this the sash isn't visible!?!?!?!
241            rh += 8 # XXX: without this the sash isn't visible!?!?!?!
242                                                                                          
243            self.__dockWindow.SetDefaultSize(wxSize(rw, rh))
244    
     def GetBestSize(self):  
         return self.dockBorder.GetBestSize()  
245    
246      def GetClientSize(self):      def Destroy(self):
247          return self.dockBorder.GetClientSize()          self.__panel.Destroy()
248            self.__floatWindow.Destroy()
249            self.__dockWindow.Destroy()
250            self.__parent.OnDockDestroy(self)
251            
252        ##
253        # Event handlers
254        #
255    
256      def GetAdjustedBestSize(self):      def _OnButtonClose(self, event):
257          return self.dockBorder.GetAdjustedBestSize()          #self.Close()
258            self.Show(False)
259    
260      def GetSizeTuple(self):      def _OnClose(self, force = False):
261          return self.dockBorder.GetSizeTuple()          self.Show(False)
262    
263      def _OnDock(self, event):      def _OnToggleDock(self, event):
264            self.__CheckAllGood()
265    
266          self.SetDock(not self.docked)          self.SetDock(not self.IsDocked())
267    
268  #       if self.IsDocked():      ##
269  #           win.SetLabel(_("Undock"))      # Private methods
270  #           self.UnDock()      #
 #       else:  
 #           win.SetLabel(_("Dock"))  
 #           self.Dock()  
271    
272      #def _OnUnDock(self, event):      def __CheckAllGood(self):
273          #self.UnDock()          if self.__panel is None:
274                raise TypeError("")
275    
276      def __CreateBorder(self):      def __DoShow(self):
277            if self.IsShown(): return
278            #print "__DoShow()", self.IsShown()
279    
280            self.__topWindow.Show()
281    
282            #if self.IsDocked():
283                #self.SetDockSize()
284    
285            if self.__topWindow is self.__dockWindow:
286                self.__parent._UpdateDocks()
287                
288        def __DoHide(self):
289            if not self.IsShown(): return
290            #print "__DoHide()", self.IsShown()
291            self.__topWindow.Show(False)
292    
293            if self.__topWindow is self.__dockWindow:
294                self.__parent._UpdateDocks()
295    
         self.panel.Reparent(self) # Make sure we hang on to the panel  
296    
297        def __CreateBorder(self):
298    
299          sizer = wxBoxSizer(self.orientation)          #self.__panel.Reparent(self) # Make sure we hang on to the panel
300    
301          self.dockBorder = wxPanel(self.dockBorderParent, -1)          self.__dockPanel = wxPanel(self.__topWindow, -1, style=wxSUNKEN_BORDER)
302          #self.dockBorder.SetBackgroundColour(wxColour(255, 0, 0))          #self.__dockPanel.SetBackgroundColour(wxColour(255, 0, 0))
303    
304          if self.orientation == wxVERTICAL:          if self.__orientation == wxLAYOUT_VERTICAL:
305              buttonBoxOrient = wxHORIZONTAL              sizer = wxBoxSizer(wxVERTICAL)
306                headerBoxOrient = wxHORIZONTAL
307          else:          else:
308              buttonBoxOrient = wxVERTICAL              sizer = wxBoxSizer(wxHORIZONTAL)
309                headerBoxOrient = wxVERTICAL
310            
311    
312          buttonBox = wxStaticBoxSizer(          headerBox = wxStaticBoxSizer(
313                          wxStaticBox(self.dockBorder, -1, ""),                          wxStaticBox(self.__dockPanel, -1, ""), headerBoxOrient)
                         buttonBoxOrient)  
314    
315          button = wxStaticText(self.dockBorder, -1,          buttonBox = wxBoxSizer(wxHORIZONTAL)
                               self.GetTitle(),  
                               style = wxSIMPLE_BORDER | wxALIGN_CENTRE)  
         buttonBox.Add(button, 0,  
                       wxSHAPED | wxALIGN_LEFT | wxALIGN_CENTRE_VERTICAL, 0)  
316    
317          buttonBox.Add(60, 20, 1, wxGROW)          #
318            # ideally, we should be able to rotate this text depending on
319            # our orientation
320            #
321            text = wxStaticText(self.__dockPanel, -1, self.GetTitle(),
322                                 style = wxALIGN_CENTRE)
323    
324          #          #
325          # Perhaps using wxToggleButton would be better, but it's only          # Perhaps using wxToggleButton would be better, but it's only
326          # supported under wxMSW and wxGTK as of v2.4.0.3          # supported under wxMSW and wxGTK as of v2.4.0.3
327          #          #
328          self.dockButton = wxButton(self.dockBorder, ID_BUTTON_DOCK,          self.__dockButton = wxButton(self.__dockPanel, ID_BUTTON_DOCK, "WWWWWW",
329              "WWWWWW", style = wxGROW | wxBU_EXACTFIT | wxADJUST_MINSIZE)                                       style = wxBU_EXACTFIT | wxADJUST_MINSIZE)
330          buttonBox.Add(self.dockButton, 0, wxALIGN_RIGHT, 0)  
331            closeX = wxButton(self.__dockPanel, ID_BUTTON_CLOSE, "X",
332                             style = wxBU_EXACTFIT | wxADJUST_MINSIZE)
333    
334            buttonBox.Add(self.__dockButton, 0, wxALIGN_RIGHT, 0)
335            buttonBox.Add(closeX, 0, wxALIGN_RIGHT, 0)
336    
337            if self.__orientation == wxLAYOUT_VERTICAL:
338                headerBox.Add(text, 0, wxALIGN_LEFT | wxALIGN_CENTRE_VERTICAL, 0)
339                headerBox.Add(1, 20, 1, wxGROW)
340                headerBox.Add(buttonBox, 0, wxGROW | wxALIGN_RIGHT, 0)
341            else:
342                headerBox.Add(buttonBox, 0, wxGROW, 0)
343                headerBox.Add(text, 0, wxALIGN_LEFT | wxALIGN_CENTRE_VERTICAL, 0)
344    
345            sizer.Add(headerBox, 0, wxGROW, 0)
346    
347            self.__panel.Reparent(self.__dockPanel)
348            self.__panel.SetId(PANEL_ID)
349    
350            sizer.Add(self.__panel, 1, wxGROW, 0)
351    
352            sizer.Fit(self.__dockPanel)
353    
354            self.__dockPanel.SetSizer(sizer)
355            self.__dockPanel.SetAutoLayout(True)
356    
357            sizer.SetSizeHints(self.__dockPanel)
358    
359            EVT_BUTTON(self.__dockPanel, ID_BUTTON_DOCK, self._OnToggleDock)
360            EVT_BUTTON(self.__dockPanel, ID_BUTTON_CLOSE, self._OnButtonClose)
361    
362    
363    class DockFrame(wxFrame):
364    
365        def __init__(self, parent, id, title, position, size):
366            wxFrame.__init__(self, parent, id, title, position, size)
367    
368          #button = wxButton(self.dockBorder, ID_BUTTON_UNDOCK,          self.openWindows = {}
         #                      _("Undock"),  
         #                      style = wxSIMPLE_BORDER | wxBU_EXACTFIT)  
         #buttonBox.Add(button, 0, wxALIGN_RIGHT, 0)  
369    
370          sizer.Add(buttonBox, 0, wxGROW, 0)          self.__update_lock = 0
371    
372          self.panel.Reparent(self.dockBorder)          self.SetMainWindow(None)
373    
         sizer.Add(self.panel, 1, wxGROW | wxALL, 0)  
         self.dockBorder.SetSizer(sizer)  
         self.dockBorder.SetAutoLayout(True)  
374    
375          #self.dockBorderParent.SetAutoLayout(True)          EVT_SIZE(self, self._OnSashSize)
376            EVT_CLOSE(self, self._OnClose)
377    
378        layout2oppSash = {
379                wxLAYOUT_NONE   : wxSASH_NONE,
380                wxLAYOUT_TOP    : wxSASH_BOTTOM,
381                wxLAYOUT_LEFT   : wxSASH_RIGHT,
382                wxLAYOUT_RIGHT  : wxSASH_LEFT,
383                wxLAYOUT_BOTTOM : wxSASH_TOP }
384    
385    
386        def _OnClose(self, event):
387    
388            self.__update_lock += 1
389    
390            #
391            # child windows are not notified when the parent is destroyed
392            # as of v2.4.0.3 so we need to interate over our children
393            # and tell them to go away.
394            #
395            for key in self.openWindows.keys():
396                win = self.openWindows[key]
397                win.Destroy()
398    
399            self.__update_lock -= 1
400    
401            # should really call _UpdateDocks() here but we don't need to
402            # since we're going away
403    
404        def CreateDock(self, name, id, title, align):
405    
406            if align in (wxLAYOUT_NONE, wxLAYOUT_LEFT, wxLAYOUT_RIGHT):
407                orient = wxLAYOUT_VERTICAL
408            else:
409                orient = wxLAYOUT_HORIZONTAL
410    
411            sash = wxSashLayoutWindow(self, id, style=wxNO_BORDER|wxSW_3D)
412            sash.SetOrientation(orient)
413            sash.SetAlignment(align)
414            #print align, DockFrame.layout2oppSash[align]
415            sash.SetSashVisible(DockFrame.layout2oppSash[align], True)
416            sash.SetSashBorder(DockFrame.layout2oppSash[align], True)
417    
418            win = DockableWindow(self, id, name, title, sash, orient)
419    
420            self.__RegisterDock(name, win)
421            EVT_SASH_DRAGGED(self, id, self._OnSashDragged)
422    
423            return win
424    
425        def FindRegisteredDock(self, name):
426            return self.openWindows.get(name)
427    
428        def OnDockDestroy(self, win):
429            del self.openWindows[win.GetName()]
430            self._UpdateDocks()
431    
432        def SetMainWindow(self, main):
433            self.__mainWindow = main
434            self._UpdateDocks()
435            
436        def _UpdateDocks(self):
437            #print "_UpdateDocks()"
438            if self.__update_lock == 0:
439                wxLayoutAlgorithm().LayoutWindow(self, self.__mainWindow)
440    
441        def _OnSashDragged(self, event):
442            if event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE:
443                return
444    
445            id = event.GetId()
446            sash = self.FindWindowById(id)
447            #assert(isinstance(win, wxPanel))
448            dockPanel = sash.GetChildren()[0]
449            #print dockPanel
450            panel = dockPanel.FindWindowById(PANEL_ID)
451            #print panel
452            assert isinstance(panel, DockPanel)
453            win = panel.GetDockParent()
454            #print win
455            assert isinstance(win, DockableWindow)
456    
457            assert win.IsDocked()
458    
459            rect = event.GetDragRect()
460    
461            win.SetDockSize(rect)
462    
463            self._UpdateDocks()
464    
465        def _OnSashSize(self, event):
466            self._UpdateDocks()
467    
468        def __RegisterDock(self, name, win):
469            if self.FindRegisteredDock(name) is not None:
470                raise ValueError(
471                    "A dockable window is already registered under the name '%s'" % name)
472    
473          EVT_BUTTON(self.dockBorder, ID_BUTTON_DOCK, self._OnDock)          self.openWindows[name] = win
         #EVT_BUTTON(self.dockBorder, ID_BUTTON_UNDOCK, self._OnUnDock)  
474    

Legend:
Removed from v.566  
changed lines
  Added in v.631

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26