public void Resort() { try { lastSelected = SelectedItem as CustomTreeNode; } catch (IndexOutOfRangeException) { // Accessing ListBox.SelectedItem sometimes throws an IndexOutOfRangeException (See CA-24396) log.Warn("IndexOutOfRangeException in ListBox.SelectedItem"); lastSelected = null; } Nodes.Sort(); Items.Clear(); foreach (CustomTreeNode node in Nodes) { if (node.Level != -1 && node.ParentNode.Expanded) { Items.Add(node); } } SelectedItem = lastSelected; // I've yet to come across the above assignement working. If we fail to restore the selection, select something so the user can see focus feedback // (the color of the selected item is the only indication as to whether it is focused or not) // Iterating through and using CustomTreeNode.equals is useless here as it compares based on index, which I think is why the above call almost never works if (SelectedItem == null && lastSelected != null && Items.Count > 0) { SelectedItem = Items[0]; } }
/// <summary> /// Finds next/previous node in Items collection. /// </summary> /// <param name="currentNode">Node where the search for next/previous node will start.</param> /// <param name="searchForward">Determines direction of search (search for next or previous node).</param> /// <returns></returns> protected CustomTreeNode GetNextNode(CustomTreeNode currentNode, bool searchForward) { if (currentNode == null) { return(null); } int index = Items.IndexOf(currentNode); if (searchForward) { index++; if (index >= Items.Count) { index = -1; } } else { index--; } if (index < 0) { return(null); } return((CustomTreeNode)Items[index]); }
public void RemoveNode(CustomTreeNode node) { Nodes.Remove(node); if (!_inUpdate) { RecalculateWidth(); Resort(); Refresh(); } }
protected override void OnMouseUp(MouseEventArgs e) { bool anythingChanged = false; bool orderChanged = false; Point loc = this.PointToClient(MousePosition); int index = this.IndexFromPoint(loc); if (index < 0 || index > Items.Count) { return; } CustomTreeNode node = this.Items[index] as CustomTreeNode; if (node == null) { return; } int indent = node.Level * NodeIndent + (ShowRootLines ? NodeIndent : 2); if (node.ChildNodes.Count > 0 && loc.X < indent - (ItemHeight - 9) && loc.X > indent - ItemHeight && (ShowRootLines || node.Level > 0)) { node.Expanded = !node.Expanded; node.PreferredExpanded = node.Expanded; anythingChanged = true; orderChanged = true; } else if (ShowCheckboxes && !node.HideCheckbox && node.Enabled && loc.X > indent && loc.X < indent + ItemHeight) { if (node.State == CheckState.Unchecked || node.State == CheckState.Indeterminate) { node.State = CheckState.Checked; } else { node.State = CheckState.Unchecked; } anythingChanged = true; } if (orderChanged) { Resort(); } if (anythingChanged) { if (ItemCheckChanged != null) { ItemCheckChanged(node, new EventArgs()); } Refresh(); } base.OnMouseUp(e); }
private bool SelectNextEnabledNode(CustomTreeNode currentNode, bool searchForward) { CustomTreeNode nextEnabledNode = GetNextEnabledNode(currentNode, searchForward); if (nextEnabledNode != null) { SelectedItem = nextEnabledNode; return(true); } return(false); }
public void AddChildNode(CustomTreeNode parent, CustomTreeNode child) { parent.AddChild(child); Nodes.Add(child); if (!_inUpdate) { RecalculateWidth(); Resort(); Refresh(); } }
protected override void OnMouseDoubleClick(MouseEventArgs e) { base.OnMouseDoubleClick(e); bool anythingChanged = false; Point loc = this.PointToClient(MousePosition); int index = this.IndexFromPoint(loc); if (index < 0 || index > Items.Count) { return; } CustomTreeNode node = this.Items[index] as CustomTreeNode; if (node == null) { return; } int indent = node.Level * NodeIndent + (ShowRootLines ? NodeIndent : 2); if (node.ChildNodes.Count > 0 && loc.X < indent - (ItemHeight - 9) && loc.X > indent - ItemHeight && (ShowRootLines || node.Level > 0)) { return; } else if (ShowCheckboxes && !node.HideCheckbox && loc.X > indent && loc.X < indent + ItemHeight) { return; } else if (node.ChildNodes.Count > 0 && (node.Level > 0 || !_rootAlwaysExpanded)) { node.Expanded = !node.Expanded; node.PreferredExpanded = node.Expanded; anythingChanged = true; } if (anythingChanged) { Resort(); Refresh(); } if (DoubleClickOnRow != null) { DoubleClickOnRow(this, e); } }
protected override int SameLevelSortOrder(CustomTreeNode other) { if (Enabled && !other.Enabled) { return(-1); } else if (!Enabled && other.Enabled) { return(1); } else { return(base.SameLevelSortOrder(other)); } }
public void AddNode(CustomTreeNode node) { if (Nodes.Count == 0) { Nodes.Add(SecretNode); } SecretNode.AddChild(node); Nodes.Add(node); if (!_inUpdate) { RecalculateWidth(); Resort(); Refresh(); } }
/// <summary> /// Finds next/previous enabled node in Items collection. /// </summary> /// <param name="currentNode">Node where the search for next/previous enabled node will start.</param> /// <param name="searchForward">Determines direction of search (search for next or previous node).</param> /// <returns></returns> protected CustomTreeNode GetNextEnabledNode(CustomTreeNode currentNode, bool searchForward) { if (currentNode == null) { return(null); } CustomTreeNode nextNode = GetNextNode(currentNode, searchForward); if (nextNode == null) { return(null); } if (nextNode.Enabled) { return(nextNode); } return(GetNextEnabledNode(nextNode, searchForward)); }
protected override int SameLevelSortOrder(CustomTreeNode other) { if (Enabled && !other.Enabled) { return(-1); } else if (!Enabled && other.Enabled) { return(1); } else if (Enabled && other.Enabled && other is HostItem) { return(TheHost.CompareTo(((HostItem)other).TheHost)); } else { return(base.SameLevelSortOrder(other)); } }
protected override int SameLevelSortOrder(CustomTreeNode other) { SrPickerItem otherItem = other as SrPickerItem; if (otherItem == null) //shouldn't ever happen!!! { return(-1); } if (!otherItem.Enabled && Enabled) { return(-1); } if (otherItem.Enabled && !Enabled) { return(1); } return(base.SameLevelSortOrder(otherItem)); }
/// <summary> /// Tries to select the node if it is a host item. If it is a host item, but is disabled, selects its parent instead. /// </summary> /// <param name="item"></param> /// <param name="host"></param> /// <returns>True if successful</returns> private bool TryToSelectHost(CustomTreeNode item, Host host) { if (item is HostItem) { HostItem hostitem = item as HostItem; if (hostitem.TheHost.opaque_ref == host.opaque_ref) { if (hostitem.Enabled) { SelectedItem = hostitem; return(true); } else if (hostitem.ParentNode is PoolItem) { SelectConnection(host.Connection); return(true); } } } return(false); }
protected override int SameLevelSortOrder(CustomTreeNode other) { if (!(other is ConnectionWrapperWithMoreStuff)) { return(-1); } ConnectionWrapperWithMoreStuff other2 = other as ConnectionWrapperWithMoreStuff; int diff = (int)(this.reason) - (int)(other2.reason); if (diff < 0) { return(-1); } else if (diff > 0) { return(1); } else { return(StringUtility.NaturalCompare(this.ToString(), other2.ToString())); } }
protected override void OnSelectedIndexChanged(EventArgs e) { if (SelectedItem is CustomTreeNode) { CustomTreeNode item = SelectedItem as CustomTreeNode; if (!item.Enabled) { SelectedItem = lastSelected; } if (!AllowPoolSelect && item is PoolItem) { SelectedItem = lastSelected; } } lastSelected = SelectedItem as CustomTreeNode; base.OnSelectedIndexChanged(e); if (SelectedItemChanged != null) { SelectedItemChanged(null, new SelectedItemEventArgs((SelectedItem is PoolItem || SelectedItem is HostItem) && (SelectedItem as CustomTreeNode).Enabled)); } }
protected override void OnDrawItem(DrawItemEventArgs e) { base.OnDrawItem(e); if (Enabled) { using (SolidBrush backBrush = new SolidBrush(BackColor)) { e.Graphics.FillRectangle(backBrush, e.Bounds); } } else { e.Graphics.FillRectangle(SystemBrushes.Control, e.Bounds); } if (e.Index == -1 || Items.Count <= e.Index) { return; } CustomTreeNode node = this.Items[e.Index] as CustomTreeNode; if (node == null) { return; } //int indent = (node.Level + 1) * NodeIndent; int indent = node.Level * NodeIndent + (ShowRootLines ? NodeIndent : 2); int TextLength = Drawing.MeasureText(node.ToString(), e.Font).Width + 2; int TextLeft = indent + (ShowCheckboxes && !node.HideCheckbox ? ItemHeight : 0) + (ShowImages ? ItemHeight : 0); //CA-59618: add top margin to the items except the first one when rendering with //visual styles because in this case there is already one pixel of margin. int topMargin = Application.RenderWithVisualStyles && e.Index == 0 ? 0 : 1; if (Enabled && node.Selectable) { Color nodeBackColor = node.Enabled ? e.BackColor : (e.BackColor == BackColor ? BackColor : SystemColors.ControlLight); using (SolidBrush backBrush = new SolidBrush(nodeBackColor)) { e.Graphics.FillRectangle(backBrush, new Rectangle(e.Bounds.Left + TextLeft + 1, e.Bounds.Top + topMargin, TextLength - 4, e.Bounds.Height)); } } //draw expander if (node.ChildNodes.Count > 0 && (ShowRootLines || node.Level > 0)) { if (!node.Expanded) { if (Application.RenderWithVisualStyles) { plusRenderer.DrawBackground(e.Graphics, new Rectangle(e.Bounds.Left + indent - ItemHeight, e.Bounds.Top + 3 + topMargin, 9, 9)); } else { e.Graphics.DrawImage(Properties.Resources.tree_plus, new Rectangle(e.Bounds.Left + indent - ItemHeight, e.Bounds.Top + 3 + topMargin, 9, 9)); } } else { if (Application.RenderWithVisualStyles) { minusRenderer.DrawBackground(e.Graphics, new Rectangle(e.Bounds.Left + indent - ItemHeight, e.Bounds.Top + 3 + topMargin, 9, 9)); } else { e.Graphics.DrawImage(Properties.Resources.tree_minus, new Rectangle(e.Bounds.Left + indent - ItemHeight, e.Bounds.Top + 3 + topMargin, 9, 9)); } } } //draw checkboxes if (ShowCheckboxes && !node.HideCheckbox) { var checkedState = CheckBoxState.UncheckedDisabled; if (node.State == CheckState.Checked) { if (node.Enabled && Enabled) { checkedState = CheckBoxState.CheckedNormal; } else if (node.CheckedIfdisabled) { checkedState = CheckBoxState.CheckedDisabled; } } else if (node.State == CheckState.Indeterminate) { checkedState = node.Enabled && Enabled ? CheckBoxState.MixedNormal : CheckBoxState.MixedDisabled; } else if (node.State == CheckState.Unchecked) { checkedState = node.Enabled && Enabled ? CheckBoxState.UncheckedNormal : CheckBoxState.UncheckedDisabled; } CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(e.Bounds.Left + indent, e.Bounds.Top + 1 + topMargin), checkedState); indent += ItemHeight; } //draw images if (ShowImages && node.Image != null) { var rectangle = new Rectangle(e.Bounds.Left + indent, e.Bounds.Top + topMargin, node.Image.Width, node.Image.Height); if (node.Enabled && Enabled) { e.Graphics.DrawImage(node.Image, rectangle); } else { e.Graphics.DrawImage(node.Image, rectangle, 0, 0, node.Image.Width, node.Image.Height, GraphicsUnit.Pixel, Drawing.GreyScaleAttributes); } indent += ItemHeight; } //draw item's main text Color textColor = node.Enabled && Enabled ? (node.Selectable ? e.ForeColor : ForeColor) : SystemColors.GrayText; Drawing.DrawText(e.Graphics, node.ToString(), e.Font, new Point(e.Bounds.Left + indent, e.Bounds.Top + topMargin), textColor); indent += TextLength; //draw item's description if (ShowDescription) { Drawing.DrawText(e.Graphics, node.Description, _descriptionFont, new Point(e.Bounds.Left + indent, e.Bounds.Top + 1 + topMargin), SystemColors.GrayText); } }