home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / snippets / SnippetPluginInstance.py < prev    next >
Encoding:
Python Source  |  2006-08-27  |  4.4 KB  |  146 lines

  1. #    Gedit snippets plugin
  2. #    Copyright (C) 2005-2006  Jesse van den Kieboom <jesse@icecrew.nl>
  3. #
  4. #    This program is free software; you can redistribute it and/or modify
  5. #    it under the terms of the GNU General Public License as published by
  6. #    the Free Software Foundation; either version 2 of the License, or
  7. #    (at your option) any later version.
  8. #
  9. #    This program is distributed in the hope that it will be useful,
  10. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. #    GNU General Public License for more details.
  13. #
  14. #    You should have received a copy of the GNU General Public License
  15. #    along with this program; if not, write to the Free Software
  16. #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17.  
  18. import gtksourceview
  19. import gedit
  20. import gtk
  21. from gtk import gdk
  22. import re
  23. import os
  24. import gettext
  25. from SnippetController import SnippetController
  26. from SnippetsLibrary import SnippetsLibrary
  27.  
  28. class SnippetsPluginInstance:
  29.     def __init__(self, plugin):
  30.         self.plugin = plugin
  31.         self.current_controller = None
  32.         self.current_language = None
  33.         self.signal_ids = {}
  34.         
  35.     def run(self, window):
  36.         self.window = window
  37.         self.insert_menu()
  38.         
  39.         self.accel_group = SnippetsLibrary().get_accel_group(None)
  40.         
  41.         window.add_accel_group(self.accel_group)
  42.         window.connect('tab-added', self.on_tab_added)
  43.         
  44.         # Add controllers to all the current views
  45.         for view in self.window.get_views():
  46.             if isinstance(view, gedit.View) and not self.has_controller(view):
  47.                 view._snippet_controller = SnippetController(self, view)
  48.         
  49.         self.update()
  50.     
  51.     def stop(self):
  52.         self.window.remove_accel_group(self.accel_group)
  53.         self.accel_group = None
  54.         
  55.         #self.window.remove_accel_group(accel)
  56.         self.remove_menu()
  57.  
  58.         # Iterate over all the tabs and remove every controller
  59.         for view in self.window.get_views():
  60.             if isinstance(view, gedit.View) and self.has_controller(view):
  61.                 view._snippet_controller.stop()
  62.                 view._snippet_controller = None        
  63.         
  64.         self.window = None
  65.         self.plugin = None
  66.     
  67.     def insert_menu(self):
  68.         manager = self.window.get_ui_manager()
  69.  
  70.         self.action_group = gtk.ActionGroup("GeditSnippetPluginActions")
  71.         self.action_group.set_translation_domain('gedit')
  72.         self.action_group.add_actions([('Snippets', None,
  73.                 _('Manage _Snippets...'), \
  74.                 None, _('Manage snippets'), \
  75.                 self.on_action_snippets_activate)])
  76.  
  77.         self.merge_id = manager.new_merge_id()
  78.         manager.insert_action_group(self.action_group, -1)        
  79.         manager.add_ui(self.merge_id, '/MenuBar/ToolsMenu/ToolsOps_5', \
  80.                 'Snippets', 'Snippets', gtk.UI_MANAGER_MENUITEM, False)
  81.  
  82.     def remove_menu(self):
  83.         manager = self.window.get_ui_manager()
  84.         manager.remove_ui(self.merge_id)
  85.         manager.remove_action_group(self.action_group)
  86.         self.action_group = None
  87.     
  88.     def find_snippet(self, snippets, tag):
  89.         result = []
  90.         
  91.         for snippet in snippets:
  92.             if Snippet(snippet)['tag'] == tag:
  93.                 result.append(snippet)
  94.         
  95.         return result
  96.  
  97.     def has_controller(self, view):
  98.         return hasattr(view, '_snippet_controller') and view._snippet_controller
  99.  
  100.     def update_language(self):
  101.         if self.current_language:
  102.             accel_group = SnippetsLibrary().get_accel_group( \
  103.                     self.current_language)
  104.             self.window.remove_accel_group(accel_group)
  105.  
  106.         if self.current_controller:
  107.             self.current_language = self.current_controller.language_id
  108.             
  109.             if self.current_language != None:
  110.                 accel_group = SnippetsLibrary().get_accel_group( \
  111.                         self.current_language)
  112.                 self.window.add_accel_group(accel_group)
  113.         else:
  114.             self.current_language = None
  115.         
  116.     def language_changed(self, controller):
  117.         if controller == self.current_controller:
  118.             self.update_language()
  119.         
  120.     def update(self):
  121.         view = self.window.get_active_view()
  122.         
  123.         if not view or not self.has_controller(view):
  124.             return
  125.         
  126.         controller = view._snippet_controller
  127.         
  128.         if controller != self.current_controller:
  129.             self.current_controller = controller
  130.             self.update_language()
  131.  
  132.     # Callbacks
  133.     
  134.     def on_tab_added(self, window, tab):
  135.         # Create a new controller for this tab if it has a standard gedit view
  136.         view = tab.get_view()
  137.         
  138.         if isinstance(view, gedit.View) and not self.has_controller(view):
  139.             view._snippet_controller = SnippetController(self, view)
  140.  
  141.     def on_action_snippets_activate(self, item):
  142.         self.plugin.create_configure_dialog()
  143.  
  144.     def accelerator_activated(self, keyval, mod):
  145.         return self.current_controller.accelerator_activate(keyval, mod)
  146.