private void AddCommentCollectionMenuOptions(ContextMenu newContextMenu, string title) { // Make the header in the menu for the comment-specific options MenuItem menuItem = new MenuItem(); menuItem.Header = title; newContextMenu.Items.Add(menuItem); // We're using this item as a label, so don't let the user click it menuItem.IsHitTestVisible = false; // Change the colors to signal to the user that it's a label menuItem.Background = new SolidColorBrush(Colors.LightGray); menuItem.Foreground = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100)); menuItem.FontWeight = FontWeights.Bold; menuItem = new MenuItem(); menuItem.Header = "Add new comment"; menuItem.Tag = m_canvas.SelectedElement; newContextMenu.Items.Add(menuItem); // Use an anonymous delegate to handle the click event menuItem.Click += delegate(object sender, RoutedEventArgs e) { MenuItem tempMI = sender as MenuItem; PFD.Streams.StreamControl stream = tempMI.Tag as PFD.Streams.StreamControl; ProcessUnitControl unit = tempMI.Tag as ProcessUnitControl; // Get a reference to the comment collection IList <StickyNote> comments; if (null != stream) { comments = stream.Stream.Comments; } else { comments = unit.ProcessUnit.Comments; } // Compute the location for the new comment MathCore.Vector location = StickyNoteControl.ComputeNewCommentNoteLocation( m_canvas, tempMI.Tag, 100.0, 100.0); // Create the new sticky note and add it to the workspace. Event handlers will update // the UI appropriately StickyNote sn = new StickyNote() { Width = 100.0, Height = 100.0, LocationX = location.X, LocationY = location.Y }; comments.Add(sn); // Add an undo that will remove the comment m_workspace.AddUndo(new UndoRedoCollection("Undo creation of comment", new RemoveComment(comments, comments.IndexOf(sn)))); // Make sure to remove the popup menu from the canvas m_canvas.Children.Remove(m_contextMenu); m_contextMenu = null; // Flip back to the default state for the canvas (null) m_canvas.CurrentState = null; }; string objName = "selected object"; if (m_canvas.SelectedElement is ProcessUnitControl) { objName = (m_canvas.SelectedElement as ProcessUnitControl).ProcessUnit.Label; } else if (m_canvas.SelectedElement is ChemProV.PFD.Streams.StreamControl) { objName = "selected stream"; } // Add a new menu item to hide all comments menuItem = new MenuItem(); menuItem.Header = "Hide all comments for " + objName; menuItem.Tag = m_canvas.SelectedElement; newContextMenu.Items.Add(menuItem); menuItem.Click += delegate(object sender, RoutedEventArgs e) { MenuItem tempMI = sender as MenuItem; (tempMI.Tag as UI.ICommentControlManager).HideAllComments(); // Make sure to remove the popup menu from the canvas m_canvas.Children.Remove(m_contextMenu); m_contextMenu = null; // Flip back to the default state for the canvas (null) m_canvas.CurrentState = null; }; // Add one to show all comments too menuItem = new MenuItem(); menuItem.Header = "Show all comments for " + objName; menuItem.Tag = m_canvas.SelectedElement; newContextMenu.Items.Add(menuItem); menuItem.Click += delegate(object sender, RoutedEventArgs e) { MenuItem tempMI = sender as MenuItem; (tempMI.Tag as UI.ICommentControlManager).ShowAllComments(); // Make sure to remove the popup menu from the canvas m_canvas.Children.Remove(m_contextMenu); m_contextMenu = null; // Flip back to the default state for the canvas (null) m_canvas.CurrentState = null; }; }
public void MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // If the placement icon is null then we don't do anything if (null == m_placementIcon) { return; } // Remove the placement icon from the canvas m_canvas.RemoveChild(m_placementIcon); m_placementIcon = null; // Make sure we don't leave anything highlighted UnhighlightHover(); Point mousePt = e.GetPosition(m_canvas); // Get a reference to the workspace Workspace ws = m_canvas.GetWorkspace(); // There are two possibilities for a mouse-down. The first is that the mouse isn't over // any object that can have comments created for it, in which case we create a free- // floating comment. The second is that there is and we need to create an anchored comment. UIElement uie = m_canvas.GetChildAtIncludeStreams(mousePt); if (uie is ProcessUnitControl || uie is PFD.Streams.StreamControl) { // All we need to do is add a comment to the appropriate collection in the workspace, // but we need to compute a smart location for it. There's a static method in the // sticky note control that does this for is. MathCore.Vector pos = StickyNoteControl.ComputeNewCommentNoteLocation( m_canvas, uie, 100.0, 100.0); // Create the new sticky note and add it to the workspace. Event handlers will update // the UI appropriately. StickyNote sn = new StickyNote() { Width = 100.0, Height = 100.0, LocationX = pos.X, LocationY = pos.Y }; if (uie is ProcessUnitControl) { (uie as ProcessUnitControl).ProcessUnit.Comments.Add(sn); } else { (uie as PFD.Streams.StreamControl).Stream.Comments.Add(sn); } // Create an undo m_canvas.GetWorkspace().AddUndo(new UndoRedoCollection( "Undo creation of anchored comment", new Logic.Undos.RemoveStickyNote(sn))); // Tell the control palette to switch back to select mode. It will also set the drawing // canvas's state to null m_creator.SwitchToSelect(); } else { // This means we need to create a free-floating sticky note. StickyNote note = new StickyNote(); note.Height = note.Width = 100.0; note.LocationX = mousePt.X; note.LocationY = mousePt.Y; // Add it to the workspace. Event subscribers will update the UI automatically. m_canvas.GetWorkspace().StickyNotes.Add(note); // Add an undo that will remove the sticky note m_canvas.GetWorkspace().AddUndo(new UndoRedoCollection( "Undo creation of free-floating comment", new Logic.Undos.RemoveStickyNote(note))); // Tell the control palette to switch back to select mode. It will also set the drawing // canvas's state to null m_creator.SwitchToSelect(); } }