]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/samples/ide/activegrid/tool/Service.py
Copy about.htm
[wxWidgets.git] / wxPython / samples / ide / activegrid / tool / Service.py
... / ...
CommitLineData
1#----------------------------------------------------------------------------
2# Name: Service.py
3# Purpose: Basic Reusable Service View for wx.lib.pydocview
4#
5# Author: Morgan Hua
6#
7# Created: 11/4/04
8# CVS-ID: $Id$
9# Copyright: (c) 2004-2005 ActiveGrid, Inc.
10# License: wxWindows License
11#----------------------------------------------------------------------------
12
13import wx
14import wx.lib.docview
15import wx.lib.pydocview
16_ = wx.GetTranslation
17
18
19FLOATING_MINIFRAME = -1
20
21
22class ServiceView(wx.EvtHandler):
23 """ Basic Service View.
24 """
25 bottomTab = None
26
27 #----------------------------------------------------------------------------
28 # Overridden methods
29 #----------------------------------------------------------------------------
30
31 def __init__(self, service):
32 wx.EvtHandler.__init__(self)
33 self._viewFrame = None
34 self._service = service
35 self._control = None
36 self._embeddedWindow = None
37
38
39 def Destroy(self):
40 wx.EvtHandler.Destroy(self)
41
42
43 def GetFrame(self):
44 return self._viewFrame
45
46
47 def SetFrame(self, frame):
48 self._viewFrame = frame
49
50
51 def _CreateControl(self, parent, id):
52 return None
53
54
55 def GetControl(self):
56 return self._control
57
58
59 def SetControl(self, control):
60 self._control = control
61
62
63 def OnCreate(self, doc, flags):
64 config = wx.ConfigBase_Get()
65 windowLoc = self._service.GetEmbeddedWindowLocation()
66 if windowLoc == FLOATING_MINIFRAME:
67 pos = config.ReadInt(self._service.GetServiceName() + "FrameXLoc", -1), config.ReadInt(self._service.GetServiceName() + "FrameYLoc", -1)
68 # make sure frame is visible
69 screenWidth = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_X)
70 screenHeight = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)
71 if pos[0] < 0 or pos[0] >= screenWidth or pos[1] < 0 or pos[1] >= screenHeight:
72 pos = wx.DefaultPosition
73
74 size = wx.Size(config.ReadInt(self._service.GetServiceName() + "FrameXSize", -1), config.ReadInt(self._service.GetServiceName() + "FrameYSize", -1))
75 title = _(self._service.GetServiceName())
76 if wx.GetApp().GetDocumentManager().GetFlags() & wx.lib.docview.DOC_SDI and wx.GetApp().GetAppName():
77 title = title + " - " + wx.GetApp().GetAppName()
78 frame = wx.MiniFrame(wx.GetApp().GetTopWindow(), -1, title, pos = pos, size = size, style = wx.CLOSE_BOX|wx.CAPTION|wx.SYSTEM_MENU)
79 wx.EVT_CLOSE(frame, self.OnCloseWindow)
80 elif wx.GetApp().IsMDI():
81 self._embeddedWindow = wx.GetApp().GetTopWindow().GetEmbeddedWindow(windowLoc)
82 frame = self._embeddedWindow
83 else:
84 pos = config.ReadInt(self._service.GetServiceName() + "FrameXLoc", -1), config.ReadInt(self._service.GetServiceName() + "FrameYLoc", -1)
85 # make sure frame is visible
86 screenWidth = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_X)
87 screenHeight = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)
88 if pos[0] < 0 or pos[0] >= screenWidth or pos[1] < 0 or pos[1] >= screenHeight:
89 pos = wx.DefaultPosition
90
91 size = wx.Size(config.ReadInt(self._service.GetServiceName() + "FrameXSize", -1), config.ReadInt(self._service.GetServiceName() + "FrameYSize", -1))
92 title = _(self._service.GetServiceName())
93 if wx.GetApp().GetDocumentManager().GetFlags() & wx.lib.docview.DOC_SDI and wx.GetApp().GetAppName():
94 title = title + " - " + wx.GetApp().GetAppName()
95 frame = wx.GetApp().CreateDocumentFrame(self, doc, flags, pos = pos, size = size)
96 frame.SetTitle(title)
97 if config.ReadInt(self._service.GetServiceName() + "FrameMaximized", False):
98 frame.Maximize(True)
99 wx.EVT_CLOSE(frame, self.OnCloseWindow)
100
101 self.SetFrame(frame)
102 sizer = wx.BoxSizer(wx.VERTICAL)
103
104 windowLoc = self._service.GetEmbeddedWindowLocation()
105 if self._embeddedWindow or windowLoc == FLOATING_MINIFRAME:
106 if (self._service.GetEmbeddedWindowLocation() == wx.lib.pydocview.EMBEDDED_WINDOW_BOTTOM):
107 if ServiceView.bottomTab == None:
108 ServiceView.bottomTab = wx.Notebook(frame, wx.NewId(), (0,0), (100,100), wx.LB_DEFAULT, "Bottom Tab")
109 sizer.Add(ServiceView.bottomTab, 1, wx.TOP|wx.EXPAND, 4)
110 def OnFrameResize(event):
111 ServiceView.bottomTab.SetSize(ServiceView.bottomTab.GetParent().GetSize())
112 frame.Bind(wx.EVT_SIZE, OnFrameResize)
113 # Factor this out.
114 self._control = self._CreateControl(ServiceView.bottomTab, wx.NewId())
115 if self._control != None:
116 ServiceView.bottomTab.AddPage(self._control, self._service.GetServiceName())
117 ServiceView.bottomTab.Layout()
118 else:
119 # Factor this out.
120 self._control = self._CreateControl(frame, wx.NewId())
121 sizer.Add(self._control)
122 else:
123 # Factor this out.
124 self._control = self._CreateControl(frame, wx.NewId())
125 sizer.Add(self._control, 1, wx.EXPAND, 0)
126 frame.SetSizer(sizer)
127 frame.Layout()
128
129 return True
130
131
132 def OnCloseWindow(self, event):
133 frame = self.GetFrame()
134 config = wx.ConfigBase_Get()
135 if frame and not self._embeddedWindow:
136 if not frame.IsMaximized():
137 config.WriteInt(self._service.GetServiceName() + "FrameXLoc", frame.GetPositionTuple()[0])
138 config.WriteInt(self._service.GetServiceName() + "FrameYLoc", frame.GetPositionTuple()[1])
139 config.WriteInt(self._service.GetServiceName() + "FrameXSize", frame.GetSizeTuple()[0])
140 config.WriteInt(self._service.GetServiceName() + "FrameYSize", frame.GetSizeTuple()[1])
141 config.WriteInt(self._service.GetServiceName() + "FrameMaximized", frame.IsMaximized())
142
143 if not self._embeddedWindow:
144 windowLoc = self._service.GetEmbeddedWindowLocation()
145 if windowLoc == FLOATING_MINIFRAME:
146 # don't destroy it, just hide it
147 frame.Hide()
148 else:
149 # Call the original OnCloseWindow, could have subclassed SDIDocFrame and MDIDocFrame but this is easier since it will work for both SDI and MDI frames without subclassing both
150 frame.OnCloseWindow(event)
151
152
153 def Activate(self, activate = True):
154 """ Dummy function for SDI mode """
155 pass
156
157
158 def Close(self, deleteWindow = True):
159 """
160 Closes the view by calling OnClose. If deleteWindow is true, this
161 function should delete the window associated with the view.
162 """
163 if deleteWindow:
164 self.Destroy()
165
166 return True
167
168
169 #----------------------------------------------------------------------------
170 # Callback Methods
171 #----------------------------------------------------------------------------
172
173 def SetCallback(self, callback):
174 """ Sets in the event table for a doubleclick to invoke the given callback.
175 Additional calls to this method overwrites the previous entry and only the last set callback will be invoked.
176 """
177 wx.stc.EVT_STC_DOUBLECLICK(self.GetControl(), self.GetControl().GetId(), callback)
178
179
180 #----------------------------------------------------------------------------
181 # Display Methods
182 #----------------------------------------------------------------------------
183
184 def IsShown(self):
185 if not self.GetFrame():
186 return False
187 return self.GetFrame().IsShown()
188
189
190 def Hide(self):
191 self.Show(False)
192
193
194 def Show(self, show = True):
195 self.GetFrame().Show(show)
196 if self._embeddedWindow:
197 mdiParentFrame = wx.GetApp().GetTopWindow()
198 mdiParentFrame.ShowEmbeddedWindow(self.GetFrame(), show)
199
200
201class Service(wx.lib.pydocview.DocService):
202
203
204 #----------------------------------------------------------------------------
205 # Constants
206 #----------------------------------------------------------------------------
207 SHOW_WINDOW = wx.NewId() # keep this line for each subclass, need unique ID for each Service
208
209
210 def __init__(self, serviceName, embeddedWindowLocation = wx.lib.pydocview.EMBEDDED_WINDOW_LEFT):
211 self._serviceName = serviceName
212 self._embeddedWindowLocation = embeddedWindowLocation
213 self._view = None
214
215
216 def GetEmbeddedWindowLocation(self):
217 return self._embeddedWindowLocation
218
219
220 def SetEmbeddedWindowLocation(self, embeddedWindowLocation):
221 self._embeddedWindowLocation = embeddedWindowLocation
222
223
224 def InstallControls(self, frame, menuBar = None, toolBar = None, statusBar = None, document = None):
225 viewMenu = menuBar.GetMenu(menuBar.FindMenu(_("&View")))
226 menuItemPos = self.GetMenuItemPos(viewMenu, viewMenu.FindItem(_("&Status Bar"))) + 1
227
228 viewMenu.InsertCheckItem(menuItemPos, self.SHOW_WINDOW, self.GetMenuString(), self.GetMenuDescr())
229 wx.EVT_MENU(frame, self.SHOW_WINDOW, frame.ProcessEvent)
230 wx.EVT_UPDATE_UI(frame, self.SHOW_WINDOW, frame.ProcessUpdateUIEvent)
231
232 return True
233
234
235 def GetServiceName(self):
236 """ String used to save out Service View configuration information """
237 return self._serviceName
238
239
240 def GetMenuString(self):
241 """ Need to override this method to provide menu item for showing Service View """
242 return _(self.GetServiceName())
243
244
245 def GetMenuDescr(self):
246 """ Need to override this method to provide menu item for showing Service View """
247 return _("Show or hides the %s window") % self.GetMenuString()
248
249
250 #----------------------------------------------------------------------------
251 # Event Processing Methods
252 #----------------------------------------------------------------------------
253
254 def ProcessEvent(self, event):
255 id = event.GetId()
256 if id == self.SHOW_WINDOW:
257 self.ToggleWindow(event)
258 return True
259 else:
260 return False
261
262
263 def ProcessUpdateUIEvent(self, event):
264 id = event.GetId()
265 if id == self.SHOW_WINDOW:
266 event.Check(self._view != None and self._view.IsShown())
267 event.Enable(True)
268 return True
269 else:
270 return False
271
272
273 #----------------------------------------------------------------------------
274 # View Methods
275 #----------------------------------------------------------------------------
276
277 def _CreateView(self):
278 """ This method needs to be overridden with corresponding ServiceView """
279 return ServiceView(self)
280
281
282 def GetView(self):
283 # Window Menu Service Method
284 return self._view
285
286
287 def SetView(self, view):
288 self._view = view
289
290
291 def ShowWindow(self, show = True):
292 if show:
293 if self._view:
294 if not self._view.IsShown():
295 self._view.Show()
296 else:
297 view = self._CreateView()
298 view.OnCreate(None, flags = 0)
299 self.SetView(view)
300 else:
301 if self._view:
302 if self._view.IsShown():
303 self._view.Hide()
304
305
306 def HideWindow(self):
307 self.ShowWindow(False)
308
309
310 def ToggleWindow(self, event):
311 show = event.IsChecked()
312 wx.ConfigBase_Get().WriteInt(self.GetServiceName()+"Shown", show)
313 self.ShowWindow(show)
314
315
316 def OnCloseFrame(self, event):
317 if not self._view:
318 return True
319
320 if wx.GetApp().IsMDI():
321 self._view.OnCloseWindow(event)
322 # This is called when any SDI frame is closed, so need to check if message window is closing or some other window
323 elif self._view == event.GetEventObject().GetView():
324 self.SetView(None)
325
326 return True
327