]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/docview/DocViewDemo.py
1 #----------------------------------------------------------------------------
3 # Purpose: Port of the wxWindows docview sample classes
9 # Copyright: (c) 2003-2005 ActiveGrid, Inc. (Port of wxWindows classes by Julian Smart et al)
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
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 #----------------------------------------------------------------------
26 #----------------------------------------------------------------------
27 #----------------------------------------------------------------------
28 # TextEdit document and view classes
30 class TextEditDocument(wx
.lib
.docview
.Document
):
33 def OnSaveDocument(self
, filename
):
34 view
= self
.GetFirstView()
35 if not view
.GetTextSW().SaveFile(filename
):
38 self
.SetDocumentSaved(True)
39 ## if wx.Platform == "__WXMAC__":
40 ## fn = wx.Filename(filename)
41 ## fn.MacSetDefaultTypeAndCreator()
45 def OnOpenDocument(self
, filename
):
46 view
= self
.GetFirstView()
47 if not view
.GetTextSW().LoadFile(filename
):
49 self
.SetFilename(filename
, True)
57 view
= self
.GetFirstView()
59 return wx
.lib
.docview
.Document
.IsModified(self
) or (view
.GetTextSW() and view
.GetTextSW().IsModified())
61 return wx
.lib
.docview
.Document
.IsModified(self
)
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()
71 class TextEditView(wx
.lib
.docview
.View
):
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
)
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
)
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
)
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
)
94 self
._frame
.Show(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)
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
):
113 elif id == wx
.ID_REDO
:
117 return wx
.lib
.docview
.View
.ProcessEvent(self
, event
)
124 def OnDraw(self
, dc
):
128 def OnClose(self
, deleteWindow
= True):
129 if not self
.GetDocument().Close():
133 if self
.GetDocument().GetDocumentManager().GetMaxDocsOpen() == 1 and self
.GetDocument().GetDocumentManager().GetFlags() & wx
.lib
.docview
.DOC_SDI
:
135 self
._textsw
.Destroy()
138 self
._frame
.Destroy()
143 class MyTextWindow(wx
.TextCtrl
):
146 def __init__(self
, view
, frame
, pos
, size
, style
):
147 wx
.TextCtrl
.__init
__(self
, frame
, -1, "", pos
, size
, style
)
152 #----------------------------------------------------------------------
153 #----------------------------------------------------------------------
154 # TextEdit Sample Application
157 class MyApp(wx
.PySimpleApp
):
163 demoMode
= wx
.GetSingleChoiceIndex(_("Select the demo mode"),
164 _("wxPython DocView Demo"),
165 [_("SDI Single Document"), _("SDI"), _("MDI")])
167 if demoMode
== 0 or demoMode
== 1:
168 flags
= wx
.lib
.docview
.DOC_SDI
170 flags
= wx
.lib
.docview
.DOC_MDI
174 self
.SetAppName(_("DocView Demo"))
176 docManager
= wx
.lib
.docview
.DocManager(flags
= flags
)
177 docManager
.AssociateTemplate(wx
.lib
.docview
.DocTemplate(docManager
,
186 #if wx.Platform == "__WXMAC__":
187 # wx.FileName.MacRegisterDefaultTypeAndCreator("txt", 'TEXT', 'WXMA')
190 docManager
.SetMaxDocsOpen(1)
192 if demoMode
== 2: # MDI
193 self
._frame
= MyMDIFrame(docManager
, None, -1, _("DocView Demo"), (0, 0), (500, 400), wx
.DEFAULT_FRAME_STYLE
)
195 self
._frame
= MyFrame(docManager
, None, -1, _("DocView Demo"), (0, 0), (500, 400), wx
.DEFAULT_FRAME_STYLE
)
200 fileMenu
.Append(wx
.ID_NEW
, _("&New..."))
201 fileMenu
.Append(wx
.ID_OPEN
, _("&Open..."))
203 if demoMode
== 2: # MDI
204 fileMenu
.Append(wx
.ID_CLOSE
, _("&Close"))
205 fileMenu
.AppendSeparator()
207 if demoMode
== 0 or demoMode
== 2: # Single Doc or MDI
208 fileMenu
.Append(wx
.ID_SAVE
, _("&Save"))
209 fileMenu
.Append(wx
.ID_SAVEAS
, _("Save &As"))
210 fileMenu
.AppendSeparator()
211 fileMenu
.Append(wx
.ID_PRINT
, _("&Print"))
212 fileMenu
.Append(wx
.ID_PRINT_SETUP
, _("Page &Setup"))
213 fileMenu
.Append(wx
.ID_PREVIEW
, _("Print Pre&view"))
216 editMenu
.Append(wx
.ID_UNDO
, _("&Undo"))
217 editMenu
.Append(wx
.ID_REDO
, _("&Redo"))
219 self
._frame
.editMenu
= editMenu
221 fileMenu
.AppendSeparator()
222 fileMenu
.Append(wx
.ID_EXIT
, _("E&xit"))
224 docManager
.FileHistoryUseMenu(fileMenu
)
227 helpMenu
.Append(wx
.ID_ABOUT
, _("&About"))
229 menuBar
= wx
.MenuBar()
230 menuBar
.Append(fileMenu
, _("&File"))
232 menuBar
.Append(editMenu
, _("&Edit"))
233 menuBar
.Append(helpMenu
, _("&Help"))
235 self
._frame
.SetMenuBar(menuBar
)
236 self
._frame
.Centre(wx
.BOTH
)
237 self
._frame
.Show(True)
239 self
.SetTopWindow(self
._frame
)
241 if demoMode
== 0: # Single doc
242 docManager
.OnFileNew(None)
247 def GetMainFrame(self
):
251 def GetDemoMode(self
):
252 return self
._demoMode
255 def CreateChildFrame(self
, doc
, view
):
256 subframe
= wx
.lib
.docview
.DocChildFrame(doc
, view
, self
.GetMainFrame(), -1, wx
.GetApp().GetAppName(), (10, 10), (300, 300), wx
.DEFAULT_FRAME_STYLE
)
259 fileMenu
.Append(wx
.ID_NEW
, _("&New"))
260 fileMenu
.Append(wx
.ID_OPEN
, _("&Open"))
261 fileMenu
.Append(wx
.ID_CLOSE
, _("&Close"))
262 fileMenu
.AppendSeparator()
263 fileMenu
.Append(wx
.ID_SAVE
, _("&Save"))
264 fileMenu
.Append(wx
.ID_SAVEAS
, _("Save &As"))
265 fileMenu
.AppendSeparator()
266 fileMenu
.Append(wx
.ID_PRINT
, _("&Print"))
267 fileMenu
.Append(wx
.ID_PRINT_SETUP
, _("Page &Setup"))
268 fileMenu
.Append(wx
.ID_PREVIEW
, _("Print Pre&view"))
271 editMenu
.Append(wx
.ID_UNDO
, _("&Undo"))
272 editMenu
.Append(wx
.ID_REDO
, _("&Redo"))
275 helpMenu
.Append(wx
.ID_ABOUT
, _("&About"))
277 menuBar
= wx
.MenuBar()
279 menuBar
.Append(fileMenu
, _("&File"))
280 menuBar
.Append(editMenu
, _("&Edit"))
281 menuBar
.Append(helpMenu
, _("&Help"))
283 subframe
.SetMenuBar(menuBar
)
284 subframe
.Centre(wx
.BOTH
)
288 class MyFrame(wx
.lib
.docview
.DocParentFrame
):
291 def __init__(self
, manager
, frame
, id, title
, pos
= wx
.DefaultPosition
, size
= wx
.DefaultSize
, style
= wx
.DEFAULT_FRAME_STYLE
, name
= "frame"):
292 wx
.lib
.docview
.DocParentFrame
.__init
__(self
, manager
, frame
, id, title
, pos
, size
, style
, name
)
294 wx
.EVT_MENU(self
, wx
.ID_ABOUT
, self
.OnAbout
)
297 def OnAbout(self
, event
):
298 wx
.MessageBox(wx
.GetApp().GetAppName(), _("About DocView"))
301 def GetMainFrame(self
):
305 class MyMDIFrame(wx
.lib
.docview
.DocMDIParentFrame
):
307 def __init__(self
, manager
, frame
, id, title
, pos
= wx
.DefaultPosition
, size
= wx
.DefaultSize
, style
= wx
.DEFAULT_FRAME_STYLE
, name
= "frame"):
308 wx
.lib
.docview
.DocMDIParentFrame
.__init
__(self
, manager
, frame
, id, title
, pos
, size
, style
, name
)
310 wx
.EVT_MENU(self
, wx
.ID_ABOUT
, self
.OnAbout
)
313 def OnAbout(self
, event
):
314 wx
.MessageBox(wx
.GetApp().GetAppName(), _("About DocView"))
317 def GetMainFrame(self
):