# Copyright (C) 2007 - Nando Vieira # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. import gedit import gtk import gtk.gdk import os import os.path from gettext import gettext as _ DEBUG_NAME = 'S_DEBUG' DEBUG_TITLE = 'sample' def debug(text, level=1): if os.environ.has_key(DEBUG_NAME): try: required_level = int(os.environ[DEBUG_NAME]) if required_level >= level: print "[%s] %s" % (DEBUG_TITLE, text) except: print "[%s] debug error" % DEBUG_TITLE class SamplePlugin(gedit.Plugin): def __init__(self): gedit.Plugin.__init__(self) self.instances = {} def activate(self, window): debug('activating plugin') self.instances[window] = SampleWindowHelper(self, window) def deactivate(self, window): debug('deactivating plugin') self.instances[window].deactivate() del self.instances[window] def update_ui(self, window): debug('updating ui') self.instances[window].update_ui() class SampleWindowHelper: handlers = {} def __init__(self, plugin, window): self.window = window self.plugin = plugin self.statusbar = window.get_statusbar() self.context_id = self.statusbar.get_context_id('SampleStatusbar') self.set_status() for view in window.get_views(): self.connect_handlers(view) window.connect('tab_added', self.on_tab_added) def deactivate(self): debug('deactivate function called') for view in self.handlers: view.disconnect(self.handlers[view]) self.window = None self.plugin = None def connect_handlers(self, view): handler = view.connect('key-press-event', self.on_key_press) self.handlers[view] = handler def on_tab_added(self, window, tab): self.connect_handlers(tab.get_view()) def update(self, text=None): pass def update_ui(self): pass def set_status(self, text=None): self.statusbar.pop(self.context_id) if text is not None: self.statusbar.push(self.context_id, "Sample: %s " % _(text)) def set_data(self, name, value): self.window.get_active_tab().get_view().set_data(name, value) def get_data(self, name): return self.window.get_active_tab().get_view().get_data(name) def on_key_press(self, view, event): pass