~ubuntu-branches/ubuntu/lucid/tomboy/lucid-proposed

1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
1
// This file has been modified from its original project.  The following is a
2
// copy of the original copyright information:
3
4
/***************************************************************************
5
 *  ActionManager.cs
6
 *
7
 *  Copyright (C) 2005-2006 Novell, Inc.
8
 *  Written by Aaron Bockover <aaron@abock.org>
9
 ****************************************************************************/
10
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
11
/*  THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW:
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
12
 *
13
 *  Permission is hereby granted, free of charge, to any person obtaining a
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
14
 *  copy of this software and associated documentation files (the "Software"),
15
 *  to deal in the Software without restriction, including without limitation
16
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
17
 *  and/or sell copies of the Software, and to permit persons to whom the
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
18
 *  Software is furnished to do so, subject to the following conditions:
19
 *
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
20
 *  The above copyright notice and this permission notice shall be included in
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
21
 *  all copies or substantial portions of the Software.
22
 *
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
23
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
29
 *  DEALINGS IN THE SOFTWARE.
30
 */
31
32
using System;
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
33
using System.IO;
34
using System.Text;
35
using System.Xml;
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
36
using System.Collections;
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
37
using System.Collections.Generic;
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
38
using Mono.Unix;
39
40
namespace Tomboy
41
{
42
	public class ActionManager : IEnumerable
43
	{
44
		private Gtk.UIManager ui = new Gtk.UIManager ();
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
45
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
46
		private Gtk.ActionGroup main_window_actions =
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
47
		        new Gtk.ActionGroup ("MainWindow");
48
1.1.32 by Pedro Fragoso
Import upstream version 0.9.5
49
		public static Gdk.Pixbuf newNote;
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
50
		public ActionManager ()
51
		{
52
			PopulateActionGroups ();
1.1.33 by Pedro Fragoso
Import upstream version 0.9.6
53
			newNote  = GuiUtils.GetIcon("note-new", 16);       // FIXME: no access to icon theme?
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
54
		}
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
55
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
56
		public void LoadInterface ()
57
		{
58
			ui.AddUiFromResource ("UIManagerLayout.xml");
59
			Gtk.Window.DefaultIconName = "tomboy";
1.1.32 by Pedro Fragoso
Import upstream version 0.9.5
60
			Gtk.ImageMenuItem imageitem = Tomboy.ActionManager.GetWidget (
61
				"/MainWindowMenubar/FileMenu/FileMenuNewNotePlaceholder/NewNote") as Gtk.ImageMenuItem;
62
			if (imageitem != null) {
63
				if (imageitem is Gtk.ImageMenuItem) {
64
					Gtk.ImageMenuItem imageItem = imageitem as Gtk.ImageMenuItem;
65
					(imageItem.Image as Gtk.Image).Pixbuf = newNote;
66
				}
67
			}
68
			
69
			imageitem = Tomboy.ActionManager.GetWidget (
70
				"/TrayIconMenu/TrayNewNotePlaceholder/TrayNewNote") as Gtk.ImageMenuItem;
71
			if (imageitem != null) {
72
				if (imageitem is Gtk.ImageMenuItem) {
73
					Gtk.ImageMenuItem imageItem = imageitem as Gtk.ImageMenuItem;
74
					(imageItem.Image as Gtk.Image).Pixbuf = newNote;
75
				}
76
			}
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
77
		}
78
		
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
79
		/// <summary>
80
		/// Get all widgets represents by XML elements that are children
81
		/// of the placeholder element specified by path.
82
		/// </summary>
83
		/// <param name="path">
84
		/// A <see cref="System.String"/> representing the path to
85
		/// the placeholder of interest.
86
		/// </param>
87
		/// <returns>
88
		/// A <see cref="IList`1"/> of Gtk.Widget objects corresponding
89
		/// to the XML child elements of the placeholder element.
90
		/// </returns>
91
		public IList<Gtk.Widget> GetPlaceholderChildren (string path)
92
		{
93
			List<Gtk.Widget> children = new List<Gtk.Widget> ();
94
			// Wrap the UIManager XML in a root element
95
			// so that it's real parseable XML.
96
			string xml = "<root>" + ui.Ui + "</root>";
97
			
98
			using (StringReader reader = new StringReader (xml)) {
99
				XmlDocument doc = new XmlDocument ();
100
				doc.Load (reader);
101
				
102
				// Get the element name
103
				string placeholderName = path.Substring (path.LastIndexOf ("/") + 1);
104
				
105
				// Find the placeholder specified in the path
106
				foreach (XmlNode placeholderNode in doc.GetElementsByTagName ("placeholder")) {
107
					if (placeholderNode.Attributes ["name"].InnerXml == placeholderName) {
108
						// Return each child element's widget
109
						foreach (XmlNode widgetNode in placeholderNode.ChildNodes) {
110
							string widgetName = widgetNode.Attributes ["name"].InnerXml;
111
							children.Add (GetWidget (path + "/" + widgetName));
112
						}
113
					}
114
				}
115
			}
116
			
117
			return children;
118
		}
119
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
120
		private void PopulateActionGroups ()
121
		{
122
			///
123
			/// Global Actions
124
			///
125
			main_window_actions.Add (new Gtk.ActionEntry [] {
126
				new Gtk.ActionEntry ("FileMenuAction", null,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
127
				Catalog.GetString ("_File"), null, null, null),
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
128
129
				new Gtk.ActionEntry ("NewNoteAction", Gtk.Stock.New,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
130
				Catalog.GetString ("_New"), "<Control>N",
131
				Catalog.GetString ("Create a new note"), null),
132
1.1.34 by Pedro Fragoso
Import upstream version 0.9.7
133
				new Gtk.ActionEntry ("OpenNoteAction", Gtk.Stock.Open,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
134
				Catalog.GetString ("_Open..."), "<Control>O",
135
				Catalog.GetString ("Open the selected note"), null),
136
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
137
				new Gtk.ActionEntry ("DeleteNoteAction", Gtk.Stock.Delete,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
138
				Catalog.GetString ("_Delete"), "Delete",
139
				Catalog.GetString ("Delete the selected note"), null),
140
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
141
				new Gtk.ActionEntry ("CloseWindowAction", Gtk.Stock.Close,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
142
				Catalog.GetString ("_Close"), "<Control>W",
143
				Catalog.GetString ("Close this window"), null),
144
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
145
				new Gtk.ActionEntry ("QuitTomboyAction", Gtk.Stock.Quit,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
146
				Catalog.GetString ("_Quit"), "<Control>Q",
147
				Catalog.GetString ("Quit Tomboy"), null),
148
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
149
				new Gtk.ActionEntry ("EditMenuAction", null,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
150
				Catalog.GetString ("_Edit"), null, null, null),
151
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
152
				new Gtk.ActionEntry ("ShowPreferencesAction", Gtk.Stock.Preferences,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
153
				Catalog.GetString ("_Preferences"), null,
154
				Catalog.GetString ("Tomboy Preferences"), null),
155
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
156
				new Gtk.ActionEntry ("HelpMenuAction", null,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
157
				Catalog.GetString ("_Help"), null, null, null),
158
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
159
				new Gtk.ActionEntry ("ShowHelpAction", Gtk.Stock.Help,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
160
				Catalog.GetString ("_Contents"), "F1",
161
				Catalog.GetString ("Tomboy Help"), null),
162
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
163
				new Gtk.ActionEntry ("ShowAboutAction", Gtk.Stock.About,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
164
				Catalog.GetString ("_About"), null,
165
				Catalog.GetString ("About Tomboy"), null),
166
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
167
				new Gtk.ActionEntry ("TrayIconMenuAction", null,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
168
				Catalog.GetString ("TrayIcon"), null, null, null),
169
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
170
				new Gtk.ActionEntry ("TrayNewNoteAction", Gtk.Stock.New,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
171
				Catalog.GetString ("Create _New Note"), null,
172
				Catalog.GetString ("Create a new note"), null),
173
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
174
				new Gtk.ActionEntry ("ShowSearchAllNotesAction", Gtk.Stock.Find,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
175
				Catalog.GetString ("_Search All Notes"), null,
176
				Catalog.GetString ("Open the Search All Notes window"), null),
177
1.1.22 by Sebastian Dröge
Import upstream version 0.7.3
178
				new Gtk.ActionEntry ("NoteSynchronizationAction", null,
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
179
				Catalog.GetString ("S_ynchronize Notes"), null,
180
				Catalog.GetString ("Start synchronizing notes"), null)
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
181
			});
182
183
			main_window_actions.GetAction ("OpenNoteAction").Sensitive = false;
184
			main_window_actions.GetAction ("DeleteNoteAction").Sensitive = false;
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
185
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
186
			ui.InsertActionGroup (main_window_actions, 0);
1.1.32 by Pedro Fragoso
Import upstream version 0.9.5
187
				
188
			
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
189
		}
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
190
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
191
		public Gtk.Action FindActionByName (string action_name)
192
		{
193
			foreach (Gtk.ActionGroup group in ui.ActionGroups) {
194
				foreach (Gtk.Action action in group.ListActions ()) {
195
					if (action.Name == action_name)
196
						return action;
197
				}
198
			}
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
199
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
200
			return null;
201
		}
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
202
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
203
		public Gtk.Action this [string widget_path_or_action_name]
204
		{
205
			get {
206
				Gtk.Action action = FindActionByName (widget_path_or_action_name);
207
				if (action == null)
208
					return ui.GetAction (widget_path_or_action_name);
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
209
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
210
				return action;
211
			}
212
		}
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
213
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
214
		public Gtk.Widget GetWidget (string widget_path)
215
		{
216
			return ui.GetWidget (widget_path);
217
		}
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
218
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
219
		public void SetActionLabel (string action_name, string label)
220
		{
221
			this [action_name].Label = label;
222
			// FIXME: SyncButtons () ?
223
		}
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
224
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
225
		public void SetActionIcon (string action_name, string icon)
226
		{
227
			this [action_name].StockId = icon;
228
			// FIXME: SyncButtons () ?
229
		}
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
230
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
231
		public void UpdateAction (string action_name, string label, string icon)
232
		{
233
			Gtk.Action action = this [action_name];
234
			action.Label = label;
235
			action.StockId = icon;
236
			// FIXME: SyncButtons () ?
237
		}
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
238
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
239
		public IEnumerator GetEnumerator ()
240
		{
241
			foreach (Gtk.ActionGroup group in ui.ActionGroups) {
242
				foreach (Gtk.Action action in group.ListActions ()) {
243
					yield return action;
244
				}
245
			}
246
		}
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
247
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
248
		public Gtk.UIManager UI
249
		{
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
250
			get {
251
				return ui;
252
			}
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
253
		}
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
254
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
255
		public Gtk.ActionGroup MainWindowActions
256
		{
1.1.31 by Pedro Fragoso
Import upstream version 0.9.4
257
			get {
258
				return main_window_actions;
259
			}
1.1.9 by Sebastian Dröge
Import upstream version 0.5.2
260
		}
261
	}
262
}