5
// Aaron Bockover <abockover@novell.com>
7
// Copyright (C) 2006-2007 Novell, Inc.
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
using System.Reflection;
32
using System.Collections.Generic;
35
using Action = Gtk.Action;
41
public class ActionManager
43
private UIManager ui_manager;
44
private Dictionary<string, ActionGroup> action_groups = new Dictionary<string, ActionGroup> ();
46
public ActionManager ()
48
ui_manager = new UIManager ();
51
public virtual void Initialize ()
55
private void InnerAddActionGroup (ActionGroup group)
57
action_groups.Add (group.Name, group);
58
ui_manager.InsertActionGroup (group, 0);
61
public void AddActionGroup (string name)
64
if (action_groups.ContainsKey (name)) {
65
throw new ApplicationException ("Group already exists");
68
InnerAddActionGroup (new ActionGroup (name));
72
public void AddActionGroup (ActionGroup group)
75
if (action_groups.ContainsKey (group.Name)) {
76
throw new ApplicationException ("Group already exists");
79
InnerAddActionGroup (group);
83
public void RemoveActionGroup (string name)
86
if (action_groups.ContainsKey (name)) {
87
ActionGroup group = action_groups[name];
88
ui_manager.RemoveActionGroup (group);
89
action_groups.Remove (name);
94
public void RemoveActionGroup (ActionGroup group)
96
RemoveActionGroup (group.Name);
99
public ActionGroup FindActionGroup (string actionGroupId)
101
foreach (ActionGroup group in action_groups.Values) {
102
if (group.Name == actionGroupId) {
110
public Gtk.Action FindAction (string actionId)
112
string [] parts = actionId.Split ('.');
114
if (parts == null || parts.Length < 2) {
118
string group_name = parts[0];
119
string action_name = parts[1];
121
ActionGroup group = FindActionGroup (group_name);
122
return group == null ? null : group.GetAction (action_name);
125
public void PopulateToolbarPlaceholder (Toolbar toolbar, string path, Widget item)
127
PopulateToolbarPlaceholder (toolbar, path, item, false);
130
public void PopulateToolbarPlaceholder (Toolbar toolbar, string path, Widget item, bool expand)
132
ToolItem placeholder = (ToolItem)UIManager.GetWidget (path);
133
int position = toolbar.GetItemIndex (placeholder);
134
toolbar.Remove (placeholder);
136
if (item is ToolItem) {
137
((ToolItem)item).Expand = expand;
138
toolbar.Insert ((ToolItem)item, position);
140
ToolItem container_item = new Hyena.Widgets.GenericToolItem<Widget> (item);
141
container_item.Expand = expand;
142
container_item.Show ();
143
toolbar.Insert (container_item, position);
147
public uint AddUiFromFileInCurrentAssembly (string ui_file)
149
return AddUiFromFile (ui_file, Assembly.GetCallingAssembly ());
152
public uint AddUiFromFile (string ui_file, Assembly assembly)
154
if (ui_file != null) {
155
using (StreamReader reader = new StreamReader (assembly.GetManifestResourceStream (ui_file))) {
156
return ui_manager.AddUiFromString (reader.ReadToEnd ());
162
public Gtk.Action this[string actionId] {
163
get { return FindAction (actionId); }
166
public UIManager UIManager {
167
get { return ui_manager; }