/// <summary> /// Raises the MouseDown event. /// </summary> /// <param name="e">A MouseEventArgs that contains the event data.</param> protected override void OnMouseDown(MouseEventArgs e) { Point pt = new Point(e.X, e.Y); // Do we currently have a command open? if (IsCurrentCommandOpen) { // Then need to close it up RevertCurrentCommandState(); } // If we have not captured the mouse if (!_mouseCapture) { // Remove any tooltip KillTooltip(); // We can only open or press a command with the left mouse if (e.Button == MouseButtons.Left) { // Find the command the mouse is over CommandState commandDown = CommandStateFromPoint(pt); // If mouse down occurs over a command if (commandDown != null) { // Can the target command be opened? if (CanCommandOpen(commandDown)) { // Yes, so ask command to open itself MakeStateOpen(commandDown, pt); } else { // No, so change state to pressed MakeStatePressed(commandDown, pt); // Remember which button caused the capture _mouseDownButton = e.Button; // Remember we have captured the mouse _mouseCapture = true; } } } } }
private void KillTooltip() { // Do not want timer to fire again _hoverTimer.Stop(); // Check if there is anything to kill if (_popupTooltip != null) { // Remove the tooltip _popupTooltip.Close(); _popupTooltip.Dispose(); _popupTooltip = null; // No longer cache associated command _tooltipCmdState = null; } }
private void InternalConstruct() { // Create internal state objects _details = new CommandDetails(this); _engine = new SingleLayoutEngine(); _states = new CommandStateCollection(); _padding = new Padding(); // Define state _initCount = 0; _layoutRequired = false; _mouseDownButton = MouseButtons.None; _mouseCapture = false; _currentCmdState = null; _tooltipCmdState = null; // Hook into padding changed events _padding.PaddingChanged += new EventHandler(OnPaddingChanged); // Create exposed/internal collections of commands _externals = new CommandBaseCollection(); _internals = new CommandBaseCollection(); // Hook into command collection modifications _externals.Clearing += new CollectionClear(OnCommandsClearing); _externals.Cleared += new CollectionClear(OnCommandsCleared); _externals.Inserted += new CollectionChange(OnCommandInserted); _externals.Removed += new CollectionChange(OnCommandRemoved); _internals.Clearing += new CollectionClear(OnCommandsClearing); _internals.Cleared += new CollectionClear(OnCommandsCleared); _internals.Inserted += new CollectionChange(OnCommandInserted); _internals.Removed += new CollectionChange(OnCommandRemoved); // Need a timer so that when the mouse hovers we can show tooltips _hoverTimer = new Timer(); _hoverTimer.Tick += new EventHandler(OnMouseHoverTick); // Need a timer so that we can send updates to top level commands _updateTimer = new Timer(); _updateTimer.Tick += new EventHandler(OnUpdateTick); }
private void MakeStatePressed(CommandState commandDown, Point pt) { // Remove any tooltip KillTooltip(); // If current command is not the new one, then revert it RevertCurrentCommandState(commandDown); // Make this the new command _currentCmdState = commandDown; // Do we have a valid command to hot track? if (_currentCmdState != null) { // Switch to the hot tracking mode _currentCmdState.State = ItemState.Pressed; // Make sure it is repainted Invalidate(_currentCmdState.DrawRect); } }
private void MakeStateHotTrack(CommandState commandOver) { // Is there a change in state? if (commandOver != _currentCmdState) { // Make any existing command revert back to normal RevertCurrentCommandState(); // Make this the new command _currentCmdState = commandOver; // Do we have a valid command to hot track? if (_currentCmdState != null) { // Switch to the hot tracking mode _currentCmdState.State = ItemState.HotTrack; // Make sure it is repainted Invalidate(_currentCmdState.DrawRect); } } }
/// <summary> /// Returns the display rectangle of the provided command instance. /// </summary> /// <param name="c">Command instance.</param> /// <returns>Display rectangle if found; otherwise Rectangle.Empty.</returns> public Rectangle RectFromCommand(CommandBase c) { // Default the returned rectangle Rectangle ret = Rectangle.Empty; // Does the command exist in our command collection? if (Commands.Contains(c)) { // Find the index of the command int index = Commands.IndexOf(c); // Get the corresponding command state CommandState state = _states[index]; // Only interested if the command is currently displayed if (state.Displayed) { // Use the stored display rectangle ret = state.DrawRect; } } return(ret); }
private void OnMouseHoverTick(object sender, EventArgs e) { // Do not want timer to fire again _hoverTimer.Stop(); // Only interested if no button is being pressed if (Control.MouseButtons == MouseButtons.None) { // Only interested if mouse has not moved if (Control.MousePosition == _hoverPoint) { // Find any command the mouse is over CommandState commandOver = CommandStateFromPoint(PointToClient(Control.MousePosition), false); // Was a command found? if (commandOver != null) { // Only interesed if the tooltip has a interesting value if (commandOver.Command.Tooltip.Length > 0) { // Cache which command the popup is for _tooltipCmdState = commandOver; // Create a tooltip control using same style as our own _popupTooltip = new PopupTooltipSingle(this.Style); // Define string for display _popupTooltip.ToolText = commandOver.Command.Tooltip; // Make it visible but without taking the foucs _popupTooltip.ShowWithoutFocus(new Point(Control.MousePosition.X, Control.MousePosition.Y + 24)); } } } } }
/// <summary> /// Returns the index of the first occurrence of the given CommandState. /// </summary> /// <param name="value">The CommandState to locate.</param> /// <returns>Index of object; otherwise -1</returns> public int IndexOf(CommandState value) { // Find the 0 based index of the requested entry return(base.List.IndexOf(value)); }
/// <summary> /// Determines whether a CommandState is in the collection. /// </summary> /// <param name="value">The CommandState to locate in the collection.</param> /// <returns>true if item is found in the collection; otherwise, false.</returns> public bool Contains(CommandState value) { // Use base class to process actual collection operation return(base.List.Contains(value as object)); }
/// <summary> /// Inserts a CommandState instance into the collection at the specified location. /// </summary> /// <param name="index">The location in the collection where you want to add the CommandState.</param> /// <param name="value">The CommandState object to insert.</param> public void Insert(int index, CommandState value) { // Use base class to process actual collection operation base.List.Insert(index, value as object); }
/// <summary> /// Removes a CommandState from the collection. /// </summary> /// <param name="value">A CommandState to remove from the collection.</param> public void Remove(CommandState value) { // Use base class to process actual collection operation base.List.Remove(value as object); }
private void AdjustButtonSizes() { // Do we need to calculate the largest button size? if (_details.EqualButtonVert || _details.EqualButtonHorz) { // Cache count of commands int commandCount = _states.Count; // Cache largest values found so far Size largest = Size.Empty; // Check each command for those that are buttons for (int i = 0; i < commandCount; i++) { CommandState state = _states[i]; CommandBase command = state.Command; // Is this treated as a button? if (command is ICommandIsButton) { Size button = state.DrawRect.Size; // Remember the largest values found if (button.Width > largest.Width) { largest.Width = button.Width; } if (button.Height > largest.Height) { largest.Height = button.Height; } } } bool equalVert = _details.EqualButtonVert; bool equalHorz = _details.EqualButtonHorz; // If showing in vertical alignment then need to swap around if (_details.Direction == LayoutDirection.Vertical) { // Swap values around bool temp = equalVert; equalVert = equalHorz; equalHorz = equalVert; } // Now process each button again, this time applying largest values for (int i = 0; i < commandCount; i++) { CommandState state = _states[i]; CommandBase command = state.Command; // Is this treated as a button? if (command is ICommandIsButton) { if (equalVert) { if (equalHorz) { // Equal in both directions, just set to largest value state.DrawRect = new Rectangle(state.DrawRect.Location, largest); } else { // Update just the vertical value state.DrawRect = new Rectangle(state.DrawRect.Location, new Size(state.DrawRect.Width, largest.Height)); } } else { // Update just the horizontal value state.DrawRect = new Rectangle(state.DrawRect.Location, new Size(largest.Width, state.DrawRect.Height)); } } } } }