summaryrefslogtreecommitdiffstats
path: root/g-ed-it/windowHelper.py
blob: dd6d2e6715b72b6398d86f64ed97b395637a6bbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
#-*- coding:utf-8 -*-

#    Copyright 2009 Matthieu Gautier

#    This file is part of g-ed-it.
#
#    g-ed-it 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 3 of the License, or
#    any later version.
#
#    g-ed-it 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 g-ed-it.  If not, see <http://www.gnu.org/licenses/>.

import gedit

import gtk
import gtk.glade

import gobject

import os
import time

import docHelper

ui_string = """<ui>
  <menubar name="MenuBar">
    <placeholder name="ExtraMenu_1">
      <menu action="GitMenu">
        <menuitem action="Commit"/>
        <menuitem action="Add"/>
      </menu>
    </placeholder>
  </menubar>
</ui>
"""

class WindowHelper:
	def __init__(self, plugin, window, gitAction):
		self.window = window
		self.plugin = plugin
		self.gitAction = gitAction
		
		self.docHelpers = {}
		self.create_all_docHelper()

		self.create_actionManager()
		# add menu
		self.ui_id = self.manager.add_ui_from_string(ui_string)
		self.set_call_back()

		
	def deactivate(self):        
		self.manager.remove_ui(self.ui_id)
		self.manager.remove_action_group(self.action_group)
		self.manager.ensure_update()
		self.manager = None
		
		self.window.disconnect(self.added_hid)
		self.window.disconnect(self.removed_hid)
	
		for docHelper in self.docHelpers:
			self.docHelpers[docHelper].deactivate()
		
		self.window = None
		self.plugin = None
		self.gitAction = None
		
	def update_ui(self):
		for docHelper in self.docHelpers:
			self.docHelpers[docHelper].update_ui()
		return
	
	def fast_update_ui(self):
		self.docHelpers[self.window.get_active_document()].update_ui()
		    
	def create_actionManager(self):
		self.manager = self.window.get_ui_manager()
		self.action_group = gtk.ActionGroup("GitPluginActions")
		
		self.git_menu_action = gtk.Action(name="GitMenu",
		                                   label="Git",
		                                   tooltip="Manage git",
		                                   stock_id=None)
		self.commit_action    = gtk.Action(name="Commit",
		                                   label="Commit",
		                                   tooltip="Commit current state",
		                                   stock_id=gtk.STOCK_GO_UP)
		self.add_action       = gtk.Action(name="Add",
		                                   label="Add to index",
		                                   tooltip="",
		                                   stock_id=gtk.STOCK_ADD)
		
		self.commit_action.connect("activate", self.gitAction.commit, self.window)
		self.add_action.connect("activate", self.gitAction.add)
		
		self.action_group.add_action(self.git_menu_action)
		self.action_group.add_action(self.commit_action)
		self.action_group.add_action(self.add_action)

		# Add the action group.
		self.manager.insert_action_group(self.action_group, -1)
		pass
		
		
	def create_all_docHelper(self):
		for view in self.window.get_views():
			tab = view
			while (tab.__class__ != gedit.Tab):
				tab = tab.get_parent()
			self.docHelpers[tab.get_document()] = docHelper.DocHelper(tab,self.window,self.gitAction)
		pass
	
	def set_call_back(self):
		self.added_hid = self.window.connect("tab-added",self.on_tab_added)
		self.removed_hid = self.window.connect("tab-removed",self.on_tab_removed)
		pass
	
	def on_tab_added(self,window,tab):
		self.docHelpers[tab.get_document()] = docHelper.DocHelper(tab,self.window,self.gitAction)
		pass
	
	def on_tab_removed(self,window,tab):
		self.docHelpers.pop(tab.get_document()).deactivate()
		pass