7
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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.
30
using System.Collections;
32
namespace MonoDevelop.Components.Commands
34
public class CommandMenuItem: Gtk.ImageMenuItem, ICommandMenuItem
36
CommandManager commandManager;
44
bool wasButtonActivation;
46
bool disabledVisible = true;
47
CommandInfo lastCmdInfo;
49
public CommandMenuItem (object commandId, CommandManager commandManager, string overrideLabel, bool disabledVisible): base ("")
51
this.commandId = commandId;
52
this.commandManager = commandManager;
53
this.overrideLabel = overrideLabel;
54
this.disabledVisible = disabledVisible;
55
ActionCommand cmd = commandManager.GetCommand (commandId) as ActionCommand;
57
isArray = cmd.CommandArray;
60
public CommandMenuItem (object commandId, CommandManager commandManager): this (commandId, commandManager, null, true)
64
void ICommandUserItem.Update (object initialTarget)
66
if (commandManager != null && !isArrayItem) {
67
CommandInfo cinfo = commandManager.GetCommandInfo (commandId, initialTarget);
68
this.initialTarget = initialTarget;
73
void ICommandMenuItem.SetUpdateInfo (CommandInfo cmdInfo, object initialTarget)
76
this.initialTarget = initialTarget;
77
arrayDataItem = cmdInfo.DataItem;
81
protected override void OnParentSet (Gtk.Widget parent)
83
base.OnParentSet (parent);
87
((ICommandUserItem)this).Update (null);
90
// Make sure the accelerators always work for this item
91
// while the menu is hidden
97
protected override bool OnButtonReleaseEvent (Gdk.EventButton ev)
99
wasButtonActivation = true;
100
return base.OnButtonReleaseEvent (ev);
103
protected override void OnActivated ()
107
if (commandManager == null)
108
throw new InvalidOperationException ();
110
if (!wasButtonActivation) {
111
// It's being activated by an accelerator.
113
commandManager.DispatchCommandFromAccel (commandId, arrayDataItem, initialTarget);
115
wasButtonActivation = false;
116
commandManager.DispatchCommand (commandId, arrayDataItem, initialTarget);
120
protected override void OnSelected ()
122
if (commandManager != null)
123
commandManager.NotifySelected (lastCmdInfo);
127
protected override void OnDeselected ()
129
if (commandManager != null)
130
commandManager.NotifyDeselected ();
131
base.OnDeselected ();
134
void Update (CommandInfo cmdInfo)
136
lastCmdInfo = cmdInfo;
137
if (isArray && !isArrayItem) {
138
this.Visible = false;
139
Gtk.Menu menu = (Gtk.Menu) Parent;
141
if (itemArray != null) {
142
foreach (Gtk.MenuItem item in itemArray)
146
itemArray = new ArrayList ();
147
int i = Array.IndexOf (menu.Children, this);
149
if (cmdInfo.ArrayInfo != null) {
150
foreach (CommandInfo info in cmdInfo.ArrayInfo) {
152
if (info.IsArraySeparator) {
153
item = new Gtk.SeparatorMenuItem ();
156
item = CommandEntry.CreateMenuItem (commandManager, commandId, false);
157
ICommandMenuItem mi = (ICommandMenuItem) item;
158
mi.SetUpdateInfo (info, initialTarget);
160
menu.Insert (item, ++i);
161
itemArray.Add (item);
165
Gtk.Widget child = Child;
169
Gtk.Label accel_label = null;
170
Gtk.Label label = null;
172
if (!(child is Gtk.HBox)) {
173
child = new Gtk.HBox (false, 0);
174
accel_label = new Gtk.Label ("");
175
accel_label.UseUnderline = false;
176
accel_label.Xalign = 1.0f;
179
label = new Gtk.Label ("");
180
label.UseUnderline = true;
184
((Gtk.Box) child).PackStart (label);
185
((Gtk.Box) child).PackStart (accel_label);
191
accel_label = (Gtk.Label) ((Gtk.Box) child).Children[1];
192
label = (Gtk.Label) ((Gtk.Box) child).Children[0];
195
if (cmdInfo.AccelKey != null)
196
accel_label.Text = " " + KeyBindingManager.BindingToDisplayLabel (cmdInfo.AccelKey, true);
198
accel_label.Text = String.Empty;
200
if (cmdInfo.UseMarkup) {
201
label.Markup = overrideLabel ?? cmdInfo.Text;
202
label.UseMarkup = true;
204
label.Text = overrideLabel ?? cmdInfo.Text;
205
label.UseMarkup = false;
208
label.UseUnderline = true;
210
this.Sensitive = cmdInfo.Enabled;
211
this.Visible = cmdInfo.Visible && (disabledVisible || cmdInfo.Enabled);
213
if (cmdInfo.Icon != null && cmdInfo.Icon != "" && cmdInfo.Icon != lastIcon) {
214
Image = new Gtk.Image (cmdInfo.Icon, Gtk.IconSize.Menu);
215
lastIcon = cmdInfo.Icon;
218
if (cmdInfo is CommandInfoSet) {
219
CommandInfoSet ciset = (CommandInfoSet) cmdInfo;
220
Gtk.Menu smenu = new Gtk.Menu ();
222
foreach (CommandInfo info in ciset.CommandInfos) {
224
if (info.IsArraySeparator) {
225
item = new Gtk.SeparatorMenuItem ();
228
item = CommandEntry.CreateMenuItem (commandManager, commandId, false);
229
ICommandMenuItem mi = (ICommandMenuItem) item;
230
mi.SetUpdateInfo (info, initialTarget);