Package pallavi :: Module Views
[hide private]
[frames] | no frames]

Source Code for Module pallavi.Views

  1  # Copyright (c) 2006-2007 Dusty Phillips 
  2   
  3  # Permission is hereby granted, free of charge, to any person obtaining a 
  4  # copy of this software and associated documentation files (the "Software"), 
  5  # to deal in the Software without restriction, including without limitation 
  6  # the rights to use, copy, modify, merge, publish, distribute, sublicense, 
  7  # and/or sell copies of the Software, and to permit persons to whom the 
  8  # Software is furnished to do so, subject to the following conditions: 
  9   
 10  # The above copyright notice and this permission notice shall be included in 
 11  # all copies or substantial portions of the Software. 
 12   
 13  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 14  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 15  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 16  # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 17  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 18  # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 19  # DEALINGS IN THE SOFTWARE. 
 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 = {}
50 - def __init__(self, pallavi):
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
99 - def ChangeFilename(self, textview, filename):
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
109 - def __fileClosed(self, event, filename):
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 # Look up BufferList class from tags/0.4 release when you decide to put it back 134