File size: 8,140 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
# ex: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:

#
#  Author(s):	Chris Abidin, Andrew LaMora
#  Created on: 2004-04-30
#
#  (C) Copyright 2004-2025 Johns Hopkins University (JHU), All Rights Reserved.

# --- begin cisst license - do not edit ---
# 
# This software is provided "as is" under an open source license, with
# no warranty.  The complete license can be found in license.txt and
# http://www.cisst.org/cisst/license.txt.
# 
# --- end cisst license ---
#
#ireEditorNotebook

import wx
import wx.py as py

from ireShell import *

TestVar = 'hi'

class ireEditorNotebook(wx.Notebook):

    def __init__(self, parent, locals={}):
        wx.Notebook.__init__(self, parent, id=-1)

        self.buffers = {}
        self.buffer = None
        self.editor = None

        ShellPanel = wx.Panel(self, -1)

        self.Shell = ireShell(ShellPanel, -1, style=wx.CLIP_CHILDREN | wx.SUNKEN_BORDER,
            introText='Welcome to the IRE Shell.\nCtrl-Up for history!', locals=locals)

        self.AddPage(page=ShellPanel, text='*Shell*', select=True)

        ShellPanelSizer = wx.BoxSizer(wx.VERTICAL)
        ShellPanelSizer.Add(self.Shell, 1, wx.EXPAND)
        ShellPanel.SetSizer(ShellPanelSizer)
        ShellPanel.SetAutoLayout(True)

        self.Shell.SetFocus()

        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged, id=self.GetId())
        #self.Bind(wx.EVT_IDLE, self.OnIdle)



    def setEditor(self, editor):
        self.editor = editor
        self.buffer = self.editor.buffer
        self.buffers[self.buffer.id] = self.buffer


    def OnClose(self, event):
        """Event handler for closing."""
        for buffer in self.buffers.values():
            self.buffer = buffer
            if buffer.hasChanged():
                cancel = self.bufferSuggestSave()
                if cancel and event.CanVeto():
                    event.Veto()
                    return
        self.Destroy()

    def OnIdle(self, event):
        """Event handler for idle time."""
        #self._updateStatus()
        self._updateTabText()
        #self._updateTitle()
        event.Skip()
        

    #def _updateTabText(self):
    #    """Show current buffer information on notebook tab."""
    #    suffix = ' **'
    #    notebook = self
    #    selection = notebook.GetSelection()
    #    if selection < 1:
    #        return
    #    text = notebook.GetPageText(selection)
    #    window = notebook.GetPage(selection)
    #    if window.editor and window.editor.buffer.hasChanged():
    #        if text.endswith(suffix):
    #            pass
    #        else:
    #            notebook.SetPageText(selection, text + suffix)
    #    else:
    #        if text.endswith(suffix):
    #            notebook.SetPageText(selection, text[:len(suffix)])
    #    window.Update()
        

    def _updateTabText(self):
        """Show current buffer information on notebook tab."""
        selection = self.GetSelection()
        if selection < 1:
            return
        if self.GetPageText(selection) != self.buffer.name:
            self.SetPageText(selection, self.buffer.name)
            self.GetPage(selection).editor.window.Update()


    def hasBuffer(self):
        """Return True if there is a current buffer."""
        if self.buffer:
            return True
        else:
            return False

    def bufferCreate(self, filename=None):
        """Create new buffer."""
        buffer = py.editor.Buffer()
        panel = wx.Panel(parent=self, id=-1)
        panel.Bind(wx.EVT_ERASE_BACKGROUND, lambda x: x)
        editor = py.editor.Editor(parent=panel)
        panel.editor = editor
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(editor.window, 1, wx.EXPAND)
        panel.SetSizer(sizer)
        panel.SetAutoLayout(True)
        sizer.Layout()
        buffer.addEditor(editor)
        buffer.open(filename)
        buffer.confirmed = True
        self.setEditor(editor)
        self.AddPage(page=panel, text=self.buffer.name, select=True)
        self.editor.setFocus()

    def bufferDestroy(self):
        """Destroy the current buffer."""
        selection = self.GetSelection()
        if selection > 0:  # Don't destroy the PyCrust tab.
            if self.buffer:
                del self.buffers[self.buffer.id]
                self.buffer = None  # Do this before DeletePage().
            self.DeletePage(selection)

    def bufferNew(self):
        """Create new buffer."""
        self.bufferCreate()
        cancel = False
        return cancel

    def bufferOpen(self):
        """Open file in buffer."""
        filedir = ''
        if self.buffer and self.buffer.doc.filedir:
            filedir = self.buffer.doc.filedir
        result = py.editor.openMultiple(None, 'Open file', filedir, '', 'Python files (*.py)|*.py|All Files (*.*)|*.*')
        for path in result.paths:
            self.bufferCreate(path)
        cancel = False
        return cancel

    def bufferClose(self):
        """Close buffer."""
        if self.bufferHasChanged():
            cancel = self.bufferSuggestSave()
            if cancel:
                return cancel
        self.bufferDestroy()
        cancel = False
        return cancel


    def bufferHasChanged(self):
        """Return True if buffer has changed since last save."""
        if self.buffer:
            return self.buffer.hasChanged()
        else:
            return False


##     def bufferPrint(self):
##         """Print buffer."""
##         pass

##     def bufferRevert(self):
##         """Revert buffer to version of file on disk."""
##         pass

    def bufferSave(self):
        """Save buffer to its file."""
        if self.buffer.doc.filepath:
            self.buffer.save()
            cancel = False
        else:
            cancel = self.bufferSaveAs()
        self._updateTabText()
        return cancel

    def bufferSaveAs(self):
        """Save buffer to a new filename."""
        if self.bufferHasChanged() and self.buffer.doc.filepath:
            cancel = self.bufferSuggestSave()
            if cancel:
                return cancel
        filedir = ''
        if self.buffer and self.buffer.doc.filedir:
            filedir = self.buffer.doc.filedir
        fn = py.editor.saveSingle(None, 'Save file', filedir, '', 'Python files (*.py)|*.py|All Files (*.*)|*.*').path
        if fn:
            # Some systems don't automatically append the .py extension
            (name, ext) = os.path.splitext(fn)
            if not ext:
               fn += '.py'
            self.buffer.saveAs(fn)
            cancel = False
        else:
            cancel = True
        return cancel

    def bufferSuggestSave(self):
        """Suggest saving changes.  Return True if user selected Cancel."""
        result = messageDialog(parent=None,
                               message='%s has changed.\n'
                                       'Would you like to save it first'
                                       '?' % self.buffer.name,
                               title='Save current file?')
        if result.positive:
            cancel = self.bufferSave()
        else:
            cancel = result.text == 'Cancel'
        return cancel

    def updateNamespace(self):
        """Update the buffer namespace for autocompletion and calltips."""
        if self.buffer.updateNamespace():
            self.SetStatusText('Namespace updated')
        else:
            self.SetStatusText('Error executing, unable to update namespace')

    #def OnPageChanged(self, event):
    #    selection = event.GetSelection()
    #    if selection == 0:
    #        self.Shell.SetFocus()
    #    event.Skip()

    def OnPageChanged(self, event):
        new = event.GetSelection()
        window = self.GetPage(new)
        if hasattr(window, 'editor'):
            self.setEditor(window.editor)
        window.SetFocus()
        event.Skip()


    def SetFocus(self):
        wx.Notebook.SetFocus(self)
        selection = self.GetSelection()
        if selection == 0:
            self.Shell.SetFocus()