]> git.saurik.com Git - wxWidgets.git/blame - wxPython/samples/docview/DocViewDemo.py
PyCrust now has an option for showing/hiding the notebook.
[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$
3fa8f722 9# Copyright: (c) 2003-2005 ActiveGrid, Inc. (Port of wxWindows classes by Julian Smart et al)
bbf7159c 10# License: wxWindows License
d1dc2b32
RD
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()
74b89458 94 self._frame.Show(True)
d1dc2b32
RD
95 self.Activate(True)
96 return True
97 else: # flags & wx.lib.docview.DOC_SDI
98 self._frame = wx.GetApp().CreateChildFrame(doc, self)
99 width, height = self._frame.GetClientSize()
100 self._textsw = MyTextWindow(self, self._frame, (0, 0), (width, height), wx.TE_MULTILINE)
101 self._frame.SetTitle(_("TextEditView"))
102 self._frame.Show(True)
103 self.Activate(True)
104 return True
105
106
107 # Since ProcessEvent is not virtual, we have to trap the relevant events using this pseudo-ProcessEvent instead of EVT_MENU
108 def ProcessEvent(self, event):
109 id = event.GetId()
110 if id == wx.ID_UNDO:
111 self._textsw.Undo()
112 return True
113 elif id == wx.ID_REDO:
114 self._textsw.Redo()
115 return True
116 else:
117 return wx.lib.docview.View.ProcessEvent(self, event)
118
119
120 def GetTextSW(self):
121 return self._textsw
122
123
124 def OnDraw(self, dc):
2eeaec19 125 """ For Print and Print Preview """
d1dc2b32
RD
126 pass
127
128
129 def OnClose(self, deleteWindow = True):
130 if not self.GetDocument().Close():
131 return False
132 self.Activate(False)
133 if deleteWindow:
134 if self.GetDocument().GetDocumentManager().GetMaxDocsOpen() == 1 and self.GetDocument().GetDocumentManager().GetFlags() & wx.lib.docview.DOC_SDI:
135 if self._textsw:
136 self._textsw.Destroy()
137 return True
138 else:
139 self._frame.Destroy()
140 return True
141 return True
142
143
144class MyTextWindow(wx.TextCtrl):
145
146
147 def __init__(self, view, frame, pos, size, style):
148 wx.TextCtrl.__init__(self, frame, -1, "", pos, size, style)
149 self._view = view
150
151
152
153#----------------------------------------------------------------------
154#----------------------------------------------------------------------
155# TextEdit Sample Application
156
157
158class MyApp(wx.PySimpleApp):
159
160
161 def OnInit(self):
162
163
164 demoMode = wx.GetSingleChoiceIndex(_("Select the demo mode"),
165 _("wxPython DocView Demo"),
166 [_("SDI Single Document"), _("SDI"), _("MDI")])
167
168 if demoMode == 0 or demoMode == 1:
169 flags = wx.lib.docview.DOC_SDI
170 elif demoMode == 2:
171 flags = wx.lib.docview.DOC_MDI
172 else:
173 return False
174
175 self.SetAppName(_("DocView Demo"))
176
177 docManager = wx.lib.docview.DocManager(flags = flags)
178 docManager.AssociateTemplate(wx.lib.docview.DocTemplate(docManager,
179 _("Text"),
180 "*.text;*.txt",
181 _("Text"),
182 _(".txt"),
183 _("Text Document"),
184 _("Text View"),
185 TextEditDocument,
186 TextEditView))
187 #if wx.Platform == "__WXMAC__":
188 # wx.FileName.MacRegisterDefaultTypeAndCreator("txt", 'TEXT', 'WXMA')
189
190 if demoMode == 0:
191 docManager.SetMaxDocsOpen(1)
192
193 if demoMode == 2: # MDI
194 self._frame = MyMDIFrame(docManager, None, -1, _("DocView Demo"), (0, 0), (500, 400), wx.DEFAULT_FRAME_STYLE)
195 else: # SDI
196 self._frame = MyFrame(docManager, None, -1, _("DocView Demo"), (0, 0), (500, 400), wx.DEFAULT_FRAME_STYLE)
197
198 fileMenu = wx.Menu()
199 editMenu = None
200
201 fileMenu.Append(wx.ID_NEW, _("&New..."))
202 fileMenu.Append(wx.ID_OPEN, _("&Open..."))
203
204 if demoMode == 2: # MDI
205 fileMenu.Append(wx.ID_CLOSE, _("&Close"))
206 fileMenu.AppendSeparator()
207
208 if demoMode == 0 or demoMode == 2: # Single Doc or MDI
209 fileMenu.Append(wx.ID_SAVE, _("&Save"))
210 fileMenu.Append(wx.ID_SAVEAS, _("Save &As"))
211 fileMenu.AppendSeparator()
212 fileMenu.Append(wx.ID_PRINT, _("&Print"))
213 fileMenu.Append(wx.ID_PRINT_SETUP, _("Page &Setup"))
214 fileMenu.Append(wx.ID_PREVIEW, _("Print Pre&view"))
215
216 editMenu = wx.Menu()
217 editMenu.Append(wx.ID_UNDO, _("&Undo"))
218 editMenu.Append(wx.ID_REDO, _("&Redo"))
219
220 self._frame.editMenu = editMenu
221
222 fileMenu.AppendSeparator()
223 fileMenu.Append(wx.ID_EXIT, _("E&xit"))
224
225 docManager.FileHistoryUseMenu(fileMenu)
226
227 helpMenu = wx.Menu()
228 helpMenu.Append(wx.ID_ABOUT, _("&About"))
229
230 menuBar = wx.MenuBar()
231 menuBar.Append(fileMenu, _("&File"))
232 if editMenu:
233 menuBar.Append(editMenu, _("&Edit"))
234 menuBar.Append(helpMenu, _("&Help"))
235
236 self._frame.SetMenuBar(menuBar)
237 self._frame.Centre(wx.BOTH)
238 self._frame.Show(True)
239
240 self.SetTopWindow(self._frame)
241
242 if demoMode == 0: # Single doc
243 docManager.OnFileNew(None)
244
245 return True
246
247
248 def GetMainFrame(self):
249 return self._frame
250
251
252 def GetDemoMode(self):
253 return self._demoMode
254
255
256 def CreateChildFrame(self, doc, view):
257 subframe = wx.lib.docview.DocChildFrame(doc, view, self.GetMainFrame(), -1, wx.GetApp().GetAppName(), (10, 10), (300, 300), wx.DEFAULT_FRAME_STYLE)
258
259 fileMenu = wx.Menu()
260 fileMenu.Append(wx.ID_NEW, _("&New"))
261 fileMenu.Append(wx.ID_OPEN, _("&Open"))
262 fileMenu.Append(wx.ID_CLOSE, _("&Close"))
263 fileMenu.AppendSeparator()
264 fileMenu.Append(wx.ID_SAVE, _("&Save"))
265 fileMenu.Append(wx.ID_SAVEAS, _("Save &As"))
266 fileMenu.AppendSeparator()
267 fileMenu.Append(wx.ID_PRINT, _("&Print"))
268 fileMenu.Append(wx.ID_PRINT_SETUP, _("Page &Setup"))
269 fileMenu.Append(wx.ID_PREVIEW, _("Print Pre&view"))
270
271 editMenu = wx.Menu()
272 editMenu.Append(wx.ID_UNDO, _("&Undo"))
273 editMenu.Append(wx.ID_REDO, _("&Redo"))
274
275 helpMenu = wx.Menu()
276 helpMenu.Append(wx.ID_ABOUT, _("&About"))
277
278 menuBar = wx.MenuBar()
279
280 menuBar.Append(fileMenu, _("&File"))
281 menuBar.Append(editMenu, _("&Edit"))
282 menuBar.Append(helpMenu, _("&Help"))
283
284 subframe.SetMenuBar(menuBar)
285 subframe.Centre(wx.BOTH)
286 return subframe
287
288
289class MyFrame(wx.lib.docview.DocParentFrame):
290
291
292 def __init__(self, manager, frame, id, title, pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, name = "frame"):
293 wx.lib.docview.DocParentFrame.__init__(self, manager, frame, id, title, pos, size, style, name)
294 self._frame = frame
295 wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
296
297
298 def OnAbout(self, event):
299 wx.MessageBox(wx.GetApp().GetAppName(), _("About DocView"))
300
301
302 def GetMainFrame(self):
303 return self._frame
304
305
306class MyMDIFrame(wx.lib.docview.DocMDIParentFrame):
307
308 def __init__(self, manager, frame, id, title, pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, name = "frame"):
309 wx.lib.docview.DocMDIParentFrame.__init__(self, manager, frame, id, title, pos, size, style, name)
310 self._frame = frame
311 wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
312
313
314 def OnAbout(self, event):
315 wx.MessageBox(wx.GetApp().GetAppName(), _("About DocView"))
316
317
318 def GetMainFrame(self):
319 return self._frame
320
321
322app = MyApp()
323app.MainLoop()
324
325