]> git.saurik.com Git - wxWidgets.git/blame - wxPython/samples/docview/DocViewDemo.py
note wxURI change
[wxWidgets.git] / wxPython / samples / docview / DocViewDemo.py
CommitLineData
d1dc2b32
RD
1#----------------------------------------------------------------------------
2# Name: DocViewDemo.py
3# Purpose: Port of the wxWindows docview sample classes
4#
5# Author: Peter Yared
6#
7# Created: 8/1/03
8# CVS-ID: $Id$
9# Copyright: (c) 2003, 2004 ActiveGrid, Inc. (Port of wxWindows classes by Julian Smart et al)
10# License: wxWindows license
11#----------------------------------------------------------------------------
12
13
14
15#----------------------------------------------------------------------
16# This isn't the most object oriented code (it is somewhat repetitive,
17# but it is true to the wxWindows C++ demos.
18#----------------------------------------------------------------------
19
20
21import wx
22import wx.lib.docview
23_ = wx.GetTranslation
24
25
26#----------------------------------------------------------------------
27#----------------------------------------------------------------------
28# TextEdit document and view classes
29
30class TextEditDocument(wx.lib.docview.Document):
31
32
33 def OnSaveDocument(self, filename):
34 view = self.GetFirstView()
35 if not view.GetTextSW().SaveFile(filename):
36 return False
37 self.Modify(False)
38 self.SetDocumentSaved(True)
39## if wx.Platform == "__WXMAC__":
40## fn = wx.Filename(filename)
41## fn.MacSetDefaultTypeAndCreator()
42 return True
43
44
45 def OnOpenDocument(self, filename):
46 view = self.GetFirstView()
47 if not view.GetTextSW().LoadFile(filename):
48 return False
49 self.SetFilename(filename, True)
50 self.Modify(False)
51 self.UpdateAllViews()
52 self._savedYet = True
53 return True
54
55
56 def IsModified(self):
57 view = self.GetFirstView()
58 if view:
59 return wx.lib.docview.Document.IsModified(self) or (view.GetTextSW() and view.GetTextSW().IsModified())
60 else:
61 return wx.lib.docview.Document.IsModified(self)
62
63
64 def Modify(self, mod):
65 view = self.GetFirstView()
66 wx.lib.docview.Document.Modify(self, mod)
67 if not mod and view and view.GetTextSW():
68 view.GetTextSW().DiscardEdits()
69
70
71class TextEditView(wx.lib.docview.View):
72
73
74 def OnCreate(self, doc, flags):
75 flags = doc.GetDocumentManager().GetFlags()
76 if flags & wx.lib.docview.DOC_SDI and doc.GetDocumentManager().GetMaxDocsOpen() == 1:
77 self._frame = wx.GetApp().GetMainFrame()
78 self.SetFrame(self._frame)
79 sizer = wx.BoxSizer()
80 self._textsw = MyTextWindow(self, self._frame, wx.DefaultPosition, wx.DefaultSize, wx.TE_MULTILINE)
81 sizer.Add(self._textsw, 1, wx.EXPAND, 0)
82 self._frame.SetSizer(sizer)
83 self._frame.Layout()
84 self.Activate(True)
85 return True
86 elif flags & wx.lib.docview.DOC_MDI:
87 self._frame = wx.lib.docview.DocMDIChildFrame(doc, self, wx.GetApp().GetMainFrame(), -1, wx.GetApp().GetAppName(), (10, 10), (300, 300), wx.DEFAULT_FRAME_STYLE)
88 self.SetFrame(self._frame)
89 sizer = wx.BoxSizer()
90 self._textsw = MyTextWindow(self, self._frame, wx.DefaultPosition, wx.DefaultSize, wx.TE_MULTILINE)
91 sizer.Add(self._textsw, 1, wx.EXPAND, 0)
92 self._frame.SetSizer(sizer)
93 self._frame.Layout()
94 self.Activate(True)
95 return True
96 else: # flags & wx.lib.docview.DOC_SDI
97 self._frame = wx.GetApp().CreateChildFrame(doc, self)
98 width, height = self._frame.GetClientSize()
99 self._textsw = MyTextWindow(self, self._frame, (0, 0), (width, height), wx.TE_MULTILINE)
100 self._frame.SetTitle(_("TextEditView"))
101 self._frame.Show(True)
102 self.Activate(True)
103 return True
104
105
106 # Since ProcessEvent is not virtual, we have to trap the relevant events using this pseudo-ProcessEvent instead of EVT_MENU
107 def ProcessEvent(self, event):
108 id = event.GetId()
109 if id == wx.ID_UNDO:
110 self._textsw.Undo()
111 return True
112 elif id == wx.ID_REDO:
113 self._textsw.Redo()
114 return True
115 else:
116 return wx.lib.docview.View.ProcessEvent(self, event)
117
118
119 def GetTextSW(self):
120 return self._textsw
121
122
123 def OnDraw(self, dc):
124 pass
125
126
127 def OnClose(self, deleteWindow = True):
128 if not self.GetDocument().Close():
129 return False
130 self.Activate(False)
131 if deleteWindow:
132 if self.GetDocument().GetDocumentManager().GetMaxDocsOpen() == 1 and self.GetDocument().GetDocumentManager().GetFlags() & wx.lib.docview.DOC_SDI:
133 if self._textsw:
134 self._textsw.Destroy()
135 return True
136 else:
137 self._frame.Destroy()
138 return True
139 return True
140
141
142class MyTextWindow(wx.TextCtrl):
143
144
145 def __init__(self, view, frame, pos, size, style):
146 wx.TextCtrl.__init__(self, frame, -1, "", pos, size, style)
147 self._view = view
148
149
150
151#----------------------------------------------------------------------
152#----------------------------------------------------------------------
153# TextEdit Sample Application
154
155
156class MyApp(wx.PySimpleApp):
157
158
159 def OnInit(self):
160
161
162 demoMode = wx.GetSingleChoiceIndex(_("Select the demo mode"),
163 _("wxPython DocView Demo"),
164 [_("SDI Single Document"), _("SDI"), _("MDI")])
165
166 if demoMode == 0 or demoMode == 1:
167 flags = wx.lib.docview.DOC_SDI
168 elif demoMode == 2:
169 flags = wx.lib.docview.DOC_MDI
170 else:
171 return False
172
173 self.SetAppName(_("DocView Demo"))
174
175 docManager = wx.lib.docview.DocManager(flags = flags)
176 docManager.AssociateTemplate(wx.lib.docview.DocTemplate(docManager,
177 _("Text"),
178 "*.text;*.txt",
179 _("Text"),
180 _(".txt"),
181 _("Text Document"),
182 _("Text View"),
183 TextEditDocument,
184 TextEditView))
185 #if wx.Platform == "__WXMAC__":
186 # wx.FileName.MacRegisterDefaultTypeAndCreator("txt", 'TEXT', 'WXMA')
187
188 if demoMode == 0:
189 docManager.SetMaxDocsOpen(1)
190
191 if demoMode == 2: # MDI
192 self._frame = MyMDIFrame(docManager, None, -1, _("DocView Demo"), (0, 0), (500, 400), wx.DEFAULT_FRAME_STYLE)
193 else: # SDI
194 self._frame = MyFrame(docManager, None, -1, _("DocView Demo"), (0, 0), (500, 400), wx.DEFAULT_FRAME_STYLE)
195
196 fileMenu = wx.Menu()
197 editMenu = None
198
199 fileMenu.Append(wx.ID_NEW, _("&New..."))
200 fileMenu.Append(wx.ID_OPEN, _("&Open..."))
201
202 if demoMode == 2: # MDI
203 fileMenu.Append(wx.ID_CLOSE, _("&Close"))
204 fileMenu.AppendSeparator()
205
206 if demoMode == 0 or demoMode == 2: # Single Doc or MDI
207 fileMenu.Append(wx.ID_SAVE, _("&Save"))
208 fileMenu.Append(wx.ID_SAVEAS, _("Save &As"))
209 fileMenu.AppendSeparator()
210 fileMenu.Append(wx.ID_PRINT, _("&Print"))
211 fileMenu.Append(wx.ID_PRINT_SETUP, _("Page &Setup"))
212 fileMenu.Append(wx.ID_PREVIEW, _("Print Pre&view"))
213
214 editMenu = wx.Menu()
215 editMenu.Append(wx.ID_UNDO, _("&Undo"))
216 editMenu.Append(wx.ID_REDO, _("&Redo"))
217
218 self._frame.editMenu = editMenu
219
220 fileMenu.AppendSeparator()
221 fileMenu.Append(wx.ID_EXIT, _("E&xit"))
222
223 docManager.FileHistoryUseMenu(fileMenu)
224
225 helpMenu = wx.Menu()
226 helpMenu.Append(wx.ID_ABOUT, _("&About"))
227
228 menuBar = wx.MenuBar()
229 menuBar.Append(fileMenu, _("&File"))
230 if editMenu:
231 menuBar.Append(editMenu, _("&Edit"))
232 menuBar.Append(helpMenu, _("&Help"))
233
234 self._frame.SetMenuBar(menuBar)
235 self._frame.Centre(wx.BOTH)
236 self._frame.Show(True)
237
238 self.SetTopWindow(self._frame)
239
240 if demoMode == 0: # Single doc
241 docManager.OnFileNew(None)
242
243 return True
244
245
246 def GetMainFrame(self):
247 return self._frame
248
249
250 def GetDemoMode(self):
251 return self._demoMode
252
253
254 def CreateChildFrame(self, doc, view):
255 subframe = wx.lib.docview.DocChildFrame(doc, view, self.GetMainFrame(), -1, wx.GetApp().GetAppName(), (10, 10), (300, 300), wx.DEFAULT_FRAME_STYLE)
256
257 fileMenu = wx.Menu()
258 fileMenu.Append(wx.ID_NEW, _("&New"))
259 fileMenu.Append(wx.ID_OPEN, _("&Open"))
260 fileMenu.Append(wx.ID_CLOSE, _("&Close"))
261 fileMenu.AppendSeparator()
262 fileMenu.Append(wx.ID_SAVE, _("&Save"))
263 fileMenu.Append(wx.ID_SAVEAS, _("Save &As"))
264 fileMenu.AppendSeparator()
265 fileMenu.Append(wx.ID_PRINT, _("&Print"))
266 fileMenu.Append(wx.ID_PRINT_SETUP, _("Page &Setup"))
267 fileMenu.Append(wx.ID_PREVIEW, _("Print Pre&view"))
268
269 editMenu = wx.Menu()
270 editMenu.Append(wx.ID_UNDO, _("&Undo"))
271 editMenu.Append(wx.ID_REDO, _("&Redo"))
272
273 helpMenu = wx.Menu()
274 helpMenu.Append(wx.ID_ABOUT, _("&About"))
275
276 menuBar = wx.MenuBar()
277
278 menuBar.Append(fileMenu, _("&File"))
279 menuBar.Append(editMenu, _("&Edit"))
280 menuBar.Append(helpMenu, _("&Help"))
281
282 subframe.SetMenuBar(menuBar)
283 subframe.Centre(wx.BOTH)
284 return subframe
285
286
287class MyFrame(wx.lib.docview.DocParentFrame):
288
289
290 def __init__(self, manager, frame, id, title, pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, name = "frame"):
291 wx.lib.docview.DocParentFrame.__init__(self, manager, frame, id, title, pos, size, style, name)
292 self._frame = frame
293 wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
294
295
296 def OnAbout(self, event):
297 wx.MessageBox(wx.GetApp().GetAppName(), _("About DocView"))
298
299
300 def GetMainFrame(self):
301 return self._frame
302
303
304class MyMDIFrame(wx.lib.docview.DocMDIParentFrame):
305
306 def __init__(self, manager, frame, id, title, pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, name = "frame"):
307 wx.lib.docview.DocMDIParentFrame.__init__(self, manager, frame, id, title, pos, size, style, name)
308 self._frame = frame
309 wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
310
311
312 def OnAbout(self, event):
313 wx.MessageBox(wx.GetApp().GetAppName(), _("About DocView"))
314
315
316 def GetMainFrame(self):
317 return self._frame
318
319
320app = MyApp()
321app.MainLoop()
322
323