~kroq-gar78/ubuntu/precise/tomboy/fix-965786

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;

namespace Tomboy
{
	/// <summary>
	/// A NoteAddin extends the functionality of a note and a NoteWindow.
	/// If you wish to extend Tomboy in a more broad sense, perhaps you
	/// should create an ApplicationAddin.
	/// <summary>
	public abstract class NoteAddin : AbstractAddin
	{
		Note note;

		List<Gtk.MenuItem> tools_menu_items;
		List<Gtk.MenuItem> text_menu_items;
		Dictionary<Gtk.ToolItem, int> toolbar_items;

		public void Initialize (Note note)
		{
			this.note = note;
			this.note.Opened += OnNoteOpenedEvent;

			Initialize ();

			if (note.IsOpened)
				OnNoteOpened ();
		}

		protected override void Dispose (bool disposing)
		{
			if (disposing) {
				if (tools_menu_items != null) {
					foreach (Gtk.Widget item in tools_menu_items)
					item.Destroy ();
				}

				if (text_menu_items != null) {
					foreach (Gtk.Widget item in text_menu_items)
					item.Destroy ();
				}
				
				if (toolbar_items != null) {
					foreach (Gtk.ToolItem item in toolbar_items.Keys)
						item.Destroy ();
				}

				Shutdown ();
			}

			note.Opened -= OnNoteOpenedEvent;
		}

		/// <summary>
		/// Called when the NoteAddin is attached to a Note
		/// </summary>
		public abstract void Initialize ();

		/// <summary>
		/// Called when a note is deleted and also when
		/// the addin is disabled.
		/// </summary>
		public abstract void Shutdown ();

		/// <summary>
		/// Called when the note is opened.
		/// </summary>
		public abstract void OnNoteOpened ();

		public Note Note
		{
			get {
				return note;
			}
		}

		public bool HasBuffer
		{
			get {
				return note.HasBuffer;
			}
		}

		public NoteBuffer Buffer
		{
			get
			{
				if (IsDisposing && !HasBuffer)
					throw new InvalidOperationException ("Plugin is disposing already");

				return note.Buffer;
			}
		}

		public bool HasWindow
		{
			get {
				return note.HasWindow;
			}
		}

		public NoteWindow Window
		{
			get
			{
				if (IsDisposing && !HasWindow)
					throw new InvalidOperationException ("Plugin is disposing already");

				return note.Window;
			}
		}

		public NoteManager Manager
		{
			get {
				return note.Manager;
			}
		}

		void OnNoteOpenedEvent (object sender, EventArgs args)
		{
			OnNoteOpened ();

			if (tools_menu_items != null) {
				foreach (Gtk.Widget item in tools_menu_items) {
					if (item.Parent == null ||
					                item.Parent != Window.PluginMenu)
						Window.PluginMenu.Add (item);
				}
			}

			if (text_menu_items != null) {
				foreach (Gtk.Widget item in text_menu_items) {
					if (item.Parent == null ||
					                item.Parent != Window.TextMenu) {
						Window.TextMenu.Add (item);
						Window.TextMenu.ReorderChild (item, 7);
					}
				}
			}
			
			if (toolbar_items != null) {
				foreach (Gtk.ToolItem item in toolbar_items.Keys) {
					if (item.Parent == null ||
									item.Parent != Window.Toolbar) {
						Window.Toolbar.Insert (item, (int) toolbar_items [item]);
					}
				}
			}
		}

		public void AddPluginMenuItem (Gtk.MenuItem item)
		{
			if (IsDisposing)
				throw new InvalidOperationException ("Plugin is disposing already");

			if (tools_menu_items == null)
				tools_menu_items = new List<Gtk.MenuItem> ();

			tools_menu_items.Add (item);

			if (note.IsOpened)
				Window.PluginMenu.Add (item);
		}
		
		public void AddToolItem (Gtk.ToolItem item, int position)
		{
			if (IsDisposing)
				throw new InvalidOperationException ("Add-in is disposing already");
				
			if (toolbar_items == null)
				toolbar_items = new Dictionary<Gtk.ToolItem, int> ();
			
			toolbar_items [item] = position;
			
			if (note.IsOpened) {
				Window.Toolbar.Insert (item, position);
			}
		}

		public void AddTextMenuItem (Gtk.MenuItem item)
		{
			if (IsDisposing)
				throw new InvalidOperationException ("Plugin is disposing already");

			if (text_menu_items == null)
				text_menu_items = new List<Gtk.MenuItem> ();

			text_menu_items.Add (item);

			if (note.IsOpened) {
				Window.TextMenu.Add (item);
				Window.TextMenu.ReorderChild (item, 7);
			}
		}
	}
}