File: DocViewDemo.py

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (325 lines) | stat: -rw-r--r-- 10,973 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#----------------------------------------------------------------------------
# Name:         DocViewDemo.py
# Purpose:      Port of the wxWindows docview sample classes
#
# Author:       Peter Yared
#
# Created:      8/1/03
# CVS-ID:       $Id$
# Copyright:    (c) 2003-2005 ActiveGrid, Inc. (Port of wxWindows classes by Julian Smart et al)
# License:      wxWindows License
#----------------------------------------------------------------------------



#----------------------------------------------------------------------
# This isn't the most object oriented code (it is somewhat repetitive,
# but it is true to the wxWindows C++ demos.
#----------------------------------------------------------------------


import wx
import wx.lib.docview
_ = wx.GetTranslation


#----------------------------------------------------------------------
#----------------------------------------------------------------------
# TextEdit document and view classes

class TextEditDocument(wx.lib.docview.Document):


    def OnSaveDocument(self, filename):
        view = self.GetFirstView()
        if not view.GetTextSW().SaveFile(filename):
            return False
        self.Modify(False)
        self.SetDocumentSaved(True)
##        if wx.Platform == "__WXMAC__":
##            fn = wx.Filename(filename)
##            fn.MacSetDefaultTypeAndCreator()
        return True


    def OnOpenDocument(self, filename):
        view = self.GetFirstView()
        if not view.GetTextSW().LoadFile(filename):
            return False
        self.SetFilename(filename, True)
        self.Modify(False)
        self.UpdateAllViews()
        self._savedYet = True
        return True


    def IsModified(self):
        view = self.GetFirstView()
        if view:
            return wx.lib.docview.Document.IsModified(self) or (view.GetTextSW() and view.GetTextSW().IsModified())
        else:
            return wx.lib.docview.Document.IsModified(self)


    def Modify(self, mod):
        view = self.GetFirstView()
        wx.lib.docview.Document.Modify(self, mod)
        if not mod and view and view.GetTextSW():
            view.GetTextSW().DiscardEdits()


class TextEditView(wx.lib.docview.View):


    def OnCreate(self, doc, flags):
        flags = doc.GetDocumentManager().GetFlags()
        if flags & wx.lib.docview.DOC_SDI and doc.GetDocumentManager().GetMaxDocsOpen() == 1:
            self._frame = wx.GetApp().GetMainFrame()
            self.SetFrame(self._frame)
            sizer = wx.BoxSizer()
            self._textsw = MyTextWindow(self, self._frame, wx.DefaultPosition, wx.DefaultSize, wx.TE_MULTILINE)
            sizer.Add(self._textsw, 1, wx.EXPAND, 0)
            self._frame.SetSizer(sizer)
            self._frame.Layout()
            self.Activate(True)
            return True
        elif flags & wx.lib.docview.DOC_MDI:
            self._frame = wx.lib.docview.DocMDIChildFrame(doc, self, wx.GetApp().GetMainFrame(), -1, wx.GetApp().GetAppName(), (10, 10), (300, 300), wx.DEFAULT_FRAME_STYLE)
            self.SetFrame(self._frame)
            sizer = wx.BoxSizer()
            self._textsw = MyTextWindow(self, self._frame, wx.DefaultPosition, wx.DefaultSize, wx.TE_MULTILINE)
            sizer.Add(self._textsw, 1, wx.EXPAND, 0)
            self._frame.SetSizer(sizer)
            self._frame.Layout()
            self._frame.Show(True)
            self.Activate(True)
            return True
        else:  # flags & wx.lib.docview.DOC_SDI
            self._frame = wx.GetApp().CreateChildFrame(doc, self)
            width, height = self._frame.GetClientSize()
            self._textsw = MyTextWindow(self, self._frame, (0, 0), (width, height), wx.TE_MULTILINE)
            self._frame.SetTitle(_("TextEditView"))
            self._frame.Show(True)
            self.Activate(True)
            return True


    # Since ProcessEvent is not virtual, we have to trap the relevant events using this pseudo-ProcessEvent instead of EVT_MENU
    def ProcessEvent(self, event):
        id = event.GetId()
        if id == wx.ID_UNDO:
            self._textsw.Undo()
            return True
        elif id == wx.ID_REDO:
            self._textsw.Redo()
            return True
        else:
            return wx.lib.docview.View.ProcessEvent(self, event)


    def GetTextSW(self):
        return self._textsw


    def OnDraw(self, dc):
        """ For Print and Print Preview """
        pass


    def OnClose(self, deleteWindow = True):
        if not self.GetDocument().Close():
            return False
        self.Activate(False)
        if deleteWindow:
            if self.GetDocument().GetDocumentManager().GetMaxDocsOpen() == 1 and self.GetDocument().GetDocumentManager().GetFlags() & wx.lib.docview.DOC_SDI:
                if self._textsw:
                    self._textsw.Destroy()
                return True
            else:
                self._frame.Destroy()
                return True
        return True


class MyTextWindow(wx.TextCtrl):


    def __init__(self, view, frame, pos, size, style):
        wx.TextCtrl.__init__(self, frame, -1, "", pos, size, style)
        self._view = view



#----------------------------------------------------------------------
#----------------------------------------------------------------------
# TextEdit Sample Application


class MyApp(wx.App):


    def OnInit(self):


        demoMode = wx.GetSingleChoiceIndex(_("Select the demo mode"),
                                           _("wxPython DocView Demo"),
                                           [_("SDI Single Document"), _("SDI"), _("MDI")])

        if demoMode == 0 or demoMode == 1:
            flags = wx.lib.docview.DOC_SDI
        elif demoMode == 2:
            flags = wx.lib.docview.DOC_MDI
        else:
            return False

        self.SetAppName(_("DocView Demo"))

        docManager = wx.lib.docview.DocManager(flags = flags)
        docManager.AssociateTemplate(wx.lib.docview.DocTemplate(docManager,
                                                         _("Text"),
                                                         "*.text;*.txt",
                                                         _("Text"),
                                                         _(".txt"),
                                                         _("Text Document"),
                                                         _("Text View"),
                                                         TextEditDocument,
                                                         TextEditView))
        #if wx.Platform == "__WXMAC__":
        #     wx.FileName.MacRegisterDefaultTypeAndCreator("txt", 'TEXT', 'WXMA')

        if demoMode == 0:
            docManager.SetMaxDocsOpen(1)

        if demoMode == 2:  # MDI
            self._frame = MyMDIFrame(docManager, None, -1, _("DocView Demo"), (0, 0), (500, 400), wx.DEFAULT_FRAME_STYLE)
        else:  # SDI
            self._frame = MyFrame(docManager, None, -1, _("DocView Demo"), (0, 0), (500, 400), wx.DEFAULT_FRAME_STYLE)

        fileMenu = wx.Menu()
        editMenu = None

        fileMenu.Append(wx.ID_NEW, _("&New..."))
        fileMenu.Append(wx.ID_OPEN, _("&Open..."))

        if demoMode == 2:  # MDI
            fileMenu.Append(wx.ID_CLOSE, _("&Close"))
            fileMenu.AppendSeparator()

        if demoMode == 0 or demoMode == 2:  # Single Doc or MDI
            fileMenu.Append(wx.ID_SAVE, _("&Save"))
            fileMenu.Append(wx.ID_SAVEAS, _("Save &As"))
            fileMenu.AppendSeparator()
            fileMenu.Append(wx.ID_PRINT, _("&Print"))
            fileMenu.Append(wx.ID_PRINT_SETUP, _("Page &Setup"))
            fileMenu.Append(wx.ID_PREVIEW, _("Print Pre&view"))

            editMenu = wx.Menu()
            editMenu.Append(wx.ID_UNDO, _("&Undo"))
            editMenu.Append(wx.ID_REDO, _("&Redo"))

            self._frame.editMenu = editMenu

        fileMenu.AppendSeparator()
        fileMenu.Append(wx.ID_EXIT, _("E&xit"))

        docManager.FileHistoryUseMenu(fileMenu)

        helpMenu = wx.Menu()
        helpMenu.Append(wx.ID_ABOUT, _("&About"))

        menuBar = wx.MenuBar()
        menuBar.Append(fileMenu, _("&File"))
        if editMenu:
            menuBar.Append(editMenu, _("&Edit"))
        menuBar.Append(helpMenu, _("&Help"))

        self._frame.SetMenuBar(menuBar)
        self._frame.Centre(wx.BOTH)
        self._frame.Show(True)

        self.SetTopWindow(self._frame)

        if demoMode == 0:  # Single doc
            docManager.OnFileNew(None)

        return True


    def GetMainFrame(self):
        return self._frame


    def GetDemoMode(self):
        return self._demoMode


    def CreateChildFrame(self, doc, view):
        subframe = wx.lib.docview.DocChildFrame(doc, view, self.GetMainFrame(), -1, wx.GetApp().GetAppName(), (10, 10), (300, 300), wx.DEFAULT_FRAME_STYLE)

        fileMenu = wx.Menu()
        fileMenu.Append(wx.ID_NEW, _("&New"))
        fileMenu.Append(wx.ID_OPEN, _("&Open"))
        fileMenu.Append(wx.ID_CLOSE, _("&Close"))
        fileMenu.AppendSeparator()
        fileMenu.Append(wx.ID_SAVE, _("&Save"))
        fileMenu.Append(wx.ID_SAVEAS, _("Save &As"))
        fileMenu.AppendSeparator()
        fileMenu.Append(wx.ID_PRINT, _("&Print"))
        fileMenu.Append(wx.ID_PRINT_SETUP, _("Page &Setup"))
        fileMenu.Append(wx.ID_PREVIEW, _("Print Pre&view"))

        editMenu = wx.Menu()
        editMenu.Append(wx.ID_UNDO, _("&Undo"))
        editMenu.Append(wx.ID_REDO, _("&Redo"))

        helpMenu = wx.Menu()
        helpMenu.Append(wx.ID_ABOUT, _("&About"))

        menuBar = wx.MenuBar()

        menuBar.Append(fileMenu, _("&File"))
        menuBar.Append(editMenu, _("&Edit"))
        menuBar.Append(helpMenu, _("&Help"))

        subframe.SetMenuBar(menuBar)
        subframe.Centre(wx.BOTH)
        return subframe


class MyFrame(wx.lib.docview.DocParentFrame):


    def __init__(self, manager, frame, id, title, pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, name = "frame"):
        wx.lib.docview.DocParentFrame.__init__(self, manager, frame, id, title, pos, size, style, name)
        self._frame = frame
        wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)


    def OnAbout(self, event):
        wx.MessageBox(wx.GetApp().GetAppName(), _("About DocView"))


    def GetMainFrame(self):
        return self._frame


class MyMDIFrame(wx.lib.docview.DocMDIParentFrame):

    def __init__(self, manager, frame, id, title, pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, name = "frame"):
        wx.lib.docview.DocMDIParentFrame.__init__(self, manager, frame, id, title, pos, size, style, name)
        self._frame = frame
        wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)


    def OnAbout(self, event):
        wx.MessageBox(wx.GetApp().GetAppName(), _("About DocView"))


    def GetMainFrame(self):
        return self._frame


app = MyApp()
app.MainLoop()