]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/tool/Service.py
Patch from Morgan Hua for 'better looking dialog box for "Add All
[wxWidgets.git] / wxPython / samples / ide / activegrid / tool / Service.py
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
13 import wx
14 import wx.lib.docview
15 import wx.lib.pydocview
16 _ = wx.GetTranslation
17
18
19 FLOATING_MINIFRAME = -1
20
21
22 class 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 if self.GetFrame():
196 self.GetFrame().Show(show)
197 if self._embeddedWindow:
198 mdiParentFrame = wx.GetApp().GetTopWindow()
199 mdiParentFrame.ShowEmbeddedWindow(self.GetFrame(), show)
200
201
202 class Service(wx.lib.pydocview.DocService):
203
204
205 #----------------------------------------------------------------------------
206 # Constants
207 #----------------------------------------------------------------------------
208 SHOW_WINDOW = wx.NewId() # keep this line for each subclass, need unique ID for each Service
209
210
211 def __init__(self, serviceName, embeddedWindowLocation = wx.lib.pydocview.EMBEDDED_WINDOW_LEFT):
212 self._serviceName = serviceName
213 self._embeddedWindowLocation = embeddedWindowLocation
214 self._view = None
215
216
217 def GetEmbeddedWindowLocation(self):
218 return self._embeddedWindowLocation
219
220
221 def SetEmbeddedWindowLocation(self, embeddedWindowLocation):
222 self._embeddedWindowLocation = embeddedWindowLocation
223
224
225 def InstallControls(self, frame, menuBar = None, toolBar = None, statusBar = None, document = None):
226 viewMenu = menuBar.GetMenu(menuBar.FindMenu(_("&View")))
227 menuItemPos = self.GetMenuItemPos(viewMenu, viewMenu.FindItem(_("&Status Bar"))) + 1
228
229 viewMenu.InsertCheckItem(menuItemPos, self.SHOW_WINDOW, self.GetMenuString(), self.GetMenuDescr())
230 wx.EVT_MENU(frame, self.SHOW_WINDOW, frame.ProcessEvent)
231 wx.EVT_UPDATE_UI(frame, self.SHOW_WINDOW, frame.ProcessUpdateUIEvent)
232
233 return True
234
235
236 def GetServiceName(self):
237 """ String used to save out Service View configuration information """
238 return self._serviceName
239
240
241 def GetMenuString(self):
242 """ Need to override this method to provide menu item for showing Service View """
243 return _(self.GetServiceName())
244
245
246 def GetMenuDescr(self):
247 """ Need to override this method to provide menu item for showing Service View """
248 return _("Show or hides the %s window") % self.GetMenuString()
249
250
251 #----------------------------------------------------------------------------
252 # Event Processing Methods
253 #----------------------------------------------------------------------------
254
255 def ProcessEvent(self, event):
256 id = event.GetId()
257 if id == self.SHOW_WINDOW:
258 self.ToggleWindow(event)
259 return True
260 else:
261 return False
262
263
264 def ProcessUpdateUIEvent(self, event):
265 id = event.GetId()
266 if id == self.SHOW_WINDOW:
267 event.Check(self._view != None and self._view.IsShown())
268 event.Enable(True)
269 return True
270 else:
271 return False
272
273
274 #----------------------------------------------------------------------------
275 # View Methods
276 #----------------------------------------------------------------------------
277
278 def _CreateView(self):
279 """ This method needs to be overridden with corresponding ServiceView """
280 return ServiceView(self)
281
282
283 def GetView(self):
284 # Window Menu Service Method
285 return self._view
286
287
288 def SetView(self, view):
289 self._view = view
290
291
292 def ShowWindow(self, show = True):
293 if show:
294 if self._view:
295 if not self._view.IsShown():
296 self._view.Show()
297 else:
298 view = self._CreateView()
299 view.OnCreate(None, flags = 0)
300 self.SetView(view)
301 else:
302 if self._view:
303 if self._view.IsShown():
304 self._view.Hide()
305
306
307 def HideWindow(self):
308 self.ShowWindow(False)
309
310
311 def ToggleWindow(self, event):
312 show = event.IsChecked()
313 wx.ConfigBase_Get().WriteInt(self.GetServiceName()+"Shown", show)
314 self.ShowWindow(show)
315
316
317 def OnCloseFrame(self, event):
318 if not self._view:
319 return True
320
321 if wx.GetApp().IsMDI():
322 self._view.OnCloseWindow(event)
323 # This is called when any SDI frame is closed, so need to check if message window is closing or some other window
324 elif self._view == event.GetEventObject().GetView():
325 self.SetView(None)
326
327 return True
328