1 |
# Copyright (c) 2003 by Intevation GmbH |
2 |
# Authors: |
3 |
# Jonathan Coles <[email protected]> |
4 |
# |
5 |
# This program is free software under the GPL (>=v2) |
6 |
# Read the file COPYING coming with Thuban for details. |
7 |
|
8 |
"""Classes for creating dockable windows""" |
9 |
|
10 |
__version__ = "$Revision$" |
11 |
|
12 |
from Thuban import _ |
13 |
|
14 |
from wxPython.wx import * |
15 |
|
16 |
from Thuban.Lib.connector import Publisher |
17 |
|
18 |
from messages import DOCKABLE_DOCKED, DOCKABLE_UNDOCKED, DOCKABLE_CLOSED |
19 |
|
20 |
ID_BUTTON_DOCK = 4001 |
21 |
ID_BUTTON_UNDOCK = 4002 |
22 |
|
23 |
class DockPanel(wxPanel): |
24 |
|
25 |
def __init__(self, parent, id): |
26 |
|
27 |
wxPanel.__init__(self, parent, id) |
28 |
|
29 |
self.SetDockParent(None) |
30 |
|
31 |
def SetDockParent(self, parent): |
32 |
self.dockParent = parent |
33 |
|
34 |
def GetDockParent(self): |
35 |
return self.dockParent |
36 |
|
37 |
def Dock(self): |
38 |
self.GetDockParent().Dock() |
39 |
|
40 |
def UnDock(self): |
41 |
self.GetDockParent().UnDock() |
42 |
|
43 |
def IsDocked(self): |
44 |
return self.GetDockParent().IsDocked() |
45 |
|
46 |
class DockableWindow(NonModalDialog, Publisher): |
47 |
|
48 |
def __init__(self, parent, id, name, |
49 |
title, dockWindow, panel, docked = False, show = True): |
50 |
|
51 |
NonModalDialog.__init__(self, parent, name, title) |
52 |
|
53 |
self.id = id |
54 |
self.dockWindow = dockWindow |
55 |
self.docked = docked |
56 |
|
57 |
self.dockBorder = None |
58 |
self.dockBorderParent = self |
59 |
|
60 |
self.size = None |
61 |
self.position = None |
62 |
|
63 |
self.SetPanel(panel) |
64 |
self.SetDock(self.docked) |
65 |
|
66 |
self.Show(show) |
67 |
|
68 |
def SetPanel(self, panel, orient = wxVERTICAL): |
69 |
|
70 |
if not isinstance(panel, DockPanel): |
71 |
raise TypeError("") |
72 |
|
73 |
self.panel = panel |
74 |
self.panel.SetDockParent(self) |
75 |
self.orientation = orient |
76 |
self.__CreateBorder() |
77 |
|
78 |
def SetDock(self, dock): |
79 |
if dock: |
80 |
self.Dock() |
81 |
else: |
82 |
self.UnDock() |
83 |
|
84 |
def Dock(self): |
85 |
#if self.IsDocked(): return |
86 |
|
87 |
self.docked = True |
88 |
|
89 |
self.size = self.GetSize() |
90 |
self.position = self.GetPosition() |
91 |
#print self.position |
92 |
|
93 |
|
94 |
NonModalDialog.Hide(self) |
95 |
|
96 |
self.dockBorderParent = self.dockWindow |
97 |
self.dockBorder.Reparent(self.dockBorderParent) |
98 |
|
99 |
self.dockButton.SetLabel(_("Undock")) |
100 |
|
101 |
self.issue(DOCKABLE_DOCKED, self.id, self) |
102 |
|
103 |
def UnDock(self): |
104 |
|
105 |
#if not self.IsDocked(): return |
106 |
|
107 |
self.docked = False |
108 |
|
109 |
self.dockBorderParent = self |
110 |
self.dockBorder.Reparent(self.dockBorderParent) |
111 |
|
112 |
self.dockButton.SetLabel(_("Dock")) |
113 |
|
114 |
if self.position is not None: |
115 |
#print self.position |
116 |
self.SetPosition(self.position) |
117 |
|
118 |
NonModalDialog.Show(self) # this needs to come before SetSize() |
119 |
|
120 |
if self.size is not None: |
121 |
self.SetSize(self.size) |
122 |
|
123 |
self.issue(DOCKABLE_UNDOCKED, self.id, self) |
124 |
|
125 |
def IsDocked(self): |
126 |
return self.docked |
127 |
|
128 |
def OnClose(self, event): |
129 |
self.issue(DOCKABLE_CLOSED, self.id, self) |
130 |
NonModalDialog.OnClose(self, event) |
131 |
|
132 |
# def Show(self, show = True): |
133 |
|
134 |
# if show: |
135 |
|
136 |
# if self.IsDocked(): |
137 |
# self.docked = False |
138 |
# self.Dock() |
139 |
# else: |
140 |
# self.docked = True |
141 |
# self.UnDock() |
142 |
# #NonModalDialog.Show(self) |
143 |
# else: |
144 |
# self.docked = True |
145 |
# self.UnDock() |
146 |
# NonModalDialog.Hide(self) |
147 |
|
148 |
def GetSize(self): |
149 |
return self.dockBorder.GetSize() |
150 |
|
151 |
def GetBestSize(self): |
152 |
return self.dockBorder.GetBestSize() |
153 |
|
154 |
def GetClientSize(self): |
155 |
return self.dockBorder.GetClientSize() |
156 |
|
157 |
def GetAdjustedBestSize(self): |
158 |
return self.dockBorder.GetAdjustedBestSize() |
159 |
|
160 |
def GetSizeTuple(self): |
161 |
return self.dockBorder.GetSizeTuple() |
162 |
|
163 |
def _OnDock(self, event): |
164 |
|
165 |
self.SetDock(not self.docked) |
166 |
|
167 |
# if self.IsDocked(): |
168 |
# win.SetLabel(_("Undock")) |
169 |
# self.UnDock() |
170 |
# else: |
171 |
# win.SetLabel(_("Dock")) |
172 |
# self.Dock() |
173 |
|
174 |
#def _OnUnDock(self, event): |
175 |
#self.UnDock() |
176 |
|
177 |
def __CreateBorder(self): |
178 |
|
179 |
self.panel.Reparent(self) # Make sure we hang on to the panel |
180 |
|
181 |
|
182 |
sizer = wxBoxSizer(self.orientation) |
183 |
|
184 |
self.dockBorder = wxPanel(self.dockBorderParent, -1) |
185 |
#self.dockBorder.SetBackgroundColour(wxColour(255, 0, 0)) |
186 |
|
187 |
if self.orientation == wxVERTICAL: |
188 |
buttonBoxOrient = wxHORIZONTAL |
189 |
else: |
190 |
buttonBoxOrient = wxVERTICAL |
191 |
|
192 |
buttonBox = wxStaticBoxSizer( |
193 |
wxStaticBox(self.dockBorder, -1, ""), |
194 |
buttonBoxOrient) |
195 |
|
196 |
button = wxStaticText(self.dockBorder, -1, |
197 |
self.GetTitle(), |
198 |
style = wxSIMPLE_BORDER | wxALIGN_CENTRE) |
199 |
buttonBox.Add(button, 0, |
200 |
wxSHAPED | wxALIGN_LEFT | wxALIGN_CENTRE_VERTICAL, 0) |
201 |
|
202 |
buttonBox.Add(60, 20, 1, wxGROW) |
203 |
|
204 |
# |
205 |
# Perhaps using wxToggleButton would be better, but it's only |
206 |
# supported under wxMSW and wxGTK as of v2.4.0.3 |
207 |
# |
208 |
self.dockButton = wxButton(self.dockBorder, ID_BUTTON_DOCK, |
209 |
"WWWWWW", style = wxGROW | wxBU_EXACTFIT | wxADJUST_MINSIZE) |
210 |
buttonBox.Add(self.dockButton, 0, wxALIGN_RIGHT, 0) |
211 |
|
212 |
#button = wxButton(self.dockBorder, ID_BUTTON_UNDOCK, |
213 |
# _("Undock"), |
214 |
# style = wxSIMPLE_BORDER | wxBU_EXACTFIT) |
215 |
#buttonBox.Add(button, 0, wxALIGN_RIGHT, 0) |
216 |
|
217 |
sizer.Add(buttonBox, 0, wxGROW, 0) |
218 |
|
219 |
self.panel.Reparent(self.dockBorder) |
220 |
|
221 |
sizer.Add(self.panel, 1, wxGROW | wxALL, 0) |
222 |
self.dockBorder.SetSizer(sizer) |
223 |
self.dockBorder.SetAutoLayout(True) |
224 |
|
225 |
#self.dockBorderParent.SetAutoLayout(True) |
226 |
|
227 |
EVT_BUTTON(self.dockBorder, ID_BUTTON_DOCK, self._OnDock) |
228 |
#EVT_BUTTON(self.dockBorder, ID_BUTTON_UNDOCK, self._OnUnDock) |
229 |
|