home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / snippets / WindowHelper.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  5.9 KB  |  149 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 re
  19. import os
  20. import gettext
  21.  
  22. import gtk
  23. from gtk import gdk
  24. import gedit
  25.  
  26. from Document import Document
  27. from Library import Library
  28.  
  29. class WindowHelper:
  30.         def __init__(self, plugin):
  31.                 self.plugin = plugin
  32.                 self.current_controller = None
  33.                 self.current_language = None
  34.                 self.signal_ids = {}
  35.                 
  36.         def run(self, window):
  37.                 self.window = window
  38.                 self.insert_menu()
  39.                 
  40.                 self.accel_group = Library().get_accel_group(None)
  41.                 
  42.                 window.add_accel_group(self.accel_group)
  43.                 window.connect('tab-added', self.on_tab_added)
  44.                 
  45.                 # Add controllers to all the current views
  46.                 for view in self.window.get_views():
  47.                         if isinstance(view, gedit.View) and not self.has_controller(view):
  48.                                 view._snippet_controller = Document(self, view)
  49.                 
  50.                 self.update()
  51.         
  52.         def stop(self):
  53.                 self.window.remove_accel_group(self.accel_group)
  54.                 self.accel_group = None
  55.                 
  56.                 #self.window.remove_accel_group(accel)
  57.                 self.remove_menu()
  58.  
  59.                 # Iterate over all the tabs and remove every controller
  60.                 for view in self.window.get_views():
  61.                         if isinstance(view, gedit.View) and self.has_controller(view):
  62.                                 view._snippet_controller.stop()
  63.                                 view._snippet_controller = None
  64.                 
  65.                 self.window = None
  66.                 self.plugin = None
  67.         
  68.         def insert_menu(self):
  69.                 manager = self.window.get_ui_manager()
  70.  
  71.                 self.action_group = gtk.ActionGroup("GeditSnippetPluginActions")
  72.                 self.action_group.set_translation_domain('gedit')
  73.                 self.action_group.add_actions([('ManageSnippets', None,
  74.                                 _('Manage _Snippets...'), \
  75.                                 None, _('Manage snippets'), \
  76.                                 self.on_action_snippets_activate)])
  77.  
  78.                 self.merge_id = manager.new_merge_id()
  79.                 manager.insert_action_group(self.action_group, -1)
  80.                 manager.add_ui(self.merge_id, '/MenuBar/ToolsMenu/ToolsOps_5', \
  81.                                 'ManageSnippets', 'ManageSnippets', gtk.UI_MANAGER_MENUITEM, False)
  82.  
  83.         def remove_menu(self):
  84.                 manager = self.window.get_ui_manager()
  85.                 manager.remove_ui(self.merge_id)
  86.                 manager.remove_action_group(self.action_group)
  87.                 self.action_group = None
  88.         
  89.         def find_snippet(self, snippets, tag):
  90.                 result = []
  91.                 
  92.                 for snippet in snippets:
  93.                         if Snippet(snippet)['tag'] == tag:
  94.                                 result.append(snippet)
  95.                 
  96.                 return result
  97.  
  98.         def has_controller(self, view):
  99.                 return hasattr(view, '_snippet_controller') and view._snippet_controller
  100.  
  101.         def update_language(self):
  102.                 if self.current_language:
  103.                         accel_group = Library().get_accel_group( \
  104.                                         self.current_language)
  105.                         self.window.remove_accel_group(accel_group)
  106.  
  107.                 if self.current_controller:
  108.                         self.current_language = self.current_controller.language_id
  109.                         
  110.                         if self.current_language != None:
  111.                                 accel_group = Library().get_accel_group( \
  112.                                                 self.current_language)
  113.                                 self.window.add_accel_group(accel_group)
  114.                 else:
  115.                         self.current_language = None
  116.                 
  117.         def language_changed(self, controller):
  118.                 if controller == self.current_controller:
  119.                         self.update_language()
  120.                 
  121.         def update(self):
  122.                 view = self.window.get_active_view()
  123.                 
  124.                 if not view or not self.has_controller(view):
  125.                         return
  126.                 
  127.                 controller = view._snippet_controller
  128.                 
  129.                 if controller != self.current_controller:
  130.                         self.current_controller = controller
  131.                         self.update_language()
  132.  
  133.         # Callbacks
  134.         
  135.         def on_tab_added(self, window, tab):
  136.                 # Create a new controller for this tab if it has a standard gedit view
  137.                 view = tab.get_view()
  138.                 
  139.                 if isinstance(view, gedit.View) and not self.has_controller(view):
  140.                         view._snippet_controller = Document(self, view)
  141.  
  142.         def on_action_snippets_activate(self, item):
  143.                 self.plugin.create_configure_dialog()
  144.  
  145.         def accelerator_activated(self, keyval, mod):
  146.                 return self.current_controller.accelerator_activate(keyval, mod)
  147.  
  148. # ex:ts=8:et:
  149.