Ejemplo n.º 1
0
 public CommandResult(Command cmd, CommandInfo ci, CommandTargetRoute route, string match, string matchedString, int rank)
     : base(match, matchedString, rank)
 {
     this.ci = ci;
     command = cmd;
     this.route = route;
 }
Ejemplo n.º 2
0
		void ICommandUserItem.Update (CommandTargetRoute targetRoute)
		{
			if (commandManager != null) {
				CommandInfo cinfo = commandManager.GetCommandInfo (commandId, targetRoute);
				this.initialTarget = targetRoute.InitialTarget;
				Update (cinfo);
			}
		}
		void ICommandUserItem.Update (CommandTargetRoute targetChain)
		{
			if (commandManager != null && !isArrayItem) {
				CommandInfo cinfo = commandManager.GetCommandInfo (commandId, targetChain);
				this.initialTarget = targetChain.InitialTarget;
				Update (cinfo);
			}
		}
		void AllResults (WorkerResult lastResult, WorkerResult newResult, CancellationToken token)
		{
			CommandTargetRoute route = new CommandTargetRoute (MainToolbar.LastCommandTarget);
			newResult.filteredCommands = new List<Command> ();
			bool startsWithLastFilter = lastResult != null && lastResult.pattern != null && newResult.pattern.StartsWith (lastResult.pattern) && lastResult.filteredCommands != null;
			IEnumerable<Command> allCommands = startsWithLastFilter ? lastResult.filteredCommands : IdeApp.CommandService.GetCommands ();
			foreach (Command cmd in allCommands) {
				token.ThrowIfCancellationRequested ();
				SearchResult curResult = newResult.CheckCommand (cmd, route);
				if (curResult != null) {
					newResult.filteredCommands.Add (cmd);
					newResult.results.AddResult (curResult);
				}
			}
		}
Ejemplo n.º 5
0
		void ICommandBar.Update (object defaultTarget)
		{
			if (!Visible)
				return;
			
			if (initialCommandTarget != null)
				defaultTarget = initialCommandTarget;

			CommandTargetRoute targetRoute = new CommandTargetRoute (defaultTarget);
			foreach (Gtk.Widget item in Children) {
				if (item is ICommandUserItem)
					((ICommandUserItem)item).Update (targetRoute);
				else
					item.Show ();
			}
		}
Ejemplo n.º 6
0
		public override Task GetResults (ISearchResultCallback searchResultCallback, SearchPopupSearchPattern pattern, CancellationToken token)
		{
			return Task.Run (delegate {
				try {
					if (pattern.HasLineNumber)
						return;
					CommandTargetRoute route = new CommandTargetRoute (MainToolbar.LastCommandTarget);
					var matcher = StringMatcher.GetMatcher (pattern.Pattern, false);

					foreach (var cmdTuple in allCommands) {
						token.ThrowIfCancellationRequested ();
						var cmd = cmdTuple.Item1;
						var matchString = cmdTuple.Item2;
						int rank;

						if (matcher.CalcMatchRank (matchString, out rank))
							searchResultCallback.ReportResult (new CommandResult (cmd, null, route, pattern.Pattern, matchString, rank));
					}
				} catch (OperationCanceledException) {
				}
			});
		}
Ejemplo n.º 7
0
		object GetNextCommandTarget (CommandTargetRoute targetRoute, object cmdTarget)
		{
			if (cmdTarget is IMultiCastCommandRouter) 
				cmdTarget = new MultiCastDelegator (this, (IMultiCastCommandRouter)cmdTarget, targetRoute);
			
			if (cmdTarget is ICommandDelegatorRouter) {
				object oldCmdTarget = cmdTarget;
				cmdTarget = ((ICommandDelegatorRouter)oldCmdTarget).GetDelegatedCommandTarget ();
				if (cmdTarget != null)
					delegatorStack.Push (oldCmdTarget);
				else
					cmdTarget = ((ICommandDelegatorRouter)oldCmdTarget).GetNextCommandTarget ();
			}
			else if (cmdTarget is ICommandRouter)
				cmdTarget = ((ICommandRouter)cmdTarget).GetNextCommandTarget ();
			else if (cmdTarget is Gtk.Widget)
				cmdTarget = ((Gtk.Widget)cmdTarget).Parent;
			else
				cmdTarget = null;
			
			if (cmdTarget == null || !visitedTargets.Add (cmdTarget)) {
				if (delegatorStack.Count > 0) {
					ICommandDelegatorRouter del = (ICommandDelegatorRouter) delegatorStack.Pop ();
					cmdTarget = del.GetNextCommandTarget ();
					if (cmdTarget == CommandManager.CommandRouteTerminator)
						return null;
					if (cmdTarget != null)
						return cmdTarget;
				}
				return globalHandlerChain;
			} else
				return cmdTarget;
		}
Ejemplo n.º 8
0
		public MultiCastDelegator (CommandManager manager, IMultiCastCommandRouter mcr, CommandTargetRoute route)
		{
			this.manager = manager;
			enumerator = mcr.GetCommandTargets ().GetEnumerator ();
			this.route = route;
		}
Ejemplo n.º 9
0
		object GetNextCommandTarget (CommandTargetRoute targetRoute, object cmdTarget, bool ignoreDelegator = false)
		{
			if (cmdTarget is IMultiCastCommandRouter) 
				cmdTarget = new MultiCastDelegator (this, (IMultiCastCommandRouter)cmdTarget, targetRoute);

			if (!ignoreDelegator && cmdTarget is ICommandDelegator) {
				if (cmdTarget is ICommandDelegatorRouter)
					throw new InvalidOperationException ("A type can't implement both ICommandDelegator and ICommandDelegatorRouter");
				object oldCmdTarget = cmdTarget;
				cmdTarget = ((ICommandDelegator)oldCmdTarget).GetDelegatedCommandTarget ();
				if (cmdTarget != null)
					delegatorStack.Push (oldCmdTarget);
				else
					cmdTarget = GetNextCommandTarget (targetRoute, oldCmdTarget, true);
			}
			else if (cmdTarget is ICommandDelegatorRouter) {
				object oldCmdTarget = cmdTarget;
				cmdTarget = ((ICommandDelegatorRouter)oldCmdTarget).GetDelegatedCommandTarget ();
				if (cmdTarget != null)
					delegatorStack.Push (oldCmdTarget);
				else
					cmdTarget = ((ICommandDelegatorRouter)oldCmdTarget).GetNextCommandTarget ();
			}
			else if (cmdTarget is ICommandRouter)
				cmdTarget = ((ICommandRouter)cmdTarget).GetNextCommandTarget ();
			else if (cmdTarget is Gtk.Widget)
				cmdTarget = ((Gtk.Widget)cmdTarget).Parent;
			else
				cmdTarget = null;
			
			if (cmdTarget == null || !visitedTargets.Add (cmdTarget)) {
				if (delegatorStack.Count > 0) {
					var del = delegatorStack.Pop ();
					if (del is ICommandDelegatorRouter)
						cmdTarget = ((ICommandDelegatorRouter)del).GetNextCommandTarget ();
					else
						cmdTarget = GetNextCommandTarget (targetRoute, del, true);
					if (cmdTarget == CommandManager.CommandRouteTerminator)
						return null;
					if (cmdTarget != null)
						return cmdTarget;
				}
				return globalHandlerChain;
			} else
				return cmdTarget;
		}
Ejemplo n.º 10
0
		internal bool DispatchCommandFromAccel (object commandId, object dataItem, object initialTarget)
		{
			// Dispatches a command that has been fired by an accelerator.
			// The difference from a normal dispatch is that there may
			// be several commands bound to the same accelerator, and in
			// this case it will execute the one that is enabled.
			
			// If the original key has been modified
			// by a CommandUpdate handler, it won't work. That's a limitation,
			// but checking all possible commands would be too slow.
			
			Command cmd = GetCommand (commandId);
			if (cmd == null)
				return false;
			
			string accel = cmd.AccelKey;
			KeyBinding binding;
			
			if (accel == null || !KeyBinding.TryParse (accel, out binding))
				return DispatchCommand (commandId, dataItem, initialTarget, CommandSource.Keybinding);
			
			List<Command> list = bindings.Commands (binding);
			if (list == null || list.Count == 1) {
				// The command is not overloaded, so it can be handled normally.
				return DispatchCommand (commandId, dataItem, initialTarget, CommandSource.Keybinding);
			}
			
			CommandTargetRoute targetChain = new CommandTargetRoute (initialTarget);
			
			// Get the accelerator used to fire the command and make sure it has not changed.
			CommandInfo accelInfo = GetCommandInfo (commandId, targetChain);
			bool res = DispatchCommand (commandId, accelInfo.DataItem, initialTarget, CommandSource.Keybinding);

			// If the accelerator has changed, we can't handle overloading.
			if (res || accel != accelInfo.AccelKey)
				return res;
			
			// Execution failed. Now try to execute alternate commands
			// bound to the same key.
			
			for (int i = 0; i < list.Count; i++) {
				if (list[i].Id == commandId) // already handled above.
					continue;
				
				CommandInfo cinfo = GetCommandInfo (list[i].Id, targetChain);
				if (cinfo.AccelKey != accel) // Key changed by a handler, just ignore the command.
					continue;
				
				if (DispatchCommand (list[i].Id, cinfo.DataItem, initialTarget, CommandSource.Keybinding))
					return true;
			}
			
			return false;
		}
Ejemplo n.º 11
0
		/// <summary>
		/// Retrieves status information about a command by looking for a handler in the active command route.
		/// </summary>
		/// <returns>
		/// The command information.
		/// </returns>
		/// <param name='commandId'>
		/// Identifier of the command.
		/// </param>
		/// <param name='targetRoute'>
		/// Command route origin
		/// </param>
		public CommandInfo GetCommandInfo (object commandId, CommandTargetRoute targetRoute)
		{
			commandId = CommandManager.ToCommandId (commandId);
			ActionCommand cmd = GetActionCommand (commandId);
			if (cmd == null)
				throw new InvalidOperationException ("Invalid action command id: " + commandId);

			NotifyCommandTargetScanStarted ();
			CommandInfo info = new CommandInfo (cmd);

			try {
				bool multiCastEnabled = true;
				bool multiCastVisible = false;

				CurrentCommand = cmd;

				object cmdTarget = GetFirstCommandTarget (targetRoute);
				
				while (cmdTarget != null)
				{
					HandlerTypeInfo typeInfo = GetTypeHandlerInfo (cmdTarget);
					CommandUpdaterInfo cui = typeInfo.GetCommandUpdater (commandId);
					
					bool bypass = false;
					bool handlerFound = false;
					
					if (cui != null) {
						if (cmd.CommandArray) {
							info.ArrayInfo = new CommandArrayInfo (info);
							cui.Run (cmdTarget, info.ArrayInfo);
							if (!info.ArrayInfo.Bypass) {
								if (guiLock > 0)
									info.Enabled = false;
								handlerFound = true;
							}
						}
						else {
							info.Bypass = false;
							cui.Run (cmdTarget, info);
							if (!info.Bypass) {
								if (guiLock > 0)
									info.Enabled = false;
								handlerFound = true;
							}
						}
						if (!handlerFound)
							bypass = true;
					}

					if (handlerFound) {
						handlerFoundInMulticast = true;
						if (!info.Enabled || !info.Visible)
							multiCastEnabled = false;
						if (info.Visible)
							multiCastVisible = true;
						cmdTarget = NextMulticastTarget (targetRoute);
						if (cmdTarget == null) {
							if (!multiCastEnabled)
								info.Enabled = false;
							if (multiCastVisible)
								info.Visible = true;
							return info;
						}
						continue;
					}
					else if (!bypass && typeInfo.GetCommandHandler (commandId) != null) {
						info.Enabled = guiLock == 0;
						info.Visible = true;
						
						return info;
					}
					
					cmdTarget = GetNextCommandTarget (targetRoute, cmdTarget);
				}
				
				info.Bypass = false;
				DefaultUpdateCommandInfo (cmd, info);
			}
			catch (Exception ex) {
				if (!commandUpdateErrors.Contains (commandId)) {
					commandUpdateErrors.Add (commandId);
					ReportError (commandId, "Error while updating status of command: " + commandId, ex);
				}
				info.Enabled = false;
				info.Visible = true;
			} finally {
				NotifyCommandTargetScanFinished ();
				CurrentCommand = null;
			}

			if (guiLock > 0)
				info.Enabled = false;
			return info;
		}
Ejemplo n.º 12
0
		object GetNextCommandTarget (CommandTargetRoute targetRoute, object cmdTarget, bool ignoreDelegator = false)
		{
			if (cmdTarget is IMultiCastCommandRouter) 
				cmdTarget = new MultiCastDelegator (this, (IMultiCastCommandRouter)cmdTarget, targetRoute);

			if (!ignoreDelegator && cmdTarget is ICommandDelegator) {
				if (cmdTarget is ICommandDelegatorRouter)
					throw new InvalidOperationException ("A type can't implement both ICommandDelegator and ICommandDelegatorRouter");
				object oldCmdTarget = cmdTarget;
				cmdTarget = ((ICommandDelegator)oldCmdTarget).GetDelegatedCommandTarget ();
				if (cmdTarget != null)
					delegatorStack.Push (oldCmdTarget);
				else
					cmdTarget = GetNextCommandTarget (targetRoute, oldCmdTarget, true);
			}
			else if (cmdTarget is ICommandDelegatorRouter) {
				object oldCmdTarget = cmdTarget;
				cmdTarget = ((ICommandDelegatorRouter)oldCmdTarget).GetDelegatedCommandTarget ();
				if (cmdTarget != null)
					delegatorStack.Push (oldCmdTarget);
				else
					cmdTarget = ((ICommandDelegatorRouter)oldCmdTarget).GetNextCommandTarget ();
			}
			else if (cmdTarget is ICommandRouter)
				cmdTarget = ((ICommandRouter)cmdTarget).GetNextCommandTarget ();
			else if (cmdTarget is Gtk.Widget)
				cmdTarget = ((Gtk.Widget)cmdTarget).Parent;
			#if MAC
			else if (cmdTarget is AppKit.NSView) {
				var v = (AppKit.NSView) cmdTarget;
				if (v.Superview != null && IsRootGdkQuartzView (v.Superview))
					// FIXME: We should get here the GTK parent of the superview. Since there is no api for this
					// right now, we rely on it being set by GetActiveWidget()
					cmdTarget = null;
				else
					cmdTarget = v.Superview;
			}
			#endif
			else
				cmdTarget = null;
			
			if (cmdTarget == null || !visitedTargets.Add (cmdTarget)) {
				if (delegatorStack.Count > 0) {
					var del = delegatorStack.Pop ();
					if (del is ICommandDelegatorRouter)
						cmdTarget = ((ICommandDelegatorRouter)del).GetNextCommandTarget ();
					else
						cmdTarget = GetNextCommandTarget (targetRoute, del, true);
					if (cmdTarget == CommandManager.CommandRouteTerminator)
						return null;
					if (cmdTarget != null)
						return cmdTarget;
				}
				return globalHandlerChain;
			} else
				return cmdTarget;
		}
Ejemplo n.º 13
0
		public void Update (CommandTargetRoute targetRoute)
		{
			CommandInfo cmdInfo = IdeApp.CommandService.GetCommandInfo (cmdId, targetRoute);
			
			bool hasAccel = string.IsNullOrEmpty (cmdInfo.AccelKey);
			bool hasIcon = !cmdInfo.Icon.IsNull;
			string desc = cmdInfo.Description;
			
			//If the button only has an icon it's not always clear what it does. In such cases, use the label as a
			//fallback tooltip. Also do this if there's an accelerator, so the user can see what it is.
			if (string.IsNullOrEmpty (desc) && (hasIcon || hasAccel))
				desc = cmdInfo.Text;
			
			if (lastDesc != desc) {
				string toolTip;
				if (hasAccel) {
					toolTip = desc;
				} else {
					toolTip = desc + " (" + KeyBindingManager.BindingToDisplayLabel (cmdInfo.AccelKey, false) + ")";
				}
				button.TooltipText = toolTip;
				lastDesc = desc;
			}
			
			if (!hasIcon && button.Label != cmdInfo.Text)
				button.Label = cmdInfo.Text;
			
			if (cmdInfo.Icon != stockId) {
				stockId = cmdInfo.Icon;
				button.Image = new Gtk.Image (cmdInfo.Icon, Gtk.IconSize.Menu);
			}
			if (cmdInfo.Enabled != button.Sensitive)
				button.Sensitive = cmdInfo.Enabled;
			if (cmdInfo.Visible != button.Visible)
				button.Visible = cmdInfo.Visible;
			
			ToggleButton toggle = button as ToggleButton;
			if (toggle != null && cmdInfo.Checked != toggle.Active)
				toggle.Active = cmdInfo.Checked;
				
			if (button.Image != null)
				button.Image.Show ();
		}
Ejemplo n.º 14
0
		internal void Update ()
		{
			CommandTargetRoute targetRoute = new CommandTargetRoute (initialCommandTarget);
			foreach (Gtk.Widget item in Children) {
				if (item is ICommandUserItem)
					((ICommandUserItem)item).Update (targetRoute);
				else if (item is Gtk.MenuItem) {
					Gtk.MenuItem mitem = (Gtk.MenuItem) item;
					CommandMenu men = mitem.Submenu as CommandMenu;
					if (men != null)
						men.InitialCommandTarget = initialCommandTarget;
					item.Show ();
					if (item is AutoHideMenuItem) {
						men.Update ();
						if (!((AutoHideMenuItem)item).HasVisibleChildren)
							item.Hide ();
					}
				}
				else
					item.Show ();
			}
			
			// After updating the menu, hide the separators which don't actually
			// separate items.
			bool prevWasItem = false;
			Gtk.Widget lastSeparator = null;
			foreach (Gtk.Widget item in Children) {
				if (item is Gtk.SeparatorMenuItem) {
					if (!prevWasItem)
						item.Hide ();
					else {
						prevWasItem = false;
						lastSeparator = item;
					}
				} else if (item.Visible)
					prevWasItem = true;
			}
			if (!prevWasItem && lastSeparator != null)
				lastSeparator.Hide ();
		}
Ejemplo n.º 15
0
		public void InsertOptions (Gtk.Menu menu, CommandEntrySet entrySet, int index)
		{
			CommandTargetRoute route = new CommandTargetRoute ();
			foreach (CommandEntry entry in entrySet) {
				Gtk.MenuItem item = entry.CreateMenuItem (this);
				CustomItem ci = item.Child as CustomItem;
				if (ci != null)
					ci.SetMenuStyle (menu);
				int n = menu.Children.Length;
				menu.Insert (item, index);
				if (item is ICommandUserItem)
					((ICommandUserItem)item).Update (route);
				else
					item.Show ();
				index += menu.Children.Length - n;
			}
		}
Ejemplo n.º 16
0
		public object VisitCommandTargets (ICommandTargetVisitor visitor, object initialTarget)
		{
			CommandTargetRoute targetRoute = new CommandTargetRoute (initialTarget);
			object cmdTarget = GetFirstCommandTarget (targetRoute);
			
			while (cmdTarget != null)
			{
				if (visitor.Visit (cmdTarget))
					return cmdTarget;

				cmdTarget = GetNextCommandTarget (targetRoute, cmdTarget);
			}
			
			visitor.Visit (null);
			return null;
		}
		public void Update (CommandTargetRoute targetRoute)
		{
			CommandInfo cmdInfo = IdeApp.CommandService.GetCommandInfo (cmdId, targetRoute);
			
			if (lastDesc != cmdInfo.Description) {
				string toolTip;
				if (string.IsNullOrEmpty (cmdInfo.AccelKey)) {
					toolTip = cmdInfo.Description;
				} else {
					toolTip = cmdInfo.Description + " (" + KeyBindingManager.BindingToDisplayLabel (cmdInfo.AccelKey, false) + ")";
				}
				button.TooltipText = toolTip;
				lastDesc = cmdInfo.Description;
			}
			
			if (cmdInfo.Icon.IsNull && button.Label != cmdInfo.Text)
				button.Label = cmdInfo.Text;
			
			if (cmdInfo.Icon != stockId) {
				stockId = cmdInfo.Icon;
				button.Image = new Gtk.Image (cmdInfo.Icon, Gtk.IconSize.Menu);
			}
			if (cmdInfo.Enabled != button.Sensitive)
				button.Sensitive = cmdInfo.Enabled;
			if (cmdInfo.Visible != button.Visible)
				button.Visible = cmdInfo.Visible;
			
			ToggleButton toggle = button as ToggleButton;
			if (toggle != null && cmdInfo.Checked != toggle.Active)
				toggle.Active = cmdInfo.Checked;
				
			if (button.Image != null)
				button.Image.Show ();
		}
Ejemplo n.º 18
0
		/// <summary>
		/// Visits the active command route
		/// </summary>
		/// <returns>
		/// Visitor result
		/// </returns>
		/// <param name='visitor'>
		/// Visitor.
		/// </param>
		/// <param name='initialTarget'>
		/// Initial target (provide null to use the default initial target)
		/// </param>
		public object VisitCommandTargets (ICommandTargetVisitor visitor, object initialTarget)
		{
			CommandTargetRoute targetRoute = new CommandTargetRoute (initialTarget);
			object cmdTarget = GetFirstCommandTarget (targetRoute);

			visitor.Start ();

			try {
				while (cmdTarget != null)
				{
					if (visitor.Visit (cmdTarget))
						return cmdTarget;

					cmdTarget = GetNextCommandTarget (targetRoute, cmdTarget);
				}
			} catch (Exception ex) {
				LoggingService.LogError ("Error while visiting command targets", ex);
			} finally {
				visitor.End ();
			}
			return null;
		}
		public void Update (object activeTarget)
		{
			CommandTargetRoute route = new CommandTargetRoute (initialTarget ?? activeTarget);
			foreach (ToolButtonStatus bs in buttons)
				bs.Update (route);
		}
Ejemplo n.º 20
0
		object GetFirstCommandTarget (CommandTargetRoute targetRoute)
		{
			delegatorStack.Clear ();
			visitedTargets.Clear ();
			handlerFoundInMulticast = false;
			object cmdTarget;
			if (targetRoute.InitialTarget != null)
				cmdTarget = targetRoute.InitialTarget;
			else {
				cmdTarget = GetActiveWidget (GetCurrentFocusedTopLevelWindow ());
				if (cmdTarget == null) {
					cmdTarget = globalHandlerChain;
				}
			}
			visitedTargets.Add (cmdTarget);
			return cmdTarget;
		}
Ejemplo n.º 21
0
            internal SearchResult CheckCommand(Command c, CommandTargetRoute route)
            {
                ActionCommand cmd = c as ActionCommand;
                if (cmd == null || cmd.CommandArray)
                    return null;

                int rank;
                string matchString = cmd.Text.Replace ("_", "");
                if (MatchName (matchString, out rank)) {
                    try {
                        var ci = IdeApp.CommandService.GetCommandInfo (cmd.Id, route);
                        if (ci.Enabled && ci.Visible)
                            return new CommandResult (cmd, ci, route, pattern, matchString, rank);
                    } catch (Exception 	ex) {
                        LoggingService.LogError ("Failure while checking command: " + cmd.Id, ex);
                    }
                }
                return null;
            }
Ejemplo n.º 22
0
		internal object NextMulticastTarget (CommandTargetRoute targetRoute)
		{
			while (delegatorStack.Count > 0) {
				MultiCastDelegator del = delegatorStack.Pop () as MultiCastDelegator;
				if (del != null) {
					object cmdTarget = GetNextCommandTarget (targetRoute, del);
					return cmdTarget == globalHandlerChain ? null : cmdTarget;
				}
			}
			return null;
		}
Ejemplo n.º 23
0
		//updates commands and populates arrays and dynamic menus
		//NOTE: when Help menu is opened, Mac OS calls this for ALL menus because the Help menu can search menu items
		static CarbonEventHandlerStatus HandleMenuOpening (IntPtr callRef, IntPtr eventRef, IntPtr user_data)
		{
			DestroyOldMenuObjects ();
			menuOpenDepth++;
			try {
				IntPtr menuRef = Carbon.GetEventParameter (eventRef, CarbonEventParameterName.DirectObject, CarbonEventParameterType.MenuRef);
				
				//don't update dynamic menus recursively
				if (!mainMenus.Contains (menuRef) && menuRef != appMenu)
					return CarbonEventHandlerStatus.NotHandled;
				
			//	uint cmd = HIToolbox.GetMenuItemCommandID (new HIMenuItem (menuRef, 0));
				
				CommandTargetRoute route = new CommandTargetRoute ();
				ushort count = HIToolbox.CountMenuItems (menuRef);
				for (ushort i = 1; i <= count; i++) {
					HIMenuItem mi = new HIMenuItem (menuRef, i);
					uint macCmdID = HIToolbox.GetMenuItemCommandID (mi);
					object cmdID;
					
					//link items
					if (macCmdID == linkCommandId) {
						if (IsGloballyDisabled)
							HIToolbox.DisableMenuItem (mi);
						else
							HIToolbox.EnableMenuItem (mi);
						continue;
					}
					
					if (macCmdID == submenuCommandId)
						continue;
					
					if (macCmdID == autohideSubmenuCommandId) {
						UpdateAutoHide (new HIMenuItem (menuRef, i));
						continue;
					}
					
					//items that map to command objects
					if (!commands.TryGetValue (macCmdID, out cmdID) || cmdID == null)
						continue;
					
					CommandInfo cinfo = manager.GetCommandInfo (cmdID, route);
					menuIdMap[cmdID] = HIToolbox.GetMenuID (menuRef);
					UpdateMenuItem (menuRef, menuRef, ref i, ref count, macCmdID, cinfo);
				}
			} catch (Exception ex) {
				System.Console.WriteLine (ex);
			}
			
			return CarbonEventHandlerStatus.NotHandled;
		}
Ejemplo n.º 24
0
		/// <summary>
		/// Dispatches a command.
		/// </summary>
		/// <returns>
		/// True if a handler for the command was found
		/// </returns>
		/// <param name='commandId'>
		/// Identifier of the command
		/// </param>
		/// <param name='dataItem'>
		/// Data item for the command. It must be one of the data items obtained by calling GetCommandInfo.
		/// </param>
		/// <param name='initialTarget'>
		/// Initial command route target. The command handler will start looking for command handlers in this object.
		/// </param>
		/// <param name='source'>
		/// What is causing the command to be dispatched
		/// </param>
		public bool DispatchCommand (object commandId, object dataItem, object initialTarget, CommandSource source)
		{
			RegisterUserInteraction ();
			
			if (guiLock > 0)
				return false;

			commandId = CommandManager.ToCommandId (commandId);
			
			List<HandlerCallback> handlers = new List<HandlerCallback> ();
			ActionCommand cmd = null;
			try {
				cmd = GetActionCommand (commandId);
				if (cmd == null)
					return false;

				CurrentCommand = cmd;
				CommandTargetRoute targetRoute = new CommandTargetRoute (initialTarget);
				object cmdTarget = GetFirstCommandTarget (targetRoute);
				CommandInfo info = new CommandInfo (cmd);

				while (cmdTarget != null)
				{
					HandlerTypeInfo typeInfo = GetTypeHandlerInfo (cmdTarget);
					
					bool bypass = false;
					
					CommandUpdaterInfo cui = typeInfo.GetCommandUpdater (commandId);
					if (cui != null) {
						if (cmd.CommandArray) {
							// Make sure that the option is still active
							info.ArrayInfo = new CommandArrayInfo (info);
							cui.Run (cmdTarget, info.ArrayInfo);
							if (!info.ArrayInfo.Bypass) {
								if (info.ArrayInfo.FindCommandInfo (dataItem) == null)
									return false;
							} else
								bypass = true;
						} else {
							info.Bypass = false;
							cui.Run (cmdTarget, info);
							bypass = info.Bypass;
							
							if (!bypass && (!info.Enabled || !info.Visible))
								return false;
						}
					}
					
					if (!bypass) {
						CommandHandlerInfo chi = typeInfo.GetCommandHandler (commandId);
						if (chi != null) {
							object localTarget = cmdTarget;
							if (cmd.CommandArray) {
								handlers.Add (delegate {
									OnCommandActivating (commandId, info, dataItem, localTarget, source);
									chi.Run (localTarget, cmd, dataItem);
									OnCommandActivated (commandId, info, dataItem, localTarget, source);
								});
							}
							else {
								handlers.Add (delegate {
									OnCommandActivating (commandId, info, dataItem, localTarget, source);
									chi.Run (localTarget, cmd);
									OnCommandActivated (commandId, info, dataItem, localTarget, source);
								});
							}
							handlerFoundInMulticast = true;
							cmdTarget = NextMulticastTarget (targetRoute);
							if (cmdTarget == null)
								break;
							else
								continue;
						}
					}
					cmdTarget = GetNextCommandTarget (targetRoute, cmdTarget);
				}

				if (handlers.Count > 0) {
					foreach (HandlerCallback c in handlers)
						c ();
					UpdateToolbars ();
					return true;
				}
	
				if (DefaultDispatchCommand (cmd, info, dataItem, cmdTarget, source)) {
					UpdateToolbars ();
					return true;
				}
			}
			catch (Exception ex) {
				string name = (cmd != null && cmd.Text != null && cmd.Text.Length > 0) ? cmd.Text : commandId.ToString ();
				name = name.Replace ("_","");
				ReportError (commandId, "Error while executing command: " + name, ex);
			}
			finally {
				CurrentCommand = null;
			}
			return false;
		}
Ejemplo n.º 25
0
		static bool HasVisibleItems (IntPtr submenu)
		{
			var route = new CommandTargetRoute ();
			ushort count = HIToolbox.CountMenuItems (submenu);
			
			for (ushort i = 1; i <= count; i++) {
				HIMenuItem mi = new HIMenuItem (submenu, i);
				uint macCmdID = HIToolbox.GetMenuItemCommandID (mi);
				object cmdID;
				
				if (macCmdID == linkCommandId)
					return true;
				
				if (!commands.TryGetValue (macCmdID, out cmdID) || cmdID == null)
					continue;
				
				CommandInfo cinfo = manager.GetCommandInfo (cmdID, route);
				if (cinfo.ArrayInfo != null) {
					foreach (CommandInfo ci in cinfo.ArrayInfo)
						if (ci.Visible)
							return true;
				} else if (cinfo.Visible) {
					return true;
				}
			}
			
			return false;
		}