1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 '''This module contains classes related to the view, textarea, and buffer. It
22 doesn't contain all the GUI code,
23 but the code for the main window and textarea is mostly here.
24
25 This module queues the following events:
26 BufferSwitched
27 Every time a new buffer is focused
28 FileClosed
29 Whenever a file is closed by clicking the 'X' on a tab. This is not
30 issued when a plugin closes a buffer via a keypress or other action;
31 it is the responsibility of such a plugin to issue this event.
32
33 This module listens for the following events:
34 FileClosed
35 Whenever a file is closed in any way. The View performs some internal
36 cleanup when a file is closed, so it is VITAL that any external plugins
37 that close a buffer notify the view that they have done so by queueing
38 this event.'''
39
40 import wx
41 from wx import stc, aui
42 import sys, os
43 from EventActionManager import Action
44
45 -class View(wx.Frame):
46 '''A Frame containing the entire text-area and any other panels, menus, toolbars, etc.
47 Uses wx.aui widgets to allow multiple (splittable) windows and plugin docking.'''
48
49 textViews = {}
51 '''Initialize and set up the frame and view'''
52 wx.Frame.__init__(self, None, size=pallavi.config.window_default_size, title="Pallavi Text Editor", name="Pallavi")
53 self.pallavi = pallavi
54 self.bufferNotebook = aui.AuiNotebook(self)
55 self.bufferNotebook.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, lambda evt: self.pallavi.eventBus.QueueEvent("BufferSwitched"))
56 self.bufferNotebook.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, lambda evt: self.pallavi.eventBus.QueueEvent("FileClosed", self.bufferNotebook.GetPage(evt.Int).filename))
57 pallavi.eventBus.AddListener("FileClosed", self.__fileClosed)
58
59 self.dockmanager = wx.aui.AuiManager(self)
60 self.dockmanager.AddPane(self.bufferNotebook, wx.CENTER)
61 self.dockmanager.Update()
62
63 icon = wx.Icon(pallavi.config.share + "/pallavi128.xpm", wx.BITMAP_TYPE_XPM, 128, 128)
64 icon.LoadFile(pallavi.config.share + "/pallavi128.xpm", wx.BITMAP_TYPE_XPM)
65 self.SetIcon(icon)
66 self.AddTextView(pallavi.nextUntitled())
67
68 self.Show()
69
70 - def AddTextView(self, filename, loadfile=False, filecontents=None):
71 '''Add a TextView to the view associated with a filename (uniquely
72 identifying the buffer). If loadfile is true, then the filename
73 must represent a unque path on the local filesystem; these will
74 be loaded. if Loadfile is False and file_contents is a string, these
75 contents will be loaded into the textview'''
76 textview = TextView(self.bufferNotebook, self)
77 if loadfile:
78 textview.LoadFile(filename)
79 elif filecontents != None:
80 textview.SetText(filecontents)
81
82 textview.filename = filename
83 self.textViews[filename] = textview
84 self.focusedTextView = textview
85 self.pallavi.eventBus.QueueEvent("TextViewAdded", textview)
86 self.bufferNotebook.AddPage(textview, os.path.basename(filename))
87 textview.SetFocus()
88
89 - def DeleteTextView(self, textview):
90 '''Remove the Textview from the view and the associated page.
91 This method does nothing to check whether the buffer has
92 been saved or exists elsewhere. These tasks should be done
93 before calling this method. It also does not queue a FileClosed
94 event.'''
95 pageidx = self.bufferNotebook.GetPageIndex(textview)
96 self.bufferNotebook.DeletePage(pageidx)
97 self.pallavi.eventBus.QueueEvent("TextViewDeleted", textview)
98
100 '''Change the filename of the given textview. Useful
101 for 'Save As' type operations'''
102 pageidx = self.bufferNotebook.GetPageIndex(textview)
103 oldfilename = textview.filename
104 textview.filename = filename
105 del self.textViews[oldfilename]
106 self.textViews[filename] = textview
107 self.bufferNotebook.SetPageText(pageidx, os.path.basename(filename))
108
110 '''private callback called whenever the 'FileClosed' event
111 is handled. Just does some basic housecleaning'''
112 if filename in self.textViews:
113 del self.textViews[filename]
114
115 -class TextView(stc.StyledTextCtrl):
116 '''The editing buffer is currently a default StyledTextCtrl with a few modifications
117 but will eventually likely be a very robust class in its own right'''
118 - def __init__(self, parent, view):
119 stc.StyledTextCtrl.__init__(self, parent, style=wx.NO_BORDER)
120 self.bufferNotebook = parent
121 self.view = view
122 self.Bind(wx.EVT_SET_FOCUS, self.OnFocus)
123 wx.CallAfter(self.SetTabIndents, False)
124
125 - def OnFocus(self, event):
126 '''Called whenever the textview is focused. Ensure the view knows which is the currently
127 focused textview and that an event is queued'''
128 self.view.focusedTextView = self
129 self.view.pallavi.eventBus.QueueEvent("TextViewFocused")
130 event.Skip()
131
132
133
134